FwUpdate: admin-pushed OTA without a bootloader change (bench stage)

This commit is contained in:
tth committed 2026-11-12 14:24:24 +01:00
1 parent ad4f53016c
commit daaf0c2267
6 files changed
+939 -83

No files matched your search

+67
View File
@@ -0,0 +1,67 @@
#pragma once
// CUSTOM (TeTeHacko): firmware update pushed by an admin, no bootloader change.
// Design and the reasons behind it: docs/ota-admin.md.
//
// The node stores a signed, deflate-compressed image in the free flash above the
// running one, 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 <bytes> start a transfer of a package (tools/ota_pack.py)
// fw w <offset> <base64> 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] battery gate, then copy and reboot
// fw abort forget the staged package
// fw bl print the bootloader settings page
#include <Arduino.h>
#include <Mesh.h>
#ifndef FW_OTA_CHUNK
#define FW_OTA_CHUNK 96 // base64 = 128 chars, fits the 160 B CLI line
#endif
#ifndef FW_OTA_MIN_MV
#define FW_OTA_MIN_MV 3800 // ~50 % of a Li-ion cell at rest
#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);
void loop();
private:
enum State : uint8_t { IDLE, RECEIVING, READY, FAILED };
void cmdStatus(char *reply);
void cmdBegin(const char *arg, char *reply);
void cmdWrite(const char *arg, char *reply);
void cmdMissing(char *reply);
void cmdVerify(char *reply);
void cmdApply(uint32_t sender_timestamp, const char *arg, char *reply);
void cmdBootloader(char *reply);
uint16_t battMinMv();
uint32_t received() const;
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;
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];
};