blob: 6301d91c65ab4cbc21e344fef69cc4c0a8e9291f [file] [log] [blame] [view]
David Brown17e20d12017-09-12 11:53:20 -06001<!--
David Vincze4e3c47b2020-04-21 17:11:33 +02002 -
3 - Licensed to the Apache Software Foundation (ASF) under one
4 - or more contributor license agreements. See the NOTICE file
5 - distributed with this work for additional information
6 - regarding copyright ownership. The ASF licenses this file
7 - to you under the Apache License, Version 2.0 (the
8 - "License"); you may not use this file except in compliance
9 - with the License. You may obtain a copy of the License at
10 -
11 - http://www.apache.org/licenses/LICENSE-2.0
12 -
13 - Unless required by applicable law or agreed to in writing,
14 - software distributed under the License is distributed on an
15 - "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 - KIND, either express or implied. See the License for the
17 - specific language governing permissions and limitations
18 - under the License.
19 -
David Brown17e20d12017-09-12 11:53:20 -060020-->
21
David Vinczeba3bd602019-06-17 16:01:43 +020022<!--
David Vincze4e3c47b2020-04-21 17:11:33 +020023 - Modifications are Copyright (c) 2019-2020 Arm Limited.
David Vinczeba3bd602019-06-17 16:01:43 +020024-->
25
David Brown17e20d12017-09-12 11:53:20 -060026# Boot Loader
27
Fabio Utzigd37d8772019-12-03 10:32:18 -030028## [Summary](#summary)
Christopher Collins92ea77f2016-12-12 15:59:26 -080029
Fabio Utzigac834962017-07-20 13:20:48 -030030mcuboot comprises two packages:
Christopher Collins92ea77f2016-12-12 15:59:26 -080031
David Brown17e20d12017-09-12 11:53:20 -060032* The bootutil library (boot/bootutil)
33* The boot application (each port has its own at boot/<port>)
Christopher Collins92ea77f2016-12-12 15:59:26 -080034
35The bootutil library performs most of the functions of a boot loader. In
36particular, the piece that is missing is the final step of actually jumping to
37the main image. This last step is instead implemented by the boot application.
38Boot loader functionality is separated in this manner to enable unit testing of
39the boot loader. A library can be unit tested, but an application can't.
40Therefore, functionality is delegated to the bootutil library when possible.
41
Fabio Utzigd37d8772019-12-03 10:32:18 -030042## [Limitations](#limitations)
Christopher Collins92ea77f2016-12-12 15:59:26 -080043
44The boot loader currently only supports images with the following
45characteristics:
David Brown17e20d12017-09-12 11:53:20 -060046* Built to run from flash.
47* Built to run from a fixed location (i.e., not position-independent).
Christopher Collins92ea77f2016-12-12 15:59:26 -080048
Fabio Utzigd37d8772019-12-03 10:32:18 -030049## [Image Format](#image-format)
Christopher Collins92ea77f2016-12-12 15:59:26 -080050
51The following definitions describe the image format.
52
David Brown17e20d12017-09-12 11:53:20 -060053``` c
Fabio Utzigea422c22017-09-11 11:02:47 -030054#define IMAGE_MAGIC 0x96f3b83d
Christopher Collins92ea77f2016-12-12 15:59:26 -080055
56#define IMAGE_HEADER_SIZE 32
57
58struct image_version {
59 uint8_t iv_major;
60 uint8_t iv_minor;
61 uint16_t iv_revision;
62 uint32_t iv_build_num;
63};
64
65/** Image header. All fields are in little endian byte order. */
66struct image_header {
67 uint32_t ih_magic;
Fabio Utzigea422c22017-09-11 11:02:47 -030068 uint32_t ih_load_addr;
David Vinczee32483f2019-06-13 10:46:24 +020069 uint16_t ih_hdr_size; /* Size of image header (bytes). */
70 uint16_t ih_protect_tlv_size; /* Size of protected TLV area (bytes). */
71 uint32_t ih_img_size; /* Does not include header. */
72 uint32_t ih_flags; /* IMAGE_F_[...]. */
Christopher Collins92ea77f2016-12-12 15:59:26 -080073 struct image_version ih_ver;
David Vinczee32483f2019-06-13 10:46:24 +020074 uint32_t _pad1;
Christopher Collins92ea77f2016-12-12 15:59:26 -080075};
76
Fabio Utzigfd140ec2019-09-12 14:37:48 -030077#define IMAGE_TLV_INFO_MAGIC 0x6907
78#define IMAGE_TLV_PROT_INFO_MAGIC 0x6908
79
Fabio Utzigea422c22017-09-11 11:02:47 -030080/** Image TLV header. All fields in little endian. */
81struct image_tlv_info {
82 uint16_t it_magic;
83 uint16_t it_tlv_tot; /* size of TLV area (including tlv_info header) */
84};
85
Christopher Collins92ea77f2016-12-12 15:59:26 -080086/** Image trailer TLV format. All fields in little endian. */
87struct image_tlv {
88 uint8_t it_type; /* IMAGE_TLV_[...]. */
89 uint8_t _pad;
Marti Bolivar49b29172017-08-04 14:50:51 -040090 uint16_t it_len; /* Data length (not including TLV header). */
Christopher Collins92ea77f2016-12-12 15:59:26 -080091};
92
93/*
94 * Image header flags.
95 */
Marti Bolivar7c057e92017-08-04 14:46:39 -040096#define IMAGE_F_PIC 0x00000001 /* Not supported. */
Marti Bolivar7c057e92017-08-04 14:46:39 -040097#define IMAGE_F_NON_BOOTABLE 0x00000010 /* Split image app. */
Fabio Utzigea422c22017-09-11 11:02:47 -030098#define IMAGE_F_RAM_LOAD 0x00000020
Christopher Collins92ea77f2016-12-12 15:59:26 -080099
100/*
101 * Image trailer TLV types.
102 */
Fabio Utzigea422c22017-09-11 11:02:47 -0300103#define IMAGE_TLV_KEYHASH 0x01 /* hash of the public key */
David Brown27648b82017-08-31 10:40:29 -0600104#define IMAGE_TLV_SHA256 0x10 /* SHA256 of image hdr and body */
Marko Kiiskila8dd56f32017-08-22 21:40:49 -0700105#define IMAGE_TLV_RSA2048_PSS 0x20 /* RSA2048 of hash output */
David Brown27648b82017-08-31 10:40:29 -0600106#define IMAGE_TLV_ECDSA224 0x21 /* ECDSA of hash output */
107#define IMAGE_TLV_ECDSA256 0x22 /* ECDSA of hash output */
Fabio Utzig3501c012019-05-13 15:07:25 -0700108#define IMAGE_TLV_RSA3072_PSS 0x23 /* RSA3072 of hash output */
Fabio Utzig195411f2019-06-28 07:48:21 -0300109#define IMAGE_TLV_ED25519 0x24 /* ED25519 of hash output */
David Vinczee32483f2019-06-13 10:46:24 +0200110#define IMAGE_TLV_ENC_RSA2048 0x30 /* Key encrypted with RSA-OAEP-2048 */
111#define IMAGE_TLV_ENC_KW128 0x31 /* Key encrypted with AES-KW-128 */
Fabio Utzig5eaa5762020-04-02 13:30:43 -0300112#define IMAGE_TLV_ENC_EC256 0x32 /* Key encrypted with ECIES-P256 */
113#define IMAGE_TLV_ENC_X25519 0x33 /* Key encrypted with ECIES-X25519 */
David Vinczee32483f2019-06-13 10:46:24 +0200114#define IMAGE_TLV_DEPENDENCY 0x40 /* Image depends on other image */
David Vinczec3084132020-02-18 14:50:47 +0100115#define IMAGE_TLV_SEC_CNT 0x50 /* security counter */
David Brown17e20d12017-09-12 11:53:20 -0600116```
Christopher Collins92ea77f2016-12-12 15:59:26 -0800117
118Optional type-length-value records (TLVs) containing image metadata are placed
119after the end of the image.
120
David Vinczee32483f2019-06-13 10:46:24 +0200121The `ih_protect_tlv_size` field indicates the length of the protected TLV area.
Fabio Utzigfd140ec2019-09-12 14:37:48 -0300122If protected TLVs are present then a TLV info header with magic equal to
123`IMAGE_TLV_PROT_INFO_MAGIC` must be present and the protected TLVs (plus the
124info header itself) have to be included in the hash calculation. Otherwise the
125hash is only calculated over the image header and the image itself. In this
David Vinczee32483f2019-06-13 10:46:24 +0200126case the value of the `ih_protect_tlv_size` field is 0.
127
David Brown17e20d12017-09-12 11:53:20 -0600128The `ih_hdr_size` field indicates the length of the header, and therefore the
Christopher Collins92ea77f2016-12-12 15:59:26 -0800129offset of the image itself. This field provides for backwards compatibility in
130case of changes to the format of the image header.
131
Fabio Utzigd37d8772019-12-03 10:32:18 -0300132## [Flash Map](#flash-map)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800133
Fabio Utzigac834962017-07-20 13:20:48 -0300134A device's flash is partitioned according to its _flash map_. At a high
Christopher Collins92ea77f2016-12-12 15:59:26 -0800135level, the flash map maps numeric IDs to _flash areas_. A flash area is a
136region of disk with the following properties:
David Brown17e20d12017-09-12 11:53:20 -06001371. An area can be fully erased without affecting any other areas.
1382. A write to one area does not restrict writes to other areas.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800139
Marti Bolivar4e64d562017-08-04 14:53:33 -0400140The boot loader uses the following flash area IDs:
David Vinczeb75c12a2019-03-22 14:58:33 +0100141```c
142/* Independent from multiple image boot */
David Vincze2d736ad2019-02-18 11:50:22 +0100143#define FLASH_AREA_BOOTLOADER 0
David Vinczeb75c12a2019-03-22 14:58:33 +0100144#define FLASH_AREA_IMAGE_SCRATCH 3
145```
146```c
147/* If the boot loader is working with the first image */
David Vincze2d736ad2019-02-18 11:50:22 +0100148#define FLASH_AREA_IMAGE_PRIMARY 1
149#define FLASH_AREA_IMAGE_SECONDARY 2
David Vinczeb75c12a2019-03-22 14:58:33 +0100150```
151```c
152/* If the boot loader is working with the second image */
153#define FLASH_AREA_IMAGE_PRIMARY 5
154#define FLASH_AREA_IMAGE_SECONDARY 6
David Brown17e20d12017-09-12 11:53:20 -0600155```
Christopher Collins92ea77f2016-12-12 15:59:26 -0800156
Marti Bolivar4e64d562017-08-04 14:53:33 -0400157The bootloader area contains the bootloader image itself. The other areas are
David Vinczeb75c12a2019-03-22 14:58:33 +0100158described in subsequent sections. The flash could contain multiple executable
159images therefore the flash area IDs of primary and secondary areas are mapped
160based on the number of the active image (on which the bootloader is currently
161working).
Marti Bolivar4e64d562017-08-04 14:53:33 -0400162
Fabio Utzigd37d8772019-12-03 10:32:18 -0300163## [Image Slots](#image-slots)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800164
David Vinczeb75c12a2019-03-22 14:58:33 +0100165A portion of the flash memory can be partitioned into multiple image areas, each
166contains two image slots: a primary slot and a secondary slot.
167The boot loader will only run an image from the primary slot, so images must be
168built such that they can run from that fixed location in flash. If the boot
169loader needs to run the image resident in the secondary slot, it must copy its
170contents into the primary slot before doing so, either by swapping the two
171images or by overwriting the contents of the primary slot. The bootloader
172supports either swap- or overwrite-based image upgrades, but must be configured
173at build time to choose one of these two strategies.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800174
David Vinczeb75c12a2019-03-22 14:58:33 +0100175In addition to the slots of image areas, the boot loader requires a scratch
176area to allow for reliable image swapping. The scratch area must have a size
177that is enough to store at least the largest sector that is going to be swapped.
178Many devices have small equally sized flash sectors, eg 4K, while others have
David Vincze2d736ad2019-02-18 11:50:22 +0100179variable sized sectors where the largest sectors might be 128K or 256K, so the
180scratch must be big enough to store that. The scratch is only ever used when
181swapping firmware, which means only when doing an upgrade. Given that, the main
182reason for using a larger size for the scratch is that flash wear will be more
183evenly distributed, because a single sector would be written twice the number of
184times than using two sectors, for example. To evaluate the ideal size of the
185scratch for your use case the following parameters are relevant:
Fabio Utziga722f5a2017-12-12 14:04:53 -0200186
187* the ratio of image size / scratch size
188* the number of erase cycles supported by the flash hardware
189
190The image size is used (instead of slot size) because only the slot's sectors
191that are actually used for storing the image are copied. The image/scratch ratio
192is the number of times the scratch will be erased on every upgrade. The number
193of erase cycles divided by the image/scratch ratio will give you the number of
194times an upgrade can be performed before the device goes out of spec.
195
196```
197num_upgrades = number_of_erase_cycles / (image_size / scratch_size)
198```
199
200Let's assume, for example, a device with 10000 erase cycles, an image size of
201150K and a scratch of 4K (usual minimum size of 4K sector devices). This would
202result in a total of:
203
204`10000 / (150 / 4) ~ 267`
205
206Increasing the scratch to 16K would give us:
207
208`10000 / (150 / 16) ~ 1067`
209
210There is no *best* ratio, as the right size is use-case dependent. Factors to
211consider include the number of times a device will be upgraded both in the field
David Vincze2d736ad2019-02-18 11:50:22 +0100212and during development, as well as any desired safety margin on the
213manufacturer's specified number of erase cycles. In general, using a ratio that
214allows hundreds to thousands of field upgrades in production is recommended.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800215
Marti Bolivara91674f2017-08-04 14:56:08 -0400216The overwrite upgrade strategy is substantially simpler to implement than the
217image swapping strategy, especially since the bootloader must work properly
218even when it is reset during the middle of an image swap. For this reason, the
219rest of the document describes its behavior when configured to swap images
220during an upgrade.
221
Fabio Utzigd37d8772019-12-03 10:32:18 -0300222## [Boot Swap Types](#boot-swap-types)
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -0800223
Marti Bolivar048d8d82017-08-04 17:14:24 -0400224When the device first boots under normal circumstances, there is an up-to-date
David Vinczeba3bd602019-06-17 16:01:43 +0200225firmware image in each primary slot, which mcuboot can validate and then
David Vincze2d736ad2019-02-18 11:50:22 +0100226chain-load. In this case, no image swaps are necessary. During device upgrades,
David Vinczeba3bd602019-06-17 16:01:43 +0200227however, new candidate image(s) is present in the secondary slot(s), which
228mcuboot must swap into the primary slot(s) before booting as discussed above.
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -0800229
Marti Bolivar048d8d82017-08-04 17:14:24 -0400230Upgrading an old image with a new one by swapping can be a two-step process. In
231this process, mcuboot performs a "test" swap of image data in flash and boots
David Vinczeba3bd602019-06-17 16:01:43 +0200232the new image or it will be executed during operation. The new image can then
233update the contents of flash at runtime to mark itself "OK", and mcuboot will
234then still choose to run it during the next boot. When this happens, the swap is
235made "permanent". If this doesn't happen, mcuboot will perform a "revert" swap
236during the next boot by swapping the image(s) back into its original location(s)
237, and attempting to boot the old image(s).
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -0800238
Marti Bolivar048d8d82017-08-04 17:14:24 -0400239Depending on the use case, the first swap can also be made permanent directly.
240In this case, mcuboot will never attempt to revert the images on the next reset.
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -0800241
Marti Bolivar048d8d82017-08-04 17:14:24 -0400242Test swaps are supported to provide a rollback mechanism to prevent devices
243from becoming "bricked" by bad firmware. If the device crashes immediately
244upon booting a new (bad) image, mcuboot will revert to the old (working) image
245at the next device reset, rather than booting the bad image again. This allows
246device firmware to make test swaps permanent only after performing a self-test
247routine.
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -0800248
David Vinczeba3bd602019-06-17 16:01:43 +0200249On startup, mcuboot inspects the contents of flash to decide for each images
250which of these "swap types" to perform; this decision determines how it
251proceeds.
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -0800252
Marti Bolivar048d8d82017-08-04 17:14:24 -0400253The possible swap types, and their meanings, are:
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -0800254
David Brown17e20d12017-09-12 11:53:20 -0600255- `BOOT_SWAP_TYPE_NONE`: The "usual" or "no upgrade" case; attempt to boot the
David Vincze2d736ad2019-02-18 11:50:22 +0100256 contents of the primary slot.
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -0800257
David Vincze2d736ad2019-02-18 11:50:22 +0100258- `BOOT_SWAP_TYPE_TEST`: Boot the contents of the secondary slot by swapping
259 images. Unless the swap is made permanent, revert back on the next boot.
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -0800260
David Brown17e20d12017-09-12 11:53:20 -0600261- `BOOT_SWAP_TYPE_PERM`: Permanently swap images, and boot the upgraded image
Marti Bolivar048d8d82017-08-04 17:14:24 -0400262 firmware.
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -0800263
David Vincze2d736ad2019-02-18 11:50:22 +0100264- `BOOT_SWAP_TYPE_REVERT`: A previous test swap was not made permanent;
265 swap back to the old image whose data are now in the secondary slot. If the
266 old image marks itself "OK" when it boots, the next boot will have swap type
267 `BOOT_SWAP_TYPE_NONE`.
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -0800268
David Brown17e20d12017-09-12 11:53:20 -0600269- `BOOT_SWAP_TYPE_FAIL`: Swap failed because image to be run is not valid.
Marti Bolivar048d8d82017-08-04 17:14:24 -0400270
David Brown17e20d12017-09-12 11:53:20 -0600271- `BOOT_SWAP_TYPE_PANIC`: Swapping encountered an unrecoverable error.
Marti Bolivar048d8d82017-08-04 17:14:24 -0400272
273The "swap type" is a high-level representation of the outcome of the
274boot. Subsequent sections describe how mcuboot determines the swap type from
275the bit-level contents of flash.
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -0800276
Fabio Utzigd37d8772019-12-03 10:32:18 -0300277## [Image Trailer](#image-trailer)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800278
Fabio Utzig86fe4b22017-07-28 18:56:29 -0300279For the bootloader to be able to determine the current state and what actions
Marti Bolivar42818032017-08-04 15:45:01 -0400280should be taken during the current boot operation, it uses metadata stored in
281the image flash areas. While swapping, some of this metadata is temporarily
282copied into and out of the scratch area.
283
284This metadata is located at the end of the image flash areas, and is called an
285image trailer. An image trailer has the following structure:
Christopher Collins92ea77f2016-12-12 15:59:26 -0800286
David Brown17e20d12017-09-12 11:53:20 -0600287```
Christopher Collins92ea77f2016-12-12 15:59:26 -0800288 0 1 2 3
289 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
290 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Christopher Collins92ea77f2016-12-12 15:59:26 -0800291 ~ ~
Fabio Utzig2c05f1b2018-04-04 10:35:17 -0300292 ~ Swap status (BOOT_MAX_IMG_SECTORS * min-write-size * 3) ~
Christopher Collins92ea77f2016-12-12 15:59:26 -0800293 ~ ~
294 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Christopher Collinsa1c12042019-05-23 14:00:28 -0700295 | Encryption key 0 (16 octets) [*] |
296 | |
Christopher Collins92ea77f2016-12-12 15:59:26 -0800297 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Christopher Collinsa1c12042019-05-23 14:00:28 -0700298 | Encryption key 1 (16 octets) [*] |
299 | |
Fabio Utzig5bd4e582017-07-20 08:55:38 -0300300 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Christopher Collinsa1c12042019-05-23 14:00:28 -0700301 | Swap size (4 octets) |
Fabio Utzigea422c22017-09-11 11:02:47 -0300302 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
David Vinczee2453472019-06-17 12:31:59 +0200303 | Swap info | 0xff padding (7 octets) |
Fabio Utzigea422c22017-09-11 11:02:47 -0300304 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Christopher Collinsa1c12042019-05-23 14:00:28 -0700305 | Copy done | 0xff padding (7 octets) |
306 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
307 | Image OK | 0xff padding (7 octets) |
308 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
309 | MAGIC (16 octets) |
310 | |
Christopher Collins92ea77f2016-12-12 15:59:26 -0800311 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
David Brown17e20d12017-09-12 11:53:20 -0600312```
Christopher Collins92ea77f2016-12-12 15:59:26 -0800313
Christopher Collinsa1c12042019-05-23 14:00:28 -0700314[*]: Only present if the encryption option is enabled (`MCUBOOT_ENC_IMAGES`).
315
Marti Bolivar42818032017-08-04 15:45:01 -0400316The offset immediately following such a record represents the start of the next
317flash area.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800318
319Note: "min-write-size" is a property of the flash hardware. If the hardware
320allows individual bytes to be written at arbitrary addresses, then
321min-write-size is 1. If the hardware only allows writes at even addresses,
322then min-write-size is 2, and so on.
323
Marti Bolivar1dcb6852017-08-04 15:59:32 -0400324An image trailer contains the following fields:
Christopher Collins92ea77f2016-12-12 15:59:26 -0800325
Marti Bolivar1dcb6852017-08-04 15:59:32 -04003261. Swap status: A series of records which records the progress of an image
David Vincze2d736ad2019-02-18 11:50:22 +0100327 swap. To swap entire images, data are swapped between the two image areas
328 one or more sectors at a time, like this:
Marti Bolivar1dcb6852017-08-04 15:59:32 -0400329
David Vincze2d736ad2019-02-18 11:50:22 +0100330 - sector data in the primary slot is copied into scratch, then erased
331 - sector data in the secondary slot is copied into the primary slot,
332 then erased
333 - sector data in scratch is copied into the secondary slot
Marti Bolivar1dcb6852017-08-04 15:59:32 -0400334
335As it swaps images, the bootloader updates the swap status field in a way that
336allows it to compute how far this swap operation has progressed for each
337sector. The swap status field can thus used to resume a swap operation if the
338bootloader is halted while a swap operation is ongoing and later reset. The
David Vincze2d736ad2019-02-18 11:50:22 +0100339`BOOT_MAX_IMG_SECTORS` value is the configurable maximum number of sectors
340mcuboot supports for each image; its value defaults to 128, but allows for
341either decreasing this size, to limit RAM usage, or to increase it in devices
342that have massive amounts of Flash or very small sized sectors and thus require
343a bigger configuration to allow for the handling of all slot's sectors.
344The factor of min-write-sz is due to the behavior of flash hardware. The factor
345of 3 is explained below.
Fabio Utzig5bd4e582017-07-20 08:55:38 -0300346
Christopher Collinsa1c12042019-05-23 14:00:28 -07003472. Encryption keys: key-encrypting keys (KEKs). These keys are needed for
348 image encryption and decryption. See the
349 [encrypted images](encrypted_images.md) document for more information.
350
3513. Swap size: When beginning a new swap operation, the total size that needs
Håkon Øye Amundsencbf30472019-07-24 08:34:03 +0000352 to be swapped (based on the slot with largest image + TLVs) is written to
David Vincze2d736ad2019-02-18 11:50:22 +0100353 this location for easier recovery in case of a reset while performing the
354 swap.
Fabio Utzigea422c22017-09-11 11:02:47 -0300355
Håkon Øye Amundsencbf30472019-07-24 08:34:03 +00003564. Swap info: A single byte which encodes the following information:
David Vinczee2453472019-06-17 12:31:59 +0200357 - Swap type: Stored in bits 0-3. Indicating the type of swap operation in
358 progress. When mcuboot resumes an interrupted swap, it uses this field to
359 determine the type of operation to perform. This field contains one of the
360 following values in the table below.
361 - Image number: Stored in bits 4-7. It has always 0 value at single image
362 boot. In case of multi image boot it indicates, which image was swapped when
363 interrupt happened. The same scratch area is used during in case of all
364 image swap operation. Therefore this field is used to determine which image
365 the trailer belongs to if boot status is found on scratch area when the swap
366 operation is resumed.
Christopher Collinsa1c12042019-05-23 14:00:28 -0700367
368| Name | Value |
369| ------------------------- | ----- |
370| `BOOT_SWAP_TYPE_TEST` | 2 |
371| `BOOT_SWAP_TYPE_PERM` | 3 |
372| `BOOT_SWAP_TYPE_REVERT` | 4 |
373
374
3755. Copy done: A single byte indicating whether the image in this slot is
David Brown17e20d12017-09-12 11:53:20 -0600376 complete (0x01=done; 0xff=not done).
Fabio Utzig5bd4e582017-07-20 08:55:38 -0300377
Christopher Collinsa1c12042019-05-23 14:00:28 -07003786. Image OK: A single byte indicating whether the image in this slot has been
David Brown17e20d12017-09-12 11:53:20 -0600379 confirmed as good by the user (0x01=confirmed; 0xff=not confirmed).
Fabio Utzig5bd4e582017-07-20 08:55:38 -0300380
Christopher Collinsa1c12042019-05-23 14:00:28 -07003817. MAGIC: The following 16 bytes, written in host-byte-order:
Christopher Collins92ea77f2016-12-12 15:59:26 -0800382
David Brown17e20d12017-09-12 11:53:20 -0600383``` c
Christopher Collins92ea77f2016-12-12 15:59:26 -0800384 const uint32_t boot_img_magic[4] = {
385 0xf395c277,
386 0x7fefd260,
387 0x0f505235,
388 0x8079b62c,
389 };
David Brown17e20d12017-09-12 11:53:20 -0600390```
Christopher Collins92ea77f2016-12-12 15:59:26 -0800391
Fabio Utzigd37d8772019-12-03 10:32:18 -0300392## [IMAGE TRAILERS](#image-trailers)
Fabio Utzig86fe4b22017-07-28 18:56:29 -0300393
Marti Bolivar048d8d82017-08-04 17:14:24 -0400394At startup, the boot loader determines the boot swap type by inspecting the
395image trailers. When using the term "image trailers" what is meant is the
396aggregate information provided by both image slot's trailers.
Fabio Utzig86fe4b22017-07-28 18:56:29 -0300397
Fabio Utzigd37d8772019-12-03 10:32:18 -0300398### [New swaps (non-resumes)](#new-swaps-non-resumes)
Christopher Collinsa1c12042019-05-23 14:00:28 -0700399
400For new swaps, mcuboot must inspect a collection of fields to determine which
401swap operation to perform.
402
David Vincze2d736ad2019-02-18 11:50:22 +0100403The image trailers records are structured around the limitations imposed by
404flash hardware. As a consequence, they do not have a very intuitive design, and
405it is difficult to get a sense of the state of the device just by looking at the
Marti Bolivar048d8d82017-08-04 17:14:24 -0400406image trailers. It is better to map all the possible trailer states to the swap
407types described above via a set of tables. These tables are reproduced below.
Fabio Utzig86fe4b22017-07-28 18:56:29 -0300408
409Note: An important caveat about the tables described below is that they must
410be evaluated in the order presented here. Lower state numbers must have a
411higher priority when testing the image trailers.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800412
David Brown17e20d12017-09-12 11:53:20 -0600413```
Christopher Collins92ea77f2016-12-12 15:59:26 -0800414 State I
David Vincze2d736ad2019-02-18 11:50:22 +0100415 | primary slot | secondary slot |
416 -----------------+--------------+----------------|
417 magic | Any | Good |
418 image-ok | Any | Unset |
419 copy-done | Any | Any |
420 -----------------+--------------+----------------'
421 result: BOOT_SWAP_TYPE_TEST |
422 -------------------------------------------------'
Fabio Utzig5bd4e582017-07-20 08:55:38 -0300423
Christopher Collins92ea77f2016-12-12 15:59:26 -0800424
Fabio Utzig86fe4b22017-07-28 18:56:29 -0300425 State II
David Vincze2d736ad2019-02-18 11:50:22 +0100426 | primary slot | secondary slot |
427 -----------------+--------------+----------------|
428 magic | Any | Good |
429 image-ok | Any | 0x01 |
430 copy-done | Any | Any |
431 -----------------+--------------+----------------'
432 result: BOOT_SWAP_TYPE_PERM |
433 -------------------------------------------------'
Fabio Utzig5bd4e582017-07-20 08:55:38 -0300434
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -0800435
Fabio Utzig86fe4b22017-07-28 18:56:29 -0300436 State III
David Vincze2d736ad2019-02-18 11:50:22 +0100437 | primary slot | secondary slot |
438 -----------------+--------------+----------------|
439 magic | Good | Unset |
440 image-ok | 0xff | Any |
441 copy-done | 0x01 | Any |
442 -----------------+--------------+----------------'
443 result: BOOT_SWAP_TYPE_REVERT |
444 -------------------------------------------------'
David Brown17e20d12017-09-12 11:53:20 -0600445```
Christopher Collins92ea77f2016-12-12 15:59:26 -0800446
Marti Bolivar048d8d82017-08-04 17:14:24 -0400447Any of the above three states results in mcuboot attempting to swap images.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800448
Marti Bolivar048d8d82017-08-04 17:14:24 -0400449Otherwise, mcuboot does not attempt to swap images, resulting in one of the
450other three swap types, as illustrated by State IV.
Fabio Utzig86fe4b22017-07-28 18:56:29 -0300451
David Brown17e20d12017-09-12 11:53:20 -0600452```
Fabio Utzig86fe4b22017-07-28 18:56:29 -0300453 State IV
David Vincze2d736ad2019-02-18 11:50:22 +0100454 | primary slot | secondary slot |
455 -----------------+--------------+----------------|
456 magic | Any | Any |
457 image-ok | Any | Any |
458 copy-done | Any | Any |
459 -----------------+--------------+----------------'
460 result: BOOT_SWAP_TYPE_NONE, |
461 BOOT_SWAP_TYPE_FAIL, or |
462 BOOT_SWAP_TYPE_PANIC |
463 -------------------------------------------------'
David Brown17e20d12017-09-12 11:53:20 -0600464```
Christopher Collins92ea77f2016-12-12 15:59:26 -0800465
Marti Bolivar048d8d82017-08-04 17:14:24 -0400466In State IV, when no errors occur, mcuboot will attempt to boot the contents of
David Vincze2d736ad2019-02-18 11:50:22 +0100467the primary slot directly, and the result is `BOOT_SWAP_TYPE_NONE`. If the image
468in the primary slot is not valid, the result is `BOOT_SWAP_TYPE_FAIL`. If a
469fatal error occurs during boot, the result is `BOOT_SWAP_TYPE_PANIC`. If the
470result is either `BOOT_SWAP_TYPE_FAIL` or `BOOT_SWAP_TYPE_PANIC`, mcuboot hangs
471rather than booting an invalid or compromised image.
Fabio Utzig86fe4b22017-07-28 18:56:29 -0300472
Marti Bolivar048d8d82017-08-04 17:14:24 -0400473Note: An important caveat to the above is the result when a swap is requested
David Vincze2d736ad2019-02-18 11:50:22 +0100474 and the image in the secondary slot fails to validate, due to a hashing or
475 signing error. This state behaves as State IV with the extra action of
476 marking the image in the primary slot as "OK", to prevent further attempts
477 to swap.
Fabio Utzig86fe4b22017-07-28 18:56:29 -0300478
Fabio Utzigd37d8772019-12-03 10:32:18 -0300479### [Resumed swaps](#resumed-swaps)
Christopher Collinsa1c12042019-05-23 14:00:28 -0700480
481If mcuboot determines that it is resuming an interrupted swap (i.e., a reset
482occurred mid-swap), it fully determines the operation to resume by reading the
David Vinczee2453472019-06-17 12:31:59 +0200483`swap info` field from the active trailer and extracting the swap type from bits
4840-3. The set of tables in the previous section are not necessary in the resume
485case.
Christopher Collinsa1c12042019-05-23 14:00:28 -0700486
Fabio Utzigd37d8772019-12-03 10:32:18 -0300487## [High-Level Operation](#high-level-operation)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800488
489With the terms defined, we can now explore the boot loader's operation. First,
490a high-level overview of the boot process is presented. Then, the following
491sections describe each step of the process in more detail.
492
493Procedure:
494
David Brown17e20d12017-09-12 11:53:20 -06004951. Inspect swap status region; is an interrupted swap being resumed?
Fabio Utzig75b34412019-09-06 08:30:43 -0300496 + Yes: Complete the partial swap operation; skip to step 3.
497 + No: Proceed to step 2.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800498
David Brown17e20d12017-09-12 11:53:20 -06004992. Inspect image trailers; is a swap requested?
Fabio Utzig75b34412019-09-06 08:30:43 -0300500 + Yes:
501 1. Is the requested image valid (integrity and security check)?
502 + Yes.
503 a. Perform swap operation.
504 b. Persist completion of swap procedure to image trailers.
505 c. Proceed to step 3.
506 + No.
507 a. Erase invalid image.
508 b. Persist failure of swap procedure to image trailers.
509 c. Proceed to step 3.
510
511 + No: Proceed to step 3.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800512
David Vincze2d736ad2019-02-18 11:50:22 +01005133. Boot into image in primary slot.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800514
Fabio Utzigd37d8772019-12-03 10:32:18 -0300515### [Multiple Image Boot](#multiple-image-boot)
David Vinczeba3bd602019-06-17 16:01:43 +0200516
517When the flash contains multiple executable images the boot loader's operation
518is a bit more complex but similar to the previously described procedure with
519one image. Every image can be updated independently therefore the flash is
520partitioned further to arrange two slots for each image.
521```
522+--------------------+
523| MCUBoot |
524+--------------------+
525 ~~~~~ <- memory might be not contiguous
526+--------------------+
527| Image 0 |
528| primary slot |
529+--------------------+
530| Image 0 |
531| secondary slot |
532+--------------------+
533 ~~~~~ <- memory might be not contiguous
534+--------------------+
535| Image N |
536| primary slot |
537+--------------------+
538| Image N |
539| secondary slot |
540+--------------------+
541| Scratch |
542+--------------------+
543```
David Vinczee32483f2019-06-13 10:46:24 +0200544MCUBoot is also capable of handling dependencies between images. For example
545if an image needs to be reverted it might be necessary to revert another one too
546(e.g. due to API incompatibilities) or simply to prevent from being updated
547because of an unsatisfied dependency. Therefore all aborted swaps have to be
548completed and all the swap types have to be determined for each image before
549the dependency checks. Dependency handling is described in more detail in a
550following section. The multiple image boot procedure is organized in loops which
551iterate over all the firmware images. The high-level overview of the boot
552process is presented below.
David Vinczeba3bd602019-06-17 16:01:43 +0200553
554+ ###### Loop 1. Iterate over all images
555 1. Inspect swap status region of current image; is an interrupted swap being
556 resumed?
557 + Yes:
558 + Review the validity of previously determined swap types
559 of other images.
560 + Complete the partial swap operation.
561 + Mark the swap type as `None`.
562 + Skip to next image.
563 + No: Proceed to step 2.
564
565 2. Inspect image trailers in the primary and secondary slot; is an image
566 swap requested?
567 + Yes: Review the validity of previously determined swap types of other
568 images. Is the requested image valid (integrity and security
569 check)?
570 + Yes:
571 + Set the previously determined swap type for the current image.
572 + Skip to next image.
573 + No:
574 + Erase invalid image.
575 + Persist failure of swap procedure to image trailers.
576 + Mark the swap type as `Fail`.
577 + Skip to next image.
578 + No:
579 + Mark the swap type as `None`.
580 + Skip to next image.
581
582+ ###### Loop 2. Iterate over all images
David Vinczee32483f2019-06-13 10:46:24 +0200583 1. Does the current image depend on other image(s)?
584 + Yes: Are all the image dependencies satisfied?
585 + Yes: Skip to next image.
586 + No:
587 + Modify swap type depending on what the previous type was.
588 + Restart dependency check from the first image.
589 + No: Skip to next image.
David Vinczeba3bd602019-06-17 16:01:43 +0200590
David Vinczee32483f2019-06-13 10:46:24 +0200591+ ###### Loop 3. Iterate over all images
David Vinczeba3bd602019-06-17 16:01:43 +0200592 1. Is an image swap requested?
593 + Yes:
594 + Perform image update operation.
595 + Persist completion of swap procedure to image trailers.
596 + Skip to next image.
597 + No: Skip to next image.
598
David Vinczee32483f2019-06-13 10:46:24 +0200599+ ###### Loop 4. Iterate over all images
David Vinczeba3bd602019-06-17 16:01:43 +0200600 1. Validate image in the primary slot (integrity and security check) or
601 at least do a basic sanity check to avoid booting into an empty flash
602 area.
603
604+ Boot into image in the primary slot of the 0th image position\
605 (other image in the boot chain is started by another image).
606
Fabio Utzigd37d8772019-12-03 10:32:18 -0300607## [Image Swapping](#image-swapping)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800608
609The boot loader swaps the contents of the two image slots for two reasons:
Fabio Utzig75b34412019-09-06 08:30:43 -0300610
611 * User has issued a "set pending" operation; the image in the secondary slot
Håkon Øye Amundsen11d91c32020-03-04 08:49:47 +0000612 should be run once (state I) or repeatedly (state II), depending on
Fabio Utzig75b34412019-09-06 08:30:43 -0300613 whether a permanent swap was specified.
614 * Test image rebooted without being confirmed; the boot loader should
Håkon Øye Amundsen11d91c32020-03-04 08:49:47 +0000615 revert to the original image currently in the secondary slot (state III).
Christopher Collins92ea77f2016-12-12 15:59:26 -0800616
Fabio Utzig86fe4b22017-07-28 18:56:29 -0300617If the image trailers indicates that the image in the secondary slot should be
Christopher Collins92ea77f2016-12-12 15:59:26 -0800618run, the boot loader needs to copy it to the primary slot. The image currently
619in the primary slot also needs to be retained in flash so that it can be used
620later. Furthermore, both images need to be recoverable if the boot loader
621resets in the middle of the swap operation. The two images are swapped
622according to the following procedure:
623
Fabio Utzig60319ac2019-09-06 08:29:50 -03006241. Determine if both slots are compatible enough to have their images swapped.
625 To be compatible, both have to have only sectors that can fit into the
626 scratch area and if one of them has larger sectors than the other, it must
627 be able to entirely fit some rounded number of sectors from the other slot.
Fabio Utzigc28005b2019-09-10 12:18:29 -0300628 In the next steps we'll use the terminology "region" for the total amount of
629 data copied/erased because this can be any amount of sectors depending on
630 how many the scratch is able to fit for some swap operation.
6312. Iterate the list of region indices in descending order (i.e., starting
632 with the greatest index); only regions that are predetermined to be part of
Fabio Utzig60319ac2019-09-06 08:29:50 -0300633 the image are copied; current element = "index".
634 + a. Erase scratch area.
635 + b. Copy secondary_slot[index] to scratch area.
Fabio Utzigc28005b2019-09-10 12:18:29 -0300636 - If this is the last region in the slot, scratch area has a temporary
Fabio Utzig60319ac2019-09-06 08:29:50 -0300637 status area initialized to store the initial state, because the
Fabio Utzigc28005b2019-09-10 12:18:29 -0300638 primary slot's last region will have to be erased. In this case,
Fabio Utzig60319ac2019-09-06 08:29:50 -0300639 only the data that was calculated to amount to the image is copied.
Fabio Utzigc28005b2019-09-10 12:18:29 -0300640 - Else if this is the first swapped region but not the last region in
Fabio Utzig60319ac2019-09-06 08:29:50 -0300641 the slot, initialize the status area in primary slot and copy the
Fabio Utzigc28005b2019-09-10 12:18:29 -0300642 full region contents.
643 - Else, copy entire region contents.
Fabio Utzig60319ac2019-09-06 08:29:50 -0300644 + c. Write updated swap status (i).
645 + d. Erase secondary_slot[index]
646 + e. Copy primary_slot[index] to secondary_slot[index] according to amount
647 previosly copied at step b.
Fabio Utzigc28005b2019-09-10 12:18:29 -0300648 - If this is not the last region in the slot, erase the trailer in the
Fabio Utzig60319ac2019-09-06 08:29:50 -0300649 secondary slot, to always use the one in the primary slot.
650 + f. Write updated swap status (ii).
651 + g. Erase primary_slot[index].
652 + h. Copy scratch area to primary_slot[index] according to amount
653 previously copied at step b.
Fabio Utzigc28005b2019-09-10 12:18:29 -0300654 - If this is the last region in the slot, the status is read from
Fabio Utzig60319ac2019-09-06 08:29:50 -0300655 scratch (where it was stored temporarily) and written anew in the
656 primary slot.
657 + i. Write updated swap status (iii).
6583. Persist completion of swap procedure to the primary slot image trailer.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800659
David Vincze2d736ad2019-02-18 11:50:22 +0100660The additional caveats in step 2f are necessary so that the secondary slot image
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -0800661trailer can be written by the user at a later time. With the image trailer
David Vincze2d736ad2019-02-18 11:50:22 +0100662unwritten, the user can test the image in the secondary slot
Håkon Øye Amundsen11d91c32020-03-04 08:49:47 +0000663(i.e., transition to state I).
Christopher Collins92ea77f2016-12-12 15:59:26 -0800664
Fabio Utzigc28005b2019-09-10 12:18:29 -0300665Note1: If the region being copied contains the last sector, then swap status is
Fabio Utzig5bd4e582017-07-20 08:55:38 -0300666temporarily maintained on scratch for the duration of this operation, always
David Vincze2d736ad2019-02-18 11:50:22 +0100667using the primary slot's area otherwise.
Fabio Utzig5bd4e582017-07-20 08:55:38 -0300668
669Note2: The bootloader tries to copy only used sectors (based on largest image
670installed on any of the slots), minimizing the amount of sectors copied and
671reducing the amount of time required for a swap operation.
672
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -0800673The particulars of step 3 vary depending on whether an image is being tested,
David Vincze2d736ad2019-02-18 11:50:22 +0100674permanently used, reverted or a validation failure of the secondary slot
675happened when a swap was requested:
Fabio Utzig86fe4b22017-07-28 18:56:29 -0300676
Christopher Collins92ea77f2016-12-12 15:59:26 -0800677 * test:
David Vincze2d736ad2019-02-18 11:50:22 +0100678 o Write primary_slot.copy_done = 1
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -0800679 (swap caused the following values to be written:
David Vincze2d736ad2019-02-18 11:50:22 +0100680 primary_slot.magic = BOOT_MAGIC
Håkon Øye Amundsencdf94c22020-03-04 08:52:31 +0000681 secondary_slot.magic = UNSET
David Vincze2d736ad2019-02-18 11:50:22 +0100682 primary_slot.image_ok = Unset)
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -0800683
684 * permanent:
David Vincze2d736ad2019-02-18 11:50:22 +0100685 o Write primary_slot.copy_done = 1
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -0800686 (swap caused the following values to be written:
David Vincze2d736ad2019-02-18 11:50:22 +0100687 primary_slot.magic = BOOT_MAGIC
Håkon Øye Amundsencdf94c22020-03-04 08:52:31 +0000688 secondary_slot.magic = UNSET
David Vincze2d736ad2019-02-18 11:50:22 +0100689 primary_slot.image_ok = 0x01)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800690
691 * revert:
David Vincze2d736ad2019-02-18 11:50:22 +0100692 o Write primary_slot.copy_done = 1
693 o Write primary_slot.image_ok = 1
Fabio Utzig86fe4b22017-07-28 18:56:29 -0300694 (swap caused the following values to be written:
David Vincze2d736ad2019-02-18 11:50:22 +0100695 primary_slot.magic = BOOT_MAGIC)
Fabio Utzig86fe4b22017-07-28 18:56:29 -0300696
David Vincze2d736ad2019-02-18 11:50:22 +0100697 * failure to validate the secondary slot:
698 o Write primary_slot.image_ok = 1
Fabio Utzig86fe4b22017-07-28 18:56:29 -0300699
David Vincze2d736ad2019-02-18 11:50:22 +0100700After completing the operations as described above the image in the primary slot
701should be booted.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800702
Fabio Utzigd37d8772019-12-03 10:32:18 -0300703## [Swap Status](#swap-status)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800704
705The swap status region allows the boot loader to recover in case it restarts in
706the middle of an image swap operation. The swap status region consists of a
707series of single-byte records. These records are written independently, and
708therefore must be padded according to the minimum write size imposed by the
709flash hardware. In the below figure, a min-write-size of 1 is assumed for
710simplicity. The structure of the swap status region is illustrated below. In
711this figure, a min-write-size of 1 is assumed for simplicity.
712
David Brown17e20d12017-09-12 11:53:20 -0600713```
Christopher Collins92ea77f2016-12-12 15:59:26 -0800714 0 1 2 3
715 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
716 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
717 |sec127,state 0 |sec127,state 1 |sec127,state 2 |sec126,state 0 |
718 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
719 |sec126,state 1 |sec126,state 2 |sec125,state 0 |sec125,state 1 |
720 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
721 |sec125,state 2 | |
722 +-+-+-+-+-+-+-+-+ +
723 ~ ~
724 ~ [Records for indices 124 through 1 ~
725 ~ ~
726 ~ +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
727 ~ |sec000,state 0 |sec000,state 1 |sec000,state 2 |
728 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
David Brown17e20d12017-09-12 11:53:20 -0600729```
Christopher Collins92ea77f2016-12-12 15:59:26 -0800730
731The above is probably not helpful at all; here is a description in English.
732
733Each image slot is partitioned into a sequence of flash sectors. If we were to
734enumerate the sectors in a single slot, starting at 0, we would have a list of
735sector indices. Since there are two image slots, each sector index would
736correspond to a pair of sectors. For example, sector index 0 corresponds to
David Vincze2d736ad2019-02-18 11:50:22 +0100737the first sector in the primary slot and the first sector in the secondary slot.
738Finally, reverse the list of indices such that the list starts with index
739`BOOT_MAX_IMG_SECTORS - 1` and ends with 0. The swap status region is a
740representation of this reversed list.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800741
742During a swap operation, each sector index transitions through four separate
743states:
David Brown17e20d12017-09-12 11:53:20 -0600744```
David Vincze2d736ad2019-02-18 11:50:22 +01007450. primary slot: image 0, secondary slot: image 1, scratch: N/A
7461. primary slot: image 0, secondary slot: N/A, scratch: image 1 (1->s, erase 1)
7472. primary slot: N/A, secondary slot: image 0, scratch: image 1 (0->1, erase 0)
7483. primary slot: image 1, secondary slot: image 0, scratch: N/A (s->0)
David Brown17e20d12017-09-12 11:53:20 -0600749```
Christopher Collins92ea77f2016-12-12 15:59:26 -0800750
751Each time a sector index transitions to a new state, the boot loader writes a
752record to the swap status region. Logically, the boot loader only needs one
753record per sector index to keep track of the current swap state. However, due
754to limitations imposed by flash hardware, a record cannot be overwritten when
755an index's state changes. To solve this problem, the boot loader uses three
756records per sector index rather than just one.
757
758Each sector-state pair is represented as a set of three records. The record
759values map to the above four states as follows
760
David Brown17e20d12017-09-12 11:53:20 -0600761```
Christopher Collins92ea77f2016-12-12 15:59:26 -0800762 | rec0 | rec1 | rec2
763 --------+------+------+------
764 state 0 | 0xff | 0xff | 0xff
765 state 1 | 0x01 | 0xff | 0xff
766 state 2 | 0x01 | 0x02 | 0xff
767 state 3 | 0x01 | 0x02 | 0x03
David Brown17e20d12017-09-12 11:53:20 -0600768```
Christopher Collins92ea77f2016-12-12 15:59:26 -0800769
Fabio Utzig2c05f1b2018-04-04 10:35:17 -0300770The swap status region can accommodate `BOOT_MAX_IMG_SECTORS` sector indices.
David Vincze2d736ad2019-02-18 11:50:22 +0100771Hence, the size of the region, in bytes, is
772`BOOT_MAX_IMG_SECTORS * min-write-size * 3`. The only requirement for the index
773count is that it is great enough to account for a maximum-sized image
774(i.e., at least as great as the total sector count in an image slot). If a
775device's image slots have been configured with `BOOT_MAX_IMG_SECTORS: 128` and
776use less than 128 sectors, the first record that gets written will be somewhere
777in the middle of the region. For example, if a slot uses 64 sectors, the first
778sector index that gets swapped is 63, which corresponds to the exact halfway
779point within the region.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800780
Fabio Utzig5bd4e582017-07-20 08:55:38 -0300781Note: since the scratch area only ever needs to record swapping of the last
782sector, it uses at most min-write-size * 3 bytes for its own status area.
783
Fabio Utzigd37d8772019-12-03 10:32:18 -0300784## [Reset Recovery](#reset-recovery)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800785
786If the boot loader resets in the middle of a swap operation, the two images may
787be discontiguous in flash. Bootutil recovers from this condition by using the
Fabio Utzig86fe4b22017-07-28 18:56:29 -0300788image trailers to determine how the image parts are distributed in flash.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800789
790The first step is determine where the relevant swap status region is located.
791Because this region is embedded within the image slots, its location in flash
Fabio Utzig86fe4b22017-07-28 18:56:29 -0300792changes during a swap operation. The below set of tables map image trailers
Christopher Collins92ea77f2016-12-12 15:59:26 -0800793contents to swap status location. In these tables, the "source" field
David Vinczeba3bd602019-06-17 16:01:43 +0200794indicates where the swap status region is located. In case of multi image boot
795the images primary area and the single scratch area is always examined in pairs.
796If swap status found on scratch area then it might not belong to the current
797image. The swap_info field of swap status stores the corresponding image number.
798If it does not match then "source: none" is returned.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800799
David Brown17e20d12017-09-12 11:53:20 -0600800```
David Vincze2d736ad2019-02-18 11:50:22 +0100801 | primary slot | scratch |
802 ----------+--------------+--------------|
803 magic | Good | Any |
804 copy-done | 0x01 | N/A |
805 ----------+--------------+--------------'
806 source: none |
807 ----------------------------------------'
Marti Bolivar49b29172017-08-04 14:50:51 -0400808
David Vincze2d736ad2019-02-18 11:50:22 +0100809 | primary slot | scratch |
810 ----------+--------------+--------------|
811 magic | Good | Any |
812 copy-done | 0xff | N/A |
813 ----------+--------------+--------------'
814 source: primary slot |
815 ----------------------------------------'
Marti Bolivar49b29172017-08-04 14:50:51 -0400816
David Vincze2d736ad2019-02-18 11:50:22 +0100817 | primary slot | scratch |
818 ----------+--------------+--------------|
819 magic | Any | Good |
820 copy-done | Any | N/A |
821 ----------+--------------+--------------'
822 source: scratch |
823 ----------------------------------------'
Marti Bolivar49b29172017-08-04 14:50:51 -0400824
David Vincze2d736ad2019-02-18 11:50:22 +0100825 | primary slot | scratch |
826 ----------+--------------+--------------|
827 magic | Unset | Any |
828 copy-done | 0xff | N/A |
829 ----------+--------------+--------------|
830 source: primary slot |
831 ----------------------------------------+------------------------------+
832 This represents one of two cases: |
833 o No swaps ever (no status to read, so no harm in checking). |
834 o Mid-revert; status in the primary slot. |
835 For this reason we assume the primary slot as source, to trigger a |
836 check of the status area and find out if there was swapping under way. |
837 -----------------------------------------------------------------------'
David Brown17e20d12017-09-12 11:53:20 -0600838```
Christopher Collins92ea77f2016-12-12 15:59:26 -0800839
Christopher Collinsa1c12042019-05-23 14:00:28 -0700840If the swap status region indicates that the images are not contiguous, mcuboot
841determines the type of swap operation that was interrupted by reading the `swap
Håkon Øye Amundsencbf30472019-07-24 08:34:03 +0000842info` field in the active image trailer and extracting the swap type from bits
David Vinczee2453472019-06-17 12:31:59 +02008430-3 then resumes the operation. In other words, it applies the procedure defined
844in the previous section, moving image 1 into the primary slot and image 0 into
845the secondary slot. If the boot status indicates that an image part is present
846in the scratch area, this part is copied into the correct location by starting
847at step e or step h in the area-swap procedure, depending on whether the part
848belongs to image 0 or image 1.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800849
850After the swap operation has been completed, the boot loader proceeds as though
851it had just been started.
852
Fabio Utzigd37d8772019-12-03 10:32:18 -0300853## [Integrity Check](#integrity-check)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800854
855An image is checked for integrity immediately before it gets copied into the
Fabio Utzig5bd4e582017-07-20 08:55:38 -0300856primary slot. If the boot loader doesn't perform an image swap, then it can
David Vincze2d736ad2019-02-18 11:50:22 +0100857perform an optional integrity check of the image in the primary slot if
858`MCUBOOT_VALIDATE_PRIMARY_SLOT` is set, otherwise it doesn't perform an
859integrity check.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800860
861During the integrity check, the boot loader verifies the following aspects of
862an image:
Fabio Utzig75b34412019-09-06 08:30:43 -0300863
Fabio Utzigfd140ec2019-09-12 14:37:48 -0300864 * 32-bit magic number must be correct (`IMAGE_MAGIC`).
Fabio Utzig75b34412019-09-06 08:30:43 -0300865 * Image must contain an `image_tlv_info` struct, identified by its magic
Fabio Utzigfd140ec2019-09-12 14:37:48 -0300866 (`IMAGE_TLV_PROT_INFO_MAGIC` or `IMAGE_TLV_INFO_MAGIC`) exactly following
867 the firmware (`hdr_size` + `img_size`). If `IMAGE_TLV_PROT_INFO_MAGIC` is
868 found then after `ih_protect_tlv_size` bytes, another `image_tlv_info`
869 with magic equal to `IMAGE_TLV_INFO_MAGIC` must be present.
Fabio Utzig75b34412019-09-06 08:30:43 -0300870 * Image must contain a SHA256 TLV.
871 * Calculated SHA256 must match SHA256 TLV contents.
872 * Image *may* contain a signature TLV. If it does, it must also have a
873 KEYHASH TLV with the hash of the key that was used to sign. The list of
874 keys will then be iterated over looking for the matching key, which then
875 will then be used to verify the image contents.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800876
Fabio Utzigd37d8772019-12-03 10:32:18 -0300877## [Security](#security)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800878
879As indicated above, the final step of the integrity check is signature
880verification. The boot loader can have one or more public keys embedded in it
881at build time. During signature verification, the boot loader verifies that an
Håkon Øye Amundsencbf30472019-07-24 08:34:03 +0000882image was signed with a private key that corresponds to the embedded KEYHASH
Fabio Utzigea422c22017-09-11 11:02:47 -0300883TLV.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800884
885For information on embedding public keys in the boot loader, as well as
Fabio Utzig4dce6aa2018-02-12 15:31:32 -0200886producing signed images, see: [signed_images](signed_images.md).
Fabio Utzigcdfa11a2018-10-01 09:45:54 -0300887
888If you want to enable and use encrypted images, see:
889[encrypted_images](encrypted_images.md).
David Vinczee32483f2019-06-13 10:46:24 +0200890
Fabio Utzigd37d8772019-12-03 10:32:18 -0300891## [Protected TLVs](#protected-tlvs)
Fabio Utzigfd140ec2019-09-12 14:37:48 -0300892
893If the TLV area contains protected TLV entries, by beginning with a `struct
894image_tlv_info` with a magic value of `IMAGE_TLV_PROT_INFO_MAGIC` then the
895data of those TLVs must also be integrity and authenticity protected. Beyond
896the full size of the protected TLVs being stored in the `image_tlv_info`,
897the size of the protected TLVs together with the size of the `image_tlv_info`
898struct itself are also saved in the `ih_protected_size` field inside the
899header.
900
901Whenever an image has protected TLVs the SHA256 has to be calculated over
902not just the image header and the image but also the TLV info header and the
903protected TLVs.
904
905```
906A +---------------------+
907 | Header | <- struct image_header
908 +---------------------+
909 | Payload |
910 +---------------------+
911 | TLV area |
912 | +-----------------+ | struct image_tlv_info with
913 | | TLV area header | | <- IMAGE_TLV_PROT_INFO_MAGIC (optional)
914 | +-----------------+ |
915 | | Protected TLVs | | <- Protected TLVs (struct image_tlv)
916B | +-----------------+ |
917 | | TLV area header | | <- struct image_tlv_info with IMAGE_TLV_INFO_MAGIC
918C | +-----------------+ |
919 | | SHA256 hash | | <- hash from A - B (struct image_tlv)
920D | +-----------------+ |
921 | | Keyhash | | <- indicates which pub. key for sig (struct image_tlv)
922 | +-----------------+ |
923 | | Signature | | <- signature from C - D (struct image_tlv), only hash
924 | +-----------------+ |
925 +---------------------+
926```
927
Fabio Utzigd37d8772019-12-03 10:32:18 -0300928## [Dependency Check](#dependency-check)
David Vinczee32483f2019-06-13 10:46:24 +0200929
930MCUBoot can handle multiple firmware images. It is possible to update them
931independently but in many cases it can be desired to be able to describe
932dependencies between the images (e.g. to ensure API compliance and avoid
933interoperability issues).
934
935The dependencies between images can be described with additional TLV entries in
Fabio Utzigfd140ec2019-09-12 14:37:48 -0300936the protected TLV area after the end of an image. There can be more than one
937dependency entry, but in practice if the platform only supports two individual
938images then there can be maximum one entry which reflects to the other image.
David Vinczee32483f2019-06-13 10:46:24 +0200939
David Vinczee32483f2019-06-13 10:46:24 +0200940At the phase of dependency check all aborted swaps are finalized if there were
941any. During the dependency check the boot loader verifies whether the image
942dependencies are all satisfied. If at least one of the dependencies of an image
943is not fulfilled then the swap type of that image has to be modified
944accordingly and the dependency check needs to be restarted. This way the number
945of unsatisfied dependencies will decrease or remain the same. There is always at
946least 1 valid configuration. In worst case, the system returns to the initial
947state after dependency check.
948
949For more information on adding dependency entries to an image,
950see: [imgtool](imgtool.md).
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000951
952## [Downgrade Prevention](#downgrade-prevention)
953
954Downgrade prevention is a feature which enforces that the new image must have a
David Vinczec3084132020-02-18 14:50:47 +0100955higher version/security counter number than the image it is replacing, thus
956preventing the malicious downgrading of the device to an older and possibly
957vulnerable version of its firmware.
958
959### [SW Based Downgrade Prevention](#sw-downgrade-prevention)
960
961During the software based downgrade prevention the image version numbers are
962compared. This feature is enabled with the `MCUBOOT_DOWNGRADE_PREVENTION`
963option. In this case downgrade prevention is only available when the
964overwrite-based image update strategy is used (i.e. `MCUBOOT_OVERWRITE_ONLY`
965is set).
966
967### [HW Based Downgrade Prevention](#hw-downgrade-prevention)
968
969Each signed image can contain a security counter in its protected TLV area.
970During the hardware based downgrade prevention (alias rollback protection) the
971new image's security counter will be compared with the currently active security
972counter value which must be stored in a non-volatile and trusted component of
973the device. This feature is enabled with the `MCUBOOT_HW_ROLLBACK_PROT` option.
974It is beneficial to handle this counter independently from image version
975number:
976
977 * It does not need to increase with each software release,
978 * It makes it possible to do software downgrade to some extent: if the
979 security counter has the same value in the older image then it is accepted.