Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Heuristic:78 Xiaozhi esp32 Rollback Protection

From Leeroopedia
Knowledge Sources
Domains Embedded, Firmware_Update
Last Updated 2026-10-17 00:00 GMT

Overview

ESP-IDF's OTA rollback mechanism prevents a device from bootlooping on a bad firmware by requiring explicit validation before the new partition becomes permanent.

Description

After an OTA update, the new firmware boots in `ESP_OTA_IMG_PENDING_VERIFY` state. If the firmware crashes or fails to initialize, the ESP-IDF bootloader automatically rolls back to the previous partition on the next boot. The application must explicitly call `esp_ota_mark_app_valid_cancel_rollback()` to confirm the new firmware is healthy. In XiaoZhi, this happens in `Ota::MarkCurrentVersionValid()`, which is called after the device successfully connects to the server and enters the `idle` state.

The factory partition is exempt — running from `factory` skips rollback marking entirely.

Usage

Use this heuristic when designing the OTA validation flow. Call `MarkCurrentVersionValid()` only after the device has proven it can operate normally — e.g., after network connection and server registration succeed. Never mark valid at boot; always defer to a "known good" checkpoint.

The Insight (Rule of Thumb)

  • Action: After OTA, boot into `PENDING_VERIFY` state. Only call `esp_ota_mark_app_valid_cancel_rollback()` after the device reaches a known-good state (server connection established).
  • Value: Factory partition: skip marking. OTA partition: check `esp_ota_get_state_partition()`, if `PENDING_VERIFY`, mark valid.
  • Trade-off: If the application never marks valid (e.g., crashes before reaching idle), the bootloader rolls back on the next reboot. This wastes one boot cycle but prevents a bricked device.
  • Compatibility: ESP-IDF OTA rollback is supported on all ESP32 variants. Requires dual-partition or factory+OTA partition layout.

Reasoning

A bad firmware update (corrupted image, incompatible partition table, crash during init) would otherwise leave the device in a bootloop — the new firmware boots, crashes, reboots, crashes, forever. The rollback mechanism breaks this cycle: the new firmware gets one chance to prove itself. If it crashes before calling `mark_app_valid_cancel_rollback`, the bootloader reverts to the previous partition. XiaoZhi defers the validation call until after server connection, ensuring that a firmware that boots but can't connect (e.g., broken WiFi driver, broken protocol) will also roll back.

The factory partition exemption is intentional — the factory partition is the known-good fallback and should never be marked invalid.

Code Evidence

Rollback marking from `main/ota.cc:260-278`:

void Ota::MarkCurrentVersionValid() {
    auto partition = esp_ota_get_running_partition();
    if (strcmp(partition->label, "factory") == 0) {
        ESP_LOGI(TAG, "Running from factory partition, skipping");
        return;
    }

    ESP_LOGI(TAG, "Running partition: %s", partition->label);
    esp_ota_img_states_t state;
    if (esp_ota_get_state_partition(partition, &state) != ESP_OK) {
        ESP_LOGE(TAG, "Failed to get state of partition");
        return;
    }

    if (state == ESP_OTA_IMG_PENDING_VERIFY) {
        ESP_LOGI(TAG, "Marking firmware as valid");
        esp_ota_mark_app_valid_cancel_rollback();
    }
}

OTA partition boot selection from `main/ota.cc:398`:

    err = esp_ota_set_boot_partition(update_partition);

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment