Skip to content

stm32u5: support 4 MB devices and TrustZone with DUALBANK_SWAP - #1021

Open
dgarske wants to merge 1 commit into
wolfSSL:masterfrom
dgarske:stm32u5_tz_dualbank
Open

dgarske wants to merge 1 commit into
wolfSSL:masterfrom
dgarske:stm32u5_tz_dualbank

Conversation

@dgarske

@dgarske dgarske commented Dec 2, 2026

Copy link
Copy Markdown
Member

Adds support for the 4 MB STM32U5 parts (STM32U59x/U5Ax/U5Fx/U5Gx) and makes TZEN=1 with DUALBANK_SWAP=1 build 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 at 0x08200000, 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 from FLASH_OPTSR_CUR, which only exists on STM32H5, so this combination did not compile. On STM32U5 it now reads FLASH_OPTR, and the claim path takes the swap into account the same way.
  • hal_flash_write() and hal_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.
  • A secure read of a non-secure flash page returns zero on this family, so wolfBoot saw an empty UPDATE partition. An SAU non-secure region now covers it.

The build now stops with an error when STM32U5_FLASH_SIZE_MB is 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 when WOLFCRYPT_TZ is combined with this layout. On STM32L5 TrustZone with DUALBANK_SWAP is 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.config and stm32u5a5-tz-dualbank.config, a "4 MB devices" subsection and a TrustZone dual-bank scenario in docs/Targets.md, and build jobs for both configurations.

wolfboot.bin for 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_TZ is 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.

Copilot AI balanced review requested due to automatic review settings December 2, 2026 19:37

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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

Comment thread hal/stm32u5.c Resolved
Comment thread arch.mk Resolved
@dgarske dgarske self-assigned this Dec 2, 2026
@dgarske
dgarske force-pushed the stm32u5_tz_dualbank branch from b2070fd to b199ecb Compare December 2, 2026 21:30
@dgarske dgarske assigned danielinux and wolfSSL-Bot and unassigned dgarske Dec 2, 2026
@dgarske
dgarske requested a review from danielinux December 2, 2026 23:02
@dgarske dgarske assigned dgarske and aidangarske and unassigned danielinux and dgarske Dec 3, 2026
@dgarske
dgarske requested review from aidangarske and removed request for danielinux December 3, 2026 23:01

@aidangarske aidangarske left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:

Suggested change
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;
}

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread CMakeLists.txt
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})

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread hal/stm32u5.c
#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) != \

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread hal/stm32u5.c Outdated

#if (TZ_SECURE()) && defined(DUALBANK_SWAP)
/* A write can cross the BOOT or UPDATE offset, or the bank end */
if (cur_addr > FLASH_TOP)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 + \

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@aidangarske aidangarske assigned dgarske and unassigned wolfSSL-Bot and aidangarske Dec 4, 2026
@dgarske
dgarske force-pushed the stm32u5_tz_dualbank branch from b199ecb to 1c44594 Compare December 5, 2026 03:06
@dgarske dgarske assigned aidangarske and wolfSSL-Bot and unassigned dgarske Dec 5, 2026
@dgarske
dgarske requested a review from aidangarske December 5, 2026 03:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants