Try Embedded

A/B updates, trial boots and rollback

Two slots, verifying in place, an atomic boot record, the trial boot and when to confirm.

What can go wrong during an update

An over-the-air update is the most dangerous thing a fielded device does. In the minutes it takes:

  • The power fails half way through writing flash.
  • The link drops, leaving a partial image.
  • The image is bad: corrupted, built for another hardware revision, or simply buggy, crashing at boot or failing to reconnect to the server that could fix it.
  • Someone sends their own image, or an old one with a known hole (the previous lesson).

A device that writes the new image over the old one has no answer to the first three: a power cut leaves neither image, and a buggy image leaves no way back. Recovering means a truck roll, a technician on site with a cable, for every device. The design that avoids it is old and simple.

Two slots

Flash is divided into two application slots, A and B, beside a small bootloader that is never updated in the field (or only with the same care, through its own redundant copy).

  1. The device runs from slot A. The update is written to slot B, the one not running. A power cut now costs nothing: A is untouched.
  2. When the image is complete, it is verified in place: its signature, its hash, that it is for this hardware, that its version is not older than allowed.
  3. Only then is the boot record changed, in one small atomic write (a record with a sequence number and a CRC, written beside the old one): "boot B, on trial".
  4. The device reboots into B.

The boot record is the only thing whose update must be atomic, and it is small enough to be. Everything else can be interrupted at any point and repeated.

The trial boot

A verified image can still fail: a bug that crashes during initialisation, a driver that does not work with this board's revision, a network stack that cannot reach the server. So the new image is not trusted until it says so itself:

            update written and verified
  A active  ─────────────────────────────▶  B pending ── boot ──▶ B on trial
     ▲                                                           │      │
     │          reset before confirming                          │      │ healthy for a while:
     └───────────────────────────────────────────────────────────┘      │ "confirm"
              (crash, watchdog, brown-out)                               ▼
                                                                   B active, confirmed
  • The bootloader, seeing a pending image, marks it on trial and boots it.
  • The new firmware runs its checks: it booted, its peripherals initialised, it reached the server, perhaps it ran for some minutes. Then it confirms: the record now says B is good.
  • If the device resets before that (a crash, the watchdog, a power cut), the bootloader finds the image still on trial and rolls back to A.

The watchdog is what makes this safe: a new image that hangs is reset by it, and the reset sends it back. MCUboot (the bootloader of Zephyr and many others) calls this the "test" and "confirm" swap; ESP-IDF's app rollback, Android's A/B updates and car ECUs' flash bootloaders work the same way.

#include <stdio.h>
#include <stdint.h>

typedef struct { uint8_t active, pending, trial; uint16_t version[2]; } slots_t;

/* The bootloader's decision at each boot: which slot to run, and what to record. */
static int boot(slots_t *s) {
  if (s->pending && s->trial) {                 /* the trial boot reset before confirming */
    s->active = (uint8_t)!s->active;
    s->pending = s->trial = 0;
    printf("rolled back to v%u\n", s->version[s->active]);
  } else if (s->pending) {
    s->trial = 1;                               /* the first boot of the new image */
  }
  printf("boot slot %c v%u%s\n", 'A' + s->active, s->version[s->active], s->trial ? " (trial)" : "");
  return s->active;
}

int main(void) {
  slots_t s = { 0, 0, 0, { 1, 0 } };
  boot(&s);                                     /* v1 from A */
  s.version[1] = 2; s.active = 1; s.pending = 1; /* v2 written and verified into B */
  boot(&s);                                     /* v2 on trial */
  boot(&s);                                     /* it crashed before confirming */
  return 0;
}

When to confirm

Confirming too early defeats the trial; too late, and an unrelated reset (a brown-out) rolls back a good update. A good confirmation means "this image can still update itself": it initialised everything, it reached the update server or completed a self-test, and it has run long enough for a slow failure to show. A product may also confirm only after its own health checks pass a second time, after the next reboot.

And confirmation is the moment to raise the anti-rollback counter, not installation: until the new image confirms, falling back to the old one must still be allowed.

Variations

  • Swap or no swap. Some bootloaders always run from slot A and swap the images (copying B to A, with a scratch area to survive power cuts); others run either slot, which needs position-independent images or one build per slot.
  • Delta updates send only the difference from the running image, which matters on slow, paid links (cellular, LoRa): the device rebuilds the new image in slot B from A and the patch, then verifies the result as usual.
  • External flash holds the second slot when internal flash has room for one image only; the image must then be verified again after it is copied in, since external flash is easy to tamper with.
  • A recovery image: a third, minimal, rarely updated firmware that can only download and install, for when both slots are bad.

Going deeper

Interview question: How would you design a firmware update so that a power failure at any point cannot brick the device?

Answer: Never overwrite the running image. Keep two slots: write the new image into the inactive one while the old one stays intact, so an interruption costs only a repeated download. Verify it completely in place, signature, hash, hardware compatibility and version, before anything refers to it. Switch with a single small atomic write of a boot record that has a sequence number and a CRC, written beside the previous record rather than over it, so that the bootloader always finds a valid one. Boot the new image on trial and roll back if it resets before confirming, with the watchdog enabled so that a hang also resets. Keep the bootloader out of the field update path, or update it with the same redundancy.

Interview question: What should a new firmware image check before it confirms itself after an update?

Answer: That it can still do the job of updating, and that it works on this device: it booted fully, initialised its peripherals and external devices, passed its own self-tests, reached the network and the update server or whatever channel can deliver a fix, and has run long enough for slow failures to appear. Only then it marks itself confirmed, and at that point the anti-rollback counter is raised to its version. Confirming immediately at boot would make the trial meaningless; never confirming would let any unrelated reset roll back a good image.

Explain it: In your own words, explain what the trial boot protects against that a signature check does not.

Answer: A signature proves the image came from the manufacturer and was not altered, but a genuine image can still be broken: a bug that crashes at start-up, an incompatibility with one hardware revision, a network stack that cannot reconnect. Without a trial, such an image would be the only one the device boots, and it could never be replaced remotely. The trial boot keeps the previous image as the default until the new one proves it can run and update itself, so a reset before that, from a crash, a hang caught by the watchdog or a power cut, returns the device to the image that worked.