blob: fd072447fb0dc34503bd0958a1975ccd8f9a0a51 [file] [log] [blame] [view]
Carles Cufiecc34bb2018-01-22 18:02:46 +01001# Building and using MCUboot with Zephyr
2
3MCUboot began its life as the bootloader for Mynewt. It has since
4acquired the ability to be used as a bootloader for Zephyr as well.
5There are some pretty significant differences in how apps are built
6for Zephyr, and these are documented here.
7
Fabio Utzig4dce6aa2018-02-12 15:31:32 -02008Please see the [design document](design.md) for documentation on the design
9and operation of the bootloader itself. This functionality should be the same
10on all supported RTOSs.
Carles Cufiecc34bb2018-01-22 18:02:46 +010011
12The first step required for Zephyr is making sure your board has flash
13partitions defined in its device tree. These partitions are:
14
15- `boot_partition`: for MCUboot itself
16- `slot0_partition`: the primary image slot
17- `slot1_partition`: the secondary image slot
18- `scratch_partition`: the scratch slot
19
20Currently, the two image slots must be contiguous. If you are running
21MCUboot as your stage 1 bootloader, `boot_partition` must be configured
22so your SoC runs it out of reset.
23
24The flash partitions are typically defined in the Zephyr boards folder, in a
25file named `boards/<arch>/<board>/<board>.dts`. An example `.dts` file with
26flash partitions defined is the frdm_k64f's in
27`boards/arm/frdm_k64f/frdm_k64f.dts`. Make sure the labels in your board's
28`.dts` file match the ones used there.
29
Piotr Mienkowski8a474ff2018-08-07 21:31:33 +020030## Installing Requirements and Dependencies
31
32Install additional packages required for development with mcuboot:
33
34```
35 cd ~/mcuboot # or to your directory where mcuboot is cloned
36 pip3 install --user -r scripts/requirements.txt
37```
38
Carles Cufiecc34bb2018-01-22 18:02:46 +010039## Building the bootloader itself
40
41The bootloader is an ordinary Zephyr application, at least from
42Zephyr's point of view. There is a bit of configuration that needs to
43be made before building it. Most of this can be done as documented in
44the `CMakeLists.txt` file in boot/zephyr. There are comments there for
45guidance. It is important to select a signature algorithm, and decide
46if slot0 should be validated on every boot.
47
48To build MCUboot, create a build directory in boot/zephyr, and build
49it as usual:
50
51```
52 cd boot/zephyr
53 mkdir build && cd build
54 cmake -GNinja -DBOARD=<board> ..
55 ninja
56```
57
58In addition to the partitions defined in DTS, some additional
59information about the flash layout is currently required to build
60MCUboot itself. All the needed configuration is collected in
61`boot/zephyr/include/target.h`. Depending on the board, this information
62may come from board-specific headers, Device Tree, or be configured by
63MCUboot on a per-SoC family basis.
64
65After building the bootloader, the binaries should reside in
66`build/zephyr/zephyr.{bin,hex,elf}`, where `build` is the build
67directory you chose when running `cmake`. Use the Zephyr build
68system `flash` target to flash these binaries, usually by running
Carles Cufi5a9688a2018-04-03 17:10:18 +020069`make flash` (or `ninja flash`, etc.) from the build directory. Depending
70on the target and flash tool used, this might erase the whole of the flash
71memory (mass erase) or only the sectors where the boot loader resides prior to
72programming the bootloader image itself.
Carles Cufiecc34bb2018-01-22 18:02:46 +010073
74## Building Applications for the bootloader
75
76In addition to flash partitions in DTS, some additional configuration
77is required to build applications for MCUboot.
78
Carles Cufiefd783c2018-03-26 17:55:40 +020079This is handled internally by the Zephyr configuration system and is wrapped
80in the `CONFIG_BOOTLOADER_MCUBOOT` Kconfig variable, which must be enabled in
81the application's `prj.conf` file.
82
Carles Cufiecc34bb2018-01-22 18:02:46 +010083The directory `samples/zephyr/hello-world` in the MCUboot tree contains
84a simple application with everything you need. You can try it on your
85board and then just make a copy of it to get started on your own
86application; see samples/zephyr/README.md for a tutorial.
87
Carles Cufiefd783c2018-03-26 17:55:40 +020088The Zephyr `CONFIG_BOOTLOADER_MCUBOOT` configuration option
89[documentation](http://docs.zephyrproject.org/reference/kconfig/CONFIG_BOOTLOADER_MCUBOOT.html)
90provides additional details regarding the changes it makes to the image
91placement and generation in order for an application to be bootable by
92MCUboot.
Carles Cufiecc34bb2018-01-22 18:02:46 +010093
94With this, build the application as your normally would.
95
96### Signing the application
97
98In order to upgrade to an image (or even boot it, if
99`MCUBOOT_VALIDATE_SLOT0` is enabled), the images must be signed.
100To make development easier, MCUboot is distributed with some example
101keys. It is important to stress that these should never be used for
102production, since the private key is publicly available in this
103repository. See below on how to make your own signatures.
104
David Brown520e31c2018-04-05 14:38:08 -0600105Images can be signed with the `scripts/imgtool.py` script. It is best
106to look at `samples/zephyr/Makefile` for examples on how to use this.
Carles Cufiecc34bb2018-01-22 18:02:46 +0100107
108### Flashing the application
109
110The application itself can flashed with regular flash tools, but will
Carles Cufi5a9688a2018-04-03 17:10:18 +0200111need to be programmed at the offset of slot-0 for this particular target.
112Depending on the platform and flash tool you might need to manually specify a
113flash offset corresponding to the slot-0 starting address. This is usually
114not relevant for flash tools that use Intel Hex images (.hex) instead of raw
115binary images (.bin) since the former include destination address information.
116Additionally you will need to make sure that the flash tool does not perform
117a mass erase (erasing the whole of the flash) or else you would be deleting
118MCUboot.
119These images can also be marked for upgrade, and loaded into slot-1,
Carles Cufiecc34bb2018-01-22 18:02:46 +0100120at which point the bootloader should perform an upgrade. It is up to
121the image to mark slot-0 as "image ok" before the next reboot,
122otherwise the bootloader will revert the application.
123
124## Managing signing keys
125
126The signing keys used by MCUboot are represented in standard formats,
127and can be generated and processed using conventional tools. However,
David Brown520e31c2018-04-05 14:38:08 -0600128`scripts/imgtool.py` is able to generate key pairs in all of the
129supported formats. See [the docs](imgtool.md) for more details on
130this tool.
Carles Cufiecc34bb2018-01-22 18:02:46 +0100131
132### Generating a new keypair
133
134Generating a keypair with imgtool is a matter of running the keygen
135subcommand:
136
137```
David Brown520e31c2018-04-05 14:38:08 -0600138 $ ./scripts/imgtool.py keygen -k mykey.pem -t rsa-2048
Carles Cufiecc34bb2018-01-22 18:02:46 +0100139```
140
141The argument to `-t` should be the desired key type. See the
David Brown520e31c2018-04-05 14:38:08 -0600142[the docs](imgtool.md) for more details on the possible key types.
Carles Cufiecc34bb2018-01-22 18:02:46 +0100143
144### Extracting the public key
145
146The generated keypair above contains both the public and the private
147key. It is necessary to extract the public key and insert it into the
148bootloader. The keys live in `boot/zephyr/keys.c`, and can be
149extracted using imgtool:
150
151```
David Brown520e31c2018-04-05 14:38:08 -0600152 $ ./scripts/imgtool.py getpub -k mykey.pem
Carles Cufiecc34bb2018-01-22 18:02:46 +0100153```
154
155This will output the public key as a C array that can be dropped
156directly into the `keys.c` file.
157
158Once this is done, this new keypair file (`mykey.pem` in this
159example) can be used to sign images.