AN-001 application note

Dual-bank bootloader: notes on a fail-safe update path

How an A/B image layout, a tiny boot-state record and a watchdog give you firmware updates that can't brick a device.

PUBLISHED ~1 minBY RaghulSERIES Bootloader from scratch

A field update has exactly one job: never leave the device unbootable. The simplest design that reliably achieves that is two application slots and a bootloader that only trusts an image after it has proven itself.

Flash layout

0x0800_0000  ┌───────────────┐
             │ Bootloader    │  32 KB, write-protected
0x0800_8000  ├───────────────┤
             │ Boot state    │   2 sectors, wear-levelled
0x0801_0000  ├───────────────┤
             │ Slot A        │ 448 KB
0x0808_0000  ├───────────────┤
             │ Slot B        │ 448 KB
             └───────────────┘

The trial-boot handshake

  1. The application downloads the new image into the inactive slot and verifies its signature.
  2. It writes state = TRIAL, slot = B, attempts = 0 and resets.
  3. The bootloader increments attempts, starts the watchdog and jumps to slot B.
  4. The new firmware runs its self-test, then writes state = CONFIRMED.
  5. If the watchdog fires or attempts > 3, the bootloader rolls back to slot A.
typedef struct {
    uint32_t magic;
    uint8_t  active_slot;
    uint8_t  state;      /* CONFIRMED, TRIAL */
    uint8_t  attempts;
    uint8_t  _rsvd;
    uint32_t crc32;
} boot_state_t;

What I’d do differently

  • Keep the boot-state record in two alternating sectors so a power cut mid-erase can’t lose it.
  • Verify the image signature in the bootloader too, not just in the application.
Rate article

Found a mistake? Let me know.

⌁ Comments & reactions

Comments are not configured yet.