Skip to content

Commit eedd86f

Browse files
Merge pull request #455 from danielinux/stm32h5_uart
STM32H5 UART Demo
2 parents 78c9e11 + 1854383 commit eedd86f

25 files changed

+1718
-198
lines changed

.github/workflows/test-configs.yml

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -251,18 +251,24 @@ jobs:
251251
arch: arm
252252
config-file: ./config/examples/stm32h5-dualbank.config
253253

254-
stm32h5_wolfcrypt_tz:
254+
stm32h5_tz_test:
255255
uses: ./.github/workflows/test-build.yml
256256
with:
257257
arch: arm
258-
config-file: ./config/examples/stm32h5-wolfcrypt-tz.config
258+
config-file: ./config/examples/stm32h5-tz.config
259259

260-
stm32h5_tz_dualbank_otp:
260+
stm32h5_tz_dualbank_otp_test:
261261
uses: ./.github/workflows/test-build.yml
262262
with:
263263
arch: arm
264264
config-file: ./config/examples/stm32h5-tz-dualbank-otp.config
265265

266+
stm32h5_tz_dualbank_otp_lms_test:
267+
uses: ./.github/workflows/test-build.yml
268+
with:
269+
arch: arm
270+
config-file: ./config/examples/stm32h5-tz-dualbank-otp-lms.config
271+
266272
stm32h7_test:
267273
uses: ./.github/workflows/test-build.yml
268274
with:
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
ARCH?=ARM
2+
TZEN?=1
3+
TARGET?=stm32h5
4+
SIGN?=LMS
5+
HASH?=SHA256
6+
DEBUG?=0
7+
VTOR?=1
8+
CORTEX_M0?=0
9+
CORTEX_M33?=1
10+
NO_ASM?=0
11+
NO_MPU=1
12+
EXT_FLASH?=0
13+
SPI_FLASH?=0
14+
ALLOW_DOWNGRADE?=0
15+
NVM_FLASH_WRITEONCE?=1
16+
WOLFBOOT_VERSION?=1
17+
V?=0
18+
SPMATH?=1
19+
RAM_CODE?=1
20+
DUALBANK_SWAP?=1
21+
WOLFBOOT_PARTITION_SIZE?=0xC0000
22+
WOLFBOOT_SECTOR_SIZE?=0x2000
23+
WOLFBOOT_PARTITION_BOOT_ADDRESS?=0x08040000
24+
WOLFBOOT_PARTITION_UPDATE_ADDRESS?=0x8140000
25+
WOLFBOOT_PARTITION_SWAP_ADDRESS?=0xFFFFFFFF
26+
FLAGS_HOME=0
27+
DISABLE_BACKUP=0
28+
FLASH_OTP_KEYSTORE=1
29+
WOLFCRYPT_TZ=1
30+
WOLFCRYPT_TZ_PKCS11=1
31+
ARMORED=1
32+
33+
LMS_LEVELS=2
34+
LMS_HEIGHT=5
35+
LMS_WINTERNITZ=8
36+
IMAGE_SIGNATURE_SIZE=2644
37+
IMAGE_HEADER_SIZE?=8192

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ 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?=1
2121
WOLFBOOT_PARTITION_SIZE?=0xC0000
2222
WOLFBOOT_SECTOR_SIZE?=0x2000
@@ -28,3 +28,4 @@ DISABLE_BACKUP=0
2828
FLASH_OTP_KEYSTORE=1
2929
WOLFCRYPT_TZ=1
3030
WOLFCRYPT_TZ_PKCS11=1
31+
ARMORED=1
File renamed without changes.

docs/STM32-TZ.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ non-secure domain can access wolfCrypt through a standard PKCS11 interface and
2727
use the crypto library with pre-provisioned keys that are never exposed to the
2828
non-secure domain.
2929

30-
### Example using STM32-L552
30+
### Example using STM32L552
3131

3232
- Copy the example configuration for STM32-L5 with support for wolfCrypt in
3333
TrustZone-M and PKCS11 interface: `cp config/examples/stm32l5-wolfcrypt-tz.config .config`
@@ -117,9 +117,10 @@ STM32_Programmer_CLI -c port=swd -d test-app/image_v1_signed.bin 0x08040000
117117

118118
### Example using STM32H563
119119

120-
121-
- Copy the example configuration for STM32-L5 with support for wolfCrypt in
122-
TrustZone-M and PKCS11 interface: `cp config/examples/stm32l5-wolfcrypt-tz.config .config`
120+
- Copy one of the example configurations for STM32H5 with support for TrustZone and PKCS11 to `.config`:
121+
`cp config/examples/stm32h5-tz.config .config`
122+
`cp config/examples/stm32h5-tz-dualbank-otp.config .config` (with Dual Bank)
123+
`cp config/examples/stm32h5-tz-dualbank-otp-lms.config .config` (with Dual Bank and PQ LMS)
123124

