ESP32 Dual-Partition OTA: Safe Rollbacks and A/B Testing

Dual-Partition OTA on the ESP32: Safe Rollbacks, A/B Testing, and Best PracticesZigbee Green Power: Ultra-Low-Power Energy Harvesting SolutionsZigbee Green Power: Ultra-Low-Power Energy Harvesting SolutionsDiscover how ZGP enables battery-free IoT devices through energy harvesting with ESP32 integrations, supporting smart home and industrial applications.

The ESP32’s dual-partition OTA update mechanism revolutionizes IoT deployments by enabling safe firmware updatesAWS IoT Core with ESP32: X.509 Certificates and Shadow UpdatesAWS IoT Core with ESP32: X.509 Certificates and Shadow UpdatesLearn to securely connect ESP32 to AWS IoT Core using X.509 certificates and device shadows, with step-by-step instructions and best practices., A/B testing, and uninterrupted operation. This guide explores the architecture, implementation strategies, and best practices to maximize reliability and minimize risks.

Table of Contents

What is Dual-Partition OTA?🔗

Dual-Partition OTA divides the ESP32’sCombining Wi-Fi with Deep Sleep for Low-Power ApplicationsCombining Wi-Fi with Deep Sleep for Low-Power ApplicationsLearn how to integrate Wi-Fi and deep sleep on ESP32 to maximize battery life in IoT devices. This guide offers practical tips and step-by-step instructions. flash into two partitions: an active partition running the current firmware and an inactive partition for updates. If the new firmware fails validation, the device reverts to the stable partition, ensuring uninterrupted operation. Key benefits include:

Dual-Partition Architecture🔗

The ESP32Setting Up ESP32 as a Wi-Fi Access PointSetting Up ESP32 as a Wi-Fi Access PointMaster ESP32 AP configuration with our step-by-step guide. Set up a secure, local IoT network using practical code examples and optimization tips. uses three critical partitions:

PartitionRoleStorage Location
factoryInitial firmwareFlash (fixed)
ota_0/ota_1OTA update slotsDynamic per partition table
otadataMetadata (active partition)0x2000 bytes

Boot Process:

1. The bootloader checks otadata to determine the active partition.

2. If the active app crashes, the bootloader marks it invalid and falls back.

3. After a successful update, otadata points to the new partition.

Example Partition Table:

# Name,   Type, SubType, Offset,  Size
factory,  app,  factory, 0x10000, 1M
ota_0,    app,  ota_0,   ,        1M
ota_1,    app,  ota_1,   ,        1M
otadata,  data, ota,     ,        0x2000

Configuring Dual-Partition OTA🔗

1. Modify the Partition Table: Define ota_0 and ota_1 partitions.

2. Enable OTAImplementing Over-the-Air (OTA) Updates via Wi-Fi on ESP32Implementing Over-the-Air (OTA) Updates via Wi-Fi on ESP32Learn how to implement secure and reliable OTA updates on ESP32 for enhanced IoT performance, easy updates, and rollback capability without physical access. in Firmware: Use the esp_ota_ops.h library.

3. Set Up OTAImplementing Over-the-Air (OTA) Updates via Wi-Fi on ESP32Implementing Over-the-Air (OTA) Updates via Wi-Fi on ESP32Learn how to implement secure and reliable OTA updates on ESP32 for enhanced IoT performance, easy updates, and rollback capability without physical access. Server: Host firmware binaries via HTTPSImplementing Secure Communication over Wi-Fi on ESP32Implementing Secure Communication over Wi-Fi on ESP32This comprehensive guide secures ESP32 IoT devices using HTTPS, TLS for MQTT, proper certificate management, and network hardening practices..

Example Initialization:

#include "esp_ota_ops.h"
#include "esp_https_ota.h"
void perform_ota_update() {
  esp_http_client_config_t config = {
    .url = "https://firmware.example.com/update.bin",
    .cert_pem = aws_root_ca_pem,
  };
  esp_err_t ret = esp_https_ota(&config);
  if (ret == ESP_OK) esp_restart();
  else ESP_LOGE("OTA", "Update failed!");
}

