Two slots
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.
Download and switch
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.
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.
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.
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.
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.
Prove it or roll back
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.
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.
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.
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.
What happens
;; step 1/10 · two-slots 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
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.