Thermostat fleet: boot the update or roll back
Decide which firmware slot to boot, trying a new update a few times before rolling back. A hard embedded C problem (OTA updates and rollback), run and graded…
hard · OTA updates and rollback · ota · bootloader · rollback · product
Product: Smart thermostat fleet (Smart home)
Requirement. A firmware update reaches 50,000 thermostats. If the new version crashes on some of them, those units must recover by themselves: nobody climbs into an attic to reflash one.
The bootloader keeps two slots. pending is the slot of an update just installed and not yet confirmed, or -1. Decide which slot to boot:
- If
pendingis a slot whose image is OK, that is not confirmed, and has fewer than 3 attempts: count this attempt (attempts++) and boot it. - Otherwise boot the confirmed slot with a good image and the highest version.
- If there is none, return -1 (recovery mode).
Starter code
int choose_slot(slot_hdr_t s[2], int pending) {
if (pending >= 0) return pending;
return s[0].version > s[1].version ? 0 : 1;
}Example tests
Trying an update, and a normal boot
slot_hdr_t s[2] = { { 1, true, true, 0 }, { 2, true, false, 0 } };
int a = choose_slot(s, 1);
slot_hdr_t t[2] = { { 1, true, true, 0 }, { 2, true, true, 1 } };
int b = choose_slot(t, -1);
printf("%d %d %u\n", a, b, s[1].attempts);Expected output:
1 1 1An update that never confirms
slot_hdr_t s[2] = { { 1, true, true, 0 }, { 2, true, false, 0 } };
for (int i = 0; i < 4; i++) printf("%d ", choose_slot(s, 1));
printf("\n");Expected output:
1 1 1 0Learn it first
- OTA updates and rollback · A/B slots, delta updates, rollback and signing.
More OTA updates and rollback problems
- Pick the slot to boot · Boot a new update on trial, and roll back if it never confirms itself.
- Is this image safe to jump to? · Check a firmware image's header, bounds, CRC and vector table before a bootloader starts it.
- Receive an update in chunks · Write update chunks to the spare slot in order, ignore resends, refuse gaps and overruns, and check the CRC at the end.
- Resume an update after a power cut · Keep download progress in an append-only flash journal, and find where to resume even after a torn write.
- Anti-rollback with one-time fuses · Refuse old, vulnerable firmware by its security version, counted in one-time-programmable fuses.