Definition
A bootloader is the first program that runs when a device starts, or the first after code fixed in the chip’s read-only memory (ROM). It selects the application image, can check it, and hands the processor to it. An application image is the complete program in stored form: one block of machine code and data, with a header that describes it, kept in flash memory §6.6. Many bootloaders also install updates, by copying or swapping a new image into place, and offer a way to recover when no valid image exists.
The idea
At reset, a processor starts executing at a fixed address, and whatever program sits there runs first and controls everything. Suppose that program were the application itself. To update itself, it would have to erase and rewrite the flash memory that holds the very instructions doing the update, and a power cut halfway through would leave a device that cannot start. A small, separate program at the start address solves both problems. It rarely changes, and it runs before the application, at a moment when nothing else uses the flash memory. It can therefore replace the application safely and, if the new application turns out to be bad, return to the old one.
Running first gives the bootloader control, not trustworthiness. A bootloader deserves trust only if nobody can modify it and only if it checks what it starts. What may be trusted before the application runs is therefore a question about protection, answered below.
How it works
A common design for a 32-bit microcontroller §6.1 divides flash memory into areas called partitions:
the bootloader, at the address where the processor starts;
the primary slot, which holds the image that runs;
the secondary slot, into which the running application downloads a new image;
a scratch area, temporary space used while the two images are swapped;
storage for the application’s lasting data.
The processor runs code directly from flash memory, which is called execute in place (XIP), so in this design an image must be moved into the primary slot before it can run.
On an Arm Cortex-M processor, a start proceeds as follows.
The processor reads the vector table §6.14 at the start of flash memory, here the bootloader’s: the first word gives the initial value of the stack pointer, the register that marks the top of the call stack, and the second gives the address of the reset handler, the first code to run.
The bootloader sets up the little hardware it needs, then reads the header of each slot and the state flags that record update requests. MCUboot, a widely used bootloader, keeps these flags in a trailer at the end of each slot.
It decides what to run. With no update pending, it chooses the primary slot. With an update pending, it first swaps the two slots, recording its progress in the trailers so that it can finish the swap after a power cut.
If it is configured to check images, it checks the chosen one: every size and offset in the header must lie inside the slot, and the image’s cryptographic hash §5.1 and digital signature §5.14 must verify. A bootloader that makes this check is the first link of secure boot §6.31.
It hands over. It switches off the interrupts §6.11 and peripherals it used, points the processor’s VTOR (vector table offset register) at the application’s vector table, loads the stack pointer from the application’s first word, and jumps to the address in its second word.
An MCUboot image starts with a header that holds a magic number, 0x96f3b83d, which marks a valid header; the load address; the sizes of the header and of the image; flags; and a version number in four parts: major, minor, revision and build. After the code comes a TLV §4.31 (type-length-value) area, a list of entries that each consist of a type, a length and a value. It holds a hash of the image, usually SHA-256 §5.2 (Secure Hash Algorithm with a 256-bit output), and the signature. Some entries, such as a security counter, sit in a protected part of this area that the signature also covers.
Worked example
This example shows why changing one check means a new image and a restart. A device runs statically linked firmware: the linker §12.4, the tool that joins compiled pieces into one program, has combined the operating system, the drivers, the libraries and the application into one 256 KiB image, and has given every function and variable a fixed address. A developer adds one bounds check to one function, and the function grows by 8 bytes. The functions that the linker placed after it move by 8 bytes, so every instruction that calls one of them, and every table that points to one, changes too. Changed bytes are spread over the whole image, and its hash and signature must be computed again. The device has no program that could link a replacement function into the running firmware, so the whole image is replaced. The costs, with assumed figures:
Transfer. Over a radio link with an assumed useful throughput of 8 KiB per second, 256 KiB take seconds.
Storage. The secondary slot is erased before the image is written. With sectors of 4 KiB, the slot has sectors; at an erase time of up to 85 ms per sector, erasing takes up to ms, about 5.4 seconds.
Restart and swap. The device restarts, and the swap erases every sector of both slots at least once: at least erases. At up to 85 ms each, these 128 erases alone take up to ms, almost 11 seconds, during which only the bootloader runs. The restart also ends every connection and loses every value held in RAM.
Wear and space. Each update uses at least one of the limited erase cycles of every sector in both slots, and the secondary slot reserves as much flash memory as the image itself.
A delta update sends only the differences between the old and the new image, which shortens the transfer, but the device still rebuilds a complete image, installs it and restarts.
Stated precisely
The argument that a device starts correctly trusts four things before the application runs, and each needs a reason.
The bootloader’s code, because hardware prevents its modification: it sits in ROM or in flash memory that the chip write-protects, and the debug access that could rewrite it is locked. Without that protection, the bootloader is just another program that an attacker can replace. With it, the bootloader, or the ROM code that runs before it, is the device’s root of trust §1.33: the part that every later check relies on and that nothing checks.
The data that the bootloader relies on and that is stored under the same protection: public keys or hashes of public keys, its configuration, and a stored security counter.
The hardware state after reset, as the chip’s reference manual specifies it.
The assumptions behind any cryptographic check it makes: that nobody without the private key can forge a signature, and that nobody can find two inputs with the same hash.
Everything else is untrusted until checked: the images, their headers and trailers, the flags that request a swap or claim confirmation, and the secondary slot as a whole, because the application, or an attacker who controls it, could have written them. The bootloader must check each such field before relying on it, and the bytes it checks must be the bytes it starts.
Test, confirm and revert in MCUboot
MCUboot is an open-source bootloader for 32-bit microcontrollers; its name joins MCU, short for microcontroller unit, and boot. The Zephyr RTOS §6.33 (real-time operating system) and several other systems use it. Its design document describes how an update is tried before it is kept.
The running application writes the new image into the secondary slot and marks it for a test.
At the next reset, MCUboot swaps the two slots, so that the new image is in the primary slot and the old image in the secondary slot, and starts the new image. The new image is not yet marked as confirmed.
The new image checks itself, for example by reconnecting to its update server. If the checks pass, it writes a confirmation flag,
image_ok, into its trailer.If the device resets before the image has confirmed itself, whether through a crash, a watchdog timer §6.16 or a power cut, MCUboot reverts at the next start: it swaps the slots back, marks the old image as confirmed and starts it.
An update can instead be marked permanent in step 1, and MCUboot then never reverts it. The test-and-confirm sequence is a commit-confirmed update §8.23: a new version stays only if it confirms itself.
MCUboot can also resist a downgrade attack §1.27, in which an attacker installs an older, validly signed version that still contains a vulnerability fixed since.
Version check. With the option
MCUBOOT_DOWNGRADE_PREVENTION, MCUboot compares the version numbers in the headers of the new image and the installed image, and refuses the older one. The comparison involves only the two images in flash memory. The design document describes this check for the overwrite-only update mode, in which the new image replaces the old one without a swap; newer releases also apply it to swaps.Security counter. An image can carry a security counter in its protected TLV area, where the signature covers it. With the option
MCUBOOT_HW_ROLLBACK_PROT, the device keeps a counter value in non-volatile storage that it trusts, and validation refuses any image whose counter is lower. MCUboot raises the stored value only once the running image is confirmed, so a revert remains possible during the test. The counter need not rise with every release: an older image with the same counter value is still accepted.
Limits and common mistakes
A bootloader that only jumps to the primary slot is not secure boot: it starts whatever is there.
A revert protects only until confirmation. An image that confirms itself and fails an hour later is not reverted, so the checks before confirmation must exercise what matters most, such as the ability to receive the next update.
Every path the bootloader offers needs the same checks. A recovery mode that accepts an image over a serial line without verifying it defeats a strict normal path.
Updating the bootloader itself is risky, because a power cut while it is rewritten can leave a device that never starts again. Many devices therefore never update their bootloader, or keep an unchangeable first stage that checks an updatable second stage.
The application inherits state from the bootloader, such as clock settings and peripheral registers. An application that assumes every peripheral is in its reset state can fail.
Questions answered
What state is trusted before the application runs?
Before the application runs, only four things may be trusted, each for a stated reason: the bootloader’s code; the data stored with it under the same protection, such as public keys or their hashes, configuration and a stored security counter; the chip’s state just after reset, as its reference manual specifies it; and the assumptions behind the cryptography the bootloader uses. The bootloader’s code is trusted because hardware prevents its modification: it is in read-only memory or in write-protected flash memory, and the debug access that could rewrite it is locked. Running before the application does not by itself make it trustworthy.
Everything stored in the image slots is untrusted until checked: the image, its header, its trailer, and any flag that requests a swap or claims that the image is confirmed, since the application or an attacker could have written them. The bootloader checks the header’s sizes and offsets before using them, verifies the image’s hash and signature if it implements secure boot, compares the version with a stored minimum if it prevents downgrades, and then starts exactly the bytes it checked.
Related entries: Secure boot (§6.31), Root of trust (§1.33), Recovery fallback (§8.29), A/B update (§8.10), Commit-confirmed update (§8.23), Rollback versus anti-rollback (§8.26), Vector table (§6.14), Linker (§12.4), Program loading: from an ELF file to a running native process (§12.49). Further reading: [21].