From daaf0c22674bcb2abf53d760535bd25105aa5ba7 Mon Sep 17 00:00:00 2001 From: TeTeHacko Date: Thu, 12 Nov 2026 14:24:24 +0100 Subject: [PATCH] FwUpdate: admin-pushed OTA without a bootloader change (bench stage) --- platformio.ini | 4 + src/helpers/FwUpdate.cpp | 757 ++++++++++++++++++++++++++++++++ src/helpers/FwUpdate.h | 67 +++ src/helpers/NodeMaintenance.cpp | 116 ++--- src/helpers/NodeMaintenance.h | 32 +- tools/build_version.py | 46 +- 6 files changed, 939 insertions(+), 83 deletions(-) create mode 100644 src/helpers/FwUpdate.cpp create mode 100644 src/helpers/FwUpdate.h diff --git a/platformio.ini b/platformio.ini index faf1bf72..e0e6474b 100644 --- a/platformio.ini +++ b/platformio.ini @@ -128,6 +128,10 @@ build_flags = ${arduino_base.build_flags} -D NRF52_PLATFORM -D LFS_NO_ASSERT=1 -D EXTRAFS=1 +; Admin-pushed OTA (src/helpers/FwUpdate, docs/ota-admin.md): PUBLIC key the +; package signature is checked against. Bench key for now +; (tools/ota_pack.py keygen, private half in ~/.config/meshcore-ota/test.key). + -D FW_OTA_PUBKEY='"4c6086ca832ede490967b76323e0762c5394f128884af2103f5bedd8c46540ce"' -D USE_CC310_HW_CRYPTO=1 lib_deps = ${arduino_base.lib_deps} diff --git a/src/helpers/FwUpdate.cpp b/src/helpers/FwUpdate.cpp new file mode 100644 index 00000000..638ff1fd --- /dev/null +++ b/src/helpers/FwUpdate.cpp @@ -0,0 +1,757 @@ +#include "FwUpdate.h" + +#if defined(NRF52_PLATFORM) + +#include +#include +#include +#include +#include + +#ifndef FW_OTA_PUBKEY +#error "FW_OTA_PUBKEY (64 hex chars) must be defined for FwUpdate" +#endif +#ifndef FW_OTA_TARGET +#error "FW_OTA_TARGET (the env name) must be defined, tools/build_version.py does it" +#endif + +// Roles with a console (repeater, room server, sensor) never mount ExtraFS, so +// the whole space up to InternalFS is free. The companion would need 0xD4000. +#ifndef FW_OTA_STAGE_END +#define FW_OTA_STAGE_END 0xED000 +#endif + +#define APP_START 0x27000 // S140 7.x, all our nRF52840 boards +#define PAGE 4096u +#define BL_SETTINGS 0xFF000u // sdk11 bootloader_settings_t, nRF52840 +#define RING_BITS 15 +#define RING_SIZE (1u << RING_BITS) +#define RING_MASK (RING_SIZE - 1) + +extern "C" uint32_t __etext, __data_start__, __data_end__; + +// --- manifest (tools/ota_pack.py writes the same layout) ---------------------- +struct __attribute__((packed)) Manifest { + char magic[4]; // "MCFW" + uint8_t format; // 1 + uint8_t reserved0[3]; + uint32_t raw_size; + uint32_t comp_size; + uint8_t raw_sha[32]; + uint8_t comp_sha[32]; + char target[32]; + char version[48]; + uint8_t reserved1[32]; + uint8_t signature[64]; // Ed25519 over bytes 0..191 +}; +static_assert(sizeof(Manifest) == FW_OTA_MANIFEST_SIZE, "manifest layout"); + +// --- inflate ------------------------------------------------------------------ +// A small RFC 1951 decoder in the style of zlib's contrib/puff. EVERYTHING it +// touches during the copy must live in RAM, because the copy erases the flash +// the firmware runs from: the functions are placed in .data (copied to RAM by +// the startup code), the tables are non-const for the same reason, and no +// library call (memset, memcpy) may be emitted -- `fw verify` runs the very same +// code, and tools/ota_check_ramfunc.sh checks the disassembly for calls out. + +#define RAMFUNC \ + __attribute__((section(".data.ramfunc"), noinline, long_call, optimize("no-tree-loop-distribute-patterns"))) +// GCC moves a never-written static table to .rodata, i.e. flash, even when it +// is not const (seen: lbase..clorder at 0x7b9fc). Pin the tables to RAM. +#define RAMDATA __attribute__((section(".data.ramtab"))) + +typedef int (*FlushFn)(void *ctx, const uint8_t *data, uint32_t off, uint32_t len, uint32_t in_used); + +struct Inflate { + const uint8_t *in; + uint32_t inlen, incnt; + uint32_t bitbuf, bitcnt; + uint8_t *ring; + uint32_t outcnt, outmax; + FlushFn flush; + void *ctx; +}; + +struct Huff { + int16_t count[16]; + int16_t symbol[288]; +}; + +RAMDATA static uint16_t lbase[29] = { 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, + 31, 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258 }; +RAMDATA static uint16_t lext[29] = { 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, + 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0 }; +RAMDATA static uint16_t dbase[30] = { 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, + 33, 49, 65, 97, 129, 193, 257, 385, 513, 769, + 1025, 1537, 2049, 3073, 4097, 6145, 8193, 12289, 16385, 24577 }; +RAMDATA static uint16_t dext[30] = { 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, + 6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 13, 13 }; +RAMDATA static uint8_t clorder[19] = { 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15 }; +static Huff g_lencode, g_distcode; // .bss is RAM; static so the stack stays small +static int16_t g_lengths[320]; + +RAMFUNC static int inf_bits(Inflate *s, int need, uint32_t *out) { + uint32_t val = s->bitbuf; + while (s->bitcnt < (uint32_t)need) { + if (s->incnt == s->inlen) return -1; + val |= (uint32_t)s->in[s->incnt++] << s->bitcnt; + s->bitcnt += 8; + } + s->bitbuf = val >> need; + s->bitcnt -= need; + *out = val & ((1u << need) - 1); + return 0; +} + +RAMFUNC static int inf_put(Inflate *s, uint8_t b) { + if (s->outcnt >= s->outmax) return -2; + s->ring[s->outcnt & RING_MASK] = b; + s->outcnt++; + if ((s->outcnt % PAGE) == 0) { + uint32_t off = s->outcnt - PAGE; + return s->flush(s->ctx, &s->ring[off & RING_MASK], off, PAGE, s->incnt); + } + return 0; +} + +RAMFUNC static int inf_decode(Inflate *s, const Huff *h, int *sym) { + int code = 0, first = 0, index = 0; + for (int len = 1; len < 16; len++) { + uint32_t b; + if (inf_bits(s, 1, &b)) return -1; + code |= (int)b; + int count = h->count[len]; + if (code - count < first) { + *sym = h->symbol[index + (code - first)]; + return 0; + } + index += count; + first += count; + first <<= 1; + code <<= 1; + } + return -3; +} + +RAMFUNC static int inf_construct(Huff *h, const int16_t *length, int n) { + int16_t offs[16]; + for (int len = 0; len < 16; len++) + h->count[len] = 0; + for (int sym = 0; sym < n; sym++) + h->count[length[sym]]++; + if (h->count[0] == n) return 0; + int left = 1; + for (int len = 1; len < 16; len++) { + left <<= 1; + left -= h->count[len]; + if (left < 0) return left; + } + offs[1] = 0; + for (int len = 1; len < 15; len++) + offs[len + 1] = offs[len] + h->count[len]; + for (int sym = 0; sym < n; sym++) + if (length[sym] != 0) h->symbol[offs[length[sym]]++] = sym; + return left; +} + +RAMFUNC static int inf_codes(Inflate *s, const Huff *lc, const Huff *dc) { + int sym; + do { + if (inf_decode(s, lc, &sym)) return -4; + if (sym < 256) { + if (inf_put(s, (uint8_t)sym)) return -5; + } else if (sym > 256) { + sym -= 257; + if (sym >= 29) return -6; + uint32_t e; + if (inf_bits(s, lext[sym], &e)) return -1; + uint32_t len = lbase[sym] + e; + if (inf_decode(s, dc, &sym)) return -4; + if (sym >= 30) return -6; + if (inf_bits(s, dext[sym], &e)) return -1; + uint32_t dist = dbase[sym] + e; + if (dist > s->outcnt || dist > RING_SIZE) return -7; + while (len--) { + if (inf_put(s, s->ring[(s->outcnt - dist) & RING_MASK])) return -5; + } + } + } while (sym != 256); + return 0; +} + +RAMFUNC static int inf_stored(Inflate *s) { + s->bitbuf = 0; + s->bitcnt = 0; + if (s->incnt + 4 > s->inlen) return -1; + uint32_t len = s->in[s->incnt] | ((uint32_t)s->in[s->incnt + 1] << 8); + uint32_t nlen = s->in[s->incnt + 2] | ((uint32_t)s->in[s->incnt + 3] << 8); + s->incnt += 4; + if (len != (~nlen & 0xffff)) return -8; + if (s->incnt + len > s->inlen) return -1; + while (len--) + if (inf_put(s, s->in[s->incnt++])) return -5; + return 0; +} + +RAMFUNC static int inf_fixed(Inflate *s) { + int sym = 0; + for (; sym < 144; sym++) + g_lengths[sym] = 8; + for (; sym < 256; sym++) + g_lengths[sym] = 9; + for (; sym < 280; sym++) + g_lengths[sym] = 7; + for (; sym < 288; sym++) + g_lengths[sym] = 8; + inf_construct(&g_lencode, g_lengths, 288); + for (sym = 0; sym < 30; sym++) + g_lengths[sym] = 5; + inf_construct(&g_distcode, g_lengths, 30); + return inf_codes(s, &g_lencode, &g_distcode); +} + +RAMFUNC static int inf_dynamic(Inflate *s) { + uint32_t nlen, ndist, ncode, v; + if (inf_bits(s, 5, &nlen)) return -1; + nlen += 257; + if (inf_bits(s, 5, &ndist)) return -1; + ndist += 1; + if (inf_bits(s, 4, &ncode)) return -1; + ncode += 4; + if (nlen > 286 || ndist > 30) return -9; + uint32_t index; + for (index = 0; index < ncode; index++) { + if (inf_bits(s, 3, &v)) return -1; + g_lengths[clorder[index]] = (int16_t)v; + } + for (; index < 19; index++) + g_lengths[clorder[index]] = 0; + if (inf_construct(&g_lencode, g_lengths, 19) != 0) return -10; + index = 0; + while (index < nlen + ndist) { + int sym; + if (inf_decode(s, &g_lencode, &sym)) return -4; + if (sym < 16) { + g_lengths[index++] = (int16_t)sym; + } else { + int16_t len = 0; + uint32_t rep; + if (sym == 16) { + if (index == 0) return -11; + len = g_lengths[index - 1]; + if (inf_bits(s, 2, &rep)) return -1; + rep += 3; + } else if (sym == 17) { + if (inf_bits(s, 3, &rep)) return -1; + rep += 3; + } else { + if (inf_bits(s, 7, &rep)) return -1; + rep += 11; + } + if (index + rep > nlen + ndist) return -12; + while (rep--) + g_lengths[index++] = len; + } + } + if (g_lengths[256] == 0) return -13; + int err = inf_construct(&g_lencode, g_lengths, nlen); + if (err && (err < 0 || nlen != (uint32_t)(g_lencode.count[0] + g_lencode.count[1]))) return -14; + err = inf_construct(&g_distcode, g_lengths + nlen, ndist); + if (err && (err < 0 || ndist != (uint32_t)(g_distcode.count[0] + g_distcode.count[1]))) return -15; + return inf_codes(s, &g_lencode, &g_distcode); +} + +// Raw deflate stream -> flush() per 4 KB page, the last page short. +RAMFUNC static int inf_run(Inflate *s) { + uint32_t last, type; + int err; + do { + if (inf_bits(s, 1, &last)) return -1; + if (inf_bits(s, 2, &type)) return -1; + if (type == 0) + err = inf_stored(s); + else if (type == 1) + err = inf_fixed(s); + else if (type == 2) + err = inf_dynamic(s); + else + err = -16; + if (err) return err; + } while (!last); + uint32_t tail = s->outcnt % PAGE; + if (tail) { + uint32_t off = s->outcnt - tail; + return s->flush(s->ctx, &s->ring[off & RING_MASK], off, tail, s->incnt); + } + return 0; +} + +// --- the copy, RAM only --------------------------------------------------------- + +struct CopyCtx { + uint32_t stage_blob; // flash address of the compressed stream + const uint32_t *settings; // RAM copy of the bootloader settings page +}; + +RAMFUNC static void nvmc_wait() { + while (NRF_NVMC->READY == NVMC_READY_READY_Busy) { + } +} + +RAMFUNC static void nvmc_erase(uint32_t addr) { + NRF_NVMC->CONFIG = NVMC_CONFIG_WEN_Een; + nvmc_wait(); + NRF_NVMC->ERASEPAGE = addr; + nvmc_wait(); + NRF_NVMC->CONFIG = NVMC_CONFIG_WEN_Ren; + nvmc_wait(); +} + +RAMFUNC static void nvmc_write(uint32_t addr, const uint8_t *src, uint32_t len) { + NRF_NVMC->CONFIG = NVMC_CONFIG_WEN_Wen; + nvmc_wait(); + for (uint32_t i = 0; i < len; i += 4) { + uint32_t w = 0xFFFFFFFFu; + for (uint32_t b = 0; b < 4 && i + b < len; b++) { + w &= ~((uint32_t)0xFF << (8 * b)); + w |= (uint32_t)src[i + b] << (8 * b); + } + *(volatile uint32_t *)(addr + i) = w; + nvmc_wait(); + } + NRF_NVMC->CONFIG = NVMC_CONFIG_WEN_Ren; + nvmc_wait(); +} + +RAMFUNC static void wdt_feed() { + if (NRF_WDT->RUNSTATUS) NRF_WDT->RR[0] = WDT_RR_RR_Reload; +} + +RAMFUNC static int copy_flush(void *vctx, const uint8_t *data, uint32_t off, uint32_t len, uint32_t in_used) { + CopyCtx *c = (CopyCtx *)vctx; + uint32_t addr = APP_START + off; + // never overwrite compressed input that has not been read yet + if (addr + PAGE > c->stage_blob + in_used) return -20; + wdt_feed(); + nvmc_erase(addr); + nvmc_write(addr, data, len); + return 0; +} + +RAMFUNC static void copy_and_reset(Inflate *s, CopyCtx *c) { + // 1) the bootloader learns the new CRC before a single app byte changes + wdt_feed(); + nvmc_erase(BL_SETTINGS); + nvmc_write(BL_SETTINGS, (const uint8_t *)c->settings, PAGE); + // 2) inflate over the running image + inf_run(s); + // 3) whatever happened, reset; a CRC mismatch keeps the node in the bootloader + wdt_feed(); + __DSB(); + SCB->AIRCR = (0x5FAUL << SCB_AIRCR_VECTKEY_Pos) | SCB_AIRCR_SYSRESETREQ_Msk; + __DSB(); + for (;;) { + } +} + +// --- helpers -------------------------------------------------------------------- + +static uint16_t crc16_update(uint16_t crc, const uint8_t *p, uint32_t len) { + // Nordic crc16_compute (CRC-16-CCITT, init 0xFFFF), as the bootloader checks it + for (uint32_t i = 0; i < len; i++) { + crc = (uint16_t)((crc >> 8) | (crc << 8)); + crc ^= p[i]; + crc ^= (uint16_t)((crc & 0xFF) >> 4); + crc ^= (uint16_t)((crc << 8) << 4); + crc ^= (uint16_t)(((crc & 0xFF) << 4) << 1); + } + return crc; +} + +static int hexval(char c) { + if (c >= '0' && c <= '9') return c - '0'; + if (c >= 'a' && c <= 'f') return c - 'a' + 10; + if (c >= 'A' && c <= 'F') return c - 'A' + 10; + return -1; +} + +static bool pubkey(uint8_t out[32]) { + const char *hex = FW_OTA_PUBKEY; + if (strlen(hex) != 64) return false; + for (int i = 0; i < 32; i++) { + int hi = hexval(hex[2 * i]), lo = hexval(hex[2 * i + 1]); + if (hi < 0 || lo < 0) return false; + out[i] = (uint8_t)(hi << 4 | lo); + } + return true; +} + +static int b64val(char c) { + if (c >= 'A' && c <= 'Z') return c - 'A'; + if (c >= 'a' && c <= 'z') return c - 'a' + 26; + if (c >= '0' && c <= '9') return c - '0' + 52; + if (c == '+') return 62; + if (c == '/') return 63; + return -1; +} + +static int b64decode(const char *in, uint8_t *out, int max) { + int n = 0, bits = 0; + uint32_t acc = 0; + for (; *in && *in != '='; in++) { + int v = b64val(*in); + if (v < 0) return -1; + acc = (acc << 6) | (uint32_t)v; + bits += 6; + if (bits >= 8) { + bits -= 8; + if (n >= max) return -1; + out[n++] = (uint8_t)(acc >> bits); + } + } + return n; +} + +static uint32_t imageEnd() { + uint32_t data_len = (uint32_t)&__data_end__ - (uint32_t)&__data_start__; + return (uint32_t)&__etext + data_len; +} + +struct VerifyCtx { + SHA256 sha; + uint16_t crc; + uint32_t stage_blob; + bool overlap; +}; + +static int verify_flush(void *vctx, const uint8_t *data, uint32_t off, uint32_t len, uint32_t in_used) { + VerifyCtx *v = (VerifyCtx *)vctx; + v->sha.update(data, len); + v->crc = crc16_update(v->crc, data, len); + if (APP_START + off + PAGE > v->stage_blob + in_used) v->overlap = true; + return 0; +} + +// --- commands ------------------------------------------------------------------- + +uint32_t FwUpdate::received() const { + uint32_t n = (_total + FW_OTA_CHUNK - 1) / FW_OTA_CHUNK, have = 0; + for (uint32_t i = 0; i < n; i++) + if (_have[i >> 3] & (1 << (i & 7))) have++; + return have; +} + +uint16_t FwUpdate::battMinMv() { + uint16_t mv = 0xFFFF; + for (int i = 0; i < 5; i++) { + uint16_t v = _board.getBattMilliVolts(); + if (v < mv) mv = v; + delay(100); + } + return mv; +} + +void FwUpdate::cmdStatus(char *reply) { + uint16_t mv = _board.getBattMilliVolts(); + uint32_t n = (_total + FW_OTA_CHUNK - 1) / FW_OTA_CHUNK; + switch (_state) { + case IDLE: + sprintf(reply, "> idle, batt %umV (min %u), free %lu B", mv, FW_OTA_MIN_MV, + (unsigned long)(FW_OTA_STAGE_END - ((imageEnd() + PAGE - 1) & ~(PAGE - 1)))); + break; + case RECEIVING: + sprintf(reply, "> rx %lu/%lu chunks, batt %umV", (unsigned long)received(), (unsigned long)n, mv); + break; + case READY: + sprintf(reply, "> ready %s, %lu B, crc %04X, batt %umV", _version, (unsigned long)_raw_size, _crc, mv); + break; + case FAILED: + sprintf(reply, "> failed: %s", _err); + break; + } +} + +void FwUpdate::cmdBegin(const char *arg, char *reply) { + uint32_t total = strtoul(arg, NULL, 10); + uint32_t stage = (imageEnd() + PAGE - 1) & ~(PAGE - 1); + if (total <= FW_OTA_MANIFEST_SIZE) { + strcpy(reply, "ERR: fw begin "); + return; + } + if (stage + total > FW_OTA_STAGE_END) { + sprintf(reply, "ERR: %lu B does not fit, %lu free", (unsigned long)total, + (unsigned long)(FW_OTA_STAGE_END - stage)); + return; + } + _stage = stage; + _total = total; + memset(_have, 0, sizeof(_have)); + _state = RECEIVING; + _apply_at = 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); +} + +void FwUpdate::cmdWrite(const char *arg, char *reply) { + if (_state != RECEIVING) { + strcpy(reply, "ERR: not receiving"); + return; + } + char *end; + uint32_t off = strtoul(arg, &end, 10); + while (*end == ' ') + 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)); + } + strcpy(reply, "ok"); +} + +void FwUpdate::cmdMissing(char *reply) { + if (_state != RECEIVING) { + strcpy(reply, "ERR: not receiving"); + return; + } + uint32_t n = (_total + FW_OTA_CHUNK - 1) / FW_OTA_CHUNK; + int pos = sprintf(reply, ">"); + int listed = 0; + for (uint32_t i = 0; i < n && pos < 140; i++) { + if (!(_have[i >> 3] & (1 << (i & 7)))) { + pos += sprintf(reply + pos, " %lu", (unsigned long)(i * FW_OTA_CHUNK)); + listed++; + } + } + if (!listed) strcpy(reply, "> none"); +} + +void FwUpdate::cmdVerify(char *reply) { + if (_state == READY) { + cmdStatus(reply); + return; + } + if (_state != RECEIVING) { + strcpy(reply, "ERR: nothing staged"); + return; + } + uint32_t n = (_total + FW_OTA_CHUNK - 1) / FW_OTA_CHUNK; + if (received() != n) { + strcpy(reply, "ERR: incomplete, see fw missing"); + return; + } + flash_nrf5x_flush(); + + _state = FAILED; + const Manifest *m = (const Manifest *)_stage; + uint8_t key[32]; + if (memcmp(m->magic, "MCFW", 4) != 0 || m->format != 1) { + strcpy(_err, "not a package"); + goto fail; + } + if (!pubkey(key)) { + strcpy(_err, "bad FW_OTA_PUBKEY"); + goto fail; + } + if (!ed25519_verify(m->signature, (const uint8_t *)m, offsetof(Manifest, signature), key)) { + strcpy(_err, "bad signature"); + goto fail; + } + if (strncmp(m->target, FW_OTA_TARGET, sizeof(m->target)) != 0) { + strcpy(_err, "wrong target"); + goto fail; + } + if (FW_OTA_MANIFEST_SIZE + m->comp_size != _total) { + strcpy(_err, "size mismatch"); + goto fail; + } + if (APP_START + m->raw_size > FW_OTA_STAGE_END) { + strcpy(_err, "image too big"); + goto fail; + } + { + uint8_t h[32]; + SHA256 sha; + sha.update((const uint8_t *)(_stage + FW_OTA_MANIFEST_SIZE), m->comp_size); + sha.finalize(h, 32); + if (memcmp(h, m->comp_sha, 32) != 0) { + strcpy(_err, "blob hash"); + goto fail; + } + } + { + uint8_t *ring = (uint8_t *)malloc(RING_SIZE); + VerifyCtx *v = new VerifyCtx(); + if (!ring || !v) { + free(ring); + delete v; + strcpy(_err, "no RAM"); + goto fail; + } + v->crc = 0xFFFF; + v->stage_blob = _stage + FW_OTA_MANIFEST_SIZE; + v->overlap = false; + Inflate s = {}; + s.in = (const uint8_t *)v->stage_blob; + s.inlen = m->comp_size; + s.ring = ring; + s.outmax = m->raw_size; + s.flush = verify_flush; + s.ctx = v; + int err = inf_run(&s); + uint8_t h[32]; + v->sha.finalize(h, 32); + _crc = v->crc; + bool overlap = v->overlap; + free(ring); + delete v; + if (err) { + sprintf(_err, "inflate %d", err); + goto fail; + } + if (s.outcnt != m->raw_size || memcmp(h, m->raw_sha, 32) != 0) { + strcpy(_err, "image hash"); + goto fail; + } + if (overlap) { + strcpy(_err, "image would overrun the package"); + goto fail; + } + } + _raw_size = m->raw_size; + strncpy(_version, m->version, sizeof(_version) - 1); + _version[sizeof(_version) - 1] = 0; + _state = READY; + cmdStatus(reply); + return; +fail: + sprintf(reply, "ERR: %s", _err); +} + +void FwUpdate::cmdBootloader(char *reply) { + const volatile uint16_t *s16 = (const volatile uint16_t *)BL_SETTINGS; + const volatile uint32_t *s32 = (const volatile uint32_t *)BL_SETTINGS; + sprintf(reply, "> bank0=%04X crc=%04X bank1=%04X size=%lu", s16[0], s16[1], s16[2], (unsigned long)s32[2]); +} + +void FwUpdate::cmdApply(uint32_t sender_timestamp, const char *arg, char *reply) { + if (_state != READY) { + strcpy(reply, "ERR: nothing verified, fw verify first"); + return; + } + bool force = strcmp(arg, "force") == 0; + if (force && sender_timestamp != 0) { + strcpy(reply, "ERR: force is local-only"); + return; + } + uint16_t bank0 = *(const volatile uint16_t *)BL_SETTINGS; + if (bank0 != 0x0001 && bank0 != 0xFFFF) { + sprintf(reply, "ERR: unknown bootloader settings (bank0=%04X)", bank0); + return; + } + uint16_t mv = battMinMv(); + if (!force) { + 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; + } + } + _apply_at = millis() + 2000; // let the reply go out first + sprintf(reply, "OK applying %s in 2 s (batt %umV), back in ~20 s", _version, mv); +} + +void FwUpdate::loop() { + if (_apply_at == 0 || (long)(millis() - _apply_at) < 0) return; + _apply_at = 0; + if (_state != READY) return; + + uint32_t *settings = (uint32_t *)malloc(PAGE); + uint8_t *ring = (uint8_t *)malloc(RING_SIZE); + static Inflate s; + static CopyCtx c; + if (!settings || !ring) { + free(settings); + free(ring); + _state = FAILED; + strcpy(_err, "no RAM for apply"); + return; + } + + memcpy(settings, (const void *)BL_SETTINGS, PAGE); + uint16_t *s16 = (uint16_t *)settings; + s16[0] = 0x0001; // BANK_VALID_APP + s16[1] = _crc; // bank_0_crc + s16[2] = 0x00FF; // bank_1 = BANK_INVALID_APP + settings[2] = _raw_size; // bank_0_size + + c.stage_blob = _stage + FW_OTA_MANIFEST_SIZE; + c.settings = settings; + memset(&s, 0, sizeof(s)); + s.in = (const uint8_t *)c.stage_blob; + s.inlen = ((const Manifest *)_stage)->comp_size; + s.ring = ring; + s.outmax = _raw_size; + s.flush = copy_flush; + s.ctx = &c; + + Serial.flush(); + NRF_USBD->USBPULLUP = 0; // a clean disconnect, see NodeMaintenance::dfuTick + delay(100); + uint8_t sd = 0; + sd_softdevice_is_enabled(&sd); + if (sd) sd_softdevice_disable(); + __disable_irq(); + copy_and_reset(&s, &c); +} + +bool FwUpdate::handleCommand(uint32_t sender_timestamp, const char *command, char *reply) { + if (memcmp(command, "fw", 2) != 0 || (command[2] != 0 && command[2] != ' ')) return false; + const char *arg = command[2] ? &command[3] : ""; + while (*arg == ' ') + arg++; + if (strncmp(arg, "w ", 2) == 0) + cmdWrite(arg + 2, reply); + else if (strcmp(arg, "status") == 0 || !*arg) + cmdStatus(reply); + else if (strncmp(arg, "begin ", 6) == 0) + cmdBegin(arg + 6, reply); + else if (strcmp(arg, "missing") == 0) + cmdMissing(reply); + else if (strcmp(arg, "verify") == 0) + cmdVerify(reply); + else if (strncmp(arg, "apply", 5) == 0) { + const char *a = arg + 5; + while (*a == ' ') + a++; + cmdApply(sender_timestamp, a, reply); + } else if (strcmp(arg, "abort") == 0) { + _state = IDLE; + _apply_at = 0; + strcpy(reply, "OK"); + } else if (strcmp(arg, "bl") == 0) + cmdBootloader(reply); + else + strcpy(reply, "ERR: fw status|begin|w|missing|verify|apply|abort|bl"); + return true; +} + +#else + +bool FwUpdate::handleCommand(uint32_t, const char *command, char *reply) { + if (memcmp(command, "fw", 2) != 0 || (command[2] != 0 && command[2] != ' ')) return false; + strcpy(reply, "ERR: not an nRF52 board"); + return true; +} +void FwUpdate::loop() {} + +#endif diff --git a/src/helpers/FwUpdate.h b/src/helpers/FwUpdate.h new file mode 100644 index 00000000..87898d89 --- /dev/null +++ b/src/helpers/FwUpdate.h @@ -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 start a transfer of a package (tools/ota_pack.py) +// 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] battery gate, then copy and reboot +// 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 +#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]; +}; diff --git a/src/helpers/NodeMaintenance.cpp b/src/helpers/NodeMaintenance.cpp index 6ab7aeba..46120cd4 100644 --- a/src/helpers/NodeMaintenance.cpp +++ b/src/helpers/NodeMaintenance.cpp @@ -7,10 +7,10 @@ #if defined(NRF52_PLATFORM) extern "C" { - void enterUf2Dfu(void); - void enterSerialDfu(void); - void enterOTADfu(void); - extern uint32_t g_hardfault_record[4]; // NRF52Board.cpp, .noinit: magic, pc, lr, psr +void enterUf2Dfu(void); +void enterSerialDfu(void); +void enterOTADfu(void); +extern uint32_t g_hardfault_record[4]; // NRF52Board.cpp, .noinit: magic, pc, lr, psr } #endif @@ -30,21 +30,21 @@ void NodeMaintenance::printBootDiag() { // the build epoch" from "restored something" -- and comparing against the // epoch of a DIFFERENT build of the same tree invents discrepancies that are // not there. Cost one wrong conclusion here. - Serial.printf("boot: FIRMWARE_BUILD_EPOCH=%lu\n", (unsigned long) FIRMWARE_BUILD_EPOCH); + Serial.printf("boot: FIRMWARE_BUILD_EPOCH=%lu\n", (unsigned long)FIRMWARE_BUILD_EPOCH); Serial.printf("boot: RESETREAS=0x%08lX%s%s%s%s%s\n", (unsigned long)rr, - rr == 0 ? " (none set = power-on/brownout: power was removed)" : "", - (rr & 0x01) ? " RESETPIN" : "", - (rr & 0x02) ? " WATCHDOG" : "", - (rr & 0x04) ? " SREQ(sw reset, e.g. `dfu`/`reboot`)" : "", - (rr & 0x08) ? " LOCKUP(!)" : ""); + rr == 0 ? " (none set = power-on/brownout: power was removed)" : "", + (rr & 0x01) ? " RESETPIN" : "", (rr & 0x02) ? " WATCHDOG" : "", + (rr & 0x04) ? " SREQ(sw reset, e.g. `dfu`/`reboot`)" : "", (rr & 0x08) ? " LOCKUP(!)" : ""); - volatile uint32_t* hf = g_hardfault_record; + volatile uint32_t *hf = g_hardfault_record; if (hf[0] == 0xFA010DEB) { - _hardfault[0] = hf[1]; _hardfault[1] = hf[2]; _hardfault[2] = hf[3]; + _hardfault[0] = hf[1]; + _hardfault[1] = hf[2]; + _hardfault[2] = hf[3]; _hardfault_seen = true; - Serial.printf("!!! HARDFAULT before last reboot: PC=%08lX LR=%08lX PSR=%08lX\n", - (unsigned long)hf[1], (unsigned long)hf[2], (unsigned long)hf[3]); - hf[0] = 0; // printed once; `get boot` keeps it until the next reset + Serial.printf("!!! HARDFAULT before last reboot: PC=%08lX LR=%08lX PSR=%08lX\n", (unsigned long)hf[1], + (unsigned long)hf[2], (unsigned long)hf[3]); + hf[0] = 0; // printed once; `get boot` keeps it until the next reset } #endif } @@ -53,7 +53,7 @@ void NodeMaintenance::printBootDiag() { // Reboots it caused and the threshold backoff survive power loss in a small file, // written only right before such a reboot and once when the radio proves itself. -void NodeMaintenance::silenceLoad(uint8_t* backoff) { +void NodeMaintenance::silenceLoad(uint8_t *backoff) { *backoff = 0; if (!_fs || !_fs->exists(RADIO_SILENCE_FILE)) return; #if defined(RP2040_PLATFORM) @@ -62,7 +62,7 @@ void NodeMaintenance::silenceLoad(uint8_t* backoff) { File f = _fs->open(RADIO_SILENCE_FILE); #endif if (!f) return; - if (f.read((uint8_t*)&_silence_reboots, sizeof(_silence_reboots)) != sizeof(_silence_reboots)) { + if (f.read((uint8_t *)&_silence_reboots, sizeof(_silence_reboots)) != sizeof(_silence_reboots)) { _silence_reboots = 0; } if (f.read(backoff, 1) != 1) *backoff = 0; @@ -80,12 +80,12 @@ void NodeMaintenance::silenceSave(uint8_t backoff) { File f = _fs->open(RADIO_SILENCE_FILE, "w", true); #endif if (!f) return; - f.write((const uint8_t*)&_silence_reboots, sizeof(_silence_reboots)); + f.write((const uint8_t *)&_silence_reboots, sizeof(_silence_reboots)); f.write(&backoff, 1); f.close(); } -void NodeMaintenance::begin(FILESYSTEM* fs, NodePrefs* prefs, CommonCLICallbacks* callbacks) { +void NodeMaintenance::begin(FILESYSTEM *fs, NodePrefs *prefs, CommonCLICallbacks *callbacks) { _fs = fs; _prefs = prefs; _callbacks = callbacks; @@ -94,58 +94,59 @@ void NodeMaintenance::begin(FILESYSTEM* fs, NodePrefs* prefs, CommonCLICallbacks _silence.begin(millis(), radioActivity(), backoff); _silence.setMinutes(_prefs->radio_silence_min); Serial.printf("boot: radio silence watchdog %u min, reboots %lu, backoff x%u\n", - (unsigned) _silence.minutes(), (unsigned long) _silence_reboots, - (unsigned) (1u << _silence.backoff())); + (unsigned)_silence.minutes(), (unsigned long)_silence_reboots, + (unsigned)(1u << _silence.backoff())); } void NodeMaintenance::loop() { dfuTick(); + _fw.loop(); switch (_silence.poll(millis(), radioActivity())) { - case RadioSilenceWatchdog::KICK_RX: - Serial.printf("radio silence: nothing received for %lu min, re-arming RX\n", - (unsigned long) (_silence.quietMs(millis()) / 60000)); - _radio.kickRx(); - break; - case RadioSilenceWatchdog::REBOOT: - Serial.printf("radio silence: still nothing after %lu min, rebooting\n", - (unsigned long) (_silence.quietMs(millis()) / 60000)); - _silence_reboots++; - silenceSave(_silence.nextBackoff()); - delay(100); // let the line out - _board.reboot(); - break; - default: - break; + case RadioSilenceWatchdog::KICK_RX: + Serial.printf("radio silence: nothing received for %lu min, re-arming RX\n", + (unsigned long)(_silence.quietMs(millis()) / 60000)); + _radio.kickRx(); + break; + case RadioSilenceWatchdog::REBOOT: + Serial.printf("radio silence: still nothing after %lu min, rebooting\n", + (unsigned long)(_silence.quietMs(millis()) / 60000)); + _silence_reboots++; + silenceSave(_silence.nextBackoff()); + delay(100); // let the line out + _board.reboot(); + break; + default: + break; } if (_silence.takeBackoffCleared()) silenceSave(0); } -bool NodeMaintenance::silenceCommand(const char* command, char* reply) { +bool NodeMaintenance::silenceCommand(const char *command, char *reply) { if (strcmp(command, "get radio.reboots") == 0) { - sprintf(reply, "> %lu", (unsigned long) _silence_reboots); + sprintf(reply, "> %lu", (unsigned long)_silence_reboots); return true; } if (strcmp(command, "get radio.silence") == 0) { if (_silence.minutes() == 0) { strcpy(reply, "> off"); } else { - sprintf(reply, "> %u min (now x%u), quiet %lu min, stage %u", - (unsigned) _silence.minutes(), (unsigned) (1u << _silence.backoff()), - (unsigned long) (_silence.quietMs(millis()) / 60000), (unsigned) _silence.stage()); + sprintf(reply, "> %u min (now x%u), quiet %lu min, stage %u", (unsigned)_silence.minutes(), + (unsigned)(1u << _silence.backoff()), (unsigned long)(_silence.quietMs(millis()) / 60000), + (unsigned)_silence.stage()); } return true; } if (memcmp(command, "set radio.silence ", 18) == 0) { - const char* arg = &command[18]; + const char *arg = &command[18]; bool numeric = *arg != 0 && strspn(arg, "0123456789") == strlen(arg); int m = strcmp(arg, "off") == 0 ? 0 : (numeric ? atoi(arg) : -1); if (m < 0 || m > RadioSilenceWatchdog::MAX_MINUTES) { - sprintf(reply, "ERR - minutes 0..%u (0/off = disabled)", (unsigned) RadioSilenceWatchdog::MAX_MINUTES); + sprintf(reply, "ERR - minutes 0..%u (0/off = disabled)", (unsigned)RadioSilenceWatchdog::MAX_MINUTES); return true; } - _prefs->radio_silence_min = (uint16_t) m; + _prefs->radio_silence_min = (uint16_t)m; _callbacks->savePrefs(); - _silence.setMinutes((uint16_t) m); + _silence.setMinutes((uint16_t)m); strcpy(reply, "OK"); return true; } @@ -213,21 +214,25 @@ void NodeMaintenance::dfuTick() { // from here, and it is exactly what detach() does on this chip. NRF_USBD->USBPULLUP = 0; delay(150); - if (_dfu_mode == 1) enterSerialDfu(); - else if (_dfu_mode == 2) enterOTADfu(); - else enterUf2Dfu(); + if (_dfu_mode == 1) + enterSerialDfu(); + else if (_dfu_mode == 2) + enterOTADfu(); + else + enterUf2Dfu(); #endif } -bool NodeMaintenance::dfuCommand(uint32_t sender_timestamp, const char* command, char* reply) { +bool NodeMaintenance::dfuCommand(uint32_t sender_timestamp, const char *command, char *reply) { if (memcmp(command, "dfu", 3) != 0 || (command[3] != 0 && command[3] != ' ')) return false; #if defined(NRF52_PLATFORM) if (sender_timestamp != 0) { strcpy(reply, "ERR: dfu is local-only (would strand the node in the bootloader)"); return true; } - const char* arg = (command[3] == ' ') ? &command[4] : ""; - while (*arg == ' ') arg++; + const char *arg = (command[3] == ' ') ? &command[4] : ""; + while (*arg == ' ') + arg++; if (strcmp(arg, "serial") == 0) { strcpy(reply, "OK - rebooting into serial DFU (CDC only, no drive)"); } else if (strcmp(arg, "ota") == 0) { @@ -246,17 +251,18 @@ bool NodeMaintenance::dfuCommand(uint32_t sender_timestamp, const char* command, return true; } -bool NodeMaintenance::handleCommand(uint32_t sender_timestamp, const char* command, char* reply) { +bool NodeMaintenance::handleCommand(uint32_t sender_timestamp, const char *command, char *reply) { if (dfuCommand(sender_timestamp, command, reply)) return true; + if (_fw.handleCommand(sender_timestamp, command, reply)) return true; if (silenceCommand(command, reply)) return true; if (strcmp(command, "get boot") == 0) { #if defined(NRF52_PLATFORM) if (_hardfault_seen) { sprintf(reply, "> RESETREAS=0x%08lX, HARDFAULT before it: PC=%08lX LR=%08lX PSR=%08lX", - (unsigned long) _reset_reason, (unsigned long) _hardfault[0], - (unsigned long) _hardfault[1], (unsigned long) _hardfault[2]); + (unsigned long)_reset_reason, (unsigned long)_hardfault[0], (unsigned long)_hardfault[1], + (unsigned long)_hardfault[2]); } else { - sprintf(reply, "> RESETREAS=0x%08lX, no hardfault", (unsigned long) _reset_reason); + sprintf(reply, "> RESETREAS=0x%08lX, no hardfault", (unsigned long)_reset_reason); } #else strcpy(reply, "> not recorded on this platform"); diff --git a/src/helpers/NodeMaintenance.h b/src/helpers/NodeMaintenance.h index 438fcfea..c9ecb0d0 100644 --- a/src/helpers/NodeMaintenance.h +++ b/src/helpers/NodeMaintenance.h @@ -11,6 +11,7 @@ // line per hook, and the upstream files it touches stay one added line each. // // dfu [uf2|serial|ota] reboot into the bootloader (local console only) +// fw ... admin-pushed firmware update, see helpers/FwUpdate.h // set radio.silence radio silence watchdog, 0/off = disabled (#6) // get radio.silence threshold, backoff, quiet time, stage // get radio.reboots reboots done by the watchdog, survives power loss @@ -25,45 +26,48 @@ #include #include #include +#include #include #include class NodeMaintenance { public: - NodeMaintenance(mesh::MainBoard& board, RadioLibWrapper& radio) : _board(board), _radio(radio) { } + NodeMaintenance(mesh::MainBoard &board, RadioLibWrapper &radio) + : _board(board), _radio(radio), _fw(board) {} // Prints (and latches for `get boot`) the reset reason and the hardfault record. // The record is cleared once read, so call it once, as early as Serial is up. void printBootDiag(); - void begin(FILESYSTEM* fs, NodePrefs* prefs, CommonCLICallbacks* callbacks); + void begin(FILESYSTEM *fs, NodePrefs *prefs, CommonCLICallbacks *callbacks); void loop(); // sender_timestamp == 0 means the local console (the same test CommonCLI uses // for `erase`); `dfu` refuses anything else. - bool handleCommand(uint32_t sender_timestamp, const char* command, char* reply); + bool handleCommand(uint32_t sender_timestamp, const char *command, char *reply); private: uint32_t radioActivity() const { return _radio.getPacketsRecv() + _radio.getPacketsRecvErrors(); } - void silenceLoad(uint8_t* backoff); + void silenceLoad(uint8_t *backoff); void silenceSave(uint8_t backoff); - bool dfuCommand(uint32_t sender_timestamp, const char* command, char* reply); - bool silenceCommand(const char* command, char* reply); + bool dfuCommand(uint32_t sender_timestamp, const char *command, char *reply); + bool silenceCommand(const char *command, char *reply); void dfuTick(); - mesh::MainBoard& _board; - RadioLibWrapper& _radio; - FILESYSTEM* _fs = NULL; - NodePrefs* _prefs = NULL; - CommonCLICallbacks* _callbacks = NULL; + mesh::MainBoard &_board; + RadioLibWrapper &_radio; + FILESYSTEM *_fs = NULL; + NodePrefs *_prefs = NULL; + CommonCLICallbacks *_callbacks = NULL; + FwUpdate _fw; RadioSilenceWatchdog _silence; uint32_t _silence_reboots = 0; - unsigned long _dfu_at = 0; // 0 = nothing armed - uint8_t _dfu_mode = 0; // 0=uf2 1=serial 2=ota + unsigned long _dfu_at = 0; // 0 = nothing armed + uint8_t _dfu_mode = 0; // 0=uf2 1=serial 2=ota uint32_t _reset_reason = 0; - uint32_t _hardfault[3] = {0, 0, 0}; // pc, lr, psr; pc 0 = none + uint32_t _hardfault[3] = { 0, 0, 0 }; // pc, lr, psr; pc 0 = none bool _hardfault_seen = false; }; diff --git a/tools/build_version.py b/tools/build_version.py index 5b80deea..8eacc509 100644 --- a/tools/build_version.py +++ b/tools/build_version.py @@ -25,8 +25,11 @@ from datetime import datetime def git(cwd, *args): - return subprocess.check_output(("git",) + args, cwd=cwd, - stderr=subprocess.DEVNULL).decode().strip() + return ( + subprocess.check_output(("git",) + args, cwd=cwd, stderr=subprocess.DEVNULL) + .decode() + .strip() + ) cwd = env.subst("$PROJECT_DIR") @@ -44,15 +47,24 @@ cwd = env.subst("$PROJECT_DIR") # version string of the thing it was protecting. base = "nover" try: - tag = git(cwd, "describe", "--tags", "--abbrev=0", - "--match", "*v[0-9]*", "--exclude", "*+tth*", - "--exclude", "backup/*") + tag = git( + cwd, + "describe", + "--tags", + "--abbrev=0", + "--match", + "*v[0-9]*", + "--exclude", + "*+tth*", + "--exclude", + "backup/*", + ) # Cut at the 'v' that STARTS THE VERSION, not at the first 'v' in the tag: # "room-server-v1.17.0" has one in "server", and find("v") turned the base # into "ver-v1.17.0". Every earlier upstream tag we saw (companion-, repeater-) # happened to have no 'v' in its prefix, so this hid until v1.17.0. m = re.search(r"v[0-9]", tag) - base = tag[m.start():] if m else tag + base = tag[m.start() :] if m else tag except Exception: pass @@ -79,10 +91,12 @@ VER_LIMIT, DATE_LIMIT = 19, 11 # so this only bites if upstream's version string grows unusually long. marker = "-tth" room = VER_LIMIT - len(base) - len(marker) - len(dirty) -version = "%s%s%s%s" % (base, marker, sha[:max(4, room)], dirty) +version = "%s%s%s%s" % (base, marker, sha[: max(4, room)], dirty) if len(version) > VER_LIMIT: - print("build_version.py: WARNING: %r is %d chars, will truncate to %d on " - "the companion protocol" % (version, len(version), VER_LIMIT)) + print( + "build_version.py: WARNING: %r is %d chars, will truncate to %d on " + "the companion protocol" % (version, len(version), VER_LIMIT) + ) # yymmdd hhmm = exactly 11. Not pretty, but complete and sortable; the readable # "%d %b %Y %H:%M" is 17 and lost its time to the 12-byte field. @@ -107,9 +121,13 @@ build_date = now.strftime("%y%m%d %H%M")[:DATE_LIMIT] # the tree is already fully recompiled each time. build_epoch = int(now.timestamp()) -env.Append(CPPDEFINES=[ - ("FIRMWARE_VERSION", env.StringifyMacro(version)), - ("FIRMWARE_BUILD_DATE", env.StringifyMacro(build_date)), - ("FIRMWARE_BUILD_EPOCH", str(build_epoch)), -]) +env.Append( + CPPDEFINES=[ + ("FIRMWARE_VERSION", env.StringifyMacro(version)), + ("FIRMWARE_BUILD_DATE", env.StringifyMacro(build_date)), + ("FIRMWARE_BUILD_EPOCH", str(build_epoch)), + # src/helpers/FwUpdate refuses a package built for another env + ("FW_OTA_TARGET", env.StringifyMacro(env["PIOENV"])), + ] +) print("build_version.py: %s (Build: %s, epoch %d)" % (version, build_date, build_epoch))