blob: 47c4d2828cf424f8901a0a8b6a9505d751082e3e [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
24The Mynewt bootloader comprises two packages:
25
26 * The bootutil library (boot/bootutil)
27 * The boot application (apps/boot)
28
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. */
87
88/*
89 * Image trailer TLV types.
90 */
91#define IMAGE_TLV_SHA256 1 /* SHA256 of image hdr and body */
92#define IMAGE_TLV_RSA2048 2 /* RSA2048 of hash output */
93#define IMAGE_TLV_ECDSA224 3 /* ECDSA of hash output */
94
95Optional type-length-value records (TLVs) containing image metadata are placed
96after the end of the image.
97
98The ih_hdr_size field indicates the length of the header, and therefore the
99offset of the image itself. This field provides for backwards compatibility in
100case of changes to the format of the image header.
101
102*** FLASH MAP
103
104A Mynewt device's flash is partitioned according to its _flash map_. At a high
105level, the flash map maps numeric IDs to _flash areas_. A flash area is a
106region of disk with the following properties:
107 (1) An area can be fully erased without affecting any other areas.
108 (2) A write to one area does not restrict writes to other areas.
109
110The boot loader uses the following flash areas:
111
112#define FLASH_AREA_BOOTLOADER 0
113#define FLASH_AREA_IMAGE_0 1
114#define FLASH_AREA_IMAGE_1 2
115#define FLASH_AREA_IMAGE_SCRATCH 3
116
117*** IMAGE SLOTS
118
119A portion of the flash memory is partitioned into two image slots: a primary
120slot (0) and a secondary slot (1). The boot loader will only run an image from
121the primary slot, so images must be built such that they can run from that
122fixed location in flash. If the boot loader needs to run the image resident in
123the secondary slot, it must swap the two images in flash prior to booting.
124
125In addition to the two image slots, the boot loader requires a scratch area to
126allow for reliable image swapping.
127
128*** BOOT VECTOR
129
130At startup, the boot loader determines which of the above three states the
131device is in by inspecting the boot vector. The boot vector consists of two
132records (called "image trailers"), one written at the end of each image slot.
133An image trailer has the following structure:
134
135 0 1 2 3
136 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
137 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
138 ~ MAGIC (16 octets) ~
139 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
140 ~ ~
141 ~ Swap status (128 * min-write-size * 3) ~
142 ~ ~
143 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
144 | Copy done | 0xff padding (up to min-write-sz - 1) ~
145 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
146 | Image OK | 0xff padding (up to min-write-sz - 1) ~
147 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
148
149These records are at the end of each image slot. The offset immediately
150following such a record represents the start of the next flash area.
151
152Note: "min-write-size" is a property of the flash hardware. If the hardware
153allows individual bytes to be written at arbitrary addresses, then
154min-write-size is 1. If the hardware only allows writes at even addresses,
155then min-write-size is 2, and so on.
156
157The fields are defined as follows:
158
1591. MAGIC: The following 16 bytes, written in host-byte-order:
160
161 const uint32_t boot_img_magic[4] = {
162 0xf395c277,
163 0x7fefd260,
164 0x0f505235,
165 0x8079b62c,
166 };
167
1682. Swap status: A series of single-byte records. Each record corresponds to a
169flash sector in an image slot. A swap status byte indicate the location of
170the corresponding sector data. During an image swap, image data is moved one
171sector at a time. The swap status is necessary for resuming a swap operation
172if the device rebooted before a swap operation completed.
173
1743. Copy done: A single byte indicating whether the image in this slot is
175complete (0x01=done; 0xff=not done).
176
1774. Image OK: A single byte indicating whether the image in this slot has been
178confirmed as good by the user (0x01=confirmed; 0xff=not confirmed).
179
180The boot vector records are structured around the limitations imposed by flash
181hardware. As a consequence, they do not have a very intuitive design, and it
182is difficult to get a sense of the state of the device just by looking at the
183boot vector. It is better to map all the possible vector states to the three
184states described above via a set of tables. These tables are reproduced below.
185In these tables, the "pending" and "confirmed" flags are shown for illustrative
186purposes; they are not actually present in the boot vector.
187
188
189 State I
190 | slot-0 | slot-1 |
191 -----------------+--------+--------|
192 magic | Unset | Unset |
193 image-ok | Any | N/A |
194 -----------------+--------+--------'
195 pending | | |
196 confirmed | X | |
197 -----------------+--------+--------'
198 swap: none |
199 -----------------------------------'
200
201
202 State II
203 | slot-0 | slot-1 |
204 -----------------+--------+--------|
205 magic | Any | Good |
206 image-ok | Any | N/A |
207 -----------------+--------+--------'
208 pending | | X |
209 confirmed | X | |
210 -----------------+--------+--------'
211 swap: test |
212 -----------------------------------'
213
214
215 State III
216 | slot-0 | slot-1 |
217 -----------------+--------+--------|
218 magic | Good | Unset |
219 image-ok | 0xff | N/A |
220 -----------------+--------+--------'
221 pending | | |
222 confirmed | | X |
223 -----------------+--------+--------'
224 swap: revert (test image running) |
225 -----------------------------------'
226
227
228 State IV
229 | slot-0 | slot-1 |
230 -----------------+--------+--------|
231 magic | Good | Unset |
232 image-ok | 0x01 | N/A |
233 -----------------+--------+--------'
234 pending | | |
235 confirmed | X | |
236 -----------------+--------+--------'
237 swap: none (confirmed test image) |
238 -----------------------------------'
239
240*** HIGH-LEVEL OPERATION
241
242With the terms defined, we can now explore the boot loader's operation. First,
243a high-level overview of the boot process is presented. Then, the following
244sections describe each step of the process in more detail.
245
246Procedure:
247
248A. Inspect swap status region; is an interrupted swap is being resumed?
249 Yes: Complete the partial swap operation; skip to step C.
250 No: Proceed to step B.
251
252B. Insect boot vector; is a swap requested?
253 Yes.
254 1. Is the requested image valid (integrity and security check)?
255 Yes.
256 a. Perform swap operation.
257 b. Persist completion of swap procedure to boot vector.
258 c. Proceed to step C.
259 No.
260 a. Erase invalid image.
261 b. Persist failure of swap procedure to boot vector.
262 c. Proceed to step C.
263 No: Proceed to step C.
264
265C. Boot into image in slot 0.
266
267*** BOOT STATES
268
269Logically, you can think of a pair of flags associated with each image slot:
270pending and confirmed. On startup, the boot loader determines the state of the
271device by inspecting each pair of flags. These flags have the following
272meanings:
273
274* pending: image gets tested on next reboot; absent subsequent confirm command,
275 revert to original image on second reboot.
276* confirmed: always use image unless excluded by a test image.
277
278In English, when the user wants to run the secondary image, they set the
279pending flag for the second slot and reboot the device. On startup, the boot
280loader will swap the two images in flash, clear the secondary slot's pending
281flag, and run the newly-copied image in slot 0. This is a temporary state; if
282the device reboots again, the boot loader swaps the images back to their
283original slots and boots into the original image. If the user doesn't want to
284revert to the original state, they can make the current state permanent by
285setting the confirmed flag in slot 0.
286
287Switching to an alternate image is a two-step process (set + confirm) to
288prevent a device from becoming "bricked" by bad firmware. If the device
289crashes immediately upon booting the second image, the boot loader reverts to
290the working image, rather than repeatedly rebooting into the bad image.
291
292The following set of tables illustrate the three possible states that the
293device can be in:
294
295 | slot-0 | slot-1 |
296 ---------------+--------+--------|
297 pending | | |
298 confirmed | X | |
299 ---------------+--------+--------'
300 Image 0 confirmed; |
301 No change on reboot |
302 ---------------------------------'
303
304 | slot-0 | slot-1 |
305 ---------------+--------+--------|
306 pending | | X |
307 confirmed | X | |
308 ---------------+--------+--------'
309 Image 0 confirmed; |
310 Test image 1 on next reboot |
311 ---------------------------------'
312
313 | slot-0 | slot-1 |
314 ---------------+--------+--------|
315 pending | | |
316 confirmed | | X |
317 ---------------+--------+--------'
318 Testing image 0; |
319 Revert to image 1 on next reboot |
320 ---------------------------------'
321
322
323
324*** IMAGE SWAPPING
325
326The boot loader swaps the contents of the two image slots for two reasons:
327 * User has issued an "image test" operation; the image in slot-1 should be
328 run once (state II).
329 * Test image rebooted without being confirmed; the boot loader should
330 revert to the original image currently in slot-1 (state III).
331
332If the boot vector indicates that the image in the secondary slot should be
333run, the boot loader needs to copy it to the primary slot. The image currently
334in the primary slot also needs to be retained in flash so that it can be used
335later. Furthermore, both images need to be recoverable if the boot loader
336resets in the middle of the swap operation. The two images are swapped
337according to the following procedure:
338
339 1. Determine how many flash sectors each image slot consists of. This
340 number must be the same for both slots.
341 2. Iterate the list of sector indices in descending order (i.e., starting
342 with the greatest index); current element = "index".
343 b. Erase scratch area.
344 c. Copy slot0[index] to scratch area.
345 d. Write updated swap status (i).
346
347 e. Erase slot1[index]
348 f. Copy slot0[index] to slot1[index]
349 - If these are the last sectors (i.e., first swap being perfomed),
350 copy the full sector *except* the image trailer.
351 - Else, copy entire sector contents.
352 g. Write updated swap status (ii).
353
354 h. Erase slot0[index].
355 i. Copy scratch area slot0[index].
356 j. Write updated swap status (iii).
357
358 3. Persist completion of swap procedure to slot 0 image trailer.
359
360The additional caveats in step 2f are necessary so that the slot 1 image trailer
361can be written by the user at a later time. With the image trailer unwritten,
362the user can test the image in slot 1 (i.e., transition to state II).
363
364The particulars of step 3 vary depending on whether an image is being tested or
365reverted:
366 * test:
367 o Write slot0.copy_done = 1
368 (should now be in state III)
369
370 * revert:
371 o Write slot0.magic = BOOT_MAGIC
372 o Write slot0.copy_done = 1
373 o Write slot0.image_ok = 1
374 (should now be in state IV)
375
376*** SWAP STATUS
377
378The swap status region allows the boot loader to recover in case it restarts in
379the middle of an image swap operation. The swap status region consists of a
380series of single-byte records. These records are written independently, and
381therefore must be padded according to the minimum write size imposed by the
382flash hardware. In the below figure, a min-write-size of 1 is assumed for
383simplicity. The structure of the swap status region is illustrated below. In
384this figure, a min-write-size of 1 is assumed for simplicity.
385
386 0 1 2 3
387 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
388 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
389 |sec127,state 0 |sec127,state 1 |sec127,state 2 |sec126,state 0 |
390 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
391 |sec126,state 1 |sec126,state 2 |sec125,state 0 |sec125,state 1 |
392 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
393 |sec125,state 2 | |
394 +-+-+-+-+-+-+-+-+ +
395 ~ ~
396 ~ [Records for indices 124 through 1 ~
397 ~ ~
398 ~ +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
399 ~ |sec000,state 0 |sec000,state 1 |sec000,state 2 |
400 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
401
402The above is probably not helpful at all; here is a description in English.
403
404Each image slot is partitioned into a sequence of flash sectors. If we were to
405enumerate the sectors in a single slot, starting at 0, we would have a list of
406sector indices. Since there are two image slots, each sector index would
407correspond to a pair of sectors. For example, sector index 0 corresponds to
408the first sector in slot 0 and the first sector in slot 1. Furthermore, we
409impose a limit of 128 indices. If an image slot consists of more than 128
410sectors, the flash layout is not compatible with this boot loader. Finally,
411reverse the list of indices such that the list starts with index 127 and ends
412with 0. The swap status region is a representation of this reversed list.
413
414During a swap operation, each sector index transitions through four separate
415states:
416 0. slot 0: image 0, slot 1: image 1, scratch: N/A
417 1. slot 0: image 0, slot 1: N/A, scratch: image 1 (1->s, erase 1)
418 2. slot 0: N/A, slot 1: image 0, scratch: image 1 (0->1, erase 0)
419 3. slot 0: image 1, slot 1: image 0, scratch: N/A (s->0)
420
421Each time a sector index transitions to a new state, the boot loader writes a
422record to the swap status region. Logically, the boot loader only needs one
423record per sector index to keep track of the current swap state. However, due
424to limitations imposed by flash hardware, a record cannot be overwritten when
425an index's state changes. To solve this problem, the boot loader uses three
426records per sector index rather than just one.
427
428Each sector-state pair is represented as a set of three records. The record
429values map to the above four states as follows
430
431 | rec0 | rec1 | rec2
432 --------+------+------+------
433 state 0 | 0xff | 0xff | 0xff
434 state 1 | 0x01 | 0xff | 0xff
435 state 2 | 0x01 | 0x02 | 0xff
436 state 3 | 0x01 | 0x02 | 0x03
437
438The swap status region can accommodate 128 sector indices. Hence, the size of
439the region, in bytes, is 128 * min-write-size * 3. The number 128 is chosen
440somewhat arbitrarily and will likely be made configurable. The only
441requirement for the index count is that is is great enough to account for a
442maximum-sized image (i.e., at least as great as the total sector count in an
443image slot). If a device's image slots use less than 128 sectors, the first
444record that gets written will be somewhere in the middle of the region. For
445example, if a slot uses 64 sectors, the first sector index that gets swapped is
44663, which corresponds to the exact halfway point within the region.
447
448
449*** RESET RECOVERY
450
451If the boot loader resets in the middle of a swap operation, the two images may
452be discontiguous in flash. Bootutil recovers from this condition by using the
453boot vector to determine how the image parts are distributed in flash.
454
455The first step is determine where the relevant swap status region is located.
456Because this region is embedded within the image slots, its location in flash
457changes during a swap operation. The below set of tables map boot vector
458contents to swap status location. In these tables, the "source" field
459indicates where the swap status region is located.
460
461 | slot-0 | scratch |
462 ----------+------------+------------|
463 magic | Good | Any |
464 copy-done | 0x01 | N/A |
465 ----------+------------+------------'
466 source: none |
467 ------------------------------------'
468
469 | slot-0 | scratch |
470 ----------+------------+------------|
471 magic | Good | Any |
472 copy-done | 0xff | N/A |
473 ----------+------------+------------'
474 source: slot 0 |
475 ------------------------------------'
476
477 | slot-0 | scratch |
478 ----------+------------+------------|
479 magic | Any | Good |
480 copy-done | Any | N/A |
481 ----------+------------+------------'
482 source: scratch |
483 ------------------------------------'
484
485 | slot-0 | scratch |
486 ----------+------------+------------|
487 magic | Unset | Any |
488 copy-done | 0xff | N/A |
489 ----------+------------+------------|
490 source: varies |
491 ------------------------------------+------------------------------+
492 This represents one of two cases: |
493 o No swaps ever (no status to read, so no harm in checking). |
494 o Mid-revert; status in slot 0. |
495 -------------------------------------------------------------------'
496
497
498If the swap status region indicates that the images are not contiguous,
499bootutil completes the swap operation that was in progress when the system was
500reset. In other words, it applies the procedure defined in the previous
501section, moving image 1 into slot 0 and image 0 into slot 1. If the boot
502status file indicates that an image part is present in the scratch area, this
503part is copied into the correct location by starting at step e or step h in the
504area-swap procedure, depending on whether the part belongs to image 0 or image
5051.
506
507After the swap operation has been completed, the boot loader proceeds as though
508it had just been started.
509
510*** INTEGRITY CHECK
511
512An image is checked for integrity immediately before it gets copied into the
513primary slot. If the boot loader doesn't perform an image swap, then it
514doesn't perform an integrity check.
515
516During the integrity check, the boot loader verifies the following aspects of
517an image:
518 * 32-bit magic number must be correct (0x96f3b83c).
519 * Image must contain a SHA256 TLV.
520 * Calculated SHA256 must matche SHA256 TLV contents.
521 * Image *may* contain a signature TLV. If it does, its contents must be
522 verifiable using a key embedded in the boot loader.
523
524*** SECURITY
525
526As indicated above, the final step of the integrity check is signature
527verification. The boot loader can have one or more public keys embedded in it
528at build time. During signature verification, the boot loader verifies that an
529image was signed with a private key that corresponds to one of its public keys.
530The image signature TLV indicates the index of the key that is has been signed
531with. The boot loader uses this index to identify the corresponding public
532key.
533
534For information on embedding public keys in the boot loader, as well as
535producing signed images, see: boot/bootutil/signed_images.md