diff --git a/src/helpers/FwUpdate.cpp b/src/helpers/FwUpdate.cpp index 0bbfaddc..6943cc42 100644 --- a/src/helpers/FwUpdate.cpp +++ b/src/helpers/FwUpdate.cpp @@ -522,20 +522,28 @@ 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 >= 9 && req[1] == 'w') { + if (len >= 11 && req[1] == 'w') { // the payload arrives padded to the AES block: trust the length byte, not len uint16_t sid; uint32_t off; memcpy(&sid, &req[2], 2); memcpy(&off, &req[4], 4); int n = req[8]; + uint16_t crc; + memcpy(&crc, &req[9], 2); if (_state != RECEIVING || sid != _sid) { // another (stale) transfer _w_drop++; return 0; } + if (11 + n > len || crc16_update(0xFFFF, &req[11], n) != crc) { + // the data got mangled on the way (seen: a fresh header with the previous + // chunk's data behind it) -- drop it, the bitmap asks for it again + _w_bad++; + return 0; + } _w_bin++; _mani_src = 'b'; - if (9 + n <= len) writeAt(off, &req[9], n); // errors show in the next 'm' + writeAt(off, &req[11], n); // errors show in the next 'm' return 0; } if (len >= 2 && req[1] == 'm' && reply_max >= 5) { @@ -641,16 +649,16 @@ void FwUpdate::cmdStatus(char *reply) { strcpy(reply, "> verifying"); break; } - sprintf(reply, "> rx %lu/%lu units, batt %umV%s", (unsigned long)received(), (unsigned long)n, mv, - _flash_diff ? ", flash!" : ""); + sprintf(reply, "> rx %lu/%lu units, batt %umV%s [b%u d%u x%u t%u]", (unsigned long)received(), + (unsigned long)n, mv, _flash_diff ? ", flash!" : "", _w_bin, _w_drop, _w_bad, _w_txt); 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%s [b%u d%u t%u last %c@%lu sid %u]", _err, _flash_diff ? " flash!" : "", _w_bin, - _w_drop, _w_txt, _mani_src, (unsigned long)_mani_off, _sid); + sprintf(reply, "> failed: %s%s [b%u d%u x%u t%u last %c@%lu sid %u]", _err, _flash_diff ? " flash!" : "", + _w_bin, _w_drop, _w_bad, _w_txt, _mani_src, (unsigned long)_mani_off, _sid); break; } } @@ -681,7 +689,7 @@ void FwUpdate::cmdBegin(const char *arg, char *reply) { _flash_diff = false; memset(_mani, 0xFF, sizeof(_mani)); _sid = (uint16_t)sid; - _w_bin = _w_drop = _w_txt = 0; + _w_bin = _w_drop = _w_bad = _w_txt = 0; _mani_src = '-'; _apply_at = 0; sprintf(reply, "OK %lu chunks of %u at %05lX", (unsigned long)((total + FW_OTA_CHUNK - 1) / FW_OTA_CHUNK), diff --git a/src/helpers/FwUpdate.h b/src/helpers/FwUpdate.h index 4a98b12c..2634c494 100644 --- a/src/helpers/FwUpdate.h +++ b/src/helpers/FwUpdate.h @@ -39,7 +39,8 @@ // 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'][sid u16][offset u32 LE][len u8][data] chunk, no reply. The +// [0xF0]['w'][sid u16][offset u32 LE][len u8][crc16 of data][data] chunk, +// no reply; a bad crc is dropped. The // length is explicit (the node only sees // the decrypted size padded to 16 B); sid // is the session from `fw begin @@ -116,7 +117,7 @@ private: uint8_t _mani[FW_OTA_MANIFEST_SIZE]; uint16_t _sid = 0; // transfer session, 0 = none given (text-only senders) // diagnosis of the "wrong base image" hunt (13. 11. 2026): who wrote the manifest - uint16_t _w_bin = 0, _w_drop = 0, _w_txt = 0; + uint16_t _w_bin = 0, _w_drop = 0, _w_bad = 0, _w_txt = 0; char _mani_src = '-'; uint32_t _mani_off = 0; bool _flash_diff = false;