blob: 523fc59d9fa9a0777363b4165040b34d2f106d3c [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
Marti Bolivar42818032017-08-04 15:45:01 -0400220should be taken during the current boot operation, it uses metadata stored in
221the image flash areas. While swapping, some of this metadata is temporarily
222copied into and out of the scratch area.
223
224This metadata is located at the end of the image flash areas, and is called an
225image trailer. An image trailer has the following structure:
Christopher Collins92ea77f2016-12-12 15:59:26 -0800226
227 0 1 2 3
228 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
229 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Christopher Collins92ea77f2016-12-12 15:59:26 -0800230 ~ ~
231 ~ Swap status (128 * min-write-size * 3) ~
232 ~ ~
233 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Fabio Utzig2c305aa2017-07-20 13:14:25 -0300234 | Copy done | 0xff padding (7 octets) |
Christopher Collins92ea77f2016-12-12 15:59:26 -0800235 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Fabio Utzig2c305aa2017-07-20 13:14:25 -0300236 | Image OK | 0xff padding (7 octets) |
Fabio Utzig5bd4e582017-07-20 08:55:38 -0300237 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
238 | MAGIC (16 octets) |
Christopher Collins92ea77f2016-12-12 15:59:26 -0800239 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
240
Marti Bolivar42818032017-08-04 15:45:01 -0400241The offset immediately following such a record represents the start of the next
242flash area.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800243
244Note: "min-write-size" is a property of the flash hardware. If the hardware
245allows individual bytes to be written at arbitrary addresses, then
246min-write-size is 1. If the hardware only allows writes at even addresses,
247then min-write-size is 2, and so on.
248
249The fields are defined as follows:
250
Fabio Utzig5bd4e582017-07-20 08:55:38 -03002511. Swap status: A series of single-byte records. Each record corresponds to a
252flash sector in an image slot. A swap status byte indicate the location of
253the corresponding sector data. During an image swap, image data is moved one
254sector at a time. The swap status is necessary for resuming a swap operation
255if the device rebooted before a swap operation completed.
256
2572. Copy done: A single byte indicating whether the image in this slot is
258complete (0x01=done; 0xff=not done).
259
2603. Image OK: A single byte indicating whether the image in this slot has been
261confirmed as good by the user (0x01=confirmed; 0xff=not confirmed).
262
2634. MAGIC: The following 16 bytes, written in host-byte-order:
Christopher Collins92ea77f2016-12-12 15:59:26 -0800264
265 const uint32_t boot_img_magic[4] = {
266 0xf395c277,
267 0x7fefd260,
268 0x0f505235,
269 0x8079b62c,
270 };
271
Fabio Utzig86fe4b22017-07-28 18:56:29 -0300272*** IMAGE TRAILERS
273
274At startup, the boot loader determines which of the above four states the
275device is in by inspecting the image trailers. When using the term "image
276trailers" what is meant is the aggregate information provided by both image
277slot's trailers.
278
279The image trailers records are structured around the limitations imposed by flash
Christopher Collins92ea77f2016-12-12 15:59:26 -0800280hardware. As a consequence, they do not have a very intuitive design, and it
281is difficult to get a sense of the state of the device just by looking at the
Fabio Utzig86fe4b22017-07-28 18:56:29 -0300282image trailers. It is better to map all the possible trailer states to the four
Christopher Collins92ea77f2016-12-12 15:59:26 -0800283states described above via a set of tables. These tables are reproduced below.
284In these tables, the "pending" and "confirmed" flags are shown for illustrative
Fabio Utzig86fe4b22017-07-28 18:56:29 -0300285purposes; they are not actually present in the image trailers.
286
287Note: An important caveat about the tables described below is that they must
288be evaluated in the order presented here. Lower state numbers must have a
289higher priority when testing the image trailers.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800290
291
292 State I
293 | slot-0 | slot-1 |
294 -----------------+--------+--------|
Christopher Collins92ea77f2016-12-12 15:59:26 -0800295 magic | Any | Good |
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -0800296 image-ok | Any | Unset |
Fabio Utzigf9d44282017-07-20 15:05:13 -0300297 copy-done | Any | Any |
Christopher Collins92ea77f2016-12-12 15:59:26 -0800298 -----------------+--------+--------'
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -0800299 pending | | T |
Christopher Collins92ea77f2016-12-12 15:59:26 -0800300 confirmed | X | |
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -0800301 -----------------+--------+--------'
302 swap: test |
303 -----------------------------------'
Fabio Utzig5bd4e582017-07-20 08:55:38 -0300304
Christopher Collins92ea77f2016-12-12 15:59:26 -0800305
Fabio Utzig86fe4b22017-07-28 18:56:29 -0300306 State II
Christopher Collins92ea77f2016-12-12 15:59:26 -0800307 | slot-0 | slot-1 |
308 -----------------+--------+--------|
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -0800309 magic | Any | Good |
310 image-ok | Any | 0x01 |
Fabio Utzigf9d44282017-07-20 15:05:13 -0300311 copy-done | Any | Any |
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -0800312 -----------------+--------+--------'
313 pending | | P |
314 confirmed | X | |
315 -----------------+--------+--------'
316 swap: permanent |
317 -----------------------------------'
Fabio Utzig5bd4e582017-07-20 08:55:38 -0300318
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -0800319
Fabio Utzig86fe4b22017-07-28 18:56:29 -0300320 State III
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -0800321 | slot-0 | slot-1 |
322 -----------------+--------+--------|
Christopher Collins92ea77f2016-12-12 15:59:26 -0800323 magic | Good | Unset |
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -0800324 image-ok | 0xff | Any |
Fabio Utzig5bd4e582017-07-20 08:55:38 -0300325 copy-done | 0x01 | Any |
Christopher Collins92ea77f2016-12-12 15:59:26 -0800326 -----------------+--------+--------'
327 pending | | |
328 confirmed | | X |
329 -----------------+--------+--------'
330 swap: revert (test image running) |
331 -----------------------------------'
332
333
Fabio Utzig86fe4b22017-07-28 18:56:29 -0300334Those three states described above are explicity tested for and result in a
335swap decision by the bootloader. If the existing values in the the image trailers
336are not one of those previously described the bootloader will assume no swap
337operation is to be performed which is illustrated by state IV.
338
339
340 State IV
Christopher Collins92ea77f2016-12-12 15:59:26 -0800341 | slot-0 | slot-1 |
342 -----------------+--------+--------|
Fabio Utzig86fe4b22017-07-28 18:56:29 -0300343 magic | Any | Any |
344 image-ok | Any | Any |
Fabio Utzigf9d44282017-07-20 15:05:13 -0300345 copy-done | Any | Any |
Christopher Collins92ea77f2016-12-12 15:59:26 -0800346 -----------------+--------+--------'
347 pending | | |
348 confirmed | X | |
349 -----------------+--------+--------'
Fabio Utzig86fe4b22017-07-28 18:56:29 -0300350 swap: none |
Christopher Collins92ea77f2016-12-12 15:59:26 -0800351 -----------------------------------'
352
Fabio Utzig86fe4b22017-07-28 18:56:29 -0300353
354Note: An important caveat to be mentioned here is, that due to the physical
355limitations of the storage and another possible failure, two extra "states"
356were added specifically to signal a failure in the boot process.
357
3581. One of them is a result of any device read/write error, called panic and
359 will result in the bootloader hanging without booting the image in slot 0.
360
3612. Another state was added specifically to handle the situation in which a swap
362 was requested and the image in slot 1 fails to validate, due to hashing
363 or signing error. This state behaves as state IV with the extra action of
364 marking the image in slot 0 as confirmed.
365
366
Christopher Collins92ea77f2016-12-12 15:59:26 -0800367*** HIGH-LEVEL OPERATION
368
369With the terms defined, we can now explore the boot loader's operation. First,
370a high-level overview of the boot process is presented. Then, the following
371sections describe each step of the process in more detail.
372
373Procedure:
374
Fabio Utzig86fe4b22017-07-28 18:56:29 -0300375A. Inspect swap status region; is an interrupted swap being resumed?
Christopher Collins92ea77f2016-12-12 15:59:26 -0800376 Yes: Complete the partial swap operation; skip to step C.
377 No: Proceed to step B.
378
Fabio Utzig86fe4b22017-07-28 18:56:29 -0300379B. Inspect image trailers; is a swap requested?
Christopher Collins92ea77f2016-12-12 15:59:26 -0800380 Yes.
381 1. Is the requested image valid (integrity and security check)?
382 Yes.
383 a. Perform swap operation.
Fabio Utzig86fe4b22017-07-28 18:56:29 -0300384 b. Persist completion of swap procedure to image trailers.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800385 c. Proceed to step C.
386 No.
387 a. Erase invalid image.
Fabio Utzig86fe4b22017-07-28 18:56:29 -0300388 b. Persist failure of swap procedure to image trailers.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800389 c. Proceed to step C.
390 No: Proceed to step C.
391
392C. Boot into image in slot 0.
393
Christopher Collins92ea77f2016-12-12 15:59:26 -0800394*** IMAGE SWAPPING
395
396The boot loader swaps the contents of the two image slots for two reasons:
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -0800397 * User has issued a "set pending" operation; the image in slot-1 should be
398 run once (state II) or repeatedly (state III), depending on whether a
399 permanent swap was specified.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800400 * Test image rebooted without being confirmed; the boot loader should
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -0800401 revert to the original image currently in slot-1 (state IV).
Christopher Collins92ea77f2016-12-12 15:59:26 -0800402
Fabio Utzig86fe4b22017-07-28 18:56:29 -0300403If the image trailers indicates that the image in the secondary slot should be
Christopher Collins92ea77f2016-12-12 15:59:26 -0800404run, the boot loader needs to copy it to the primary slot. The image currently
405in the primary slot also needs to be retained in flash so that it can be used
406later. Furthermore, both images need to be recoverable if the boot loader
407resets in the middle of the swap operation. The two images are swapped
408according to the following procedure:
409
410 1. Determine how many flash sectors each image slot consists of. This
411 number must be the same for both slots.
412 2. Iterate the list of sector indices in descending order (i.e., starting
413 with the greatest index); current element = "index".
414 b. Erase scratch area.
415 c. Copy slot0[index] to scratch area.
Fabio Utzig5bd4e582017-07-20 08:55:38 -0300416 - If these are the last sectors (i.e., first swap being perfomed),
417 copy the full sector *except* the image trailer.
418 - Else, copy entire sector contents.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800419 d. Write updated swap status (i).
420
421 e. Erase slot1[index]
422 f. Copy slot0[index] to slot1[index]
423 - If these are the last sectors (i.e., first swap being perfomed),
424 copy the full sector *except* the image trailer.
425 - Else, copy entire sector contents.
426 g. Write updated swap status (ii).
427
428 h. Erase slot0[index].
429 i. Copy scratch area slot0[index].
Fabio Utzig5bd4e582017-07-20 08:55:38 -0300430 - If these are the last sectors (i.e., first swap being perfomed),
431 copy the full sector *except* the image trailer.
432 - Else, copy entire sector contents.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800433 j. Write updated swap status (iii).
434
435 3. Persist completion of swap procedure to slot 0 image trailer.
436
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -0800437The additional caveats in step 2f are necessary so that the slot 1 image
438trailer can be written by the user at a later time. With the image trailer
439unwritten, the user can test the image in slot 1 (i.e., transition to state
440II).
Christopher Collins92ea77f2016-12-12 15:59:26 -0800441
Fabio Utzig5bd4e582017-07-20 08:55:38 -0300442Note1: If the sector being copied is the last sector, then swap status is
443temporarily maintained on scratch for the duration of this operation, always
444using slot0's area otherwise.
445
446Note2: The bootloader tries to copy only used sectors (based on largest image
447installed on any of the slots), minimizing the amount of sectors copied and
448reducing the amount of time required for a swap operation.
449
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -0800450The particulars of step 3 vary depending on whether an image is being tested,
Fabio Utzig86fe4b22017-07-28 18:56:29 -0300451permanently used, reverted or a validation failure of slot 1 happened when a
452swap was requested:
453
Christopher Collins92ea77f2016-12-12 15:59:26 -0800454 * test:
455 o Write slot0.copy_done = 1
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -0800456 (swap caused the following values to be written:
457 slot0.magic = BOOT_MAGIC
458 slot0.image_ok = Unset)
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -0800459
460 * permanent:
461 o Write slot0.copy_done = 1
462 (swap caused the following values to be written:
463 slot0.magic = BOOT_MAGIC
464 slot0.image_ok = 0x01)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800465
466 * revert:
Christopher Collins92ea77f2016-12-12 15:59:26 -0800467 o Write slot0.copy_done = 1
468 o Write slot0.image_ok = 1
Fabio Utzig86fe4b22017-07-28 18:56:29 -0300469 (swap caused the following values to be written:
470 slot0.magic = BOOT_MAGIC)
471
472 * failure to validate slot 1:
473 o Write slot0.image_ok = 1
474
475After completing the operations as described above the image in slot 0 should
476be booted.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800477
478*** SWAP STATUS
479
480The swap status region allows the boot loader to recover in case it restarts in
481the middle of an image swap operation. The swap status region consists of a
482series of single-byte records. These records are written independently, and
483therefore must be padded according to the minimum write size imposed by the
484flash hardware. In the below figure, a min-write-size of 1 is assumed for
485simplicity. The structure of the swap status region is illustrated below. In
486this figure, a min-write-size of 1 is assumed for simplicity.
487
488 0 1 2 3
489 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
490 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
491 |sec127,state 0 |sec127,state 1 |sec127,state 2 |sec126,state 0 |
492 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
493 |sec126,state 1 |sec126,state 2 |sec125,state 0 |sec125,state 1 |
494 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
495 |sec125,state 2 | |
496 +-+-+-+-+-+-+-+-+ +
497 ~ ~
498 ~ [Records for indices 124 through 1 ~
499 ~ ~
500 ~ +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
501 ~ |sec000,state 0 |sec000,state 1 |sec000,state 2 |
502 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
503
504The above is probably not helpful at all; here is a description in English.
505
506Each image slot is partitioned into a sequence of flash sectors. If we were to
507enumerate the sectors in a single slot, starting at 0, we would have a list of
508sector indices. Since there are two image slots, each sector index would
509correspond to a pair of sectors. For example, sector index 0 corresponds to
510the first sector in slot 0 and the first sector in slot 1. Furthermore, we
511impose a limit of 128 indices. If an image slot consists of more than 128
512sectors, the flash layout is not compatible with this boot loader. Finally,
513reverse the list of indices such that the list starts with index 127 and ends
514with 0. The swap status region is a representation of this reversed list.
515
516During a swap operation, each sector index transitions through four separate
517states:
518 0. slot 0: image 0, slot 1: image 1, scratch: N/A
519 1. slot 0: image 0, slot 1: N/A, scratch: image 1 (1->s, erase 1)
520 2. slot 0: N/A, slot 1: image 0, scratch: image 1 (0->1, erase 0)
521 3. slot 0: image 1, slot 1: image 0, scratch: N/A (s->0)
522
523Each time a sector index transitions to a new state, the boot loader writes a
524record to the swap status region. Logically, the boot loader only needs one
525record per sector index to keep track of the current swap state. However, due
526to limitations imposed by flash hardware, a record cannot be overwritten when
527an index's state changes. To solve this problem, the boot loader uses three
528records per sector index rather than just one.
529
530Each sector-state pair is represented as a set of three records. The record
531values map to the above four states as follows
532
533 | rec0 | rec1 | rec2
534 --------+------+------+------
535 state 0 | 0xff | 0xff | 0xff
536 state 1 | 0x01 | 0xff | 0xff
537 state 2 | 0x01 | 0x02 | 0xff
538 state 3 | 0x01 | 0x02 | 0x03
539
540The swap status region can accommodate 128 sector indices. Hence, the size of
541the region, in bytes, is 128 * min-write-size * 3. The number 128 is chosen
542somewhat arbitrarily and will likely be made configurable. The only
543requirement for the index count is that is is great enough to account for a
544maximum-sized image (i.e., at least as great as the total sector count in an
545image slot). If a device's image slots use less than 128 sectors, the first
546record that gets written will be somewhere in the middle of the region. For
547example, if a slot uses 64 sectors, the first sector index that gets swapped is
54863, which corresponds to the exact halfway point within the region.
549
Fabio Utzig5bd4e582017-07-20 08:55:38 -0300550Note: since the scratch area only ever needs to record swapping of the last
551sector, it uses at most min-write-size * 3 bytes for its own status area.
552
Christopher Collins92ea77f2016-12-12 15:59:26 -0800553*** RESET RECOVERY
554
555If the boot loader resets in the middle of a swap operation, the two images may
556be discontiguous in flash. Bootutil recovers from this condition by using the
Fabio Utzig86fe4b22017-07-28 18:56:29 -0300557image trailers to determine how the image parts are distributed in flash.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800558
559The first step is determine where the relevant swap status region is located.
560Because this region is embedded within the image slots, its location in flash
Fabio Utzig86fe4b22017-07-28 18:56:29 -0300561changes during a swap operation. The below set of tables map image trailers
Christopher Collins92ea77f2016-12-12 15:59:26 -0800562contents to swap status location. In these tables, the "source" field
563indicates where the swap status region is located.
564
565 | slot-0 | scratch |
566 ----------+------------+------------|
567 magic | Good | Any |
568 copy-done | 0x01 | N/A |
569 ----------+------------+------------'
570 source: none |
571 ------------------------------------'
Marti Bolivar49b29172017-08-04 14:50:51 -0400572
Christopher Collins92ea77f2016-12-12 15:59:26 -0800573 | slot-0 | scratch |
574 ----------+------------+------------|
575 magic | Good | Any |
576 copy-done | 0xff | N/A |
577 ----------+------------+------------'
578 source: slot 0 |
579 ------------------------------------'
Marti Bolivar49b29172017-08-04 14:50:51 -0400580
Christopher Collins92ea77f2016-12-12 15:59:26 -0800581 | slot-0 | scratch |
582 ----------+------------+------------|
583 magic | Any | Good |
584 copy-done | Any | N/A |
585 ----------+------------+------------'
586 source: scratch |
587 ------------------------------------'
Marti Bolivar49b29172017-08-04 14:50:51 -0400588
Christopher Collins92ea77f2016-12-12 15:59:26 -0800589 | slot-0 | scratch |
590 ----------+------------+------------|
591 magic | Unset | Any |
592 copy-done | 0xff | N/A |
593 ----------+------------+------------|
Fabio Utzig86fe4b22017-07-28 18:56:29 -0300594 source: slot 0 |
Christopher Collins92ea77f2016-12-12 15:59:26 -0800595 ------------------------------------+------------------------------+
596 This represents one of two cases: |
597 o No swaps ever (no status to read, so no harm in checking). |
598 o Mid-revert; status in slot 0. |
Fabio Utzig86fe4b22017-07-28 18:56:29 -0300599 For this reason we assume slot 0 as source, to trigger a check |
600 of the status area and find out if there was swapping under way. |
Christopher Collins92ea77f2016-12-12 15:59:26 -0800601 -------------------------------------------------------------------'
602
603
604If the swap status region indicates that the images are not contiguous,
605bootutil completes the swap operation that was in progress when the system was
606reset. In other words, it applies the procedure defined in the previous
607section, moving image 1 into slot 0 and image 0 into slot 1. If the boot
608status file indicates that an image part is present in the scratch area, this
609part is copied into the correct location by starting at step e or step h in the
610area-swap procedure, depending on whether the part belongs to image 0 or image
6111.
612
613After the swap operation has been completed, the boot loader proceeds as though
614it had just been started.
615
616*** INTEGRITY CHECK
617
618An image is checked for integrity immediately before it gets copied into the
Fabio Utzig5bd4e582017-07-20 08:55:38 -0300619primary slot. If the boot loader doesn't perform an image swap, then it can
620perform an optional integrity check of the image in slot0 if
621MCUBOOT_VALIDATE_SLOT0 is set, otherwise it doesn't perform an integrity check.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800622
623During the integrity check, the boot loader verifies the following aspects of
624an image:
625 * 32-bit magic number must be correct (0x96f3b83c).
626 * Image must contain a SHA256 TLV.
Fabio Utzig86fe4b22017-07-28 18:56:29 -0300627 * Calculated SHA256 must match SHA256 TLV contents.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800628 * Image *may* contain a signature TLV. If it does, its contents must be
629 verifiable using a key embedded in the boot loader.
630
631*** SECURITY
632
633As indicated above, the final step of the integrity check is signature
634verification. The boot loader can have one or more public keys embedded in it
635at build time. During signature verification, the boot loader verifies that an
636image was signed with a private key that corresponds to one of its public keys.
637The image signature TLV indicates the index of the key that is has been signed
638with. The boot loader uses this index to identify the corresponding public
639key.
640
641For information on embedding public keys in the boot loader, as well as
Fabio Utzig5bd4e582017-07-20 08:55:38 -0300642producing signed images, see: doc/signed_images.md