#pragma once // CUSTOM (TeTeHacko): firmware update pushed by an admin, no bootloader change. // Design and the reasons behind it: docs/ota.md. // // Two package formats, one signed 256 B manifest: // 1 deflate-compressed full image, staged in the free flash above the running // one, inflated over it by a RAM routine; // 2 detools in-place DELTA against the running image (FwPatch.cpp), staged in // the top FW_OTA_PATCH_AREA below InternalFS. The manifest names the base // (sha256 of the running image) and the node refuses a package for another // base as soon as the manifest chunks arrive. // For both, the node checks it completely (signature, hashes, a dry-run inflate), and // only then, on an explicit `fw apply`, disables the SoftDevice and runs a copy // routine from RAM that inflates the image over itself. The bootloader settings // page gets the CRC of the new image FIRST, so a power cut mid-copy leaves a // node that stays in the bootloader (BLE/USB DFU recovers it) instead of one // that jumps into half an image. // // fw status state, progress, battery // fw begin start a transfer of a package (tools/ota_pack.py); // over the mesh only a delta (<= FW_OTA_PATCH_AREA) // fw w one chunk, offset a multiple of FW_OTA_CHUNK // fw missing first missing chunk offsets (for resending) // fw verify signature + hashes + dry-run inflate // fw apply [force] [downgrade] battery gate, then copy and reboot; both // arguments local only (no downgrade over the mesh) // fw ver target and build epoch a package for this image needs // fw abort forget the staged package // fw bl print the bootloader settings page #include #include #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 128 // binary chunk: 4 units; the companion host frame (176 B) limits it // 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'][sid u16][offset u32 LE][len u8][crc16 of sid..len + data][data] chunk, // no reply; a bad crc is dropped. The // length is explicit (the node only sees // the decrypted size padded to 16 B); sid // is the session from `fw begin // `, 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] // [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 #ifndef FW_OTA_PATCH_AREA #define FW_OTA_PATCH_AREA 0x10000 // delta packages (manifest + patch) up to 64 kB #endif #define FW_OTA_MANIFEST_SIZE 256 class FwUpdate { public: FwUpdate(mesh::MainBoard &board) : _board(board) {} // 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: enum State : uint8_t { IDLE, RECEIVING, READY, FAILED }; // Heavy work (ed25519, sha256 of the running image, the dry run) is never done // inside a command handler: over the mesh that runs deep in the packet path, // and on x1 (13. 11. 2026) the manifest check there overflowed the stack -- // hardfault in RadioLib's SPI code moments later. The handler queues it, // loop() runs it with a shallow stack, `fw status` reports the result. enum Job : uint8_t { JOB_NONE, JOB_MANIFEST, JOB_VERIFY }; void cmdStatus(char *reply); void cmdBegin(uint32_t sender_timestamp, const char *arg, char *reply); void cmdWrite(uint32_t sender_timestamp, const char *arg, char *reply); void cmdMissing(char *reply); void cmdVerify(char *reply); void doVerify(); void cmdApply(uint32_t sender_timestamp, const char *arg, char *reply); void cmdBootloader(char *reply); 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)); } 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 mesh::MainBoard &_board; State _state = IDLE; uint32_t _stage = 0; // flash address of the package uint32_t _total = 0; // package size (manifest + blob) uint32_t _raw_size = 0; uint16_t _crc = 0; uint8_t _format = 0; Job _job = JOB_NONE; bool _manifest_ok = false; // Any part of the transfer came over the mesh (remote CLI or a binary // request). Then only a DELTA is accepted: a full image is ~300 kB, hours of // airtime on a shared channel -- it goes over USB or the BLE console. bool _remote = false; bool _older = false; // the package is not newer than this image (manifest build_epoch) bool _base_known = false; uint8_t _base_sha[32]; // The manifest is checked from this RAM copy, not from the stage in flash: three // bench runs failed with "wrong base image" while the node's base and the // package's matched exactly -- the check had read an older package's manifest // that was still in flash at the same address. _flash_diff records a readback // mismatch after the flush, for diagnosis (`fw status`). uint8_t _mani[FW_OTA_MANIFEST_SIZE]; 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 uint16_t _w_bin = 0, _w_drop = 0, _w_bad = 0, _w_txt = 0; char _mani_src = '-'; uint32_t _mani_off = 0; bool _flash_diff = false; 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]; };