Repository navigation
Conversation
There was a problem hiding this comment.
🟡 Changes recommended
Cross-boundary flash writes can silently leave data unwritten while returning success, and make config drops the 4 MB flash setting, causing partition-layout checks to fail.
2 open issues
What changed in this PR
Adds 4 MB STM32U5 support and enables TrustZone with hardware bank swapping in wolfBoot.
Changes:
- Makes flash geometry configurable and validates dual-bank layouts.
- Updates secure flash routing, bootloader copying, and TrustZone mappings.
- Adds example configurations, routing tests, documentation, and CI builds.
| File | Description |
|---|---|
| tools/unit-tests/unit-stm32u5-tz-dualbank.c | Tests flash routing for both device sizes. |
| tools/unit-tests/Makefile | Builds the new routing tests. |
| hal/stm32u5.h | Defines configurable flash geometry and copy size. |
| hal/stm32u5.c | Updates routing, layout checks, and bootloader copying. |
| hal/stm32_tz.c | Handles U5 swap state and UPDATE mapping. |
| docs/Targets.md | Documents 4 MB devices and TrustZone bank swapping. |
| config/examples/stm32u5a5-tz-dualbank.config | Adds a TrustZone dual-bank example. |
| config/examples/stm32u5a5-nonsecure-dualbank.config | Adds a non-secure dual-bank example. |
| arch.mk | Passes flash-size configuration to compilation. |
| .gitignore | Excludes generated test artifacts. |
| .github/workflows/test-configs.yml | Adds builds for both examples. |
| .github/workflows/test-build.yml | Includes both examples in STM32 build setup. |
Review effort: Balanced
b2070fd to
b199ecb
Compare
aidangarske
left a comment
There was a problem hiding this comment.
Skoll Code Review
Scan type: review
Overall recommendation: COMMENT
Findings: 1 total — 1 posted, 0 skipped
1 finding(s) posted as inline comments (see file-level comments below)
Posted findings
- [Medium] Guard Linux-specific mmap flags in the host test —
tools/unit-tests/unit-stm32u5-tz-dualbank.c:503-505
Review generated by Skoll
|
|
||
| static uint8_t *map_flash(uintptr_t addr) | ||
| { | ||
| void *p = mmap((void *)addr, FLASH_SZ, PROT_READ | PROT_WRITE, |
There was a problem hiding this comment.
Guard Linux-specific mmap flags in the host test
The new target is included in the default TESTS list, but map_flash() unconditionally uses MAP_FIXED_NOREPLACE. That flag is absent from macOS \<sys/mman.h> and other non-Linux or older environments, causing make -C tools/unit-tests to fail at compilation before the test can run. MAP_NORESERVE is also implementation-specific and should be guarded. Other unit tests in this repository already conditionally select MAP_FIXED_NOREPLACE.
Suggested fix: Construct the mmap flags conditionally. Where no fixed, non-replacing mapping flag exists, treat the requested address as a hint, verify the returned address, and unmap it before failing if the hint was not honored.
Basis: Linux mmap(2) documents MAP_FIXED_NOREPLACE as available since Linux 4.17 and documents that a non-fixed addr is only a hint whose returned address must be checked.
Suggestion:
| void *p = mmap((void *)addr, FLASH_SZ, PROT_READ | PROT_WRITE, | |
| int flags = MAP_PRIVATE | MAP_ANONYMOUS; | |
| void *p; | |
| #ifdef MAP_FIXED_NOREPLACE | |
| flags |= MAP_FIXED_NOREPLACE; | |
| #endif | |
| #ifdef MAP_NORESERVE | |
| flags |= MAP_NORESERVE; | |
| #endif | |
| p = mmap((void *)addr, FLASH_SZ, PROT_READ | PROT_WRITE, flags, -1, 0); | |
| if (p != MAP_FAILED && p != (void *)addr) { | |
| munmap(p, FLASH_SZ); | |
| p = MAP_FAILED; | |
| } |
There was a problem hiding this comment.
Fixed. MAP_FIXED_NOREPLACE and MAP_NORESERVE are guarded with #ifdef. If the hint isn't honored, the mapping is unmapped and the test fails cleanly.
| if(${WOLFBOOT_TARGET} STREQUAL "stm32u5") | ||
| set(ARCH_FLASH_OFFSET 0x08000000) | ||
| if(DEFINED STM32U5_FLASH_SIZE_MB) | ||
| list(APPEND WOLFBOOT_DEFS STM32U5_FLASH_SIZE_MB=${STM32U5_FLASH_SIZE_MB}) |
There was a problem hiding this comment.
CMake reads DUALBANK_SWAP=1 from the new config, but the generated build omits the define and compiles update_flash.c. Can you select update_flash_hwswap.c, pass the define to the HAL, and check that selection in the CMake CI matrix?
There was a problem hiding this comment.
Agreed it's a gap, but it isn't specific to U5: CMake never selects update_flash_hwswap.c or passes DUALBANK_SWAP for any target, H5 dual-bank included, and none of the dual-bank configs are in the CMake .config matrix. I'd like to do that as a separate CMake PR that covers all the dual-bank targets, rather than special-case U5 here.
| # and hardware-assisted bank swap. Required option bytes: | ||
| # TZEN=1 | ||
| # SECWM1_PSTRT=0x0 SECWM1_PEND=0x1F | ||
| # SECWM2_PSTRT=0x0 SECWM2_PEND=0x1F |
There was a problem hiding this comment.
This config requires pages 0 through 31 to be secure in both banks, but set-stm32-tz-option-bytes.sh disables the bank 2 watermark on U5. Can you have the script apply the bank 1 range to bank 2 when DUALBANK_SWAP=1?
There was a problem hiding this comment.
Fixed. On stm32u5 with DUALBANK_SWAP=1, set-stm32-tz-option-bytes.sh now applies the bank 1 secure range to bank 2 (SECWM2_PSTRT=0x0, SECWM2_PEND=<last wolfBoot page>). Other configs keep bank 2 non-secure as before.
| #if (WOLFBOOT_PARTITION_BOOT_ADDRESS + WOLFBOOT_PARTITION_SIZE) > FLASH_BANK2_BASE | ||
| #error "DUALBANK_SWAP: the BOOT partition must end in bank 1" | ||
| #endif | ||
| #if (WOLFBOOT_PARTITION_UPDATE_ADDRESS - FLASH_BANK2_BASE) != \ |
There was a problem hiding this comment.
With PULL_LINKER_DEFINES, the partition addresses are C constants. The preprocessor reads both as zero here and rejects the valid 4 MB layout. Can you check the layout at link time for that configuration?
There was a problem hiding this comment.
Fixed. The preprocessor layout checks are now skipped when PULL_LINKER_DEFINES is set, since the addresses are only known at link time there. Confirmed hal/stm32u5.c builds with -DPULL_LINKER_DEFINES and the TZ dual-bank config.
|
|
||
| #if (TZ_SECURE()) && defined(DUALBANK_SWAP) | ||
| /* A write can cross the BOOT or UPDATE offset, or the bank end */ | ||
| if (cur_addr > FLASH_TOP) |
There was a problem hiding this comment.
A write that crosses FLASH_TOP can program the in-range quadwords and then return success with the rest unwritten. Can you check the full range before programming the first quadword and return an error if it won't fit?
There was a problem hiding this comment.
Fixed. The full range is now checked before the first quadword is programmed, and an out-of-range write returns -1 instead of 0. Added a unit test for a write that straddles FLASH_TOP; it fails with nothing programmed.
| #define FLASHMEM_ADDRESS_SPACE (0x08000000) | ||
| #define ARCH_FLASH_OFFSET FLASHMEM_ADDRESS_SPACE | ||
| #define FLASH_PAGE_SIZE (0x2000) | ||
| #define FLASH_BANK2_BASE (FLASHMEM_ADDRESS_SPACE + \ |
There was a problem hiding this comment.
The test defines its own FLASH_BANK2_BASE and FLASH_TOP, so both layout tests still pass if the values in stm32u5.h regress. Can you check the production header against the expected addresses for both flash sizes?
There was a problem hiding this comment.
Fixed. The test now extracts FLASH_BANK2_BASE and FLASH_TOP from hal/stm32u5.h (same sed approach as the function extract), so a header regression fails the build or test_layout for both the 2 MB and 4 MB builds. Checked by breaking each value in the header.
b199ecb to
1c44594
Compare


Adds support for the 4 MB STM32U5 parts (STM32U59x/U5Ax/U5Fx/U5Gx) and makes
TZEN=1withDUALBANK_SWAP=1build and work on STM32U5.1. 4 MB devices
The HAL assumed the 2 MB geometry of the U575/U585, with bank 2 at
0x08100000. On a 4 MB part bank 2 is at0x08200000, so the dual-bank copy of the bootloader and every bank 2 erase or write went to the wrong address.STM32U5_FLASH_SIZE_MB(default 2) now sets the bank 2 base, the flash top and the number of block-based security registers.2. TrustZone with dual-bank
hal_tz_release_nonsecure_area()read the swap state fromFLASH_OPTSR_CUR, which only exists on STM32H5, so this combination did not compile. On STM32U5 it now readsFLASH_OPTR, and the claim path takes the swap into account the same way.hal_flash_write()andhal_flash_erase()treated all of bank 1 as secure and all of bank 2 as non-secure. Both banks have the same layout, so the flash interface is now chosen from the offset inside the bank, and an erase that crosses from one interface to the other clears the page-erase bit on the one it leaves.fork_bootloader()copied a fixed 32 KB, less than the TrustZone secure area and, in the non-TrustZone configurations, less than the space reserved below BOOT. It now copies everything below the BOOT partition one page at a time, skipping pages that already match.The build now stops with an error when
STM32U5_FLASH_SIZE_MBis not 2 or 4, when the BOOT and UPDATE partitions are not at the same offset in their banks, do not start on a page boundary or do not fit in them, and whenWOLFCRYPT_TZis combined with this layout. On STM32L5 TrustZone withDUALBANK_SWAPis still not supported, now with an explicit error.Adds a host unit test for the secure and non-secure flash routing (4 MB and 2 MB layouts),
stm32u5a5-nonsecure-dualbank.configandstm32u5a5-tz-dualbank.config, a "4 MB devices" subsection and a TrustZone dual-bank scenario indocs/Targets.md, and build jobs for both configurations.wolfboot.binfor the existing STM32L5, STM32H5 and non-dual-bank STM32U5 configurations is byte-identical before and after.Hardware / test status
Validated on a NUCLEO-U5A5ZJ-Q with both new configurations, TrustZone off and on: first boot, the bootloader copy to bank 2, update with bank swap, rejection and erase of an invalid update image, and a further update with the banks swapped. The STM32U5 emulator tests, including the dual-bank fallback test on a 2 MB part, pass. The same four cases pass with TrustZone on a 2 MB B-U585I-IOT02A.
Scope
WOLFCRYPT_TZis rejected with dual-bank because the keyvault in the secure area is only mirrored to bank 2 while the banks are not swapped. The secure watermarks must be the same for both banks; they are not rewritten on a swap.