blob: 697243a89bf93ef49e0d5564cfbcd0fd19e11adb [file] [log] [blame] [view]
David Brown17e20d12017-09-12 11:53:20 -06001<!--
David Brownaac71112020-02-03 16:13:42 -07002 - SPDX-License-Identifier: Apache-2.0
David Brown17e20d12017-09-12 11:53:20 -06003
David Vinczee574f2d2020-07-10 11:42:03 +02004 - Copyright (c) 2017-2020 Linaro LTD
David Brownaac71112020-02-03 16:13:42 -07005 - Copyright (c) 2017-2019 JUUL Labs
6 - Copyright (c) 2019-2020 Arm Limited
7
8 - Original license:
9
10 - Licensed to the Apache Software Foundation (ASF) under one
11 - or more contributor license agreements. See the NOTICE file
12 - distributed with this work for additional information
13 - regarding copyright ownership. The ASF licenses this file
14 - to you under the Apache License, Version 2.0 (the
15 - "License"); you may not use this file except in compliance
16 - with the License. You may obtain a copy of the License at
17
18 - http://www.apache.org/licenses/LICENSE-2.0
19
20 - Unless required by applicable law or agreed to in writing,
21 - software distributed under the License is distributed on an
22 - "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
23 - KIND, either express or implied. See the License for the
24 - specific language governing permissions and limitations
25 - under the License.
David Vinczeba3bd602019-06-17 16:01:43 +020026-->
27
David Brown17e20d12017-09-12 11:53:20 -060028# Boot Loader
29
Fabio Utzigd37d8772019-12-03 10:32:18 -030030## [Summary](#summary)
Christopher Collins92ea77f2016-12-12 15:59:26 -080031
Fabio Utzigac834962017-07-20 13:20:48 -030032mcuboot comprises two packages:
Christopher Collins92ea77f2016-12-12 15:59:26 -080033
David Brown17e20d12017-09-12 11:53:20 -060034* The bootutil library (boot/bootutil)
35* The boot application (each port has its own at boot/<port>)
Christopher Collins92ea77f2016-12-12 15:59:26 -080036
37The bootutil library performs most of the functions of a boot loader. In
38particular, the piece that is missing is the final step of actually jumping to
39the main image. This last step is instead implemented by the boot application.
40Boot loader functionality is separated in this manner to enable unit testing of
41the boot loader. A library can be unit tested, but an application can't.
42Therefore, functionality is delegated to the bootutil library when possible.
43
Fabio Utzigd37d8772019-12-03 10:32:18 -030044## [Limitations](#limitations)
Christopher Collins92ea77f2016-12-12 15:59:26 -080045
46The boot loader currently only supports images with the following
47characteristics:
David Brown17e20d12017-09-12 11:53:20 -060048* Built to run from flash.
49* Built to run from a fixed location (i.e., not position-independent).
Christopher Collins92ea77f2016-12-12 15:59:26 -080050
Fabio Utzigd37d8772019-12-03 10:32:18 -030051## [Image Format](#image-format)
Christopher Collins92ea77f2016-12-12 15:59:26 -080052
53The following definitions describe the image format.
54
David Brown17e20d12017-09-12 11:53:20 -060055``` c
Fabio Utzigea422c22017-09-11 11:02:47 -030056#define IMAGE_MAGIC 0x96f3b83d
Christopher Collins92ea77f2016-12-12 15:59:26 -080057
58#define IMAGE_HEADER_SIZE 32
59
60struct image_version {
61 uint8_t iv_major;
62 uint8_t iv_minor;
63 uint16_t iv_revision;
64 uint32_t iv_build_num;
65};
66
67/** Image header. All fields are in little endian byte order. */
68struct image_header {
69 uint32_t ih_magic;
Fabio Utzigea422c22017-09-11 11:02:47 -030070 uint32_t ih_load_addr;
David Vinczee32483f2019-06-13 10:46:24 +020071 uint16_t ih_hdr_size; /* Size of image header (bytes). */
72 uint16_t ih_protect_tlv_size; /* Size of protected TLV area (bytes). */
73 uint32_t ih_img_size; /* Does not include header. */
74 uint32_t ih_flags; /* IMAGE_F_[...]. */
Christopher Collins92ea77f2016-12-12 15:59:26 -080075 struct image_version ih_ver;
David Vinczee32483f2019-06-13 10:46:24 +020076 uint32_t _pad1;
Christopher Collins92ea77f2016-12-12 15:59:26 -080077};
78
Fabio Utzigfd140ec2019-09-12 14:37:48 -030079#define IMAGE_TLV_INFO_MAGIC 0x6907
80#define IMAGE_TLV_PROT_INFO_MAGIC 0x6908
81
Fabio Utzigea422c22017-09-11 11:02:47 -030082/** Image TLV header. All fields in little endian. */
83struct image_tlv_info {
84 uint16_t it_magic;
85 uint16_t it_tlv_tot; /* size of TLV area (including tlv_info header) */
86};
87
Christopher Collins92ea77f2016-12-12 15:59:26 -080088/** Image trailer TLV format. All fields in little endian. */
89struct image_tlv {
90 uint8_t it_type; /* IMAGE_TLV_[...]. */
91 uint8_t _pad;
Marti Bolivar49b29172017-08-04 14:50:51 -040092 uint16_t it_len; /* Data length (not including TLV header). */
Christopher Collins92ea77f2016-12-12 15:59:26 -080093};
94
95/*
96 * Image header flags.
97 */
Marti Bolivar7c057e92017-08-04 14:46:39 -040098#define IMAGE_F_PIC 0x00000001 /* Not supported. */
Marti Bolivar7c057e92017-08-04 14:46:39 -040099#define IMAGE_F_NON_BOOTABLE 0x00000010 /* Split image app. */
Fabio Utzigea422c22017-09-11 11:02:47 -0300100#define IMAGE_F_RAM_LOAD 0x00000020
Christopher Collins92ea77f2016-12-12 15:59:26 -0800101
102/*
103 * Image trailer TLV types.
104 */
Fabio Utzigea422c22017-09-11 11:02:47 -0300105#define IMAGE_TLV_KEYHASH 0x01 /* hash of the public key */
David Brown27648b82017-08-31 10:40:29 -0600106#define IMAGE_TLV_SHA256 0x10 /* SHA256 of image hdr and body */
Marko Kiiskila8dd56f32017-08-22 21:40:49 -0700107#define IMAGE_TLV_RSA2048_PSS 0x20 /* RSA2048 of hash output */
David Brown27648b82017-08-31 10:40:29 -0600108#define IMAGE_TLV_ECDSA224 0x21 /* ECDSA of hash output */
109#define IMAGE_TLV_ECDSA256 0x22 /* ECDSA of hash output */
Fabio Utzig3501c012019-05-13 15:07:25 -0700110#define IMAGE_TLV_RSA3072_PSS 0x23 /* RSA3072 of hash output */
Fabio Utzig195411f2019-06-28 07:48:21 -0300111#define IMAGE_TLV_ED25519 0x24 /* ED25519 of hash output */
David Vinczee32483f2019-06-13 10:46:24 +0200112#define IMAGE_TLV_ENC_RSA2048 0x30 /* Key encrypted with RSA-OAEP-2048 */
113#define IMAGE_TLV_ENC_KW128 0x31 /* Key encrypted with AES-KW-128 */
Fabio Utzig5eaa5762020-04-02 13:30:43 -0300114#define IMAGE_TLV_ENC_EC256 0x32 /* Key encrypted with ECIES-P256 */
115#define IMAGE_TLV_ENC_X25519 0x33 /* Key encrypted with ECIES-X25519 */
David Vinczee32483f2019-06-13 10:46:24 +0200116#define IMAGE_TLV_DEPENDENCY 0x40 /* Image depends on other image */
David Vinczec3084132020-02-18 14:50:47 +0100117#define IMAGE_TLV_SEC_CNT 0x50 /* security counter */
David Brown17e20d12017-09-12 11:53:20 -0600118```
Christopher Collins92ea77f2016-12-12 15:59:26 -0800119
120Optional type-length-value records (TLVs) containing image metadata are placed
121after the end of the image.
122
David Vinczee32483f2019-06-13 10:46:24 +0200123The `ih_protect_tlv_size` field indicates the length of the protected TLV area.
Fabio Utzigfd140ec2019-09-12 14:37:48 -0300124If protected TLVs are present then a TLV info header with magic equal to
125`IMAGE_TLV_PROT_INFO_MAGIC` must be present and the protected TLVs (plus the
126info header itself) have to be included in the hash calculation. Otherwise the
127hash is only calculated over the image header and the image itself. In this
David Vinczee32483f2019-06-13 10:46:24 +0200128case the value of the `ih_protect_tlv_size` field is 0.
129
David Brown17e20d12017-09-12 11:53:20 -0600130The `ih_hdr_size` field indicates the length of the header, and therefore the
Christopher Collins92ea77f2016-12-12 15:59:26 -0800131offset of the image itself. This field provides for backwards compatibility in
132case of changes to the format of the image header.
133
Fabio Utzigd37d8772019-12-03 10:32:18 -0300134## [Flash Map](#flash-map)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800135
Fabio Utzigac834962017-07-20 13:20:48 -0300136A device's flash is partitioned according to its _flash map_. At a high
Christopher Collins92ea77f2016-12-12 15:59:26 -0800137level, the flash map maps numeric IDs to _flash areas_. A flash area is a
138region of disk with the following properties:
David Brown17e20d12017-09-12 11:53:20 -06001391. An area can be fully erased without affecting any other areas.
1402. A write to one area does not restrict writes to other areas.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800141
Marti Bolivar4e64d562017-08-04 14:53:33 -0400142The boot loader uses the following flash area IDs:
David Vinczeb75c12a2019-03-22 14:58:33 +0100143```c
144/* Independent from multiple image boot */
David Vincze2d736ad2019-02-18 11:50:22 +0100145#define FLASH_AREA_BOOTLOADER 0
David Vinczeb75c12a2019-03-22 14:58:33 +0100146#define FLASH_AREA_IMAGE_SCRATCH 3
147```
148```c
149/* If the boot loader is working with the first image */
David Vincze2d736ad2019-02-18 11:50:22 +0100150#define FLASH_AREA_IMAGE_PRIMARY 1
151#define FLASH_AREA_IMAGE_SECONDARY 2
David Vinczeb75c12a2019-03-22 14:58:33 +0100152```
153```c
154/* If the boot loader is working with the second image */
155#define FLASH_AREA_IMAGE_PRIMARY 5
156#define FLASH_AREA_IMAGE_SECONDARY 6
David Brown17e20d12017-09-12 11:53:20 -0600157```
Christopher Collins92ea77f2016-12-12 15:59:26 -0800158
Marti Bolivar4e64d562017-08-04 14:53:33 -0400159The bootloader area contains the bootloader image itself. The other areas are
David Vinczeb75c12a2019-03-22 14:58:33 +0100160described in subsequent sections. The flash could contain multiple executable
161images therefore the flash area IDs of primary and secondary areas are mapped
162based on the number of the active image (on which the bootloader is currently
163working).
Marti Bolivar4e64d562017-08-04 14:53:33 -0400164
Fabio Utzigd37d8772019-12-03 10:32:18 -0300165## [Image Slots](#image-slots)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800166
David Vinczeb75c12a2019-03-22 14:58:33 +0100167A portion of the flash memory can be partitioned into multiple image areas, each
168contains two image slots: a primary slot and a secondary slot.
David Vinczee574f2d2020-07-10 11:42:03 +0200169Normally, the boot loader will only run an image from the primary slot, so
170images must be built such that they can run from that fixed location in flash
171(the exception to this is the [direct-xip](#direct-xip) upgrade mode). If the
172boot loader needs to run the image resident in the secondary slot, it must copy
173its contents into the primary slot before doing so, either by swapping the two
David Vinczeb75c12a2019-03-22 14:58:33 +0100174images or by overwriting the contents of the primary slot. The bootloader
175supports either swap- or overwrite-based image upgrades, but must be configured
176at build time to choose one of these two strategies.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800177
David Vinczeb75c12a2019-03-22 14:58:33 +0100178In addition to the slots of image areas, the boot loader requires a scratch
179area to allow for reliable image swapping. The scratch area must have a size
180that is enough to store at least the largest sector that is going to be swapped.
181Many devices have small equally sized flash sectors, eg 4K, while others have
David Vincze2d736ad2019-02-18 11:50:22 +0100182variable sized sectors where the largest sectors might be 128K or 256K, so the
183scratch must be big enough to store that. The scratch is only ever used when
184swapping firmware, which means only when doing an upgrade. Given that, the main
185reason for using a larger size for the scratch is that flash wear will be more
186evenly distributed, because a single sector would be written twice the number of
187times than using two sectors, for example. To evaluate the ideal size of the
188scratch for your use case the following parameters are relevant:
Fabio Utziga722f5a2017-12-12 14:04:53 -0200189
190* the ratio of image size / scratch size
191* the number of erase cycles supported by the flash hardware
192
193The image size is used (instead of slot size) because only the slot's sectors
194that are actually used for storing the image are copied. The image/scratch ratio
195is the number of times the scratch will be erased on every upgrade. The number
196of erase cycles divided by the image/scratch ratio will give you the number of
197times an upgrade can be performed before the device goes out of spec.
198
199```
200num_upgrades = number_of_erase_cycles / (image_size / scratch_size)
201```
202
203Let's assume, for example, a device with 10000 erase cycles, an image size of
204150K and a scratch of 4K (usual minimum size of 4K sector devices). This would
205result in a total of:
206
207`10000 / (150 / 4) ~ 267`
208
209Increasing the scratch to 16K would give us:
210
211`10000 / (150 / 16) ~ 1067`
212
213There is no *best* ratio, as the right size is use-case dependent. Factors to
214consider include the number of times a device will be upgraded both in the field
David Vincze2d736ad2019-02-18 11:50:22 +0100215and during development, as well as any desired safety margin on the
216manufacturer's specified number of erase cycles. In general, using a ratio that
217allows hundreds to thousands of field upgrades in production is recommended.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800218
David Vinczee574f2d2020-07-10 11:42:03 +0200219### [Equal slots (direct-xip)](#direct-xip)
220
221When the direct-xip mode is enabled the active image flag is "moved" between the
222slots during image upgrade and in contrast to the above, the bootloader can
223run an image directly from either the primary or the secondary slot (without
224having to move/copy it into the primary slot). Therefore the image update
225client, which downloads the new images must be aware, which slot contains the
226active image and which acts as a staging area and it is responsible for loading
227the proper images into the proper slot. All this requires that the images be
228built to be executed from the corresponding slot. At boot time the bootloader
229first looks for images in the slots and then inspects the version numbers in the
230image headers. It selects the newest image (with the highest version number) and
231then checks its validity (integrity check, signature verification etc.). If the
232image is invalid MCUboot erases its memory slot and starts to validate the other
233image. After a successful validation of the selected image the bootloader
234chain-loads it.
235Handling the primary and secondary slots as equals has its drawbacks. Since the
236images are not moved between the slots, the on-the-fly image
237encryption/decryption can't be supported (it only applies to storing the image
238in an external flash on the device, the transport of encrypted image data is
239still feasible).
240
241The overwrite and the direct-xip upgrade strategies are substantially simpler to
242implement than the image swapping strategy, especially since the bootloader must
243work properly even when it is reset during the middle of an image swap. For this
244reason, the rest of the document describes its behavior when configured to swap
245images during an upgrade.
Marti Bolivara91674f2017-08-04 14:56:08 -0400246
Fabio Utzigd37d8772019-12-03 10:32:18 -0300247## [Boot Swap Types](#boot-swap-types)
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -0800248
Marti Bolivar048d8d82017-08-04 17:14:24 -0400249When the device first boots under normal circumstances, there is an up-to-date
David Vinczeba3bd602019-06-17 16:01:43 +0200250firmware image in each primary slot, which mcuboot can validate and then
David Vincze2d736ad2019-02-18 11:50:22 +0100251chain-load. In this case, no image swaps are necessary. During device upgrades,
David Vinczeba3bd602019-06-17 16:01:43 +0200252however, new candidate image(s) is present in the secondary slot(s), which
253mcuboot must swap into the primary slot(s) before booting as discussed above.
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -0800254
Marti Bolivar048d8d82017-08-04 17:14:24 -0400255Upgrading an old image with a new one by swapping can be a two-step process. In
256this process, mcuboot performs a "test" swap of image data in flash and boots
David Vinczeba3bd602019-06-17 16:01:43 +0200257the new image or it will be executed during operation. The new image can then
258update the contents of flash at runtime to mark itself "OK", and mcuboot will
259then still choose to run it during the next boot. When this happens, the swap is
260made "permanent". If this doesn't happen, mcuboot will perform a "revert" swap
261during the next boot by swapping the image(s) back into its original location(s)
262, and attempting to boot the old image(s).
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -0800263
Marti Bolivar048d8d82017-08-04 17:14:24 -0400264Depending on the use case, the first swap can also be made permanent directly.
265In this case, mcuboot will never attempt to revert the images on the next reset.
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -0800266
Marti Bolivar048d8d82017-08-04 17:14:24 -0400267Test swaps are supported to provide a rollback mechanism to prevent devices
268from becoming "bricked" by bad firmware. If the device crashes immediately
269upon booting a new (bad) image, mcuboot will revert to the old (working) image
270at the next device reset, rather than booting the bad image again. This allows
271device firmware to make test swaps permanent only after performing a self-test
272routine.
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -0800273
David Vinczeba3bd602019-06-17 16:01:43 +0200274On startup, mcuboot inspects the contents of flash to decide for each images
275which of these "swap types" to perform; this decision determines how it
276proceeds.
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -0800277
Marti Bolivar048d8d82017-08-04 17:14:24 -0400278The possible swap types, and their meanings, are:
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -0800279
David Brown17e20d12017-09-12 11:53:20 -0600280- `BOOT_SWAP_TYPE_NONE`: The "usual" or "no upgrade" case; attempt to boot the
David Vincze2d736ad2019-02-18 11:50:22 +0100281 contents of the primary slot.
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -0800282
David Vincze2d736ad2019-02-18 11:50:22 +0100283- `BOOT_SWAP_TYPE_TEST`: Boot the contents of the secondary slot by swapping
284 images. Unless the swap is made permanent, revert back on the next boot.
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -0800285
David Brown17e20d12017-09-12 11:53:20 -0600286- `BOOT_SWAP_TYPE_PERM`: Permanently swap images, and boot the upgraded image
Marti Bolivar048d8d82017-08-04 17:14:24 -0400287 firmware.
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -0800288
David Vincze2d736ad2019-02-18 11:50:22 +0100289- `BOOT_SWAP_TYPE_REVERT`: A previous test swap was not made permanent;
290 swap back to the old image whose data are now in the secondary slot. If the
291 old image marks itself "OK" when it boots, the next boot will have swap type
292 `BOOT_SWAP_TYPE_NONE`.
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -0800293
David Brown17e20d12017-09-12 11:53:20 -0600294- `BOOT_SWAP_TYPE_FAIL`: Swap failed because image to be run is not valid.
Marti Bolivar048d8d82017-08-04 17:14:24 -0400295
David Brown17e20d12017-09-12 11:53:20 -0600296- `BOOT_SWAP_TYPE_PANIC`: Swapping encountered an unrecoverable error.
Marti Bolivar048d8d82017-08-04 17:14:24 -0400297
298The "swap type" is a high-level representation of the outcome of the
299boot. Subsequent sections describe how mcuboot determines the swap type from
300the bit-level contents of flash.
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -0800301
Fabio Utzigd37d8772019-12-03 10:32:18 -0300302## [Image Trailer](#image-trailer)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800303
Fabio Utzig86fe4b22017-07-28 18:56:29 -0300304For the bootloader to be able to determine the current state and what actions
Marti Bolivar42818032017-08-04 15:45:01 -0400305should be taken during the current boot operation, it uses metadata stored in
306the image flash areas. While swapping, some of this metadata is temporarily
307copied into and out of the scratch area.
308
309This metadata is located at the end of the image flash areas, and is called an
310image trailer. An image trailer has the following structure:
Christopher Collins92ea77f2016-12-12 15:59:26 -0800311
David Brown17e20d12017-09-12 11:53:20 -0600312```
Christopher Collins92ea77f2016-12-12 15:59:26 -0800313 0 1 2 3
314 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
315 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Christopher Collins92ea77f2016-12-12 15:59:26 -0800316 ~ ~
Fabio Utzig2c05f1b2018-04-04 10:35:17 -0300317 ~ Swap status (BOOT_MAX_IMG_SECTORS * min-write-size * 3) ~
Christopher Collins92ea77f2016-12-12 15:59:26 -0800318 ~ ~
319 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Christopher Collinsa1c12042019-05-23 14:00:28 -0700320 | Encryption key 0 (16 octets) [*] |
321 | |
Christopher Collins92ea77f2016-12-12 15:59:26 -0800322 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Christopher Collinsa1c12042019-05-23 14:00:28 -0700323 | Encryption key 1 (16 octets) [*] |
324 | |
Fabio Utzig5bd4e582017-07-20 08:55:38 -0300325 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Christopher Collinsa1c12042019-05-23 14:00:28 -0700326 | Swap size (4 octets) |
Fabio Utzigea422c22017-09-11 11:02:47 -0300327 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
David Vinczee2453472019-06-17 12:31:59 +0200328 | Swap info | 0xff padding (7 octets) |
Fabio Utzigea422c22017-09-11 11:02:47 -0300329 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Christopher Collinsa1c12042019-05-23 14:00:28 -0700330 | Copy done | 0xff padding (7 octets) |
331 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
332 | Image OK | 0xff padding (7 octets) |
333 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
334 | MAGIC (16 octets) |
335 | |
Christopher Collins92ea77f2016-12-12 15:59:26 -0800336 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
David Brown17e20d12017-09-12 11:53:20 -0600337```
Christopher Collins92ea77f2016-12-12 15:59:26 -0800338
Christopher Collinsa1c12042019-05-23 14:00:28 -0700339[*]: Only present if the encryption option is enabled (`MCUBOOT_ENC_IMAGES`).
340
Marti Bolivar42818032017-08-04 15:45:01 -0400341The offset immediately following such a record represents the start of the next
342flash area.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800343
344Note: "min-write-size" is a property of the flash hardware. If the hardware
345allows individual bytes to be written at arbitrary addresses, then
346min-write-size is 1. If the hardware only allows writes at even addresses,
347then min-write-size is 2, and so on.
348
Marti Bolivar1dcb6852017-08-04 15:59:32 -0400349An image trailer contains the following fields:
Christopher Collins92ea77f2016-12-12 15:59:26 -0800350
Marti Bolivar1dcb6852017-08-04 15:59:32 -04003511. Swap status: A series of records which records the progress of an image
David Vincze2d736ad2019-02-18 11:50:22 +0100352 swap. To swap entire images, data are swapped between the two image areas
353 one or more sectors at a time, like this:
Marti Bolivar1dcb6852017-08-04 15:59:32 -0400354
David Vincze2d736ad2019-02-18 11:50:22 +0100355 - sector data in the primary slot is copied into scratch, then erased
356 - sector data in the secondary slot is copied into the primary slot,
357 then erased
358 - sector data in scratch is copied into the secondary slot
Marti Bolivar1dcb6852017-08-04 15:59:32 -0400359
360As it swaps images, the bootloader updates the swap status field in a way that
361allows it to compute how far this swap operation has progressed for each
362sector. The swap status field can thus used to resume a swap operation if the
363bootloader is halted while a swap operation is ongoing and later reset. The
David Vincze2d736ad2019-02-18 11:50:22 +0100364`BOOT_MAX_IMG_SECTORS` value is the configurable maximum number of sectors
365mcuboot supports for each image; its value defaults to 128, but allows for
366either decreasing this size, to limit RAM usage, or to increase it in devices
367that have massive amounts of Flash or very small sized sectors and thus require
368a bigger configuration to allow for the handling of all slot's sectors.
369The factor of min-write-sz is due to the behavior of flash hardware. The factor
370of 3 is explained below.
Fabio Utzig5bd4e582017-07-20 08:55:38 -0300371
Christopher Collinsa1c12042019-05-23 14:00:28 -07003722. Encryption keys: key-encrypting keys (KEKs). These keys are needed for
373 image encryption and decryption. See the
374 [encrypted images](encrypted_images.md) document for more information.
375
3763. Swap size: When beginning a new swap operation, the total size that needs
Håkon Øye Amundsencbf30472019-07-24 08:34:03 +0000377 to be swapped (based on the slot with largest image + TLVs) is written to
David Vincze2d736ad2019-02-18 11:50:22 +0100378 this location for easier recovery in case of a reset while performing the
379 swap.
Fabio Utzigea422c22017-09-11 11:02:47 -0300380
Håkon Øye Amundsencbf30472019-07-24 08:34:03 +00003814. Swap info: A single byte which encodes the following information:
David Vinczee2453472019-06-17 12:31:59 +0200382 - Swap type: Stored in bits 0-3. Indicating the type of swap operation in
383 progress. When mcuboot resumes an interrupted swap, it uses this field to
384 determine the type of operation to perform. This field contains one of the
385 following values in the table below.
386 - Image number: Stored in bits 4-7. It has always 0 value at single image
387 boot. In case of multi image boot it indicates, which image was swapped when
388 interrupt happened. The same scratch area is used during in case of all
389 image swap operation. Therefore this field is used to determine which image
390 the trailer belongs to if boot status is found on scratch area when the swap
391 operation is resumed.
Christopher Collinsa1c12042019-05-23 14:00:28 -0700392
393| Name | Value |
394| ------------------------- | ----- |
395| `BOOT_SWAP_TYPE_TEST` | 2 |
396| `BOOT_SWAP_TYPE_PERM` | 3 |
397| `BOOT_SWAP_TYPE_REVERT` | 4 |
398
399
4005. Copy done: A single byte indicating whether the image in this slot is
David Brown17e20d12017-09-12 11:53:20 -0600401 complete (0x01=done; 0xff=not done).
Fabio Utzig5bd4e582017-07-20 08:55:38 -0300402
Christopher Collinsa1c12042019-05-23 14:00:28 -07004036. Image OK: A single byte indicating whether the image in this slot has been
David Brown17e20d12017-09-12 11:53:20 -0600404 confirmed as good by the user (0x01=confirmed; 0xff=not confirmed).
Fabio Utzig5bd4e582017-07-20 08:55:38 -0300405
Christopher Collinsa1c12042019-05-23 14:00:28 -07004067. MAGIC: The following 16 bytes, written in host-byte-order:
Christopher Collins92ea77f2016-12-12 15:59:26 -0800407
David Brown17e20d12017-09-12 11:53:20 -0600408``` c
Christopher Collins92ea77f2016-12-12 15:59:26 -0800409 const uint32_t boot_img_magic[4] = {
410 0xf395c277,
411 0x7fefd260,
412 0x0f505235,
413 0x8079b62c,
414 };
David Brown17e20d12017-09-12 11:53:20 -0600415```
Christopher Collins92ea77f2016-12-12 15:59:26 -0800416
Fabio Utzigd37d8772019-12-03 10:32:18 -0300417## [IMAGE TRAILERS](#image-trailers)
Fabio Utzig86fe4b22017-07-28 18:56:29 -0300418
Marti Bolivar048d8d82017-08-04 17:14:24 -0400419At startup, the boot loader determines the boot swap type by inspecting the
420image trailers. When using the term "image trailers" what is meant is the
421aggregate information provided by both image slot's trailers.
Fabio Utzig86fe4b22017-07-28 18:56:29 -0300422
Fabio Utzigd37d8772019-12-03 10:32:18 -0300423### [New swaps (non-resumes)](#new-swaps-non-resumes)
Christopher Collinsa1c12042019-05-23 14:00:28 -0700424
425For new swaps, mcuboot must inspect a collection of fields to determine which
426swap operation to perform.
427
David Vincze2d736ad2019-02-18 11:50:22 +0100428The image trailers records are structured around the limitations imposed by
429flash hardware. As a consequence, they do not have a very intuitive design, and
430it is difficult to get a sense of the state of the device just by looking at the
Marti Bolivar048d8d82017-08-04 17:14:24 -0400431image trailers. It is better to map all the possible trailer states to the swap
432types described above via a set of tables. These tables are reproduced below.
Fabio Utzig86fe4b22017-07-28 18:56:29 -0300433
434Note: An important caveat about the tables described below is that they must
435be evaluated in the order presented here. Lower state numbers must have a
436higher priority when testing the image trailers.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800437
David Brown17e20d12017-09-12 11:53:20 -0600438```
Christopher Collins92ea77f2016-12-12 15:59:26 -0800439 State I
David Vincze2d736ad2019-02-18 11:50:22 +0100440 | primary slot | secondary slot |
441 -----------------+--------------+----------------|
442 magic | Any | Good |
443 image-ok | Any | Unset |
444 copy-done | Any | Any |
445 -----------------+--------------+----------------'
446 result: BOOT_SWAP_TYPE_TEST |
447 -------------------------------------------------'
Fabio Utzig5bd4e582017-07-20 08:55:38 -0300448
Christopher Collins92ea77f2016-12-12 15:59:26 -0800449
Fabio Utzig86fe4b22017-07-28 18:56:29 -0300450 State II
David Vincze2d736ad2019-02-18 11:50:22 +0100451 | primary slot | secondary slot |
452 -----------------+--------------+----------------|
453 magic | Any | Good |
454 image-ok | Any | 0x01 |
455 copy-done | Any | Any |
456 -----------------+--------------+----------------'
457 result: BOOT_SWAP_TYPE_PERM |
458 -------------------------------------------------'
Fabio Utzig5bd4e582017-07-20 08:55:38 -0300459
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -0800460
Fabio Utzig86fe4b22017-07-28 18:56:29 -0300461 State III
David Vincze2d736ad2019-02-18 11:50:22 +0100462 | primary slot | secondary slot |
463 -----------------+--------------+----------------|
464 magic | Good | Unset |
465 image-ok | 0xff | Any |
466 copy-done | 0x01 | Any |
467 -----------------+--------------+----------------'
468 result: BOOT_SWAP_TYPE_REVERT |
469 -------------------------------------------------'
David Brown17e20d12017-09-12 11:53:20 -0600470```
Christopher Collins92ea77f2016-12-12 15:59:26 -0800471
Marti Bolivar048d8d82017-08-04 17:14:24 -0400472Any of the above three states results in mcuboot attempting to swap images.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800473
Marti Bolivar048d8d82017-08-04 17:14:24 -0400474Otherwise, mcuboot does not attempt to swap images, resulting in one of the
475other three swap types, as illustrated by State IV.
Fabio Utzig86fe4b22017-07-28 18:56:29 -0300476
David Brown17e20d12017-09-12 11:53:20 -0600477```
Fabio Utzig86fe4b22017-07-28 18:56:29 -0300478 State IV
David Vincze2d736ad2019-02-18 11:50:22 +0100479 | primary slot | secondary slot |
480 -----------------+--------------+----------------|
481 magic | Any | Any |
482 image-ok | Any | Any |
483 copy-done | Any | Any |
484 -----------------+--------------+----------------'
485 result: BOOT_SWAP_TYPE_NONE, |
486 BOOT_SWAP_TYPE_FAIL, or |
487 BOOT_SWAP_TYPE_PANIC |
488 -------------------------------------------------'
David Brown17e20d12017-09-12 11:53:20 -0600489```
Christopher Collins92ea77f2016-12-12 15:59:26 -0800490
Marti Bolivar048d8d82017-08-04 17:14:24 -0400491In State IV, when no errors occur, mcuboot will attempt to boot the contents of
David Vincze2d736ad2019-02-18 11:50:22 +0100492the primary slot directly, and the result is `BOOT_SWAP_TYPE_NONE`. If the image
493in the primary slot is not valid, the result is `BOOT_SWAP_TYPE_FAIL`. If a
494fatal error occurs during boot, the result is `BOOT_SWAP_TYPE_PANIC`. If the
495result is either `BOOT_SWAP_TYPE_FAIL` or `BOOT_SWAP_TYPE_PANIC`, mcuboot hangs
496rather than booting an invalid or compromised image.
Fabio Utzig86fe4b22017-07-28 18:56:29 -0300497
Marti Bolivar048d8d82017-08-04 17:14:24 -0400498Note: An important caveat to the above is the result when a swap is requested
David Vincze2d736ad2019-02-18 11:50:22 +0100499 and the image in the secondary slot fails to validate, due to a hashing or
500 signing error. This state behaves as State IV with the extra action of
501 marking the image in the primary slot as "OK", to prevent further attempts
502 to swap.
Fabio Utzig86fe4b22017-07-28 18:56:29 -0300503
Fabio Utzigd37d8772019-12-03 10:32:18 -0300504### [Resumed swaps](#resumed-swaps)
Christopher Collinsa1c12042019-05-23 14:00:28 -0700505
506If mcuboot determines that it is resuming an interrupted swap (i.e., a reset
507occurred mid-swap), it fully determines the operation to resume by reading the
David Vinczee2453472019-06-17 12:31:59 +0200508`swap info` field from the active trailer and extracting the swap type from bits
5090-3. The set of tables in the previous section are not necessary in the resume
510case.
Christopher Collinsa1c12042019-05-23 14:00:28 -0700511
Fabio Utzigd37d8772019-12-03 10:32:18 -0300512## [High-Level Operation](#high-level-operation)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800513
514With the terms defined, we can now explore the boot loader's operation. First,
515a high-level overview of the boot process is presented. Then, the following
516sections describe each step of the process in more detail.
517
518Procedure:
519
David Brown17e20d12017-09-12 11:53:20 -06005201. Inspect swap status region; is an interrupted swap being resumed?
Fabio Utzig75b34412019-09-06 08:30:43 -0300521 + Yes: Complete the partial swap operation; skip to step 3.
522 + No: Proceed to step 2.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800523
David Brown17e20d12017-09-12 11:53:20 -06005242. Inspect image trailers; is a swap requested?
Fabio Utzig75b34412019-09-06 08:30:43 -0300525 + Yes:
526 1. Is the requested image valid (integrity and security check)?
527 + Yes.
528 a. Perform swap operation.
529 b. Persist completion of swap procedure to image trailers.
530 c. Proceed to step 3.
531 + No.
532 a. Erase invalid image.
533 b. Persist failure of swap procedure to image trailers.
534 c. Proceed to step 3.
535
536 + No: Proceed to step 3.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800537
David Vincze2d736ad2019-02-18 11:50:22 +01005383. Boot into image in primary slot.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800539
Fabio Utzigd37d8772019-12-03 10:32:18 -0300540### [Multiple Image Boot](#multiple-image-boot)
David Vinczeba3bd602019-06-17 16:01:43 +0200541
542When the flash contains multiple executable images the boot loader's operation
543is a bit more complex but similar to the previously described procedure with
544one image. Every image can be updated independently therefore the flash is
545partitioned further to arrange two slots for each image.
546```
547+--------------------+
548| MCUBoot |
549+--------------------+
550 ~~~~~ <- memory might be not contiguous
551+--------------------+
552| Image 0 |
553| primary slot |
554+--------------------+
555| Image 0 |
556| secondary slot |
557+--------------------+
558 ~~~~~ <- memory might be not contiguous
559+--------------------+
560| Image N |
561| primary slot |
562+--------------------+
563| Image N |
564| secondary slot |
565+--------------------+
566| Scratch |
567+--------------------+
568```
David Vinczee32483f2019-06-13 10:46:24 +0200569MCUBoot is also capable of handling dependencies between images. For example
570if an image needs to be reverted it might be necessary to revert another one too
571(e.g. due to API incompatibilities) or simply to prevent from being updated
572because of an unsatisfied dependency. Therefore all aborted swaps have to be
573completed and all the swap types have to be determined for each image before
574the dependency checks. Dependency handling is described in more detail in a
575following section. The multiple image boot procedure is organized in loops which
576iterate over all the firmware images. The high-level overview of the boot
577process is presented below.
David Vinczeba3bd602019-06-17 16:01:43 +0200578
579+ ###### Loop 1. Iterate over all images
580 1. Inspect swap status region of current image; is an interrupted swap being
581 resumed?
582 + Yes:
583 + Review the validity of previously determined swap types
584 of other images.
585 + Complete the partial swap operation.
586 + Mark the swap type as `None`.
587 + Skip to next image.
588 + No: Proceed to step 2.
589
590 2. Inspect image trailers in the primary and secondary slot; is an image
591 swap requested?
592 + Yes: Review the validity of previously determined swap types of other
593 images. Is the requested image valid (integrity and security
594 check)?
595 + Yes:
596 + Set the previously determined swap type for the current image.
597 + Skip to next image.
598 + No:
599 + Erase invalid image.
600 + Persist failure of swap procedure to image trailers.
601 + Mark the swap type as `Fail`.
602 + Skip to next image.
603 + No:
604 + Mark the swap type as `None`.
605 + Skip to next image.
606
607+ ###### Loop 2. Iterate over all images
David Vinczee32483f2019-06-13 10:46:24 +0200608 1. Does the current image depend on other image(s)?
609 + Yes: Are all the image dependencies satisfied?
610 + Yes: Skip to next image.
611 + No:
612 + Modify swap type depending on what the previous type was.
613 + Restart dependency check from the first image.
614 + No: Skip to next image.
David Vinczeba3bd602019-06-17 16:01:43 +0200615
David Vinczee32483f2019-06-13 10:46:24 +0200616+ ###### Loop 3. Iterate over all images
David Vinczeba3bd602019-06-17 16:01:43 +0200617 1. Is an image swap requested?
618 + Yes:
619 + Perform image update operation.
620 + Persist completion of swap procedure to image trailers.
621 + Skip to next image.
622 + No: Skip to next image.
623
David Vinczee32483f2019-06-13 10:46:24 +0200624+ ###### Loop 4. Iterate over all images
David Vinczeba3bd602019-06-17 16:01:43 +0200625 1. Validate image in the primary slot (integrity and security check) or
626 at least do a basic sanity check to avoid booting into an empty flash
627 area.
628
629+ Boot into image in the primary slot of the 0th image position\
630 (other image in the boot chain is started by another image).
631
Fabio Utzigd37d8772019-12-03 10:32:18 -0300632## [Image Swapping](#image-swapping)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800633
634The boot loader swaps the contents of the two image slots for two reasons:
Fabio Utzig75b34412019-09-06 08:30:43 -0300635
636 * User has issued a "set pending" operation; the image in the secondary slot
Håkon Øye Amundsen11d91c32020-03-04 08:49:47 +0000637 should be run once (state I) or repeatedly (state II), depending on
Fabio Utzig75b34412019-09-06 08:30:43 -0300638 whether a permanent swap was specified.
639 * Test image rebooted without being confirmed; the boot loader should
Håkon Øye Amundsen11d91c32020-03-04 08:49:47 +0000640 revert to the original image currently in the secondary slot (state III).
Christopher Collins92ea77f2016-12-12 15:59:26 -0800641
Fabio Utzig86fe4b22017-07-28 18:56:29 -0300642If the image trailers indicates that the image in the secondary slot should be
Christopher Collins92ea77f2016-12-12 15:59:26 -0800643run, the boot loader needs to copy it to the primary slot. The image currently
644in the primary slot also needs to be retained in flash so that it can be used
645later. Furthermore, both images need to be recoverable if the boot loader
646resets in the middle of the swap operation. The two images are swapped
647according to the following procedure:
648
Fabio Utzig60319ac2019-09-06 08:29:50 -03006491. Determine if both slots are compatible enough to have their images swapped.
650 To be compatible, both have to have only sectors that can fit into the
651 scratch area and if one of them has larger sectors than the other, it must
652 be able to entirely fit some rounded number of sectors from the other slot.
Fabio Utzigc28005b2019-09-10 12:18:29 -0300653 In the next steps we'll use the terminology "region" for the total amount of
654 data copied/erased because this can be any amount of sectors depending on
655 how many the scratch is able to fit for some swap operation.
6562. Iterate the list of region indices in descending order (i.e., starting
657 with the greatest index); only regions that are predetermined to be part of
Fabio Utzig60319ac2019-09-06 08:29:50 -0300658 the image are copied; current element = "index".
659 + a. Erase scratch area.
660 + b. Copy secondary_slot[index] to scratch area.
Fabio Utzigc28005b2019-09-10 12:18:29 -0300661 - If this is the last region in the slot, scratch area has a temporary
Fabio Utzig60319ac2019-09-06 08:29:50 -0300662 status area initialized to store the initial state, because the
Fabio Utzigc28005b2019-09-10 12:18:29 -0300663 primary slot's last region will have to be erased. In this case,
Fabio Utzig60319ac2019-09-06 08:29:50 -0300664 only the data that was calculated to amount to the image is copied.
Fabio Utzigc28005b2019-09-10 12:18:29 -0300665 - Else if this is the first swapped region but not the last region in
Fabio Utzig60319ac2019-09-06 08:29:50 -0300666 the slot, initialize the status area in primary slot and copy the
Fabio Utzigc28005b2019-09-10 12:18:29 -0300667 full region contents.
668 - Else, copy entire region contents.
Fabio Utzig60319ac2019-09-06 08:29:50 -0300669 + c. Write updated swap status (i).
670 + d. Erase secondary_slot[index]
671 + e. Copy primary_slot[index] to secondary_slot[index] according to amount
672 previosly copied at step b.
Fabio Utzigc28005b2019-09-10 12:18:29 -0300673 - If this is not the last region in the slot, erase the trailer in the
Fabio Utzig60319ac2019-09-06 08:29:50 -0300674 secondary slot, to always use the one in the primary slot.
675 + f. Write updated swap status (ii).
676 + g. Erase primary_slot[index].
677 + h. Copy scratch area to primary_slot[index] according to amount
678 previously copied at step b.
Fabio Utzigc28005b2019-09-10 12:18:29 -0300679 - If this is the last region in the slot, the status is read from
Fabio Utzig60319ac2019-09-06 08:29:50 -0300680 scratch (where it was stored temporarily) and written anew in the
681 primary slot.
682 + i. Write updated swap status (iii).
6833. Persist completion of swap procedure to the primary slot image trailer.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800684
David Vincze2d736ad2019-02-18 11:50:22 +0100685The additional caveats in step 2f are necessary so that the secondary slot image
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -0800686trailer can be written by the user at a later time. With the image trailer
David Vincze2d736ad2019-02-18 11:50:22 +0100687unwritten, the user can test the image in the secondary slot
Håkon Øye Amundsen11d91c32020-03-04 08:49:47 +0000688(i.e., transition to state I).
Christopher Collins92ea77f2016-12-12 15:59:26 -0800689
Fabio Utzigc28005b2019-09-10 12:18:29 -0300690Note1: If the region being copied contains the last sector, then swap status is
Fabio Utzig5bd4e582017-07-20 08:55:38 -0300691temporarily maintained on scratch for the duration of this operation, always
David Vincze2d736ad2019-02-18 11:50:22 +0100692using the primary slot's area otherwise.
Fabio Utzig5bd4e582017-07-20 08:55:38 -0300693
694Note2: The bootloader tries to copy only used sectors (based on largest image
695installed on any of the slots), minimizing the amount of sectors copied and
696reducing the amount of time required for a swap operation.
697
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -0800698The particulars of step 3 vary depending on whether an image is being tested,
David Vincze2d736ad2019-02-18 11:50:22 +0100699permanently used, reverted or a validation failure of the secondary slot
700happened when a swap was requested:
Fabio Utzig86fe4b22017-07-28 18:56:29 -0300701
Christopher Collins92ea77f2016-12-12 15:59:26 -0800702 * test:
David Vincze2d736ad2019-02-18 11:50:22 +0100703 o Write primary_slot.copy_done = 1
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -0800704 (swap caused the following values to be written:
David Vincze2d736ad2019-02-18 11:50:22 +0100705 primary_slot.magic = BOOT_MAGIC
Håkon Øye Amundsencdf94c22020-03-04 08:52:31 +0000706 secondary_slot.magic = UNSET
David Vincze2d736ad2019-02-18 11:50:22 +0100707 primary_slot.image_ok = Unset)
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -0800708
709 * permanent:
David Vincze2d736ad2019-02-18 11:50:22 +0100710 o Write primary_slot.copy_done = 1
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -0800711 (swap caused the following values to be written:
David Vincze2d736ad2019-02-18 11:50:22 +0100712 primary_slot.magic = BOOT_MAGIC
Håkon Øye Amundsencdf94c22020-03-04 08:52:31 +0000713 secondary_slot.magic = UNSET
David Vincze2d736ad2019-02-18 11:50:22 +0100714 primary_slot.image_ok = 0x01)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800715
716 * revert:
David Vincze2d736ad2019-02-18 11:50:22 +0100717 o Write primary_slot.copy_done = 1
718 o Write primary_slot.image_ok = 1
Fabio Utzig86fe4b22017-07-28 18:56:29 -0300719 (swap caused the following values to be written:
David Vincze2d736ad2019-02-18 11:50:22 +0100720 primary_slot.magic = BOOT_MAGIC)
Fabio Utzig86fe4b22017-07-28 18:56:29 -0300721
David Vincze2d736ad2019-02-18 11:50:22 +0100722 * failure to validate the secondary slot:
723 o Write primary_slot.image_ok = 1
Fabio Utzig86fe4b22017-07-28 18:56:29 -0300724
David Vincze2d736ad2019-02-18 11:50:22 +0100725After completing the operations as described above the image in the primary slot
726should be booted.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800727
Fabio Utzigd37d8772019-12-03 10:32:18 -0300728## [Swap Status](#swap-status)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800729
730The swap status region allows the boot loader to recover in case it restarts in
731the middle of an image swap operation. The swap status region consists of a
732series of single-byte records. These records are written independently, and
733therefore must be padded according to the minimum write size imposed by the
734flash hardware. In the below figure, a min-write-size of 1 is assumed for
735simplicity. The structure of the swap status region is illustrated below. In
736this figure, a min-write-size of 1 is assumed for simplicity.
737
David Brown17e20d12017-09-12 11:53:20 -0600738```
Christopher Collins92ea77f2016-12-12 15:59:26 -0800739 0 1 2 3
740 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
741 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
742 |sec127,state 0 |sec127,state 1 |sec127,state 2 |sec126,state 0 |
743 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
744 |sec126,state 1 |sec126,state 2 |sec125,state 0 |sec125,state 1 |
745 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
746 |sec125,state 2 | |
747 +-+-+-+-+-+-+-+-+ +
748 ~ ~
749 ~ [Records for indices 124 through 1 ~
750 ~ ~
751 ~ +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
752 ~ |sec000,state 0 |sec000,state 1 |sec000,state 2 |
753 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
David Brown17e20d12017-09-12 11:53:20 -0600754```
Christopher Collins92ea77f2016-12-12 15:59:26 -0800755
756The above is probably not helpful at all; here is a description in English.
757
758Each image slot is partitioned into a sequence of flash sectors. If we were to
759enumerate the sectors in a single slot, starting at 0, we would have a list of
760sector indices. Since there are two image slots, each sector index would
761correspond to a pair of sectors. For example, sector index 0 corresponds to
David Vincze2d736ad2019-02-18 11:50:22 +0100762the first sector in the primary slot and the first sector in the secondary slot.
763Finally, reverse the list of indices such that the list starts with index
764`BOOT_MAX_IMG_SECTORS - 1` and ends with 0. The swap status region is a
765representation of this reversed list.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800766
767During a swap operation, each sector index transitions through four separate
768states:
David Brown17e20d12017-09-12 11:53:20 -0600769```
David Vincze2d736ad2019-02-18 11:50:22 +01007700. primary slot: image 0, secondary slot: image 1, scratch: N/A
7711. primary slot: image 0, secondary slot: N/A, scratch: image 1 (1->s, erase 1)
7722. primary slot: N/A, secondary slot: image 0, scratch: image 1 (0->1, erase 0)
7733. primary slot: image 1, secondary slot: image 0, scratch: N/A (s->0)
David Brown17e20d12017-09-12 11:53:20 -0600774```
Christopher Collins92ea77f2016-12-12 15:59:26 -0800775
776Each time a sector index transitions to a new state, the boot loader writes a
777record to the swap status region. Logically, the boot loader only needs one
778record per sector index to keep track of the current swap state. However, due
779to limitations imposed by flash hardware, a record cannot be overwritten when
780an index's state changes. To solve this problem, the boot loader uses three
781records per sector index rather than just one.
782
783Each sector-state pair is represented as a set of three records. The record
784values map to the above four states as follows
785
David Brown17e20d12017-09-12 11:53:20 -0600786```
Christopher Collins92ea77f2016-12-12 15:59:26 -0800787 | rec0 | rec1 | rec2
788 --------+------+------+------
789 state 0 | 0xff | 0xff | 0xff
790 state 1 | 0x01 | 0xff | 0xff
791 state 2 | 0x01 | 0x02 | 0xff
792 state 3 | 0x01 | 0x02 | 0x03
David Brown17e20d12017-09-12 11:53:20 -0600793```
Christopher Collins92ea77f2016-12-12 15:59:26 -0800794
Fabio Utzig2c05f1b2018-04-04 10:35:17 -0300795The swap status region can accommodate `BOOT_MAX_IMG_SECTORS` sector indices.
David Vincze2d736ad2019-02-18 11:50:22 +0100796Hence, the size of the region, in bytes, is
797`BOOT_MAX_IMG_SECTORS * min-write-size * 3`. The only requirement for the index
798count is that it is great enough to account for a maximum-sized image
799(i.e., at least as great as the total sector count in an image slot). If a
800device's image slots have been configured with `BOOT_MAX_IMG_SECTORS: 128` and
801use less than 128 sectors, the first record that gets written will be somewhere
802in the middle of the region. For example, if a slot uses 64 sectors, the first
803sector index that gets swapped is 63, which corresponds to the exact halfway
804point within the region.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800805
Fabio Utzig5bd4e582017-07-20 08:55:38 -0300806Note: since the scratch area only ever needs to record swapping of the last
807sector, it uses at most min-write-size * 3 bytes for its own status area.
808
Fabio Utzigd37d8772019-12-03 10:32:18 -0300809## [Reset Recovery](#reset-recovery)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800810
811If the boot loader resets in the middle of a swap operation, the two images may
812be discontiguous in flash. Bootutil recovers from this condition by using the
Fabio Utzig86fe4b22017-07-28 18:56:29 -0300813image trailers to determine how the image parts are distributed in flash.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800814
815The first step is determine where the relevant swap status region is located.
816Because this region is embedded within the image slots, its location in flash
Fabio Utzig86fe4b22017-07-28 18:56:29 -0300817changes during a swap operation. The below set of tables map image trailers
Christopher Collins92ea77f2016-12-12 15:59:26 -0800818contents to swap status location. In these tables, the "source" field
David Vinczeba3bd602019-06-17 16:01:43 +0200819indicates where the swap status region is located. In case of multi image boot
820the images primary area and the single scratch area is always examined in pairs.
821If swap status found on scratch area then it might not belong to the current
822image. The swap_info field of swap status stores the corresponding image number.
823If it does not match then "source: none" is returned.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800824
David Brown17e20d12017-09-12 11:53:20 -0600825```
David Vincze2d736ad2019-02-18 11:50:22 +0100826 | primary slot | scratch |
827 ----------+--------------+--------------|
828 magic | Good | Any |
829 copy-done | 0x01 | N/A |
830 ----------+--------------+--------------'
831 source: none |
832 ----------------------------------------'
Marti Bolivar49b29172017-08-04 14:50:51 -0400833
David Vincze2d736ad2019-02-18 11:50:22 +0100834 | primary slot | scratch |
835 ----------+--------------+--------------|
836 magic | Good | Any |
837 copy-done | 0xff | N/A |
838 ----------+--------------+--------------'
839 source: primary slot |
840 ----------------------------------------'
Marti Bolivar49b29172017-08-04 14:50:51 -0400841
David Vincze2d736ad2019-02-18 11:50:22 +0100842 | primary slot | scratch |
843 ----------+--------------+--------------|
844 magic | Any | Good |
845 copy-done | Any | N/A |
846 ----------+--------------+--------------'
847 source: scratch |
848 ----------------------------------------'
Marti Bolivar49b29172017-08-04 14:50:51 -0400849
David Vincze2d736ad2019-02-18 11:50:22 +0100850 | primary slot | scratch |
851 ----------+--------------+--------------|
852 magic | Unset | Any |
853 copy-done | 0xff | N/A |
854 ----------+--------------+--------------|
855 source: primary slot |
856 ----------------------------------------+------------------------------+
857 This represents one of two cases: |
858 o No swaps ever (no status to read, so no harm in checking). |
859 o Mid-revert; status in the primary slot. |
860 For this reason we assume the primary slot as source, to trigger a |
861 check of the status area and find out if there was swapping under way. |
862 -----------------------------------------------------------------------'
David Brown17e20d12017-09-12 11:53:20 -0600863```
Christopher Collins92ea77f2016-12-12 15:59:26 -0800864
Christopher Collinsa1c12042019-05-23 14:00:28 -0700865If the swap status region indicates that the images are not contiguous, mcuboot
866determines the type of swap operation that was interrupted by reading the `swap
Håkon Øye Amundsencbf30472019-07-24 08:34:03 +0000867info` field in the active image trailer and extracting the swap type from bits
David Vinczee2453472019-06-17 12:31:59 +02008680-3 then resumes the operation. In other words, it applies the procedure defined
869in the previous section, moving image 1 into the primary slot and image 0 into
870the secondary slot. If the boot status indicates that an image part is present
871in the scratch area, this part is copied into the correct location by starting
872at step e or step h in the area-swap procedure, depending on whether the part
873belongs to image 0 or image 1.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800874
875After the swap operation has been completed, the boot loader proceeds as though
876it had just been started.
877
Fabio Utzigd37d8772019-12-03 10:32:18 -0300878## [Integrity Check](#integrity-check)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800879
880An image is checked for integrity immediately before it gets copied into the
Fabio Utzig5bd4e582017-07-20 08:55:38 -0300881primary slot. If the boot loader doesn't perform an image swap, then it can
David Vincze2d736ad2019-02-18 11:50:22 +0100882perform an optional integrity check of the image in the primary slot if
883`MCUBOOT_VALIDATE_PRIMARY_SLOT` is set, otherwise it doesn't perform an
884integrity check.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800885
886During the integrity check, the boot loader verifies the following aspects of
887an image:
Fabio Utzig75b34412019-09-06 08:30:43 -0300888
Fabio Utzigfd140ec2019-09-12 14:37:48 -0300889 * 32-bit magic number must be correct (`IMAGE_MAGIC`).
Fabio Utzig75b34412019-09-06 08:30:43 -0300890 * Image must contain an `image_tlv_info` struct, identified by its magic
Fabio Utzigfd140ec2019-09-12 14:37:48 -0300891 (`IMAGE_TLV_PROT_INFO_MAGIC` or `IMAGE_TLV_INFO_MAGIC`) exactly following
892 the firmware (`hdr_size` + `img_size`). If `IMAGE_TLV_PROT_INFO_MAGIC` is
893 found then after `ih_protect_tlv_size` bytes, another `image_tlv_info`
894 with magic equal to `IMAGE_TLV_INFO_MAGIC` must be present.
Fabio Utzig75b34412019-09-06 08:30:43 -0300895 * Image must contain a SHA256 TLV.
896 * Calculated SHA256 must match SHA256 TLV contents.
897 * Image *may* contain a signature TLV. If it does, it must also have a
898 KEYHASH TLV with the hash of the key that was used to sign. The list of
899 keys will then be iterated over looking for the matching key, which then
900 will then be used to verify the image contents.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800901
Fabio Utzigd37d8772019-12-03 10:32:18 -0300902## [Security](#security)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800903
904As indicated above, the final step of the integrity check is signature
905verification. The boot loader can have one or more public keys embedded in it
906at build time. During signature verification, the boot loader verifies that an
Håkon Øye Amundsencbf30472019-07-24 08:34:03 +0000907image was signed with a private key that corresponds to the embedded KEYHASH
Fabio Utzigea422c22017-09-11 11:02:47 -0300908TLV.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800909
910For information on embedding public keys in the boot loader, as well as
Fabio Utzig4dce6aa2018-02-12 15:31:32 -0200911producing signed images, see: [signed_images](signed_images.md).
Fabio Utzigcdfa11a2018-10-01 09:45:54 -0300912
913If you want to enable and use encrypted images, see:
914[encrypted_images](encrypted_images.md).
David Vinczee32483f2019-06-13 10:46:24 +0200915
David Vinczee574f2d2020-07-10 11:42:03 +0200916Note: Image encryption is not supported when the direct-xip upgrade strategy
917is selected.
918
David Vincze25459bf2020-04-21 17:11:20 +0200919### [Using Hardware Keys for Verification](#hw-key-support)
920
921By default, the whole public key is embedded in the bootloader code and its
922hash is added to the image manifest as a KEYHASH TLV entry. As an alternative
923the bootloader can be made independent of the keys by setting the
924`MCUBOOT_HW_KEY` option. In this case the hash of the public key must be
925provisioned to the target device and mcuboot must be able to retrieve the
926key-hash from there. For this reason the target must provide a definition
927for the `boot_retrieve_public_key_hash()` function which is declared in
928`boot/bootutil/include/bootutil/sign_key.h`. It is also required to use
929the `full` option for the `--public-key-format` imgtool argument in order to
930add the whole public key (PUBKEY TLV) to the image manifest instead of its
931hash (KEYHASH TLV). During boot the public key is validated before using it for
932signature verification, mcuboot calculates the hash of the public key from the
933TLV area and compares it with the key-hash that was retrieved from the device.
934This way mcuboot is independent from the public key(s). The key(s) can be
935provisioned any time and by different parties.
936
Fabio Utzigd37d8772019-12-03 10:32:18 -0300937## [Protected TLVs](#protected-tlvs)
Fabio Utzigfd140ec2019-09-12 14:37:48 -0300938
939If the TLV area contains protected TLV entries, by beginning with a `struct
940image_tlv_info` with a magic value of `IMAGE_TLV_PROT_INFO_MAGIC` then the
941data of those TLVs must also be integrity and authenticity protected. Beyond
942the full size of the protected TLVs being stored in the `image_tlv_info`,
943the size of the protected TLVs together with the size of the `image_tlv_info`
944struct itself are also saved in the `ih_protected_size` field inside the
945header.
946
947Whenever an image has protected TLVs the SHA256 has to be calculated over
948not just the image header and the image but also the TLV info header and the
949protected TLVs.
950
951```
952A +---------------------+
953 | Header | <- struct image_header
954 +---------------------+
955 | Payload |
956 +---------------------+
957 | TLV area |
958 | +-----------------+ | struct image_tlv_info with
959 | | TLV area header | | <- IMAGE_TLV_PROT_INFO_MAGIC (optional)
960 | +-----------------+ |
961 | | Protected TLVs | | <- Protected TLVs (struct image_tlv)
962B | +-----------------+ |
963 | | TLV area header | | <- struct image_tlv_info with IMAGE_TLV_INFO_MAGIC
964C | +-----------------+ |
965 | | SHA256 hash | | <- hash from A - B (struct image_tlv)
966D | +-----------------+ |
967 | | Keyhash | | <- indicates which pub. key for sig (struct image_tlv)
968 | +-----------------+ |
969 | | Signature | | <- signature from C - D (struct image_tlv), only hash
970 | +-----------------+ |
971 +---------------------+
972```
973
Fabio Utzigd37d8772019-12-03 10:32:18 -0300974## [Dependency Check](#dependency-check)
David Vinczee32483f2019-06-13 10:46:24 +0200975
976MCUBoot can handle multiple firmware images. It is possible to update them
977independently but in many cases it can be desired to be able to describe
978dependencies between the images (e.g. to ensure API compliance and avoid
979interoperability issues).
980
981The dependencies between images can be described with additional TLV entries in
Fabio Utzigfd140ec2019-09-12 14:37:48 -0300982the protected TLV area after the end of an image. There can be more than one
983dependency entry, but in practice if the platform only supports two individual
984images then there can be maximum one entry which reflects to the other image.
David Vinczee32483f2019-06-13 10:46:24 +0200985
David Vinczee32483f2019-06-13 10:46:24 +0200986At the phase of dependency check all aborted swaps are finalized if there were
987any. During the dependency check the boot loader verifies whether the image
988dependencies are all satisfied. If at least one of the dependencies of an image
989is not fulfilled then the swap type of that image has to be modified
990accordingly and the dependency check needs to be restarted. This way the number
991of unsatisfied dependencies will decrease or remain the same. There is always at
992least 1 valid configuration. In worst case, the system returns to the initial
993state after dependency check.
994
995For more information on adding dependency entries to an image,
996see: [imgtool](imgtool.md).
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000997
998## [Downgrade Prevention](#downgrade-prevention)
999
1000Downgrade prevention is a feature which enforces that the new image must have a
David Vinczec3084132020-02-18 14:50:47 +01001001higher version/security counter number than the image it is replacing, thus
1002preventing the malicious downgrading of the device to an older and possibly
1003vulnerable version of its firmware.
1004
1005### [SW Based Downgrade Prevention](#sw-downgrade-prevention)
1006
1007During the software based downgrade prevention the image version numbers are
1008compared. This feature is enabled with the `MCUBOOT_DOWNGRADE_PREVENTION`
1009option. In this case downgrade prevention is only available when the
1010overwrite-based image update strategy is used (i.e. `MCUBOOT_OVERWRITE_ONLY`
1011is set).
1012
1013### [HW Based Downgrade Prevention](#hw-downgrade-prevention)
1014
David Vincze25459bf2020-04-21 17:11:20 +02001015Each signed image can contain a security counter in its protected TLV area, which
1016can be added to the image using the `-s` option of the [imgtool](imgtool.md) script.
David Vinczec3084132020-02-18 14:50:47 +01001017During the hardware based downgrade prevention (alias rollback protection) the
1018new image's security counter will be compared with the currently active security
1019counter value which must be stored in a non-volatile and trusted component of
David Vincze25459bf2020-04-21 17:11:20 +02001020the device. It is beneficial to handle this counter independently from image
1021version number:
David Vinczec3084132020-02-18 14:50:47 +01001022
1023 * It does not need to increase with each software release,
1024 * It makes it possible to do software downgrade to some extent: if the
1025 security counter has the same value in the older image then it is accepted.
David Vincze25459bf2020-04-21 17:11:20 +02001026
1027It is an optional step of the image validation process and can be enabled with
1028the `MCUBOOT_HW_ROLLBACK_PROT` config option. When enabled, the target must
1029provide an implementation of the security counter interface defined in
1030`boot/bootutil/include/security_cnt.h`.
1031
1032## [Measured boot and data sharing](#boot-data-sharing)
1033
1034MCUBoot defines a mechanism for sharing boot status information (also known as
1035measured boot) and an interface for sharing application specific information
1036with the runtime software. If any of these are enabled the target must provide
1037a shared data area between the bootloader and runtime firmware and define the
1038following parameters:
1039
1040```c
1041#define MCUBOOT_SHARED_DATA_BASE <area_base_addr>
1042#define MCUBOOT_SHARED_DATA_SIZE <area_size_in_bytes>
1043```
1044
1045In the shared memory area all data entries are stored in a type-length-value
1046(TLV) format. Before adding the first data entry, the whole area is overwritten
1047with zeros and a TLV header is added at the beginning of the area during an
1048initialization phase. This TLV header contains a `tlv_magic` field with a value
1049of `SHARED_DATA_TLV_INFO_MAGIC` and a `tlv_tot_len` field which is indicating
1050the total length of shared TLV area including this header. The header is
1051followed by the the data TLV entries which are composed from a
1052`shared_data_tlv_entry` header and the data itself. In the data header there is
1053a `tlv_type` field which identifies the consumer of the entry (in the runtime
1054software) and specifies the subtype of that data item. More information about
1055the `tlv_type` field and data types can be found in the
1056`boot/bootutil/include/bootutil/boot_status.h` file. The type is followed by a
1057`tlv_len` field which indicates the size of the data entry in bytes, not
1058including the entry header. After this header structure comes the actual data.
1059
1060```c
1061/** Shared data TLV header. All fields in little endian. */
1062struct shared_data_tlv_header {
1063 uint16_t tlv_magic;
1064 uint16_t tlv_tot_len; /* size of whole TLV area (including this header) */
1065};
1066
1067/** Shared data TLV entry header format. All fields in little endian. */
1068struct shared_data_tlv_entry {
1069 uint16_t tlv_type;
1070 uint16_t tlv_len; /* TLV data length (not including this header). */
1071};
1072```
1073
1074The measured boot can be enabled with the `MCUBOOT_MEASURED_BOOT` config option.
1075When enabled, the `--boot_record` argument of the imgtool script must also be
1076used during the image signing process to add a BOOT_RECORD TLV to the image
1077manifest. This TLV contains the following attributes/measurements of the
1078image in CBOR encoded format:
1079
1080 * Software type (role of the software component)
1081 * Software version
1082 * Signer ID (identifies the signing authority)
1083 * Measurement value (hash of the image)
1084 * Measurement type (algorithm used to calculate the measurement value)
1085
1086The `sw_type` string that is passed as the `--boot_record` option's parameter
1087will be the value of the "Software type" attribute in the generated BOOT_RECORD
1088TLV. The target must also define the `MAX_BOOT_RECORD_SZ` macro which indicates
1089the maximum size of the CBOR encoded boot record in bytes.
1090During boot, MCUBoot will look for these TLVs (in case of multiple images) in
1091the manifests of the active images (the latest and validated) and copy the CBOR
1092encoded binary data to the shared data area. Preserving all these image
1093attributes from the boot stage for use by later runtime services (such as an
1094attestation service) is known as a measured boot.
1095
1096Setting the `MCUBOOT_DATA_SHARING` option enables the sharing of application
1097specific data using the same shared data area as for the measured boot. For
1098this, the target must provide a definition for the `boot_save_shared_data()`
1099function which is declared in `boot/bootutil/include/bootutil/boot_record.h`.
1100The `boot_add_data_to_shared_area()` function can be used for adding new TLV
1101entries to the shared data area.