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