stm32h723
The stm32h723
example uses a Nucleo-h723zg board. The board has three LEDs
of which two LEDs
are used in this example.
Note:
- If you're using a different version of the board, you'll probably need to edit your firmware's
partition-addresses
to accommodate for differences.- Just make sure you don't change the names of files or the folder structure, as cargo xtask looks for these file/folder names.
Partitioning:
The first step in integrating rustBoot is flash-memory partitioning
i.e. we divide the stm32h723
's flash-memory into 4 partitions, taking into account the geometry of the flash memory.
Note:
- Error correction code memory is a type of flash memory data storage that uses error correction code to detect and correct n-bit data corruption that occurs in memory.
- Since the STM32H7 series devices have a 10-bit
ECC
function, it is not possible to write partition status/sector flags more than once, so use a biggerPARTITION_SIZE
. The last128KB
sector in each partition will be reserved for bootloader flags or partition status.
You can read more about
mcu
partitioning here
In this example, we'll be using the following partitioning scheme. You can locate these constants in the constants module
#![allow(unused)] fn main() { #[cfg(feature = "stm32h723")] pub const SECTOR_SIZE: usize = 0x20000; #[cfg(feature = "stm32h723")] pub const PARTITION_SIZE: usize = 0x40000; #[cfg(feature = "stm32h723")] pub const BOOT_PARTITION_ADDRESS: usize = 0x08020000; #[cfg(feature = "stm32h723")] pub const SWAP_PARTITION_ADDRESS: usize = 0x080A0000; #[cfg(feature = "stm32h723")] pub const UPDATE_PARTITION_ADDRESS: usize = 0x08060000; }
RUSTBOOT partition:
contains the bootloader (its code and data) and a (test) public-key embedded as part of the bootloader image, starts at address0x0
.BOOT partition:
contains boot firmware, starts at addressPARTITION_BOOT_ADDRESS
.UPDATE partition:
contains update firmware, starts at addressUPDATE_PARTITION_ADDRESS
. The boot firmware is responsible for downloading and installing the update firmware into this partition via a secure channel.SWAP partition:
is the temporary swap space, starts at addressSWAP_PARTITION_ADDRESS
.
Compiling, Signing and Programming:
Now that we have properly partitioned the stm32h723
's on-board flash-memory, the next step is - compiling, signing and programming
We will compile the following
- bootloader
- boot and update firmware
sign both pieces of firmware with a (test) private-key and finally create valid rustBoot mcu-images
i.e. signed boot and update firmware images.
Note:
- the
ecc256.der
file contains a public-key and a private-key, the first 64 bytes being the public-key and remaining 32 bytes make up the private-key.- This is a test key file and is to be used for testing purposes only.
Compiling, signing and programming can be performed via a single command
cargo stm32h723 build-sign-flash rustBoot [boot-ver] [updt-ver]
Note:
- The
updt-ver
number should be greater thanboot-ver
.
This will build, sign and flash all 3 packages (i.e. bootloader + boot-fw + update-fw) onto the board.
Note:
- The corresponding public-key is embedded in the bootloader's source.
- In order to test this example, you'll have to install a couple of pre-requisites - probe-rs-cli, cargo-binutils and cargo-flash.
$ cargo install probe-rs-cli
$ cargo install cargo-binutils
$ cargo install cargo-flash
Here's the command line output that should be produced.
$ cargo stm32h723 build-sign-flash rustBoot 1234 1235
Finished dev [unoptimized + debuginfo] target(s) in 0.10s
Running `target/debug/xtask stm32h723 build-sign-flash rustBoot 1234 1235`
$ cargo build --release
Compiling version_check v0.9.4
Compiling typenum v1.15.0
...
...
Compiling rustBoot-update v0.1.0 (/Users/imrankhaleelsab/Imran/Boschspace/RB_workspace/rustBoot-mcusigner/rustBoot/boards/update)
Finished release [optimized] target(s) in 53.55s
$ cargo build --release
Compiling stm32h723_updtfw v0.1.0 (/Users/imrankhaleelsab/Imran/Boschspace/RB_workspace/rustBoot-mcusigner/rustBoot/boards/firmware/stm32h723/updt_fw_blinky_red)
Finished release [optimized] target(s) in 1.54s
$ cargo build --release
Compiling stm32h723 v0.1.0 (/Users/imrankhaleelsab/Imran/Boschspace/RB_workspace/rustBoot-mcusigner/rustBoot/boards/bootloaders/stm32h723)
Finished release [optimized] target(s) in 2.25s
$ rust-objcopy -I elf32-littlearm ../../target/thumbv7em-none-eabihf/release/stm32h723_bootfw -O binary stm32h723_bootfw.bin
$ rust-objcopy -I elf32-littlearm ../../target/thumbv7em-none-eabihf/release/stm32h723_updtfw -O binary stm32h723_updtfw.bin
$ cargo run mcu-image ../boards/rbSigner/signed_images/stm32h723_bootfw.bin nistp256 ../boards/rbSigner/keygen/ecc256.der 1234
Compiling rbsigner v0.1.0 (/Users/imrankhaleelsab/Imran/Boschspace/RB_workspace/rustBoot-mcusigner/rustBoot/rbsigner)
Finished dev [unoptimized + debuginfo] target(s) in 0.59s
Running `/Users/imrankhaleelsab/Imran/Boschspace/RB_workspace/rustBoot-mcusigner/rustBoot/target/debug/rbsigner mcu-image ../boards/rbSigner/signed_images/stm32h723_bootfw.bin nistp256 ../boards/rbSigner/keygen/ecc256.der 1234`
Update type :Firmware
Curve type :nistp256
Input image :stm32h723_bootfw.bin
Public key :ecc256.der
Image version :1234
Output image :stm32h723_bootfw_v1234_signed.bin
Calculating sha256 digest...
Signing the firmware...
Done.
Output image successfully created with 4608 bytes.
$ cargo run mcu-image ../boards/rbSigner/signed_images/stm32h723_updtfw.bin nistp256 ../boards/rbSigner/keygen/ecc256.der 1235
Finished dev [unoptimized + debuginfo] target(s) in 0.05s
Running `/Users/imrankhaleelsab/Imran/Boschspace/RB_workspace/rustBoot-mcusigner/rustBoot/target/debug/rbsigner mcu-image ../boards/rbSigner/signed_images/stm32h723_updtfw.bin nistp256 ../boards/rbSigner/keygen/ecc256.der 1235`
Update type :Firmware
Curve type :nistp256
Input image :stm32h723_updtfw.bin
Public key :ecc256.der
Image version :1235
Output image :stm32h723_updtfw_v1235_signed.bin
Calculating sha256 digest...
Signing the firmware...
Done.
Output image successfully created with 4624 bytes.
$ probe-rs-cli erase --chip stm32h723ZGTx
$ probe-rs-cli download --format Bin --base-address 0x8020000 --chip STM32H723ZGTx stm32h723_bootfw_v1234_signed.bin
Erasing sectors ✔ [00:00:02] [############################] 128.00KiB/128.00KiB @ 63.21KiB/s (eta 0s )
Programming pages ✔ [00:00:00] [##############################] 5.00KiB/ 5.00KiB @ 1.05KiB/s (eta 0s )
Finished in 2.165s
$ probe-rs-cli download --format Bin --base-address 0x8060000 --chip STM32H723ZGTx stm32h723_updtfw_v1235_signed.bin
Erasing sectors ✔ [00:00:01] [############################] 128.00KiB/128.00KiB @ 64.68KiB/s (eta 0s )
Programming pages ✔ [00:00:00] [##############################] 5.00KiB/ 5.00KiB @ 1.08KiB/s (eta 0s )
Finished in 2.117s
$ cargo flash --chip stm32h723ZGTx --release
Finished release [optimized] target(s) in 0.08s
Flashing /Users/imrankhaleelsab/Imran/Boschspace/RB_workspace/rustBoot-mcusigner/rustBoot/boards/target/thumbv7em-none-eabihf/release/stm32h723
WARN probe_rs::config::target > Using custom sequence for STM32H7
Erasing sectors ✔ [00:00:01] [############################] 128.00KiB/128.00KiB @ 66.35KiB/s (eta 0s )
Programming pages ✔ [00:00:00] [##############################] 44.00KiB/44.00KiB @ 13.45KiB/s (eta 0s )
Finished in 2.878s
Verifying:
blinky leds
are used to confirm that rustBoot works as expected. Here's the flow
- Upon supplying power to the board, rustBoot takes over
- validates the firmware image stored in the BOOT partition
- verifies the signature attached against a known public key stored in the rustBoot image.
- If the signature checks out, rustBoot boots into the bootfw and blinks a
green-led
for a few seconds,- post which, the boot firmware triggers the update and performs a system reset.
- Upon reset, the rustBoot again takes over
- validates the firmware image stored in the UPDATE partition
- swaps the contents of the BOOT and the UPDATE partitions
- marks the new firmware in the BOOT partition as in state STATE_TESTING
- boots into the UPDATE'd firmware
- Now that execution-control has been transferred to the UPDATE'd firmware
- it will attempt to blink a
red-led
- and set a
confirmation flag
to indicate that the update was successful. - post which, it continuously blinks a
red-led
.
- it will attempt to blink a