STM32F4(Flash读保护)* N) ?1 r) O4 x5 N. B
& W! p9 G Q8 ~/ e& U$ \+ p
! M9 F2 W/ _1 X转载来源:STM32F4(Flash读保护)7 d, ]& X$ y- V
& B A1 X1 M: r% b
" i- \' f; v8 g* I; }! t5 l9 ]/ r
1,目的
/ g2 r; K2 m7 E' a 在实际的产品发布中,如果不对储存在单片机Flash中的程序做一些保护的话,就有可能被一些不法公司,通过仿真器(J-Link,ST-Link等)把Flash中的程序读取回来,得到bin文件或hex文件,然后去山寨产品。所以我们需要对程序进行保护,一种比较简单可靠的方法就是把Flash设置成读保护。6 @3 `# |' X( I8 V
6 i; o" j- ^: k5 {9 l: c( p. D
2,开发环境7 n% s" {& O4 b; e0 Y% ^
1,适用芯片:STM32F4全部芯片" ?4 y% ]8 A R$ g1 I& q2 a
2,固件库:STM32F4xx_DSP_StdPeriph_Lib_V1.8.0
. f& a! O# X7 M' P 3,IDE:MDK517
5 E. s, `& a+ R, C; D x
( I) w7 B9 W6 ^/ K; H; S3,程序源码8 V9 @3 W. X9 ]; N
1 {9 a4 ?+ r/ i+ S
- /****************************************************************
( M$ N" V, X5 d5 k k - * Function: Flash_EnableReadProtection
0 }5 q0 ?! b4 h4 f8 z7 X" `% L - * Description: Enable the read protection of user flash area./ r9 u1 L" {: E1 @: e) s* h
- * Input:
4 p! B! C1 q$ ]( d' k' { - * Output:
" q0 ]( x& M) a! D( q J( n - * Return: 1: Read Protection successfully enable2 ?( A) {; P$ c9 ?! m
- * 2: Error: Flash read unprotection failed
$ [. P6 L6 M, a% f - *****************************************************************/
& P f) z# ]. ^6 A$ _ - uint32_t Flash_EnableReadProtection(void)
8 B0 W8 `) a7 U" U$ ~ - {
1 I- _! \& K+ p7 S U0 S" v3 O( m - /* Returns the FLASH Read Protection level. */
( g( H" X2 N" Y% u* d; m5 Q0 P - if( FLASH_OB_GetRDP() == RESET )
+ S& A: `6 [6 s- H - {
% l4 U4 z: g3 Y1 p3 e6 B - /* Unlock the Option Bytes */( G2 M% o5 t# B3 o3 O
- FLASH_OB_Unlock();
1 H9 ?- }, U2 q -
, |( S, N- ?( M( ?+ r, W4 H - /* Sets the read protection level. */
( M2 P; }. \& [3 L8 g - FLASH_OB_RDPConfig(OB_RDP_Level_1);) B* H+ f) T" ]; `: N
-
# H! ], k& a L) K - /* Start the Option Bytes programming process. */ ' [8 B3 Y6 y2 V. @; c+ ~
- if (FLASH_OB_Launch() != FLASH_COMPLETE)
* v* w/ o- w- l1 p' L- \ - {
- J& {! X! N0 T% U - /* Disable the Flash option control register access (recommended to protect
3 [8 K. d) w8 Z8 Q/ ? - the option Bytes against possible unwanted operations) */
, j L# r9 W H* [' q& k" o/ s. h: ] - FLASH_OB_Lock();6 W) ~4 z' n3 L/ Y
- 2 E4 @! ~( }4 z' m! n9 c
- /* Error: Flash read unprotection failed */( K; H: ^3 m$ A( K2 c' K( n
- return (2);. T1 ? P2 I) B$ l
- }3 z C9 l2 J L( z
- - m/ C; w8 Q0 @5 C2 q: r
- /* Disable the Flash option control register access (recommended to protect 5 \0 d$ m1 f; P* C) _& [8 S* f" Q* E4 a8 V
- the option Bytes against possible unwanted operations) */! a9 i1 r% E+ @) p, L5 i2 y
- FLASH_OB_Lock();
$ i# }) Y, x2 m5 @/ T3 B
! M* n# R3 {' }' g. v- /* Read Protection successfully enable *// |+ y F) F8 P- u" @
- return (1);
! `# I) f! Z* P" N$ q" s9 q - }
0 y' o+ T) b0 k- \/ m+ F: z6 |, e - ) K V& B! P$ f2 E8 {) I
- /* Read Protection successfully enable */" a8 U, X5 J8 h; T
- return (1);
" i1 c& L# x x, i9 V - }
- h1 o0 Z# v u. r& D
/ [0 {7 C" @$ U8 E* C& I$ H- /****************************************************************
, H* X5 J4 r- ? - * Function: Flash_DisableReadProtection
- j* d# h! T3 M - * Description: Disable the read protection of user flash area.1 U- Q% Q/ b7 B: g6 b+ j5 t' j2 o
- * Input:$ N9 A- I& f+ z
- * Output:0 E$ [# F! P M# x
- * Return: 1: Read Protection successfully disable8 \$ r q4 _0 ~+ {
- * 2: Error: Flash read unprotection failed
, U+ f$ P& k" W7 _6 A# K - *****************************************************************/* R7 e" Q( F! r' R# D
- uint32_t Flash_DisableReadProtection(void)
* F+ x+ _' {: C' z6 q0 i - {; I' p1 u8 w9 @3 I! T+ [) W4 A
- /* Returns the FLASH Read Protection level. */8 k; n4 O, _6 T4 [! W
- if( FLASH_OB_GetRDP() != RESET )
3 i! p+ X" n( |% J - {/ K* T, t) a1 Y) p+ d
- /* Unlock the Option Bytes */
9 |* ]( y1 p. m: d }9 w& e7 E - FLASH_OB_Unlock();+ U, J) F' [5 b: a/ C+ M, h) d
- * j( B* l4 c: m9 K. ~! {) b9 {
- /* Sets the read protection level. */
5 \6 H2 i" Q/ L- l: F' X1 q8 N - FLASH_OB_RDPConfig(OB_RDP_Level_0);
% e# w. ?8 q; `- S) z; c* O$ i% e - 4 o6 _+ Y7 G8 X2 S/ @4 P8 a
- /* Start the Option Bytes programming process. */
4 i) ^) C7 F5 L6 q - if (FLASH_OB_Launch() != FLASH_COMPLETE)7 |: {, n6 Q( \& M* s
- {6 v( l6 R7 G( n/ Z. l, ? o
- /* Disable the Flash option control register access (recommended to protect ' R& `. M7 t2 y
- the option Bytes against possible unwanted operations) */7 t9 [" x8 J; o
- FLASH_OB_Lock();
3 R" w/ V6 _! O$ d$ r' s - 4 Z w/ f* V- A7 W/ c/ W- l
- /* Error: Flash read unprotection failed */
' [- j7 a8 e- X$ G# |8 W6 X6 a' k - return (2);
" @* W$ q0 @/ Z5 \4 H# r - }
) S# N; x& {& k# b) E -
3 i: |* k! R, w+ T. C - /* Disable the Flash option control register access (recommended to protect 0 P# n' I* D# B( d
- the option Bytes against possible unwanted operations) */- W7 M* M K, C4 d# l8 L
- FLASH_OB_Lock();1 a, O5 P j6 U
/ Y( J2 } O L5 g& |- n" X) V- /* Read Protection successfully disable */& \- `* f2 G: g% \, F, t
- return (1);! T: o/ K8 c+ h A: X
- }
! _' d: g# j+ t3 H7 @+ r" v -
D& l/ v$ n" W9 R! [ - /* Read Protection successfully disable */- I3 @4 _1 |% n# F
- return (1);
T; W- V5 [1 Z4 K+ m - }
复制代码
- @1 _; E: ^* j! W& t V- {
# E; H2 |: X `9 j0 M# f! R6 Z |
是不是如果就这么将LZ提供的代码放进去的话就无法对产品进行软件升级了?9 A1 ~" i* u2 }2 F! A* F
比如说产品早期推出的时候有BUG,后期发现可以通过(例如说)产品接口的串口进行软件升级解决。但是由于进行Flash读保护,因此就无法实施了?
那么楼主提供的代码是属于哪个级别的操作?我觉得已经是级别2了,因为需要防止别人抄里面的程序。
是等级1的,可以通过程序改为等级0,然后Flash中的程序就会被擦除,之后芯片就变成新的了,就可以使用仿真器了。如果把等级设置成2,就没有办法再修改等级了,仿真器接口直接熔断,只能通过boot升级程序了。
其实楼主的代码 根本就是官方的例程提供的 一开始自己都没详细的说明保护的机制和原理
解密要钱的啊,而且还不少,小众产品或者产品中有多芯片协同工作的,破解难度极大,可以交叉验证版本。