一.前言
. @* J7 M: a& cProprietary Code Read Out Protection (PCROP) ----- 专有代码读取保护
, q* X( M' V, c6 Q1 ` v4 S1 A2 V现在产品开发过程中,二次开发将会越来越多,设计公司开发出自己产品后交给终端客户进行二次功能或补充开发,简称二次开发,设计公司某些程序代码不希望公开给终端客户,但同时又希望部分函数功能可以给终端客户使用,这时就需要有一种专有代码保护机制供客户使用,STM32F4xx 芯片中的PCROP 可以解决类似问题。
, a1 D. |. i1 y0 H) I4 ]* {
3 A/ m" g& }% |% Z+ j二.PCROP 简要描述
) ~1 H' @& R8 ~0 IPCROP 功能在 STM32F42xxx 和 STM32F43xxx 芯片中包含,当使用 PCROP 时,用户扇区(0--23)Flash 能够阻止 D-Bus 的读取指令;保护功能选择通过 FLASH_OPTCR 寄存器的 SPRMOD 选择位进行选择:
3 v" e3 ]9 c8 A D• SPRMOD = 0: nWRPi 用于控制用户扇区写保护" q. d9 h4 j+ N* B* i+ @/ i4 B j
• SPRMOD = 1: nWRPi 用于控制 PCROP 读写保护8 g9 z. b- w& T E) j) O4 P1 q
当 PCROP 功能使能时:# H0 X. ]( a1 h* e
任何通过 D-Buss 的读取动作将会有 RDERR 错误标志. b: q b# I% D
任何对 PCROP 保护的扇区的擦除/写入操作将会有 WRPERR 错误标志
3 D& F4 o0 x7 Y2 |* e3 ^" oPCROP 保护等级设定如下:! z% E5 ]+ `- m, }
8 g6 {6 W! I, H
# J m+ g0 u, m# k
9 k5 O5 Q) t0 T0 @6 G# P三. 如何使用 PCROP 功能+ S" f8 P6 O$ { T6 F
1. 需要使用 6.50 以上版本的 EWARM(IAR),使能 C/C++ Complier --> Code--> No data reads in code memory ;
% Y8 H' i, t& T, f6 t
/ Y- @7 Y0 X" W* ^
7 b) y, z+ j. w3 M
3 g& r# q* i, L+ R8 _' D* f4 t' a2. 修改 stm32f4xx_flash.icf 文件,定义 PCROP 保护的地址空间;
# A- b5 q8 V6 H+ w; X: Q W% n# w- {: O S, m
0 |' _9 Y( o- E+ b& o& o
, v/ u& k# d* ^/ o- i. G
本文使用 STM32F429NIH6 芯片的 0x08008000---0x0800BFFF(Sector 2)作为 PCROP 保护区域,stm32f4xx_flash.icf 修改如下:
1 T7 n ^% K/ w- /*###ICF### Section handled by ICF editor, don't touch! ****/1 R: C6 i2 `; f+ b7 H0 E9 a; e
- /*-Editor annotation file-*/* w4 C: F* M7 T- ~! A
- /* IcfEditorFile="$TOOLKIT_DIR$\config\ide\IcfEditor\cortex_v1_0.xml" */
5 w# Z1 Y3 W/ R; ~# e - /*-Specials-*/
* u1 z6 B& a! K! {; j& T - define symbol __ICFEDIT_intvec_start__ = 0x08000000;& i. A6 Y2 G; c# \, @$ t* H7 C
- /*-Memory Regions-*/
: }0 c1 K$ k; J4 Z( u - define symbol __ICFEDIT_region_ROM_start__ = 0x08000000;
% P0 N0 f1 {' x0 |' b - define symbol __ICFEDIT_region_ROM_end__ = 0x08003FFF;) c3 A( E% D* V1 X) A! N0 f
- define symbol __ICFEDIT_region_CODE_start__ = 0x08008000;9 v9 \3 b7 t" F: n) Q4 G
- define symbol __ICFEDIT_region_CODE_end__ = 0x0800BFFF;$ L" P! T! {9 F7 \6 m
- / A s/ k* M! a p6 `3 T# G
- define symbol __ICFEDIT_region_RAM_start__ = 0x20000000;
' S5 W$ d1 V0 M5 \. R - define symbol __ICFEDIT_region_RAM_end__ = 0x2002FFFF;: ~" }& L% N! I1 w4 F, s
- define symbol __ICFEDIT_region_CCMRAM_start__ = 0x10000000;
: g6 b1 h1 c0 ]2 k& w - define symbol __ICFEDIT_region_CCMRAM_end__ = 0x1000FFFF;
8 J8 ^$ r+ X( W4 A* k( ^0 e - /*-Sizes-*/
" b1 Y+ R" G g% G, D6 I' Y - define symbol __ICFEDIT_size_cstack__ = 0x400;
! R" G8 w& a# Z) }* a: A - define symbol __ICFEDIT_size_heap__ = 0x200;
& x# N* I# n+ z( z' | - /**** End of ICF editor section. ###ICF###*/) t7 M B M" m
- define memory mem with size = 4G; x8 W" _: }: g
- define region ROM_region = mem:[from __ICFEDIT_region_ROM_start__ to
8 z5 A% i' k! j1 g& d - __ICFEDIT_region_ROM_end__];
7 ^3 I; @- w4 _* s3 f - define region CODE_region = mem:[from __ICFEDIT_region_CODE_start__ to! W7 i' _0 E4 g3 j
- __ICFEDIT_region_CODE_end__];
3 W M. z! _5 Q" r* ~ y/ Q" t$ W4 J - define region RAM_region = mem:[from __ICFEDIT_region_RAM_start__ to- k4 _7 O1 @7 [
- __ICFEDIT_region_RAM_end__];' G1 R! a; D h" Y+ }
- define region CCMRAM_region = mem:[from __ICFEDIT_region_CCMRAM_start__ to8 k0 p" Z' ?8 Z/ w2 Q% m) I
- __ICFEDIT_region_CCMRAM_end__];# O5 g6 a8 v3 s) `; K
- define region TabCode = [from 0x08004000 to 0x08007FFF];9 B! g% H7 \- A9 w# ~' {- m# {
- place in TabCode { ro section .tab, };
/ E6 M) U! B ^7 x - define block CSTACK with alignment = 8, size = __ICFEDIT_size_cstack__ { };
' |4 U$ Z3 T ?5 q. p7 w - define block HEAP with alignment = 8, size = __ICFEDIT_size_heap__ { };3 O) @3 o8 a6 U4 X$ y
- initialize by copy { readwrite };
3 j3 W" g- k* \; t6 }2 m1 y - do not initialize { section .noinit };5 b- f s$ Y4 h5 n+ L, @% F# D
- place at address mem:__ICFEDIT_intvec_start__ { readonly section .intvec };* o+ j. h6 ?0 i* G* C1 T
- place in CODE_region {section .CODE_Flash};. ?) f9 c6 ^( ]0 W$ r* j7 v4 p
- place in ROM_region { readonly };9 U8 C% v0 p( f: E
- place in RAM_region { readwrite,/ x: |8 [$ K+ E/ Y$ ~9 j# M
- block CSTACK, block HEAP };
复制代码
$ ]+ p% H+ T7 H7 A3. 修改 startup file (.s)文件 Reset_Handler 中 LDR 修改为 BLX:+ D0 ^% [' j Y
- Reset_Handler& e" _* v+ z6 j0 E$ V, p7 L: Y
- LDR R0, =SystemInit
0 B4 T# B* u# N0 V% g - BLX R0$ M+ C4 a$ S* Z+ d' W
- LDR R0, =__iar_program_start
# j: {5 T% ]; J) Q! D; p - BX R0( f2 t, e) c Q4 X: Q) ^5 e- [7 n
- 修改为:, X, K! z9 w( H% M0 E
- Reset_Handler
* d% J. {" d, W$ M& W# n2 S' E - BLX SystemInit
# c, `" A B! R' @ - BLX __iar_program_start
3 n" `: g5 \0 H. T- G8 g, D# Z
复制代码 / Q- F5 c4 T3 |
4. 定义程序到 PCROP 保护区域中:8 N. u9 P9 |0 w& s0 [
本文实例文件 Sector_Test.c 中定义 Light_Togger()函数定义在 0x08008000-0x0800BFFF 区域:
5 Z4 _/ n* Y0 A+ {- l- #include "stm32f4xx.h"7 v8 ^3 d3 l1 e ^- C8 U& P
- #pragma location = ".CODE_Flash". F- N# ~5 o/ Q/ h! U' l3 D
- void Light_Toggle(void)
: s$ B( Z. q x6 Q1 Q - {5 L2 W: @# q6 a. r& L
- GPIOG->BSRRH = GPIO_Pin_6 | GPIO_Pin_7;
% Y Y- c! g2 A; M0 Q - }
复制代码
/ O# r( o" C1 h5. 使用 STVP 对 OPTION BYTE 进行编程,使能 PCROP,并且选择保护区域:6 H5 }% v4 w; y( `) E
0 e% c% p' G. Y9 }+ ?& V4 |
' i3 n" D5 b7 N: G+ E0 r
# E5 v- t; N7 M% U' \
此时可以读取下 0x08008000---0x0800BFFF 区域的数据,可以看到改 PCROP 空间数据不可读,但程序依然可以调用该区域函数---- Light_Togger()0 M U( C- N( {& w& V, V
% e2 S8 T8 P; ]# l1 b$ Q" p- [
# D) [" ~$ `6 w' ]* U6 y8 f6 P, s1 o9 V# R/ s
四.本文所用测试代码说明以及代码! _! n# o- O8 \7 T2 n
& X9 W- X) l. a9 A- V1 F7 u1 W) uMain 函数作为客户程序代码,而需要调用的函数 Light_Togger()位于 PCROP 保护区域(0x08008000---0x0800BFFF),实现 GPIOG_6/GPIOG_7 端口的 Toggle;
% a8 z+ p- y, j8 S" M1 zMain.c 文件如下:9 g6 t' C6 V! g! U
- /* Includes ------------------------------------------------------------------*/
5 w" P7 q8 m* V - #include "main.h"
+ z" N& b; V1 j2 }% Z - extern void Light_Toggle(void);
/ u: r8 t5 b% O6 w/ G( F, o - int main(void) ?7 v, e a1 W& o
- {
0 x- Y) b) c1 E: R# U+ j1 p - unsigned int i;! @1 P( L+ Q9 E
- GPIO_InitTypeDef GPIO_InitStructure;
a$ |: L0 J" b - /* GPIOG Peripheral clock enable */2 G1 Z( X# |7 e2 l$ D$ ?/ w
- RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOG, ENABLE);
3 ^+ d% i& M% q W- I6 q- s - /* Configure PG6 and PG8 in output pushpull mode */
9 m N4 v( l/ O) o: B" |# z - GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
9 Z3 R' F! `! Q3 f+ W4 q) T6 R1 ^ n - GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
/ D0 u2 Q" E% `" \4 o4 d - GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;$ W& D3 w! h7 O: y x# ^
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;( B' g) u) @ k* j! Z& n
- GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;9 z G& ?7 ^! H
- GPIO_Init(GPIOG, &GPIO_InitStructure);
9 Z3 z B& L5 R
( Y* I5 J7 S3 G3 M- while(1)
$ H, i" u3 j$ d+ s: x4 Z% | - {+ i6 ?# P! I$ j o6 t7 O
- GPIOG->BSRRL = GPIO_Pin_6 | GPIO_Pin_7;
' G& v8 L# _/ w: i+ _ - i = 0xFFFFFF;
; ^: F8 o( P/ }& z; O) p5 Q - while(i--);( A# U. H/ q* m
- Light_Toggle();9 X8 U: c" M+ g! k/ T
; f/ K3 d6 [0 u5 Y& i7 [- i = 0xFFFFFF;8 [, \# e; r* w, R' x
- while(i--);
4 l6 d6 ~$ v; v1 \( { - }
4 u, s/ V' _' A& U - }
复制代码 " D, K- L. Y# j' q+ y- d- c
Sector_Test.c 如下:3 e1 D6 F* Y( {/ B6 J" O: R
- #include "stm32f4xx.h"6 W2 ^. @) J! v
- #pragma location = ".CODE_Flash"5 a# N e% n! w7 h% n1 d9 }' l3 h2 N
- void Light_Toggle(void)
+ p; M8 S1 _1 D5 Q1 Z - {
& Z0 m: H9 u( k& b - GPIOG->BSRRH = GPIO_Pin_6 | GPIO_Pin_7;
1 T5 H- x) y3 ` ~0 n - }
复制代码 / p2 E- |! b! V- Q+ P$ J+ n7 t
$ U( n& }7 Z# z1 M6 j9 L' K |