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

Principle:78 Xiaozhi esp32 OTA Firmware Update

From Leeroopedia
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() calls esp_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_t magic bytes and esp_app_desc_t before esp_ota_begin().
  • Use sequential writes: OTA_WITH_SEQUENTIAL_WRITES allows 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

Uses Heuristic

Page Connections

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