Overview
Curated: · Written: · Reviewed:
Firmware & Bootloaders
In one line
The bootloader decides which firmware may run, how the device recovers from an interrupted or faulty update, and where every image lives in flash and RAM. Reach for it whenever a question involves secure boot, A/B or swap updates, rollback, or the handoff into application code.
Key definitions
- Root of trust: the immutable first stage (mask ROM or protected flash) that verifies the next stage against a key or hash fixed at manufacture.
- Authentication vs. integrity: a signature proves the signing key holder produced the image; a checksum or CRC only detects accidental corruption and an attacker can recompute it.
- Anti-rollback counter: a monotonic value in OTP, eFuse or a wear-leveled protected region, raised only after a confirmed boot. A counter in RAM is not anti-rollback.
- Confirm window: the period (30 s in the reference design) after which the new image must pass sensor and comms health checks, or the bootloader reverts.
- Handoff contract: the state the application expects at entry: VTOR, MSP, interrupts disabled, SYSCLK, watchdog, cache and MPU.
Formulas and complexity
CRC32 collision probability against accidental flash errors, for any image:
That figure holds only against random corruption. An attacker who recomputes the CRC defeats it completely.
How it works
- The ROM checks the second-stage bootloader against the burned-in key or hash and jumps only on success; otherwise it enters recovery.
- The second stage parses the manifest bounds-checked, verifies the hash and signature, then checks hardware identity, version, dependencies and the anti-rollback counter.
- It selects the slot named by power-fail-safe metadata and sets VTOR, MSP and the clock state the application expects.
- After boot, the application must pass its health checks inside the confirm window; only then does the slot become confirmed and the counter advance.
- Any failure at any step falls back to the last confirmed slot or to bounded, authenticated recovery.
When to use and when not to
| Choice | Use when | Avoid when |
|---|---|---|
| A/B (dual bank) | Flash allows a second full slot and you need a bootable image intact through power loss | Flash is too small for two images |
| Swap with staging | Flash is tight and you can reserve scratch at least the largest erase unit plus a trailer | Scratch is smaller than the erase unit being moved |
| Overwrite, XIP or recovery-only | Recoverability is not a requirement | Any field update that must survive a bad image |
| Confirm after health checks | The app can measure sensor and comms health | You only know main() was reached |
| Transport trust (TLS, signed CDN) | Added to device-side verification | Used as a replacement for it |
Traps interviewers probe
- CRC as authentication. An attacker who edits the payload recomputes the CRC and passes. Fix: verify an ECDSA or RSA signature over a hash of the image, with the key in ROM or a secure element.
- Integer wrap in manifest bounds.
offset + lengthin uint32_t wraps past 2^32. Fix: checklength <= total_sizeandoffset <= total_size - lengthbefore the addition, and validate against the containing object. - Anti-rollback in RAM or in a writable version string. A glitch reboot restores the old value. Fix: keep the counter in OTP, eFuse or a protected wear-leveled region and advance it only after a confirmed boot.
- Confirm on main(). A firmware that hard-faults in the 31st second loops on an unconfirmed slot. Fix: confirm after 30 s of explicit sensor and comms checks.
- Handoff by a bare branch. The application inherits the bootloader's stack and clocks. Fix: set MSP and VTOR to the application's values, disable interrupts, and normalize the PLL, caches and watchdog before the jump.
Interview tips
Opening. Name the trust boundary first: root of trust, signature verification, anti-rollback, and the production invariant that after any reset the device runs only authorized, compatible firmware or enters bounded recovery.
Expected follow-ups.
- Where does the anti-rollback counter physically live, and when is it advanced?
- What happens on power loss at each program and erase boundary?
- How do you rotate a key without bricking devices that missed the first update?
- How does the bootloader update itself safely?
- Why does the linker script have to agree with the image header and the bootloader?
30-second outline. Verification is a signature over a hash of the manifest and image, checked on device before every boot. Rollback is a separate monotonic counter in OTP, advanced only after a health-checked confirm. A/B keeps the last good slot intact, and power-fail-safe metadata names the active slot. The handoff sets VTOR, MSP and clocks explicitly. Failures fall back to the last confirmed slot or to authenticated recovery, and retained evidence records each decision.
Feedback on this overview
Log in free to save your progress and keep bookmarks and notes for a quick revision before your interview.