FwUpdate: binary firmware chunks over the mesh (REQ_TYPE_FW_OTA 0xF0)

This commit is contained in:
tth committed 2026-11-13 00:55:06 +01:00
1 parent 5f8788c4b4
commit 637b0d78b7
7 files changed
+126 -28

No files matched your search

+72 -25
View File
@@ -482,12 +482,69 @@ static int verify_flush(void *vctx, const uint8_t *data, uint32_t off, uint32_t
// --- commands -------------------------------------------------------------------
uint32_t FwUpdate::received() const {
uint32_t n = (_total + FW_OTA_CHUNK - 1) / FW_OTA_CHUNK, have = 0;
uint32_t n = units(), have = 0;
for (uint32_t i = 0; i < n; i++)
if (_have[i >> 3] & (1 << (i & 7))) have++;
if (haveUnit(i)) have++;
return have;
}
// One chunk from either transport. Offsets on unit boundaries, lengths whole
// units except at the end of the package. Rewriting a chunk is harmless.
const char *FwUpdate::writeAt(uint32_t off, const uint8_t *buf, int len) {
if (_state == FAILED) return _err;
if (_state != RECEIVING) return "not receiving";
if (len <= 0 || off % FW_OTA_UNIT || off + len > _total || (len % FW_OTA_UNIT && off + len != _total))
return "bad chunk";
uint32_t u0 = off / FW_OTA_UNIT, u1 = (off + len + FW_OTA_UNIT - 1) / FW_OTA_UNIT;
bool all = true;
for (uint32_t u = u0; u < u1; u++)
if (!haveUnit(u)) all = false;
if (!all) {
flash_nrf5x_write(_stage + off, buf, len);
for (uint32_t u = u0; u < u1; u++)
_have[u >> 3] |= (1 << (u & 7));
}
if (!_manifest_ok && _job == JOB_NONE) {
// as soon as the manifest is complete: refuse a package for another board or
// another base before the rest of it is sent (loop() checks it)
bool m = true;
for (uint32_t u = 0; u * FW_OTA_UNIT < FW_OTA_MANIFEST_SIZE; u++)
if (!haveUnit(u)) m = false;
if (m) _job = JOB_MANIFEST;
}
return NULL;
}
int FwUpdate::handleBinary(const uint8_t *req, int len, uint8_t *reply, int reply_max) {
if (len >= 6 && req[1] == 'w') {
uint32_t off;
memcpy(&off, &req[2], 4);
writeAt(off, &req[6], len - 6); // errors show in the next 'm'
return 0;
}
if (len >= 2 && req[1] == 'm' && reply_max >= 5) {
uint8_t st = _state == IDLE ? 0 : _state == READY ? 2 : _state == FAILED ? 3 : 1;
if (_job != JOB_NONE) st = 4;
uint16_t n = (uint16_t)units(), cnt = 0;
reply[0] = st;
memcpy(&reply[1], &n, 2);
int pos = 5;
if (_state == RECEIVING) {
for (uint32_t u = 0; u < n && pos + 2 <= reply_max; u++) {
if (!haveUnit(u)) {
uint16_t v = (uint16_t)u;
memcpy(&reply[pos], &v, 2);
pos += 2;
cnt++;
}
}
}
memcpy(&reply[3], &cnt, 2);
return pos;
}
return 0;
}
uint16_t FwUpdate::battMinMv() {
uint16_t mv = 0xFFFF;
for (int i = 0; i < 5; i++) {
@@ -551,7 +608,7 @@ bool FwUpdate::checkManifest() {
void FwUpdate::cmdStatus(char *reply) {
uint16_t mv = _board.getBattMilliVolts();
uint32_t n = (_total + FW_OTA_CHUNK - 1) / FW_OTA_CHUNK;
uint32_t n = units();
switch (_state) {
case IDLE:
{
@@ -566,7 +623,7 @@ void FwUpdate::cmdStatus(char *reply) {
strcpy(reply, "> verifying");
break;
}
sprintf(reply, "> rx %lu/%lu chunks, batt %umV", (unsigned long)received(), (unsigned long)n, mv);
sprintf(reply, "> rx %lu/%lu units, batt %umV", (unsigned long)received(), (unsigned long)n, mv);
break;
case READY:
sprintf(reply, "> ready %s%s, %lu B, crc %04X, batt %umV", _version, _format == 2 ? " (delta)" : "",
@@ -619,24 +676,11 @@ void FwUpdate::cmdWrite(const char *arg, char *reply) {
end++;
uint8_t buf[FW_OTA_CHUNK];
int len = b64decode(end, buf, sizeof(buf));
if (len <= 0 || off % FW_OTA_CHUNK || off + len > _total || (len != FW_OTA_CHUNK && off + len != _total)) {
strcpy(reply, "ERR: bad chunk");
return;
}
uint32_t idx = off / FW_OTA_CHUNK;
if (!(_have[idx >> 3] & (1 << (idx & 7)))) {
flash_nrf5x_write(_stage + off, buf, len);
_have[idx >> 3] |= (1 << (idx & 7));
}
if (!_manifest_ok) {
// as soon as the manifest is complete: refuse a package for another board or
// another base before the rest of it is sent
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 && _job == JOB_NONE) _job = JOB_MANIFEST; // loop() checks it
}
strcpy(reply, "ok");
const char *err = writeAt(off, buf, len);
if (err)
sprintf(reply, "ERR: %s", err);
else
strcpy(reply, "ok");
}
void FwUpdate::cmdMissing(char *reply) {
@@ -647,8 +691,12 @@ void FwUpdate::cmdMissing(char *reply) {
uint32_t n = (_total + FW_OTA_CHUNK - 1) / FW_OTA_CHUNK;
int pos = sprintf(reply, ">");
int listed = 0;
const uint32_t per = FW_OTA_CHUNK / FW_OTA_UNIT;
for (uint32_t i = 0; i < n && pos < 140; i++) {
if (!(_have[i >> 3] & (1 << (i & 7)))) {
bool have = true;
for (uint32_t u = i * per; u < (i + 1) * per && u < units(); u++)
if (!haveUnit(u)) have = false;
if (!have) {
pos += sprintf(reply + pos, " %lu", (unsigned long)(i * FW_OTA_CHUNK));
listed++;
}
@@ -665,8 +713,7 @@ void FwUpdate::cmdVerify(char *reply) {
strcpy(reply, "ERR: nothing staged");
return;
}
uint32_t n = (_total + FW_OTA_CHUNK - 1) / FW_OTA_CHUNK;
if (received() != n) {
if (received() != units()) {
strcpy(reply, "ERR: incomplete, see fw missing");
return;
}