Simvik

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:

  1. If pending is a slot whose image is OK, that is not confirmed, and has fewer than 3 attempts: count this attempt (attempts++) and boot it.
  2. Otherwise boot the confirmed slot with a good image and the highest version.
  3. 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 1

An 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 0

Learn it first

More OTA updates and rollback problems