11@ echo off
2+ setlocal EnableDelayedExpansion
23
34echo =========================================================
45echo PortaPack Mayhem - Device Flasher
@@ -57,33 +58,17 @@ exit /b
5758REM ── Action 1: Flash Mayhem ─────────────────────────────────
5859:flash_mayhem
5960echo .
60- if " %DEVICE_CHOICE% " == " 1" set FIRMWARE = firmware\firmware_hackrf.bin
61- if " %DEVICE_CHOICE% " == " 2" set FIRMWARE = firmware\firmware_portarf.bin
62- if " %DEVICE_CHOICE% " == " 3" set FIRMWARE = firmware\firmware_hpro.bin
63-
61+ call :set_firmware
6462if " %DEVICE_CHOICE% " == " 1" (
6563 echo If your device has a PortaPack attached, switch it to HackRF mode first by
6664 echo selecting the " HackRF" option from the main menu.
6765 echo .
6866)
6967echo Firmware: %FIRMWARE%
7068echo .
71-
72- if not exist %FIRMWARE% (
73- echo ERROR: The firmware file " %FIRMWARE% " was not found.
74- echo Please ensure you have downloaded the latest release from:
75- echo https://release.hackrf.app/
76- echo .
77- pause
78- exit /b
79- )
80-
81- echo .
82- " utils/hackrf_spiflash.exe" -R -w %FIRMWARE%
83- echo .
84- echo If your device does not boot after flashing, see the troubleshooting wiki:
85- echo https://github.com/portapack-mayhem/mayhem-firmware/wiki/Wont-boot
86- echo .
69+ call :check_file_exists " %FIRMWARE% " " firmware file" || (pause & exit /b)
70+ call :do_flash
71+ if " !FLASH_OK! " == " 1" call :print_success
8772pause
8873exit /b
8974
@@ -101,24 +86,10 @@ echo 1. Hold down both the DFU and RESET buttons.
10186echo 2. Release the RESET button first (the one closest to the edge).
10287echo 3. Then release the DFU button.
10388echo .
104-
105- if " %DEVICE_CHOICE% " == " 3" (
106- set DFU_FILE = firmware\hackrf_hpro_usb.dfu
107- ) else (
108- set DFU_FILE = firmware\hackrf_usb.dfu
109- )
110-
111- if not exist " %DFU_FILE% " (
112- echo ERROR: " %DFU_FILE% " was not found.
113- echo Please ensure you have downloaded the latest release from:
114- echo https://release.hackrf.app/
115- echo .
116- pause
117- exit /b
118- )
119-
89+ call :set_dfu_file
90+ call :check_file_exists " %DFU_FILE% " " DFU file" || (pause & exit /b)
12091echo .
121- " utils/dfu-util-static.exe " --device 1fc9:000c --download " %DFU_FILE% "
92+ call : do_dfu
12293echo .
12394pause
12495exit /b
@@ -136,55 +107,80 @@ echo 1. Hold down both the DFU and RESET buttons.
136107echo 2. Release the RESET button first (the one closest to the edge).
137108echo 3. Then release the DFU button.
138109echo .
139-
140- if " %DEVICE_CHOICE% " == " 3" (
141- set DFU_FILE = firmware\hackrf_hpro_usb.dfu
142- ) else (
143- set DFU_FILE = firmware\hackrf_usb.dfu
144- )
145-
146- if not exist " %DFU_FILE% " (
147- echo ERROR: " %DFU_FILE% " was not found.
148- echo Please ensure you have downloaded the latest release from:
149- echo https://release.hackrf.app/
150- echo .
151- pause
152- exit /b
153- )
154-
155- if " %DEVICE_CHOICE% " == " 1" set FIRMWARE = firmware\firmware_hackrf.bin
156- if " %DEVICE_CHOICE% " == " 2" set FIRMWARE = firmware\firmware_portarf.bin
157- if " %DEVICE_CHOICE% " == " 3" set FIRMWARE = firmware\firmware_hpro.bin
158-
159- if not exist " %FIRMWARE% " (
160- echo ERROR: The firmware file " %FIRMWARE% " was not found.
161- echo Please ensure you have downloaded the latest release from:
162- echo https://release.hackrf.app/
163- echo .
164- pause
165- exit /b
166- )
110+ call :set_dfu_file
111+ call :check_file_exists " %DFU_FILE% " " DFU file" || (pause & exit /b)
112+ call :set_firmware
113+ call :check_file_exists " %FIRMWARE% " " firmware file" || (pause & exit /b)
167114
168115echo Step 1 of 2: Loading HackRF firmware via DFU...
169116echo .
170- " utils/dfu-util-static.exe " --device 1fc9:000c --download " %DFU_FILE% "
117+ call : do_dfu
171118
172119echo .
173- echo DFU complete. Waiting 5 seconds for device to re-enumerate ...
120+ echo DFU complete. Waiting 5 seconds for device to reconnect ...
174121timeout /t 5 /nobreak > nul
175122
176123echo .
177124echo Step 2 of 2: Flashing Mayhem firmware (%FIRMWARE% )...
178125echo .
179- " utils/hackrf_spiflash.exe" -R -w " %FIRMWARE% "
180- echo .
181- echo If your device does not boot after flashing, see the troubleshooting wiki:
182- echo https://github.com/portapack-mayhem/mayhem-firmware/wiki/Wont-boot
183- echo .
126+ call :do_flash
127+ if " !FLASH_OK! " == " 1" call :print_success
184128pause
185129exit /b
186130
187131
132+ REM ── Serial fallback: boot device into HackRF mode via serial ──
133+ :serial_fallback
134+ set SERIAL_OK = 0
135+ set COM_PORT =
136+ echo .
137+
138+ REM Enumerate available COM ports into a temp file
139+ powershell -NoProfile -Command " [System.IO.Ports.SerialPort]::GetPortNames() | Sort-Object | ForEach-Object { $_ }" > " %TEMP% \mayhem_comports.txt" 2 > nul
140+
141+ REM Count and index ports
142+ set PORT_COUNT = 0
143+ for /f " usebackq tokens=*" %%P in (" %TEMP% \mayhem_comports.txt" ) do (
144+ set /a PORT_COUNT += 1
145+ set PORT_!PORT_COUNT! = %%P
146+ )
147+
148+ if !PORT_COUNT! equ 0 (
149+ goto :eof
150+ )
151+
152+ if !PORT_COUNT! equ 1 (
153+ set COM_PORT = !PORT_1!
154+ ) else (
155+ echo Multiple COM ports detected. Select the one your device is on:
156+ echo .
157+ for /l %%I in (1,1,!PORT_COUNT! ) do (
158+ echo %%I . !PORT_%%I !
159+ )
160+ echo .
161+ set /p PORT_CHOICE = " Enter your choice (1-!PORT_COUNT! ): "
162+ set COM_PORT =
163+ for /l %%I in (1,1,!PORT_COUNT! ) do (
164+ if " !PORT_CHOICE! " == " %%I " set COM_PORT = !PORT_%%I !
165+ )
166+ if not defined COM_PORT (
167+ echo Invalid selection.
168+ goto :eof
169+ )
170+ )
171+
172+ echo Connecting to !COM_PORT! ...
173+ powershell -NoProfile -Command " $p = New-Object System.IO.Ports.SerialPort('!COM_PORT! ', 115200, [System.IO.Ports.Parity]::None, 8, [System.IO.Ports.StopBits]::One); try { $p.Open(); $p.Write('hackrf' + [char]13 + [char]10); Start-Sleep -Milliseconds 500 } catch { Write-Host ('Serial error: ' + $_.Exception.Message); exit 1 }; try { $p.Close() } catch {}; exit 0"
174+ if %ERRORLEVEL% equ 0 (
175+ set SERIAL_OK = 1
176+ echo Switching to HackRF mode, waiting for device...
177+ timeout /t 7 /nobreak > nul
178+ ) else (
179+ echo Could not open !COM_PORT! . Make sure the port is not in use.
180+ )
181+ goto :eof
182+
183+
188184REM ── Action 4: Flash factory HackRF firmware ────────────────
189185:flash_factory
190186echo .
@@ -200,17 +196,115 @@ if "%DEVICE_CHOICE%"=="3" (
200196 set FACTORY_BIN = firmware\hackrf_usb.bin
201197)
202198
203- if not exist " %FACTORY_BIN% " (
204- echo ERROR: " %FACTORY_BIN% " was not found.
199+ call :check_file_exists " %FACTORY_BIN% " " factory firmware" || (pause & exit /b)
200+ echo .
201+ " utils/hackrf_spiflash.exe" -R -w " %FACTORY_BIN% "
202+ echo .
203+ pause
204+ exit /b
205+
206+
207+ REM ── Helper: Set FIRMWARE based on DEVICE_CHOICE ─────────────
208+ :set_firmware
209+ if " %DEVICE_CHOICE% " == " 1" set FIRMWARE = firmware\firmware_hackrf.bin
210+ if " %DEVICE_CHOICE% " == " 2" set FIRMWARE = firmware\firmware_portarf.bin
211+ if " %DEVICE_CHOICE% " == " 3" set FIRMWARE = firmware\firmware_hpro.bin
212+ goto :eof
213+
214+
215+ REM ── Helper: Set DFU_FILE based on DEVICE_CHOICE ─────────────
216+ :set_dfu_file
217+ if " %DEVICE_CHOICE% " == " 3" (
218+ set DFU_FILE = firmware\hackrf_hpro_usb.dfu
219+ ) else (
220+ set DFU_FILE = firmware\hackrf_usb.dfu
221+ )
222+ goto :eof
223+
224+
225+ REM ── Helper: Check a required file exists ─────────────────────
226+ REM %1 = quoted file path %2 = human-readable label
227+ :check_file_exists
228+ if not exist %1 (
229+ echo ERROR: The %~2 file %1 was not found.
205230 echo Please ensure you have downloaded the latest release from:
206231 echo https://release.hackrf.app/
207232 echo .
208- pause
209- exit /b
233+ exit /b 1
234+ )
235+ exit /b 0
236+
237+
238+ REM ── Helper: Flash FIRMWARE with serial fallback ───────────────
239+ :do_flash
240+ set FLASH_OK = 0
241+ " utils/hackrf_spiflash.exe" -R -w " %FIRMWARE% "
242+ if %ERRORLEVEL% equ 0 (
243+ set FLASH_OK = 1
244+ goto :eof
245+ )
246+ call :serial_fallback
247+ if " !SERIAL_OK! " == " 1" (
248+ echo .
249+ echo Device should now be in HackRF mode. Retrying flash...
250+ echo .
251+ " utils/hackrf_spiflash.exe" -R -w " %FIRMWARE% "
252+ if !ERRORLEVEL! equ 0 (
253+ set FLASH_OK = 1
254+ ) else (
255+ echo .
256+ call :print_error
257+ )
258+ ) else (
259+ echo .
260+ call :print_error
210261)
262+ goto :eof
263+
211264
265+ REM ── Helper: Run DFU flash ────────────────────────────────────
266+ :do_dfu
267+ " utils/dfu-util-static.exe" --device 1fc9:000c --download " %DFU_FILE% "
268+ goto :eof
269+
270+
271+ REM ── Helper: Print error banner ──────────────────────────────
272+ :print_error
212273echo .
213- " utils/hackrf_spiflash.exe" -R -w " %FACTORY_BIN% "
274+ echo $$$$$$\
275+ echo $$ __$$\
276+ echo $$ / $$ ^ | $$$$$$\ $$$$$$\ $$$$$$$\
277+ echo $$ ^ | $$ ^ |$$ __$$\ $$ __$$\ $$ _____^ |
278+ echo $$ ^ | $$ ^ |$$ / $$ ^ |$$ / $$ ^ |\$$$$$$\
279+ echo $$ ^ | $$ ^ |$$ ^ | $$ ^ |$$ ^ | $$ ^ | \____$$\
280+ echo $$$$$$ ^ |\$$$$$$ ^ |$$$$$$$ ^ |$$$$$$$ ^ |
281+ echo \______/ \______/ $$ ____/ \_______/
282+ echo $$ ^ |
283+ echo $$ ^ |
284+ echo \__^ |
214285echo .
215- pause
216- exit /b
286+ echo ERROR: Device not found. Make sure your device is connected,
287+ echo powered on, and in HackRF mode, then try again.
288+ echo .
289+ goto :eof
290+
291+
292+ REM ── Helper: Print success banner ─────────────────────────────
293+ :print_success
294+ echo .
295+ echo ______ __ __ ______ ______ ________ ______ ______
296+ echo / \ ^ | \ ^ | \ / \ / \ ^ | \ / \ / \
297+ echo ^ | $$$$$$\^ | $$ ^ | $$^ | $$$$$$\^ | $$$$$$\^ | $$$$$$$$^ | $$$$$$\^ | $$$$$$\
298+ echo ^ | $$___\$$^ | $$ ^ | $$^ | $$ \$$^ | $$ \$$^ | $$__ ^ | $$___\$$^ | $$___\$$
299+ echo \$$ \ ^ | $$ ^ | $$^ | $$ ^ | $$ ^ | $$ \ \$$ \ \$$ \
300+ echo _\$$$$$$\^ | $$ ^ | $$^ | $$ __ ^ | $$ __ ^ | $$$$$ _\$$$$$$\ _\$$$$$$\
301+ echo ^ | \__^ | $$^ | $$__/ $$^ | $$__/ \^ | $$__/ \^ | $$_____ ^ | \__^ | $$^ | \__^ | $$
302+ echo \$$ $$ \$$ $$ \$$ $$ \$$ $$^ | $$ \ \$$ $$ \$$ $$
303+ echo \$$$$$$ \$$$$$$ \$$$$$$ \$$$$$$ \$$$$$$$$ \$$$$$$ \$$$$$$
304+ echo .
305+ echo Firmware flashed successfully. Your device is rebooting...
306+ echo .
307+ echo If your device does not boot after flashing, see the troubleshooting wiki:
308+ echo https://github.com/portapack-mayhem/mayhem-firmware/wiki/Wont-boot
309+ echo .
310+ goto :eof
0 commit comments