Skip to content

Instantly share code, notes, and snippets.

@daparic
Last active April 18, 2025 03:37
Show Gist options
  • Save daparic/2f3563c195eda31cac46bd058f643c03 to your computer and use it in GitHub Desktop.
Save daparic/2f3563c195eda31cac46bd058f643c03 to your computer and use it in GitHub Desktop.

STM32 firmware command line tools (please adjust 0x200000):

STM32_Programmer_CLI.exe -c port=SWD --upload 0x8000000 0x200000 firmware.bin # extract from board
STM32_Programmer_CLI.exe -c port=SWD -w firmware.bin 0x08000000 -rst # flash to board
@daparic
Copy link
Author

daparic commented Apr 18, 2025

Extracting the firmware from a NUCLEO-G071RB board and saving to firmware.bin file:

$ STM32_Programmer_CLI.exe -c port=SWD --upload 0x8000000 0x20000 firmware.bin
      -------------------------------------------------------------------
                       STM32CubeProgrammer v2.17.0
      -------------------------------------------------------------------

ST-LINK SN  : 066CFF3031454D3043094425
ST-LINK FW  : V2J46M31
Board       : NUCLEO-G071RB
Voltage     : 3.22V
SWD freq    : 4000 KHz
Connect mode: Normal
Reset mode  : Software reset
Device ID   : 0x460
Revision ID : Rev B
Device name : STM32G07x/STM32G08x
Flash size  : 128 KBytes
Device type : MCU
Device CPU  : Cortex-M0+
BL Version  : 0xB2
Debug in Low Power mode enabled


UPLOADING ...
  File          : firmware.bin
  Size          : 131072 Bytes
  Address:      : 0x8000000
Read progress:
██████████████████████████████████████████████████ 100%
Data read successfully
Time elapsed during read operation is: 00:00:00.963

LAPTOP-1QQN8248 ~/bin
$ ls -l firmware.bin
-rw-r--r-- 1 dx 197121 131072 Apr 18 09:47 firmware.bin

@daparic
Copy link
Author

daparic commented Apr 18, 2025

Flashing an updated demo2.elf firmware to a NUCLEO-G071RB board:

$ STM32_Programmer_CLI.exe -c port=SWD -w demo2.elf 0x08000000 -rst
      -------------------------------------------------------------------
                       STM32CubeProgrammer v2.17.0
      -------------------------------------------------------------------

ST-LINK SN  : 066CFF3031454D3043094425
ST-LINK FW  : V2J46M31
Board       : NUCLEO-G071RB
Voltage     : 3.22V
SWD freq    : 4000 KHz
Connect mode: Normal
Reset mode  : Software reset
Device ID   : 0x460
Revision ID : Rev B
Device name : STM32G07x/STM32G08x
Flash size  : 128 KBytes
Device type : MCU
Device CPU  : Cortex-M0+
BL Version  : 0xB2
Debug in Low Power mode enabled



Warning: Write address is ignored for hex, Srec, s19 and elf files


Memory Programming ...
Opening and parsing file: demo2.elf
  File          : demo2.elf
  Size          : 10.77 KB
  Address       : 0x08000000


Erasing memory corresponding to segment 0:
Erasing internal memory sectors [0 5]
Download in Progress:
██████████████████████████████████████████████████ 100%

File download complete
Time elapsed during download operation: 00:00:00.435

MCU Reset

Software reset is performed

NOTE: The extracted firmware.bin and the updated demo2.elf (from an IDE build) are in different format. The openocd, while usable, is not as flexible, so I find it best to use STM32_Programmer_CLI.exe. For example, we can extract the firmware using either openocd or STM32_Programmer_CLI command:

STM32_Programmer_CLI.exe -c port=SWD --upload 0x8000000 0x20000 firmware1.bin
openocd -f interface/stlink-v2.cfg -f target/stm32g0x.cfg -c init -c "reset halt" -c "flash read_bank 0 firmware2.bin 0 0x20000" -c "reset" -c shutdown

$ md5sum firmware1.bin firmware2.bin
bd345b8cf8334ff7ffaf4993922d8012 *firmware1.bin
bd345b8cf8334ff7ffaf4993922d8012 *firmware2.bin

Furthermore, you can flush to the board using:

openocd -f interface/stlink-v2.cfg -f target/stm32g0x.cfg -c init -c "reset halt" -c "flash write_image erase firmware2.bin 0x08000000" -c "reset" -c shutdown

But, YOU CANNOT flush demo2.elf using openocd. Hence, this is why I prefer STM32_Programmer_CLI because it can flush to the board both binary formats.

@daparic
Copy link
Author

daparic commented Apr 18, 2025

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment