1
\$\begingroup\$

So, I am making a USB MSC firmware updater bootloader for STM32H750. The problem is that this microcontroller can only erase its flash memory entirely to rewrite it later, so the bootloader and all currently running code will be erased.

My idea to solve this problem is to put everything into RAM from flash and run the code from there. So, I've succesfully copied all code from FLASH to RAM, modified the vector table, and ran the code from RAM. The problem then was that all functions were actually running from flash, because the jump addresses weren't modified.

I've recompiled the code then as position-independent (-fPIC flag). Functions then started running from the RAM, but global variables don't work now - every usage of them puts me in HardFault. I think it happens because of the memory offset between flash and RAM: the code tries to use the same offset while the code is in ram, putting all global variables outside the memory space.

  1. Am I right in my assumption of what happens?
  2. If I am - how do I make jump addresses position independent, but variable addresses - still absolute?
  3. Perhaps, an easier solution is to make all functions "RAM Functions" - they work exactly as I need - stored initially in FLASH, then copied to RAM and afterwards they run from there. But how do I make everything .RamFunc without putting proper attribute in front of every single function manually?

I am using stm32cubeide.

\$\endgroup\$
3
  • \$\begingroup\$ Use the linker's control source file. It should have all of what you need. It can copy segments, link them as if they are located in different places, etc. Modern linkers are very powerful the days. Read the manual. \$\endgroup\$ Commented Aug 4 at 9:16
  • 1
    \$\begingroup\$ Why the fast track to a brick? The glaring fault in this is that an interrupted update bricks the device. These devices are really intended to be used with external flash. \$\endgroup\$ Commented Aug 4 at 12:22
  • \$\begingroup\$ Solved thanks to the second answer to this question: stackoverflow.com/questions/69255181/… \$\endgroup\$
    – sx107
    Commented Aug 5 at 6:00

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.