STM32F4(Flash读保护)* W% o1 M! Y! B' E% v2 O
; O1 f& r( a% E2 D3 i- T1 n7 @. J1 |
4 b% m# {$ b5 M1 V
转载来源:STM32F4(Flash读保护)
, L) m7 U1 K5 L7 W& g$ Q, Y7 C! u2 R/ O8 T" i
5 p5 T- }& R' d! g( g& d1,目的7 V" E' @% z* n" } S6 J4 z5 Y6 z
在实际的产品发布中,如果不对储存在单片机Flash中的程序做一些保护的话,就有可能被一些不法公司,通过仿真器(J-Link,ST-Link等)把Flash中的程序读取回来,得到bin文件或hex文件,然后去山寨产品。所以我们需要对程序进行保护,一种比较简单可靠的方法就是把Flash设置成读保护。
* B/ W% a( L8 h, V5 t4 v
/ j# A* J+ F% f$ A3 j, o6 W6 _6 i2,开发环境& w6 s* B4 L1 q- R( N
1,适用芯片:STM32F4全部芯片; j% U/ `9 V, W( |6 a/ g1 H- V
2,固件库:STM32F4xx_DSP_StdPeriph_Lib_V1.8.0& x' h! Q; f0 @
3,IDE:MDK517& R- j6 o. [& ?) m" `
0 p& I/ G9 E2 x: Q; [; z3,程序源码& z3 L: j/ X( G! F2 I& ^) i
7 ~! [* x8 |' C/ d/ F9 S* n- /****************************************************************' z0 r- a0 v& ]* F# a. ^# {; n# z
- * Function: Flash_EnableReadProtection
" k0 b, @5 l7 V/ K - * Description: Enable the read protection of user flash area.
: A" ?& X' k' X. o9 ? - * Input:; g' U) P# P) n( S# ^
- * Output: k3 _ Y8 z q/ S3 d$ A
- * Return: 1: Read Protection successfully enable* ^6 X6 z# I8 `) E' g* N B
- * 2: Error: Flash read unprotection failed5 W5 r) j( G; o4 ^( |* B& i
- *****************************************************************/
5 V4 W$ I* V! \: t+ a - uint32_t Flash_EnableReadProtection(void)5 p4 q( h) v6 y0 }
- {
; O, I8 a* A d# {# v5 A( ?! t - /* Returns the FLASH Read Protection level. */
# \- u: D8 w6 [: ~& v - if( FLASH_OB_GetRDP() == RESET )
1 M+ X! ]9 c0 U9 ]' _) s - {: E+ O% A# M0 l8 I( O
- /* Unlock the Option Bytes */; V8 `0 I# J& f
- FLASH_OB_Unlock();
8 ?' E+ s4 o/ M" `4 y0 \! g -
+ J; |, g% ?3 c$ T5 @( S - /* Sets the read protection level. */+ H r- P! d4 g v( A
- FLASH_OB_RDPConfig(OB_RDP_Level_1);" h: {+ v6 _9 i. i. I- r
- 2 Z" v) j0 P0 z% C: H7 H! e! q* y
- /* Start the Option Bytes programming process. */
+ F% M2 {1 W" w+ \8 E( y4 ~ - if (FLASH_OB_Launch() != FLASH_COMPLETE)2 U* v5 o8 ~& A! |+ j
- {) P) q- ^% f/ O' S
- /* Disable the Flash option control register access (recommended to protect
* i. ? B$ \9 W - the option Bytes against possible unwanted operations) */6 }' @5 g% V. u% h; V9 y
- FLASH_OB_Lock();
@8 s u q* R4 }; D! B - , \4 `/ {; y% U$ \9 _" Q1 x7 V
- /* Error: Flash read unprotection failed */8 Q- |) v6 G) J" P9 q( V% U3 Z8 \
- return (2);0 Y9 Q0 Q1 W1 B
- }
6 V, q7 A" p* X& ^. D& W, ~) k6 g( k8 Y -
$ s h. {8 Z ]6 X0 p8 @6 |4 V, u - /* Disable the Flash option control register access (recommended to protect $ c* A1 w2 @8 W3 V, `1 F G, T
- the option Bytes against possible unwanted operations) */" \5 n# t( R; n
- FLASH_OB_Lock();
/ L6 t9 F5 _/ H0 N
1 L( g& t, p+ K4 o$ S' Q- /* Read Protection successfully enable */
6 _ y3 ~1 R5 E' s - return (1);! h8 m$ R: R; ?% _$ m- m [
- }
- t4 {# V V$ G$ H# S# X - 4 S6 j2 J" y0 F I
- /* Read Protection successfully enable */
) ?+ O( B! V, g - return (1);; @" @: l/ U, s% ?" f( I6 e" r
- }
/ h$ T% t8 J) |. O - ( ~" @1 t. A& t: @% j* b
- /****************************************************************0 D% F" F% A7 \# B6 F) C) Q
- * Function: Flash_DisableReadProtection
; g, L* R x9 [' X# { - * Description: Disable the read protection of user flash area.- F3 M/ H% r0 U" V/ Q$ w; H
- * Input:
/ K: ^0 z7 H9 n$ @6 t, S: b - * Output:
. h, M A1 B ]/ d/ \" d- G: q - * Return: 1: Read Protection successfully disable$ w5 z& }, q/ c7 \3 T' n
- * 2: Error: Flash read unprotection failed9 u( d3 x( `/ y# }2 _
- *****************************************************************/1 u7 v. u* f5 s9 j
- uint32_t Flash_DisableReadProtection(void)5 p N( O0 c4 c3 B$ P
- {
5 t9 a! J( b' G m3 I - /* Returns the FLASH Read Protection level. */
. H3 W0 ]5 N) k5 Y& {- R% \ - if( FLASH_OB_GetRDP() != RESET )$ V6 ?1 O- P$ I. m
- {
$ ]9 l' f6 s6 e& i - /* Unlock the Option Bytes */0 w/ s+ C1 L6 f2 [# d
- FLASH_OB_Unlock();
/ A, Y# V2 W0 v( F7 S1 a8 } - [- h' Z0 i+ F9 N: m' I* L7 j1 z, X
- /* Sets the read protection level. */8 C' B, e" o2 A2 x
- FLASH_OB_RDPConfig(OB_RDP_Level_0);+ Z9 N7 V4 n4 Y, f
- * p$ t) f9 N3 C h0 I% ]
- /* Start the Option Bytes programming process. */
. W$ I3 s3 f/ z7 t! F7 W - if (FLASH_OB_Launch() != FLASH_COMPLETE)0 X* q2 J0 B f5 u
- {9 U$ d" A+ S0 E+ R. i
- /* Disable the Flash option control register access (recommended to protect ' E! Y6 W2 w. S
- the option Bytes against possible unwanted operations) */
3 N1 R9 S$ y( v4 b - FLASH_OB_Lock();
8 p" f; c5 b& k" L; S$ Z; a -
7 k& w9 _; i: ?5 u; \ - /* Error: Flash read unprotection failed */
: s- o4 |4 H$ F6 f) d - return (2);
) }8 A& L3 n4 `: `7 s2 _7 N* K - }- I ~0 ?$ }3 s6 U. L
- * k' D# K! ]" `: w
- /* Disable the Flash option control register access (recommended to protect
: [8 r6 B. W: [ W8 z - the option Bytes against possible unwanted operations) */
. A' E$ x7 W1 z- P( e+ a: L - FLASH_OB_Lock();
) v6 B) i* l8 c% h1 f% C' C' T - . M* F; c4 w. y: T; _; f
- /* Read Protection successfully disable */
1 |8 @3 m) v. _5 ]7 l" A9 Z - return (1);
, @0 l% q+ j) |: e- U4 ^7 S3 W" ] - }1 \' H0 l* Y; p& x7 u& C: [
- - f# p1 x: E F# \" k& c
- /* Read Protection successfully disable */8 }6 _# ?$ P F
- return (1);
' @0 K; I4 p* I Y6 d: z- | - }
复制代码 $ T: D) h6 [0 K. [% @
! E+ u, v% z$ o. W |
是不是如果就这么将LZ提供的代码放进去的话就无法对产品进行软件升级了?4 h, P" l+ P. }" d- t# e* W
比如说产品早期推出的时候有BUG,后期发现可以通过(例如说)产品接口的串口进行软件升级解决。但是由于进行Flash读保护,因此就无法实施了?
那么楼主提供的代码是属于哪个级别的操作?我觉得已经是级别2了,因为需要防止别人抄里面的程序。
是等级1的,可以通过程序改为等级0,然后Flash中的程序就会被擦除,之后芯片就变成新的了,就可以使用仿真器了。如果把等级设置成2,就没有办法再修改等级了,仿真器接口直接熔断,只能通过boot升级程序了。
+ j9 a r4 E: l6 P
其实楼主的代码 根本就是官方的例程提供的 一开始自己都没详细的说明保护的机制和原理
解密要钱的啊,而且还不少,小众产品或者产品中有多芯片协同工作的,破解难度极大,可以交叉验证版本。