FwUpdate binary: crc16 per chunk; a mangled chunk is dropped and asked for again
This commit is contained in:
1 parent
0c06994d1f
commit
b8ce87c6ff
2 files changed
+18
-9
No files matched your search
@@ -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) {
|
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
|
// the payload arrives padded to the AES block: trust the length byte, not len
|
||||||
uint16_t sid;
|
uint16_t sid;
|
||||||
uint32_t off;
|
uint32_t off;
|
||||||
memcpy(&sid, &req[2], 2);
|
memcpy(&sid, &req[2], 2);
|
||||||
memcpy(&off, &req[4], 4);
|
memcpy(&off, &req[4], 4);
|
||||||
int n = req[8];
|
int n = req[8];
|
||||||
|
uint16_t crc;
|
||||||
|
memcpy(&crc, &req[9], 2);
|
||||||
if (_state != RECEIVING || sid != _sid) { // another (stale) transfer
|
if (_state != RECEIVING || sid != _sid) { // another (stale) transfer
|
||||||
_w_drop++;
|
_w_drop++;
|
||||||
return 0;
|
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++;
|
_w_bin++;
|
||||||
_mani_src = 'b';
|
_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;
|
return 0;
|
||||||
}
|
}
|
||||||
if (len >= 2 && req[1] == 'm' && reply_max >= 5) {
|
if (len >= 2 && req[1] == 'm' && reply_max >= 5) {
|
||||||
@@ -641,16 +649,16 @@ void FwUpdate::cmdStatus(char *reply) {
|
|||||||
strcpy(reply, "> verifying");
|
strcpy(reply, "> verifying");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
sprintf(reply, "> rx %lu/%lu units, batt %umV%s", (unsigned long)received(), (unsigned long)n, mv,
|
sprintf(reply, "> rx %lu/%lu units, batt %umV%s [b%u d%u x%u t%u]", (unsigned long)received(),
|
||||||
_flash_diff ? ", flash!" : "");
|
(unsigned long)n, mv, _flash_diff ? ", flash!" : "", _w_bin, _w_drop, _w_bad, _w_txt);
|
||||||
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%s [b%u d%u t%u last %c@%lu sid %u]", _err, _flash_diff ? " flash!" : "", _w_bin,
|
sprintf(reply, "> failed: %s%s [b%u d%u x%u t%u last %c@%lu sid %u]", _err, _flash_diff ? " flash!" : "",
|
||||||
_w_drop, _w_txt, _mani_src, (unsigned long)_mani_off, _sid);
|
_w_bin, _w_drop, _w_bad, _w_txt, _mani_src, (unsigned long)_mani_off, _sid);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -681,7 +689,7 @@ void FwUpdate::cmdBegin(const char *arg, char *reply) {
|
|||||||
_flash_diff = false;
|
_flash_diff = false;
|
||||||
memset(_mani, 0xFF, sizeof(_mani));
|
memset(_mani, 0xFF, sizeof(_mani));
|
||||||
_sid = (uint16_t)sid;
|
_sid = (uint16_t)sid;
|
||||||
_w_bin = _w_drop = _w_txt = 0;
|
_w_bin = _w_drop = _w_bad = _w_txt = 0;
|
||||||
_mani_src = '-';
|
_mani_src = '-';
|
||||||
_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),
|
||||||
|
|||||||
@@ -39,7 +39,8 @@
|
|||||||
|
|
||||||
// Binary request carrying firmware data (over the mesh, admin only). Upstream
|
// Binary request carrying firmware data (over the mesh, admin only). Upstream
|
||||||
// request types are 0x01..0x07; 0xF0 is far from them on purpose.
|
// 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
|
// length is explicit (the node only sees
|
||||||
// the decrypted size padded to 16 B); sid
|
// the decrypted size padded to 16 B); sid
|
||||||
// is the session from `fw begin <bytes>
|
// is the session from `fw begin <bytes>
|
||||||
@@ -116,7 +117,7 @@ private:
|
|||||||
uint8_t _mani[FW_OTA_MANIFEST_SIZE];
|
uint8_t _mani[FW_OTA_MANIFEST_SIZE];
|
||||||
uint16_t _sid = 0; // transfer session, 0 = none given (text-only senders)
|
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
|
// 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 = '-';
|
char _mani_src = '-';
|
||||||
uint32_t _mani_off = 0;
|
uint32_t _mani_off = 0;
|
||||||
bool _flash_diff = false;
|
bool _flash_diff = false;
|
||||||
|
|||||||
Reference in new issue
Block a user