FwUpdate: chunk offset overflow, no heavy work in handlers, crc 0 refused
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
a029863d66
commit
46d33bd2b0
2 files changed
+65
-23
No files matched your search
+61
-22
@@ -493,7 +493,10 @@ uint32_t FwUpdate::received() const {
|
||||
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))
|
||||
// never `off + len > _total`: off comes from the sender, 0xFFFFFFE0 + 32 wraps to
|
||||
// 0 and the write would land below the stage, in the running image
|
||||
if (len <= 0 || off % FW_OTA_UNIT || off > _total || (uint32_t)len > _total - off ||
|
||||
(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;
|
||||
@@ -572,6 +575,23 @@ int FwUpdate::handleBinary(const uint8_t *req, int len, uint8_t *reply, int repl
|
||||
return 0;
|
||||
}
|
||||
|
||||
// USB VBUS present: the copy cannot run out of power, and a board without
|
||||
// a cell reads only ~3.7 V on VBAT (x4 on the bench), so the gate would
|
||||
// refuse a perfectly safe apply.
|
||||
const char *FwUpdate::battGate(uint16_t mv, bool force) {
|
||||
static char why[40];
|
||||
if (force || _board.isExternalPowered()) return NULL;
|
||||
if (mv < 2000) {
|
||||
sprintf(why, "battery unknown (%umV)", mv);
|
||||
return why;
|
||||
}
|
||||
if (mv < FW_OTA_MIN_MV) {
|
||||
sprintf(why, "batt %umV < %u", mv, FW_OTA_MIN_MV);
|
||||
return why;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
uint16_t FwUpdate::battMinMv() {
|
||||
uint16_t mv = 0xFFFF;
|
||||
for (int i = 0; i < 5; i++) {
|
||||
@@ -656,6 +676,10 @@ void FwUpdate::cmdStatus(char *reply) {
|
||||
(unsigned long)n, mv, _flash_diff ? ", flash!" : "", _w_bin, _w_drop, _w_bad, _w_txt);
|
||||
break;
|
||||
case READY:
|
||||
if (_apply_err[0]) {
|
||||
sprintf(reply, "> ready %s%s, apply refused: %s", _version, _format == 2 ? " (delta)" : "", _apply_err);
|
||||
break;
|
||||
}
|
||||
sprintf(reply, "> ready %s%s, %lu B, crc %04X, batt %umV [b%u d%u x%u t%u]", _version,
|
||||
_format == 2 ? " (delta)" : "", (unsigned long)_raw_size, _crc, mv, _w_bin, _w_drop, _w_bad, _w_txt);
|
||||
break;
|
||||
@@ -695,6 +719,7 @@ void FwUpdate::cmdBegin(const char *arg, char *reply) {
|
||||
_w_bin = _w_drop = _w_bad = _w_txt = 0;
|
||||
_mani_src = '-';
|
||||
_apply_at = 0;
|
||||
_apply_err[0] = 0;
|
||||
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);
|
||||
}
|
||||
@@ -882,26 +907,31 @@ void FwUpdate::cmdApply(uint32_t sender_timestamp, const char *arg, char *reply)
|
||||
sprintf(reply, "ERR: unknown bootloader settings (bank0=%04X)", bank0);
|
||||
return;
|
||||
}
|
||||
uint16_t mv = battMinMv();
|
||||
// USB VBUS present: the copy cannot run out of power, and a board without
|
||||
// a cell reads only ~3.7 V on VBAT (x4 on the bench), so the gate would
|
||||
// refuse a perfectly safe apply.
|
||||
bool ext = _board.isExternalPowered();
|
||||
if (!force && !ext) {
|
||||
if (mv < 2000) {
|
||||
sprintf(reply, "ERR: battery unknown (%umV), fw apply force (local)", mv);
|
||||
return;
|
||||
}
|
||||
if (mv < FW_OTA_MIN_MV) {
|
||||
sprintf(reply, "ERR: batt %umV < %u", mv, FW_OTA_MIN_MV);
|
||||
return;
|
||||
}
|
||||
// The bootloader skips its CRC check when the stored crc is 0 -- a power cut
|
||||
// mid-copy would then boot half an image. One build in 65536; rebuild it.
|
||||
if (_crc == 0) {
|
||||
strcpy(reply, "ERR: image crc is 0000 (disables the bootloader check), rebuild");
|
||||
return;
|
||||
}
|
||||
// One quick read for the reply; loop() repeats the gate with the slow min of 5
|
||||
// reads right before the rewrite (500 ms of delay() has no place in the packet path).
|
||||
uint16_t mv = _board.getBattMilliVolts();
|
||||
const char *why = battGate(mv, force);
|
||||
if (why) {
|
||||
sprintf(reply, "ERR: %s%s", why, mv < 2000 ? ", fw apply force (local)" : "");
|
||||
return;
|
||||
}
|
||||
_apply_force = force;
|
||||
_apply_err[0] = 0;
|
||||
_apply_at = millis() + 2000; // let the reply go out first
|
||||
sprintf(reply, "OK applying %s in 2 s (batt %umV%s), back in ~20 s", _version, mv, ext ? ", USB" : "");
|
||||
sprintf(reply, "OK applying %s in 2 s (batt %umV%s), back in ~20 s", _version, mv,
|
||||
_board.isExternalPowered() ? ", USB" : "");
|
||||
}
|
||||
|
||||
void FwUpdate::loop() {
|
||||
// sha256 of ~400 kB takes a few hundred ms: once here, right after boot, so
|
||||
// `fw status` (the first command of every transfer) never runs it in a handler
|
||||
if (!_base_known) baseSha();
|
||||
if (_job == JOB_MANIFEST) {
|
||||
_job = JOB_NONE;
|
||||
flash_nrf5x_flush();
|
||||
@@ -922,6 +952,11 @@ void FwUpdate::loop() {
|
||||
if (_apply_at == 0 || (long)(millis() - _apply_at) < 0) return;
|
||||
_apply_at = 0;
|
||||
if (_state != READY) return;
|
||||
const char *why = battGate(battMinMv(), _apply_force);
|
||||
if (why) {
|
||||
strncpy(_apply_err, why, sizeof(_apply_err) - 1); // `fw status` shows it
|
||||
return;
|
||||
}
|
||||
|
||||
uint32_t *settings = (uint32_t *)malloc(PAGE);
|
||||
uint8_t *ring = _format == 2 ? (uint8_t *)1 : (uint8_t *)malloc(RING_SIZE); // delta needs no ring
|
||||
@@ -997,6 +1032,7 @@ bool FwUpdate::handleCommand(uint32_t sender_timestamp, const char *command, cha
|
||||
cmdApply(sender_timestamp, a, reply);
|
||||
} else if (strcmp(arg, "abort") == 0) {
|
||||
_state = IDLE;
|
||||
_job = JOB_NONE; // a queued verify would turn IDLE back into READY
|
||||
_apply_at = 0;
|
||||
strcpy(reply, "OK");
|
||||
} else if (strcmp(arg, "bl") == 0)
|
||||
@@ -1026,16 +1062,19 @@ bool FwUpdate::handleCommand(uint32_t sender_timestamp, const char *command, cha
|
||||
for (int i = 0; i < r && pos < 150; i++)
|
||||
pos += sprintf(reply + pos, "%02x", out[i]);
|
||||
} else if (strcmp(arg, "base") == 0) {
|
||||
// full sha256 of the running image: the cached value and a fresh one
|
||||
uint8_t fresh[32];
|
||||
SHA256 sha;
|
||||
sha.update((const uint8_t *)APP_START, imageEnd() - APP_START);
|
||||
sha.finalize(fresh, 32);
|
||||
// full sha256 of the running image: the cached value, and on the local
|
||||
// console also a fresh one (not over the mesh: hundreds of ms in the handler)
|
||||
const uint8_t *c = baseSha();
|
||||
int pos = sprintf(reply, "> ");
|
||||
for (int i = 0; i < 32; i++)
|
||||
pos += sprintf(reply + pos, "%02x", c[i]);
|
||||
sprintf(reply + pos, " %s", memcmp(c, fresh, 32) == 0 ? "ok" : "CACHE DIFFERS");
|
||||
if (sender_timestamp == 0) {
|
||||
uint8_t fresh[32];
|
||||
SHA256 sha;
|
||||
sha.update((const uint8_t *)APP_START, imageEnd() - APP_START);
|
||||
sha.finalize(fresh, 32);
|
||||
sprintf(reply + pos, " %s", memcmp(c, fresh, 32) == 0 ? "ok" : "CACHE DIFFERS");
|
||||
}
|
||||
}
|
||||
else
|
||||
strcpy(reply, "ERR: fw status|begin|w|missing|verify|apply|abort|bl|base");
|
||||
|
||||
@@ -90,7 +90,8 @@ private:
|
||||
void doVerify();
|
||||
void cmdApply(uint32_t sender_timestamp, const char *arg, char *reply);
|
||||
void cmdBootloader(char *reply);
|
||||
uint16_t battMinMv();
|
||||
uint16_t battMinMv(); // 5 reads 100 ms apart: loop() only, never a handler
|
||||
const char *battGate(uint16_t mv, bool force); // NULL = ok
|
||||
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)); }
|
||||
@@ -124,6 +125,8 @@ private:
|
||||
char _version[48] = { 0 };
|
||||
char _err[64] = { 0 };
|
||||
unsigned long _apply_at = 0;
|
||||
bool _apply_force = false;
|
||||
char _apply_err[40] = { 0 }; // why loop() refused the queued apply
|
||||
static constexpr uint32_t MAX_UNITS = (0xED000 - 0x27000) / FW_OTA_UNIT + 1; // whole stage
|
||||
uint8_t _have[(MAX_UNITS + 7) / 8];
|
||||
};
|
||||
Reference in new issue
Block a user