From 709faedd948fed8c3cc0886eb85ba55d7a904251 Mon Sep 17 00:00:00 2001 From: Aidan Garske Date: Mon, 9 Nov 2026 14:56:39 -0800 Subject: [PATCH 01/18] Derive the STM32L5 PSA lifecycle from the RDP and TZEN option bytes --- hal/stm32l5.c | 8 +-- hal/stm32l5_lifecycle.h | 67 ++++++++++++++++++++++++++ tools/unit-tests/unit-secure-handoff.c | 54 +++++++++++++++++++++ 3 files changed, 123 insertions(+), 6 deletions(-) create mode 100644 hal/stm32l5_lifecycle.h diff --git a/hal/stm32l5.c b/hal/stm32l5.c index 48ebaa2039..8c03662e9c 100644 --- a/hal/stm32l5.c +++ b/hal/stm32l5.c @@ -36,6 +36,7 @@ #include "hal.h" #include "hal/stm32l5.h" +#include "hal/stm32l5_lifecycle.h" static void RAMFUNCTION flash_set_waitstates(unsigned int waitstates) @@ -220,12 +221,7 @@ int hal_uds_derive_key(uint8_t *out, size_t out_len) int hal_attestation_get_lifecycle(uint32_t *lifecycle) { - if (lifecycle == NULL) { - return -1; - } - - *lifecycle = 0x3000u; /* PSA_LIFECYCLE_SECURED (default) */ - return 0; + return stm32l5_optr_to_psa_lifecycle(FLASH_OPTR, lifecycle); } void RAMFUNCTION hal_flash_unlock(void) diff --git a/hal/stm32l5_lifecycle.h b/hal/stm32l5_lifecycle.h new file mode 100644 index 0000000000..8dad16a552 --- /dev/null +++ b/hal/stm32l5_lifecycle.h @@ -0,0 +1,67 @@ +/* stm32l5_lifecycle.h + * + * 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 . + */ + +#ifndef WOLFBOOT_STM32L5_LIFECYCLE_H +#define WOLFBOOT_STM32L5_LIFECYCLE_H + +#include +#include + +#define STM32L5_FLASH_OPTR_RDP_MASK 0xFFu +#define STM32L5_FLASH_OPTR_TZEN (1u << 31) + +#define STM32L5_RDP_LEVEL_0 0xAAu +#define STM32L5_RDP_LEVEL_0_5 0x55u +#define STM32L5_RDP_LEVEL_2 0xCCu + +/* Maps FLASH_OPTR to a PSA lifecycle. Without TZEN there is no Secure + * world to attest, so the state is reported as unknown (-1). */ +static inline int stm32l5_optr_to_psa_lifecycle(uint32_t optr, + uint32_t *lifecycle) +{ + int ret = 0; + + if (lifecycle == NULL) { + ret = -1; + } + else if ((optr & STM32L5_FLASH_OPTR_TZEN) == 0u) { + ret = -1; + } + else { + switch (optr & STM32L5_FLASH_OPTR_RDP_MASK) { + case STM32L5_RDP_LEVEL_0: + *lifecycle = 0x2000u; /* PSA_LIFECYCLE_PSA_ROT_PROVISIONING */ + break; + case STM32L5_RDP_LEVEL_0_5: + *lifecycle = 0x4000u; /* PSA_LIFECYCLE_NON_PSA_ROT_DEBUG */ + break; + case STM32L5_RDP_LEVEL_2: + *lifecycle = 0x3000u; /* PSA_LIFECYCLE_SECURED */ + break; + default: + /* Any other value is RDP level 1 */ + *lifecycle = 0x5000u; /* PSA_LIFECYCLE_RECOVERABLE_PSA_ROT_DEBUG */ + break; + } + } + return ret; +} + +#endif /* WOLFBOOT_STM32L5_LIFECYCLE_H */ diff --git a/tools/unit-tests/unit-secure-handoff.c b/tools/unit-tests/unit-secure-handoff.c index 7148d30ace..c7bb0c4d25 100644 --- a/tools/unit-tests/unit-secure-handoff.c +++ b/tools/unit-tests/unit-secure-handoff.c @@ -24,6 +24,7 @@ #define WOLFBOOT_HASH_SHA256 #include "wolfboot/secure_handoff.h" #include "../../hal/stm32h5_lifecycle.h" +#include "../../hal/stm32l5_lifecycle.h" static int test_secure_handoff_record(void) { @@ -153,6 +154,56 @@ static int test_stm32h5_debug_lifecycle_map(void) return 0; } +static int stm32l5_expect_lifecycle(uint32_t optr, uint32_t expected) +{ + uint32_t lifecycle = 0xFFFFFFFFu; + + if (stm32l5_optr_to_psa_lifecycle(optr, &lifecycle) != 0) { + return -1; + } + return (lifecycle == expected) ? 0 : -1; +} + +static int test_stm32l5_optr_lifecycle_map(void) +{ + uint32_t lifecycle = 0x1234u; + + if (stm32l5_expect_lifecycle(STM32L5_FLASH_OPTR_TZEN | + STM32L5_RDP_LEVEL_0, 0x2000u) != 0) { + return -1; + } + if (stm32l5_expect_lifecycle(STM32L5_FLASH_OPTR_TZEN | + STM32L5_RDP_LEVEL_0_5, 0x4000u) != 0) { + return -1; + } + if (stm32l5_expect_lifecycle(STM32L5_FLASH_OPTR_TZEN | + STM32L5_RDP_LEVEL_2, 0x3000u) != 0) { + return -1; + } + if ((stm32l5_expect_lifecycle(STM32L5_FLASH_OPTR_TZEN | 0xBBu, + 0x5000u) != 0) || + (stm32l5_expect_lifecycle(STM32L5_FLASH_OPTR_TZEN | 0x00u, + 0x5000u) != 0) || + (stm32l5_expect_lifecycle(STM32L5_FLASH_OPTR_TZEN | 0xFFu, + 0x5000u) != 0)) { + return -1; + } + /* Only RDP and TZEN select the lifecycle */ + if (stm32l5_expect_lifecycle(0x7FFFFF00u | STM32L5_FLASH_OPTR_TZEN | + STM32L5_RDP_LEVEL_2, 0x3000u) != 0) { + return -1; + } + if ((stm32l5_optr_to_psa_lifecycle(STM32L5_RDP_LEVEL_2, &lifecycle) != + -1) || (lifecycle != 0x1234u)) { + return -1; + } + if (stm32l5_optr_to_psa_lifecycle(STM32L5_FLASH_OPTR_TZEN | + STM32L5_RDP_LEVEL_0, NULL) != -1) { + return -1; + } + return 0; +} + int main(void) { if (test_secure_handoff_record() != 0) { @@ -167,5 +218,8 @@ int main(void) if (test_stm32h5_debug_lifecycle_map() != 0) { return 1; } + if (test_stm32l5_optr_lifecycle_map() != 0) { + return 1; + } return 0; } From 8ab44fab80ef786fbe74f563ca277f448a77dc59 Mon Sep 17 00:00:00 2001 From: Aidan Garske Date: Mon, 9 Nov 2026 14:56:44 -0800 Subject: [PATCH 02/18] Keep STM32L5 flash, SRAM and peripherals Secure for a secure application handoff --- Makefile | 6 +++- arch.mk | 3 ++ hal/stm32_tz.c | 15 +++++++++ hal/stm32l5-secure-app.ld | 70 +++++++++++++++++++++++++++++++++++++++ hal/stm32l5.c | 14 ++++++-- 5 files changed, 105 insertions(+), 3 deletions(-) create mode 100644 hal/stm32l5-secure-app.ld diff --git a/Makefile b/Makefile index 4c85898abf..e1f6cedebb 100644 --- a/Makefile +++ b/Makefile @@ -402,7 +402,11 @@ TARGET_H_TEMPLATE:=include/target.h.in ifeq ($(TZEN),1) ifeq ($(TARGET),stm32l5) # Don't build a contiguous image - MAIN_TARGET:=wolfboot.bin test-app/image_v1_signed.bin + ifeq ($(WOLFBOOT_SECURE_APP),1) + MAIN_TARGET:=wolfboot.bin + else + MAIN_TARGET:=wolfboot.bin test-app/image_v1_signed.bin + endif endif ifeq ($(TARGET),stm32u5) diff --git a/arch.mk b/arch.mk index e4f8390704..ad6cc2a98d 100644 --- a/arch.mk +++ b/arch.mk @@ -680,6 +680,9 @@ ifeq ($(ARCH),ARM) ARCH_FLASH_OFFSET=0x08000000 ifeq ($(TZEN),1) WOLFBOOT_ORIGIN=0x0C000000 + ifeq ($(WOLFBOOT_SECURE_APP),1) + $(call LSCRIPT_IN_DEFAULT,hal/$(TARGET)-secure-app.ld) + endif else WOLFBOOT_ORIGIN=0x08000000 $(call LSCRIPT_IN_DEFAULT,hal/$(TARGET)-ns.ld) diff --git a/hal/stm32_tz.c b/hal/stm32_tz.c index fb3566e381..605c3308ec 100644 --- a/hal/stm32_tz.c +++ b/hal/stm32_tz.c @@ -317,6 +317,16 @@ void hal_gtzc_init(void) int i; /* One bit in the bitmask: 256B */ +#if defined(WOLFBOOT_SECURE_APP) + /* SRAM1 (192 KB) and SRAM2 (64 KB) stay Secure until the secure + * application installs its own split. */ + for (i = 0; i < 24; i++) { + SET_GTZC_MPCBBx_S_VCTR(1, i, 0xFFFFFFFF); + } + for (i = 0; i < 8; i++) { + SET_GTZC_MPCBBx_S_VCTR(2, i, 0xFFFFFFFF); + } +#else /* Configure lower half of total RAM as secure * 0x3000 0000 : 0x3001 FFFF - 128KB */ @@ -337,6 +347,7 @@ void hal_gtzc_init(void) for (i = 0; i < 8; i++) { SET_GTZC_MPCBBx_S_VCTR(2, i, 0x0); } +#endif /* WOLFBOOT_SECURE_APP */ } #endif @@ -383,6 +394,9 @@ void hal_tz_sau_init(void) sau_init_region(0, WOLFBOOT_NSC_ADDRESS, WOLFBOOT_NSC_ADDRESS + WOLFBOOT_NSC_SIZE - 1, 1); + /* A secure application is entered Secure and programs its own + * Non-secure flash and RAM windows. */ +#if !defined(WOLFBOOT_SECURE_APP) /* Non-secure: application flash area (boot partition only) */ sau_init_region(1, WOLFBOOT_PARTITION_BOOT_ADDRESS, WOLFBOOT_PARTITION_BOOT_ADDRESS + WOLFBOOT_PARTITION_SIZE - 1, @@ -390,6 +404,7 @@ void hal_tz_sau_init(void) /* Non-secure RAM region in SRAM1/SRAM2 */ sau_init_region(2, 0x20020000, 0x2003FFFF, 0); +#endif /* Non-secure: internal peripherals */ sau_init_region(3, 0x40000000, 0x4FFFFFFF, 0); diff --git a/hal/stm32l5-secure-app.ld b/hal/stm32l5-secure-app.ld new file mode 100644 index 0000000000..50bbbb73e9 --- /dev/null +++ b/hal/stm32l5-secure-app.ld @@ -0,0 +1,70 @@ +/* SRAM below 0x30030000 holds the handoff record and the secure app's RAM. */ +MEMORY +{ + FLASH (rx) : ORIGIN = @WOLFBOOT_ORIGIN@, LENGTH = @WOLFBOOT_KEYVAULT_ADDRESS@ - @WOLFBOOT_ORIGIN@ + RAM (rwx) : ORIGIN = 0x30030000, LENGTH = 0x0000C000 + RAM_HEAP (rw): ORIGIN = 0x3003C000, LENGTH = 0x4000 + FLASH_KEYVAULT(rw): ORIGIN = @WOLFBOOT_KEYVAULT_ADDRESS@, LENGTH = @WOLFBOOT_KEYVAULT_SIZE@ + FLASH_NSC(rx): ORIGIN = @WOLFBOOT_NSC_ADDRESS@, LENGTH = @WOLFBOOT_NSC_SIZE@ +} + +SECTIONS +{ + .text : + { + _start_text = .; + KEEP(*(.isr_vector)) + *(.text*) + *(.rodata*) + . = ALIGN(8); + _end_text = .; + } > FLASH + + .edidx : + { + . = ALIGN(4); + *(.ARM.exidx*) + } > FLASH + + /* Data is stored in FLASH, not after the NSC: the boot partition + * follows the NSC directly. */ + .data : + { + _start_data = .; + KEEP(*(.data*)) + . = ALIGN(8); + KEEP(*(.ramcode)) + . = ALIGN(8); + _end_data = .; + } > RAM AT > FLASH + _stored_data = LOADADDR(.data); + + .gnu.sgstubs ORIGIN(FLASH_NSC) : + { + . += 0x400; + . = ALIGN(4); + *(.gnu.sgstubs*) /* Secure Gateway stubs */ + . = ALIGN(4); + } >FLASH_NSC + + .bss (NOLOAD) : + { + _start_bss = .; + __bss_start__ = .; + *(.bss*) + *(COMMON) + . = ALIGN(8); + _end_bss = .; + __bss_end__ = .; + _end = .; + } > RAM + . = ALIGN(8); +} + +END_STACK = ORIGIN(RAM) + LENGTH(RAM); + +_flash_keyvault = ORIGIN(FLASH_KEYVAULT); +_flash_keyvault_size = LENGTH(FLASH_KEYVAULT); + +_start_heap = ORIGIN(RAM_HEAP); +_heap_size = LENGTH(RAM_HEAP); diff --git a/hal/stm32l5.c b/hal/stm32l5.c index 8c03662e9c..a3361b2986 100644 --- a/hal/stm32l5.c +++ b/hal/stm32l5.c @@ -276,6 +276,15 @@ int RAMFUNCTION hal_flash_erase(uint32_t address, int len) if (len == 0) return -1; +#if TZ_SECURE() && defined(WOLFBOOT_SECURE_APP) + /* Secure-app partitions are linked at the Secure flash alias */ + if ((address >= FLASH_SECURE_MMAP_BASE) && + (address <= (FLASH_SECURE_MMAP_BASE + + (FLASH_TOP - FLASHMEM_ADDRESS_SPACE)))) { + address -= (FLASH_SECURE_MMAP_BASE - FLASHMEM_ADDRESS_SPACE); + } +#endif + if (address < ARCH_FLASH_OFFSET) return -1; @@ -421,7 +430,8 @@ static void clock_pll_on(int powersave) DMB(); } -#if TZ_SECURE() +/* A secure application keeps every peripheral Secure across the jump */ +#if TZ_SECURE() && !defined(WOLFBOOT_SECURE_APP) static void periph_unsecure() { volatile uint32_t reg; @@ -540,7 +550,7 @@ void hal_prepare_boot(void) #ifdef WOLFBOOT_RESTORE_CLOCK clock_pll_off(); #endif -#if TZ_SECURE() +#if TZ_SECURE() && !defined(WOLFBOOT_SECURE_APP) periph_unsecure(); #endif } From 8c1539f81d91397337aa2df756a90511f5eabd91 Mon Sep 17 00:00:00 2001 From: Aidan Garske Date: Mon, 9 Nov 2026 15:09:17 -0800 Subject: [PATCH 03/18] Fix an unused variable that breaks STM32L5 DEBUG_UART builds --- hal/uart/uart_drv_stm32l5.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hal/uart/uart_drv_stm32l5.c b/hal/uart/uart_drv_stm32l5.c index 347cf3027f..de27ffdc63 100644 --- a/hal/uart/uart_drv_stm32l5.c +++ b/hal/uart/uart_drv_stm32l5.c @@ -149,7 +149,7 @@ int uart_tx(const uint8_t c) int uart_rx(uint8_t *c, int len) { volatile uint32_t reg; - int i = 0; + (void)len; reg = UART1_ISR; if (reg & UART_ISR_RX_NOTEMPTY) { *c = (uint8_t)UART1_RDR; From 361816b6c62d8d5ca91b7013045fe1217c0ce500 Mon Sep 17 00:00:00 2001 From: Aidan Garske Date: Mon, 9 Nov 2026 15:09:17 -0800 Subject: [PATCH 04/18] Add an STM32L5 wolfTrust secure application handoff config --- .github/workflows/test-configs.yml | 6 +++ config/examples/stm32l5-tz-wolftrust.config | 43 +++++++++++++++++++++ 2 files changed, 49 insertions(+) create mode 100644 config/examples/stm32l5-tz-wolftrust.config diff --git a/.github/workflows/test-configs.yml b/.github/workflows/test-configs.yml index 452ebc6a5f..b19cda5ead 100644 --- a/.github/workflows/test-configs.yml +++ b/.github/workflows/test-configs.yml @@ -1169,6 +1169,12 @@ jobs: arch: arm config-file: ./config/examples/stm32l5-wolfcrypt-tz.config + stm32l5_tz_wolftrust_test: + uses: ./.github/workflows/test-build.yml + with: + arch: arm + config-file: ./config/examples/stm32l5-tz-wolftrust.config + stm32u3_test: uses: ./.github/workflows/test-build.yml with: diff --git a/config/examples/stm32l5-tz-wolftrust.config b/config/examples/stm32l5-tz-wolftrust.config new file mode 100644 index 0000000000..4217dabe4e --- /dev/null +++ b/config/examples/stm32l5-tz-wolftrust.config @@ -0,0 +1,43 @@ +ARCH?=ARM +TZEN?=1 +TARGET?=stm32l5 +SIGN?=ECC256 +HASH?=SHA256 +DEBUG?=0 +DEBUG_UART?=0 +VTOR?=1 +CORTEX_M0?=0 +CORTEX_M33?=1 +NO_ASM?=0 +NO_MPU=1 +EXT_FLASH?=0 +SPI_FLASH?=0 +ALLOW_DOWNGRADE?=0 +NVM_FLASH_WRITEONCE?=1 +WOLFBOOT_VERSION?=1 +V?=0 +SPMATH?=1 +RAM_CODE?=1 +DUALBANK_SWAP?=0 +IMAGE_HEADER_SIZE?=1024 +WOLFCRYPT_TZ?=0 +ARMORED=1 + +WOLFBOOT_SECURE_APP?=1 +WOLFBOOT_SECURE_HANDOFF_ADDRESS?=0x30000000 + +# 2KB pages, dual bank (DBANK=1) +WOLFBOOT_SECTOR_SIZE?=0x800 + +# 48KB wolfBoot region (bootloader + empty NSC), partitions at the Secure +# flash alias: 128KB boot and the swap page in bank 1, 128KB update in bank 2. +# 0x0C038000-0x0C03DFFF is the secure app's NVM and is never touched here. +BOOTLOADER_PARTITION_SIZE?=0xC000 +WOLFBOOT_KEYVAULT_ADDRESS?=0x0C00B000 +WOLFBOOT_KEYVAULT_SIZE?=0 +WOLFBOOT_NSC_ADDRESS?=0x0C00B000 +WOLFBOOT_NSC_SIZE?=0x1000 +WOLFBOOT_PARTITION_SIZE?=0x20000 +WOLFBOOT_PARTITION_BOOT_ADDRESS?=0x0C00C000 +WOLFBOOT_PARTITION_UPDATE_ADDRESS?=0x0C040000 +WOLFBOOT_PARTITION_SWAP_ADDRESS?=0x0C03E000 From fb0ca8d365ac73d421501a1e5c84ebaa60061df3 Mon Sep 17 00:00:00 2001 From: Aidan Garske Date: Mon, 9 Nov 2026 15:13:45 -0800 Subject: [PATCH 05/18] Run the STM32L5 secure application handoff on the m33mu emulator --- .../workflows/trustzone-emulator-tests.yml | 32 ++++++ test-app/emu-test-apps/common/emu_hal.c | 103 ++++++++++++++++++ test-app/emu-test-apps/stm32l552/Makefile | 20 +++- .../stm32l552/target-secure-app.ld.in | 87 +++++++++++++++ test-app/emu-test-apps/stm32l552/uart.c | 17 ++- test-app/emu-test-apps/test.sh | 5 +- 6 files changed, 252 insertions(+), 12 deletions(-) create mode 100644 test-app/emu-test-apps/stm32l552/target-secure-app.ld.in diff --git a/.github/workflows/trustzone-emulator-tests.yml b/.github/workflows/trustzone-emulator-tests.yml index a7cd3b9430..ba13c8ddc4 100644 --- a/.github/workflows/trustzone-emulator-tests.yml +++ b/.github/workflows/trustzone-emulator-tests.yml @@ -249,3 +249,35 @@ jobs: | tee /tmp/m33mu-lpc55s69-pkcs11-second.log grep -q "pkcs11: restored persistent PKCS11 objects" /tmp/m33mu-lpc55s69-pkcs11-second.log grep -q "pkcs11: success" /tmp/m33mu-lpc55s69-pkcs11-second.log + + # Boot, update and rollback on the m33mu STM32L552 model with the + # TrustZone secure-application (wolfTrust) handoff config. + stm32l5-secure-app-emulator-tests: + runs-on: ubuntu-latest + container: + image: ghcr.io/wolfssl/wolfboot-ci-m33mu:latest + timeout-minutes: 30 + steps: + - uses: actions/checkout@v4 + + - name: Init submodules + run: | + git config --global --add safe.directory "$GITHUB_WORKSPACE" + git submodule update --init --single-branch + + - name: Configure stm32l5-tz-wolftrust and build wolfboot + run: | + cp config/examples/stm32l5-tz-wolftrust.config .config + make wolfboot.bin + + - name: Run emu test (stm32l5 secure app) + working-directory: test-app/emu-test-apps + run: | + BOOT_TIMEOUT=30 UPDATE_TIMEOUT=60 REBOOT_TIMEOUT=30 TARGET=stm32l5 ./test.sh + + - name: Upload emulator logs on failure + if: failure() + uses: actions/upload-artifact@v4 + with: + name: stm32l5-secure-app-emu-logs + path: test-app/emu-test-apps/stm32l552/*.log diff --git a/test-app/emu-test-apps/common/emu_hal.c b/test-app/emu-test-apps/common/emu_hal.c index 661b618841..e01047ea25 100644 --- a/test-app/emu-test-apps/common/emu_hal.c +++ b/test-app/emu-test-apps/common/emu_hal.c @@ -42,6 +42,95 @@ #define FLASH_CR_SNB_MASK (0x7fu << FLASH_CR_SNB_SHIFT) #endif +#if defined(EMU_STM32L5_SECURE) +/* The secure-app payload programs flash through the Secure register bank; + * the partitions sit at the Secure flash alias (DBANK=1, 2KB pages). */ +#define L5_FLASH_BASE 0x50022000u +#define L5_FLASH_SECKEYR (*(volatile uint32_t *)(L5_FLASH_BASE + 0x0Cu)) +#define L5_FLASH_SECSR (*(volatile uint32_t *)(L5_FLASH_BASE + 0x24u)) +#define L5_FLASH_SECCR (*(volatile uint32_t *)(L5_FLASH_BASE + 0x2Cu)) +#define L5_FLASH_KEY1 0x45670123u +#define L5_FLASH_KEY2 0xCDEF89ABu +#define L5_FLASH_SR_BSY (1u << 16) +#define L5_FLASH_CR_PG (1u << 0) +#define L5_FLASH_CR_PER (1u << 1) +#define L5_FLASH_CR_PNB_SHIFT 3 +#define L5_FLASH_CR_PNB_MASK (0x7Fu << L5_FLASH_CR_PNB_SHIFT) +#define L5_FLASH_CR_BKER (1u << 11) +#define L5_FLASH_CR_STRT (1u << 16) +#define L5_FLASH_CR_LOCK (1u << 31) +#define L5_FLASH_S_ALIAS 0x0C000000u +#define L5_FLASH_BANK_SIZE 0x00040000u +#define L5_FLASH_PAGE_SIZE 0x00000800u + +static void stm32l5_flash_wait(void) +{ + while ((L5_FLASH_SECSR & L5_FLASH_SR_BSY) != 0u) { + } +} + +/* Programs whole 64-bit units, keeping the bytes outside the request */ +static int stm32l5_flash_write(uint32_t address, const uint8_t *data, int len) +{ + volatile uint32_t *unit; + uint32_t dword[2]; + uint8_t *bytes = (uint8_t *)dword; + uint32_t here; + int off; + int i = 0; + int j; + + while (i < len) { + here = address + (uint32_t)i; + unit = (volatile uint32_t *)(here & ~7u); + off = (int)(here & 7u); + for (j = 0; j < 8; j++) { + if ((j >= off) && ((i + j - off) < len)) { + bytes[j] = data[i + j - off]; + } + else { + bytes[j] = ((const volatile uint8_t *)unit)[j]; + } + } + L5_FLASH_SECCR |= L5_FLASH_CR_PG; + unit[0] = dword[0]; + unit[1] = dword[1]; + stm32l5_flash_wait(); + L5_FLASH_SECCR &= ~L5_FLASH_CR_PG; + i += 8 - off; + } + return 0; +} + +static int stm32l5_flash_erase(uint32_t address, int len) +{ + uint32_t p; + uint32_t end; + uint32_t cr; + + if (address >= L5_FLASH_S_ALIAS) { + address -= L5_FLASH_S_ALIAS; + } + end = address + (uint32_t)len; + for (p = address & ~(L5_FLASH_PAGE_SIZE - 1u); p < end; + p += L5_FLASH_PAGE_SIZE) { + cr = L5_FLASH_SECCR & ~(L5_FLASH_CR_PNB_MASK | L5_FLASH_CR_BKER | + L5_FLASH_CR_PG); + cr |= L5_FLASH_CR_PER | + (((p % L5_FLASH_BANK_SIZE) / L5_FLASH_PAGE_SIZE) << + L5_FLASH_CR_PNB_SHIFT); + if (p >= L5_FLASH_BANK_SIZE) { + cr |= L5_FLASH_CR_BKER; + } + L5_FLASH_SECCR = cr; + L5_FLASH_SECCR = cr | L5_FLASH_CR_STRT; + stm32l5_flash_wait(); + } + L5_FLASH_SECCR &= ~L5_FLASH_CR_PER; + return 0; +} +#endif + #if defined(EMU_NRF5340) #define NVMC_BASE 0x40039000u #define NVMC_CONFIG (*(volatile uint32_t *)(NVMC_BASE + 0x504u)) @@ -419,6 +508,8 @@ int hal_flash_write(uint32_t address, const uint8_t *data, int len) return mcxn_flash_write(address, data, len); #elif defined(EMU_LPC55S69) return lpc55s69_flash_write(address, data, len); +#elif defined(EMU_STM32L5_SECURE) + return stm32l5_flash_write(address, data, len); #else memcpy((void *)address, data, (size_t)len); #endif @@ -460,6 +551,11 @@ int hal_flash_erase(uint32_t address, int len) return 0; } return lpc55s69_flash_erase(address, len); +#elif defined(EMU_STM32L5_SECURE) + if (len <= 0) { + return 0; + } + return stm32l5_flash_erase(address, len); #else uint32_t end; #if defined(EMU_STM32) @@ -510,6 +606,11 @@ void hal_flash_unlock(void) SYS_REGLCTL = 0x16u; SYS_REGLCTL = 0x88u; FMC_ISPCTL |= FMC_ISPCTL_ISPEN | FMC_ISPCTL_APUEN; +#elif defined(EMU_STM32L5_SECURE) + if ((L5_FLASH_SECCR & L5_FLASH_CR_LOCK) != 0u) { + L5_FLASH_SECKEYR = L5_FLASH_KEY1; + L5_FLASH_SECKEYR = L5_FLASH_KEY2; + } #elif defined(EMU_STM32) if ((FLASH_NSCR & FLASH_CR_LOCK) != 0u) { FLASH_NSKEYR = FLASH_KEY1; @@ -526,6 +627,8 @@ void hal_flash_lock(void) #if defined(EMU_M2354) FMC_ISPCTL &= ~(FMC_ISPCTL_ISPEN | FMC_ISPCTL_APUEN); SYS_REGLCTL = 0u; +#elif defined(EMU_STM32L5_SECURE) + L5_FLASH_SECCR |= L5_FLASH_CR_LOCK; #elif defined(EMU_STM32) FLASH_NSCR &= ~FLASH_CR_PG; FLASH_NSCR |= FLASH_CR_LOCK; diff --git a/test-app/emu-test-apps/stm32l552/Makefile b/test-app/emu-test-apps/stm32l552/Makefile index 9e89ca360a..8a49f6af4d 100644 --- a/test-app/emu-test-apps/stm32l552/Makefile +++ b/test-app/emu-test-apps/stm32l552/Makefile @@ -22,16 +22,24 @@ CC=arm-none-eabi-gcc OBJCOPY ?= arm-none-eabi-objcopy CFLAGS := -mcpu=cortex-m33 -mthumb -Os -ffreestanding -fdata-sections -ffunction-sections -g -ggdb -CFLAGS += -I. -I../common -I../../../include -DEMU_STM32 +CFLAGS += -I. -I../common -I../../../include CFLAGS += -DIMAGE_HEADER_SIZE=$(IMAGE_HEADER_SIZE) LDFLAGS := -nostdlib -T target.ld -Wl,-gc-sections LDLIBS := -Wl,--start-group -lc -lm -lgcc -lnosys -Wl,--end-group NSC_OBJ := -ifeq ($(TZEN),1) - CFLAGS += -DTZEN - CFLAGS += -DNONSECURE_APP - CFLAGS += -DWOLFCRYPT_SECURE_MODE - NSC_OBJ := ../../../src/wolfboot_tz_nsc.o +ifneq ($(SECURE_HANDOFF_ADDRESS),) + # Secure-app payload (WOLFBOOT_SECURE_APP): runs Secure and programs the + # flash itself; write-once like the wolfBoot build. + CFLAGS += -DEMU_STM32L5_SECURE -DNVM_FLASH_WRITEONCE + CFLAGS += -DEMU_SECURE_HANDOFF_ADDRESS=$(SECURE_HANDOFF_ADDRESS) +else + CFLAGS += -DEMU_STM32 + ifeq ($(TZEN),1) + CFLAGS += -DTZEN + CFLAGS += -DNONSECURE_APP + CFLAGS += -DWOLFCRYPT_SECURE_MODE + NSC_OBJ := ../../../src/wolfboot_tz_nsc.o + endif endif VPATH := ../common diff --git a/test-app/emu-test-apps/stm32l552/target-secure-app.ld.in b/test-app/emu-test-apps/stm32l552/target-secure-app.ld.in new file mode 100644 index 0000000000..aa00be9b1a --- /dev/null +++ b/test-app/emu-test-apps/stm32l552/target-secure-app.ld.in @@ -0,0 +1,87 @@ +/* target.ld.in + * + * 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 + */ + +/* STM32L552 secure-app test app: runs Secure from the boot partition at the + * Secure flash alias, with data and stack in Secure SRAM above the handoff + * record. */ +MEMORY +{ + FLASH (rx) : ORIGIN = @FLASH_ORIGIN@, LENGTH = @FLASH_SIZE@ + RAM (rwx) : ORIGIN = 0x30001000, LENGTH = 0x0002F000 +} + +_estack = ORIGIN(RAM) + LENGTH(RAM); +_sidata = LOADADDR(.data); + +SECTIONS +{ + .isr_vector : + { + KEEP(*(.isr_vector)) + } > FLASH + + .text : + { + *(.text*) + *(.rodata*) + *(.ARM.extab* .gnu.linkonce.armextab.*) + *(.ARM.exidx* .gnu.linkonce.armexidx.*) + *(.glue_7) + *(.glue_7t) + *(.eh_frame) + } > FLASH + + .preinit_array : + { + __preinit_array_start = .; + KEEP(*(.preinit_array*)) + __preinit_array_end = .; + } > FLASH + + .init_array : + { + __init_array_start = .; + KEEP(*(.init_array*)) + __init_array_end = .; + } > FLASH + + .fini_array : + { + __fini_array_start = .; + KEEP(*(.fini_array*)) + __fini_array_end = .; + } > FLASH + + .data : + { + _sdata = .; + *(.data*) + _edata = .; + } > RAM AT > FLASH + + .bss (NOLOAD) : + { + _sbss = .; + *(.bss*) + *(COMMON) + _ebss = .; + } > RAM +} diff --git a/test-app/emu-test-apps/stm32l552/uart.c b/test-app/emu-test-apps/stm32l552/uart.c index 0eddec05d0..b26fe9129a 100644 --- a/test-app/emu-test-apps/stm32l552/uart.c +++ b/test-app/emu-test-apps/stm32l552/uart.c @@ -24,15 +24,22 @@ #define SYSCLK_HZ 64000000u +/* The secure-app payload runs Secure and uses the Secure peripheral aliases */ +#ifdef EMU_STM32L5_SECURE +#define PERIPH_ALIAS 0x10000000u +#else +#define PERIPH_ALIAS 0x00000000u +#endif + /* RCC */ -#define RCC_BASE 0x40021000u +#define RCC_BASE (0x40021000u + PERIPH_ALIAS) #define RCC_AHB2ENR (*(volatile uint32_t *)(RCC_BASE + 0x4Cu)) #define RCC_APB1LENR (*(volatile uint32_t *)(RCC_BASE + 0x58u)) /* GPIOA/GPIOB/GPIOD */ -#define GPIOA_BASE 0x42020000u -#define GPIOB_BASE 0x42020400u -#define GPIOD_BASE 0x42020C00u +#define GPIOA_BASE (0x42020000u + PERIPH_ALIAS) +#define GPIOB_BASE (0x42020400u + PERIPH_ALIAS) +#define GPIOD_BASE (0x42020C00u + PERIPH_ALIAS) #define GPIO_MODER(x) (*(volatile uint32_t *)((x) + 0x00u)) #define GPIO_OTYPER(x) (*(volatile uint32_t *)((x) + 0x04u)) #define GPIO_OSPEEDR(x) (*(volatile uint32_t *)((x) + 0x08u)) @@ -40,7 +47,7 @@ #define GPIO_AFRH(x) (*(volatile uint32_t *)((x) + 0x24u)) /* USART3 */ -#define USART3_BASE 0x40004800u +#define USART3_BASE (0x40004800u + PERIPH_ALIAS) #define USART_CR1(b) (*(volatile uint32_t *)((b) + 0x00u)) #define USART_CR2(b) (*(volatile uint32_t *)((b) + 0x04u)) #define USART_CR3(b) (*(volatile uint32_t *)((b) + 0x08u)) diff --git a/test-app/emu-test-apps/test.sh b/test-app/emu-test-apps/test.sh index be8842cad5..983cdd59ac 100755 --- a/test-app/emu-test-apps/test.sh +++ b/test-app/emu-test-apps/test.sh @@ -77,7 +77,7 @@ get_m33mu_target() { case "$TARGET" in stm32h563|stm32h5) EMU_DIR=stm32h563; UART_BASE=40004800 ;; stm32u585|stm32u5) EMU_DIR=stm32u585; UART_BASE=40004800 ;; - stm32l552|stm32l5) EMU_DIR=stm32l552; UART_BASE=40004800 ;; + stm32l552|stm32l5) EMU_DIR=stm32l552; UART_BASE=40004800; HANDOFF_CHECK=1 ;; nrf5340) EMU_DIR=nrf5340; UART_BASE=40008000 ;; mcxw|mcxw71) EMU_DIR=mcxw71; UART_BASE=40038000 ;; m2354) EMU_DIR=m2354; UART_BASE=40070000 ;; @@ -240,6 +240,9 @@ write_target_ld() { esac emu_tpl="$EMU_PATH/target.ld.in" + if [[ "$SECURE_APP" == "1" && -f "$EMU_PATH/target-secure-app.ld.in" ]]; then + emu_tpl="$EMU_PATH/target-secure-app.ld.in" + fi if [[ -f "$emu_tpl" ]]; then sed -e "s/@FLASH_ORIGIN@/0x$(printf '%x' "$addr")/g" \ -e "s/@FLASH_SIZE@/0x$(printf '%x' "$size")/g" \ From 506cf2fe87d409883ee10c670044ecba8c0094e9 Mon Sep 17 00:00:00 2001 From: Aidan Garske Date: Mon, 9 Nov 2026 15:13:45 -0800 Subject: [PATCH 06/18] Document the STM32L5 secure application handoff --- docs/Targets.md | 53 ++++++++++++++++++++++++++++++++++++++ docs/secure_app_handoff.md | 15 ++++++++++- 2 files changed, 67 insertions(+), 1 deletion(-) diff --git a/docs/Targets.md b/docs/Targets.md index ac736baf21..200ff853ff 100644 --- a/docs/Targets.md +++ b/docs/Targets.md @@ -285,6 +285,59 @@ Updates can be flashed at 0x0804a000: The two partition are logically remapped by using BANK_SWAP capabilities. This partition swap is immediate and does not require a SWAP partition. +### Scenario 4: TrustZone Enabled, Secure application handoff (wolfTrust) + +wolfBoot verifies and measures a signed **Secure** runtime (for example +wolfTrust) and jumps to it without leaving the Secure state, publishing the +measured-boot record at `0x30000000` (see +[Secure Application Handoff](secure_app_handoff.md)). The secure application +then programs the SAU and GTZC itself and launches its Non-secure guests. + +The example configuration is available in +[/config/examples/stm32l5-tz-wolftrust.config](/config/examples/stm32l5-tz-wolftrust.config). +All partitions are at the Secure flash alias: + +| Region | Address | Size | +| --- | --- | --- | +| wolfBoot (with an empty NSC) | `0x0C000000` | 48 KB | +| Boot partition (secure application) | `0x0C00C000` | 128 KB | +| Secure application NVM (not touched by wolfBoot) | `0x0C038000` | 24 KB | +| Swap page | `0x0C03E000` | 2 KB | +| Update partition (start of bank 2) | `0x0C040000` | 128 KB | +| Non-secure guests | `0x08060000` | 128 KB | + +wolfBoot keeps SRAM1, SRAM2 and every peripheral Secure across the jump, and +links its own RAM at `0x30030000` so that `0x30000000-0x3002FFFF` (the record +and the secure application's RAM) is left untouched. The PSA lifecycle in the +record comes from the RDP level in `FLASH_OPTR` (`hal/stm32l5_lifecycle.h`). + +The partition values are `?=` defaults, so a larger slot can be set on the +`make` command line (for example `WOLFBOOT_PARTITION_SIZE=0x28000 +WOLFBOOT_PARTITION_UPDATE_ADDRESS=0x0C058000`). + +Option bytes (bank 1 fully secure, bank 2 pages 0-63 secure for the update +slot, pages 64-127 Non-secure for the guests): + +``` +TZEN = 1 System with TrustZone-M enabled +DBANK = 1 Dual bank mode +SECWM1_STRT=0x0 SECWM1_END=0x7F All 128 pages of Bank1 secure +SECWM2_STRT=0x0 SECWM2_END=0x3F Bank2 pages 0-63 (0x08040000-0x0805FFFF) secure +``` + +1. `cp ./config/examples/stm32l5-tz-wolftrust.config .config` +2. `make` (builds `wolfboot.bin` only) +3. Sign the secure application, e.g. + `./tools/keytools/sign --ecc256 --sha256 wolftrust.bin wolfboot_signing_private_key.der 1` +4. Set the option bytes: + - `STM32_Programmer_CLI -c port=swd mode=hotplug -ob TZEN=1 DBANK=1` + - `STM32_Programmer_CLI -c port=swd mode=hotplug -ob SECWM1_STRT=0x0 SECWM1_END=0x7F SECWM2_STRT=0x0 SECWM2_END=0x3F` +5. `STM32_Programmer_CLI -c port=swd -d ./wolfboot.bin 0x0C000000` +6. `STM32_Programmer_CLI -c port=swd -d ./wolftrust_v1_signed.bin 0x0C00C000` + +The `trustzone-emulator-tests` workflow runs boot, update and rollback for this +configuration on the m33mu STM32L552 model. + ### Debugging diff --git a/docs/secure_app_handoff.md b/docs/secure_app_handoff.md index 80df0e9fc7..5ac49a6a27 100644 --- a/docs/secure_app_handoff.md +++ b/docs/secure_app_handoff.md @@ -96,10 +96,23 @@ the partitions at the Secure flash alias. `hal/mcxn.c` uses the shared reports the lifecycle through the weak default. `mcxn-emulator-tests.yml` runs boot, update and rollback on the m33mu MCXN947 model. +## STM32L5 + +`config/examples/stm32l5-tz-wolftrust.config` puts the record at `0x30000000` +and the partitions at the Secure flash alias (`0x0C00C000` boot, `0x0C040000` +update, both 128 KB). wolfBoot links its RAM at `0x30030000` +(`hal/stm32l5-secure-app.ld`) to stay clear of the record and the secure app's +RAM. `hal/stm32_tz.c` keeps SRAM1 and SRAM2 Secure and opens no Non-secure +flash or RAM in the SAU, `hal/stm32l5.c` leaves every peripheral Secure, and +`hal_attestation_get_lifecycle()` maps the RDP level to a PSA lifecycle +(`hal/stm32l5_lifecycle.h`; it reports unknown when TZEN is off). The +`trustzone-emulator-tests` workflow runs boot, update and rollback on the m33mu +STM32L552 model and checks the record on every boot. + ## Tests - `tools/unit-tests/unit-secure-handoff.c` — record layout, the builder, and the - STM32H5 lifecycle mapping. + STM32H5 and STM32L5 lifecycle mappings. - `tools/unit-tests/test-secure-handoff-config.sh` — the configure-time guards (rejects `SIGN=NONE` and `WOLFBOOT_SKIP_BOOT_VERIFY`) and the STM32H5 stack floors. From cffba364f4f9f397b3915dbea3637d690c86fbcb Mon Sep 17 00:00:00 2001 From: Aidan Garske Date: Tue, 10 Nov 2026 13:00:37 -0800 Subject: [PATCH 07/18] Fix the STM32L5 secure application option byte names and signing header size --- docs/Targets.md | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/docs/Targets.md b/docs/Targets.md index 200ff853ff..b707580949 100644 --- a/docs/Targets.md +++ b/docs/Targets.md @@ -319,19 +319,20 @@ Option bytes (bank 1 fully secure, bank 2 pages 0-63 secure for the update slot, pages 64-127 Non-secure for the guests): ``` -TZEN = 1 System with TrustZone-M enabled -DBANK = 1 Dual bank mode -SECWM1_STRT=0x0 SECWM1_END=0x7F All 128 pages of Bank1 secure -SECWM2_STRT=0x0 SECWM2_END=0x3F Bank2 pages 0-63 (0x08040000-0x0805FFFF) secure +TZEN = 1 System with TrustZone-M enabled +DBANK = 1 Dual bank mode +SECWM1_PSTRT=0x0 SECWM1_PEND=0x7F All 128 pages of Bank1 secure +SECWM2_PSTRT=0x0 SECWM2_PEND=0x3F Bank2 pages 0-63 (0x08040000-0x0805FFFF) secure ``` 1. `cp ./config/examples/stm32l5-tz-wolftrust.config .config` 2. `make` (builds `wolfboot.bin` only) 3. Sign the secure application, e.g. - `./tools/keytools/sign --ecc256 --sha256 wolftrust.bin wolfboot_signing_private_key.der 1` + `IMAGE_HEADER_SIZE=1024 ./tools/keytools/sign --ecc256 --sha256 wolftrust.bin wolfboot_signing_private_key.der 1` + (the header size must match the config's `IMAGE_HEADER_SIZE`) 4. Set the option bytes: - `STM32_Programmer_CLI -c port=swd mode=hotplug -ob TZEN=1 DBANK=1` - - `STM32_Programmer_CLI -c port=swd mode=hotplug -ob SECWM1_STRT=0x0 SECWM1_END=0x7F SECWM2_STRT=0x0 SECWM2_END=0x3F` + - `STM32_Programmer_CLI -c port=swd mode=hotplug -ob SECWM1_PSTRT=0x0 SECWM1_PEND=0x7F SECWM2_PSTRT=0x0 SECWM2_PEND=0x3F` 5. `STM32_Programmer_CLI -c port=swd -d ./wolfboot.bin 0x0C000000` 6. `STM32_Programmer_CLI -c port=swd -d ./wolftrust_v1_signed.bin 0x0C00C000` From f00ac707ffa05c1a63a1835e5e75ce129afb5ea7 Mon Sep 17 00:00:00 2001 From: Aidan Garske Date: Tue, 10 Nov 2026 13:42:55 -0800 Subject: [PATCH 08/18] Report STM32L5 RDP level 1 as Non-PSA RoT debug and drop the overlapping layout example --- docs/Targets.md | 7 ++++--- hal/stm32l5_lifecycle.h | 4 ++-- tools/unit-tests/unit-secure-handoff.c | 6 +++--- 3 files changed, 9 insertions(+), 8 deletions(-) diff --git a/docs/Targets.md b/docs/Targets.md index b707580949..4291f97581 100644 --- a/docs/Targets.md +++ b/docs/Targets.md @@ -311,9 +311,10 @@ links its own RAM at `0x30030000` so that `0x30000000-0x3002FFFF` (the record and the secure application's RAM) is left untouched. The PSA lifecycle in the record comes from the RDP level in `FLASH_OPTR` (`hal/stm32l5_lifecycle.h`). -The partition values are `?=` defaults, so a larger slot can be set on the -`make` command line (for example `WOLFBOOT_PARTITION_SIZE=0x28000 -WOLFBOOT_PARTITION_UPDATE_ADDRESS=0x0C058000`). +The partition values are `?=` defaults. A different layout set on the `make` +command line must keep the boot, NVM, swap, update and guest regions disjoint +(the Secure and Non-secure aliases are the same flash) and move the `SECWM` +watermarks below to match. Option bytes (bank 1 fully secure, bank 2 pages 0-63 secure for the update slot, pages 64-127 Non-secure for the guests): diff --git a/hal/stm32l5_lifecycle.h b/hal/stm32l5_lifecycle.h index 8dad16a552..9cb9c58bef 100644 --- a/hal/stm32l5_lifecycle.h +++ b/hal/stm32l5_lifecycle.h @@ -56,8 +56,8 @@ static inline int stm32l5_optr_to_psa_lifecycle(uint32_t optr, *lifecycle = 0x3000u; /* PSA_LIFECYCLE_SECURED */ break; default: - /* Any other value is RDP level 1 */ - *lifecycle = 0x5000u; /* PSA_LIFECYCLE_RECOVERABLE_PSA_ROT_DEBUG */ + /* Any other value is RDP level 1: Secure debug is closed */ + *lifecycle = 0x4000u; /* PSA_LIFECYCLE_NON_PSA_ROT_DEBUG */ break; } } diff --git a/tools/unit-tests/unit-secure-handoff.c b/tools/unit-tests/unit-secure-handoff.c index c7bb0c4d25..d28dfe8692 100644 --- a/tools/unit-tests/unit-secure-handoff.c +++ b/tools/unit-tests/unit-secure-handoff.c @@ -181,11 +181,11 @@ static int test_stm32l5_optr_lifecycle_map(void) return -1; } if ((stm32l5_expect_lifecycle(STM32L5_FLASH_OPTR_TZEN | 0xBBu, - 0x5000u) != 0) || + 0x4000u) != 0) || (stm32l5_expect_lifecycle(STM32L5_FLASH_OPTR_TZEN | 0x00u, - 0x5000u) != 0) || + 0x4000u) != 0) || (stm32l5_expect_lifecycle(STM32L5_FLASH_OPTR_TZEN | 0xFFu, - 0x5000u) != 0)) { + 0x4000u) != 0)) { return -1; } /* Only RDP and TZEN select the lifecycle */ From 5ed6d5f4aa9f4f5f5d369269249b25bb26d3a337 Mon Sep 17 00:00:00 2001 From: Aidan Garske Date: Tue, 10 Nov 2026 14:12:51 -0800 Subject: [PATCH 09/18] Keep the STM32L5 peripheral window Secure for a secure application handoff --- docs/Targets.md | 7 ++++- docs/secure_app_handoff.md | 6 ++-- hal/stm32_tz.c | 4 +-- hal/stm32l5_lifecycle.h | 2 ++ test-app/emu-test-apps/common/emu_update.c | 32 ++++++++++++++++++++++ 5 files changed, 46 insertions(+), 5 deletions(-) diff --git a/docs/Targets.md b/docs/Targets.md index 4291f97581..8be3e631b7 100644 --- a/docs/Targets.md +++ b/docs/Targets.md @@ -306,10 +306,15 @@ All partitions are at the Secure flash alias: | Update partition (start of bank 2) | `0x0C040000` | 128 KB | | Non-secure guests | `0x08060000` | 128 KB | -wolfBoot keeps SRAM1, SRAM2 and every peripheral Secure across the jump, and +wolfBoot keeps SRAM1 and SRAM2 Secure across the jump, opens no Non-secure +flash, RAM or peripheral window in the SAU, and links its own RAM at `0x30030000` so that `0x30000000-0x3002FFFF` (the record and the secure application's RAM) is left untouched. The PSA lifecycle in the record comes from the RDP level in `FLASH_OPTR` (`hal/stm32l5_lifecycle.h`). +The STM32L5 has no product-state register, so with `TZEN=1` RDP level 0 is +reported as PSA RoT provisioning (`0x2000`), levels 0.5 and 1 as Non-PSA RoT +debug (`0x4000`, Secure debug closed), and level 2 as secured (`0x3000`). +Without `TZEN` the lifecycle is unknown. The partition values are `?=` defaults. A different layout set on the `make` command line must keep the boot, NVM, swap, update and guest regions disjoint diff --git a/docs/secure_app_handoff.md b/docs/secure_app_handoff.md index 5ac49a6a27..cadc2b92bb 100644 --- a/docs/secure_app_handoff.md +++ b/docs/secure_app_handoff.md @@ -103,11 +103,13 @@ and the partitions at the Secure flash alias (`0x0C00C000` boot, `0x0C040000` update, both 128 KB). wolfBoot links its RAM at `0x30030000` (`hal/stm32l5-secure-app.ld`) to stay clear of the record and the secure app's RAM. `hal/stm32_tz.c` keeps SRAM1 and SRAM2 Secure and opens no Non-secure -flash or RAM in the SAU, `hal/stm32l5.c` leaves every peripheral Secure, and +flash, RAM or peripheral window in the SAU (only the NSC region), +`hal/stm32l5.c` skips the GTZC peripheral unsecuring, and `hal_attestation_get_lifecycle()` maps the RDP level to a PSA lifecycle (`hal/stm32l5_lifecycle.h`; it reports unknown when TZEN is off). The `trustzone-emulator-tests` workflow runs boot, update and rollback on the m33mu -STM32L552 model and checks the record on every boot. +STM32L552 model and checks on every boot the record and that no +Non-secure SAU window is open. ## Tests diff --git a/hal/stm32_tz.c b/hal/stm32_tz.c index 605c3308ec..db9a843f44 100644 --- a/hal/stm32_tz.c +++ b/hal/stm32_tz.c @@ -395,7 +395,7 @@ void hal_tz_sau_init(void) WOLFBOOT_NSC_ADDRESS + WOLFBOOT_NSC_SIZE - 1, 1); /* A secure application is entered Secure and programs its own - * Non-secure flash and RAM windows. */ + * Non-secure flash, RAM and peripheral windows. */ #if !defined(WOLFBOOT_SECURE_APP) /* Non-secure: application flash area (boot partition only) */ sau_init_region(1, WOLFBOOT_PARTITION_BOOT_ADDRESS, @@ -404,10 +404,10 @@ void hal_tz_sau_init(void) /* Non-secure RAM region in SRAM1/SRAM2 */ sau_init_region(2, 0x20020000, 0x2003FFFF, 0); -#endif /* Non-secure: internal peripherals */ sau_init_region(3, 0x40000000, 0x4FFFFFFF, 0); +#endif /* Enable SAU */ SAU_CTRL = SAU_INIT_CTRL_ENABLE; diff --git a/hal/stm32l5_lifecycle.h b/hal/stm32l5_lifecycle.h index 9cb9c58bef..a79187fef9 100644 --- a/hal/stm32l5_lifecycle.h +++ b/hal/stm32l5_lifecycle.h @@ -47,6 +47,8 @@ static inline int stm32l5_optr_to_psa_lifecycle(uint32_t optr, else { switch (optr & STM32L5_FLASH_OPTR_RDP_MASK) { case STM32L5_RDP_LEVEL_0: + /* No product state on L5: TZEN at RDP 0 is the provisioning + * stage, and assembly/test has no distinct encoding */ *lifecycle = 0x2000u; /* PSA_LIFECYCLE_PSA_ROT_PROVISIONING */ break; case STM32L5_RDP_LEVEL_0_5: diff --git a/test-app/emu-test-apps/common/emu_update.c b/test-app/emu-test-apps/common/emu_update.c index 261bc269d4..12edf6f98a 100644 --- a/test-app/emu-test-apps/common/emu_update.c +++ b/test-app/emu-test-apps/common/emu_update.c @@ -287,6 +287,32 @@ static void wait_for_update(uint32_t version) } #ifdef EMU_SECURE_HANDOFF_ADDRESS +#if defined(EMU_STM32L5_SECURE) +#define EMU_SAU_RNR (*(volatile uint32_t *)0xE000EDD8u) +#define EMU_SAU_RLAR (*(volatile uint32_t *)0xE000EDE0u) +#define EMU_SAU_TYPE (*(volatile uint32_t *)0xE000EDD4u) +#define EMU_SAU_RLAR_ENABLE 0x1u +#define EMU_SAU_RLAR_NSC 0x2u + +/* wolfBoot must hand over with no Non-secure SAU window, only its NSC one */ +static int emu_check_sau(void) +{ + uint32_t regions = EMU_SAU_TYPE & 0xFFu; + uint32_t rlar; + uint32_t r; + + for (r = 0u; r < regions; r++) { + EMU_SAU_RNR = r; + rlar = EMU_SAU_RLAR; + if (((rlar & EMU_SAU_RLAR_ENABLE) != 0u) && + ((rlar & EMU_SAU_RLAR_NSC) == 0u)) { + return -1; + } + } + return 0; +} +#endif + /* The record wolfBoot published must describe the image that is running. */ static int emu_check_handoff(uint32_t version) { @@ -295,6 +321,12 @@ static int emu_check_handoff(uint32_t version) uint8_t *hash = NULL; uint32_t i; +#if defined(EMU_STM32L5_SECURE) + if (emu_check_sau() != 0) { + return -1; + } +#endif + if ((rec->magic != WOLFBOOT_SECURE_HANDOFF_MAGIC) || (rec->magic_inverse != ~WOLFBOOT_SECURE_HANDOFF_MAGIC) || (rec->version != WOLFBOOT_SECURE_HANDOFF_VERSION) || From 86852e0c6f743b181b03ff2e9471027a3b006c6c Mon Sep 17 00:00:00 2001 From: Aidan Garske Date: Tue, 10 Nov 2026 14:29:03 -0800 Subject: [PATCH 10/18] Disable bootloader self-update in the STM32L5 secure application config --- config/examples/stm32l5-tz-wolftrust.config | 3 ++- hal/stm32l5.c | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/config/examples/stm32l5-tz-wolftrust.config b/config/examples/stm32l5-tz-wolftrust.config index 4217dabe4e..80d95ed62f 100644 --- a/config/examples/stm32l5-tz-wolftrust.config +++ b/config/examples/stm32l5-tz-wolftrust.config @@ -17,7 +17,8 @@ NVM_FLASH_WRITEONCE?=1 WOLFBOOT_VERSION?=1 V?=0 SPMATH?=1 -RAM_CODE?=1 +# No bootloader self-update: its erase range assumes the non-secure alias +RAM_CODE?=0 DUALBANK_SWAP?=0 IMAGE_HEADER_SIZE?=1024 WOLFCRYPT_TZ?=0 diff --git a/hal/stm32l5.c b/hal/stm32l5.c index a3361b2986..283828fb14 100644 --- a/hal/stm32l5.c +++ b/hal/stm32l5.c @@ -430,7 +430,7 @@ static void clock_pll_on(int powersave) DMB(); } -/* A secure application keeps every peripheral Secure across the jump */ +/* A secure application programs its own GTZC peripheral attribution */ #if TZ_SECURE() && !defined(WOLFBOOT_SECURE_APP) static void periph_unsecure() { From 998e8edea554af3b5410c3fa90dd7ad555545e4a Mon Sep 17 00:00:00 2001 From: Aidan Garske Date: Wed, 11 Nov 2026 14:24:21 -0800 Subject: [PATCH 11/18] Keep the PLL clock running for an STM32L5 secure application handoff --- docs/secure_app_handoff.md | 3 ++- hal/stm32l5.c | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/docs/secure_app_handoff.md b/docs/secure_app_handoff.md index cadc2b92bb..82ba0a680d 100644 --- a/docs/secure_app_handoff.md +++ b/docs/secure_app_handoff.md @@ -104,7 +104,8 @@ update, both 128 KB). wolfBoot links its RAM at `0x30030000` (`hal/stm32l5-secure-app.ld`) to stay clear of the record and the secure app's RAM. `hal/stm32_tz.c` keeps SRAM1 and SRAM2 Secure and opens no Non-secure flash, RAM or peripheral window in the SAU (only the NSC region), -`hal/stm32l5.c` skips the GTZC peripheral unsecuring, and +`hal/stm32l5.c` skips the GTZC peripheral unsecuring and keeps the 110 MHz +PLL clock running, and `hal_attestation_get_lifecycle()` maps the RDP level to a PSA lifecycle (`hal/stm32l5_lifecycle.h`; it reports unknown when TZEN is off). The `trustzone-emulator-tests` workflow runs boot, update and rollback on the m33mu diff --git a/hal/stm32l5.c b/hal/stm32l5.c index 283828fb14..81d26569b6 100644 --- a/hal/stm32l5.c +++ b/hal/stm32l5.c @@ -547,7 +547,8 @@ void hal_init(void) void hal_prepare_boot(void) { -#ifdef WOLFBOOT_RESTORE_CLOCK + /* A secure application runs on wolfBoot's 110 MHz PLL clock */ +#if defined(WOLFBOOT_RESTORE_CLOCK) && !defined(WOLFBOOT_SECURE_APP) clock_pll_off(); #endif #if TZ_SECURE() && !defined(WOLFBOOT_SECURE_APP) From 7c24c9d3aa578e6c873782b02e8be43e56c5d215 Mon Sep 17 00:00:00 2001 From: Aidan Garske Date: Thu, 12 Nov 2026 15:35:41 -0800 Subject: [PATCH 12/18] Use the 110 MHz handoff clock for the STM32L5 secure app test UART --- test-app/emu-test-apps/stm32l552/uart.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/test-app/emu-test-apps/stm32l552/uart.c b/test-app/emu-test-apps/stm32l552/uart.c index b26fe9129a..8ec16b786b 100644 --- a/test-app/emu-test-apps/stm32l552/uart.c +++ b/test-app/emu-test-apps/stm32l552/uart.c @@ -22,12 +22,13 @@ #include #include "emu_app.h" -#define SYSCLK_HZ 64000000u - -/* The secure-app payload runs Secure and uses the Secure peripheral aliases */ +/* The secure-app payload runs Secure and uses the Secure peripheral aliases; + * wolfBoot hands it the 110 MHz PLL with APB1 undivided. */ #ifdef EMU_STM32L5_SECURE +#define SYSCLK_HZ 110000000u #define PERIPH_ALIAS 0x10000000u #else +#define SYSCLK_HZ 64000000u #define PERIPH_ALIAS 0x00000000u #endif From 0695fb147a77968f896cabeac6fce8086c9a0eb3 Mon Sep 17 00:00:00 2001 From: Aidan Garske Date: Fri, 13 Nov 2026 11:40:44 -0800 Subject: [PATCH 13/18] Export no NSC entries from an STM32L5 secure application without WOLFCRYPT_TZ --- arch.mk | 13 +++++++++++-- config/examples/stm32l5-tz-wolftrust.config | 6 ++---- hal/stm32_tz.c | 3 +++ hal/stm32l5-secure-app.ld | 11 ----------- src/libwolfboot.c | 7 +++++-- test-app/emu-test-apps/common/emu_update.c | 6 ++---- 6 files changed, 23 insertions(+), 23 deletions(-) diff --git a/arch.mk b/arch.mk index ad6cc2a98d..b44178dbd5 100644 --- a/arch.mk +++ b/arch.mk @@ -681,6 +681,9 @@ ifeq ($(ARCH),ARM) ifeq ($(TZEN),1) WOLFBOOT_ORIGIN=0x0C000000 ifeq ($(WOLFBOOT_SECURE_APP),1) + ifeq ($(WOLFCRYPT_TZ),1) + $(error stm32l5 WOLFBOOT_SECURE_APP=1 links no NSC; set WOLFCRYPT_TZ=0) + endif $(call LSCRIPT_IN_DEFAULT,hal/$(TARGET)-secure-app.ld) endif else @@ -1108,7 +1111,10 @@ else # machinery as CORTEX_M33 applies. Kept separate from the M33 block so # that adding this arm cannot change any existing target. CFLAGS+=-mcmse - SECURE_LDFLAGS+=-Wl,--cmse-implib -Wl,--out-implib=./src/wolfboot_tz_nsc.o + # A secure app without WOLFCRYPT_TZ has no NSC entries to export + ifneq ($(WOLFBOOT_SECURE_APP)/$(filter 1,$(WOLFCRYPT_TZ)),1/) + SECURE_LDFLAGS+=-Wl,--cmse-implib -Wl,--out-implib=./src/wolfboot_tz_nsc.o + endif ifeq ($(WOLFCRYPT_TZ),1) SECURE_OBJS+=./src/wc_callable.o WOLFCRYPT_OBJS+=$(WOLFBOOT_LIB_WOLFSSL)/wolfcrypt/src/random.o @@ -1139,7 +1145,10 @@ else endif endif CFLAGS+=-mcmse - SECURE_LDFLAGS+=-Wl,--cmse-implib -Wl,--out-implib=./src/wolfboot_tz_nsc.o + # A secure app without WOLFCRYPT_TZ has no NSC entries to export + ifneq ($(WOLFBOOT_SECURE_APP)/$(filter 1,$(WOLFCRYPT_TZ)),1/) + SECURE_LDFLAGS+=-Wl,--cmse-implib -Wl,--out-implib=./src/wolfboot_tz_nsc.o + endif ifeq ($(WOLFCRYPT_TZ),1) CORTEXM_ARM_EXTRA_OBJS= CORTEXM_ARM_EXTRA_CFLAGS= diff --git a/config/examples/stm32l5-tz-wolftrust.config b/config/examples/stm32l5-tz-wolftrust.config index 80d95ed62f..8129fefac2 100644 --- a/config/examples/stm32l5-tz-wolftrust.config +++ b/config/examples/stm32l5-tz-wolftrust.config @@ -30,14 +30,12 @@ WOLFBOOT_SECURE_HANDOFF_ADDRESS?=0x30000000 # 2KB pages, dual bank (DBANK=1) WOLFBOOT_SECTOR_SIZE?=0x800 -# 48KB wolfBoot region (bootloader + empty NSC), partitions at the Secure -# flash alias: 128KB boot and the swap page in bank 1, 128KB update in bank 2. +# 48KB wolfBoot region with no NSC, partitions at the Secure flash alias: +# 128KB boot and the swap page in bank 1, 128KB update in bank 2. # 0x0C038000-0x0C03DFFF is the secure app's NVM and is never touched here. BOOTLOADER_PARTITION_SIZE?=0xC000 WOLFBOOT_KEYVAULT_ADDRESS?=0x0C00B000 WOLFBOOT_KEYVAULT_SIZE?=0 -WOLFBOOT_NSC_ADDRESS?=0x0C00B000 -WOLFBOOT_NSC_SIZE?=0x1000 WOLFBOOT_PARTITION_SIZE?=0x20000 WOLFBOOT_PARTITION_BOOT_ADDRESS?=0x0C00C000 WOLFBOOT_PARTITION_UPDATE_ADDRESS?=0x0C040000 diff --git a/hal/stm32_tz.c b/hal/stm32_tz.c index db9a843f44..c57bb8a8b2 100644 --- a/hal/stm32_tz.c +++ b/hal/stm32_tz.c @@ -390,9 +390,12 @@ void hal_tz_sau_init(void) #else void hal_tz_sau_init(void) { + /* A secure application without wolfCrypt TZ exports no NSC entries */ +#if !defined(WOLFBOOT_SECURE_APP) || defined(WOLFCRYPT_SECURE_MODE) /* Non-secure callable: NSC functions area */ sau_init_region(0, WOLFBOOT_NSC_ADDRESS, WOLFBOOT_NSC_ADDRESS + WOLFBOOT_NSC_SIZE - 1, 1); +#endif /* A secure application is entered Secure and programs its own * Non-secure flash, RAM and peripheral windows. */ diff --git a/hal/stm32l5-secure-app.ld b/hal/stm32l5-secure-app.ld index 50bbbb73e9..4adfcd9706 100644 --- a/hal/stm32l5-secure-app.ld +++ b/hal/stm32l5-secure-app.ld @@ -5,7 +5,6 @@ MEMORY RAM (rwx) : ORIGIN = 0x30030000, LENGTH = 0x0000C000 RAM_HEAP (rw): ORIGIN = 0x3003C000, LENGTH = 0x4000 FLASH_KEYVAULT(rw): ORIGIN = @WOLFBOOT_KEYVAULT_ADDRESS@, LENGTH = @WOLFBOOT_KEYVAULT_SIZE@ - FLASH_NSC(rx): ORIGIN = @WOLFBOOT_NSC_ADDRESS@, LENGTH = @WOLFBOOT_NSC_SIZE@ } SECTIONS @@ -26,8 +25,6 @@ SECTIONS *(.ARM.exidx*) } > FLASH - /* Data is stored in FLASH, not after the NSC: the boot partition - * follows the NSC directly. */ .data : { _start_data = .; @@ -39,14 +36,6 @@ SECTIONS } > RAM AT > FLASH _stored_data = LOADADDR(.data); - .gnu.sgstubs ORIGIN(FLASH_NSC) : - { - . += 0x400; - . = ALIGN(4); - *(.gnu.sgstubs*) /* Secure Gateway stubs */ - . = ALIGN(4); - } >FLASH_NSC - .bss (NOLOAD) : { _start_bss = .; diff --git a/src/libwolfboot.c b/src/libwolfboot.c index 40ba5e0e78..b87e327077 100644 --- a/src/libwolfboot.c +++ b/src/libwolfboot.c @@ -3809,7 +3809,10 @@ int wolfBoot_ram_decrypt(uint8_t *src, uint8_t *dst) #endif /* MMU */ #endif /* EXT_ENCRYPTED */ -#if defined(__WOLFBOOT) && defined(TZEN) +/* A secure application without wolfCrypt TZ is entered Secure and exports no + * NSC entries, so Non-secure code cannot reach the update partition. */ +#if defined(__WOLFBOOT) && defined(TZEN) && \ + (!defined(WOLFBOOT_SECURE_APP) || defined(WOLFCRYPT_SECURE_MODE)) /* The wolfBoot_nsc_* functions are cmse_nonsecure_entry veneers callable by any * non-secure code. Pointer arguments arrive directly from the NS caller and are @@ -3902,4 +3905,4 @@ int wolfBoot_nsc_write_update(uint32_t address, const uint8_t *buf, uint32_t len return ret; } -#endif /* __WOLFBOOT && TZEN */ +#endif /* __WOLFBOOT && TZEN && (!WOLFBOOT_SECURE_APP || WOLFCRYPT_SECURE_MODE) */ diff --git a/test-app/emu-test-apps/common/emu_update.c b/test-app/emu-test-apps/common/emu_update.c index 12edf6f98a..72f6f03672 100644 --- a/test-app/emu-test-apps/common/emu_update.c +++ b/test-app/emu-test-apps/common/emu_update.c @@ -292,9 +292,8 @@ static void wait_for_update(uint32_t version) #define EMU_SAU_RLAR (*(volatile uint32_t *)0xE000EDE0u) #define EMU_SAU_TYPE (*(volatile uint32_t *)0xE000EDD4u) #define EMU_SAU_RLAR_ENABLE 0x1u -#define EMU_SAU_RLAR_NSC 0x2u -/* wolfBoot must hand over with no Non-secure SAU window, only its NSC one */ +/* wolfBoot must hand over with no SAU region: no Non-secure or NSC window */ static int emu_check_sau(void) { uint32_t regions = EMU_SAU_TYPE & 0xFFu; @@ -304,8 +303,7 @@ static int emu_check_sau(void) for (r = 0u; r < regions; r++) { EMU_SAU_RNR = r; rlar = EMU_SAU_RLAR; - if (((rlar & EMU_SAU_RLAR_ENABLE) != 0u) && - ((rlar & EMU_SAU_RLAR_NSC) == 0u)) { + if ((rlar & EMU_SAU_RLAR_ENABLE) != 0u) { return -1; } } From de2da43040af06ff2d73551a9d69aec523ab6473 Mon Sep 17 00:00:00 2001 From: Aidan Garske Date: Fri, 13 Nov 2026 11:40:44 -0800 Subject: [PATCH 14/18] Keep the STM32L5 PLL for the secure app with WOLFBOOT_RESTORE_CLOCK=0 --- config/examples/stm32l5-tz-wolftrust.config | 2 ++ hal/stm32l5.c | 3 +-- test-app/emu-test-apps/stm32l552/uart.c | 3 ++- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/config/examples/stm32l5-tz-wolftrust.config b/config/examples/stm32l5-tz-wolftrust.config index 8129fefac2..6cea68c1de 100644 --- a/config/examples/stm32l5-tz-wolftrust.config +++ b/config/examples/stm32l5-tz-wolftrust.config @@ -26,6 +26,8 @@ ARMORED=1 WOLFBOOT_SECURE_APP?=1 WOLFBOOT_SECURE_HANDOFF_ADDRESS?=0x30000000 +# The secure app runs on the 110 MHz PLL clock wolfBoot leaves running +WOLFBOOT_RESTORE_CLOCK?=0 # 2KB pages, dual bank (DBANK=1) WOLFBOOT_SECTOR_SIZE?=0x800 diff --git a/hal/stm32l5.c b/hal/stm32l5.c index 81d26569b6..283828fb14 100644 --- a/hal/stm32l5.c +++ b/hal/stm32l5.c @@ -547,8 +547,7 @@ void hal_init(void) void hal_prepare_boot(void) { - /* A secure application runs on wolfBoot's 110 MHz PLL clock */ -#if defined(WOLFBOOT_RESTORE_CLOCK) && !defined(WOLFBOOT_SECURE_APP) +#ifdef WOLFBOOT_RESTORE_CLOCK clock_pll_off(); #endif #if TZ_SECURE() && !defined(WOLFBOOT_SECURE_APP) diff --git a/test-app/emu-test-apps/stm32l552/uart.c b/test-app/emu-test-apps/stm32l552/uart.c index 8ec16b786b..03ef4e2730 100644 --- a/test-app/emu-test-apps/stm32l552/uart.c +++ b/test-app/emu-test-apps/stm32l552/uart.c @@ -23,7 +23,8 @@ #include "emu_app.h" /* The secure-app payload runs Secure and uses the Secure peripheral aliases; - * wolfBoot hands it the 110 MHz PLL with APB1 undivided. */ + * wolfBoot hands it the 110 MHz PLL with APB1 undivided. This assumes + * WOLFBOOT_RESTORE_CLOCK=0 as in stm32l5-tz-wolftrust.config. */ #ifdef EMU_STM32L5_SECURE #define SYSCLK_HZ 110000000u #define PERIPH_ALIAS 0x10000000u From c3003c21db16a35f6a8699c6811c67cac4319ec5 Mon Sep 17 00:00:00 2001 From: Aidan Garske Date: Fri, 13 Nov 2026 11:40:44 -0800 Subject: [PATCH 15/18] Leave the STM32L5 MPCBB reset value in place for a secure application --- hal/stm32_tz.c | 11 ----------- hal/stm32l5.c | 3 +++ 2 files changed, 3 insertions(+), 11 deletions(-) diff --git a/hal/stm32_tz.c b/hal/stm32_tz.c index c57bb8a8b2..bc3422a6d7 100644 --- a/hal/stm32_tz.c +++ b/hal/stm32_tz.c @@ -317,16 +317,6 @@ void hal_gtzc_init(void) int i; /* One bit in the bitmask: 256B */ -#if defined(WOLFBOOT_SECURE_APP) - /* SRAM1 (192 KB) and SRAM2 (64 KB) stay Secure until the secure - * application installs its own split. */ - for (i = 0; i < 24; i++) { - SET_GTZC_MPCBBx_S_VCTR(1, i, 0xFFFFFFFF); - } - for (i = 0; i < 8; i++) { - SET_GTZC_MPCBBx_S_VCTR(2, i, 0xFFFFFFFF); - } -#else /* Configure lower half of total RAM as secure * 0x3000 0000 : 0x3001 FFFF - 128KB */ @@ -347,7 +337,6 @@ void hal_gtzc_init(void) for (i = 0; i < 8; i++) { SET_GTZC_MPCBBx_S_VCTR(2, i, 0x0); } -#endif /* WOLFBOOT_SECURE_APP */ } #endif diff --git a/hal/stm32l5.c b/hal/stm32l5.c index 283828fb14..8ebff41296 100644 --- a/hal/stm32l5.c +++ b/hal/stm32l5.c @@ -539,7 +539,10 @@ void hal_init(void) #if TZ_SECURE() hal_tz_sau_init(); + /* A secure application keeps the MPCBB reset value (all SRAM Secure) */ +#if !defined(WOLFBOOT_SECURE_APP) hal_gtzc_init(); +#endif #endif clock_pll_on(0); From 781343ee7b7c76e02515439b19153411598999bf Mon Sep 17 00:00:00 2001 From: Aidan Garske Date: Fri, 13 Nov 2026 11:40:44 -0800 Subject: [PATCH 16/18] Drop the unused heap from the STM32L5 secure app linker script --- hal/stm32l5-secure-app.ld | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/hal/stm32l5-secure-app.ld b/hal/stm32l5-secure-app.ld index 4adfcd9706..2085fe5abc 100644 --- a/hal/stm32l5-secure-app.ld +++ b/hal/stm32l5-secure-app.ld @@ -2,8 +2,7 @@ MEMORY { FLASH (rx) : ORIGIN = @WOLFBOOT_ORIGIN@, LENGTH = @WOLFBOOT_KEYVAULT_ADDRESS@ - @WOLFBOOT_ORIGIN@ - RAM (rwx) : ORIGIN = 0x30030000, LENGTH = 0x0000C000 - RAM_HEAP (rw): ORIGIN = 0x3003C000, LENGTH = 0x4000 + RAM (rwx) : ORIGIN = 0x30030000, LENGTH = 0x00010000 FLASH_KEYVAULT(rw): ORIGIN = @WOLFBOOT_KEYVAULT_ADDRESS@, LENGTH = @WOLFBOOT_KEYVAULT_SIZE@ } @@ -52,8 +51,8 @@ SECTIONS END_STACK = ORIGIN(RAM) + LENGTH(RAM); +_min_stack = 0x2000; +ASSERT(_end + _min_stack <= END_STACK, "stm32l5 secure-app RAM: .bss leaves less than _min_stack for the stack") + _flash_keyvault = ORIGIN(FLASH_KEYVAULT); _flash_keyvault_size = LENGTH(FLASH_KEYVAULT); - -_start_heap = ORIGIN(RAM_HEAP); -_heap_size = LENGTH(RAM_HEAP); From ee7a70061d980cee1d2aef205709f52063fd2839 Mon Sep 17 00:00:00 2001 From: Aidan Garske Date: Fri, 13 Nov 2026 11:40:44 -0800 Subject: [PATCH 17/18] Translate the STM32L5 Secure flash alias on erase with the write path mask --- hal/stm32l5.c | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/hal/stm32l5.c b/hal/stm32l5.c index 8ebff41296..767a8e084d 100644 --- a/hal/stm32l5.c +++ b/hal/stm32l5.c @@ -278,11 +278,8 @@ int RAMFUNCTION hal_flash_erase(uint32_t address, int len) #if TZ_SECURE() && defined(WOLFBOOT_SECURE_APP) /* Secure-app partitions are linked at the Secure flash alias */ - if ((address >= FLASH_SECURE_MMAP_BASE) && - (address <= (FLASH_SECURE_MMAP_BASE + - (FLASH_TOP - FLASHMEM_ADDRESS_SPACE)))) { - address -= (FLASH_SECURE_MMAP_BASE - FLASHMEM_ADDRESS_SPACE); - } + if (address >= FLASH_SECURE_MMAP_BASE) + address = (address & ~FLASH_SECURE_MMAP_BASE) | FLASHMEM_ADDRESS_SPACE; #endif if (address < ARCH_FLASH_OFFSET) From 6e60901783e0dd8b114d2840e7fb665bd7e712d2 Mon Sep 17 00:00:00 2001 From: Aidan Garske Date: Fri, 13 Nov 2026 11:40:45 -0800 Subject: [PATCH 18/18] Update the STM32L5 secure app docs for the NSC, clock and GTZC changes --- docs/Targets.md | 7 ++++--- docs/secure_app_handoff.md | 13 +++++++------ 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/docs/Targets.md b/docs/Targets.md index 8be3e631b7..91c5223fca 100644 --- a/docs/Targets.md +++ b/docs/Targets.md @@ -299,15 +299,16 @@ All partitions are at the Secure flash alias: | Region | Address | Size | | --- | --- | --- | -| wolfBoot (with an empty NSC) | `0x0C000000` | 48 KB | +| wolfBoot | `0x0C000000` | 48 KB | | Boot partition (secure application) | `0x0C00C000` | 128 KB | | Secure application NVM (not touched by wolfBoot) | `0x0C038000` | 24 KB | | Swap page | `0x0C03E000` | 2 KB | | Update partition (start of bank 2) | `0x0C040000` | 128 KB | | Non-secure guests | `0x08060000` | 128 KB | -wolfBoot keeps SRAM1 and SRAM2 Secure across the jump, opens no Non-secure -flash, RAM or peripheral window in the SAU, and +wolfBoot keeps SRAM1 and SRAM2 Secure across the jump (the GTZC reset value), +enables no SAU region and builds no Non-secure callable entries, keeps the +110 MHz PLL clock running (`WOLFBOOT_RESTORE_CLOCK=0`), and links its own RAM at `0x30030000` so that `0x30000000-0x3002FFFF` (the record and the secure application's RAM) is left untouched. The PSA lifecycle in the record comes from the RDP level in `FLASH_OPTR` (`hal/stm32l5_lifecycle.h`). diff --git a/docs/secure_app_handoff.md b/docs/secure_app_handoff.md index 82ba0a680d..25137f4446 100644 --- a/docs/secure_app_handoff.md +++ b/docs/secure_app_handoff.md @@ -102,15 +102,16 @@ boot, update and rollback on the m33mu MCXN947 model. and the partitions at the Secure flash alias (`0x0C00C000` boot, `0x0C040000` update, both 128 KB). wolfBoot links its RAM at `0x30030000` (`hal/stm32l5-secure-app.ld`) to stay clear of the record and the secure app's -RAM. `hal/stm32_tz.c` keeps SRAM1 and SRAM2 Secure and opens no Non-secure -flash, RAM or peripheral window in the SAU (only the NSC region), -`hal/stm32l5.c` skips the GTZC peripheral unsecuring and keeps the 110 MHz -PLL clock running, and +RAM. SRAM1 and SRAM2 stay Secure at their GTZC reset value, the SAU has no +region enabled, and without `WOLFCRYPT_TZ` wolfBoot links no `wolfBoot_nsc_*` +entries, so Non-secure code has no path into the update partition. +`hal/stm32l5.c` skips the GTZC peripheral unsecuring, the config sets +`WOLFBOOT_RESTORE_CLOCK=0` so the 110 MHz PLL keeps running, and `hal_attestation_get_lifecycle()` maps the RDP level to a PSA lifecycle (`hal/stm32l5_lifecycle.h`; it reports unknown when TZEN is off). The `trustzone-emulator-tests` workflow runs boot, update and rollback on the m33mu -STM32L552 model and checks on every boot the record and that no -Non-secure SAU window is open. +STM32L552 model and checks on every boot the record and that no SAU region +is enabled. ## Tests