OpenOCD scripts for read STM32 firmware binary
OpenOCD (Open On-Chip Debugger) is open-source software that interfaces with a hardware debugger's JTAG port. OpenOCD provides debugging and in-system programming for embedded target devices. OpenOCD provides the ability to flash NAND and NOR FLASH memory devices that are attached to the processor on the target system. Flash programming is supported for external CFI compatible flashes (Intel and AMD/Spansion command set) and several internal flashes (LPC2000, AT91SAM7, STR7x, STR9x, LM3 and STM32x).
OpenOCD was originally developed by Dominic Rath at the University of Applied Sciences Augsburg. The OpenOCD source code is now available through the GNU General Public License (GPL).
Under ubuntu 20.04 just install with this:
sudo apt-get install openocd
Under Arch/Manjaro:
sudo pacman -S openocd
Here is the locaiton prebuild binary can be downloaded from: https://gnutoolchains.com/arm-eabi/openocd/
For STM32f103c8 with 64Kbytes Flash. Target is target/stm32f1x.cfg
. Size to read is 0x10000
. Read firmware as firmwareF1.bin
.
openocd -f interface/stlink-v2.cfg -f target/stm32f1x.cfg -c init -c "reset halt" -c "flash read_bank 0 firmwareF1.bin 0 0x10000" -c "reset" -c shutdown
For STM32f407vg with 1Mbytes Flash. Target is target/stm32f4x.cfg
. Size to read is 0x100000
. Read firmware as firmwareF4.bin
.
openocd -f interface/stlink-v2.cfg -f target/stm32f4x.cfg -c init -c "reset halt" -c "flash read_bank 0 firmwareF4.bin 0 0x100000" -c "reset" -c shutdown
For STM32f103c8 with 64Kbytes Flash. Target is target/stm32f1x.cfg
. Write firmware firmwareF1.bin
to flash.
openocd -f interface/stlink-v2.cfg -f target/stm32f1x.cfg -c init -c "reset halt" -c "flash write_image erase firmwareF1.bin 0x08000000" -c "reset" -c shutdown
For STM32f407vg with 1Mbytes Flash. Target is target/stm32f4x.cfg
. Write firmware firmwareF4.bin
to flash.
openocd -f interface/stlink-v2.cfg -f target/stm32f4x.cfg -c init -c "reset halt" -c "flash write_image erase firmwareF4.bin 0x08000000" -c "reset" -c shutdown
Uploading a new firmware
The original board was not detected by SO as show below
Now, trying to upload a new firmware (after the backup is done!)
Reading the firmware
Comparing hashes
Both are different! Why? But now it is recognized!
By pushing the old firmware file (the original one that blinks the board LED), the board is not identified by SO but returns to blink the board LED as before (original application delivered with the board).