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:
1 parent
c93f5d906d
commit
0b38eab008
7 files changed
+1107
-12
No files matched your search
@@ -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
|
||||
Reference in new issue
Block a user