En DM00186528
En DM00186528
En DM00186528
Application note
Proprietary code read-out protection on microcontrollers
of the STM32F4 Series
Introduction
Software providers are developing complex middleware solutions (Intellectual Propriety
code, or IP-Code), protecting them is an issue of high importance for microcontrollers.
In order to respond to this important requirement, STM32F4xx MCUs feature:
• Read Protection (RDP): protection against read operations
• Write Protection: protection against undesired write or erase operations
• Proprietary Code Read Out Protection (PCROP): protection against read and write
operations.
This application note provides a description of these Flash memory protection techniques,
focusing on the Proprietary Code Read Out Protection (PCROP) and providing a basic
example of PCROP protection.
The X-CUBE-PCROP firmware package delivered with this document contains the source
code of the PCROP example with all firmware modules required to run the example.
This application note has to be read in conjunction with reference manuals as listed in
Table 1. These documents, as well as the corresponding datasheets, are all available at
www.st.com.
STM32F401 RM0368
STM32F410 RM0401
STM32F411 RM0383
STM32F412 RM0402
STM32F413 RM0430
STM32F42x/STM32F43x RM0090
STM32F446 RM0390
STM32F469/479 RM0386
Contents
2 Write protection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
4 PCROP example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
4.1 Example requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
4.1.1 Hardware requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
4.1.2 Software requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
4.2 Example overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
4.2.1 Software settings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
4.3 PCROP-ed IP-Code: FIR low pass filter . . . . . . . . . . . . . . . . . . . . . . . . . . 15
4.4 STEP1: ST Customer level n . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
4.4.1 Generating an execute-only IP-Code . . . . . . . . . . . . . . . . . . . . . . . . . . 16
4.4.2 Placing the IP-Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
4.4.3 Placing constants outside of PCROP-ed sector . . . . . . . . . . . . . . . . . . 21
4.4.4 Protecting the IP-Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
4.4.5 Step1-ST_Customer_level_n project flow . . . . . . . . . . . . . . . . . . . . . . . 23
4.4.6 Executing PCROP-ed IP-Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
4.4.7 Creating header file and generating symbol definition file . . . . . . . . . . . 26
5 Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
6 Revision history . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
List of tables
List of figures
The Read Out Protection is a global Flash memory read protection allowing the embedded
firmware code to be protected against copy, reverse engineering, dumping using debug
tools or other means of intrusive attack. This protection should be set by the user after the
binary code is loaded to the embedded Flash memory.
2 Write protection
The write protection is used to protect the content of specified sectors against code update
or erase. This protection can be applied by sector.
Any write request generates a Write Protection Error. The WRPERR flag is set by hardware
when an address to be erased/programmed belongs to a write-protected part of the Flash
memory.
For example the mass erase of a Flash memory where at least one sector is write protected
is not possible and the WRPERR flag is set.
Flash memory sectors can be either in Write protection or Read & Write (PCROP) protection
mode, depending on the Selection of protection mode (SPRMOD option bit) of nWPRi
option bits. To activate the write protection (SPRMOD = 0) for each Flash memory sector i,
one option bit (nWRPi) is used. When the Write protection is set for sector i (option bit
nWRPi = 0), this sector cannot be erased or programmed.
Enabling or disabling write protection can be managed either by embedded user code or by
using STM32 ST-Link Utility software and debug interfaces.
8VHUFRGHBVWDUW#
8VHUFRGH
8VHUFRGHBHQG#
3&523BVWDUW#
3&523HG &DQEHRQHRU
,3&RGHDUHD PRUHVHFWRUV
3&523BHQG#
069
independently PCROP-ed, and protecting additional sectors is possible (when RDP is set to
level 0 or 1) without full chip erase unlike disabling PCROP protection.
Some precautions should be taken when activating the PCROP feature. The active values
of nWRPi bits are inverted when PCROP mode is active, so if SPRMOD = 1 and nWRPi = 1,
then the user Sector i is PCROP-ed.
To correctly activate PCROP on Sector i, the sequence detailed below must be followed:
1. Clear all nWRP bits except those for already PCROP-ed sectors (for STM32F4
products with two Flash memory banks such as STM32F429ZIT, bank2 nWRP bits
must be cleared too);
2. Set nWRPi bit for Sector i to be PCROP-ed;
3. Enable PCROP Protection mode by setting SPRMOD bit
For more details on PCROP enabling implementation, refer to the PCROP_Enable()
function described in the provided FW package (Step1-ST_Customer_level_n project
main.c file).
It’s important to note that it is not possible to have at the same time one write protected
sector and another PCROP-ed sector. So user can have either write protection mode and
sectors could be write protected, or PCROP mode and sectors could be PCROP-ed.
Figure 2 shows the difference between PCROP and write protection.
3&523SURWHFWLRQPRGH :ULWHSURWHFWLRQPRGH
6HFWRUQ 6HFWRUQ
6HFWRU 6HFWRU
069
The only way to disable PCROP on a protected sector is by changing RDP level from 1 to 0
and at the same time clearing the SPRMOD bit.
During application development user may need to disable PCROP or global RDP protection
without spending time in developing and disabling protection functions. STM32 ST-LINK
Utility tool can be a very simple way for disabling or enabling protection using debug
interfaces as JTAG or SWD without the need for developing dedicated functions.
For more details on how to use STM32 ST-LINK Utility software user should refer to user
manual UM0892, available at www.st.com.
Note: Disabling PCROP or/and Global RDP protection leads to a full chip erase.
Figure 3. PCROP-ed code calling a function located outside the PCROP-ed region
8VHUFRGHBVWDUW#
)XQFWLRQB%IL[HG# )XQFWLRQB%
^
««
8VHUFRGHUHJLRQ
««
««
`
8VHUFRGHBHQG#
3&523BVWDUW#
)XQFWLRQB$
^
3&523UHJLRQ
)XQFWLRQB%
`
3&523BHQG#
069
4 PCROP example
The firmware example provided with this application note illustrates a use case of PCROP
protection. All steps required to develop this firmware are detailed in this section.
67&XVWRPHU±/HYHOQ
3URJUDPVSURWHFWHG,3&RGH
&XVWRPHU±/HYHOQ
3URJUDPV(QGXVHU&RGH
XVLQJ,3&RGHIXQFWLRQV
(QGXVHUDSSOLFDWLRQ
069
2(0
67&XVWRPHUOHYHOQ
3URYLGHV 3URYLGHV
670) SUHSURJUDPPHG
0&8V 670)0&8V
(1'&86720(5
67&XVWRPHUOHYHOQ
069
WHVW,QSXWBIBN+]BN+] WHVW2XWSXW
),5BORZSDVVBILOWHU
069
The following function was created using the CMSIS DSP functions described above:
• FIR_lowpass_filter(): the global function representing the FIR Filter, described in
fir_filter.c file.
The FPU and DSP embedded in STM32F4 microcontrollers are used for signal processing
and floating point calculation to output the correct signal.
For more details on FIR functions users should refer to CMSIS documentation in
“Drivers/CMSIS/Documentation/DSP” directory included in the associated software
package.
In the following window (see Figure 9) check the option Execute-only Code, then the
–execute_only command is added in compiler control string field.
Then in the scatter file replace the option read only +RO by execute-only +XO.
Then, in the following window check the option “No data reads in code memory”, as
indicated in Figure 11.
[
9HFWRUWDEOH
8VHUFRGH
[)))
[
3&523HG,3&RGH
[%)))
[&
,3&RGHFRQVWDQWV
[))))
$YDLODEOHVSDFH
[)))))
069
}
}
Another way is placing constants at a fixed address directly in user code when defining the
const using the @ operator for IAR and __attribute__((at(address))) for Keil®. This approach
has been used in this example and constants definition were done in fir_filter.c file which is
similar to the following example.
Caution: When placing IP-Code constants, user should take in account that these constants can be
deleted or modified by ST Customer level n + 1, consequently the IP-Code functions will be
useless. It’s then recommended to place these constants in a dedicated sector where no
user code will be programmed. Then this memory region should be highlighted in the Flash
memory map provided to ST Customer level n+1.
6WDUW
5HG/('WRJJOLQJ
FRQWLQXRXVO\
8VHUEXWWRQ 1R
SUHVVHG"
<HV
<HV 3&523
DOUHDG\HQDEOHG"
1R
(QDEOH3&523
RQ6HFWRU
3&523 1R ,QILQLWHORRS
VXFFHVVIXOO\
5HG/('21
HQDEOHG"
<HV
6HFWRU3&523HG
*UHHQ/('21
5HDG2SHUDWLRQ
(UURULQWHUUXSW
8VHUEXWWRQ 1R
6\VWHP5HVHW SUHVVHG"
LQLWLDWLRQ
<HV
6\VWHPUHVWDUW 3&523HG
,3&RGHH[HFXWLRQ
3&523,3&RGH 3&523,3&RGH;2
SURMHFWFRQILJXUDWLRQ SURMHFWFRQILJXUDWLRQ
069
PCROP-IP-Code (for test only and must not be used for STEP2)
No special compiler option used, just for testing purposes to show that avoiding data in code
(as literal pools and branch tables) is mandatory for PCROP-ed codes.
1. In the same project located in Step1-ST_Customer_level_n directory select PCROP-
IP-Code configuration
2. Rebuild all files.
3. Run the example following the sequence below:
a) Power on the board and before loading the code, check if there is any PCROP-ed
or write protected sector. If yes disable the protection using STM32 STLink Utility,
then load the code; Once program has been loaded, red LED should toggle
continuously;
b) press the user button key to activate PCROP protection, once done green LED is
ON and Sector 2 is PCROP-ed, else red LED is ON and PCROP activation failed;
c) press the user button key to execute the PCROP-ed IP-Code called in main.c file,
an Error Operation Interrupt is generated, system Reset is initiated and the red
LED toggles continuously.
Interpretation
The low pass filter function computes the testInput_f32_1kHz_15kHz input signal and
should output a 1 KHz sine wave. The output data testOutput is then compared to the
reference refOutput already calculated with MATLAB, if it matches, the green LED toggles
continuously, else the red LED toggles continuously.
For the PCROP-IP-Code configuration where PCROP-ed IP-Code contains literal pools:
when executing IP-Code (FIR_lowpass_filter() function), literal pools could not be accessed
through D-Code bus then the RDERR flag is set. OPERR flag is set as well and a read
operation error interrupt is generated, then a system reset is initiated in
HAL_FLASH_OperationErrorCallback() function and red LED toggles continuously.
However for the PCROP-IP-Code-XO configuration the IP-Code is executed correctly and
green LED should toggle continuously.
Note: For more details, refer to the readme.txt inside the firmware package.
PCROP-ed IP-Code functions are then used to create end user application. The project
located in Step2-ST_Customer_level_n+1 directory is an example where PCROP-ed FIR
Filter functions are called in main.c file.
The added fir_filter.txt file type has to be changed to Object file instead of text document file,
as indicated in Figure 19.
6WDUW
5HDG2SHUDWLRQ 5HG/('WRJJOLQJ
(UURULQWHUUXSW FRQWLQXRXVO\
6\VWHP5HVHW 8VHUEXWWRQ 1R
LQLWLDWLRQ SUHVVHG"
<HV
6\VWHPUHVWDUW 3&523HG
,3&RGHH[HFXWLRQ
069
7. Fill in the address field the PCROP-ed sector starting address and click on Go To
button as shown in Figure 23.
As shown in Figure 24, the PCROP-ed IP-Code loaded in Sector 2 is unreadable while code
located just before 0x08008000 address can be read. Reading PCROP-ed sector sets
RDERR and OPERR flags in FLASH_SR register.
A Flash operation error interrupt is generated due to Flash memory read operation request
through D-Code bus when debugging.
Then a software Reset is initiated in the HAL_FLASH_OperationErrorCallback() function
(described in main.c file) and Red LED4 toggles continuously.
5 Conclusion
Microcontrollers of the STM32F4 Series provide very flexible and useful Read and/or Write
protection features that can be used in applications where protection is required.
This application note shows how Read, Write and PCROP protection features provided on
STM32F4xx MCUs can be used.
6 Revision history
STMicroelectronics NV and its subsidiaries (“ST”) reserve the right to make changes, corrections, enhancements, modifications, and
improvements to ST products and/or to this document at any time without notice. Purchasers should obtain the latest relevant information on
ST products before placing orders. ST products are sold pursuant to ST’s terms and conditions of sale in place at the time of order
acknowledgement.
Purchasers are solely responsible for the choice, selection, and use of ST products and ST assumes no liability for application assistance or
the design of Purchasers’ products.
Resale of ST products with provisions different from the information set forth herein shall void any warranty granted by ST for such product.
ST and the ST logo are trademarks of ST. All other product or service names are the property of their respective owners.
Information in this document supersedes and replaces information previously supplied in any prior versions of this document.