|
STM32F4(Flash读保护)% N; }' B% `* B! Z
# r; E4 y' x2 C% i" y0 }; ~1 Y" b2 y/ `6 k# ^% o4 m
转载来源:STM32F4(Flash读保护); }7 m$ u- m$ j- c2 w2 P2 ?
8 E g4 J! ]6 ^/ y$ e% P/ ^& t# v" y+ `( e2 G v( s) {% @
1,目的
6 E2 ]/ H% U' [& V* V- n+ B } 在实际的产品发布中,如果不对储存在单片机Flash中的程序做一些保护的话,就有可能被一些不法公司,通过仿真器(J-Link,ST-Link等)把Flash中的程序读取回来,得到bin文件或hex文件,然后去山寨产品。所以我们需要对程序进行保护,一种比较简单可靠的方法就是把Flash设置成读保护。2 R* @7 {' U! n3 y9 ]
6 Q, m; ?; B' `2,开发环境
+ w! K! u; B/ p; x. V/ x; S 1,适用芯片:STM32F4全部芯片& ]* p# w% y& b) y/ o3 [
2,固件库:STM32F4xx_DSP_StdPeriph_Lib_V1.8.0) A- O) V! y1 E R; i7 P
3,IDE:MDK517
- l3 r5 `* k7 A" ]/ I4 x/ @ X; h* k) P! ^% Z1 f2 p' M
3,程序源码
; y; ~) F: s3 n/ c! ?6 r8 u
7 N/ V' l! c5 j! K- X; T" F- /****************************************************************
7 v; A1 o. g8 _- P* E; E - * Function: Flash_EnableReadProtection
& |4 f2 y3 P1 J2 |7 Z# g5 z - * Description: Enable the read protection of user flash area.3 I6 B( B1 i1 E: `6 b9 |
- * Input:. y, F" E+ G6 f: v3 I2 {
- * Output:- y" l6 n8 e* r
- * Return: 1: Read Protection successfully enable2 ?) y, ]/ Y/ D
- * 2: Error: Flash read unprotection failed! h3 {. ~* n. d! f( U T5 G5 M
- *****************************************************************/
7 L8 s, D1 ]" v5 h. a4 j2 H# _4 S. F/ ? - uint32_t Flash_EnableReadProtection(void)
: f: `, }7 }9 g; D. ^. B - {
* I( G+ C) d+ R6 w8 u0 e- q - /* Returns the FLASH Read Protection level. */
1 [" e- j* t1 l- J \' g8 w - if( FLASH_OB_GetRDP() == RESET )4 {4 s1 n2 n, C6 k
- {
) b1 \. e2 M) z8 I/ m3 r5 Y" w - /* Unlock the Option Bytes */
5 ~5 {; \- h: f! G: F/ u& Z2 Q2 Z - FLASH_OB_Unlock();' T# W9 v8 I* c! G$ I, W# \4 w* }) v# T
- ( e+ z4 o' Z* s0 [$ U+ _( V
- /* Sets the read protection level. */) G- q6 w' e0 q- R
- FLASH_OB_RDPConfig(OB_RDP_Level_1);
" T9 @& c. }2 G - 2 _7 O4 p- {% r: R
- /* Start the Option Bytes programming process. */
$ D7 A" c2 a( { - if (FLASH_OB_Launch() != FLASH_COMPLETE). G. I9 e8 M% S4 e2 h* z0 w3 L5 b
- {) I$ Q# X& @ y) h
- /* Disable the Flash option control register access (recommended to protect
- Q: Z6 N! ?! k, W N6 u2 P - the option Bytes against possible unwanted operations) */2 I3 F. W6 c1 u. ^/ ^
- FLASH_OB_Lock();; H/ W5 x7 f- \3 }) A$ Y: b# _
- 9 n* ^. |8 i1 k1 t! r' t. M
- /* Error: Flash read unprotection failed */% D6 a0 F) n& b3 u: D0 Q+ K
- return (2);! B. l! W7 m& G- ?
- }, S* L2 E5 e* }* n1 t- O4 z
- 1 R p1 q% |( r" t
- /* Disable the Flash option control register access (recommended to protect : p, |: i4 y: D& |6 i8 W
- the option Bytes against possible unwanted operations) */" ` l) C1 ]/ b" G: J* l' W
- FLASH_OB_Lock();
) P) G" W& ?: E. M+ N$ f - % U) k! ?! Y4 B2 y+ e
- /* Read Protection successfully enable */
9 B* W' X. i ~4 g Y7 O2 q - return (1);
0 [; _- v5 Q+ I - }& n9 f$ A$ q3 t) G0 Z
- 9 Q& _# F# H' D8 L
- /* Read Protection successfully enable */3 O, v8 [: e2 u. ?" X; i
- return (1);* [- Y. G' t0 Z+ I
- }
: y7 ]( O) V. k8 Y$ n: e# Y# A( A- u
5 o' Q9 X4 X4 |& f- /****************************************************************
, f/ P+ J: S1 k5 w0 P - * Function: Flash_DisableReadProtection3 H* A5 T( n$ a8 r
- * Description: Disable the read protection of user flash area.+ w& l/ s) N5 u* P1 C
- * Input:
- _+ ?; B2 x( q$ B0 n! G* ^ - * Output:6 f2 E1 a' Q0 T& }
- * Return: 1: Read Protection successfully disable
7 J# C- s% e" f# } - * 2: Error: Flash read unprotection failed
8 t b! F; Y% J: ?. O0 h( J. p' T - *****************************************************************/6 D, X* F( s# u
- uint32_t Flash_DisableReadProtection(void)
* Z5 g! j* J8 Z I - {
4 B7 {+ `' K' U* k! O - /* Returns the FLASH Read Protection level. */1 b7 n% v$ I7 i n3 V2 u$ v J' I
- if( FLASH_OB_GetRDP() != RESET )
. l. u0 J& Q; [; H - {
& E8 X9 `$ N h/ ?( s I - /* Unlock the Option Bytes */7 |; Y6 i* F. @. t! u, a
- FLASH_OB_Unlock();( b4 d' v1 x+ a
-
: C) [$ K, Y' G% q! N - /* Sets the read protection level. */
1 F0 u0 f: ?+ h" c - FLASH_OB_RDPConfig(OB_RDP_Level_0);, r0 S+ D& i1 o$ z1 W+ J) Y" d
- ' Q2 a* A7 N$ B0 a) k
- /* Start the Option Bytes programming process. */
1 V. X! }& ]& x! {0 ]; o7 t8 Z - if (FLASH_OB_Launch() != FLASH_COMPLETE)
) I4 w1 f1 k% V$ w' J% m( i - { W0 \% \4 w3 J2 b9 ~0 Q
- /* Disable the Flash option control register access (recommended to protect , q% r9 T7 G4 _
- the option Bytes against possible unwanted operations) */: u. W2 v" @, r( U) {0 E
- FLASH_OB_Lock();
9 ~% H0 d5 [$ ^. { -
$ O& F, N6 m3 [ - /* Error: Flash read unprotection failed */
' ~' x* E1 Q& @$ G0 i# Y - return (2);
8 G6 N( S$ d0 o/ T" Z - }. _- H+ R, Q$ p' o" ^" {$ ?2 p0 I+ j
- . x5 Z6 o+ H+ N
- /* Disable the Flash option control register access (recommended to protect
, o+ F9 } S) Q. ?$ D8 s - the option Bytes against possible unwanted operations) */
3 b- j' i& j7 P$ @ W& R" ~ - FLASH_OB_Lock();
Z) T! R$ e8 T) k6 L- n, Q3 ^, ^ - ' Q _1 E5 D3 b+ \& O
- /* Read Protection successfully disable */
% Y! b+ Y+ o4 P; g0 E& t1 ? - return (1);
8 P, m1 t8 Q# ^4 u1 f( u5 ]7 ^ - }
, V8 T& q) M: M+ c1 s - : s' `+ l, l. F1 Y( ]+ J) Z
- /* Read Protection successfully disable */, H" d0 O9 }. M1 k) E/ F
- return (1);4 g1 J" l: r. I8 S( Z
- }
复制代码
# U& Y" ?( _) s
2 B, M, }) S" G# }( g a |
是不是如果就这么将LZ提供的代码放进去的话就无法对产品进行软件升级了?, J% _& N* @5 c5 ?+ S1 C& z6 A) k
比如说产品早期推出的时候有BUG,后期发现可以通过(例如说)产品接口的串口进行软件升级解决。但是由于进行Flash读保护,因此就无法实施了?
那么楼主提供的代码是属于哪个级别的操作?我觉得已经是级别2了,因为需要防止别人抄里面的程序。
是等级1的,可以通过程序改为等级0,然后Flash中的程序就会被擦除,之后芯片就变成新的了,就可以使用仿真器了。如果把等级设置成2,就没有办法再修改等级了,仿真器接口直接熔断,只能通过boot升级程序了。
其实楼主的代码 根本就是官方的例程提供的 一开始自己都没详细的说明保护的机制和原理
解密要钱的啊,而且还不少,小众产品或者产品中有多芯片协同工作的,破解难度极大,可以交叉验证版本。