OtaRollback - OTA rollback protection / soft-brick safeguard
Layer: L7 Application ยท Build flags: PROTOCORE_ENABLE_OTA_ROLLBACK
What this example teaches
A bad OTA image that boots but cannot do its job will soft-brick a remote device. The ESP32 bootloader can mark a freshly flashed image PENDING_VERIFY and roll back to the previous one unless the new firmware confirms itself. This wraps that mechanism: each loop it runs a self-test (here WiFi up + healthy heap) and ticks the rollback service - a passing self-test commits the image, a failing one (or no confirm within PROTOCORE_OTA_CONFIRM_WINDOW_MS) rolls back. So a bad update self-heals instead of bricking. It is the safety net for the OTA upload in OTA.
Define a self-test, then tick until committed:
void loop() {
static bool done = false;
if (!done) {
protocore_ota_action a = protocore_ota_rollback_tick(self_test());
if (a == protocore_ota_action::PROTOCORE_OTA_COMMIT) { Serial.println("[ota] image committed"); done = true; }
}
server.handle();
}
EMBED_INLINE embed_bool protocore_physical_wifi_up(void)
Physical.wifi_ready as an expression, for a bring-up loop to wait on.
protocore_ota_rollback_tick(ok) is a no-op once the image is committed or on a normally-booted image, so it is safe to call every loop. protocore_ota_rollback_state() reports the current image state for the /ota-state endpoint.
Requirement. Actual rollback needs the bootloader's app-rollback support (CONFIG_BOOTLOADER_APP_ROLLBACK_ENABLE) enabled in the build.
Build and run
pio ci --board=esp32dev --project-option="framework=arduino" \
--project-option="build_flags=-DPROTOCORE_ENABLE_OTA_ROLLBACK=1" \
--lib="." examples/L7-Application/OtaRollback/OtaRollback.ino
curl http://<ip>/ota-state # {"img_state":...} - the running image's verify state
Annotated source
The complete sketch (OtaRollback.ino), reproduced verbatim with added explanatory comments:
#define PROTOCORE_ENABLE_OTA_ROLLBACK 1
#include "protocore.h"
static const char *SSID = "YOUR_SSID";
static const char *PASSWORD = "YOUR_PASSWORD";
PC server;
static bool self_test()
{
}
void setup()
{
Serial.begin(115200);
delay(250);
Serial.print("IP: ");
Serial.printf("IP: %u.%u.%u.%u\n", (unsigned)(ip & 0xFF), (unsigned)((ip >> 8) & 0xFF),
(unsigned)((ip >> 16) & 0xFF), (unsigned)((ip >> 24) & 0xFF));
char b[48];
snprintf(b, sizeof(b), "{\"img_state\":%u}", protocore_ota_img_state());
server.send(id, 200, "application/json", b);
});
server.begin(80);
}
void loop()
{
static bool done = false;
if (!done)
{
protocore_ota_action a = protocore_ota_rollback_tick(self_test());
if (a == protocore_ota_action::PROTOCORE_OTA_COMMIT)
{
Serial.println("[ota] image committed");
done = true;
}
}
server.handle();
}
@ HTTP_GET
Safe, idempotent read.
OTA rollback protection / soft-brick safeguard (PROTOCORE_ENABLE_OTA_ROLLBACK).
Layer 1 (Physical) - link bring-up, the interface registry, and live egress reporting.
EMBED_INLINE uint32_t protocore_physical_egress_ipv4(void)
Physical.egress_ip as an expression: the address a sketch prints once its link is up.
PhysicalVars PhysicalV
The operands and the outcome.
uint8_t * protocore_physical_span(void)
The PROTOCORE_PHYSICAL_BORROW bytes this module's state lives in.