From b199ecb2bb3f48a256ca80cd597baaa429629617 Mon Sep 17 00:00:00 2001 From: David Garske Date: Tue, 1 Dec 2026 16:31:19 -0800 Subject: [PATCH] stm32u5: support 4 MB devices and TrustZone with DUALBANK_SWAP --- .github/workflows/test-build.yml | 2 + .github/workflows/test-configs.yml | 12 + .gitignore | 3 + CMakeLists.txt | 3 + arch.mk | 2 + .../stm32u5a5-nonsecure-dualbank.config | 30 + config/examples/stm32u5a5-tz-dualbank.config | 41 ++ docs/Targets.md | 57 +- hal/stm32_tz.c | 26 +- hal/stm32u5.c | 90 ++- hal/stm32u5.h | 22 +- tools/config.mk | 2 +- tools/unit-tests/Makefile | 30 + tools/unit-tests/unit-stm32u5-tz-dualbank.c | 533 ++++++++++++++++++ 14 files changed, 831 insertions(+), 22 deletions(-) create mode 100644 config/examples/stm32u5a5-nonsecure-dualbank.config create mode 100644 config/examples/stm32u5a5-tz-dualbank.config create mode 100644 tools/unit-tests/unit-stm32u5-tz-dualbank.c diff --git a/.github/workflows/test-build.yml b/.github/workflows/test-build.yml index 7e1adb9862..3ce2536681 100644 --- a/.github/workflows/test-build.yml +++ b/.github/workflows/test-build.yml @@ -93,6 +93,8 @@ jobs: inputs.config-file == './config/examples/stm32u5.config' || inputs.config-file == './config/examples/stm32u5-wolfcrypt-tz.config' || inputs.config-file == './config/examples/stm32u5-nonsecure-dualbank.config' || + inputs.config-file == './config/examples/stm32u5a5-nonsecure-dualbank.config' || + inputs.config-file == './config/examples/stm32u5a5-tz-dualbank.config' || inputs.config-file == './config/examples/stm32n6.config' || inputs.config-file == './config/examples/stm32n6-tz.config' run: | diff --git a/.github/workflows/test-configs.yml b/.github/workflows/test-configs.yml index aef24ad2d9..d76cbb38c1 100644 --- a/.github/workflows/test-configs.yml +++ b/.github/workflows/test-configs.yml @@ -1481,6 +1481,18 @@ jobs: arch: arm config-file: ./config/examples/stm32u5-nonsecure-dualbank.config + stm32u5a5_nonsecure_dualbank_test: + uses: ./.github/workflows/test-build.yml + with: + arch: arm + config-file: ./config/examples/stm32u5a5-nonsecure-dualbank.config + + stm32u5a5_tz_dualbank_test: + uses: ./.github/workflows/test-build.yml + with: + arch: arm + config-file: ./config/examples/stm32u5a5-tz-dualbank.config + stm32u5_test: uses: ./.github/workflows/test-build.yml with: diff --git a/.gitignore b/.gitignore index e58350d14b..16d2121d6a 100644 --- a/.gitignore +++ b/.gitignore @@ -554,6 +554,8 @@ tools/unit-tests/unit-sdhci-uhs-recover tools/unit-tests/unit-sdhci-wait-busy tools/unit-tests/unit-stm32l5-write tools/unit-tests/unit-stm32u5-write +tools/unit-tests/unit-stm32u5-tz-dualbank +tools/unit-tests/unit-stm32u5-tz-dualbank-2mb tools/unit-tests/unit-t10xx-dts-memac tools/unit-tests/unit-t10xx-qe-firmware tools/unit-tests/unit-t2080-fman-loader @@ -573,6 +575,7 @@ tools/unit-tests/unit-sama5d3-ext-read tools/unit-tests/aurix_erased_extract.h tools/unit-tests/nvm_cache_scrub_extract.h tools/unit-tests/imx95_ele_extract.h +tools/unit-tests/stm32u5_tz_dualbank_extract.h tools/unit-tests/imx95_disk_extract.h tools/unit-tests/nxp_ls1028a_host.c tools/unit-tests/nxp_p1021_host.c diff --git a/CMakeLists.txt b/CMakeLists.txt index a4a0a3c180..aef3447890 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -732,6 +732,9 @@ if(ARCH STREQUAL "ARM") endif() if(${WOLFBOOT_TARGET} STREQUAL "stm32u5") set(ARCH_FLASH_OFFSET 0x08000000) + if(DEFINED STM32U5_FLASH_SIZE_MB) + list(APPEND WOLFBOOT_DEFS STM32U5_FLASH_SIZE_MB=${STM32U5_FLASH_SIZE_MB}) + endif() if(TZEN) set(WOLFBOOT_ORIGIN 0x0C000000) else() diff --git a/arch.mk b/arch.mk index 9b55016bc9..b5e75ba86c 100644 --- a/arch.mk +++ b/arch.mk @@ -802,6 +802,8 @@ ifeq ($(ARCH),ARM) ifeq ($(TARGET),stm32u5) CORTEX_M33=1 CFLAGS+=-Ihal + STM32U5_FLASH_SIZE_MB?=2 + CFLAGS+=-DSTM32U5_FLASH_SIZE_MB=$(STM32U5_FLASH_SIZE_MB) ARCH_FLASH_OFFSET=0x08000000 ifeq ($(TZEN),1) WOLFBOOT_ORIGIN=0x0C000000 diff --git a/config/examples/stm32u5a5-nonsecure-dualbank.config b/config/examples/stm32u5a5-nonsecure-dualbank.config new file mode 100644 index 0000000000..3659c53fdf --- /dev/null +++ b/config/examples/stm32u5a5-nonsecure-dualbank.config @@ -0,0 +1,30 @@ +ARCH?=ARM +TZEN?=0 +TARGET?=stm32u5 +SIGN?=ECC256 +HASH?=SHA256 +DEBUG?=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?=0 +DUALBANK_SWAP?=1 +# STM32U5A5/U5A9/U599/U5Fx/U5Gx: 4 MB flash, two 2 MB banks +STM32U5_FLASH_SIZE_MB?=4 +WOLFBOOT_PARTITION_SIZE?=0x100000 +WOLFBOOT_SECTOR_SIZE?=0x2000 +WOLFBOOT_PARTITION_BOOT_ADDRESS?=0x08010000 +WOLFBOOT_PARTITION_UPDATE_ADDRESS?=0x08210000 +WOLFBOOT_PARTITION_SWAP_ADDRESS?=0xFFFFFFFF + +# Use a larger image header size to enforce alignment requirements for the interrupt vector table +IMAGE_HEADER_SIZE?=1024 diff --git a/config/examples/stm32u5a5-tz-dualbank.config b/config/examples/stm32u5a5-tz-dualbank.config new file mode 100644 index 0000000000..276f18ddc2 --- /dev/null +++ b/config/examples/stm32u5a5-tz-dualbank.config @@ -0,0 +1,41 @@ +# STM32U5A5/U5A9/U599/U5Fx/U5Gx (4 MB flash, two 2 MB banks) with TrustZone +# and hardware-assisted bank swap. Required option bytes: +# TZEN=1 +# SECWM1_PSTRT=0x0 SECWM1_PEND=0x1F +# SECWM2_PSTRT=0x0 SECWM2_PEND=0x1F +# SWAP_BANK=0 on the first boot +ARCH?=ARM +TZEN?=1 +TARGET?=stm32u5 +SIGN?=ECC256 +HASH?=SHA256 +DEBUG?=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?=0 +DUALBANK_SWAP?=1 +STM32U5_FLASH_SIZE_MB?=4 +WOLFBOOT_PARTITION_SIZE?=0x100000 +WOLFBOOT_SECTOR_SIZE?=0x2000 +WOLFBOOT_KEYVAULT_ADDRESS?=0x0C038000 +WOLFBOOT_KEYVAULT_SIZE?=0 +WOLFBOOT_NSC_ADDRESS?=0x0C038000 +WOLFBOOT_NSC_SIZE?=0x8000 +WOLFBOOT_PARTITION_BOOT_ADDRESS?=0x08040000 +WOLFBOOT_PARTITION_UPDATE_ADDRESS?=0x08240000 +WOLFBOOT_PARTITION_SWAP_ADDRESS?=0xFFFFFFFF +FLAGS_HOME=0 +DISABLE_BACKUP=0 + +# Use a larger image header size to enforce alignment requirements for the interrupt vector table +IMAGE_HEADER_SIZE?=1024 diff --git a/docs/Targets.md b/docs/Targets.md index e0f51d7ba3..09d4113ddf 100644 --- a/docs/Targets.md +++ b/docs/Targets.md @@ -463,7 +463,7 @@ For more information, see [/docs/STM32-TZ.md](/docs/STM32-TZ.md). #### Example Description The implementation shows how to use STM32U5xx in DUAL_BANK mode, with TrustZone disabled. -The DUAL_BANK option is only available on this target when TrustZone is disabled (TZEN = 0). +For DUAL_BANK with TrustZone enabled see [Scenario 4](#scenario-4-trustzone-enabled-dual-bank-mode). The flash memory is segmented into two different banks: @@ -484,6 +484,46 @@ Program each partition using: RED LD9 will be on indicating successful boot () +### Scenario 4: TrustZone Enabled (DUAL BANK mode) + +`DUALBANK_SWAP=1` can be combined with `TZEN=1`. Both banks have the same layout: a secure area (wolfBoot, keyvault and non-secure callable area) from the start of the bank up to the offset of the BOOT partition, and a non-secure application partition above it. wolfBoot copies the secure area of bank 1 to bank 2 on the first boot. The update is stored in the bank that is not mapped at `0x08000000`, at the same offset as the BOOT partition. When it holds a valid image with a higher version, wolfBoot toggles the `SWAP_BANK` option bit and reboots; an image that fails verification is erased and the current one keeps booting. + +The secure watermark of both banks must cover the secure area and nothing else, so that the two banks are interchangeable. A secure access to a non-secure flash page reads as zero on this family, so wolfBoot maps the UPDATE partition, like the BOOT partition, as a non-secure SAU region. The application can program an update there through the non-secure flash interface. + +The example configuration [/config/examples/stm32u5a5-tz-dualbank.config](/config/examples/stm32u5a5-tz-dualbank.config) is for 4 MB devices (`STM32U5_FLASH_SIZE_MB=4`) and has been tested on the NUCLEO-U5A5ZJ-Q: + +| Area | Bank 1 | Bank 2 | +|------------------------------|--------------|--------------| +| wolfBoot (secure alias) | `0x0C000000` | `0x0C200000` | +| Application (BOOT / UPDATE) | `0x08040000` | `0x08240000` | + +Option bytes (the secure area is 256 KB, pages 0 to 31 of each bank): + +``` +TZEN = 1 +SECWM1_PSTRT=0x0 SECWM1_PEND=0x1F +SECWM2_PSTRT=0x0 SECWM2_PEND=0x1F +SWAP_BANK = 0 On the first boot +``` + +``` +cp config/examples/stm32u5a5-tz-dualbank.config .config +make +STM32_Programmer_CLI -c port=swd mode=UR -ob TZEN=1 +STM32_Programmer_CLI -c port=swd mode=UR -ob SECWM1_PSTRT=0x0 SECWM1_PEND=0x1F SECWM2_PSTRT=0x0 SECWM2_PEND=0x1F +STM32_Programmer_CLI -c port=swd mode=UR -d ./wolfboot.bin 0x0C000000 +STM32_Programmer_CLI -c port=swd mode=UR -d ./test-app/image_v1_signed.bin 0x08040000 +``` + +To update, sign a new version and program it to the UPDATE partition: + +``` +IMAGE_HEADER_SIZE=1024 ./tools/keytools/sign --ecc256 --sha256 test-app/image.bin wolfboot_signing_private_key.der 2 +STM32_Programmer_CLI -c port=swd mode=UR -d ./test-app/image_v2_signed.bin 0x08240000 +``` + +On a 2 MB device (STM32U575/U585) bank 2 starts at `0x08100000` and each bank has `0xC0000` left after the secure area: set `STM32U5_FLASH_SIZE_MB=2` and `WOLFBOOT_PARTITION_SIZE=0xC0000` and place the UPDATE partition at `0x08140000`. The build stops with an error if the BOOT and UPDATE partitions are not at the same offset in their banks or do not fit in them. `WOLFCRYPT_TZ` is not supported with this layout. + ### Debugging Use `make DEBUG=1` and reload firmware. @@ -516,6 +556,21 @@ add-symbol-file test-app/image.elf ``` +### 4 MB devices (STM32U59x/U5Ax/U5Fx/U5Gx) + +The HAL defaults to the 2 MB flash geometry of the STM32U575/U585 (bank 2 at `0x08100000`). For a 4 MB device set `STM32U5_FLASH_SIZE_MB=4`, which moves bank 2 to `0x08200000`, the end of flash to `0x083FFFFF` and doubles the number of secure block-based registers handled per bank. These devices are always dual-bank. + +The example configuration [/config/examples/stm32u5a5-nonsecure-dualbank.config](/config/examples/stm32u5a5-nonsecure-dualbank.config) uses `DUALBANK_SWAP=1` with TrustZone disabled: the bootloader at `0x08000000`, the application at `0x08010000` and the update at the same offset in bank 2, `0x08210000`. It has been tested on the NUCLEO-U5A5ZJ-Q. + +``` +cp config/examples/stm32u5a5-nonsecure-dualbank.config .config +make +STM32_Programmer_CLI -c port=swd -d ./factory.bin 0x08000000 +IMAGE_HEADER_SIZE=1024 ./tools/keytools/sign --ecc256 --sha256 test-app/image.bin wolfboot_signing_private_key.der 2 +STM32_Programmer_CLI -c port=swd -d ./test-app/image_v2_signed.bin 0x08210000 +``` + + ## STM32L0 Example 192KB partitioning on STM32-L073 diff --git a/hal/stm32_tz.c b/hal/stm32_tz.c index bc3422a6d7..e7c6a97d7e 100644 --- a/hal/stm32_tz.c +++ b/hal/stm32_tz.c @@ -38,6 +38,18 @@ #include "image.h" #include "hal.h" #include "target.h" + +#ifdef TARGET_stm32h5 + #define FLASH_BANKS_SWAPPED() \ + ((FLASH_OPTSR_CUR & FLASH_OPTSR_SWAP_BANK) >> 31) +#elif defined(TARGET_stm32u5) + #define FLASH_BANKS_SWAPPED() \ + ((FLASH_OPTR & FLASH_OPTR_SWAP_BANK) != 0) +#elif defined(DUALBANK_SWAP) && defined(__ARM_FEATURE_CMSE) && \ + (__ARM_FEATURE_CMSE == 3U) + #error "DUALBANK_SWAP with TrustZone is not supported on this target" +#endif + #if defined (__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3U) && (!defined(FLAGS_HOME) || !defined(DISABLE_BACKUP)) @@ -117,9 +129,9 @@ void hal_tz_claim_nonsecure_area(uint32_t address, int len) bank = 1; } page_n = start_page_n; -#ifdef TARGET_stm32h5 +#if defined(TARGET_stm32h5) || defined(TARGET_stm32u5) /* Take into account current swap configuration */ - if ((FLASH_OPTSR_CUR & FLASH_OPTSR_SWAP_BANK) >> 31) + if (FLASH_BANKS_SWAPPED()) bank = !bank; #endif while (address < end) { @@ -155,7 +167,7 @@ void hal_tz_release_nonsecure_area(void) uint32_t addr; int bank_swp = 0; /* Take into account current swap configuration */ - if ((FLASH_OPTSR_CUR & FLASH_OPTSR_SWAP_BANK) >> 31) + if (FLASH_BANKS_SWAPPED()) bank_swp = 1; /* Bank 1 */ @@ -394,6 +406,14 @@ void hal_tz_sau_init(void) WOLFBOOT_PARTITION_BOOT_ADDRESS + WOLFBOOT_PARTITION_SIZE - 1, 0); +#if defined(TARGET_stm32u5) && defined(DUALBANK_SWAP) + /* Non-secure: update partition. A secure access to a non-secure flash + * page reads as zero, so it is accessed through a non-secure window. */ + sau_init_region(4, WOLFBOOT_PARTITION_UPDATE_ADDRESS, + WOLFBOOT_PARTITION_UPDATE_ADDRESS + WOLFBOOT_PARTITION_SIZE - 1, + 0); +#endif + /* Non-secure RAM region in SRAM1/SRAM2 */ sau_init_region(2, 0x20020000, 0x2003FFFF, 0); diff --git a/hal/stm32u5.c b/hal/stm32u5.c index 1c467a9530..b657ab0280 100644 --- a/hal/stm32u5.c +++ b/hal/stm32u5.c @@ -71,6 +71,39 @@ static uint32_t RAMFUNCTION flash_addr_unalias(uint32_t address) address = (address & ~FLASH_SECURE_MMAP_BASE) | FLASHMEM_ADDRESS_SPACE; return address; } + +#ifdef DUALBANK_SWAP +/* Both banks have the same layout: secure (FLASH_CR) below the offset of the + * BOOT partition, non-secure (FLASH_NS_CR) above it. */ +static int RAMFUNCTION flash_addr_is_secure(uint32_t address) +{ + if (address >= FLASH_BANK2_BASE) + address -= (FLASH_BANK2_BASE - FLASHMEM_ADDRESS_SPACE); + return (address < WOLFBOOT_PARTITION_BOOT_ADDRESS); +} + +#ifdef WOLFCRYPT_SECURE_MODE +/* The keyvault in the secure area is only mirrored to bank 2 while the banks + * are not swapped, so run-time keyvault writes would be lost on a swap. */ +#error "DUALBANK_SWAP with TrustZone does not support WOLFCRYPT_TZ" +#endif +#endif +#endif + +#ifdef DUALBANK_SWAP +#if (WOLFBOOT_PARTITION_BOOT_ADDRESS + WOLFBOOT_PARTITION_SIZE) > FLASH_BANK2_BASE +#error "DUALBANK_SWAP: the BOOT partition must end in bank 1" +#endif +#if (WOLFBOOT_PARTITION_UPDATE_ADDRESS - FLASH_BANK2_BASE) != \ + (WOLFBOOT_PARTITION_BOOT_ADDRESS - FLASHMEM_ADDRESS_SPACE) +#error "DUALBANK_SWAP: UPDATE must be at the same offset in bank 2 as BOOT in bank 1" +#endif +#if (WOLFBOOT_PARTITION_UPDATE_ADDRESS + WOLFBOOT_PARTITION_SIZE - 1) > FLASH_TOP +#error "DUALBANK_SWAP: the UPDATE partition must end in bank 2" +#endif +#if ((WOLFBOOT_PARTITION_BOOT_ADDRESS - FLASHMEM_ADDRESS_SPACE) % FLASH_PAGE_SIZE) != 0 +#error "DUALBANK_SWAP: the BOOT partition must start on a flash page boundary" +#endif #endif int RAMFUNCTION hal_flash_write(uint32_t address, const uint8_t *data, int len) @@ -87,7 +120,14 @@ int RAMFUNCTION hal_flash_write(uint32_t address, const uint8_t *data, int len) #endif dst = (uint32_t*)address; -#if (TZ_SECURE()) +#if (TZ_SECURE()) && defined(DUALBANK_SWAP) + if (address > FLASH_TOP) { + return 0; /* Address out of range */ + } + /* Interface and alias are chosen per 128-bit unit below */ + cr = &FLASH_NS_CR; + sr = &FLASH_NS_SR; +#elif (TZ_SECURE()) if ((((FLASH_OPTR & FLASH_OPTR_DBANK) == 0) && (address <= FLASH_TOP)) || (address < FLASH_BANK2_BASE)) { cr = &FLASH_CR; sr = &FLASH_SR; @@ -113,6 +153,22 @@ int RAMFUNCTION hal_flash_write(uint32_t address, const uint8_t *data, int len) int off = (int)(cur_addr & 0x0FUL); int i_aligned = i - off; +#if (TZ_SECURE()) && defined(DUALBANK_SWAP) + /* A write can cross the BOOT or UPDATE offset, or the bank end */ + if (cur_addr > FLASH_TOP) + return 0; + if (flash_addr_is_secure(cur_addr)) { + cr = &FLASH_CR; + sr = &FLASH_SR; + unit = (uint32_t *)(((cur_addr & (~0x0FUL)) & + (~FLASHMEM_ADDRESS_SPACE)) | FLASH_SECURE_MMAP_BASE); + } + else { + cr = &FLASH_NS_CR; + sr = &FLASH_NS_SR; + } +#endif + /* Read-modify-write the whole 128-bit unit (as stm32h5.c): * the program only starts on the 4th word, and a partial * quad-word leaves FLASH_SR_WDW set, hanging the wait. The @@ -250,6 +306,9 @@ int RAMFUNCTION hal_flash_erase(uint32_t address, int len) *cr &= ~FLASH_CR_PER ; return 0; /* Address out of range */ } +#if (TZ_SECURE()) && defined(DUALBANK_SWAP) + cr = flash_addr_is_secure(p) ? &FLASH_CR : &FLASH_NS_CR; +#endif /* BKER refers to the physical bank, whatever the SWAP_BANK setting * (RM0456 7.5.8): invert it when the banks are swapped, so that the * erased bank is the one currently mapped at the target address. */ @@ -263,6 +322,10 @@ int RAMFUNCTION hal_flash_erase(uint32_t address, int len) DMB(); *cr |= FLASH_CR_STRT; hal_flash_wait_complete(0); +#if (TZ_SECURE()) && defined(DUALBANK_SWAP) + /* The next page may use the other controller */ + *cr &= ~FLASH_CR_PER; +#endif } /* If the erase operation is completed, disable the associated bits */ *cr &= ~FLASH_CR_PER ; @@ -589,24 +652,25 @@ static void periph_unsecure(void) #endif #if defined(DUALBANK_SWAP) && defined(__WOLFBOOT) -static uint8_t bootloader_copy_mem[BOOTLOADER_SIZE]; +static uint8_t bootloader_copy_mem[BOOTLOADER_COPY_MEM_SIZE]; static void RAMFUNCTION fork_bootloader(void) { uint8_t *data = (uint8_t *) FLASHMEM_ADDRESS_SPACE; uint32_t dst = FLASH_BANK2_BASE; + uint32_t i; - /* Return if content already matches */ - if (memcmp(data, (void *)FLASH_BANK2_BASE, BOOTLOADER_SIZE) == 0) - return; - - /* Read the wolfBoot image in RAM */ - memcpy(bootloader_copy_mem, data, BOOTLOADER_SIZE); + for (i = 0; i < BOOTLOADER_SIZE; i += BOOTLOADER_COPY_MEM_SIZE) { + /* Skip pages that already match */ + if (memcmp(data + i, (void *)(dst + i), BOOTLOADER_COPY_MEM_SIZE) == 0) + continue; + /* Read the wolfBoot image in RAM */ + memcpy(bootloader_copy_mem, data + i, BOOTLOADER_COPY_MEM_SIZE); - /* Mass-erase */ - hal_flash_unlock(); - hal_flash_erase(dst, BOOTLOADER_SIZE); - hal_flash_write(dst, bootloader_copy_mem, BOOTLOADER_SIZE); - hal_flash_lock(); + hal_flash_unlock(); + hal_flash_erase(dst + i, BOOTLOADER_COPY_MEM_SIZE); + hal_flash_write(dst + i, bootloader_copy_mem, BOOTLOADER_COPY_MEM_SIZE); + hal_flash_lock(); + } } #endif diff --git a/hal/stm32u5.h b/hal/stm32u5.h index 122efaac06..81ff77c893 100644 --- a/hal/stm32u5.h +++ b/hal/stm32u5.h @@ -164,6 +164,14 @@ #define PWR_UCPDR_DBDIS (1 << 0) /*** FLASH ***/ +/* Total flash size: 2 for U575/U585, 4 for U59x/U5Ax/U5Fx/U5Gx. The two + * banks are each half of it. */ +#ifndef STM32U5_FLASH_SIZE_MB +#define STM32U5_FLASH_SIZE_MB 2 +#endif +#if (STM32U5_FLASH_SIZE_MB != 2) && (STM32U5_FLASH_SIZE_MB != 4) +#error "STM32U5_FLASH_SIZE_MB must be 2 (U575/U585) or 4 (U59x/U5Ax/U5Fx/U5Gx)" +#endif #define SYSCFG_APB2_CLOCK_ER_VAL (1 << 0) /* <> - RCC_APB2ENR - SYSCFGEN */ @@ -177,7 +185,7 @@ #define FLASH_SECBB1 ((volatile uint32_t *)(FLASH_BASE + 0x80)) /* Array */ #define FLASH_SECBB2 ((volatile uint32_t *)(FLASH_BASE + 0xA0)) /* Array */ -#define FLASH_SECBB_NREGS 4 /* Array length for the two above */ +#define FLASH_SECBB_NREGS (STM32U5_FLASH_SIZE_MB * 2) /* 32 pages per register */ #define FLASH_NS_BASE (0x40022000) /* RM0456 - Table 4 */ #define FLASH_NS_KEYR (*(volatile uint32_t *)(FLASH_NS_BASE + 0x08)) @@ -240,9 +248,15 @@ #define FLASHMEM_ADDRESS_SPACE (0x08000000) #define FLASH_PAGE_SIZE (0x2000) /* 8KB */ -#define FLASH_BANK2_BASE (0x08100000) /*!< Base address of Flash Bank2 */ -#define BOOTLOADER_SIZE (0x8000) -#define FLASH_TOP (0x081FFFFF) /*!< FLASH end address */ +#define FLASH_BANK2_BASE (FLASHMEM_ADDRESS_SPACE + \ + (STM32U5_FLASH_SIZE_MB * 0x80000)) /*!< Base address of Flash Bank2 */ +/* Everything below the BOOT partition: wolfBoot, and with TrustZone also the + * keyvault and NSC area */ +#define BOOTLOADER_SIZE (WOLFBOOT_PARTITION_BOOT_ADDRESS - \ + FLASHMEM_ADDRESS_SPACE) +#define BOOTLOADER_COPY_MEM_SIZE FLASH_PAGE_SIZE +#define FLASH_TOP (FLASHMEM_ADDRESS_SPACE + \ + (STM32U5_FLASH_SIZE_MB * 0x100000) - 1) /*!< FLASH end address */ #define FLASH_KEY1 (0x45670123) #define FLASH_KEY2 (0xCDEF89AB) diff --git a/tools/config.mk b/tools/config.mk index ee5fa9d734..7ed9649fd1 100644 --- a/tools/config.mk +++ b/tools/config.mk @@ -138,7 +138,7 @@ CONFIG_VARS:= ARCH TARGET SIGN HASH MCUXSDK MCUXPRESSO MCUXPRESSO_CPU MCUXPRESSO WOLFCRYPT_TZ_PSA \ WOLFBOOT_DICE_HW \ WOLFCRYPT_TZ_FWTPM \ - WOLFBOOT_PARTITION_SIZE WOLFBOOT_SECTOR_SIZE WOLFBOOT_RAM_SIZE \ + WOLFBOOT_PARTITION_SIZE WOLFBOOT_SECTOR_SIZE WOLFBOOT_RAM_SIZE STM32U5_FLASH_SIZE_MB \ WOLFBOOT_PARTITION_BOOT_ADDRESS WOLFBOOT_PARTITION_UPDATE_ADDRESS \ WOLFBOOT_PARTITION_SWAP_ADDRESS WOLFBOOT_LOAD_ADDRESS \ WOLFBOOT_LOAD_DTS_ADDRESS WOLFBOOT_LOAD_RAMDISK_ADDRESS \ diff --git a/tools/unit-tests/Makefile b/tools/unit-tests/Makefile index 81c6cf0e5d..d8d84008f5 100644 --- a/tools/unit-tests/Makefile +++ b/tools/unit-tests/Makefile @@ -225,6 +225,7 @@ TESTS+=unit-stm32l4-write TESTS+=unit-stm32wb-write TESTS+=unit-stm32l5-write TESTS+=unit-stm32u5-write +TESTS+=unit-stm32u5-tz-dualbank unit-stm32u5-tz-dualbank-2mb TESTS+=unit-stm32c0-write TESTS+=unit-stm32h7s-write TESTS+=unit-nvm-cache-scrub @@ -2732,6 +2733,34 @@ 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) +# unit-stm32u5-tz-dualbank runs the real flash_addr_unalias(), +# flash_addr_is_secure(), hal_flash_write() and hal_flash_erase() from +# hal/stm32u5.c with TZ_SECURE() true and DUALBANK_SWAP: the area below +# the BOOT offset in either bank goes through FLASH_CR at the 0x0C +# alias, the partitions through FLASH_NS_CR at 0x08. The DUALBANK_SWAP +# layout guards are extracted too, so each layout is checked by them. +stm32u5_tz_dualbank_extract.h: ../../hal/stm32u5.c + sed -n '/^void RAMFUNCTION hal_flash_clear_errors/,/^}/p' $< > $@ + sed -n '/^static uint32_t RAMFUNCTION flash_addr_unalias(/,/^}/p' $< >> $@ + sed -n '/^static int RAMFUNCTION flash_addr_is_secure(/,/^}/p' $< >> $@ + sed -n '/^#if (WOLFBOOT_PARTITION_BOOT_ADDRESS + WOLFBOOT_PARTITION_SIZE) > FLASH_BANK2_BASE/,/^#endif/p' $< >> $@ + sed -n '/^#if (WOLFBOOT_PARTITION_UPDATE_ADDRESS - FLASH_BANK2_BASE)/,/^#endif/p' $< >> $@ + sed -n '/^#if (WOLFBOOT_PARTITION_UPDATE_ADDRESS + WOLFBOOT_PARTITION_SIZE - 1) > FLASH_TOP/,/^#endif/p' $< >> $@ + sed -n '/^int RAMFUNCTION hal_flash_write/,/^}/p' $< >> $@ + sed -n '/^int RAMFUNCTION hal_flash_erase/,/^}/p' $< >> $@ + +unit-stm32u5-tz-dualbank: unit-stm32u5-tz-dualbank.c stm32u5_tz_dualbank_extract.h + gcc -o $@ unit-stm32u5-tz-dualbank.c -DSTM32U5_FLASH_SIZE_MB=4 \ + -DWOLFBOOT_PARTITION_BOOT_ADDRESS=0x08040000 \ + -DWOLFBOOT_PARTITION_UPDATE_ADDRESS=0x08240000 \ + -DWOLFBOOT_PARTITION_SIZE=0x100000 $(CFLAGS) $(LDFLAGS) + +unit-stm32u5-tz-dualbank-2mb: unit-stm32u5-tz-dualbank.c stm32u5_tz_dualbank_extract.h + gcc -o $@ unit-stm32u5-tz-dualbank.c -DSTM32U5_FLASH_SIZE_MB=2 \ + -DWOLFBOOT_PARTITION_BOOT_ADDRESS=0x08040000 \ + -DWOLFBOOT_PARTITION_UPDATE_ADDRESS=0x08140000 \ + -DWOLFBOOT_PARTITION_SIZE=0xC0000 $(CFLAGS) $(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 @@ -3031,6 +3060,7 @@ GENERATED_SRC:=aurix_erased_extract.h elf_store_extract.h \ stm32c0_write_extract.h stm32g0_write_extract.h stm32g4_write_extract.h stm32l4_write_extract.h \ stm32h7s_write_extract.h stm32h7_trailer_extract.h \ stm32l5_write_extract.h stm32u5_write_extract.h stm32wb_write_extract.h \ + stm32u5_tz_dualbank_extract.h \ t10xx_flash_status_extract.h t10xx_qe_firmware_extract.h \ t2080_fman_extract.h \ ti_hercules_write_extract.h versal_ext_write_extract.h versal_host.c \ diff --git a/tools/unit-tests/unit-stm32u5-tz-dualbank.c b/tools/unit-tests/unit-stm32u5-tz-dualbank.c new file mode 100644 index 0000000000..5927690369 --- /dev/null +++ b/tools/unit-tests/unit-stm32u5-tz-dualbank.c @@ -0,0 +1,533 @@ +/* unit-stm32u5-tz-dualbank.c + * + * Flash routing of hal/stm32u5.c in a TrustZone build with + * DUALBANK_SWAP. Both banks have the same layout: the area below the + * offset of the BOOT partition (wolfBoot and its bank 2 copy) is + * secure and is programmed through FLASH_CR at the secure alias + * (0x0C...); the BOOT and UPDATE partitions are non-secure and are + * programmed through FLASH_NS_CR at the 0x08... address. + * + * The real flash_addr_unalias(), flash_addr_is_secure(), + * hal_flash_clear_errors(), hal_flash_write() and hal_flash_erase(), + * plus the DUALBANK_SWAP layout guards, are extracted by the Makefile + * and built with TZ_SECURE() true. The secure and non-secure register + * files are separate host arrays. The 0x08... and 0x0C... flash + * aliases are two separate host mappings at those addresses, so a + * program lands in the alias it was issued to and the test can tell + * which one it was. The mock hal_flash_wait_complete() records which + * controller had PG (write) or STRT (erase) set. + * + * Built twice: 4 MB (STM32U5_FLASH_SIZE_MB=4) and 2 MB. + * + * 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 +#include + +#ifndef STM32U5_FLASH_SIZE_MB +#error "build with -DSTM32U5_FLASH_SIZE_MB=2 or 4" +#endif + +/* Host stand-ins for the ARM primitives and the TZ build selection. */ +#define RAMFUNCTION +#define ISB() do { } while (0) +#define DMB() do { } while (0) +#define TZ_SECURE() (1) +#ifndef DUALBANK_SWAP +#define DUALBANK_SWAP +#endif + +/* Host FLASH register files (offsets as in hal/stm32u5.h). */ +static uint32_t g_flash_s_regs[0x100 / sizeof(uint32_t)]; +static uint32_t g_flash_ns_regs[0x100 / sizeof(uint32_t)]; +#define FLASH_BASE ((uintptr_t)g_flash_s_regs) +#define FLASH_NS_BASE ((uintptr_t)g_flash_ns_regs) +#define FLASH_SR (*(volatile uint32_t *)(FLASH_BASE + 0x24)) +#define FLASH_CR (*(volatile uint32_t *)(FLASH_BASE + 0x2C)) +#define FLASH_OPTR (*(volatile uint32_t *)(FLASH_BASE + 0x40)) +#define FLASH_NS_SR (*(volatile uint32_t *)(FLASH_NS_BASE + 0x20)) +#define FLASH_NS_CR (*(volatile uint32_t *)(FLASH_NS_BASE + 0x28)) +#define FLASH_SR_EOP (1 << 0) +#define FLASH_SR_OPERR (1 << 1) +#define FLASH_SR_PROGERR (1 << 3) +#define FLASH_SR_WRPERR (1 << 4) +#define FLASH_SR_PGAERR (1 << 5) +#define FLASH_SR_SIZERR (1 << 6) +#define FLASH_SR_PGSERR (1 << 7) +#define FLASH_SR_OPTWERR (1 << 13) +#define FLASH_SR_BSY (1 << 16) +#define FLASH_SR_WDW (1 << 17) +#define FLASH_CR_PG (1 << 0) +#define FLASH_CR_PER (1 << 1) +#define FLASH_CR_PNB_SHIFT 3 +#define FLASH_CR_PNB_MASK 0xFF +#define FLASH_CR_BKER (1 << 11) +#define FLASH_CR_STRT (1 << 16) +#define FLASH_OPTR_DBANK (1 << 21) +#define FLASH_OPTR_SWAP_BANK (1 << 20) + +/* Flash map (as in hal/stm32u5.h). */ +#define FLASH_SECURE_MMAP_BASE (0x0C000000) +#define FLASHMEM_ADDRESS_SPACE (0x08000000) +#define ARCH_FLASH_OFFSET FLASHMEM_ADDRESS_SPACE +#define FLASH_PAGE_SIZE (0x2000) +#define FLASH_BANK2_BASE (FLASHMEM_ADDRESS_SPACE + \ + (STM32U5_FLASH_SIZE_MB * 0x80000)) +#define FLASH_TOP (FLASHMEM_ADDRESS_SPACE + \ + (STM32U5_FLASH_SIZE_MB * 0x100000) - 1) +#define FLASH_SZ (STM32U5_FLASH_SIZE_MB * 0x100000) + +#define BOOT WOLFBOOT_PARTITION_BOOT_ADDRESS +#define UPDATE WOLFBOOT_PARTITION_UPDATE_ADDRESS +#define BANK2 FLASH_BANK2_BASE +#define S_ALIAS(a) (((a) & ~FLASHMEM_ADDRESS_SPACE) | FLASH_SECURE_MMAP_BASE) + +static uint8_t *g_ns_mem; /* 0x08000000 */ +static uint8_t *g_s_mem; /* 0x0C000000 */ + +/* Per wait for completion: which controller was programming or + * erasing, and the CR value it held. */ +#define MAX_OPS 8 +static int g_s_ops, g_ns_ops; +static uint32_t g_op_cr[MAX_OPS]; +static int g_op_secure[MAX_OPS]; +static int g_nops; + +static void record(int secure, uint32_t cr) +{ + if (g_nops < MAX_OPS) { + g_op_cr[g_nops] = cr; + g_op_secure[g_nops] = secure; + } + g_nops++; +} + +/* Mock hal_flash_wait_complete(): the real one only spins on BSY. The + * controller has STRT cleared by hardware at the end of an erase. */ +static void hal_flash_wait_complete(uint8_t bank) +{ + (void)bank; + if ((FLASH_CR & (FLASH_CR_PG | FLASH_CR_STRT)) != 0) { + g_s_ops++; + record(1, FLASH_CR); + } + if ((FLASH_NS_CR & (FLASH_CR_PG | FLASH_CR_STRT)) != 0) { + g_ns_ops++; + record(0, FLASH_NS_CR); + } + FLASH_CR &= ~FLASH_CR_STRT; + FLASH_NS_CR &= ~FLASH_CR_STRT; +} + +/* The real functions and layout guards from hal/stm32u5.c. */ +#include "stm32u5_tz_dualbank_extract.h" + +static uint8_t g_data[32]; + +static void setup(void) +{ + int i; + + memset(g_flash_s_regs, 0, sizeof(g_flash_s_regs)); + memset(g_flash_ns_regs, 0, sizeof(g_flash_ns_regs)); + memset(g_ns_mem, 0xFF, FLASH_SZ); + memset(g_s_mem, 0xFF, FLASH_SZ); + FLASH_OPTR = FLASH_OPTR_DBANK; + g_s_ops = g_ns_ops = g_nops = 0; + memset(g_op_cr, 0, sizeof(g_op_cr)); + memset(g_op_secure, 0, sizeof(g_op_secure)); + for (i = 0; i < (int)sizeof(g_data); i++) + g_data[i] = (uint8_t)(0x30 + i); +} + +static void teardown(void) +{ +} + +static uint8_t *ns_ptr(uint32_t a) +{ + return g_ns_mem + (a - FLASHMEM_ADDRESS_SPACE); +} + +static uint8_t *s_ptr(uint32_t a) +{ + return g_s_mem + (a - FLASHMEM_ADDRESS_SPACE); +} + +static int is_erased(const uint8_t *p, int len) +{ + int i; + + for (i = 0; i < len; i++) + if (p[i] != 0xFF) + return 0; + return 1; +} + +/* Write 16 bytes at a 0x08 address that must be secure: the data lands + * in the 0x0C alias through FLASH_CR, and the 0x08 alias is untouched. */ +static void check_write_secure(uint32_t addr) +{ + ck_assert_int_eq(hal_flash_write(addr, g_data, 16), 0); + ck_assert_int_eq(g_s_ops, 1); + ck_assert_int_eq(g_ns_ops, 0); + ck_assert_int_eq(memcmp(s_ptr(addr), g_data, 16), 0); + ck_assert_int_eq(is_erased(ns_ptr(addr), 16), 1); + ck_assert_uint_eq(FLASH_CR & FLASH_CR_PG, 0); +} + +/* Write 16 bytes that must be non-secure: the data lands at the 0x08 + * address through FLASH_NS_CR, and the 0x0C alias is untouched. */ +static void check_write_nonsecure(uint32_t addr, uint32_t in_addr) +{ + ck_assert_int_eq(hal_flash_write(in_addr, g_data, 16), 0); + ck_assert_int_eq(g_ns_ops, 1); + ck_assert_int_eq(g_s_ops, 0); + ck_assert_int_eq(memcmp(ns_ptr(addr), g_data, 16), 0); + ck_assert_int_eq(is_erased(s_ptr(addr), 16), 1); + ck_assert_uint_eq(FLASH_NS_CR & FLASH_CR_PG, 0); +} + +/* Write 32 bytes starting 16 bytes before a boundary: the first unit + * goes to one interface and alias, the second to the other. */ +static void check_write_across(uint32_t boundary, int first_secure) +{ + uint32_t a = boundary - 16; + + ck_assert_int_eq(hal_flash_write(a, g_data, 32), 0); + ck_assert_int_eq(g_s_ops, 1); + ck_assert_int_eq(g_ns_ops, 1); + ck_assert_int_eq(g_op_secure[0], first_secure); + ck_assert_int_eq(g_op_secure[1], !first_secure); + ck_assert_int_eq(memcmp(first_secure ? s_ptr(a) : ns_ptr(a), + g_data, 16), 0); + ck_assert_int_eq(memcmp(first_secure ? ns_ptr(boundary) : + s_ptr(boundary), g_data + 16, 16), 0); + ck_assert_int_eq(is_erased(first_secure ? ns_ptr(a) : s_ptr(a), 16), 1); + ck_assert_int_eq(is_erased(first_secure ? s_ptr(boundary) : + ns_ptr(boundary), 16), 1); +} + +static uint32_t pnb(uint32_t cr) +{ + return (cr >> FLASH_CR_PNB_SHIFT) & FLASH_CR_PNB_MASK; +} + +/* Erase one page and check the controller, page number and bank. */ +static void check_erase(uint32_t in_addr, int secure, uint32_t page, + int bker) +{ + ck_assert_int_eq(hal_flash_erase(in_addr, FLASH_PAGE_SIZE), 0); + ck_assert_int_eq(g_nops, 1); + ck_assert_int_eq(g_s_ops, secure ? 1 : 0); + ck_assert_int_eq(g_ns_ops, secure ? 0 : 1); + ck_assert_int_eq(g_op_secure[0], secure); + ck_assert_uint_eq(pnb(g_op_cr[0]), page); + ck_assert_uint_ne(g_op_cr[0] & FLASH_CR_PER, 0); + ck_assert_int_eq((g_op_cr[0] & FLASH_CR_BKER) != 0, bker); +} + +/* The layout under test is the one the request specifies. */ +START_TEST(test_layout) +{ +#if STM32U5_FLASH_SIZE_MB == 4 + ck_assert_uint_eq(BANK2, 0x08200000); + ck_assert_uint_eq(FLASH_TOP, 0x083FFFFF); + ck_assert_uint_eq(BOOT, 0x08040000); + ck_assert_uint_eq(UPDATE, 0x08240000); +#else + ck_assert_uint_eq(BANK2, 0x08100000); + ck_assert_uint_eq(FLASH_TOP, 0x081FFFFF); + ck_assert_uint_eq(BOOT, 0x08040000); + ck_assert_uint_eq(UPDATE, 0x08140000); +#endif + ck_assert_int_eq(flash_addr_is_secure(FLASHMEM_ADDRESS_SPACE), 1); + ck_assert_int_eq(flash_addr_is_secure(BOOT - 1), 1); + ck_assert_int_eq(flash_addr_is_secure(BOOT), 0); + ck_assert_int_eq(flash_addr_is_secure(BANK2 - 1), 0); + ck_assert_int_eq(flash_addr_is_secure(BANK2), 1); + ck_assert_int_eq(flash_addr_is_secure(UPDATE - 1), 1); + ck_assert_int_eq(flash_addr_is_secure(UPDATE), 0); + ck_assert_int_eq(flash_addr_is_secure(FLASH_TOP), 0); +} +END_TEST + +/* Bank 1, wolfBoot area (secure). */ +START_TEST(test_write_bank1_secure) +{ + check_write_secure(FLASHMEM_ADDRESS_SPACE + 0x100); +} +END_TEST + +/* Secure area into BOOT, secure area into UPDATE, and the end of + * bank 1 (non-secure BOOT) into the start of bank 2 (secure). */ +START_TEST(test_write_across_boot) +{ + check_write_across(BOOT, 1); +} +END_TEST + +START_TEST(test_write_across_update) +{ + check_write_across(UPDATE, 1); +} +END_TEST + +START_TEST(test_write_across_bank_end) +{ + check_write_across(BANK2, 0); +} +END_TEST + +/* Last unit before BOOT: still secure. */ +START_TEST(test_write_bank1_secure_last_unit) +{ + check_write_secure(BOOT - 16); +} +END_TEST + +/* BOOT partition (non-secure). */ +START_TEST(test_write_boot_nonsecure) +{ + check_write_nonsecure(BOOT, BOOT); +} +END_TEST + +/* Bank 2, secure copy of wolfBoot. */ +START_TEST(test_write_bank2_secure) +{ + check_write_secure(BANK2); +} +END_TEST + +/* Last unit before UPDATE: still secure. */ +START_TEST(test_write_bank2_secure_last_unit) +{ + check_write_secure(UPDATE - 16); +} +END_TEST + +/* UPDATE partition (non-secure). */ +START_TEST(test_write_update_nonsecure) +{ + check_write_nonsecure(UPDATE, UPDATE); +} +END_TEST + +/* A secure-alias input in UPDATE is unaliased and routed as non-secure: + * programmed at the 0x08 address through FLASH_NS_CR. */ +START_TEST(test_write_update_alias_unaliased) +{ + check_write_nonsecure(UPDATE + 0x10, S_ALIAS(UPDATE + 0x10)); +} +END_TEST + +/* A secure-alias input in BOOT, likewise. */ +START_TEST(test_write_boot_alias_unaliased) +{ + check_write_nonsecure(BOOT + 0x20, S_ALIAS(BOOT + 0x20)); +} +END_TEST + +/* A secure-alias input in the secure area stays secure. */ +START_TEST(test_write_bank2_alias_secure) +{ + check_write_secure(BANK2 + 0x40); + memset(s_ptr(BANK2 + 0x40), 0xFF, 16); + g_s_ops = 0; + ck_assert_int_eq(hal_flash_write(S_ALIAS(BANK2 + 0x40), g_data, 16), 0); + ck_assert_int_eq(g_s_ops, 1); + ck_assert_int_eq(g_ns_ops, 0); + ck_assert_int_eq(memcmp(s_ptr(BANK2 + 0x40), g_data, 16), 0); +} +END_TEST + +/* Above FLASH_TOP (in either alias): rejected, nothing programmed. */ +START_TEST(test_write_above_top_rejected) +{ + ck_assert_int_eq(hal_flash_write(FLASH_TOP + 1, g_data, 16), 0); + ck_assert_int_eq(hal_flash_write(S_ALIAS(FLASH_TOP + 1), g_data, 16), 0); + ck_assert_int_eq(g_s_ops + g_ns_ops, 0); + ck_assert_uint_eq(FLASH_CR & FLASH_CR_PG, 0); + ck_assert_uint_eq(FLASH_NS_CR & FLASH_CR_PG, 0); +} +END_TEST + +START_TEST(test_erase_bank1_secure) +{ + check_erase(FLASHMEM_ADDRESS_SPACE, 1, 0, 0); +} +END_TEST + +START_TEST(test_erase_bank1_secure_last_page) +{ + check_erase(BOOT - FLASH_PAGE_SIZE, 1, + ((BOOT - FLASHMEM_ADDRESS_SPACE) >> 13) - 1, 0); +} +END_TEST + +START_TEST(test_erase_boot_nonsecure) +{ + check_erase(BOOT, 0, (BOOT - FLASHMEM_ADDRESS_SPACE) >> 13, 0); +} +END_TEST + +/* Bank 2 pages set BKER. */ +START_TEST(test_erase_bank2_secure) +{ + check_erase(BANK2, 1, 0, 1); +} +END_TEST + +START_TEST(test_erase_update_nonsecure) +{ + check_erase(UPDATE, 0, (UPDATE - BANK2) >> 13, 1); +} +END_TEST + +START_TEST(test_erase_last_page) +{ + check_erase(FLASH_TOP + 1 - FLASH_PAGE_SIZE, 0, + (FLASH_TOP + 1 - FLASH_PAGE_SIZE - BANK2) >> 13, 1); +} +END_TEST + +START_TEST(test_erase_update_alias_unaliased) +{ + check_erase(S_ALIAS(UPDATE), 0, (UPDATE - BANK2) >> 13, 1); +} +END_TEST + +/* With the banks swapped, BKER names the physical bank: inverted. */ +START_TEST(test_erase_swapped_bker) +{ + FLASH_OPTR = FLASH_OPTR_DBANK | FLASH_OPTR_SWAP_BANK; + check_erase(UPDATE, 0, (UPDATE - BANK2) >> 13, 0); + setup(); + FLASH_OPTR = FLASH_OPTR_DBANK | FLASH_OPTR_SWAP_BANK; + check_erase(FLASHMEM_ADDRESS_SPACE, 1, 0, 1); +} +END_TEST + +/* An erase across the BOOT boundary switches controller per page. */ +START_TEST(test_erase_across_boot_boundary) +{ + ck_assert_int_eq(hal_flash_erase(BOOT - FLASH_PAGE_SIZE, + 2 * FLASH_PAGE_SIZE), 0); + ck_assert_int_eq(g_nops, 2); + ck_assert_int_eq(g_op_secure[0], 1); + ck_assert_int_eq(g_op_secure[1], 0); + ck_assert_uint_eq(pnb(g_op_cr[1]), (BOOT - FLASHMEM_ADDRESS_SPACE) >> 13); +} +END_TEST + +/* An erase across the bank 2 UPDATE boundary, likewise, both BKER. */ +START_TEST(test_erase_across_update_boundary) +{ + ck_assert_int_eq(hal_flash_erase(UPDATE - FLASH_PAGE_SIZE, + 2 * FLASH_PAGE_SIZE), 0); + ck_assert_int_eq(g_nops, 2); + ck_assert_int_eq(g_op_secure[0], 1); + ck_assert_int_eq(g_op_secure[1], 0); + ck_assert_uint_ne(g_op_cr[0] & FLASH_CR_BKER, 0); + ck_assert_uint_ne(g_op_cr[1] & FLASH_CR_BKER, 0); +} +END_TEST + +START_TEST(test_erase_above_top_rejected) +{ + ck_assert_int_eq(hal_flash_erase(FLASH_TOP + 1, FLASH_PAGE_SIZE), 0); + ck_assert_int_eq(hal_flash_erase(S_ALIAS(FLASH_TOP + 1), + FLASH_PAGE_SIZE), 0); + ck_assert_int_eq(g_nops, 0); +} +END_TEST + +Suite *stm32u5_tz_dualbank_suite(void) +{ + Suite *s = suite_create("stm32u5-tz-dualbank"); + TCase *tc = tcase_create("stm32u5-tz-dualbank"); + + tcase_add_checked_fixture(tc, setup, teardown); + tcase_add_test(tc, test_layout); + tcase_add_test(tc, test_write_bank1_secure); + tcase_add_test(tc, test_write_bank1_secure_last_unit); + tcase_add_test(tc, test_write_boot_nonsecure); + tcase_add_test(tc, test_write_bank2_secure); + tcase_add_test(tc, test_write_bank2_secure_last_unit); + tcase_add_test(tc, test_write_update_nonsecure); + tcase_add_test(tc, test_write_update_alias_unaliased); + tcase_add_test(tc, test_write_boot_alias_unaliased); + tcase_add_test(tc, test_write_bank2_alias_secure); + tcase_add_test(tc, test_write_above_top_rejected); + tcase_add_test(tc, test_write_across_boot); + tcase_add_test(tc, test_write_across_update); + tcase_add_test(tc, test_write_across_bank_end); + tcase_add_test(tc, test_erase_bank1_secure); + tcase_add_test(tc, test_erase_bank1_secure_last_page); + tcase_add_test(tc, test_erase_boot_nonsecure); + tcase_add_test(tc, test_erase_bank2_secure); + tcase_add_test(tc, test_erase_update_nonsecure); + tcase_add_test(tc, test_erase_last_page); + tcase_add_test(tc, test_erase_update_alias_unaliased); + tcase_add_test(tc, test_erase_swapped_bker); + tcase_add_test(tc, test_erase_across_boot_boundary); + tcase_add_test(tc, test_erase_across_update_boundary); + tcase_add_test(tc, test_erase_above_top_rejected); + suite_add_tcase(s, tc); + + return s; +} + +static uint8_t *map_flash(uintptr_t addr) +{ + void *p = mmap((void *)addr, FLASH_SZ, PROT_READ | PROT_WRITE, + MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED_NOREPLACE | MAP_NORESERVE, + -1, 0); + + if (p == MAP_FAILED || p != (void *)addr) + return NULL; + return p; +} + +int main(void) +{ + int fails; + Suite *s; + SRunner *sr; + + g_ns_mem = map_flash(FLASHMEM_ADDRESS_SPACE); + g_s_mem = map_flash(FLASH_SECURE_MMAP_BASE); + if (g_ns_mem == NULL || g_s_mem == NULL) + return 99; + + s = stm32u5_tz_dualbank_suite(); + sr = srunner_create(s); + srunner_run_all(sr, CK_NORMAL); + fails = srunner_ntests_failed(sr); + srunner_free(sr); + + munmap(g_ns_mem, FLASH_SZ); + munmap(g_s_mem, FLASH_SZ); + + return fails; +}