Skip to content

Commit af0d388

Browse files
authored
Merge branch 'main' into feature/state-fuzz
2 parents 8a382c4 + 2b49e18 commit af0d388

File tree

2 files changed

+71
-37
lines changed

2 files changed

+71
-37
lines changed

README.md

Lines changed: 58 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,79 @@
1-
# ec-slimloader **OUTDATED**
1+
# ec-slimloader
22

3-
A light-weight stage-two bootloader written in Rust for loading an app image as configured by ec-slimloader-descriptors. Also contains a tool for signing images, flashing them to the device, setting fuses (or shadow registers) containing crypto keys, and an example application to showcase the bootloaders A/B state functionality.
3+
A light-weight bootloader written in Rust with a fail-safe NOR-flash backed state journal.
4+
This framework can run on any platform if support for the platform is implemented.
5+
It is only opinionated with regards to how the state is stored.
46

5-
Currently this bootloader can only be used on the IMXRT600 series of chipsets from NXP.
7+
Currently only supports the NXP IMXRT685S and IMXRT633S where it acts as a stage-two bootloader and copies the program to application RAM.
8+
Also contains a tool for signing images, flashing them to the device, setting fuses (or shadow registers) containing crypto keys,
9+
and an example application to showcase the bootloaders A/B state functionality for this family of chipsets.
610

711
## Organisation
812

9-
This repository is split up into four parts:
10-
* ec-slimloader: the binary project which forms the second stage bootloader
11-
* ec-slimloader-descriptors: the library crate containing a descriptor of where each image slot exists, as well as a persistent fail-safe state journal for recording the A/B bootloading state.
12-
* bootloader-tool: a command-line utility using the NXP SPSDK tooling to generate keys, sign images, and flash them to the target device. Also integrates probe-rs and allows for attaching to the RTT buffer for displaying `defmt` output.
13-
* example: an example application image that uses the state-journal to select alternative images to execute.
13+
This repository is split up into three parts:
14+
* libs: library crates that implement the core functionality.
15+
* examples/rt685s: example binary applications that be run on the RT685S evaluation kit.
16+
* bootloader-tool: a command-line utility only used to perform operations related to the NXP RT685S platform.
17+
It uses the NXP SPSDK tooling to generate keys, sign images, and flash them to the target device. Also integrates probe-rs and allows for attaching to the RTT buffer for displaying `defmt` output.
18+
This tool is not relevant if you want to use `ec-slimloader` with any other platform.
1419

15-
## Memory layout
16-
This repository has default configuration files detailing the used memory layout. This layout will probably will need to be adapted for your specific usecase.
20+
The libraries are split out as follows:
21+
* ec-slimloader: general library crate providing a basic structure to build your bootloader binary application.
22+
* ec-slimloader-state: library crate with all code relating to managing the state journal. Used by both the bootloader and the application to change which image slot should be booted.
23+
* ec-slimloader-imxrt: library crate implementing support for the NXP IMXRT685S and IMXRT633S.
24+
* imxrt-rom: library crate implementing Rust support for the NXP ROM API which provides access to fuses and allows calling into a verification routine for images.
25+
26+
## How it works
27+
Assuming your platform is already supported, you can define:
28+
* a region of NOR-flash memory containing at least 2 pages for the bootloader state.
29+
* at least two regions of any memory that will fit an application image.
30+
31+
Using the library crate for your platform (like `ec-slimloader-imxrt`) you can then implement your own bootloader binary by calling the `start` function in the `ec-slimloader` library crate.
32+
33+
The `ec-slimloader` crate will handle for you:
34+
* it will read from the state journal what image slot will be booted.
35+
* on subsequent reboots, it will fall back to your defined backup slot if you do not mark your current application image as `confirmed`.
36+
37+
However, some aspects are handled by the platform support crate (and can differ from project-to-project):
38+
* how application images are loaded. For `ec-slimloader-imxrt` images are copied to RAM in a quite chip-specific way. Typically for other platforms you might want to swap images between on-chip NOR flash and external NOR flash. The latter method is not implemented in this repository (yet).
39+
* how application images are verified. By default the images themselves are not checked at all. `ec-slimloader-imxrt` leverages the native NXP authentication routines to check image integrity.
40+
* how application images are bootloaded, or in other words are jumped to. This differs for cortex-m or RISCV processors.
41+
42+
Even when using `ec-slimloader-imxrt`, you will still have to implement a few details:
43+
* from what memory is the `ec-slimloader` started, and what memory range is used for the bootloader data?
44+
* which memory regions are mapped to be state journal and mapped to be a image slot 0, 1, etc.?
45+
* what is a valid memory range for the application?
46+
47+
Finally, your application needs to also work with the state journal to:
48+
* after writing a new application image to a slot, marking that image slot as to be booted in the state journal.
49+
* after rebooting, mark the current image slot from which the application is running as `confirmed`.
50+
If the application does not do this, the bootloader will load the old 'backup' image and mark the current boot as `failed`.
51+
52+
For a full tour on how to use this framework, please refer to the `examples/rt685s` folder.
1753

