Search code examples
gdbeclipse-cdtstm32

Eclipse CDT & STM32: force predefined program memory


I have an uncommon but in my eyes reasonable use case: I have to build two STM32 firmware images: a boot loader and an application (by using the latest Eclipse CDT based IDE from ST Microelectronics, called: "STM32CubeIDE").

Because my constraints are mostly low power consumption and not security, therefore I have only the requirement for data integrity for the DFU (Device Firmware Upgrade) scenario and for this I implemented a CRC32 check over the complete FW images. The tricky part is, that the firmware itself contains its actually size within a C-struct at a fixed offset address 0x200 in the code memory (the benefit for this design is, that not the complete code memory has to be transmitted, but the FW is always protected by the CRC32):

The layout of a firmware is something like this:

<ISR Table> <FW-Header@FixedAddress0x200> <RestFWCode> " + CRC32
  1. FW Header contains FW size
  2. The complete FW size which is used by the booloader to flash the application is the stored FW size (see 1.) + 4 byte of the appended CRC32

For my implementation I need to replace a memory area within the "FW Header" area with the actual FW size (which is only available after the build process).

For this I made a python script which patches the binary "*.bin" file, but it seems, that Eclipse/GDB uses for debugging the ELF-file, which looks for me much more complicated for a custom patch compared to the binary image, since I found no easy way to do this (to replace the actual FW size and append the 4 bytes of the CRC32).

Therefore, I thought the easiest way would be to patch the code memory right after the firmware got loader from the debugger. I tested successfully a command-line tool from ST which can manipulate arbitrary memory even in the code memory (my code memory starts at 0x08000000 + application at offset 0x4000 and FW header at öffset 0x200 -> 0x08004200):

ST-LINK_CLI.exe -c SWD -w32 0x08004200 0xAABBCCDD

(see: https://www.st.com/resource/en/user_manual/cd00262073.pdf)

My Problem is, I don't know how to initiate this simple EXE call right before the debugger got attached to the MCU... I tried the "Debug Configuration"-> "Startup" -> "Run Commands", but without success...

Does anybody know a way how to achieve this?


Solution

  • Running a program before starting a debug session can be done using Eclipse's "Launch group" located under Debug Configurations, e.g. top menu -> Run -> Debug Configurations.

    enter image description here

    However before doing that you should go to project properties -> Builders and add your program invocation there - path to the executable plus its arguments. Make sure it's NOT checked so that it doesn't run when you build your project. Then you can go to the Launch Groups described above and create a group that contains the program you've defined in the project Builders section, then after that your regular debug session which you should already have available on the list.