Skip to content

Commit 0645f18

Browse files
authored
Flashing: overhaul mayhem_flasher.bat with serial fallback and improved UX (portapack-mayhem#3074)
* Refactor mayhem_flasher.bat to streamline firmware flashing process and add serial fallback for device connection * Improve COM port selection logic in mayhem_flasher.bat for better user input handling * Enhance error handling in firmware flashing process for improved user feedback
1 parent da0a5c3 commit 0645f18

1 file changed

Lines changed: 173 additions & 79 deletions

File tree

flashing/mayhem_flasher.bat

Lines changed: 173 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
@echo off
2+
setlocal EnableDelayedExpansion
23

34
echo =========================================================
45
echo PortaPack Mayhem - Device Flasher
@@ -57,33 +58,17 @@ exit /b
5758
REM ── Action 1: Flash Mayhem ─────────────────────────────────
5859
:flash_mayhem
5960
echo.
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
6462
if "%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
)
6967
echo Firmware: %FIRMWARE%
7068
echo.
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
8772
pause
8873
exit /b
8974

@@ -101,24 +86,10 @@ echo 1. Hold down both the DFU and RESET buttons.
10186
echo 2. Release the RESET button first (the one closest to the edge).
10287
echo 3. Then release the DFU button.
10388
echo.
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)
12091
echo.
121-
"utils/dfu-util-static.exe" --device 1fc9:000c --download "%DFU_FILE%"
92+
call :do_dfu
12293
echo.
12394
pause
12495
exit /b
@@ -136,55 +107,80 @@ echo 1. Hold down both the DFU and RESET buttons.
136107
echo 2. Release the RESET button first (the one closest to the edge).
137108
echo 3. Then release the DFU button.
138109
echo.
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

168115
echo Step 1 of 2: Loading HackRF firmware via DFU...
169116
echo.
170-
"utils/dfu-util-static.exe" --device 1fc9:000c --download "%DFU_FILE%"
117+
call :do_dfu
171118

172119
echo.
173-
echo DFU complete. Waiting 5 seconds for device to re-enumerate...
120+
echo DFU complete. Waiting 5 seconds for device to reconnect...
174121
timeout /t 5 /nobreak >nul
175122

176123
echo.
177124
echo Step 2 of 2: Flashing Mayhem firmware (%FIRMWARE%)...
178125
echo.
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
184128
pause
185129
exit /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+
188184
REM ── Action 4: Flash factory HackRF firmware ────────────────
189185
:flash_factory
190186
echo.
@@ -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
212273
echo.
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 \__^|
214285
echo.
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

Comments
 (0)