How dual-slot bootloaders, the confirm/revert lifecycle, and a monotonic counter turn "works on my bench" into "survives the field."
Most firmware engineers get the OTA upload working on the first or second try. The image transfers, the device reboots, the new firmware runs. Then a week later a buggy build ships over the air, the device crashes in a loop, and the only recovery is a truck roll. The upload was never the hard part. The rollback is.
This post is the pipeline the tutorials skip: how MCUboot's swap-and-confirm lifecycle and ESP-IDF's dual-partition OTA actually protect you, where each one silently fails, and the one test that tells you whether your rollback works before your fleet finds out for you.
The three ways an OTA bricks a device
Every bricked device traces back to one of three causes:
- Power loss or reset during the write. If your updater overwrites the only copy of working firmware in place, an interruption leaves you with no valid image. Overwrite-only is simple — and, per the MCUboot documentation, it has no fail-safe: if power fails mid-copy, the device may brick.
- A bad image that boots. The update transferred completely, the signature was valid, and the firmware still hangs on a peripheral init. Without a test-then-confirm step, the device is now a brick with a valid signature.
- A malicious or stale rollback. RFC 9019, the IETF's firmware-update architecture, is blunt: a firmware update mechanism is "essentially authorized remote code execution" — whoever can get an image accepted controls the device.
A safe OTA design answers all three: staging in a second slot, boot-test with automatic revert, and anti-rollback enforcement in the bootloader.
The core pattern: stage, test, confirm
Every serious embedded OTA scheme — MCUboot on Zephyr/nRF, ESP-IDF's OTA on ESP32, Mender on Linux-class devices, and the IETF SUIT architecture — converges on the same state machine:
+-------------+ download +------------------+
| Slot 0 / | ---------> | Slot 1 / |
| ota_0: | | ota_1: |
| running FW | | staged update |
+-------------+ +--------+---------+
| reboot
v
+-----------------------+
| Bootloader swaps / |
| points at new image |
+-----------+-----------+
|
v
+-----------------------------------+
| New image boots UNCONFIRMED |
| App runs self-test / health check|
+-----------+---------------+-------+
pass -+ +--- fail / crash / watchdog
| |
v v
+-------------------+ +-----------------------+
| CONFIRM image | | Next boot: bootloader |
| (now permanent) | | REVERTS automatically |
+-------------------+ +-----------------------+
The new image is guilty until proven innocent. If the application never explicitly confirms its own health, the bootloader treats the boot as a failed test and swaps back on the next reset. This is the entire mechanism — and it only protects you if you wire up the confirm step.
MCUboot (Zephyr, nRF): swap mode and the confirm call
MCUboot splits flash into two slots: slot 0 runs the current image, slot 1 stages the update. It offers three strategies — overwrite-only, swap-using-scratch, and swap-move. The choice matters: overwrite-only gives you no fail-safe, while the swap modes track progress in metadata so an interrupted swap can resume or revert, which is what makes rollback possible at all.
The lifecycle in your application:
- Write the new image to slot 1 (BLE via SMP/mcumgr, UDP, or your own transport).
- Call
boot_request_upgrade(BOOT_UPGRADE_TEST)and reboot. - MCUboot swaps the slots and boots the new image in an unconfirmed (test) state.
- Your application validates itself — sensor reads sane, radio associates, filesystem mounts — then calls
boot_write_img_confirmed(). The MCUboot docs state it plainly: it's up to the image to mark the primary slot "image ok" before the next reboot, otherwise the bootloader reverts the application. - If step 4 never happens, the next reboot triggers an automatic swap back to the previous image.
Two sharp edges to know about:
Corruption can hang the bootloader. There are open reports of MCUboot hanging at boot with corrupted staged images rather than cleanly reverting. Test the unhappy path.
Watchdogs. MCUboot's release notes record a fix where the watchdog wasn't fed during long flash erase operations, timing out and preventing firmware updates from being possible. If your device runs a watchdog, the bootloader must be fed during swap.
Downgrade protection: MCUboot can be built to accept only newer firmware versions, preventing rollback to older, vulnerable images.
ESP-IDF (ESP32): dual partitions and the confirm call
ESP-IDF implements the same pattern at the partition level: ota_0/ota_1 in the partition table, esp_ota_begin()/esp_ota_write() streaming into the inactive partition, and a pending state until the app calls esp_ota_mark_app_valid_cancel_rollback().
A checkpoint like "successful Wi-Fi connection cancels the rollback and marks the image valid" — used in the ESP-Journal's walkthrough — is the right shape of health check: something your firmware provably cannot do when broken.
Anti-rollback on ESP32: eFuse and its limits
Real downgrade protection burns a monotonic secure_version into eFuse — bits programmed to 1 can never go back to 0 — checked during boot and during OTA. The gotchas, from Espressif's docs:
- The field is limited to 32 bits — only 32 anti-rollback increments. Budget them.
- Works only if the eFuse encoding scheme is
NONE. - The partition table must not contain a
factoryortestpartition.
This mirrors SUIT's rule from RFC 9124: the manifest carries a sequence number, not a version string, and it must only increase. A version string is advisory; a monotonic counter is enforced.
Signing: the fleet's master key
None of this matters if the device can't tell a genuine image from an attacker's. RFC 9019 assumes updates cross untrusted infrastructure — CDNs, gateways — so the device verifies the manifest signature and image digest itself.
- Asymmetric signatures. A fleet-wide symmetric key means one extracted key compromises every device; RFC 9019 allows symmetric MACs only for the most constrained devices.
- Sign the manifest that carries the digest, verify the signature, then check the image against the digest inside the signed manifest.
- Keep the private key in an HSM; plan rotation before you ship.
- On ESP-IDF: secure boot means the server hosts the signed image, and flash encryption is handled on-device during write — plain HTTPS transport suffices.
Linux-class devices: Mender's state machine
If your device runs Linux, the same lifecycle is expressed as states: ArtifactInstall, ArtifactReboot, ArtifactVerifyReboot, ArtifactCommit, ArtifactRollback, ArtifactFailure. One sobering line from Mender's own docs: if ArtifactVerifyRollbackReboot keeps failing, "the system may be left in a permanently inconsistent state." Even mature platforms have a cliff edge — your rollback verification must eventually give up and boot something known-good.
The test that tells you the truth
One act of sabotage validates the whole pipeline:
- Ship v1.0, confirm it.
- Build v1.1 that deliberately omits the confirm call.
- Push it OTA, let it boot, let it "fail."
- Reboot and verify the device comes back on v1.0.
If step 4 works in the lab, your field rollback works. If you never run this test, rollback is a hypothesis. Also run: pull power at randomized points during the slot-1 write and during the swap. MCUboot's swap metadata is designed to tolerate this, but the release notes document real edge-case bugs — an interrupted swap-move that bricked devices when the primary image was padded, and an interrupted scratch-erase that corrupted the primary image.
Checklist
| Layer | Must have | Common failure |
|---|---|---|
| Flash layout | Two slots + swap scratch/move region | Single slot, overwrite in place → power-loss brick |
| Bootloader | Verify signature before boot; swap modes, not overwrite-only | Signature checked only by the updater app |
| Application | Confirm only after a real health check | Confirm unconditionally in main() → bad images stick |
| Health check | Something the firmware provably can't do when broken | Checking only "did we boot" |
| Anti-rollback | Monotonic counter (eFuse) or enforced newer-version check | Version string compared as text |
| Watchdog | Fed during long erase/swap operations | Watchdog resets mid-update → update never completes |
| Signing | Asymmetric, key in HSM, manifest carries image digest | Fleet-wide symmetric key |
| Server | Signed images over HTTPS; staged rollouts | Unsigned images on plain HTTP |
| Test | Confirm-omission test + power-cut fuzzing | Happy-path-only testing |
Why this is table stakes now
Regulation has caught up with the engineering. ETSI EN 303 645 and the EU Cyber Resilience Act both expect secure updates for a device's support period — and CRA vulnerability-handling requirements apply even to offline devices reached via USB stick: the device must verify the signature before installing, period. Rollback protection and signed A/B updates are moving from best practice to audit item.
The upload was never the hard part. Confirm-after-health-check, automatic revert, a monotonic counter, and a sabotage test are what separate an OTA pipeline that works on your bench from one that survives 10,000 devices you can't reach.
What broke your first OTA in the field — power loss, a bad image, or something weirder?

Top comments (0)