Skip to content

Add a non-secure data flash window to the STM32 TrustZone HAL - #1023

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

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

Conversation

@dgarske

@dgarske dgarske commented Dec 2, 2026 •

Copy link
Copy Markdown
Member

On STM32L5, STM32U5 and STM32H5 with TrustZone enabled, wolfBoot opens SAU windows for the boot partition, the non-secure RAM, the peripherals and the NSC area, and nothing else. A non-secure application that keeps data in flash outside the partitions (device credentials, settings, a key blob) cannot reach it: the access raises a SecureFault even when the option bytes already leave those pages non-secure.

What it adds

  • hal/stm32_tz.c - WOLFBOOT_NS_DATA_ADDRESS and WOLFBOOT_NS_DATA_SIZE add SAU region 5 over that area in hal_tz_sau_init on all three targets, in the non-secure alias whichever alias the address is given in. Nothing changes when the two are not defined, and a WOLFBOOT_SECURE_APP build, which has no non-secure side, gets no window. The build stops when the area isn't a non-zero multiple of 32 bytes (the SAU granularity), lies below the boot partition or outside internal flash, or overlaps the boot, update (at its own size) or swap partition, and a PULL_LINKER_DEFINES build, whose partition addresses the checks cannot see, cannot use the window.
  • options.mk, CMakeLists.txt, tools/config.mk - the two .config (or CMake) variables become the defines under TZEN=1 and survive make config; an address without a size is an error.
  • tools/scripts/set-stm32-tz-option-bytes.sh - on STM32H5 the bank 2 watermark (SECWM2_END) stops at the sector before the data area, which must start on a sector boundary above the update and swap partitions and end inside bank 2; on L5 and U5 bank 2 is already non-secure and the script leaves it alone. Its .config reads use the field separator [?]=|:=|=, which mawk and busybox awk accept as well as gawk.
  • docs/STM32-TZ.md, docs/Targets.md - the two options, the placement rules, the erase-page advice for a rewritten area, the watermark requirement and the H5 example values.
  • tools/unit-tests/unit-stm32-tz-nsdata.c, test-stm32-tz-nsdata-config.sh, tools/scripts/test-set-stm32-tz-option-bytes.sh - region 5 with the address in either alias, without the options and for a secure application; every placement rule refusing a bad layout and accepting a good one; the option-bytes script against a stub programmer. make -C tools/unit-tests run runs all three.

Hardware / test status

Validated on a NUCLEO-H563ZI (stm32h5.config with the area at 0x081A2000, 0x6000 bytes, SECWM2_END 0x50): the non-secure application reads its device key and credential from the area after a reset, programs it through the non-secure flash registers, and completes a network onboarding that depends on them. That run used this change on an earlier master. On current master it builds with and without the options for stm32h5-tz, stm32h5-tz-wolftrust, stm32l5-wolfcrypt-tz, stm32l5-tz-wolftrust and stm32u5-wolfcrypt-tz. Not run on L5 or U5 hardware.

Scope

The pages must be non-secure in the SECWM option bytes; wolfBoot does not change the watermarks at run time and this PR keeps it that way. Region 5 leaves region 4 to the STM32U5 dual-bank update window in #1021, so the two PRs do not overlap.

Copilot AI balanced review requested due to automatic review settings December 2, 2026 20:51

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

Unvalidated flash ranges can expose adjacent memory or remove protection from update and swap areas, while STM32L5 provisioning rejects valid addresses and configuration regeneration drops both settings.

4 open issues
What changed in this PR

Adds an optional non-secure flash data window to wolfBoot’s STM32 TrustZone support, allowing applications to access persistent data outside firmware partitions.

Changes:

  • Adds configurable SAU region 5 for STM32L5/U5/H5, excluding secure-app builds.
  • Exposes settings through Make and CMake and adjusts H5 watermark provisioning.
  • Documents data placement and security requirements.
File Description
tools/​scripts/​set-stm32-tz-option-bytes.sh Adjusts the bank-2 watermark before the data area.
options.mk Adds TrustZone data-window compiler definitions.
hal/​stm32_tz.c Configures the optional non-secure SAU window.
docs/​Targets.md Links to data-window configuration guidance.
docs/​STM32-TZ.md Documents settings, placement, and watermark requirements.
CMakeLists.txt Adds equivalent CMake configuration support.

Review effort: Balanced

Comment thread hal/stm32_tz.c Resolved
Comment thread options.mk Resolved
@dgarske dgarske assigned danielinux and wolfSSL-Bot and unassigned dgarske Dec 2, 2026
@dgarske
dgarske requested review from danielinux and a balanced review from Copilot December 2, 2026 23:06

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

UPDATE boundary checks incorrectly use the BOOT size, allowing layouts where update erasure destroys application data and provisioning leaves part of UPDATE unprotected.

3 open issues
4 resolved since last review

Review effort: Balanced

Comment thread hal/stm32_tz.c Resolved
Comment thread hal/stm32_tz.c Resolved
@dgarske dgarske self-assigned this Dec 2, 2026
@dgarske dgarske assigned aidangarske and unassigned dgarske and danielinux Dec 2, 2026
@dgarske
dgarske requested review from aidangarske and removed request for danielinux December 3, 2026 23:00

@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: REQUEST_CHANGES
Findings: 3 total — 3 posted, 0 skipped
3 finding(s) posted as inline comments (see file-level comments below)

Posted findings

  • [High] SAU data window is not constrained to physical flash — hal/stm32_tz.c:359-365
  • [Medium] New SAU validation and alias mapping lack automated tests — hal/stm32_tz.c:343-390
  • [Medium] H5 watermark calculations have only manual coverage — tools/scripts/set-stm32-tz-option-bytes.sh:49-75

Review generated by Skoll

Comment thread hal/stm32_tz.c Resolved
Comment thread hal/stm32_tz.c Resolved
Comment thread hal/stm32_tz.c Resolved
Comment thread docs/STM32-TZ.md Resolved
@aidangarske aidangarske assigned dgarske and unassigned wolfSSL-Bot and aidangarske Dec 4, 2026
@dgarske
dgarske requested a review from aidangarske December 4, 2026 19:55
@dgarske dgarske assigned wolfSSL-Bot and aidangarske and unassigned dgarske Dec 4, 2026
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