2017-09-13 01:53:20 +08:00
|
|
|
<!--
|
|
|
|
Licensed to the Apache Software Foundation (ASF) under one
|
|
|
|
or more contributor license agreements. See the NOTICE file
|
|
|
|
distributed with this work for additional information
|
|
|
|
regarding copyright ownership. The ASF licenses this file
|
|
|
|
to you under the Apache License, Version 2.0 (the
|
|
|
|
"License"); you may not use this file except in compliance
|
|
|
|
with the License. You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing,
|
|
|
|
software distributed under the License is distributed on an
|
|
|
|
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
|
|
KIND, either express or implied. See the License for the
|
|
|
|
specific language governing permissions and limitations
|
|
|
|
under the License.
|
|
|
|
-->
|
|
|
|
|
|
|
|
# Boot Loader
|
|
|
|
|
|
|
|
## Summary
|
2016-12-13 07:59:26 +08:00
|
|
|
|
2017-07-21 00:20:48 +08:00
|
|
|
mcuboot comprises two packages:
|
2016-12-13 07:59:26 +08:00
|
|
|
|
2017-09-13 01:53:20 +08:00
|
|
|
* The bootutil library (boot/bootutil)
|
|
|
|
* The boot application (each port has its own at boot/<port>)
|
2016-12-13 07:59:26 +08:00
|
|
|
|
|
|
|
The bootutil library performs most of the functions of a boot loader. In
|
|
|
|
particular, the piece that is missing is the final step of actually jumping to
|
|
|
|
the main image. This last step is instead implemented by the boot application.
|
|
|
|
Boot loader functionality is separated in this manner to enable unit testing of
|
|
|
|
the boot loader. A library can be unit tested, but an application can't.
|
|
|
|
Therefore, functionality is delegated to the bootutil library when possible.
|
|
|
|
|
2017-09-13 01:53:20 +08:00
|
|
|
## Limitations
|
2016-12-13 07:59:26 +08:00
|
|
|
|
|
|
|
The boot loader currently only supports images with the following
|
|
|
|
characteristics:
|
2017-09-13 01:53:20 +08:00
|
|
|
* Built to run from flash.
|
|
|
|
* Built to run from a fixed location (i.e., not position-independent).
|
2016-12-13 07:59:26 +08:00
|
|
|
|
2017-09-13 01:53:20 +08:00
|
|
|
## Image Format
|
2016-12-13 07:59:26 +08:00
|
|
|
|
|
|
|
The following definitions describe the image format.
|
|
|
|
|
2017-09-13 01:53:20 +08:00
|
|
|
``` c
|
2017-09-11 22:02:47 +08:00
|
|
|
#define IMAGE_MAGIC 0x96f3b83d
|
2016-12-13 07:59:26 +08:00
|
|
|
|
|
|
|
#define IMAGE_HEADER_SIZE 32
|
|
|
|
|
|
|
|
struct image_version {
|
|
|
|
uint8_t iv_major;
|
|
|
|
uint8_t iv_minor;
|
|
|
|
uint16_t iv_revision;
|
|
|
|
uint32_t iv_build_num;
|
|
|
|
};
|
|
|
|
|
|
|
|
/** Image header. All fields are in little endian byte order. */
|
|
|
|
struct image_header {
|
|
|
|
uint32_t ih_magic;
|
2017-09-11 22:02:47 +08:00
|
|
|
uint32_t ih_load_addr;
|
2016-12-13 07:59:26 +08:00
|
|
|
uint16_t ih_hdr_size; /* Size of image header (bytes). */
|
|
|
|
uint16_t _pad2;
|
|
|
|
uint32_t ih_img_size; /* Does not include header. */
|
2017-09-11 22:02:47 +08:00
|
|
|
uint32_t ih_flags; /* IMAGE_F_[...]. */
|
2016-12-13 07:59:26 +08:00
|
|
|
struct image_version ih_ver;
|
|
|
|
uint32_t _pad3;
|
|
|
|
};
|
|
|
|
|
2017-09-11 22:02:47 +08:00
|
|
|
/** Image TLV header. All fields in little endian. */
|
|
|
|
struct image_tlv_info {
|
|
|
|
uint16_t it_magic;
|
|
|
|
uint16_t it_tlv_tot; /* size of TLV area (including tlv_info header) */
|
|
|
|
};
|
|
|
|
|
2016-12-13 07:59:26 +08:00
|
|
|
/** Image trailer TLV format. All fields in little endian. */
|
|
|
|
struct image_tlv {
|
|
|
|
uint8_t it_type; /* IMAGE_TLV_[...]. */
|
|
|
|
uint8_t _pad;
|
2017-08-05 02:50:51 +08:00
|
|
|
uint16_t it_len; /* Data length (not including TLV header). */
|
2016-12-13 07:59:26 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Image header flags.
|
|
|
|
*/
|
2017-08-05 02:46:39 +08:00
|
|
|
#define IMAGE_F_PIC 0x00000001 /* Not supported. */
|
|
|
|
#define IMAGE_F_NON_BOOTABLE 0x00000010 /* Split image app. */
|
2017-09-11 22:02:47 +08:00
|
|
|
#define IMAGE_F_RAM_LOAD 0x00000020
|
2016-12-13 07:59:26 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Image trailer TLV types.
|
|
|
|
*/
|
2017-09-11 22:02:47 +08:00
|
|
|
#define IMAGE_TLV_KEYHASH 0x01 /* hash of the public key */
|
2017-09-01 00:40:29 +08:00
|
|
|
#define IMAGE_TLV_SHA256 0x10 /* SHA256 of image hdr and body */
|
2017-08-23 12:40:49 +08:00
|
|
|
#define IMAGE_TLV_RSA2048_PSS 0x20 /* RSA2048 of hash output */
|
2017-09-01 00:40:29 +08:00
|
|
|
#define IMAGE_TLV_ECDSA224 0x21 /* ECDSA of hash output */
|
|
|
|
#define IMAGE_TLV_ECDSA256 0x22 /* ECDSA of hash output */
|
2017-09-13 01:53:20 +08:00
|
|
|
```
|
2016-12-13 07:59:26 +08:00
|
|
|
|
|
|
|
Optional type-length-value records (TLVs) containing image metadata are placed
|
|
|
|
after the end of the image.
|
|
|
|
|
2017-09-13 01:53:20 +08:00
|
|
|
The `ih_hdr_size` field indicates the length of the header, and therefore the
|
2016-12-13 07:59:26 +08:00
|
|
|
offset of the image itself. This field provides for backwards compatibility in
|
|
|
|
case of changes to the format of the image header.
|
|
|
|
|
2017-09-13 01:53:20 +08:00
|
|
|
## Flash Map
|
2016-12-13 07:59:26 +08:00
|
|
|
|
2017-07-21 00:20:48 +08:00
|
|
|
A device's flash is partitioned according to its _flash map_. At a high
|
2016-12-13 07:59:26 +08:00
|
|
|
level, the flash map maps numeric IDs to _flash areas_. A flash area is a
|
|
|
|
region of disk with the following properties:
|
2017-09-13 01:53:20 +08:00
|
|
|
1. An area can be fully erased without affecting any other areas.
|
|
|
|
2. A write to one area does not restrict writes to other areas.
|
2016-12-13 07:59:26 +08:00
|
|
|
|
2017-08-05 02:53:33 +08:00
|
|
|
The boot loader uses the following flash area IDs:
|
2016-12-13 07:59:26 +08:00
|
|
|
|
2017-09-13 01:53:20 +08:00
|
|
|
``` c
|
2016-12-13 07:59:26 +08:00
|
|
|
#define FLASH_AREA_BOOTLOADER 0
|
|
|
|
#define FLASH_AREA_IMAGE_0 1
|
|
|
|
#define FLASH_AREA_IMAGE_1 2
|
|
|
|
#define FLASH_AREA_IMAGE_SCRATCH 3
|
2017-09-13 01:53:20 +08:00
|
|
|
```
|
2016-12-13 07:59:26 +08:00
|
|
|
|
2017-08-05 02:53:33 +08:00
|
|
|
The bootloader area contains the bootloader image itself. The other areas are
|
|
|
|
described in subsequent sections.
|
|
|
|
|
2017-09-13 01:53:20 +08:00
|
|
|
## Image Slots
|
2016-12-13 07:59:26 +08:00
|
|
|
|
|
|
|
A portion of the flash memory is partitioned into two image slots: a primary
|
|
|
|
slot (0) and a secondary slot (1). The boot loader will only run an image from
|
|
|
|
the primary slot, so images must be built such that they can run from that
|
|
|
|
fixed location in flash. If the boot loader needs to run the image resident in
|
2017-08-05 02:56:08 +08:00
|
|
|
the secondary slot, it must copy its contents into the primary slot before doing
|
|
|
|
so, either by swapping the two images or by overwriting the contents of the
|
|
|
|
primary slot. The bootloader supports either swap- or overwrite-based image
|
|
|
|
upgrades, but must be configured at build time to choose one of these two
|
|
|
|
strategies.
|
2016-12-13 07:59:26 +08:00
|
|
|
|
|
|
|
In addition to the two image slots, the boot loader requires a scratch area to
|
2017-12-13 00:04:53 +08:00
|
|
|
allow for reliable image swapping. The scratch area must have a size that is
|
|
|
|
enough to store at least the largest sector that is going to be swapped. Many
|
|
|
|
devices have small equally sized flash sectors, eg 4K, while others have variable
|
|
|
|
sized sectors where the largest sectors might be 128K or 256K, so the scratch
|
|
|
|
must be big enough to store that. The scratch is only ever used when swapping
|
|
|
|
firmware, which means only when doing an upgrade. Given that, the main reason
|
|
|
|
for using a larger size for the scratch is that flash wear will be more evenly
|
|
|
|
distributed, because a single sector would be written twice the number of times
|
|
|
|
than using two sectors, for example. To evaluate the ideal size of the scratch
|
|
|
|
for your use case the following parameters are relevant:
|
|
|
|
|
|
|
|
* the ratio of image size / scratch size
|
|
|
|
* the number of erase cycles supported by the flash hardware
|
|
|
|
|
|
|
|
The image size is used (instead of slot size) because only the slot's sectors
|
|
|
|
that are actually used for storing the image are copied. The image/scratch ratio
|
|
|
|
is the number of times the scratch will be erased on every upgrade. The number
|
|
|
|
of erase cycles divided by the image/scratch ratio will give you the number of
|
|
|
|
times an upgrade can be performed before the device goes out of spec.
|
|
|
|
|
|
|
|
```
|
|
|
|
num_upgrades = number_of_erase_cycles / (image_size / scratch_size)
|
|
|
|
```
|
|
|
|
|
|
|
|
Let's assume, for example, a device with 10000 erase cycles, an image size of
|
|
|
|
150K and a scratch of 4K (usual minimum size of 4K sector devices). This would
|
|
|
|
result in a total of:
|
|
|
|
|
|
|
|
`10000 / (150 / 4) ~ 267`
|
|
|
|
|
|
|
|
Increasing the scratch to 16K would give us:
|
|
|
|
|
|
|
|
`10000 / (150 / 16) ~ 1067`
|
|
|
|
|
|
|
|
There is no *best* ratio, as the right size is use-case dependent. Factors to
|
|
|
|
consider include the number of times a device will be upgraded both in the field
|
|
|
|
and during development, as well as any desired safety margin on the manufacturer's
|
|
|
|
specified number of erase cycles. In general, using a ratio that allows hundreds
|
|
|
|
to thousands of field upgrades in production is recommended.
|
2016-12-13 07:59:26 +08:00
|
|
|
|
2017-08-05 02:56:08 +08:00
|
|
|
The overwrite upgrade strategy is substantially simpler to implement than the
|
|
|
|
image swapping strategy, especially since the bootloader must work properly
|
|
|
|
even when it is reset during the middle of an image swap. For this reason, the
|
|
|
|
rest of the document describes its behavior when configured to swap images
|
|
|
|
during an upgrade.
|
|
|
|
|
2017-09-13 01:53:20 +08:00
|
|
|
## Boot Swap Types
|
doc: design: rewrite state space in terms of "swap types"
The current "boot states" description doesn't make sense and shouldn't
be used.
For one thing, with three possible pending states, two possible
confirmed states, and two image slots each with a combined (pending,
confirmed) state, the total number of boot states is 36, but the
document says there are "four possible states".
For another, the actual bits on flash map to the "boot states" in a
way that is carefully designed to ensure that only those 4 are the
"outcome" of a boot.
The fact that this map does not cover the entire space of what is
being presented as the "logical" states of the device is a strong
indication that the pending/confirmed state space is a bad choice, not
connected with the actual operation of the bootloader.
A state space that is better for describing how the bootloader behaves
is given the by the enumeration of "swap types" which appear under
each of the tables in the "IMAGE TRAILERS" section, as well as the
bootloader code itself.
To help fix the description of the bootloader' operation, rewrite the
"boot states" portions of the design document, deleting the
pending/confirmed "states" and replacing them with swap types.
There is still more work to do here:
- There is still an "important caveat" to describing things in
terms of swap types, which means it's not quite right.
- It's strange to say that "none" is a swap type.
- This doesn't provide a clean explanation for how mcuboot handles an
interrupted swap.
But it's another step.
Signed-off-by: Marti Bolivar <marti.bolivar@linaro.org>
2017-08-05 05:14:24 +08:00
|
|
|
|
|
|
|
When the device first boots under normal circumstances, there is an up-to-date
|
|
|
|
firmware image in slot 0, which mcuboot can validate and then chain-load. In
|
|
|
|
this case, no image swaps are necessary. During device upgrades, however, new
|
|
|
|
candidate images are present in slot 1, which mcuboot must swap into slot 0
|
|
|
|
before booting as discussed above.
|
|
|
|
|
|
|
|
Upgrading an old image with a new one by swapping can be a two-step process. In
|
|
|
|
this process, mcuboot performs a "test" swap of image data in flash and boots
|
|
|
|
the new image. The new image can then update the contents of flash at runtime
|
|
|
|
to mark itself "OK", and mcuboot will then still choose to run it during the
|
|
|
|
next boot. When this happens, the swap is made "permanent". If this doesn't
|
|
|
|
happen, mcuboot will perform a "revert" swap during the next boot by swapping
|
|
|
|
the images back into their original locations, and attempting to boot the old
|
|
|
|
image.
|
|
|
|
|
|
|
|
Depending on the use case, the first swap can also be made permanent directly.
|
|
|
|
In this case, mcuboot will never attempt to revert the images on the next reset.
|
|
|
|
|
|
|
|
Test swaps are supported to provide a rollback mechanism to prevent devices
|
|
|
|
from becoming "bricked" by bad firmware. If the device crashes immediately
|
|
|
|
upon booting a new (bad) image, mcuboot will revert to the old (working) image
|
|
|
|
at the next device reset, rather than booting the bad image again. This allows
|
|
|
|
device firmware to make test swaps permanent only after performing a self-test
|
|
|
|
routine.
|
|
|
|
|
|
|
|
On startup, mcuboot inspects the contents of flash to decide which of these
|
|
|
|
"swap types" to perform; this decision determines how it proceeds.
|
|
|
|
|
|
|
|
The possible swap types, and their meanings, are:
|
|
|
|
|
2017-09-13 01:53:20 +08:00
|
|
|
- `BOOT_SWAP_TYPE_NONE`: The "usual" or "no upgrade" case; attempt to boot the
|
doc: design: rewrite state space in terms of "swap types"
The current "boot states" description doesn't make sense and shouldn't
be used.
For one thing, with three possible pending states, two possible
confirmed states, and two image slots each with a combined (pending,
confirmed) state, the total number of boot states is 36, but the
document says there are "four possible states".
For another, the actual bits on flash map to the "boot states" in a
way that is carefully designed to ensure that only those 4 are the
"outcome" of a boot.
The fact that this map does not cover the entire space of what is
being presented as the "logical" states of the device is a strong
indication that the pending/confirmed state space is a bad choice, not
connected with the actual operation of the bootloader.
A state space that is better for describing how the bootloader behaves
is given the by the enumeration of "swap types" which appear under
each of the tables in the "IMAGE TRAILERS" section, as well as the
bootloader code itself.
To help fix the description of the bootloader' operation, rewrite the
"boot states" portions of the design document, deleting the
pending/confirmed "states" and replacing them with swap types.
There is still more work to do here:
- There is still an "important caveat" to describing things in
terms of swap types, which means it's not quite right.
- It's strange to say that "none" is a swap type.
- This doesn't provide a clean explanation for how mcuboot handles an
interrupted swap.
But it's another step.
Signed-off-by: Marti Bolivar <marti.bolivar@linaro.org>
2017-08-05 05:14:24 +08:00
|
|
|
contents of slot 0.
|
|
|
|
|
2017-09-13 01:53:20 +08:00
|
|
|
- `BOOT_SWAP_TYPE_TEST`: Boot the contents of slot 1 by swapping images. Unless
|
doc: design: rewrite state space in terms of "swap types"
The current "boot states" description doesn't make sense and shouldn't
be used.
For one thing, with three possible pending states, two possible
confirmed states, and two image slots each with a combined (pending,
confirmed) state, the total number of boot states is 36, but the
document says there are "four possible states".
For another, the actual bits on flash map to the "boot states" in a
way that is carefully designed to ensure that only those 4 are the
"outcome" of a boot.
The fact that this map does not cover the entire space of what is
being presented as the "logical" states of the device is a strong
indication that the pending/confirmed state space is a bad choice, not
connected with the actual operation of the bootloader.
A state space that is better for describing how the bootloader behaves
is given the by the enumeration of "swap types" which appear under
each of the tables in the "IMAGE TRAILERS" section, as well as the
bootloader code itself.
To help fix the description of the bootloader' operation, rewrite the
"boot states" portions of the design document, deleting the
pending/confirmed "states" and replacing them with swap types.
There is still more work to do here:
- There is still an "important caveat" to describing things in
terms of swap types, which means it's not quite right.
- It's strange to say that "none" is a swap type.
- This doesn't provide a clean explanation for how mcuboot handles an
interrupted swap.
But it's another step.
Signed-off-by: Marti Bolivar <marti.bolivar@linaro.org>
2017-08-05 05:14:24 +08:00
|
|
|
the swap is made permanent, revert back on the next boot.
|
|
|
|
|
2017-09-13 01:53:20 +08:00
|
|
|
- `BOOT_SWAP_TYPE_PERM`: Permanently swap images, and boot the upgraded image
|
doc: design: rewrite state space in terms of "swap types"
The current "boot states" description doesn't make sense and shouldn't
be used.
For one thing, with three possible pending states, two possible
confirmed states, and two image slots each with a combined (pending,
confirmed) state, the total number of boot states is 36, but the
document says there are "four possible states".
For another, the actual bits on flash map to the "boot states" in a
way that is carefully designed to ensure that only those 4 are the
"outcome" of a boot.
The fact that this map does not cover the entire space of what is
being presented as the "logical" states of the device is a strong
indication that the pending/confirmed state space is a bad choice, not
connected with the actual operation of the bootloader.
A state space that is better for describing how the bootloader behaves
is given the by the enumeration of "swap types" which appear under
each of the tables in the "IMAGE TRAILERS" section, as well as the
bootloader code itself.
To help fix the description of the bootloader' operation, rewrite the
"boot states" portions of the design document, deleting the
pending/confirmed "states" and replacing them with swap types.
There is still more work to do here:
- There is still an "important caveat" to describing things in
terms of swap types, which means it's not quite right.
- It's strange to say that "none" is a swap type.
- This doesn't provide a clean explanation for how mcuboot handles an
interrupted swap.
But it's another step.
Signed-off-by: Marti Bolivar <marti.bolivar@linaro.org>
2017-08-05 05:14:24 +08:00
|
|
|
firmware.
|
|
|
|
|
2017-09-13 01:53:20 +08:00
|
|
|
- `BOOT_SWAP_TYPE_REVERT`: A previous test swap was not made permanent; swap back
|
doc: design: rewrite state space in terms of "swap types"
The current "boot states" description doesn't make sense and shouldn't
be used.
For one thing, with three possible pending states, two possible
confirmed states, and two image slots each with a combined (pending,
confirmed) state, the total number of boot states is 36, but the
document says there are "four possible states".
For another, the actual bits on flash map to the "boot states" in a
way that is carefully designed to ensure that only those 4 are the
"outcome" of a boot.
The fact that this map does not cover the entire space of what is
being presented as the "logical" states of the device is a strong
indication that the pending/confirmed state space is a bad choice, not
connected with the actual operation of the bootloader.
A state space that is better for describing how the bootloader behaves
is given the by the enumeration of "swap types" which appear under
each of the tables in the "IMAGE TRAILERS" section, as well as the
bootloader code itself.
To help fix the description of the bootloader' operation, rewrite the
"boot states" portions of the design document, deleting the
pending/confirmed "states" and replacing them with swap types.
There is still more work to do here:
- There is still an "important caveat" to describing things in
terms of swap types, which means it's not quite right.
- It's strange to say that "none" is a swap type.
- This doesn't provide a clean explanation for how mcuboot handles an
interrupted swap.
But it's another step.
Signed-off-by: Marti Bolivar <marti.bolivar@linaro.org>
2017-08-05 05:14:24 +08:00
|
|
|
to the old image whose data are now in slot 1. If the old image marks itself
|
2017-09-13 01:53:20 +08:00
|
|
|
"OK" when it boots, the next boot will have swap type `BOOT_SWAP_TYPE_NONE`.
|
doc: design: rewrite state space in terms of "swap types"
The current "boot states" description doesn't make sense and shouldn't
be used.
For one thing, with three possible pending states, two possible
confirmed states, and two image slots each with a combined (pending,
confirmed) state, the total number of boot states is 36, but the
document says there are "four possible states".
For another, the actual bits on flash map to the "boot states" in a
way that is carefully designed to ensure that only those 4 are the
"outcome" of a boot.
The fact that this map does not cover the entire space of what is
being presented as the "logical" states of the device is a strong
indication that the pending/confirmed state space is a bad choice, not
connected with the actual operation of the bootloader.
A state space that is better for describing how the bootloader behaves
is given the by the enumeration of "swap types" which appear under
each of the tables in the "IMAGE TRAILERS" section, as well as the
bootloader code itself.
To help fix the description of the bootloader' operation, rewrite the
"boot states" portions of the design document, deleting the
pending/confirmed "states" and replacing them with swap types.
There is still more work to do here:
- There is still an "important caveat" to describing things in
terms of swap types, which means it's not quite right.
- It's strange to say that "none" is a swap type.
- This doesn't provide a clean explanation for how mcuboot handles an
interrupted swap.
But it's another step.
Signed-off-by: Marti Bolivar <marti.bolivar@linaro.org>
2017-08-05 05:14:24 +08:00
|
|
|
|
2017-09-13 01:53:20 +08:00
|
|
|
- `BOOT_SWAP_TYPE_FAIL`: Swap failed because image to be run is not valid.
|
doc: design: rewrite state space in terms of "swap types"
The current "boot states" description doesn't make sense and shouldn't
be used.
For one thing, with three possible pending states, two possible
confirmed states, and two image slots each with a combined (pending,
confirmed) state, the total number of boot states is 36, but the
document says there are "four possible states".
For another, the actual bits on flash map to the "boot states" in a
way that is carefully designed to ensure that only those 4 are the
"outcome" of a boot.
The fact that this map does not cover the entire space of what is
being presented as the "logical" states of the device is a strong
indication that the pending/confirmed state space is a bad choice, not
connected with the actual operation of the bootloader.
A state space that is better for describing how the bootloader behaves
is given the by the enumeration of "swap types" which appear under
each of the tables in the "IMAGE TRAILERS" section, as well as the
bootloader code itself.
To help fix the description of the bootloader' operation, rewrite the
"boot states" portions of the design document, deleting the
pending/confirmed "states" and replacing them with swap types.
There is still more work to do here:
- There is still an "important caveat" to describing things in
terms of swap types, which means it's not quite right.
- It's strange to say that "none" is a swap type.
- This doesn't provide a clean explanation for how mcuboot handles an
interrupted swap.
But it's another step.
Signed-off-by: Marti Bolivar <marti.bolivar@linaro.org>
2017-08-05 05:14:24 +08:00
|
|
|
|
2017-09-13 01:53:20 +08:00
|
|
|
- `BOOT_SWAP_TYPE_PANIC`: Swapping encountered an unrecoverable error.
|
doc: design: rewrite state space in terms of "swap types"
The current "boot states" description doesn't make sense and shouldn't
be used.
For one thing, with three possible pending states, two possible
confirmed states, and two image slots each with a combined (pending,
confirmed) state, the total number of boot states is 36, but the
document says there are "four possible states".
For another, the actual bits on flash map to the "boot states" in a
way that is carefully designed to ensure that only those 4 are the
"outcome" of a boot.
The fact that this map does not cover the entire space of what is
being presented as the "logical" states of the device is a strong
indication that the pending/confirmed state space is a bad choice, not
connected with the actual operation of the bootloader.
A state space that is better for describing how the bootloader behaves
is given the by the enumeration of "swap types" which appear under
each of the tables in the "IMAGE TRAILERS" section, as well as the
bootloader code itself.
To help fix the description of the bootloader' operation, rewrite the
"boot states" portions of the design document, deleting the
pending/confirmed "states" and replacing them with swap types.
There is still more work to do here:
- There is still an "important caveat" to describing things in
terms of swap types, which means it's not quite right.
- It's strange to say that "none" is a swap type.
- This doesn't provide a clean explanation for how mcuboot handles an
interrupted swap.
But it's another step.
Signed-off-by: Marti Bolivar <marti.bolivar@linaro.org>
2017-08-05 05:14:24 +08:00
|
|
|
|
|
|
|
The "swap type" is a high-level representation of the outcome of the
|
|
|
|
boot. Subsequent sections describe how mcuboot determines the swap type from
|
|
|
|
the bit-level contents of flash.
|
2016-12-22 05:46:08 +08:00
|
|
|
|
2017-09-13 01:53:20 +08:00
|
|
|
## Image Trailer
|
2016-12-13 07:59:26 +08:00
|
|
|
|
2017-07-29 05:56:29 +08:00
|
|
|
For the bootloader to be able to determine the current state and what actions
|
2017-08-05 03:45:01 +08:00
|
|
|
should be taken during the current boot operation, it uses metadata stored in
|
|
|
|
the image flash areas. While swapping, some of this metadata is temporarily
|
|
|
|
copied into and out of the scratch area.
|
|
|
|
|
|
|
|
This metadata is located at the end of the image flash areas, and is called an
|
|
|
|
image trailer. An image trailer has the following structure:
|
2016-12-13 07:59:26 +08:00
|
|
|
|
2017-09-13 01:53:20 +08:00
|
|
|
```
|
2016-12-13 07:59:26 +08:00
|
|
|
0 1 2 3
|
|
|
|
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
|
|
|
|
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
|
|
|
~ ~
|
2018-04-04 21:35:17 +08:00
|
|
|
~ Swap status (BOOT_MAX_IMG_SECTORS * min-write-size * 3) ~
|
2016-12-13 07:59:26 +08:00
|
|
|
~ ~
|
|
|
|
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
2017-09-11 22:02:47 +08:00
|
|
|
| Swap size |
|
|
|
|
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
|
|
|
| 0xff padding (4 octets) |
|
2017-07-20 19:55:38 +08:00
|
|
|
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
2017-09-11 22:02:47 +08:00
|
|
|
| Copy done | 0xff padding (7 octets) ~
|
2016-12-13 07:59:26 +08:00
|
|
|
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
2017-09-11 22:02:47 +08:00
|
|
|
| Image OK | 0xff padding (7 octets) ~
|
|
|
|
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
|
|
|
~ MAGIC (16 octets) ~
|
2016-12-13 07:59:26 +08:00
|
|
|
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
2017-09-13 01:53:20 +08:00
|
|
|
```
|
2016-12-13 07:59:26 +08:00
|
|
|
|
2017-08-05 03:45:01 +08:00
|
|
|
The offset immediately following such a record represents the start of the next
|
|
|
|
flash area.
|
2016-12-13 07:59:26 +08:00
|
|
|
|
|
|
|
Note: "min-write-size" is a property of the flash hardware. If the hardware
|
|
|
|
allows individual bytes to be written at arbitrary addresses, then
|
|
|
|
min-write-size is 1. If the hardware only allows writes at even addresses,
|
|
|
|
then min-write-size is 2, and so on.
|
|
|
|
|
2017-08-05 03:59:32 +08:00
|
|
|
An image trailer contains the following fields:
|
|
|
|
|
|
|
|
1. Swap status: A series of records which records the progress of an image
|
2017-09-13 01:53:20 +08:00
|
|
|
swap. To swap entire images, data are swapped between the two image areas one
|
|
|
|
or more sectors at a time, like this:
|
2017-08-05 03:59:32 +08:00
|
|
|
|
2017-09-13 01:53:20 +08:00
|
|
|
- sector data in slot 0 is copied into scratch, then erased
|
|
|
|
- sector data in slot 1 is copied into slot 0, then erased
|
|
|
|
- sector data in scratch is copied into slot 1
|
2017-08-05 03:59:32 +08:00
|
|
|
|
|
|
|
As it swaps images, the bootloader updates the swap status field in a way that
|
|
|
|
allows it to compute how far this swap operation has progressed for each
|
|
|
|
sector. The swap status field can thus used to resume a swap operation if the
|
|
|
|
bootloader is halted while a swap operation is ongoing and later reset. The
|
2018-04-04 21:35:17 +08:00
|
|
|
`BOOT_MAX_IMG_SECTORS` value is the configurable maximum number of sectors mcuboot
|
|
|
|
supports for each image; its value defaults to 128, but allows for either
|
|
|
|
decreasing this size, to limit RAM usage, or to increase it in devices that have
|
|
|
|
massive amounts of Flash or very small sized sectors and thus require a bigger
|
|
|
|
configuration to allow for the handling of all slot's sectors. The factor of
|
|
|
|
min-write-sz is due to the behavior of flash hardware. The factor of 3 is
|
|
|
|
explained below.
|
2016-12-13 07:59:26 +08:00
|
|
|
|
2017-09-11 22:02:47 +08:00
|
|
|
2. Swap size: When beginning a new swap operation, the total size that needs
|
2017-09-13 01:53:20 +08:00
|
|
|
to be swapped (based on the slot with largest image + tlvs) is written to this
|
|
|
|
location for easier recovery in case of a reset while performing the swap.
|
2017-09-11 22:02:47 +08:00
|
|
|
|
|
|
|
3. Copy done: A single byte indicating whether the image in this slot is
|
2017-09-13 01:53:20 +08:00
|
|
|
complete (0x01=done; 0xff=not done).
|
2016-12-13 07:59:26 +08:00
|
|
|
|
2017-09-11 22:02:47 +08:00
|
|
|
4. Image OK: A single byte indicating whether the image in this slot has been
|
2017-09-13 01:53:20 +08:00
|
|
|
confirmed as good by the user (0x01=confirmed; 0xff=not confirmed).
|
2016-12-13 07:59:26 +08:00
|
|
|
|
2017-09-11 22:02:47 +08:00
|
|
|
5. MAGIC: The following 16 bytes, written in host-byte-order:
|
2017-07-20 19:55:38 +08:00
|
|
|
|
2017-09-13 01:53:20 +08:00
|
|
|
``` c
|
2017-07-20 19:55:38 +08:00
|
|
|
const uint32_t boot_img_magic[4] = {
|
|
|
|
0xf395c277,
|
|
|
|
0x7fefd260,
|
|
|
|
0x0f505235,
|
|
|
|
0x8079b62c,
|
|
|
|
};
|
2017-09-13 01:53:20 +08:00
|
|
|
```
|
2017-07-20 19:55:38 +08:00
|
|
|
|
2017-09-13 01:53:20 +08:00
|
|
|
## IMAGE TRAILERS
|
2017-07-29 05:56:29 +08:00
|
|
|
|
doc: design: rewrite state space in terms of "swap types"
The current "boot states" description doesn't make sense and shouldn't
be used.
For one thing, with three possible pending states, two possible
confirmed states, and two image slots each with a combined (pending,
confirmed) state, the total number of boot states is 36, but the
document says there are "four possible states".
For another, the actual bits on flash map to the "boot states" in a
way that is carefully designed to ensure that only those 4 are the
"outcome" of a boot.
The fact that this map does not cover the entire space of what is
being presented as the "logical" states of the device is a strong
indication that the pending/confirmed state space is a bad choice, not
connected with the actual operation of the bootloader.
A state space that is better for describing how the bootloader behaves
is given the by the enumeration of "swap types" which appear under
each of the tables in the "IMAGE TRAILERS" section, as well as the
bootloader code itself.
To help fix the description of the bootloader' operation, rewrite the
"boot states" portions of the design document, deleting the
pending/confirmed "states" and replacing them with swap types.
There is still more work to do here:
- There is still an "important caveat" to describing things in
terms of swap types, which means it's not quite right.
- It's strange to say that "none" is a swap type.
- This doesn't provide a clean explanation for how mcuboot handles an
interrupted swap.
But it's another step.
Signed-off-by: Marti Bolivar <marti.bolivar@linaro.org>
2017-08-05 05:14:24 +08:00
|
|
|
At startup, the boot loader determines the boot swap type by inspecting the
|
|
|
|
image trailers. When using the term "image trailers" what is meant is the
|
|
|
|
aggregate information provided by both image slot's trailers.
|
2017-07-29 05:56:29 +08:00
|
|
|
|
|
|
|
The image trailers records are structured around the limitations imposed by flash
|
2016-12-13 07:59:26 +08:00
|
|
|
hardware. As a consequence, they do not have a very intuitive design, and it
|
|
|
|
is difficult to get a sense of the state of the device just by looking at the
|
doc: design: rewrite state space in terms of "swap types"
The current "boot states" description doesn't make sense and shouldn't
be used.
For one thing, with three possible pending states, two possible
confirmed states, and two image slots each with a combined (pending,
confirmed) state, the total number of boot states is 36, but the
document says there are "four possible states".
For another, the actual bits on flash map to the "boot states" in a
way that is carefully designed to ensure that only those 4 are the
"outcome" of a boot.
The fact that this map does not cover the entire space of what is
being presented as the "logical" states of the device is a strong
indication that the pending/confirmed state space is a bad choice, not
connected with the actual operation of the bootloader.
A state space that is better for describing how the bootloader behaves
is given the by the enumeration of "swap types" which appear under
each of the tables in the "IMAGE TRAILERS" section, as well as the
bootloader code itself.
To help fix the description of the bootloader' operation, rewrite the
"boot states" portions of the design document, deleting the
pending/confirmed "states" and replacing them with swap types.
There is still more work to do here:
- There is still an "important caveat" to describing things in
terms of swap types, which means it's not quite right.
- It's strange to say that "none" is a swap type.
- This doesn't provide a clean explanation for how mcuboot handles an
interrupted swap.
But it's another step.
Signed-off-by: Marti Bolivar <marti.bolivar@linaro.org>
2017-08-05 05:14:24 +08:00
|
|
|
image trailers. It is better to map all the possible trailer states to the swap
|
|
|
|
types described above via a set of tables. These tables are reproduced below.
|
2016-12-13 07:59:26 +08:00
|
|
|
|
2017-07-29 05:56:29 +08:00
|
|
|
Note: An important caveat about the tables described below is that they must
|
|
|
|
be evaluated in the order presented here. Lower state numbers must have a
|
|
|
|
higher priority when testing the image trailers.
|
2017-07-20 19:55:38 +08:00
|
|
|
|
2017-09-13 01:53:20 +08:00
|
|
|
```
|
2017-07-29 05:56:29 +08:00
|
|
|
State I
|
2016-12-13 07:59:26 +08:00
|
|
|
| slot-0 | slot-1 |
|
|
|
|
-----------------+--------+--------|
|
|
|
|
magic | Any | Good |
|
2016-12-22 05:46:08 +08:00
|
|
|
image-ok | Any | Unset |
|
2017-07-21 02:05:13 +08:00
|
|
|
copy-done | Any | Any |
|
2016-12-13 07:59:26 +08:00
|
|
|
-----------------+--------+--------'
|
doc: design: rewrite state space in terms of "swap types"
The current "boot states" description doesn't make sense and shouldn't
be used.
For one thing, with three possible pending states, two possible
confirmed states, and two image slots each with a combined (pending,
confirmed) state, the total number of boot states is 36, but the
document says there are "four possible states".
For another, the actual bits on flash map to the "boot states" in a
way that is carefully designed to ensure that only those 4 are the
"outcome" of a boot.
The fact that this map does not cover the entire space of what is
being presented as the "logical" states of the device is a strong
indication that the pending/confirmed state space is a bad choice, not
connected with the actual operation of the bootloader.
A state space that is better for describing how the bootloader behaves
is given the by the enumeration of "swap types" which appear under
each of the tables in the "IMAGE TRAILERS" section, as well as the
bootloader code itself.
To help fix the description of the bootloader' operation, rewrite the
"boot states" portions of the design document, deleting the
pending/confirmed "states" and replacing them with swap types.
There is still more work to do here:
- There is still an "important caveat" to describing things in
terms of swap types, which means it's not quite right.
- It's strange to say that "none" is a swap type.
- This doesn't provide a clean explanation for how mcuboot handles an
interrupted swap.
But it's another step.
Signed-off-by: Marti Bolivar <marti.bolivar@linaro.org>
2017-08-05 05:14:24 +08:00
|
|
|
result: BOOT_SWAP_TYPE_TEST |
|
2016-12-22 05:46:08 +08:00
|
|
|
-----------------------------------'
|
2017-07-20 19:55:38 +08:00
|
|
|
|
2016-12-13 07:59:26 +08:00
|
|
|
|
2017-07-29 05:56:29 +08:00
|
|
|
State II
|
2016-12-13 07:59:26 +08:00
|
|
|
| slot-0 | slot-1 |
|
2016-12-22 05:46:08 +08:00
|
|
|
-----------------+--------+--------|
|
|
|
|
magic | Any | Good |
|
|
|
|
image-ok | Any | 0x01 |
|
2017-07-21 02:05:13 +08:00
|
|
|
copy-done | Any | Any |
|
2016-12-22 05:46:08 +08:00
|
|
|
-----------------+--------+--------'
|
doc: design: rewrite state space in terms of "swap types"
The current "boot states" description doesn't make sense and shouldn't
be used.
For one thing, with three possible pending states, two possible
confirmed states, and two image slots each with a combined (pending,
confirmed) state, the total number of boot states is 36, but the
document says there are "four possible states".
For another, the actual bits on flash map to the "boot states" in a
way that is carefully designed to ensure that only those 4 are the
"outcome" of a boot.
The fact that this map does not cover the entire space of what is
being presented as the "logical" states of the device is a strong
indication that the pending/confirmed state space is a bad choice, not
connected with the actual operation of the bootloader.
A state space that is better for describing how the bootloader behaves
is given the by the enumeration of "swap types" which appear under
each of the tables in the "IMAGE TRAILERS" section, as well as the
bootloader code itself.
To help fix the description of the bootloader' operation, rewrite the
"boot states" portions of the design document, deleting the
pending/confirmed "states" and replacing them with swap types.
There is still more work to do here:
- There is still an "important caveat" to describing things in
terms of swap types, which means it's not quite right.
- It's strange to say that "none" is a swap type.
- This doesn't provide a clean explanation for how mcuboot handles an
interrupted swap.
But it's another step.
Signed-off-by: Marti Bolivar <marti.bolivar@linaro.org>
2017-08-05 05:14:24 +08:00
|
|
|
result: BOOT_SWAP_TYPE_PERM |
|
2016-12-22 05:46:08 +08:00
|
|
|
-----------------------------------'
|
2017-07-20 19:55:38 +08:00
|
|
|
|
2016-12-22 05:46:08 +08:00
|
|
|
|
2017-07-29 05:56:29 +08:00
|
|
|
State III
|
2016-12-22 05:46:08 +08:00
|
|
|
| slot-0 | slot-1 |
|
2016-12-13 07:59:26 +08:00
|
|
|
-----------------+--------+--------|
|
|
|
|
magic | Good | Unset |
|
2016-12-22 05:46:08 +08:00
|
|
|
image-ok | 0xff | Any |
|
2017-07-20 19:55:38 +08:00
|
|
|
copy-done | 0x01 | Any |
|
2016-12-13 07:59:26 +08:00
|
|
|
-----------------+--------+--------'
|
doc: design: rewrite state space in terms of "swap types"
The current "boot states" description doesn't make sense and shouldn't
be used.
For one thing, with three possible pending states, two possible
confirmed states, and two image slots each with a combined (pending,
confirmed) state, the total number of boot states is 36, but the
document says there are "four possible states".
For another, the actual bits on flash map to the "boot states" in a
way that is carefully designed to ensure that only those 4 are the
"outcome" of a boot.
The fact that this map does not cover the entire space of what is
being presented as the "logical" states of the device is a strong
indication that the pending/confirmed state space is a bad choice, not
connected with the actual operation of the bootloader.
A state space that is better for describing how the bootloader behaves
is given the by the enumeration of "swap types" which appear under
each of the tables in the "IMAGE TRAILERS" section, as well as the
bootloader code itself.
To help fix the description of the bootloader' operation, rewrite the
"boot states" portions of the design document, deleting the
pending/confirmed "states" and replacing them with swap types.
There is still more work to do here:
- There is still an "important caveat" to describing things in
terms of swap types, which means it's not quite right.
- It's strange to say that "none" is a swap type.
- This doesn't provide a clean explanation for how mcuboot handles an
interrupted swap.
But it's another step.
Signed-off-by: Marti Bolivar <marti.bolivar@linaro.org>
2017-08-05 05:14:24 +08:00
|
|
|
result: BOOT_SWAP_TYPE_REVERT |
|
2016-12-13 07:59:26 +08:00
|
|
|
-----------------------------------'
|
2017-09-13 01:53:20 +08:00
|
|
|
```
|
2016-12-13 07:59:26 +08:00
|
|
|
|
doc: design: rewrite state space in terms of "swap types"
The current "boot states" description doesn't make sense and shouldn't
be used.
For one thing, with three possible pending states, two possible
confirmed states, and two image slots each with a combined (pending,
confirmed) state, the total number of boot states is 36, but the
document says there are "four possible states".
For another, the actual bits on flash map to the "boot states" in a
way that is carefully designed to ensure that only those 4 are the
"outcome" of a boot.
The fact that this map does not cover the entire space of what is
being presented as the "logical" states of the device is a strong
indication that the pending/confirmed state space is a bad choice, not
connected with the actual operation of the bootloader.
A state space that is better for describing how the bootloader behaves
is given the by the enumeration of "swap types" which appear under
each of the tables in the "IMAGE TRAILERS" section, as well as the
bootloader code itself.
To help fix the description of the bootloader' operation, rewrite the
"boot states" portions of the design document, deleting the
pending/confirmed "states" and replacing them with swap types.
There is still more work to do here:
- There is still an "important caveat" to describing things in
terms of swap types, which means it's not quite right.
- It's strange to say that "none" is a swap type.
- This doesn't provide a clean explanation for how mcuboot handles an
interrupted swap.
But it's another step.
Signed-off-by: Marti Bolivar <marti.bolivar@linaro.org>
2017-08-05 05:14:24 +08:00
|
|
|
Any of the above three states results in mcuboot attempting to swap images.
|
2016-12-13 07:59:26 +08:00
|
|
|
|
doc: design: rewrite state space in terms of "swap types"
The current "boot states" description doesn't make sense and shouldn't
be used.
For one thing, with three possible pending states, two possible
confirmed states, and two image slots each with a combined (pending,
confirmed) state, the total number of boot states is 36, but the
document says there are "four possible states".
For another, the actual bits on flash map to the "boot states" in a
way that is carefully designed to ensure that only those 4 are the
"outcome" of a boot.
The fact that this map does not cover the entire space of what is
being presented as the "logical" states of the device is a strong
indication that the pending/confirmed state space is a bad choice, not
connected with the actual operation of the bootloader.
A state space that is better for describing how the bootloader behaves
is given the by the enumeration of "swap types" which appear under
each of the tables in the "IMAGE TRAILERS" section, as well as the
bootloader code itself.
To help fix the description of the bootloader' operation, rewrite the
"boot states" portions of the design document, deleting the
pending/confirmed "states" and replacing them with swap types.
There is still more work to do here:
- There is still an "important caveat" to describing things in
terms of swap types, which means it's not quite right.
- It's strange to say that "none" is a swap type.
- This doesn't provide a clean explanation for how mcuboot handles an
interrupted swap.
But it's another step.
Signed-off-by: Marti Bolivar <marti.bolivar@linaro.org>
2017-08-05 05:14:24 +08:00
|
|
|
Otherwise, mcuboot does not attempt to swap images, resulting in one of the
|
|
|
|
other three swap types, as illustrated by State IV.
|
2017-07-29 05:56:29 +08:00
|
|
|
|
2017-09-13 01:53:20 +08:00
|
|
|
```
|
2017-07-29 05:56:29 +08:00
|
|
|
State IV
|
2016-12-13 07:59:26 +08:00
|
|
|
| slot-0 | slot-1 |
|
|
|
|
-----------------+--------+--------|
|
2017-07-29 05:56:29 +08:00
|
|
|
magic | Any | Any |
|
|
|
|
image-ok | Any | Any |
|
2017-07-21 02:05:13 +08:00
|
|
|
copy-done | Any | Any |
|
2016-12-13 07:59:26 +08:00
|
|
|
-----------------+--------+--------'
|
doc: design: rewrite state space in terms of "swap types"
The current "boot states" description doesn't make sense and shouldn't
be used.
For one thing, with three possible pending states, two possible
confirmed states, and two image slots each with a combined (pending,
confirmed) state, the total number of boot states is 36, but the
document says there are "four possible states".
For another, the actual bits on flash map to the "boot states" in a
way that is carefully designed to ensure that only those 4 are the
"outcome" of a boot.
The fact that this map does not cover the entire space of what is
being presented as the "logical" states of the device is a strong
indication that the pending/confirmed state space is a bad choice, not
connected with the actual operation of the bootloader.
A state space that is better for describing how the bootloader behaves
is given the by the enumeration of "swap types" which appear under
each of the tables in the "IMAGE TRAILERS" section, as well as the
bootloader code itself.
To help fix the description of the bootloader' operation, rewrite the
"boot states" portions of the design document, deleting the
pending/confirmed "states" and replacing them with swap types.
There is still more work to do here:
- There is still an "important caveat" to describing things in
terms of swap types, which means it's not quite right.
- It's strange to say that "none" is a swap type.
- This doesn't provide a clean explanation for how mcuboot handles an
interrupted swap.
But it's another step.
Signed-off-by: Marti Bolivar <marti.bolivar@linaro.org>
2017-08-05 05:14:24 +08:00
|
|
|
result: BOOT_SWAP_TYPE_NONE, |
|
|
|
|
BOOT_SWAP_TYPE_FAIL, or |
|
|
|
|
BOOT_SWAP_TYPE_PANIC |
|
2016-12-13 07:59:26 +08:00
|
|
|
-----------------------------------'
|
2017-09-13 01:53:20 +08:00
|
|
|
```
|
2016-12-13 07:59:26 +08:00
|
|
|
|
doc: design: rewrite state space in terms of "swap types"
The current "boot states" description doesn't make sense and shouldn't
be used.
For one thing, with three possible pending states, two possible
confirmed states, and two image slots each with a combined (pending,
confirmed) state, the total number of boot states is 36, but the
document says there are "four possible states".
For another, the actual bits on flash map to the "boot states" in a
way that is carefully designed to ensure that only those 4 are the
"outcome" of a boot.
The fact that this map does not cover the entire space of what is
being presented as the "logical" states of the device is a strong
indication that the pending/confirmed state space is a bad choice, not
connected with the actual operation of the bootloader.
A state space that is better for describing how the bootloader behaves
is given the by the enumeration of "swap types" which appear under
each of the tables in the "IMAGE TRAILERS" section, as well as the
bootloader code itself.
To help fix the description of the bootloader' operation, rewrite the
"boot states" portions of the design document, deleting the
pending/confirmed "states" and replacing them with swap types.
There is still more work to do here:
- There is still an "important caveat" to describing things in
terms of swap types, which means it's not quite right.
- It's strange to say that "none" is a swap type.
- This doesn't provide a clean explanation for how mcuboot handles an
interrupted swap.
But it's another step.
Signed-off-by: Marti Bolivar <marti.bolivar@linaro.org>
2017-08-05 05:14:24 +08:00
|
|
|
In State IV, when no errors occur, mcuboot will attempt to boot the contents of
|
2017-09-13 01:53:20 +08:00
|
|
|
slot 0 directly, and the result is `BOOT_SWAP_TYPE_NONE`. If the image in slot 0
|
|
|
|
is not valid, the result is `BOOT_SWAP_TYPE_FAIL`. If a fatal error occurs during
|
|
|
|
boot, the result is `BOOT_SWAP_TYPE_PANIC`. If the result is either
|
|
|
|
`BOOT_SWAP_TYPE_FAIL` or `BOOT_SWAP_TYPE_PANIC`, mcuboot hangs rather than booting
|
doc: design: rewrite state space in terms of "swap types"
The current "boot states" description doesn't make sense and shouldn't
be used.
For one thing, with three possible pending states, two possible
confirmed states, and two image slots each with a combined (pending,
confirmed) state, the total number of boot states is 36, but the
document says there are "four possible states".
For another, the actual bits on flash map to the "boot states" in a
way that is carefully designed to ensure that only those 4 are the
"outcome" of a boot.
The fact that this map does not cover the entire space of what is
being presented as the "logical" states of the device is a strong
indication that the pending/confirmed state space is a bad choice, not
connected with the actual operation of the bootloader.
A state space that is better for describing how the bootloader behaves
is given the by the enumeration of "swap types" which appear under
each of the tables in the "IMAGE TRAILERS" section, as well as the
bootloader code itself.
To help fix the description of the bootloader' operation, rewrite the
"boot states" portions of the design document, deleting the
pending/confirmed "states" and replacing them with swap types.
There is still more work to do here:
- There is still an "important caveat" to describing things in
terms of swap types, which means it's not quite right.
- It's strange to say that "none" is a swap type.
- This doesn't provide a clean explanation for how mcuboot handles an
interrupted swap.
But it's another step.
Signed-off-by: Marti Bolivar <marti.bolivar@linaro.org>
2017-08-05 05:14:24 +08:00
|
|
|
an invalid or compromised image.
|
|
|
|
|
|
|
|
Note: An important caveat to the above is the result when a swap is requested
|
|
|
|
and the image in slot 1 fails to validate, due to a hashing or signing
|
|
|
|
error. This state behaves as State IV with the extra action of marking
|
|
|
|
the image in slot 0 as "OK", to prevent further attempts to swap.
|
2017-07-29 05:56:29 +08:00
|
|
|
|
|
|
|
|
2017-09-13 01:53:20 +08:00
|
|
|
## High-Level Operation
|
2016-12-13 07:59:26 +08:00
|
|
|
|
|
|
|
With the terms defined, we can now explore the boot loader's operation. First,
|
|
|
|
a high-level overview of the boot process is presented. Then, the following
|
|
|
|
sections describe each step of the process in more detail.
|
|
|
|
|
|
|
|
Procedure:
|
|
|
|
|
2017-09-13 01:53:20 +08:00
|
|
|
1. Inspect swap status region; is an interrupted swap being resumed?
|
|
|
|
Yes: Complete the partial swap operation; skip to step 3.
|
|
|
|
No: Proceed to step 2.
|
2016-12-13 07:59:26 +08:00
|
|
|
|
2017-09-13 01:53:20 +08:00
|
|
|
2. Inspect image trailers; is a swap requested?
|
2016-12-13 07:59:26 +08:00
|
|
|
Yes.
|
|
|
|
1. Is the requested image valid (integrity and security check)?
|
|
|
|
Yes.
|
|
|
|
a. Perform swap operation.
|
2017-07-29 05:56:29 +08:00
|
|
|
b. Persist completion of swap procedure to image trailers.
|
2017-09-13 01:53:20 +08:00
|
|
|
c. Proceed to step 3.
|
2016-12-13 07:59:26 +08:00
|
|
|
No.
|
|
|
|
a. Erase invalid image.
|
2017-07-29 05:56:29 +08:00
|
|
|
b. Persist failure of swap procedure to image trailers.
|
2017-09-13 01:53:20 +08:00
|
|
|
c. Proceed to step 3.
|
|
|
|
No: Proceed to step 3.
|
2016-12-13 07:59:26 +08:00
|
|
|
|
2017-09-13 01:53:20 +08:00
|
|
|
3. Boot into image in slot 0.
|
2016-12-13 07:59:26 +08:00
|
|
|
|
2017-09-13 01:53:20 +08:00
|
|
|
## Image Swapping
|
2016-12-13 07:59:26 +08:00
|
|
|
|
|
|
|
The boot loader swaps the contents of the two image slots for two reasons:
|
2016-12-22 05:46:08 +08:00
|
|
|
* User has issued a "set pending" operation; the image in slot-1 should be
|
|
|
|
run once (state II) or repeatedly (state III), depending on whether a
|
|
|
|
permanent swap was specified.
|
2016-12-13 07:59:26 +08:00
|
|
|
* Test image rebooted without being confirmed; the boot loader should
|
2016-12-22 05:46:08 +08:00
|
|
|
revert to the original image currently in slot-1 (state IV).
|
2016-12-13 07:59:26 +08:00
|
|
|
|
2017-07-29 05:56:29 +08:00
|
|
|
If the image trailers indicates that the image in the secondary slot should be
|
2016-12-13 07:59:26 +08:00
|
|
|
run, the boot loader needs to copy it to the primary slot. The image currently
|
|
|
|
in the primary slot also needs to be retained in flash so that it can be used
|
|
|
|
later. Furthermore, both images need to be recoverable if the boot loader
|
|
|
|
resets in the middle of the swap operation. The two images are swapped
|
|
|
|
according to the following procedure:
|
|
|
|
|
2017-09-13 01:53:20 +08:00
|
|
|
<!-- Markdown doesn't do nested numbered lists. It will do nested
|
|
|
|
bulletted lists, so maybe that is better. -->
|
2016-12-13 07:59:26 +08:00
|
|
|
1. Determine how many flash sectors each image slot consists of. This
|
|
|
|
number must be the same for both slots.
|
|
|
|
2. Iterate the list of sector indices in descending order (i.e., starting
|
|
|
|
with the greatest index); current element = "index".
|
|
|
|
b. Erase scratch area.
|
2017-10-20 03:27:46 +08:00
|
|
|
c. Copy slot1[index] to scratch area.
|
2017-07-20 19:55:38 +08:00
|
|
|
- If these are the last sectors (i.e., first swap being perfomed),
|
|
|
|
copy the full sector *except* the image trailer.
|
|
|
|
- Else, copy entire sector contents.
|
2016-12-13 07:59:26 +08:00
|
|
|
d. Write updated swap status (i).
|
|
|
|
|
|
|
|
e. Erase slot1[index]
|
|
|
|
f. Copy slot0[index] to slot1[index]
|
|
|
|
- If these are the last sectors (i.e., first swap being perfomed),
|
|
|
|
copy the full sector *except* the image trailer.
|
|
|
|
- Else, copy entire sector contents.
|
|
|
|
g. Write updated swap status (ii).
|
|
|
|
|
|
|
|
h. Erase slot0[index].
|
2017-10-20 03:27:46 +08:00
|
|
|
i. Copy scratch area to slot0[index].
|
2017-07-20 19:55:38 +08:00
|
|
|
- If these are the last sectors (i.e., first swap being perfomed),
|
|
|
|
copy the full sector *except* the image trailer.
|
|
|
|
- Else, copy entire sector contents.
|
2016-12-13 07:59:26 +08:00
|
|
|
j. Write updated swap status (iii).
|
|
|
|
|
|
|
|
3. Persist completion of swap procedure to slot 0 image trailer.
|
|
|
|
|
2016-12-22 05:46:08 +08:00
|
|
|
The additional caveats in step 2f are necessary so that the slot 1 image
|
|
|
|
trailer can be written by the user at a later time. With the image trailer
|
|
|
|
unwritten, the user can test the image in slot 1 (i.e., transition to state
|
|
|
|
II).
|
2016-12-13 07:59:26 +08:00
|
|
|
|
2017-07-20 19:55:38 +08:00
|
|
|
Note1: If the sector being copied is the last sector, then swap status is
|
|
|
|
temporarily maintained on scratch for the duration of this operation, always
|
|
|
|
using slot0's area otherwise.
|
|
|
|
|
|
|
|
Note2: The bootloader tries to copy only used sectors (based on largest image
|
|
|
|
installed on any of the slots), minimizing the amount of sectors copied and
|
|
|
|
reducing the amount of time required for a swap operation.
|
|
|
|
|
2016-12-22 05:46:08 +08:00
|
|
|
The particulars of step 3 vary depending on whether an image is being tested,
|
2017-07-29 05:56:29 +08:00
|
|
|
permanently used, reverted or a validation failure of slot 1 happened when a
|
|
|
|
swap was requested:
|
|
|
|
|
2016-12-13 07:59:26 +08:00
|
|
|
* test:
|
|
|
|
o Write slot0.copy_done = 1
|
2016-12-22 05:46:08 +08:00
|
|
|
(swap caused the following values to be written:
|
|
|
|
slot0.magic = BOOT_MAGIC
|
|
|
|
slot0.image_ok = Unset)
|
|
|
|
|
|
|
|
* permanent:
|
|
|
|
o Write slot0.copy_done = 1
|
|
|
|
(swap caused the following values to be written:
|
|
|
|
slot0.magic = BOOT_MAGIC
|
|
|
|
slot0.image_ok = 0x01)
|
2016-12-13 07:59:26 +08:00
|
|
|
|
|
|
|
* revert:
|
|
|
|
o Write slot0.copy_done = 1
|
|
|
|
o Write slot0.image_ok = 1
|
2017-07-29 05:56:29 +08:00
|
|
|
(swap caused the following values to be written:
|
|
|
|
slot0.magic = BOOT_MAGIC)
|
|
|
|
|
|
|
|
* failure to validate slot 1:
|
|
|
|
o Write slot0.image_ok = 1
|
|
|
|
|
|
|
|
After completing the operations as described above the image in slot 0 should
|
|
|
|
be booted.
|
2016-12-13 07:59:26 +08:00
|
|
|
|
2017-09-13 01:53:20 +08:00
|
|
|
## Swap Status
|
2016-12-13 07:59:26 +08:00
|
|
|
|
|
|
|
The swap status region allows the boot loader to recover in case it restarts in
|
|
|
|
the middle of an image swap operation. The swap status region consists of a
|
|
|
|
series of single-byte records. These records are written independently, and
|
|
|
|
therefore must be padded according to the minimum write size imposed by the
|
|
|
|
flash hardware. In the below figure, a min-write-size of 1 is assumed for
|
|
|
|
simplicity. The structure of the swap status region is illustrated below. In
|
|
|
|
this figure, a min-write-size of 1 is assumed for simplicity.
|
|
|
|
|
2017-09-13 01:53:20 +08:00
|
|
|
```
|
2016-12-13 07:59:26 +08:00
|
|
|
0 1 2 3
|
|
|
|
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
|
|
|
|
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
|
|
|
|sec127,state 0 |sec127,state 1 |sec127,state 2 |sec126,state 0 |
|
|
|
|
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
|
|
|
|sec126,state 1 |sec126,state 2 |sec125,state 0 |sec125,state 1 |
|
|
|
|
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
|
|
|
|sec125,state 2 | |
|
|
|
|
+-+-+-+-+-+-+-+-+ +
|
|
|
|
~ ~
|
|
|
|
~ [Records for indices 124 through 1 ~
|
|
|
|
~ ~
|
|
|
|
~ +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
|
|
|
~ |sec000,state 0 |sec000,state 1 |sec000,state 2 |
|
|
|
|
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
2017-09-13 01:53:20 +08:00
|
|
|
```
|
2016-12-13 07:59:26 +08:00
|
|
|
|
|
|
|
The above is probably not helpful at all; here is a description in English.
|
|
|
|
|
|
|
|
Each image slot is partitioned into a sequence of flash sectors. If we were to
|
|
|
|
enumerate the sectors in a single slot, starting at 0, we would have a list of
|
|
|
|
sector indices. Since there are two image slots, each sector index would
|
|
|
|
correspond to a pair of sectors. For example, sector index 0 corresponds to
|
2018-04-04 21:35:17 +08:00
|
|
|
the first sector in slot 0 and the first sector in slot 1. Finally, reverse
|
|
|
|
the list of indices such that the list starts with index `BOOT_MAX_IMG_SECTORS - 1`
|
|
|
|
and ends with 0. The swap status region is a representation of this reversed list.
|
2016-12-13 07:59:26 +08:00
|
|
|
|
|
|
|
During a swap operation, each sector index transitions through four separate
|
|
|
|
states:
|
2017-09-13 01:53:20 +08:00
|
|
|
```
|
2016-12-13 07:59:26 +08:00
|
|
|
0. slot 0: image 0, slot 1: image 1, scratch: N/A
|
|
|
|
1. slot 0: image 0, slot 1: N/A, scratch: image 1 (1->s, erase 1)
|
|
|
|
2. slot 0: N/A, slot 1: image 0, scratch: image 1 (0->1, erase 0)
|
|
|
|
3. slot 0: image 1, slot 1: image 0, scratch: N/A (s->0)
|
2017-09-13 01:53:20 +08:00
|
|
|
```
|
2016-12-13 07:59:26 +08:00
|
|
|
|
|
|
|
Each time a sector index transitions to a new state, the boot loader writes a
|
|
|
|
record to the swap status region. Logically, the boot loader only needs one
|
|
|
|
record per sector index to keep track of the current swap state. However, due
|
|
|
|
to limitations imposed by flash hardware, a record cannot be overwritten when
|
|
|
|
an index's state changes. To solve this problem, the boot loader uses three
|
|
|
|
records per sector index rather than just one.
|
|
|
|
|
|
|
|
Each sector-state pair is represented as a set of three records. The record
|
|
|
|
values map to the above four states as follows
|
|
|
|
|
2017-09-13 01:53:20 +08:00
|
|
|
```
|
2016-12-13 07:59:26 +08:00
|
|
|
| rec0 | rec1 | rec2
|
|
|
|
--------+------+------+------
|
|
|
|
state 0 | 0xff | 0xff | 0xff
|
|
|
|
state 1 | 0x01 | 0xff | 0xff
|
|
|
|
state 2 | 0x01 | 0x02 | 0xff
|
|
|
|
state 3 | 0x01 | 0x02 | 0x03
|
2017-09-13 01:53:20 +08:00
|
|
|
```
|
2016-12-13 07:59:26 +08:00
|
|
|
|
2018-04-04 21:35:17 +08:00
|
|
|
The swap status region can accommodate `BOOT_MAX_IMG_SECTORS` sector indices.
|
|
|
|
Hence, the size of the region, in bytes, is `BOOT_MAX_IMG_SECTORS * min-write-size * 3`.
|
|
|
|
The only requirement for the index count is that it is great enough to account for a
|
2016-12-13 07:59:26 +08:00
|
|
|
maximum-sized image (i.e., at least as great as the total sector count in an
|
2018-04-04 21:35:17 +08:00
|
|
|
image slot). If a device's image slots have been configured with
|
|
|
|
`BOOT_MAX_IMG_SECTORS: 128` and use less than 128 sectors, the first
|
2016-12-13 07:59:26 +08:00
|
|
|
record that gets written will be somewhere in the middle of the region. For
|
|
|
|
example, if a slot uses 64 sectors, the first sector index that gets swapped is
|
|
|
|
63, which corresponds to the exact halfway point within the region.
|
|
|
|
|
2017-07-20 19:55:38 +08:00
|
|
|
Note: since the scratch area only ever needs to record swapping of the last
|
|
|
|
sector, it uses at most min-write-size * 3 bytes for its own status area.
|
|
|
|
|
2017-09-13 01:53:20 +08:00
|
|
|
## Reset Recovery
|
2016-12-13 07:59:26 +08:00
|
|
|
|
|
|
|
If the boot loader resets in the middle of a swap operation, the two images may
|
|
|
|
be discontiguous in flash. Bootutil recovers from this condition by using the
|
2017-07-29 05:56:29 +08:00
|
|
|
image trailers to determine how the image parts are distributed in flash.
|
2016-12-13 07:59:26 +08:00
|
|
|
|
|
|
|
The first step is determine where the relevant swap status region is located.
|
|
|
|
Because this region is embedded within the image slots, its location in flash
|
2017-07-29 05:56:29 +08:00
|
|
|
changes during a swap operation. The below set of tables map image trailers
|
2016-12-13 07:59:26 +08:00
|
|
|
contents to swap status location. In these tables, the "source" field
|
|
|
|
indicates where the swap status region is located.
|
|
|
|
|
2017-09-13 01:53:20 +08:00
|
|
|
```
|
2016-12-13 07:59:26 +08:00
|
|
|
| slot-0 | scratch |
|
|
|
|
----------+------------+------------|
|
|
|
|
magic | Good | Any |
|
|
|
|
copy-done | 0x01 | N/A |
|
|
|
|
----------+------------+------------'
|
|
|
|
source: none |
|
|
|
|
------------------------------------'
|
2017-08-05 02:50:51 +08:00
|
|
|
|
2016-12-13 07:59:26 +08:00
|
|
|
| slot-0 | scratch |
|
|
|
|
----------+------------+------------|
|
|
|
|
magic | Good | Any |
|
|
|
|
copy-done | 0xff | N/A |
|
|
|
|
----------+------------+------------'
|
|
|
|
source: slot 0 |
|
|
|
|
------------------------------------'
|
2017-08-05 02:50:51 +08:00
|
|
|
|
2016-12-13 07:59:26 +08:00
|
|
|
| slot-0 | scratch |
|
|
|
|
----------+------------+------------|
|
|
|
|
magic | Any | Good |
|
|
|
|
copy-done | Any | N/A |
|
|
|
|
----------+------------+------------'
|
|
|
|
source: scratch |
|
|
|
|
------------------------------------'
|
2017-08-05 02:50:51 +08:00
|
|
|
|
2016-12-13 07:59:26 +08:00
|
|
|
| slot-0 | scratch |
|
|
|
|
----------+------------+------------|
|
|
|
|
magic | Unset | Any |
|
|
|
|
copy-done | 0xff | N/A |
|
|
|
|
----------+------------+------------|
|
2017-07-29 05:56:29 +08:00
|
|
|
source: slot 0 |
|
2016-12-13 07:59:26 +08:00
|
|
|
------------------------------------+------------------------------+
|
|
|
|
This represents one of two cases: |
|
|
|
|
o No swaps ever (no status to read, so no harm in checking). |
|
|
|
|
o Mid-revert; status in slot 0. |
|
2017-07-29 05:56:29 +08:00
|
|
|
For this reason we assume slot 0 as source, to trigger a check |
|
|
|
|
of the status area and find out if there was swapping under way. |
|
2016-12-13 07:59:26 +08:00
|
|
|
-------------------------------------------------------------------'
|
2017-09-13 01:53:20 +08:00
|
|
|
```
|
2016-12-13 07:59:26 +08:00
|
|
|
|
|
|
|
If the swap status region indicates that the images are not contiguous,
|
|
|
|
bootutil completes the swap operation that was in progress when the system was
|
|
|
|
reset. In other words, it applies the procedure defined in the previous
|
|
|
|
section, moving image 1 into slot 0 and image 0 into slot 1. If the boot
|
|
|
|
status file indicates that an image part is present in the scratch area, this
|
|
|
|
part is copied into the correct location by starting at step e or step h in the
|
|
|
|
area-swap procedure, depending on whether the part belongs to image 0 or image
|
|
|
|
1.
|
|
|
|
|
|
|
|
After the swap operation has been completed, the boot loader proceeds as though
|
|
|
|
it had just been started.
|
|
|
|
|
2017-09-13 01:53:20 +08:00
|
|
|
## Integrity Check
|
2016-12-13 07:59:26 +08:00
|
|
|
|
|
|
|
An image is checked for integrity immediately before it gets copied into the
|
2017-07-20 19:55:38 +08:00
|
|
|
primary slot. If the boot loader doesn't perform an image swap, then it can
|
|
|
|
perform an optional integrity check of the image in slot0 if
|
2017-09-13 01:53:20 +08:00
|
|
|
`MCUBOOT_VALIDATE_SLOT0` is set, otherwise it doesn't perform an integrity check.
|
2016-12-13 07:59:26 +08:00
|
|
|
|
|
|
|
During the integrity check, the boot loader verifies the following aspects of
|
|
|
|
an image:
|
2017-09-11 22:02:47 +08:00
|
|
|
* 32-bit magic number must be correct (0x96f3b83d).
|
2017-09-13 01:53:20 +08:00
|
|
|
* Image must contain an `image_tlv_info` struct, identified by its magic
|
2017-09-11 22:02:47 +08:00
|
|
|
(0x6907) exactly following the firmware (hdr_size + img_size).
|
2016-12-13 07:59:26 +08:00
|
|
|
* Image must contain a SHA256 TLV.
|
2017-07-29 05:56:29 +08:00
|
|
|
* Calculated SHA256 must match SHA256 TLV contents.
|
2017-09-11 22:02:47 +08:00
|
|
|
* Image *may* contain a signature TLV. If it does, it must also have a
|
|
|
|
KEYHASH TLV with the hash of the key that was used to sign. The list of
|
|
|
|
keys will then be iterated over looking for the matching key, which then
|
|
|
|
will then be used to verify the image contents.
|
2016-12-13 07:59:26 +08:00
|
|
|
|
2017-09-13 01:53:20 +08:00
|
|
|
## Security
|
2016-12-13 07:59:26 +08:00
|
|
|
|
|
|
|
As indicated above, the final step of the integrity check is signature
|
|
|
|
verification. The boot loader can have one or more public keys embedded in it
|
|
|
|
at build time. During signature verification, the boot loader verifies that an
|
2017-09-11 22:02:47 +08:00
|
|
|
image was signed with a private key that corresponds to the embedded keyhash
|
|
|
|
TLV.
|
2016-12-13 07:59:26 +08:00
|
|
|
|
|
|
|
For information on embedding public keys in the boot loader, as well as
|
2018-02-13 01:31:32 +08:00
|
|
|
producing signed images, see: [signed_images](signed_images.md).
|