blob: e0b6067ed32130fc3c3dc26a87fab4a261724c54 [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
Fabio Utzig86fe4b22017-07-28 18:56:29 -0300203*** IMAGE TRAILER
Christopher Collins92ea77f2016-12-12 15:59:26 -0800204
Fabio Utzig86fe4b22017-07-28 18:56:29 -0300205For the bootloader to be able to determine the current state and what actions
206should be taken during the current boot operation, it uses metadata existing
207on both slots, and while doing a swap operation it can be located in scratch.
208This metadata is located at the end of the slot and is called image trailer.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800209An image trailer has the following structure:
210
211 0 1 2 3
212 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
213 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Christopher Collins92ea77f2016-12-12 15:59:26 -0800214 ~ ~
215 ~ Swap status (128 * min-write-size * 3) ~
216 ~ ~
217 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Fabio Utzig2c305aa2017-07-20 13:14:25 -0300218 | Copy done | 0xff padding (7 octets) |
Christopher Collins92ea77f2016-12-12 15:59:26 -0800219 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Fabio Utzig2c305aa2017-07-20 13:14:25 -0300220 | Image OK | 0xff padding (7 octets) |
Fabio Utzig5bd4e582017-07-20 08:55:38 -0300221 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
222 | MAGIC (16 octets) |
Christopher Collins92ea77f2016-12-12 15:59:26 -0800223 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
224
225These records are at the end of each image slot. The offset immediately
226following such a record represents the start of the next flash area.
227
228Note: "min-write-size" is a property of the flash hardware. If the hardware
229allows individual bytes to be written at arbitrary addresses, then
230min-write-size is 1. If the hardware only allows writes at even addresses,
231then min-write-size is 2, and so on.
232
233The fields are defined as follows:
234
Fabio Utzig5bd4e582017-07-20 08:55:38 -03002351. Swap status: A series of single-byte records. Each record corresponds to a
236flash sector in an image slot. A swap status byte indicate the location of
237the corresponding sector data. During an image swap, image data is moved one
238sector at a time. The swap status is necessary for resuming a swap operation
239if the device rebooted before a swap operation completed.
240
2412. Copy done: A single byte indicating whether the image in this slot is
242complete (0x01=done; 0xff=not done).
243
2443. Image OK: A single byte indicating whether the image in this slot has been
245confirmed as good by the user (0x01=confirmed; 0xff=not confirmed).
246
2474. MAGIC: The following 16 bytes, written in host-byte-order:
Christopher Collins92ea77f2016-12-12 15:59:26 -0800248
249 const uint32_t boot_img_magic[4] = {
250 0xf395c277,
251 0x7fefd260,
252 0x0f505235,
253 0x8079b62c,
254 };
255
Fabio Utzig86fe4b22017-07-28 18:56:29 -0300256*** IMAGE TRAILERS
257
258At startup, the boot loader determines which of the above four states the
259device is in by inspecting the image trailers. When using the term "image
260trailers" what is meant is the aggregate information provided by both image
261slot's trailers.
262
263The image trailers records are structured around the limitations imposed by flash
Christopher Collins92ea77f2016-12-12 15:59:26 -0800264hardware. As a consequence, they do not have a very intuitive design, and it
265is difficult to get a sense of the state of the device just by looking at the
Fabio Utzig86fe4b22017-07-28 18:56:29 -0300266image trailers. It is better to map all the possible trailer states to the four
Christopher Collins92ea77f2016-12-12 15:59:26 -0800267states described above via a set of tables. These tables are reproduced below.
268In these tables, the "pending" and "confirmed" flags are shown for illustrative
Fabio Utzig86fe4b22017-07-28 18:56:29 -0300269purposes; they are not actually present in the image trailers.
270
271Note: An important caveat about the tables described below is that they must
272be evaluated in the order presented here. Lower state numbers must have a
273higher priority when testing the image trailers.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800274
275
276 State I
277 | slot-0 | slot-1 |
278 -----------------+--------+--------|
Christopher Collins92ea77f2016-12-12 15:59:26 -0800279 magic | Any | Good |
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -0800280 image-ok | Any | Unset |
Fabio Utzigf9d44282017-07-20 15:05:13 -0300281 copy-done | Any | Any |
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
Fabio Utzig86fe4b22017-07-28 18:56:29 -0300290 State II
Christopher Collins92ea77f2016-12-12 15:59:26 -0800291 | slot-0 | slot-1 |
292 -----------------+--------+--------|
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -0800293 magic | Any | Good |
294 image-ok | Any | 0x01 |
Fabio Utzigf9d44282017-07-20 15:05:13 -0300295 copy-done | Any | Any |
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -0800296 -----------------+--------+--------'
297 pending | | P |
298 confirmed | X | |
299 -----------------+--------+--------'
300 swap: permanent |
301 -----------------------------------'
Fabio Utzig5bd4e582017-07-20 08:55:38 -0300302
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -0800303
Fabio Utzig86fe4b22017-07-28 18:56:29 -0300304 State III
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -0800305 | slot-0 | slot-1 |
306 -----------------+--------+--------|
Christopher Collins92ea77f2016-12-12 15:59:26 -0800307 magic | Good | Unset |
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -0800308 image-ok | 0xff | Any |
Fabio Utzig5bd4e582017-07-20 08:55:38 -0300309 copy-done | 0x01 | Any |
Christopher Collins92ea77f2016-12-12 15:59:26 -0800310 -----------------+--------+--------'
311 pending | | |
312 confirmed | | X |
313 -----------------+--------+--------'
314 swap: revert (test image running) |
315 -----------------------------------'
316
317
Fabio Utzig86fe4b22017-07-28 18:56:29 -0300318Those three states described above are explicity tested for and result in a
319swap decision by the bootloader. If the existing values in the the image trailers
320are not one of those previously described the bootloader will assume no swap
321operation is to be performed which is illustrated by state IV.
322
323
324 State IV
Christopher Collins92ea77f2016-12-12 15:59:26 -0800325 | slot-0 | slot-1 |
326 -----------------+--------+--------|
Fabio Utzig86fe4b22017-07-28 18:56:29 -0300327 magic | Any | Any |
328 image-ok | Any | Any |
Fabio Utzigf9d44282017-07-20 15:05:13 -0300329 copy-done | Any | Any |
Christopher Collins92ea77f2016-12-12 15:59:26 -0800330 -----------------+--------+--------'
331 pending | | |
332 confirmed | X | |
333 -----------------+--------+--------'
Fabio Utzig86fe4b22017-07-28 18:56:29 -0300334 swap: none |
Christopher Collins92ea77f2016-12-12 15:59:26 -0800335 -----------------------------------'
336
Fabio Utzig86fe4b22017-07-28 18:56:29 -0300337
338Note: An important caveat to be mentioned here is, that due to the physical
339limitations of the storage and another possible failure, two extra "states"
340were added specifically to signal a failure in the boot process.
341
3421. One of them is a result of any device read/write error, called panic and
343 will result in the bootloader hanging without booting the image in slot 0.
344
3452. Another state was added specifically to handle the situation in which a swap
346 was requested and the image in slot 1 fails to validate, due to hashing
347 or signing error. This state behaves as state IV with the extra action of
348 marking the image in slot 0 as confirmed.
349
350
Christopher Collins92ea77f2016-12-12 15:59:26 -0800351*** HIGH-LEVEL OPERATION
352
353With the terms defined, we can now explore the boot loader's operation. First,
354a high-level overview of the boot process is presented. Then, the following
355sections describe each step of the process in more detail.
356
357Procedure:
358
Fabio Utzig86fe4b22017-07-28 18:56:29 -0300359A. Inspect swap status region; is an interrupted swap being resumed?
Christopher Collins92ea77f2016-12-12 15:59:26 -0800360 Yes: Complete the partial swap operation; skip to step C.
361 No: Proceed to step B.
362
Fabio Utzig86fe4b22017-07-28 18:56:29 -0300363B. Inspect image trailers; is a swap requested?
Christopher Collins92ea77f2016-12-12 15:59:26 -0800364 Yes.
365 1. Is the requested image valid (integrity and security check)?
366 Yes.
367 a. Perform swap operation.
Fabio Utzig86fe4b22017-07-28 18:56:29 -0300368 b. Persist completion of swap procedure to image trailers.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800369 c. Proceed to step C.
370 No.
371 a. Erase invalid image.
Fabio Utzig86fe4b22017-07-28 18:56:29 -0300372 b. Persist failure of swap procedure to image trailers.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800373 c. Proceed to step C.
374 No: Proceed to step C.
375
376C. Boot into image in slot 0.
377
Christopher Collins92ea77f2016-12-12 15:59:26 -0800378*** IMAGE SWAPPING
379
380The boot loader swaps the contents of the two image slots for two reasons:
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -0800381 * User has issued a "set pending" operation; the image in slot-1 should be
382 run once (state II) or repeatedly (state III), depending on whether a
383 permanent swap was specified.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800384 * Test image rebooted without being confirmed; the boot loader should
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -0800385 revert to the original image currently in slot-1 (state IV).
Christopher Collins92ea77f2016-12-12 15:59:26 -0800386
Fabio Utzig86fe4b22017-07-28 18:56:29 -0300387If the image trailers indicates that the image in the secondary slot should be
Christopher Collins92ea77f2016-12-12 15:59:26 -0800388run, the boot loader needs to copy it to the primary slot. The image currently
389in the primary slot also needs to be retained in flash so that it can be used
390later. Furthermore, both images need to be recoverable if the boot loader
391resets in the middle of the swap operation. The two images are swapped
392according to the following procedure:
393
394 1. Determine how many flash sectors each image slot consists of. This
395 number must be the same for both slots.
396 2. Iterate the list of sector indices in descending order (i.e., starting
397 with the greatest index); current element = "index".
398 b. Erase scratch area.
399 c. Copy slot0[index] to scratch area.
Fabio Utzig5bd4e582017-07-20 08:55:38 -0300400 - If these are the last sectors (i.e., first swap being perfomed),
401 copy the full sector *except* the image trailer.
402 - Else, copy entire sector contents.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800403 d. Write updated swap status (i).
404
405 e. Erase slot1[index]
406 f. Copy slot0[index] to slot1[index]
407 - If these are the last sectors (i.e., first swap being perfomed),
408 copy the full sector *except* the image trailer.
409 - Else, copy entire sector contents.
410 g. Write updated swap status (ii).
411
412 h. Erase slot0[index].
413 i. Copy scratch area slot0[index].
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 j. Write updated swap status (iii).
418
419 3. Persist completion of swap procedure to slot 0 image trailer.
420
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -0800421The additional caveats in step 2f are necessary so that the slot 1 image
422trailer can be written by the user at a later time. With the image trailer
423unwritten, the user can test the image in slot 1 (i.e., transition to state
424II).
Christopher Collins92ea77f2016-12-12 15:59:26 -0800425
Fabio Utzig5bd4e582017-07-20 08:55:38 -0300426Note1: If the sector being copied is the last sector, then swap status is
427temporarily maintained on scratch for the duration of this operation, always
428using slot0's area otherwise.
429
430Note2: The bootloader tries to copy only used sectors (based on largest image
431installed on any of the slots), minimizing the amount of sectors copied and
432reducing the amount of time required for a swap operation.
433
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -0800434The particulars of step 3 vary depending on whether an image is being tested,
Fabio Utzig86fe4b22017-07-28 18:56:29 -0300435permanently used, reverted or a validation failure of slot 1 happened when a
436swap was requested:
437
Christopher Collins92ea77f2016-12-12 15:59:26 -0800438 * test:
439 o Write slot0.copy_done = 1
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -0800440 (swap caused the following values to be written:
441 slot0.magic = BOOT_MAGIC
442 slot0.image_ok = Unset)
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -0800443
444 * permanent:
445 o Write slot0.copy_done = 1
446 (swap caused the following values to be written:
447 slot0.magic = BOOT_MAGIC
448 slot0.image_ok = 0x01)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800449
450 * revert:
Christopher Collins92ea77f2016-12-12 15:59:26 -0800451 o Write slot0.copy_done = 1
452 o Write slot0.image_ok = 1
Fabio Utzig86fe4b22017-07-28 18:56:29 -0300453 (swap caused the following values to be written:
454 slot0.magic = BOOT_MAGIC)
455
456 * failure to validate slot 1:
457 o Write slot0.image_ok = 1
458
459After completing the operations as described above the image in slot 0 should
460be booted.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800461
462*** SWAP STATUS
463
464The swap status region allows the boot loader to recover in case it restarts in
465the middle of an image swap operation. The swap status region consists of a
466series of single-byte records. These records are written independently, and
467therefore must be padded according to the minimum write size imposed by the
468flash hardware. In the below figure, a min-write-size of 1 is assumed for
469simplicity. The structure of the swap status region is illustrated below. In
470this figure, a min-write-size of 1 is assumed for simplicity.
471
472 0 1 2 3
473 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
474 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
475 |sec127,state 0 |sec127,state 1 |sec127,state 2 |sec126,state 0 |
476 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
477 |sec126,state 1 |sec126,state 2 |sec125,state 0 |sec125,state 1 |
478 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
479 |sec125,state 2 | |
480 +-+-+-+-+-+-+-+-+ +
481 ~ ~
482 ~ [Records for indices 124 through 1 ~
483 ~ ~
484 ~ +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
485 ~ |sec000,state 0 |sec000,state 1 |sec000,state 2 |
486 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
487
488The above is probably not helpful at all; here is a description in English.
489
490Each image slot is partitioned into a sequence of flash sectors. If we were to
491enumerate the sectors in a single slot, starting at 0, we would have a list of
492sector indices. Since there are two image slots, each sector index would
493correspond to a pair of sectors. For example, sector index 0 corresponds to
494the first sector in slot 0 and the first sector in slot 1. Furthermore, we
495impose a limit of 128 indices. If an image slot consists of more than 128
496sectors, the flash layout is not compatible with this boot loader. Finally,
497reverse the list of indices such that the list starts with index 127 and ends
498with 0. The swap status region is a representation of this reversed list.
499
500During a swap operation, each sector index transitions through four separate
501states:
502 0. slot 0: image 0, slot 1: image 1, scratch: N/A
503 1. slot 0: image 0, slot 1: N/A, scratch: image 1 (1->s, erase 1)
504 2. slot 0: N/A, slot 1: image 0, scratch: image 1 (0->1, erase 0)
505 3. slot 0: image 1, slot 1: image 0, scratch: N/A (s->0)
506
507Each time a sector index transitions to a new state, the boot loader writes a
508record to the swap status region. Logically, the boot loader only needs one
509record per sector index to keep track of the current swap state. However, due
510to limitations imposed by flash hardware, a record cannot be overwritten when
511an index's state changes. To solve this problem, the boot loader uses three
512records per sector index rather than just one.
513
514Each sector-state pair is represented as a set of three records. The record
515values map to the above four states as follows
516
517 | rec0 | rec1 | rec2
518 --------+------+------+------
519 state 0 | 0xff | 0xff | 0xff
520 state 1 | 0x01 | 0xff | 0xff
521 state 2 | 0x01 | 0x02 | 0xff
522 state 3 | 0x01 | 0x02 | 0x03
523
524The swap status region can accommodate 128 sector indices. Hence, the size of
525the region, in bytes, is 128 * min-write-size * 3. The number 128 is chosen
526somewhat arbitrarily and will likely be made configurable. The only
527requirement for the index count is that is is great enough to account for a
528maximum-sized image (i.e., at least as great as the total sector count in an
529image slot). If a device's image slots use less than 128 sectors, the first
530record that gets written will be somewhere in the middle of the region. For
531example, if a slot uses 64 sectors, the first sector index that gets swapped is
53263, which corresponds to the exact halfway point within the region.
533
Fabio Utzig5bd4e582017-07-20 08:55:38 -0300534Note: since the scratch area only ever needs to record swapping of the last
535sector, it uses at most min-write-size * 3 bytes for its own status area.
536
Christopher Collins92ea77f2016-12-12 15:59:26 -0800537*** RESET RECOVERY
538
539If the boot loader resets in the middle of a swap operation, the two images may
540be discontiguous in flash. Bootutil recovers from this condition by using the
Fabio Utzig86fe4b22017-07-28 18:56:29 -0300541image trailers to determine how the image parts are distributed in flash.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800542
543The first step is determine where the relevant swap status region is located.
544Because this region is embedded within the image slots, its location in flash
Fabio Utzig86fe4b22017-07-28 18:56:29 -0300545changes during a swap operation. The below set of tables map image trailers
Christopher Collins92ea77f2016-12-12 15:59:26 -0800546contents to swap status location. In these tables, the "source" field
547indicates where the swap status region is located.
548
549 | slot-0 | scratch |
550 ----------+------------+------------|
551 magic | Good | Any |
552 copy-done | 0x01 | N/A |
553 ----------+------------+------------'
554 source: none |
555 ------------------------------------'
556
557 | slot-0 | scratch |
558 ----------+------------+------------|
559 magic | Good | Any |
560 copy-done | 0xff | N/A |
561 ----------+------------+------------'
562 source: slot 0 |
563 ------------------------------------'
564
565 | slot-0 | scratch |
566 ----------+------------+------------|
567 magic | Any | Good |
568 copy-done | Any | N/A |
569 ----------+------------+------------'
570 source: scratch |
571 ------------------------------------'
572
573 | slot-0 | scratch |
574 ----------+------------+------------|
575 magic | Unset | Any |
576 copy-done | 0xff | N/A |
577 ----------+------------+------------|
Fabio Utzig86fe4b22017-07-28 18:56:29 -0300578 source: slot 0 |
Christopher Collins92ea77f2016-12-12 15:59:26 -0800579 ------------------------------------+------------------------------+
580 This represents one of two cases: |
581 o No swaps ever (no status to read, so no harm in checking). |
582 o Mid-revert; status in slot 0. |
Fabio Utzig86fe4b22017-07-28 18:56:29 -0300583 For this reason we assume slot 0 as source, to trigger a check |
584 of the status area and find out if there was swapping under way. |
Christopher Collins92ea77f2016-12-12 15:59:26 -0800585 -------------------------------------------------------------------'
586
587
588If the swap status region indicates that the images are not contiguous,
589bootutil completes the swap operation that was in progress when the system was
590reset. In other words, it applies the procedure defined in the previous
591section, moving image 1 into slot 0 and image 0 into slot 1. If the boot
592status file indicates that an image part is present in the scratch area, this
593part is copied into the correct location by starting at step e or step h in the
594area-swap procedure, depending on whether the part belongs to image 0 or image
5951.
596
597After the swap operation has been completed, the boot loader proceeds as though
598it had just been started.
599
600*** INTEGRITY CHECK
601
602An image is checked for integrity immediately before it gets copied into the
Fabio Utzig5bd4e582017-07-20 08:55:38 -0300603primary slot. If the boot loader doesn't perform an image swap, then it can
604perform an optional integrity check of the image in slot0 if
605MCUBOOT_VALIDATE_SLOT0 is set, otherwise it doesn't perform an integrity check.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800606
607During the integrity check, the boot loader verifies the following aspects of
608an image:
609 * 32-bit magic number must be correct (0x96f3b83c).
610 * Image must contain a SHA256 TLV.
Fabio Utzig86fe4b22017-07-28 18:56:29 -0300611 * Calculated SHA256 must match SHA256 TLV contents.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800612 * Image *may* contain a signature TLV. If it does, its contents must be
613 verifiable using a key embedded in the boot loader.
614
615*** SECURITY
616
617As indicated above, the final step of the integrity check is signature
618verification. The boot loader can have one or more public keys embedded in it
619at build time. During signature verification, the boot loader verifies that an
620image was signed with a private key that corresponds to one of its public keys.
621The image signature TLV indicates the index of the key that is has been signed
622with. The boot loader uses this index to identify the corresponding public
623key.
624
625For information on embedding public keys in the boot loader, as well as
Fabio Utzig5bd4e582017-07-20 08:55:38 -0300626producing signed images, see: doc/signed_images.md