FwUpdate: binary firmware chunks over the mesh (REQ_TYPE_FW_OTA 0xF0)
This commit is contained in:
1 parent
5f8788c4b4
commit
637b0d78b7
7 files changed
+126
-28
No files matched your search
@@ -477,6 +477,14 @@ int MyMesh::handleRequest(ClientInfo *sender, uint32_t sender_timestamp, uint8_t
|
||||
// memcpy(reply_data, &now, 4); // response packets always prefixed with timestamp
|
||||
memcpy(reply_data, &sender_timestamp, 4); // reflect sender_timestamp back in response packet (kind of like a 'tag')
|
||||
|
||||
// CUSTOM (TeTeHacko): firmware chunks for FwUpdate (REQ_TYPE_FW_OTA = 0xF0, admin
|
||||
// only, helpers/FwUpdate.h). 0 = no reply: chunks are fire and forget.
|
||||
if (payload_len >= 1 && payload[0] == 0xF0) {
|
||||
extern int maintHandleRequest(bool admin, const uint8_t* req, int len, uint8_t* reply, int reply_max);
|
||||
int n = maintHandleRequest(sender->isAdmin(), payload, (int)payload_len, &reply_data[4], 160);
|
||||
return n > 0 ? 4 + n : 0;
|
||||
}
|
||||
|
||||
if (payload[0] == REQ_TYPE_GET_STATUS) { // guests can also access this now
|
||||
RepeaterStats stats;
|
||||
stats.batt_milli_volts = board.getBattMilliVolts();
|
||||
|
||||
@@ -652,6 +652,11 @@ bool maintHandleCommand(uint32_t sender_timestamp, const char* command, char* re
|
||||
return maint.handleCommand(sender_timestamp, command, reply);
|
||||
}
|
||||
|
||||
// CUSTOM (TeTeHacko): binary firmware chunks (REQ_TYPE_FW_OTA), see helpers/FwUpdate.h.
|
||||
int maintHandleRequest(bool admin, const uint8_t* req, int len, uint8_t* reply, int reply_max) {
|
||||
return maint.handleRequest(admin, req, len, reply, reply_max);
|
||||
}
|
||||
|
||||
#ifdef RADIO_DIAG
|
||||
// CUSTOM (TeTeHacko): `radiostat` / `radiokick` -- see RadioLibWrappers.cpp.
|
||||
// On the masts since 2. 11. 2026: mast-three came up deaf after a BLE OTA (recv 0,
|
||||
|
||||
@@ -155,6 +155,14 @@ int MyMesh::handleRequest(ClientInfo *sender, uint32_t sender_timestamp, uint8_t
|
||||
// memcpy(reply_data, &now, 4); // response packets always prefixed with timestamp
|
||||
memcpy(reply_data, &sender_timestamp, 4); // reflect sender_timestamp back in response packet (kind of like a 'tag')
|
||||
|
||||
// CUSTOM (TeTeHacko): firmware chunks for FwUpdate (REQ_TYPE_FW_OTA = 0xF0, admin
|
||||
// only, helpers/FwUpdate.h). 0 = no reply: chunks are fire and forget.
|
||||
if (payload_len >= 1 && payload[0] == 0xF0) {
|
||||
extern int maintHandleRequest(bool admin, const uint8_t* req, int len, uint8_t* reply, int reply_max);
|
||||
int n = maintHandleRequest(sender->isAdmin(), payload, (int)payload_len, &reply_data[4], 160);
|
||||
return n > 0 ? 4 + n : 0;
|
||||
}
|
||||
|
||||
if (payload[0] == REQ_TYPE_GET_STATUS) {
|
||||
ServerStats stats;
|
||||
stats.batt_milli_volts = board.getBattMilliVolts();
|
||||
|
||||
@@ -24,6 +24,11 @@ bool maintHandleCommand(uint32_t sender_timestamp, const char* command, char* re
|
||||
return maint.handleCommand(sender_timestamp, command, reply);
|
||||
}
|
||||
|
||||
// CUSTOM (TeTeHacko): binary firmware chunks (REQ_TYPE_FW_OTA), see helpers/FwUpdate.h.
|
||||
int maintHandleRequest(bool admin, const uint8_t* req, int len, uint8_t* reply, int reply_max) {
|
||||
return maint.handleRequest(admin, req, len, reply, reply_max);
|
||||
}
|
||||
|
||||
void halt() {
|
||||
while (1) ;
|
||||
}
|
||||
|
||||
+72
-25
@@ -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;
|
||||
}
|
||||
|
||||
+21
-3
@@ -32,6 +32,18 @@
|
||||
#ifndef FW_OTA_CHUNK
|
||||
#define FW_OTA_CHUNK 96 // base64 = 128 chars, fits the 160 B CLI line
|
||||
#endif
|
||||
// Received data is tracked in units of FW_OTA_UNIT bytes, so text chunks (96 B)
|
||||
// and binary chunks (FW_OTA_BCHUNK) can both land in it.
|
||||
#define FW_OTA_UNIT 32
|
||||
#define FW_OTA_BCHUNK 160 // binary chunk: 5 units, fits one encrypted REQ (166 B max)
|
||||
|
||||
// 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][data] chunk, no reply (fire and forget)
|
||||
// [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)
|
||||
#define REQ_TYPE_FW_OTA 0xF0
|
||||
#ifndef FW_OTA_MIN_MV
|
||||
#define FW_OTA_MIN_MV 3800 // ~50 % of a Li-ion cell at rest
|
||||
#endif
|
||||
@@ -48,6 +60,9 @@ public:
|
||||
|
||||
// sender_timestamp == 0 means the local console.
|
||||
bool handleCommand(uint32_t sender_timestamp, const char *command, char *reply);
|
||||
// Binary REQ_TYPE_FW_OTA payload (req[0] is the type). Returns the reply
|
||||
// length, 0 = no reply. The caller checks that the sender is an admin.
|
||||
int handleBinary(const uint8_t *req, int len, uint8_t *reply, int reply_max);
|
||||
void loop();
|
||||
|
||||
private:
|
||||
@@ -68,7 +83,10 @@ private:
|
||||
void cmdApply(uint32_t sender_timestamp, const char *arg, char *reply);
|
||||
void cmdBootloader(char *reply);
|
||||
uint16_t battMinMv();
|
||||
uint32_t received() const;
|
||||
uint32_t received() const; // units
|
||||
uint32_t units() const { return (_total + FW_OTA_UNIT - 1) / FW_OTA_UNIT; }
|
||||
bool haveUnit(uint32_t u) const { return _have[u >> 3] & (1 << (u & 7)); }
|
||||
const char *writeAt(uint32_t off, const uint8_t *buf, int len); // NULL = ok
|
||||
bool checkManifest(); // signature, target, base; sets _err
|
||||
const uint8_t *baseSha(); // sha256 of the running image, computed once
|
||||
|
||||
@@ -86,6 +104,6 @@ private:
|
||||
char _version[48] = { 0 };
|
||||
char _err[48] = { 0 };
|
||||
unsigned long _apply_at = 0;
|
||||
static constexpr uint32_t MAX_CHUNKS = (0x100000 / FW_OTA_CHUNK) + 1;
|
||||
uint8_t _have[(MAX_CHUNKS + 7) / 8];
|
||||
static constexpr uint32_t MAX_UNITS = (0xED000 - 0x27000) / FW_OTA_UNIT + 1; // whole stage
|
||||
uint8_t _have[(MAX_UNITS + 7) / 8];
|
||||
};
|
||||
@@ -46,6 +46,13 @@ public:
|
||||
// for `erase`); `dfu` refuses anything else.
|
||||
bool handleCommand(uint32_t sender_timestamp, const char *command, char *reply);
|
||||
|
||||
// Binary request from a logged-in client (REQ_TYPE_FW_OTA, firmware chunks).
|
||||
// Returns the reply length, 0 = no reply. Admins only.
|
||||
int handleRequest(bool admin, const uint8_t *req, int len, uint8_t *reply, int reply_max) {
|
||||
if (!admin || len < 1 || req[0] != REQ_TYPE_FW_OTA) return 0;
|
||||
return _fw.handleBinary(req, len, reply, reply_max);
|
||||
}
|
||||
|
||||
private:
|
||||
uint32_t radioActivity() const { return _radio.getPacketsRecv() + _radio.getPacketsRecvErrors(); }
|
||||
void silenceLoad(uint8_t *backoff);
|
||||
|
||||
Reference in new issue
Block a user