From 228e7a129b3aa276e3a7fa6383e89548cf4d9bc8 Mon Sep 17 00:00:00 2001 From: TeTeHacko Date: Fri, 13 Nov 2026 00:23:05 +0100 Subject: [PATCH] FwUpdate: manifest check and verify run in loop(), not in the command handler --- src/helpers/FwUpdate.cpp | 45 +++++++++++++++++++++++++++++----------- src/helpers/FwUpdate.h | 8 +++++++ 2 files changed, 41 insertions(+), 12 deletions(-) diff --git a/src/helpers/FwUpdate.cpp b/src/helpers/FwUpdate.cpp index d9315c00..3f57c799 100644 --- a/src/helpers/FwUpdate.cpp +++ b/src/helpers/FwUpdate.cpp @@ -562,6 +562,10 @@ void FwUpdate::cmdStatus(char *reply) { } break; case RECEIVING: + if (_job == JOB_VERIFY) { + strcpy(reply, "> verifying"); + break; + } sprintf(reply, "> rx %lu/%lu chunks, batt %umV", (unsigned long)received(), (unsigned long)n, mv); break; case READY: @@ -594,12 +598,17 @@ void FwUpdate::cmdBegin(const char *arg, char *reply) { _state = RECEIVING; _manifest_ok = false; _format = 0; + _job = JOB_NONE; _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); } void FwUpdate::cmdWrite(const char *arg, char *reply) { + if (_state == FAILED) { + sprintf(reply, "ERR: %s", _err); + return; + } if (_state != RECEIVING) { strcpy(reply, "ERR: not receiving"); return; @@ -625,15 +634,7 @@ void FwUpdate::cmdWrite(const char *arg, char *reply) { bool all = true; for (uint32_t i = 0; i * FW_OTA_CHUNK < FW_OTA_MANIFEST_SIZE; i++) if (!(_have[i >> 3] & (1 << (i & 7)))) all = false; - if (all) { - flash_nrf5x_flush(); - if (!checkManifest()) { - _state = FAILED; - sprintf(reply, "ERR: %s", _err); - return; - } - _manifest_ok = true; - } + if (all && _job == JOB_NONE) _job = JOB_MANIFEST; // loop() checks it } strcpy(reply, "ok"); } @@ -656,7 +657,7 @@ void FwUpdate::cmdMissing(char *reply) { } void FwUpdate::cmdVerify(char *reply) { - if (_state == READY) { + if (_state == READY || _state == FAILED || _job == JOB_VERIFY) { cmdStatus(reply); return; } @@ -669,6 +670,11 @@ void FwUpdate::cmdVerify(char *reply) { strcpy(reply, "ERR: incomplete, see fw missing"); return; } + _job = JOB_VERIFY; // loop() runs it; poll `fw status` + strcpy(reply, "> verifying"); +} + +void FwUpdate::doVerify() { flash_nrf5x_flush(); _state = FAILED; @@ -763,10 +769,9 @@ void FwUpdate::cmdVerify(char *reply) { strncpy(_version, m->version, sizeof(_version) - 1); _version[sizeof(_version) - 1] = 0; _state = READY; - cmdStatus(reply); return; fail: - sprintf(reply, "ERR: %s", _err); + _state = FAILED; } void FwUpdate::cmdBootloader(char *reply) { @@ -810,6 +815,22 @@ void FwUpdate::cmdApply(uint32_t sender_timestamp, const char *arg, char *reply) } void FwUpdate::loop() { + if (_job == JOB_MANIFEST) { + _job = JOB_NONE; + flash_nrf5x_flush(); + if (checkManifest()) { + _manifest_ok = true; + } else { + _state = FAILED; + } + } else if (_job == JOB_VERIFY) { + _job = JOB_NONE; + if (!_manifest_ok && !checkManifest()) { + _state = FAILED; + } else { + doVerify(); + } + } if (_apply_at == 0 || (long)(millis() - _apply_at) < 0) return; _apply_at = 0; if (_state != READY) return; diff --git a/src/helpers/FwUpdate.h b/src/helpers/FwUpdate.h index 2fb99d23..2ecac63d 100644 --- a/src/helpers/FwUpdate.h +++ b/src/helpers/FwUpdate.h @@ -52,12 +52,19 @@ public: private: enum State : uint8_t { IDLE, RECEIVING, READY, FAILED }; + // Heavy work (ed25519, sha256 of the running image, the dry run) is never done + // inside a command handler: over the mesh that runs deep in the packet path, + // and on x1 (13. 11. 2026) the manifest check there overflowed the stack -- + // hardfault in RadioLib's SPI code moments later. The handler queues it, + // loop() runs it with a shallow stack, `fw status` reports the result. + enum Job : uint8_t { JOB_NONE, JOB_MANIFEST, JOB_VERIFY }; void cmdStatus(char *reply); void cmdBegin(const char *arg, char *reply); void cmdWrite(const char *arg, char *reply); void cmdMissing(char *reply); void cmdVerify(char *reply); + void doVerify(); void cmdApply(uint32_t sender_timestamp, const char *arg, char *reply); void cmdBootloader(char *reply); uint16_t battMinMv(); @@ -72,6 +79,7 @@ private: uint32_t _raw_size = 0; uint16_t _crc = 0; uint8_t _format = 0; + Job _job = JOB_NONE; bool _manifest_ok = false; bool _base_known = false; uint8_t _base_sha[32];