124125
- Run `make`. `wolfboot.elf` and the test applications are built as separate
125126
objects. The application is signed and stored as `test-app/image_v1_signed.bin`.

docs/Targets.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -884,7 +884,7 @@ The example configuration for this scenario is available in [/config/examples/st
884884

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

887-
### Scenario 2: Trustzone Enabled, wolfCrypt as secure engine for NS applications
887+
### Scenario 2: TrustZone Enabled, wolfCrypt as secure engine for NS applications
888888

889889
This is similar to Scenario 1, but also includes wolfCrypt in secure mode, and
890890
that can be accessed via PKCS11 interface by non-secure applications.
@@ -893,15 +893,17 @@ This option can be enabled with the `WOLFCRYPT_TZ=1` and `WOLFCRYPT_TZ_PKCS11=1`
893893
options in your configuration. This enables a PKCS11 accessible from NS domain via
894894
non-secure callables (NSC).
895895

896-
The example configuration for this scenario is available in [/config/examples/stm32h5-wolfcrypt-tz.config](/config/examples/stm32h5-wolfcrypt-tz.config).
896+
The example configuration for this scenario is available in [/config/examples/stm32h5-tz.config](/config/examples/stm32h5-tz.config).
897897

898898
For more information, see [/docs/STM32-TZ.md](/docs/STM32-TZ.md).
899899

900-
### Scenario 3: DUALBANK mode (Trustzone disabled)
900+
### Scenario 3: DUALBANK mode
901901

902902
The STM32H5 can be configured to use hardware-assisted bank swapping to facilitate the update.
903903
The configuration file to copy into `.config` is `config/examples/stm32h5-dualbank.config`.
904904

905+
For DUALBANK with TrustZone use `stm32h5-tz-dualbank-otp.config`.
906+
905907
DUALBANK configuration (Tested on NUCLEO-STM32H563ZI):
906908

907909
BANK A: 0x08000000 to 0x080FFFFFF (1MB)

docs/flash-OTP.md

Lines changed: 137 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
## Using One-Time Programmable (OTP) flash area for keystore
1+
# Using One-Time Programmable (OTP) flash area for keystore
22

33
Some microcontrollers provide a special area in flash memory that can
44
only be written once and cannot be erased.
@@ -9,7 +9,7 @@ key is a cryptographic key that can be freely distributed and is used to verify
99
of the firmware update image. By storing the public keys in the OTP area, you can ensure that
1010
they are immutable and cannot be tampered with.
1111

12-
### Compiling wolfBoot to access OTP as keystore
12+
## Compiling wolfBoot to access OTP as keystore
1313

1414
To use the OTP area as a keystore, you need to compile wolfBoot with the `FLASH_OTP_KEYSTORE`
1515
option enabled. This option is disabled by default, which means that the keystore is incorporated into
@@ -24,26 +24,29 @@ must be provisioned to the OTP area in a separate step, as described in the next
2424

2525
Depending on the target device, you can either prepare a binary image of the OTP area content, or use `otp-keystore-primer` firmware to directly provision the keys on the target.
2626

27-
### Creating an image of the OTP area content
27+
## Creating an image of the OTP area content
2828

2929
It is possible to create a binary image of the content for the OTP area. The resulting file (otp.bin) can be manually provisioned using any external tool that allows writing to the target OTP area.
3030

3131
To compile the otp-keystore-gen tool using the current keystore content:
3232

33-
```
33+
```sh
3434
make otpgen
3535
```
3636

3737
And then, to create the image file `otp.bin`:
3838

39-
tools/keytools/otp/otp-keystore-gen
39+
40+
```sh
41+
./tools/keytools/otp/otp-keystore-gen
42+
```
4043

4144

42-
### Directly provisioning the public keys to the OTP area (primer)
45+
## Directly provisioning the public keys to the OTP area (primer)
4346

4447
After enabling the `FLASH_OTP_KEYSTORE` option in your `.config` file, when you compile wolfBoot by running "make",
4548
an additional application called `otp-keystore-primer` is generated under `tools/keytools/otp`. This application is used to
46-
provision the public keys to the OTP area. By flashing this application to the microcontroller, the public keys contained
49+
provision the public keys to the OTP area. By flashing this application to the microcontroller, the public keys contained
4750
in your keystore (previously generated by `keygen`) are written to the OTP area.
4851

4952
The `otp-keystore-primer` application is generated with the public keys embedded in it. The keys are retrieved from the `keystore.c` file,
@@ -59,3 +62,130 @@ After generating a new `keystore.c` with the `keygen` application, you can gener
5962
> [!CAUTION]
6063
> ** Be very careful when using the `otp-keystore-primer` application. Use it at your own risk. **
6164
65+
66+
## Examples
67+
68+
### STM32H5 OTP KeyStore
69+
70+
Example for NULCLEO-STM32H563ZI with TrustZone (via PKCS11), DualBank and signing with PQ LMS:
71+
72+
1) Setup the configuration and key tools:
73+
74+
```sh
75+
cp config/examples/stm32h5-tz-dualbank-otp-lms.config .config
76+
make include/target.h
77+
make keytools
78+
```
79+
80+
2) Generate key(s) to write to OTP
81+
82+
- `./examples/keytools/keygen --lms -g 1.key -g 2.key -g 3.key -g 4.key -g 5.key`
83+
84+
3) Backup the generated keys and `src/keystore.c`
85+
- Save to safe place outside of the wolfBoot tree
86+
87+
4) Set the signing key to use
88+
89+
- Copy one of the generated keys to `wolfboot_signing_private_key.der`
90+
- `cp 1.key wolfboot_signing_private_key.der`
91+
92+
5) Setup the OTP keystore
93+
94+
Flash the OTP keystore primer:
95+
- Run `make otp`
96+
- Flash `./tools/keytools/otp/otp-keystore-primer.bin` to `0x08000000`
97+
- Disconnect the tool and hit reset button
98+
- The primer will run and flash keystore.c to OTP and enable write protection on those blocks
99+
100+
OR
101+
102+
Generate OTP (otp.bin) and flash using external tool
103+
- Run `make otpgen`
104+
- Run `./tools/keytools/otp/otp-keystore-gen` to generate an otp.bin file
105+
- Program otp.bin to `0x08FFF000` using external tool like STM32CubeProgrammer
106+
107+
6) Verify OTP keystore
108+
- Read memory at address `0x08FFF000` (should start with ASCII "WOLFBOOT")
109+
- Typically use STM32CubeProgrammer for this
110+
111+
7) Setup the option bytes
112+
- User Configuration 2 -> TrustZone Enable (TZEN=0xB4)
113+
- Bank1 - Flash Watermark area (SECWM1_START=0x00, SECWM1_END=0x1F)
114+
- Bank2 - Flash Watermark area (SECWM2_START=0x00, SECWM2_END=0x1F)
115+
116+
8) Mass erase the device
117+
- STM32CubeProgrammer -> Full chip erase
118+
119+
9) Build wolfBoot and test application using `make`
120+
121+
10) Flash wolfBoot and test-app
122+
123+
- Flash `wolfboot.bin` at `0x0C000000`
124+
- Flash `test-app/image_v1_signed.bin` at `0x08040000`
125+
126+
11) Disconnect and reboot, the red LED should turn on.
127+
128+
12) Connect to USB UART on NUCLEO board for console
129+
130+
Explore the command line (run help)
131+
132+
```sh
133+
========================
134+
STM32H5 wolfBoot demo Application
135+
Copyright 2024 wolfSSL Inc
136+
GPL v3
137+
Version : 0x1
138+
========================
139+
140+
cmd> help
141+
help : shows this help message
142+
info : display information about the system and partitions
143+
success : confirm a successful update
144+
pkcs11 : enable and test crypto calls with PKCS11 in secure mode
145+
random : generate a random number
146+
timestamp : print the current timestamp
147+
benchmark : run the wolfCrypt benchmark
148+
test : run the wolfCrypt test
149+
update : update the firmware via XMODEM
150+
reboot : reboot the system
151+
```
152+
153+
13) Test Update
154+
155+
- Sign a new version of the firmware: `./tools/keytools/sign --lms test-app/image.bin wolfboot_signing_private_key.der 2`
156+
- Run "update" command on the shell and wait for xmodem transfer
157+
- Use serial terminal that supports xmodem like "minicom" or "CoolTerm".
158+
* Run `minicom` on `/dev/ttyACM0` and start file transfer using "CTRL+A; S"
159+
* Select xmodem then navigate to the new signed firmware file `test-app/image_v2_signed.bin`
160+
- During the transfer, the yellow LED will flash.
161+
- The green LED is dim because it's sync with the UART RX
162+
- At the end of the transfer, the new image will be in the update partition.
163+
- Reset board to install new firmware and confirm new version number.
164+
165+
Example update output:
166+
167+
```sh
168+
cmd> update
169+
Erasing update partition...Done.
170+
Waiting for XMODEM transfer...
171+
.......................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................
172+
173+
174+
175+
176+
End of transfer. ret: 0
177+
New firmware version: 0x2
178+
Triggering update...
179+
Update completed successfully.
180+
181+
cmd> reboot
182+
183+
========================
184+
STM32H5 wolfBoot demo Application
185+
Copyright 2024 wolfSSL Inc
186+
GPL v3
187+
Version : 0x2
188+
========================
189+
190+
cmd>
191+
```

0 commit comments

Comments
 (0)