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 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
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.

4 participants