FwUpdate: transfer session id; binary chunks of another session are dropped

This commit is contained in:
tth committed 2026-11-13 01:49:55 +01:00
1 parent 3a7140fd51
commit b4d2601c8f
2 files changed
+20 -8

No files matched your search

+11 -5
View File
@@ -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) { 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 // the payload arrives padded to the AES block: trust the length byte, not len
uint16_t sid;
uint32_t off; uint32_t off;
memcpy(&off, &req[2], 4); memcpy(&sid, &req[2], 2);
int n = req[6]; memcpy(&off, &req[4], 4);
if (7 + n <= len) writeAt(off, &req[7], n); // errors show in the next 'm' 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; return 0;
} }
if (len >= 2 && req[1] == 'm' && reply_max >= 5) { 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) { 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; // 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 // 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); 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; _job = JOB_NONE;
_flash_diff = false; _flash_diff = false;
memset(_mani, 0xFF, sizeof(_mani)); memset(_mani, 0xFF, sizeof(_mani));
_sid = (uint16_t)sid;
_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),
FW_OTA_CHUNK, (unsigned long)stage); FW_OTA_CHUNK, (unsigned long)stage);
+9 -3
View File
@@ -39,9 +39,14 @@
// 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'][offset u32 LE][len u8][data] chunk, no reply. The length is // [0xF0]['w'][sid u16][offset u32 LE][len u8][data] chunk, no reply. The
// explicit: the node only sees the // length is explicit (the node only sees
// decrypted size padded to 16 B. // the decrypted size padded to 16 B); sid
// is the session from `fw begin <bytes>
// <sid>`, 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] // [0xF0]['m'] reply: [state][units u16][count u16]
// [missing unit index u16 ...] // [missing unit index u16 ...]
// state: 0 idle, 1 receiving, 2 ready, 3 failed, 4 busy (manifest/verify) // 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 // that was still in flash at the same address. _flash_diff records a readback
// mismatch after the flush, for diagnosis (`fw status`). // mismatch after the flush, for diagnosis (`fw status`).
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)
bool _flash_diff = false; bool _flash_diff = false;
char _version[48] = { 0 }; char _version[48] = { 0 };
char _err[64] = { 0 }; char _err[64] = { 0 };