blob: f0a3f33fabe0df70f0ccaefc7d3302bec7c884a1 [file] [log] [blame]
Christopher Collins92ea77f2016-12-12 15:59:26 -08001#
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 Bolivar49b29172017-08-04 14:50:51 -04009#
Christopher Collins92ea77f2016-12-12 15:59:26 -080010# 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 Utzigac834962017-07-20 13:20:48 -030024mcuboot comprises two packages:
Christopher Collins92ea77f2016-12-12 15:59:26 -080025
26 * The bootutil library (boot/bootutil)
Fabio Utzig5bd4e582017-07-20 08:55:38 -030027 * 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
36*** LIMITATIONS
37
38The boot loader currently only supports images with the following
39characteristics:
40 * Built to run from flash.
Marti Bolivar49b29172017-08-04 14:50:51 -040041 * Built to run from a fixed location (i.e., not position-independent).
Christopher Collins92ea77f2016-12-12 15:59:26 -080042
43*** IMAGE FORMAT
44
45The following definitions describe the image format.
46
47#define IMAGE_MAGIC 0x96f3b83c
48
49#define IMAGE_HEADER_SIZE 32
50
51struct 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. */
59struct 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. */
73struct image_tlv {
74 uint8_t it_type; /* IMAGE_TLV_[...]. */
75 uint8_t _pad;
Marti Bolivar49b29172017-08-04 14:50:51 -040076 uint16_t it_len; /* Data length (not including TLV header). */
Christopher Collins92ea77f2016-12-12 15:59:26 -080077};
78
79/*
80 * Image header flags.
81 */
Marti Bolivar7c057e92017-08-04 14:46:39 -040082#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 Collins92ea77f2016-12-12 15:59:26 -080089
90/*
91 * Image trailer TLV types.
92 */
Fabio Utzig150ea962017-03-08 11:25:09 -030093#define IMAGE_TLV_SHA256 1 /* SHA256 of image hdr and body */
94#define IMAGE_TLV_RSA2048 2 /* RSA2048 of hash output */
Christopher Collins92ea77f2016-12-12 15:59:26 -080095#define IMAGE_TLV_ECDSA224 3 /* ECDSA of hash output */
Fabio Utzig150ea962017-03-08 11:25:09 -030096#define IMAGE_TLV_ECDSA256 4 /* ECDSA of hash output */
Christopher Collins92ea77f2016-12-12 15:59:26 -080097
98Optional type-length-value records (TLVs) containing image metadata are placed
99after the end of the image.
100
101The ih_hdr_size field indicates the length of the header, and therefore the
102offset of the image itself. This field provides for backwards compatibility in
103case of changes to the format of the image header.
104
105*** FLASH MAP
106
Fabio Utzigac834962017-07-20 13:20:48 -0300107A device's flash is partitioned according to its _flash map_. At a high
Christopher Collins92ea77f2016-12-12 15:59:26 -0800108level, the flash map maps numeric IDs to _flash areas_. A flash area is a
109region 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 Bolivar4e64d562017-08-04 14:53:33 -0400113The boot loader uses the following flash area IDs:
Christopher Collins92ea77f2016-12-12 15:59:26 -0800114
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 Bolivar4e64d562017-08-04 14:53:33 -0400120The bootloader area contains the bootloader image itself. The other areas are
121described in subsequent sections.
122
Christopher Collins92ea77f2016-12-12 15:59:26 -0800123*** IMAGE SLOTS
124
125A portion of the flash memory is partitioned into two image slots: a primary
126slot (0) and a secondary slot (1). The boot loader will only run an image from
127the primary slot, so images must be built such that they can run from that
128fixed location in flash. If the boot loader needs to run the image resident in
Marti Bolivara91674f2017-08-04 14:56:08 -0400129the secondary slot, it must copy its contents into the primary slot before doing
130so, either by swapping the two images or by overwriting the contents of the
131primary slot. The bootloader supports either swap- or overwrite-based image
132upgrades, but must be configured at build time to choose one of these two
133strategies.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800134
135In addition to the two image slots, the boot loader requires a scratch area to
136allow for reliable image swapping.
137
Marti Bolivara91674f2017-08-04 14:56:08 -0400138The overwrite upgrade strategy is substantially simpler to implement than the
139image swapping strategy, especially since the bootloader must work properly
140even when it is reset during the middle of an image swap. For this reason, the
141rest of the document describes its behavior when configured to swap images
142during an upgrade.
143
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -0800144*** BOOT STATES
145
146Logically, you can think of a pair of values associated with each image slot:
147pending and confirmed. On startup, the boot loader determines the state of the
148device by inspecting each pair of values. These values have the following
149meanings:
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
160In English, when the user wants to run the secondary image, they set the
161pending flag for the second slot and reboot the device. On startup, the boot
162loader will swap the two images in flash, clear the secondary slot's pending
163flag, and run the newly-copied image in slot 0. If the user set the pending
164flag to "temporary," then this is only a temporary state; if the device reboots
165again, the boot loader swaps the images back to their original slots and boots
166into the original image. If the user doesn't want to revert to the original
167state, they can make the current state permanent by setting the confirmed flag
168in slot 0.
169
170Switching to an alternate image is a two-step process (set + confirm) to
171prevent a device from becoming "bricked" by bad firmware. If the device
172crashes immediately upon booting the second image, the boot loader reverts to
173the working image, rather than repeatedly rebooting into the bad image.
174
175Alternatively, if the user is confident that the second image is good, they can
176set and confirm in a single action by setting the pending flag to "permanent."
177
178The following set of tables illustrate the four possible states that the device
179can 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 Utzig86fe4b22017-07-28 18:56:29 -0300217*** IMAGE TRAILER
Christopher Collins92ea77f2016-12-12 15:59:26 -0800218
Fabio Utzig86fe4b22017-07-28 18:56:29 -0300219For the bootloader to be able to determine the current state and what actions
220should be taken during the current boot operation, it uses metadata existing
221on both slots, and while doing a swap operation it can be located in scratch.
222This metadata is located at the end of the slot and is called image trailer.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800223An image trailer has the following structure:
224
225 0 1 2 3
226 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
227 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Christopher Collins92ea77f2016-12-12 15:59:26 -0800228 ~ ~
229 ~ Swap status (128 * min-write-size * 3) ~
230 ~ ~
231 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Fabio Utzig2c305aa2017-07-20 13:14:25 -0300232 | Copy done | 0xff padding (7 octets) |
Christopher Collins92ea77f2016-12-12 15:59:26 -0800233 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Fabio Utzig2c305aa2017-07-20 13:14:25 -0300234 | Image OK | 0xff padding (7 octets) |
Fabio Utzig5bd4e582017-07-20 08:55:38 -0300235 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
236 | MAGIC (16 octets) |
Christopher Collins92ea77f2016-12-12 15:59:26 -0800237 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
238
239These records are at the end of each image slot. The offset immediately
240following such a record represents the start of the next flash area.
241
242Note: "min-write-size" is a property of the flash hardware. If the hardware
243allows individual bytes to be written at arbitrary addresses, then
244min-write-size is 1. If the hardware only allows writes at even addresses,
245then min-write-size is 2, and so on.
246
247The fields are defined as follows:
248
Fabio Utzig5bd4e582017-07-20 08:55:38 -03002491. Swap status: A series of single-byte records. Each record corresponds to a
250flash sector in an image slot. A swap status byte indicate the location of
251the corresponding sector data. During an image swap, image data is moved one
252sector at a time. The swap status is necessary for resuming a swap operation
253if the device rebooted before a swap operation completed.
254
2552. Copy done: A single byte indicating whether the image in this slot is
256complete (0x01=done; 0xff=not done).
257
2583. Image OK: A single byte indicating whether the image in this slot has been
259confirmed as good by the user (0x01=confirmed; 0xff=not confirmed).
260
2614. MAGIC: The following 16 bytes, written in host-byte-order:
Christopher Collins92ea77f2016-12-12 15:59:26 -0800262
263 const uint32_t boot_img_magic[4] = {
264 0xf395c277,
265 0x7fefd260,
266 0x0f505235,
267 0x8079b62c,
268 };
269
Fabio Utzig86fe4b22017-07-28 18:56:29 -0300270*** IMAGE TRAILERS
271
272At startup, the boot loader determines which of the above four states the
273device is in by inspecting the image trailers. When using the term "image
274trailers" what is meant is the aggregate information provided by both image
275slot's trailers.
276
277The image trailers records are structured around the limitations imposed by flash
Christopher Collins92ea77f2016-12-12 15:59:26 -0800278hardware. As a consequence, they do not have a very intuitive design, and it
279is difficult to get a sense of the state of the device just by looking at the
Fabio Utzig86fe4b22017-07-28 18:56:29 -0300280image trailers. It is better to map all the possible trailer states to the four
Christopher Collins92ea77f2016-12-12 15:59:26 -0800281states described above via a set of tables. These tables are reproduced below.
282In these tables, the "pending" and "confirmed" flags are shown for illustrative
Fabio Utzig86fe4b22017-07-28 18:56:29 -0300283purposes; they are not actually present in the image trailers.
284
285Note: An important caveat about the tables described below is that they must
286be evaluated in the order presented here. Lower state numbers must have a
287higher priority when testing the image trailers.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800288
289
290 State I
291 | slot-0 | slot-1 |
292 -----------------+--------+--------|
Christopher Collins92ea77f2016-12-12 15:59:26 -0800293 magic | Any | Good |
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -0800294 image-ok | Any | Unset |
Fabio Utzigf9d44282017-07-20 15:05:13 -0300295 copy-done | Any | Any |
Christopher Collins92ea77f2016-12-12 15:59:26 -0800296 -----------------+--------+--------'
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -0800297 pending | | T |
Christopher Collins92ea77f2016-12-12 15:59:26 -0800298 confirmed | X | |
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -0800299 -----------------+--------+--------'
300 swap: test |
301 -----------------------------------'
Fabio Utzig5bd4e582017-07-20 08:55:38 -0300302
Christopher Collins92ea77f2016-12-12 15:59:26 -0800303
Fabio Utzig86fe4b22017-07-28 18:56:29 -0300304 State II
Christopher Collins92ea77f2016-12-12 15:59:26 -0800305 | slot-0 | slot-1 |
306 -----------------+--------+--------|
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -0800307 magic | Any | Good |
308 image-ok | Any | 0x01 |
Fabio Utzigf9d44282017-07-20 15:05:13 -0300309 copy-done | Any | Any |
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -0800310 -----------------+--------+--------'
311 pending | | P |
312 confirmed | X | |
313 -----------------+--------+--------'
314 swap: permanent |
315 -----------------------------------'
Fabio Utzig5bd4e582017-07-20 08:55:38 -0300316
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -0800317
Fabio Utzig86fe4b22017-07-28 18:56:29 -0300318 State III
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -0800319 | slot-0 | slot-1 |
320 -----------------+--------+--------|
Christopher Collins92ea77f2016-12-12 15:59:26 -0800321 magic | Good | Unset |
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -0800322 image-ok | 0xff | Any |
Fabio Utzig5bd4e582017-07-20 08:55:38 -0300323 copy-done | 0x01 | Any |
Christopher Collins92ea77f2016-12-12 15:59:26 -0800324 -----------------+--------+--------'
325 pending | | |
326 confirmed | | X |
327 -----------------+--------+--------'
328 swap: revert (test image running) |
329 -----------------------------------'
330
331
Fabio Utzig86fe4b22017-07-28 18:56:29 -0300332Those three states described above are explicity tested for and result in a
333swap decision by the bootloader. If the existing values in the the image trailers
334are not one of those previously described the bootloader will assume no swap
335operation is to be performed which is illustrated by state IV.
336
337
338 State IV
Christopher Collins92ea77f2016-12-12 15:59:26 -0800339 | slot-0 | slot-1 |
340 -----------------+--------+--------|
Fabio Utzig86fe4b22017-07-28 18:56:29 -0300341 magic | Any | Any |
342 image-ok | Any | Any |
Fabio Utzigf9d44282017-07-20 15:05:13 -0300343 copy-done | Any | Any |
Christopher Collins92ea77f2016-12-12 15:59:26 -0800344 -----------------+--------+--------'
345 pending | | |
346 confirmed | X | |
347 -----------------+--------+--------'
Fabio Utzig86fe4b22017-07-28 18:56:29 -0300348 swap: none |
Christopher Collins92ea77f2016-12-12 15:59:26 -0800349 -----------------------------------'
350
Fabio Utzig86fe4b22017-07-28 18:56:29 -0300351
352Note: An important caveat to be mentioned here is, that due to the physical
353limitations of the storage and another possible failure, two extra "states"
354were added specifically to signal a failure in the boot process.
355
3561. One of them is a result of any device read/write error, called panic and
357 will result in the bootloader hanging without booting the image in slot 0.
358
3592. Another state was added specifically to handle the situation in which a swap
360 was requested and the image in slot 1 fails to validate, due to hashing
361 or signing error. This state behaves as state IV with the extra action of
362 marking the image in slot 0 as confirmed.
363
364
Christopher Collins92ea77f2016-12-12 15:59:26 -0800365*** HIGH-LEVEL OPERATION
366
367With the terms defined, we can now explore the boot loader's operation. First,
368a high-level overview of the boot process is presented. Then, the following
369sections describe each step of the process in more detail.
370
371Procedure:
372
Fabio Utzig86fe4b22017-07-28 18:56:29 -0300373A. Inspect swap status region; is an interrupted swap being resumed?
Christopher Collins92ea77f2016-12-12 15:59:26 -0800374 Yes: Complete the partial swap operation; skip to step C.
375 No: Proceed to step B.
376
Fabio Utzig86fe4b22017-07-28 18:56:29 -0300377B. Inspect image trailers; is a swap requested?
Christopher Collins92ea77f2016-12-12 15:59:26 -0800378 Yes.
379 1. Is the requested image valid (integrity and security check)?
380 Yes.
381 a. Perform swap operation.
Fabio Utzig86fe4b22017-07-28 18:56:29 -0300382 b. Persist completion of swap procedure to image trailers.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800383 c. Proceed to step C.
384 No.
385 a. Erase invalid image.
Fabio Utzig86fe4b22017-07-28 18:56:29 -0300386 b. Persist failure of swap procedure to image trailers.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800387 c. Proceed to step C.
388 No: Proceed to step C.
389
390C. Boot into image in slot 0.
391
Christopher Collins92ea77f2016-12-12 15:59:26 -0800392*** IMAGE SWAPPING
393
394The boot loader swaps the contents of the two image slots for two reasons:
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -0800395 * User has issued a "set pending" operation; the image in slot-1 should be
396 run once (state II) or repeatedly (state III), depending on whether a
397 permanent swap was specified.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800398 * Test image rebooted without being confirmed; the boot loader should
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -0800399 revert to the original image currently in slot-1 (state IV).
Christopher Collins92ea77f2016-12-12 15:59:26 -0800400
Fabio Utzig86fe4b22017-07-28 18:56:29 -0300401If the image trailers indicates that the image in the secondary slot should be
Christopher Collins92ea77f2016-12-12 15:59:26 -0800402run, the boot loader needs to copy it to the primary slot. The image currently
403in the primary slot also needs to be retained in flash so that it can be used
404later. Furthermore, both images need to be recoverable if the boot loader
405resets in the middle of the swap operation. The two images are swapped
406according to the following procedure:
407
408 1. Determine how many flash sectors each image slot consists of. This
409 number must be the same for both slots.
410 2. Iterate the list of sector indices in descending order (i.e., starting
411 with the greatest index); current element = "index".
412 b. Erase scratch area.
413 c. Copy slot0[index] to scratch area.
Fabio Utzig5bd4e582017-07-20 08:55:38 -0300414 - If these are the last sectors (i.e., first swap being perfomed),
415 copy the full sector *except* the image trailer.
416 - Else, copy entire sector contents.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800417 d. Write updated swap status (i).
418
419 e. Erase slot1[index]
420 f. Copy slot0[index] to slot1[index]
421 - If these are the last sectors (i.e., first swap being perfomed),
422 copy the full sector *except* the image trailer.
423 - Else, copy entire sector contents.
424 g. Write updated swap status (ii).
425
426 h. Erase slot0[index].
427 i. Copy scratch area slot0[index].
Fabio Utzig5bd4e582017-07-20 08:55:38 -0300428 - If these are the last sectors (i.e., first swap being perfomed),
429 copy the full sector *except* the image trailer.
430 - Else, copy entire sector contents.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800431 j. Write updated swap status (iii).
432
433 3. Persist completion of swap procedure to slot 0 image trailer.
434
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -0800435The additional caveats in step 2f are necessary so that the slot 1 image
436trailer can be written by the user at a later time. With the image trailer
437unwritten, the user can test the image in slot 1 (i.e., transition to state
438II).
Christopher Collins92ea77f2016-12-12 15:59:26 -0800439
Fabio Utzig5bd4e582017-07-20 08:55:38 -0300440Note1: If the sector being copied is the last sector, then swap status is
441temporarily maintained on scratch for the duration of this operation, always
442using slot0's area otherwise.
443
444Note2: The bootloader tries to copy only used sectors (based on largest image
445installed on any of the slots), minimizing the amount of sectors copied and
446reducing the amount of time required for a swap operation.
447
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -0800448The particulars of step 3 vary depending on whether an image is being tested,
Fabio Utzig86fe4b22017-07-28 18:56:29 -0300449permanently used, reverted or a validation failure of slot 1 happened when a
450swap was requested:
451
Christopher Collins92ea77f2016-12-12 15:59:26 -0800452 * test:
453 o Write slot0.copy_done = 1
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -0800454 (swap caused the following values to be written:
455 slot0.magic = BOOT_MAGIC
456 slot0.image_ok = Unset)
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -0800457
458 * permanent:
459 o Write slot0.copy_done = 1
460 (swap caused the following values to be written:
461 slot0.magic = BOOT_MAGIC
462 slot0.image_ok = 0x01)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800463
464 * revert:
Christopher Collins92ea77f2016-12-12 15:59:26 -0800465 o Write slot0.copy_done = 1
466 o Write slot0.image_ok = 1
Fabio Utzig86fe4b22017-07-28 18:56:29 -0300467 (swap caused the following values to be written:
468 slot0.magic = BOOT_MAGIC)
469
470 * failure to validate slot 1:
471 o Write slot0.image_ok = 1
472
473After completing the operations as described above the image in slot 0 should
474be booted.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800475
476*** SWAP STATUS
477
478The swap status region allows the boot loader to recover in case it restarts in
479the middle of an image swap operation. The swap status region consists of a
480series of single-byte records. These records are written independently, and
481therefore must be padded according to the minimum write size imposed by the
482flash hardware. In the below figure, a min-write-size of 1 is assumed for
483simplicity. The structure of the swap status region is illustrated below. In
484this figure, a min-write-size of 1 is assumed for simplicity.
485
486 0 1 2 3
487 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
488 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
489 |sec127,state 0 |sec127,state 1 |sec127,state 2 |sec126,state 0 |
490 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
491 |sec126,state 1 |sec126,state 2 |sec125,state 0 |sec125,state 1 |
492 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
493 |sec125,state 2 | |
494 +-+-+-+-+-+-+-+-+ +
495 ~ ~
496 ~ [Records for indices 124 through 1 ~
497 ~ ~
498 ~ +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
499 ~ |sec000,state 0 |sec000,state 1 |sec000,state 2 |
500 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
501
502The above is probably not helpful at all; here is a description in English.
503
504Each image slot is partitioned into a sequence of flash sectors. If we were to
505enumerate the sectors in a single slot, starting at 0, we would have a list of
506sector indices. Since there are two image slots, each sector index would
507correspond to a pair of sectors. For example, sector index 0 corresponds to
508the first sector in slot 0 and the first sector in slot 1. Furthermore, we
509impose a limit of 128 indices. If an image slot consists of more than 128
510sectors, the flash layout is not compatible with this boot loader. Finally,
511reverse the list of indices such that the list starts with index 127 and ends
512with 0. The swap status region is a representation of this reversed list.
513
514During a swap operation, each sector index transitions through four separate
515states:
516 0. slot 0: image 0, slot 1: image 1, scratch: N/A
517 1. slot 0: image 0, slot 1: N/A, scratch: image 1 (1->s, erase 1)
518 2. slot 0: N/A, slot 1: image 0, scratch: image 1 (0->1, erase 0)
519 3. slot 0: image 1, slot 1: image 0, scratch: N/A (s->0)
520
521Each time a sector index transitions to a new state, the boot loader writes a
522record to the swap status region. Logically, the boot loader only needs one
523record per sector index to keep track of the current swap state. However, due
524to limitations imposed by flash hardware, a record cannot be overwritten when
525an index's state changes. To solve this problem, the boot loader uses three
526records per sector index rather than just one.
527
528Each sector-state pair is represented as a set of three records. The record
529values map to the above four states as follows
530
531 | rec0 | rec1 | rec2
532 --------+------+------+------
533 state 0 | 0xff | 0xff | 0xff
534 state 1 | 0x01 | 0xff | 0xff
535 state 2 | 0x01 | 0x02 | 0xff
536 state 3 | 0x01 | 0x02 | 0x03
537
538The swap status region can accommodate 128 sector indices. Hence, the size of
539the region, in bytes, is 128 * min-write-size * 3. The number 128 is chosen
540somewhat arbitrarily and will likely be made configurable. The only
541requirement for the index count is that is is great enough to account for a
542maximum-sized image (i.e., at least as great as the total sector count in an
543image slot). If a device's image slots use less than 128 sectors, the first
544record that gets written will be somewhere in the middle of the region. For
545example, if a slot uses 64 sectors, the first sector index that gets swapped is
54663, which corresponds to the exact halfway point within the region.
547
Fabio Utzig5bd4e582017-07-20 08:55:38 -0300548Note: since the scratch area only ever needs to record swapping of the last
549sector, it uses at most min-write-size * 3 bytes for its own status area.
550
Christopher Collins92ea77f2016-12-12 15:59:26 -0800551*** RESET RECOVERY
552
553If the boot loader resets in the middle of a swap operation, the two images may
554be discontiguous in flash. Bootutil recovers from this condition by using the
Fabio Utzig86fe4b22017-07-28 18:56:29 -0300555image trailers to determine how the image parts are distributed in flash.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800556
557The first step is determine where the relevant swap status region is located.
558Because this region is embedded within the image slots, its location in flash
Fabio Utzig86fe4b22017-07-28 18:56:29 -0300559changes during a swap operation. The below set of tables map image trailers
Christopher Collins92ea77f2016-12-12 15:59:26 -0800560contents to swap status location. In these tables, the "source" field
561indicates where the swap status region is located.
562
563 | slot-0 | scratch |
564 ----------+------------+------------|
565 magic | Good | Any |
566 copy-done | 0x01 | N/A |
567 ----------+------------+------------'
568 source: none |
569 ------------------------------------'
Marti Bolivar49b29172017-08-04 14:50:51 -0400570
Christopher Collins92ea77f2016-12-12 15:59:26 -0800571 | slot-0 | scratch |
572 ----------+------------+------------|
573 magic | Good | Any |
574 copy-done | 0xff | N/A |
575 ----------+------------+------------'
576 source: slot 0 |
577 ------------------------------------'
Marti Bolivar49b29172017-08-04 14:50:51 -0400578
Christopher Collins92ea77f2016-12-12 15:59:26 -0800579 | slot-0 | scratch |
580 ----------+------------+------------|
581 magic | Any | Good |
582 copy-done | Any | N/A |
583 ----------+------------+------------'
584 source: scratch |
585 ------------------------------------'
Marti Bolivar49b29172017-08-04 14:50:51 -0400586
Christopher Collins92ea77f2016-12-12 15:59:26 -0800587 | slot-0 | scratch |
588 ----------+------------+------------|
589 magic | Unset | Any |
590 copy-done | 0xff | N/A |
591 ----------+------------+------------|
Fabio Utzig86fe4b22017-07-28 18:56:29 -0300592 source: slot 0 |
Christopher Collins92ea77f2016-12-12 15:59:26 -0800593 ------------------------------------+------------------------------+
594 This represents one of two cases: |
595 o No swaps ever (no status to read, so no harm in checking). |
596 o Mid-revert; status in slot 0. |
Fabio Utzig86fe4b22017-07-28 18:56:29 -0300597 For this reason we assume slot 0 as source, to trigger a check |
598 of the status area and find out if there was swapping under way. |
Christopher Collins92ea77f2016-12-12 15:59:26 -0800599 -------------------------------------------------------------------'
600
601
602If the swap status region indicates that the images are not contiguous,
603bootutil completes the swap operation that was in progress when the system was
604reset. In other words, it applies the procedure defined in the previous
605section, moving image 1 into slot 0 and image 0 into slot 1. If the boot
606status file indicates that an image part is present in the scratch area, this
607part is copied into the correct location by starting at step e or step h in the
608area-swap procedure, depending on whether the part belongs to image 0 or image
6091.
610
611After the swap operation has been completed, the boot loader proceeds as though
612it had just been started.
613
614*** INTEGRITY CHECK
615
616An image is checked for integrity immediately before it gets copied into the
Fabio Utzig5bd4e582017-07-20 08:55:38 -0300617primary slot. If the boot loader doesn't perform an image swap, then it can
618perform an optional integrity check of the image in slot0 if
619MCUBOOT_VALIDATE_SLOT0 is set, otherwise it doesn't perform an integrity check.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800620
621During the integrity check, the boot loader verifies the following aspects of
622an image:
623 * 32-bit magic number must be correct (0x96f3b83c).
624 * Image must contain a SHA256 TLV.
Fabio Utzig86fe4b22017-07-28 18:56:29 -0300625 * Calculated SHA256 must match SHA256 TLV contents.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800626 * Image *may* contain a signature TLV. If it does, its contents must be
627 verifiable using a key embedded in the boot loader.
628
629*** SECURITY
630
631As indicated above, the final step of the integrity check is signature
632verification. The boot loader can have one or more public keys embedded in it
633at build time. During signature verification, the boot loader verifies that an
634image was signed with a private key that corresponds to one of its public keys.
635The image signature TLV indicates the index of the key that is has been signed
636with. The boot loader uses this index to identify the corresponding public
637key.
638
639For information on embedding public keys in the boot loader, as well as
Fabio Utzig5bd4e582017-07-20 08:55:38 -0300640producing signed images, see: doc/signed_images.md