Skip to content

Commit ace95cc

Browse files
committed
Added instructions for STM32H5 demo to docs/flash-OTP.md.
1 parent 11ce50f commit ace95cc

File tree

9 files changed

+184
-30
lines changed

9 files changed

+184
-30
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: 122 additions & 5 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,7 +24,7 @@ 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

@@ -39,11 +39,11 @@ And then, to create the image file `otp.bin`:
3939
tools/keytools/otp/otp-keystore-gen
4040

4141

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

4444
After enabling the `FLASH_OTP_KEYSTORE` option in your `.config` file, when you compile wolfBoot by running "make",
4545
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
46+
provision the public keys to the OTP area. By flashing this application to the microcontroller, the public keys contained
4747
in your keystore (previously generated by `keygen`) are written to the OTP area.
4848

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

hal/stm32h7.h

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -279,20 +279,12 @@
279279
#define FLASH_OPT_KEY1 (0x08192A3BU)
280280
#define FLASH_OPT_KEY2 (0x4C5D6E7FU)
281281

282-
#ifndef FLASH_OTP_BASE
282+
/* OTP FLASH AREA */
283283
#define FLASH_OTP_BASE 0x08FFF000
284-
#endif
285-
#ifndef FLASH_OTP_END
286284
#define FLASH_OTP_END 0x08FFF3FF
287-
#endif
288-
#ifndef OTP_SIZE
289285
#define OTP_SIZE 1024
290-
#endif
291-
#ifndef OTP_BLOCKS
292286
#define OTP_BLOCKS 16
293-
#endif
294287

295-
#define OTP_BLOCK_SIZE (OTP_SIZE / OTP_BLOCKS) /* 64 bytes */
296288

297289
/* STM32H7: Due to ECC functionality, it is not possible to write partition/sector
298290
* flags and signature more than once. This flags_cache is used to intercept write operations and

tools/keytools/otp/otp-keystore-gen.c

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* otp-keystore-primer.c
1+
/* otp-keystore-gen.c
22
*
33
* Command line utility to create a OTP image
44
*
@@ -30,8 +30,6 @@
3030
#include <unistd.h>
3131
#include <errno.h>
3232

33-
#define OTP_SIZE 4096
34-
3533
#include "wolfboot/wolfboot.h"
3634
#include "keystore.h"
3735
#include "otp_keystore.h"
@@ -59,7 +57,7 @@ int main(void)
5957
fprintf(stderr, "Error: too few keys (%d), refusing to create %s\n", n_keys, outfile);
6058
exit(1);
6159
}
62-
60+
6361
slot_size = keystore_get_size(0);
6462
slot_size += KEYSTORE_HDR_SIZE;
6563
fprintf(stderr, "Slot size: %d\n", slot_size);
@@ -71,7 +69,7 @@ int main(void)
7169
perror("opening output file");
7270
exit(2);
7371
}
74-
72+
7573
/* Write the header to the beginning of the OTP binary file */
7674
if (write(ofd, &hdr, sizeof(hdr)) != sizeof(hdr)) {
7775
fprintf(stderr, "Error writing to %s: %s\n", outfile, strerror(errno));

0 commit comments

Comments
 (0)