Jlink使用技巧之--芯片加密(FLASH读保护-F4系列)

文章介绍了如何使用STM32F4的固件库功能来启用和禁用Flash的读保护,以防止程序被非法读取和复制。通过调用Flash_EnableReadProtection和Flash_DisableReadProtection函数,可以设置和解除读保护。在启用保护后未测试读取,而禁用保护可能导致串口通信异常,需要重新下载程序才能恢复正常。
摘要由CSDN通过智能技术生成

吃水不忘挖井人:

(1条消息) STM32F4(Flash读保护)_stm32f4读保护_XinLiBK的博客-CSDN博客

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

1. 读保护函数及解除读保护函数体

/****************************************************************
 * Function:    Flash_EnableReadProtection
 * Description: Enable the read protection of user flash area.
 * Input:
 * Output:
 * Return:      1: Read Protection successfully enable
 *              2: Error: Flash read unprotection failed
*****************************************************************/
uint32_t Flash_EnableReadProtection(void)
{
  /* Returns the FLASH Read Protection level. */
  if( FLASH_OB_GetRDP() == RESET )
  {
    /* Unlock the Option Bytes */
    FLASH_OB_Unlock();
    
    /* Sets the read protection level. */
    FLASH_OB_RDPConfig(OB_RDP_Level_1);
    
    /* Start the Option Bytes programming process. */  
    if (FLASH_OB_Launch() != FLASH_COMPLETE)
    {
      /* Disable the Flash option control register access (recommended to protect 
         the option Bytes against possible unwanted operations) */
      FLASH_OB_Lock();
      
      /* Error: Flash read unprotection failed */
      return (2);
    }
  
    /* Disable the Flash option control register access (recommended to protect 
       the option Bytes against possible unwanted operations) */
    FLASH_OB_Lock();
 
    /* Read Protection successfully enable */
    return (1);
  }
  
  /* Read Protection successfully enable */
  return (1);
}
 
/****************************************************************
 * Function:    Flash_DisableReadProtection
 * Description: Disable the read protection of user flash area.
 * Input:
 * Output:
 * Return:      1: Read Protection successfully disable
 *              2: Error: Flash read unprotection failed
*****************************************************************/
uint32_t Flash_DisableReadProtection(void)
{
  /* Returns the FLASH Read Protection level. */
  if( FLASH_OB_GetRDP() != RESET )
  {
    /* Unlock the Option Bytes */
    FLASH_OB_Unlock();
    
    /* Sets the read protection level. */
    FLASH_OB_RDPConfig(OB_RDP_Level_0);
    
    /* Start the Option Bytes programming process. */  
    if (FLASH_OB_Launch() != FLASH_COMPLETE)
    {
      /* Disable the Flash option control register access (recommended to protect 
         the option Bytes against possible unwanted operations) */
      FLASH_OB_Lock();
      
      /* Error: Flash read unprotection failed */
      return (2);
    }
  
    /* Disable the Flash option control register access (recommended to protect 
       the option Bytes against possible unwanted operations) */
    FLASH_OB_Lock();
 
    /* Read Protection successfully disable */
    return (1);
  }
  
  /* Read Protection successfully disable */
  return (1);
}

2. 函数的声明;

3. 添加固件库函数:stm32f4xx_flash.c

4.  在调用文件内进行外部函数声明:

extern uint32_t Flash_EnableReadProtection(void);
extern uint32_t Flash_DisableReadProtection(void);

5. 一般通过串口进行调用。

6. 调用Flash_EnableReadProtection(void)后,没有测试是否能读出来;

  调用Flash_DisableReadProtection(void);后,串口工作不正常,下电后上电也不正常,重新下载程序后正常。

  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值