Principle:78 Xiaozhi esp32 OTA Firmware Update
| Knowledge Sources | |
|---|---|
| Domains | Embedded_Systems, Firmware_Update, Security |
| Last Updated | 2026-10-17 00:00 GMT |
Overview
A firmware update mechanism for embedded devices that downloads a new firmware image from a server, validates its integrity, writes it to an inactive flash partition, and atomically switches the boot partition, enabling field updates with automatic rollback on failure.
Description
Over-The-Air (OTA) firmware update is a critical capability for deployed embedded devices. The XiaoZhi OTA system uses ESP-IDF's dual-partition OTA mechanism:
- Version Check: The device queries an OTA server endpoint with its current firmware version, device ID, and system info. The server responds with available firmware version, URL, and whether the update is forced.
- Download and Flash: The firmware binary is downloaded via HTTP and streamed to the inactive OTA partition in 4KB chunks using
esp_ota_write(). The image header is validated before flashing begins. - Activation: Before firmware update, the device must complete server activation using an HMAC-SHA256 challenge-response, proving its identity.
- Boot Partition Switch: After successful flash,
esp_ota_set_boot_partition()sets the update partition as the next boot target. - Rollback Protection: On next boot,
MarkCurrentVersionValid()callsesp_ota_mark_app_valid_cancel_rollback()to confirm the new firmware works. If the device fails to call this (e.g., crashes on boot), the bootloader rolls back to the previous partition.
The OTA system also handles asset updates: speech recognition models, fonts, and UI themes stored on a separate flash partition, downloaded and applied after firmware update.
Usage
Use this principle when:
- Deployed devices need remote firmware updates without physical access
- Rollback protection is required (automatic revert on boot failure)
- Firmware and asset updates must be atomic and validated
- Device activation (challenge-response) must precede firmware download
Theoretical Basis
The dual-partition OTA uses an A/B partition scheme:
Flash Layout:
┌──────────────────┐
│ Factory/OTA_0 │ ← Current boot (factory_0 or ota_0)
├──────────────────┤
│ OTA_1 │ ← Inactive (target for update)
├──────────────────┤
│ Assets │ ← SR models, fonts, themes
└──────────────────┘
Update Flow:
1. esp_ota_get_next_update_partition() → OTA_1
2. esp_ota_begin(OTA_1, OTA_WITH_SEQUENTIAL_WRITES)
3. Loop: HTTP read 4KB → esp_ota_write(OTA_1, buf)
4. Validate: esp_image_header_t + esp_app_desc_t
5. esp_ota_end(OTA_1)
6. esp_ota_set_boot_partition(OTA_1)
7. Reboot → boots from OTA_1
8. On next boot: MarkCurrentVersionValid() → cancel rollback
The activation challenge-response:
Server → Device: challenge (random nonce)
Device computes: HMAC-SHA256(HMAC_KEY0, challenge)
Device → Server: {serial_number, challenge, hmac(hex)}
Server verifies HMAC using eFuse-burned key
→ 200 OK: activated, proceed with firmware update
→ 202: pending, retry (up to 10x with backoff)
Practical Guide
When implementing OTA updates:
- Validate image header before flashing: check
esp_image_header_tmagic bytes andesp_app_desc_tbeforeesp_ota_begin(). - Use sequential writes:
OTA_WITH_SEQUENTIAL_WRITESallows streaming without knowing total size upfront. - Report progress: fire the callback every 1 second with percentage and speed for UI feedback.
- Mark valid after boot: call
MarkCurrentVersionValid()only after confirming the firmware is functional. - Handle activation first: if
HasActivationChallenge()is true, complete activation before proceeding with protocol initialization. - Allocate buffer in internal RAM: use
heap_caps_malloc(MALLOC_CAP_INTERNAL)for the 4KB download buffer to avoid PSRAM latency.
Related Pages
Implemented By
- Implementation:78_Xiaozhi_esp32_Ota
- Implementation:78_Xiaozhi_esp32_Assets
- Implementation:78_Xiaozhi_esp32_Application_Boot