Skip to content

Commit 7f193af

Browse files
dgarskedanielinux
authored andcommitted
Fixes for STM32H5 TrustZone and PKCS11 store
1 parent 8ef4199 commit 7f193af

22 files changed

+266
-268
lines changed

arch.mk

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1346,6 +1346,11 @@ WOLFCRYPT_OBJS+=$(WOLFBOOT_LIB_WOLFSSL)/wolfcrypt/src/logging.o
13461346
# Debug UART
13471347
ifeq ($(DEBUG_UART),1)
13481348
CFLAGS+=-DDEBUG_UART
1349+
1350+
# If this target has a UART driver, add it to the OBJS
1351+
ifneq (,$(wildcard hal/uart/uart_drv_$(TARGET).c))
1352+
OBJS+=hal/uart/uart_drv_$(TARGET).o
1353+
endif
13491354
endif
13501355

13511356
ifeq ($(NXP_CUSTOM_DCD),1)

config/examples/stm32h5-dualbank.config

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,12 @@ V?=0
1818
SPMATH?=1
1919
RAM_CODE?=0
2020
DUALBANK_SWAP?=1
21-
WOLFBOOT_PARTITION_SIZE?=0xC0000
21+
WOLFBOOT_PARTITION_SIZE?=0xA0000
2222
WOLFBOOT_SECTOR_SIZE?=0x2000
23-
WOLFBOOT_PARTITION_BOOT_ADDRESS?=0x08040000
24-
WOLFBOOT_PARTITION_UPDATE_ADDRESS?=0x8140000
23+
WOLFBOOT_PARTITION_BOOT_ADDRESS?=0x08060000
24+
WOLFBOOT_PARTITION_UPDATE_ADDRESS?=0x08160000
2525
WOLFBOOT_PARTITION_SWAP_ADDRESS?=0xFFFFFFFF
2626
FLAGS_HOME=0
2727
DISABLE_BACKUP=0
2828
IMAGE_HEADER_SIZE?=1024
29+
ARMORED=1

config/examples/stm32h5-tz-dualbank-otp-lms.config

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ V?=0
1818
SPMATH?=1
1919
RAM_CODE?=1
2020
DUALBANK_SWAP?=1
21-
WOLFBOOT_PARTITION_SIZE?=0xC0000
21+
WOLFBOOT_PARTITION_SIZE?=0xA0000
2222
WOLFBOOT_SECTOR_SIZE?=0x2000
23-
WOLFBOOT_PARTITION_BOOT_ADDRESS?=0x08040000
24-
WOLFBOOT_PARTITION_UPDATE_ADDRESS?=0x8140000
23+
WOLFBOOT_PARTITION_BOOT_ADDRESS?=0x08060000
24+
WOLFBOOT_PARTITION_UPDATE_ADDRESS?=0x08160000
2525
WOLFBOOT_PARTITION_SWAP_ADDRESS?=0xFFFFFFFF
2626
FLAGS_HOME=0
2727
DISABLE_BACKUP=0

config/examples/stm32h5-tz-dualbank-otp.config

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ V?=0
1818
SPMATH?=1
1919
RAM_CODE?=1
2020
DUALBANK_SWAP?=1
21-
WOLFBOOT_PARTITION_SIZE?=0xC0000
21+
WOLFBOOT_PARTITION_SIZE?=0xA0000
2222
WOLFBOOT_SECTOR_SIZE?=0x2000
23-
WOLFBOOT_PARTITION_BOOT_ADDRESS?=0x08040000
24-
WOLFBOOT_PARTITION_UPDATE_ADDRESS?=0x8140000
23+
WOLFBOOT_PARTITION_BOOT_ADDRESS?=0x08060000
24+
WOLFBOOT_PARTITION_UPDATE_ADDRESS?=0x08160000
2525
WOLFBOOT_PARTITION_SWAP_ADDRESS?=0xFFFFFFFF
2626
FLAGS_HOME=0
2727
DISABLE_BACKUP=0

config/examples/stm32h5-tz.config

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,16 @@ NVM_FLASH_WRITEONCE?=1
1616
WOLFBOOT_VERSION?=1
1717
V?=0
1818
SPMATH?=1
19-
RAM_CODE?=0
19+
RAM_CODE?=1
2020
DUALBANK_SWAP?=0
21-
WOLFBOOT_PARTITION_SIZE?=0xC0000
21+
WOLFBOOT_PARTITION_SIZE?=0xA0000
2222
WOLFBOOT_SECTOR_SIZE?=0x2000
23-
WOLFBOOT_PARTITION_BOOT_ADDRESS?=0x08040000
24-
WOLFBOOT_PARTITION_UPDATE_ADDRESS?=0x8140000
25-
WOLFBOOT_PARTITION_SWAP_ADDRESS?=0x08010000
23+
WOLFBOOT_PARTITION_BOOT_ADDRESS?=0x08060000
24+
WOLFBOOT_PARTITION_UPDATE_ADDRESS?=0x08100000
25+
WOLFBOOT_PARTITION_SWAP_ADDRESS?=0x081A0000
2626
FLAGS_HOME=0
2727
DISABLE_BACKUP=0
2828
WOLFCRYPT_TZ=1
2929
WOLFCRYPT_TZ_PKCS11=1
3030
IMAGE_HEADER_SIZE?=1024
31+
ARMORED=1

config/examples/stm32h5.config

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,12 @@ V?=0
1818
SPMATH?=1
1919
RAM_CODE?=0
2020
DUALBANK_SWAP?=0
21-
WOLFBOOT_PARTITION_SIZE?=0xC0000
21+
WOLFBOOT_PARTITION_SIZE?=0xA0000
2222
WOLFBOOT_SECTOR_SIZE?=0x2000
23-
WOLFBOOT_PARTITION_BOOT_ADDRESS?=0x08040000
24-
WOLFBOOT_PARTITION_UPDATE_ADDRESS?=0x8140000
25-
WOLFBOOT_PARTITION_SWAP_ADDRESS?=0x08010000
23+
WOLFBOOT_PARTITION_BOOT_ADDRESS?=0x08060000
24+
WOLFBOOT_PARTITION_UPDATE_ADDRESS?=0x08100000
25+
WOLFBOOT_PARTITION_SWAP_ADDRESS?=0x081A0000
2626
FLAGS_HOME=0
2727
DISABLE_BACKUP=0
2828
IMAGE_HEADER_SIZE?=1024
29+
ARMORED=1

docs/Targets.md

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -885,6 +885,7 @@ arm-none-eabi-gdb
885885
(gdb) target remote:3333
886886
```
887887

888+
888889
## STM32H5
889890

890891
Like [STM32L5](#stm32l5) and [STM32U5](#stm32u5), STM32H5 support is also demonstrated
@@ -902,8 +903,8 @@ For more information, see [/docs/flash-OTP.md](/docs/flash-OTP.md).
902903
The implementation shows how to switch from secure application to non-secure application,
903904
thanks to the system isolation performed, which splits the internal Flash and internal
904905
SRAM memories into two parts:
905-
- the first 256KB are used by wolfboot running in secure mode and the secure application
906-
- the remaining available space is used for non-secure application and update partition
906+
- the first 384KB are used by wolfboot running in secure mode and the secure application
907+
- the remaining available space (640KB) is used for non-secure application and update partition
907908

908909
The example configuration for this scenario is available in [/config/examples/stm32h5.config](/config/examples/stm32h5.config).
909910

@@ -913,14 +914,14 @@ The example configuration for this scenario is available in [/config/examples/st
913914

914915
`STM32_Programmer_CLI -c port=swd -ob TZEN=0xB4`
915916

916-
- set the option bytes to enable flash secure protection of first 256KB:
917-
`STM32_Programmer_CLI -c port=swd -ob SECWM1_PSTRT=0x0 SECWM1_PEND=0x1F SECWM2_PSTRT=0x1F SECWM2_PEND=0x0`
917+
- set the option bytes to enable flash secure protection of first 384KB:
918+
`STM32_Programmer_CLI -c port=swd -ob SECWM1_PSTRT=0x0 SECWM1_PEND=0x2F SECWM2_PEND=0x0 SECWM2_PSTRT=0x2F`
918919

919920
- flash the wolfboot image to the secure partition:
920921
`STM32_Programmer_CLI -c port=swd -d wolfboot.bin 0x0C000000`
921922

922923
- flash the application image to the non-secure partition:
923-
`STM32_Programmer_CLI -c port=swd -d test-app/image_v1_signed.bin 0x08040000`
924+
`STM32_Programmer_CLI -c port=swd -d test-app/image_v1_signed.bin 0x08060000`
924925

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

@@ -946,50 +947,54 @@ For DUALBANK with TrustZone use `stm32h5-tz-dualbank-otp.config`.
946947

947948
DUALBANK configuration (Tested on NUCLEO-STM32H563ZI):
948949

950+
```
949951
BANK A: 0x08000000 to 0x080FFFFFF (1MB)
950952
BANK B: 0x08100000 to 0x081FFFFFF (1MB)
953+
```
951954

952955
First of all, ensure that the `SWAP_BANK` option byte is off when running wolfBoot
953956
for the first time:
954-
955-
```
956-
STM32_Programmer_CLI -c port=swd -ob SWAP_BANK=0
957-
```
957+
`STM32_Programmer_CLI -c port=swd -ob SWAP_BANK=0`
958958

959959
It is a good idea to start with an empty flash, by erasing all sectors via:
960+
`STM32_Programmer_CLI -c port=swd -e 0 255`
960961

961-
```
962-
STM32_Programmer_CLI -c port=swd -e 0 255
963-
```
964962
Compile wolfBoot with `make`. The file `factory.bin` contains both wolfboot and the
965963
version 1 of the application, and can be uploaded to the board at the beginning
966964
of the first bank using `STM32_Programmer_CLI` tool:
967-
968-
```
969-
STM32_Programmer_CLI -c port=swd -d factory.bin 0x08000000
970-
```
965+
`STM32_Programmer_CLI -c port=swd -d factory.bin 0x08000000`
971966

972967
Optionally, you can upload another copy of wolfboot.bin to the beginning of the second bank.
973-
Wolfboot should take care of copying itself to the second bank upon first boot if you don't.:
974-
975-
```
976-
STM32_Programmer_CLI -c port=swd -d wolfboot.bin 0x08100000
977-
```
968+
Wolfboot should take care of copying itself to the second bank upon first boot if you don't:
969+
`STM32_Programmer_CLI -c port=swd -d wolfboot.bin 0x08100000`
978970

979971
After uploading the images, reboot your board. The green LED should indicate that v1 of the
980972
test application is running.
981973

982974
To initiate an update, sign a new version of the app and upload the v3 to the update partition
983975
on the second bank:
984976

985-
```
977+
```sh
986978
tools/keytools/sign --ecc256 test-app/image.bin wolfboot_signing_private_key.der 3
987-
STM32_Programmer_CLI -c port=swd -d test-app/image_v3_signed.bin 0x08110000
979+
STM32_Programmer_CLI -c port=swd -d test-app/image_v3_signed.bin 0x08160000
988980
```
989981

990982
Reboot the board to initiate an update via DUALBANK hw-assisted swap.
991983
Any version except the first one will also turn on the orange LED.
992984

985+
### STM32H5 Debugging
986+
987+
OpenOCD: `openocd -s /usr/local/share/openocd/scripts -f board/st_nucleo_h5.cfg`
988+
989+
```sh
990+
arm-none-eabi-gdb
991+
source .gdbinit
992+
add-symbol-file test-app/image.elf 0x08060000
993+
mon reset init
994+
b main
995+
c
996+
```
997+
993998

994999
## STM32H7
9951000

docs/flash-OTP.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ reboot : reboot the system
155155
- Sign a new version of the firmware: `./tools/keytools/sign --lms test-app/image.bin wolfboot_signing_private_key.der 2`
156156
- Run "update" command on the shell and wait for xmodem transfer
157157
- Use serial terminal that supports xmodem like "minicom" or "CoolTerm".
158-
* Run `minicom` on `/dev/ttyACM0` and start file transfer using "CTRL+A; S"
158+
* Run `minicom` on `/dev/ttyACM0` and start file transfer using "CTRL+A; S" -> `minicom -D /dev/ttyACM0 -b 115200`
159159
* Select xmodem then navigate to the new signed firmware file `test-app/image_v2_signed.bin`
160160
- During the transfer, the yellow LED will flash.
161161
- The green LED is dim because it's sync with the UART RX

hal/stm32_tz.c

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -213,31 +213,34 @@ void hal_tz_release_nonsecure_area(void)
213213
#define GTZC1_BASE (0x50032400)
214214
#define GTZC1_TZSC (*(volatile uint32_t *)(GTZC1_BASE + 0x00))
215215
#define GTZC1_TZIC (*(volatile uint32_t *)(GTZC1_BASE + 0x0400))
216-
#define GTZC1_MPCBB1_S ((volatile uint32_t *)(GTZC1_BASE + 0x0800 + 0x100))
217-
#define GTZC1_MPCBB2_S ((volatile uint32_t *)(GTZC1_BASE + 0x0C00 + 0x100))
218-
#define GTZC1_MPCBB3_S ((volatile uint32_t *)(GTZC1_BASE + 0x1000 + 0x100))
216+
#define GTZC1_MPCBB1_SECCFGR ((volatile uint32_t *)(GTZC1_BASE + 0x0800 + 0x100))
217+
#define GTZC1_MPCBB2_SECCFGR ((volatile uint32_t *)(GTZC1_BASE + 0x0C00 + 0x100))
218+
#define GTZC1_MPCBB3_SECCFGR ((volatile uint32_t *)(GTZC1_BASE + 0x1000 + 0x100))
219219

