fix(ota): refuse a channel switch to a build that cannot carry this node

A switch can land on an older build: e.g. prod cannot read dev's /mqtt.json,
and prod's M7 image has no Ethernet. Each image carries an ota-compat tag
(state generation + transports). A switch is flashed without rebooting, the
new image's tag is read (bounded by its length), and boot is pointed back at
the running image unless the target keeps this node's state and transports.
CI now requires the tag in every published binary.
This commit is contained in:
agessaman committed 2026-10-03 15:46:46 -07:00
1 parent f652e7d185
commit ed8af1b7ca
4 files changed
+153 -4

No files matched your search

+56 -2
View File
@@ -78,6 +78,9 @@ bool ESP32Board::startOTAUpdate(const char* id, char reply[], bool force_ap) {
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
#include <esp_partition.h>
#include <esp_ota_ops.h>
#include <esp_image_format.h>
#include "OtaChannel.h"
// Embedded CA bundle (produced by board_build.embed_files). Weak so non-bundle
// builds still link; we check for presence at runtime.
@@ -123,6 +126,31 @@ static void ota_parseVersion(const char* ver, char* base_out, size_t base_sz, in
}
}
// Read the compat tag of the image just written to `part`, bounded by the image's own
// length: bytes past it can be a stale, tagged image from an earlier flash.
static bool ota_readImageCompat(const esp_partition_t* part, OtaCompat* out) {
const esp_partition_pos_t pos = { part->address, part->size };
esp_image_metadata_t meta;
if (esp_image_verify(ESP_IMAGE_VERIFY_SILENT, &pos, &meta) != ESP_OK) return false;
static const size_t kChunk = 2048, kTail = 64; // tail > tag + value, so a split tag is seen whole
static uint8_t buf[kTail + kChunk];
size_t have = 0;
for (uint32_t off = 0; off < meta.image_len;) {
size_t n = meta.image_len - off;
if (n > kChunk) n = kChunk;
if (esp_partition_read(part, off, buf + have, n) != ESP_OK) return false;
have += n;
off += n;
const char* v = ota_compat_find(buf, have);
if (v) return ota_compat_parse(v, out);
if (have > kTail) {
memmove(buf, buf + have - kTail, kTail);
have = kTail;
}
}
return false;
}
// Canonical signature of the FLASHED partition table — MUST match
// scripts/partition_signature.py: each entry "type:subtype:offset:size" in
// lowercase hex, sorted by offset, joined by ','. Lets `ota update` compare the
@@ -412,9 +440,35 @@ bool ESP32Board::otaFromManifestImpl(const char* manifest_base, const char* curr
int d = (int)((int64_t)cur * 10 / total);
if (d != ota_progress_decile) { ota_progress_decile = d; Serial.printf("OTA: %d%%\n", d * 10); }
});
httpUpdate.onEnd([]() { Serial.println("OTA: write complete, rebooting..."); });
httpUpdate.rebootOnUpdate(true); // reboots into the new image on success
httpUpdate.onEnd([]() { Serial.println("OTA: write complete"); });
// A channel switch is checked before it may boot (below); native updates reboot here.
httpUpdate.rebootOnUpdate(!cross_channel);
t_httpUpdate_return ret = httpUpdate.update(uclient, file_url);
if (ret == HTTP_UPDATE_OK && cross_channel) {
const esp_partition_t* running = esp_ota_get_running_partition();
const esp_partition_t* target = esp_ota_get_boot_partition();
OtaCompat own = {0, 0}, img = {0, 0};
const char* volatile own_tag = ota_compat_tag; // volatile: the tag must stay in the image
ota_compat_parse(own_tag + sizeof(OTA_COMPAT_TAG) - 1, &own);
bool tagged = target && target != running && ota_readImageCompat(target, &img);
if (tagged && ota_compat_ok(own, img)) {
Serial.println("OTA: channel switch compatible, rebooting...");
delay(100);
ESP.restart();
}
// Point boot back at the running image so the refused build never starts.
bool reverted = (esp_ota_set_boot_partition(running) == ESP_OK);
inhibit_sleep = false;
if (!tagged) {
snprintf(reply, 160, "ERR: channel switch refused: %s has no compat tag; cable flash%s",
avail_disp, reverted ? "" : " [boot revert FAILED]");
} else {
snprintf(reply, 160, "ERR: channel switch refused: target compat %d/%x < this node %d/%x; cable flash%s",
img.gen, img.caps, own.gen, own.caps, reverted ? "" : " [boot revert FAILED]");
}
Serial.print("OTA: "); Serial.println(reply);
return false;
}
// Only reached on failure (success reboots inside update()).
inhibit_sleep = false;