diff --git a/src/helpers/FwUpdate.cpp b/src/helpers/FwUpdate.cpp index c638192c..794b5b44 100644 --- a/src/helpers/FwUpdate.cpp +++ b/src/helpers/FwUpdate.cpp @@ -521,12 +521,15 @@ const char *FwUpdate::writeAt(uint32_t off, const uint8_t *buf, int len) { } int FwUpdate::handleBinary(const uint8_t *req, int len, uint8_t *reply, int reply_max) { - if (len >= 7 && req[1] == 'w') { + if (len >= 9 && req[1] == 'w') { // the payload arrives padded to the AES block: trust the length byte, not len + uint16_t sid; uint32_t off; - memcpy(&off, &req[2], 4); - int n = req[6]; - if (7 + n <= len) writeAt(off, &req[7], n); // errors show in the next 'm' + memcpy(&sid, &req[2], 2); + memcpy(&off, &req[4], 4); + int n = req[8]; + if (_state != RECEIVING || sid != _sid) return 0; // another (stale) transfer + if (9 + n <= len) writeAt(off, &req[9], n); // errors show in the next 'm' return 0; } if (len >= 2 && req[1] == 'm' && reply_max >= 5) { @@ -646,7 +649,9 @@ void FwUpdate::cmdStatus(char *reply) { } void FwUpdate::cmdBegin(const char *arg, char *reply) { - uint32_t total = strtoul(arg, NULL, 10); + char *rest; + uint32_t total = strtoul(arg, &rest, 10); + uint32_t sid = strtoul(rest, NULL, 10); // optional transfer session (binary chunks) // small = delta: top of the free flash, out of reach of the in-place shift; // big = full image: right above the running one, as before uint32_t stage = total <= FW_OTA_PATCH_AREA ? PATCH_LO : (imageEnd() + PAGE - 1) & ~(PAGE - 1); @@ -668,6 +673,7 @@ void FwUpdate::cmdBegin(const char *arg, char *reply) { _job = JOB_NONE; _flash_diff = false; memset(_mani, 0xFF, sizeof(_mani)); + _sid = (uint16_t)sid; _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); diff --git a/src/helpers/FwUpdate.h b/src/helpers/FwUpdate.h index 4df46283..3fa5ce19 100644 --- a/src/helpers/FwUpdate.h +++ b/src/helpers/FwUpdate.h @@ -39,9 +39,14 @@ // Binary request carrying firmware data (over the mesh, admin only). Upstream // request types are 0x01..0x07; 0xF0 is far from them on purpose. -// [0xF0]['w'][offset u32 LE][len u8][data] chunk, no reply. The length is -// explicit: the node only sees the -// decrypted size padded to 16 B. +// [0xF0]['w'][sid u16][offset u32 LE][len u8][data] chunk, no reply. The +// length is explicit (the node only sees +// the decrypted size padded to 16 B); sid +// is the session from `fw begin +// `, chunks of another session are +// dropped -- stale chunks of an earlier +// transfer did arrive late on the bench and +// replaced the new manifest. // [0xF0]['m'] reply: [state][units u16][count u16] // [missing unit index u16 ...] // state: 0 idle, 1 receiving, 2 ready, 3 failed, 4 busy (manifest/verify) @@ -109,6 +114,7 @@ private: // 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]; + uint16_t _sid = 0; // transfer session, 0 = none given (text-only senders) bool _flash_diff = false; char _version[48] = { 0 }; char _err[64] = { 0 };