cancel
Showing results for 
Search instead for 
Did you mean: 

How to erase running code of STM32?

ali ergin gursoy
Associate II
Posted on August 17, 2016 at 08:45

Hi,

Assume there is a button for emergency on the hardware, so the program which is currently running on ST will erase its own code when someone pressed the emergency button more than 10seconds.

How could you implement it? Could anybody lead me how to do it?

Regards
7 REPLIES 7
troy1818
Senior
Posted on August 17, 2016 at 10:31

Hi,

I assume that you just want to remove the code and thereby making the device unusable.

This is very possible and should not be hard. The hard part is when you need to get things working, not the other way around 🙂

I would put some erase procedure either in ram when device is started or in flash somewhere and just simply call this code to erase the flash section containing the program code.

If you do not need to erase everything you could render the device useless by simply writing a couple of zeros to the startup address (for stm32 usually 0x80000000). For this option you do not need dedicated code to erase flash. Just write the zeros and do soft reset (NVIC).

ali ergin gursoy
Associate II
Posted on August 17, 2016 at 10:46

Hi,

You are right, I want to make the device unsable, however, this is not the only purpose also want to prevent downloading the code by anybody, so need to write 0xFF or zeros to entire flash memory of device.

The thing that I do not get is erasing from the beggining of flash while running the code. How could it affect the running code? Does not it break if I start to write zeros from the beggining?

Thanks 
troy1818
Senior
Posted on August 17, 2016 at 11:01

If you want to erase everything, including the code that does the erase, I would suggest jumping to an erase function in RAM (I have never done this so cannot give any guarantee that it will work or how difficult it is). Perhaps it is also possible to jump to the STM32 default bootloader and call some procedure there. This I do not know.

adam23
Associate II
Posted on August 17, 2016 at 15:16

Flash is erased by blocks, the block is set to 0xFF. You do not need to overwrite it byte by byte. Use spare flash block to store ''erase routine'' (functions are part of STM libs), jump there, disable all interrupts and erase all blocks with code except this one, then stay in infinite loop. You can place code at certain address using some pragma or SCT (scatter) file.

If you do not have a spare flash block, than use RAM, but this needs some more fiddling - otherwise you end up erasing your routine and the CPU will jump to hard fault or something leaving the rest not erased.

Osto
Senior
Posted on August 19, 2016 at 16:19

Hi,

You can find the flash erase example code in ST documentation. Search for IAP (in application programming) and you will find code for programming by Serial, I2C, USB, and Ethernet. All of them erase first the flash then programm the flash. You can use the Erase part for your job.

After erasing dont forget to go in infinte loop or enable watch dog for generating a rest to be back in an defined state.

Regards,

Osto

re.wolff9
Senior
Posted on August 20, 2016 at 08:30

Instead of messing with a function in RAM I would just disable interrupts, erase all flash blocks one by one, saving the block with the erase routine for last. (if the erase routine is 0x100 bytes align it to 0x100 bytes, to make sure that it doesn't end up on two pages. You can save this for last, as it is unlikely to cause problems). 

Posted on August 20, 2016 at 17:53

Lighting off a mass-erase should be sufficient to nuke the part, and wouldn't require code in RAM or other non-sense.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..