GD32F4 芯片读保护启动与解除
·
GD32F4 芯片读保护启动与解除
设计目标
实际使用中,传统的开启读保护使用程序启用即可,但是解除读保护需要专门的 GD32 Program 软件,操作复杂。故而本文特别使用指令实现读保护的启用和解除。# 读保护特性
实测发现,开启读保护之后,J-Link RTT Viewer 失效,仅能读到最后 1K 缓存的数据后,停止响应。
解除读保护后,固件区域清零,无法再运行程序,需要重新下载 boot 与 app 程序。# 读保护的设置和解除
void rdprotect(int argc, char **argv)
{
if(argc != 2) return;
/* 清除 FMC 可能残留的错误标志,避免后续操作失败 */
fmc_flag_clear(FMC_FLAG_END | FMC_FLAG_OPERR | FMC_FLAG_WPERR |
FMC_FLAG_PGMERR | FMC_FLAG_PGSERR | FMC_FLAG_RDDERR);
ob_unlock();
if(!strcmp("on", argv[1])){
/* 设置低等级读保护,ob_start() 后 FMC 硬件自动执行擦除+编程 */
ob_security_protection_config(FMC_LSPC);
ob_start();
if(fmc_ready_wait(FMC_TIMEOUT_COUNT) != FMC_READY){
fct_printf("rdprotect on: program fail, state=0x%08x\n", (unsigned int)FMC_STAT);
ob_lock();
return;
}
senddata_tofct("AT+rdprotect=on\r\n", 18);
} else if(!strcmp("off", argv[1])){
/* 取消读保护,FMC 会自动擦除 Option Bytes 并全片擦除 Main Flash */
ob_security_protection_config(FMC_NSPC);
ob_start();
if(fmc_ready_wait(FMC_TIMEOUT_COUNT) != FMC_READY){
fct_printf("rdprotect off: program fail, state=0x%08x\n", (unsigned int)FMC_STAT);
ob_lock();
return;
}
ob_lock();
senddata_tofct("AT+rdprotect=off\r\n", 19);
} else {
ob_lock();
return;
}
/* Option Byte 修改后必须复位才能生效 */
fct_printf("rdprotect success, system will reset with main flash %s ...\n",
(!strcmp("on", argv[1])) ? "protected" : "erased(unprotected)");
vTaskDelay(pdMS_TO_TICKS(1000));
NVIC_SystemReset();
}
CMD_EXPORT(rdprotect, "rd protect")
按照此操作后, 可以实现指令的读保护开启, 和读保护解除, 而无需特殊的软件, 和特别的调试器, 从而方便了调试开发
DAMO开发者矩阵,由阿里巴巴达摩院和中国互联网协会联合发起,致力于探讨最前沿的技术趋势与应用成果,搭建高质量的交流与分享平台,推动技术创新与产业应用链接,围绕“人工智能与新型计算”构建开放共享的开发者生态。
更多推荐



所有评论(0)