STM32F4(Flash读保护)0 I1 i& @! l9 B/ A+ } l) Z+ ~3 z
$ l, F" ` [* K
2 J: E" H1 z. G: U' { A转载来源:STM32F4(Flash读保护)
0 j9 u0 i1 j% V# p1 j5 x3 S1 c" t. q9 q" W5 Z( ^# g3 `% s8 Y
; P2 q2 a; [4 D0 o
1,目的/ i3 J( d" g, K8 `8 O9 o: H
在实际的产品发布中,如果不对储存在单片机Flash中的程序做一些保护的话,就有可能被一些不法公司,通过仿真器(J-Link,ST-Link等)把Flash中的程序读取回来,得到bin文件或hex文件,然后去山寨产品。所以我们需要对程序进行保护,一种比较简单可靠的方法就是把Flash设置成读保护。
+ K4 O& e- p3 K5 d: \
6 ^) \: K$ N; O, D; z- Y/ n2,开发环境; N5 U' i! B% i! \5 s# [4 z
1,适用芯片:STM32F4全部芯片
( D, B7 ^8 [! j" I4 M r2 h 2,固件库:STM32F4xx_DSP_StdPeriph_Lib_V1.8.0
A- [; j ^; a 3,IDE:MDK517
) L2 Y! D1 l$ r0 i0 j3 \1 O$ Y: B! D
3,程序源码
" ]3 e% \0 Q6 U' B& c" ^6 O+ u& q. D/ z) W; r7 m! O
- /****************************************************************
) @, H7 o3 N1 W+ w* X- t - * Function: Flash_EnableReadProtection
0 d+ ?9 d2 b P - * Description: Enable the read protection of user flash area.8 Y* l" E3 \6 O; o
- * Input:% E0 I) }% C1 H$ G( d; R# C6 b8 m
- * Output:
1 M- y7 T& m! H* A - * Return: 1: Read Protection successfully enable
: ?" e, ?4 C8 x9 g3 [ - * 2: Error: Flash read unprotection failed
& E" E* e( |) D# Z! O2 j - *****************************************************************/1 Z( D2 I% z. B( D3 A' N2 B
- uint32_t Flash_EnableReadProtection(void)
& {+ X1 d' O9 u0 ?' i; j - {
/ k2 R$ @2 {' m9 A1 F+ } - /* Returns the FLASH Read Protection level. */
/ N& N2 `( C8 }* I; Z5 \) `" W+ j* r/ C - if( FLASH_OB_GetRDP() == RESET )0 x( L H: `+ \4 J
- {8 Y5 r6 ?5 m; X; Z# M
- /* Unlock the Option Bytes */0 H. j; R$ O' q7 R
- FLASH_OB_Unlock();
! w' X/ ]. h# w: s; h - + Q: ~/ S$ \" y" i6 U5 V1 Z, a
- /* Sets the read protection level. */' t) ]. ]# t+ v/ u, U2 Y! C; _
- FLASH_OB_RDPConfig(OB_RDP_Level_1);( X0 f# h9 X8 U
- " ^8 B6 Y/ q5 s6 ^5 @6 d
- /* Start the Option Bytes programming process. */ ! q9 o/ w' i7 X( B3 w
- if (FLASH_OB_Launch() != FLASH_COMPLETE)
' z2 [$ v* s* y; ~2 i Q" | - {* | H0 u0 s4 W9 l
- /* Disable the Flash option control register access (recommended to protect 2 g" s+ m4 K. L2 d8 [$ i
- the option Bytes against possible unwanted operations) */
/ F5 Q! I' ^% G K - FLASH_OB_Lock();
- ?' {) a) u. `% A/ h - / _ f( Q0 c, R: V. i* i) j5 Q
- /* Error: Flash read unprotection failed */
7 e9 l1 g, Y3 Z& U - return (2);# Z) Q2 J9 O, w4 ]2 \; v
- }+ q" x2 K. [$ O& _4 {
-
1 h* D: l. _/ Z: j% X9 K- F - /* Disable the Flash option control register access (recommended to protect 9 g- J' e( N( l
- the option Bytes against possible unwanted operations) */; e5 K. |* t' E/ a. b+ }
- FLASH_OB_Lock();4 ?6 I" j# }; m: H% s; O
- ' K+ Z6 y. A, J2 z2 O' y5 h
- /* Read Protection successfully enable */
' r. |6 ^4 A8 d' I - return (1);9 \7 `$ n0 t! R
- }
% o& Y! g* f, S1 o& h/ _' { - i6 c4 ]$ h8 g4 N% Y) o# D* r; T& Z
- /* Read Protection successfully enable */* T3 a1 X& P5 X+ b0 ^/ _- R s: _
- return (1);
4 r, u a. ?: d5 |5 D- l - }
! T2 ]1 m' @/ X0 O6 v
5 |* I4 m3 ]3 u5 W- /****************************************************************
5 d% Y1 A; p! D( K - * Function: Flash_DisableReadProtection5 x1 O5 r$ Z4 t/ {
- * Description: Disable the read protection of user flash area.
/ `/ }" x9 R# O q% X ]/ \) z - * Input:( M5 M. Q- Q( `
- * Output:
- t9 K' d* b6 K - * Return: 1: Read Protection successfully disable
% i' X X! [9 ]; | - * 2: Error: Flash read unprotection failed
5 L( r+ m* i2 b2 t5 m! S7 D - *****************************************************************/
7 o, H2 A, u# t- D S - uint32_t Flash_DisableReadProtection(void)
6 M2 E/ m7 w& [0 S2 b - {% M$ V6 t% t4 H$ O9 I3 o* a
- /* Returns the FLASH Read Protection level. */
s) B% g5 E* ^7 R) }5 C4 a$ Y" M, T# ~ - if( FLASH_OB_GetRDP() != RESET )
/ G* r6 r. F# U1 B; B - {
9 d$ S1 g5 _7 n# u0 ~. f }# } - /* Unlock the Option Bytes */2 e9 `; a4 O& ]
- FLASH_OB_Unlock();3 t. M9 a+ v# D$ `8 ^' W& m0 d
- ; H7 I+ ~" h. I- H9 I# K
- /* Sets the read protection level. */
5 v: s- |: h9 h- H- J1 d8 e - FLASH_OB_RDPConfig(OB_RDP_Level_0);* J; I1 H3 V1 k9 L# {* `0 U9 ^
-
7 s: b; y( S- ?4 B+ p, c* X - /* Start the Option Bytes programming process. */ 1 D3 X0 U+ J5 h+ s7 b+ \0 A# P f
- if (FLASH_OB_Launch() != FLASH_COMPLETE)2 C: c5 Z2 l2 g, o2 Q
- {
& W* H8 U! c/ F2 d8 i8 P9 X f - /* Disable the Flash option control register access (recommended to protect
# o. ^, o. R0 ] - the option Bytes against possible unwanted operations) */- L: K- v; I7 e: p
- FLASH_OB_Lock();
8 {* m+ x M/ z `6 y+ U0 ^ - ) G: @6 K2 a: t+ Z
- /* Error: Flash read unprotection failed */. [5 Y$ ~# P; p
- return (2);8 x* ~. m$ V0 U& Y( R5 R
- }
3 A" {% P+ f0 J- D; p4 i -
; J' c% ]9 d3 y9 |3 F - /* Disable the Flash option control register access (recommended to protect , n/ M2 o# f z0 _' Z
- the option Bytes against possible unwanted operations) */# ?" e- t. K' U4 R
- FLASH_OB_Lock();
! J* a( N# r) U
8 t6 \, @5 [0 z- /* Read Protection successfully disable */
/ m6 K- o, T- S8 r+ W! I! v+ z - return (1);" j$ i1 `; w2 Z1 G1 m8 `4 M
- }
" C/ v8 f6 D; g -
& i C! R: B, Y( S* W1 \ - /* Read Protection successfully disable */4 Q+ W& n$ _! G3 z; z, V
- return (1);2 _5 Q2 a3 u& `: Y
- }
复制代码
, u7 m! Y6 Z# P! j# V$ s( P* j5 I
: U; K4 W$ g9 Z/ w1 q \& g) X |
是不是如果就这么将LZ提供的代码放进去的话就无法对产品进行软件升级了?) ?4 K9 Y! a: p# H- R; ^6 q, C
比如说产品早期推出的时候有BUG,后期发现可以通过(例如说)产品接口的串口进行软件升级解决。但是由于进行Flash读保护,因此就无法实施了?
那么楼主提供的代码是属于哪个级别的操作?我觉得已经是级别2了,因为需要防止别人抄里面的程序。
是等级1的,可以通过程序改为等级0,然后Flash中的程序就会被擦除,之后芯片就变成新的了,就可以使用仿真器了。如果把等级设置成2,就没有办法再修改等级了,仿真器接口直接熔断,只能通过boot升级程序了。
" c9 n$ d; u2 _- l
其实楼主的代码 根本就是官方的例程提供的 一开始自己都没详细的说明保护的机制和原理
解密要钱的啊,而且还不少,小众产品或者产品中有多芯片协同工作的,破解难度极大,可以交叉验证版本。