cancel
Showing results for 
Search instead for 
Did you mean: 

F4 and Code Protect

picguy2
Associate II
Posted on March 11, 2013 at 15:53

I loaded data into ROM page 11.  I then changed from 1’s to 0’s from my running code.  So far so good.

Now I want to prevent my code from being read via a debugger.

I can set FLASH_OPTCR bits 8..15 to 0xBB in my code.  If I read the RM correctly this protects my code from being read and manipulated by a debugger.  I am using ST-LINK to load my code.  Have I have missed some strange extra option that automatically sets FLASH_OPTCR for code protect when I load my code?

Yet, if I understand correctly, this setting goes away on reset.  That’s useless for protecting my code from debugger inspection.

I know about the option bytes location 0x1FFFC000.  Using IAR tools and ST-LINK its default value appears as 0x5500AAFF.  I can change that with my code to 0x5500BBFF (or 0x550022FF if I can’t change a 0 to a 1.)  I assume this changes the default value of FLASH_OPTCR.

But will doing that prevent me from loading code with the debugger?  I would hope that I would be limited to erasing the entire part before I can do anything else with the debugger.  I only have 2 F4 discovery boards and I would hate to have to buy another.

My plan is to have the option bytes set to AA, level 0, read protection not active.  Load code that way with ST-LINK.  Then run the code using an external source to download the final updates to ROM page 11.  After that I would change the option bytes for RAM and ROM read protection from debuggers and other nefarious attempts to discover what is going on inside the microcontroller.

Side issue:

About how long is the processor suspended during a single word ROM write?  I am running with 3.3V.  

I ask because I will be unable to interrupt to transfer external data during the time the ROM is dedicated to the update process.  If this time is over 200 microseconds I will have to suspend 38K baud RS-232 for the duration.  If ROM write time never exceeds 100 microseconds I can ignore the temporary break in execution without worry.
5 REPLIES 5
Posted on March 11, 2013 at 16:51

The 100 us number is quoted as the worst time, with the flash array in it's most worn out condition. The erase times, are the REAL killer.  Generally you can avoid the stall completely if you run your code from RAM, including interrupts, or use the dual banked 2MB parts (427/429).

As I recall the option words are written as a pair, the second an inverted version of the first.
Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
picguy2
Associate II
Posted on March 12, 2013 at 18:49

(something funny in prior post -- hope this one is right)

Thanks clive1, for the info about word update time.

Lacking any real information I updated the option byte to level 1 (disable debugger and other ways of looking at code.)  Big trouble.  At least using IAR tools I found no way to load new code.  

Started looking for ST-LINK on st.com.  Direct search failed but found something at Home > Tools and Software > STSW-LINK004.  I downloaded the ST-LINK Utility.  That utility (Target > Option Bytes) was what I needed.  I set Read Out Protection to Level zero.  As expected the entire part was erased and I was back in business.

frankmeyer9
Associate II
Posted on March 12, 2013 at 19:06

I hope you are aware that this behavior is according to the specification.

And for CRP level 2, there is no return.

Posted on March 12, 2013 at 19:24

(something funny in prior post -- hope this one is right)

Yeah, you have to top post your response and watch for invisible codes which are hard to see/delete. I really hate this embedded WordPad thing, totally didn't work on Nexus 7, and broke with earlier FireFox releases. It probably works on a Surface RT or Pro, but Microsoft has irretrievably lost my tablet and phone business already.

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
Amel NASRI
ST Employee
Posted on June 28, 2013 at 18:05

(something funny in prior post -- hope this one is right)

The ''funny post'' is removed to keep the thread cleaner.

-Mayla-

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.