1854
## Quick guide
1955
This guide details how to use this repository on the NXP MIMXRT685S-EVK. First step is compiling the bootloader and application:
2056

2157
```bash
22-
pushd ec-slimloader
58+
pushd examples/rt685s
2359
cargo build --release --features defmt
2460
popd
25-
pushd examples/rt685s-application
26-
cargo build --release
27-
popd
2861
```
2962

3063
In general, the bootloader-tool is a `clap` supported CLI application with for each subcommand a full `--help`:
3164
```
65+
popd bootloader-tool
3266
cargo run -- --help
33-
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.10s
67+
Compiling bootloader-tool v0.1.0 (ec-slimloader/bootloader-tool)
68+
Finished `dev` profile [unoptimized + debuginfo] target(s) in 1.22s
3469
Running `target/debug/bootloader-tool --help`
3570
Usage: bootloader-tool [OPTIONS] [COMMAND]
3671
3772
Commands:
3873
generate Generate keys and certificates
3974
sign Sign binaries for flashing or OTA
4075
download Download binaries to the device
41-
run Run binaries, setting the shadow registers, by going through the bootloader chain for testing purposes
76+
run Run binaries by going through the bootloader chain for testing purposes
4277
fuse Burn fuse registers with key material and settings
4378
help Print this message or the help of the given subcommand(s)
4479
@@ -50,41 +85,39 @@ Options:
5085

5186
For now we need to prepare our testing setup by generating the key material:
5287
```bash
53-
cd bootloader-tool
5488
cargo run -- generate certificates
5589
cargo run -- generate otp
5690
```
5791

5892
This key material is only used for testing right now, and everything is put in the `./artifacts` directory. This can be configured in the `./config.toml` file.
59-
We are working on a setup to also support external HSM integration.
6093

6194
Now we have everything ready to start flashing.
6295
We can use run `run` command to immediately flash and `attach` in the same way you are familiar with from `probe-rs`. However, we need the bootloader to start up the application, and we need the FCB (we call everything in 0x0 to 0x1000 the 'prelude') to start the bootloader. We can extract the FCB from the `ec-slimloader` as it is built with the appropriate feature flags to include a FCB in the ELF file. Extraction happens as a side-product of signing:
6396

6497
```bash
65-
cargo run -- sign bootloader -i ../target/thumbv8m.main-none-eabihf/release/ec-slimloader
98+
cargo run -- sign bootloader -i ../examples/rt685s/target/thumbv8m.main-none-eabihf/release/example-bootloader
6699
```
67100

68101
We can now flash the FCB:
69102

70103
```bash
71-
cargo run -- download prelude --prelude-path ../target/thumbv8m.main-none-eabihf/release/ec-slimloader.prelude.elf
104+
cargo run -- download prelude --prelude-path ../examples/rt685s/target/thumbv8m.main-none-eabihf/release/example-bootloader/ec-slimloader.prelude.elf
72105
```
73106

74107
And we can flash the application into *both slots*:
75108
```bash
76-
cargo run -- download application -i ../examples/rt685s-application/target/thumbv8m.main-none-eabihf/release/example-application --slot 0
77-
cargo run -- download application -i ../examples/rt685s-application/target/thumbv8m.main-none-eabihf/release/example-application --slot 1
109+
cargo run -- download application -i ../examples/rt685s/target/thumbv8m.main-none-eabihf/release/example-application --slot 0 --certificate 0
110+
cargo run -- download application -i ../examples/rt685s/target/thumbv8m.main-none-eabihf/release/example-application --slot 1 --certificate 0
78111
```
79112

80113
To flash & attach to the bootloader now run, whilst setting the OTP shadow registers:
81114
```bash
82-
cargo run -- download bootloader -i ../target/thumbv8m.main-none-eabihf/release/ec-slimloader
115+
cargo run -- download bootloader -i ../examples/rt685s/target/thumbv8m.main-none-eabihf/release/example-bootloader
83116
```
84117

85-
To flash & attach to the application (TODO it now is not resetting the state journal so take care), assuming you have a and FCB bootloader already flashed:
118+
To flash & attach to the application, assuming you have a bootloader and FCB already flashed:
86119
```bash
87-
cargo run -- run application -i ../examples/rt685s-application/target/thumbv8m.main-none-eabihf/release/example-application
120+
cargo run -- run application -i ../examples/rt685s/target/thumbv8m.main-none-eabihf/release/example-application
88121
```
89122

90123
You can use the `USER_1` button to change the state journal to either `confirmed` or try the other slot in state `initial` if the current image is already `confirmed`.

bootloader-tool/README.md

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
This binary provides support for signing, downloading and running bootloaders via the secure pathway on the NXP RT6xx family of chips. It automates the signing process, as well as setting up the chip temporarily for running signed binaries. Note: It does not blow any fuses, unless using the `fuse` command, and any boot configuration changes made are erased on a power cycle.
44

5-
**Note**: the RT6xx chipset enters a FAULT condition when image verification fails. You might need to periodically powercycle your board until the complete chain runs successfully. (TODO why?)
5+
**Note**: the RT6xx chipset enters a FAULT condition when image verification fails in the ROM bootloader. You might need to periodically powercycle your board until the complete chain runs successfully.
66

77
## Installation
88

@@ -35,13 +35,13 @@ cargo run -- generate otp
3535

3636
Then, assuming you have a bootloader and application ready (see the example folder to quickly build something that runs on the RT685S EVK), you can use the following to flash an application to slot 0:
3737

38-
```
39-
cargo run -- run application --input-path ./example/application/target/thumbv8m.main-none-eabihf/release/example-application
38+
```bash
39+
cargo run -- run application --input-path ../examples/rt685s/target/thumbv8m.main-none-eabihf/release/example-application
4040
```
4141

