FwUpdate: format 2 delta packages (FwPatch in RAM), base checked with the manifest
This commit is contained in:
1 parent
8d3e554ca3
commit
aaca10958e
6 files changed
+225
-43
No files matched your search
+179
-31
@@ -1,4 +1,5 @@
|
||||
#include "FwUpdate.h"
|
||||
#include "FwPatch.h"
|
||||
|
||||
#if defined(NRF52_PLATFORM)
|
||||
|
||||
@@ -22,6 +23,7 @@
|
||||
#endif
|
||||
|
||||
#define APP_START 0x27000 // S140 7.x, all our nRF52840 boards
|
||||
#define PATCH_LO (FW_OTA_STAGE_END - FW_OTA_PATCH_AREA) // delta packages live here
|
||||
#define PAGE 4096u
|
||||
#define BL_SETTINGS 0xFF000u // sdk11 bootloader_settings_t, nRF52840
|
||||
#define RING_BITS 15
|
||||
@@ -33,7 +35,7 @@ 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 format; // 1 = deflate full image, 2 = in-place delta (FwPatch)
|
||||
uint8_t reserved0[3];
|
||||
uint32_t raw_size;
|
||||
uint32_t comp_size;
|
||||
@@ -41,7 +43,7 @@ struct __attribute__((packed)) Manifest {
|
||||
uint8_t comp_sha[32];
|
||||
char target[32];
|
||||
char version[48];
|
||||
uint8_t reserved1[32];
|
||||
uint8_t base_sha[32]; // format 2: sha256 of the image the delta applies to
|
||||
uint8_t signature[64]; // Ed25519 over bytes 0..191
|
||||
};
|
||||
static_assert(sizeof(Manifest) == FW_OTA_MANIFEST_SIZE, "manifest layout");
|
||||
@@ -354,6 +356,45 @@ RAMFUNC static void copy_and_reset(Inflate *s, CopyCtx *c) {
|
||||
}
|
||||
}
|
||||
|
||||
// --- delta apply (format 2) ------------------------------------------------------
|
||||
// The applier itself (FwPatch.cpp, detools) is moved to RAM by tools/ota_ramlib.py;
|
||||
// these are its flash callbacks and the entry point, all RAM as well.
|
||||
|
||||
RAMFUNC static void fwp_erase_page(uintptr_t a) {
|
||||
wdt_feed();
|
||||
nvmc_erase((uint32_t)a);
|
||||
}
|
||||
|
||||
RAMFUNC static void fwp_write_page(uintptr_t a, const uint8_t *src, uint32_t n) {
|
||||
nvmc_write((uint32_t)a, src, n);
|
||||
}
|
||||
|
||||
RAMFUNC static void fwp_tick() {
|
||||
wdt_feed();
|
||||
}
|
||||
|
||||
RAMFUNC static void patch_and_reset(fwp_job *j, const uint32_t *settings) {
|
||||
// 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 *)settings, PAGE);
|
||||
// 2) patch the running image in place
|
||||
fwp_run(j);
|
||||
// 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 (;;) {
|
||||
}
|
||||
}
|
||||
|
||||
// An env without tools/ota_ramlib.py would leave the applier in flash, which the
|
||||
// real run erases under itself. Refuse delta packages there.
|
||||
static bool deltaInRam() {
|
||||
return (uintptr_t)&fwp_run >= 0x20000000u;
|
||||
}
|
||||
|
||||
// --- helpers --------------------------------------------------------------------
|
||||
|
||||
static uint16_t crc16_update(uint16_t crc, const uint8_t *p, uint32_t len) {
|
||||
@@ -424,6 +465,12 @@ struct VerifyCtx {
|
||||
bool overlap;
|
||||
};
|
||||
|
||||
static void delta_sink(void *vctx, const uint8_t *data, uint32_t len) {
|
||||
VerifyCtx *v = (VerifyCtx *)vctx;
|
||||
v->sha.update(data, len);
|
||||
v->crc = crc16_update(v->crc, data, len);
|
||||
}
|
||||
|
||||
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);
|
||||
@@ -451,19 +498,75 @@ uint16_t FwUpdate::battMinMv() {
|
||||
return mv;
|
||||
}
|
||||
|
||||
const uint8_t *FwUpdate::baseSha() {
|
||||
if (!_base_known) {
|
||||
SHA256 sha;
|
||||
sha.update((const uint8_t *)APP_START, imageEnd() - APP_START);
|
||||
sha.finalize(_base_sha, 32);
|
||||
_base_known = true;
|
||||
}
|
||||
return _base_sha;
|
||||
}
|
||||
|
||||
bool FwUpdate::checkManifest() {
|
||||
const Manifest *m = (const Manifest *)_stage;
|
||||
uint8_t key[32];
|
||||
if (memcmp(m->magic, "MCFW", 4) != 0 || (m->format != 1 && m->format != 2)) {
|
||||
strcpy(_err, "not a package");
|
||||
return false;
|
||||
}
|
||||
if (!pubkey(key)) {
|
||||
strcpy(_err, "bad FW_OTA_PUBKEY");
|
||||
return false;
|
||||
}
|
||||
if (!ed25519_verify(m->signature, (const uint8_t *)m, offsetof(Manifest, signature), key)) {
|
||||
strcpy(_err, "bad signature");
|
||||
return false;
|
||||
}
|
||||
if (strncmp(m->target, FW_OTA_TARGET, sizeof(m->target)) != 0) {
|
||||
strcpy(_err, "wrong target");
|
||||
return false;
|
||||
}
|
||||
if (FW_OTA_MANIFEST_SIZE + m->comp_size != _total) {
|
||||
strcpy(_err, "size mismatch");
|
||||
return false;
|
||||
}
|
||||
if (m->format == 2) {
|
||||
if (!deltaInRam()) {
|
||||
strcpy(_err, "no delta support in this build");
|
||||
return false;
|
||||
}
|
||||
if (_stage != PATCH_LO) {
|
||||
strcpy(_err, "delta too big for the patch area");
|
||||
return false;
|
||||
}
|
||||
if (memcmp(m->base_sha, baseSha(), 32) != 0) {
|
||||
strcpy(_err, "wrong base image");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
_format = m->format;
|
||||
return true;
|
||||
}
|
||||
|
||||
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))));
|
||||
{
|
||||
const uint8_t *b = baseSha();
|
||||
sprintf(reply, "> idle, batt %umV (min %u), free %lu B, base %lu %02x%02x%02x%02x%s", mv, FW_OTA_MIN_MV,
|
||||
(unsigned long)(FW_OTA_STAGE_END - ((imageEnd() + PAGE - 1) & ~(PAGE - 1))),
|
||||
(unsigned long)(imageEnd() - APP_START), b[0], b[1], b[2], b[3], deltaInRam() ? ", delta" : "");
|
||||
}
|
||||
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);
|
||||
sprintf(reply, "> ready %s%s, %lu B, crc %04X, batt %umV", _version, _format == 2 ? " (delta)" : "",
|
||||
(unsigned long)_raw_size, _crc, mv);
|
||||
break;
|
||||
case FAILED:
|
||||
sprintf(reply, "> failed: %s", _err);
|
||||
@@ -473,7 +576,9 @@ void FwUpdate::cmdStatus(char *reply) {
|
||||
|
||||
void FwUpdate::cmdBegin(const char *arg, char *reply) {
|
||||
uint32_t total = strtoul(arg, NULL, 10);
|
||||
uint32_t stage = (imageEnd() + PAGE - 1) & ~(PAGE - 1);
|
||||
// small = delta: top of the free flash, out of reach of the in-place shift;
|
||||
// big = full image: right above the running one, as before
|
||||
uint32_t stage = total <= FW_OTA_PATCH_AREA ? PATCH_LO : (imageEnd() + PAGE - 1) & ~(PAGE - 1);
|
||||
if (total <= FW_OTA_MANIFEST_SIZE) {
|
||||
strcpy(reply, "ERR: fw begin <bytes>");
|
||||
return;
|
||||
@@ -487,6 +592,8 @@ void FwUpdate::cmdBegin(const char *arg, char *reply) {
|
||||
_total = total;
|
||||
memset(_have, 0, sizeof(_have));
|
||||
_state = RECEIVING;
|
||||
_manifest_ok = false;
|
||||
_format = 0;
|
||||
_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);
|
||||
@@ -512,6 +619,22 @@ void FwUpdate::cmdWrite(const char *arg, char *reply) {
|
||||
flash_nrf5x_write(_stage + off, buf, len);
|
||||
_have[idx >> 3] |= (1 << (idx & 7));
|
||||
}
|
||||
if (!_manifest_ok) {
|
||||
// as soon as the manifest is complete: refuse a package for another board or
|
||||
// another base before the rest of it is sent
|
||||
bool all = true;
|
||||
for (uint32_t i = 0; i * FW_OTA_CHUNK < FW_OTA_MANIFEST_SIZE; i++)
|
||||
if (!(_have[i >> 3] & (1 << (i & 7)))) all = false;
|
||||
if (all) {
|
||||
flash_nrf5x_flush();
|
||||
if (!checkManifest()) {
|
||||
_state = FAILED;
|
||||
sprintf(reply, "ERR: %s", _err);
|
||||
return;
|
||||
}
|
||||
_manifest_ok = true;
|
||||
}
|
||||
}
|
||||
strcpy(reply, "ok");
|
||||
}
|
||||
|
||||
@@ -550,28 +673,8 @@ void FwUpdate::cmdVerify(char *reply) {
|
||||
|
||||
_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) {
|
||||
if (!checkManifest()) goto fail;
|
||||
if (APP_START + m->raw_size > (m->format == 2 ? PATCH_LO : FW_OTA_STAGE_END)) {
|
||||
strcpy(_err, "image too big");
|
||||
goto fail;
|
||||
}
|
||||
@@ -585,7 +688,39 @@ void FwUpdate::cmdVerify(char *reply) {
|
||||
goto fail;
|
||||
}
|
||||
}
|
||||
{
|
||||
if (m->format == 2) {
|
||||
// dry run of the very code `fw apply` runs: no flash touched, the new image
|
||||
// streamed through sha256 + the bootloader's CRC16
|
||||
VerifyCtx *v = new VerifyCtx();
|
||||
if (!v) {
|
||||
strcpy(_err, "no RAM");
|
||||
goto fail;
|
||||
}
|
||||
v->crc = 0xFFFF;
|
||||
fwp_job j;
|
||||
memset(&j, 0, sizeof(j));
|
||||
j.mem_base = APP_START;
|
||||
j.mem_size = PATCH_LO - APP_START;
|
||||
j.from_size = imageEnd() - APP_START;
|
||||
j.patch = (const uint8_t *)(_stage + FW_OTA_MANIFEST_SIZE);
|
||||
j.patch_size = m->comp_size;
|
||||
j.dry = 1;
|
||||
j.sink = delta_sink;
|
||||
j.sink_ctx = v;
|
||||
int err = fwp_run(&j);
|
||||
uint8_t h[32];
|
||||
v->sha.finalize(h, 32);
|
||||
_crc = v->crc;
|
||||
delete v;
|
||||
if (err) {
|
||||
sprintf(_err, "delta %d/%d", err, j.detools_err);
|
||||
goto fail;
|
||||
}
|
||||
if (j.to_size != m->raw_size || memcmp(h, m->raw_sha, 32) != 0) {
|
||||
strcpy(_err, "image hash");
|
||||
goto fail;
|
||||
}
|
||||
} else {
|
||||
uint8_t *ring = (uint8_t *)malloc(RING_SIZE);
|
||||
VerifyCtx *v = new VerifyCtx();
|
||||
if (!ring || !v) {
|
||||
@@ -680,12 +815,12 @@ void FwUpdate::loop() {
|
||||
if (_state != READY) return;
|
||||
|
||||
uint32_t *settings = (uint32_t *)malloc(PAGE);
|
||||
uint8_t *ring = (uint8_t *)malloc(RING_SIZE);
|
||||
uint8_t *ring = _format == 2 ? (uint8_t *)1 : (uint8_t *)malloc(RING_SIZE); // delta needs no ring
|
||||
static Inflate s;
|
||||
static CopyCtx c;
|
||||
if (!settings || !ring) {
|
||||
free(settings);
|
||||
free(ring);
|
||||
if (_format != 2) free(ring);
|
||||
_state = FAILED;
|
||||
strcpy(_err, "no RAM for apply");
|
||||
return;
|
||||
@@ -698,6 +833,18 @@ void FwUpdate::loop() {
|
||||
s16[2] = 0x00FF; // bank_1 = BANK_INVALID_APP
|
||||
settings[2] = _raw_size; // bank_0_size
|
||||
|
||||
static fwp_job j;
|
||||
if (_format == 2) {
|
||||
memset(&j, 0, sizeof(j));
|
||||
j.mem_base = APP_START;
|
||||
j.mem_size = PATCH_LO - APP_START;
|
||||
j.from_size = imageEnd() - APP_START;
|
||||
j.patch = (const uint8_t *)(_stage + FW_OTA_MANIFEST_SIZE);
|
||||
j.patch_size = ((const Manifest *)_stage)->comp_size;
|
||||
j.erase = fwp_erase_page;
|
||||
j.write = fwp_write_page;
|
||||
j.tick = fwp_tick;
|
||||
}
|
||||
c.stage_blob = _stage + FW_OTA_MANIFEST_SIZE;
|
||||
c.settings = settings;
|
||||
memset(&s, 0, sizeof(s));
|
||||
@@ -715,6 +862,7 @@ void FwUpdate::loop() {
|
||||
sd_softdevice_is_enabled(&sd);
|
||||
if (sd) sd_softdevice_disable();
|
||||
__disable_irq();
|
||||
if (_format == 2) patch_and_reset(&j, settings);
|
||||
copy_and_reset(&s, &c);
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user