Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [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 |
Marti Bolivar | 49b2917 | 2017-08-04 14:50:51 -0400 | [diff] [blame] | 9 | # |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 10 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | # |
| 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 | |
| 20 | ****** BOOT LOADER |
| 21 | |
| 22 | *** SUMMARY |
| 23 | |
Fabio Utzig | ac83496 | 2017-07-20 13:20:48 -0300 | [diff] [blame] | 24 | mcuboot comprises two packages: |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 25 | |
| 26 | * The bootutil library (boot/bootutil) |
Fabio Utzig | 5bd4e58 | 2017-07-20 08:55:38 -0300 | [diff] [blame] | 27 | * The boot application (each port has its own at boot/<port>) |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 28 | |
| 29 | The bootutil library performs most of the functions of a boot loader. In |
| 30 | particular, the piece that is missing is the final step of actually jumping to |
| 31 | the main image. This last step is instead implemented by the boot application. |
| 32 | Boot loader functionality is separated in this manner to enable unit testing of |
| 33 | the boot loader. A library can be unit tested, but an application can't. |
| 34 | Therefore, functionality is delegated to the bootutil library when possible. |
| 35 | |
| 36 | *** LIMITATIONS |
| 37 | |
| 38 | The boot loader currently only supports images with the following |
| 39 | characteristics: |
| 40 | * Built to run from flash. |
Marti Bolivar | 49b2917 | 2017-08-04 14:50:51 -0400 | [diff] [blame] | 41 | * Built to run from a fixed location (i.e., not position-independent). |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 42 | |
| 43 | *** IMAGE FORMAT |
| 44 | |
| 45 | The following definitions describe the image format. |
| 46 | |
Fabio Utzig | ea422c2 | 2017-09-11 11:02:47 -0300 | [diff] [blame^] | 47 | #define IMAGE_MAGIC 0x96f3b83d |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 48 | |
| 49 | #define IMAGE_HEADER_SIZE 32 |
| 50 | |
| 51 | struct image_version { |
| 52 | uint8_t iv_major; |
| 53 | uint8_t iv_minor; |
| 54 | uint16_t iv_revision; |
| 55 | uint32_t iv_build_num; |
| 56 | }; |
| 57 | |
| 58 | /** Image header. All fields are in little endian byte order. */ |
| 59 | struct image_header { |
| 60 | uint32_t ih_magic; |
Fabio Utzig | ea422c2 | 2017-09-11 11:02:47 -0300 | [diff] [blame^] | 61 | uint32_t ih_load_addr; |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 62 | uint16_t ih_hdr_size; /* Size of image header (bytes). */ |
| 63 | uint16_t _pad2; |
| 64 | uint32_t ih_img_size; /* Does not include header. */ |
Fabio Utzig | ea422c2 | 2017-09-11 11:02:47 -0300 | [diff] [blame^] | 65 | uint32_t ih_flags; /* IMAGE_F_[...]. */ |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 66 | struct image_version ih_ver; |
| 67 | uint32_t _pad3; |
| 68 | }; |
| 69 | |
Fabio Utzig | ea422c2 | 2017-09-11 11:02:47 -0300 | [diff] [blame^] | 70 | /** Image TLV header. All fields in little endian. */ |
| 71 | struct image_tlv_info { |
| 72 | uint16_t it_magic; |
| 73 | uint16_t it_tlv_tot; /* size of TLV area (including tlv_info header) */ |
| 74 | }; |
| 75 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 76 | /** Image trailer TLV format. All fields in little endian. */ |
| 77 | struct image_tlv { |
| 78 | uint8_t it_type; /* IMAGE_TLV_[...]. */ |
| 79 | uint8_t _pad; |
Marti Bolivar | 49b2917 | 2017-08-04 14:50:51 -0400 | [diff] [blame] | 80 | uint16_t it_len; /* Data length (not including TLV header). */ |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 81 | }; |
| 82 | |
| 83 | /* |
| 84 | * Image header flags. |
| 85 | */ |
Marti Bolivar | 7c057e9 | 2017-08-04 14:46:39 -0400 | [diff] [blame] | 86 | #define IMAGE_F_PIC 0x00000001 /* Not supported. */ |
Marti Bolivar | 7c057e9 | 2017-08-04 14:46:39 -0400 | [diff] [blame] | 87 | #define IMAGE_F_NON_BOOTABLE 0x00000010 /* Split image app. */ |
Fabio Utzig | ea422c2 | 2017-09-11 11:02:47 -0300 | [diff] [blame^] | 88 | #define IMAGE_F_RAM_LOAD 0x00000020 |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 89 | |
| 90 | /* |
| 91 | * Image trailer TLV types. |
| 92 | */ |
Fabio Utzig | ea422c2 | 2017-09-11 11:02:47 -0300 | [diff] [blame^] | 93 | #define IMAGE_TLV_KEYHASH 0x01 /* hash of the public key */ |
David Brown | 27648b8 | 2017-08-31 10:40:29 -0600 | [diff] [blame] | 94 | #define IMAGE_TLV_SHA256 0x10 /* SHA256 of image hdr and body */ |
Marko Kiiskila | 8dd56f3 | 2017-08-22 21:40:49 -0700 | [diff] [blame] | 95 | #define IMAGE_TLV_RSA2048_PSS 0x20 /* RSA2048 of hash output */ |
David Brown | 27648b8 | 2017-08-31 10:40:29 -0600 | [diff] [blame] | 96 | #define IMAGE_TLV_ECDSA224 0x21 /* ECDSA of hash output */ |
| 97 | #define IMAGE_TLV_ECDSA256 0x22 /* ECDSA of hash output */ |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 98 | |
| 99 | Optional type-length-value records (TLVs) containing image metadata are placed |
| 100 | after the end of the image. |
| 101 | |
| 102 | The ih_hdr_size field indicates the length of the header, and therefore the |
| 103 | offset of the image itself. This field provides for backwards compatibility in |
| 104 | case of changes to the format of the image header. |
| 105 | |
| 106 | *** FLASH MAP |
| 107 | |
Fabio Utzig | ac83496 | 2017-07-20 13:20:48 -0300 | [diff] [blame] | 108 | 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] | 109 | level, the flash map maps numeric IDs to _flash areas_. A flash area is a |
| 110 | region of disk with the following properties: |
| 111 | (1) An area can be fully erased without affecting any other areas. |
| 112 | (2) A write to one area does not restrict writes to other areas. |
| 113 | |
Marti Bolivar | 4e64d56 | 2017-08-04 14:53:33 -0400 | [diff] [blame] | 114 | The boot loader uses the following flash area IDs: |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 115 | |
| 116 | #define FLASH_AREA_BOOTLOADER 0 |
| 117 | #define FLASH_AREA_IMAGE_0 1 |
| 118 | #define FLASH_AREA_IMAGE_1 2 |
| 119 | #define FLASH_AREA_IMAGE_SCRATCH 3 |
| 120 | |
Marti Bolivar | 4e64d56 | 2017-08-04 14:53:33 -0400 | [diff] [blame] | 121 | The bootloader area contains the bootloader image itself. The other areas are |
| 122 | described in subsequent sections. |
| 123 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 124 | *** IMAGE SLOTS |
| 125 | |
| 126 | A portion of the flash memory is partitioned into two image slots: a primary |
| 127 | slot (0) and a secondary slot (1). The boot loader will only run an image from |
| 128 | the primary slot, so images must be built such that they can run from that |
| 129 | fixed location in flash. If the boot loader needs to run the image resident in |
Marti Bolivar | a91674f | 2017-08-04 14:56:08 -0400 | [diff] [blame] | 130 | the secondary slot, it must copy its contents into the primary slot before doing |
| 131 | so, either by swapping the two images or by overwriting the contents of the |
| 132 | primary slot. The bootloader supports either swap- or overwrite-based image |
| 133 | upgrades, but must be configured at build time to choose one of these two |
| 134 | strategies. |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 135 | |
| 136 | In addition to the two image slots, the boot loader requires a scratch area to |
| 137 | allow for reliable image swapping. |
| 138 | |
Marti Bolivar | a91674f | 2017-08-04 14:56:08 -0400 | [diff] [blame] | 139 | The overwrite upgrade strategy is substantially simpler to implement than the |
| 140 | image swapping strategy, especially since the bootloader must work properly |
| 141 | even when it is reset during the middle of an image swap. For this reason, the |
| 142 | rest of the document describes its behavior when configured to swap images |
| 143 | during an upgrade. |
| 144 | |
Marti Bolivar | 048d8d8 | 2017-08-04 17:14:24 -0400 | [diff] [blame] | 145 | *** BOOT SWAP TYPES |
Christopher Collins | fd7eb5c | 2016-12-21 13:46:08 -0800 | [diff] [blame] | 146 | |
Marti Bolivar | 048d8d8 | 2017-08-04 17:14:24 -0400 | [diff] [blame] | 147 | When the device first boots under normal circumstances, there is an up-to-date |
| 148 | firmware image in slot 0, which mcuboot can validate and then chain-load. In |
| 149 | this case, no image swaps are necessary. During device upgrades, however, new |
| 150 | candidate images are present in slot 1, which mcuboot must swap into slot 0 |
| 151 | before booting as discussed above. |
Christopher Collins | fd7eb5c | 2016-12-21 13:46:08 -0800 | [diff] [blame] | 152 | |
Marti Bolivar | 048d8d8 | 2017-08-04 17:14:24 -0400 | [diff] [blame] | 153 | Upgrading an old image with a new one by swapping can be a two-step process. In |
| 154 | this process, mcuboot performs a "test" swap of image data in flash and boots |
| 155 | the new image. The new image can then update the contents of flash at runtime |
| 156 | to mark itself "OK", and mcuboot will then still choose to run it during the |
| 157 | next boot. When this happens, the swap is made "permanent". If this doesn't |
| 158 | happen, mcuboot will perform a "revert" swap during the next boot by swapping |
| 159 | the images back into their original locations, and attempting to boot the old |
| 160 | image. |
Christopher Collins | fd7eb5c | 2016-12-21 13:46:08 -0800 | [diff] [blame] | 161 | |
Marti Bolivar | 048d8d8 | 2017-08-04 17:14:24 -0400 | [diff] [blame] | 162 | Depending on the use case, the first swap can also be made permanent directly. |
| 163 | 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] | 164 | |
Marti Bolivar | 048d8d8 | 2017-08-04 17:14:24 -0400 | [diff] [blame] | 165 | Test swaps are supported to provide a rollback mechanism to prevent devices |
| 166 | from becoming "bricked" by bad firmware. If the device crashes immediately |
| 167 | upon booting a new (bad) image, mcuboot will revert to the old (working) image |
| 168 | at the next device reset, rather than booting the bad image again. This allows |
| 169 | device firmware to make test swaps permanent only after performing a self-test |
| 170 | routine. |
Christopher Collins | fd7eb5c | 2016-12-21 13:46:08 -0800 | [diff] [blame] | 171 | |
Marti Bolivar | 048d8d8 | 2017-08-04 17:14:24 -0400 | [diff] [blame] | 172 | On startup, mcuboot inspects the contents of flash to decide which of these |
| 173 | "swap types" to perform; this decision determines how it proceeds. |
Christopher Collins | fd7eb5c | 2016-12-21 13:46:08 -0800 | [diff] [blame] | 174 | |
Marti Bolivar | 048d8d8 | 2017-08-04 17:14:24 -0400 | [diff] [blame] | 175 | The possible swap types, and their meanings, are: |
Christopher Collins | fd7eb5c | 2016-12-21 13:46:08 -0800 | [diff] [blame] | 176 | |
Marti Bolivar | 048d8d8 | 2017-08-04 17:14:24 -0400 | [diff] [blame] | 177 | - BOOT_SWAP_TYPE_NONE: The "usual" or "no upgrade" case; attempt to boot the |
| 178 | contents of slot 0. |
Christopher Collins | fd7eb5c | 2016-12-21 13:46:08 -0800 | [diff] [blame] | 179 | |
Marti Bolivar | 048d8d8 | 2017-08-04 17:14:24 -0400 | [diff] [blame] | 180 | - BOOT_SWAP_TYPE_TEST: Boot the contents of slot 1 by swapping images. Unless |
| 181 | the swap is made permanent, revert back on the next boot. |
Christopher Collins | fd7eb5c | 2016-12-21 13:46:08 -0800 | [diff] [blame] | 182 | |
Marti Bolivar | 048d8d8 | 2017-08-04 17:14:24 -0400 | [diff] [blame] | 183 | - BOOT_SWAP_TYPE_PERM: Permanently swap images, and boot the upgraded image |
| 184 | firmware. |
Christopher Collins | fd7eb5c | 2016-12-21 13:46:08 -0800 | [diff] [blame] | 185 | |
Marti Bolivar | 048d8d8 | 2017-08-04 17:14:24 -0400 | [diff] [blame] | 186 | - BOOT_SWAP_TYPE_REVERT: A previous test swap was not made permanent; swap back |
| 187 | to the old image whose data are now in slot 1. If the old image marks itself |
| 188 | "OK" when it boots, the next boot will have swap type BOOT_SWAP_TYPE_NONE. |
Christopher Collins | fd7eb5c | 2016-12-21 13:46:08 -0800 | [diff] [blame] | 189 | |
Marti Bolivar | 048d8d8 | 2017-08-04 17:14:24 -0400 | [diff] [blame] | 190 | - BOOT_SWAP_TYPE_FAIL: Swap failed because image to be run is not valid. |
| 191 | |
| 192 | - BOOT_SWAP_TYPE_PANIC: Swapping encountered an unrecoverable error. |
| 193 | |
| 194 | The "swap type" is a high-level representation of the outcome of the |
| 195 | boot. Subsequent sections describe how mcuboot determines the swap type from |
| 196 | the bit-level contents of flash. |
Christopher Collins | fd7eb5c | 2016-12-21 13:46:08 -0800 | [diff] [blame] | 197 | |
Fabio Utzig | 86fe4b2 | 2017-07-28 18:56:29 -0300 | [diff] [blame] | 198 | *** IMAGE TRAILER |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 199 | |
Fabio Utzig | 86fe4b2 | 2017-07-28 18:56:29 -0300 | [diff] [blame] | 200 | 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] | 201 | should be taken during the current boot operation, it uses metadata stored in |
| 202 | the image flash areas. While swapping, some of this metadata is temporarily |
| 203 | copied into and out of the scratch area. |
| 204 | |
| 205 | This metadata is located at the end of the image flash areas, and is called an |
| 206 | image trailer. An image trailer has the following structure: |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 207 | |
| 208 | 0 1 2 3 |
| 209 | 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 |
| 210 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 211 | ~ ~ |
| 212 | ~ Swap status (128 * min-write-size * 3) ~ |
| 213 | ~ ~ |
| 214 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
Fabio Utzig | ea422c2 | 2017-09-11 11:02:47 -0300 | [diff] [blame^] | 215 | | Swap size | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 216 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
Fabio Utzig | ea422c2 | 2017-09-11 11:02:47 -0300 | [diff] [blame^] | 217 | | 0xff padding (4 octets) | |
Fabio Utzig | 5bd4e58 | 2017-07-20 08:55:38 -0300 | [diff] [blame] | 218 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
Fabio Utzig | ea422c2 | 2017-09-11 11:02:47 -0300 | [diff] [blame^] | 219 | | Copy done | 0xff padding (7 octets) ~ |
| 220 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 221 | | Image OK | 0xff padding (7 octets) ~ |
| 222 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 223 | ~ MAGIC (16 octets) ~ |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 224 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 225 | |
Marti Bolivar | 4281803 | 2017-08-04 15:45:01 -0400 | [diff] [blame] | 226 | The offset immediately following such a record represents the start of the next |
| 227 | flash area. |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 228 | |
| 229 | Note: "min-write-size" is a property of the flash hardware. If the hardware |
| 230 | allows individual bytes to be written at arbitrary addresses, then |
| 231 | min-write-size is 1. If the hardware only allows writes at even addresses, |
| 232 | then min-write-size is 2, and so on. |
| 233 | |
Marti Bolivar | 1dcb685 | 2017-08-04 15:59:32 -0400 | [diff] [blame] | 234 | An image trailer contains the following fields: |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 235 | |
Marti Bolivar | 1dcb685 | 2017-08-04 15:59:32 -0400 | [diff] [blame] | 236 | 1. Swap status: A series of records which records the progress of an image |
| 237 | swap. To swap entire images, data are swapped between the two image areas one |
| 238 | or more sectors at a time, like this: |
| 239 | |
| 240 | - sector data in slot 0 is copied into scratch, then erased |
| 241 | - sector data in slot 1 is copied into slot 0, then erased |
| 242 | - sector data in scratch is copied into slot 1 |
| 243 | |
| 244 | As it swaps images, the bootloader updates the swap status field in a way that |
| 245 | allows it to compute how far this swap operation has progressed for each |
| 246 | sector. The swap status field can thus used to resume a swap operation if the |
| 247 | bootloader is halted while a swap operation is ongoing and later reset. The |
| 248 | factor of 128 is the maximum number of sectors mcuboot supports for each image; |
| 249 | its value is a bootloader design decision. The factor of min-write-sz is due to |
| 250 | the behavior of flash hardware. The factor of 3 is explained below. |
Fabio Utzig | 5bd4e58 | 2017-07-20 08:55:38 -0300 | [diff] [blame] | 251 | |
Fabio Utzig | ea422c2 | 2017-09-11 11:02:47 -0300 | [diff] [blame^] | 252 | 2. Swap size: When beginning a new swap operation, the total size that needs |
| 253 | to be swapped (based on the slot with largest image + tlvs) is written to this |
| 254 | location for easier recovery in case of a reset while performing the swap. |
| 255 | |
| 256 | 3. Copy done: A single byte indicating whether the image in this slot is |
Fabio Utzig | 5bd4e58 | 2017-07-20 08:55:38 -0300 | [diff] [blame] | 257 | complete (0x01=done; 0xff=not done). |
| 258 | |
Fabio Utzig | ea422c2 | 2017-09-11 11:02:47 -0300 | [diff] [blame^] | 259 | 4. Image OK: A single byte indicating whether the image in this slot has been |
Fabio Utzig | 5bd4e58 | 2017-07-20 08:55:38 -0300 | [diff] [blame] | 260 | confirmed as good by the user (0x01=confirmed; 0xff=not confirmed). |
| 261 | |
Fabio Utzig | ea422c2 | 2017-09-11 11:02:47 -0300 | [diff] [blame^] | 262 | 5. MAGIC: The following 16 bytes, written in host-byte-order: |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 263 | |
| 264 | const uint32_t boot_img_magic[4] = { |
| 265 | 0xf395c277, |
| 266 | 0x7fefd260, |
| 267 | 0x0f505235, |
| 268 | 0x8079b62c, |
| 269 | }; |
| 270 | |
Fabio Utzig | 86fe4b2 | 2017-07-28 18:56:29 -0300 | [diff] [blame] | 271 | *** IMAGE TRAILERS |
| 272 | |
Marti Bolivar | 048d8d8 | 2017-08-04 17:14:24 -0400 | [diff] [blame] | 273 | At startup, the boot loader determines the boot swap type by inspecting the |
| 274 | image trailers. When using the term "image trailers" what is meant is the |
| 275 | aggregate information provided by both image slot's trailers. |
Fabio Utzig | 86fe4b2 | 2017-07-28 18:56:29 -0300 | [diff] [blame] | 276 | |
| 277 | The image trailers records are structured around the limitations imposed by flash |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 278 | hardware. As a consequence, they do not have a very intuitive design, and it |
| 279 | 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] | 280 | image trailers. It is better to map all the possible trailer states to the swap |
| 281 | 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] | 282 | |
| 283 | Note: An important caveat about the tables described below is that they must |
| 284 | be evaluated in the order presented here. Lower state numbers must have a |
| 285 | higher priority when testing the image trailers. |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 286 | |
| 287 | |
| 288 | State I |
| 289 | | slot-0 | slot-1 | |
| 290 | -----------------+--------+--------| |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 291 | magic | Any | Good | |
Christopher Collins | fd7eb5c | 2016-12-21 13:46:08 -0800 | [diff] [blame] | 292 | image-ok | Any | Unset | |
Fabio Utzig | f9d4428 | 2017-07-20 15:05:13 -0300 | [diff] [blame] | 293 | copy-done | Any | Any | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 294 | -----------------+--------+--------' |
Marti Bolivar | 048d8d8 | 2017-08-04 17:14:24 -0400 | [diff] [blame] | 295 | result: BOOT_SWAP_TYPE_TEST | |
Christopher Collins | fd7eb5c | 2016-12-21 13:46:08 -0800 | [diff] [blame] | 296 | -----------------------------------' |
Fabio Utzig | 5bd4e58 | 2017-07-20 08:55:38 -0300 | [diff] [blame] | 297 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 298 | |
Fabio Utzig | 86fe4b2 | 2017-07-28 18:56:29 -0300 | [diff] [blame] | 299 | State II |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 300 | | slot-0 | slot-1 | |
| 301 | -----------------+--------+--------| |
Christopher Collins | fd7eb5c | 2016-12-21 13:46:08 -0800 | [diff] [blame] | 302 | magic | Any | Good | |
| 303 | image-ok | Any | 0x01 | |
Fabio Utzig | f9d4428 | 2017-07-20 15:05:13 -0300 | [diff] [blame] | 304 | copy-done | Any | Any | |
Christopher Collins | fd7eb5c | 2016-12-21 13:46:08 -0800 | [diff] [blame] | 305 | -----------------+--------+--------' |
Marti Bolivar | 048d8d8 | 2017-08-04 17:14:24 -0400 | [diff] [blame] | 306 | result: BOOT_SWAP_TYPE_PERM | |
Christopher Collins | fd7eb5c | 2016-12-21 13:46:08 -0800 | [diff] [blame] | 307 | -----------------------------------' |
Fabio Utzig | 5bd4e58 | 2017-07-20 08:55:38 -0300 | [diff] [blame] | 308 | |
Christopher Collins | fd7eb5c | 2016-12-21 13:46:08 -0800 | [diff] [blame] | 309 | |
Fabio Utzig | 86fe4b2 | 2017-07-28 18:56:29 -0300 | [diff] [blame] | 310 | State III |
Christopher Collins | fd7eb5c | 2016-12-21 13:46:08 -0800 | [diff] [blame] | 311 | | slot-0 | slot-1 | |
| 312 | -----------------+--------+--------| |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 313 | magic | Good | Unset | |
Christopher Collins | fd7eb5c | 2016-12-21 13:46:08 -0800 | [diff] [blame] | 314 | image-ok | 0xff | Any | |
Fabio Utzig | 5bd4e58 | 2017-07-20 08:55:38 -0300 | [diff] [blame] | 315 | copy-done | 0x01 | Any | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 316 | -----------------+--------+--------' |
Marti Bolivar | 048d8d8 | 2017-08-04 17:14:24 -0400 | [diff] [blame] | 317 | result: BOOT_SWAP_TYPE_REVERT | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 318 | -----------------------------------' |
| 319 | |
Marti Bolivar | 048d8d8 | 2017-08-04 17:14:24 -0400 | [diff] [blame] | 320 | 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] | 321 | |
Marti Bolivar | 048d8d8 | 2017-08-04 17:14:24 -0400 | [diff] [blame] | 322 | Otherwise, mcuboot does not attempt to swap images, resulting in one of the |
| 323 | other three swap types, as illustrated by State IV. |
Fabio Utzig | 86fe4b2 | 2017-07-28 18:56:29 -0300 | [diff] [blame] | 324 | |
| 325 | |
| 326 | State IV |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 327 | | slot-0 | slot-1 | |
| 328 | -----------------+--------+--------| |
Fabio Utzig | 86fe4b2 | 2017-07-28 18:56:29 -0300 | [diff] [blame] | 329 | magic | Any | Any | |
| 330 | image-ok | Any | Any | |
Fabio Utzig | f9d4428 | 2017-07-20 15:05:13 -0300 | [diff] [blame] | 331 | copy-done | Any | Any | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 332 | -----------------+--------+--------' |
Marti Bolivar | 048d8d8 | 2017-08-04 17:14:24 -0400 | [diff] [blame] | 333 | result: BOOT_SWAP_TYPE_NONE, | |
| 334 | BOOT_SWAP_TYPE_FAIL, or | |
| 335 | BOOT_SWAP_TYPE_PANIC | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 336 | -----------------------------------' |
| 337 | |
Marti Bolivar | 048d8d8 | 2017-08-04 17:14:24 -0400 | [diff] [blame] | 338 | In State IV, when no errors occur, mcuboot will attempt to boot the contents of |
| 339 | slot 0 directly, and the result is BOOT_SWAP_TYPE_NONE. If the image in slot 0 |
| 340 | is not valid, the result is BOOT_SWAP_TYPE_FAIL. If a fatal error occurs during |
| 341 | boot, the result is BOOT_SWAP_TYPE_PANIC. If the result is either |
| 342 | BOOT_SWAP_TYPE_FAIL or BOOT_SWAP_TYPE_PANIC, mcuboot hangs rather than booting |
| 343 | an invalid or compromised image. |
Fabio Utzig | 86fe4b2 | 2017-07-28 18:56:29 -0300 | [diff] [blame] | 344 | |
Marti Bolivar | 048d8d8 | 2017-08-04 17:14:24 -0400 | [diff] [blame] | 345 | Note: An important caveat to the above is the result when a swap is requested |
| 346 | and the image in slot 1 fails to validate, due to a hashing or signing |
| 347 | error. This state behaves as State IV with the extra action of marking |
| 348 | the image in slot 0 as "OK", to prevent further attempts to swap. |
Fabio Utzig | 86fe4b2 | 2017-07-28 18:56:29 -0300 | [diff] [blame] | 349 | |
| 350 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 351 | *** HIGH-LEVEL OPERATION |
| 352 | |
| 353 | With the terms defined, we can now explore the boot loader's operation. First, |
| 354 | a high-level overview of the boot process is presented. Then, the following |
| 355 | sections describe each step of the process in more detail. |
| 356 | |
| 357 | Procedure: |
| 358 | |
Fabio Utzig | 86fe4b2 | 2017-07-28 18:56:29 -0300 | [diff] [blame] | 359 | A. Inspect swap status region; is an interrupted swap being resumed? |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 360 | Yes: Complete the partial swap operation; skip to step C. |
| 361 | No: Proceed to step B. |
| 362 | |
Fabio Utzig | 86fe4b2 | 2017-07-28 18:56:29 -0300 | [diff] [blame] | 363 | B. Inspect image trailers; is a swap requested? |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 364 | Yes. |
| 365 | 1. Is the requested image valid (integrity and security check)? |
| 366 | Yes. |
| 367 | a. Perform swap operation. |
Fabio Utzig | 86fe4b2 | 2017-07-28 18:56:29 -0300 | [diff] [blame] | 368 | b. Persist completion of swap procedure to image trailers. |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 369 | c. Proceed to step C. |
| 370 | No. |
| 371 | a. Erase invalid image. |
Fabio Utzig | 86fe4b2 | 2017-07-28 18:56:29 -0300 | [diff] [blame] | 372 | b. Persist failure of swap procedure to image trailers. |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 373 | c. Proceed to step C. |
| 374 | No: Proceed to step C. |
| 375 | |
| 376 | C. Boot into image in slot 0. |
| 377 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 378 | *** IMAGE SWAPPING |
| 379 | |
| 380 | The boot loader swaps the contents of the two image slots for two reasons: |
Christopher Collins | fd7eb5c | 2016-12-21 13:46:08 -0800 | [diff] [blame] | 381 | * User has issued a "set pending" operation; the image in slot-1 should be |
| 382 | run once (state II) or repeatedly (state III), depending on whether a |
| 383 | permanent swap was specified. |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 384 | * Test image rebooted without being confirmed; the boot loader should |
Christopher Collins | fd7eb5c | 2016-12-21 13:46:08 -0800 | [diff] [blame] | 385 | revert to the original image currently in slot-1 (state IV). |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 386 | |
Fabio Utzig | 86fe4b2 | 2017-07-28 18:56:29 -0300 | [diff] [blame] | 387 | 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] | 388 | run, the boot loader needs to copy it to the primary slot. The image currently |
| 389 | in the primary slot also needs to be retained in flash so that it can be used |
| 390 | later. Furthermore, both images need to be recoverable if the boot loader |
| 391 | resets in the middle of the swap operation. The two images are swapped |
| 392 | according to the following procedure: |
| 393 | |
| 394 | 1. Determine how many flash sectors each image slot consists of. This |
| 395 | number must be the same for both slots. |
| 396 | 2. Iterate the list of sector indices in descending order (i.e., starting |
| 397 | with the greatest index); current element = "index". |
| 398 | b. Erase scratch area. |
| 399 | c. Copy slot0[index] to scratch area. |
Fabio Utzig | 5bd4e58 | 2017-07-20 08:55:38 -0300 | [diff] [blame] | 400 | - If these are the last sectors (i.e., first swap being perfomed), |
| 401 | copy the full sector *except* the image trailer. |
| 402 | - Else, copy entire sector contents. |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 403 | d. Write updated swap status (i). |
| 404 | |
| 405 | e. Erase slot1[index] |
| 406 | f. Copy slot0[index] to slot1[index] |
| 407 | - If these are the last sectors (i.e., first swap being perfomed), |
| 408 | copy the full sector *except* the image trailer. |
| 409 | - Else, copy entire sector contents. |
| 410 | g. Write updated swap status (ii). |
| 411 | |
| 412 | h. Erase slot0[index]. |
| 413 | i. Copy scratch area slot0[index]. |
Fabio Utzig | 5bd4e58 | 2017-07-20 08:55:38 -0300 | [diff] [blame] | 414 | - If these are the last sectors (i.e., first swap being perfomed), |
| 415 | copy the full sector *except* the image trailer. |
| 416 | - Else, copy entire sector contents. |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 417 | j. Write updated swap status (iii). |
| 418 | |
| 419 | 3. Persist completion of swap procedure to slot 0 image trailer. |
| 420 | |
Christopher Collins | fd7eb5c | 2016-12-21 13:46:08 -0800 | [diff] [blame] | 421 | The additional caveats in step 2f are necessary so that the slot 1 image |
| 422 | trailer can be written by the user at a later time. With the image trailer |
| 423 | unwritten, the user can test the image in slot 1 (i.e., transition to state |
| 424 | II). |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 425 | |
Fabio Utzig | 5bd4e58 | 2017-07-20 08:55:38 -0300 | [diff] [blame] | 426 | Note1: If the sector being copied is the last sector, then swap status is |
| 427 | temporarily maintained on scratch for the duration of this operation, always |
| 428 | using slot0's area otherwise. |
| 429 | |
| 430 | Note2: The bootloader tries to copy only used sectors (based on largest image |
| 431 | installed on any of the slots), minimizing the amount of sectors copied and |
| 432 | reducing the amount of time required for a swap operation. |
| 433 | |
Christopher Collins | fd7eb5c | 2016-12-21 13:46:08 -0800 | [diff] [blame] | 434 | The particulars of step 3 vary depending on whether an image is being tested, |
Fabio Utzig | 86fe4b2 | 2017-07-28 18:56:29 -0300 | [diff] [blame] | 435 | permanently used, reverted or a validation failure of slot 1 happened when a |
| 436 | swap was requested: |
| 437 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 438 | * test: |
| 439 | o Write slot0.copy_done = 1 |
Christopher Collins | fd7eb5c | 2016-12-21 13:46:08 -0800 | [diff] [blame] | 440 | (swap caused the following values to be written: |
| 441 | slot0.magic = BOOT_MAGIC |
| 442 | slot0.image_ok = Unset) |
Christopher Collins | fd7eb5c | 2016-12-21 13:46:08 -0800 | [diff] [blame] | 443 | |
| 444 | * permanent: |
| 445 | o Write slot0.copy_done = 1 |
| 446 | (swap caused the following values to be written: |
| 447 | slot0.magic = BOOT_MAGIC |
| 448 | slot0.image_ok = 0x01) |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 449 | |
| 450 | * revert: |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 451 | o Write slot0.copy_done = 1 |
| 452 | o Write slot0.image_ok = 1 |
Fabio Utzig | 86fe4b2 | 2017-07-28 18:56:29 -0300 | [diff] [blame] | 453 | (swap caused the following values to be written: |
| 454 | slot0.magic = BOOT_MAGIC) |
| 455 | |
| 456 | * failure to validate slot 1: |
| 457 | o Write slot0.image_ok = 1 |
| 458 | |
| 459 | After completing the operations as described above the image in slot 0 should |
| 460 | be booted. |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 461 | |
| 462 | *** SWAP STATUS |
| 463 | |
| 464 | The swap status region allows the boot loader to recover in case it restarts in |
| 465 | the middle of an image swap operation. The swap status region consists of a |
| 466 | series of single-byte records. These records are written independently, and |
| 467 | therefore must be padded according to the minimum write size imposed by the |
| 468 | flash hardware. In the below figure, a min-write-size of 1 is assumed for |
| 469 | simplicity. The structure of the swap status region is illustrated below. In |
| 470 | this figure, a min-write-size of 1 is assumed for simplicity. |
| 471 | |
| 472 | 0 1 2 3 |
| 473 | 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 |
| 474 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 475 | |sec127,state 0 |sec127,state 1 |sec127,state 2 |sec126,state 0 | |
| 476 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 477 | |sec126,state 1 |sec126,state 2 |sec125,state 0 |sec125,state 1 | |
| 478 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 479 | |sec125,state 2 | | |
| 480 | +-+-+-+-+-+-+-+-+ + |
| 481 | ~ ~ |
| 482 | ~ [Records for indices 124 through 1 ~ |
| 483 | ~ ~ |
| 484 | ~ +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 485 | ~ |sec000,state 0 |sec000,state 1 |sec000,state 2 | |
| 486 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 487 | |
| 488 | The above is probably not helpful at all; here is a description in English. |
| 489 | |
| 490 | Each image slot is partitioned into a sequence of flash sectors. If we were to |
| 491 | enumerate the sectors in a single slot, starting at 0, we would have a list of |
| 492 | sector indices. Since there are two image slots, each sector index would |
| 493 | correspond to a pair of sectors. For example, sector index 0 corresponds to |
| 494 | the first sector in slot 0 and the first sector in slot 1. Furthermore, we |
| 495 | impose a limit of 128 indices. If an image slot consists of more than 128 |
| 496 | sectors, the flash layout is not compatible with this boot loader. Finally, |
| 497 | reverse the list of indices such that the list starts with index 127 and ends |
| 498 | with 0. The swap status region is a representation of this reversed list. |
| 499 | |
| 500 | During a swap operation, each sector index transitions through four separate |
| 501 | states: |
| 502 | 0. slot 0: image 0, slot 1: image 1, scratch: N/A |
| 503 | 1. slot 0: image 0, slot 1: N/A, scratch: image 1 (1->s, erase 1) |
| 504 | 2. slot 0: N/A, slot 1: image 0, scratch: image 1 (0->1, erase 0) |
| 505 | 3. slot 0: image 1, slot 1: image 0, scratch: N/A (s->0) |
| 506 | |
| 507 | Each time a sector index transitions to a new state, the boot loader writes a |
| 508 | record to the swap status region. Logically, the boot loader only needs one |
| 509 | record per sector index to keep track of the current swap state. However, due |
| 510 | to limitations imposed by flash hardware, a record cannot be overwritten when |
| 511 | an index's state changes. To solve this problem, the boot loader uses three |
| 512 | records per sector index rather than just one. |
| 513 | |
| 514 | Each sector-state pair is represented as a set of three records. The record |
| 515 | values map to the above four states as follows |
| 516 | |
| 517 | | rec0 | rec1 | rec2 |
| 518 | --------+------+------+------ |
| 519 | state 0 | 0xff | 0xff | 0xff |
| 520 | state 1 | 0x01 | 0xff | 0xff |
| 521 | state 2 | 0x01 | 0x02 | 0xff |
| 522 | state 3 | 0x01 | 0x02 | 0x03 |
| 523 | |
| 524 | The swap status region can accommodate 128 sector indices. Hence, the size of |
| 525 | the region, in bytes, is 128 * min-write-size * 3. The number 128 is chosen |
| 526 | somewhat arbitrarily and will likely be made configurable. The only |
| 527 | requirement for the index count is that is is great enough to account for a |
| 528 | maximum-sized image (i.e., at least as great as the total sector count in an |
| 529 | image slot). If a device's image slots use less than 128 sectors, the first |
| 530 | record that gets written will be somewhere in the middle of the region. For |
| 531 | example, if a slot uses 64 sectors, the first sector index that gets swapped is |
| 532 | 63, which corresponds to the exact halfway point within the region. |
| 533 | |
Fabio Utzig | 5bd4e58 | 2017-07-20 08:55:38 -0300 | [diff] [blame] | 534 | Note: since the scratch area only ever needs to record swapping of the last |
| 535 | sector, it uses at most min-write-size * 3 bytes for its own status area. |
| 536 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 537 | *** RESET RECOVERY |
| 538 | |
| 539 | If the boot loader resets in the middle of a swap operation, the two images may |
| 540 | be discontiguous in flash. Bootutil recovers from this condition by using the |
Fabio Utzig | 86fe4b2 | 2017-07-28 18:56:29 -0300 | [diff] [blame] | 541 | image trailers to determine how the image parts are distributed in flash. |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 542 | |
| 543 | The first step is determine where the relevant swap status region is located. |
| 544 | 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] | 545 | 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] | 546 | contents to swap status location. In these tables, the "source" field |
| 547 | indicates where the swap status region is located. |
| 548 | |
| 549 | | slot-0 | scratch | |
| 550 | ----------+------------+------------| |
| 551 | magic | Good | Any | |
| 552 | copy-done | 0x01 | N/A | |
| 553 | ----------+------------+------------' |
| 554 | source: none | |
| 555 | ------------------------------------' |
Marti Bolivar | 49b2917 | 2017-08-04 14:50:51 -0400 | [diff] [blame] | 556 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 557 | | slot-0 | scratch | |
| 558 | ----------+------------+------------| |
| 559 | magic | Good | Any | |
| 560 | copy-done | 0xff | N/A | |
| 561 | ----------+------------+------------' |
| 562 | source: slot 0 | |
| 563 | ------------------------------------' |
Marti Bolivar | 49b2917 | 2017-08-04 14:50:51 -0400 | [diff] [blame] | 564 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 565 | | slot-0 | scratch | |
| 566 | ----------+------------+------------| |
| 567 | magic | Any | Good | |
| 568 | copy-done | Any | N/A | |
| 569 | ----------+------------+------------' |
| 570 | source: scratch | |
| 571 | ------------------------------------' |
Marti Bolivar | 49b2917 | 2017-08-04 14:50:51 -0400 | [diff] [blame] | 572 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 573 | | slot-0 | scratch | |
| 574 | ----------+------------+------------| |
| 575 | magic | Unset | Any | |
| 576 | copy-done | 0xff | N/A | |
| 577 | ----------+------------+------------| |
Fabio Utzig | 86fe4b2 | 2017-07-28 18:56:29 -0300 | [diff] [blame] | 578 | source: slot 0 | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 579 | ------------------------------------+------------------------------+ |
| 580 | This represents one of two cases: | |
| 581 | o No swaps ever (no status to read, so no harm in checking). | |
| 582 | o Mid-revert; status in slot 0. | |
Fabio Utzig | 86fe4b2 | 2017-07-28 18:56:29 -0300 | [diff] [blame] | 583 | For this reason we assume slot 0 as source, to trigger a check | |
| 584 | of the status area and find out if there was swapping under way. | |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 585 | -------------------------------------------------------------------' |
| 586 | |
| 587 | |
| 588 | If the swap status region indicates that the images are not contiguous, |
| 589 | bootutil completes the swap operation that was in progress when the system was |
| 590 | reset. In other words, it applies the procedure defined in the previous |
| 591 | section, moving image 1 into slot 0 and image 0 into slot 1. If the boot |
| 592 | status file indicates that an image part is present in the scratch area, this |
| 593 | part is copied into the correct location by starting at step e or step h in the |
| 594 | area-swap procedure, depending on whether the part belongs to image 0 or image |
| 595 | 1. |
| 596 | |
| 597 | After the swap operation has been completed, the boot loader proceeds as though |
| 598 | it had just been started. |
| 599 | |
| 600 | *** INTEGRITY CHECK |
| 601 | |
| 602 | 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] | 603 | primary slot. If the boot loader doesn't perform an image swap, then it can |
| 604 | perform an optional integrity check of the image in slot0 if |
| 605 | MCUBOOT_VALIDATE_SLOT0 is set, otherwise it doesn't perform an integrity check. |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 606 | |
| 607 | During the integrity check, the boot loader verifies the following aspects of |
| 608 | an image: |
Fabio Utzig | ea422c2 | 2017-09-11 11:02:47 -0300 | [diff] [blame^] | 609 | * 32-bit magic number must be correct (0x96f3b83d). |
| 610 | * Image must contain an image_tlv_info struct, identified by its magic |
| 611 | (0x6907) exactly following the firmware (hdr_size + img_size). |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 612 | * Image must contain a SHA256 TLV. |
Fabio Utzig | 86fe4b2 | 2017-07-28 18:56:29 -0300 | [diff] [blame] | 613 | * Calculated SHA256 must match SHA256 TLV contents. |
Fabio Utzig | ea422c2 | 2017-09-11 11:02:47 -0300 | [diff] [blame^] | 614 | * Image *may* contain a signature TLV. If it does, it must also have a |
| 615 | KEYHASH TLV with the hash of the key that was used to sign. The list of |
| 616 | keys will then be iterated over looking for the matching key, which then |
| 617 | will then be used to verify the image contents. |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 618 | |
| 619 | *** SECURITY |
| 620 | |
| 621 | As indicated above, the final step of the integrity check is signature |
| 622 | verification. The boot loader can have one or more public keys embedded in it |
| 623 | at build time. During signature verification, the boot loader verifies that an |
Fabio Utzig | ea422c2 | 2017-09-11 11:02:47 -0300 | [diff] [blame^] | 624 | image was signed with a private key that corresponds to the embedded keyhash |
| 625 | TLV. |
Christopher Collins | 92ea77f | 2016-12-12 15:59:26 -0800 | [diff] [blame] | 626 | |
| 627 | For information on embedding public keys in the boot loader, as well as |
Fabio Utzig | 5bd4e58 | 2017-07-20 08:55:38 -0300 | [diff] [blame] | 628 | producing signed images, see: doc/signed_images.md |