blob: 24f988ae2fb52d2a6fcf569874c6d4494509a183 [file] [log] [blame] [view]
David Brown17e20d12017-09-12 11:53:20 -06001<!--
2 Licensed to the Apache Software Foundation (ASF) under one
3 or more contributor license agreements. See the NOTICE file
4 distributed with this work for additional information
5 regarding copyright ownership. The ASF licenses this file
6 to you under the Apache License, Version 2.0 (the
7 "License"); you may not use this file except in compliance
8 with the License. You may obtain a copy of the License at
Christopher Collins92ea77f2016-12-12 15:59:26 -08009
David Brown17e20d12017-09-12 11:53:20 -060010 http://www.apache.org/licenses/LICENSE-2.0
Christopher Collins92ea77f2016-12-12 15:59:26 -080011
David Brown17e20d12017-09-12 11:53:20 -060012 Unless required by applicable law or agreed to in writing,
13 software distributed under the License is distributed on an
14 "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 KIND, either express or implied. See the License for the
16 specific language governing permissions and limitations
17 under the License.
18-->
19
20# Boot Loader
21
22## Summary
Christopher Collins92ea77f2016-12-12 15:59:26 -080023
Fabio Utzigac834962017-07-20 13:20:48 -030024mcuboot comprises two packages:
Christopher Collins92ea77f2016-12-12 15:59:26 -080025
David Brown17e20d12017-09-12 11:53:20 -060026* The bootutil library (boot/bootutil)
27* The boot application (each port has its own at boot/<port>)
Christopher Collins92ea77f2016-12-12 15:59:26 -080028
29The bootutil library performs most of the functions of a boot loader. In
30particular, the piece that is missing is the final step of actually jumping to
31the main image. This last step is instead implemented by the boot application.
32Boot loader functionality is separated in this manner to enable unit testing of
33the boot loader. A library can be unit tested, but an application can't.
34Therefore, functionality is delegated to the bootutil library when possible.
35
David Brown17e20d12017-09-12 11:53:20 -060036## Limitations
Christopher Collins92ea77f2016-12-12 15:59:26 -080037
38The boot loader currently only supports images with the following
39characteristics:
David Brown17e20d12017-09-12 11:53:20 -060040* Built to run from flash.
41* Built to run from a fixed location (i.e., not position-independent).
Christopher Collins92ea77f2016-12-12 15:59:26 -080042
David Brown17e20d12017-09-12 11:53:20 -060043## Image Format
Christopher Collins92ea77f2016-12-12 15:59:26 -080044
45The following definitions describe the image format.
46
David Brown17e20d12017-09-12 11:53:20 -060047``` c
Fabio Utzigea422c22017-09-11 11:02:47 -030048#define IMAGE_MAGIC 0x96f3b83d
Christopher Collins92ea77f2016-12-12 15:59:26 -080049
50#define IMAGE_HEADER_SIZE 32
51
52struct image_version {
53 uint8_t iv_major;
54 uint8_t iv_minor;
55 uint16_t iv_revision;
56 uint32_t iv_build_num;
57};
58
59/** Image header. All fields are in little endian byte order. */
60struct image_header {
61 uint32_t ih_magic;
Fabio Utzigea422c22017-09-11 11:02:47 -030062 uint32_t ih_load_addr;
Christopher Collins92ea77f2016-12-12 15:59:26 -080063 uint16_t ih_hdr_size; /* Size of image header (bytes). */
64 uint16_t _pad2;
65 uint32_t ih_img_size; /* Does not include header. */
Fabio Utzigea422c22017-09-11 11:02:47 -030066 uint32_t ih_flags; /* IMAGE_F_[...]. */
Christopher Collins92ea77f2016-12-12 15:59:26 -080067 struct image_version ih_ver;
68 uint32_t _pad3;
69};
70
Fabio Utzigea422c22017-09-11 11:02:47 -030071/** Image TLV header. All fields in little endian. */
72struct image_tlv_info {
73 uint16_t it_magic;
74 uint16_t it_tlv_tot; /* size of TLV area (including tlv_info header) */
75};
76
Christopher Collins92ea77f2016-12-12 15:59:26 -080077/** Image trailer TLV format. All fields in little endian. */
78struct image_tlv {
79 uint8_t it_type; /* IMAGE_TLV_[...]. */
80 uint8_t _pad;
Marti Bolivar49b29172017-08-04 14:50:51 -040081 uint16_t it_len; /* Data length (not including TLV header). */
Christopher Collins92ea77f2016-12-12 15:59:26 -080082};
83
84/*
85 * Image header flags.
86 */
Marti Bolivar7c057e92017-08-04 14:46:39 -040087#define IMAGE_F_PIC 0x00000001 /* Not supported. */
Marti Bolivar7c057e92017-08-04 14:46:39 -040088#define IMAGE_F_NON_BOOTABLE 0x00000010 /* Split image app. */
Fabio Utzigea422c22017-09-11 11:02:47 -030089#define IMAGE_F_RAM_LOAD 0x00000020
Christopher Collins92ea77f2016-12-12 15:59:26 -080090
91/*
92 * Image trailer TLV types.
93 */
Fabio Utzigea422c22017-09-11 11:02:47 -030094#define IMAGE_TLV_KEYHASH 0x01 /* hash of the public key */
David Brown27648b82017-08-31 10:40:29 -060095#define IMAGE_TLV_SHA256 0x10 /* SHA256 of image hdr and body */
Marko Kiiskila8dd56f32017-08-22 21:40:49 -070096#define IMAGE_TLV_RSA2048_PSS 0x20 /* RSA2048 of hash output */
David Brown27648b82017-08-31 10:40:29 -060097#define IMAGE_TLV_ECDSA224 0x21 /* ECDSA of hash output */
98#define IMAGE_TLV_ECDSA256 0x22 /* ECDSA of hash output */
David Brown17e20d12017-09-12 11:53:20 -060099```
Christopher Collins92ea77f2016-12-12 15:59:26 -0800100
101Optional type-length-value records (TLVs) containing image metadata are placed
102after the end of the image.
103
David Brown17e20d12017-09-12 11:53:20 -0600104The `ih_hdr_size` field indicates the length of the header, and therefore the
Christopher Collins92ea77f2016-12-12 15:59:26 -0800105offset of the image itself. This field provides for backwards compatibility in
106case of changes to the format of the image header.
107
David Brown17e20d12017-09-12 11:53:20 -0600108## Flash Map
Christopher Collins92ea77f2016-12-12 15:59:26 -0800109
Fabio Utzigac834962017-07-20 13:20:48 -0300110A device's flash is partitioned according to its _flash map_. At a high
Christopher Collins92ea77f2016-12-12 15:59:26 -0800111level, the flash map maps numeric IDs to _flash areas_. A flash area is a
112region of disk with the following properties:
David Brown17e20d12017-09-12 11:53:20 -06001131. An area can be fully erased without affecting any other areas.
1142. A write to one area does not restrict writes to other areas.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800115
Marti Bolivar4e64d562017-08-04 14:53:33 -0400116The boot loader uses the following flash area IDs:
Christopher Collins92ea77f2016-12-12 15:59:26 -0800117
David Brown17e20d12017-09-12 11:53:20 -0600118``` c
Christopher Collins92ea77f2016-12-12 15:59:26 -0800119#define FLASH_AREA_BOOTLOADER 0
120#define FLASH_AREA_IMAGE_0 1
121#define FLASH_AREA_IMAGE_1 2
122#define FLASH_AREA_IMAGE_SCRATCH 3
David Brown17e20d12017-09-12 11:53:20 -0600123```
Christopher Collins92ea77f2016-12-12 15:59:26 -0800124
Marti Bolivar4e64d562017-08-04 14:53:33 -0400125The bootloader area contains the bootloader image itself. The other areas are
126described in subsequent sections.
127
David Brown17e20d12017-09-12 11:53:20 -0600128## Image Slots
Christopher Collins92ea77f2016-12-12 15:59:26 -0800129
130A portion of the flash memory is partitioned into two image slots: a primary
131slot (0) and a secondary slot (1). The boot loader will only run an image from
132the primary slot, so images must be built such that they can run from that
133fixed location in flash. If the boot loader needs to run the image resident in
Marti Bolivara91674f2017-08-04 14:56:08 -0400134the secondary slot, it must copy its contents into the primary slot before doing
135so, either by swapping the two images or by overwriting the contents of the
136primary slot. The bootloader supports either swap- or overwrite-based image
137upgrades, but must be configured at build time to choose one of these two
138strategies.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800139
140In addition to the two image slots, the boot loader requires a scratch area to
Fabio Utziga722f5a2017-12-12 14:04:53 -0200141allow for reliable image swapping. The scratch area must have a size that is
142enough to store at least the largest sector that is going to be swapped. Many
143devices have small equally sized flash sectors, eg 4K, while others have variable
144sized sectors where the largest sectors might be 128K or 256K, so the scratch
145must be big enough to store that. The scratch is only ever used when swapping
146firmware, which means only when doing an upgrade. Given that, the main reason
147for using a larger size for the scratch is that flash wear will be more evenly
148distributed, because a single sector would be written twice the number of times
149than using two sectors, for example. To evaluate the ideal size of the scratch
150for your use case the following parameters are relevant:
151
152* the ratio of image size / scratch size
153* the number of erase cycles supported by the flash hardware
154
155The image size is used (instead of slot size) because only the slot's sectors
156that are actually used for storing the image are copied. The image/scratch ratio
157is the number of times the scratch will be erased on every upgrade. The number
158of erase cycles divided by the image/scratch ratio will give you the number of
159times an upgrade can be performed before the device goes out of spec.
160
161```
162num_upgrades = number_of_erase_cycles / (image_size / scratch_size)
163```
164
165Let's assume, for example, a device with 10000 erase cycles, an image size of
166150K and a scratch of 4K (usual minimum size of 4K sector devices). This would
167result in a total of:
168
169`10000 / (150 / 4) ~ 267`
170
171Increasing the scratch to 16K would give us:
172
173`10000 / (150 / 16) ~ 1067`
174
175There is no *best* ratio, as the right size is use-case dependent. Factors to
176consider include the number of times a device will be upgraded both in the field
177and during development, as well as any desired safety margin on the manufacturer's
178specified number of erase cycles. In general, using a ratio that allows hundreds
179to thousands of field upgrades in production is recommended.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800180
Marti Bolivara91674f2017-08-04 14:56:08 -0400181The overwrite upgrade strategy is substantially simpler to implement than the
182image swapping strategy, especially since the bootloader must work properly
183even when it is reset during the middle of an image swap. For this reason, the
184rest of the document describes its behavior when configured to swap images
185during an upgrade.
186
David Brown17e20d12017-09-12 11:53:20 -0600187## Boot Swap Types
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -0800188
Marti Bolivar048d8d82017-08-04 17:14:24 -0400189When the device first boots under normal circumstances, there is an up-to-date
190firmware image in slot 0, which mcuboot can validate and then chain-load. In
191this case, no image swaps are necessary. During device upgrades, however, new
192candidate images are present in slot 1, which mcuboot must swap into slot 0
193before booting as discussed above.
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -0800194
Marti Bolivar048d8d82017-08-04 17:14:24 -0400195Upgrading an old image with a new one by swapping can be a two-step process. In
196this process, mcuboot performs a "test" swap of image data in flash and boots
197the new image. The new image can then update the contents of flash at runtime
198to mark itself "OK", and mcuboot will then still choose to run it during the
199next boot. When this happens, the swap is made "permanent". If this doesn't
200happen, mcuboot will perform a "revert" swap during the next boot by swapping
201the images back into their original locations, and attempting to boot the old
202image.
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -0800203
Marti Bolivar048d8d82017-08-04 17:14:24 -0400204Depending on the use case, the first swap can also be made permanent directly.
205In this case, mcuboot will never attempt to revert the images on the next reset.
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -0800206
Marti Bolivar048d8d82017-08-04 17:14:24 -0400207Test swaps are supported to provide a rollback mechanism to prevent devices
208from becoming "bricked" by bad firmware. If the device crashes immediately
209upon booting a new (bad) image, mcuboot will revert to the old (working) image
210at the next device reset, rather than booting the bad image again. This allows
211device firmware to make test swaps permanent only after performing a self-test
212routine.
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -0800213
Marti Bolivar048d8d82017-08-04 17:14:24 -0400214On startup, mcuboot inspects the contents of flash to decide which of these
215"swap types" to perform; this decision determines how it proceeds.
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -0800216
Marti Bolivar048d8d82017-08-04 17:14:24 -0400217The possible swap types, and their meanings, are:
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -0800218
David Brown17e20d12017-09-12 11:53:20 -0600219- `BOOT_SWAP_TYPE_NONE`: The "usual" or "no upgrade" case; attempt to boot the
Marti Bolivar048d8d82017-08-04 17:14:24 -0400220 contents of slot 0.
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -0800221
David Brown17e20d12017-09-12 11:53:20 -0600222- `BOOT_SWAP_TYPE_TEST`: Boot the contents of slot 1 by swapping images. Unless
Marti Bolivar048d8d82017-08-04 17:14:24 -0400223 the swap is made permanent, revert back on the next boot.
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -0800224
David Brown17e20d12017-09-12 11:53:20 -0600225- `BOOT_SWAP_TYPE_PERM`: Permanently swap images, and boot the upgraded image
Marti Bolivar048d8d82017-08-04 17:14:24 -0400226 firmware.
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -0800227
David Brown17e20d12017-09-12 11:53:20 -0600228- `BOOT_SWAP_TYPE_REVERT`: A previous test swap was not made permanent; swap back
Marti Bolivar048d8d82017-08-04 17:14:24 -0400229 to the old image whose data are now in slot 1. If the old image marks itself
David Brown17e20d12017-09-12 11:53:20 -0600230 "OK" when it boots, the next boot will have swap type `BOOT_SWAP_TYPE_NONE`.
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -0800231
David Brown17e20d12017-09-12 11:53:20 -0600232- `BOOT_SWAP_TYPE_FAIL`: Swap failed because image to be run is not valid.
Marti Bolivar048d8d82017-08-04 17:14:24 -0400233
David Brown17e20d12017-09-12 11:53:20 -0600234- `BOOT_SWAP_TYPE_PANIC`: Swapping encountered an unrecoverable error.
Marti Bolivar048d8d82017-08-04 17:14:24 -0400235
236The "swap type" is a high-level representation of the outcome of the
237boot. Subsequent sections describe how mcuboot determines the swap type from
238the bit-level contents of flash.
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -0800239
David Brown17e20d12017-09-12 11:53:20 -0600240## Image Trailer
Christopher Collins92ea77f2016-12-12 15:59:26 -0800241
Fabio Utzig86fe4b22017-07-28 18:56:29 -0300242For the bootloader to be able to determine the current state and what actions
Marti Bolivar42818032017-08-04 15:45:01 -0400243should be taken during the current boot operation, it uses metadata stored in
244the image flash areas. While swapping, some of this metadata is temporarily
245copied into and out of the scratch area.
246
247This metadata is located at the end of the image flash areas, and is called an
248image trailer. An image trailer has the following structure:
Christopher Collins92ea77f2016-12-12 15:59:26 -0800249
David Brown17e20d12017-09-12 11:53:20 -0600250```
Christopher Collins92ea77f2016-12-12 15:59:26 -0800251 0 1 2 3
252 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
253 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Christopher Collins92ea77f2016-12-12 15:59:26 -0800254 ~ ~
255 ~ Swap status (128 * min-write-size * 3) ~
256 ~ ~
257 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Fabio Utzigea422c22017-09-11 11:02:47 -0300258 | Swap size |
Christopher Collins92ea77f2016-12-12 15:59:26 -0800259 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Fabio Utzigea422c22017-09-11 11:02:47 -0300260 | 0xff padding (4 octets) |
Fabio Utzig5bd4e582017-07-20 08:55:38 -0300261 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Fabio Utzigea422c22017-09-11 11:02:47 -0300262 | Copy done | 0xff padding (7 octets) ~
263 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
264 | Image OK | 0xff padding (7 octets) ~
265 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
266 ~ MAGIC (16 octets) ~
Christopher Collins92ea77f2016-12-12 15:59:26 -0800267 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
David Brown17e20d12017-09-12 11:53:20 -0600268```
Christopher Collins92ea77f2016-12-12 15:59:26 -0800269
Marti Bolivar42818032017-08-04 15:45:01 -0400270The offset immediately following such a record represents the start of the next
271flash area.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800272
273Note: "min-write-size" is a property of the flash hardware. If the hardware
274allows individual bytes to be written at arbitrary addresses, then
275min-write-size is 1. If the hardware only allows writes at even addresses,
276then min-write-size is 2, and so on.
277
Marti Bolivar1dcb6852017-08-04 15:59:32 -0400278An image trailer contains the following fields:
Christopher Collins92ea77f2016-12-12 15:59:26 -0800279
Marti Bolivar1dcb6852017-08-04 15:59:32 -04002801. Swap status: A series of records which records the progress of an image
David Brown17e20d12017-09-12 11:53:20 -0600281 swap. To swap entire images, data are swapped between the two image areas one
282 or more sectors at a time, like this:
Marti Bolivar1dcb6852017-08-04 15:59:32 -0400283
David Brown17e20d12017-09-12 11:53:20 -0600284 - sector data in slot 0 is copied into scratch, then erased
285 - sector data in slot 1 is copied into slot 0, then erased
286 - sector data in scratch is copied into slot 1
Marti Bolivar1dcb6852017-08-04 15:59:32 -0400287
288As it swaps images, the bootloader updates the swap status field in a way that
289allows it to compute how far this swap operation has progressed for each
290sector. The swap status field can thus used to resume a swap operation if the
291bootloader is halted while a swap operation is ongoing and later reset. The
292factor of 128 is the maximum number of sectors mcuboot supports for each image;
293its value is a bootloader design decision. The factor of min-write-sz is due to
294the behavior of flash hardware. The factor of 3 is explained below.
Fabio Utzig5bd4e582017-07-20 08:55:38 -0300295
Fabio Utzigea422c22017-09-11 11:02:47 -03002962. Swap size: When beginning a new swap operation, the total size that needs
David Brown17e20d12017-09-12 11:53:20 -0600297 to be swapped (based on the slot with largest image + tlvs) is written to this
298 location for easier recovery in case of a reset while performing the swap.
Fabio Utzigea422c22017-09-11 11:02:47 -0300299
3003. Copy done: A single byte indicating whether the image in this slot is
David Brown17e20d12017-09-12 11:53:20 -0600301 complete (0x01=done; 0xff=not done).
Fabio Utzig5bd4e582017-07-20 08:55:38 -0300302
Fabio Utzigea422c22017-09-11 11:02:47 -03003034. Image OK: A single byte indicating whether the image in this slot has been
David Brown17e20d12017-09-12 11:53:20 -0600304 confirmed as good by the user (0x01=confirmed; 0xff=not confirmed).
Fabio Utzig5bd4e582017-07-20 08:55:38 -0300305
Fabio Utzigea422c22017-09-11 11:02:47 -03003065. MAGIC: The following 16 bytes, written in host-byte-order:
Christopher Collins92ea77f2016-12-12 15:59:26 -0800307
David Brown17e20d12017-09-12 11:53:20 -0600308``` c
Christopher Collins92ea77f2016-12-12 15:59:26 -0800309 const uint32_t boot_img_magic[4] = {
310 0xf395c277,
311 0x7fefd260,
312 0x0f505235,
313 0x8079b62c,
314 };
David Brown17e20d12017-09-12 11:53:20 -0600315```
Christopher Collins92ea77f2016-12-12 15:59:26 -0800316
David Brown17e20d12017-09-12 11:53:20 -0600317## IMAGE TRAILERS
Fabio Utzig86fe4b22017-07-28 18:56:29 -0300318
Marti Bolivar048d8d82017-08-04 17:14:24 -0400319At startup, the boot loader determines the boot swap type by inspecting the
320image trailers. When using the term "image trailers" what is meant is the
321aggregate information provided by both image slot's trailers.
Fabio Utzig86fe4b22017-07-28 18:56:29 -0300322
323The image trailers records are structured around the limitations imposed by flash
Christopher Collins92ea77f2016-12-12 15:59:26 -0800324hardware. As a consequence, they do not have a very intuitive design, and it
325is difficult to get a sense of the state of the device just by looking at the
Marti Bolivar048d8d82017-08-04 17:14:24 -0400326image trailers. It is better to map all the possible trailer states to the swap
327types described above via a set of tables. These tables are reproduced below.
Fabio Utzig86fe4b22017-07-28 18:56:29 -0300328
329Note: An important caveat about the tables described below is that they must
330be evaluated in the order presented here. Lower state numbers must have a
331higher priority when testing the image trailers.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800332
David Brown17e20d12017-09-12 11:53:20 -0600333```
Christopher Collins92ea77f2016-12-12 15:59:26 -0800334 State I
335 | slot-0 | slot-1 |
336 -----------------+--------+--------|
Christopher Collins92ea77f2016-12-12 15:59:26 -0800337 magic | Any | Good |
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -0800338 image-ok | Any | Unset |
Fabio Utzigf9d44282017-07-20 15:05:13 -0300339 copy-done | Any | Any |
Christopher Collins92ea77f2016-12-12 15:59:26 -0800340 -----------------+--------+--------'
Marti Bolivar048d8d82017-08-04 17:14:24 -0400341 result: BOOT_SWAP_TYPE_TEST |
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -0800342 -----------------------------------'
Fabio Utzig5bd4e582017-07-20 08:55:38 -0300343
Christopher Collins92ea77f2016-12-12 15:59:26 -0800344
Fabio Utzig86fe4b22017-07-28 18:56:29 -0300345 State II
Christopher Collins92ea77f2016-12-12 15:59:26 -0800346 | slot-0 | slot-1 |
347 -----------------+--------+--------|
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -0800348 magic | Any | Good |
349 image-ok | Any | 0x01 |
Fabio Utzigf9d44282017-07-20 15:05:13 -0300350 copy-done | Any | Any |
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -0800351 -----------------+--------+--------'
Marti Bolivar048d8d82017-08-04 17:14:24 -0400352 result: BOOT_SWAP_TYPE_PERM |
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -0800353 -----------------------------------'
Fabio Utzig5bd4e582017-07-20 08:55:38 -0300354
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -0800355
Fabio Utzig86fe4b22017-07-28 18:56:29 -0300356 State III
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -0800357 | slot-0 | slot-1 |
358 -----------------+--------+--------|
Christopher Collins92ea77f2016-12-12 15:59:26 -0800359 magic | Good | Unset |
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -0800360 image-ok | 0xff | Any |
Fabio Utzig5bd4e582017-07-20 08:55:38 -0300361 copy-done | 0x01 | Any |
Christopher Collins92ea77f2016-12-12 15:59:26 -0800362 -----------------+--------+--------'
Marti Bolivar048d8d82017-08-04 17:14:24 -0400363 result: BOOT_SWAP_TYPE_REVERT |
Christopher Collins92ea77f2016-12-12 15:59:26 -0800364 -----------------------------------'
David Brown17e20d12017-09-12 11:53:20 -0600365```
Christopher Collins92ea77f2016-12-12 15:59:26 -0800366
Marti Bolivar048d8d82017-08-04 17:14:24 -0400367Any of the above three states results in mcuboot attempting to swap images.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800368
Marti Bolivar048d8d82017-08-04 17:14:24 -0400369Otherwise, mcuboot does not attempt to swap images, resulting in one of the
370other three swap types, as illustrated by State IV.
Fabio Utzig86fe4b22017-07-28 18:56:29 -0300371
David Brown17e20d12017-09-12 11:53:20 -0600372```
Fabio Utzig86fe4b22017-07-28 18:56:29 -0300373 State IV
Christopher Collins92ea77f2016-12-12 15:59:26 -0800374 | slot-0 | slot-1 |
375 -----------------+--------+--------|
Fabio Utzig86fe4b22017-07-28 18:56:29 -0300376 magic | Any | Any |
377 image-ok | Any | Any |
Fabio Utzigf9d44282017-07-20 15:05:13 -0300378 copy-done | Any | Any |
Christopher Collins92ea77f2016-12-12 15:59:26 -0800379 -----------------+--------+--------'
Marti Bolivar048d8d82017-08-04 17:14:24 -0400380 result: BOOT_SWAP_TYPE_NONE, |
381 BOOT_SWAP_TYPE_FAIL, or |
382 BOOT_SWAP_TYPE_PANIC |
Christopher Collins92ea77f2016-12-12 15:59:26 -0800383 -----------------------------------'
David Brown17e20d12017-09-12 11:53:20 -0600384```
Christopher Collins92ea77f2016-12-12 15:59:26 -0800385
Marti Bolivar048d8d82017-08-04 17:14:24 -0400386In State IV, when no errors occur, mcuboot will attempt to boot the contents of
David Brown17e20d12017-09-12 11:53:20 -0600387slot 0 directly, and the result is `BOOT_SWAP_TYPE_NONE`. If the image in slot 0
388is not valid, the result is `BOOT_SWAP_TYPE_FAIL`. If a fatal error occurs during
389boot, the result is `BOOT_SWAP_TYPE_PANIC`. If the result is either
390`BOOT_SWAP_TYPE_FAIL` or `BOOT_SWAP_TYPE_PANIC`, mcuboot hangs rather than booting
Marti Bolivar048d8d82017-08-04 17:14:24 -0400391an invalid or compromised image.
Fabio Utzig86fe4b22017-07-28 18:56:29 -0300392
Marti Bolivar048d8d82017-08-04 17:14:24 -0400393Note: An important caveat to the above is the result when a swap is requested
394 and the image in slot 1 fails to validate, due to a hashing or signing
395 error. This state behaves as State IV with the extra action of marking
396 the image in slot 0 as "OK", to prevent further attempts to swap.
Fabio Utzig86fe4b22017-07-28 18:56:29 -0300397
398
David Brown17e20d12017-09-12 11:53:20 -0600399## High-Level Operation
Christopher Collins92ea77f2016-12-12 15:59:26 -0800400
401With the terms defined, we can now explore the boot loader's operation. First,
402a high-level overview of the boot process is presented. Then, the following
403sections describe each step of the process in more detail.
404
405Procedure:
406
David Brown17e20d12017-09-12 11:53:20 -06004071. Inspect swap status region; is an interrupted swap being resumed?
408 Yes: Complete the partial swap operation; skip to step 3.
409 No: Proceed to step 2.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800410
David Brown17e20d12017-09-12 11:53:20 -06004112. Inspect image trailers; is a swap requested?
Christopher Collins92ea77f2016-12-12 15:59:26 -0800412 Yes.
413 1. Is the requested image valid (integrity and security check)?
414 Yes.
415 a. Perform swap operation.
Fabio Utzig86fe4b22017-07-28 18:56:29 -0300416 b. Persist completion of swap procedure to image trailers.
David Brown17e20d12017-09-12 11:53:20 -0600417 c. Proceed to step 3.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800418 No.
419 a. Erase invalid image.
Fabio Utzig86fe4b22017-07-28 18:56:29 -0300420 b. Persist failure of swap procedure to image trailers.
David Brown17e20d12017-09-12 11:53:20 -0600421 c. Proceed to step 3.
422 No: Proceed to step 3.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800423
David Brown17e20d12017-09-12 11:53:20 -06004243. Boot into image in slot 0.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800425
David Brown17e20d12017-09-12 11:53:20 -0600426## Image Swapping
Christopher Collins92ea77f2016-12-12 15:59:26 -0800427
428The boot loader swaps the contents of the two image slots for two reasons:
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -0800429 * User has issued a "set pending" operation; the image in slot-1 should be
430 run once (state II) or repeatedly (state III), depending on whether a
431 permanent swap was specified.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800432 * Test image rebooted without being confirmed; the boot loader should
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -0800433 revert to the original image currently in slot-1 (state IV).
Christopher Collins92ea77f2016-12-12 15:59:26 -0800434
Fabio Utzig86fe4b22017-07-28 18:56:29 -0300435If the image trailers indicates that the image in the secondary slot should be
Christopher Collins92ea77f2016-12-12 15:59:26 -0800436run, the boot loader needs to copy it to the primary slot. The image currently
437in the primary slot also needs to be retained in flash so that it can be used
438later. Furthermore, both images need to be recoverable if the boot loader
439resets in the middle of the swap operation. The two images are swapped
440according to the following procedure:
441
David Brown17e20d12017-09-12 11:53:20 -0600442<!-- Markdown doesn't do nested numbered lists. It will do nested
443bulletted lists, so maybe that is better. -->
Christopher Collins92ea77f2016-12-12 15:59:26 -0800444 1. Determine how many flash sectors each image slot consists of. This
445 number must be the same for both slots.
446 2. Iterate the list of sector indices in descending order (i.e., starting
447 with the greatest index); current element = "index".
448 b. Erase scratch area.
Ryan C Johnsonce4fa442017-10-19 12:27:46 -0700449 c. Copy slot1[index] to scratch area.
Fabio Utzig5bd4e582017-07-20 08:55:38 -0300450 - If these are the last sectors (i.e., first swap being perfomed),
451 copy the full sector *except* the image trailer.
452 - Else, copy entire sector contents.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800453 d. Write updated swap status (i).
454
455 e. Erase slot1[index]
456 f. Copy slot0[index] to slot1[index]
457 - If these are the last sectors (i.e., first swap being perfomed),
458 copy the full sector *except* the image trailer.
459 - Else, copy entire sector contents.
460 g. Write updated swap status (ii).
461
462 h. Erase slot0[index].
Ryan C Johnsonce4fa442017-10-19 12:27:46 -0700463 i. Copy scratch area to slot0[index].
Fabio Utzig5bd4e582017-07-20 08:55:38 -0300464 - If these are the last sectors (i.e., first swap being perfomed),
465 copy the full sector *except* the image trailer.
466 - Else, copy entire sector contents.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800467 j. Write updated swap status (iii).
468
469 3. Persist completion of swap procedure to slot 0 image trailer.
470
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -0800471The additional caveats in step 2f are necessary so that the slot 1 image
472trailer can be written by the user at a later time. With the image trailer
473unwritten, the user can test the image in slot 1 (i.e., transition to state
474II).
Christopher Collins92ea77f2016-12-12 15:59:26 -0800475
Fabio Utzig5bd4e582017-07-20 08:55:38 -0300476Note1: If the sector being copied is the last sector, then swap status is
477temporarily maintained on scratch for the duration of this operation, always
478using slot0's area otherwise.
479
480Note2: The bootloader tries to copy only used sectors (based on largest image
481installed on any of the slots), minimizing the amount of sectors copied and
482reducing the amount of time required for a swap operation.
483
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -0800484The particulars of step 3 vary depending on whether an image is being tested,
Fabio Utzig86fe4b22017-07-28 18:56:29 -0300485permanently used, reverted or a validation failure of slot 1 happened when a
486swap was requested:
487
Christopher Collins92ea77f2016-12-12 15:59:26 -0800488 * test:
489 o Write slot0.copy_done = 1
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -0800490 (swap caused the following values to be written:
491 slot0.magic = BOOT_MAGIC
492 slot0.image_ok = Unset)
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -0800493
494 * permanent:
495 o Write slot0.copy_done = 1
496 (swap caused the following values to be written:
497 slot0.magic = BOOT_MAGIC
498 slot0.image_ok = 0x01)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800499
500 * revert:
Christopher Collins92ea77f2016-12-12 15:59:26 -0800501 o Write slot0.copy_done = 1
502 o Write slot0.image_ok = 1
Fabio Utzig86fe4b22017-07-28 18:56:29 -0300503 (swap caused the following values to be written:
504 slot0.magic = BOOT_MAGIC)
505
506 * failure to validate slot 1:
507 o Write slot0.image_ok = 1
508
509After completing the operations as described above the image in slot 0 should
510be booted.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800511
David Brown17e20d12017-09-12 11:53:20 -0600512## Swap Status
Christopher Collins92ea77f2016-12-12 15:59:26 -0800513
514The swap status region allows the boot loader to recover in case it restarts in
515the middle of an image swap operation. The swap status region consists of a
516series of single-byte records. These records are written independently, and
517therefore must be padded according to the minimum write size imposed by the
518flash hardware. In the below figure, a min-write-size of 1 is assumed for
519simplicity. The structure of the swap status region is illustrated below. In
520this figure, a min-write-size of 1 is assumed for simplicity.
521
David Brown17e20d12017-09-12 11:53:20 -0600522```
Christopher Collins92ea77f2016-12-12 15:59:26 -0800523 0 1 2 3
524 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
525 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
526 |sec127,state 0 |sec127,state 1 |sec127,state 2 |sec126,state 0 |
527 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
528 |sec126,state 1 |sec126,state 2 |sec125,state 0 |sec125,state 1 |
529 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
530 |sec125,state 2 | |
531 +-+-+-+-+-+-+-+-+ +
532 ~ ~
533 ~ [Records for indices 124 through 1 ~
534 ~ ~
535 ~ +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
536 ~ |sec000,state 0 |sec000,state 1 |sec000,state 2 |
537 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
David Brown17e20d12017-09-12 11:53:20 -0600538```
Christopher Collins92ea77f2016-12-12 15:59:26 -0800539
540The above is probably not helpful at all; here is a description in English.
541
542Each image slot is partitioned into a sequence of flash sectors. If we were to
543enumerate the sectors in a single slot, starting at 0, we would have a list of
544sector indices. Since there are two image slots, each sector index would
545correspond to a pair of sectors. For example, sector index 0 corresponds to
546the first sector in slot 0 and the first sector in slot 1. Furthermore, we
547impose a limit of 128 indices. If an image slot consists of more than 128
548sectors, the flash layout is not compatible with this boot loader. Finally,
549reverse the list of indices such that the list starts with index 127 and ends
550with 0. The swap status region is a representation of this reversed list.
551
552During a swap operation, each sector index transitions through four separate
553states:
David Brown17e20d12017-09-12 11:53:20 -0600554```
Christopher Collins92ea77f2016-12-12 15:59:26 -0800555 0. slot 0: image 0, slot 1: image 1, scratch: N/A
556 1. slot 0: image 0, slot 1: N/A, scratch: image 1 (1->s, erase 1)
557 2. slot 0: N/A, slot 1: image 0, scratch: image 1 (0->1, erase 0)
558 3. slot 0: image 1, slot 1: image 0, scratch: N/A (s->0)
David Brown17e20d12017-09-12 11:53:20 -0600559```
Christopher Collins92ea77f2016-12-12 15:59:26 -0800560
561Each time a sector index transitions to a new state, the boot loader writes a
562record to the swap status region. Logically, the boot loader only needs one
563record per sector index to keep track of the current swap state. However, due
564to limitations imposed by flash hardware, a record cannot be overwritten when
565an index's state changes. To solve this problem, the boot loader uses three
566records per sector index rather than just one.
567
568Each sector-state pair is represented as a set of three records. The record
569values map to the above four states as follows
570
David Brown17e20d12017-09-12 11:53:20 -0600571```
Christopher Collins92ea77f2016-12-12 15:59:26 -0800572 | rec0 | rec1 | rec2
573 --------+------+------+------
574 state 0 | 0xff | 0xff | 0xff
575 state 1 | 0x01 | 0xff | 0xff
576 state 2 | 0x01 | 0x02 | 0xff
577 state 3 | 0x01 | 0x02 | 0x03
David Brown17e20d12017-09-12 11:53:20 -0600578```
Christopher Collins92ea77f2016-12-12 15:59:26 -0800579
580The swap status region can accommodate 128 sector indices. Hence, the size of
David Brown17e20d12017-09-12 11:53:20 -0600581the region, in bytes, is `128 * min-write-size * 3`. The number 128 is chosen
Christopher Collins92ea77f2016-12-12 15:59:26 -0800582somewhat arbitrarily and will likely be made configurable. The only
583requirement for the index count is that is is great enough to account for a
584maximum-sized image (i.e., at least as great as the total sector count in an
585image slot). If a device's image slots use less than 128 sectors, the first
586record that gets written will be somewhere in the middle of the region. For
587example, if a slot uses 64 sectors, the first sector index that gets swapped is
58863, which corresponds to the exact halfway point within the region.
589
Fabio Utzig5bd4e582017-07-20 08:55:38 -0300590Note: since the scratch area only ever needs to record swapping of the last
591sector, it uses at most min-write-size * 3 bytes for its own status area.
592
David Brown17e20d12017-09-12 11:53:20 -0600593## Reset Recovery
Christopher Collins92ea77f2016-12-12 15:59:26 -0800594
595If the boot loader resets in the middle of a swap operation, the two images may
596be discontiguous in flash. Bootutil recovers from this condition by using the
Fabio Utzig86fe4b22017-07-28 18:56:29 -0300597image trailers to determine how the image parts are distributed in flash.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800598
599The first step is determine where the relevant swap status region is located.
600Because this region is embedded within the image slots, its location in flash
Fabio Utzig86fe4b22017-07-28 18:56:29 -0300601changes during a swap operation. The below set of tables map image trailers
Christopher Collins92ea77f2016-12-12 15:59:26 -0800602contents to swap status location. In these tables, the "source" field
603indicates where the swap status region is located.
604
David Brown17e20d12017-09-12 11:53:20 -0600605```
Christopher Collins92ea77f2016-12-12 15:59:26 -0800606 | slot-0 | scratch |
607 ----------+------------+------------|
608 magic | Good | Any |
609 copy-done | 0x01 | N/A |
610 ----------+------------+------------'
611 source: none |
612 ------------------------------------'
Marti Bolivar49b29172017-08-04 14:50:51 -0400613
Christopher Collins92ea77f2016-12-12 15:59:26 -0800614 | slot-0 | scratch |
615 ----------+------------+------------|
616 magic | Good | Any |
617 copy-done | 0xff | N/A |
618 ----------+------------+------------'
619 source: slot 0 |
620 ------------------------------------'
Marti Bolivar49b29172017-08-04 14:50:51 -0400621
Christopher Collins92ea77f2016-12-12 15:59:26 -0800622 | slot-0 | scratch |
623 ----------+------------+------------|
624 magic | Any | Good |
625 copy-done | Any | N/A |
626 ----------+------------+------------'
627 source: scratch |
628 ------------------------------------'
Marti Bolivar49b29172017-08-04 14:50:51 -0400629
Christopher Collins92ea77f2016-12-12 15:59:26 -0800630 | slot-0 | scratch |
631 ----------+------------+------------|
632 magic | Unset | Any |
633 copy-done | 0xff | N/A |
634 ----------+------------+------------|
Fabio Utzig86fe4b22017-07-28 18:56:29 -0300635 source: slot 0 |
Christopher Collins92ea77f2016-12-12 15:59:26 -0800636 ------------------------------------+------------------------------+
637 This represents one of two cases: |
638 o No swaps ever (no status to read, so no harm in checking). |
639 o Mid-revert; status in slot 0. |
Fabio Utzig86fe4b22017-07-28 18:56:29 -0300640 For this reason we assume slot 0 as source, to trigger a check |
641 of the status area and find out if there was swapping under way. |
Christopher Collins92ea77f2016-12-12 15:59:26 -0800642 -------------------------------------------------------------------'
David Brown17e20d12017-09-12 11:53:20 -0600643```
Christopher Collins92ea77f2016-12-12 15:59:26 -0800644
645If the swap status region indicates that the images are not contiguous,
646bootutil completes the swap operation that was in progress when the system was
647reset. In other words, it applies the procedure defined in the previous
648section, moving image 1 into slot 0 and image 0 into slot 1. If the boot
649status file indicates that an image part is present in the scratch area, this
650part is copied into the correct location by starting at step e or step h in the
651area-swap procedure, depending on whether the part belongs to image 0 or image
6521.
653
654After the swap operation has been completed, the boot loader proceeds as though
655it had just been started.
656
David Brown17e20d12017-09-12 11:53:20 -0600657## Integrity Check
Christopher Collins92ea77f2016-12-12 15:59:26 -0800658
659An image is checked for integrity immediately before it gets copied into the
Fabio Utzig5bd4e582017-07-20 08:55:38 -0300660primary slot. If the boot loader doesn't perform an image swap, then it can
661perform an optional integrity check of the image in slot0 if
David Brown17e20d12017-09-12 11:53:20 -0600662`MCUBOOT_VALIDATE_SLOT0` is set, otherwise it doesn't perform an integrity check.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800663
664During the integrity check, the boot loader verifies the following aspects of
665an image:
Fabio Utzigea422c22017-09-11 11:02:47 -0300666 * 32-bit magic number must be correct (0x96f3b83d).
David Brown17e20d12017-09-12 11:53:20 -0600667 * Image must contain an `image_tlv_info` struct, identified by its magic
Fabio Utzigea422c22017-09-11 11:02:47 -0300668 (0x6907) exactly following the firmware (hdr_size + img_size).
Christopher Collins92ea77f2016-12-12 15:59:26 -0800669 * Image must contain a SHA256 TLV.
Fabio Utzig86fe4b22017-07-28 18:56:29 -0300670 * Calculated SHA256 must match SHA256 TLV contents.
Fabio Utzigea422c22017-09-11 11:02:47 -0300671 * Image *may* contain a signature TLV. If it does, it must also have a
672 KEYHASH TLV with the hash of the key that was used to sign. The list of
673 keys will then be iterated over looking for the matching key, which then
674 will then be used to verify the image contents.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800675
David Brown17e20d12017-09-12 11:53:20 -0600676## Security
Christopher Collins92ea77f2016-12-12 15:59:26 -0800677
678As indicated above, the final step of the integrity check is signature
679verification. The boot loader can have one or more public keys embedded in it
680at build time. During signature verification, the boot loader verifies that an
Fabio Utzigea422c22017-09-11 11:02:47 -0300681image was signed with a private key that corresponds to the embedded keyhash
682TLV.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800683
684For information on embedding public keys in the boot loader, as well as
David Brown17e20d12017-09-12 11:53:20 -0600685producing signed images, see: [signed_images]({% link signed_images.md
686%}).