硬汉嵌入式论坛

 找回密码
 立即注册
查看: 3100|回复: 3
收起左侧

[客户分享] STM32F4(Flash读保护)

[复制链接]

19

主题

15

回帖

72

积分

初级会员

积分
72
发表于 2018-2-1 23:57:07 | 显示全部楼层 |阅读模式
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金币 +50 收起 理由
eric2013 + 50 赞一个!

查看全部评分

回复

使用道具 举报

1万

主题

6万

回帖

10万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
107868
QQ
发表于 2018-2-2 01:39:26 | 显示全部楼层
非常感谢楼主分享
回复

使用道具 举报

338

主题

2051

回帖

3070

积分

版主

Rank: 7Rank: 7Rank: 7

积分
3070
发表于 2018-2-2 10:28:28 | 显示全部楼层
F4的读保护,我实验过。 只要代码中加入了读保护,jlink就无法直接下载程序。必须分两步,先解除保护,再刷程序。显得非常繁琐。
最后,只能在产品的生产最后阶段加入读保护,那么现场维护工程师更新程序,也是很繁琐。
回复

使用道具 举报

19

主题

15

回帖

72

积分

初级会员

积分
72
 楼主| 发表于 2018-2-2 13:26:25 | 显示全部楼层
caicaptain2 发表于 2018-2-2 10:28
F4的读保护,我实验过。 只要代码中加入了读保护,jlink就无法直接下载程序。必须分两步,先解除保护,再刷 ...

就是为了防止程序被盗啊,bin文件加密,Bootloader解密,就可以很好的升级,然后程序也不会泄漏。

一般Flash读保护都是在Bootloader中去做的。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|小黑屋|Archiver|手机版|硬汉嵌入式论坛

GMT+8, 2024-6-16 20:01 , Processed in 0.251257 second(s), 26 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2023, Tencent Cloud.

快速回复 返回顶部 返回列表