blob: c4439face6c508c6f3f3559d97cb41efcd7752ab [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
67The directory `samples/zephyr/hello-world` in the MCUboot tree contains
68a simple application with everything you need. You can try it on your
69board and then just make a copy of it to get started on your own
70application; see samples/zephyr/README.md for a tutorial.
71
72More details:
73
74- `CONFIG_TEXT_SECTION_OFFSET` must be set to allow room for the
75 boot image header. Typically this is set in the app's prj.conf. It
76 must also be aligned to a boundary that the particular MCU requires
77 the vector table to be aligned on.
78
79- Your board must provide a DTS `zephyr,code-partition` chosen node
80 which ensures it is built and linked into the DTS `slot0_partition`. This
81 is typically achieved by creating a
82 [dts.overlay](https://github.com/runtimeco/mcuboot/blob/master/samples/zephyr/hello-world/dts.overlay)
83 file that contains the chose node description:
84
85```
86 chosen {
87 zephyr,code-partition = &slot0_partition;
88 };
89```
90
91With this, build the application as your normally would.
92
93### Signing the application
94
95In order to upgrade to an image (or even boot it, if
96`MCUBOOT_VALIDATE_SLOT0` is enabled), the images must be signed.
97To make development easier, MCUboot is distributed with some example
98keys. It is important to stress that these should never be used for
99production, since the private key is publicly available in this
100repository. See below on how to make your own signatures.
101
102There is a `sign.sh` script that gives some examples of how to make
103these signatures.
104
105### Flashing the application
106
107The application itself can flashed with regular flash tools, but will
108need to be loaded at the offset of SLOT-0 for this particular target.
109These images can also be marked for upgrade, and loaded into SLOT-1,
110at which point the bootloader should perform an upgrade. It is up to
111the image to mark slot-0 as "image ok" before the next reboot,
112otherwise the bootloader will revert the application.
113
114## Managing signing keys
115
116The signing keys used by MCUboot are represented in standard formats,
117and can be generated and processed using conventional tools. However,
118the Mynewt project has developed some tools to make this easier, and
119the `imgtool` directory contains a small program to use these tools,
120as well as some additional tools for generating and extracting public
121keys. If you will be using your own keys, it is recommended to build
122this tool following the directions within the directory.
123
124### Generating a new keypair
125
126Generating a keypair with imgtool is a matter of running the keygen
127subcommand:
128
129```
130 $ imgtool keygen -k mykey.pem -t rsa-2048
131```
132
133The argument to `-t` should be the desired key type. See the
134imgtool README.rst for more details on the possible key types.
135
136### Extracting the public key
137
138The generated keypair above contains both the public and the private
139key. It is necessary to extract the public key and insert it into the
140bootloader. The keys live in `boot/zephyr/keys.c`, and can be
141extracted using imgtool:
142
143```
144 $ imgtool getpub -k mykey.pem
145```
146
147This will output the public key as a C array that can be dropped
148directly into the `keys.c` file.
149
150Once this is done, this new keypair file (`mykey.pem` in this
151example) can be used to sign images.