ota: platform apply + flash staging (ESP32 A/B, nRF52 in-place via bootloader)
This commit is contained in:
1 parent
5da783040f
commit
9b61169c16
8 files changed
+1263
No files matched your search
@@ -0,0 +1,509 @@
|
||||
#include "OtaApply.h"
|
||||
#include "OtaFormat.h"
|
||||
#include "MotaContainer.h"
|
||||
#include "Identity.h"
|
||||
#include <string.h>
|
||||
|
||||
#if defined(ESP32_PLATFORM)
|
||||
#include <SHA256.h> // rweather streaming SHA-256 (for hashing the slot in chunks)
|
||||
#include "esp_ota_ops.h"
|
||||
#include "esp_partition.h"
|
||||
#include "esp_system.h"
|
||||
extern "C" {
|
||||
#include "detools/detools.h" // vendored detools 0.53.0 embeddable decoder (CRLE-only build)
|
||||
}
|
||||
#if defined(OTA_FLASH_STORE)
|
||||
#include "OtaStoreFlashEsp32.h" // flash-staged container (delta patch / full image in the slot)
|
||||
#include "OtaSelf.h" // SelfFwInfo / ota_self_firmware (running-image base_hash gate)
|
||||
#endif
|
||||
#elif defined(NRF52_PLATFORM)
|
||||
#include "OtaVerify.h"
|
||||
#include "OtaSelf.h"
|
||||
#include "OtaFlashLayout_nrf52.h"
|
||||
#include "OtaBlInfo.h" // read the bootloader capability marker before arming an apply
|
||||
#include "flash/flash_nrf5x.h" // Adafruit core internal-flash driver (has its own extern "C")
|
||||
#include "nrf.h"
|
||||
#include "nrf_soc.h"
|
||||
#include "nrf_sdm.h"
|
||||
#endif
|
||||
|
||||
namespace mesh {
|
||||
namespace ota {
|
||||
|
||||
#if defined(ESP32_PLATFORM)
|
||||
|
||||
bool ota_apply_slot_info(uint32_t* addr, uint32_t* size) {
|
||||
const esp_partition_t* p = esp_ota_get_next_update_partition(nullptr);
|
||||
if (!p) return false;
|
||||
if (addr) *addr = p->address;
|
||||
if (size) *size = p->size;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ota_apply_set_manifest(const uint8_t* mf, uint32_t len, const SignerAllowlist& allow, ApplyState& st) {
|
||||
st = ApplyState();
|
||||
ota_apply_slot_info(&st.slot_addr, &st.slot_size);
|
||||
MotaManifest m;
|
||||
if (!mota_parse_manifest(mf, len, m)) return false;
|
||||
if (!m.is_full()) return false; // A/B apply takes a full image (delta would need decode)
|
||||
st.image_size = m.image_size;
|
||||
memcpy(st.image_hash, m.image_hash, 32);
|
||||
st.manifest_ok = true;
|
||||
if (m.is_signed()) {
|
||||
mesh::Identity signer(m.signer_pubkey);
|
||||
st.sig_ok = signer.verify(m.signature, m.manifest_start, (int)m.signed_len);
|
||||
st.trusted = st.sig_ok && allow.contains(m.signer_pubkey);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ota_apply_verify_slot(ApplyState& st) {
|
||||
st.slot_ok = false;
|
||||
if (!st.manifest_ok || st.image_size == 0 || st.image_size > st.slot_size) return false;
|
||||
const esp_partition_t* p = esp_ota_get_next_update_partition(nullptr);
|
||||
if (!p) return false;
|
||||
SHA256 sha;
|
||||
uint8_t buf[512];
|
||||
uint32_t off = 0;
|
||||
while (off < st.image_size) {
|
||||
uint32_t n = st.image_size - off; if (n > sizeof(buf)) n = sizeof(buf);
|
||||
if (esp_partition_read(p, off, buf, n) != ESP_OK) return false;
|
||||
sha.update(buf, n);
|
||||
off += n;
|
||||
}
|
||||
uint8_t h[32];
|
||||
sha.finalize(h, 32);
|
||||
st.slot_ok = (memcmp(h, st.image_hash, 32) == 0);
|
||||
return st.slot_ok;
|
||||
}
|
||||
|
||||
bool ota_apply_commit() {
|
||||
const esp_partition_t* p = esp_ota_get_next_update_partition(nullptr);
|
||||
if (!p) return false;
|
||||
if (esp_ota_set_boot_partition(p) != ESP_OK) return false;
|
||||
esp_restart(); // does not return
|
||||
return true;
|
||||
}
|
||||
|
||||
// --- detools callback context -----------------------------------------------------------------
|
||||
// The delta base is the running OTA slot; the reconstructed image is streamed into the inactive slot
|
||||
// via esp_ota_write (sequential, append-only -- matches detools' sequential output ordering) and
|
||||
// hashed on the fly so we can check it against the signed manifest image_hash before arming.
|
||||
struct DetoolsCtx {
|
||||
const esp_partition_t* base; // delta base (running image), read at absolute `from_pos`
|
||||
long from_pos; // absolute byte offset into `base`
|
||||
#if defined(OTA_FLASH_STORE)
|
||||
OtaStoreFlashEsp32* store; // staged container; patch = payload region [patch_base, +patch_len)
|
||||
uint32_t patch_base; // container offset where the payload (patch) begins
|
||||
#else
|
||||
const uint8_t* patch; // .mota payload held wholly in RAM (RAM store; bring-up/host)
|
||||
#endif
|
||||
uint32_t patch_len;
|
||||
uint32_t patch_pos;
|
||||
esp_ota_handle_t out; // inactive slot write handle
|
||||
SHA256* sha; // running hash of the reconstructed output
|
||||
uint32_t out_pos; // #bytes written to the output slot
|
||||
bool io_ok;
|
||||
};
|
||||
|
||||
static int dt_from_read(void* arg, uint8_t* buf, size_t size) {
|
||||
DetoolsCtx* c = (DetoolsCtx*)arg;
|
||||
if (c->from_pos < 0 || (uint32_t)(c->from_pos) + size > c->base->size) return -DETOOLS_IO_FAILED;
|
||||
if (esp_partition_read(c->base, (size_t)c->from_pos, buf, size) != ESP_OK) { c->io_ok = false; return -DETOOLS_IO_FAILED; }
|
||||
c->from_pos += (long)size;
|
||||
return DETOOLS_OK;
|
||||
}
|
||||
static int dt_from_seek(void* arg, int offset) { // detools uses relative seeks
|
||||
DetoolsCtx* c = (DetoolsCtx*)arg;
|
||||
c->from_pos += offset;
|
||||
if (c->from_pos < 0 || (uint32_t)c->from_pos > c->base->size) return -DETOOLS_IO_FAILED;
|
||||
return DETOOLS_OK;
|
||||
}
|
||||
static int dt_patch_read(void* arg, uint8_t* buf, size_t size) {
|
||||
DetoolsCtx* c = (DetoolsCtx*)arg;
|
||||
if (c->patch_pos + size > c->patch_len) return -DETOOLS_IO_FAILED;
|
||||
#if defined(OTA_FLASH_STORE)
|
||||
if (!c->store->read(c->patch_base + c->patch_pos, buf, size)) { c->io_ok = false; return -DETOOLS_IO_FAILED; }
|
||||
#else
|
||||
memcpy(buf, c->patch + c->patch_pos, size);
|
||||
#endif
|
||||
c->patch_pos += (uint32_t)size;
|
||||
return DETOOLS_OK;
|
||||
}
|
||||
static int dt_to_write(void* arg, const uint8_t* buf, size_t size) {
|
||||
DetoolsCtx* c = (DetoolsCtx*)arg;
|
||||
if (esp_ota_write(c->out, buf, size) != ESP_OK) { c->io_ok = false; return -DETOOLS_IO_FAILED; }
|
||||
c->sha->update(buf, size);
|
||||
c->out_pos += (uint32_t)size;
|
||||
return DETOOLS_OK;
|
||||
}
|
||||
|
||||
#if defined(OTA_FLASH_STORE)
|
||||
// --- in-place delta on ESP32 (codec 2) ----------------------------------------------------------
|
||||
// A single in-place `.mota` can target BOTH nRF52 (bootloader applies it) and ESP32. On ESP32 the
|
||||
// inactive slot is used as the in-place working memory: we copy the running image (the base) into the
|
||||
// slot's bottom-staged-container-FREE region [0, write_start), then run detools' in-place decoder over
|
||||
// that region (it reads the base, erases segments, writes the target back), reading the patch from the
|
||||
// staged container's payload (which lives at/below write_start, disjoint from the working region). The
|
||||
// decoded image is hashed against the signed image_hash BEFORE arming, so a bad decode never boots; the
|
||||
// callbacks are bounded to [0, write_start) so they fail gracefully instead of touching the patch.
|
||||
// (Sequential is still preferred on ESP32 — it streams straight to the slot with no base-copy; in-place
|
||||
// exists only for single-artifact distribution. Requires the patch built with --inplace-segment 4096.)
|
||||
struct InPlaceCtx {
|
||||
const esp_partition_t* slot; // in-place working memory = slot[0, mem_max)
|
||||
uint32_t mem_max; // = container write_start; accesses beyond this are refused
|
||||
OtaStoreFlashEsp32* store; // staged container; patch = payload region [patch_base, +patch_len)
|
||||
uint32_t patch_base, patch_len, patch_pos;
|
||||
int step; // detools resume cursor (RAM; no cross-reboot resume of the apply)
|
||||
bool io_ok;
|
||||
const char* fail; // first failure point (diagnostic), nullptr until set
|
||||
uint32_t fa, fn; int frc; // failing addr / len / esp_err
|
||||
};
|
||||
static inline int ip_fail(InPlaceCtx* c, const char* w, uint32_t a, size_t n, int rc) {
|
||||
if (!c->fail) { c->fail = w; c->fa = a; c->fn = (uint32_t)n; c->frc = rc; }
|
||||
c->io_ok = false; return -DETOOLS_IO_FAILED;
|
||||
}
|
||||
static int ip_mem_read(void* a, void* dst, uintptr_t src, size_t n) {
|
||||
InPlaceCtx* c = (InPlaceCtx*)a;
|
||||
if ((uint32_t)src + n > c->mem_max) return ip_fail(c, "rd>max", (uint32_t)src, n, 0);
|
||||
int rc = esp_partition_read(c->slot, (size_t)src, dst, n);
|
||||
if (rc != ESP_OK) return ip_fail(c, "rd", (uint32_t)src, n, rc);
|
||||
return DETOOLS_OK;
|
||||
}
|
||||
static int ip_mem_write(void* a, uintptr_t dst, void* src, size_t n) {
|
||||
InPlaceCtx* c = (InPlaceCtx*)a;
|
||||
if ((uint32_t)dst + n > c->mem_max) return ip_fail(c, "wr>max", (uint32_t)dst, n, 0);
|
||||
int rc = esp_partition_write(c->slot, (size_t)dst, src, n);
|
||||
if (rc != ESP_OK) return ip_fail(c, "wr", (uint32_t)dst, n, rc);
|
||||
return DETOOLS_OK;
|
||||
}
|
||||
static int ip_mem_erase(void* a, uintptr_t addr, size_t n) {
|
||||
InPlaceCtx* c = (InPlaceCtx*)a;
|
||||
// esp_partition_erase_range requires a SECTOR-aligned size; detools' final in-place segment is partial
|
||||
// (the image tail past the last full sector). addr is sector-aligned (== --inplace-segment), so round
|
||||
// the length UP to a full sector. The over-erased bytes are scratch beyond image_size (never hashed),
|
||||
// and — since detools processes high→low and erases-before-writing — they are never live patch data.
|
||||
const uint32_t SEC = 4096;
|
||||
if ((uint32_t)addr % SEC != 0) return ip_fail(c, "er!align", (uint32_t)addr, n, 0);
|
||||
uint32_t len = ((uint32_t)n + SEC - 1) & ~(SEC - 1);
|
||||
if ((uint32_t)addr + len > c->mem_max) return ip_fail(c, "er>max", (uint32_t)addr, len, 0);
|
||||
int rc = esp_partition_erase_range(c->slot, (size_t)addr, len);
|
||||
if (rc != ESP_OK) return ip_fail(c, "er", (uint32_t)addr, len, rc);
|
||||
return DETOOLS_OK;
|
||||
}
|
||||
static int ip_step_set(void* a, int s) { ((InPlaceCtx*)a)->step = s; return DETOOLS_OK; }
|
||||
static int ip_step_get(void* a, int* s) { *s = ((InPlaceCtx*)a)->step; return DETOOLS_OK; }
|
||||
static int ip_patch_read(void* a, uint8_t* b, size_t n) {
|
||||
InPlaceCtx* c = (InPlaceCtx*)a;
|
||||
if (c->patch_pos + n > c->patch_len) return ip_fail(c, "patch>len", c->patch_pos, n, 0);
|
||||
if (!c->store->read(c->patch_base + c->patch_pos, b, n)) return ip_fail(c, "patch_rd", c->patch_pos, n, 0);
|
||||
c->patch_pos += (uint32_t)n;
|
||||
return DETOOLS_OK;
|
||||
}
|
||||
|
||||
static bool esp32_inplace_apply(OtaStoreFlashEsp32& store, const MotaManifest& m, ApplyState& st, char* msg) {
|
||||
const esp_partition_t* slot = store.partition();
|
||||
const esp_partition_t* base = esp_ota_get_running_partition();
|
||||
if (!slot || !base) { strcpy(msg, "no slot/base partition"); return false; }
|
||||
SelfFwInfo fi;
|
||||
if (!ota_self_firmware(fi) || !fi.valid) { strcpy(msg, "cannot read running firmware (no EndF)"); return false; }
|
||||
if (!m.base_hash || memcmp(m.base_hash, fi.body_hash, 8) != 0) { strcpy(msg, "not built for the running firmware (base mismatch)"); return false; }
|
||||
uint32_t mem_max = store.write_start(); // working region [0, mem_max); the patch sits at/above it
|
||||
if (mem_max == 0) { strcpy(msg, "in-place needs a bottom-staged container"); return false; }
|
||||
if (fi.image_len > mem_max || m.image_size > mem_max) { strcpy(msg, "in-place region too small for base/image"); return false; }
|
||||
|
||||
// load the base (running image) into the working region [0, base_len), sector by sector (erase + copy)
|
||||
uint8_t buf[512];
|
||||
for (uint32_t off = 0; off < fi.image_len; ) {
|
||||
uint32_t sec = off & ~(4096u - 1);
|
||||
if (esp_partition_erase_range(slot, sec, 4096) != ESP_OK) { strcpy(msg, "base erase failed"); return false; }
|
||||
uint32_t secend = sec + 4096; if (secend > fi.image_len) secend = fi.image_len;
|
||||
for (uint32_t p = (off > sec ? off : sec); p < secend; ) {
|
||||
uint32_t n = secend - p; if (n > sizeof(buf)) n = sizeof(buf);
|
||||
if (esp_partition_read(base, p, buf, n) != ESP_OK || esp_partition_write(slot, p, buf, n) != ESP_OK) {
|
||||
strcpy(msg, "base copy failed"); return false; }
|
||||
p += n;
|
||||
}
|
||||
off = secend;
|
||||
}
|
||||
|
||||
// patch in place over the working region; patch streamed from the staged container payload
|
||||
InPlaceCtx c;
|
||||
c.slot = slot; c.mem_max = mem_max; c.store = &store;
|
||||
c.patch_base = store.meta_bytes(); c.patch_len = m.payload_size; c.patch_pos = 0; c.step = 0; c.io_ok = true;
|
||||
c.fail = nullptr; c.fa = c.fn = 0; c.frc = 0;
|
||||
int r = detools_apply_patch_in_place_callbacks(ip_mem_read, ip_mem_write, ip_mem_erase,
|
||||
ip_step_set, ip_step_get, ip_patch_read,
|
||||
(size_t)m.payload_size, &c);
|
||||
if (r < 0 || !c.io_ok) {
|
||||
if (c.fail) sprintf(msg, "in-place decode err %d @%s a=%u n=%u rc=%d max=%u", r, c.fail,
|
||||
(unsigned)c.fa, (unsigned)c.fn, c.frc, (unsigned)mem_max);
|
||||
else sprintf(msg, "in-place decode err %d", r);
|
||||
return false;
|
||||
}
|
||||
if ((uint32_t)r != m.image_size) { sprintf(msg, "in-place size %u!=%u", (unsigned)r, (unsigned)m.image_size); return false; }
|
||||
|
||||
// verify the decoded slot image against the signed image_hash BEFORE arming (mismatch -> never boots)
|
||||
SHA256 sha;
|
||||
for (uint32_t off = 0; off < m.image_size; ) {
|
||||
uint32_t n = m.image_size - off; if (n > sizeof(buf)) n = sizeof(buf);
|
||||
if (esp_partition_read(slot, off, buf, n) != ESP_OK) { strcpy(msg, "slot read failed"); return false; }
|
||||
sha.update(buf, n); off += n;
|
||||
}
|
||||
uint8_t hh[32]; sha.finalize(hh, 32);
|
||||
st.slot_ok = (memcmp(hh, m.image_hash, 32) == 0);
|
||||
if (!st.slot_ok) { strcpy(msg, "image_hash MISMATCH after in-place decode"); return false; }
|
||||
if (esp_ota_set_boot_partition(slot) != ESP_OK) { strcpy(msg, "set_boot failed"); return false; }
|
||||
sprintf(msg, "verified%s; in-place decoded %u B, image hash OK — armed, rebooting to apply",
|
||||
m.is_signed() ? " (signer trusted)" : " (unsigned)", (unsigned)m.image_size);
|
||||
return true;
|
||||
}
|
||||
|
||||
// Apply the `.mota` staged in the inactive slot by OtaStoreFlashEsp32 (no contiguous RAM copy).
|
||||
// FULL: the payload was streamed straight to slot offset 0 during the fetch -> hash the slot image
|
||||
// and compare to the signed image_hash, then arm. No decode, no copy.
|
||||
// DELTA (sequential): base = the running slot; the patch is read from the staged payload region (the
|
||||
// slot's bottom); the reconstructed image is written to the inactive slot via esp_ota_write and
|
||||
// hashed vs image_hash. esp_ota_begin only erases [0, image_size], which the fetch-time fit
|
||||
// check kept below the bottom-staged container, so the patch survives while we decode over it.
|
||||
// DELTA (in-place): copy the running image into the slot's working region then patch in place
|
||||
// (esp32_inplace_apply); image_hash-gated before arming. Lets one in-place .mota target both
|
||||
// ESP32 and nRF52. Sequential is still preferred on ESP32 (no base-copy).
|
||||
// The result is verified (signature/trust up front, image_hash after) and the slot armed; the caller
|
||||
// reboots once the confirmation reply has gone out.
|
||||
bool ota_apply_detools_mota(OtaStoreFlashEsp32& store, const SignerAllowlist& allow, ApplyState& st, char* msg) {
|
||||
st = ApplyState();
|
||||
const esp_partition_t* slot = store.partition();
|
||||
if (!slot || store.staged_size() < 16) { strcpy(msg, "no staged update"); return false; }
|
||||
st.slot_addr = slot->address; st.slot_size = slot->size;
|
||||
|
||||
// read + parse the manifest out of the staged container (header = MAGIC(4) + total(4))
|
||||
uint8_t hdr[8];
|
||||
if (!store.read(0, hdr, 8) || memcmp(hdr, MOTA_MAGIC, 4) != 0) { strcpy(msg, "bad container"); return false; }
|
||||
uint8_t mfbuf[256];
|
||||
uint32_t mflen = store.meta_bytes() > 8 ? store.meta_bytes() - 8 : 0; // manifest+leaves; cap to mfbuf
|
||||
if (mflen > sizeof(mfbuf)) mflen = sizeof(mfbuf);
|
||||
MotaManifest m;
|
||||
if (mflen < 57 || !store.read(8, mfbuf, mflen) || !mota_parse_manifest(mfbuf, mflen, m)) {
|
||||
strcpy(msg, "manifest parse failed"); return false; }
|
||||
st.image_size = m.image_size; memcpy(st.image_hash, m.image_hash, 32); st.manifest_ok = true;
|
||||
if (m.image_size == 0 || m.image_size > slot->size) { strcpy(msg, "image > slot"); return false; }
|
||||
|
||||
// signature / trust BEFORE arming an untrusted image (image_hash below is the target-firmware gate)
|
||||
if (m.is_signed()) {
|
||||
mesh::Identity signer(m.signer_pubkey);
|
||||
st.sig_ok = signer.verify(m.signature, m.manifest_start, (int)m.signed_len);
|
||||
st.trusted = st.sig_ok && allow.contains(m.signer_pubkey);
|
||||
if (!st.sig_ok) { strcpy(msg, "bad signature"); return false; }
|
||||
if (!st.trusted) { strcpy(msg, "untrusted signer (pubkey not in allowlist)"); return false; }
|
||||
}
|
||||
|
||||
// ---- FULL: payload already in slot[0]; verify hash + arm ----
|
||||
if (m.is_full()) {
|
||||
SHA256 sha; uint8_t buf[512];
|
||||
for (uint32_t off = 0; off < m.image_size; ) {
|
||||
uint32_t n = m.image_size - off; if (n > sizeof(buf)) n = sizeof(buf);
|
||||
if (esp_partition_read(slot, off, buf, n) != ESP_OK) { strcpy(msg, "slot read failed"); return false; }
|
||||
sha.update(buf, n); off += n;
|
||||
}
|
||||
uint8_t hh[32]; sha.finalize(hh, 32);
|
||||
st.slot_ok = (memcmp(hh, m.image_hash, 32) == 0);
|
||||
if (!st.slot_ok) { strcpy(msg, "image_hash MISMATCH (slot)"); return false; }
|
||||
if (esp_ota_set_boot_partition(slot) != ESP_OK) { strcpy(msg, "set_boot failed"); return false; }
|
||||
sprintf(msg, "verified%s full image %u B in slot — armed, rebooting to apply",
|
||||
m.is_signed() ? " (trusted)" : " (unsigned)", (unsigned)m.image_size);
|
||||
return true;
|
||||
}
|
||||
|
||||
// ---- DELTA ----
|
||||
if (m.codec_id == CODEC_DETOOLS_INPLACE) return esp32_inplace_apply(store, m, st, msg); // single-artifact codec
|
||||
if (m.codec_id != CODEC_DETOOLS_SEQUENTIAL) { strcpy(msg, "unknown delta codec"); return false; }
|
||||
|
||||
// delta must be built for the running firmware (cheap early gate; image_hash is the definitive check)
|
||||
if (m.base_hash) {
|
||||
SelfFwInfo fi;
|
||||
if (!ota_self_firmware(fi) || !fi.valid) { strcpy(msg, "cannot read running firmware (no EndF)"); return false; }
|
||||
if (memcmp(m.base_hash, fi.body_hash, 8) != 0) { strcpy(msg, "delta not built for the running firmware (base mismatch)"); return false; }
|
||||
}
|
||||
|
||||
const esp_partition_t* base = esp_ota_get_running_partition();
|
||||
if (!base) { strcpy(msg, "no running partition"); return false; }
|
||||
esp_ota_handle_t h;
|
||||
if (esp_ota_begin(slot, m.image_size, &h) != ESP_OK) { strcpy(msg, "ota_begin failed"); return false; }
|
||||
|
||||
SHA256 sha;
|
||||
DetoolsCtx ctx;
|
||||
ctx.base = base; ctx.from_pos = 0;
|
||||
ctx.store = &store; ctx.patch_base = store.meta_bytes(); ctx.patch_len = m.payload_size; ctx.patch_pos = 0;
|
||||
ctx.out = h; ctx.sha = &sha; ctx.out_pos = 0; ctx.io_ok = true;
|
||||
|
||||
int r = detools_apply_patch_callbacks(dt_from_read, dt_from_seek, dt_patch_read,
|
||||
(size_t)m.payload_size, dt_to_write, &ctx);
|
||||
if (r < 0 || !ctx.io_ok) { esp_ota_abort(h); sprintf(msg, "detools err %d @%u/%u",
|
||||
ctx.io_ok ? r : -DETOOLS_IO_FAILED, (unsigned)ctx.out_pos, (unsigned)m.image_size); return false; }
|
||||
if ((uint32_t)r != m.image_size || ctx.out_pos != m.image_size) {
|
||||
esp_ota_abort(h); sprintf(msg, "size mismatch %u!=%u", (unsigned)ctx.out_pos, (unsigned)m.image_size); return false; }
|
||||
uint8_t hh[32]; sha.finalize(hh, 32);
|
||||
st.slot_ok = (memcmp(hh, m.image_hash, 32) == 0);
|
||||
if (!st.slot_ok) { esp_ota_abort(h); strcpy(msg, "image_hash MISMATCH after decode"); return false; }
|
||||
if (esp_ota_end(h) != ESP_OK) { strcpy(msg, "ota_end failed"); return false; }
|
||||
if (esp_ota_set_boot_partition(slot) != ESP_OK) { strcpy(msg, "set_boot failed"); return false; }
|
||||
sprintf(msg, "verified%s; decoded %u B, image hash OK — armed, rebooting to apply",
|
||||
m.is_signed() ? " (signer trusted)" : " (unsigned)", (unsigned)m.image_size);
|
||||
return true;
|
||||
}
|
||||
|
||||
#else // !OTA_FLASH_STORE: RAM-staged apply (whole .mota in a contiguous RAM buffer; bring-up/host)
|
||||
|
||||
bool ota_apply_detools_mota(const uint8_t* buf, uint32_t len, const SignerAllowlist& allow,
|
||||
ApplyState& st, char* msg) {
|
||||
st = ApplyState();
|
||||
MotaManifest m;
|
||||
if (!mota_parse(buf, len, m)) { strcpy(msg, "no valid .mota (parse failed)"); return false; }
|
||||
if (m.is_full() || m.codec_id != CODEC_DETOOLS_SEQUENTIAL) { strcpy(msg, "not a detools-sequential delta"); return false; }
|
||||
st.image_size = m.image_size;
|
||||
memcpy(st.image_hash, m.image_hash, 32);
|
||||
st.manifest_ok = true;
|
||||
if (m.is_signed()) {
|
||||
mesh::Identity signer(m.signer_pubkey);
|
||||
st.sig_ok = signer.verify(m.signature, m.manifest_start, (int)m.signed_len);
|
||||
st.trusted = st.sig_ok && allow.contains(m.signer_pubkey);
|
||||
if (!st.sig_ok) { strcpy(msg, "bad signature"); return false; }
|
||||
if (!st.trusted) { strcpy(msg, "untrusted signer (pubkey not in allowlist)"); return false; }
|
||||
}
|
||||
const esp_partition_t* base = esp_ota_get_running_partition();
|
||||
const esp_partition_t* out = esp_ota_get_next_update_partition(nullptr);
|
||||
if (!base || !out) { strcpy(msg, "no A/B slot"); return false; }
|
||||
st.slot_addr = out->address; st.slot_size = out->size;
|
||||
if (m.image_size > out->size) { strcpy(msg, "image > slot"); return false; }
|
||||
esp_ota_handle_t h;
|
||||
if (esp_ota_begin(out, m.image_size, &h) != ESP_OK) { strcpy(msg, "ota_begin failed"); return false; }
|
||||
SHA256 sha;
|
||||
DetoolsCtx ctx;
|
||||
ctx.base = base; ctx.from_pos = 0;
|
||||
ctx.patch = m.payload; ctx.patch_len = m.payload_size; ctx.patch_pos = 0;
|
||||
ctx.out = h; ctx.sha = &sha; ctx.out_pos = 0; ctx.io_ok = true;
|
||||
int r = detools_apply_patch_callbacks(dt_from_read, dt_from_seek, dt_patch_read,
|
||||
(size_t)m.payload_size, dt_to_write, &ctx);
|
||||
if (r < 0 || !ctx.io_ok) { esp_ota_abort(h); sprintf(msg, "detools err %d @%u/%u",
|
||||
ctx.io_ok ? r : -DETOOLS_IO_FAILED, (unsigned)ctx.out_pos, (unsigned)m.image_size); return false; }
|
||||
if ((uint32_t)r != m.image_size || ctx.out_pos != m.image_size) {
|
||||
esp_ota_abort(h); sprintf(msg, "size mismatch %u!=%u", (unsigned)ctx.out_pos, (unsigned)m.image_size); return false; }
|
||||
uint8_t hh[32]; sha.finalize(hh, 32);
|
||||
st.slot_ok = (memcmp(hh, m.image_hash, 32) == 0);
|
||||
if (!st.slot_ok) { esp_ota_abort(h); strcpy(msg, "image_hash MISMATCH after decode"); return false; }
|
||||
if (esp_ota_end(h) != ESP_OK) { strcpy(msg, "ota_end failed"); return false; }
|
||||
if (esp_ota_set_boot_partition(out) != ESP_OK) { strcpy(msg, "set_boot failed"); return false; }
|
||||
sprintf(msg, "verified%s; decoded %u B, image hash OK — armed, rebooting to apply",
|
||||
m.is_signed() ? " (signer trusted)" : " (unsigned)", (unsigned)m.image_size);
|
||||
return true;
|
||||
}
|
||||
#endif // OTA_FLASH_STORE
|
||||
|
||||
bool ota_apply_mota_nrf52(const uint8_t*, uint32_t, const SignerAllowlist&, ApplyState& st, char* msg) {
|
||||
st = ApplyState(); strcpy(msg, "nRF52-only (ESP32 uses ota_apply_detools_mota)"); return false;
|
||||
}
|
||||
|
||||
void ota_reboot_to_apply() { esp_restart(); } // boots the slot armed by ota_apply_detools_mota; no return
|
||||
|
||||
#elif defined(NRF52_PLATFORM) // single-slot: verify + mark APPROVED + hand off to the bootloader
|
||||
|
||||
// ESP32 A/B-only entry points are unsupported on nRF52.
|
||||
bool ota_apply_slot_info(uint32_t*, uint32_t*) { return false; }
|
||||
bool ota_apply_set_manifest(const uint8_t*, uint32_t, const SignerAllowlist&, ApplyState& st) { st = ApplyState(); return false; }
|
||||
bool ota_apply_verify_slot(ApplyState&) { return false; }
|
||||
bool ota_apply_commit() { return false; }
|
||||
bool ota_apply_detools_mota(const uint8_t*, uint32_t, const SignerAllowlist&, ApplyState& st, char* msg) { st = ApplyState(); strcpy(msg, "use ota_apply_mota_nrf52"); return false; }
|
||||
|
||||
void ota_reboot_to_apply() { // public: set the apply magic + reset (does not return)
|
||||
uint8_t sd_en = 0;
|
||||
sd_softdevice_is_enabled(&sd_en);
|
||||
if (sd_en) { // POWER is SD-restricted while the SoftDevice runs
|
||||
sd_power_gpregret_clr(0, 0xFFFFFFFF);
|
||||
sd_power_gpregret_set(0, GPREGRET_OTA_APPLY);
|
||||
} else {
|
||||
NRF_POWER->GPREGRET = GPREGRET_OTA_APPLY;
|
||||
}
|
||||
NVIC_SystemReset(); // does not return
|
||||
}
|
||||
|
||||
uint8_t ota_bootloader_last_rc() { // bootloader's last in-place-apply code, stashed in GPREGRET2
|
||||
uint32_t v = 0;
|
||||
uint8_t en = 0; sd_softdevice_is_enabled(&en);
|
||||
if (en) sd_power_gpregret_get(1, &v); else v = NRF_POWER->GPREGRET2;
|
||||
return (uint8_t)v;
|
||||
}
|
||||
|
||||
bool ota_apply_mota_nrf52(const uint8_t* buf, uint32_t len, const SignerAllowlist& allow,
|
||||
ApplyState& st, char* msg) {
|
||||
st = ApplyState();
|
||||
MotaManifest m;
|
||||
if (!mota_parse(buf, len, m)) { strcpy(msg, "parse failed"); return false; }
|
||||
if (m.is_full() || m.codec_id != CODEC_DETOOLS_INPLACE) { strcpy(msg, "not an in-place delta"); return false; }
|
||||
st.image_size = m.image_size;
|
||||
memcpy(st.image_hash, m.image_hash, 32);
|
||||
st.manifest_ok = true;
|
||||
|
||||
// 0) THIS device's bootloader must be able to apply this .mota — otherwise staging + approving + rebooting
|
||||
// just bounces back unchanged (a legacy/stock/older-OTAFIX bootloader). Refuse here, before any reboot.
|
||||
{
|
||||
OtaBlCaps bl = ota_bootloader_caps();
|
||||
if (!bl.present) { strcpy(msg, "this bootloader has no OTA-apply support — update the bootloader first"); return false; }
|
||||
if (bl.apply_abi < m.format_ver || !(bl.codec_mask & (1u << m.codec_id))) {
|
||||
snprintf(msg, 159, "bootloader too old to apply this update (bl abi=%u codecs=0x%x; need fmt>=%u codec=%u) — update the bootloader",
|
||||
bl.apply_abi, bl.codec_mask, m.format_ver, m.codec_id);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// Gated verification, in order, returning the FIRST failing reason (the bootloader re-checks integrity
|
||||
// again before booting, so authenticity is gated here and re-validated there):
|
||||
VerifyResult vr = ota_verify(buf, len, allow);
|
||||
st.sig_ok = vr.sig_ok; st.trusted = vr.trusted;
|
||||
|
||||
// 1) downloaded payload: the fetched blocks must match the manifest's merkle root (intact + complete)
|
||||
if (!vr.root_ok || !vr.image_ok) { strcpy(msg, "payload hash mismatch (incomplete or corrupt .mota)"); return false; }
|
||||
|
||||
// 2) target firmware: the delta must be built against THIS running image (base_hash == our EndF body
|
||||
// hash). The resulting image_hash is re-checked by the bootloader after the in-place decode -- a
|
||||
// single-slot device cannot produce the target image to hash it before applying.
|
||||
SelfFwInfo fi;
|
||||
if (!ota_self_firmware(fi) || !fi.valid) { strcpy(msg, "cannot read running firmware (no EndF)"); return false; }
|
||||
if (!m.base_hash || memcmp(m.base_hash, fi.body_hash, 8) != 0) { strcpy(msg, "not built for the running firmware (base mismatch)"); return false; }
|
||||
st.slot_ok = true;
|
||||
|
||||
// 3) signature (only if the .mota is signed): valid Ed25519 AND signer in this device's allowlist
|
||||
if (vr.is_signed) {
|
||||
if (!vr.sig_ok) { strcpy(msg, "bad signature"); return false; }
|
||||
if (!vr.trusted) { strcpy(msg, "untrusted signer (pubkey not in allowlist)"); return false; }
|
||||
}
|
||||
|
||||
// mark the staged manifest APPROVED in flash (buf is the memory-mapped staging region, so
|
||||
// m.approval is a real flash address). NOR-clear over the erased 0xFFFFFFFF -> "APRV".
|
||||
uint32_t approval_addr = (uint32_t)(uintptr_t)m.approval;
|
||||
if (flash_nrf5x_write(approval_addr, APPROVAL_YES, 4) < 0) { strcpy(msg, "approval write failed"); return false; }
|
||||
flash_nrf5x_flush();
|
||||
if (memcmp((const void*)(uintptr_t)approval_addr, APPROVAL_YES, 4) != 0) { strcpy(msg, "approval not set"); return false; }
|
||||
|
||||
// Approved. Do NOT reset here — return so the caller can deliver `msg` to the operator first; the
|
||||
// deferred ota_reboot_to_apply() (after the reply is sent) does the actual handoff to the bootloader.
|
||||
sprintf(msg, "verified%s; applying — rebooting into bootloader once this reply is sent",
|
||||
vr.is_signed ? " (signer trusted)" : " (unsigned)");
|
||||
return true;
|
||||
}
|
||||
|
||||
#else // native / other platforms
|
||||
|
||||
bool ota_apply_slot_info(uint32_t*, uint32_t*) { return false; }
|
||||
bool ota_apply_set_manifest(const uint8_t*, uint32_t, const SignerAllowlist&, ApplyState& st) { st = ApplyState(); return false; }
|
||||
bool ota_apply_verify_slot(ApplyState&) { return false; }
|
||||
bool ota_apply_commit() { return false; }
|
||||
bool ota_apply_detools_mota(const uint8_t*, uint32_t, const SignerAllowlist&, ApplyState& st, char* msg) { st = ApplyState(); strcpy(msg, "unsupported"); return false; }
|
||||
bool ota_apply_mota_nrf52(const uint8_t*, uint32_t, const SignerAllowlist&, ApplyState& st, char* msg) { st = ApplyState(); strcpy(msg, "unsupported"); return false; }
|
||||
void ota_reboot_to_apply() {}
|
||||
uint8_t ota_bootloader_last_rc() { return 0; }
|
||||
|
||||
#endif
|
||||
|
||||
} // namespace ota
|
||||
} // namespace mesh
|
||||
@@ -0,0 +1,66 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
#include "SignerAllowlist.h"
|
||||
|
||||
// P6 apply (full-image, ESP32 A/B). The new image is delivered into the inactive OTA slot; the device
|
||||
// then verifies that slot against the signed manifest's image_hash (+ Ed25519/allowlist), and commits
|
||||
// by setting it as the boot partition and rebooting. Safe + rollback-capable (the bootloader validates
|
||||
// the image; a bad image rolls back). nRF52 apply is the bootloader-handoff path (separate). Functions
|
||||
// return false on platforms without an A/B OTA layout.
|
||||
|
||||
namespace mesh {
|
||||
namespace ota {
|
||||
|
||||
struct ApplyState {
|
||||
bool manifest_ok = false;
|
||||
bool sig_ok = false;
|
||||
bool trusted = false; // signer in allowlist
|
||||
bool slot_ok = false; // inactive slot image hashes to manifest.image_hash
|
||||
uint32_t slot_addr = 0, slot_size = 0;
|
||||
uint32_t image_size = 0;
|
||||
uint8_t image_hash[32] = {0};
|
||||
};
|
||||
|
||||
bool ota_apply_slot_info(uint32_t* addr, uint32_t* size); // the inactive A/B slot
|
||||
bool ota_apply_set_manifest(const uint8_t* mf, uint32_t len,
|
||||
const SignerAllowlist& allow, ApplyState& st); // parse + verify signature
|
||||
bool ota_apply_verify_slot(ApplyState& st); // hash the slot vs image_hash
|
||||
bool ota_apply_commit(); // set-boot + reboot (no return)
|
||||
|
||||
// Apply an ESP32 A/B `.mota` (CODEC_DETOOLS_SEQUENTIAL delta or CODEC_FULL image) and arm the inactive
|
||||
// slot; the caller reboots after the confirmation reply. `msg` (>=80 bytes) receives a human-readable
|
||||
// result. With OTA_FLASH_STORE the container is staged in the inactive slot (no contiguous RAM copy): a
|
||||
// full payload is already in the slot (just verified), a sequential delta is decoded from the running
|
||||
// slot over the staged patch. Without OTA_FLASH_STORE (bring-up) the whole container is a RAM buffer.
|
||||
#if defined(ESP32_PLATFORM) && defined(OTA_FLASH_STORE)
|
||||
class OtaStoreFlashEsp32;
|
||||
bool ota_apply_detools_mota(OtaStoreFlashEsp32& store,
|
||||
const SignerAllowlist& allow, ApplyState& st, char* msg);
|
||||
#else
|
||||
bool ota_apply_detools_mota(const uint8_t* buf, uint32_t len,
|
||||
const SignerAllowlist& allow, ApplyState& st, char* msg);
|
||||
#endif
|
||||
|
||||
// nRF52 (RAK4631) single-slot apply. The running app can't rewrite itself, so it does NOT decode: it
|
||||
// runs the gated verification chain (payload hash -> built-for-this-firmware -> signature/trust) and,
|
||||
// only if all pass, marks the staged manifest APPROVED in flash. It does NOT reboot — so the caller can
|
||||
// first send the result back to the operator — the actual handoff is ota_reboot_to_apply() below.
|
||||
// Returns true (msg = "verified...") when approved, false (msg = the first failing gate) otherwise.
|
||||
bool ota_apply_mota_nrf52(const uint8_t* buf, uint32_t len,
|
||||
const SignerAllowlist& allow, ApplyState& st, char* msg);
|
||||
|
||||
// Commit the (already approved/armed) update and reboot into it — does NOT return. Call this only after
|
||||
// a successful ota_apply_* AND after the confirmation reply has been delivered, so the operator knows
|
||||
// the apply started (over LoRa the device then goes silent while the bootloader applies). nRF52: set
|
||||
// the GPREGRET apply magic + reset (the bootloader does the in-place decode + verify). ESP32: reboot
|
||||
// into the slot already armed by ota_apply_detools_mota.
|
||||
void ota_reboot_to_apply();
|
||||
|
||||
// DIAGNOSTIC (nRF52): the bootloader stashes its last in-place-apply bail/progress code in GPREGRET2 (see
|
||||
// ota_delta.c). Read it back so `ota status` can show why an apply didn't take. 0 / other platforms = n/a.
|
||||
uint8_t ota_bootloader_last_rc();
|
||||
|
||||
} // namespace ota
|
||||
} // namespace mesh
|
||||
@@ -0,0 +1,54 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
|
||||
// Read the bootloader's OTA capability marker so the app can decide, BEFORE staging+approving+rebooting,
|
||||
// whether THIS device's bootloader can actually apply a `.mota`. Without this the app would reboot into a
|
||||
// bootloader that silently can't apply (stock Adafruit, or an OLDER OTAFIX predating a `.mota` format
|
||||
// change) and the device would just come back up unchanged.
|
||||
//
|
||||
// Mirror of Adafruit_nRF52_Bootloader_OTAFIX/src/ota_bl_info.h — keep byte-identical.
|
||||
// nRF52 only (the bootloader flash is memory-mapped + readable by the app); a no-op elsewhere.
|
||||
|
||||
#if defined(NRF52_PLATFORM)
|
||||
#include "OtaFlashLayout_nrf52.h"
|
||||
#endif
|
||||
|
||||
namespace mesh {
|
||||
namespace ota {
|
||||
|
||||
// 16-byte marker: magic[8] "MOTABLDR" + apply_abi(2) + codec_mask(2) + reserved(4).
|
||||
static const uint8_t OTA_BL_MAGIC[8] = { 'M','O','T','A','B','L','D','R' };
|
||||
|
||||
struct OtaBlCaps {
|
||||
bool present = false;
|
||||
uint16_t apply_abi = 0; // max .mota format_ver the bootloader can apply
|
||||
uint16_t codec_mask = 0; // bit i set => can apply codec_id i (in-place delta = bit 2)
|
||||
};
|
||||
|
||||
// Scan the bootloader flash region for the marker. Returns {present=false} if not found / non-nRF52.
|
||||
inline OtaBlCaps ota_bootloader_caps() {
|
||||
OtaBlCaps c;
|
||||
#if defined(NRF52_PLATFORM)
|
||||
const uint8_t* lo = (const uint8_t*)(uintptr_t)MOTA_NRF52_BL_START;
|
||||
const uint8_t* hi = (const uint8_t*)(uintptr_t)MOTA_NRF52_BL_END;
|
||||
for (const uint8_t* p = lo; p + 16 <= hi; p++) {
|
||||
if (p[0] != OTA_BL_MAGIC[0] || memcmp(p, OTA_BL_MAGIC, 8) != 0) continue;
|
||||
c.present = true;
|
||||
c.apply_abi = (uint16_t)(p[8] | ((uint16_t)p[9] << 8));
|
||||
c.codec_mask = (uint16_t)(p[10] | ((uint16_t)p[11] << 8));
|
||||
break;
|
||||
}
|
||||
#endif
|
||||
return c;
|
||||
}
|
||||
|
||||
// True if this device's bootloader can apply a .mota of the given format_ver + codec_id.
|
||||
inline bool ota_bootloader_can_apply(uint8_t format_ver, uint8_t codec_id) {
|
||||
OtaBlCaps c = ota_bootloader_caps();
|
||||
return c.present && c.apply_abi >= format_ver && (c.codec_mask & (1u << codec_id)) != 0;
|
||||
}
|
||||
|
||||
} // namespace ota
|
||||
} // namespace mesh
|
||||
@@ -0,0 +1,39 @@
|
||||
#pragma once
|
||||
|
||||
// Shared OTA flash-layout constants for the nRF52840 (RAK4631) single-slot delta-apply path.
|
||||
// SINGLE SOURCE OF TRUTH — keep byte-identical with the bootloader's src/ota_layout.h.
|
||||
//
|
||||
// The running app occupies [APP_BASE, app_end]; the primary LittleFS (InternalFS) starts at FS_START.
|
||||
// MeshCore stages a verified+approved `.mota` in the free flash below FS_START (bottom-aligned), then
|
||||
// sets GPREGRET_OTA_APPLY and resets; the bootloader scans [APP_BASE, FS_START) for it and applies it
|
||||
// in place. These must match the bootloader and the running SoftDevice's app base.
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
namespace mesh {
|
||||
namespace ota {
|
||||
|
||||
static const uint32_t MOTA_NRF52_APP_BASE = 0x00026000u; // S140 end (== CODE_REGION_1_START)
|
||||
// Staging ceiling: the lowest filesystem region above the app. RAK4631 companion builds use the
|
||||
// extrafs ldscript with ExtraFS at 0xD4000..0xED000 (and InternalFS at 0xED000), while the repeater
|
||||
// uses the default ldscript (InternalFS at 0xED000, 0xD4000..0xED000 free). 0xD4000 is the safe
|
||||
// universal ceiling for ALL RAK4631 roles: staging below it never touches ExtraFS or InternalFS, and
|
||||
// the app (~520 KB) sits well below 0xD4000 either way.
|
||||
static const uint32_t MOTA_NRF52_FS_START = 0x000D4000u; // ExtraFS start (universal staging ceiling)
|
||||
static const uint32_t MOTA_NRF52_FLASH_PAGE = 4096u;
|
||||
static const uint8_t GPREGRET_OTA_APPLY = 0x6Au; // distinct from DFU magics 0x57/0x4E/0xA8
|
||||
|
||||
// In-place patches are built with --inplace-memory = this (the apply workspace, from APP_BASE up).
|
||||
// It must hold the new image (~520 KB) yet leave the staged mota room below FS_START: workspace ends
|
||||
// at APP_BASE+this = 0xBE000, leaving 0xBE000..0xD4000 (~88 KB) for the staged delta. The bootloader
|
||||
// also bounds writes to < the (scanned) mota start, so a mis-sized memory still fails safe.
|
||||
static const uint32_t MOTA_NRF52_INPLACE_MEMORY = 0x00098000u; // 608 KB (APP_BASE .. 0xBE000)
|
||||
|
||||
// Bootloader flash region (nRF52840: 39 KB ending just below the CF2/MBR-params pages). The app scans
|
||||
// this for the bootloader capability marker (OtaBlInfo.h) to know whether THIS device's bootloader can
|
||||
// actually apply a .mota before staging+approving+rebooting.
|
||||
static const uint32_t MOTA_NRF52_BL_START = 0x000F4000u;
|
||||
static const uint32_t MOTA_NRF52_BL_END = 0x000FE000u;
|
||||
|
||||
} // namespace ota
|
||||
} // namespace mesh
|
||||
@@ -0,0 +1,243 @@
|
||||
#include "OtaStoreFlashEsp32.h"
|
||||
|
||||
#if defined(ESP32_PLATFORM) && defined(OTA_FLASH_STORE)
|
||||
|
||||
#include "OtaDebug.h"
|
||||
#include "OtaByteIO.h" // align_up / align_down (flash-sector geometry)
|
||||
#include "MotaContainer.h" // mota_parse_manifest (reopen: rebuild geometry from the staged manifest)
|
||||
#include <string.h>
|
||||
#include <stdlib.h> // malloc/free (the meta buffer is sized per fetch)
|
||||
#include "esp_ota_ops.h" // esp_ota_get_next_update_partition (the inactive A/B slot)
|
||||
|
||||
namespace mesh {
|
||||
namespace ota {
|
||||
|
||||
OtaStoreFlashEsp32::~OtaStoreFlashEsp32() { free(_meta); }
|
||||
|
||||
bool OtaStoreFlashEsp32::acquire() {
|
||||
if (!_part) {
|
||||
_part = esp_ota_get_next_update_partition(nullptr);
|
||||
_psize = _part ? _part->size : 0;
|
||||
}
|
||||
return _part != nullptr;
|
||||
}
|
||||
|
||||
// Compute the slot placement from _full/_image_size/_meta_bytes/_pay_size (already set). Returns false if
|
||||
// it won't fit. Shared by plan_layout (fresh fetch) and reopen (resume) so both derive identical geometry.
|
||||
bool OtaStoreFlashEsp32::layout() {
|
||||
uint32_t total = _meta_bytes + _pay_size + 5;
|
||||
if (_full) {
|
||||
// payload streams to slot offset 0 (it IS the image); header+manifest+leaves+trailer persist at the
|
||||
// bottom so the container survives a reboot (resume / re-serve).
|
||||
_meta_span = _meta_bytes; // routing boundary: [0,meta) -> RAM meta buffer
|
||||
_meta_flush = align_up(_meta_bytes + 5, SEC); // meta + 5-byte trailer, whole sectors
|
||||
if (_meta_flush > OTA_ESP32_META_CAP) return false;
|
||||
uint32_t bottom = align_down(_psize - _meta_flush, SEC);
|
||||
_meta_part = bottom;
|
||||
_pay_log0 = _meta_bytes; _pay_part0 = 0;
|
||||
_write_start = 0;
|
||||
if (_image_size > bottom) return false; // image would overrun the bottom meta region
|
||||
} else {
|
||||
// whole container staged bottom-aligned; the decoded image fills the slot from offset 0.
|
||||
_meta_span = align_up(_meta_bytes, SEC); // pin whole sectors covering meta (+ spillover payload)
|
||||
_meta_flush = _meta_span;
|
||||
if (_meta_flush > OTA_ESP32_META_CAP) return false;
|
||||
if (total > _psize) return false;
|
||||
_write_start = align_down(_psize - total, SEC);
|
||||
_meta_part = _write_start;
|
||||
_pay_log0 = _meta_span; _pay_part0 = _write_start + _meta_span;
|
||||
if (_image_size > _write_start) return false; // decoded output would overlap the staged container
|
||||
}
|
||||
_total = total;
|
||||
return true;
|
||||
}
|
||||
|
||||
// Choose placement from the parsed manifest and refuse anything that won't fit, BEFORE any block is
|
||||
// staged. (See the header for the delta-vs-full layout rationale.)
|
||||
bool OtaStoreFlashEsp32::plan_layout(bool is_full, uint32_t image_size, uint32_t payload_off, uint32_t payload_size) {
|
||||
if (!acquire()) return false;
|
||||
_full = is_full; _image_size = image_size; _meta_bytes = payload_off; _pay_size = payload_size;
|
||||
bool ok = layout();
|
||||
OTA_DBG("OTA esp32: plan %s total=%u image=%u meta=%u write_start=%u meta_part=%u ok=%d\n",
|
||||
is_full ? "FULL" : "DELTA", (unsigned)_total, (unsigned)image_size, (unsigned)_meta_bytes,
|
||||
(unsigned)_write_start, (unsigned)_meta_part, (int)ok);
|
||||
return ok;
|
||||
}
|
||||
|
||||
bool OtaStoreFlashEsp32::begin(uint32_t total_size) {
|
||||
if (!_part || _total == 0 || total_size != _total) return false; // plan_layout must have run + agreed
|
||||
free(_meta);
|
||||
_meta = (uint8_t*)malloc(_meta_flush); // header+manifest+leaves(+full trailer)
|
||||
if (!_meta) { _total = 0; return false; }
|
||||
memset(_meta, 0xFF, _meta_flush);
|
||||
memset(_trailer, 0xFF, sizeof(_trailer));
|
||||
_pay_open = false; _pay_sec = 0; _pay_max_sec = 0; _flushed = false;
|
||||
_io_ok = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
void OtaStoreFlashEsp32::clear() {
|
||||
free(_meta); _meta = nullptr;
|
||||
_total = 0; _pay_open = false; _flushed = false; // _part kept (re-acquire is fine)
|
||||
}
|
||||
|
||||
uint8_t* OtaStoreFlashEsp32::meta_slot(uint32_t L) {
|
||||
if (in_trailer(L)) return _full ? (_meta + _meta_bytes + (L - (_total - 5)))
|
||||
: (_trailer + (L - (_total - 5)));
|
||||
if (L < _meta_span) return _meta + L;
|
||||
return nullptr; // payload -> sliding sector / flash
|
||||
}
|
||||
const uint8_t* OtaStoreFlashEsp32::meta_slot_c(uint32_t L) const {
|
||||
if (in_trailer(L)) return _full ? (_meta + _meta_bytes + (L - (_total - 5)))
|
||||
: (_trailer + (L - (_total - 5)));
|
||||
if (L < _meta_span) return _meta + L;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
// Bytes from `pos` that stay in one region, and (for payload) one flash sector.
|
||||
uint32_t OtaStoreFlashEsp32::run(uint32_t pos, uint32_t remain) const {
|
||||
if (pos >= _total - 5) return remain; // trailer (<=5, one buffer)
|
||||
if (pos < _meta_span) { uint32_t c = _meta_span - pos; return remain < c ? remain : c; }
|
||||
uint32_t poff = pay_part(pos);
|
||||
uint32_t to_sec = SEC - (poff % SEC);
|
||||
uint32_t to_end = (_total - 5) - pos; // don't cross into the trailer
|
||||
uint32_t c = remain;
|
||||
if (to_sec < c) c = to_sec;
|
||||
if (to_end < c) c = to_end;
|
||||
return c;
|
||||
}
|
||||
|
||||
void OtaStoreFlashEsp32::flush_sector(uint32_t slot_off, const uint8_t* buf, uint32_t n) {
|
||||
if (!_io_ok) return;
|
||||
if (esp_partition_erase_range(_part, slot_off, n) != ESP_OK) { _io_ok = false; return; }
|
||||
if (esp_partition_write(_part, slot_off, buf, n) != ESP_OK) { _io_ok = false; return; }
|
||||
OTA_DBG("OTA esp32: flushed %u B @ slot+%u\n", (unsigned)n, (unsigned)slot_off);
|
||||
}
|
||||
|
||||
void OtaStoreFlashEsp32::flush_pay() {
|
||||
if (_pay_open) { flush_sector((uint32_t)_pay_sec * SEC, _pay, SEC); _pay_open = false; }
|
||||
}
|
||||
|
||||
void OtaStoreFlashEsp32::open_pay(uint32_t sec) {
|
||||
if (_pay_open) flush_pay();
|
||||
if (sec < _pay_max_sec) {
|
||||
// revisiting an already-flushed sector (out-of-order block) -> read it back so the gaps we don't
|
||||
// touch are preserved (they were programmed as 0xFF or earlier block data); we erase+reprogram on flush.
|
||||
if (esp_partition_read(_part, (size_t)sec * SEC, _pay, SEC) != ESP_OK) _io_ok = false;
|
||||
} else {
|
||||
memset(_pay, 0xFF, SEC); // fresh sector
|
||||
_pay_max_sec = sec;
|
||||
}
|
||||
_pay_sec = sec; _pay_open = true;
|
||||
}
|
||||
|
||||
bool OtaStoreFlashEsp32::write(uint32_t offset, const uint8_t* d, uint32_t len) {
|
||||
if ((uint64_t)offset + len > _total || !_io_ok) return false;
|
||||
for (uint32_t pos = offset, end = offset + len; pos < end; ) {
|
||||
uint32_t n = run(pos, end - pos);
|
||||
if (uint8_t* dst = meta_slot(pos)) { // meta / leaves / trailer -> pinned RAM
|
||||
memcpy(dst, d, n);
|
||||
} else { // payload -> sliding sector
|
||||
uint32_t poff = pay_part(pos);
|
||||
uint32_t sec = poff / SEC;
|
||||
if (!_pay_open || sec != _pay_sec) open_pay(sec);
|
||||
memcpy(_pay + (poff % SEC), d, n);
|
||||
}
|
||||
pos += n; d += n;
|
||||
if (!_io_ok) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool OtaStoreFlashEsp32::read(uint32_t offset, uint8_t* buf, uint32_t len) const {
|
||||
if ((uint64_t)offset + len > _total) return false;
|
||||
for (uint32_t pos = offset, end = offset + len; pos < end; ) {
|
||||
uint32_t n = run(pos, end - pos);
|
||||
if (const uint8_t* src = meta_slot_c(pos)) {
|
||||
memcpy(buf, src, n);
|
||||
} else {
|
||||
uint32_t poff = pay_part(pos);
|
||||
if (_pay_open && poff / SEC == _pay_sec) memcpy(buf, _pay + (poff % SEC), n); // still in RAM
|
||||
else if (esp_partition_read(_part, poff, buf, n) != ESP_OK) return false; // flushed -> flash
|
||||
}
|
||||
pos += n; buf += n;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void OtaStoreFlashEsp32::finalize() {
|
||||
if (_flushed || _total == 0) return;
|
||||
if (!_full) {
|
||||
// delta: drop the 5-byte trailer into the sliding payload sector(s) so it flushes with them (the
|
||||
// trailer sits right after the payload; this also covers the rare case it spills into a fresh sector).
|
||||
uint32_t tpoff = _write_start + (_total - 5);
|
||||
for (uint32_t off = 0; off < 5; ) {
|
||||
uint32_t sec = (tpoff + off) / SEC;
|
||||
if (!_pay_open || sec != _pay_sec) open_pay(sec);
|
||||
uint32_t in = SEC - ((tpoff + off) % SEC); if (in > 5 - off) in = 5 - off;
|
||||
memcpy(_pay + ((tpoff + off) % SEC), _trailer + off, in);
|
||||
off += in;
|
||||
}
|
||||
}
|
||||
flush_pay(); // last payload sector(s) (+ delta trailer)
|
||||
flush_sector(_meta_part, _meta, _meta_flush); // meta (+ trailer for full)
|
||||
_flushed = true;
|
||||
OTA_DBG("OTA esp32: finalize %s io_ok=%d\n", _full ? "FULL" : "DELTA", (int)_io_ok);
|
||||
}
|
||||
|
||||
// Persist mid-transfer progress so a reboot can resume. Flush the open payload sector (KEEP it buffered so
|
||||
// continued in-order writes aren't lost), then the meta region (leaves). Payload-before-leaves keeps it
|
||||
// consistent: every block whose leaf is now in flash also has its payload in flash. Infrequent.
|
||||
void OtaStoreFlashEsp32::checkpoint() {
|
||||
if (_total == 0 || _flushed || !_io_ok) return;
|
||||
if (_pay_open) flush_sector((uint32_t)_pay_sec * SEC, _pay, SEC); // flush but leave _pay/_pay_open intact
|
||||
flush_sector(_meta_part, _meta, _meta_flush); // header + manifest + leaves (+full trailer)
|
||||
}
|
||||
|
||||
// Re-attach to a container already staged in the slot (after a reboot), WITHOUT erasing. The header
|
||||
// (MOTA_MAGIC + total) sits at a sector boundary (meta_part for full, write_start for delta — both
|
||||
// sector-aligned), so scan sector starts from the bottom up. A candidate is accepted only if: magic +
|
||||
// plausible total, the manifest parses, the container geometry is self-consistent with the total, AND the
|
||||
// recomputed placement lands the meta exactly where we found the magic. Any miss -> false (fetch fresh),
|
||||
// so a stray/stale match can only cost a restart, never a corrupt adopt.
|
||||
bool OtaStoreFlashEsp32::reopen() {
|
||||
if (!acquire() || _psize < SEC) return false;
|
||||
uint8_t hb[8];
|
||||
// the meta/container is staged at the BOTTOM of the slot, so scan upward from there; cap the scan (a
|
||||
// miss just means "fetch fresh"). 128 sectors (512 KB) covers any full image's meta + typical deltas.
|
||||
uint32_t scanned = 0;
|
||||
for (uint32_t o = align_down(_psize - SEC, SEC); ; o -= SEC) {
|
||||
if (esp_partition_read(_part, o, hb, 8) == ESP_OK && memcmp(hb, MOTA_MAGIC, 4) == 0) {
|
||||
uint32_t total = rd_u32le(hb + 4);
|
||||
if (total >= 13 && total <= _psize) {
|
||||
uint8_t mbuf[256]; uint32_t mread = total - 8; if (mread > sizeof(mbuf)) mread = sizeof(mbuf);
|
||||
MotaManifest m;
|
||||
if (esp_partition_read(_part, o + 8, mbuf, mread) == ESP_OK && mota_parse_manifest(mbuf, mread, m)) {
|
||||
uint32_t mfl = (uint32_t)(m.approval - m.manifest_start) + 4;
|
||||
uint32_t payload_off = 8 + mfl + m.block_count * 4;
|
||||
if ((uint64_t)payload_off + m.payload_size + 5 == total) {
|
||||
_full = m.is_full(); _image_size = m.image_size; _meta_bytes = payload_off; _pay_size = m.payload_size;
|
||||
if (layout() && _meta_part == o) { // geometry agrees AND magic is where we'd place meta
|
||||
free(_meta); _meta = (uint8_t*)malloc(_meta_flush);
|
||||
if (!_meta) { _total = 0; return false; }
|
||||
if (esp_partition_read(_part, _meta_part, _meta, _meta_flush) != ESP_OK) {
|
||||
free(_meta); _meta = nullptr; _total = 0; return false; }
|
||||
memset(_trailer, 0xFF, sizeof(_trailer)); // delta trailer (re-written at finalize); full reads it from _meta
|
||||
_pay_open = false; _pay_sec = 0; _flushed = false; _io_ok = true;
|
||||
_pay_max_sec = (_pay_part0 + _pay_size + SEC) / SEC; // treat all payload sectors as seen -> RMW preserves committed blocks
|
||||
OTA_DBG("OTA esp32: reopen %s total=%u meta_part=%u\n", _full ? "FULL" : "DELTA", (unsigned)total, (unsigned)o);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (o == 0 || ++scanned >= 128) break;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
} // namespace ota
|
||||
} // namespace mesh
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,116 @@
|
||||
#pragma once
|
||||
|
||||
#if defined(ESP32_PLATFORM) && defined(OTA_FLASH_STORE)
|
||||
|
||||
#include "OtaStore.h"
|
||||
#include "esp_partition.h"
|
||||
|
||||
// Persistent flash-backed OtaStore for ESP32 (A/B). Stages the received `.mota` in the INACTIVE OTA
|
||||
// slot, so a delta/full of any size is bounded to O(one sector) of RAM, never O(mota) -- lifting the old
|
||||
// RAM store's ~16 KB ceiling (a full 1 MB+ image now fetches over the air). Placement is chosen from the
|
||||
// parsed manifest (plan_layout), keyed only on full-vs-delta:
|
||||
//
|
||||
// - DELTA (codec sequential/in-place): the whole container is staged bottom-aligned in the slot. At
|
||||
// apply the decoder reads the patch from it -- sequential: base from the running slot -> output to
|
||||
// the inactive slot from offset 0; in-place: copy running->slot then patch in place. The decoded
|
||||
// image fills the slot from offset 0, so plan_layout refuses unless `image_size + container` fits
|
||||
// (the output must never reach the bottom-staged container; the fit makes them disjoint).
|
||||
//
|
||||
// - FULL (codec full): the payload IS the final image (no decode), so it streams straight to slot
|
||||
// offset 0 while the small header+manifest+merkle-leaves and the trailer persist at the bottom of the
|
||||
// slot (so the whole container survives a reboot -- for fetch-resume and for re-serving to other
|
||||
// nodes). One default for every A/B full payload; plan_layout refuses unless `image_size + meta`
|
||||
// fits, and if that doesn't fit nothing would.
|
||||
//
|
||||
// RX-safety: an ESP32 flash erase+write disables the XIP instruction cache and stalls code running from
|
||||
// flash (the dispatcher loop), exactly like the nRF52 page erase that starved the LoRa RX. So writes are
|
||||
// COALESCED to the 4 KB sector and each sector is programmed once. The meta region (header+manifest+
|
||||
// leaves -- written all transfer long as blocks/leaves arrive, often out of order) is PINNED in RAM and
|
||||
// flushed at finalize() with the radio idle; the bulk payload streams through ONE sliding sector buffer,
|
||||
// flushing the sector it leaves behind (~1 flush per 4 KB, off the per-packet path). OTA is also the
|
||||
// lowest-priority TX, so a brief stall yields to real traffic. A small delta whose container fits the
|
||||
// pinned meta region does ZERO flash I/O until COMPLETE.
|
||||
|
||||
namespace mesh {
|
||||
namespace ota {
|
||||
|
||||
#ifndef OTA_ESP32_META_CAP
|
||||
#define OTA_ESP32_META_CAP 65536 // max heap for header+manifest+merkle leaves (4 B/block) -> ~16k blocks
|
||||
#endif // (a 1.27 MB full image at 128 B LoRa blocks ~= 40 KB of leaves)
|
||||
|
||||
class OtaStoreFlashEsp32 : public OtaStore {
|
||||
static const uint32_t SEC = 4096; // ESP32 NOR flash erase unit
|
||||
|
||||
const esp_partition_t* _part = nullptr; // inactive OTA slot (acquired in plan_layout/begin)
|
||||
uint32_t _psize = 0; // slot size
|
||||
|
||||
// container geometry (from plan_layout / handleManifest)
|
||||
uint32_t _total = 0; // container size (0 = none staged)
|
||||
uint32_t _meta_bytes = 0; // header+manifest+leaves (== payload offset in the container)
|
||||
uint32_t _pay_size = 0; // payload bytes
|
||||
uint32_t _image_size = 0; // reconstructed image (delta) / == _pay_size (full)
|
||||
bool _full = false;
|
||||
|
||||
// logical->partition placement (see header doc). For delta everything is contiguous at _write_start;
|
||||
// for full the payload lands at slot 0 and meta+trailer at the bottom.
|
||||
uint32_t _write_start = 0; // delta: container offset 0 in the slot (sector-aligned)
|
||||
uint32_t _meta_span = 0; // container bytes held in the RAM meta buffer (whole sectors)
|
||||
uint32_t _meta_part = 0; // slot offset the meta buffer flushes to
|
||||
uint32_t _pay_log0 = 0; // first container offset that streams to the payload region
|
||||
uint32_t _pay_part0 = 0; // slot offset of that first payload byte
|
||||
uint32_t _trailer_part = 0; // slot offset of the 5-byte trailer
|
||||
|
||||
// RX-safe staging buffers
|
||||
uint8_t* _meta = nullptr; // heap, sized per fetch: header+manifest+leaves(+full trailer)
|
||||
uint8_t _pay[SEC]; // one sliding payload sector (slot-sector aligned)
|
||||
uint32_t _pay_sec = 0; // slot sector index currently in _pay (0 = none open)
|
||||
uint8_t _trailer[5];
|
||||
uint32_t _meta_flush = 0; // whole-sector byte count to program for the meta buffer
|
||||
uint32_t _pay_max_sec = 0; // highest payload slot-sector opened (out-of-order detection)
|
||||
bool _pay_open = false; // a sliding sector is currently buffered in _pay
|
||||
bool _flushed = false;
|
||||
bool _io_ok = true; // cleared if any flash erase/write/read fails
|
||||
|
||||
bool acquire(); // resolve the inactive slot (idempotent)
|
||||
bool layout(); // compute placement from _full/_image_size/_meta_bytes/_pay_size
|
||||
uint32_t pay_part(uint32_t L) const { return _pay_part0 + (L - _pay_log0); } // payload slot offset
|
||||
bool in_trailer(uint32_t L) const { return L >= _total - 5; }
|
||||
uint32_t run(uint32_t pos, uint32_t remain) const; // bytes from `pos` that stay in one region+sector
|
||||
uint8_t* meta_slot(uint32_t L); // RAM home of a meta/trailer byte (nullptr if it's payload)
|
||||
const uint8_t* meta_slot_c(uint32_t L) const;
|
||||
void open_pay(uint32_t sec); // make `sec` the buffered sliding sector (RMW if revisited)
|
||||
void flush_pay(); // erase+write the open sliding sector
|
||||
void flush_sector(uint32_t slot_off, const uint8_t* buf, uint32_t n); // erase+program sector(s)
|
||||
|
||||
public:
|
||||
~OtaStoreFlashEsp32() override;
|
||||
bool plan_layout(bool is_full, uint32_t image_size, uint32_t payload_off, uint32_t payload_size) override;
|
||||
bool begin(uint32_t total_size) override;
|
||||
bool write(uint32_t offset, const uint8_t* data, uint32_t len) override;
|
||||
bool read(uint32_t offset, uint8_t* buf, uint32_t len) const override;
|
||||
uint32_t capacity() const override { return _psize; } // loose bound; plan_layout does the real check
|
||||
uint32_t staged_size() const override { return _total; }
|
||||
void clear() override;
|
||||
bool set_meta_size(uint32_t meta_bytes) override { return meta_bytes < OTA_ESP32_META_CAP; }
|
||||
void finalize() override;
|
||||
void checkpoint() override; // persist meta(leaves) + open payload sector so a reboot can resume
|
||||
bool reopen() override; // re-attach to a container already staged in the slot (scan + rebuild geometry)
|
||||
|
||||
// No contiguous RAM/mmap view: the staged container lives in the slot (split for FULL). The ESP32
|
||||
// apply path reads what it needs via read()/esp_partition_read instead of a data() pointer, so this
|
||||
// returns nullptr (kept for interface parity with the nRF52 store, whose flash is memory-mapped).
|
||||
const uint8_t* data() const { return nullptr; }
|
||||
|
||||
// Apply-path accessors: where the staged container physically lives in the inactive slot.
|
||||
const esp_partition_t* partition() const { return _part; }
|
||||
uint32_t write_start() const { return _write_start; } // delta: container offset 0 in the slot
|
||||
bool is_full() const { return _full; }
|
||||
uint32_t image_size() const { return _image_size; }
|
||||
uint32_t meta_bytes() const { return _meta_bytes; }
|
||||
uint32_t payload_slot_off(uint32_t k) const { return _pay_part0 + k; } // slot off of payload byte k
|
||||
};
|
||||
|
||||
} // namespace ota
|
||||
} // namespace mesh
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,159 @@
|
||||
#include "OtaStoreFlashNrf52.h"
|
||||
|
||||
#if defined(NRF52_PLATFORM) && defined(OTA_FLASH_STORE)
|
||||
|
||||
#include "OtaSelf.h"
|
||||
#include "OtaDebug.h"
|
||||
#include "OtaByteIO.h" // align_down / rd_u32le (flash-page geometry + header read)
|
||||
#include <string.h>
|
||||
#include "flash/flash_nrf5x.h" // Adafruit core internal-flash driver (SoftDevice-safe; LittleFS path)
|
||||
|
||||
namespace mesh {
|
||||
namespace ota {
|
||||
|
||||
// Write one whole 4 KB page from `buf` to flash (erase + program, ~85 ms). `buf` is PG bytes, 0xFF-padded
|
||||
// past the container, so the program is clean. The last container page ends exactly at FS_START (both
|
||||
// FS_START and _write_start are page-aligned and the container ends <= FS_START), so a full-page write
|
||||
// never reaches into ExtraFS.
|
||||
void OtaStoreFlashNrf52::flush_page(uint32_t page_idx, const uint8_t* buf) {
|
||||
uint32_t addr = _write_start + page_idx * PG;
|
||||
if (addr + PG > MOTA_NRF52_FS_START) return; // defensive: never cross the staging ceiling
|
||||
OTA_DBG("OTA flash: write page %u @ %08x\n", (unsigned)page_idx, (unsigned)addr);
|
||||
flash_nrf5x_write(addr, buf, PG);
|
||||
flash_nrf5x_flush();
|
||||
}
|
||||
|
||||
void OtaStoreFlashNrf52::flush_pay() {
|
||||
if (_pay_idx != 0) flush_page(_pay_idx, _pay_page); // _pay_idx 0 == no payload page open
|
||||
}
|
||||
|
||||
uint32_t OtaStoreFlashNrf52::run(uint32_t pos, uint32_t remain) const {
|
||||
uint32_t trailer = _total - 5; // container is always >= 13 bytes (begin checks)
|
||||
if (pos >= trailer) return remain; // tail: caller bounds remain to <= 5 already
|
||||
uint32_t end = pos + remain;
|
||||
uint32_t page_end = (pos / PG + 1) * PG;
|
||||
if (end > page_end) end = page_end; // a run stays within one flash page,
|
||||
if (end > trailer) end = trailer; // and never crosses into the trailer tail
|
||||
return end - pos;
|
||||
}
|
||||
|
||||
const uint8_t* OtaStoreFlashNrf52::read_slot(uint32_t pos) const {
|
||||
if (pos >= _total - 5) return _trailer + (pos - (_total - 5)); // trailer tail (RAM until finalize)
|
||||
uint32_t page = pos / PG;
|
||||
if (page == 0) return _meta_page + pos; // pinned page 0 (incl. leaves)
|
||||
if (page == _pay_idx) return _pay_page + (pos - page * PG); // current sliding payload page
|
||||
return (const uint8_t*)(uintptr_t)(_write_start + pos); // already flushed -> memory-mapped
|
||||
}
|
||||
|
||||
uint8_t* OtaStoreFlashNrf52::write_slot(uint32_t pos) {
|
||||
if (pos >= _total - 5) return _trailer + (pos - (_total - 5));
|
||||
uint32_t page = pos / PG;
|
||||
if (page == 0) return _meta_page + pos;
|
||||
if (page > _pay_idx) { flush_pay(); _pay_idx = page; memset(_pay_page, 0xFF, PG); } // advance, fresh page
|
||||
if (page == _pay_idx) return _pay_page + (pos - page * PG);
|
||||
return nullptr; // page < _pay_idx: already flushed
|
||||
}
|
||||
|
||||
bool OtaStoreFlashNrf52::begin(uint32_t total_size) {
|
||||
clear();
|
||||
if (total_size < 13 || total_size > capacity()) return false; // 13 = header(8) + trailer(5)
|
||||
|
||||
// bottom-align against FS_START so the trailer ends exactly at FS_START (bootloader scans for it)
|
||||
uint32_t start = align_down(MOTA_NRF52_FS_START - total_size, PG);
|
||||
|
||||
// never collide with the running application image (its extent comes from its EndF trailer)
|
||||
uint32_t app_end = MOTA_NRF52_APP_BASE;
|
||||
SelfFwInfo fi;
|
||||
if (ota_self_firmware(fi) && fi.valid) app_end = MOTA_NRF52_APP_BASE + fi.image_len;
|
||||
if (start < app_end) return false;
|
||||
|
||||
_write_start = start;
|
||||
_total = total_size;
|
||||
memset(_meta_page, 0xFF, PG); // assemble page 0 in RAM; 0xFF = erased sentinel (unfilled leaf slots)
|
||||
memset(_trailer, 0xFF, sizeof(_trailer));
|
||||
_pay_idx = 0;
|
||||
_flushed = false;
|
||||
OTA_DBG("OTA flash: begin total=%u start=%08x app_end=%08x\n",
|
||||
(unsigned)total_size, (unsigned)start, (unsigned)app_end);
|
||||
return true; // no pre-erase: each page is erased by its own (single) flush
|
||||
}
|
||||
|
||||
bool OtaStoreFlashNrf52::write(uint32_t offset, const uint8_t* d, uint32_t len) {
|
||||
if ((uint64_t)offset + len > _total) return false;
|
||||
for (uint32_t pos = offset, end = offset + len; pos < end; ) {
|
||||
uint32_t n = run(pos, end - pos);
|
||||
if (uint8_t* dst = write_slot(pos)) {
|
||||
memcpy(dst, d, n);
|
||||
} else {
|
||||
// out-of-order write to an already-flushed page: read-modify-write straight to flash. Safe -- the
|
||||
// driver erases the page before programming, so re-touching it never breaks writes-per-word.
|
||||
OTA_DBG("OTA flash: RMW page %u (out-of-order) @ off %u\n", (unsigned)(pos / PG), (unsigned)pos);
|
||||
if (flash_nrf5x_write(_write_start + pos, d, n) < 0) return false;
|
||||
flash_nrf5x_flush();
|
||||
}
|
||||
pos += n; d += n;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool OtaStoreFlashNrf52::read(uint32_t offset, uint8_t* buf, uint32_t len) const {
|
||||
if ((uint64_t)offset + len > _total) return false;
|
||||
for (uint32_t pos = offset, end = offset + len; pos < end; ) {
|
||||
uint32_t n = run(pos, end - pos);
|
||||
memcpy(buf, read_slot(pos), n);
|
||||
pos += n; buf += n;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void OtaStoreFlashNrf52::finalize() {
|
||||
if (_flushed || _total == 0) return;
|
||||
OTA_DBG("OTA flash: finalize total=%u\n", (unsigned)_total);
|
||||
flush_pay(); // the last (highest) payload page, if one is open
|
||||
flush_page(0, _meta_page); // page 0: header + manifest + leaves + first payload bytes
|
||||
flash_nrf5x_write(_write_start + _total - 5, _trailer, 5); // trailer tail (radio idle at COMPLETE)
|
||||
flash_nrf5x_flush();
|
||||
_flushed = true;
|
||||
}
|
||||
|
||||
// Persist mid-transfer progress so a reboot can resume. Order matters for consistency: flush the open
|
||||
// payload page FIRST, then page 0 (the leaf-progress markers) -- so every block whose leaf is now in flash
|
||||
// also has its payload in flash. Infrequent (every OTA_CHECKPOINT_BLOCKS blocks), so the 2 extra page
|
||||
// erases don't matter; at LoRa block rates it's roughly once per many minutes.
|
||||
void OtaStoreFlashNrf52::checkpoint() {
|
||||
if (_total == 0 || _flushed) return;
|
||||
flush_pay(); // keep _pay_idx open (it may still receive writes); just re-flush its bytes
|
||||
flush_page(0, _meta_page); // header + manifest + leaves accumulated so far
|
||||
}
|
||||
|
||||
// Re-attach to a container already staged in flash (after a reboot), without erasing. The container is
|
||||
// bottom-aligned (begin: start = (FS_START - total) & ~(PG-1)) and flash is memory-mapped, so scan page
|
||||
// starts from just below FS_START down to the app end for MOTA_MAGIC with a self-consistent total; adopt
|
||||
// the first match (highest address = most recent for the common single-container case). The manager then
|
||||
// parses the loaded manifest and validates geometry/root, so a stale leftover is rejected there.
|
||||
bool OtaStoreFlashNrf52::reopen() {
|
||||
uint32_t app_end = MOTA_NRF52_APP_BASE;
|
||||
SelfFwInfo fi;
|
||||
if (ota_self_firmware(fi) && fi.valid) app_end = MOTA_NRF52_APP_BASE + fi.image_len;
|
||||
for (uint32_t start = align_down(MOTA_NRF52_FS_START - PG, PG); start >= app_end; start -= PG) {
|
||||
const uint8_t* p = (const uint8_t*)(uintptr_t)start;
|
||||
if (memcmp(p, MOTA_MAGIC, 4) != 0) continue;
|
||||
uint32_t total = rd_u32le(p + 4);
|
||||
if (total < 13 || total > capacity()) continue;
|
||||
if (align_down(MOTA_NRF52_FS_START - total, PG) != start) continue; // must match begin()'s placement
|
||||
_write_start = start;
|
||||
_total = total;
|
||||
memcpy(_meta_page, p, PG); // load page 0 (header+manifest+leaves) into RAM to continue
|
||||
memcpy(_trailer, p + (total - 5), 5); // recover the trailer tail (flushed at last finalize, if any)
|
||||
_pay_idx = 0;
|
||||
_flushed = false;
|
||||
OTA_DBG("OTA flash: reopen total=%u start=%08x\n", (unsigned)total, (unsigned)start);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
} // namespace ota
|
||||
} // namespace mesh
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,77 @@
|
||||
#pragma once
|
||||
|
||||
#if defined(NRF52_PLATFORM) && defined(OTA_FLASH_STORE)
|
||||
|
||||
#include "OtaStore.h"
|
||||
#include "OtaFlashLayout_nrf52.h"
|
||||
|
||||
// Persistent flash-backed OtaStore for nRF52 (RAK4631). Stages the received `.mota` in the free flash
|
||||
// below the primary LittleFS (FS_START), bottom-aligned so its trailer ends at FS_START and the
|
||||
// bootloader can scan for it. Survives reboot — the whole point — so the bootloader can apply the
|
||||
// staged delta on the next boot.
|
||||
//
|
||||
// RAM is bounded to O(one flash page), NEVER O(mota): a 100 KB+ delta must not live in RAM.
|
||||
// - On nRF52 the flash *erase* unit is one 4 KB page and the only SoftDevice-safe writer
|
||||
// (Adafruit `flash_nrf5x`) erases the whole page on every flush (~85 ms, CPU stalled → LoRa RX
|
||||
// starved). Writing to flash per received packet therefore drops in-flight DATA and the transfer
|
||||
// stalls. The fix: coalesce to the *page*, the hardware-natural unit, and write each page once.
|
||||
// - `_meta_page` pins flash page 0 (header + manifest + the merkle-leaf progress markers, which are
|
||||
// written one-per-block all transfer long). Keeping it in RAM means streaming the payload never
|
||||
// re-erases the leaves' page. Flushed once at finalize(). Requires metadata <= one page
|
||||
// (set_meta_size enforces it; true for <= ~979 blocks, i.e. any realistic MeshCore image).
|
||||
// - `_pay_page` is a single sliding buffer for one payload page (index >= 1). It advances
|
||||
// monotonically with the (mostly in-order) block stream and flushes the page it leaves behind.
|
||||
// Rare out-of-order writes to an already-flushed page go straight to flash as a safe read-modify-
|
||||
// write (flash_nrf5x erases before programming, so re-touching a page never violates the
|
||||
// writes-per-word limit — it just costs one extra erase).
|
||||
// - The 5-byte trailer is buffered and written at finalize().
|
||||
// Net: flash is touched ~once per 4 KB page (≈ 1 per 4 blocks at 1 KB), off the per-packet path; page
|
||||
// 0 and the last page are written at finalize() with the radio idle. For a small delta (whole .mota
|
||||
// in page 0) there is ZERO flash I/O during the transfer.
|
||||
|
||||
namespace mesh {
|
||||
namespace ota {
|
||||
|
||||
class OtaStoreFlashNrf52 : public OtaStore {
|
||||
static const uint32_t PG = MOTA_NRF52_FLASH_PAGE; // 4096
|
||||
|
||||
uint32_t _write_start = 0; // flash address of container offset 0 (page-aligned)
|
||||
uint32_t _total = 0; // staged container size (0 = none)
|
||||
bool _flushed = false; // finalize() committed everything to flash
|
||||
|
||||
uint8_t _meta_page[PG]; // pinned flash page 0 (header + manifest + leaves + 1st payload)
|
||||
uint8_t _pay_page[PG]; // sliding buffer for one payload page (index _pay_idx)
|
||||
uint32_t _pay_idx = 0; // page index currently held in _pay_page (0 = none open; pages >= 1)
|
||||
uint8_t _trailer[5]; // last 5 container bytes (kept in RAM, written at finalize)
|
||||
|
||||
// Bytes from `pos` that stay in one store region (a single flash page, or the trailer tail).
|
||||
uint32_t run(uint32_t pos, uint32_t remain) const;
|
||||
// RAM home of byte `pos`: read_slot always resolves (flushed pages → memory-mapped flash); write_slot
|
||||
// opens/advances the sliding payload page and returns nullptr if `pos` is in an already-flushed page.
|
||||
const uint8_t* read_slot(uint32_t pos) const;
|
||||
uint8_t* write_slot(uint32_t pos);
|
||||
void flush_pay(); // commit _pay_page to flash (erase + program, one page)
|
||||
void flush_page(uint32_t page_idx, const uint8_t* buf); // write a full page to flash
|
||||
|
||||
public:
|
||||
bool begin(uint32_t total_size) override;
|
||||
bool write(uint32_t offset, const uint8_t* data, uint32_t len) override;
|
||||
bool read(uint32_t offset, uint8_t* buf, uint32_t len) const override;
|
||||
uint32_t capacity() const override { return MOTA_NRF52_FS_START - MOTA_NRF52_APP_BASE; }
|
||||
uint32_t staged_size() const override { return _total; }
|
||||
void clear() override { _total = 0; _pay_idx = 0; _flushed = false; }
|
||||
bool set_meta_size(uint32_t meta_bytes) override { return meta_bytes <= PG; } // leaves must fit page 0
|
||||
void finalize() override;
|
||||
void checkpoint() override; // persist page 0 (leaves) + the open payload page so a reboot can resume
|
||||
bool reopen() override; // re-attach to a container already staged in flash (scan for it)
|
||||
|
||||
// Contiguous view (flash is memory-mapped). VALID ONLY AFTER finalize() — before that, page 0 and the
|
||||
// tail are still in RAM. OtaManager/OtaCli/verify use this only once the transfer is COMPLETE.
|
||||
const uint8_t* data() const { return (const uint8_t*)(uintptr_t)_write_start; }
|
||||
uint32_t write_start() const { return _write_start; }
|
||||
};
|
||||
|
||||
} // namespace ota
|
||||
} // namespace mesh
|
||||
|
||||
#endif
|
||||
Reference in new issue
Block a user