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