diff --git a/.gitignore b/.gitignore index f1a7bb4295..308f720ddb 100644 --- a/.gitignore +++ b/.gitignore @@ -705,6 +705,10 @@ tools/unit-tests/unit-stm32h7-trailer-journal tools/unit-tests/unit-stm32l4-write tools/unit-tests/unit-stm32u3-flash-write tools/unit-tests/unit-stm32wb-write +tools/unit-tests/unit-stm32-tz-nsdata +tools/unit-tests/unit-stm32-tz-nsdata-none +tools/unit-tests/unit-stm32-tz-nsdata-salias +tools/unit-tests/unit-stm32-tz-nsdata-sapp tools/unit-tests/unit-t10xx-flash-status tools/unit-tests/unit-ti-hercules-erase tools/unit-tests/unit-update-disk-fsp diff --git a/CMakeLists.txt b/CMakeLists.txt index 888accf65e..289723126b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -763,6 +763,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..f66bbac8a5 100644 --- a/docs/STM32-TZ.md +++ b/docs/STM32-TZ.md @@ -100,6 +100,37 @@ 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, inside internal flash and clear of the boot, update and +swap partitions; the build stops otherwise. Flash is erased a whole page at a +time (8 KB on STM32H5 and STM32U5, 2 KB on STM32L5 in the dual-bank mode +wolfBoot uses), so an area the application rewrites should start and end on +page boundaries and have its pages to itself, the last one included when the +data ends partway through it. 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 27a9153df7..341939e5af 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..64405bde09 100644 --- a/hal/stm32_tz.c +++ b/hal/stm32_tz.c @@ -340,6 +340,68 @@ 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) +#error "WOLFBOOT_NS_DATA_ADDRESS needs the partition addresses at build time (not PULL_LINKER_DEFINES)" +#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) < FLASHMEM_ADDRESS_SPACE) || \ + ((NS_DATA_FLASH(WOLFBOOT_NS_DATA_ADDRESS) + (WOLFBOOT_NS_DATA_SIZE)) > \ + ((FLASH_TOP) + 1ULL)) +#error "WOLFBOOT_NS_DATA_ADDRESS and _SIZE must stay inside internal flash" +#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 +431,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 +465,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 d29b117cac..1cc8f58ac6 100644 --- a/options.mk +++ b/options.mk @@ -2571,6 +2571,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 ead717fbd7..da86a04032 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..99a97c7b5f 100755 --- a/tools/scripts/set-stm32-tz-option-bytes.sh +++ b/tools/scripts/set-stm32-tz-option-bytes.sh @@ -13,14 +13,14 @@ if [ ! -f .config ]; then fi -TARGET="$(awk -F '?=|:=|=' '$1 == "TARGET" { print $2 }' .config)" +TARGET="$(awk -F '[?]=|:=|=' '$1 == "TARGET" { print $2 }' .config)" if [ -z "$TARGET" -o \( "$TARGET" != "stm32h5" -a "$TARGET" != "stm32l5" -a "$TARGET" != "stm32u5" \) ]; then echo "Error: target found in .config: '$TARGET'." >&2 echo "This script only supports the targets stm32h5, stm32l5 and stm32u5." >&2 exit 1 fi -TZEN="$(awk -F '?=|:=|=' '$1 == "TZEN" { print $2 }' .config)" +TZEN="$(awk -F '[?]=|:=|=' '$1 == "TZEN" { print $2 }' .config)" if [ -z "$TZEN" -o "$TZEN" != 1 ]; then echo "Error: this script requires a .config with TZEN=1" >&2 exit 1 @@ -28,10 +28,10 @@ fi FLASH_OFFSET=0x08000000 -WOLFBOOT_PARTITION_BOOT_ADDRESS="$(awk -F '?=|:=|=' '$1 == "WOLFBOOT_PARTITION_BOOT_ADDRESS" { print $2 }' .config)" -WOLFBOOT_PARTITION_SIZE="$(awk -F '?=|:=|=' '$1 == "WOLFBOOT_PARTITION_SIZE" { print $2 }' .config)" -WOLFBOOT_SECTOR_SIZE="$(awk -F '?=|:=|=' '$1 == "WOLFBOOT_SECTOR_SIZE" { print $2 }' .config)" -DUALBANK_SWAP="$(awk -F '?=|:=|=' '$1 == "DUALBANK_SWAP" { print $2 }' .config)" +WOLFBOOT_PARTITION_BOOT_ADDRESS="$(awk -F '[?]=|:=|=' '$1 == "WOLFBOOT_PARTITION_BOOT_ADDRESS" { print $2 }' .config)" +WOLFBOOT_PARTITION_SIZE="$(awk -F '[?]=|:=|=' '$1 == "WOLFBOOT_PARTITION_SIZE" { print $2 }' .config)" +WOLFBOOT_SECTOR_SIZE="$(awk -F '[?]=|:=|=' '$1 == "WOLFBOOT_SECTOR_SIZE" { print $2 }' .config)" +DUALBANK_SWAP="$(awk -F '[?]=|:=|=' '$1 == "DUALBANK_SWAP" { print $2 }' .config)" last_wolfboot_sector=$(((WOLFBOOT_PARTITION_BOOT_ADDRESS - FLASH_OFFSET - 1) / WOLFBOOT_SECTOR_SIZE)) last_wolfboot_sector_hex="$(printf "0x%x" "$last_wolfboot_sector")" @@ -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 diff --git a/tools/scripts/test-set-stm32-tz-option-bytes.sh b/tools/scripts/test-set-stm32-tz-option-bytes.sh new file mode 100755 index 0000000000..3f8d44bcf8 --- /dev/null +++ b/tools/scripts/test-set-stm32-tz-option-bytes.sh @@ -0,0 +1,135 @@ +#!/bin/bash + +# test-set-stm32-tz-option-bytes.sh +# +# Runs set-stm32-tz-option-bytes.sh against a stub STM32_Programmer_CLI and +# checks the STM32H5 bank 2 watermark (SECWM2_END) it would program for the +# non-secure data window (WOLFBOOT_NS_DATA_ADDRESS, WOLFBOOT_NS_DATA_SIZE), +# and that the layouts it must refuse are refused. +# +# Copyright (C) 2026 wolfSSL Inc. +# +# This file is part of wolfBoot. +# +# wolfBoot is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# wolfBoot is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, see . + +set -eu + +SCRIPT="$(CDPATH='' cd -- "$(dirname -- "$0")" && pwd)/set-stm32-tz-option-bytes.sh" +WORK="$(mktemp -d)" +trap 'rm -rf "$WORK"' EXIT + +mkdir -p "$WORK/bin" +cat > "$WORK/bin/STM32_Programmer_CLI" <<'EOF' +#!/bin/sh +echo "$@" > "$STUB_ARGS" +EOF +chmod +x "$WORK/bin/STM32_Programmer_CLI" +export PATH="$WORK/bin:$PATH" +export STUB_ARGS="$WORK/args" + +# stm32h5-tz.config, partitions in the secure alias +BASE="TARGET=stm32h5 TZEN=1 WOLFBOOT_SECTOR_SIZE=0x2000 +WOLFBOOT_PARTITION_SIZE=0xA0000 WOLFBOOT_PARTITION_BOOT_ADDRESS=0x08060000 +WOLFBOOT_PARTITION_UPDATE_ADDRESS=0x0C100000 +WOLFBOOT_PARTITION_SWAP_ADDRESS=0x0C1A0000" + +# .config from BASE with the KEY=VALUE arguments replacing or adding keys +write_config() +{ + local kv key + : > "$WORK/.config" + for kv in $BASE "$@"; do + key="${kv%%=*}" + grep -v "^$key=" "$WORK/.config" > "$WORK/.config.tmp" || true + mv "$WORK/.config.tmp" "$WORK/.config" + echo "$kv" >> "$WORK/.config" + done +} + +run_script() +{ + rm -f "$STUB_ARGS" + (cd "$WORK" && echo y | bash "$SCRIPT") > "$WORK/out" 2>&1 +} + +expect_secwm2_end() +{ + local expected="$1" actual + shift + write_config "$@" + if ! run_script; then + echo "refused, expected SECWM2_END=$expected: $*" >&2 + cat "$WORK/out" >&2 + exit 1 + fi + actual="$(grep -o 'SECWM2_END=0x[0-9a-f]*' "$STUB_ARGS" || true)" + if [ "$actual" != "SECWM2_END=$expected" ]; then + echo "expected SECWM2_END=$expected, got '$actual': $*" >&2 + exit 1 + fi +} + +expect_refused() +{ + local expected="$1" + shift + write_config "$@" + if run_script; then + echo "accepted, expected a refusal: $*" >&2 + exit 1 + fi + if [ -f "$STUB_ARGS" ]; then + echo "programmer called for a refused layout: $*" >&2 + exit 1 + fi + if ! grep -Fq "$expected" "$WORK/out"; then + echo "expected '$expected': $*" >&2 + cat "$WORK/out" >&2 + exit 1 + fi +} + +# no window: bank 2 secure to its last sector +expect_secwm2_end 0x7f +# the window at 0x081A2000 leaves sectors 0x51 up non-secure, in either alias +expect_secwm2_end 0x50 WOLFBOOT_NS_DATA_ADDRESS=0x081A2000 \ + WOLFBOOT_NS_DATA_SIZE=0x6000 +expect_secwm2_end 0x50 WOLFBOOT_NS_DATA_ADDRESS=0x0C1A2000 \ + WOLFBOOT_NS_DATA_SIZE=0x6000 +# an update partition larger than the boot partition moves the window up +expect_secwm2_end 0x5f WOLFBOOT_PARTITION_SIZE=0x80000 \ + WOLFBOOT_PARTITION_UPDATE_ADDRESS=0x08100000 \ + WOLFBOOT_PARTITION_UPDATE_SIZE=0xC0000 \ + WOLFBOOT_PARTITION_SWAP_ADDRESS=0x080E0000 \ + WOLFBOOT_NS_DATA_ADDRESS=0x081C0000 WOLFBOOT_NS_DATA_SIZE=0x6000 + +expect_refused "inside flash bank 2" WOLFBOOT_NS_DATA_ADDRESS=0x081A2000 +expect_refused "inside flash bank 2" WOLFBOOT_NS_DATA_ADDRESS=0x081FE000 \ + WOLFBOOT_NS_DATA_SIZE=0x4000 +expect_refused "inside flash bank 2" WOLFBOOT_NS_DATA_ADDRESS=0x080F0000 \ + WOLFBOOT_NS_DATA_SIZE=0x2000 +expect_refused "sector boundary" WOLFBOOT_NS_DATA_ADDRESS=0x081A2020 \ + WOLFBOOT_NS_DATA_SIZE=0x6000 +expect_refused "above the update and swap" \ + WOLFBOOT_NS_DATA_ADDRESS=0x08190000 WOLFBOOT_NS_DATA_SIZE=0x2000 +expect_refused "above the update and swap" \ + WOLFBOOT_NS_DATA_ADDRESS=0x081A0000 WOLFBOOT_NS_DATA_SIZE=0x2000 +expect_refused "above the update and swap" WOLFBOOT_PARTITION_SIZE=0x80000 \ + WOLFBOOT_PARTITION_UPDATE_ADDRESS=0x08100000 \ + WOLFBOOT_PARTITION_UPDATE_SIZE=0xC0000 \ + WOLFBOOT_PARTITION_SWAP_ADDRESS=0x080E0000 \ + WOLFBOOT_NS_DATA_ADDRESS=0x081A2000 WOLFBOOT_NS_DATA_SIZE=0x6000 + +echo "set-stm32-tz-option-bytes.sh STM32H5 data window: ok" diff --git a/tools/unit-tests/Makefile b/tools/unit-tests/Makefile index 2b443e2754..fd119b0dd8 100644 --- a/tools/unit-tests/Makefile +++ b/tools/unit-tests/Makefile @@ -230,6 +230,8 @@ TESTS+=unit-stm32l5-write TESTS+=unit-stm32u5-write TESTS+=unit-stm32c0-write TESTS+=unit-stm32h7s-write +TESTS+=unit-stm32-tz-nsdata unit-stm32-tz-nsdata-salias +TESTS+=unit-stm32-tz-nsdata-none unit-stm32-tz-nsdata-sapp TESTS+=unit-nvm-cache-scrub TESTS+=unit-nvm-journal unit-nvm-journal-ecc TESTS+=unit-nvm-journal-dual unit-nvm-journal-dual-ecc @@ -375,6 +377,8 @@ run: $(TESTS) WOLFBOOT_SECTOR_SIZE=0x400 ./$$unit || exit 1; \ done ./test-secure-handoff-config.sh + ./test-stm32-tz-nsdata-config.sh + ../scripts/test-set-stm32-tz-option-bytes.sh python3 unit-sign-delta-tlv.py || exit 1 python3 unit-sign-delta-cert-inv-off.py || exit 1 python3 unit-sign-delta-basehash-cleanup.py || exit 1 @@ -2762,6 +2766,26 @@ stm32u5_write_extract.h: ../../hal/stm32u5.c unit-stm32u5-write: unit-stm32u5-write.c stm32u5_write_extract.h gcc -o $@ unit-stm32u5-write.c $(CFLAGS) $(LDFLAGS) +# The STM32 TrustZone non-secure data window (SAU region 5), on the +# stm32h5-tz layout: the address in either alias, no window, and a secure +# application, which gets none. test-stm32-tz-nsdata-config.sh checks the +# placements the build refuses. +STM32_TZ_NSDATA=-DWOLFBOOT_NS_DATA_SIZE=0x6000 +unit-stm32-tz-nsdata: unit-stm32-tz-nsdata.c ../../hal/stm32_tz.c + gcc -o $@ unit-stm32-tz-nsdata.c -I../.. $(CFLAGS) $(STM32_TZ_NSDATA) \ + -DWOLFBOOT_NS_DATA_ADDRESS=0x081A2000 $(LDFLAGS) + +unit-stm32-tz-nsdata-salias: unit-stm32-tz-nsdata.c ../../hal/stm32_tz.c + gcc -o $@ unit-stm32-tz-nsdata.c -I../.. $(CFLAGS) $(STM32_TZ_NSDATA) \ + -DWOLFBOOT_NS_DATA_ADDRESS=0x0C1A2000 $(LDFLAGS) + +unit-stm32-tz-nsdata-none: unit-stm32-tz-nsdata.c ../../hal/stm32_tz.c + gcc -o $@ unit-stm32-tz-nsdata.c -I../.. $(CFLAGS) $(LDFLAGS) + +unit-stm32-tz-nsdata-sapp: unit-stm32-tz-nsdata.c ../../hal/stm32_tz.c + gcc -o $@ unit-stm32-tz-nsdata.c -I../.. $(CFLAGS) $(STM32_TZ_NSDATA) \ + -DWOLFBOOT_NS_DATA_ADDRESS=0x081A2000 -DWOLFBOOT_SECURE_APP $(LDFLAGS) + # unit-nvm-cache-scrub runs the real trailer_write() and # partition_magic_write() from src/libwolfboot.c (under # NVM_FLASH_WRITEONCE the staged sector can hold the firmware diff --git a/tools/unit-tests/test-stm32-tz-nsdata-config.sh b/tools/unit-tests/test-stm32-tz-nsdata-config.sh new file mode 100755 index 0000000000..c7a57ad49e --- /dev/null +++ b/tools/unit-tests/test-stm32-tz-nsdata-config.sh @@ -0,0 +1,91 @@ +#!/bin/sh + +# test-stm32-tz-nsdata-config.sh +# +# Placement rules of the STM32 TrustZone non-secure data window +# (WOLFBOOT_NS_DATA_ADDRESS, WOLFBOOT_NS_DATA_SIZE in hal/stm32_tz.c): each +# bad placement must stop the build with its own error, and a good one must +# build. Uses the stm32h5-tz.config layout of unit-stm32-tz-nsdata.c. +# +# Copyright (C) 2026 wolfSSL Inc. +# +# This file is part of wolfBoot. +# +# wolfBoot is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# wolfBoot is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, see . + +set -eu + +DIR=$(CDPATH='' cd -- "$(dirname -- "$0")" && pwd) +CC=${CC:-gcc} +CFLAGS="-I$DIR -I$DIR/../../src -I$DIR/../../include \ +-I$DIR/../../lib/wolfssl -I$DIR/../.. -DUNIT_TEST -DWOLFSSL_USER_SETTINGS \ +-std=gnu17" + +compile() +{ + # shellcheck disable=SC2086 + $CC $CFLAGS -fsyntax-only "$@" "$DIR/unit-stm32-tz-nsdata.c" 2>&1 +} + +expect_rejected() +{ + expected=$1 + shift + if output=$(compile "$@"); then + printf 'placement unexpectedly accepted: %s\n' "$*" >&2 + exit 1 + fi + if ! printf '%s\n' "$output" | grep -Fq "$expected"; then + printf 'placement %s: expected "%s", got:\n%s\n' "$*" "$expected" \ + "$output" >&2 + exit 1 + fi +} + +expect_accepted() +{ + if ! output=$(compile "$@"); then + printf 'placement unexpectedly refused: %s\n%s\n' "$*" "$output" >&2 + exit 1 + fi +} + +window() +{ + printf -- '-DWOLFBOOT_NS_DATA_ADDRESS=%s -DWOLFBOOT_NS_DATA_SIZE=%s' "$1" "$2" +} + +# shellcheck disable=SC2046 +{ +expect_accepted $(window 0x081A2000 0x6000) +expect_accepted $(window 0x0C1A2000 0x6000) +expect_accepted $(window 0x081B0000 0x2000) + +expect_rejected "non-zero multiples of 32" $(window 0x081A2000 0) +expect_rejected "non-zero multiples of 32" $(window 0x081A2010 0x6000) +expect_rejected "non-zero multiples of 32" $(window 0x081A2000 0x6010) +expect_rejected "inside internal flash" $(window 0x081FFFE0 0x40) +expect_rejected "inside internal flash" $(window 0x0C1FFFE0 0x40) +expect_rejected "inside internal flash" $(window 0x08200000 0x2000) +expect_rejected "must be above wolfBoot" $(window 0x08040000 0x2000) +expect_rejected "overlaps the boot partition" $(window 0x08080000 0x2000) +expect_rejected "overlaps the update partition" $(window 0x08110000 0x2000) +expect_rejected "overlaps the update partition" $(window 0x081B0000 0x2000) \ + -DWOLFBOOT_PARTITION_UPDATE_SIZE=0xC0000 +expect_rejected "overlaps the swap partition" $(window 0x081A0000 0x2000) +expect_rejected "not PULL_LINKER_DEFINES" $(window 0x081A2000 0x6000) \ + -DPULL_LINKER_DEFINES +} + +printf 'stm32 TrustZone non-secure data window placements: ok\n' diff --git a/tools/unit-tests/unit-stm32-tz-nsdata.c b/tools/unit-tests/unit-stm32-tz-nsdata.c new file mode 100644 index 0000000000..4df9bc481a --- /dev/null +++ b/tools/unit-tests/unit-stm32-tz-nsdata.c @@ -0,0 +1,166 @@ +/* unit-stm32-tz-nsdata.c + * + * Unit tests for the non-secure data window of the STM32 TrustZone HAL + * (hal/stm32_tz.c): SAU region 5 over WOLFBOOT_NS_DATA_ADDRESS for + * WOLFBOOT_NS_DATA_SIZE bytes. Built once per configuration in the + * Makefile; test-stm32-tz-nsdata-config.sh checks the placement rules. + * + * Copyright (C) 2026 wolfSSL Inc. + * + * This file is part of wolfBoot. + * + * wolfBoot is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * wolfBoot is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA + */ + +#include +#include +#include + +#define TARGET_stm32h5 +#define TZEN +#define __WOLFBOOT +#define WOLFBOOT_HASH_SHA256 +#define WOLFBOOT_SIGN_ECC256 +/* Replace hal/armv8m_tz.h with a recording SAU */ +#define TZ_INCLUDED + +#define STM32_SAU_REGIONS 8 + +struct sau_region { + uint32_t start; + uint32_t end; + int nsc; + int set; +}; + +static struct sau_region sau[STM32_SAU_REGIONS]; +static int sau_bad_region; +static uint32_t mock_sau_ctrl; +static uint32_t mock_shcsr; + +#define SAU_CTRL mock_sau_ctrl +#define SAU_INIT_CTRL_ENABLE (1U << 0) +#define SCB_SHCSR mock_shcsr +#define SCB_SHCSR_SECUREFAULT_EN (1U << 19) + +static void sau_init_region(uint32_t region, uint32_t start_addr, + uint32_t end_addr, int secure) +{ + if (region >= STM32_SAU_REGIONS) { + sau_bad_region = 1; + return; + } + sau[region].start = start_addr; + sau[region].end = end_addr; + sau[region].nsc = secure; + sau[region].set = 1; +} + +/* stm32h5-tz.config layout, in place of target.h; the partitions are given + * in the secure alias, as the example configuration does */ +#define H_TARGETS_TARGET_ +#define WOLFBOOT_FIXED_PARTITIONS +#define WOLFBOOT_SECTOR_SIZE 0x2000 +#define WOLFBOOT_PARTITION_SIZE 0xA0000 +#define WOLFBOOT_PARTITION_BOOT_ADDRESS 0x08060000 +#define WOLFBOOT_PARTITION_UPDATE_ADDRESS 0x0C100000 +#define WOLFBOOT_PARTITION_SWAP_ADDRESS 0x0C1A0000 +#ifndef WOLFBOOT_PARTITION_UPDATE_SIZE +#define WOLFBOOT_PARTITION_UPDATE_SIZE WOLFBOOT_PARTITION_SIZE +#endif +#define WOLFBOOT_NSC_ADDRESS 0x0C05C000 +#define WOLFBOOT_NSC_SIZE 0x4000 +#define ARCH_FLASH_OFFSET 0x08000000 + +#include "../../hal/stm32_tz.c" + +#if defined(WOLFBOOT_NS_DATA_ADDRESS) && !defined(WOLFBOOT_SECURE_APP) + #define EXPECT_WINDOW 1 + #define EXPECT_START ((uint32_t)(WOLFBOOT_NS_DATA_ADDRESS) & 0xFBFFFFFFU) + #define EXPECT_END (EXPECT_START + (WOLFBOOT_NS_DATA_SIZE) - 1U) +#else + #define EXPECT_WINDOW 0 +#endif + +static void reset_sau(void) +{ + memset(sau, 0, sizeof(sau)); + sau_bad_region = 0; + mock_sau_ctrl = 0; + mock_shcsr = 0; +} + +START_TEST(test_ns_data_window) +{ + reset_sau(); + hal_tz_sau_init(); + + ck_assert_int_eq(sau_bad_region, 0); + ck_assert_uint_eq(mock_sau_ctrl, SAU_INIT_CTRL_ENABLE); + ck_assert_int_eq(sau[5].set, EXPECT_WINDOW); +#if EXPECT_WINDOW + /* the non-secure alias, whichever alias the address was given in */ + ck_assert_uint_eq(sau[5].start, EXPECT_START); + ck_assert_uint_eq(sau[5].end, EXPECT_END); + ck_assert_uint_eq(sau[5].start & 0x04000000U, 0); + ck_assert_int_eq(sau[5].nsc, 0); +#endif +} +END_TEST + +START_TEST(test_other_regions_unchanged) +{ + reset_sau(); + hal_tz_sau_init(); + + ck_assert(sau[0].set); + ck_assert_uint_eq(sau[0].start, WOLFBOOT_NSC_ADDRESS); + ck_assert_int_eq(sau[0].nsc, 1); +#if defined(WOLFBOOT_SECURE_APP) + ck_assert(!sau[1].set); +#else + ck_assert(sau[1].set); + ck_assert_uint_eq(sau[1].start, WOLFBOOT_PARTITION_BOOT_ADDRESS); +#endif + ck_assert(sau[4].set); + ck_assert_uint_eq(sau[4].start, 0x08FFF000U); + ck_assert(!sau[6].set); + ck_assert(!sau[7].set); +} +END_TEST + +static Suite *stm32_tz_nsdata_suite(void) +{ + Suite *s = suite_create("stm32-tz-nsdata"); + TCase *tc = tcase_create("sau"); + + tcase_add_test(tc, test_ns_data_window); + tcase_add_test(tc, test_other_regions_unchanged); + suite_add_tcase(s, tc); + return s; +} + +int main(void) +{ + int fails; + Suite *s = stm32_tz_nsdata_suite(); + SRunner *sr = srunner_create(s); + + srunner_run_all(sr, CK_NORMAL); + fails = srunner_ntests_failed(sr); + srunner_free(sr); + + return fails; +}