FwUpdate phase 1: only in FW_OTA images, manifest layout 2, no downgrade over the mesh

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
tthandClaude Opus 5.5 committed 2026-11-13 16:33:29 +01:00
1 parent c93f5d906d
commit 0b38eab008
7 files changed
+1107 -12

No files matched your search

+53 -9
View File
@@ -1,3 +1,7 @@
// Compiled only into the experimental -ota images (-D FW_OTA=1); a classic
// image has no firmware update at all (helpers/NodeMaintenance.h).
#ifdef FW_OTA
#include "FwUpdate.h"
#include "FwPatch.h"
@@ -36,17 +40,32 @@ extern "C" uint32_t __etext, __data_start__, __data_end__;
struct __attribute__((packed)) Manifest {
char magic[4]; // "MCFW"
uint8_t format; // 1 = deflate full image, 2 = in-place delta (FwPatch)
uint8_t reserved0[3];
uint8_t layout; // MANIFEST_LAYOUT; was a reserved 0 in the first layout
uint8_t reserved0[2];
uint32_t raw_size;
uint32_t comp_size;
uint8_t raw_sha[32];
uint8_t comp_sha[32];
char target[32];
char version[48];
char target[48]; // env + image variant, e.g. "SenseCap_Solar_repeater-observer-ota"
char version[28]; // FIRMWARE_VERSION, at most 19 chars (tools/build_version.py)
uint32_t build_epoch; // FIRMWARE_BUILD_EPOCH of the new image: no downgrade over the mesh
uint8_t base_sha[32]; // format 2: sha256 of the image the delta applies to
uint8_t signature[64]; // Ed25519 over bytes 0..191
};
#define MANIFEST_LAYOUT 2
// What a package for THIS image must carry, read by tools/ota_pack.py from the
// ELF -- the target string and the epoch come from the build, never from a
// command line that could disagree with it. `fw ver` prints it (and keeps it
// in the image: an unreferenced string would fall to --gc-sections).
#define FWU_STR2(x) #x
#define FWU_STR(x) FWU_STR2(x)
static const char FW_OTA_TAGS[] = "MCFW-TAGS target=" FW_OTA_TARGET " epoch=" FWU_STR(FIRMWARE_BUILD_EPOCH);
static_assert(sizeof(Manifest) == FW_OTA_MANIFEST_SIZE, "manifest layout");
static_assert(offsetof(Manifest, target) == 80 && offsetof(Manifest, version) == 128 &&
offsetof(Manifest, build_epoch) == 156 && offsetof(Manifest, base_sha) == 160 &&
offsetof(Manifest, signature) == 192,
"manifest offsets shared with tools/ota_pack.py");
// --- inflate ------------------------------------------------------------------
// A small RFC 1951 decoder in the style of zlib's contrib/puff. EVERYTHING it
@@ -628,6 +647,19 @@ bool FwUpdate::checkManifest() {
strcpy(_err, "bad signature");
return false;
}
if (m->layout != MANIFEST_LAYOUT) {
strcpy(_err, "old package layout, repack it (tools/ota_pack.py)");
return false;
}
// Not newer than what runs (an older build, or this very one): never over the
// mesh -- a bad release is undone by a revert and a NEW release. Locally it
// takes `fw apply downgrade`.
_older = m->build_epoch <= (uint32_t)FIRMWARE_BUILD_EPOCH;
if (_older && _remote) {
sprintf(_err, "not newer than this image (pkg %lu, node %lu)", (unsigned long)m->build_epoch,
(unsigned long)FIRMWARE_BUILD_EPOCH);
return false;
}
if (strncmp(m->target, FW_OTA_TARGET, sizeof(m->target)) != 0) {
strcpy(_err, "wrong target");
return false;
@@ -719,6 +751,7 @@ void FwUpdate::cmdBegin(uint32_t sender_timestamp, const char *arg, char *reply)
_stage = stage;
_total = total;
_remote = sender_timestamp != 0;
_older = false;
memset(_have, 0, sizeof(_have));
_state = RECEIVING;
_manifest_ok = false;
@@ -890,8 +923,8 @@ void FwUpdate::doVerify() {
}
}
_raw_size = m->raw_size;
strncpy(_version, m->version, sizeof(_version) - 1);
_version[sizeof(_version) - 1] = 0;
memcpy(_version, m->version, sizeof(m->version)); // may fill all 28 bytes, no NUL
_version[sizeof(m->version)] = 0;
_state = READY;
return;
fail:
@@ -909,9 +942,15 @@ void FwUpdate::cmdApply(uint32_t sender_timestamp, const char *arg, char *reply)
strcpy(reply, "ERR: nothing verified, fw verify first");
return;
}
bool force = strcmp(arg, "force") == 0;
if (force && sender_timestamp != 0) {
strcpy(reply, "ERR: force is local-only");
// arguments: `force` (skip the battery gate) and/or `downgrade`, both local only
bool force = strstr(arg, "force") != NULL;
bool downgrade = strstr(arg, "downgrade") != NULL;
if ((force || downgrade) && sender_timestamp != 0) {
strcpy(reply, "ERR: force / downgrade are local-only");
return;
}
if (_older && !downgrade) {
strcpy(reply, "ERR: not newer than this image: fw apply downgrade (local only)");
return;
}
uint16_t bank0 = *(const volatile uint16_t *)BL_SETTINGS;
@@ -1049,6 +1088,9 @@ bool FwUpdate::handleCommand(uint32_t sender_timestamp, const char *command, cha
strcpy(reply, "OK");
} else if (strcmp(arg, "bl") == 0)
cmdBootloader(reply);
else if (strcmp(arg, "ver") == 0)
sprintf(reply, "> %s", FW_OTA_TAGS + 10); // "target=... epoch=..."
else if (strncmp(arg, "bin ", 4) == 0) {
// Bench: feed a binary REQ_TYPE_FW_OTA payload (base64) through the same
// handler the mesh uses -- padded to the AES block exactly as the node sees
@@ -1089,7 +1131,7 @@ bool FwUpdate::handleCommand(uint32_t sender_timestamp, const char *command, cha
}
}
else
strcpy(reply, "ERR: fw status|begin|w|missing|verify|apply|abort|bl|base");
strcpy(reply, "ERR: fw status|begin|w|missing|verify|apply|abort|bl|base|ver");
return true;
}
@@ -1103,3 +1145,5 @@ bool FwUpdate::handleCommand(uint32_t, const char *command, char *reply) {
void FwUpdate::loop() {}
#endif
#endif // FW_OTA
+4 -1
View File
@@ -23,7 +23,9 @@
// fw w <offset> <base64> one chunk, offset a multiple of FW_OTA_CHUNK
// fw missing first missing chunk offsets (for resending)
// fw verify signature + hashes + dry-run inflate
// fw apply [force] battery gate, then copy and reboot
// fw apply [force] [downgrade] battery gate, then copy and reboot; both
// arguments local only (no downgrade over the mesh)
// fw ver target and build epoch a package for this image needs
// fw abort forget the staged package
// fw bl print the bootloader settings page
@@ -113,6 +115,7 @@ private:
// request). Then only a DELTA is accepted: a full image is ~300 kB, hours of
// airtime on a shared channel -- it goes over USB or the BLE console.
bool _remote = false;
bool _older = false; // the package is not newer than this image (manifest build_epoch)
bool _base_known = false;
uint8_t _base_sha[32];
// The manifest is checked from this RAM copy, not from the stage in flash: three
+4
View File
@@ -100,7 +100,9 @@ void NodeMaintenance::begin(FILESYSTEM* fs, NodePrefs* prefs, CommonCLICallbacks
void NodeMaintenance::loop() {
dfuTick();
#ifdef FW_OTA
_fw.loop();
#endif
switch (_silence.poll(millis(), radioActivity())) {
case RadioSilenceWatchdog::KICK_RX:
Serial.printf("radio silence: nothing received for %lu min, re-arming RX\n",
@@ -249,7 +251,9 @@ bool NodeMaintenance::dfuCommand(uint32_t sender_timestamp, const char* command,
bool NodeMaintenance::handleCommand(uint32_t sender_timestamp, const char* command, char* reply) {
if (dfuCommand(sender_timestamp, command, reply)) return true;
#ifdef FW_OTA
if (_fw.handleCommand(sender_timestamp, command, reply)) return true;
#endif
if (silenceCommand(command, reply)) return true;
if (strcmp(command, "get boot") == 0) {
#if defined(NRF52_PLATFORM)
+14
View File
@@ -12,6 +12,7 @@
//
// dfu [uf2|serial|ota] reboot into the bootloader (local console only)
// fw ... admin-pushed firmware update, see helpers/FwUpdate.h
// (only with -D FW_OTA=1: the experimental -ota images)
// set radio.silence <min> radio silence watchdog, 0/off = disabled (#6)
// get radio.silence threshold, backoff, quiet time, stage
// get radio.reboots reboots done by the watchdog, survives power loss
@@ -26,13 +27,19 @@
#include <Arduino.h>
#include <Mesh.h>
#include <helpers/CommonCLI.h>
#ifdef FW_OTA
#include <helpers/FwUpdate.h>
#endif
#include <helpers/RadioSilenceWatchdog.h>
#include <helpers/radiolib/RadioLibWrappers.h>
class NodeMaintenance {
public:
#ifdef FW_OTA
NodeMaintenance(mesh::MainBoard& board, RadioLibWrapper& radio) : _board(board), _radio(radio), _fw(board) { }
#else
NodeMaintenance(mesh::MainBoard& board, RadioLibWrapper& radio) : _board(board), _radio(radio) { }
#endif
// Prints (and latches for `get boot`) the reset reason and the hardfault record.
// The record is cleared once read, so call it once, as early as Serial is up.
@@ -48,8 +55,13 @@ public:
// Binary request from a logged-in client (REQ_TYPE_FW_OTA, firmware chunks).
// Returns the reply length, 0 = no reply. Admins only.
int handleRequest(bool admin, const uint8_t* req, int len, uint8_t* reply, int reply_max) {
#ifdef FW_OTA
if (!admin || len < 1 || req[0] != REQ_TYPE_FW_OTA) return 0;
return _fw.handleBinary(req, len, reply, reply_max);
#else
(void)admin; (void)req; (void)len; (void)reply; (void)reply_max;
return 0; // a classic image: no firmware update, the request is dropped
#endif
}
private:
@@ -66,7 +78,9 @@ private:
NodePrefs* _prefs = NULL;
CommonCLICallbacks* _callbacks = NULL;
#ifdef FW_OTA
FwUpdate _fw;
#endif
RadioSilenceWatchdog _silence;
uint32_t _silence_reboots = 0;