blob: 26bc51939b984109c108d98f7a1849bb91c9c194 [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
David Vinczeba3bd602019-06-17 16:01:43 +020016- `image_0_primary_partition`: the primary slot of Image 0
17- `image_0_secondary_partition`: the secondary slot of Image 0
Carles Cufiecc34bb2018-01-22 18:02:46 +010018- `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
David Vinczeba3bd602019-06-17 16:01:43 +020022so your SoC runs it out of reset. If there are multiple updateable images
23then the corresponding primary and secondary partitions must be defined for
24the rest of the images too (e.g. `image_1_primary_partition` and
25`image_1_secondary_partition` for Image 1).
Carles Cufiecc34bb2018-01-22 18:02:46 +010026
27The flash partitions are typically defined in the Zephyr boards folder, in a
28file named `boards/<arch>/<board>/<board>.dts`. An example `.dts` file with
29flash partitions defined is the frdm_k64f's in
30`boards/arm/frdm_k64f/frdm_k64f.dts`. Make sure the labels in your board's
31`.dts` file match the ones used there.
32
Piotr Mienkowski8a474ff2018-08-07 21:31:33 +020033## Installing Requirements and Dependencies
34
35Install additional packages required for development with mcuboot:
36
37```
38 cd ~/mcuboot # or to your directory where mcuboot is cloned
39 pip3 install --user -r scripts/requirements.txt
40```
41
Carles Cufiecc34bb2018-01-22 18:02:46 +010042## Building the bootloader itself
43
44The bootloader is an ordinary Zephyr application, at least from
45Zephyr's point of view. There is a bit of configuration that needs to
46be made before building it. Most of this can be done as documented in
47the `CMakeLists.txt` file in boot/zephyr. There are comments there for
48guidance. It is important to select a signature algorithm, and decide
David Vincze2d736ad2019-02-18 11:50:22 +010049if the primary slot should be validated on every boot.
Carles Cufiecc34bb2018-01-22 18:02:46 +010050
51To build MCUboot, create a build directory in boot/zephyr, and build
52it as usual:
53
54```
55 cd boot/zephyr
56 mkdir build && cd build
57 cmake -GNinja -DBOARD=<board> ..
58 ninja
59```
60
61In addition to the partitions defined in DTS, some additional
62information about the flash layout is currently required to build
63MCUboot itself. All the needed configuration is collected in
64`boot/zephyr/include/target.h`. Depending on the board, this information
65may come from board-specific headers, Device Tree, or be configured by
66MCUboot on a per-SoC family basis.
67
68After building the bootloader, the binaries should reside in
69`build/zephyr/zephyr.{bin,hex,elf}`, where `build` is the build
70directory you chose when running `cmake`. Use the Zephyr build
71system `flash` target to flash these binaries, usually by running
Carles Cufi5a9688a2018-04-03 17:10:18 +020072`make flash` (or `ninja flash`, etc.) from the build directory. Depending
73on the target and flash tool used, this might erase the whole of the flash
74memory (mass erase) or only the sectors where the boot loader resides prior to
75programming the bootloader image itself.
Carles Cufiecc34bb2018-01-22 18:02:46 +010076
77## Building Applications for the bootloader
78
79In addition to flash partitions in DTS, some additional configuration
80is required to build applications for MCUboot.
81
Carles Cufiefd783c2018-03-26 17:55:40 +020082This is handled internally by the Zephyr configuration system and is wrapped
83in the `CONFIG_BOOTLOADER_MCUBOOT` Kconfig variable, which must be enabled in
84the application's `prj.conf` file.
85
Carles Cufiecc34bb2018-01-22 18:02:46 +010086The directory `samples/zephyr/hello-world` in the MCUboot tree contains
87a simple application with everything you need. You can try it on your
88board and then just make a copy of it to get started on your own
89application; see samples/zephyr/README.md for a tutorial.
90
Carles Cufiefd783c2018-03-26 17:55:40 +020091The Zephyr `CONFIG_BOOTLOADER_MCUBOOT` configuration option
92[documentation](http://docs.zephyrproject.org/reference/kconfig/CONFIG_BOOTLOADER_MCUBOOT.html)
93provides additional details regarding the changes it makes to the image
94placement and generation in order for an application to be bootable by
95MCUboot.
Carles Cufiecc34bb2018-01-22 18:02:46 +010096
97With this, build the application as your normally would.
98
99### Signing the application
100
101In order to upgrade to an image (or even boot it, if
David Vincze2d736ad2019-02-18 11:50:22 +0100102`MCUBOOT_VALIDATE_PRIMARY_SLOT` is enabled), the images must be signed.
Carles Cufiecc34bb2018-01-22 18:02:46 +0100103To make development easier, MCUboot is distributed with some example
104keys. It is important to stress that these should never be used for
105production, since the private key is publicly available in this
106repository. See below on how to make your own signatures.
107
David Brown520e31c2018-04-05 14:38:08 -0600108Images can be signed with the `scripts/imgtool.py` script. It is best
109to look at `samples/zephyr/Makefile` for examples on how to use this.
Carles Cufiecc34bb2018-01-22 18:02:46 +0100110
111### Flashing the application
112
113The application itself can flashed with regular flash tools, but will
David Vincze2d736ad2019-02-18 11:50:22 +0100114need to be programmed at the offset of the primary slot for this particular
115target. Depending on the platform and flash tool you might need to manually
116specify a flash offset corresponding to the primary slot starting address. This
117is usually not relevant for flash tools that use Intel Hex images (.hex) instead
118of raw binary images (.bin) since the former include destination address
119information. Additionally you will need to make sure that the flash tool does
120not perform a mass erase (erasing the whole of the flash) or else you would be
121deleting MCUboot.
122These images can also be marked for upgrade, and loaded into the secondary slot,
Carles Cufiecc34bb2018-01-22 18:02:46 +0100123at which point the bootloader should perform an upgrade. It is up to
David Vincze2d736ad2019-02-18 11:50:22 +0100124the image to mark the primary slot as "image ok" before the next reboot,
Carles Cufiecc34bb2018-01-22 18:02:46 +0100125otherwise the bootloader will revert the application.
126
127## Managing signing keys
128
129The signing keys used by MCUboot are represented in standard formats,
130and can be generated and processed using conventional tools. However,
David Brown520e31c2018-04-05 14:38:08 -0600131`scripts/imgtool.py` is able to generate key pairs in all of the
132supported formats. See [the docs](imgtool.md) for more details on
133this tool.
Carles Cufiecc34bb2018-01-22 18:02:46 +0100134
135### Generating a new keypair
136
137Generating a keypair with imgtool is a matter of running the keygen
138subcommand:
139
140```
David Brown520e31c2018-04-05 14:38:08 -0600141 $ ./scripts/imgtool.py keygen -k mykey.pem -t rsa-2048
Carles Cufiecc34bb2018-01-22 18:02:46 +0100142```
143
144The argument to `-t` should be the desired key type. See the
David Brown520e31c2018-04-05 14:38:08 -0600145[the docs](imgtool.md) for more details on the possible key types.
Carles Cufiecc34bb2018-01-22 18:02:46 +0100146
147### Extracting the public key
148
149The generated keypair above contains both the public and the private
150key. It is necessary to extract the public key and insert it into the
151bootloader. The keys live in `boot/zephyr/keys.c`, and can be
152extracted using imgtool:
153
154```
David Brown520e31c2018-04-05 14:38:08 -0600155 $ ./scripts/imgtool.py getpub -k mykey.pem
Carles Cufiecc34bb2018-01-22 18:02:46 +0100156```
157
158This will output the public key as a C array that can be dropped
159directly into the `keys.c` file.
160
161Once this is done, this new keypair file (`mykey.pem` in this
162example) can be used to sign images.