ESP-IDF OTA rollback, and why it did not roll back

You turned on rollback, sent an update that broke the WiFi, and the device stayed broken. That is common, and it is almost always one of five things. Here is how rollback works and how to check each of them.

How rollback works

With CONFIG_BOOTLOADER_APP_ROLLBACK_ENABLE=y, a freshly written OTA image goes through these states, kept in the otadata partition:

  1. ESP_OTA_IMG_NEW: written and selected by esp_ota_set_boot_partition(), not booted yet.
  2. ESP_OTA_IMG_PENDING_VERIFY: the bootloader set this on the first boot of the new image. The image is on trial.
  3. ESP_OTA_IMG_VALID: the application called esp_ota_mark_app_valid_cancel_rollback(). It is kept from now on.
  4. ESP_OTA_IMG_ABORTED: the chip restarted while the image was still pending. The bootloader marks it aborted and boots the previous image.

Your application can also give up on purpose with esp_ota_mark_app_invalid_rollback_and_reboot().

The pattern in the application:

const esp_partition_t *running = esp_ota_get_running_partition();
esp_ota_img_states_t state;

if (esp_ota_get_state_partition(running, &state) == ESP_OK &&
    state == ESP_OTA_IMG_PENDING_VERIFY) {
    if (self_test_passed()) {      /* your test: see below */
        esp_ota_mark_app_valid_cancel_rollback();
    } else {
        esp_ota_mark_app_invalid_rollback_and_reboot();
    }
}

Why it did not roll back

1. The bootloader on the device does not have rollback

Rollback is a bootloader feature. An OTA update replaces the application, not the bootloader. If the devices were flashed with a bootloader built before you turned the option on, they will never roll back, whatever the new application does. Check the bootloader that is actually on the device: CONFIG_BOOTLOADER_APP_ROLLBACK_ENABLE must have been set when that bootloader was built and flashed over USB. For devices already in the field, the fix is a USB visit or a bootloader update, which ESP-IDF supports but which can brick a device if power fails during it.

2. The image was confirmed too early

Calling esp_ota_mark_app_valid_cancel_rollback() at the top of app_main confirms an image before it has shown that it works. Most broken updates fail later: the WiFi does not connect, the TLS certificate is wrong, the server rejects the device. Confirm only after the test that matters to you, usually "the new firmware reached my server".

On Arduino-ESP32 the core confirms the image for you at start-up. To decide yourself, define

extern "C" bool verifyRollbackLater() { return true; }

and call esp_ota_mark_app_valid_cancel_rollback() from your sketch when the test has passed.

3. The broken image never restarts

Rollback happens only on a restart. An image that hangs, or sits in a loop waiting for a WiFi that never comes, stays on trial forever. Give it a deadline:

static void confirm_deadline(void *arg) {
    /* still pending after 5 minutes: give up */
    esp_ota_mark_app_invalid_rollback_and_reboot();
}

esp_timer_handle_t t;
esp_timer_create_args_t args = { .callback = confirm_deadline, .name = "ota-deadline" };
esp_timer_create(&args, &t);
esp_timer_start_once(t, 5 * 60 * 1000000ULL);
/* call esp_timer_stop(t) when you confirm the image */

Also keep the task watchdog set to panic (CONFIG_ESP_TASK_WDT_PANIC=y), so a task stuck at start-up restarts the chip.

4. The previous image is gone

Rollback needs a valid image in the other slot. If you sent two updates in a row and the first one was confirmed, the slot now holds that one, not the version from before. And if the device ran from the factory partition, only that is left to go back to. esp_ota_check_rollback_is_possible() tells you whether there is anything to roll back to.

5. You were thinking of anti-rollback

Anti-rollback (CONFIG_BOOTLOADER_APP_ANTI_ROLLBACK) is a different feature, and close to the opposite: it stops a device from going back to an older version, using a secure_version stored in eFuse. It is meant for security fixes, so an attacker cannot install an old, vulnerable image. It uses eFuse bits that cannot be reset, so only turn it on once you know your release process.

Test it before the first device ships

How Pinscope does it

The Pinscope component confirms a new image when it first reaches the Pinscope server after the update, and restarts the device if that has not happened within five minutes, so an update that cannot reach the server undoes itself. The dashboard shows which version each device runs, and a fleet update goes in waves that stop at the first device that does not come back. It still needs the rollback option in your bootloader.

Related: ESP32 OTA updates over the internet, staged OTA rollouts for a fleet and ArduinoOTA over the internet.

Try it

Three devices are free, and no card is needed. Create an account, or read the guide for your platform: ESP32, Zephyr, Raspberry Pi Pico, Linux, ESP8266.

More