4242
And then flash the bootloader to test that it works:
43-
```
44-
cargo run -- run bootloader --input-path ./example/bootloader/target/thumbv8m.main-none-eabihf/release/example-bootloader
43+
```bash
44+
cargo run -- run bootloader --input-path ../examples/rt685s/target/thumbv8m.main-none-eabihf/release/example-bootloader
4545
```
4646

4747
**Note**: initially flashing the application causes the target to lock up, and you might need to powercycle before
@@ -52,7 +52,7 @@ running the bootloader.
5252
```bash
5353
# Copy in your image
5454
mkdir sign_me
55-
cp example/bootloader/target/thumbv8m.main-none-eabihf/release/example-bootloader sign_me/
55+
cp ../examples/rt685s/target/thumbv8m.main-none-eabihf/release/example-bootloader sign_me/
5656

5757
# Prepare image for signing
5858
cargo run -- sign bootloader --input-path sign_me/example-bootloader --dont-sign
@@ -75,12 +75,13 @@ This tool takes an input ELF image and:
7575
1. extracts all relevant sections from the given ELF
7676
2. performs checks, such as that the sections are consecutive, not too large, and that the vector table exists on the expected memory address
7777
3. converts the ELF sections into a consecutive binary image
78-
4. calls the SPSDK tooling to package the image as a Master Boot Image in signed and encrypted mode, set to be loaded into RAM
79-
5. checks the integrity of this image
80-
6. (optionally) loads the relevant shadow registers for RTKH (certificate hashes) and OTP (decryption)
81-
7. (optionally) uploads the signed binary to external NOR flash on the address that the 1st stage ROM bootloader
82-
8. (optionally TODO) loads the other sections required like the FCB into external NOR flash
83-
9. (optionally) resets the target device, causing the 2nd stage bootloader to be executed
78+
4. calls the SPSDK tooling to generate a certificate block
79+
5. packages the image as a Master Boot Image in signed and encrypted mode, set to be loaded into RAM (XIP mode but in RAM range)
80+
6. checks the integrity of this image
81+
7. (optionally) loads the relevant shadow registers for RTKH (certificate hashes) and OTP (decryption)
82+
8. (optionally) uploads the signed binary to external NOR flash on the address that the 1st stage ROM bootloader
83+
9. (optionally TODO) loads the other sections required like the FCB into external NOR flash
84+
10. (optionally) resets the target device, causing the 2nd stage bootloader to be executed
8485

8586
### Other sections
8687
The following sections need to also be set (TODO support) before the bootloader can be run:

0 commit comments

Comments
 (0)