FwUpdate: check the manifest from a RAM copy; flag a flash readback mismatch

This commit is contained in:
tth committed 2026-11-13 01:26:10 +01:00
1 parent d1e893f692
commit dc13c5b286
2 files changed
+22 -6

No files matched your search

+15 -6
View File
@@ -499,6 +499,11 @@ const char *FwUpdate::writeAt(uint32_t off, const uint8_t *buf, int len) {
bool all = true; bool all = true;
for (uint32_t u = u0; u < u1; u++) for (uint32_t u = u0; u < u1; u++)
if (!haveUnit(u)) all = false; if (!haveUnit(u)) all = false;
if (off < FW_OTA_MANIFEST_SIZE) {
uint32_t n = (uint32_t)len;
if (off + n > FW_OTA_MANIFEST_SIZE) n = FW_OTA_MANIFEST_SIZE - off;
memcpy(&_mani[off], buf, n);
}
if (!all) { if (!all) {
flash_nrf5x_write(_stage + off, buf, len); flash_nrf5x_write(_stage + off, buf, len);
for (uint32_t u = u0; u < u1; u++) for (uint32_t u = u0; u < u1; u++)
@@ -568,7 +573,7 @@ const uint8_t *FwUpdate::baseSha() {
} }
bool FwUpdate::checkManifest() { bool FwUpdate::checkManifest() {
const Manifest *m = (const Manifest *)_stage; const Manifest *m = (const Manifest *)_mani;
uint8_t key[32]; uint8_t key[32];
if (memcmp(m->magic, "MCFW", 4) != 0 || (m->format != 1 && m->format != 2)) { if (memcmp(m->magic, "MCFW", 4) != 0 || (m->format != 1 && m->format != 2)) {
strcpy(_err, "not a package"); strcpy(_err, "not a package");
@@ -625,14 +630,15 @@ void FwUpdate::cmdStatus(char *reply) {
strcpy(reply, "> verifying"); strcpy(reply, "> verifying");
break; break;
} }
sprintf(reply, "> rx %lu/%lu units, batt %umV", (unsigned long)received(), (unsigned long)n, mv); sprintf(reply, "> rx %lu/%lu units, batt %umV%s", (unsigned long)received(), (unsigned long)n, mv,
_flash_diff ? ", flash!" : "");
break; break;
case READY: case READY:
sprintf(reply, "> ready %s%s, %lu B, crc %04X, batt %umV", _version, _format == 2 ? " (delta)" : "", sprintf(reply, "> ready %s%s, %lu B, crc %04X, batt %umV", _version, _format == 2 ? " (delta)" : "",
(unsigned long)_raw_size, _crc, mv); (unsigned long)_raw_size, _crc, mv);
break; break;
case FAILED: case FAILED:
sprintf(reply, "> failed: %s", _err); sprintf(reply, "> failed: %s%s", _err, _flash_diff ? " (flash readback differed)" : "");
break; break;
} }
} }
@@ -658,6 +664,8 @@ void FwUpdate::cmdBegin(const char *arg, char *reply) {
_manifest_ok = false; _manifest_ok = false;
_format = 0; _format = 0;
_job = JOB_NONE; _job = JOB_NONE;
_flash_diff = false;
memset(_mani, 0xFF, sizeof(_mani));
_apply_at = 0; _apply_at = 0;
sprintf(reply, "OK %lu chunks of %u at %05lX", (unsigned long)((total + FW_OTA_CHUNK - 1) / FW_OTA_CHUNK), sprintf(reply, "OK %lu chunks of %u at %05lX", (unsigned long)((total + FW_OTA_CHUNK - 1) / FW_OTA_CHUNK),
FW_OTA_CHUNK, (unsigned long)stage); FW_OTA_CHUNK, (unsigned long)stage);
@@ -727,7 +735,7 @@ void FwUpdate::doVerify() {
flash_nrf5x_flush(); flash_nrf5x_flush();
_state = FAILED; _state = FAILED;
const Manifest *m = (const Manifest *)_stage; const Manifest *m = (const Manifest *)_mani;
if (!checkManifest()) goto fail; if (!checkManifest()) goto fail;
if (APP_START + m->raw_size > (m->format == 2 ? PATCH_LO : FW_OTA_STAGE_END)) { if (APP_START + m->raw_size > (m->format == 2 ? PATCH_LO : FW_OTA_STAGE_END)) {
strcpy(_err, "image too big"); strcpy(_err, "image too big");
@@ -867,6 +875,7 @@ void FwUpdate::loop() {
if (_job == JOB_MANIFEST) { if (_job == JOB_MANIFEST) {
_job = JOB_NONE; _job = JOB_NONE;
flash_nrf5x_flush(); flash_nrf5x_flush();
if (memcmp((const void *)_stage, _mani, FW_OTA_MANIFEST_SIZE) != 0) _flash_diff = true;
if (checkManifest()) { if (checkManifest()) {
_manifest_ok = true; _manifest_ok = true;
} else { } else {
@@ -910,7 +919,7 @@ void FwUpdate::loop() {
j.mem_size = PATCH_LO - APP_START; j.mem_size = PATCH_LO - APP_START;
j.from_size = imageEnd() - APP_START; j.from_size = imageEnd() - APP_START;
j.patch = (const uint8_t *)(_stage + FW_OTA_MANIFEST_SIZE); j.patch = (const uint8_t *)(_stage + FW_OTA_MANIFEST_SIZE);
j.patch_size = ((const Manifest *)_stage)->comp_size; j.patch_size = ((const Manifest *)_mani)->comp_size;
j.erase = fwp_erase_page; j.erase = fwp_erase_page;
j.write = fwp_write_page; j.write = fwp_write_page;
j.tick = fwp_tick; j.tick = fwp_tick;
@@ -919,7 +928,7 @@ void FwUpdate::loop() {
c.settings = settings; c.settings = settings;
memset(&s, 0, sizeof(s)); memset(&s, 0, sizeof(s));
s.in = (const uint8_t *)c.stage_blob; s.in = (const uint8_t *)c.stage_blob;
s.inlen = ((const Manifest *)_stage)->comp_size; s.inlen = ((const Manifest *)_mani)->comp_size;
s.ring = ring; s.ring = ring;
s.outmax = _raw_size; s.outmax = _raw_size;
s.flush = copy_flush; s.flush = copy_flush;
+7
View File
@@ -103,6 +103,13 @@ private:
bool _manifest_ok = false; bool _manifest_ok = false;
bool _base_known = false; bool _base_known = false;
uint8_t _base_sha[32]; uint8_t _base_sha[32];
// The manifest is checked from this RAM copy, not from the stage in flash: three
// bench runs failed with "wrong base image" while the node's base and the
// package's matched exactly -- the check had read an older package's manifest
// that was still in flash at the same address. _flash_diff records a readback
// mismatch after the flush, for diagnosis (`fw status`).
uint8_t _mani[FW_OTA_MANIFEST_SIZE];
bool _flash_diff = false;
char _version[48] = { 0 }; char _version[48] = { 0 };
char _err[48] = { 0 }; char _err[48] = { 0 };
unsigned long _apply_at = 0; unsigned long _apply_at = 0;