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