设置 PCROP STM32H7 (STM32H743) 时出现错误

发布于 2025-01-14 21:57:50 字数 1998 浏览 1 评论 0原文

目标

我尝试在 STM32H743VI 微控制器上设置 PCROP 区域,但在执行 HAL_FLASH_OB_Launch() 时收到错误代码 HAL_FLASH_ERROR_OB_CHANGE 并且我的 PCROP 区域未设置。

我正在使用的代码的相关部分应该是以下部分

我的代码

    #include "stm32h7xx_hal.h"

    FLASH_OBProgramInitTypeDef OBInit;
    HAL_FLASHEx_OBGetConfig(&OBInit);

    HAL_FLASH_Unlock();
    HAL_FLASH_OB_Unlock();

    // program OB
    OBInit.OptionType       = OPTIONBYTE_PCROP;
    OBInit.PCROPStartAddr   = 0x8030000;
    OBInit.PCROPEndAddr     = 0x8031000;
    OBInit.PCROPConfig      = OB_PCROP_RDP_ERASE;
    OBInit.Banks            = FLASH_BANK_1; // (1, 2 oder BOTH)
    HAL_FLASHEx_OBProgram(&OBInit);

    /* write Option Bytes */
    if (HAL_FLASH_OB_Launch() != HAL_OK) {
        // Error handling
        while (1)
        {
        }
    }

    HAL_FLASH_OB_Lock();
    HAL_FLASH_Lock();

该代码主要受youtube视频“Security Part3 - STM32 Security features - 07 - PCROP lab”(由STMicroElectronics提供)和我必须的工作代码的启发更改 RDP 级别。

设置

我的秘密函数位于预期的地址范围内,我通过在 Flash.Id 文件中添加内存区域

MEMORY
{
    [...]
    PCROP   (x)     : ORIGIN = 0x08030000,  LENGTH = 16K
}

并将函数文件放入相应的部分

SECTIONS
{
    [...]
    .PCROPed :
    {
        . = ALIGN(4);
        *led_blinking.o (.text .text*)
        . = ALIGN(4);
    } > PCROP
    [...]
}

来做到这一点,我设置了标志 -mslow-flash-data 对于我保留秘密功能的文件。我按照视频中的教程(见上文)在没有真正理解原因的情况下执行了此操作。

我尝试

使用 J-Trace 调试器调试我的程序,看起来我正在成功执行 STM32H743/753 参考手册(第 159 页)中描述的选项字节修改序列。

保护整个 Flashpage(开始 0x080020000,结束 0x0803FFFF)也不起作用,尽管我并不期望它会产生影响。

我还尝试了 PCROPConfig 的另一个选项,即 OB_PCROP_RDP_NOT_ERASE 选项。

HAL_FLASHEx_OBProgram(&OBInit) 按预期工作,并且 ObInit 配置已正确设置到 _FLASH->PRAR_PRG1 寄存器中。对于我的代码,寄存器的内容是 0x80880080

我确实断开并重新连接了微控制器的电源和调试器,以防我没有正确 POR。

我检查了勘误表,但没有适用于我的问题的勘误表。

编辑

我更改了我的代码部分中受PCROP保护的区域。 我这样做是因为我的代码通常可以正常工作,并且我发现保护第一个闪存页面中的区域不是一个好主意!

Goal

I'm trying to set a PCROP area on my STM32H743VI microcontroller, but I'm getting the error code HAL_FLASH_ERROR_OB_CHANGE when executing HAL_FLASH_OB_Launch() and my PCROP area is not set.

The relevant part of the code I'm using should be the following sections

My code

    #include "stm32h7xx_hal.h"

    FLASH_OBProgramInitTypeDef OBInit;
    HAL_FLASHEx_OBGetConfig(&OBInit);

    HAL_FLASH_Unlock();
    HAL_FLASH_OB_Unlock();

    // program OB
    OBInit.OptionType       = OPTIONBYTE_PCROP;
    OBInit.PCROPStartAddr   = 0x8030000;
    OBInit.PCROPEndAddr     = 0x8031000;
    OBInit.PCROPConfig      = OB_PCROP_RDP_ERASE;
    OBInit.Banks            = FLASH_BANK_1; // (1, 2 oder BOTH)
    HAL_FLASHEx_OBProgram(&OBInit);

    /* write Option Bytes */
    if (HAL_FLASH_OB_Launch() != HAL_OK) {
        // Error handling
        while (1)
        {
        }
    }

    HAL_FLASH_OB_Lock();
    HAL_FLASH_Lock();

The code is mostly inspired by the youtube video "Security Part3 - STM32 Security features - 07 - PCROP lab" (by STMicroelectronics) and the working code I have to change RDP level.

Setup

My secret function is in the expected address range, I did that by adding the memory area in the Flash.Id File

MEMORY
{
    [...]
    PCROP   (x)     : ORIGIN = 0x08030000,  LENGTH = 16K
}

and putting the function file into the section accordingly

SECTIONS
{
    [...]
    .PCROPed :
    {
        . = ALIGN(4);
        *led_blinking.o (.text .text*)
        . = ALIGN(4);
    } > PCROP
    [...]
}

I set the flag -mslow-flash-data for the file I'm keeping my secret function in. I did this without really understanding why, following the tutorial in the video (see above).

What I tried

I debugged my program with my J-Trace Debugger and it seems like I'm doing the Option byte modification sequence described in the STM32H743/753 reference manual (p. 159) succesfully.

Securing an entire Flashpage (start 0x080020000, end 0x0803FFFF) didn't work either, even though I did not expect it to make a difference.

I also tried the other option for PCROPConfig, namely the OB_PCROP_RDP_NOT_ERASE option.

HAL_FLASHEx_OBProgram(&OBInit) works as intended and the ObInit Configuration is set correctly into the _FLASH->PRAR_PRG1 register. For my code the content of the register is 0x80880080

I did disconnect and reconnect the microcontroller from power and debugger in case I did not POR correctly.

I checked the errata sheet, but there is no errata which would be applicable to my problem.

EDIT

I changed the area which is protected by PCROP in the My code section.
I did this, since my code is generally functional and I found out it's not a good idea to protect an area in the first flash page!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

疏忽 2025-01-21 21:57:50

嗯,到目前为止,代码看起来不错。

您确定在重新设置之前已禁用 PCROP 吗?

检查 PRAR_CUR1 是否未从其他地方设置。尽管已设置,但尝试设置时 PCROP 将引发失败。

Hmm, the code looks good so far.

Are you sure PCROP is disabled before setting it again?

Check if PRAR_CUR1 is not set from somewhere else. PCROP will raise a failure when trying to set although it is set.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文