Safe Rollback Mechanism🔗

1. Automatic Rollback:

Enable CONFIG_BOOTLOADER_APP_ROLLBACK_ENABLE to trigger reversion on boot failure or CRCSigfox Message Encoding: Packing Sensor Data into 12-byte PayloadsSigfox Message Encoding: Packing Sensor Data into 12-byte PayloadsLearn efficient data encoding techniques for Sigfox's constrained 12-byte payloads. Discover bitwise operations, structured encoding & CBOR strategies. mismatch.

2. Manual Rollback:

Force reversion via code:

const esp_partition_t *fallback = esp_ota_get_last_valid_partition();
esp_ota_set_boot_partition(fallback);
esp_restart();

3. Validation Hooks:

Add runtime checks (e.g., sensor initialization):

if (sensor_init() != ESP_OK) trigger_rollback();

A/B Testing Strategies🔗

1. Staged Rollouts:

  • Segment devices using NVS flags:
nvs_set_u8(handle, "group", device_id % 2);  // 50% Group A/B

2. Metrics Collection:

Post metrics to the cloud before switching partitions:

esp_http_client_post(client, "https://api.example.com/metrics", json_data, ...);

3. Gradual Promotion:

Code Walkthrough: OTA with Rollback🔗

void perform_ota_update() {
  esp_ota_handle_t ota_handle;
  const esp_partition_t *update_partition = esp_ota_get_next_update_partition(NULL);
  esp_ota_begin(update_partition, OTA_SIZE_UNKNOWN, &ota_handle);
  // Download and write firmware...
  while ((n = esp_http_client_read(client, ota_buffer, BUFFER_SIZE)) {
    esp_ota_write(ota_handle, ota_buffer, n);
  }
  if (esp_ota_end(ota_handle) == ESP_OK) {
    esp_ota_set_boot_partition(update_partition);
    esp_restart();
  } else {
    esp_ota_abort(ota_handle);
  }
}

Security and Anti-Rollback Protections🔗

1. Signed Updates:

Verify firmware signatures with ECDSA:

if (esp_ota_verify_signature(update_partition, public_key) != ESP_OK) abort_rollback();

2. Anti-Rollback Counters:

Store security versions in NVS to block downgrades:

if (new_version < current_version) return ESP_FAIL;

3. Secure Boot:

Enable CONFIG_SECURE_BOOT to ensure only trusted firmware runs.

Troubleshooting Common Issues🔗

IssueDiagnosisFix
Boot Loopotadata corruptionErase otadata, reflash factory
OTA Fails at 97%Insufficient SPIFFS spaceAdjust partition sizes
Rollback Not TriggeredRollback config disabledEnable CONFIG_BOOTLOADER_APP_ROLLBACK_ENABLE
A/B Groups ResetNVS partition fullIncrease NVS size or prune keys

Log Analysis:

idf.py monitor | grep "ota_ops"

Recovery Mode:

Hold GPIO0 low during boot to force the factory partition:

if (gpio_get_level(RECOVERY_GPIO) == 0) esp_ota_set_boot_partition(factory_partition);

Best Practices🔗

By mastering dual-partition OTAImplementing Over-the-Air (OTA) Updates via Wi-Fi on ESP32Implementing Over-the-Air (OTA) Updates via Wi-Fi on ESP32Learn how to implement secure and reliable OTA updates on ESP32 for enhanced IoT performance, easy updates, and rollback capability without physical access., you can deploy updates confidently, test features safely, and maintain robust IoT deployments at scale.

Author: Marcelo V. Souza - Engenheiro de Sistemas e Entusiasta em IoT e Desenvolvimento de Software, com foco em inovação tecnológica.

References🔗

Share article

Related Articles