Over-the-air updates and rollback
About this lesson
A device in the field cannot be plugged into your laptop, so its firmware must be replaced over the network: an over-the-air (OTA) update. Follow one on an ESP32 board, 4 MB flash running ESP-IDF. The new version is written into a second app slot while the old one keeps running, a small record called otadata is switched only at the very end, and with rollback enabled the new firmware gets one boot to prove itself. Ours crashes, and the bootloader brings the old version back by itself. The lesson “Joining Wi-Fi from an ESP32” explains how the board reaches the server; the lesson “Securing a device: TLS, secrets and secure boot” covers signing updates.
Two slots for one program
An over-the-air (OTA) update installs new firmware through the network, with no cable. Our ESP32's flash has two OTA app slots of 1 MB each. Version v1 runs from ota_0. A small record, otadata, says which slot to start.
flash (4 MB) nvs · otadata · phy_init · factory · ota_0 · ota_1 otadata 0xd000, 0x2000 bytes: which slot boots ota_0 0x110000, 1 MB v1, running ota_1 0x210000, 1 MB empty
Read the lesson step by step
Two slots for one program
An over-the-air (OTA) update installs new firmware through the network, with no cable. Our ESP32's flash has two OTA app slots of 1 MB each. Version v1 runs from ota_0. A small record, otadata, says which slot to start.
flash (4 MB) nvs · otadata · phy_init · factory · ota_0 · ota_1 otadata 0xd000, 0x2000 bytes: which slot boots ota_0 0x110000, 1 MB v1, running ota_1 0x210000, 1 MB emptyFetch v2 over Wi-Fi
v1 calls esp_https_ota(). It downloads the file v2.bin from the update server over Wi-Fi, using HTTPS, so the data is encrypted and the server proves who it is with its certificate.
line 1 esp_https_ota(&cfg); cfg .url = "https://example.com/v2.bin" .cert_pem = the server's certificate HTTPS TLS: encrypted, and the server is checkedWrite into the other slot
Each chunk that arrives is written into ota_1, the slot that is not running. v1 is never touched and keeps working the whole time. Inside, esp_https_ota calls esp_ota_begin, then esp_ota_write for every chunk.
next slot esp_ota_get_next_update_partition() -> ota_1 begin esp_ota_begin() erase ota_1 write esp_ota_write() chunk by chunk (4 drawn) ota_0 v1, still running, untouchedWhat if the power fails?
Suppose the battery dies halfway through. otadata still says ota_0, because it only changes at the very end. At the next power on the chip starts v1 as if nothing happened. The half-written slot is simply ignored.
power lost while writing ota_1 otadata still -> ota_0 next boot v1 from ota_0 ota_1 half written, ignored; the next update erases itCheck the whole image
When the last chunk is written, esp_ota_end checks the new image in ota_1. A broken or cut-short download fails the check, and the update stops there. otadata never changes, so v1 stays in charge.
end esp_ota_end() validates the image in ota_1 fail update refused, otadata unchanged, v1 runs on pass ready to switchFlip the pointer, restart
Only now does otadata change: esp_ota_set_boot_partition makes it point at ota_1, and the new image starts in the NEW state. esp_https_ota returns ESP_OK, and esp_restart() resets the chip.
set esp_ota_set_boot_partition(ota_1) otadata -> ota_1 state of ota_1: NEW line 2 esp_restart();One try for v2
At reset the bootloader runs first. It reads otadata and starts v2 from ota_1. Because v2 is NEW, it gets exactly one try: the bootloader marks it PENDING_VERIFY before starting it.
reset bootloader reads otadata -> ota_1 state NEW -> PENDING_VERIFY (booted only once) v2 runs but has not yet proved itselfv2 must prove itself
v2 should test itself early: join Wi-Fi, reach the server, read its sensor. If all is well it calls esp_ota_mark_app_valid_cancel_rollback(). The state becomes VALID and v2 is kept for good.
line 3 if (self_test_ok()) line 4 esp_ota_mark_app_valid_cancel_rollback(); PENDING_VERIFY -> VALID (rollback cancelled) else esp_ota_mark_app_invalid_rollback_and_reboot();Our v2 crashes first
But our v2 has a bug: it reads through a NULL pointer before the self-test ends. The chip faults, and by default ESP-IDF prints the registers and reboots. Line 4 never ran, so v2 is still PENDING_VERIFY.
bug NULL pointer read -> exception panic print registers and reboot (ESP-IDF default) line 4 never reached state still PENDING_VERIFYRoll back to v1
The bootloader finds v2 still PENDING_VERIFY: it had its one try. It marks v2 ABORTED and starts v1 from ota_0 again. The device fixed itself, and v1 can report the failure and wait for a better update.
reset bootloader: ota_1 is PENDING_VERIFY state PENDING_VERIFY -> ABORTED (never booted again) boot v1 from ota_0 device working again, no cable, no visit
5 quick questions
Choose one answer per question. You can change your choices before checking them.
01While v2 is being downloaded, where is it written?
ESP-IDF always writes the new image into the OTA slot that is not selected for booting (esp_ota_get_next_update_partition). v1 in ota_0 is never touched, so the device keeps working.
02The battery dies when ota_1 is half written. What runs at the next power on?
otadata only changes after esp_ota_end has checked the whole image and esp_ota_set_boot_partition is called. Until then it still says ota_0, so v1 boots as before.
03With rollback enabled, what state is a freshly installed image in when it boots for the first time?
It starts as NEW. The bootloader selects a NEW image only once and changes it to PENDING_VERIFY. The app must then confirm itself, or it will not be booted again.
04v2 crashes and resets before calling esp_ota_mark_app_valid_cancel_rollback(). What happens next?
A PENDING_VERIFY image is not selected on the next boot: its state becomes ABORTED and the previous working app runs. The device recovers by itself.
05When should v2 call esp_ota_mark_app_valid_cancel_rollback()?
Marking valid cancels the rollback for good. Do it once the firmware has shown it can still do the things it needs, especially reaching the network to get the next update; ESP-IDF advises running the check as early as possible.
Glossary
- OTA update
- Over-the-air update: new firmware delivered through a network connection instead of a cable.
- App slot (OTA partition)
- A region of flash that holds one complete firmware image. The ESP-IDF table in this lesson has two, ota_0 and ota_1, of 1 MB each.
- Partition table
- A small list stored in flash that names each region (nvs, otadata, the app slots), its type, offset and size.
- otadata
- A 8 KB flash partition that records which app slot the bootloader should start.
- Bootloader
- A small program that runs first after every reset. On the ESP32 it reads otadata and starts the chosen app slot.
- Rollback
- Going back to the previous firmware automatically when a new one fails to confirm itself.
- PENDING_VERIFY
- The ESP-IDF state of a new image during its one trial boot. If the chip resets in this state, the image becomes ABORTED.
- Anti-rollback
- An option that stores a security version in eFuse and refuses to boot older firmware, so an attacker cannot install a known-vulnerable version.
Notes and sources
- The card shows four lines. The full code also fills in the server address and certificate, and calls esp_ota_mark_app_invalid_rollback_and_reboot() when the self-test fails. esp_https_ota() runs esp_ota_begin, esp_ota_write, esp_ota_end and esp_ota_set_boot_partition for you.
- Rollback only works when the bootloader is built with CONFIG_BOOTLOADER_APP_ROLLBACK_ENABLE (in menuconfig). Without it the new image boots with no trial, and the confirm call is optional.
- The built-in table also has nvs, phy_init and a factory slot at 0x10000; the figure leaves them out. It needs at least 4 MB of flash. otadata is 2 flash sectors (0x2000 bytes); the figure only shows which slot it picks.
- PENDING is short for ESP_OTA_IMG_PENDING_VERIFY. There are also INVALID (set by the invalid-and-reboot call) and UNDEFINED. The figure draws the download as four chunks; the real code streams many small blocks.
- Rolling back to an old version can reopen an old security hole. ESP-IDF's anti-rollback option stores a secure version in eFuse and refuses older apps; the lesson on device security covers signing and secure boot.
- Espressif, ESP-IDF Programming Guide (ESP32): Over The Air Updates (OTA), including App rollback and Anti-rollback
- Espressif, ESP-IDF Programming Guide (ESP32): Partition Tables
- Espressif, ESP-IDF Programming Guide (ESP32): ESP HTTPS OTA
- Espressif, ESP-IDF Programming Guide (ESP32): Fatal Errors (panic handler)
Quiz Unlocks after the last step.