David Brown | 17e20d1 | 2017-09-12 11:53:20 -0600 | [diff] [blame] | 1 | <!-- |
| 2 | Licensed to the Apache Software Foundation (ASF) under one |
| 3 | or more contributor license agreements. See the NOTICE file |
| 4 | distributed with this work for additional information |
| 5 | regarding copyright ownership. The ASF licenses this file |
| 6 | to you under the Apache License, Version 2.0 (the |
| 7 | "License"); you may not use this file except in compliance |
| 8 | with the License. You may obtain a copy of the License at |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 9 | |
David Brown | 17e20d1 | 2017-09-12 11:53:20 -0600 | [diff] [blame] | 10 | http://www.apache.org/licenses/LICENSE-2.0 |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 11 | |
David Brown | 17e20d1 | 2017-09-12 11:53:20 -0600 | [diff] [blame] | 12 | Unless required by applicable law or agreed to in writing, |
| 13 | software distributed under the License is distributed on an |
| 14 | "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | KIND, either express or implied. See the License for the |
| 16 | specific language governing permissions and limitations |
| 17 | under the License. |
| 18 | --> |
| 19 | |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 20 | <!-- |
| 21 | Modifications are Copyright (c) 2019 Arm Limited. |
| 22 | --> |
| 23 | |
David Brown | 17e20d1 | 2017-09-12 11:53:20 -0600 | [diff] [blame] | 24 | # Boot Loader |
| 25 | |
Fabio Utzig | d37d877 | 2019-12-03 10:32:18 -0300 | [diff] [blame] | 26 | ## [Summary](#summary) |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 27 | |
Fabio Utzig | ac83496 | 2017-07-20 13:20:48 -0300 | [diff] [blame] | 28 | mcuboot comprises two packages: |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 29 | |
David Brown | 17e20d1 | 2017-09-12 11:53:20 -0600 | [diff] [blame] | 30 | * The bootutil library (boot/bootutil) |
| 31 | * The boot application (each port has its own at boot/<port>) |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 32 | |
| 33 | The bootutil library performs most of the functions of a boot loader. In |
| 34 | particular, the piece that is missing is the final step of actually jumping to |
| 35 | the main image. This last step is instead implemented by the boot application. |
| 36 | Boot loader functionality is separated in this manner to enable unit testing of |
| 37 | the boot loader. A library can be unit tested, but an application can't. |
| 38 | Therefore, functionality is delegated to the bootutil library when possible. |
| 39 | |
Fabio Utzig | d37d877 | 2019-12-03 10:32:18 -0300 | [diff] [blame] | 40 | ## [Limitations](#limitations) |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 41 | |
| 42 | The boot loader currently only supports images with the following |
| 43 | characteristics: |
David Brown | 17e20d1 | 2017-09-12 11:53:20 -0600 | [diff] [blame] | 44 | * Built to run from flash. |
| 45 | * Built to run from a fixed location (i.e., not position-independent). |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 46 | |
Fabio Utzig | d37d877 | 2019-12-03 10:32:18 -0300 | [diff] [blame] | 47 | ## [Image Format](#image-format) |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 48 | |
| 49 | The following definitions describe the image format. |
| 50 | |
David Brown | 17e20d1 | 2017-09-12 11:53:20 -0600 | [diff] [blame] | 51 | ``` c |
Fabio Utzig | ea422c2 | 2017-09-11 11:02:47 -0300 | [diff] [blame] | 52 | #define IMAGE_MAGIC 0x96f3b83d |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 53 | |
| 54 | #define IMAGE_HEADER_SIZE 32 |
| 55 | |
| 56 | struct image_version { |
| 57 | uint8_t iv_major; |
| 58 | uint8_t iv_minor; |
| 59 | uint16_t iv_revision; |
| 60 | uint32_t iv_build_num; |
| 61 | }; |
| 62 | |
| 63 | /** Image header. All fields are in little endian byte order. */ |
| 64 | struct image_header { |
| 65 | uint32_t ih_magic; |
Fabio Utzig | ea422c2 | 2017-09-11 11:02:47 -0300 | [diff] [blame] | 66 | uint32_t ih_load_addr; |
David Vincze | e32483f | 2019-06-13 10:46:24 +0200 | [diff] [blame] | 67 | uint16_t ih_hdr_size; /* Size of image header (bytes). */ |
| 68 | uint16_t ih_protect_tlv_size; /* Size of protected TLV area (bytes). */ |
| 69 | uint32_t ih_img_size; /* Does not include header. */ |
| 70 | uint32_t ih_flags; /* IMAGE_F_[...]. */ |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 71 | struct image_version ih_ver; |
David Vincze | e32483f | 2019-06-13 10:46:24 +0200 | [diff] [blame] | 72 | uint32_t _pad1; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 73 | }; |
| 74 | |
Fabio Utzig | fd140ec | 2019-09-12 14:37:48 -0300 | [diff] [blame] | 75 | #define IMAGE_TLV_INFO_MAGIC 0x6907 |
| 76 | #define IMAGE_TLV_PROT_INFO_MAGIC 0x6908 |
| 77 | |
Fabio Utzig | ea422c2 | 2017-09-11 11:02:47 -0300 | [diff] [blame] | 78 | /** Image TLV header. All fields in little endian. */ |
| 79 | struct image_tlv_info { |
| 80 | uint16_t it_magic; |
| 81 | uint16_t it_tlv_tot; /* size of TLV area (including tlv_info header) */ |
| 82 | }; |
| 83 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 84 | /** Image trailer TLV format. All fields in little endian. */ |
| 85 | struct image_tlv { |
| 86 | uint8_t it_type; /* IMAGE_TLV_[...]. */ |
| 87 | uint8_t _pad; |
Marti Bolivar | 49b2917 | 2017-08-04 14:50:51 -0400 | [diff] [blame] | 88 | uint16_t it_len; /* Data length (not including TLV header). */ |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 89 | }; |
| 90 | |
| 91 | /* |
| 92 | * Image header flags. |
| 93 | */ |
Marti Bolivar | 7c057e9 | 2017-08-04 14:46:39 -0400 | [diff] [blame] | 94 | #define IMAGE_F_PIC 0x00000001 /* Not supported. */ |
Marti Bolivar | 7c057e9 | 2017-08-04 14:46:39 -0400 | [diff] [blame] | 95 | #define IMAGE_F_NON_BOOTABLE 0x00000010 /* Split image app. */ |
Fabio Utzig | ea422c2 | 2017-09-11 11:02:47 -0300 | [diff] [blame] | 96 | #define IMAGE_F_RAM_LOAD 0x00000020 |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 97 | |
| 98 | /* |
| 99 | * Image trailer TLV types. |
| 100 | */ |
Fabio Utzig | ea422c2 | 2017-09-11 11:02:47 -0300 | [diff] [blame] | 101 | #define IMAGE_TLV_KEYHASH 0x01 /* hash of the public key */ |
David Brown | 27648b8 | 2017-08-31 10:40:29 -0600 | [diff] [blame] | 102 | #define IMAGE_TLV_SHA256 0x10 /* SHA256 of image hdr and body */ |
Marko Kiiskila | 8dd56f3 | 2017-08-22 21:40:49 -0700 | [diff] [blame] | 103 | #define IMAGE_TLV_RSA2048_PSS 0x20 /* RSA2048 of hash output */ |
David Brown | 27648b8 | 2017-08-31 10:40:29 -0600 | [diff] [blame] | 104 | #define IMAGE_TLV_ECDSA224 0x21 /* ECDSA of hash output */ |
| 105 | #define IMAGE_TLV_ECDSA256 0x22 /* ECDSA of hash output */ |
Fabio Utzig | 3501c01 | 2019-05-13 15:07:25 -0700 | [diff] [blame] | 106 | #define IMAGE_TLV_RSA3072_PSS 0x23 /* RSA3072 of hash output */ |
Fabio Utzig | 195411f | 2019-06-28 07:48:21 -0300 | [diff] [blame] | 107 | #define IMAGE_TLV_ED25519 0x24 /* ED25519 of hash output */ |
David Vincze | e32483f | 2019-06-13 10:46:24 +0200 | [diff] [blame] | 108 | #define IMAGE_TLV_ENC_RSA2048 0x30 /* Key encrypted with RSA-OAEP-2048 */ |
| 109 | #define IMAGE_TLV_ENC_KW128 0x31 /* Key encrypted with AES-KW-128 */ |
Fabio Utzig | b3f058c | 2019-10-30 10:51:06 -0300 | [diff] [blame] | 110 | #define IMAGE_TLV_ENC_EC256 0x32 /* Key encrypted with ECIES P256 */ |
David Vincze | e32483f | 2019-06-13 10:46:24 +0200 | [diff] [blame] | 111 | #define IMAGE_TLV_DEPENDENCY 0x40 /* Image depends on other image */ |
David Brown | 17e20d1 | 2017-09-12 11:53:20 -0600 | [diff] [blame] | 112 | ``` |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 113 | |
| 114 | Optional type-length-value records (TLVs) containing image metadata are placed |
| 115 | after the end of the image. |
| 116 | |
David Vincze | e32483f | 2019-06-13 10:46:24 +0200 | [diff] [blame] | 117 | The `ih_protect_tlv_size` field indicates the length of the protected TLV area. |
Fabio Utzig | fd140ec | 2019-09-12 14:37:48 -0300 | [diff] [blame] | 118 | If protected TLVs are present then a TLV info header with magic equal to |
| 119 | `IMAGE_TLV_PROT_INFO_MAGIC` must be present and the protected TLVs (plus the |
| 120 | info header itself) have to be included in the hash calculation. Otherwise the |
| 121 | hash is only calculated over the image header and the image itself. In this |
David Vincze | e32483f | 2019-06-13 10:46:24 +0200 | [diff] [blame] | 122 | case the value of the `ih_protect_tlv_size` field is 0. |
| 123 | |
David Brown | 17e20d1 | 2017-09-12 11:53:20 -0600 | [diff] [blame] | 124 | The `ih_hdr_size` field indicates the length of the header, and therefore the |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 125 | offset of the image itself. This field provides for backwards compatibility in |
| 126 | case of changes to the format of the image header. |
| 127 | |
Fabio Utzig | d37d877 | 2019-12-03 10:32:18 -0300 | [diff] [blame] | 128 | ## [Flash Map](#flash-map) |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 129 | |
Fabio Utzig | ac83496 | 2017-07-20 13:20:48 -0300 | [diff] [blame] | 130 | A device's flash is partitioned according to its _flash map_. At a high |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 131 | level, the flash map maps numeric IDs to _flash areas_. A flash area is a |
| 132 | region of disk with the following properties: |
David Brown | 17e20d1 | 2017-09-12 11:53:20 -0600 | [diff] [blame] | 133 | 1. An area can be fully erased without affecting any other areas. |
| 134 | 2. A write to one area does not restrict writes to other areas. |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 135 | |
Marti Bolivar | 4e64d56 | 2017-08-04 14:53:33 -0400 | [diff] [blame] | 136 | The boot loader uses the following flash area IDs: |
David Vincze | b75c12a | 2019-03-22 14:58:33 +0100 | [diff] [blame] | 137 | ```c |
| 138 | /* Independent from multiple image boot */ |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 139 | #define FLASH_AREA_BOOTLOADER 0 |
David Vincze | b75c12a | 2019-03-22 14:58:33 +0100 | [diff] [blame] | 140 | #define FLASH_AREA_IMAGE_SCRATCH 3 |
| 141 | ``` |
| 142 | ```c |
| 143 | /* If the boot loader is working with the first image */ |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 144 | #define FLASH_AREA_IMAGE_PRIMARY 1 |
| 145 | #define FLASH_AREA_IMAGE_SECONDARY 2 |
David Vincze | b75c12a | 2019-03-22 14:58:33 +0100 | [diff] [blame] | 146 | ``` |
| 147 | ```c |
| 148 | /* If the boot loader is working with the second image */ |
| 149 | #define FLASH_AREA_IMAGE_PRIMARY 5 |
| 150 | #define FLASH_AREA_IMAGE_SECONDARY 6 |
David Brown | 17e20d1 | 2017-09-12 11:53:20 -0600 | [diff] [blame] | 151 | ``` |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 152 | |
Marti Bolivar | 4e64d56 | 2017-08-04 14:53:33 -0400 | [diff] [blame] | 153 | The bootloader area contains the bootloader image itself. The other areas are |
David Vincze | b75c12a | 2019-03-22 14:58:33 +0100 | [diff] [blame] | 154 | described in subsequent sections. The flash could contain multiple executable |
| 155 | images therefore the flash area IDs of primary and secondary areas are mapped |
| 156 | based on the number of the active image (on which the bootloader is currently |
| 157 | working). |
Marti Bolivar | 4e64d56 | 2017-08-04 14:53:33 -0400 | [diff] [blame] | 158 | |
Fabio Utzig | d37d877 | 2019-12-03 10:32:18 -0300 | [diff] [blame] | 159 | ## [Image Slots](#image-slots) |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 160 | |
David Vincze | b75c12a | 2019-03-22 14:58:33 +0100 | [diff] [blame] | 161 | A portion of the flash memory can be partitioned into multiple image areas, each |
| 162 | contains two image slots: a primary slot and a secondary slot. |
| 163 | The boot loader will only run an image from the primary slot, so images must be |
| 164 | built such that they can run from that fixed location in flash. If the boot |
| 165 | loader needs to run the image resident in the secondary slot, it must copy its |
| 166 | contents into the primary slot before doing so, either by swapping the two |
| 167 | images or by overwriting the contents of the primary slot. The bootloader |
| 168 | supports either swap- or overwrite-based image upgrades, but must be configured |
| 169 | at build time to choose one of these two strategies. |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 170 | |
David Vincze | b75c12a | 2019-03-22 14:58:33 +0100 | [diff] [blame] | 171 | In addition to the slots of image areas, the boot loader requires a scratch |
| 172 | area to allow for reliable image swapping. The scratch area must have a size |
| 173 | that is enough to store at least the largest sector that is going to be swapped. |
| 174 | Many devices have small equally sized flash sectors, eg 4K, while others have |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 175 | variable sized sectors where the largest sectors might be 128K or 256K, so the |
| 176 | scratch must be big enough to store that. The scratch is only ever used when |
| 177 | swapping firmware, which means only when doing an upgrade. Given that, the main |
| 178 | reason for using a larger size for the scratch is that flash wear will be more |
| 179 | evenly distributed, because a single sector would be written twice the number of |
| 180 | times than using two sectors, for example. To evaluate the ideal size of the |
| 181 | scratch for your use case the following parameters are relevant: |
Fabio Utzig | a722f5a | 2017-12-12 14:04:53 -0200 | [diff] [blame] | 182 | |
| 183 | * the ratio of image size / scratch size |
| 184 | * the number of erase cycles supported by the flash hardware |
| 185 | |
| 186 | The image size is used (instead of slot size) because only the slot's sectors |
| 187 | that are actually used for storing the image are copied. The image/scratch ratio |
| 188 | is the number of times the scratch will be erased on every upgrade. The number |
| 189 | of erase cycles divided by the image/scratch ratio will give you the number of |
| 190 | times an upgrade can be performed before the device goes out of spec. |
| 191 | |
| 192 | ``` |
| 193 | num_upgrades = number_of_erase_cycles / (image_size / scratch_size) |
| 194 | ``` |
| 195 | |
| 196 | Let's assume, for example, a device with 10000 erase cycles, an image size of |
| 197 | 150K and a scratch of 4K (usual minimum size of 4K sector devices). This would |
| 198 | result in a total of: |
| 199 | |
| 200 | `10000 / (150 / 4) ~ 267` |
| 201 | |
| 202 | Increasing the scratch to 16K would give us: |
| 203 | |
| 204 | `10000 / (150 / 16) ~ 1067` |
| 205 | |
| 206 | There is no *best* ratio, as the right size is use-case dependent. Factors to |
| 207 | consider include the number of times a device will be upgraded both in the field |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 208 | and during development, as well as any desired safety margin on the |
| 209 | manufacturer's specified number of erase cycles. In general, using a ratio that |
| 210 | allows hundreds to thousands of field upgrades in production is recommended. |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 211 | |
Marti Bolivar | a91674f | 2017-08-04 14:56:08 -0400 | [diff] [blame] | 212 | The overwrite upgrade strategy is substantially simpler to implement than the |
| 213 | image swapping strategy, especially since the bootloader must work properly |
| 214 | even when it is reset during the middle of an image swap. For this reason, the |
| 215 | rest of the document describes its behavior when configured to swap images |
| 216 | during an upgrade. |
| 217 | |
Fabio Utzig | d37d877 | 2019-12-03 10:32:18 -0300 | [diff] [blame] | 218 | ## [Boot Swap Types](#boot-swap-types) |
Christopher Collins | fd7eb5c | 2016-12-21 13:46:08 -0800 | [diff] [blame] | 219 | |
Marti Bolivar | 048d8d8 | 2017-08-04 17:14:24 -0400 | [diff] [blame] | 220 | When the device first boots under normal circumstances, there is an up-to-date |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 221 | firmware image in each primary slot, which mcuboot can validate and then |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 222 | chain-load. In this case, no image swaps are necessary. During device upgrades, |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 223 | however, new candidate image(s) is present in the secondary slot(s), which |
| 224 | mcuboot must swap into the primary slot(s) before booting as discussed above. |
Christopher Collins | fd7eb5c | 2016-12-21 13:46:08 -0800 | [diff] [blame] | 225 | |
Marti Bolivar | 048d8d8 | 2017-08-04 17:14:24 -0400 | [diff] [blame] | 226 | Upgrading an old image with a new one by swapping can be a two-step process. In |
| 227 | this process, mcuboot performs a "test" swap of image data in flash and boots |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 228 | the new image or it will be executed during operation. The new image can then |
| 229 | update the contents of flash at runtime to mark itself "OK", and mcuboot will |
| 230 | then still choose to run it during the next boot. When this happens, the swap is |
| 231 | made "permanent". If this doesn't happen, mcuboot will perform a "revert" swap |
| 232 | during the next boot by swapping the image(s) back into its original location(s) |
| 233 | , and attempting to boot the old image(s). |
Christopher Collins | fd7eb5c | 2016-12-21 13:46:08 -0800 | [diff] [blame] | 234 | |
Marti Bolivar | 048d8d8 | 2017-08-04 17:14:24 -0400 | [diff] [blame] | 235 | Depending on the use case, the first swap can also be made permanent directly. |
| 236 | In this case, mcuboot will never attempt to revert the images on the next reset. |
Christopher Collins | fd7eb5c | 2016-12-21 13:46:08 -0800 | [diff] [blame] | 237 | |
Marti Bolivar | 048d8d8 | 2017-08-04 17:14:24 -0400 | [diff] [blame] | 238 | Test swaps are supported to provide a rollback mechanism to prevent devices |
| 239 | from becoming "bricked" by bad firmware. If the device crashes immediately |
| 240 | upon booting a new (bad) image, mcuboot will revert to the old (working) image |
| 241 | at the next device reset, rather than booting the bad image again. This allows |
| 242 | device firmware to make test swaps permanent only after performing a self-test |
| 243 | routine. |
Christopher Collins | fd7eb5c | 2016-12-21 13:46:08 -0800 | [diff] [blame] | 244 | |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 245 | On startup, mcuboot inspects the contents of flash to decide for each images |
| 246 | which of these "swap types" to perform; this decision determines how it |
| 247 | proceeds. |
Christopher Collins | fd7eb5c | 2016-12-21 13:46:08 -0800 | [diff] [blame] | 248 | |
Marti Bolivar | 048d8d8 | 2017-08-04 17:14:24 -0400 | [diff] [blame] | 249 | The possible swap types, and their meanings, are: |
Christopher Collins | fd7eb5c | 2016-12-21 13:46:08 -0800 | [diff] [blame] | 250 | |
David Brown | 17e20d1 | 2017-09-12 11:53:20 -0600 | [diff] [blame] | 251 | - `BOOT_SWAP_TYPE_NONE`: The "usual" or "no upgrade" case; attempt to boot the |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 252 | contents of the primary slot. |
Christopher Collins | fd7eb5c | 2016-12-21 13:46:08 -0800 | [diff] [blame] | 253 | |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 254 | - `BOOT_SWAP_TYPE_TEST`: Boot the contents of the secondary slot by swapping |
| 255 | images. Unless the swap is made permanent, revert back on the next boot. |
Christopher Collins | fd7eb5c | 2016-12-21 13:46:08 -0800 | [diff] [blame] | 256 | |
David Brown | 17e20d1 | 2017-09-12 11:53:20 -0600 | [diff] [blame] | 257 | - `BOOT_SWAP_TYPE_PERM`: Permanently swap images, and boot the upgraded image |
Marti Bolivar | 048d8d8 | 2017-08-04 17:14:24 -0400 | [diff] [blame] | 258 | firmware. |
Christopher Collins | fd7eb5c | 2016-12-21 13:46:08 -0800 | [diff] [blame] | 259 | |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 260 | - `BOOT_SWAP_TYPE_REVERT`: A previous test swap was not made permanent; |
| 261 | swap back to the old image whose data are now in the secondary slot. If the |
| 262 | old image marks itself "OK" when it boots, the next boot will have swap type |
| 263 | `BOOT_SWAP_TYPE_NONE`. |
Christopher Collins | fd7eb5c | 2016-12-21 13:46:08 -0800 | [diff] [blame] | 264 | |
David Brown | 17e20d1 | 2017-09-12 11:53:20 -0600 | [diff] [blame] | 265 | - `BOOT_SWAP_TYPE_FAIL`: Swap failed because image to be run is not valid. |
Marti Bolivar | 048d8d8 | 2017-08-04 17:14:24 -0400 | [diff] [blame] | 266 | |
David Brown | 17e20d1 | 2017-09-12 11:53:20 -0600 | [diff] [blame] | 267 | - `BOOT_SWAP_TYPE_PANIC`: Swapping encountered an unrecoverable error. |
Marti Bolivar | 048d8d8 | 2017-08-04 17:14:24 -0400 | [diff] [blame] | 268 | |
| 269 | The "swap type" is a high-level representation of the outcome of the |
| 270 | boot. Subsequent sections describe how mcuboot determines the swap type from |
| 271 | the bit-level contents of flash. |
Christopher Collins | fd7eb5c | 2016-12-21 13:46:08 -0800 | [diff] [blame] | 272 | |
Fabio Utzig | d37d877 | 2019-12-03 10:32:18 -0300 | [diff] [blame] | 273 | ## [Image Trailer](#image-trailer) |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 274 | |
Fabio Utzig | 86fe4b2 | 2017-07-28 18:56:29 -0300 | [diff] [blame] | 275 | For the bootloader to be able to determine the current state and what actions |
Marti Bolivar | 4281803 | 2017-08-04 15:45:01 -0400 | [diff] [blame] | 276 | should be taken during the current boot operation, it uses metadata stored in |
| 277 | the image flash areas. While swapping, some of this metadata is temporarily |
| 278 | copied into and out of the scratch area. |
| 279 | |
| 280 | This metadata is located at the end of the image flash areas, and is called an |
| 281 | image trailer. An image trailer has the following structure: |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 282 | |
David Brown | 17e20d1 | 2017-09-12 11:53:20 -0600 | [diff] [blame] | 283 | ``` |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 284 | 0 1 2 3 |
| 285 | 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 |
| 286 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 287 | ~ ~ |
Fabio Utzig | 2c05f1b | 2018-04-04 10:35:17 -0300 | [diff] [blame] | 288 | ~ Swap status (BOOT_MAX_IMG_SECTORS * min-write-size * 3) ~ |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 289 | ~ ~ |
| 290 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
Christopher Collins | a1c1204 | 2019-05-23 14:00:28 -0700 | [diff] [blame] | 291 | | Encryption key 0 (16 octets) [*] | |
| 292 | | | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 293 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
Christopher Collins | a1c1204 | 2019-05-23 14:00:28 -0700 | [diff] [blame] | 294 | | Encryption key 1 (16 octets) [*] | |
| 295 | | | |
Fabio Utzig | 5bd4e58 | 2017-07-20 08:55:38 -0300 | [diff] [blame] | 296 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
Christopher Collins | a1c1204 | 2019-05-23 14:00:28 -0700 | [diff] [blame] | 297 | | Swap size (4 octets) | |
Fabio Utzig | ea422c2 | 2017-09-11 11:02:47 -0300 | [diff] [blame] | 298 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
David Vincze | e245347 | 2019-06-17 12:31:59 +0200 | [diff] [blame] | 299 | | Swap info | 0xff padding (7 octets) | |
Fabio Utzig | ea422c2 | 2017-09-11 11:02:47 -0300 | [diff] [blame] | 300 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
Christopher Collins | a1c1204 | 2019-05-23 14:00:28 -0700 | [diff] [blame] | 301 | | Copy done | 0xff padding (7 octets) | |
| 302 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 303 | | Image OK | 0xff padding (7 octets) | |
| 304 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 305 | | MAGIC (16 octets) | |
| 306 | | | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 307 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
David Brown | 17e20d1 | 2017-09-12 11:53:20 -0600 | [diff] [blame] | 308 | ``` |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 309 | |
Christopher Collins | a1c1204 | 2019-05-23 14:00:28 -0700 | [diff] [blame] | 310 | [*]: Only present if the encryption option is enabled (`MCUBOOT_ENC_IMAGES`). |
| 311 | |
Marti Bolivar | 4281803 | 2017-08-04 15:45:01 -0400 | [diff] [blame] | 312 | The offset immediately following such a record represents the start of the next |
| 313 | flash area. |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 314 | |
| 315 | Note: "min-write-size" is a property of the flash hardware. If the hardware |
| 316 | allows individual bytes to be written at arbitrary addresses, then |
| 317 | min-write-size is 1. If the hardware only allows writes at even addresses, |
| 318 | then min-write-size is 2, and so on. |
| 319 | |
Marti Bolivar | 1dcb685 | 2017-08-04 15:59:32 -0400 | [diff] [blame] | 320 | An image trailer contains the following fields: |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 321 | |
Marti Bolivar | 1dcb685 | 2017-08-04 15:59:32 -0400 | [diff] [blame] | 322 | 1. Swap status: A series of records which records the progress of an image |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 323 | swap. To swap entire images, data are swapped between the two image areas |
| 324 | one or more sectors at a time, like this: |
Marti Bolivar | 1dcb685 | 2017-08-04 15:59:32 -0400 | [diff] [blame] | 325 | |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 326 | - sector data in the primary slot is copied into scratch, then erased |
| 327 | - sector data in the secondary slot is copied into the primary slot, |
| 328 | then erased |
| 329 | - sector data in scratch is copied into the secondary slot |
Marti Bolivar | 1dcb685 | 2017-08-04 15:59:32 -0400 | [diff] [blame] | 330 | |
| 331 | As it swaps images, the bootloader updates the swap status field in a way that |
| 332 | allows it to compute how far this swap operation has progressed for each |
| 333 | sector. The swap status field can thus used to resume a swap operation if the |
| 334 | bootloader is halted while a swap operation is ongoing and later reset. The |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 335 | `BOOT_MAX_IMG_SECTORS` value is the configurable maximum number of sectors |
| 336 | mcuboot supports for each image; its value defaults to 128, but allows for |
| 337 | either decreasing this size, to limit RAM usage, or to increase it in devices |
| 338 | that have massive amounts of Flash or very small sized sectors and thus require |
| 339 | a bigger configuration to allow for the handling of all slot's sectors. |
| 340 | The factor of min-write-sz is due to the behavior of flash hardware. The factor |
| 341 | of 3 is explained below. |
Fabio Utzig | 5bd4e58 | 2017-07-20 08:55:38 -0300 | [diff] [blame] | 342 | |
Christopher Collins | a1c1204 | 2019-05-23 14:00:28 -0700 | [diff] [blame] | 343 | 2. Encryption keys: key-encrypting keys (KEKs). These keys are needed for |
| 344 | image encryption and decryption. See the |
| 345 | [encrypted images](encrypted_images.md) document for more information. |
| 346 | |
| 347 | 3. Swap size: When beginning a new swap operation, the total size that needs |
Håkon Øye Amundsen | cbf3047 | 2019-07-24 08:34:03 +0000 | [diff] [blame] | 348 | to be swapped (based on the slot with largest image + TLVs) is written to |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 349 | this location for easier recovery in case of a reset while performing the |
| 350 | swap. |
Fabio Utzig | ea422c2 | 2017-09-11 11:02:47 -0300 | [diff] [blame] | 351 | |
Håkon Øye Amundsen | cbf3047 | 2019-07-24 08:34:03 +0000 | [diff] [blame] | 352 | 4. Swap info: A single byte which encodes the following information: |
David Vincze | e245347 | 2019-06-17 12:31:59 +0200 | [diff] [blame] | 353 | - Swap type: Stored in bits 0-3. Indicating the type of swap operation in |
| 354 | progress. When mcuboot resumes an interrupted swap, it uses this field to |
| 355 | determine the type of operation to perform. This field contains one of the |
| 356 | following values in the table below. |
| 357 | - Image number: Stored in bits 4-7. It has always 0 value at single image |
| 358 | boot. In case of multi image boot it indicates, which image was swapped when |
| 359 | interrupt happened. The same scratch area is used during in case of all |
| 360 | image swap operation. Therefore this field is used to determine which image |
| 361 | the trailer belongs to if boot status is found on scratch area when the swap |
| 362 | operation is resumed. |
Christopher Collins | a1c1204 | 2019-05-23 14:00:28 -0700 | [diff] [blame] | 363 | |
| 364 | | Name | Value | |
| 365 | | ------------------------- | ----- | |
| 366 | | `BOOT_SWAP_TYPE_TEST` | 2 | |
| 367 | | `BOOT_SWAP_TYPE_PERM` | 3 | |
| 368 | | `BOOT_SWAP_TYPE_REVERT` | 4 | |
| 369 | |
| 370 | |
| 371 | 5. Copy done: A single byte indicating whether the image in this slot is |
David Brown | 17e20d1 | 2017-09-12 11:53:20 -0600 | [diff] [blame] | 372 | complete (0x01=done; 0xff=not done). |
Fabio Utzig | 5bd4e58 | 2017-07-20 08:55:38 -0300 | [diff] [blame] | 373 | |
Christopher Collins | a1c1204 | 2019-05-23 14:00:28 -0700 | [diff] [blame] | 374 | 6. Image OK: A single byte indicating whether the image in this slot has been |
David Brown | 17e20d1 | 2017-09-12 11:53:20 -0600 | [diff] [blame] | 375 | confirmed as good by the user (0x01=confirmed; 0xff=not confirmed). |
Fabio Utzig | 5bd4e58 | 2017-07-20 08:55:38 -0300 | [diff] [blame] | 376 | |
Christopher Collins | a1c1204 | 2019-05-23 14:00:28 -0700 | [diff] [blame] | 377 | 7. MAGIC: The following 16 bytes, written in host-byte-order: |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 378 | |
David Brown | 17e20d1 | 2017-09-12 11:53:20 -0600 | [diff] [blame] | 379 | ``` c |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 380 | const uint32_t boot_img_magic[4] = { |
| 381 | 0xf395c277, |
| 382 | 0x7fefd260, |
| 383 | 0x0f505235, |
| 384 | 0x8079b62c, |
| 385 | }; |
David Brown | 17e20d1 | 2017-09-12 11:53:20 -0600 | [diff] [blame] | 386 | ``` |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 387 | |
Fabio Utzig | d37d877 | 2019-12-03 10:32:18 -0300 | [diff] [blame] | 388 | ## [IMAGE TRAILERS](#image-trailers) |
Fabio Utzig | 86fe4b2 | 2017-07-28 18:56:29 -0300 | [diff] [blame] | 389 | |
Marti Bolivar | 048d8d8 | 2017-08-04 17:14:24 -0400 | [diff] [blame] | 390 | At startup, the boot loader determines the boot swap type by inspecting the |
| 391 | image trailers. When using the term "image trailers" what is meant is the |
| 392 | aggregate information provided by both image slot's trailers. |
Fabio Utzig | 86fe4b2 | 2017-07-28 18:56:29 -0300 | [diff] [blame] | 393 | |
Fabio Utzig | d37d877 | 2019-12-03 10:32:18 -0300 | [diff] [blame] | 394 | ### [New swaps (non-resumes)](#new-swaps-non-resumes) |
Christopher Collins | a1c1204 | 2019-05-23 14:00:28 -0700 | [diff] [blame] | 395 | |
| 396 | For new swaps, mcuboot must inspect a collection of fields to determine which |
| 397 | swap operation to perform. |
| 398 | |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 399 | The image trailers records are structured around the limitations imposed by |
| 400 | flash hardware. As a consequence, they do not have a very intuitive design, and |
| 401 | it is difficult to get a sense of the state of the device just by looking at the |
Marti Bolivar | 048d8d8 | 2017-08-04 17:14:24 -0400 | [diff] [blame] | 402 | image trailers. It is better to map all the possible trailer states to the swap |
| 403 | types described above via a set of tables. These tables are reproduced below. |
Fabio Utzig | 86fe4b2 | 2017-07-28 18:56:29 -0300 | [diff] [blame] | 404 | |
| 405 | Note: An important caveat about the tables described below is that they must |
| 406 | be evaluated in the order presented here. Lower state numbers must have a |
| 407 | higher priority when testing the image trailers. |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 408 | |
David Brown | 17e20d1 | 2017-09-12 11:53:20 -0600 | [diff] [blame] | 409 | ``` |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 410 | State I |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 411 | | primary slot | secondary slot | |
| 412 | -----------------+--------------+----------------| |
| 413 | magic | Any | Good | |
| 414 | image-ok | Any | Unset | |
| 415 | copy-done | Any | Any | |
| 416 | -----------------+--------------+----------------' |
| 417 | result: BOOT_SWAP_TYPE_TEST | |
| 418 | -------------------------------------------------' |
Fabio Utzig | 5bd4e58 | 2017-07-20 08:55:38 -0300 | [diff] [blame] | 419 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 420 | |
Fabio Utzig | 86fe4b2 | 2017-07-28 18:56:29 -0300 | [diff] [blame] | 421 | State II |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 422 | | primary slot | secondary slot | |
| 423 | -----------------+--------------+----------------| |
| 424 | magic | Any | Good | |
| 425 | image-ok | Any | 0x01 | |
| 426 | copy-done | Any | Any | |
| 427 | -----------------+--------------+----------------' |
| 428 | result: BOOT_SWAP_TYPE_PERM | |
| 429 | -------------------------------------------------' |
Fabio Utzig | 5bd4e58 | 2017-07-20 08:55:38 -0300 | [diff] [blame] | 430 | |
Christopher Collins | fd7eb5c | 2016-12-21 13:46:08 -0800 | [diff] [blame] | 431 | |
Fabio Utzig | 86fe4b2 | 2017-07-28 18:56:29 -0300 | [diff] [blame] | 432 | State III |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 433 | | primary slot | secondary slot | |
| 434 | -----------------+--------------+----------------| |
| 435 | magic | Good | Unset | |
| 436 | image-ok | 0xff | Any | |
| 437 | copy-done | 0x01 | Any | |
| 438 | -----------------+--------------+----------------' |
| 439 | result: BOOT_SWAP_TYPE_REVERT | |
| 440 | -------------------------------------------------' |
David Brown | 17e20d1 | 2017-09-12 11:53:20 -0600 | [diff] [blame] | 441 | ``` |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 442 | |
Marti Bolivar | 048d8d8 | 2017-08-04 17:14:24 -0400 | [diff] [blame] | 443 | Any of the above three states results in mcuboot attempting to swap images. |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 444 | |
Marti Bolivar | 048d8d8 | 2017-08-04 17:14:24 -0400 | [diff] [blame] | 445 | Otherwise, mcuboot does not attempt to swap images, resulting in one of the |
| 446 | other three swap types, as illustrated by State IV. |
Fabio Utzig | 86fe4b2 | 2017-07-28 18:56:29 -0300 | [diff] [blame] | 447 | |
David Brown | 17e20d1 | 2017-09-12 11:53:20 -0600 | [diff] [blame] | 448 | ``` |
Fabio Utzig | 86fe4b2 | 2017-07-28 18:56:29 -0300 | [diff] [blame] | 449 | State IV |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 450 | | primary slot | secondary slot | |
| 451 | -----------------+--------------+----------------| |
| 452 | magic | Any | Any | |
| 453 | image-ok | Any | Any | |
| 454 | copy-done | Any | Any | |
| 455 | -----------------+--------------+----------------' |
| 456 | result: BOOT_SWAP_TYPE_NONE, | |
| 457 | BOOT_SWAP_TYPE_FAIL, or | |
| 458 | BOOT_SWAP_TYPE_PANIC | |
| 459 | -------------------------------------------------' |
David Brown | 17e20d1 | 2017-09-12 11:53:20 -0600 | [diff] [blame] | 460 | ``` |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 461 | |
Marti Bolivar | 048d8d8 | 2017-08-04 17:14:24 -0400 | [diff] [blame] | 462 | In State IV, when no errors occur, mcuboot will attempt to boot the contents of |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 463 | the primary slot directly, and the result is `BOOT_SWAP_TYPE_NONE`. If the image |
| 464 | in the primary slot is not valid, the result is `BOOT_SWAP_TYPE_FAIL`. If a |
| 465 | fatal error occurs during boot, the result is `BOOT_SWAP_TYPE_PANIC`. If the |
| 466 | result is either `BOOT_SWAP_TYPE_FAIL` or `BOOT_SWAP_TYPE_PANIC`, mcuboot hangs |
| 467 | rather than booting an invalid or compromised image. |
Fabio Utzig | 86fe4b2 | 2017-07-28 18:56:29 -0300 | [diff] [blame] | 468 | |
Marti Bolivar | 048d8d8 | 2017-08-04 17:14:24 -0400 | [diff] [blame] | 469 | Note: An important caveat to the above is the result when a swap is requested |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 470 | and the image in the secondary slot fails to validate, due to a hashing or |
| 471 | signing error. This state behaves as State IV with the extra action of |
| 472 | marking the image in the primary slot as "OK", to prevent further attempts |
| 473 | to swap. |
Fabio Utzig | 86fe4b2 | 2017-07-28 18:56:29 -0300 | [diff] [blame] | 474 | |
Fabio Utzig | d37d877 | 2019-12-03 10:32:18 -0300 | [diff] [blame] | 475 | ### [Resumed swaps](#resumed-swaps) |
Christopher Collins | a1c1204 | 2019-05-23 14:00:28 -0700 | [diff] [blame] | 476 | |
| 477 | If mcuboot determines that it is resuming an interrupted swap (i.e., a reset |
| 478 | occurred mid-swap), it fully determines the operation to resume by reading the |
David Vincze | e245347 | 2019-06-17 12:31:59 +0200 | [diff] [blame] | 479 | `swap info` field from the active trailer and extracting the swap type from bits |
| 480 | 0-3. The set of tables in the previous section are not necessary in the resume |
| 481 | case. |
Christopher Collins | a1c1204 | 2019-05-23 14:00:28 -0700 | [diff] [blame] | 482 | |
Fabio Utzig | d37d877 | 2019-12-03 10:32:18 -0300 | [diff] [blame] | 483 | ## [High-Level Operation](#high-level-operation) |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 484 | |
| 485 | With the terms defined, we can now explore the boot loader's operation. First, |
| 486 | a high-level overview of the boot process is presented. Then, the following |
| 487 | sections describe each step of the process in more detail. |
| 488 | |
| 489 | Procedure: |
| 490 | |
David Brown | 17e20d1 | 2017-09-12 11:53:20 -0600 | [diff] [blame] | 491 | 1. Inspect swap status region; is an interrupted swap being resumed? |
Fabio Utzig | 75b3441 | 2019-09-06 08:30:43 -0300 | [diff] [blame] | 492 | + Yes: Complete the partial swap operation; skip to step 3. |
| 493 | + No: Proceed to step 2. |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 494 | |
David Brown | 17e20d1 | 2017-09-12 11:53:20 -0600 | [diff] [blame] | 495 | 2. Inspect image trailers; is a swap requested? |
Fabio Utzig | 75b3441 | 2019-09-06 08:30:43 -0300 | [diff] [blame] | 496 | + Yes: |
| 497 | 1. Is the requested image valid (integrity and security check)? |
| 498 | + Yes. |
| 499 | a. Perform swap operation. |
| 500 | b. Persist completion of swap procedure to image trailers. |
| 501 | c. Proceed to step 3. |
| 502 | + No. |
| 503 | a. Erase invalid image. |
| 504 | b. Persist failure of swap procedure to image trailers. |
| 505 | c. Proceed to step 3. |
| 506 | |
| 507 | + No: Proceed to step 3. |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 508 | |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 509 | 3. Boot into image in primary slot. |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 510 | |
Fabio Utzig | d37d877 | 2019-12-03 10:32:18 -0300 | [diff] [blame] | 511 | ### [Multiple Image Boot](#multiple-image-boot) |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 512 | |
| 513 | When the flash contains multiple executable images the boot loader's operation |
| 514 | is a bit more complex but similar to the previously described procedure with |
| 515 | one image. Every image can be updated independently therefore the flash is |
| 516 | partitioned further to arrange two slots for each image. |
| 517 | ``` |
| 518 | +--------------------+ |
| 519 | | MCUBoot | |
| 520 | +--------------------+ |
| 521 | ~~~~~ <- memory might be not contiguous |
| 522 | +--------------------+ |
| 523 | | Image 0 | |
| 524 | | primary slot | |
| 525 | +--------------------+ |
| 526 | | Image 0 | |
| 527 | | secondary slot | |
| 528 | +--------------------+ |
| 529 | ~~~~~ <- memory might be not contiguous |
| 530 | +--------------------+ |
| 531 | | Image N | |
| 532 | | primary slot | |
| 533 | +--------------------+ |
| 534 | | Image N | |
| 535 | | secondary slot | |
| 536 | +--------------------+ |
| 537 | | Scratch | |
| 538 | +--------------------+ |
| 539 | ``` |
David Vincze | e32483f | 2019-06-13 10:46:24 +0200 | [diff] [blame] | 540 | MCUBoot is also capable of handling dependencies between images. For example |
| 541 | if an image needs to be reverted it might be necessary to revert another one too |
| 542 | (e.g. due to API incompatibilities) or simply to prevent from being updated |
| 543 | because of an unsatisfied dependency. Therefore all aborted swaps have to be |
| 544 | completed and all the swap types have to be determined for each image before |
| 545 | the dependency checks. Dependency handling is described in more detail in a |
| 546 | following section. The multiple image boot procedure is organized in loops which |
| 547 | iterate over all the firmware images. The high-level overview of the boot |
| 548 | process is presented below. |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 549 | |
| 550 | + ###### Loop 1. Iterate over all images |
| 551 | 1. Inspect swap status region of current image; is an interrupted swap being |
| 552 | resumed? |
| 553 | + Yes: |
| 554 | + Review the validity of previously determined swap types |
| 555 | of other images. |
| 556 | + Complete the partial swap operation. |
| 557 | + Mark the swap type as `None`. |
| 558 | + Skip to next image. |
| 559 | + No: Proceed to step 2. |
| 560 | |
| 561 | 2. Inspect image trailers in the primary and secondary slot; is an image |
| 562 | swap requested? |
| 563 | + Yes: Review the validity of previously determined swap types of other |
| 564 | images. Is the requested image valid (integrity and security |
| 565 | check)? |
| 566 | + Yes: |
| 567 | + Set the previously determined swap type for the current image. |
| 568 | + Skip to next image. |
| 569 | + No: |
| 570 | + Erase invalid image. |
| 571 | + Persist failure of swap procedure to image trailers. |
| 572 | + Mark the swap type as `Fail`. |
| 573 | + Skip to next image. |
| 574 | + No: |
| 575 | + Mark the swap type as `None`. |
| 576 | + Skip to next image. |
| 577 | |
| 578 | + ###### Loop 2. Iterate over all images |
David Vincze | e32483f | 2019-06-13 10:46:24 +0200 | [diff] [blame] | 579 | 1. Does the current image depend on other image(s)? |
| 580 | + Yes: Are all the image dependencies satisfied? |
| 581 | + Yes: Skip to next image. |
| 582 | + No: |
| 583 | + Modify swap type depending on what the previous type was. |
| 584 | + Restart dependency check from the first image. |
| 585 | + No: Skip to next image. |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 586 | |
David Vincze | e32483f | 2019-06-13 10:46:24 +0200 | [diff] [blame] | 587 | + ###### Loop 3. Iterate over all images |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 588 | 1. Is an image swap requested? |
| 589 | + Yes: |
| 590 | + Perform image update operation. |
| 591 | + Persist completion of swap procedure to image trailers. |
| 592 | + Skip to next image. |
| 593 | + No: Skip to next image. |
| 594 | |
David Vincze | e32483f | 2019-06-13 10:46:24 +0200 | [diff] [blame] | 595 | + ###### Loop 4. Iterate over all images |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 596 | 1. Validate image in the primary slot (integrity and security check) or |
| 597 | at least do a basic sanity check to avoid booting into an empty flash |
| 598 | area. |
| 599 | |
| 600 | + Boot into image in the primary slot of the 0th image position\ |
| 601 | (other image in the boot chain is started by another image). |
| 602 | |
Fabio Utzig | d37d877 | 2019-12-03 10:32:18 -0300 | [diff] [blame] | 603 | ## [Image Swapping](#image-swapping) |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 604 | |
| 605 | The boot loader swaps the contents of the two image slots for two reasons: |
Fabio Utzig | 75b3441 | 2019-09-06 08:30:43 -0300 | [diff] [blame] | 606 | |
| 607 | * User has issued a "set pending" operation; the image in the secondary slot |
| 608 | should be run once (state II) or repeatedly (state III), depending on |
| 609 | whether a permanent swap was specified. |
| 610 | * Test image rebooted without being confirmed; the boot loader should |
| 611 | revert to the original image currently in the secondary slot (state IV). |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 612 | |
Fabio Utzig | 86fe4b2 | 2017-07-28 18:56:29 -0300 | [diff] [blame] | 613 | If the image trailers indicates that the image in the secondary slot should be |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 614 | run, the boot loader needs to copy it to the primary slot. The image currently |
| 615 | in the primary slot also needs to be retained in flash so that it can be used |
| 616 | later. Furthermore, both images need to be recoverable if the boot loader |
| 617 | resets in the middle of the swap operation. The two images are swapped |
| 618 | according to the following procedure: |
| 619 | |
Fabio Utzig | 60319ac | 2019-09-06 08:29:50 -0300 | [diff] [blame] | 620 | 1. Determine if both slots are compatible enough to have their images swapped. |
| 621 | To be compatible, both have to have only sectors that can fit into the |
| 622 | scratch area and if one of them has larger sectors than the other, it must |
| 623 | be able to entirely fit some rounded number of sectors from the other slot. |
Fabio Utzig | c28005b | 2019-09-10 12:18:29 -0300 | [diff] [blame] | 624 | In the next steps we'll use the terminology "region" for the total amount of |
| 625 | data copied/erased because this can be any amount of sectors depending on |
| 626 | how many the scratch is able to fit for some swap operation. |
| 627 | 2. Iterate the list of region indices in descending order (i.e., starting |
| 628 | with the greatest index); only regions that are predetermined to be part of |
Fabio Utzig | 60319ac | 2019-09-06 08:29:50 -0300 | [diff] [blame] | 629 | the image are copied; current element = "index". |
| 630 | + a. Erase scratch area. |
| 631 | + b. Copy secondary_slot[index] to scratch area. |
Fabio Utzig | c28005b | 2019-09-10 12:18:29 -0300 | [diff] [blame] | 632 | - If this is the last region in the slot, scratch area has a temporary |
Fabio Utzig | 60319ac | 2019-09-06 08:29:50 -0300 | [diff] [blame] | 633 | status area initialized to store the initial state, because the |
Fabio Utzig | c28005b | 2019-09-10 12:18:29 -0300 | [diff] [blame] | 634 | primary slot's last region will have to be erased. In this case, |
Fabio Utzig | 60319ac | 2019-09-06 08:29:50 -0300 | [diff] [blame] | 635 | only the data that was calculated to amount to the image is copied. |
Fabio Utzig | c28005b | 2019-09-10 12:18:29 -0300 | [diff] [blame] | 636 | - Else if this is the first swapped region but not the last region in |
Fabio Utzig | 60319ac | 2019-09-06 08:29:50 -0300 | [diff] [blame] | 637 | the slot, initialize the status area in primary slot and copy the |
Fabio Utzig | c28005b | 2019-09-10 12:18:29 -0300 | [diff] [blame] | 638 | full region contents. |
| 639 | - Else, copy entire region contents. |
Fabio Utzig | 60319ac | 2019-09-06 08:29:50 -0300 | [diff] [blame] | 640 | + c. Write updated swap status (i). |
| 641 | + d. Erase secondary_slot[index] |
| 642 | + e. Copy primary_slot[index] to secondary_slot[index] according to amount |
| 643 | previosly copied at step b. |
Fabio Utzig | c28005b | 2019-09-10 12:18:29 -0300 | [diff] [blame] | 644 | - If this is not the last region in the slot, erase the trailer in the |
Fabio Utzig | 60319ac | 2019-09-06 08:29:50 -0300 | [diff] [blame] | 645 | secondary slot, to always use the one in the primary slot. |
| 646 | + f. Write updated swap status (ii). |
| 647 | + g. Erase primary_slot[index]. |
| 648 | + h. Copy scratch area to primary_slot[index] according to amount |
| 649 | previously copied at step b. |
Fabio Utzig | c28005b | 2019-09-10 12:18:29 -0300 | [diff] [blame] | 650 | - If this is the last region in the slot, the status is read from |
Fabio Utzig | 60319ac | 2019-09-06 08:29:50 -0300 | [diff] [blame] | 651 | scratch (where it was stored temporarily) and written anew in the |
| 652 | primary slot. |
| 653 | + i. Write updated swap status (iii). |
| 654 | 3. Persist completion of swap procedure to the primary slot image trailer. |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 655 | |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 656 | The additional caveats in step 2f are necessary so that the secondary slot image |
Christopher Collins | fd7eb5c | 2016-12-21 13:46:08 -0800 | [diff] [blame] | 657 | trailer can be written by the user at a later time. With the image trailer |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 658 | unwritten, the user can test the image in the secondary slot |
| 659 | (i.e., transition to state II). |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 660 | |
Fabio Utzig | c28005b | 2019-09-10 12:18:29 -0300 | [diff] [blame] | 661 | Note1: If the region being copied contains the last sector, then swap status is |
Fabio Utzig | 5bd4e58 | 2017-07-20 08:55:38 -0300 | [diff] [blame] | 662 | temporarily maintained on scratch for the duration of this operation, always |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 663 | using the primary slot's area otherwise. |
Fabio Utzig | 5bd4e58 | 2017-07-20 08:55:38 -0300 | [diff] [blame] | 664 | |
| 665 | Note2: The bootloader tries to copy only used sectors (based on largest image |
| 666 | installed on any of the slots), minimizing the amount of sectors copied and |
| 667 | reducing the amount of time required for a swap operation. |
| 668 | |
Christopher Collins | fd7eb5c | 2016-12-21 13:46:08 -0800 | [diff] [blame] | 669 | The particulars of step 3 vary depending on whether an image is being tested, |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 670 | permanently used, reverted or a validation failure of the secondary slot |
| 671 | happened when a swap was requested: |
Fabio Utzig | 86fe4b2 | 2017-07-28 18:56:29 -0300 | [diff] [blame] | 672 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 673 | * test: |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 674 | o Write primary_slot.copy_done = 1 |
Christopher Collins | fd7eb5c | 2016-12-21 13:46:08 -0800 | [diff] [blame] | 675 | (swap caused the following values to be written: |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 676 | primary_slot.magic = BOOT_MAGIC |
| 677 | primary_slot.image_ok = Unset) |
Christopher Collins | fd7eb5c | 2016-12-21 13:46:08 -0800 | [diff] [blame] | 678 | |
| 679 | * permanent: |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 680 | o Write primary_slot.copy_done = 1 |
Christopher Collins | fd7eb5c | 2016-12-21 13:46:08 -0800 | [diff] [blame] | 681 | (swap caused the following values to be written: |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 682 | primary_slot.magic = BOOT_MAGIC |
| 683 | primary_slot.image_ok = 0x01) |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 684 | |
| 685 | * revert: |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 686 | o Write primary_slot.copy_done = 1 |
| 687 | o Write primary_slot.image_ok = 1 |
Fabio Utzig | 86fe4b2 | 2017-07-28 18:56:29 -0300 | [diff] [blame] | 688 | (swap caused the following values to be written: |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 689 | primary_slot.magic = BOOT_MAGIC) |
Fabio Utzig | 86fe4b2 | 2017-07-28 18:56:29 -0300 | [diff] [blame] | 690 | |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 691 | * failure to validate the secondary slot: |
| 692 | o Write primary_slot.image_ok = 1 |
Fabio Utzig | 86fe4b2 | 2017-07-28 18:56:29 -0300 | [diff] [blame] | 693 | |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 694 | After completing the operations as described above the image in the primary slot |
| 695 | should be booted. |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 696 | |
Fabio Utzig | d37d877 | 2019-12-03 10:32:18 -0300 | [diff] [blame] | 697 | ## [Swap Status](#swap-status) |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 698 | |
| 699 | The swap status region allows the boot loader to recover in case it restarts in |
| 700 | the middle of an image swap operation. The swap status region consists of a |
| 701 | series of single-byte records. These records are written independently, and |
| 702 | therefore must be padded according to the minimum write size imposed by the |
| 703 | flash hardware. In the below figure, a min-write-size of 1 is assumed for |
| 704 | simplicity. The structure of the swap status region is illustrated below. In |
| 705 | this figure, a min-write-size of 1 is assumed for simplicity. |
| 706 | |
David Brown | 17e20d1 | 2017-09-12 11:53:20 -0600 | [diff] [blame] | 707 | ``` |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 708 | 0 1 2 3 |
| 709 | 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 |
| 710 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 711 | |sec127,state 0 |sec127,state 1 |sec127,state 2 |sec126,state 0 | |
| 712 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 713 | |sec126,state 1 |sec126,state 2 |sec125,state 0 |sec125,state 1 | |
| 714 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 715 | |sec125,state 2 | | |
| 716 | +-+-+-+-+-+-+-+-+ + |
| 717 | ~ ~ |
| 718 | ~ [Records for indices 124 through 1 ~ |
| 719 | ~ ~ |
| 720 | ~ +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 721 | ~ |sec000,state 0 |sec000,state 1 |sec000,state 2 | |
| 722 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
David Brown | 17e20d1 | 2017-09-12 11:53:20 -0600 | [diff] [blame] | 723 | ``` |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 724 | |
| 725 | The above is probably not helpful at all; here is a description in English. |
| 726 | |
| 727 | Each image slot is partitioned into a sequence of flash sectors. If we were to |
| 728 | enumerate the sectors in a single slot, starting at 0, we would have a list of |
| 729 | sector indices. Since there are two image slots, each sector index would |
| 730 | correspond to a pair of sectors. For example, sector index 0 corresponds to |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 731 | the first sector in the primary slot and the first sector in the secondary slot. |
| 732 | Finally, reverse the list of indices such that the list starts with index |
| 733 | `BOOT_MAX_IMG_SECTORS - 1` and ends with 0. The swap status region is a |
| 734 | representation of this reversed list. |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 735 | |
| 736 | During a swap operation, each sector index transitions through four separate |
| 737 | states: |
David Brown | 17e20d1 | 2017-09-12 11:53:20 -0600 | [diff] [blame] | 738 | ``` |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 739 | 0. primary slot: image 0, secondary slot: image 1, scratch: N/A |
| 740 | 1. primary slot: image 0, secondary slot: N/A, scratch: image 1 (1->s, erase 1) |
| 741 | 2. primary slot: N/A, secondary slot: image 0, scratch: image 1 (0->1, erase 0) |
| 742 | 3. primary slot: image 1, secondary slot: image 0, scratch: N/A (s->0) |
David Brown | 17e20d1 | 2017-09-12 11:53:20 -0600 | [diff] [blame] | 743 | ``` |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 744 | |
| 745 | Each time a sector index transitions to a new state, the boot loader writes a |
| 746 | record to the swap status region. Logically, the boot loader only needs one |
| 747 | record per sector index to keep track of the current swap state. However, due |
| 748 | to limitations imposed by flash hardware, a record cannot be overwritten when |
| 749 | an index's state changes. To solve this problem, the boot loader uses three |
| 750 | records per sector index rather than just one. |
| 751 | |
| 752 | Each sector-state pair is represented as a set of three records. The record |
| 753 | values map to the above four states as follows |
| 754 | |
David Brown | 17e20d1 | 2017-09-12 11:53:20 -0600 | [diff] [blame] | 755 | ``` |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 756 | | rec0 | rec1 | rec2 |
| 757 | --------+------+------+------ |
| 758 | state 0 | 0xff | 0xff | 0xff |
| 759 | state 1 | 0x01 | 0xff | 0xff |
| 760 | state 2 | 0x01 | 0x02 | 0xff |
| 761 | state 3 | 0x01 | 0x02 | 0x03 |
David Brown | 17e20d1 | 2017-09-12 11:53:20 -0600 | [diff] [blame] | 762 | ``` |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 763 | |
Fabio Utzig | 2c05f1b | 2018-04-04 10:35:17 -0300 | [diff] [blame] | 764 | The swap status region can accommodate `BOOT_MAX_IMG_SECTORS` sector indices. |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 765 | Hence, the size of the region, in bytes, is |
| 766 | `BOOT_MAX_IMG_SECTORS * min-write-size * 3`. The only requirement for the index |
| 767 | count is that it is great enough to account for a maximum-sized image |
| 768 | (i.e., at least as great as the total sector count in an image slot). If a |
| 769 | device's image slots have been configured with `BOOT_MAX_IMG_SECTORS: 128` and |
| 770 | use less than 128 sectors, the first record that gets written will be somewhere |
| 771 | in the middle of the region. For example, if a slot uses 64 sectors, the first |
| 772 | sector index that gets swapped is 63, which corresponds to the exact halfway |
| 773 | point within the region. |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 774 | |
Fabio Utzig | 5bd4e58 | 2017-07-20 08:55:38 -0300 | [diff] [blame] | 775 | Note: since the scratch area only ever needs to record swapping of the last |
| 776 | sector, it uses at most min-write-size * 3 bytes for its own status area. |
| 777 | |
Fabio Utzig | d37d877 | 2019-12-03 10:32:18 -0300 | [diff] [blame] | 778 | ## [Reset Recovery](#reset-recovery) |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 779 | |
| 780 | If the boot loader resets in the middle of a swap operation, the two images may |
| 781 | be discontiguous in flash. Bootutil recovers from this condition by using the |
Fabio Utzig | 86fe4b2 | 2017-07-28 18:56:29 -0300 | [diff] [blame] | 782 | image trailers to determine how the image parts are distributed in flash. |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 783 | |
| 784 | The first step is determine where the relevant swap status region is located. |
| 785 | Because this region is embedded within the image slots, its location in flash |
Fabio Utzig | 86fe4b2 | 2017-07-28 18:56:29 -0300 | [diff] [blame] | 786 | changes during a swap operation. The below set of tables map image trailers |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 787 | contents to swap status location. In these tables, the "source" field |
David Vincze | ba3bd60 | 2019-06-17 16:01:43 +0200 | [diff] [blame] | 788 | indicates where the swap status region is located. In case of multi image boot |
| 789 | the images primary area and the single scratch area is always examined in pairs. |
| 790 | If swap status found on scratch area then it might not belong to the current |
| 791 | image. The swap_info field of swap status stores the corresponding image number. |
| 792 | If it does not match then "source: none" is returned. |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 793 | |
David Brown | 17e20d1 | 2017-09-12 11:53:20 -0600 | [diff] [blame] | 794 | ``` |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 795 | | primary slot | scratch | |
| 796 | ----------+--------------+--------------| |
| 797 | magic | Good | Any | |
| 798 | copy-done | 0x01 | N/A | |
| 799 | ----------+--------------+--------------' |
| 800 | source: none | |
| 801 | ----------------------------------------' |
Marti Bolivar | 49b2917 | 2017-08-04 14:50:51 -0400 | [diff] [blame] | 802 | |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 803 | | primary slot | scratch | |
| 804 | ----------+--------------+--------------| |
| 805 | magic | Good | Any | |
| 806 | copy-done | 0xff | N/A | |
| 807 | ----------+--------------+--------------' |
| 808 | source: primary slot | |
| 809 | ----------------------------------------' |
Marti Bolivar | 49b2917 | 2017-08-04 14:50:51 -0400 | [diff] [blame] | 810 | |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 811 | | primary slot | scratch | |
| 812 | ----------+--------------+--------------| |
| 813 | magic | Any | Good | |
| 814 | copy-done | Any | N/A | |
| 815 | ----------+--------------+--------------' |
| 816 | source: scratch | |
| 817 | ----------------------------------------' |
Marti Bolivar | 49b2917 | 2017-08-04 14:50:51 -0400 | [diff] [blame] | 818 | |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 819 | | primary slot | scratch | |
| 820 | ----------+--------------+--------------| |
| 821 | magic | Unset | Any | |
| 822 | copy-done | 0xff | N/A | |
| 823 | ----------+--------------+--------------| |
| 824 | source: primary slot | |
| 825 | ----------------------------------------+------------------------------+ |
| 826 | This represents one of two cases: | |
| 827 | o No swaps ever (no status to read, so no harm in checking). | |
| 828 | o Mid-revert; status in the primary slot. | |
| 829 | For this reason we assume the primary slot as source, to trigger a | |
| 830 | check of the status area and find out if there was swapping under way. | |
| 831 | -----------------------------------------------------------------------' |
David Brown | 17e20d1 | 2017-09-12 11:53:20 -0600 | [diff] [blame] | 832 | ``` |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 833 | |
Christopher Collins | a1c1204 | 2019-05-23 14:00:28 -0700 | [diff] [blame] | 834 | If the swap status region indicates that the images are not contiguous, mcuboot |
| 835 | determines the type of swap operation that was interrupted by reading the `swap |
Håkon Øye Amundsen | cbf3047 | 2019-07-24 08:34:03 +0000 | [diff] [blame] | 836 | info` field in the active image trailer and extracting the swap type from bits |
David Vincze | e245347 | 2019-06-17 12:31:59 +0200 | [diff] [blame] | 837 | 0-3 then resumes the operation. In other words, it applies the procedure defined |
| 838 | in the previous section, moving image 1 into the primary slot and image 0 into |
| 839 | the secondary slot. If the boot status indicates that an image part is present |
| 840 | in the scratch area, this part is copied into the correct location by starting |
| 841 | at step e or step h in the area-swap procedure, depending on whether the part |
| 842 | belongs to image 0 or image 1. |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 843 | |
| 844 | After the swap operation has been completed, the boot loader proceeds as though |
| 845 | it had just been started. |
| 846 | |
Fabio Utzig | d37d877 | 2019-12-03 10:32:18 -0300 | [diff] [blame] | 847 | ## [Integrity Check](#integrity-check) |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 848 | |
| 849 | An image is checked for integrity immediately before it gets copied into the |
Fabio Utzig | 5bd4e58 | 2017-07-20 08:55:38 -0300 | [diff] [blame] | 850 | primary slot. If the boot loader doesn't perform an image swap, then it can |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 851 | perform an optional integrity check of the image in the primary slot if |
| 852 | `MCUBOOT_VALIDATE_PRIMARY_SLOT` is set, otherwise it doesn't perform an |
| 853 | integrity check. |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 854 | |
| 855 | During the integrity check, the boot loader verifies the following aspects of |
| 856 | an image: |
Fabio Utzig | 75b3441 | 2019-09-06 08:30:43 -0300 | [diff] [blame] | 857 | |
Fabio Utzig | fd140ec | 2019-09-12 14:37:48 -0300 | [diff] [blame] | 858 | * 32-bit magic number must be correct (`IMAGE_MAGIC`). |
Fabio Utzig | 75b3441 | 2019-09-06 08:30:43 -0300 | [diff] [blame] | 859 | * Image must contain an `image_tlv_info` struct, identified by its magic |
Fabio Utzig | fd140ec | 2019-09-12 14:37:48 -0300 | [diff] [blame] | 860 | (`IMAGE_TLV_PROT_INFO_MAGIC` or `IMAGE_TLV_INFO_MAGIC`) exactly following |
| 861 | the firmware (`hdr_size` + `img_size`). If `IMAGE_TLV_PROT_INFO_MAGIC` is |
| 862 | found then after `ih_protect_tlv_size` bytes, another `image_tlv_info` |
| 863 | with magic equal to `IMAGE_TLV_INFO_MAGIC` must be present. |
Fabio Utzig | 75b3441 | 2019-09-06 08:30:43 -0300 | [diff] [blame] | 864 | * Image must contain a SHA256 TLV. |
| 865 | * Calculated SHA256 must match SHA256 TLV contents. |
| 866 | * Image *may* contain a signature TLV. If it does, it must also have a |
| 867 | KEYHASH TLV with the hash of the key that was used to sign. The list of |
| 868 | keys will then be iterated over looking for the matching key, which then |
| 869 | will then be used to verify the image contents. |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 870 | |
Fabio Utzig | d37d877 | 2019-12-03 10:32:18 -0300 | [diff] [blame] | 871 | ## [Security](#security) |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 872 | |
| 873 | As indicated above, the final step of the integrity check is signature |
| 874 | verification. The boot loader can have one or more public keys embedded in it |
| 875 | at build time. During signature verification, the boot loader verifies that an |
Håkon Øye Amundsen | cbf3047 | 2019-07-24 08:34:03 +0000 | [diff] [blame] | 876 | image was signed with a private key that corresponds to the embedded KEYHASH |
Fabio Utzig | ea422c2 | 2017-09-11 11:02:47 -0300 | [diff] [blame] | 877 | TLV. |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 878 | |
| 879 | For information on embedding public keys in the boot loader, as well as |
Fabio Utzig | 4dce6aa | 2018-02-12 15:31:32 -0200 | [diff] [blame] | 880 | producing signed images, see: [signed_images](signed_images.md). |
Fabio Utzig | cdfa11a | 2018-10-01 09:45:54 -0300 | [diff] [blame] | 881 | |
| 882 | If you want to enable and use encrypted images, see: |
| 883 | [encrypted_images](encrypted_images.md). |
David Vincze | e32483f | 2019-06-13 10:46:24 +0200 | [diff] [blame] | 884 | |
Fabio Utzig | d37d877 | 2019-12-03 10:32:18 -0300 | [diff] [blame] | 885 | ## [Protected TLVs](#protected-tlvs) |
Fabio Utzig | fd140ec | 2019-09-12 14:37:48 -0300 | [diff] [blame] | 886 | |
| 887 | If the TLV area contains protected TLV entries, by beginning with a `struct |
| 888 | image_tlv_info` with a magic value of `IMAGE_TLV_PROT_INFO_MAGIC` then the |
| 889 | data of those TLVs must also be integrity and authenticity protected. Beyond |
| 890 | the full size of the protected TLVs being stored in the `image_tlv_info`, |
| 891 | the size of the protected TLVs together with the size of the `image_tlv_info` |
| 892 | struct itself are also saved in the `ih_protected_size` field inside the |
| 893 | header. |
| 894 | |
| 895 | Whenever an image has protected TLVs the SHA256 has to be calculated over |
| 896 | not just the image header and the image but also the TLV info header and the |
| 897 | protected TLVs. |
| 898 | |
| 899 | ``` |
| 900 | A +---------------------+ |
| 901 | | Header | <- struct image_header |
| 902 | +---------------------+ |
| 903 | | Payload | |
| 904 | +---------------------+ |
| 905 | | TLV area | |
| 906 | | +-----------------+ | struct image_tlv_info with |
| 907 | | | TLV area header | | <- IMAGE_TLV_PROT_INFO_MAGIC (optional) |
| 908 | | +-----------------+ | |
| 909 | | | Protected TLVs | | <- Protected TLVs (struct image_tlv) |
| 910 | B | +-----------------+ | |
| 911 | | | TLV area header | | <- struct image_tlv_info with IMAGE_TLV_INFO_MAGIC |
| 912 | C | +-----------------+ | |
| 913 | | | SHA256 hash | | <- hash from A - B (struct image_tlv) |
| 914 | D | +-----------------+ | |
| 915 | | | Keyhash | | <- indicates which pub. key for sig (struct image_tlv) |
| 916 | | +-----------------+ | |
| 917 | | | Signature | | <- signature from C - D (struct image_tlv), only hash |
| 918 | | +-----------------+ | |
| 919 | +---------------------+ |
| 920 | ``` |
| 921 | |
Fabio Utzig | d37d877 | 2019-12-03 10:32:18 -0300 | [diff] [blame] | 922 | ## [Dependency Check](#dependency-check) |
David Vincze | e32483f | 2019-06-13 10:46:24 +0200 | [diff] [blame] | 923 | |
| 924 | MCUBoot can handle multiple firmware images. It is possible to update them |
| 925 | independently but in many cases it can be desired to be able to describe |
| 926 | dependencies between the images (e.g. to ensure API compliance and avoid |
| 927 | interoperability issues). |
| 928 | |
| 929 | The dependencies between images can be described with additional TLV entries in |
Fabio Utzig | fd140ec | 2019-09-12 14:37:48 -0300 | [diff] [blame] | 930 | the protected TLV area after the end of an image. There can be more than one |
| 931 | dependency entry, but in practice if the platform only supports two individual |
| 932 | images then there can be maximum one entry which reflects to the other image. |
David Vincze | e32483f | 2019-06-13 10:46:24 +0200 | [diff] [blame] | 933 | |
David Vincze | e32483f | 2019-06-13 10:46:24 +0200 | [diff] [blame] | 934 | At the phase of dependency check all aborted swaps are finalized if there were |
| 935 | any. During the dependency check the boot loader verifies whether the image |
| 936 | dependencies are all satisfied. If at least one of the dependencies of an image |
| 937 | is not fulfilled then the swap type of that image has to be modified |
| 938 | accordingly and the dependency check needs to be restarted. This way the number |
| 939 | of unsatisfied dependencies will decrease or remain the same. There is always at |
| 940 | least 1 valid configuration. In worst case, the system returns to the initial |
| 941 | state after dependency check. |
| 942 | |
| 943 | For more information on adding dependency entries to an image, |
| 944 | see: [imgtool](imgtool.md). |