Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/test-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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: |
Expand Down
12 changes: 12 additions & 0 deletions .github/workflows/test-configs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
2 changes: 2 additions & 0 deletions arch.mk
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Comment thread
dgarske marked this conversation as resolved.
Resolved
ARCH_FLASH_OFFSET=0x08000000
ifeq ($(TZEN),1)
WOLFBOOT_ORIGIN=0x0C000000
Expand Down
30 changes: 30 additions & 0 deletions config/examples/stm32u5a5-nonsecure-dualbank.config
Original file line number Diff line number Diff line change
@@ -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
41 changes: 41 additions & 0 deletions config/examples/stm32u5a5-tz-dualbank.config
Original file line number Diff line number Diff line change
@@ -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
57 changes: 56 additions & 1 deletion docs/Targets.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand All @@ -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.
Expand Down Expand Up @@ -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
Expand Down
26 changes: 23 additions & 3 deletions hal/stm32_tz.c
Original file line number Diff line number Diff line change
Expand Up @@ -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))


Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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 */
Expand Down Expand Up @@ -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);

Expand Down
Loading
Loading