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
8 changes: 8 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -760,6 +760,14 @@ if(ARCH STREQUAL "ARM")
# TrustZone support for Cortex-M33 targets
if(TZEN)
list(APPEND WOLFBOOT_DEFS TZEN)
# non-secure application data outside the partitions (STM32L5/U5/H5)
if(WOLFBOOT_NS_DATA_ADDRESS)
if(NOT WOLFBOOT_NS_DATA_SIZE)
message(FATAL_ERROR "WOLFBOOT_NS_DATA_ADDRESS requires WOLFBOOT_NS_DATA_SIZE")
endif()
list(APPEND WOLFBOOT_DEFS WOLFBOOT_NS_DATA_ADDRESS=${WOLFBOOT_NS_DATA_ADDRESS}
WOLFBOOT_NS_DATA_SIZE=${WOLFBOOT_NS_DATA_SIZE})
endif()
if(CMAKE_SYSTEM_PROCESSOR STREQUAL "cortex-m33")
list(APPEND WOLFBOOT_COMPILE_OPTIONS -mcmse)
list(APPEND WOLFBOOT_LINK_OPTIONS -mcmse)
Expand Down
27 changes: 27 additions & 0 deletions docs/STM32-TZ.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,33 @@ wolfBoot provides a few Non-Secure Callable functions to allow a non-secure
application to perform certain operations that must be run from the secure
domain. For more information, see [API](API.md#nsc-api).

### Non-secure data outside the partitions

The SAU windows wolfBoot opens for the non-secure application cover the boot
partition, the non-secure RAM, the peripherals and the NSC area. An
application that keeps data in flash outside the partitions (device
credentials, settings, a key blob) names that area in `.config` and wolfBoot
adds a SAU region for it on STM32L5, STM32U5 and STM32H5:

```
WOLFBOOT_NS_DATA_ADDRESS?=0x081A2000
WOLFBOOT_NS_DATA_SIZE?=0x6000
```

Both must be multiples of 32 bytes (the SAU granularity), and the area must
lie above wolfBoot and clear of the boot, update and swap partitions; the
build stops otherwise. The same pages must be non-secure in the flash
watermark option bytes (`SECWM`), which wolfBoot does not change at run
time. On STM32H5 the area starts on a sector boundary above the swap
partition in bank 2, so `SECWM2_END` stops at the sector before it (`0x50`
for the example above with `stm32h5.config`); on STM32L5 and STM32U5 bank 2
is already non-secure. `tools/scripts/set-stm32-tz-option-bytes.sh` takes
the two variables into account and refuses an STM32H5 area that would leave
part of the update or swap partition non-secure. The application then reads the area
through its non-secure alias and programs it through the non-secure flash
registers. A `WOLFBOOT_SECURE_APP` build has no non-secure application and
opens no window.

### Example using STM32L552

- Copy the example configuration for STM32-L5 with support for wolfCrypt in
Expand Down
5 changes: 5 additions & 0 deletions docs/Targets.md
Original file line number Diff line number Diff line change
Expand Up @@ -1398,6 +1398,11 @@ The example configuration for this scenario is available in [/config/examples/st

For a full list of all the option bytes tested with this configuration, refer to [STM32-TZ.md](/docs/STM32-TZ.md).

A non-secure application that keeps data in flash above the partitions (for
example device credentials) names the area with `WOLFBOOT_NS_DATA_ADDRESS` and
`WOLFBOOT_NS_DATA_SIZE` in `.config`; see
[Non-secure data outside the partitions](/docs/STM32-TZ.md#non-secure-data-outside-the-partitions).

You can use the "update" command and XMODEM to send a newly signed update (see docs/flash-OTP.md) or use the steps below using the STM32_Programmer:

```sh
Expand Down
57 changes: 57 additions & 0 deletions hal/stm32_tz.c
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,59 @@ void hal_gtzc_init(void)
}
#endif

#if defined(WOLFBOOT_NS_DATA_ADDRESS) && defined(WOLFBOOT_NS_DATA_SIZE) && \
!defined(WOLFBOOT_SECURE_APP) && !defined(PULL_LINKER_DEFINES)
/* The SAU works in 32-byte units, and the area must keep clear of wolfBoot
* and of the partitions, which updates erase. Checked in the non-secure
* alias, since the partitions may be given in the secure one. */
#define NS_DATA_FLASH(a) ((a) & ~0x04000000UL)
#define NS_DATA_OVERLAPS(base, len) \
((NS_DATA_FLASH(WOLFBOOT_NS_DATA_ADDRESS) < \
(NS_DATA_FLASH(base) + (len))) && \
(NS_DATA_FLASH(base) < \
(NS_DATA_FLASH(WOLFBOOT_NS_DATA_ADDRESS) + WOLFBOOT_NS_DATA_SIZE)))
#if ((WOLFBOOT_NS_DATA_SIZE) == 0) || \
(((WOLFBOOT_NS_DATA_ADDRESS) % 32) != 0) || \
(((WOLFBOOT_NS_DATA_SIZE) % 32) != 0)
#error "WOLFBOOT_NS_DATA_ADDRESS and _SIZE must be non-zero multiples of 32"
#endif
#if ((WOLFBOOT_NS_DATA_ADDRESS) + (WOLFBOOT_NS_DATA_SIZE)) > 0x100000000
#error "WOLFBOOT_NS_DATA_ADDRESS + WOLFBOOT_NS_DATA_SIZE passes 4 GB"
#endif
#if NS_DATA_FLASH(WOLFBOOT_NS_DATA_ADDRESS) < \
NS_DATA_FLASH(WOLFBOOT_PARTITION_BOOT_ADDRESS)
#error "WOLFBOOT_NS_DATA_ADDRESS must be above wolfBoot"
#endif
#if NS_DATA_OVERLAPS(WOLFBOOT_PARTITION_BOOT_ADDRESS, WOLFBOOT_PARTITION_SIZE)
#error "WOLFBOOT_NS_DATA_ADDRESS overlaps the boot partition"
#endif
#if NS_DATA_OVERLAPS(WOLFBOOT_PARTITION_UPDATE_ADDRESS, \
WOLFBOOT_PARTITION_UPDATE_SIZE)
#error "WOLFBOOT_NS_DATA_ADDRESS overlaps the update partition"
#endif
#if NS_DATA_OVERLAPS(WOLFBOOT_PARTITION_SWAP_ADDRESS, WOLFBOOT_SECTOR_SIZE)
#error "WOLFBOOT_NS_DATA_ADDRESS overlaps the swap partition"
#endif
#endif

/* Non-secure application data kept in flash outside the partitions (device
* credentials, settings): a SAU window over WOLFBOOT_NS_DATA_ADDRESS for
* WOLFBOOT_NS_DATA_SIZE bytes. The same pages must be non-secure in the
* SECWM option bytes, which hal_tz_release_nonsecure_area does not touch.
* A secure application has no non-secure side, so it gets no window. */
static void sau_init_ns_data(uint32_t region)
{
#if defined(WOLFBOOT_NS_DATA_ADDRESS) && defined(WOLFBOOT_NS_DATA_SIZE) && \
!defined(WOLFBOOT_SECURE_APP)
/* the window is in the non-secure alias, whichever one is configured */
uint32_t start = (uint32_t)(WOLFBOOT_NS_DATA_ADDRESS) & ~0x04000000UL;

sau_init_region(region, start, start + WOLFBOOT_NS_DATA_SIZE - 1, 0);
#else
(void)region;
#endif
}

#ifdef TARGET_stm32h5

void hal_tz_sau_init(void)
Expand Down Expand Up @@ -369,6 +422,8 @@ void hal_tz_sau_init(void)
/* Set as non-secure: OTP + RO area */
sau_init_region(4, 0x08FFF000, 0x08FFFFFF, 0);

sau_init_ns_data(5);

/* Enable SAU */
SAU_CTRL = SAU_INIT_CTRL_ENABLE;

Expand Down Expand Up @@ -401,6 +456,8 @@ void hal_tz_sau_init(void)
sau_init_region(3, 0x40000000, 0x4FFFFFFF, 0);
#endif

sau_init_ns_data(5);

/* Enable SAU */
SAU_CTRL = SAU_INIT_CTRL_ENABLE;

Expand Down
9 changes: 9 additions & 0 deletions options.mk
Original file line number Diff line number Diff line change
Expand Up @@ -2567,6 +2567,15 @@ endif

ifeq ($(TZEN),1)
CFLAGS+=-DTZEN
# non-secure application data outside the partitions (STM32L5/U5/H5):
# a SAU window the non-secure image may read and program
ifneq ($(WOLFBOOT_NS_DATA_ADDRESS),)
ifeq ($(WOLFBOOT_NS_DATA_SIZE),)
$(error WOLFBOOT_NS_DATA_ADDRESS requires WOLFBOOT_NS_DATA_SIZE)
endif
CFLAGS+=-DWOLFBOOT_NS_DATA_ADDRESS=$(WOLFBOOT_NS_DATA_ADDRESS)
CFLAGS+=-DWOLFBOOT_NS_DATA_SIZE=$(WOLFBOOT_NS_DATA_SIZE)
Comment thread
dgarske marked this conversation as resolved.
Resolved
endif
endif

ifeq ($(WOLFBOOT_SECURE_APP),1)
Expand Down
1 change: 1 addition & 0 deletions tools/config.mk
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ CONFIG_VARS:= ARCH TARGET SIGN HASH MCUXSDK MCUXPRESSO MCUXPRESSO_CPU MCUXPRESSO
WOLFBOOT_PARTITION_SIZE WOLFBOOT_SECTOR_SIZE WOLFBOOT_RAM_SIZE \
WOLFBOOT_PARTITION_BOOT_ADDRESS WOLFBOOT_PARTITION_UPDATE_ADDRESS \
WOLFBOOT_PARTITION_SWAP_ADDRESS WOLFBOOT_LOAD_ADDRESS \
WOLFBOOT_NS_DATA_ADDRESS WOLFBOOT_NS_DATA_SIZE \
WOLFBOOT_LOAD_DTS_ADDRESS WOLFBOOT_LOAD_RAMDISK_ADDRESS \
WOLFBOOT_LOAD_KERNEL_ADDRESS \
WOLFBOOT_VERSION_SECTION \
Expand Down
36 changes: 36 additions & 0 deletions tools/scripts/set-stm32-tz-option-bytes.sh
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,42 @@ last_wolfboot_sector_hex="$(printf "0x%x" "$last_wolfboot_sector")"
last_partition_sector=$((last_wolfboot_sector + WOLFBOOT_PARTITION_SIZE / WOLFBOOT_SECTOR_SIZE))
last_partition_sector_hex="$(printf "0x%x" "$last_partition_sector")"

# Non-secure application data outside the partitions (WOLFBOOT_NS_DATA_ADDRESS
# and _SIZE). On STM32H5 the bank 2 watermark covers the whole bank, so it
# stops at the sector before the data area, which must start on a sector
# boundary above the update and swap partitions and end inside bank 2. On
# STM32L5 and STM32U5 bank 2 is non-secure already.
WOLFBOOT_NS_DATA_ADDRESS="$(awk -F '?=|:=|=' '$1 == "WOLFBOOT_NS_DATA_ADDRESS" { print $2 }' .config)"
WOLFBOOT_NS_DATA_SIZE="$(awk -F '?=|:=|=' '$1 == "WOLFBOOT_NS_DATA_SIZE" { print $2 }' .config)"
if [ "$TARGET" = stm32h5 ] && [ -n "$WOLFBOOT_NS_DATA_ADDRESS" ]; then
WOLFBOOT_PARTITION_UPDATE_ADDRESS="$(awk -F '?=|:=|=' '$1 == "WOLFBOOT_PARTITION_UPDATE_ADDRESS" { print $2 }' .config)"
WOLFBOOT_PARTITION_SWAP_ADDRESS="$(awk -F '?=|:=|=' '$1 == "WOLFBOOT_PARTITION_SWAP_ADDRESS" { print $2 }' .config)"
WOLFBOOT_PARTITION_UPDATE_SIZE="$(awk -F '?=|:=|=' '$1 == "WOLFBOOT_PARTITION_UPDATE_SIZE" { print $2 }' .config)"
FLASH_BANK2_OFFSET=0x08100000
FLASH_END=0x08200000
ns_alias=0x04000000
ns_data=$((WOLFBOOT_NS_DATA_ADDRESS & ~ns_alias))
ns_end=$((ns_data + ${WOLFBOOT_NS_DATA_SIZE:-0}))
update_end=$(((WOLFBOOT_PARTITION_UPDATE_ADDRESS & ~ns_alias) + ${WOLFBOOT_PARTITION_UPDATE_SIZE:-$WOLFBOOT_PARTITION_SIZE}))
swap_end=$(((WOLFBOOT_PARTITION_SWAP_ADDRESS & ~ns_alias) + WOLFBOOT_SECTOR_SIZE))
if [ "$ns_end" -le "$ns_data" ] || [ "$ns_data" -lt $((FLASH_BANK2_OFFSET)) ] || \
[ "$ns_end" -gt $((FLASH_END)) ]; then
echo "Error: WOLFBOOT_NS_DATA_ADDRESS and _SIZE must describe an area inside flash bank 2" >&2
exit 1
fi
if [ $(((ns_data - FLASH_BANK2_OFFSET) % WOLFBOOT_SECTOR_SIZE)) -ne 0 ]; then
echo "Error: WOLFBOOT_NS_DATA_ADDRESS must start on a flash sector boundary" >&2
exit 1
fi
if [ "$ns_data" -lt "$update_end" ] || [ "$ns_data" -lt "$swap_end" ]; then
echo "Error: WOLFBOOT_NS_DATA_ADDRESS must be above the update and swap partitions" >&2
exit 1
fi
# bank 2 stays secure up to the sector before the data area
last_partition_sector=$(((ns_data - FLASH_BANK2_OFFSET) / WOLFBOOT_SECTOR_SIZE - 1))
last_partition_sector_hex="$(printf "0x%x" "$last_partition_sector")"
fi

declare -a obn # option bytes names
declare -a obv # option bytes values
oblen=0 # length of the above arrays
Expand Down
Loading