diff --git a/src/helpers/FwUpdate.cpp b/src/helpers/FwUpdate.cpp index 3934a2f2..8aa86edf 100644 --- a/src/helpers/FwUpdate.cpp +++ b/src/helpers/FwUpdate.cpp @@ -499,6 +499,11 @@ const char *FwUpdate::writeAt(uint32_t off, const uint8_t *buf, int len) { bool all = true; for (uint32_t u = u0; u < u1; u++) 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) { flash_nrf5x_write(_stage + off, buf, len); for (uint32_t u = u0; u < u1; u++) @@ -568,7 +573,7 @@ const uint8_t *FwUpdate::baseSha() { } bool FwUpdate::checkManifest() { - const Manifest *m = (const Manifest *)_stage; + const Manifest *m = (const Manifest *)_mani; uint8_t key[32]; if (memcmp(m->magic, "MCFW", 4) != 0 || (m->format != 1 && m->format != 2)) { strcpy(_err, "not a package"); @@ -625,14 +630,15 @@ void FwUpdate::cmdStatus(char *reply) { strcpy(reply, "> verifying"); 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; case READY: sprintf(reply, "> ready %s%s, %lu B, crc %04X, batt %umV", _version, _format == 2 ? " (delta)" : "", (unsigned long)_raw_size, _crc, mv); break; case FAILED: - sprintf(reply, "> failed: %s", _err); + sprintf(reply, "> failed: %s%s", _err, _flash_diff ? " (flash readback differed)" : ""); break; } } @@ -658,6 +664,8 @@ void FwUpdate::cmdBegin(const char *arg, char *reply) { _manifest_ok = false; _format = 0; _job = JOB_NONE; + _flash_diff = false; + memset(_mani, 0xFF, sizeof(_mani)); _apply_at = 0; 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); @@ -727,7 +735,7 @@ void FwUpdate::doVerify() { flash_nrf5x_flush(); _state = FAILED; - const Manifest *m = (const Manifest *)_stage; + const Manifest *m = (const Manifest *)_mani; if (!checkManifest()) goto fail; if (APP_START + m->raw_size > (m->format == 2 ? PATCH_LO : FW_OTA_STAGE_END)) { strcpy(_err, "image too big"); @@ -867,6 +875,7 @@ void FwUpdate::loop() { if (_job == JOB_MANIFEST) { _job = JOB_NONE; flash_nrf5x_flush(); + if (memcmp((const void *)_stage, _mani, FW_OTA_MANIFEST_SIZE) != 0) _flash_diff = true; if (checkManifest()) { _manifest_ok = true; } else { @@ -910,7 +919,7 @@ void FwUpdate::loop() { j.mem_size = PATCH_LO - APP_START; j.from_size = imageEnd() - APP_START; 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.write = fwp_write_page; j.tick = fwp_tick; @@ -919,7 +928,7 @@ void FwUpdate::loop() { c.settings = settings; memset(&s, 0, sizeof(s)); 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.outmax = _raw_size; s.flush = copy_flush; diff --git a/src/helpers/FwUpdate.h b/src/helpers/FwUpdate.h index 840d909f..1868c628 100644 --- a/src/helpers/FwUpdate.h +++ b/src/helpers/FwUpdate.h @@ -103,6 +103,13 @@ private: bool _manifest_ok = false; bool _base_known = false; 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 _err[48] = { 0 }; unsigned long _apply_at = 0;