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:
- A pending image that already had its trial (
pendingandtrialset) reset before it confirmed: switchactiveto the other slot and clearpendingandtrial. - Otherwise a pending image gets its trial now: set
trial. - Then, if the active slot is not valid, switch to the other slot (clearing
pendingandtrial) 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
- A/B updates, trial boots and rollback · Two slots, verifying in place, an atomic boot record, the trial boot and when to confirm.