AN0040 AT32F403A 407 Security Library Application Note EN V2.0.2
AN0040 AT32F403A 407 Security Library Application Note EN V2.0.2
AN0040
Application Note
Introduction
This application note mainly introduces the security library (sLib) application principle of
AT32F403A/407 MCUs, operation methods and example projects.
Applicable products:
AT32F403A
Part number
AT32F407
Contents
Overview ................................................................................................................. 6
Application principles ........................................................................................... 7
Application principle of sLib ...................................................................................... 7
How to enable sLib protection .................................................................................. 9
How to disable sLib protection .................................................................................. 9
Compile and execute program in sLib .................................................................... 10
Setting interrupt vector table as security library not allowed ..................................... 11
Correlation between sLib area and user code area ................................................... 11
Overview ................................................................................................................ 14
SLIB protected code: FIR low-pass filter................................................................. 15
Project_L0: example for solution providers ............................................................. 16
Generate execute-only code ......................................................................................16
Compile security library address ................................................................................18
Enable sLib protection................................................................................................22
Project_L0 execution process ....................................................................................23
Generate header file and symbol definition file..........................................................25
List of tables
Table 1. Flash size of AT32F403A/407 ................................................................................................ 8
Table 2. Document revision history.................................................................................................... 39
List of figures
Figure 1. Mapping of main Flash memory featured with sLib ............................................................. 8
Figure 2. Literal pool example (1) ...................................................................................................... 10
Figure 3. Literal pool example (2) ...................................................................................................... 11
Figure 4. Function in sLib area calling the function in user code area .............................................. 12
Figure 5. Example of user-defined function....................................................................................... 13
Figure 6. Example application process.............................................................................................. 15
Figure 7. Example application ........................................................................................................... 15
Figure 8. FIR low-pass filter ............................................................................................................... 15
Figure 9. Enter Option interface in Keil.............................................................................................. 17
Figure 10. Select Execute-only Code in Keil ..................................................................................... 17
Figure 11. Enter Option interface in IAR ............................................................................................ 17
Figure 12. Set C/C++ options in IAR ................................................................................................. 18
Figure 13. Main Flash memory mapping and RAM partition ............................................................. 19
Figure 14. Set Linker option in Keil.................................................................................................... 19
Figure 15. Modify scatter in Keil ........................................................................................................ 20
Figure 16. Modify SLIB RAM address in KEIL .................................................................................. 20
Figure 17. Modify SLIB constant address in KEIL ............................................................................. 20
Figure 18. SLIB address definition in icf ............................................................................................ 21
Figure 19. Address assignment in icf file ........................................................................................... 21
Figure 20. Modify IP-Code RAM in icf file.......................................................................................... 21
Figure 21. Modify SLIB constant address in IAR............................................................................... 22
Figure 22. Configure ICP Programmer .............................................................................................. 22
Figure 23. Set parameters in Download Form .................................................................................. 23
Figure 24. Project_L0 execution process .......................................................................................... 24
Figure 25. Set Misc controls in Keil ................................................................................................... 25
Figure 26. Contents of modified fir_filter_symbol.txt ......................................................................... 25
Figure 27. Set Build Actions in IAR.................................................................................................... 26
Figure 28. Edit steering_file.txt content ............................................................................................. 26
Figure 29. Modified scatter file........................................................................................................... 27
Figure 30. Modified icf file .................................................................................................................. 27
Figure 31. Add symbol definition file in Keil ....................................................................................... 28
Figure 32. Modify symbol definition file type to “Object file” .............................................................. 28
Figure 33. Add symbol definition file in IAR ....................................................................................... 28
Figure 34. Project_L1 execution process .......................................................................................... 29
Overview
As more and more MCU applications require complex algorithms and middleware solutions, it has
become an important issue that how to protect IP-Codes (such as core algorithms) developed by
software solution providers.
The AT32F403A/407 series MCUs are designed with a security library (sLib) to protect important
IP-Codes against being changed or read by the end user’s program.
This application note details the sLib application principle and operation methods of
AT32F403A/407 MCUs.
Application principles
Application principle of sLib
Security library is a defined area protected by a code in the main memory, so that solution
providers can program core algorithm into this area, and the rest of the area can be used for
secondary development by end customers.
Security library includes instruction security library (SLIB_INSTRUCTION) and data security
library (SLIB_DATA), users can select part of or the whole security library for instruction
storage, but using the whole security library for storing data is not supported.
Program codes in the instruction security library (SLIB_INSTRUCTION) can only be fetched
(can only be executed) by MCU through I-Code bus and cannot be read through D-Code
(including ISP/ICP debug mode and programs that boot from internal RAM). When accessing
the SLIB_INSTRUCTION in the manner of reading data, values are all read 0xFF.
Data in the data security library (SLIB_DATA) can only be read through D-Code bus and
cannot be programmed.
The program code and data in security library cannot be erased unless the correct code is
keyed in. If a wrong code is keyed in, in an attempt of writing or erasing the security library, a
warning message will be issued by EPPERR=1 in the FLASH_STS register.
The program code and data in security library are not erased when the end users perform a
mass erase on the main Flash memory.
Users can write the previously defined password in the SLIB_PWD_CLR register to disable
security library protection. When the security library protection is disabled, the chip will perform
a mass erase on the main Flash memory (including the contents of security library). Therefore,
even if the code defined by the software solution provider is leaked, the program code will not
be leaked.
The mapping of main Flash memory featured with sLib is shown in Figure 1. The program codes in
security library can be easily called and executed by end users, but cannot be read directly.
User_Code_Start@
USER CODE
User_Code_End@
SLIB_Start@
SLIB_INSTRUCTION
SLIB_DATA
SLIB_End@
The range of sLib is set by sector, and the size of each sector is subject to the specific MCUs.
Table 1 lists the main Flash size, sector size and configurable range of AT32F403A/407 series
MCUs.
Part number Internal Flash size(Byte) Sector size (Byte) Configurable range
AT32F403AxC Sector 2 ~ 63
256K 2K
AT32F407xC (0x08001000 ~ 0x0801FFFF)
AT32F403AxE Sector 2 ~ 63
512K 2K
AT32F407xE (0x08001000 ~ 0x0801FFFF)
AT32F403AxG Sector 2 ~ 63
1024K 2K
AT32F407xG (0x08001000 ~ 0x0801FFFF)
For details of security library setting register, please refer to AT32F403A/407 Series Reference
Manual.
The program to start security library can be found in the slib_enable() function in main.c file of
project_l0. In addition, users can use Artery ICP/ISP tools for configuration.
User_Code_Start@
In addition, the standard function library of C programming language is commonly used, such as
memset() and memcpy() functions. If both IP-Code and user area code call such functions, the
above mentioned error may occur. The two solutions are recommended:
1) Compile into the sLib area (refer to Keil or IAR documents for details about implementation).
2) Do not use the standard function library of C programming language in IP-Code. If it is
necessary to use in IP-Code, functions to be used must be renamed. Figure 5 shows an
example of writing the my_memset() function to replace the original memset() in IP-Code.
Overview
This application note provides two example projects to demonstrate that software developers
develop IP-Code for end-user applications.
Project_L0: Solution provider develops algorithm and compiles to sLib
Project_L1: Apply algorithm for end users
The algorithm completed in Project_L0 will be pre-downloaded and pre-burned to AT32F403A chip
and configured as sLib protected. In addition, the following settings are available for the end-user
applications.
Main Flash memory mapping, showing the area occupied by sLib and the area where users
can develop programs
Header file that contains algorithm function definitions, allowing end users to call relevant
functions;
Symbol definition file, which contains the actual address of each IP-Code function, so that
functions can be called properly by the end-user application.
Project_L0
Programs SLIB protected code
Project_L1
Programs End User Code
Using SLIB protected functions
Software solution providers can refer to the Project_L0 to develop algorithm code and refer to
Project_L1 for end-user application.
In this example, FPU and DSP instructions in the MCU are used for signal processing and floating
point arithmetic to realize accurate calculation and correct output signal.
Tick “Execute-only Code” in the C/C++ interface, and the “--execute_only” instruction is added
to the compiler control string, as shown in Figure 10;
The arm_fir_f32.c, arm_fir_init_f32.c and fir_filter.c files are in the SLIB_INSTRUCTION area,
and these files need to be set as generating execute-only code.
Enter "C/C++" interface and tick “Override inherited settings” and “No data read in code
memory”, as shown in Figure 12;
The arm_fir_f32.c, arm_fir_init_f32.c and fir_filter.c files are in the SLIB_INSTRUCTION area,
and these files need to be set as generating execute-only code.
Compile security library address
As aforementioned, the first sector (sector0) of the main Flash memory is used to store interrupt
vector table. Therefore, the security library is set from sector 2 in this example, with sector 2 and
sector 3 being set as instruction security library, and sector 4 and sector 5 being set as data
security library. Figure 13 shows the main Flash memory mapping and RAM partition. The main
purpose of RAM partitioning is to avoid the same RAM being used by sLib-protected code and end-
user code.
0x20000000 0x08000000
Vector table
User RAM User code
0x08000FFF
0x20016FFF 0x08001000
0x20017000
SLIB_INSTRUCTION
SLIB used RAM
0x08001FFF
0x08002000
SLIB_DATA
0x08002FFF
0x08003000
User code
0x080FFFFF
Open scatter file, load the object file of the code to be placed in SLIB_INSTRUCTION area to
“LR_SLIB_INSTRUCTION” (a dedicated loading area that starts from sector 2 and occupies
two sectors), and modify the label to “execute-only (+XO)”. In addition, place the area occupied
by SLIB_Data to a dedicated loading area named “LR_SLIB_DATA” to avoid the compiler
compiling other non-IP-code functions to the SLIB area. The RW_IRAM2 assigns the region
from 0x20017000 to 0x20017FFF to the algorithm functions to avoid the same RAM region
2022.4.13 19 Ver 2.0.2
AT32F403A/407 Security Library Application Note
being used by end-user project, causing fault or error in program execution.
In addition to modifying the scatter file, for the RAM used by IP-Code, users can also use the
Keil “__attribute__((at(address)))” descriptor to load variables to 0x20017000, as shown in
Figure 16.
The start address of data security library is sector 4 (0x08002000). To compile the constants
used by FIR low-pass filter functions to this address, users can modify the scatter file as
aforementioned, or use the Keil “__attribute__((at(address)))” descriptor to load the constants
to a fixed address, as shown in Figure 17.
In the icf file, the area occupied by SLIB is reserved to avoid the compiler compiling other non-
IP-code functions to the SLIB area, and the RAM region used by IP-Code is reserved.
For the RAM used by IP-Code, users can use the IAR @ descriptor to load variables to a fixed
address 0x20017000 or modify the icf file, as shown in Figure 20.
The start address of data security library is sector 4 (0x08002000). To compile the constants
used by FIR low-pass filter functions to this address, users can modify the icf file as
aforementioned, or use the IAR @ descriptor to load the constants to a fixed address, as
shown in Figure 21.
Click “Download” and the “Download Form” pops up, which shows SLIB status and relevant
parameters. Set sector 2 as the start sector, sector 4 as the data start sector and sector 5 as
the end sector; set the enable password as “0x55665566” (user-defined), tick “Enable SLIB”,
and then click “Start Download” to complete programming and enable SLIB, as shown in
Figure 23.
For details about ICP Programmer, refer to ICP Programmer User Manual.
(2) Use slib_enable() function in main.c
After the slib_enable() function is verified correct by low-pass filter function and then executed, the
sLib protection can be enabled. To execute this function, enable the “#define
USE_SLIB_FUNCTION” in main.c.
Start
LED3 toggle
continuously
No
Execute
User button
system rest to activate
Pressed ?
SLIB
yes
Execute
Green LED4 on
FIR filter
3 seconds
test
Yes
SLIB Check
Red LED2 toggle
Operate FIR test Fail
in infinite loop
successfully? result
Success
SLIB
No Enable SLIB No already
enabled?
Yes
After compiling the project, a symbol definition file named “fir_filter_symbol.txt” is generated
under project_l0\mdk_v5\Objects;
This symbol definition file contains all symbol definitions of the project, and it needs to be
modified to only remain the definitions of low-pass filter functions to be called by end users.
The modified fir_filter_symbol.txt is shown in Figure 26;
The fir_filter_symbol.o is the symbol definition file to be generated, and the steering_file.txt is
saved under project_l0\iar_v8.2, which is used to select function symbols to be generated.
Users can manually edit according to the contents called by sLib. As shown in Figure 28, the
"show" is the command used to select functions.
After adding this file to fir_filter, modify its file type from “text” to “Object”, as shown in Figure 32.
Start
LED3 toggle
continuously
No
User button
Pressed ?
yes
Execute
FIR filter
test
Check
Green LED4 toggle Red LED2 toggle
Success FIR test Fail
in infinite loop in infinite loop
result
In the “Memory” window, enter the address 0x08002000 of SLIB_DATA start sector (sector 4);
this region is allowed to be read through D-Code bus, so original values can be found, as
shown in Figure 39;
In the “Memory” window, double click to modify the value of 0x08002000, and a warning pops
up by setting EPPERR=1 in the FLASH_STS register, indicating the protection is enabled;
In case of enable erase/program protection error interrupt, continuing execution will enter the
interrupt program;
Method B: The solution provider uses the compiled project to generate a bin file directly, and
take the corresponding section in the SLIB area. For example, in the KEIL project, add “fromelf.exe
(3) End users also can use ICP Programmer to set an offline project and save to AT-Link, and
then complete offline programming to MCU through AT-Link, as shown in Figure 45.
(4) After completing step 2/3, end users can get the MCU with programmed SLIB area (SLIB
status: enabled), and program the application code to MCU through online or offline
programming, as shown in Figure 46.
(3) After obtaining the offline project, the end user should use ICP Programmer to open the project
file and add the application codes to the offline project; then save to PC or AT-Link, and
perform offline download. Figure 48 shows how to add the project file.
Note: To protect codes from being leaked or decoded, do not other change settings when adding code file to
the offline project, which requires the solution provider to configure the final settings in advance.
Revision history
Table 2. Document revision history
Date Version Revision note
2021.05.21 2.0.0 Initial release.
2021.11.01 2.0.1 Optimized format.
2022.4.13 2.0.2 Modified the configurable range of sLib in the Flash.
Purchasers are solely responsible for the selection and use of ARTERY’s products and services, and ARTERY assumes no liability
whatsoever relating to the choice, selection or use of the ARTERY products and services described herein.
No license, express or implied, to any intellectual property rights is granted under this document. If any part of this document deals with any
third party products or services, it shall not be deemed a license grant by ARTERY for the use of such third party products or services, or any
intellectual property contained therein, or considered as a warranty regarding the use in any manner whatsoever of such third party products
or services or any intellectual property contained therein.
Unless otherwise specified in ARTERY’s terms and conditions of sale, ARTERY provides no warranties, express or implied, regarding the
use and/or sale of ARTERY products, including but not limited to any implied warranties of merchantability, fitness for a particular purpose
(and their equivalents under the laws of any jurisdiction), or infringement of any patent, copyright or other intellectual property right.
Purchasers hereby agrees that ARTERY’s products are not designed or authorized for use in: (A) any application with special requirements
of safety such as life support and active implantable device, or system with functional safety requirements; (B) any air craft application; (C)
any automotive application or environment; (D) any space application or environment, and/or (E) any weapon application. Purchasers’
unauthorized use of them in the aforementioned applications, even if with a written notice, is solely at purchasers’ risk, and is solely
responsible for meeting all legal and regulatory requirement in such use.
Resale of ARTERY products with provisions different from the statements and/or technical features stated in this document shall
immediately void any warranty grant by ARTERY for ARTERY products or services described herein and shall not create or expand in any
manner whatsoever, any liability of ARTERY.