STM32F4(Flash读保护)
, y3 h) a$ z# B% f3 y1 p 7 E4 g! a' }. {$ ^! x/ S
4 n( C" j. Z4 [0 v' {2 \7 n$ f4 @
转载来源:STM32F4(Flash读保护)/ ]1 O' c+ B% G/ I k1 \# {! |
# @! Y) t3 k }1 o' |& h
. {1 {2 i$ S3 _9 @
1,目的6 p5 s5 F$ S5 b4 s9 V
在实际的产品发布中,如果不对储存在单片机Flash中的程序做一些保护的话,就有可能被一些不法公司,通过仿真器(J-Link,ST-Link等)把Flash中的程序读取回来,得到bin文件或hex文件,然后去山寨产品。所以我们需要对程序进行保护,一种比较简单可靠的方法就是把Flash设置成读保护。/ X9 M; K, F& n, r9 I) Y" T
9 ~/ Y' z. ^+ k2 X# l
2,开发环境8 |/ _$ ?2 C1 t8 V. T) U+ H. W
1,适用芯片:STM32F4全部芯片0 ~8 ?' c) t4 y" D9 t- @. D1 u
2,固件库:STM32F4xx_DSP_StdPeriph_Lib_V1.8.0
$ t+ I& v" {2 `* |1 u 3,IDE:MDK517
: F4 L2 l) t# t; N+ H- s l7 x. A. u7 I; t' s$ C
3,程序源码% ~% N) a3 A+ ?
) s( T# x; x- n) P! r" S, X* J: F- /****************************************************************; m6 V4 K4 ^" F Y) N& P3 V( @
- * Function: Flash_EnableReadProtection
! R: G8 }3 b: n* S) _: F - * Description: Enable the read protection of user flash area.
5 E; V* t4 F6 v8 S1 b, |& z! ~$ p - * Input:; N! E; L# R6 T
- * Output:
% I- k4 ^3 i' M, L* w- f - * Return: 1: Read Protection successfully enable
1 |) b6 K0 `7 r3 _; f, K; Y9 g/ O - * 2: Error: Flash read unprotection failed- d8 U; l9 _; p8 K
- *****************************************************************/
! n5 ^0 @: ~7 G" w: q' p. p$ z - uint32_t Flash_EnableReadProtection(void)
9 [6 Z- {, V2 N: W; u; _" c - {
9 t" A# J4 U+ P8 |1 j+ q - /* Returns the FLASH Read Protection level. */
0 P( h+ c4 g, O# h }1 y2 T - if( FLASH_OB_GetRDP() == RESET )
1 L3 Z( J1 Q: ~8 g( g [9 Z) n - {
# P% z( X+ w! N# g) [0 Q - /* Unlock the Option Bytes */
% J8 r- y/ v5 l- O2 J3 M - FLASH_OB_Unlock();
: e% k& F& [8 U; B -
8 E$ I. v; h) m% I7 d - /* Sets the read protection level. */
9 { a- r; k% Q- C9 z - FLASH_OB_RDPConfig(OB_RDP_Level_1);
: _9 }9 o5 W; h - 8 G( j: z$ a1 t9 m
- /* Start the Option Bytes programming process. */
/ P( _( N9 R: v0 q - if (FLASH_OB_Launch() != FLASH_COMPLETE)( y. R( i: d: I: }/ O, N8 E
- {
& Z! w5 p$ N) k3 S - /* Disable the Flash option control register access (recommended to protect 8 v" \) c7 i. A3 V; w
- the option Bytes against possible unwanted operations) */: ? L/ W U: l% @: E# @' }
- FLASH_OB_Lock();
. s& ^% [0 F" S3 s$ ? - - u# a9 ?7 B# |7 c/ N3 v! a
- /* Error: Flash read unprotection failed */4 }- Z6 \2 p! z+ c3 T0 G; M
- return (2);
& B5 B" C0 y, v3 Y0 O - }0 m: \9 {) J% J; N: u, w
-
5 |& ^5 u) l2 H6 p- ~1 I* f - /* Disable the Flash option control register access (recommended to protect 5 d. f0 f: R% C* c Y
- the option Bytes against possible unwanted operations) */
8 p" ]/ S( C9 i ` - FLASH_OB_Lock();* g! w$ z9 M$ ]1 N& o2 p3 e) l
" E! ~0 @/ u3 o- /* Read Protection successfully enable */
( u4 H2 d8 @* x# r: u - return (1);
9 q; Y4 U0 L6 A6 z! `3 Q. J. K - }: ?# q% Z8 B7 J
-
7 s+ B+ E1 n7 _9 Z1 f0 ^- o - /* Read Protection successfully enable */' G( L1 j2 y. P
- return (1);6 V/ K) u' [# F2 I2 x) x& ?+ P, u5 r3 t
- }. p0 ^4 Q Z4 t k0 J
- , g& a# [0 q# M* [. e
- /****************************************************************
2 T: k$ A" o8 u: u+ ?# a - * Function: Flash_DisableReadProtection/ D$ j! T* ~; \
- * Description: Disable the read protection of user flash area.7 l: K6 P0 K- C+ l( \! ~9 r1 }9 N
- * Input:0 k# U! z: o9 p) G* Y, m
- * Output:6 e3 L3 J7 B# y! B/ B# Y
- * Return: 1: Read Protection successfully disable
" g, Z& e2 n. u: A - * 2: Error: Flash read unprotection failed4 I( Y! H* i' T0 ~1 A1 L
- *****************************************************************/) D" z: Z5 F y" N0 x0 E
- uint32_t Flash_DisableReadProtection(void)
& k; m2 R* K' ~! Z( f - {
g* b0 |" D0 X. f) Z - /* Returns the FLASH Read Protection level. */: T, e. O% H$ R n; z
- if( FLASH_OB_GetRDP() != RESET )3 v' v {+ P, L6 B6 h8 j( V9 C
- {* I7 E, M: u' I1 x
- /* Unlock the Option Bytes */ s8 P, R: O( m2 R
- FLASH_OB_Unlock();( x3 W/ e1 W6 J, z/ j
-
5 K1 ?9 }/ |" q' U9 J p - /* Sets the read protection level. */) E2 i2 }3 C: p3 f, d
- FLASH_OB_RDPConfig(OB_RDP_Level_0);
/ f/ B1 b- @4 `3 ~4 G# w$ ^ - * x: `+ g4 P9 D8 h! g
- /* Start the Option Bytes programming process. */ ) G: U5 ]) M1 M/ `1 H w4 n3 l8 \" ^
- if (FLASH_OB_Launch() != FLASH_COMPLETE)
; C) `6 K- e5 d% t7 J - {
) h5 e% a2 F$ j! b - /* Disable the Flash option control register access (recommended to protect 1 M' L- g2 l4 z1 H e3 c6 p
- the option Bytes against possible unwanted operations) */
8 ~6 f# z* `* m- I$ g - FLASH_OB_Lock();
) T. ~9 ^/ x# I - ; L) c, W n4 \
- /* Error: Flash read unprotection failed *// }6 A; B4 B( @/ V% n* {3 G* |, ^2 X5 @
- return (2);
, L4 l# r3 U/ Q U" U - }
4 h2 o/ ^2 k, y4 Y* g+ \% D) i- @$ j - 5 U \5 ~1 a: C8 S0 d$ z! f
- /* Disable the Flash option control register access (recommended to protect
- a; `; s( k9 s; h2 y - the option Bytes against possible unwanted operations) */9 I5 Q+ }) a: @5 u
- FLASH_OB_Lock();: T/ _7 @- \* d4 ~* S6 y
- % H) ]# j8 Q! R. G. U
- /* Read Protection successfully disable */
' d$ p$ F2 M6 [2 J - return (1);# O, H1 X6 X# N
- }
; @" Y* y$ U) @$ K9 ?" h - % B% o6 _; x7 X/ z# b7 N1 D
- /* Read Protection successfully disable */5 F+ v" W: N2 `
- return (1);
4 ?, A8 x0 X4 g1 u+ d - }
复制代码
4 W' K0 }" t- }8 g/ ?) L; W
, R' s! [5 }. J o |
是不是如果就这么将LZ提供的代码放进去的话就无法对产品进行软件升级了?
比如说产品早期推出的时候有BUG,后期发现可以通过(例如说)产品接口的串口进行软件升级解决。但是由于进行Flash读保护,因此就无法实施了?
那么楼主提供的代码是属于哪个级别的操作?我觉得已经是级别2了,因为需要防止别人抄里面的程序。
是等级1的,可以通过程序改为等级0,然后Flash中的程序就会被擦除,之后芯片就变成新的了,就可以使用仿真器了。如果把等级设置成2,就没有办法再修改等级了,仿真器接口直接熔断,只能通过boot升级程序了。
0 I! {/ R( F% }# u$ C
其实楼主的代码 根本就是官方的例程提供的 一开始自己都没详细的说明保护的机制和原理
解密要钱的啊,而且还不少,小众产品或者产品中有多芯片协同工作的,破解难度极大,可以交叉验证版本。