blob: 0cacd37a507545ee650dd4bc1a01ca8a4e317883 [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
30## Building the bootloader itself
31
32The bootloader is an ordinary Zephyr application, at least from
33Zephyr's point of view. There is a bit of configuration that needs to
34be made before building it. Most of this can be done as documented in
35the `CMakeLists.txt` file in boot/zephyr. There are comments there for
36guidance. It is important to select a signature algorithm, and decide
37if slot0 should be validated on every boot.
38
39To build MCUboot, create a build directory in boot/zephyr, and build
40it as usual:
41
42```
43 cd boot/zephyr
44 mkdir build && cd build
45 cmake -GNinja -DBOARD=<board> ..
46 ninja
47```
48
49In addition to the partitions defined in DTS, some additional
50information about the flash layout is currently required to build
51MCUboot itself. All the needed configuration is collected in
52`boot/zephyr/include/target.h`. Depending on the board, this information
53may come from board-specific headers, Device Tree, or be configured by
54MCUboot on a per-SoC family basis.
55
56After building the bootloader, the binaries should reside in
57`build/zephyr/zephyr.{bin,hex,elf}`, where `build` is the build
58directory you chose when running `cmake`. Use the Zephyr build
59system `flash` target to flash these binaries, usually by running
60`make flash` (or `ninja flash`, etc.) from the build directory.
61
62## Building Applications for the bootloader
63
64In addition to flash partitions in DTS, some additional configuration
65is required to build applications for MCUboot.
66
Carles Cufiefd783c2018-03-26 17:55:40 +020067This is handled internally by the Zephyr configuration system and is wrapped
68in the `CONFIG_BOOTLOADER_MCUBOOT` Kconfig variable, which must be enabled in
69the application's `prj.conf` file.
70
Carles Cufiecc34bb2018-01-22 18:02:46 +010071The directory `samples/zephyr/hello-world` in the MCUboot tree contains
72a simple application with everything you need. You can try it on your
73board and then just make a copy of it to get started on your own
74application; see samples/zephyr/README.md for a tutorial.
75
Carles Cufiefd783c2018-03-26 17:55:40 +020076The Zephyr `CONFIG_BOOTLOADER_MCUBOOT` configuration option
77[documentation](http://docs.zephyrproject.org/reference/kconfig/CONFIG_BOOTLOADER_MCUBOOT.html)
78provides additional details regarding the changes it makes to the image
79placement and generation in order for an application to be bootable by
80MCUboot.
Carles Cufiecc34bb2018-01-22 18:02:46 +010081
82With this, build the application as your normally would.
83
84### Signing the application
85
86In order to upgrade to an image (or even boot it, if
87`MCUBOOT_VALIDATE_SLOT0` is enabled), the images must be signed.
88To make development easier, MCUboot is distributed with some example
89keys. It is important to stress that these should never be used for
90production, since the private key is publicly available in this
91repository. See below on how to make your own signatures.
92
93There is a `sign.sh` script that gives some examples of how to make
94these signatures.
95
96### Flashing the application
97
98The application itself can flashed with regular flash tools, but will
99need to be loaded at the offset of SLOT-0 for this particular target.
100These images can also be marked for upgrade, and loaded into SLOT-1,
101at which point the bootloader should perform an upgrade. It is up to
102the image to mark slot-0 as "image ok" before the next reboot,
103otherwise the bootloader will revert the application.
104
105## Managing signing keys
106
107The signing keys used by MCUboot are represented in standard formats,
108and can be generated and processed using conventional tools. However,
109the Mynewt project has developed some tools to make this easier, and
110the `imgtool` directory contains a small program to use these tools,
111as well as some additional tools for generating and extracting public
112keys. If you will be using your own keys, it is recommended to build
113this tool following the directions within the directory.
114
115### Generating a new keypair
116
117Generating a keypair with imgtool is a matter of running the keygen
118subcommand:
119
120```
121 $ imgtool keygen -k mykey.pem -t rsa-2048
122```
123
124The argument to `-t` should be the desired key type. See the
125imgtool README.rst for more details on the possible key types.
126
127### Extracting the public key
128
129The generated keypair above contains both the public and the private
130key. It is necessary to extract the public key and insert it into the
131bootloader. The keys live in `boot/zephyr/keys.c`, and can be
132extracted using imgtool:
133
134```
135 $ imgtool getpub -k mykey.pem
136```
137
138This will output the public key as a C array that can be dropped
139directly into the `keys.c` file.
140
141Once this is done, this new keypair file (`mykey.pem` in this
142example) can be used to sign images.