Section 8.10

A/B update

Definition

An A/B update is a way of installing new software on a device that has storage for two complete copies of it, called slots A and B. The device runs from one slot while the new version, a complete program file called an image, is written into the other. A small, separately stored selection record tells the boot code which slot to start and whether the new image is still on trial. An interrupted update or a faulty new image leaves the old image intact in its own slot.

The idea

A device with room for only one copy of its firmware, the software stored in its flash memory §6.6, updates by erasing that copy and writing the new one. If power fails halfway, the device holds part of each version and cannot start; repairing it needs a cable or a return to the factory, and the device is said to be bricked. Even a complete new image can be faulty. It may crash as soon as it starts, and then it can no longer receive the next update that would fix it.

An A/B update removes both dangers by never writing over the image in use. The new image goes into the other slot, is checked there, and becomes the one to start only through one small change to the selection record. If the new image fails its first starts, the boot code returns to the old slot, the device’s last-known-good state §8.25. A theater understudy is a fair picture: the new actor rehearses on a second stage while the old cast keeps playing, and the notice naming tonight’s cast changes only when the new actor is ready. The picture stops at failure. An actor who freezes is noticed at once, but a device needs an explicit rule, such as a confirmation or a limited number of start attempts, to notice that a new image does not work.

How it works

An over-the-air (OTA) update is one delivered through a radio link, such as Wi-Fi, a cellular network or BLE §7.1 (Bluetooth Low Energy), instead of a cable. The names DFU (device firmware update; the USB specification of the same name says upgrade) and FOTA (firmware over-the-air) are also used for transferring and installing a firmware image. On a microcontroller §6.1 with two slots, an update over the air runs in seven stages.

  1. Transfer. The running software receives the new image and writes it into the inactive slot, the one it is not running from. The device keeps working. Before writing, the inactive slot is marked as not bootable, meaning that the bootloader §6.30 must not start it, so that a half-written image is never selected.

  2. Verify. A cryptographic hash §5.1, a fixed-length digest of the image’s bytes, shows that the image arrived complete and unchanged, and a digital signature §5.14, checked with the vendor’s public key, shows that the holder of the vendor’s private key approved it. A version check can refuse an older image.

  3. Mark pending. One write to the selection record §8.21 marks the new slot as the one to try at the next start. This is the commit point: before it, the next start runs the old image; after it, the next start tries the new one.

  4. Reset. The device restarts. This is the stage at which a firmware update normally interrupts the device’s work, because the new image can start only from a reset, which ends everything the old image was doing.

  5. Trial start. The bootloader, the small program that runs first after every reset and chooses what to start, reads the selection record, checks the new image again and starts it. Before starting it, the bootloader records the attempt, for example by lowering a stored count of remaining tries.

  6. Confirm. The new image tests itself, for example by checking that it can reach the network and accept the next update, and then writes a confirmation into the selection record. From then on the new image is the permanent choice.

  7. Fall back. If the new image resets before confirming, for example because it crashed and was restarted by a watchdog timer §6.16, a hardware timer that restarts a device that stops responding, the bootloader finds an unconfirmed slot with no tries left and starts the old slot again.

Stages 3 to 7 make this a commit-confirmed update §8.23: the new image is committed on trial at stage 3 and becomes permanent only when it is confirmed at stage 6. The bootloader never chooses a slot merely because it contains bytes. It chooses by the selection record and by checking the image in the chosen slot. Every change to the record is one small write, so that a power failure at any moment leaves a record that names a complete, checked image.

Worked example

Layout. A microcontroller has 1024 KiB of flash memory. The bootloader takes 48 KiB, and the selection record and other settings take 16 KiB, leaving 1024−48−16=9601024 - 48 - 16 = 960 KiB. Two equal slots get 960/2=480960 / 2 = 480 KiB each, so the largest image is 480 KiB, less the slot’s own metadata. One slot could have held an image of up to 960 KiB: the A/B scheme halves the largest possible image in exchange for always keeping a working one.

Transfer time. Assume a radio link that delivers 10 KiB/s of image data. A 300 KiB image then takes 300/10=30300 / 10 = 30 s to transfer. The device works normally during those 30 s; the interruption comes only at the reset.

Interruptions. The selection record holds, for each slot, whether it is bootable, whether it is confirmed and how many tries it has left. At the start, A is bootable and confirmed, and B is not bootable.

A faulty new image. The new image crashes every time it starts. Start 1: tries go from 3 to 2, B starts and crashes. Start 2: from 2 to 1. Start 3: from 1 to 0. Start 4: B is unconfirmed with 0 tries left, so the bootloader marks B not bootable and starts A. The device ran the faulty image 3 times and was back on the old image at the 4th start, with no help from outside.

Stated precisely

