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/.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/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..b44178dbd5 100644 --- a/arch.mk +++ b/arch.mk @@ -680,6 +680,12 @@ ifeq ($(ARCH),ARM) ARCH_FLASH_OFFSET=0x08000000 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 WOLFBOOT_ORIGIN=0x08000000 $(call LSCRIPT_IN_DEFAULT,hal/$(TARGET)-ns.ld) @@ -1105,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 @@ -1136,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 new file mode 100644 index 0000000000..6cea68c1de --- /dev/null +++ b/config/examples/stm32l5-tz-wolftrust.config @@ -0,0 +1,44 @@ +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 +# 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 +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 + +# 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_PARTITION_SIZE?=0x20000 +WOLFBOOT_PARTITION_BOOT_ADDRESS?=0x0C00C000 +WOLFBOOT_PARTITION_UPDATE_ADDRESS?=0x0C040000 +WOLFBOOT_PARTITION_SWAP_ADDRESS?=0x0C03E000 diff --git a/docs/Targets.md b/docs/Targets.md index ac736baf21..91c5223fca 100644 --- a/docs/Targets.md +++ b/docs/Targets.md @@ -285,6 +285,67 @@ 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 | `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 (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`). +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 +(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): + +``` +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. + `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_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` + +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..25137f4446 100644 --- a/docs/secure_app_handoff.md +++ b/docs/secure_app_handoff.md @@ -96,10 +96,27 @@ 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. 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 SAU region +is enabled. + ## 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. diff --git a/hal/stm32_tz.c b/hal/stm32_tz.c index fb3566e381..bc3422a6d7 100644 --- a/hal/stm32_tz.c +++ b/hal/stm32_tz.c @@ -379,10 +379,16 @@ 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. */ +#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, @@ -393,6 +399,7 @@ void hal_tz_sau_init(void) /* 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-secure-app.ld b/hal/stm32l5-secure-app.ld new file mode 100644 index 0000000000..2085fe5abc --- /dev/null +++ b/hal/stm32l5-secure-app.ld @@ -0,0 +1,58 @@ +/* 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 = 0x00010000 + FLASH_KEYVAULT(rw): ORIGIN = @WOLFBOOT_KEYVAULT_ADDRESS@, LENGTH = @WOLFBOOT_KEYVAULT_SIZE@ +} + +SECTIONS +{ + .text : + { + _start_text = .; + KEEP(*(.isr_vector)) + *(.text*) + *(.rodata*) + . = ALIGN(8); + _end_text = .; + } > FLASH + + .edidx : + { + . = ALIGN(4); + *(.ARM.exidx*) + } > FLASH + + .data : + { + _start_data = .; + KEEP(*(.data*)) + . = ALIGN(8); + KEEP(*(.ramcode)) + . = ALIGN(8); + _end_data = .; + } > RAM AT > FLASH + _stored_data = LOADADDR(.data); + + .bss (NOLOAD) : + { + _start_bss = .; + __bss_start__ = .; + *(.bss*) + *(COMMON) + . = ALIGN(8); + _end_bss = .; + __bss_end__ = .; + _end = .; + } > RAM + . = ALIGN(8); +} + +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); diff --git a/hal/stm32l5.c b/hal/stm32l5.c index 48ebaa2039..767a8e084d 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) @@ -280,6 +276,12 @@ 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 = (address & ~FLASH_SECURE_MMAP_BASE) | FLASHMEM_ADDRESS_SPACE; +#endif + if (address < ARCH_FLASH_OFFSET) return -1; @@ -425,7 +427,8 @@ static void clock_pll_on(int powersave) DMB(); } -#if TZ_SECURE() +/* A secure application programs its own GTZC peripheral attribution */ +#if TZ_SECURE() && !defined(WOLFBOOT_SECURE_APP) static void periph_unsecure() { volatile uint32_t reg; @@ -533,7 +536,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); @@ -544,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 } diff --git a/hal/stm32l5_lifecycle.h b/hal/stm32l5_lifecycle.h new file mode 100644 index 0000000000..a79187fef9 --- /dev/null +++ b/hal/stm32l5_lifecycle.h @@ -0,0 +1,69 @@ +/* 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: + /* 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: + *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: Secure debug is closed */ + *lifecycle = 0x4000u; /* PSA_LIFECYCLE_NON_PSA_ROT_DEBUG */ + break; + } + } + return ret; +} + +#endif /* WOLFBOOT_STM32L5_LIFECYCLE_H */ 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; 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_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/common/emu_update.c b/test-app/emu-test-apps/common/emu_update.c index 261bc269d4..72f6f03672 100644 --- a/test-app/emu-test-apps/common/emu_update.c +++ b/test-app/emu-test-apps/common/emu_update.c @@ -287,6 +287,30 @@ 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 + +/* 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; + 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) { + 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 +319,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) || 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..03ef4e2730 100644 --- a/test-app/emu-test-apps/stm32l552/uart.c +++ b/test-app/emu-test-apps/stm32l552/uart.c @@ -22,17 +22,26 @@ #include #include "emu_app.h" -#define SYSCLK_HZ 64000000u +/* The secure-app payload runs Secure and uses the Secure peripheral aliases; + * 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 +#else +#define SYSCLK_HZ 64000000u +#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 +49,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" \ diff --git a/tools/unit-tests/unit-secure-handoff.c b/tools/unit-tests/unit-secure-handoff.c index 7148d30ace..d28dfe8692 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, + 0x4000u) != 0) || + (stm32l5_expect_lifecycle(STM32L5_FLASH_OPTR_TZEN | 0x00u, + 0x4000u) != 0) || + (stm32l5_expect_lifecycle(STM32L5_FLASH_OPTR_TZEN | 0xFFu, + 0x4000u) != 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; }