|
STM32F4(Flash读保护)
/ Q V4 s* z2 n: W : D( i1 z/ K; x3 n
! t# E" L6 |- x0 u; y. t转载来源:STM32F4(Flash读保护)
8 S3 F! o1 ~, t ]1 U4 a7 v6 {1 d3 g( [5 A: F0 m
, M! k( T# ^9 b4 c0 V# Y
1,目的
0 @( S6 k( v, K4 S/ y 在实际的产品发布中,如果不对储存在单片机Flash中的程序做一些保护的话,就有可能被一些不法公司,通过仿真器(J-Link,ST-Link等)把Flash中的程序读取回来,得到bin文件或hex文件,然后去山寨产品。所以我们需要对程序进行保护,一种比较简单可靠的方法就是把Flash设置成读保护。# P5 s& @, P, l: y" e' G. ?
A* `6 X9 w& Z, Y# |2,开发环境
1 k* E3 p% F8 D' K! K 1,适用芯片:STM32F4全部芯片! u e5 K7 V7 S0 ?$ m# s
2,固件库:STM32F4xx_DSP_StdPeriph_Lib_V1.8.0
, [$ f) ^1 R( L& P+ D! f2 f- ]) h 3,IDE:MDK517
5 T7 \9 I8 P6 j) G* _; H1 p8 O z
4 Y( Z# ?4 r! ~4 e! _$ f3,程序源码) B$ T- w+ L) [
! v2 ?3 j/ ~9 W& E: z
- /****************************************************************
+ ~7 r. }( `: T( n3 U6 M& T4 f - * Function: Flash_EnableReadProtection
- M/ l- d9 n3 t9 \1 F: i2 Z - * Description: Enable the read protection of user flash area.2 q% J/ H3 A- a
- * Input:
; ~$ Z4 e- N0 q8 B - * Output:$ W M: O! j Y# `
- * Return: 1: Read Protection successfully enable
4 E# R6 j8 H/ i2 k - * 2: Error: Flash read unprotection failed
1 n& B; w# K' b" Z6 f, j - *****************************************************************/
0 B2 K: B5 Y' u6 Y+ a3 U( N. j - uint32_t Flash_EnableReadProtection(void). Z4 U. I' O2 r7 x4 l- q
- {
" {& @! u! `1 F. x- m, ^ K - /* Returns the FLASH Read Protection level. */
: a4 b+ k+ A3 _6 l+ ], O4 S3 R - if( FLASH_OB_GetRDP() == RESET )
8 B1 p( M) S# |6 i - { m5 \% O e0 t
- /* Unlock the Option Bytes */
, {3 Q" B( d0 B1 o2 C) A2 `- l6 T - FLASH_OB_Unlock();
) w3 Q7 W: w4 i; ~ - . C( k2 q/ D6 h7 v4 k
- /* Sets the read protection level. */
/ ?0 U: g0 A" ~% V3 F: X - FLASH_OB_RDPConfig(OB_RDP_Level_1);
# Z! l& ~" a: S -
5 E6 S( ~5 J. y/ {# R" k' t - /* Start the Option Bytes programming process. */ $ |6 l* M& B% l" Q6 i8 [
- if (FLASH_OB_Launch() != FLASH_COMPLETE)1 o% x9 d* s4 t0 a
- {) |6 G3 v w2 y4 y% N
- /* Disable the Flash option control register access (recommended to protect % b$ u: r4 S, O* }$ E
- the option Bytes against possible unwanted operations) */
0 U! e) _4 H1 V1 h; v7 L2 F - FLASH_OB_Lock();
/ D1 ~ x, ~0 p3 K - 2 E. z5 D( |% U/ G+ @
- /* Error: Flash read unprotection failed */# [* b. T# b- g$ o
- return (2);
5 X4 O6 r6 u+ H7 `( P - }- e& u0 ?! |7 W; z! A" p' a8 {6 E
- 6 S+ ^& t/ s2 V6 Y) @) Z; c4 p
- /* Disable the Flash option control register access (recommended to protect
" x. m. p: U" a8 Y - the option Bytes against possible unwanted operations) */9 B; s! e2 U {) U1 \) `
- FLASH_OB_Lock();# h% K$ X: }8 j0 g. M2 e( q
8 d1 W$ E+ [ n: `+ y1 a3 k+ k ^- /* Read Protection successfully enable */7 J! I. {& ]8 {5 @/ E0 U
- return (1);
5 }" A. O$ I$ `2 x - }
8 k' S3 c& ^7 {( w' ?, C -
) p: _# H9 s' Z, d - /* Read Protection successfully enable */0 Z1 `, f) O6 z3 _4 @/ e. H
- return (1);
: e$ ]3 a6 u+ u/ O, } - }
0 C4 s. B' }9 I) o
9 I2 K+ ^6 n3 f; |' H, I& i) K3 o- /****************************************************************
$ C+ {# G& h9 ]4 Z) G7 } - * Function: Flash_DisableReadProtection
! f: `) L2 _0 p; S0 f - * Description: Disable the read protection of user flash area.
2 E" u( I3 X$ e7 w! i2 n* C - * Input:
3 P0 b- ~$ X9 ^: { - * Output:
8 V/ S; w E. I: t) t/ N - * Return: 1: Read Protection successfully disable
4 V( Y; G G8 a+ v - * 2: Error: Flash read unprotection failed
) V3 s1 Y2 @, B) V0 C* Y - *****************************************************************/
% {/ F2 S8 K- S, N0 s - uint32_t Flash_DisableReadProtection(void)
% ~( X) i9 T' V/ C7 | - {
0 u1 f5 g4 C5 c3 s% J - /* Returns the FLASH Read Protection level. */
^; u5 y$ h* r - if( FLASH_OB_GetRDP() != RESET )
' y3 y( C& N4 V% U0 J - {! a+ R; K! z! S' m2 d0 }! m
- /* Unlock the Option Bytes */
+ Q5 S7 Z7 F' ^" Q( h - FLASH_OB_Unlock();
P+ i$ B: I. V - & {- f* o$ t4 P# f5 f ^7 n$ z
- /* Sets the read protection level. */- O: a, m! _& D: _" D7 |" e; g+ [
- FLASH_OB_RDPConfig(OB_RDP_Level_0);) C* F) [, Y: e" X' h3 y) x2 u
-
8 _; t! X Z- [3 y1 u4 z+ O$ ` - /* Start the Option Bytes programming process. */
# F5 l& F3 M, [7 [8 @( D - if (FLASH_OB_Launch() != FLASH_COMPLETE)
5 h6 h6 O0 G; N! _ m - {
4 B$ `; ?, m# U - /* Disable the Flash option control register access (recommended to protect
5 ]& [& d4 Z& A: R6 W - the option Bytes against possible unwanted operations) */
]+ ]! A. M4 m5 h - FLASH_OB_Lock();
7 |2 a y1 i% G- ^% r8 C -
. J' z3 E8 u7 n# ^ - /* Error: Flash read unprotection failed */
( i5 t" H7 W) k+ x" E% U5 A; S - return (2);4 {6 F% u4 i2 x# {) z
- }' m2 I# [4 Z/ P; }1 l
-
9 z$ {5 k; f9 u - /* Disable the Flash option control register access (recommended to protect 9 a6 J8 V; `8 D; P4 J0 \2 Z
- the option Bytes against possible unwanted operations) */
8 n, S0 n* d0 L$ _; X - FLASH_OB_Lock();
9 Y& B6 c+ z. H* w$ M& A - t- _, r6 k- Y5 J4 _
- /* Read Protection successfully disable */6 ^* _* [3 Z5 a1 A! I. ?! G2 \
- return (1);2 r7 W- d( w- x, e+ b+ w0 m
- }8 x0 {8 k- [$ Q+ p: x
- $ ? l" i* \3 T% J F. K
- /* Read Protection successfully disable */+ k' i( V# _$ `( C: S8 Y
- return (1);7 p& }* `7 U: ?
- }
复制代码
( G* W9 s5 ]7 G. D' Z
. S* ~( Q" c9 }2 z3 Z: K! f |
是不是如果就这么将LZ提供的代码放进去的话就无法对产品进行软件升级了?3 S! S* s+ C# ]1 I. g+ o
比如说产品早期推出的时候有BUG,后期发现可以通过(例如说)产品接口的串口进行软件升级解决。但是由于进行Flash读保护,因此就无法实施了?
那么楼主提供的代码是属于哪个级别的操作?我觉得已经是级别2了,因为需要防止别人抄里面的程序。
是等级1的,可以通过程序改为等级0,然后Flash中的程序就会被擦除,之后芯片就变成新的了,就可以使用仿真器了。如果把等级设置成2,就没有办法再修改等级了,仿真器接口直接熔断,只能通过boot升级程序了。
! _3 u1 K+ w# S. v9 b2 z" M/ X
其实楼主的代码 根本就是官方的例程提供的 一开始自己都没详细的说明保护的机制和原理
解密要钱的啊,而且还不少,小众产品或者产品中有多芯片协同工作的,破解难度极大,可以交叉验证版本。