Add guarded nRF52 OTA rescue and SF5 test mode
This commit is contained in:
1 parent
710a6e02c5
commit
4a3d86ef98
14 files changed
+225
-47
No files matched your search
@@ -407,6 +407,10 @@ bool ota_apply_detools_mota(const uint8_t* buf, uint32_t len, const SignerAllowl
|
||||
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;
|
||||
}
|
||||
bool ota_rescue_mota_nrf52(const uint8_t*, uint32_t, const SignerAllowlist&, const uint8_t*,
|
||||
uint32_t, ApplyState& st, char* msg) {
|
||||
st = ApplyState(); strcpy(msg, "rescue is internal-flash nRF52-only"); return false;
|
||||
}
|
||||
|
||||
void ota_reboot_to_apply() { esp_restart(); } // boots the slot armed by ota_apply_detools_mota; no return
|
||||
|
||||
@@ -438,8 +442,11 @@ uint8_t ota_bootloader_last_rc() { // bootloader's last in-place-app
|
||||
return (uint8_t)v;
|
||||
}
|
||||
|
||||
bool ota_apply_mota_nrf52(const uint8_t* buf, uint32_t len, const SignerAllowlist& allow,
|
||||
ApplyState& st, char* msg) {
|
||||
static bool ota_apply_mota_nrf52_impl(const uint8_t* buf, uint32_t len,
|
||||
const SignerAllowlist& allow,
|
||||
const uint8_t* rescue_base_hash,
|
||||
uint32_t local_target_id,
|
||||
ApplyState& st, char* msg) {
|
||||
st = ApplyState();
|
||||
MotaManifest m;
|
||||
if (!mota_parse(buf, len, m)) { strcpy(msg, "parse failed"); return false; }
|
||||
@@ -470,12 +477,31 @@ bool ota_apply_mota_nrf52(const uint8_t* buf, uint32_t len, const SignerAllowlis
|
||||
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.
|
||||
// 2) target firmware. Normally the delta must match the validated EndF body hash. The explicit rescue
|
||||
// path exists only for a firmware whose normal EndF validation is broken: it requires the operator's
|
||||
// exact package base hash and the package's target_id. It then delegates the actual running-image
|
||||
// base check to the bootloader, which performs that check before its first application write.
|
||||
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; }
|
||||
const bool self_valid = ota_self_firmware(fi) && fi.valid;
|
||||
if (rescue_base_hash) {
|
||||
switch (ota_nrf52_rescue_gate(self_valid, m.base_hash, rescue_base_hash,
|
||||
m.target_id, local_target_id)) {
|
||||
case NRF52_RESCUE_SELF_VALID:
|
||||
strcpy(msg, "EndF OK; use ota install"); return false;
|
||||
case NRF52_RESCUE_BASE_MISSING:
|
||||
case NRF52_RESCUE_BASE_MISMATCH:
|
||||
strcpy(msg, "rescue base mismatch"); return false;
|
||||
case NRF52_RESCUE_TARGET_MISMATCH:
|
||||
strcpy(msg, "rescue target mismatch"); return false;
|
||||
case NRF52_RESCUE_OK:
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
if (!self_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
|
||||
@@ -493,11 +519,26 @@ bool ota_apply_mota_nrf52(const uint8_t* buf, uint32_t len, const SignerAllowlis
|
||||
|
||||
// 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)");
|
||||
if (rescue_base_hash) {
|
||||
strcpy(msg, "rescue armed; bootloader checks base");
|
||||
} else {
|
||||
sprintf(msg, "verified%s; applying - rebooting into bootloader once this reply is sent",
|
||||
vr.is_signed ? " (signer trusted)" : " (unsigned)");
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ota_apply_mota_nrf52(const uint8_t* buf, uint32_t len, const SignerAllowlist& allow,
|
||||
ApplyState& st, char* msg) {
|
||||
return ota_apply_mota_nrf52_impl(buf, len, allow, nullptr, 0, st, msg);
|
||||
}
|
||||
|
||||
bool ota_rescue_mota_nrf52(const uint8_t* buf, uint32_t len, const SignerAllowlist& allow,
|
||||
const uint8_t operator_base_hash[8], uint32_t local_target_id,
|
||||
ApplyState& st, char* msg) {
|
||||
return ota_apply_mota_nrf52_impl(buf, len, allow, operator_base_hash, local_target_id, st, msg);
|
||||
}
|
||||
|
||||
#if defined(OTA_SD_STORE)
|
||||
bool ota_apply_mota_nrf52(OtaStoreSdNrf52& store, const SignerAllowlist& allow,
|
||||
ApplyState& st, char* msg) {
|
||||
@@ -586,6 +627,10 @@ 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; }
|
||||
bool ota_rescue_mota_nrf52(const uint8_t*, uint32_t, const SignerAllowlist&, const uint8_t*,
|
||||
uint32_t, ApplyState& st, char* msg) {
|
||||
st = ApplyState(); strcpy(msg, "unsupported"); return false;
|
||||
}
|
||||
void ota_reboot_to_apply() {}
|
||||
uint8_t ota_bootloader_last_rc() { return 0; }
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
#include <string.h>
|
||||
#include "SignerAllowlist.h"
|
||||
|
||||
// P6 apply (full-image, ESP32 A/B). The new image is delivered into the inactive OTA slot; the device
|
||||
@@ -23,6 +24,31 @@ struct ApplyState {
|
||||
uint8_t image_hash[32] = {0};
|
||||
};
|
||||
|
||||
// Policy gate for the nRF52 recovery-only apply path. This is intentionally separate from the normal
|
||||
// EndF/base check so it cannot become an accidental fallback: the operator must invoke the rescue
|
||||
// command with the exact base hash carried by the staged package, and the package must target this
|
||||
// build. The bootloader still recomputes and checks the actual running-image base before writing it.
|
||||
enum Nrf52RescueGate : uint8_t {
|
||||
NRF52_RESCUE_OK = 0,
|
||||
NRF52_RESCUE_SELF_VALID,
|
||||
NRF52_RESCUE_BASE_MISSING,
|
||||
NRF52_RESCUE_BASE_MISMATCH,
|
||||
NRF52_RESCUE_TARGET_MISMATCH,
|
||||
};
|
||||
|
||||
inline Nrf52RescueGate ota_nrf52_rescue_gate(bool self_valid,
|
||||
const uint8_t* manifest_base_hash,
|
||||
const uint8_t* operator_base_hash,
|
||||
uint32_t manifest_target_id,
|
||||
uint32_t local_target_id) {
|
||||
if (self_valid) return NRF52_RESCUE_SELF_VALID;
|
||||
if (!manifest_base_hash || !operator_base_hash) return NRF52_RESCUE_BASE_MISSING;
|
||||
if (memcmp(manifest_base_hash, operator_base_hash, 8) != 0) return NRF52_RESCUE_BASE_MISMATCH;
|
||||
if (!manifest_target_id || !local_target_id || manifest_target_id != local_target_id)
|
||||
return NRF52_RESCUE_TARGET_MISMATCH;
|
||||
return NRF52_RESCUE_OK;
|
||||
}
|
||||
|
||||
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
|
||||
@@ -50,6 +76,11 @@ bool ota_apply_detools_mota(const uint8_t* buf, uint32_t len,
|
||||
// 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);
|
||||
// Recovery for failed app-side EndF validation. This is available only through the explicit
|
||||
// `ota rescue install <base_hash16>` CLI command and never through normal or automatic installation.
|
||||
bool ota_rescue_mota_nrf52(const uint8_t* buf, uint32_t len,
|
||||
const SignerAllowlist& allow, const uint8_t operator_base_hash[8],
|
||||
uint32_t local_target_id, ApplyState& st, char* msg);
|
||||
#if defined(NRF52_PLATFORM) && defined(OTA_SD_STORE)
|
||||
class OtaStoreSdNrf52;
|
||||
bool ota_apply_mota_nrf52(OtaStoreSdNrf52& store,
|
||||
|
||||
@@ -131,6 +131,10 @@ bool handle_ota_command(const char* command, char* reply, mesh::MainBoard& board
|
||||
snprintf(reply, 160,
|
||||
"OTA seeder: status | stats | ls=find images | get <#> folder=capture | cancel | "
|
||||
"announce | folder | config. LoRa install is disabled.");
|
||||
#elif defined(NRF52_PLATFORM) && defined(OTA_FLASH_STORE) && !defined(OTA_SD_STORE)
|
||||
strcpy(reply,
|
||||
"OTA: status | stats | ls | get | install | rescue install <hash16> | cancel | announce | "
|
||||
"self | folder | config | key");
|
||||
#else
|
||||
snprintf(reply, 160,
|
||||
"OTA: status | stats=admin ids/hashes | ls=find updates | get <#>=download | install | cancel | "
|
||||
@@ -395,6 +399,32 @@ bool handle_ota_command(const char* command, char* reply, mesh::MainBoard& board
|
||||
#endif
|
||||
#endif
|
||||
|
||||
} else if (is_cmd(a, "rescue", &rest)) {
|
||||
// Recovery-only nRF52 handoff for firmware whose normal EndF validation is broken. This command is
|
||||
// deliberately not an alias or automatic fallback: the operator must name `install` and provide
|
||||
// the exact 8-byte base hash carried by the already-fetched package. The bootloader independently
|
||||
// hashes the running app and refuses a mismatch before writing any application flash.
|
||||
#if defined(NRF52_PLATFORM) && defined(OTA_FLASH_STORE) && !defined(OTA_SD_STORE) && !defined(OTA_SEEDER_ONLY)
|
||||
const char* hash_text = nullptr;
|
||||
uint8_t operator_base_hash[8];
|
||||
if (!is_cmd(rest, "install", &hash_text) ||
|
||||
!mesh::Utils::fromHex(operator_base_hash, sizeof(operator_base_hash), hash_text)) {
|
||||
strcpy(reply, "ERR usage: ota rescue install <hash16>");
|
||||
return true;
|
||||
}
|
||||
if (c.manager.fetchState() != OtaManager::COMPLETE || c.fetch_store.staged_size() == 0) {
|
||||
sprintf(reply, "ERR no complete update fetched (fetch=%c %u/%u)",
|
||||
fstate_char(c.manager.fetchState()), (unsigned)c.manager.blocksHave(),
|
||||
(unsigned)c.manager.blocksTotal());
|
||||
return true;
|
||||
}
|
||||
char m2[100];
|
||||
bool ok = c.apply_fetched_rescue(operator_base_hash, m2);
|
||||
sprintf(reply, "%s | %s", ok ? "OK" : "ERR", m2);
|
||||
#else
|
||||
strcpy(reply, "ERR rescue requires an internal-flash nRF52 LoRa OTA build");
|
||||
#endif
|
||||
|
||||
} else if (is_cmd(a, "install|apply|applydelta", &rest)) {
|
||||
// Apply the fetched update. Destructive (reflashes + reboots) and GATED, not interactive (no "type
|
||||
// yes" round-trip - unreliable over LoRa): refuse unless the fetch is COMPLETE, then the apply path
|
||||
|
||||
@@ -140,11 +140,28 @@ struct OtaContext {
|
||||
// so the deferred-reboot path (mesh loop) takes over. Caller ensures the fetch is COMPLETE. Shared by
|
||||
// manual `ota applydelta` and the auto-install path.
|
||||
bool apply_fetched(char* msg) {
|
||||
return apply_fetched_impl(nullptr, msg);
|
||||
}
|
||||
|
||||
// Explicit recovery entry point for an internal-flash nRF52 whose normal EndF validation fails. This
|
||||
// is never called by auto-install; the CLI requires the package's exact 8-byte base hash.
|
||||
bool apply_fetched_rescue(const uint8_t operator_base_hash[8], char* msg) {
|
||||
return apply_fetched_impl(operator_base_hash, msg);
|
||||
}
|
||||
|
||||
bool apply_fetched_impl(const uint8_t* rescue_base_hash, char* msg) {
|
||||
#if defined(OTA_SEEDER_ONLY)
|
||||
strncpy(msg, "refused: this build serves mOTA images but cannot install one", 96);
|
||||
msg[95] = 0;
|
||||
return false;
|
||||
#else
|
||||
#if !defined(NRF52_PLATFORM) || defined(OTA_SD_STORE)
|
||||
if (rescue_base_hash) {
|
||||
strncpy(msg, "rescue is only for internal-flash nRF52 builds", 96);
|
||||
msg[95] = 0;
|
||||
return false;
|
||||
}
|
||||
#endif
|
||||
#if defined(NRF52_PLATFORM) && defined(OTA_SD_STORE)
|
||||
if (sdCacheFetching()) {
|
||||
strncpy(msg, "refused: SD archive capture owns the OTA receive slot", 96);
|
||||
@@ -171,7 +188,12 @@ struct OtaContext {
|
||||
#if defined(NRF52_PLATFORM) && defined(OTA_SD_STORE)
|
||||
ok = ota_apply_mota_nrf52(fetch_store, allow, apply_st, msg);
|
||||
#elif defined(NRF52_PLATFORM)
|
||||
ok = ota_apply_mota_nrf52(fetch_store.data(), fetch_store.staged_size(), allow, apply_st, msg);
|
||||
if (rescue_base_hash) {
|
||||
ok = ota_rescue_mota_nrf52(fetch_store.data(), fetch_store.staged_size(), allow,
|
||||
rescue_base_hash, manager.target(), apply_st, msg);
|
||||
} else {
|
||||
ok = ota_apply_mota_nrf52(fetch_store.data(), fetch_store.staged_size(), allow, apply_st, msg);
|
||||
}
|
||||
#elif defined(ESP32_PLATFORM) && defined(OTA_FLASH_STORE)
|
||||
ok = ota_apply_detools_mota(fetch_store, allow, apply_st, msg);
|
||||
#else
|
||||
|
||||
Reference in new issue
Block a user