For each slot ss, let the selection record hold bootable(s)\mathrm{bootable}(s), confirmed(s)\mathrm{confirmed}(s), tries(s)\mathrm{tries}(s) and a priority p(s)p(s). At each reset the bootloader starts the slot ss with the highest p(s)p(s) among the slots that meet three conditions: bootable(s)\mathrm{bootable}(s) is true; the image in ss passes validation; and either confirmed(s)\mathrm{confirmed}(s) is true, or tries(s)>0\mathrm{tries}(s) > 0, in which case the bootloader first stores tries(s)−1\mathrm{tries}(s) - 1. A slot that fails the conditions is marked not bootable.

The guarantee: after a power loss at any instant, at least one slot meets the conditions, provided that

  1. the slot that holds the last confirmed image is not written until another image has been confirmed;

  2. a slot is marked bootable only after its image is completely written and verified;

  3. each change to the selection record is atomic with respect to power loss, for example because the record is kept in two copies with sequence numbers and checksums §4.32 and the bootloader uses the newer valid copy;

  4. the reduced try count is stored before the trial image runs; and

  5. the update does not change the bootloader itself.

A faulty new image then runs at most tries(s)\mathrm{tries}(s) times, counting from the value set by the pending mark, before the old image returns. Schemes that swap slot contents, such as the swap modes of MCUboot, break condition 1 while they swap and replace it with a stored progress record that lets an interrupted swap resume.

Two published schemes

Android A/B system updates, which Android also calls seamless updates, keep two copies of the system partitions, the named regions of storage that hold the operating system. Each slot has three attributes: bootable, meaning it holds a correct system; active, meaning the bootloader will start it next; and successful, meaning a started system has passed its checks. The updater first marks the running slot successful and the other slot unbootable, writes and verifies the new system in the other slot, and then marks that slot active. After the restart, a service in the new system, update_verifier, runs its checks and marks the slot successful. If a bootable slot is still not marked successful after several attempts to start it, Android requires the bootloader to mark it unbootable and to make the previous slot active again. The device stays usable while the update installs, so the only interruption is the one restart.

MCUboot, an open-source bootloader for microcontrollers (MCU stands for microcontroller unit), names its two slots primary and secondary. In its swap modes an image always runs from the primary slot, and a new image is written to the secondary slot. A small trailer at the end of each slot holds flags, among them image OK, which records confirmation. When the updater marks the new image for a test, MCUboot swaps the contents of the two slots at the next reset and starts the new image with image OK unset. If the running image confirms itself, usually by calling a library function that sets image OK, or if a management tool sends a confirm command, the swap becomes permanent. If the device resets before confirmation, the next boot swaps the images back, which MCUboot calls a revert. MCUboot records the progress of a swap in the trailer, so a swap interrupted by power loss resumes at the next boot. In its direct-XIP (execute in place) mode, MCUboot instead runs an image from either slot without moving it: it selects the slot with the higher version number and falls back to the other slot if that image fails validation. MCUboot can also refuse a downgrade by comparing version numbers or a security counter kept in protected storage.

Limits and common mistakes

Two slots halve the space available for an image. Android’s later Virtual A/B scheme avoids most of that cost by storing the update as copy-on-write §8.11 snapshots instead of a full second copy.

Starting is not working. A new image that starts, confirms itself at once and then cannot reach the network can never receive the fix. The confirmation must come after the checks that matter, above all the ability to accept the next update.

A/B protects the image, not the device’s stored data. If the new image converts stored settings to a new format, a fall back to the old image meets data it cannot read. That conversion is a schema migration §8.36 and needs its own rule, such as keeping the old format until the new image is confirmed.

Two slots do not prevent a downgrade attack §1.27, in which an attacker installs an older, correctly signed image that has a known flaw. Refusing older images needs a version floor stored in the device. Raising that floor before the new image is confirmed makes the fall back to the old image impossible; the Rollback versus anti-rollback §8.26 entry treats that conflict.

The bootloader is normally the one part the scheme does not update. A fault in it, or an interrupted update of it, cannot be repaired by switching slots.

Bootloaders implement the stages differently: whether the image runs from its slot or is copied, who confirms, how many tries are allowed, and what happens to a slot that fails. The defaults of one scheme must not be assumed for another.

Questions answered

What identifies the active slot after an interrupted update?

The active slot is identified by the stored selection record together with the bootloader’s check of the image in the slot that the record names, never by which slot happens to contain data. In an A/B update the device keeps two slots, A and B, for complete software images, runs from one and writes the new image into the other. The selection record says which slots are bootable, meaning they hold a complete image that may be started, which one to start next, whether the new one is still on trial and how many trial starts it has left. At reset the bootloader, the first program to run, picks the preferred bootable slot, verifies the image there, for example by its cryptographic hash and digital signature, and falls back to the other slot if the check fails or the trial starts run out.

Each possible interruption then leads to a defined slot.

Related entries: Commit record and selector (§8.21), Commit-confirmed update (§8.23), Rollback versus anti-rollback (§8.26), Secure boot (§6.31), Bootloader (§6.30), Last-known-good state (§8.25), Schema migration (§8.36). Further reading: [21].