From 6a642990d25ee3f2561cd2010896e5fb62f2a622 Mon Sep 17 00:00:00 2001 From: David Garske Date: Thu, 5 Nov 2026 15:03:43 -0800 Subject: [PATCH] Add a non-secure data flash window to the STM32 TrustZone HAL --- CMakeLists.txt | 8 +++ docs/STM32-TZ.md | 27 ++++++++++ docs/Targets.md | 5 ++ hal/stm32_tz.c | 57 ++++++++++++++++++++++ options.mk | 9 ++++ tools/config.mk | 1 + tools/scripts/set-stm32-tz-option-bytes.sh | 36 ++++++++++++++ 7 files changed, 143 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index a4a0a3c180..ad3acf3322 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -760,6 +760,14 @@ if(ARCH STREQUAL "ARM") # TrustZone support for Cortex-M33 targets if(TZEN) list(APPEND WOLFBOOT_DEFS TZEN) + # non-secure application data outside the partitions (STM32L5/U5/H5) + if(WOLFBOOT_NS_DATA_ADDRESS) + if(NOT WOLFBOOT_NS_DATA_SIZE) + message(FATAL_ERROR "WOLFBOOT_NS_DATA_ADDRESS requires WOLFBOOT_NS_DATA_SIZE") + endif() + list(APPEND WOLFBOOT_DEFS WOLFBOOT_NS_DATA_ADDRESS=${WOLFBOOT_NS_DATA_ADDRESS} + WOLFBOOT_NS_DATA_SIZE=${WOLFBOOT_NS_DATA_SIZE}) + endif() if(CMAKE_SYSTEM_PROCESSOR STREQUAL "cortex-m33") list(APPEND WOLFBOOT_COMPILE_OPTIONS -mcmse) list(APPEND WOLFBOOT_LINK_OPTIONS -mcmse) diff --git a/docs/STM32-TZ.md b/docs/STM32-TZ.md index 982546d3df..45dd37042c 100644 --- a/docs/STM32-TZ.md +++ b/docs/STM32-TZ.md @@ -100,6 +100,33 @@ wolfBoot provides a few Non-Secure Callable functions to allow a non-secure application to perform certain operations that must be run from the secure domain. For more information, see [API](API.md#nsc-api). +### Non-secure data outside the partitions + +The SAU windows wolfBoot opens for the non-secure application cover the boot +partition, the non-secure RAM, the peripherals and the NSC area. An +application that keeps data in flash outside the partitions (device +credentials, settings, a key blob) names that area in `.config` and wolfBoot +adds a SAU region for it on STM32L5, STM32U5 and STM32H5: + +``` +WOLFBOOT_NS_DATA_ADDRESS?=0x081A2000 +WOLFBOOT_NS_DATA_SIZE?=0x6000 +``` + +Both must be multiples of 32 bytes (the SAU granularity), and the area must +lie above wolfBoot and clear of the boot, update and swap partitions; the +build stops otherwise. The same pages must be non-secure in the flash +watermark option bytes (`SECWM`), which wolfBoot does not change at run +time. On STM32H5 the area starts on a sector boundary above the swap +partition in bank 2, so `SECWM2_END` stops at the sector before it (`0x50` +for the example above with `stm32h5.config`); on STM32L5 and STM32U5 bank 2 +is already non-secure. `tools/scripts/set-stm32-tz-option-bytes.sh` takes +the two variables into account and refuses an STM32H5 area that would leave +part of the update or swap partition non-secure. The application then reads the area +through its non-secure alias and programs it through the non-secure flash +registers. A `WOLFBOOT_SECURE_APP` build has no non-secure application and +opens no window. + ### Example using STM32L552 - Copy the example configuration for STM32-L5 with support for wolfCrypt in diff --git a/docs/Targets.md b/docs/Targets.md index e0f51d7ba3..86d8a2887a 100644 --- a/docs/Targets.md +++ b/docs/Targets.md @@ -1398,6 +1398,11 @@ The example configuration for this scenario is available in [/config/examples/st For a full list of all the option bytes tested with this configuration, refer to [STM32-TZ.md](/docs/STM32-TZ.md). +A non-secure application that keeps data in flash above the partitions (for +example device credentials) names the area with `WOLFBOOT_NS_DATA_ADDRESS` and +`WOLFBOOT_NS_DATA_SIZE` in `.config`; see +[Non-secure data outside the partitions](/docs/STM32-TZ.md#non-secure-data-outside-the-partitions). + You can use the "update" command and XMODEM to send a newly signed update (see docs/flash-OTP.md) or use the steps below using the STM32_Programmer: ```sh diff --git a/hal/stm32_tz.c b/hal/stm32_tz.c index bc3422a6d7..e46a1f8dd1 100644 --- a/hal/stm32_tz.c +++ b/hal/stm32_tz.c @@ -340,6 +340,59 @@ void hal_gtzc_init(void) } #endif +#if defined(WOLFBOOT_NS_DATA_ADDRESS) && defined(WOLFBOOT_NS_DATA_SIZE) && \ + !defined(WOLFBOOT_SECURE_APP) && !defined(PULL_LINKER_DEFINES) +/* The SAU works in 32-byte units, and the area must keep clear of wolfBoot + * and of the partitions, which updates erase. Checked in the non-secure + * alias, since the partitions may be given in the secure one. */ +#define NS_DATA_FLASH(a) ((a) & ~0x04000000UL) +#define NS_DATA_OVERLAPS(base, len) \ + ((NS_DATA_FLASH(WOLFBOOT_NS_DATA_ADDRESS) < \ + (NS_DATA_FLASH(base) + (len))) && \ + (NS_DATA_FLASH(base) < \ + (NS_DATA_FLASH(WOLFBOOT_NS_DATA_ADDRESS) + WOLFBOOT_NS_DATA_SIZE))) +#if ((WOLFBOOT_NS_DATA_SIZE) == 0) || \ + (((WOLFBOOT_NS_DATA_ADDRESS) % 32) != 0) || \ + (((WOLFBOOT_NS_DATA_SIZE) % 32) != 0) +#error "WOLFBOOT_NS_DATA_ADDRESS and _SIZE must be non-zero multiples of 32" +#endif +#if ((WOLFBOOT_NS_DATA_ADDRESS) + (WOLFBOOT_NS_DATA_SIZE)) > 0x100000000 +#error "WOLFBOOT_NS_DATA_ADDRESS + WOLFBOOT_NS_DATA_SIZE passes 4 GB" +#endif +#if NS_DATA_FLASH(WOLFBOOT_NS_DATA_ADDRESS) < \ + NS_DATA_FLASH(WOLFBOOT_PARTITION_BOOT_ADDRESS) +#error "WOLFBOOT_NS_DATA_ADDRESS must be above wolfBoot" +#endif +#if NS_DATA_OVERLAPS(WOLFBOOT_PARTITION_BOOT_ADDRESS, WOLFBOOT_PARTITION_SIZE) +#error "WOLFBOOT_NS_DATA_ADDRESS overlaps the boot partition" +#endif +#if NS_DATA_OVERLAPS(WOLFBOOT_PARTITION_UPDATE_ADDRESS, \ + WOLFBOOT_PARTITION_UPDATE_SIZE) +#error "WOLFBOOT_NS_DATA_ADDRESS overlaps the update partition" +#endif +#if NS_DATA_OVERLAPS(WOLFBOOT_PARTITION_SWAP_ADDRESS, WOLFBOOT_SECTOR_SIZE) +#error "WOLFBOOT_NS_DATA_ADDRESS overlaps the swap partition" +#endif +#endif + +/* Non-secure application data kept in flash outside the partitions (device + * credentials, settings): a SAU window over WOLFBOOT_NS_DATA_ADDRESS for + * WOLFBOOT_NS_DATA_SIZE bytes. The same pages must be non-secure in the + * SECWM option bytes, which hal_tz_release_nonsecure_area does not touch. + * A secure application has no non-secure side, so it gets no window. */ +static void sau_init_ns_data(uint32_t region) +{ +#if defined(WOLFBOOT_NS_DATA_ADDRESS) && defined(WOLFBOOT_NS_DATA_SIZE) && \ + !defined(WOLFBOOT_SECURE_APP) + /* the window is in the non-secure alias, whichever one is configured */ + uint32_t start = (uint32_t)(WOLFBOOT_NS_DATA_ADDRESS) & ~0x04000000UL; + + sau_init_region(region, start, start + WOLFBOOT_NS_DATA_SIZE - 1, 0); +#else + (void)region; +#endif +} + #ifdef TARGET_stm32h5 void hal_tz_sau_init(void) @@ -369,6 +422,8 @@ void hal_tz_sau_init(void) /* Set as non-secure: OTP + RO area */ sau_init_region(4, 0x08FFF000, 0x08FFFFFF, 0); + sau_init_ns_data(5); + /* Enable SAU */ SAU_CTRL = SAU_INIT_CTRL_ENABLE; @@ -401,6 +456,8 @@ void hal_tz_sau_init(void) sau_init_region(3, 0x40000000, 0x4FFFFFFF, 0); #endif + sau_init_ns_data(5); + /* Enable SAU */ SAU_CTRL = SAU_INIT_CTRL_ENABLE; diff --git a/options.mk b/options.mk index 46ae658032..c00056b0af 100644 --- a/options.mk +++ b/options.mk @@ -2567,6 +2567,15 @@ endif ifeq ($(TZEN),1) CFLAGS+=-DTZEN + # non-secure application data outside the partitions (STM32L5/U5/H5): + # a SAU window the non-secure image may read and program + ifneq ($(WOLFBOOT_NS_DATA_ADDRESS),) + ifeq ($(WOLFBOOT_NS_DATA_SIZE),) + $(error WOLFBOOT_NS_DATA_ADDRESS requires WOLFBOOT_NS_DATA_SIZE) + endif + CFLAGS+=-DWOLFBOOT_NS_DATA_ADDRESS=$(WOLFBOOT_NS_DATA_ADDRESS) + CFLAGS+=-DWOLFBOOT_NS_DATA_SIZE=$(WOLFBOOT_NS_DATA_SIZE) + endif endif ifeq ($(WOLFBOOT_SECURE_APP),1) diff --git a/tools/config.mk b/tools/config.mk index ee5fa9d734..c046935c33 100644 --- a/tools/config.mk +++ b/tools/config.mk @@ -141,6 +141,7 @@ CONFIG_VARS:= ARCH TARGET SIGN HASH MCUXSDK MCUXPRESSO MCUXPRESSO_CPU MCUXPRESSO WOLFBOOT_PARTITION_SIZE WOLFBOOT_SECTOR_SIZE WOLFBOOT_RAM_SIZE \ WOLFBOOT_PARTITION_BOOT_ADDRESS WOLFBOOT_PARTITION_UPDATE_ADDRESS \ WOLFBOOT_PARTITION_SWAP_ADDRESS WOLFBOOT_LOAD_ADDRESS \ + WOLFBOOT_NS_DATA_ADDRESS WOLFBOOT_NS_DATA_SIZE \ WOLFBOOT_LOAD_DTS_ADDRESS WOLFBOOT_LOAD_RAMDISK_ADDRESS \ WOLFBOOT_LOAD_KERNEL_ADDRESS \ WOLFBOOT_VERSION_SECTION \ diff --git a/tools/scripts/set-stm32-tz-option-bytes.sh b/tools/scripts/set-stm32-tz-option-bytes.sh index b4d3dd2547..a9ecdf1f8c 100755 --- a/tools/scripts/set-stm32-tz-option-bytes.sh +++ b/tools/scripts/set-stm32-tz-option-bytes.sh @@ -39,6 +39,42 @@ last_wolfboot_sector_hex="$(printf "0x%x" "$last_wolfboot_sector")" last_partition_sector=$((last_wolfboot_sector + WOLFBOOT_PARTITION_SIZE / WOLFBOOT_SECTOR_SIZE)) last_partition_sector_hex="$(printf "0x%x" "$last_partition_sector")" +# Non-secure application data outside the partitions (WOLFBOOT_NS_DATA_ADDRESS +# and _SIZE). On STM32H5 the bank 2 watermark covers the whole bank, so it +# 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 +# STM32L5 and STM32U5 bank 2 is non-secure already. +WOLFBOOT_NS_DATA_ADDRESS="$(awk -F '?=|:=|=' '$1 == "WOLFBOOT_NS_DATA_ADDRESS" { print $2 }' .config)" +WOLFBOOT_NS_DATA_SIZE="$(awk -F '?=|:=|=' '$1 == "WOLFBOOT_NS_DATA_SIZE" { print $2 }' .config)" +if [ "$TARGET" = stm32h5 ] && [ -n "$WOLFBOOT_NS_DATA_ADDRESS" ]; then + WOLFBOOT_PARTITION_UPDATE_ADDRESS="$(awk -F '?=|:=|=' '$1 == "WOLFBOOT_PARTITION_UPDATE_ADDRESS" { print $2 }' .config)" + WOLFBOOT_PARTITION_SWAP_ADDRESS="$(awk -F '?=|:=|=' '$1 == "WOLFBOOT_PARTITION_SWAP_ADDRESS" { print $2 }' .config)" + WOLFBOOT_PARTITION_UPDATE_SIZE="$(awk -F '?=|:=|=' '$1 == "WOLFBOOT_PARTITION_UPDATE_SIZE" { print $2 }' .config)" + FLASH_BANK2_OFFSET=0x08100000 + FLASH_END=0x08200000 + ns_alias=0x04000000 + ns_data=$((WOLFBOOT_NS_DATA_ADDRESS & ~ns_alias)) + ns_end=$((ns_data + ${WOLFBOOT_NS_DATA_SIZE:-0})) + update_end=$(((WOLFBOOT_PARTITION_UPDATE_ADDRESS & ~ns_alias) + ${WOLFBOOT_PARTITION_UPDATE_SIZE:-$WOLFBOOT_PARTITION_SIZE})) + swap_end=$(((WOLFBOOT_PARTITION_SWAP_ADDRESS & ~ns_alias) + WOLFBOOT_SECTOR_SIZE)) + if [ "$ns_end" -le "$ns_data" ] || [ "$ns_data" -lt $((FLASH_BANK2_OFFSET)) ] || \ + [ "$ns_end" -gt $((FLASH_END)) ]; then + echo "Error: WOLFBOOT_NS_DATA_ADDRESS and _SIZE must describe an area inside flash bank 2" >&2 + exit 1 + fi + if [ $(((ns_data - FLASH_BANK2_OFFSET) % WOLFBOOT_SECTOR_SIZE)) -ne 0 ]; then + echo "Error: WOLFBOOT_NS_DATA_ADDRESS must start on a flash sector boundary" >&2 + exit 1 + fi + if [ "$ns_data" -lt "$update_end" ] || [ "$ns_data" -lt "$swap_end" ]; then + echo "Error: WOLFBOOT_NS_DATA_ADDRESS must be above the update and swap partitions" >&2 + exit 1 + fi + # bank 2 stays secure up to the sector before the data area + last_partition_sector=$(((ns_data - FLASH_BANK2_OFFSET) / WOLFBOOT_SECTOR_SIZE - 1)) + last_partition_sector_hex="$(printf "0x%x" "$last_partition_sector")" +fi + declare -a obn # option bytes names declare -a obv # option bytes values oblen=0 # length of the above arrays