C1244 of 55
0 XP
  1. Hook
  2. Play
  3. Predict
  4. Name it
  5. Check
  6. Explain

C12 · Lesson 44 · Bootloaders and firmware updates

Replacing a device's program in the field without ever leaving it unable to start

Every device shipped with a bug needs a way to receive new firmware, often over a serial link, USB or radio, from a user who will unplug it at the worst moment. The program that makes this possible is the bootloader: a small, rarely changed piece of code at the reset address that decides which application to run, checks that it's intact, and can rewrite the rest of the flash. Lesson 43's rules for flash apply with higher stakes: erasing takes whole pages and tens of milliseconds, and a power cut halfway through an erase must not leave a device that can't boot. The standard answer is two copies of the application and a commit flag written last.

  • The time to update a 200 KB image, split into transfer, erase and programming, against the link's speed in bits per second. The flash has 4 KB pages that take 20 ms to erase and 256-byte rows that take 0.5 ms to program.
  • Over a slow UART, transfer dominates; over USB or a fast link, the flash itself becomes the limit, about 1.4 s however fast the data arrives.

Predict

A device has one application slot. Its bootloader erases the slot and then writes the new image as it arrives over a radio link. Power fails halfway through. What happens at the next reset?

Name it

What a bootloader does

At reset, the bootloader runs first. It:

  1. checks whether an update is requested (a flag in flash, a button held at power-up, a command over the link);
  2. if so, receives the new image, erasing and programming flash page by page;
  3. verifies the application's integrity, with a CRC (lesson 33) or, for secure boot, a cryptographic signature checked against a public key stored in the bootloader;
  4. sets the processor's vector table and stack pointer for the application, and jumps to it.

The bootloader is kept small and almost never updated itself, because a failed bootloader update is the one mistake that can't be repaired in the field.

Flash in practice

Flash erases in pages (often 1 to 128 KB) to all 1s, programs in smaller rows or words, and endures 10 000 to 100 000 erase cycles. An update of S bytes takes about

t≈10Sbaud+⌈SP⌉terase+SRtprog

for a UART with 10 bits per byte, page size P and row size R.

A/B updates

Divide the application area into two slots. The bootloader runs whichever slot is marked active. An update writes the inactive slot, verifies it, and only then flips a small commit record to mark it active, one atomic write as in lesson 43's power-safe records. A power failure at any moment leaves at least one valid, committed image. Many systems add a trial boot: the new image must confirm itself (for example, after its self-test passes) within a few boots, or the watchdog's resets (lesson 41) make the bootloader roll back. The cost is flash: each slot can be only half the space left after the bootloader and records.

Linking for a bootloader

The application is linked to start at its slot's address, not at the reset vector, with its own vector table there. On processors that can relocate the vector table, the bootloader points it at the application before jumping; on others, the bootloader forwards interrupts.

Worked example

A microcontroller has 512 KB of flash. The bootloader takes 32 KB and the commit records 4 KB. How large can an application be with A/B slots, and with a single slot?

Show the solution
  1. Space left for the application. 512−32−4=476KB
  2. A/B slots. Two slots share it, 238 KB each.
  3. A single slot. It could use all 476 KB, twice as much, which is the price of never being unbootable.
  4. Compromises. Some designs compromise with compression or with a small recovery image instead of a full second copy.

Answer 238 KB with A/B slots, and 476 KB with a single slot.

Worked example

A 200 KB image is sent over a 115 200-baud UART into flash with 4 KB pages (20 ms erase) and 256-byte rows (0.5 ms to program). How long does the update take?

Show the solution
  1. Transfer. t=200×1024×10115200s=17.8s
  2. Erase. ⌈200/4⌉=50 pages: 50×20ms=1.0s
  3. Program. 800 rows: 800×0.5ms=0.4s
  4. Total. About 19.2 s, almost all of it the link.
  5. Over USB. Over a 12 Mb/s USB link the transfer would take 0.17 s and the flash's 1.4 s would dominate.

Answer about 19.2 s, almost all of it the link.

Worked example

A device runs a bootloader that checks a CRC-32 over the 200 KB application at every boot, at about 10 cycles per byte on a 48 MHz core. How much does that add to start-up, and how often can it be updated if each update erases each page of the slot once and the flash endures 10 000 cycles?

Show the solution
  1. CRC time per boot. t=204800×1048×106s=43ms usually acceptable.
  2. Erases per update. Each update erases every page of one slot once; alternating between two slots, each slot is erased on every second update.
  3. Updates the flash supports. About 20,000, far more than a product will ever receive.
  4. Where endurance matters. Endurance is a concern for data logging (lesson 43), and hardly ever for firmware.

Answer 43 ms per boot, and about 20,000 updates.

Check

A device has 512 KB of flash, a 16 KB bootloader and 8 KB of records. How large can each of two A/B application slots be?

Check

How long does it take to send a 200 KB image over a UART at 9600 baud with 10 bits per byte?

Write units the way you normally would: 1.5 s and 1500 ms are the same answer.

Check

Erasing room for a 210 KB image in flash with 8 KB pages, each taking 40 ms, takes how long?

Write units the way you normally would: 1.5 s and 1500 ms are the same answer.

Check

A bootloader checks a CRC over 64 KB at 4 cycles per byte on a 200 MHz core. How long does that take?

Write units the way you normally would: 1.5 ms, 1500 µs and 0.0015 s are the same answer.

Check

A data logger shares its flash pages with firmware updates. Each update erases every page of one A/B slot once; the flash endures 10000 erase cycles per page.

(a)

How many updates can the device take before a slot's pages wear out, alternating slots?

(b)

At 2 updates per day, how many years is that?

Check

In an A/B update, why is the commit record written only after the new image has been fully written and verified?

Check

Simulate power failures during A/B updates. State: two slots, each None (erased), 'partial', or a version number; and active, the committed slot index. An update to version v runs these steps in order on the inactive slot: erase (slot becomes None), write (becomes 'partial'), finish (becomes v), commit (active switches to it). Write update(state, v, fail_at) that performs steps until index fail_at (0 to 4; 4 means no failure), and boot(state) returning the version that would run, or None if the active slot isn't a complete version. Starting from slots [1, None], active 0, apply 200 updates with fail_at = rng.randrange(5) (rng = random.Random(seed)) and version numbers 2, 3, ...; set bricked (how many times boot returned None after an update) and final (the running version at the end).

Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.

Explain why

A product's firmware update over Bluetooth occasionally fails when users walk away from their phones mid-update. Some devices come back up on the old firmware; a few never boot again. Explain what the bootloader must be doing differently in the two cases, and how to make every device recover.

Practice

Keep practicing

8 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.