220-
#define SET_GTZC1_MPCBBx_S_VCTR(bank,n,val) \
221-
(*((volatile uint32_t *)(GTZC1_MPCBB##bank##_S) + n ))= val
220+
#define SET_GTZC1_MPCBBx_SECCFGR_VCTR(bank,n,val) \
221+
(*((volatile uint32_t *)(GTZC1_MPCBB##bank##_SECCFGR) + n )) = val
222222

223223
void hal_gtzc_init(void)
224224
{
225225
int i;
226-
/* One bit in the bitmask: 512B */
226+
/* One bit in the bitmask: 512B
227+
* 1: Secure access only to block
228+
* 0: Non-secure access only to block
229+
*/
227230

228231
/* Configure SRAM1 as secure (Low 256 KB) */
229232
for (i = 0; i < 16; i++) {
230-
SET_GTZC1_MPCBBx_S_VCTR(1, i, 0xFFFFFFFF);
233+
SET_GTZC1_MPCBBx_SECCFGR_VCTR(1, i, 0xFFFFFFFF);
231234
}
232235

233236
/* Configure SRAM2 as secure (64 KB) */
234237
for (i = 0; i < 4; i++) {
235-
SET_GTZC1_MPCBBx_S_VCTR(2, i, 0xFFFFFFFF);
238+
SET_GTZC1_MPCBBx_SECCFGR_VCTR(2, i, 0xFFFFFFFF);
236239
}
237240

238241
/* Configure SRAM3 as non-secure (320 KB) */
239242
for (i = 0; i < 20; i++) {
240-
SET_GTZC1_MPCBBx_S_VCTR(3, i, 0x0);
243+
SET_GTZC1_MPCBBx_SECCFGR_VCTR(3, i, 0x0);
241244
}
242245
}
243246

hal/stm32h5.c

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,34 +27,46 @@
2727
#include "hal/stm32h5.h"
2828
#include "hal/armv8m_tz.h"
2929

30+
#include "uart_drv.h"
31+
3032
#define PLL_SRC_HSE 1
3133

3234
#if TZ_SECURE()
3335

3436
/* This function assumes that the boot and the update
3537
* partitions are at the same address in the two banks,
36-
* regardless wheather DUALBANK_SWAP is active or not.
38+
* regardless if DUALBANK_SWAP is active or not.
3739
*/
3840
static int is_flash_nonsecure(uint32_t address)
3941
{
40-
uint32_t in_bank_offset = address & 0x000FFFFF;
41-
if (in_bank_offset >= (WOLFBOOT_PARTITION_BOOT_ADDRESS - FLASHMEM_ADDRESS_SPACE))
42+
uint32_t in_bank_offset = (address & 0x000FFFFF);
43+
if (in_bank_offset >= (WOLFBOOT_PARTITION_BOOT_ADDRESS - FLASHMEM_ADDRESS_SPACE)) {
4244
return 1;
43-
else
44-
return 0;
45+
}
46+
return 0;
4547
}
4648
#endif
4749

4850

4951
static void RAMFUNCTION flash_set_waitstates(unsigned int waitstates)
5052
{
5153
uint32_t reg = FLASH_ACR;
52-
if ((reg & FLASH_ACR_LATENCY_MASK) < waitstates)
53-
do {
54-
FLASH_ACR = (reg & ~(FLASH_ACR_LATENCY_MASK | (FLASH_ACR_WRHIGHFREQ_MASK << FLASH_ACR_WRHIGHFREQ_SHIFT))) |
55-
waitstates | (0x02 << FLASH_ACR_WRHIGHFREQ_SHIFT) ;
54+
uint32_t wrhighfreq = 1; /* default flash signal delay */
55+
56+
if ((reg & FLASH_ACR_LATENCY_MASK) < waitstates) {
57+
/* clear wrhighfreq and latency */
58+
reg &= ~(FLASH_ACR_LATENCY_MASK |
59+
(FLASH_ACR_WRHIGHFREQ_MASK << FLASH_ACR_WRHIGHFREQ_SHIFT));
60+
if (waitstates > 3) { /* wait states 4 and 5 require = 2 */
61+
wrhighfreq = 2;
5662
}
57-
while ((FLASH_ACR & FLASH_ACR_LATENCY_MASK) != waitstates);
63+
reg |= (waitstates | (wrhighfreq << FLASH_ACR_WRHIGHFREQ_SHIFT));
64+
FLASH_ACR = reg;
65+
ISB();
66+
DMB();
67+
/* wait for the register to be updated */
68+
while (FLASH_ACR != reg);
69+
}
5870
}
5971

6072
void RAMFUNCTION hal_flash_wait_complete(uint8_t bank)
@@ -538,10 +550,15 @@ static void fork_bootloader(void)
538550
}
539551
#endif
540552

541-
#include "uart_drv.h"
542553
void hal_init(void)
543554
{
544555
clock_pll_on();
556+
557+
#ifdef DEBUG_UART
558+
uart_init(115200, 8, 'N', 1);
559+
uart_write("wolfBoot Init\n", 14);
560+
#endif
561+
545562
#if TZ_SECURE()
546563
hal_gtzc_init();
547564
hal_tz_sau_init();

0 commit comments

Comments
 (0)