2018-01-23 01:02:46 +08:00
|
|
|
# Building and using MCUboot with Zephyr
|
2017-02-03 00:17:02 +08:00
|
|
|
|
2018-01-23 01:02:46 +08:00
|
|
|
MCUboot began its life as the bootloader for Mynewt. It has since
|
2017-09-02 02:26:02 +08:00
|
|
|
acquired the ability to be used as a bootloader for Zephyr as well.
|
2017-02-03 00:17:02 +08:00
|
|
|
There are some pretty significant differences in how apps are built
|
|
|
|
for Zephyr, and these are documented here.
|
|
|
|
|
2018-02-13 01:31:32 +08:00
|
|
|
Please see the [design document](design.md) for documentation on the design
|
|
|
|
and operation of the bootloader itself. This functionality should be the same
|
|
|
|
on all supported RTOSs.
|
2017-09-02 02:26:02 +08:00
|
|
|
|
|
|
|
The first step required for Zephyr is making sure your board has flash
|
|
|
|
partitions defined in its device tree. These partitions are:
|
|
|
|
|
2018-01-23 01:02:46 +08:00
|
|
|
- `boot_partition`: for MCUboot itself
|
2019-06-17 22:01:43 +08:00
|
|
|
- `image_0_primary_partition`: the primary slot of Image 0
|
|
|
|
- `image_0_secondary_partition`: the secondary slot of Image 0
|
2018-01-23 01:02:46 +08:00
|
|
|
- `scratch_partition`: the scratch slot
|
2017-09-02 02:26:02 +08:00
|
|
|
|
|
|
|
Currently, the two image slots must be contiguous. If you are running
|
2018-01-23 01:02:46 +08:00
|
|
|
MCUboot as your stage 1 bootloader, `boot_partition` must be configured
|
2019-06-17 22:01:43 +08:00
|
|
|
so your SoC runs it out of reset. If there are multiple updateable images
|
|
|
|
then the corresponding primary and secondary partitions must be defined for
|
|
|
|
the rest of the images too (e.g. `image_1_primary_partition` and
|
|
|
|
`image_1_secondary_partition` for Image 1).
|
2017-09-02 02:26:02 +08:00
|
|
|
|
2018-01-23 01:02:46 +08:00
|
|
|
The flash partitions are typically defined in the Zephyr boards folder, in a
|
|
|
|
file named `boards/<arch>/<board>/<board>.dts`. An example `.dts` file with
|
|
|
|
flash partitions defined is the frdm_k64f's in
|
|
|
|
`boards/arm/frdm_k64f/frdm_k64f.dts`. Make sure the labels in your board's
|
|
|
|
`.dts` file match the ones used there.
|
2017-02-03 00:17:02 +08:00
|
|
|
|
2018-08-08 03:31:33 +08:00
|
|
|
## Installing Requirements and Dependencies
|
|
|
|
|
|
|
|
Install additional packages required for development with mcuboot:
|
|
|
|
|
|
|
|
```
|
|
|
|
cd ~/mcuboot # or to your directory where mcuboot is cloned
|
|
|
|
pip3 install --user -r scripts/requirements.txt
|
|
|
|
```
|
|
|
|
|
2018-01-23 01:02:46 +08:00
|
|
|
## Building the bootloader itself
|
2017-02-03 00:17:02 +08:00
|
|
|
|
|
|
|
The bootloader is an ordinary Zephyr application, at least from
|
|
|
|
Zephyr's point of view. There is a bit of configuration that needs to
|
2017-11-14 08:43:46 +08:00
|
|
|
be made before building it. Most of this can be done as documented in
|
2018-01-23 01:02:46 +08:00
|
|
|
the `CMakeLists.txt` file in boot/zephyr. There are comments there for
|
2017-02-03 00:17:02 +08:00
|
|
|
guidance. It is important to select a signature algorithm, and decide
|
2019-02-18 18:50:22 +08:00
|
|
|
if the primary slot should be validated on every boot.
|
2017-02-03 00:17:02 +08:00
|
|
|
|
2018-01-23 01:02:46 +08:00
|
|
|
To build MCUboot, create a build directory in boot/zephyr, and build
|
2017-11-14 08:43:46 +08:00
|
|
|
it as usual:
|
|
|
|
|
2018-01-23 01:02:46 +08:00
|
|
|
```
|
|
|
|
cd boot/zephyr
|
|
|
|
mkdir build && cd build
|
|
|
|
cmake -GNinja -DBOARD=<board> ..
|
|
|
|
ninja
|
|
|
|
```
|
2017-09-02 00:52:56 +08:00
|
|
|
|
2017-09-02 02:26:02 +08:00
|
|
|
In addition to the partitions defined in DTS, some additional
|
|
|
|
information about the flash layout is currently required to build
|
2018-01-23 01:02:46 +08:00
|
|
|
MCUboot itself. All the needed configuration is collected in
|
|
|
|
`boot/zephyr/include/target.h`. Depending on the board, this information
|
2017-09-02 02:26:02 +08:00
|
|
|
may come from board-specific headers, Device Tree, or be configured by
|
2018-01-23 01:02:46 +08:00
|
|
|
MCUboot on a per-SoC family basis.
|
2017-02-03 00:17:02 +08:00
|
|
|
|
2017-09-02 02:26:02 +08:00
|
|
|
After building the bootloader, the binaries should reside in
|
2018-01-23 01:02:46 +08:00
|
|
|
`build/zephyr/zephyr.{bin,hex,elf}`, where `build` is the build
|
|
|
|
directory you chose when running `cmake`. Use the Zephyr build
|
|
|
|
system `flash` target to flash these binaries, usually by running
|
2018-04-03 23:10:18 +08:00
|
|
|
`make flash` (or `ninja flash`, etc.) from the build directory. Depending
|
|
|
|
on the target and flash tool used, this might erase the whole of the flash
|
|
|
|
memory (mass erase) or only the sectors where the boot loader resides prior to
|
|
|
|
programming the bootloader image itself.
|
2017-02-03 00:17:02 +08:00
|
|
|
|
2018-01-23 01:02:46 +08:00
|
|
|
## Building Applications for the bootloader
|
2017-02-03 00:17:02 +08:00
|
|
|
|
2017-09-02 02:26:02 +08:00
|
|
|
In addition to flash partitions in DTS, some additional configuration
|
2018-01-23 01:02:46 +08:00
|
|
|
is required to build applications for MCUboot.
|
2017-09-02 02:26:02 +08:00
|
|
|
|
2018-03-26 23:55:40 +08:00
|
|
|
This is handled internally by the Zephyr configuration system and is wrapped
|
|
|
|
in the `CONFIG_BOOTLOADER_MCUBOOT` Kconfig variable, which must be enabled in
|
|
|
|
the application's `prj.conf` file.
|
|
|
|
|
2018-01-23 01:02:46 +08:00
|
|
|
The directory `samples/zephyr/hello-world` in the MCUboot tree contains
|
2017-09-02 02:26:02 +08:00
|
|
|
a simple application with everything you need. You can try it on your
|
|
|
|
board and then just make a copy of it to get started on your own
|
|
|
|
application; see samples/zephyr/README.md for a tutorial.
|
|
|
|
|
2018-03-26 23:55:40 +08:00
|
|
|
The Zephyr `CONFIG_BOOTLOADER_MCUBOOT` configuration option
|
|
|
|
[documentation](http://docs.zephyrproject.org/reference/kconfig/CONFIG_BOOTLOADER_MCUBOOT.html)
|
|
|
|
provides additional details regarding the changes it makes to the image
|
|
|
|
placement and generation in order for an application to be bootable by
|
|
|
|
MCUboot.
|
2017-02-03 00:17:02 +08:00
|
|
|
|
|
|
|
With this, build the application as your normally would.
|
|
|
|
|
2018-01-23 01:02:46 +08:00
|
|
|
### Signing the application
|
2017-02-03 00:17:02 +08:00
|
|
|
|
|
|
|
In order to upgrade to an image (or even boot it, if
|
2019-02-18 18:50:22 +08:00
|
|
|
`MCUBOOT_VALIDATE_PRIMARY_SLOT` is enabled), the images must be signed.
|
2018-01-23 01:02:46 +08:00
|
|
|
To make development easier, MCUboot is distributed with some example
|
2017-02-03 00:17:02 +08:00
|
|
|
keys. It is important to stress that these should never be used for
|
2017-09-02 02:26:02 +08:00
|
|
|
production, since the private key is publicly available in this
|
2017-02-03 00:17:02 +08:00
|
|
|
repository. See below on how to make your own signatures.
|
|
|
|
|
2018-04-06 04:38:08 +08:00
|
|
|
Images can be signed with the `scripts/imgtool.py` script. It is best
|
|
|
|
to look at `samples/zephyr/Makefile` for examples on how to use this.
|
2017-02-03 00:17:02 +08:00
|
|
|
|
2018-01-23 01:02:46 +08:00
|
|
|
### Flashing the application
|
2017-02-03 00:17:02 +08:00
|
|
|
|
|
|
|
The application itself can flashed with regular flash tools, but will
|
2019-02-18 18:50:22 +08:00
|
|
|
need to be programmed at the offset of the primary slot for this particular
|
|
|
|
target. Depending on the platform and flash tool you might need to manually
|
|
|
|
specify a flash offset corresponding to the primary slot starting address. This
|
|
|
|
is usually not relevant for flash tools that use Intel Hex images (.hex) instead
|
|
|
|
of raw binary images (.bin) since the former include destination address
|
|
|
|
information. Additionally you will need to make sure that the flash tool does
|
|
|
|
not perform a mass erase (erasing the whole of the flash) or else you would be
|
|
|
|
deleting MCUboot.
|
|
|
|
These images can also be marked for upgrade, and loaded into the secondary slot,
|
2017-02-03 00:17:02 +08:00
|
|
|
at which point the bootloader should perform an upgrade. It is up to
|
2019-02-18 18:50:22 +08:00
|
|
|
the image to mark the primary slot as "image ok" before the next reboot,
|
2017-02-03 00:17:02 +08:00
|
|
|
otherwise the bootloader will revert the application.
|
|
|
|
|
2018-01-23 01:02:46 +08:00
|
|
|
## Managing signing keys
|
2017-02-03 00:17:02 +08:00
|
|
|
|
2018-01-23 01:02:46 +08:00
|
|
|
The signing keys used by MCUboot are represented in standard formats,
|
2017-02-03 00:17:02 +08:00
|
|
|
and can be generated and processed using conventional tools. However,
|
2018-04-06 04:38:08 +08:00
|
|
|
`scripts/imgtool.py` is able to generate key pairs in all of the
|
|
|
|
supported formats. See [the docs](imgtool.md) for more details on
|
|
|
|
this tool.
|
2017-02-03 00:17:02 +08:00
|
|
|
|
2018-01-23 01:02:46 +08:00
|
|
|
### Generating a new keypair
|
2017-02-03 00:17:02 +08:00
|
|
|
|
|
|
|
Generating a keypair with imgtool is a matter of running the keygen
|
2018-01-23 01:02:46 +08:00
|
|
|
subcommand:
|
2017-02-03 00:17:02 +08:00
|
|
|
|
2018-01-23 01:02:46 +08:00
|
|
|
```
|
2018-04-06 04:38:08 +08:00
|
|
|
$ ./scripts/imgtool.py keygen -k mykey.pem -t rsa-2048
|
2018-01-23 01:02:46 +08:00
|
|
|
```
|
2017-02-03 00:17:02 +08:00
|
|
|
|
2018-01-23 01:02:46 +08:00
|
|
|
The argument to `-t` should be the desired key type. See the
|
2018-04-06 04:38:08 +08:00
|
|
|
[the docs](imgtool.md) for more details on the possible key types.
|
2017-02-03 00:17:02 +08:00
|
|
|
|
2018-01-23 01:02:46 +08:00
|
|
|
### Extracting the public key
|
2017-02-03 00:17:02 +08:00
|
|
|
|
|
|
|
The generated keypair above contains both the public and the private
|
|
|
|
key. It is necessary to extract the public key and insert it into the
|
2018-01-23 01:02:46 +08:00
|
|
|
bootloader. The keys live in `boot/zephyr/keys.c`, and can be
|
|
|
|
extracted using imgtool:
|
2017-02-03 00:17:02 +08:00
|
|
|
|
2018-01-23 01:02:46 +08:00
|
|
|
```
|
2018-04-06 04:38:08 +08:00
|
|
|
$ ./scripts/imgtool.py getpub -k mykey.pem
|
2018-01-23 01:02:46 +08:00
|
|
|
```
|
2017-02-03 00:17:02 +08:00
|
|
|
|
|
|
|
This will output the public key as a C array that can be dropped
|
2018-01-23 01:02:46 +08:00
|
|
|
directly into the `keys.c` file.
|
2017-02-03 00:17:02 +08:00
|
|
|
|
2018-01-23 01:02:46 +08:00
|
|
|
Once this is done, this new keypair file (`mykey.pem` in this
|
2017-02-03 00:17:02 +08:00
|
|
|
example) can be used to sign images.
|