ENع
←OTA updates
Lesson 51 · Connectivity and the capstone node

Over-the-air updates and rollback

14 min10 steps5 questionsAdvanced
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.

You'll learn to

Tick what you feel confident about. These self-checks do not affect your lesson score.

Fig 51 · Interactive
01 / 10

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
Loading figure…
or ← →
Read the lesson step by step
  1. 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
  2. Fetch 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 checked
  3. Write 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, untouched
  4. What 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 it
  5. Check 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 switch
  6. Flip 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();
  7. 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 itself
  8. v2 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();
  9. 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_VERIFY
  10. Roll 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

Quiz Unlocks after the last step.