【学习笔记】STM32U575使用SBSFU,移植ST Cube源码
这是参考STM32U585修改的工程,用于U575实验学习。
代码下载地址:git clone https://gitcode.com/u012988644/stm32u5-sbsfu-release.git
代码分支:stm32u575
调试记录
手里没有 STM32U585 板子,只有一块 NUCLEO-U575ZI-Q 评估板(STM32U575)。
阶段一:直接使用 U585 固件烧录 U575(失败)
步骤:
- 在 STM32CubeIDE 下依次编译 SBSFU_Boot、SBSFU_Loader、SBSFU_Appli
- 在
SBSFU_Boot/STM32CubeIDE路径下打开 Git Bash,连接开发板,运行regression.sh回滚选项字节配置 - 运行
SBSFU_UPDATE.sh烧录固件 - 打开串口,复位后持续触发 TAMPER 检测,循环打印日志:
[INF] TAMPER SEED [0x3fb7deba,0x7c7718e3,0x53d6d43b,0x3a978920]
[INF] TAMPER Activated
[INF] BANK 1 secur[INF] TAMPER SEED [0x509fe951,0x695476d2,0xe72c8c8d,0xc2208dfc]
[INF] TAMPER Activated
...
原因: 默认同时激活了内部和外部 tamper(TFM_TAMPER_ENABLE = ALL_TAMPER):
// SBSFU_Boot/Inc/boot_hal_cfg.h
#define NO_TAMPER (0) /*!< No tamper activated */
#define INTERNAL_TAMPER_ONLY (1) /*!< Only Internal tamper activated */
#define ALL_TAMPER (2) /*!< Internal and External tamper activated */
#define TFM_TAMPER_ENABLE ALL_TAMPER
MCU 变砖与恢复:
- 过了一会 log 停止输出,STLink 也无法连接
- 由于未运行
hardening.sh(该脚本会锁死启动地址),仍可恢复:- 短接
PH3(BOOT 引脚)到 VDD,按复位按钮 - STLink 连接后,同时修改选项字
RDP=0xAA并取消TZEN(TZ 关闭必须伴随 RDP 降级,需同时 Apply)
- 短接
- MCU 恢复正常
临时绕过外部 TAMPER:
查数据手册,TAMP8 的 IN/OUT 引脚为 PE5/PE4,短接这两个引脚后重新烧录,TAMPER 循环日志消失:
[INF] BANK 1 secure flash [0, 15] : OB [0, 127]
[ERR] Unexpected value for secure flash protection: set wmsec1
[INF] BANK 1 flash write protection [2, 12] : OB [127, 0]
[ERR] Unexpected value for write protection : set wrp1
[INF] BANK 2 flash write protection [123, 127] : OB [127, 0]
[ERR] Unexpected value for write protection : set wrp2
[INF] BANK 1 secure user flash [0, 11] : OB [0, 0]
[ERR] Unexpected value for secure user flash protection : set hdp1
[INF] RDPLevel 0xaa (0xbb)
[ERR] Unexpected value for RDP level
[INF] Programming RDP to bb
[INF] Unplug/Plug jumper JP3 (IDD)
最简解法: 将
TFM_TAMPER_ENABLE改为INTERNAL_TAMPER_ONLY重新编译烧录,直接关闭外部 tamper 检测,无需短接引脚。
- 根据 log 提示断开 IDD 跳线(代码内部修改了 RDP 等级,必须断电才能生效,复位无效)。断电重新上电后:
[INF] Starting bootloader OEMiROT
[INF] Initializing BL2 NV area : Power down/reset not supported...
[INF] BL2 NV Area Initialized : Power Down/reset supported
[INF] Consistent BL2 NV Counter 0 = 0x0
[WRN] Image 0 is installed
[INF] verify sig key id 0
[INF] checking public key 47 5b
[INF] verifying signature hlen 20
- 日志未完成跳转,菜单无输出 —— 确认是未修改目标芯片型号导致的,不能偷懒。
阶段二:移植到 STM32U575(成功)
修改芯片型号(STM32CubeIDE):
Properties→C/C++ Build→Settings→Build Steps→ Pre-build steps:将STM32U585xx改为STM32U575xxProperties→C/C++ General→Paths and Symbols→Symbols(GNU C):同样替换编译宏
SBSFU_Boot 移植:
问题 1:STM32U585 有 CRYP 外设(CRYP_HandleTypeDef),STM32U575 只有 AES 外设(AES_HandleTypeDef)
修改 config-boot.h,禁用硬件加速:
/* STM32U575 has no CRYP peripheral (only AES), disable HW accel */
/* #define BL2_HW_ACCEL_ENABLE */
效果:MBEDTLS_AES_ALT、MBEDTLS_ECDSA_VERIFY_ALT、MBEDTLS_ECP_ALT 均不启用,aes_alt.c 不参与编译,改用 MBEDTLS_HMAC_DRBG_C(软件 DRBG)。
问题 2:STM32U575 无 SAES 外设,RCC_PeriphCLKInitTypeDef 中无 SaesClockSelection 字段
修改 stm32u5xx_hal_msp.c,加条件编译:
#if defined(SAES)
.SaesClockSelection = 0,
#endif /* defined(SAES) */
Boot 工程编译通过。✅
SBSFU_Loader 移植:
问题:编译 Secure 分区报错,缺少 partition_stm32u575xx.h
将 partition_stm32u585xx.h 复制为 partition_stm32u575xx.h,修改文件头 include guard 即可(内容完全基于 region_defs.h 宏,无需其他改动)。Appli 工程同步处理。
Loader 工程 S/NS 全部编译通过。✅
SBSFU_Appli 移植:
问题:缺少链接脚本 STM32U575AIIX_FLASH.ld
将 STM32U585AIIX_FLASH.ld 复制为 STM32U575AIIX_FLASH.ld(内容同样基于 region_defs.h 宏,无需修改),S 和 NS 子工程均需添加。
Appli 工程 S/NS 全部编译通过。✅
烧录与启动:
依次执行 regression.sh → SBSFU_UPDATE.sh,开机成功,串口输出:
[INF] TAMPER Activated
[INF] BANK 1 secure flash [0, 15] : OB [0, 127]
[ERR] Unexpected value for secure flash protection: set wmsec1
[INF] BANK 1 flash write protection [2, 12] : OB [127, 0]
[ERR] Unexpected value for write protection : set wrp1
[INF] BANK 2 flash write protection [123, 127] : OB [127, 0]
[ERR] Unexpected value for write protection : set wrp2
[INF] BANK 1 secure user flash [0, 11] : OB [0, 0]
[ERR] Unexpected value for secure user flash protection : set hdp1
[INF] RDPLevel 0xaa (0xbb)
[ERR] Unexpected value for RDP level
[INF] Programming RDP to bb
[INF] Unplug/Plug jumper JP3 (IDD)
[INF] Flash operation: Op=0x0, Area=0x0, Address=0x0
[INF] Starting bootloader OEMiROT
[INF] Initializing BL2 NV area : Power down/reset not supported...
[INF] Init BL2 NV Header area: Done
[INF] Initializing BL2 NV Counters
[INF] Init BL2 NV counters to 0 : Done
[INF] BL2 NV Area Initialized : Power Down/reset supported
[INF] Checking BL2 NV area
[INF] Checking BL2 NV area header
[INF] Checking BL2 NV Counter consistency
[INF] Consistent BL2 NV Counter 0 = 0x0
[WRN] Image 0 is installed
[INF] Starting validation of primary slot(s)
[INF] verify counter 0 1000000 0
[INF] counter 0 : ok
[INF] verify sig key id 0
[INF] checking public key 46 5b
[INF] verifying signature hlen 20
[INF] signature OK
[INF] Counter 0 set to 0x1000000
[INF] Bootloader chainload address offset: 0x1a000
[INF] Jumping to the first image slot
======================================================================
= (C) COPYRIGHT 2021 STMicroelectronics =
= =
= User App #A =
======================================================================
=================== Main Menu ============================
Test Protections -------------------------------------- 1
Toggle Secure LED ------------------------------------- 2
Selection :
启动成功,应用菜单正常输出。✅
更多推荐



所有评论(0)