blob: edb98c696866b3754cbb28b37ad7a586e7b089ca [file] [log] [blame] [view]
David Brown17e20d12017-09-12 11:53:20 -06001<!--
2 Licensed to the Apache Software Foundation (ASF) under one
3 or more contributor license agreements. See the NOTICE file
4 distributed with this work for additional information
5 regarding copyright ownership. The ASF licenses this file
6 to you under the Apache License, Version 2.0 (the
7 "License"); you may not use this file except in compliance
8 with the License. You may obtain a copy of the License at
Christopher Collins92ea77f2016-12-12 15:59:26 -08009
David Brown17e20d12017-09-12 11:53:20 -060010 http://www.apache.org/licenses/LICENSE-2.0
Christopher Collins92ea77f2016-12-12 15:59:26 -080011
David Brown17e20d12017-09-12 11:53:20 -060012 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
Christopher Collins92ea77f2016-12-12 15:59:26 -080023
Fabio Utzigac834962017-07-20 13:20:48 -030024mcuboot comprises two packages:
Christopher Collins92ea77f2016-12-12 15:59:26 -080025
David Brown17e20d12017-09-12 11:53:20 -060026* The bootutil library (boot/bootutil)
27* The boot application (each port has its own at boot/<port>)
Christopher Collins92ea77f2016-12-12 15:59:26 -080028
29The bootutil library performs most of the functions of a boot loader. In
30particular, the piece that is missing is the final step of actually jumping to
31the main image. This last step is instead implemented by the boot application.
32Boot loader functionality is separated in this manner to enable unit testing of
33the boot loader. A library can be unit tested, but an application can't.
34Therefore, functionality is delegated to the bootutil library when possible.
35
David Brown17e20d12017-09-12 11:53:20 -060036## Limitations
Christopher Collins92ea77f2016-12-12 15:59:26 -080037
38The boot loader currently only supports images with the following
39characteristics:
David Brown17e20d12017-09-12 11:53:20 -060040* Built to run from flash.
41* Built to run from a fixed location (i.e., not position-independent).
Christopher Collins92ea77f2016-12-12 15:59:26 -080042
David Brown17e20d12017-09-12 11:53:20 -060043## Image Format
Christopher Collins92ea77f2016-12-12 15:59:26 -080044
45The following definitions describe the image format.
46
David Brown17e20d12017-09-12 11:53:20 -060047``` c
Fabio Utzigea422c22017-09-11 11:02:47 -030048#define IMAGE_MAGIC 0x96f3b83d
Christopher Collins92ea77f2016-12-12 15:59:26 -080049
50#define IMAGE_HEADER_SIZE 32
51
52struct image_version {
53 uint8_t iv_major;
54 uint8_t iv_minor;
55 uint16_t iv_revision;
56 uint32_t iv_build_num;
57};
58
59/** Image header. All fields are in little endian byte order. */
60struct image_header {
61 uint32_t ih_magic;
Fabio Utzigea422c22017-09-11 11:02:47 -030062 uint32_t ih_load_addr;
Christopher Collins92ea77f2016-12-12 15:59:26 -080063 uint16_t ih_hdr_size; /* Size of image header (bytes). */
64 uint16_t _pad2;
65 uint32_t ih_img_size; /* Does not include header. */
Fabio Utzigea422c22017-09-11 11:02:47 -030066 uint32_t ih_flags; /* IMAGE_F_[...]. */
Christopher Collins92ea77f2016-12-12 15:59:26 -080067 struct image_version ih_ver;
68 uint32_t _pad3;
69};
70
Fabio Utzigea422c22017-09-11 11:02:47 -030071/** Image TLV header. All fields in little endian. */
72struct image_tlv_info {
73 uint16_t it_magic;
74 uint16_t it_tlv_tot; /* size of TLV area (including tlv_info header) */
75};
76
Christopher Collins92ea77f2016-12-12 15:59:26 -080077/** Image trailer TLV format. All fields in little endian. */
78struct image_tlv {
79 uint8_t it_type; /* IMAGE_TLV_[...]. */
80 uint8_t _pad;
Marti Bolivar49b29172017-08-04 14:50:51 -040081 uint16_t it_len; /* Data length (not including TLV header). */
Christopher Collins92ea77f2016-12-12 15:59:26 -080082};
83
84/*
85 * Image header flags.
86 */
Marti Bolivar7c057e92017-08-04 14:46:39 -040087#define IMAGE_F_PIC 0x00000001 /* Not supported. */
Marti Bolivar7c057e92017-08-04 14:46:39 -040088#define IMAGE_F_NON_BOOTABLE 0x00000010 /* Split image app. */
Fabio Utzigea422c22017-09-11 11:02:47 -030089#define IMAGE_F_RAM_LOAD 0x00000020
Christopher Collins92ea77f2016-12-12 15:59:26 -080090
91/*
92 * Image trailer TLV types.
93 */
Fabio Utzigea422c22017-09-11 11:02:47 -030094#define IMAGE_TLV_KEYHASH 0x01 /* hash of the public key */
David Brown27648b82017-08-31 10:40:29 -060095#define IMAGE_TLV_SHA256 0x10 /* SHA256 of image hdr and body */
Marko Kiiskila8dd56f32017-08-22 21:40:49 -070096#define IMAGE_TLV_RSA2048_PSS 0x20 /* RSA2048 of hash output */
David Brown27648b82017-08-31 10:40:29 -060097#define IMAGE_TLV_ECDSA224 0x21 /* ECDSA of hash output */
98#define IMAGE_TLV_ECDSA256 0x22 /* ECDSA of hash output */
Fabio Utzig3501c012019-05-13 15:07:25 -070099#define IMAGE_TLV_RSA3072_PSS 0x23 /* RSA3072 of hash output */
Fabio Utzig195411f2019-06-28 07:48:21 -0300100#define IMAGE_TLV_ED25519 0x24 /* ED25519 of hash output */
David Brown17e20d12017-09-12 11:53:20 -0600101```
Christopher Collins92ea77f2016-12-12 15:59:26 -0800102
103Optional type-length-value records (TLVs) containing image metadata are placed
104after the end of the image.
105
David Brown17e20d12017-09-12 11:53:20 -0600106The `ih_hdr_size` field indicates the length of the header, and therefore the
Christopher Collins92ea77f2016-12-12 15:59:26 -0800107offset of the image itself. This field provides for backwards compatibility in
108case of changes to the format of the image header.
109
David Brown17e20d12017-09-12 11:53:20 -0600110## Flash Map
Christopher Collins92ea77f2016-12-12 15:59:26 -0800111
Fabio Utzigac834962017-07-20 13:20:48 -0300112A device's flash is partitioned according to its _flash map_. At a high
Christopher Collins92ea77f2016-12-12 15:59:26 -0800113level, the flash map maps numeric IDs to _flash areas_. A flash area is a
114region of disk with the following properties:
David Brown17e20d12017-09-12 11:53:20 -06001151. An area can be fully erased without affecting any other areas.
1162. A write to one area does not restrict writes to other areas.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800117
Marti Bolivar4e64d562017-08-04 14:53:33 -0400118The boot loader uses the following flash area IDs:
David Vinczeb75c12a2019-03-22 14:58:33 +0100119```c
120/* Independent from multiple image boot */
David Vincze2d736ad2019-02-18 11:50:22 +0100121#define FLASH_AREA_BOOTLOADER 0
David Vinczeb75c12a2019-03-22 14:58:33 +0100122#define FLASH_AREA_IMAGE_SCRATCH 3
123```
124```c
125/* If the boot loader is working with the first image */
David Vincze2d736ad2019-02-18 11:50:22 +0100126#define FLASH_AREA_IMAGE_PRIMARY 1
127#define FLASH_AREA_IMAGE_SECONDARY 2
David Vinczeb75c12a2019-03-22 14:58:33 +0100128```
129```c
130/* If the boot loader is working with the second image */
131#define FLASH_AREA_IMAGE_PRIMARY 5
132#define FLASH_AREA_IMAGE_SECONDARY 6
David Brown17e20d12017-09-12 11:53:20 -0600133```
Christopher Collins92ea77f2016-12-12 15:59:26 -0800134
Marti Bolivar4e64d562017-08-04 14:53:33 -0400135The bootloader area contains the bootloader image itself. The other areas are
David Vinczeb75c12a2019-03-22 14:58:33 +0100136described in subsequent sections. The flash could contain multiple executable
137images therefore the flash area IDs of primary and secondary areas are mapped
138based on the number of the active image (on which the bootloader is currently
139working).
Marti Bolivar4e64d562017-08-04 14:53:33 -0400140
David Brown17e20d12017-09-12 11:53:20 -0600141## Image Slots
Christopher Collins92ea77f2016-12-12 15:59:26 -0800142
David Vinczeb75c12a2019-03-22 14:58:33 +0100143A portion of the flash memory can be partitioned into multiple image areas, each
144contains two image slots: a primary slot and a secondary slot.
145The boot loader will only run an image from the primary slot, so images must be
146built such that they can run from that fixed location in flash. If the boot
147loader needs to run the image resident in the secondary slot, it must copy its
148contents into the primary slot before doing so, either by swapping the two
149images or by overwriting the contents of the primary slot. The bootloader
150supports either swap- or overwrite-based image upgrades, but must be configured
151at build time to choose one of these two strategies.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800152
David Vinczeb75c12a2019-03-22 14:58:33 +0100153In addition to the slots of image areas, the boot loader requires a scratch
154area to allow for reliable image swapping. The scratch area must have a size
155that is enough to store at least the largest sector that is going to be swapped.
156Many devices have small equally sized flash sectors, eg 4K, while others have
David Vincze2d736ad2019-02-18 11:50:22 +0100157variable sized sectors where the largest sectors might be 128K or 256K, so the
158scratch must be big enough to store that. The scratch is only ever used when
159swapping firmware, which means only when doing an upgrade. Given that, the main
160reason for using a larger size for the scratch is that flash wear will be more
161evenly distributed, because a single sector would be written twice the number of
162times than using two sectors, for example. To evaluate the ideal size of the
163scratch for your use case the following parameters are relevant:
Fabio Utziga722f5a2017-12-12 14:04:53 -0200164
165* the ratio of image size / scratch size
166* the number of erase cycles supported by the flash hardware
167
168The image size is used (instead of slot size) because only the slot's sectors
169that are actually used for storing the image are copied. The image/scratch ratio
170is the number of times the scratch will be erased on every upgrade. The number
171of erase cycles divided by the image/scratch ratio will give you the number of
172times an upgrade can be performed before the device goes out of spec.
173
174```
175num_upgrades = number_of_erase_cycles / (image_size / scratch_size)
176```
177
178Let's assume, for example, a device with 10000 erase cycles, an image size of
179150K and a scratch of 4K (usual minimum size of 4K sector devices). This would
180result in a total of:
181
182`10000 / (150 / 4) ~ 267`
183
184Increasing the scratch to 16K would give us:
185
186`10000 / (150 / 16) ~ 1067`
187
188There is no *best* ratio, as the right size is use-case dependent. Factors to
189consider include the number of times a device will be upgraded both in the field
David Vincze2d736ad2019-02-18 11:50:22 +0100190and during development, as well as any desired safety margin on the
191manufacturer's specified number of erase cycles. In general, using a ratio that
192allows hundreds to thousands of field upgrades in production is recommended.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800193
Marti Bolivara91674f2017-08-04 14:56:08 -0400194The overwrite upgrade strategy is substantially simpler to implement than the
195image swapping strategy, especially since the bootloader must work properly
196even when it is reset during the middle of an image swap. For this reason, the
197rest of the document describes its behavior when configured to swap images
198during an upgrade.
199
David Brown17e20d12017-09-12 11:53:20 -0600200## Boot Swap Types
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -0800201
Marti Bolivar048d8d82017-08-04 17:14:24 -0400202When the device first boots under normal circumstances, there is an up-to-date
David Vincze2d736ad2019-02-18 11:50:22 +0100203firmware image in the primary slot, which mcuboot can validate and then
204chain-load. In this case, no image swaps are necessary. During device upgrades,
205however, new candidate images are present in the secondary slot, which mcuboot
206must swap into the primary slot before booting as discussed above.
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -0800207
Marti Bolivar048d8d82017-08-04 17:14:24 -0400208Upgrading an old image with a new one by swapping can be a two-step process. In
209this process, mcuboot performs a "test" swap of image data in flash and boots
210the new image. The new image can then update the contents of flash at runtime
211to mark itself "OK", and mcuboot will then still choose to run it during the
212next boot. When this happens, the swap is made "permanent". If this doesn't
213happen, mcuboot will perform a "revert" swap during the next boot by swapping
214the images back into their original locations, and attempting to boot the old
215image.
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -0800216
Marti Bolivar048d8d82017-08-04 17:14:24 -0400217Depending on the use case, the first swap can also be made permanent directly.
218In this case, mcuboot will never attempt to revert the images on the next reset.
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -0800219
Marti Bolivar048d8d82017-08-04 17:14:24 -0400220Test swaps are supported to provide a rollback mechanism to prevent devices
221from becoming "bricked" by bad firmware. If the device crashes immediately
222upon booting a new (bad) image, mcuboot will revert to the old (working) image
223at the next device reset, rather than booting the bad image again. This allows
224device firmware to make test swaps permanent only after performing a self-test
225routine.
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -0800226
Marti Bolivar048d8d82017-08-04 17:14:24 -0400227On startup, mcuboot inspects the contents of flash to decide which of these
228"swap types" to perform; this decision determines how it proceeds.
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -0800229
Marti Bolivar048d8d82017-08-04 17:14:24 -0400230The possible swap types, and their meanings, are:
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -0800231
David Brown17e20d12017-09-12 11:53:20 -0600232- `BOOT_SWAP_TYPE_NONE`: The "usual" or "no upgrade" case; attempt to boot the
David Vincze2d736ad2019-02-18 11:50:22 +0100233 contents of the primary slot.
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -0800234
David Vincze2d736ad2019-02-18 11:50:22 +0100235- `BOOT_SWAP_TYPE_TEST`: Boot the contents of the secondary slot by swapping
236 images. Unless the swap is made permanent, revert back on the next boot.
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -0800237
David Brown17e20d12017-09-12 11:53:20 -0600238- `BOOT_SWAP_TYPE_PERM`: Permanently swap images, and boot the upgraded image
Marti Bolivar048d8d82017-08-04 17:14:24 -0400239 firmware.
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -0800240
David Vincze2d736ad2019-02-18 11:50:22 +0100241- `BOOT_SWAP_TYPE_REVERT`: A previous test swap was not made permanent;
242 swap back to the old image whose data are now in the secondary slot. If the
243 old image marks itself "OK" when it boots, the next boot will have swap type
244 `BOOT_SWAP_TYPE_NONE`.
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -0800245
David Brown17e20d12017-09-12 11:53:20 -0600246- `BOOT_SWAP_TYPE_FAIL`: Swap failed because image to be run is not valid.
Marti Bolivar048d8d82017-08-04 17:14:24 -0400247
David Brown17e20d12017-09-12 11:53:20 -0600248- `BOOT_SWAP_TYPE_PANIC`: Swapping encountered an unrecoverable error.
Marti Bolivar048d8d82017-08-04 17:14:24 -0400249
250The "swap type" is a high-level representation of the outcome of the
251boot. Subsequent sections describe how mcuboot determines the swap type from
252the bit-level contents of flash.
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -0800253
David Brown17e20d12017-09-12 11:53:20 -0600254## Image Trailer
Christopher Collins92ea77f2016-12-12 15:59:26 -0800255
Fabio Utzig86fe4b22017-07-28 18:56:29 -0300256For the bootloader to be able to determine the current state and what actions
Marti Bolivar42818032017-08-04 15:45:01 -0400257should be taken during the current boot operation, it uses metadata stored in
258the image flash areas. While swapping, some of this metadata is temporarily
259copied into and out of the scratch area.
260
261This metadata is located at the end of the image flash areas, and is called an
262image trailer. An image trailer has the following structure:
Christopher Collins92ea77f2016-12-12 15:59:26 -0800263
David Brown17e20d12017-09-12 11:53:20 -0600264```
Christopher Collins92ea77f2016-12-12 15:59:26 -0800265 0 1 2 3
266 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
267 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Christopher Collins92ea77f2016-12-12 15:59:26 -0800268 ~ ~
Fabio Utzig2c05f1b2018-04-04 10:35:17 -0300269 ~ Swap status (BOOT_MAX_IMG_SECTORS * min-write-size * 3) ~
Christopher Collins92ea77f2016-12-12 15:59:26 -0800270 ~ ~
271 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Christopher Collinsa1c12042019-05-23 14:00:28 -0700272 | Encryption key 0 (16 octets) [*] |
273 | |
Christopher Collins92ea77f2016-12-12 15:59:26 -0800274 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Christopher Collinsa1c12042019-05-23 14:00:28 -0700275 | Encryption key 1 (16 octets) [*] |
276 | |
Fabio Utzig5bd4e582017-07-20 08:55:38 -0300277 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Christopher Collinsa1c12042019-05-23 14:00:28 -0700278 | Swap size (4 octets) |
Fabio Utzigea422c22017-09-11 11:02:47 -0300279 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
David Vinczee2453472019-06-17 12:31:59 +0200280 | Swap info | 0xff padding (7 octets) |
Fabio Utzigea422c22017-09-11 11:02:47 -0300281 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Christopher Collinsa1c12042019-05-23 14:00:28 -0700282 | Copy done | 0xff padding (7 octets) |
283 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
284 | Image OK | 0xff padding (7 octets) |
285 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
286 | MAGIC (16 octets) |
287 | |
Christopher Collins92ea77f2016-12-12 15:59:26 -0800288 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
David Brown17e20d12017-09-12 11:53:20 -0600289```
Christopher Collins92ea77f2016-12-12 15:59:26 -0800290
Christopher Collinsa1c12042019-05-23 14:00:28 -0700291[*]: Only present if the encryption option is enabled (`MCUBOOT_ENC_IMAGES`).
292
Marti Bolivar42818032017-08-04 15:45:01 -0400293The offset immediately following such a record represents the start of the next
294flash area.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800295
296Note: "min-write-size" is a property of the flash hardware. If the hardware
297allows individual bytes to be written at arbitrary addresses, then
298min-write-size is 1. If the hardware only allows writes at even addresses,
299then min-write-size is 2, and so on.
300
Marti Bolivar1dcb6852017-08-04 15:59:32 -0400301An image trailer contains the following fields:
Christopher Collins92ea77f2016-12-12 15:59:26 -0800302
Marti Bolivar1dcb6852017-08-04 15:59:32 -04003031. Swap status: A series of records which records the progress of an image
David Vincze2d736ad2019-02-18 11:50:22 +0100304 swap. To swap entire images, data are swapped between the two image areas
305 one or more sectors at a time, like this:
Marti Bolivar1dcb6852017-08-04 15:59:32 -0400306
David Vincze2d736ad2019-02-18 11:50:22 +0100307 - sector data in the primary slot is copied into scratch, then erased
308 - sector data in the secondary slot is copied into the primary slot,
309 then erased
310 - sector data in scratch is copied into the secondary slot
Marti Bolivar1dcb6852017-08-04 15:59:32 -0400311
312As it swaps images, the bootloader updates the swap status field in a way that
313allows it to compute how far this swap operation has progressed for each
314sector. The swap status field can thus used to resume a swap operation if the
315bootloader is halted while a swap operation is ongoing and later reset. The
David Vincze2d736ad2019-02-18 11:50:22 +0100316`BOOT_MAX_IMG_SECTORS` value is the configurable maximum number of sectors
317mcuboot supports for each image; its value defaults to 128, but allows for
318either decreasing this size, to limit RAM usage, or to increase it in devices
319that have massive amounts of Flash or very small sized sectors and thus require
320a bigger configuration to allow for the handling of all slot's sectors.
321The factor of min-write-sz is due to the behavior of flash hardware. The factor
322of 3 is explained below.
Fabio Utzig5bd4e582017-07-20 08:55:38 -0300323
Christopher Collinsa1c12042019-05-23 14:00:28 -07003242. Encryption keys: key-encrypting keys (KEKs). These keys are needed for
325 image encryption and decryption. See the
326 [encrypted images](encrypted_images.md) document for more information.
327
3283. Swap size: When beginning a new swap operation, the total size that needs
David Vincze2d736ad2019-02-18 11:50:22 +0100329 to be swapped (based on the slot with largest image + tlvs) is written to
330 this location for easier recovery in case of a reset while performing the
331 swap.
Fabio Utzigea422c22017-09-11 11:02:47 -0300332
David Vinczee2453472019-06-17 12:31:59 +02003334. Swap info: A single byte which encodes the following informations:
334 - Swap type: Stored in bits 0-3. Indicating the type of swap operation in
335 progress. When mcuboot resumes an interrupted swap, it uses this field to
336 determine the type of operation to perform. This field contains one of the
337 following values in the table below.
338 - Image number: Stored in bits 4-7. It has always 0 value at single image
339 boot. In case of multi image boot it indicates, which image was swapped when
340 interrupt happened. The same scratch area is used during in case of all
341 image swap operation. Therefore this field is used to determine which image
342 the trailer belongs to if boot status is found on scratch area when the swap
343 operation is resumed.
Christopher Collinsa1c12042019-05-23 14:00:28 -0700344
345| Name | Value |
346| ------------------------- | ----- |
347| `BOOT_SWAP_TYPE_TEST` | 2 |
348| `BOOT_SWAP_TYPE_PERM` | 3 |
349| `BOOT_SWAP_TYPE_REVERT` | 4 |
350
351
3525. Copy done: A single byte indicating whether the image in this slot is
David Brown17e20d12017-09-12 11:53:20 -0600353 complete (0x01=done; 0xff=not done).
Fabio Utzig5bd4e582017-07-20 08:55:38 -0300354
Christopher Collinsa1c12042019-05-23 14:00:28 -07003556. Image OK: A single byte indicating whether the image in this slot has been
David Brown17e20d12017-09-12 11:53:20 -0600356 confirmed as good by the user (0x01=confirmed; 0xff=not confirmed).
Fabio Utzig5bd4e582017-07-20 08:55:38 -0300357
Christopher Collinsa1c12042019-05-23 14:00:28 -07003587. MAGIC: The following 16 bytes, written in host-byte-order:
Christopher Collins92ea77f2016-12-12 15:59:26 -0800359
David Brown17e20d12017-09-12 11:53:20 -0600360``` c
Christopher Collins92ea77f2016-12-12 15:59:26 -0800361 const uint32_t boot_img_magic[4] = {
362 0xf395c277,
363 0x7fefd260,
364 0x0f505235,
365 0x8079b62c,
366 };
David Brown17e20d12017-09-12 11:53:20 -0600367```
Christopher Collins92ea77f2016-12-12 15:59:26 -0800368
David Brown17e20d12017-09-12 11:53:20 -0600369## IMAGE TRAILERS
Fabio Utzig86fe4b22017-07-28 18:56:29 -0300370
Marti Bolivar048d8d82017-08-04 17:14:24 -0400371At startup, the boot loader determines the boot swap type by inspecting the
372image trailers. When using the term "image trailers" what is meant is the
373aggregate information provided by both image slot's trailers.
Fabio Utzig86fe4b22017-07-28 18:56:29 -0300374
Christopher Collinsa1c12042019-05-23 14:00:28 -0700375### New swaps (non-resumes)
376
377For new swaps, mcuboot must inspect a collection of fields to determine which
378swap operation to perform.
379
David Vincze2d736ad2019-02-18 11:50:22 +0100380The image trailers records are structured around the limitations imposed by
381flash hardware. As a consequence, they do not have a very intuitive design, and
382it is difficult to get a sense of the state of the device just by looking at the
Marti Bolivar048d8d82017-08-04 17:14:24 -0400383image trailers. It is better to map all the possible trailer states to the swap
384types described above via a set of tables. These tables are reproduced below.
Fabio Utzig86fe4b22017-07-28 18:56:29 -0300385
386Note: An important caveat about the tables described below is that they must
387be evaluated in the order presented here. Lower state numbers must have a
388higher priority when testing the image trailers.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800389
David Brown17e20d12017-09-12 11:53:20 -0600390```
Christopher Collins92ea77f2016-12-12 15:59:26 -0800391 State I
David Vincze2d736ad2019-02-18 11:50:22 +0100392 | primary slot | secondary slot |
393 -----------------+--------------+----------------|
394 magic | Any | Good |
395 image-ok | Any | Unset |
396 copy-done | Any | Any |
397 -----------------+--------------+----------------'
398 result: BOOT_SWAP_TYPE_TEST |
399 -------------------------------------------------'
Fabio Utzig5bd4e582017-07-20 08:55:38 -0300400
Christopher Collins92ea77f2016-12-12 15:59:26 -0800401
Fabio Utzig86fe4b22017-07-28 18:56:29 -0300402 State II
David Vincze2d736ad2019-02-18 11:50:22 +0100403 | primary slot | secondary slot |
404 -----------------+--------------+----------------|
405 magic | Any | Good |
406 image-ok | Any | 0x01 |
407 copy-done | Any | Any |
408 -----------------+--------------+----------------'
409 result: BOOT_SWAP_TYPE_PERM |
410 -------------------------------------------------'
Fabio Utzig5bd4e582017-07-20 08:55:38 -0300411
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -0800412
Fabio Utzig86fe4b22017-07-28 18:56:29 -0300413 State III
David Vincze2d736ad2019-02-18 11:50:22 +0100414 | primary slot | secondary slot |
415 -----------------+--------------+----------------|
416 magic | Good | Unset |
417 image-ok | 0xff | Any |
418 copy-done | 0x01 | Any |
419 -----------------+--------------+----------------'
420 result: BOOT_SWAP_TYPE_REVERT |
421 -------------------------------------------------'
David Brown17e20d12017-09-12 11:53:20 -0600422```
Christopher Collins92ea77f2016-12-12 15:59:26 -0800423
Marti Bolivar048d8d82017-08-04 17:14:24 -0400424Any of the above three states results in mcuboot attempting to swap images.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800425
Marti Bolivar048d8d82017-08-04 17:14:24 -0400426Otherwise, mcuboot does not attempt to swap images, resulting in one of the
427other three swap types, as illustrated by State IV.
Fabio Utzig86fe4b22017-07-28 18:56:29 -0300428
David Brown17e20d12017-09-12 11:53:20 -0600429```
Fabio Utzig86fe4b22017-07-28 18:56:29 -0300430 State IV
David Vincze2d736ad2019-02-18 11:50:22 +0100431 | primary slot | secondary slot |
432 -----------------+--------------+----------------|
433 magic | Any | Any |
434 image-ok | Any | Any |
435 copy-done | Any | Any |
436 -----------------+--------------+----------------'
437 result: BOOT_SWAP_TYPE_NONE, |
438 BOOT_SWAP_TYPE_FAIL, or |
439 BOOT_SWAP_TYPE_PANIC |
440 -------------------------------------------------'
David Brown17e20d12017-09-12 11:53:20 -0600441```
Christopher Collins92ea77f2016-12-12 15:59:26 -0800442
Marti Bolivar048d8d82017-08-04 17:14:24 -0400443In State IV, when no errors occur, mcuboot will attempt to boot the contents of
David Vincze2d736ad2019-02-18 11:50:22 +0100444the primary slot directly, and the result is `BOOT_SWAP_TYPE_NONE`. If the image
445in the primary slot is not valid, the result is `BOOT_SWAP_TYPE_FAIL`. If a
446fatal error occurs during boot, the result is `BOOT_SWAP_TYPE_PANIC`. If the
447result is either `BOOT_SWAP_TYPE_FAIL` or `BOOT_SWAP_TYPE_PANIC`, mcuboot hangs
448rather than booting an invalid or compromised image.
Fabio Utzig86fe4b22017-07-28 18:56:29 -0300449
Marti Bolivar048d8d82017-08-04 17:14:24 -0400450Note: An important caveat to the above is the result when a swap is requested
David Vincze2d736ad2019-02-18 11:50:22 +0100451 and the image in the secondary slot fails to validate, due to a hashing or
452 signing error. This state behaves as State IV with the extra action of
453 marking the image in the primary slot as "OK", to prevent further attempts
454 to swap.
Fabio Utzig86fe4b22017-07-28 18:56:29 -0300455
Christopher Collinsa1c12042019-05-23 14:00:28 -0700456### Resumed swaps
457
458If mcuboot determines that it is resuming an interrupted swap (i.e., a reset
459occurred mid-swap), it fully determines the operation to resume by reading the
David Vinczee2453472019-06-17 12:31:59 +0200460`swap info` field from the active trailer and extracting the swap type from bits
4610-3. The set of tables in the previous section are not necessary in the resume
462case.
Christopher Collinsa1c12042019-05-23 14:00:28 -0700463
David Brown17e20d12017-09-12 11:53:20 -0600464## High-Level Operation
Christopher Collins92ea77f2016-12-12 15:59:26 -0800465
466With the terms defined, we can now explore the boot loader's operation. First,
467a high-level overview of the boot process is presented. Then, the following
468sections describe each step of the process in more detail.
469
470Procedure:
471
David Brown17e20d12017-09-12 11:53:20 -06004721. Inspect swap status region; is an interrupted swap being resumed?
473 Yes: Complete the partial swap operation; skip to step 3.
474 No: Proceed to step 2.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800475
David Brown17e20d12017-09-12 11:53:20 -06004762. Inspect image trailers; is a swap requested?
Christopher Collins92ea77f2016-12-12 15:59:26 -0800477 Yes.
David Vincze2d736ad2019-02-18 11:50:22 +0100478 ​ 1. Is the requested image valid (integrity and security check)?
479 ​ Yes.
480 ​ a. Perform swap operation.
481 ​ b. Persist completion of swap procedure to image trailers.
482 ​ c. Proceed to step 3.
483 ​ No.
484 ​ a. Erase invalid image.
485 ​ b. Persist failure of swap procedure to image trailers.
486 ​ c. Proceed to step 3.
David Brown17e20d12017-09-12 11:53:20 -0600487 No: Proceed to step 3.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800488
David Vincze2d736ad2019-02-18 11:50:22 +01004893. Boot into image in primary slot.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800490
David Brown17e20d12017-09-12 11:53:20 -0600491## Image Swapping
Christopher Collins92ea77f2016-12-12 15:59:26 -0800492
493The boot loader swaps the contents of the two image slots for two reasons:
David Vincze2d736ad2019-02-18 11:50:22 +0100494 * User has issued a "set pending" operation; the image in the secondary slot
495 should be run once (state II) or repeatedly (state III), depending on
496 whether a permanent swap was specified.
497​ * Test image rebooted without being confirmed; the boot loader should
498 revert to the original image currently in the secondary slot (state IV).
Christopher Collins92ea77f2016-12-12 15:59:26 -0800499
Fabio Utzig86fe4b22017-07-28 18:56:29 -0300500If the image trailers indicates that the image in the secondary slot should be
Christopher Collins92ea77f2016-12-12 15:59:26 -0800501run, the boot loader needs to copy it to the primary slot. The image currently
502in the primary slot also needs to be retained in flash so that it can be used
503later. Furthermore, both images need to be recoverable if the boot loader
504resets in the middle of the swap operation. The two images are swapped
505according to the following procedure:
506
David Brown17e20d12017-09-12 11:53:20 -0600507<!-- Markdown doesn't do nested numbered lists. It will do nested
508bulletted lists, so maybe that is better. -->
David Vincze2d736ad2019-02-18 11:50:22 +0100509​ 1. Determine how many flash sectors each image slot consists of. This
510​ number must be the same for both slots.
511​ 2. Iterate the list of sector indices in descending order (i.e., starting
512​ with the greatest index); current element = "index".
513​ b. Erase scratch area.
514​ c. Copy secondary_slot[index] to scratch area.
515​ - If these are the last sectors (i.e., first swap being perfomed),
516​ copy the full sector *except* the image trailer.
517​ - Else, copy entire sector contents.
518​ d. Write updated swap status (i).
519​ e. Erase secondary_slot[index]
520​ f. Copy primary_slot[index] to secondary_slot[index]
521​ - If these are the last sectors (i.e., first swap being perfomed),
522​ copy the full sector *except* the image trailer.
523​ - Else, copy entire sector contents.
524​ g. Write updated swap status (ii).
525​ h. Erase primary_slot[index].
526​ i. Copy scratch area to primary_slot[index].
527​ - If these are the last sectors (i.e., first swap being perfomed),
528​ copy the full sector *except* the image trailer.
529​ - Else, copy entire sector contents.
530​ j. Write updated swap status (iii).
531​ 3. Persist completion of swap procedure to the primary slot image trailer.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800532
David Vincze2d736ad2019-02-18 11:50:22 +0100533The additional caveats in step 2f are necessary so that the secondary slot image
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -0800534trailer can be written by the user at a later time. With the image trailer
David Vincze2d736ad2019-02-18 11:50:22 +0100535unwritten, the user can test the image in the secondary slot
536(i.e., transition to state II).
Christopher Collins92ea77f2016-12-12 15:59:26 -0800537
Fabio Utzig5bd4e582017-07-20 08:55:38 -0300538Note1: If the sector being copied is the last sector, then swap status is
539temporarily maintained on scratch for the duration of this operation, always
David Vincze2d736ad2019-02-18 11:50:22 +0100540using the primary slot's area otherwise.
Fabio Utzig5bd4e582017-07-20 08:55:38 -0300541
542Note2: The bootloader tries to copy only used sectors (based on largest image
543installed on any of the slots), minimizing the amount of sectors copied and
544reducing the amount of time required for a swap operation.
545
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -0800546The particulars of step 3 vary depending on whether an image is being tested,
David Vincze2d736ad2019-02-18 11:50:22 +0100547permanently used, reverted or a validation failure of the secondary slot
548happened when a swap was requested:
Fabio Utzig86fe4b22017-07-28 18:56:29 -0300549
Christopher Collins92ea77f2016-12-12 15:59:26 -0800550 * test:
David Vincze2d736ad2019-02-18 11:50:22 +0100551 o Write primary_slot.copy_done = 1
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -0800552 (swap caused the following values to be written:
David Vincze2d736ad2019-02-18 11:50:22 +0100553 primary_slot.magic = BOOT_MAGIC
554 primary_slot.image_ok = Unset)
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -0800555
556 * permanent:
David Vincze2d736ad2019-02-18 11:50:22 +0100557 o Write primary_slot.copy_done = 1
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -0800558 (swap caused the following values to be written:
David Vincze2d736ad2019-02-18 11:50:22 +0100559 primary_slot.magic = BOOT_MAGIC
560 primary_slot.image_ok = 0x01)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800561
562 * revert:
David Vincze2d736ad2019-02-18 11:50:22 +0100563 o Write primary_slot.copy_done = 1
564 o Write primary_slot.image_ok = 1
Fabio Utzig86fe4b22017-07-28 18:56:29 -0300565 (swap caused the following values to be written:
David Vincze2d736ad2019-02-18 11:50:22 +0100566 primary_slot.magic = BOOT_MAGIC)
Fabio Utzig86fe4b22017-07-28 18:56:29 -0300567
David Vincze2d736ad2019-02-18 11:50:22 +0100568 * failure to validate the secondary slot:
569 o Write primary_slot.image_ok = 1
Fabio Utzig86fe4b22017-07-28 18:56:29 -0300570
David Vincze2d736ad2019-02-18 11:50:22 +0100571After completing the operations as described above the image in the primary slot
572should be booted.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800573
David Brown17e20d12017-09-12 11:53:20 -0600574## Swap Status
Christopher Collins92ea77f2016-12-12 15:59:26 -0800575
576The swap status region allows the boot loader to recover in case it restarts in
577the middle of an image swap operation. The swap status region consists of a
578series of single-byte records. These records are written independently, and
579therefore must be padded according to the minimum write size imposed by the
580flash hardware. In the below figure, a min-write-size of 1 is assumed for
581simplicity. The structure of the swap status region is illustrated below. In
582this figure, a min-write-size of 1 is assumed for simplicity.
583
David Brown17e20d12017-09-12 11:53:20 -0600584```
Christopher Collins92ea77f2016-12-12 15:59:26 -0800585 0 1 2 3
586 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
587 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
588 |sec127,state 0 |sec127,state 1 |sec127,state 2 |sec126,state 0 |
589 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
590 |sec126,state 1 |sec126,state 2 |sec125,state 0 |sec125,state 1 |
591 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
592 |sec125,state 2 | |
593 +-+-+-+-+-+-+-+-+ +
594 ~ ~
595 ~ [Records for indices 124 through 1 ~
596 ~ ~
597 ~ +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
598 ~ |sec000,state 0 |sec000,state 1 |sec000,state 2 |
599 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
David Brown17e20d12017-09-12 11:53:20 -0600600```
Christopher Collins92ea77f2016-12-12 15:59:26 -0800601
602The above is probably not helpful at all; here is a description in English.
603
604Each image slot is partitioned into a sequence of flash sectors. If we were to
605enumerate the sectors in a single slot, starting at 0, we would have a list of
606sector indices. Since there are two image slots, each sector index would
607correspond to a pair of sectors. For example, sector index 0 corresponds to
David Vincze2d736ad2019-02-18 11:50:22 +0100608the first sector in the primary slot and the first sector in the secondary slot.
609Finally, reverse the list of indices such that the list starts with index
610`BOOT_MAX_IMG_SECTORS - 1` and ends with 0. The swap status region is a
611representation of this reversed list.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800612
613During a swap operation, each sector index transitions through four separate
614states:
David Brown17e20d12017-09-12 11:53:20 -0600615```
David Vincze2d736ad2019-02-18 11:50:22 +01006160. primary slot: image 0, secondary slot: image 1, scratch: N/A
6171. primary slot: image 0, secondary slot: N/A, scratch: image 1 (1->s, erase 1)
6182. primary slot: N/A, secondary slot: image 0, scratch: image 1 (0->1, erase 0)
6193. primary slot: image 1, secondary slot: image 0, scratch: N/A (s->0)
David Brown17e20d12017-09-12 11:53:20 -0600620```
Christopher Collins92ea77f2016-12-12 15:59:26 -0800621
622Each time a sector index transitions to a new state, the boot loader writes a
623record to the swap status region. Logically, the boot loader only needs one
624record per sector index to keep track of the current swap state. However, due
625to limitations imposed by flash hardware, a record cannot be overwritten when
626an index's state changes. To solve this problem, the boot loader uses three
627records per sector index rather than just one.
628
629Each sector-state pair is represented as a set of three records. The record
630values map to the above four states as follows
631
David Brown17e20d12017-09-12 11:53:20 -0600632```
Christopher Collins92ea77f2016-12-12 15:59:26 -0800633 | rec0 | rec1 | rec2
634 --------+------+------+------
635 state 0 | 0xff | 0xff | 0xff
636 state 1 | 0x01 | 0xff | 0xff
637 state 2 | 0x01 | 0x02 | 0xff
638 state 3 | 0x01 | 0x02 | 0x03
David Brown17e20d12017-09-12 11:53:20 -0600639```
Christopher Collins92ea77f2016-12-12 15:59:26 -0800640
Fabio Utzig2c05f1b2018-04-04 10:35:17 -0300641The swap status region can accommodate `BOOT_MAX_IMG_SECTORS` sector indices.
David Vincze2d736ad2019-02-18 11:50:22 +0100642Hence, the size of the region, in bytes, is
643`BOOT_MAX_IMG_SECTORS * min-write-size * 3`. The only requirement for the index
644count is that it is great enough to account for a maximum-sized image
645(i.e., at least as great as the total sector count in an image slot). If a
646device's image slots have been configured with `BOOT_MAX_IMG_SECTORS: 128` and
647use less than 128 sectors, the first record that gets written will be somewhere
648in the middle of the region. For example, if a slot uses 64 sectors, the first
649sector index that gets swapped is 63, which corresponds to the exact halfway
650point within the region.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800651
Fabio Utzig5bd4e582017-07-20 08:55:38 -0300652Note: since the scratch area only ever needs to record swapping of the last
653sector, it uses at most min-write-size * 3 bytes for its own status area.
654
David Brown17e20d12017-09-12 11:53:20 -0600655## Reset Recovery
Christopher Collins92ea77f2016-12-12 15:59:26 -0800656
657If the boot loader resets in the middle of a swap operation, the two images may
658be discontiguous in flash. Bootutil recovers from this condition by using the
Fabio Utzig86fe4b22017-07-28 18:56:29 -0300659image trailers to determine how the image parts are distributed in flash.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800660
661The first step is determine where the relevant swap status region is located.
662Because this region is embedded within the image slots, its location in flash
Fabio Utzig86fe4b22017-07-28 18:56:29 -0300663changes during a swap operation. The below set of tables map image trailers
Christopher Collins92ea77f2016-12-12 15:59:26 -0800664contents to swap status location. In these tables, the "source" field
665indicates where the swap status region is located.
666
David Brown17e20d12017-09-12 11:53:20 -0600667```
David Vincze2d736ad2019-02-18 11:50:22 +0100668 | primary slot | scratch |
669 ----------+--------------+--------------|
670 magic | Good | Any |
671 copy-done | 0x01 | N/A |
672 ----------+--------------+--------------'
673 source: none |
674 ----------------------------------------'
Marti Bolivar49b29172017-08-04 14:50:51 -0400675
David Vincze2d736ad2019-02-18 11:50:22 +0100676 | primary slot | scratch |
677 ----------+--------------+--------------|
678 magic | Good | Any |
679 copy-done | 0xff | N/A |
680 ----------+--------------+--------------'
681 source: primary slot |
682 ----------------------------------------'
Marti Bolivar49b29172017-08-04 14:50:51 -0400683
David Vincze2d736ad2019-02-18 11:50:22 +0100684 | primary slot | scratch |
685 ----------+--------------+--------------|
686 magic | Any | Good |
687 copy-done | Any | N/A |
688 ----------+--------------+--------------'
689 source: scratch |
690 ----------------------------------------'
Marti Bolivar49b29172017-08-04 14:50:51 -0400691
David Vincze2d736ad2019-02-18 11:50:22 +0100692 | primary slot | scratch |
693 ----------+--------------+--------------|
694 magic | Unset | Any |
695 copy-done | 0xff | N/A |
696 ----------+--------------+--------------|
697 source: primary slot |
698 ----------------------------------------+------------------------------+
699 This represents one of two cases: |
700 o No swaps ever (no status to read, so no harm in checking). |
701 o Mid-revert; status in the primary slot. |
702 For this reason we assume the primary slot as source, to trigger a |
703 check of the status area and find out if there was swapping under way. |
704 -----------------------------------------------------------------------'
David Brown17e20d12017-09-12 11:53:20 -0600705```
Christopher Collins92ea77f2016-12-12 15:59:26 -0800706
Christopher Collinsa1c12042019-05-23 14:00:28 -0700707If the swap status region indicates that the images are not contiguous, mcuboot
708determines the type of swap operation that was interrupted by reading the `swap
David Vinczee2453472019-06-17 12:31:59 +0200709info` field in the active image trailer and extarcting the swap type from bits
7100-3 then resumes the operation. In other words, it applies the procedure defined
711in the previous section, moving image 1 into the primary slot and image 0 into
712the secondary slot. If the boot status indicates that an image part is present
713in the scratch area, this part is copied into the correct location by starting
714at step e or step h in the area-swap procedure, depending on whether the part
715belongs to image 0 or image 1.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800716
717After the swap operation has been completed, the boot loader proceeds as though
718it had just been started.
719
David Brown17e20d12017-09-12 11:53:20 -0600720## Integrity Check
Christopher Collins92ea77f2016-12-12 15:59:26 -0800721
722An image is checked for integrity immediately before it gets copied into the
Fabio Utzig5bd4e582017-07-20 08:55:38 -0300723primary slot. If the boot loader doesn't perform an image swap, then it can
David Vincze2d736ad2019-02-18 11:50:22 +0100724perform an optional integrity check of the image in the primary slot if
725`MCUBOOT_VALIDATE_PRIMARY_SLOT` is set, otherwise it doesn't perform an
726integrity check.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800727
728During the integrity check, the boot loader verifies the following aspects of
729an image:
David Vincze2d736ad2019-02-18 11:50:22 +0100730​ * 32-bit magic number must be correct (0x96f3b83d).
731​ * Image must contain an `image_tlv_info` struct, identified by its magic
732​ (0x6907) exactly following the firmware (hdr_size + img_size).
733​ * Image must contain a SHA256 TLV.
734​ * Calculated SHA256 must match SHA256 TLV contents.
735​ * Image *may* contain a signature TLV. If it does, it must also have a
736​ KEYHASH TLV with the hash of the key that was used to sign. The list of
737​ keys will then be iterated over looking for the matching key, which then
738​ will then be used to verify the image contents.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800739
David Brown17e20d12017-09-12 11:53:20 -0600740## Security
Christopher Collins92ea77f2016-12-12 15:59:26 -0800741
742As indicated above, the final step of the integrity check is signature
743verification. The boot loader can have one or more public keys embedded in it
744at build time. During signature verification, the boot loader verifies that an
Fabio Utzigea422c22017-09-11 11:02:47 -0300745image was signed with a private key that corresponds to the embedded keyhash
746TLV.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800747
748For information on embedding public keys in the boot loader, as well as
Fabio Utzig4dce6aa2018-02-12 15:31:32 -0200749producing signed images, see: [signed_images](signed_images.md).
Fabio Utzigcdfa11a2018-10-01 09:45:54 -0300750
751If you want to enable and use encrypted images, see:
752[encrypted_images](encrypted_images.md).