Simvik

The A/B boot decision

Choose the slot to boot from the boot record: a trial for a new image, a rollback when the trial reset, and never an invalid slot. A medium embedded C…

medium · A/B updates, trial boots and rollback · bootloader · ota · state-machine

The boot record says which slot is active, whether its image is new (pending) and whether it already had its trial boot (trial), and whether each slot holds a valid (verified) image:

typedef struct { uint8_t active, pending, trial; uint8_t valid[2]; } boot_record_t;

Write boot_select(r), called once at every boot. It updates the record and returns the slot to boot (0 or 1), or -1 when there is none:

  1. A pending image that already had its trial (pending and trial set) reset before it confirmed: switch active to the other slot and clear pending and trial.
  2. Otherwise a pending image gets its trial now: set trial.
  3. Then, if the active slot is not valid, switch to the other slot (clearing pending and trial) if that one is valid; if neither is, return -1.

Starter code

int boot_select(boot_record_t *r) {
  return r->active;
}

Example tests

A new image in slot B: its trial, then a reset before it confirmed

boot_record_t r = { 1, 1, 0, { 1, 1 } };
boot(&r);
boot(&r);
boot(&r);

Expected output:

boot 1 (active 1 pending 1 trial 1)
boot 0 (active 0 pending 0 trial 0)
boot 0 (active 0 pending 0 trial 0)

A confirmed image boots as it is

boot_record_t r = { 0, 0, 0, { 1, 0 } };
boot(&r);

Expected output:

boot 0 (active 0 pending 0 trial 0)

Learn it first