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