你的浏览器版本过低,可能导致网站不能正常访问!
为了你能正常使用网站功能,请使用这些浏览器。

STM32F4(Flash读保护)

[复制链接]
XinLiYF 发布时间:2018-2-2 21:35
STM32F4(Flash读保护)


转载来源:STM32F4(Flash读保护)


1,目的
        在实际的产品发布中,如果不对储存在单片机Flash中的程序做一些保护的话,就有可能被一些不法公司,通过仿真器(J-Link,ST-Link等)把Flash中的程序读取回来,得到bin文件或hex文件,然后去山寨产品。所以我们需要对程序进行保护,一种比较简单可靠的方法就是把Flash设置成读保护。

2,开发环境
        1,适用芯片:STM32F4全部芯片
        2,固件库:STM32F4xx_DSP_StdPeriph_Lib_V1.8.0
        3,IDE:MDK517

3,程序源码

  1. /****************************************************************
  2. * Function:    Flash_EnableReadProtection
  3. * Description: Enable the read protection of user flash area.
  4. * Input:
  5. * Output:
  6. * Return:      1: Read Protection successfully enable
  7. *              2: Error: Flash read unprotection failed
  8. *****************************************************************/
  9. uint32_t Flash_EnableReadProtection(void)
  10. {
  11.   /* Returns the FLASH Read Protection level. */
  12.   if( FLASH_OB_GetRDP() == RESET )
  13.   {
  14.     /* Unlock the Option Bytes */
  15.     FLASH_OB_Unlock();
  16.    
  17.     /* Sets the read protection level. */
  18.     FLASH_OB_RDPConfig(OB_RDP_Level_1);
  19.    
  20.     /* Start the Option Bytes programming process. */  
  21.     if (FLASH_OB_Launch() != FLASH_COMPLETE)
  22.     {
  23.       /* Disable the Flash option control register access (recommended to protect
  24.          the option Bytes against possible unwanted operations) */
  25.       FLASH_OB_Lock();
  26.       
  27.       /* Error: Flash read unprotection failed */
  28.       return (2);
  29.     }
  30.   
  31.     /* Disable the Flash option control register access (recommended to protect
  32.        the option Bytes against possible unwanted operations) */
  33.     FLASH_OB_Lock();

  34.     /* Read Protection successfully enable */
  35.     return (1);
  36.   }
  37.   
  38.   /* Read Protection successfully enable */
  39.   return (1);
  40. }

  41. /****************************************************************
  42. * Function:    Flash_DisableReadProtection
  43. * Description: Disable the read protection of user flash area.
  44. * Input:
  45. * Output:
  46. * Return:      1: Read Protection successfully disable
  47. *              2: Error: Flash read unprotection failed
  48. *****************************************************************/
  49. uint32_t Flash_DisableReadProtection(void)
  50. {
  51.   /* Returns the FLASH Read Protection level. */
  52.   if( FLASH_OB_GetRDP() != RESET )
  53.   {
  54.     /* Unlock the Option Bytes */
  55.     FLASH_OB_Unlock();
  56.    
  57.     /* Sets the read protection level. */
  58.     FLASH_OB_RDPConfig(OB_RDP_Level_0);
  59.    
  60.     /* Start the Option Bytes programming process. */  
  61.     if (FLASH_OB_Launch() != FLASH_COMPLETE)
  62.     {
  63.       /* Disable the Flash option control register access (recommended to protect
  64.          the option Bytes against possible unwanted operations) */
  65.       FLASH_OB_Lock();
  66.       
  67.       /* Error: Flash read unprotection failed */
  68.       return (2);
  69.     }
  70.   
  71.     /* Disable the Flash option control register access (recommended to protect
  72.        the option Bytes against possible unwanted operations) */
  73.     FLASH_OB_Lock();

  74.     /* Read Protection successfully disable */
  75.     return (1);
  76.   }
  77.   
  78.   /* Read Protection successfully disable */
  79.   return (1);
  80. }
复制代码


评分

参与人数 1 ST金币 +2 收起 理由
努力的人 + 2 可以对程序录一个简短的视频进行讲解一下.

查看全部评分

收藏 2 评论9 发布时间:2018-2-2 21:35

举报

9个回答
maxtch 回答时间:2018-2-3 00:56:27
一般来说不要直接在程序代码里面这么写,而是生产烧录完成之后有生产烧录设备来操作。要不然如果在开发设备上这么折腾一下芯片就不能做开发了。
奏奏奏 回答时间:2018-2-3 07:39:35
maxtch 发表于 2018-2-3 00:56
一般来说不要直接在程序代码里面这么写,而是生产烧录完成之后有生产烧录设备来操作。要不然如果在开发设备 ...

是不是如果就这么将LZ提供的代码放进去的话就无法对产品进行软件升级了?
比如说产品早期推出的时候有BUG,后期发现可以通过(例如说)产品接口的串口进行软件升级解决。但是由于进行Flash读保护,因此就无法实施了?
XinLiYF 回答时间:2018-2-3 08:57:25
奏奏奏 发表于 2018-2-3 07:39
是不是如果就这么将LZ提供的代码放进去的话就无法对产品进行软件升级了?
比如说产品早期推出的时候有BUG ...

捕获1.JPG
捕获2.JPG

奏奏奏 回答时间:2018-2-3 09:44:52

那么楼主提供的代码是属于哪个级别的操作?我觉得已经是级别2了,因为需要防止别人抄里面的程序。
时光虫子 回答时间:2018-2-3 10:28:48
对于专业芯片解密人来说这些都没有用的,现在破解方法太多了,相信伟大的华强北
XinLiYF 回答时间:2018-2-3 18:44:07
奏奏奏 发表于 2018-2-3 09:44
那么楼主提供的代码是属于哪个级别的操作?我觉得已经是级别2了,因为需要防止别人抄里面的程序。 ...

是等级1的,可以通过程序改为等级0,然后Flash中的程序就会被擦除,之后芯片就变成新的了,就可以使用仿真器了。如果把等级设置成2,就没有办法再修改等级了,仿真器接口直接熔断,只能通过boot升级程序了。
Tcreat 回答时间:2018-2-3 19:50:38
本帖最后由 Tcreat 于 2018-2-3 19:52 编辑

其实楼主的代码  根本就是官方的例程提供的  一开始自己都没详细的说明保护的机制和原理
cloudmr 回答时间:2018-3-25 15:58:45
Talons 回答时间:2019-4-7 09:03:35
时光虫子 发表于 2018-2-3 10:28
对于专业芯片解密人来说这些都没有用的,现在破解方法太多了,相信伟大的华强北 ...

解密要钱的啊,而且还不少,小众产品或者产品中有多芯片协同工作的,破解难度极大,可以交叉验证版本。

所属标签

相似分享

关于
我们是谁
投资者关系
意法半导体可持续发展举措
创新与技术
意法半导体官网
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
官方最新发布
STM32N6 AI生态系统
STM32MCU,MPU高性能GUI
ST ACEPACK电源模块
意法半导体生物传感器
STM32Cube扩展软件包
关注我们
st-img 微信公众号
st-img 手机版