Add signed XIAO bootloader updates over LoRa
This commit is contained in:
1 parent
9aae602023
commit
75a2b8f002
34 files changed
+1604
-78
No files matched your search
+4
-2
@@ -335,7 +335,8 @@ void __attribute__((noinline)) Mesh::serviceLoopMaintenance() {
|
||||
oc.apply_hard = futureMillis(15000);
|
||||
} else if (millisHasNowPassed(oc.apply_at) &&
|
||||
(_mgr->getOutboundTotal() == 0 || millisHasNowPassed(oc.apply_hard))) {
|
||||
ota::ota_reboot_to_apply(); // does not return
|
||||
if (oc.bootloader_apply_pending) ota::ota_reboot_to_bootloader_update();
|
||||
else ota::ota_reboot_to_apply();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -410,7 +411,8 @@ void __attribute__((noinline)) Mesh::serviceLoopMaintenance() {
|
||||
_ota_autoinstall_tried = false;
|
||||
} else if (!_ota_autoinstall_tried && !oc.apply_pending
|
||||
&& oc.autoinstall == ota::OtaContext::AUTOINSTALL_TRUSTED
|
||||
&& oc.manager.fetched_is_signed()) {
|
||||
&& oc.manager.fetched_is_signed()
|
||||
&& !oc.manager.fetched_is_bootloader()) {
|
||||
_ota_autoinstall_tried = true;
|
||||
char msg[100];
|
||||
oc.apply_fetched(msg); // arms + sets apply_pending only if signed & allowlisted; refused otherwise
|
||||
|
||||
@@ -17,8 +17,16 @@ bool MotaManifest::is_approved() const {
|
||||
// always covers manifest[0, MOTA_SIGNED_LEN). Returns false on any over-read or bad format_ver.
|
||||
static bool parse_manifest_fields(ByteReader& r, MotaManifest& out) {
|
||||
out.format_ver = r.u8();
|
||||
if (out.format_ver != MOTA_FORMAT_VER) return false;
|
||||
out.flags = r.u8();
|
||||
// v2 is application-only and v3 is bootloader-only. Requiring exact flags on v3 prevents a
|
||||
// malformed or downgraded package from entering either apply path.
|
||||
if (out.format_ver == MOTA_APP_FORMAT_VER) {
|
||||
if (out.flags & MFLAG_BOOTLOADER || out.flags & ~MFLAG_KNOWN) return false;
|
||||
} else if (out.format_ver == MOTA_BOOT_FORMAT_VER) {
|
||||
if (out.flags != (MFLAG_FULL | MFLAG_SIGNED | MFLAG_BOOTLOADER)) return false;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
out.hash_algo = r.u8();
|
||||
out.target_id = r.u32();
|
||||
out.fw_version = r.u32();
|
||||
@@ -37,6 +45,13 @@ static bool parse_manifest_fields(ByteReader& r, MotaManifest& out) {
|
||||
if (!r.ok) return false;
|
||||
if (out.block_size_log2 == 0 || out.block_size_log2 > 24 || out.payload_size == 0) return false;
|
||||
out.block_count = (out.payload_size + out.block_size() - 1) / out.block_size();
|
||||
if (out.is_bootloader()) {
|
||||
static const uint8_t zero_base[8] = {0};
|
||||
if (out.fw_version == 0 || out.codec_id != CODEC_FULL || out.image_size != 0xA000u ||
|
||||
out.payload_size != 0xA000u || out.block_size_log2 != 10 ||
|
||||
out.block_count != 40 || memcmp(out.base_hash, zero_base, sizeof(zero_base)) != 0)
|
||||
return false;
|
||||
}
|
||||
// block_idx is uint16 on the wire; capping here also keeps block_count*4 (leaves length) from overflowing.
|
||||
return out.block_count != 0 && out.block_count <= 0xFFFFu;
|
||||
}
|
||||
|
||||
@@ -40,6 +40,7 @@ struct MotaManifest {
|
||||
|
||||
bool is_full() const { return flags & MFLAG_FULL; }
|
||||
bool is_signed() const { return flags & MFLAG_SIGNED; }
|
||||
bool is_bootloader() const { return flags & MFLAG_BOOTLOADER; }
|
||||
uint32_t block_size() const { return 1u << block_size_log2; }
|
||||
bool is_approved() const;
|
||||
};
|
||||
|
||||
@@ -52,7 +52,7 @@ bool ota_apply_set_manifest(const uint8_t* mf, uint32_t len, const SignerAllowli
|
||||
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)
|
||||
if (m.is_bootloader() || !m.is_full()) return false; // boot packages never enter an application slot
|
||||
st.image_size = m.image_size;
|
||||
memcpy(st.image_hash, m.image_hash, 32);
|
||||
st.manifest_ok = true;
|
||||
@@ -293,6 +293,7 @@ bool ota_apply_detools_mota(OtaStoreFlashEsp32& store, const SignerAllowlist& al
|
||||
MotaManifest m;
|
||||
if (mflen < 57 || !store.read(8, mfbuf, mflen) || !mota_parse_manifest(mfbuf, mflen, m)) {
|
||||
strcpy(msg, "manifest parse failed"); return false; }
|
||||
if (m.is_bootloader()) { strcpy(msg, "bootloader package requires explicit bootloader install"); 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; }
|
||||
|
||||
@@ -367,6 +368,7 @@ bool ota_apply_detools_mota(const uint8_t* buf, uint32_t len, const SignerAllowl
|
||||
st = ApplyState();
|
||||
MotaManifest m;
|
||||
if (!mota_parse(buf, len, m)) { strcpy(msg, "no valid .mota (parse failed)"); return false; }
|
||||
if (m.is_bootloader()) { strcpy(msg, "bootloader package cannot enter an application slot"); 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);
|
||||
@@ -416,6 +418,10 @@ bool ota_rescue_mota_nrf52(const uint8_t*, uint32_t, const SignerAllowlist&, con
|
||||
}
|
||||
|
||||
void ota_reboot_to_apply() { esp_restart(); } // boots the slot armed by ota_apply_detools_mota; no return
|
||||
void ota_reboot_to_bootloader_update() {}
|
||||
bool ota_installed_bootloader_identity(OtaBootloaderIdentity& out) {
|
||||
out = OtaBootloaderIdentity(); return false;
|
||||
}
|
||||
|
||||
#elif defined(NRF52_PLATFORM) // single-slot: verify + mark APPROVED + hand off to the bootloader
|
||||
|
||||
@@ -473,8 +479,28 @@ void ota_reboot_to_apply() { // public: set the apply magic +
|
||||
NVIC_SystemReset(); // does not return
|
||||
}
|
||||
|
||||
void ota_reboot_to_bootloader_update() {
|
||||
uint8_t sd_en = 0;
|
||||
sd_softdevice_is_enabled(&sd_en);
|
||||
if (sd_en) {
|
||||
sd_power_gpregret_clr(1, 0xFFFFFFFF);
|
||||
sd_power_gpregret_set(1, GPREGRET2_OTA_STAGE_QSPI);
|
||||
sd_power_gpregret_clr(0, 0xFFFFFFFF);
|
||||
sd_power_gpregret_set(0, GPREGRET_OTA_BOOTLOADER_UPDATE);
|
||||
} else {
|
||||
NRF_POWER->GPREGRET2 = GPREGRET2_OTA_STAGE_QSPI;
|
||||
NRF_POWER->GPREGRET = GPREGRET_OTA_BOOTLOADER_UPDATE;
|
||||
}
|
||||
NVIC_SystemReset();
|
||||
}
|
||||
|
||||
uint8_t ota_bootloader_last_rc() { return g_bootloader_last_rc; }
|
||||
|
||||
bool ota_installed_bootloader_identity(OtaBootloaderIdentity& out) {
|
||||
return ota_bootloader_identity_from_image(
|
||||
(const uint8_t*)(uintptr_t)OTA_BOOT_IMAGE_START, OTA_BOOT_IMAGE_SIZE, out);
|
||||
}
|
||||
|
||||
static bool ota_apply_mota_nrf52_impl(const uint8_t* buf, uint32_t len,
|
||||
const SignerAllowlist& allow,
|
||||
const uint8_t* rescue_base_hash,
|
||||
@@ -483,6 +509,7 @@ static bool ota_apply_mota_nrf52_impl(const uint8_t* buf, uint32_t len,
|
||||
st = ApplyState();
|
||||
MotaManifest m;
|
||||
if (!mota_parse(buf, len, m)) { strcpy(msg, "parse failed"); return false; }
|
||||
if (m.is_bootloader()) { strcpy(msg, "use explicit ota bootloader install"); 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);
|
||||
@@ -618,6 +645,10 @@ static bool ota_apply_mota_nrf52_external(Store& store, const SignerAllowlist& a
|
||||
snprintf(msg, NRF52_APPLY_MSG_CAP, "%s manifest parse failed", storage_name);
|
||||
return false;
|
||||
}
|
||||
if (m.is_bootloader()) {
|
||||
strcpy(msg, "use explicit ota bootloader install");
|
||||
return false;
|
||||
}
|
||||
const bool full = m.is_full() && m.codec_id == CODEC_FULL &&
|
||||
m.payload_size == m.image_size;
|
||||
const bool delta = !m.is_full() && m.codec_id == CODEC_DETOOLS_INPLACE;
|
||||
@@ -627,8 +658,9 @@ static bool ota_apply_mota_nrf52_external(Store& store, const SignerAllowlist& a
|
||||
return false;
|
||||
}
|
||||
const uint32_t app_base = mota_nrf52_app_base();
|
||||
if (m.image_size == 0 || app_base >= MOTA_NRF52_APP_END ||
|
||||
m.image_size > MOTA_NRF52_APP_END - app_base) {
|
||||
const uint32_t app_ceiling = mota_nrf52_application_ceiling();
|
||||
if (m.image_size == 0 || app_base >= app_ceiling ||
|
||||
m.image_size > app_ceiling - app_base) {
|
||||
strcpy(msg, "image exceeds nRF52 application region");
|
||||
return false;
|
||||
}
|
||||
@@ -687,7 +719,7 @@ static bool ota_apply_mota_nrf52_external(Store& store, const SignerAllowlist& a
|
||||
}
|
||||
if (!mota_nrf52_external_patch_geometry_valid(
|
||||
d.memory, d.segment, d.shift, d.from, d.to,
|
||||
MOTA_NRF52_APP_END - app_base, fi.image_len, m.image_size)) {
|
||||
app_ceiling - app_base, fi.image_len, m.image_size)) {
|
||||
strcpy(msg, "invalid in-place patch geometry");
|
||||
return false;
|
||||
}
|
||||
@@ -717,6 +749,148 @@ bool ota_apply_mota_nrf52(OtaStoreQspiNrf52& store, const SignerAllowlist& allow
|
||||
ApplyState& st, char* msg) {
|
||||
return ota_apply_mota_nrf52_external(store, allow, OTA_BL_STORAGE_QSPI, "QSPI", st, msg);
|
||||
}
|
||||
|
||||
#if defined(OTA_QSPI_BOOTLOADER_UPDATE)
|
||||
static bool qspi_bootloader_crc_ok(OtaStoreQspiNrf52& store, uint32_t payload_off,
|
||||
const OtaBootloaderIdentity& identity) {
|
||||
uint8_t buf[512];
|
||||
uint32_t crc = UINT32_MAX;
|
||||
for (uint32_t off = 0; off < OTA_BOOT_IMAGE_SIZE; off += sizeof(buf)) {
|
||||
uint32_t len = OTA_BOOT_IMAGE_SIZE - off;
|
||||
if (len > sizeof(buf)) len = sizeof(buf);
|
||||
if (!store.read(payload_off + off, buf, len)) return false;
|
||||
for (uint32_t i = 0; i < len; i++) {
|
||||
const uint32_t pos = off + i;
|
||||
const uint8_t value = pos >= identity.manifest_offset + 40u &&
|
||||
pos < identity.manifest_offset + 44u ? 0 : buf[i];
|
||||
crc = ota_boot_crc32_update(crc, value);
|
||||
}
|
||||
}
|
||||
return ~crc == identity.crc32;
|
||||
}
|
||||
|
||||
static bool qspi_bootloader_image_metadata(OtaStoreQspiNrf52& store, uint32_t payload_off,
|
||||
const OtaBootloaderIdentity& installed,
|
||||
OtaBootloaderIdentity& candidate,
|
||||
OtaBootloaderCapsMarker& caps) {
|
||||
// Keep enough overlap to catch a 44-byte manifest or 16-byte capability marker split across a
|
||||
// window. Every aligned manifest candidate is considered; a magic string in a literal pool is not
|
||||
// a terminal failure.
|
||||
static const uint32_t STEP = 512;
|
||||
uint8_t buf[STEP + OTA_BOOT_MANIFEST_SIZE - 1];
|
||||
uint8_t valid_identities = 0;
|
||||
for (uint32_t base = 0; base < OTA_BOOT_IMAGE_SIZE; base += STEP) {
|
||||
uint32_t len = OTA_BOOT_IMAGE_SIZE - base;
|
||||
if (len > sizeof(buf)) len = sizeof(buf);
|
||||
if (!store.read(payload_off + base, buf, len)) return false;
|
||||
const uint32_t starts = (OTA_BOOT_IMAGE_SIZE - base < STEP)
|
||||
? OTA_BOOT_IMAGE_SIZE - base : STEP;
|
||||
for (uint32_t local = 0; local < starts; local++) {
|
||||
const uint32_t absolute = base + local;
|
||||
if (!caps.present && (absolute & 3u) == 0 && local + 16u <= len) {
|
||||
OtaBootloaderCapsMarker parsed;
|
||||
if (ota_bootloader_caps_marker_parse(buf + local, parsed) &&
|
||||
parsed.apply_abi >= MOTA_BOOT_FORMAT_VER &&
|
||||
(parsed.codec_mask & (1u << CODEC_FULL)) != 0 &&
|
||||
(parsed.storage_flags & (OTA_BL_STORAGE_QSPI | OTA_BL_STORAGE_BOOT_UPDATE)) ==
|
||||
(OTA_BL_STORAGE_QSPI | OTA_BL_STORAGE_BOOT_UPDATE)) {
|
||||
caps = parsed;
|
||||
}
|
||||
}
|
||||
if ((absolute & 3u) != 0 || local + OTA_BOOT_MANIFEST_SIZE > len) continue;
|
||||
OtaBootloaderIdentity parsed;
|
||||
if (!ota_bootloader_manifest_parse(buf + local, absolute, parsed) ||
|
||||
parsed.board_id != installed.board_id ||
|
||||
memcmp(parsed.device_name, installed.device_name, OTA_BOOT_DEVICE_NAME_SIZE) != 0) continue;
|
||||
if (qspi_bootloader_crc_ok(store, payload_off, parsed)) {
|
||||
parsed.crc_ok = true;
|
||||
if (!ota_bootloader_identity_add_unique(parsed, candidate, valid_identities)) return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
return valid_identities == 1u && candidate.present && candidate.crc_ok && caps.present;
|
||||
}
|
||||
|
||||
bool ota_prepare_bootloader_update_nrf52(OtaStoreQspiNrf52& store,
|
||||
const SignerAllowlist& allow,
|
||||
const OtaBootloaderIdentity& installed,
|
||||
const uint8_t actual_mid[4],
|
||||
const uint8_t operator_mid[4],
|
||||
const uint8_t operator_hash8[8],
|
||||
ApplyState& st, char* msg) {
|
||||
static const size_t CAP = 96;
|
||||
st = ApplyState();
|
||||
const uint32_t total = store.staged_size();
|
||||
uint8_t hdr[8], manifest[MOTA_MFL];
|
||||
if (total < 8u + MOTA_MFL + 5u || !store.read(0, hdr, sizeof(hdr)) ||
|
||||
memcmp(hdr, MOTA_MAGIC, sizeof(MOTA_MAGIC)) != 0 || rd_u32le(hdr + 4) != total ||
|
||||
!store.read(8, manifest, sizeof(manifest))) {
|
||||
strcpy(msg, "QSPI bootloader container parse failed"); return false;
|
||||
}
|
||||
MotaManifest m;
|
||||
if (!mota_parse_manifest(manifest, sizeof(manifest), m)) {
|
||||
strcpy(msg, "not a valid v3 bootloader package"); return false;
|
||||
}
|
||||
switch (ota_bootloader_confirmation_gate(m, installed, actual_mid, operator_mid,
|
||||
operator_hash8)) {
|
||||
case OTA_BOOT_CONFIRM_OK: break;
|
||||
case OTA_BOOT_CONFIRM_NOT_BOOT_PACKAGE: strcpy(msg, "not a bootloader package"); return false;
|
||||
case OTA_BOOT_CONFIRM_GEOMETRY: strcpy(msg, "bootloader package geometry mismatch"); return false;
|
||||
case OTA_BOOT_CONFIRM_LOCAL_IDENTITY: strcpy(msg, "installed bootloader identity is invalid"); return false;
|
||||
case OTA_BOOT_CONFIRM_TARGET: strcpy(msg, "bootloader board ID mismatch"); return false;
|
||||
case OTA_BOOT_CONFIRM_HW_ID: strcpy(msg, "bootloader signed hw_id mismatch"); return false;
|
||||
case OTA_BOOT_CONFIRM_MID: strcpy(msg, "bootloader MID confirmation mismatch"); return false;
|
||||
case OTA_BOOT_CONFIRM_IMAGE_HASH: strcpy(msg, "bootloader image-hash confirmation mismatch"); return false;
|
||||
}
|
||||
|
||||
const OtaBlCaps current_caps = ota_bootloader_caps();
|
||||
if (!current_caps.present || current_caps.apply_abi < MOTA_BOOT_FORMAT_VER ||
|
||||
(current_caps.codec_mask & (1u << CODEC_FULL)) == 0 ||
|
||||
(current_caps.storage_flags & (OTA_BL_STORAGE_QSPI | OTA_BL_STORAGE_BOOT_UPDATE)) !=
|
||||
(OTA_BL_STORAGE_QSPI | OTA_BL_STORAGE_BOOT_UPDATE)) {
|
||||
strcpy(msg, "installed bootloader cannot safely self-update from QSPI"); return false;
|
||||
}
|
||||
|
||||
const uint64_t payload_off64 = 8u + MOTA_MFL + (uint64_t)m.block_count * 4u;
|
||||
if (payload_off64 > UINT32_MAX || payload_off64 + OTA_BOOT_IMAGE_SIZE + 5u != total) {
|
||||
strcpy(msg, "bootloader container layout mismatch"); return false;
|
||||
}
|
||||
const uint32_t payload_off = (uint32_t)payload_off64;
|
||||
|
||||
VerifyResult vr = ota_verify(static_cast<const OtaStore&>(store), allow);
|
||||
st.manifest_ok = vr.parsed;
|
||||
st.sig_ok = vr.sig_ok;
|
||||
st.trusted = vr.trusted;
|
||||
st.image_size = m.image_size;
|
||||
memcpy(st.image_hash, m.image_hash, sizeof(st.image_hash));
|
||||
if (!vr.auto_appliable()) {
|
||||
if (!vr.root_ok || !vr.payload_ok || !vr.image_ok)
|
||||
strcpy(msg, "bootloader payload hash mismatch");
|
||||
else if (!vr.sig_ok)
|
||||
strcpy(msg, "bootloader package signature invalid");
|
||||
else
|
||||
strcpy(msg, "bootloader signer is not in the trusted allowlist");
|
||||
return false;
|
||||
}
|
||||
|
||||
uint8_t vectors[8];
|
||||
if (!store.read(payload_off, vectors, sizeof(vectors)) || !ota_bootloader_vector_sane(vectors)) {
|
||||
strcpy(msg, "bootloader vector table is invalid"); return false;
|
||||
}
|
||||
OtaBootloaderIdentity candidate;
|
||||
OtaBootloaderCapsMarker candidate_caps;
|
||||
if (!qspi_bootloader_image_metadata(store, payload_off, installed, candidate, candidate_caps) ||
|
||||
!ota_bootloader_identity_matches(installed, candidate)) {
|
||||
strcpy(msg, "candidate bootloader identity/capability/CRC mismatch"); return false;
|
||||
}
|
||||
st.slot_ok = true;
|
||||
if (!store.approve_for_bootloader()) {
|
||||
snprintf(msg, CAP, "QSPI bootloader handoff failed: %s", store.last_error()); return false;
|
||||
}
|
||||
strcpy(msg, "trusted bootloader verified and armed; rebooting after this reply");
|
||||
return true;
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#else // native / other platforms
|
||||
@@ -732,6 +906,10 @@ bool ota_rescue_mota_nrf52(const uint8_t*, uint32_t, const SignerAllowlist&, con
|
||||
st = ApplyState(); strcpy(msg, "unsupported"); return false;
|
||||
}
|
||||
void ota_reboot_to_apply() {}
|
||||
void ota_reboot_to_bootloader_update() {}
|
||||
bool ota_installed_bootloader_identity(OtaBootloaderIdentity& out) {
|
||||
out = OtaBootloaderIdentity(); return false;
|
||||
}
|
||||
uint8_t ota_bootloader_last_rc() { return 0; }
|
||||
|
||||
#endif
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
#include <stddef.h>
|
||||
#include <string.h>
|
||||
#include "SignerAllowlist.h"
|
||||
#include "OtaBootloaderUpdate.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
|
||||
@@ -90,6 +91,15 @@ bool ota_apply_mota_nrf52(OtaStoreSdNrf52& store,
|
||||
class OtaStoreQspiNrf52;
|
||||
bool ota_apply_mota_nrf52(OtaStoreQspiNrf52& store,
|
||||
const SignerAllowlist& allow, ApplyState& st, char* msg);
|
||||
#if defined(OTA_QSPI_BOOTLOADER_UPDATE)
|
||||
bool ota_prepare_bootloader_update_nrf52(OtaStoreQspiNrf52& store,
|
||||
const SignerAllowlist& allow,
|
||||
const OtaBootloaderIdentity& installed,
|
||||
const uint8_t actual_mid[4],
|
||||
const uint8_t operator_mid[4],
|
||||
const uint8_t operator_hash8[8],
|
||||
ApplyState& st, char* msg);
|
||||
#endif
|
||||
#endif
|
||||
|
||||
// Commit the (already approved/armed) update and reboot into it - does NOT return. Call this only after
|
||||
@@ -98,10 +108,17 @@ bool ota_apply_mota_nrf52(OtaStoreQspiNrf52& store,
|
||||
// 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();
|
||||
void ota_reboot_to_bootloader_update();
|
||||
bool ota_installed_bootloader_identity(OtaBootloaderIdentity& out);
|
||||
|
||||
inline uint8_t ota_nrf52_boot_result_or_zero(uint8_t value) {
|
||||
return ((value >= 0x90u && value <= 0x9Fu) ||
|
||||
(value >= 0xB0u && value <= 0xBCu)) ? value : 0u;
|
||||
(value >= 0xB0u && value <= 0xBCu) ||
|
||||
(value >= 0xC0u && value <= 0xCFu)) ? value : 0u;
|
||||
}
|
||||
|
||||
inline bool ota_nrf52_boot_update_result(uint8_t value) {
|
||||
return value >= 0xC0u && value <= 0xCFu;
|
||||
}
|
||||
|
||||
// DIAGNOSTIC (nRF52): the bootloader stashes its last in-place-apply bail/progress code in GPREGRET2 (see
|
||||
|
||||
+51
-10
@@ -1,5 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
|
||||
@@ -31,23 +32,63 @@ struct OtaBlCaps {
|
||||
static const uint8_t OTA_BL_STORAGE_SD = 0x01;
|
||||
static const uint8_t OTA_BL_STORAGE_STAGE_CEILING = 0x02;
|
||||
static const uint8_t OTA_BL_STORAGE_QSPI = 0x04;
|
||||
static const uint8_t OTA_BL_STORAGE_BOOT_UPDATE = 0x08;
|
||||
|
||||
// Prefer a continuity-capable marker over a numerically newer legacy-looking candidate. Bootloader
|
||||
// images contain the marker magic in code/literal data as well as in the real structure, so the scan
|
||||
// must not let a later plausible literal shadow the privileged marker merely because its following
|
||||
// bytes decode as a larger ABI.
|
||||
inline bool ota_bl_caps_prefer(const OtaBlCaps& current, uint16_t candidate_abi,
|
||||
uint8_t candidate_storage, bool require_boot_update) {
|
||||
if (!current.present) return true;
|
||||
if (require_boot_update) {
|
||||
const bool candidate_boot = (candidate_storage & OTA_BL_STORAGE_BOOT_UPDATE) != 0;
|
||||
const bool current_boot = (current.storage_flags & OTA_BL_STORAGE_BOOT_UPDATE) != 0;
|
||||
if (candidate_boot != current_boot) return candidate_boot;
|
||||
}
|
||||
return candidate_abi > current.apply_abi;
|
||||
}
|
||||
|
||||
// Scan a region whose offset zero is word-aligned. The marker structure contains
|
||||
// uint16_t fields and OTAFIX emits it aligned(4); accepting a byte-shifted magic
|
||||
// occurrence would let instruction/literal bytes masquerade as capabilities.
|
||||
inline OtaBlCaps ota_bl_caps_scan_aligned(const uint8_t* bytes, size_t len,
|
||||
bool require_boot_update) {
|
||||
OtaBlCaps c;
|
||||
if (!bytes) return c;
|
||||
for (size_t off = 0; off + 16u <= len; off += 4u) {
|
||||
const uint8_t* p = bytes + off;
|
||||
if (p[0] != OTA_BL_MAGIC[0] || memcmp(p, OTA_BL_MAGIC, 8) != 0) continue;
|
||||
const uint16_t abi = (uint16_t)(p[8] | ((uint16_t)p[9] << 8));
|
||||
const uint16_t codecs = (uint16_t)(p[10] | ((uint16_t)p[11] << 8));
|
||||
const uint8_t storage = p[12];
|
||||
if (abi == 0 || abi == 0xFFFFu || codecs == 0 || (storage & ~0x0Fu) != 0 ||
|
||||
p[13] != 0 || p[14] != 0 || p[15] != 0) continue;
|
||||
if (ota_bl_caps_prefer(c, abi, storage, require_boot_update)) {
|
||||
c.present = true;
|
||||
c.apply_abi = abi;
|
||||
c.codec_mask = codecs;
|
||||
c.storage_flags = storage;
|
||||
}
|
||||
}
|
||||
return c;
|
||||
}
|
||||
|
||||
// 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));
|
||||
c.storage_flags = p[12];
|
||||
break;
|
||||
}
|
||||
return ota_bl_caps_scan_aligned(lo, (size_t)(hi - lo),
|
||||
#if defined(OTA_QSPI_BOOTLOADER_UPDATE)
|
||||
true
|
||||
#else
|
||||
false
|
||||
#endif
|
||||
);
|
||||
#else
|
||||
return OtaBlCaps();
|
||||
#endif
|
||||
return c;
|
||||
}
|
||||
|
||||
// True if this device's bootloader can apply a .mota of the given format_ver + codec_id.
|
||||
|
||||
@@ -0,0 +1,242 @@
|
||||
#pragma once
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "MotaContainer.h"
|
||||
|
||||
#if defined(OTA_QSPI_BOOTLOADER_UPDATE)
|
||||
#if !defined(NRF52_PLATFORM) || !defined(OTA_QSPI_STORE)
|
||||
#error "OTA_QSPI_BOOTLOADER_UPDATE requires nRF52 raw-QSPI staging"
|
||||
#endif
|
||||
#if !defined(XIAO_NRF52) && !defined(IKOKA_NRF52)
|
||||
#error "OTA_QSPI_BOOTLOADER_UPDATE is restricted to XIAO nRF52840 module builds"
|
||||
#endif
|
||||
#if defined(QSPIFLASH) || defined(OTA_SD_STORE)
|
||||
#error "bootloader-update QSPI cannot share a filesystem or SD staging configuration"
|
||||
#endif
|
||||
#endif
|
||||
|
||||
namespace mesh {
|
||||
namespace ota {
|
||||
|
||||
// Mirror of OTAFIX src/usb/uf2/bootloader_image.h. This manifest is embedded in the complete,
|
||||
// padded bootloader region and is covered both by its own CRC32 and the signed .mota image_hash.
|
||||
static const uint32_t OTA_BOOT_MANIFEST_MAGIC0 = 0x464D4C42UL; // bytes "BLMF"
|
||||
static const uint32_t OTA_BOOT_MANIFEST_MAGIC1 = 0x31435243UL; // bytes "CRC1"
|
||||
static const uint16_t OTA_BOOT_MANIFEST_VERSION = 1;
|
||||
static const uint16_t OTA_BOOT_MANIFEST_SIZE = 44;
|
||||
static const uint8_t OTA_BOOT_DEVICE_NAME_SIZE = 16;
|
||||
static const uint32_t OTA_BOOT_IMAGE_START = 0x000F4000UL;
|
||||
static const uint32_t OTA_BOOT_IMAGE_SIZE = 0x0000A000UL; // F4000..FE000, padded to exactly 40 KiB
|
||||
static const uint32_t OTA_BOOT_SCRATCH_START = 0x000E0000UL;
|
||||
static const uint32_t OTA_BOOT_SCRATCH_END = 0x000EA000UL;
|
||||
static const uint32_t OTA_NRF52840_RAM_START = 0x20000000UL;
|
||||
static const uint32_t OTA_NRF52840_RAM_END = 0x20040000UL;
|
||||
|
||||
// Only the two official XIAO nRF52840 OTAFIX identities are eligible. Runtime matching against the
|
||||
// installed, CRC-valid manifest distinguishes the base and Sense modules even for carrier-board aliases.
|
||||
static const uint32_t OTA_XIAO_BOARD_ID_BASE = 0x28860044UL;
|
||||
static const uint32_t OTA_XIAO_BOARD_ID_SENSE = 0x28860045UL;
|
||||
static const uint8_t OTA_XIAO_BOOT_DEVICE_NAME[OTA_BOOT_DEVICE_NAME_SIZE] = {
|
||||
'X','I','A','O','_','D','F','U', 0,0,0,0,0,0,0,0
|
||||
};
|
||||
|
||||
struct OtaBootloaderIdentity {
|
||||
bool present = false;
|
||||
bool crc_ok = false;
|
||||
uint32_t manifest_offset = 0;
|
||||
uint32_t image_start = 0;
|
||||
uint32_t image_size = 0;
|
||||
uint32_t board_id = 0;
|
||||
char device_name[OTA_BOOT_DEVICE_NAME_SIZE + 1] = {0};
|
||||
uint32_t crc32 = 0;
|
||||
};
|
||||
|
||||
// Accumulate a stream of already CRC-validated candidates without silently
|
||||
// choosing between two privileged identities. Both the memory-mapped and QSPI
|
||||
// scanners use this helper so their duplicate policy cannot drift.
|
||||
inline bool ota_bootloader_identity_add_unique(const OtaBootloaderIdentity& candidate,
|
||||
OtaBootloaderIdentity& selected,
|
||||
uint8_t& valid_count) {
|
||||
if (!candidate.present || !candidate.crc_ok) return true;
|
||||
if (valid_count != 0u) {
|
||||
selected = OtaBootloaderIdentity();
|
||||
return false;
|
||||
}
|
||||
selected = candidate;
|
||||
valid_count = 1u;
|
||||
return true;
|
||||
}
|
||||
|
||||
inline uint16_t ota_boot_rd16(const uint8_t* p) {
|
||||
return (uint16_t)(p[0] | ((uint16_t)p[1] << 8));
|
||||
}
|
||||
|
||||
inline uint32_t ota_boot_rd32(const uint8_t* p) {
|
||||
return (uint32_t)p[0] | ((uint32_t)p[1] << 8) | ((uint32_t)p[2] << 16) |
|
||||
((uint32_t)p[3] << 24);
|
||||
}
|
||||
|
||||
inline bool ota_xiao_bootloader_board_id(uint32_t board_id) {
|
||||
return board_id == OTA_XIAO_BOARD_ID_BASE || board_id == OTA_XIAO_BOARD_ID_SENSE;
|
||||
}
|
||||
|
||||
// Canonical signed .mota hw_id. It is intentionally distinct from application hw_id values and is
|
||||
// derived solely from the exact embedded board ID.
|
||||
inline bool ota_xiao_bootloader_hw_id(uint32_t board_id, uint8_t out[32]) {
|
||||
if (!out || !ota_xiao_bootloader_board_id(board_id)) return false;
|
||||
memset(out, 0, 32);
|
||||
const char* value = board_id == OTA_XIAO_BOARD_ID_BASE
|
||||
? "XIAO_BL_28860044" : "XIAO_BL_28860045";
|
||||
memcpy(out, value, strlen(value));
|
||||
return true;
|
||||
}
|
||||
|
||||
inline uint32_t ota_boot_crc32_update(uint32_t crc, uint8_t value) {
|
||||
crc ^= value;
|
||||
for (uint8_t bit = 0; bit < 8; bit++)
|
||||
crc = (crc >> 1) ^ (0xEDB88320UL & (uint32_t)-(int32_t)(crc & 1U));
|
||||
return crc;
|
||||
}
|
||||
|
||||
inline uint32_t ota_boot_image_crc32(const uint8_t* image, size_t image_size,
|
||||
size_t crc_offset) {
|
||||
if (!image || crc_offset > image_size || image_size - crc_offset < sizeof(uint32_t)) return 0;
|
||||
uint32_t crc = UINT32_MAX;
|
||||
for (size_t i = 0; i < image_size; i++) {
|
||||
const uint8_t value = (i >= crc_offset && i < crc_offset + sizeof(uint32_t)) ? 0 : image[i];
|
||||
crc = ota_boot_crc32_update(crc, value);
|
||||
}
|
||||
return ~crc;
|
||||
}
|
||||
|
||||
// Parse one candidate header. The caller independently validates the complete-region CRC so a magic
|
||||
// string in a literal pool cannot establish an identity.
|
||||
inline bool ota_bootloader_manifest_parse(const uint8_t* raw, uint32_t raw_offset,
|
||||
OtaBootloaderIdentity& out) {
|
||||
if (!raw || ota_boot_rd32(raw) != OTA_BOOT_MANIFEST_MAGIC0 ||
|
||||
ota_boot_rd32(raw + 4) != OTA_BOOT_MANIFEST_MAGIC1 ||
|
||||
ota_boot_rd16(raw + 8) != OTA_BOOT_MANIFEST_VERSION ||
|
||||
ota_boot_rd16(raw + 10) != OTA_BOOT_MANIFEST_SIZE ||
|
||||
ota_boot_rd32(raw + 12) != OTA_BOOT_IMAGE_START ||
|
||||
ota_boot_rd32(raw + 16) != OTA_BOOT_IMAGE_SIZE ||
|
||||
!ota_xiao_bootloader_board_id(ota_boot_rd32(raw + 20)) ||
|
||||
memcmp(raw + 24, OTA_XIAO_BOOT_DEVICE_NAME, OTA_BOOT_DEVICE_NAME_SIZE) != 0) return false;
|
||||
|
||||
out = OtaBootloaderIdentity();
|
||||
out.present = true;
|
||||
out.manifest_offset = raw_offset;
|
||||
out.image_start = ota_boot_rd32(raw + 12);
|
||||
out.image_size = ota_boot_rd32(raw + 16);
|
||||
out.board_id = ota_boot_rd32(raw + 20);
|
||||
memcpy(out.device_name, raw + 24, OTA_BOOT_DEVICE_NAME_SIZE);
|
||||
out.device_name[OTA_BOOT_DEVICE_NAME_SIZE] = 0;
|
||||
out.crc32 = ota_boot_rd32(raw + 40);
|
||||
return true;
|
||||
}
|
||||
|
||||
// Validate an installed memory-mapped bootloader and recover its exact identity. Scan on word
|
||||
// boundaries to mirror OTAFIX's self-update validator.
|
||||
inline bool ota_bootloader_identity_from_image(const uint8_t* image, size_t image_size,
|
||||
OtaBootloaderIdentity& out) {
|
||||
out = OtaBootloaderIdentity();
|
||||
if (!image || image_size != OTA_BOOT_IMAGE_SIZE) return false;
|
||||
uint8_t valid_count = 0;
|
||||
for (size_t offset = 0; offset + OTA_BOOT_MANIFEST_SIZE <= image_size; offset += 4) {
|
||||
OtaBootloaderIdentity candidate;
|
||||
if (!ota_bootloader_manifest_parse(image + offset, (uint32_t)offset, candidate)) continue;
|
||||
candidate.crc_ok = ota_boot_image_crc32(image, image_size, offset + 40) == candidate.crc32;
|
||||
if (!candidate.crc_ok) continue;
|
||||
if (!ota_bootloader_identity_add_unique(candidate, out, valid_count)) return false;
|
||||
}
|
||||
return valid_count == 1u;
|
||||
}
|
||||
|
||||
inline bool ota_bootloader_identity_matches(const OtaBootloaderIdentity& a,
|
||||
const OtaBootloaderIdentity& b) {
|
||||
return a.present && a.crc_ok && b.present && b.crc_ok &&
|
||||
a.image_start == b.image_start && a.image_size == b.image_size &&
|
||||
a.board_id == b.board_id &&
|
||||
memcmp(a.device_name, b.device_name, OTA_BOOT_DEVICE_NAME_SIZE) == 0;
|
||||
}
|
||||
|
||||
// Cheap first-line rejection of a malformed raw bootloader region. OTAFIX repeats this check before
|
||||
// touching internal flash. MSP may point one byte beyond RAM; the reset vector must be Thumb code in
|
||||
// the exact bootloader image range.
|
||||
inline bool ota_bootloader_vector_sane(const uint8_t vectors[8]) {
|
||||
if (!vectors) return false;
|
||||
const uint32_t sp = ota_boot_rd32(vectors);
|
||||
const uint32_t reset = ota_boot_rd32(vectors + 4);
|
||||
const uint32_t entry = reset & ~1UL;
|
||||
return (sp & 7u) == 0 && sp >= OTA_NRF52840_RAM_START && sp <= OTA_NRF52840_RAM_END &&
|
||||
(reset & 1u) != 0 && entry >= OTA_BOOT_IMAGE_START &&
|
||||
entry < OTA_BOOT_IMAGE_START + OTA_BOOT_IMAGE_SIZE;
|
||||
}
|
||||
|
||||
struct OtaBootloaderCapsMarker {
|
||||
bool present = false;
|
||||
uint16_t apply_abi = 0;
|
||||
uint16_t codec_mask = 0;
|
||||
uint8_t storage_flags = 0;
|
||||
};
|
||||
|
||||
inline bool ota_bootloader_caps_marker_parse(const uint8_t raw[16],
|
||||
OtaBootloaderCapsMarker& out) {
|
||||
static const uint8_t magic[8] = {'M','O','T','A','B','L','D','R'};
|
||||
if (!raw || memcmp(raw, magic, sizeof(magic)) != 0 ||
|
||||
raw[8] == 0 || (raw[8] == 0xFF && raw[9] == 0xFF) ||
|
||||
(raw[10] == 0 && raw[11] == 0) || (raw[12] & ~0x0Fu) != 0 ||
|
||||
raw[13] != 0 || raw[14] != 0 || raw[15] != 0) return false;
|
||||
out.present = true;
|
||||
out.apply_abi = ota_boot_rd16(raw + 8);
|
||||
out.codec_mask = ota_boot_rd16(raw + 10);
|
||||
out.storage_flags = raw[12];
|
||||
return true;
|
||||
}
|
||||
|
||||
enum OtaBootloaderConfirmGate : uint8_t {
|
||||
OTA_BOOT_CONFIRM_OK = 0,
|
||||
OTA_BOOT_CONFIRM_NOT_BOOT_PACKAGE,
|
||||
OTA_BOOT_CONFIRM_GEOMETRY,
|
||||
OTA_BOOT_CONFIRM_LOCAL_IDENTITY,
|
||||
OTA_BOOT_CONFIRM_TARGET,
|
||||
OTA_BOOT_CONFIRM_HW_ID,
|
||||
OTA_BOOT_CONFIRM_MID,
|
||||
OTA_BOOT_CONFIRM_IMAGE_HASH,
|
||||
};
|
||||
|
||||
// Pure explicit-confirmation gate used by the device command and native tests. Cryptographic and
|
||||
// candidate-image checks run immediately after this structural/typed gate.
|
||||
inline OtaBootloaderConfirmGate ota_bootloader_confirmation_gate(
|
||||
const MotaManifest& m, const OtaBootloaderIdentity& installed,
|
||||
const uint8_t actual_mid[4], const uint8_t operator_mid[4],
|
||||
const uint8_t operator_hash8[8]) {
|
||||
if (m.format_ver != MOTA_BOOT_FORMAT_VER ||
|
||||
m.flags != (MFLAG_FULL | MFLAG_SIGNED | MFLAG_BOOTLOADER))
|
||||
return OTA_BOOT_CONFIRM_NOT_BOOT_PACKAGE;
|
||||
static const uint8_t zero_base[8] = {0};
|
||||
if (m.codec_id != CODEC_FULL || m.image_size != OTA_BOOT_IMAGE_SIZE ||
|
||||
m.payload_size != OTA_BOOT_IMAGE_SIZE || m.block_size_log2 != 10 ||
|
||||
m.block_count != OTA_BOOT_IMAGE_SIZE / 1024u || !m.base_hash ||
|
||||
memcmp(m.base_hash, zero_base, sizeof(zero_base)) != 0)
|
||||
return OTA_BOOT_CONFIRM_GEOMETRY;
|
||||
if (!installed.present || !installed.crc_ok ||
|
||||
!ota_xiao_bootloader_board_id(installed.board_id))
|
||||
return OTA_BOOT_CONFIRM_LOCAL_IDENTITY;
|
||||
if (m.target_id != installed.board_id) return OTA_BOOT_CONFIRM_TARGET;
|
||||
uint8_t expected_hw[32];
|
||||
if (!ota_xiao_bootloader_hw_id(installed.board_id, expected_hw) || !m.hw_id ||
|
||||
memcmp(m.hw_id, expected_hw, sizeof(expected_hw)) != 0)
|
||||
return OTA_BOOT_CONFIRM_HW_ID;
|
||||
if (!actual_mid || !operator_mid || memcmp(actual_mid, operator_mid, 4) != 0 ||
|
||||
!m.merkle_root || memcmp(m.merkle_root, actual_mid, 4) != 0)
|
||||
return OTA_BOOT_CONFIRM_MID;
|
||||
if (!operator_hash8 || !m.image_hash || memcmp(m.image_hash, operator_hash8, 8) != 0)
|
||||
return OTA_BOOT_CONFIRM_IMAGE_HASH;
|
||||
return OTA_BOOT_CONFIRM_OK;
|
||||
}
|
||||
|
||||
} // namespace ota
|
||||
} // namespace mesh
|
||||
+133
-11
@@ -51,8 +51,30 @@ static char fstate_char(OtaManager::FetchState s) {
|
||||
}
|
||||
}
|
||||
|
||||
// For users, the only distinction that matters is full image vs. delta (which delta codec is internal).
|
||||
static const char* codec_kind(uint8_t c) { return c == CODEC_FULL ? "full" : "delta"; }
|
||||
// For users, the only distinction that matters is application full/delta vs the separately gated
|
||||
// bootloader package. Which application delta codec is an implementation detail.
|
||||
static const char* codec_kind(uint8_t flags, uint8_t c) {
|
||||
return (flags & MFLAG_BOOTLOADER) ? "bootloader" : c == CODEC_FULL ? "full" : "delta";
|
||||
}
|
||||
|
||||
static bool parse_hex_exact(const char* text, uint8_t* out, size_t bytes) {
|
||||
if (!text || strlen(text) != bytes * 2u) return false;
|
||||
for (size_t i = 0; i < bytes * 2u; i++) {
|
||||
const char c = text[i];
|
||||
if (!((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F')))
|
||||
return false;
|
||||
}
|
||||
return mesh::Utils::fromHex(out, bytes, text);
|
||||
}
|
||||
|
||||
static bool staged_manifest(OtaContext& c, MotaManifest& m) {
|
||||
uint8_t hdr[8], manifest[MOTA_MFL];
|
||||
const uint32_t total = c.fetch_store.staged_size();
|
||||
return total >= 8u + MOTA_MFL + 5u && c.fetch_store.read(0, hdr, sizeof(hdr)) &&
|
||||
memcmp(hdr, MOTA_MAGIC, sizeof(MOTA_MAGIC)) == 0 && rd_u32le(hdr + 4) == total &&
|
||||
c.fetch_store.read(8, manifest, sizeof(manifest)) &&
|
||||
mota_parse_manifest(manifest, sizeof(manifest), m);
|
||||
}
|
||||
|
||||
// A plain-language word for the fetch state (shown in `ota status`).
|
||||
static const char* state_word(OtaManager::FetchState s) {
|
||||
@@ -152,9 +174,15 @@ bool handle_ota_command(const char* command, char* reply, mesh::MainBoard& board
|
||||
"OTA: status | stats | ls | get <id> flash [rescue] | install | rescue install <hash16> | "
|
||||
"cancel | announce | self | folder | config | key");
|
||||
#elif defined(NRF52_PLATFORM) && defined(OTA_QSPI_STORE)
|
||||
#if defined(OTA_QSPI_BOOTLOADER_UPDATE)
|
||||
strcpy(reply,
|
||||
"OTA: status | stats | ls | get <id> flash | install | bootloader | cancel | announce | self | "
|
||||
"qspi | folder | config | key");
|
||||
#else
|
||||
strcpy(reply,
|
||||
"OTA: status | stats | ls | get <id> flash | install | cancel | announce | self | qspi | "
|
||||
"folder | config | key");
|
||||
#endif
|
||||
#else
|
||||
snprintf(reply, 160,
|
||||
"OTA: status | stats | ls | get <id> flash | install | cancel | announce | self | folder | "
|
||||
@@ -186,7 +214,8 @@ bool handle_ota_command(const char* command, char* reply, mesh::MainBoard& board
|
||||
snprintf(dl, sizeof dl, "download: failed (%s) %u/%u id=%s",
|
||||
fetch_error_word(c.manager.fetchError()), have, tot, midhx);
|
||||
else
|
||||
snprintf(dl, sizeof dl, "download: %s %u/%u (%u%%) id=%s %us",
|
||||
snprintf(dl, sizeof dl, "%sdownload: %s %u/%u (%u%%) id=%s %us",
|
||||
c.manager.fetched_is_bootloader() ? "bootloader " : "",
|
||||
state_word(fs), have, tot, pct, midhx, age);
|
||||
}
|
||||
const char* hw = (c.hw_id[0]) ? c.hw_id : "?";
|
||||
@@ -208,8 +237,13 @@ bool handle_ota_command(const char* command, char* reply, mesh::MainBoard& board
|
||||
#else
|
||||
const char* bl_state = bl.present ? "apply" : "NONE";
|
||||
#endif
|
||||
if (n < 146) n += snprintf(reply + n, 160 - n, " | bl:%s blrc:%02X",
|
||||
bl_state, ota_bootloader_last_rc());
|
||||
const uint8_t last_rc = ota_bootloader_last_rc();
|
||||
if (n < 146) {
|
||||
if (ota_nrf52_boot_update_result(last_rc))
|
||||
n += snprintf(reply + n, 160 - n, " | bl:%s blup:%02X", bl_state, last_rc);
|
||||
else
|
||||
n += snprintf(reply + n, 160 - n, " | bl:%s blrc:%02X", bl_state, last_rc);
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
|
||||
@@ -304,7 +338,20 @@ bool handle_ota_command(const char* command, char* reply, mesh::MainBoard& board
|
||||
char hwbuf[16];
|
||||
const char* fit;
|
||||
const char* env = ota_target_env_name(h->target_id);
|
||||
if (myt && h->target_id == myt) {
|
||||
const bool boot_package = (h->flags & MFLAG_BOOTLOADER) != 0;
|
||||
if (boot_package) {
|
||||
bool installable = false;
|
||||
#if defined(NRF52_PLATFORM) && defined(OTA_QSPI_STORE) && defined(OTA_QSPI_BOOTLOADER_UPDATE)
|
||||
const OtaBootloaderIdentity& bid = c.bootloaderIdentity();
|
||||
installable = h->flags == (MFLAG_FULL | MFLAG_SIGNED | MFLAG_BOOTLOADER) &&
|
||||
h->codec == CODEC_FULL && bid.present && bid.crc_ok &&
|
||||
h->target_id == bid.board_id && list_bl.present &&
|
||||
list_bl.apply_abi >= MOTA_BOOT_FORMAT_VER &&
|
||||
(list_bl.storage_flags & (OTA_BL_STORAGE_QSPI | OTA_BL_STORAGE_BOOT_UPDATE)) ==
|
||||
(OTA_BL_STORAGE_QSPI | OTA_BL_STORAGE_BOOT_UPDATE);
|
||||
#endif
|
||||
fit = installable ? "yours" : "bootloader unsupported";
|
||||
} else if (myt && h->target_id == myt) {
|
||||
bool installable = c.manager.codecOk(h->codec);
|
||||
#if defined(NRF52_PLATFORM)
|
||||
installable = installable && list_bl.present && list_bl.apply_abi >= MOTA_FORMAT_VER
|
||||
@@ -326,7 +373,7 @@ bool handle_ota_command(const char* command, char* reply, mesh::MainBoard& board
|
||||
else { snprintf(hwbuf, sizeof hwbuf, "hw %08X", (unsigned)h->target_id); fit = hwbuf; }
|
||||
char midhx[9]; mesh::Utils::toHex(midhx, h->mid, 4);
|
||||
n += snprintf(reply + n, CAP - n, "\n %u) %s %s %s [%s] %un %us%s",
|
||||
(unsigned)(i + 1), midhx, ver, codec_kind(h->codec), fit,
|
||||
(unsigned)(i + 1), midhx, ver, codec_kind(h->flags, h->codec), fit,
|
||||
(unsigned)h->n_seeders, (unsigned)age, tag);
|
||||
shown++;
|
||||
}
|
||||
@@ -401,6 +448,8 @@ bool handle_ota_command(const char* command, char* reply, mesh::MainBoard& board
|
||||
}
|
||||
|
||||
uint8_t selmid[4]; uint32_t seltgt = sel->target_id; uint8_t selcodec = sel->codec;
|
||||
const uint8_t selflags = sel->flags;
|
||||
const bool selboot = (selflags & MFLAG_BOOTLOADER) != 0;
|
||||
memcpy(selmid, sel->mid, 4); // sel may move when catalog traffic arrives
|
||||
|
||||
#if defined(NRF52_PLATFORM) && defined(OTA_SD_STORE)
|
||||
@@ -418,10 +467,33 @@ bool handle_ota_command(const char* command, char* reply, mesh::MainBoard& board
|
||||
strcpy(reply, "ERR seeder-only build cannot stage or install firmware; use `folder`");
|
||||
return true;
|
||||
#else
|
||||
if (!c.manager.codecOk(selcodec)) {
|
||||
if (selboot) {
|
||||
#if defined(NRF52_PLATFORM) && defined(OTA_QSPI_STORE) && defined(OTA_QSPI_BOOTLOADER_UPDATE)
|
||||
const OtaBootloaderIdentity& bid = c.bootloaderIdentity();
|
||||
const OtaBlCaps& bl = c.bootloaderCaps();
|
||||
if (selflags != (MFLAG_FULL | MFLAG_SIGNED | MFLAG_BOOTLOADER) ||
|
||||
selcodec != CODEC_FULL) {
|
||||
strcpy(reply, "ERR malformed bootloader catalog row; capture it to folder for inspection");
|
||||
return true;
|
||||
}
|
||||
if (!bid.present || !bid.crc_ok || seltgt != bid.board_id) {
|
||||
strcpy(reply, "ERR bootloader package does not match this installed XIAO bootloader");
|
||||
return true;
|
||||
}
|
||||
if (!bl.present || bl.apply_abi < MOTA_BOOT_FORMAT_VER ||
|
||||
(bl.storage_flags & (OTA_BL_STORAGE_QSPI | OTA_BL_STORAGE_BOOT_UPDATE)) !=
|
||||
(OTA_BL_STORAGE_QSPI | OTA_BL_STORAGE_BOOT_UPDATE)) {
|
||||
strcpy(reply, "ERR installed bootloader lacks safe LoRa bootloader-update support");
|
||||
return true;
|
||||
}
|
||||
#else
|
||||
strcpy(reply, "ERR this build cannot install bootloader packages; use folder capture or USB DFU");
|
||||
return true;
|
||||
#endif
|
||||
} else if (!c.manager.codecOk(selcodec)) {
|
||||
snprintf(reply, 160,
|
||||
"ERR %s codec %u cannot be installed by this build; use `ota pull %s folder` to capture it",
|
||||
codec_kind(selcodec), (unsigned)selcodec, selstr);
|
||||
codec_kind(selflags, selcodec), (unsigned)selcodec, selstr);
|
||||
return true;
|
||||
}
|
||||
#if defined(NRF52_PLATFORM)
|
||||
@@ -430,9 +502,10 @@ bool handle_ota_command(const char* command, char* reply, mesh::MainBoard& board
|
||||
strcpy(reply, "ERR bootloader has no mOTA apply support; update it over USB first");
|
||||
return true;
|
||||
}
|
||||
if (bl.apply_abi < MOTA_FORMAT_VER || selcodec >= 16 || !(bl.codec_mask & (1u << selcodec))) {
|
||||
const uint8_t need_abi = selboot ? MOTA_BOOT_FORMAT_VER : MOTA_APP_FORMAT_VER;
|
||||
if (bl.apply_abi < need_abi || selcodec >= 16 || !(bl.codec_mask & (1u << selcodec))) {
|
||||
snprintf(reply, 160, "ERR bootloader cannot apply mOTA ABI %u codec %u (has abi=%u codecs=0x%x)",
|
||||
MOTA_FORMAT_VER, (unsigned)selcodec, bl.apply_abi, bl.codec_mask);
|
||||
need_abi, (unsigned)selcodec, bl.apply_abi, bl.codec_mask);
|
||||
return true;
|
||||
}
|
||||
#if defined(OTA_SD_STORE)
|
||||
@@ -498,6 +571,7 @@ bool handle_ota_command(const char* command, char* reply, mesh::MainBoard& board
|
||||
|
||||
// ---- discard the current session (e.g. a stalled old fetch) to free the slot ----
|
||||
} else if (is_cmd(a, "drop|cancel|stop", &rest)) {
|
||||
if (c.apply_pending) { strcpy(reply, "ERR update is armed; reboot is pending"); return true; }
|
||||
OtaManager::FetchState fs = c.manager.fetchState();
|
||||
char midhx[9]; strcpy(midhx, "-");
|
||||
if (fs != OtaManager::IDLE) mesh::Utils::toHex(midhx, c.manager.fetchManifestId(), 4);
|
||||
@@ -607,6 +681,54 @@ bool handle_ota_command(const char* command, char* reply, mesh::MainBoard& board
|
||||
strcpy(reply, "ERR rescue requires an internal-flash nRF52 LoRa OTA build");
|
||||
#endif
|
||||
|
||||
} else if (is_cmd(a, "bootloader|blupdate", &rest)) {
|
||||
#if defined(NRF52_PLATFORM) && defined(OTA_QSPI_STORE) && \
|
||||
defined(OTA_QSPI_BOOTLOADER_UPDATE) && !defined(OTA_SEEDER_ONLY)
|
||||
const OtaBootloaderIdentity& bid = c.bootloaderIdentity();
|
||||
const OtaBlCaps& bl = c.bootloaderCaps();
|
||||
if (*rest == 0 || strcmp(rest, "status") == 0) {
|
||||
if (!bid.present || !bid.crc_ok) {
|
||||
strcpy(reply, "Bootloader update unavailable: installed embedded manifest/CRC is invalid");
|
||||
return true;
|
||||
}
|
||||
MotaManifest staged;
|
||||
char midhx[9] = "-", hashhx[17] = "-";
|
||||
const bool ready = c.manager.fetchState() == OtaManager::COMPLETE &&
|
||||
staged_manifest(c, staged) && staged.is_bootloader();
|
||||
if (ready) {
|
||||
mesh::Utils::toHex(midhx, c.manager.fetchManifestId(), 4);
|
||||
mesh::Utils::toHex(hashhx, staged.image_hash, 8);
|
||||
}
|
||||
snprintf(reply, 160, "BL board=%08X name=%s crc=%08X abi=%u caps=%02X | staged:%s mid=%s hash=%s",
|
||||
(unsigned)bid.board_id, bid.device_name, (unsigned)bid.crc32,
|
||||
bl.apply_abi, bl.storage_flags, ready ? "ready" : "none", midhx, hashhx);
|
||||
return true;
|
||||
}
|
||||
const char* confirm = nullptr;
|
||||
if (!is_cmd(rest, "install", &confirm)) {
|
||||
strcpy(reply, "ERR usage: ota bootloader install <MID8> <HASH16>"); return true;
|
||||
}
|
||||
char midtxt[9] = {0}, hashtxt[17] = {0}, extra[2] = {0};
|
||||
if (sscanf(confirm, "%8s %16s %1s", midtxt, hashtxt, extra) != 2) {
|
||||
strcpy(reply, "ERR usage: ota bootloader install <MID8> <HASH16>"); return true;
|
||||
}
|
||||
uint8_t operator_mid[4], operator_hash8[8];
|
||||
if (!parse_hex_exact(midtxt, operator_mid, sizeof(operator_mid)) ||
|
||||
!parse_hex_exact(hashtxt, operator_hash8, sizeof(operator_hash8))) {
|
||||
strcpy(reply, "ERR MID must be 8 hex and HASH must be 16 hex characters"); return true;
|
||||
}
|
||||
if (c.apply_pending) { strcpy(reply, "ERR another update is already armed"); return true; }
|
||||
if (c.fetch_to_folder || c.manager.fetchState() != OtaManager::COMPLETE ||
|
||||
c.fetch_store.staged_size() == 0 || !c.manager.fetched_is_bootloader()) {
|
||||
strcpy(reply, "ERR no complete bootloader package in local QSPI storage"); return true;
|
||||
}
|
||||
char m2[100];
|
||||
const bool ok = c.apply_fetched_bootloader(operator_mid, operator_hash8, m2);
|
||||
snprintf(reply, 160, "%s | %s", ok ? "OK" : "ERR", m2);
|
||||
#else
|
||||
strcpy(reply, "ERR this build cannot update its bootloader over LoRa");
|
||||
#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
|
||||
|
||||
@@ -187,7 +187,14 @@ struct OtaContext {
|
||||
if (total >= 13 && fetch_store.read(0, hdr, 8) && memcmp(hdr, MOTA_MAGIC, 4) == 0) {
|
||||
uint32_t mr = total - 8; if (mr > sizeof(mb)) mr = sizeof(mb);
|
||||
MotaManifest mm;
|
||||
if (fetch_store.read(8, mb, mr) && mota_parse_manifest(mb, mr, mm) && !hwMatches(mm.hw_id)) {
|
||||
if (!fetch_store.read(8, mb, mr) || !mota_parse_manifest(mb, mr, mm)) {
|
||||
strncpy(msg, "refused: invalid staged manifest", 96); msg[95] = 0; return false;
|
||||
}
|
||||
if (mm.is_bootloader()) {
|
||||
strncpy(msg, "refused: use ota bootloader install <MID8> <HASH16>", 96);
|
||||
msg[95] = 0; return false;
|
||||
}
|
||||
if (!hwMatches(mm.hw_id)) {
|
||||
char want[33] = {0}; memcpy(want, mm.hw_id, 32);
|
||||
snprintf(msg, 96, "refused: .mota hw_id '%.32s' != this device '%s' (incompatible hardware)", want, hw_id);
|
||||
return false;
|
||||
@@ -209,16 +216,42 @@ struct OtaContext {
|
||||
#else
|
||||
ok = ota_apply_detools_mota(fetch_store.data(), fetch_store.staged_size(), allow, apply_st, msg);
|
||||
#endif
|
||||
if (ok) apply_pending = true;
|
||||
if (ok) { bootloader_apply_pending = false; apply_pending = true; }
|
||||
return ok;
|
||||
#endif
|
||||
}
|
||||
|
||||
bool apply_fetched_bootloader(const uint8_t operator_mid[4],
|
||||
const uint8_t operator_hash8[8], char* msg) {
|
||||
#if defined(NRF52_PLATFORM) && defined(OTA_QSPI_STORE) && \
|
||||
defined(OTA_QSPI_BOOTLOADER_UPDATE) && !defined(OTA_SEEDER_ONLY)
|
||||
if (fetch_to_folder) {
|
||||
strncpy(msg, "refused: bootloader package is not in local QSPI storage", 96);
|
||||
msg[95] = 0; return false;
|
||||
}
|
||||
if (manager.fetchState() != OtaManager::COMPLETE || fetch_store.staged_size() == 0) {
|
||||
strncpy(msg, "refused: no complete bootloader package fetched", 96);
|
||||
msg[95] = 0; return false;
|
||||
}
|
||||
const OtaBootloaderIdentity& installed = bootloaderIdentity();
|
||||
bool ok = ota_prepare_bootloader_update_nrf52(
|
||||
fetch_store, allow, installed, manager.fetchManifestId(), operator_mid,
|
||||
operator_hash8, apply_st, msg);
|
||||
if (ok) { bootloader_apply_pending = true; apply_pending = true; }
|
||||
return ok;
|
||||
#else
|
||||
(void)operator_mid; (void)operator_hash8;
|
||||
strncpy(msg, "refused: this build cannot update its bootloader over LoRa", 96);
|
||||
msg[95] = 0; return false;
|
||||
#endif
|
||||
}
|
||||
|
||||
// Deferred apply-reboot: a verified `ota applydelta` approves the update but does NOT reboot inline,
|
||||
// so the CLI can first deliver the "verified; applying" reply (over LoRa it's the only way the
|
||||
// operator learns the apply started). The mesh loop then calls ota_reboot_to_apply() once that reply
|
||||
// has actually been transmitted. apply_at/apply_hard are mesh-clock deadlines the loop fills in.
|
||||
bool apply_pending = false;
|
||||
bool bootloader_apply_pending = false;
|
||||
uint32_t apply_at = 0; // earliest reboot time (lets the reply get queued + start sending)
|
||||
uint32_t apply_hard = 0; // hard cap, in case the TX queue never idles on a busy node
|
||||
|
||||
@@ -230,6 +263,15 @@ struct OtaContext {
|
||||
if (!_bl_caps_read) { _bl_caps = ota_bootloader_caps(); _bl_caps_read = true; }
|
||||
return _bl_caps;
|
||||
}
|
||||
OtaBootloaderIdentity _bl_identity;
|
||||
bool _bl_identity_read = false;
|
||||
const OtaBootloaderIdentity& bootloaderIdentity() {
|
||||
if (!_bl_identity_read) {
|
||||
ota_installed_bootloader_identity(_bl_identity);
|
||||
_bl_identity_read = true;
|
||||
}
|
||||
return _bl_identity;
|
||||
}
|
||||
|
||||
// --- discovery: the "what mOTAs are available around me" view ----------------------------------
|
||||
// The catalog (heard mOTAs) + the heard-sources table now live in OtaManager (built from beacons +
|
||||
@@ -460,6 +502,11 @@ struct OtaContext {
|
||||
manager.set_accept_full(true);
|
||||
manager.set_apply_codec(CODEC_DETOOLS_SEQUENTIAL); // preferred (streams straight to the slot)
|
||||
manager.set_apply_codec2(CODEC_DETOOLS_INPLACE); // also accepted -> a single in-place .mota fits both
|
||||
#endif
|
||||
#if defined(NRF52_PLATFORM) && defined(OTA_QSPI_STORE) && defined(OTA_QSPI_BOOTLOADER_UPDATE)
|
||||
manager.set_accept_bootloader(true);
|
||||
#else
|
||||
manager.set_accept_bootloader(false);
|
||||
#endif
|
||||
manager.set_fetch_store(&fetch_store);
|
||||
#if defined(NRF52_PLATFORM) && defined(OTA_SD_STORE)
|
||||
|
||||
@@ -27,6 +27,11 @@ inline uint32_t mota_nrf52_app_base() {
|
||||
inline uint32_t mota_nrf52_app_base() { return MOTA_NRF52_APP_BASE_S140_V6; }
|
||||
#endif
|
||||
static const uint32_t MOTA_NRF52_EXTRAFS_START = 0x000D4000u;
|
||||
// Bootloader-update-capable XIAO builds deliberately end the linked application here. The following
|
||||
// 40 KiB is reserved as internal scratch while OTAFIX copies a verified QSPI image into its own
|
||||
// 0xF4000..0xFE000 region. Ordinary nRF52 builds continue to use the 0xED000 application ceiling.
|
||||
static const uint32_t MOTA_NRF52_BOOT_SCRATCH_START = 0x000E0000u;
|
||||
static const uint32_t MOTA_NRF52_BOOT_SCRATCH_END = 0x000EA000u;
|
||||
static const uint32_t MOTA_NRF52_APP_END = 0x000ED000u;
|
||||
static const uint32_t MOTA_NRF52_STAGE_CEILING_LEGACY = MOTA_NRF52_EXTRAFS_START;
|
||||
static const uint32_t MOTA_NRF52_STAGE_CEILING_EXPANDED = MOTA_NRF52_APP_END;
|
||||
@@ -35,6 +40,7 @@ static const uint32_t MOTA_NRF52_STAGE_CEILING_EXPANDED = MOTA_NRF52_APP_END;
|
||||
static const uint32_t MOTA_NRF52_FS_START = MOTA_NRF52_EXTRAFS_START;
|
||||
static const uint32_t MOTA_NRF52_FLASH_PAGE = 4096u;
|
||||
static const uint8_t GPREGRET_OTA_APPLY = 0x6Au; // distinct from DFU magics 0x57/0x4E/0xA8
|
||||
static const uint8_t GPREGRET_OTA_BOOTLOADER_UPDATE = 0x6Bu;
|
||||
static const uint8_t GPREGRET2_OTA_STAGE_LEGACY = 0xD4u;
|
||||
static const uint8_t GPREGRET2_OTA_STAGE_EXPANDED = 0xEDu;
|
||||
static const uint8_t GPREGRET2_OTA_STAGE_QSPI = 0x51u;
|
||||
@@ -44,12 +50,20 @@ static const uint8_t GPREGRET2_OTA_STAGE_QSPI = 0x51u;
|
||||
// staged-container start; the app and bootloader both validate that per-patch value before writing.
|
||||
static const uint32_t MOTA_NRF52_FALLBACK_INPLACE_MEMORY = 0x00098000u; // 608 KB
|
||||
|
||||
// Bootloader flash region (nRF52840: 39 KB ending just below the CF2/MBR-params pages). The app scans
|
||||
// Bootloader flash region (nRF52840: 40 KiB 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;
|
||||
|
||||
inline uint32_t mota_nrf52_application_ceiling() {
|
||||
#if defined(OTA_QSPI_BOOTLOADER_UPDATE)
|
||||
return MOTA_NRF52_BOOT_SCRATCH_START;
|
||||
#else
|
||||
return MOTA_NRF52_APP_END;
|
||||
#endif
|
||||
}
|
||||
|
||||
// Compile-time layout-ordering invariants. If a constant above is edited inconsistently these fail the
|
||||
// BUILD rather than silently letting a stage/apply corrupt the filesystem (user prefs) or the app.
|
||||
static_assert((MOTA_NRF52_APP_BASE_S140_V6 % MOTA_NRF52_FLASH_PAGE) == 0, "S140 v6 base must be page-aligned");
|
||||
@@ -60,6 +74,10 @@ static_assert((MOTA_NRF52_STAGE_CEILING_EXPANDED % MOTA_NRF52_FLASH_PAGE) == 0,
|
||||
"expanded staging ceiling must be page-aligned");
|
||||
static_assert(MOTA_NRF52_STAGE_CEILING_LEGACY < MOTA_NRF52_STAGE_CEILING_EXPANDED,
|
||||
"legacy staging ceiling must precede expanded ceiling");
|
||||
static_assert(MOTA_NRF52_BOOT_SCRATCH_END - MOTA_NRF52_BOOT_SCRATCH_START == 0xA000u,
|
||||
"bootloader scratch must be exactly one padded bootloader region");
|
||||
static_assert(MOTA_NRF52_BOOT_SCRATCH_END <= MOTA_NRF52_APP_END,
|
||||
"bootloader scratch must remain below InternalFS");
|
||||
static_assert(MOTA_NRF52_STAGE_CEILING_EXPANDED < MOTA_NRF52_BL_START,
|
||||
"staging and filesystems must end below the bootloader");
|
||||
static_assert(MOTA_NRF52_BL_START < MOTA_NRF52_BL_END, "bootloader region must be non-empty");
|
||||
@@ -74,7 +92,9 @@ static_assert(MOTA_NRF52_APP_BASE_S140_V7 + MOTA_NRF52_FALLBACK_INPLACE_MEMORY <
|
||||
inline uint32_t mota_nrf52_stage_ceiling_for_layout(uint32_t linked_app_end,
|
||||
bool uses_internal_extrafs) {
|
||||
if (uses_internal_extrafs) return MOTA_NRF52_STAGE_CEILING_LEGACY;
|
||||
if (linked_app_end == MOTA_NRF52_APP_END || linked_app_end == MOTA_NRF52_EXTRAFS_START)
|
||||
if (linked_app_end == MOTA_NRF52_APP_END ||
|
||||
linked_app_end == MOTA_NRF52_BOOT_SCRATCH_START ||
|
||||
linked_app_end == MOTA_NRF52_EXTRAFS_START)
|
||||
return MOTA_NRF52_STAGE_CEILING_EXPANDED;
|
||||
return MOTA_NRF52_STAGE_CEILING_LEGACY;
|
||||
}
|
||||
|
||||
@@ -20,7 +20,12 @@ static const uint8_t ENDF_MAGIC[4] = { 'E', 'n', 'd', 'F' }; // 45 6E 64 4
|
||||
static const uint32_t ENDF_LEN = 56;
|
||||
|
||||
// ---- manifest -------------------------------------------------------------
|
||||
static const uint8_t MOTA_FORMAT_VER = 2; // fixed-layout manifest (see offsets below)
|
||||
// Application packages remain v2. Bootloader packages deliberately use v3 even though the byte
|
||||
// layout is unchanged: every pre-bootloader-update parser rejects v3 before it can mistake the raw
|
||||
// bootloader region for a full application image.
|
||||
static const uint8_t MOTA_FORMAT_VER = 2; // application package (compatibility name)
|
||||
static const uint8_t MOTA_APP_FORMAT_VER = 2;
|
||||
static const uint8_t MOTA_BOOT_FORMAT_VER = 3;
|
||||
static const uint8_t HASH_ALGO_SHA256 = 0x12; // multihash code
|
||||
|
||||
// Fixed manifest layout (manifest-minus-leaves) - every field is present at a constant offset, so the
|
||||
@@ -42,6 +47,8 @@ static const uint8_t MOTA_HW_ID_LEN = 32;
|
||||
|
||||
static const uint8_t MFLAG_FULL = 0x01; // 0 = delta/partial, 1 = full image
|
||||
static const uint8_t MFLAG_SIGNED = 0x02;
|
||||
static const uint8_t MFLAG_BOOTLOADER = 0x04; // v3 only; exact flags must be FULL|SIGNED|BOOTLOADER
|
||||
static const uint8_t MFLAG_KNOWN = MFLAG_FULL | MFLAG_SIGNED | MFLAG_BOOTLOADER;
|
||||
|
||||
static const uint8_t CODEC_FULL = 0;
|
||||
static const uint8_t CODEC_DETOOLS_SEQUENTIAL = 1;
|
||||
|
||||
@@ -921,6 +921,10 @@ bool OtaManager::wantRow(const uint8_t* mid, uint32_t target, uint8_t codec, uin
|
||||
|| _fstate == WANT_LEAVES || _fstate == VERIFYING_STAGED
|
||||
|| _fstate == PAUSED) return false; // busy
|
||||
if (_fstate == COMPLETE && memcmp(mid, _fid, 4) == 0) return false; // already have it
|
||||
if (flags & ~MFLAG_KNOWN) return false;
|
||||
const bool bootloader = (flags & MFLAG_BOOTLOADER) != 0;
|
||||
if (!_archive_fetch && bootloader &&
|
||||
(!_accept_bootloader || !_have_desired_mid || codec != CODEC_FULL)) return false;
|
||||
if (!_archive_fetch && !codecOk(codec)) return false; // can't apply this codec
|
||||
if (_have_desired_mid) // manual pull of a specific mid
|
||||
return memcmp(mid, _desired_mid, 4) == 0 && (_desired_target == 0 || target == _desired_target);
|
||||
@@ -1169,10 +1173,16 @@ void OtaManager::handleManifest(const uint8_t* m, uint16_t n) {
|
||||
const uint8_t* mf = _mf_buf; // fully reassembled manifest-minus-leaves
|
||||
uint32_t mfl = _mf_len;
|
||||
if (mfl != MOTA_MFL) { failFetch(FETCH_ERROR_MANIFEST); return; } // fixed 197-byte manifest
|
||||
if (mf[2] != HASH_ALGO_SHA256) { failFetch(FETCH_ERROR_HASH_ALGO); return; }
|
||||
if (!_archive_fetch && !codecOk(mf[56])) { failFetch(FETCH_ERROR_CODEC); return; }
|
||||
uint32_t payload_size = rd_u32le(mf + 15);
|
||||
uint8_t bsl = mf[19];
|
||||
MotaManifest parsed;
|
||||
if (!mota_parse_manifest(mf, mfl, parsed)) { failFetch(FETCH_ERROR_MANIFEST); return; }
|
||||
if (parsed.hash_algo != HASH_ALGO_SHA256) { failFetch(FETCH_ERROR_HASH_ALGO); return; }
|
||||
if (!_archive_fetch && parsed.is_bootloader() &&
|
||||
(!_accept_bootloader || !_have_desired_mid)) {
|
||||
failFetch(FETCH_ERROR_CODEC); return; // bootloader packages are manual-pull only
|
||||
}
|
||||
if (!_archive_fetch && !codecOk(parsed.codec_id)) { failFetch(FETCH_ERROR_CODEC); return; }
|
||||
uint32_t payload_size = parsed.payload_size;
|
||||
uint8_t bsl = parsed.block_size_log2;
|
||||
if (bsl >= 32) { failFetch(FETCH_ERROR_GEOMETRY); return; }
|
||||
uint32_t bs = 1u << bsl;
|
||||
// a block must fit our reassembly buffer (and be non-empty) - reject an oversized block_size up front
|
||||
@@ -1193,8 +1203,8 @@ void OtaManager::handleManifest(const uint8_t* m, uint16_t n) {
|
||||
// Hand the store the parsed layout BEFORE begin(), so a partition-backed store (ESP32) can choose
|
||||
// placement and refuse an unfittable fetch up front: a FULL payload streams to the inactive slot,
|
||||
// a delta's whole container is staged together. (image_size at mf+11, is_full from flags at mf+1.)
|
||||
bool is_full = (mf[1] & MFLAG_FULL) != 0;
|
||||
if (!_fetch->plan_layout(is_full, rd_u32le(mf + 11), payload_off, payload_size)) {
|
||||
bool is_full = parsed.is_full();
|
||||
if (!_fetch->plan_layout(is_full, parsed.image_size, payload_off, payload_size)) {
|
||||
failFetch(FETCH_ERROR_STORAGE); return;
|
||||
}
|
||||
if (!_fetch->begin(total)) { failFetch(FETCH_ERROR_STORAGE); return; }
|
||||
@@ -1316,6 +1326,7 @@ bool OtaManager::resumeStaged(const uint8_t* want_mid) {
|
||||
if (!_fetch->read(8, mbuf, mread) || !mota_parse_manifest(mbuf, mread, m)) return false;
|
||||
if (want_mid && memcmp(m.merkle_root, want_mid, 4) != 0) return false; // a different fw is staged
|
||||
if (m.hash_algo != HASH_ALGO_SHA256) return false;
|
||||
if (!_archive_fetch && m.is_bootloader() && !_accept_bootloader) return false;
|
||||
if (!_archive_fetch && !codecOk(m.codec_id)) return false;
|
||||
uint32_t mfl = (uint32_t)(m.approval - m.manifest_start) + 4; // manifest-minus-leaves length
|
||||
uint32_t bs = m.block_size();
|
||||
|
||||
@@ -359,6 +359,8 @@ public:
|
||||
// in-place `.mota` can target both ESP32 and nRF52). 0xFF = unset.
|
||||
void set_apply_codec2(uint8_t c) { _apply_codec2 = c; }
|
||||
void set_accept_full(bool on) { _accept_full = on; }
|
||||
void set_accept_bootloader(bool on) { _accept_bootloader = on; }
|
||||
bool accepts_bootloader() const { return _accept_bootloader; }
|
||||
bool codecOk(uint8_t c) const {
|
||||
return (c == CODEC_FULL && _accept_full) || c == _apply_codec || c == _apply_codec2;
|
||||
}
|
||||
@@ -391,6 +393,7 @@ public:
|
||||
void set_max_hops(uint8_t h) { _max_hops = h; }
|
||||
uint8_t max_hops() const { return _max_hops; }
|
||||
bool fetched_is_signed() const { return (_fflags & MFLAG_SIGNED) != 0; } // flags of the fetched manifest
|
||||
bool fetched_is_bootloader() const { return (_fflags & MFLAG_BOOTLOADER) != 0; }
|
||||
|
||||
// This node's id (pubkey[0:4]), stamped into adverts we send so receivers can count distinct seeders.
|
||||
void set_seeder_id(const uint8_t* id4) { if (id4) for (int i = 0; i < 4; i++) _seeder_id[i] = id4[i]; }
|
||||
@@ -601,6 +604,7 @@ private:
|
||||
uint8_t _apply_codec = CODEC_DETOOLS_SEQUENTIAL; // platform's delta codec (OtaContext sets it)
|
||||
uint8_t _apply_codec2 = 0xFF; // optional 2nd accepted delta codec (ESP32: in-place)
|
||||
bool _accept_full = true; // false on nRF52 flash (single-slot cannot apply full)
|
||||
bool _accept_bootloader = false; // explicit/manual only; never an auto-fetch signal
|
||||
bool _archive_fetch = false; // capture-only pull: accept any target/codec
|
||||
bool _archive_interest = false; // query full catalogs for the persistent archive
|
||||
uint8_t _seeder_id[4] = {0,0,0,0}; // our node id (pubkey[0:4]) for advert seeder counting
|
||||
|
||||
@@ -684,8 +684,9 @@ bool OtaStoreQspiNrf52::plan_layout(bool, uint32_t image_size, uint32_t, uint32_
|
||||
_io_ok = true;
|
||||
_stage = OtaQspiStage::IDLE;
|
||||
const uint32_t app_base = mota_nrf52_app_base();
|
||||
if (image_size == 0 || payload_size == 0 || app_base >= MOTA_NRF52_APP_END ||
|
||||
image_size > MOTA_NRF52_APP_END - app_base) {
|
||||
const uint32_t app_ceiling = mota_nrf52_application_ceiling();
|
||||
if (image_size == 0 || payload_size == 0 || app_base >= app_ceiling ||
|
||||
image_size > app_ceiling - app_base) {
|
||||
fail("image exceeds nRF52 application region");
|
||||
releaseFlash();
|
||||
return false;
|
||||
|
||||
Reference in new issue
Block a user