Add nRF52 Repeater and Room variants with embedded OTAFIX 2.4.12

This commit is contained in:
mikecarper committed 2026-10-23 16:36:18 -07:00
1 parent 9b0e013f4b
commit d808d964d2
32 files changed
+2702 -287

No files matched your search

+17
View File
@@ -10,6 +10,9 @@
#include "helpers/ota/OtaContext.h" // OTA mesh-integration is centralized here so every role gets it
#include "helpers/ota/OtaSpeedConfig.h"
#include "helpers/ota/OtaProtocol.h" // decode_adv -> the `ota neighbors` discovery table
#if defined(OTA_BOOTLOADER_MIGRATION)
#include "helpers/ota/BootloaderMigration.h"
#endif
#if MESH_ENABLE_OTA_KISS_DIRECT
#include "helpers/ota/OtaDirectWire.h"
#endif
@@ -419,6 +422,9 @@ void Mesh::loop() {
}
bool Mesh::hasPendingOtaApply() const {
#if defined(OTA_BOOTLOADER_MIGRATION)
if (ota::bootloader_migration_pending()) return true;
#endif
#if defined(ENABLE_OTA) && !defined(OTA_SEEDER_ONLY)
// A released dynamic context cannot hold a pending apply: the context is only
// handed back once apply_pending is clear.
@@ -474,6 +480,17 @@ void __attribute__((noinline)) Mesh::serviceLoopMaintenance() {
}
}
#if defined(ENABLE_OTA)
#if defined(OTA_BOOTLOADER_MIGRATION)
if (ota::bootloader_migration_pending()) {
// This bridge owns no OTA workspace. Service it even after a dynamic
// context has been released, and never allocate one just to poll it.
const ota::OtaContext* context = ota::ota_context_if_active();
const bool ota_idle = !context || context->embeddedBootloaderIdle();
const bool outbound_idle = !hasPendingRadioWork();
const bool ready = outbound_idle && ota_idle && prepareForOtaReboot();
ota::bootloader_migration_tick(ready, ota_idle);
}
#endif
#if OTA_DYNAMIC_CONTEXT
// Nothing below can run without storage, and a released context holds no
// pending apply or egress. Bail before any ota_ctx() dereference.
+260
View File
@@ -0,0 +1,260 @@
#if defined(OTA_BOOTLOADER_MIGRATION)
#if !defined(NRF52_PLATFORM) || !defined(ENABLE_OTA)
#error Bootloader migration requires nRF52 OTA infrastructure firmware
#endif
#include "BootloaderMigration.h"
#include "BootloaderMigrationPolicy.h"
#include "BootloaderMigrationResource.h"
#include "OtaSelf.h"
#include <Arduino.h>
#include <SHA256.h>
#include <nrf.h>
#include <nrf_mbr.h>
#include <nrf_sdm.h>
#include <nrf_soc.h>
#include <stdio.h>
#include <string.h>
namespace mesh {
namespace ota {
static_assert(migration::ACL_READ_DENIED == ACL_ACL_PERM_READ_Msk, "ACL read permission bit");
static_assert(migration::ACL_WRITE_DENIED == ACL_ACL_PERM_WRITE_Msk, "ACL write permission bit");
namespace {
bool pending = false;
uint32_t armed_at = 0;
uint8_t last_result = 0;
bool result_is_current = false;
bool already_installed = false;
// The pinned framework's historical SVC wrapper has no memory clobber or IPA
// barrier. The MBR reads this pointed-to command after exception entry, which
// must remain visible to the compiler even under whole-program optimization.
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wreturn-type"
__attribute__((naked, noipa)) uint32_t migration_mbr_command(sd_mbr_command_t *) {
__asm volatile("svc %0\n"
"bx lr"
:
: "I"(SD_MBR_COMMAND)
: "r0", "memory");
}
#pragma GCC diagnostic pop
void hex8(const uint8_t *value, char output[17]) {
static const char digits[] = "0123456789ABCDEF";
for (unsigned i = 0; i < 8; ++i) {
output[2 * i] = digits[value[i] >> 4];
output[2 * i + 1] = digits[value[i] & 15];
}
output[16] = 0;
}
void hash_region(uint32_t address, uint32_t size, uint8_t output[32]) {
SHA256 hash;
hash.reset();
uint8_t buffer[128];
const volatile uint8_t *source = reinterpret_cast<const volatile uint8_t *>(address);
while (size) {
const uint32_t take = size < sizeof(buffer) ? size : sizeof(buffer);
for (uint32_t i = 0; i < take; ++i)
buffer[i] = source[i];
hash.update(buffer, take);
source += take;
size -= take;
}
hash.finalize(output, 32);
}
uint8_t retained_result() {
if (result_is_current) return last_result;
uint8_t enabled = 0;
uint32_t result = 0;
if (sd_softdevice_is_enabled(&enabled) != NRF_SUCCESS) return 0;
if (enabled) {
if (sd_power_gpregret_get(0, &result) != NRF_SUCCESS) return 0;
} else {
result = NRF_POWER->GPREGRET;
}
return result >= 0xC0 && result <= 0xCF ? result & 15 : last_result;
}
// Called only after the operator reply has drained and SoftDevice is stopped.
// GPREGRET (not GPREGRET2) survives this legacy loader's diagnostic writes.
void fail_and_reset(uint8_t result) {
NRF_POWER->GPREGRET = 0xC0u | (result & 15u);
NVIC_SystemReset();
while (true) {
}
}
void install() {
SelfFwInfo app;
if (!ota_self_firmware(app) || !app.valid) {
pending = false;
last_result = 1;
return;
}
uint8_t enabled = 0;
if (sd_softdevice_is_enabled(&enabled) != NRF_SUCCESS ||
(enabled && sd_softdevice_disable() != NRF_SUCCESS)) {
pending = false;
last_result = 2;
return;
}
// ACL is SoftDevice-owned while enabled. Inspect it only after disabling
// SoftDevice; no ACL/MPU writes or attempts to defeat locked regions.
migration::Snapshot snapshot = {};
snapshot.part = NRF_FICR->INFO.PART;
snapshot.flash_page_size = NRF_FICR->CODEPAGESIZE;
snapshot.flash_pages = NRF_FICR->CODESIZE;
for (unsigned i = 0; i < 8; ++i) {
snapshot.acl[i] = { NRF_ACL->ACL[i].ADDR, NRF_ACL->ACL[i].SIZE, NRF_ACL->ACL[i].PERM };
const auto &region = snapshot.acl[i];
if ((region.permissions & migration::ACL_READ_DENIED) &&
(migration::overlaps(region.address, region.size, 0, migration::APP_CEILING) ||
migration::overlaps(region.address, region.size, migration::BOOT_BASE, migration::SETTINGS_BASE)))
fail_and_reset(9);
}
snapshot.app_base = mota_nrf52_app_base();
snapshot.app_ceiling = static_cast<uint32_t>(reinterpret_cast<uintptr_t>(__flash_arduino_end));
snapshot.expected_softdevice_fwid = migration_softdevice_fwid;
snapshot.softdevice_size = *reinterpret_cast<const volatile uint32_t *>(0x3008);
snapshot.softdevice_fwid = *reinterpret_cast<const volatile uint16_t *>(0x300C);
snapshot.mbr_boot_override = *reinterpret_cast<const volatile uint32_t *>(0xFF8);
snapshot.mbr_params_override = *reinterpret_cast<const volatile uint32_t *>(0xFFC);
snapshot.uicr_boot = NRF_UICR->NRFFW[0];
snapshot.uicr_params = NRF_UICR->NRFFW[1];
snapshot.source = static_cast<uint32_t>(reinterpret_cast<uintptr_t>(migration_candidate));
snapshot.source_size = sizeof(migration_candidate);
snapshot.app_body_size = app.body_len;
snapshot.app_image_size = app.image_len;
const auto gate = migration::preflight(snapshot);
if (gate != migration::Gate::Ok) fail_and_reset(3 + static_cast<uint8_t>(gate));
if (snapshot.app_base != migration_app_base) fail_and_reset(5);
uint8_t digest[32];
hash_region(snapshot.source, snapshot.source_size, digest);
if (memcmp(digest, migration_candidate_sha256, sizeof(digest)) != 0) fail_and_reset(10);
OtaBootloaderIdentity candidate;
if (!ota_bootloader_vector_sane(migration_candidate) ||
!ota_bootloader_candidate_identity_from_image(migration_candidate, migration::BOOT_SIZE, candidate) ||
!migration::candidate_matches_resource(candidate, migration_board_id, migration_device_name,
migration_boot_version, snapshot.app_base,
snapshot.softdevice_fwid))
fail_and_reset(10);
hash_region(migration::BOOT_BASE, migration::BOOT_SIZE, digest);
OtaBootloaderIdentity installed;
const uint8_t *boot = reinterpret_cast<const uint8_t *>(migration::BOOT_BASE);
const bool valid_identity = ota_bootloader_identity_from_image(boot, migration::BOOT_SIZE, installed);
// A damaged or ambiguous modern manifest cannot masquerade as a legacy
// loader. Only the explicitly pinned, byte-exact legacy image is eligible.
if (!valid_identity) installed = OtaBootloaderIdentity();
if (!migration::installed_can_migrate(installed, candidate, migration_has_legacy,
memcmp(digest, migration_legacy_sha256, sizeof(digest)) == 0,
snapshot.app_base, snapshot.softdevice_fwid))
fail_and_reset(12);
// Do not start an uninterruptible MBR copy under an unexpectedly short WDT.
if (NRF_WDT->RUNSTATUS && NRF_WDT->CRV < 5u * 32768u) fail_and_reset(13);
if (NRF_WDT->RUNSTATUS) {
for (unsigned i = 0; i < 8; ++i) {
if (NRF_WDT->RREN & (1u << i)) NRF_WDT->RR[i] = 0x6E524635u;
}
}
NRF_POWER->GPREGRET = 0;
NRF_POWER->GPREGRET2 = 0;
sd_mbr_command_t command = {};
command.command = SD_MBR_COMMAND_COPY_BL;
command.params.copy_bl.bl_src = reinterpret_cast<uint32_t *>(snapshot.source);
command.params.copy_bl.bl_len = migration::BOOT_SIZE / sizeof(uint32_t);
// MBR owns the erase/copy and persistent recovery parameters. It never
// returns on success. No application, filesystem or settings erase occurs.
(void)migration_mbr_command(&command);
fail_and_reset(14);
}
} // namespace
bool bootloader_migration_pending() {
return pending;
}
void bootloader_migration_status(char *reply, size_t capacity) {
char hash[17];
hex8(migration_candidate_sha256, hash);
snprintf(reply, capacity, "BL embedded 2.4.12: 40960 B; hash=%s; %s; last=%u", hash,
pending ? "armed" : already_installed ? "already installed" : "idle", retained_result());
}
bool bootloader_migration_request(const char *confirmation, char *reply, size_t capacity) {
char hash[17];
hex8(migration_candidate_sha256, hash);
if (pending) {
snprintf(reply, capacity, "ERR bootloader migration is already armed");
return false;
}
if (strcmp(confirmation, hash) != 0) {
snprintf(reply, capacity, "ERR confirm exact image: ota bootloader embedded install %s", hash);
return false;
}
// Bootloader diagnostics already read this memory-mapped region while
// SoftDevice runs. Detect a no-op before stopping it so BLE stays usable.
uint8_t digest[32];
hash_region(migration::BOOT_BASE, migration::BOOT_SIZE, digest);
if (memcmp(digest, migration_candidate_sha256, sizeof(digest)) == 0) {
already_installed = true;
last_result = 0;
result_is_current = true;
snprintf(reply, capacity, "OK bootloader already installed");
return true;
}
already_installed = false;
pending = true;
armed_at = millis();
last_result = 0;
result_is_current = true;
snprintf(reply, capacity, "OK migration armed; checking exact old loader and protection after TX drains");
return true;
}
void bootloader_migration_audit(char *reply, size_t capacity) {
SelfFwInfo app;
if (!ota_self_firmware(app) || !app.valid) {
snprintf(reply, capacity, "ERR no valid application EndF");
return;
}
uint8_t digest[32];
char app_hash[17], settings_hash[17], fs_hash[17], sd_hash[17];
hash_region(mota_nrf52_app_base(), app.image_len, digest);
hex8(digest, app_hash);
hash_region(migration::SETTINGS_BASE, 4096, digest);
hex8(digest, settings_hash);
const uint32_t fs_start = mota_nrf52_layout_stage_ceiling();
hash_region(fs_start, migration::BOOT_BASE - fs_start, digest);
hex8(digest, fs_hash);
hash_region(0, mota_nrf52_app_base(), digest);
hex8(digest, sd_hash);
snprintf(reply, capacity, "app=%s settings=%s fs=%s sd=%s", app_hash, settings_hash, fs_hash, sd_hash);
}
void bootloader_migration_tick(bool outbound_idle, bool ota_idle) {
if (!pending) return;
const uint32_t elapsed = millis() - armed_at;
// An incoming advert may have started autofetch since the admin command.
// Never disable SoftDevice while another OTA session owns the flash store.
if (!ota_idle) {
pending = false;
last_result = 16;
} else if (elapsed >= 30000u) {
pending = false;
last_result = 15;
} else if (elapsed >= 1500u && outbound_idle) {
install();
}
}
} // namespace ota
} // namespace mesh
#endif
+15
View File
@@ -0,0 +1,15 @@
#pragma once
#if defined(OTA_BOOTLOADER_MIGRATION)
#include <stddef.h>
namespace mesh {
namespace ota {
bool bootloader_migration_pending();
bool bootloader_migration_request(const char *confirmation, char *reply, size_t capacity);
void bootloader_migration_status(char *reply, size_t capacity);
void bootloader_migration_audit(char *reply, size_t capacity);
void bootloader_migration_tick(bool outbound_idle, bool ota_idle);
} // namespace ota
} // namespace mesh
#endif
+106
View File
@@ -0,0 +1,106 @@
#pragma once
#include <stddef.h>
#include <stdint.h>
#include "OtaBootloaderUpdate.h"
namespace mesh {
namespace ota {
namespace migration {
static const uint32_t APP_CEILING = 0xED000u;
static const uint32_t BOOT_BASE = 0xF4000u;
static const uint32_t BOOT_SIZE = 0xA000u;
static const uint32_t PARAMS_BASE = 0xFE000u;
static const uint32_t SETTINGS_BASE = 0xFF000u;
static const uint32_t ACL_READ_DENIED = 1u << 2;
static const uint32_t ACL_WRITE_DENIED = 1u << 1;
struct AclRegion {
uint32_t address;
uint32_t size;
uint32_t permissions;
};
struct Snapshot {
uint32_t part;
uint32_t flash_page_size;
uint32_t flash_pages;
uint32_t softdevice_size;
uint16_t softdevice_fwid;
uint32_t app_base;
uint32_t app_ceiling;
uint16_t expected_softdevice_fwid;
uint32_t mbr_boot_override;
uint32_t mbr_params_override;
uint32_t uicr_boot;
uint32_t uicr_params;
uint32_t source;
uint32_t source_size;
uint32_t app_body_size;
uint32_t app_image_size;
AclRegion acl[8];
};
enum class Gate { Ok, Chip, Softdevice, BootAddress, ParamsAddress, Source, Protection };
inline bool overlaps(uint32_t start, uint32_t size, uint32_t lo, uint32_t hi) {
return size && uint64_t(start) < hi && uint64_t(start) + size > lo;
}
inline Gate preflight(const Snapshot &s) {
if (s.part != 0x52840u || s.flash_page_size != 4096u || s.flash_pages != 256u) return Gate::Chip;
if ((s.app_base != 0x26000u && s.app_base != 0x27000u) ||
s.app_ceiling > APP_CEILING || s.app_ceiling <= s.app_base ||
s.softdevice_size != s.app_base || s.softdevice_fwid != s.expected_softdevice_fwid ||
(s.app_base == 0x26000u ? s.softdevice_fwid != 0xB6u : s.softdevice_fwid != 0x123u))
return Gate::Softdevice;
const uint32_t boot = s.mbr_boot_override == UINT32_MAX ? s.uicr_boot : s.mbr_boot_override;
const uint32_t params = s.mbr_params_override == UINT32_MAX ? s.uicr_params : s.mbr_params_override;
if (boot != BOOT_BASE) return Gate::BootAddress;
if (params != PARAMS_BASE) return Gate::ParamsAddress;
// The source is a linked application resource, never SRAM, external memory,
// a filesystem, the live bootloader, or a dynamically chosen scratch bank.
if (s.source_size != BOOT_SIZE || (s.source & 3u) || s.source < s.app_base || !s.app_body_size ||
s.app_body_size > s.app_ceiling - s.app_base ||
s.app_image_size != uint64_t(s.app_body_size) + ENDF_LEN ||
s.app_image_size > s.app_ceiling - s.app_base ||
uint64_t(s.source) + BOOT_SIZE > uint64_t(s.app_base) + s.app_body_size)
return Gate::Source;
for (const auto &r : s.acl) {
// nRF52840 ACL bit 2 denies reads; bit 1 denies writes. COPY_BL also needs its
// parameter page writable. Never try to unlock an existing ACL region.
if ((r.permissions & ACL_READ_DENIED) &&
(overlaps(r.address, r.size, s.app_base, s.app_base + s.app_body_size) ||
overlaps(r.address, r.size, BOOT_BASE, SETTINGS_BASE)))
return Gate::Protection;
if ((r.permissions & ACL_WRITE_DENIED) && overlaps(r.address, r.size, BOOT_BASE, SETTINGS_BASE))
return Gate::Protection;
}
return Gate::Ok;
}
inline bool candidate_matches_resource(const OtaBootloaderIdentity &candidate,
uint32_t board_id, const char name[16],
uint32_t version, uint32_t app_base, uint16_t fwid) {
return ota_bootloader_candidate_identity_canonical(candidate) &&
candidate.board_id == board_id && memcmp(candidate.device_name, name, 16) == 0 &&
candidate.boot_version == version &&
ota_bootloader_continuity_matches_runtime(candidate, 140, fwid, app_base, 1);
}
inline bool installed_can_migrate(const OtaBootloaderIdentity &installed,
const OtaBootloaderIdentity &candidate,
bool has_legacy, bool legacy_hash_matches,
uint32_t app_base, uint16_t fwid) {
if (!installed.present)
return has_legacy && legacy_hash_matches;
return installed.crc_ok && installed.continuity_present &&
ota_bootloader_identity_matches(installed, candidate) &&
ota_bootloader_continuity_gate(installed, candidate, candidate.boot_version,
140, fwid, app_base, 1) == OTA_BOOT_CONTINUITY_OK;
}
} // namespace migration
} // namespace ota
} // namespace mesh
+20
View File
@@ -1030,6 +1030,26 @@ static __attribute__((noinline)) bool handle_control_command(
#endif
} else if (is_cmd(a, "bootloader|blupdate", &rest)) {
#if defined(OTA_BOOTLOADER_MIGRATION)
const char* embedded = nullptr;
if (is_cmd(rest, "embedded", &embedded)) {
if (*embedded == 0 || strcmp(embedded, "status") == 0) {
bootloader_migration_status(reply, 160);
} else if (strcmp(embedded, "audit") == 0) {
bootloader_migration_audit(reply, 160);
} else {
const char* confirmation = nullptr;
if (!is_cmd(embedded, "install", &confirmation)) {
strcpy(reply, "ERR usage: ota bootloader embedded [status|audit|install HASH16]");
} else if (!c.embeddedBootloaderIdle()) {
strcpy(reply, "ERR another OTA operation is active");
} else {
bootloader_migration_request(confirmation, reply, 160);
}
}
return true;
}
#endif
#if defined(NRF52_PLATFORM) && !defined(OTA_SEEDER_ONLY) && \
(defined(OTA_QSPI_BOOTLOADER_UPDATE) || defined(OTA_INTERNAL_BOOTLOADER_UPDATE) || \
defined(OTA_SD_BOOTLOADER_UPDATE))
+25
View File
@@ -15,6 +15,9 @@
#include "OtaSelf.h" // ota_self_firmware() - prefer self-describing EndF identity at begin()
#include "OtaSelfServePolicy.h"
#include "OtaBlInfo.h" // bootloader OTA-apply capability marker (nRF52); cached after first read
#if defined(OTA_BOOTLOADER_MIGRATION)
#include "BootloaderMigration.h"
#endif
// Storage policy for the mOTA context. A "dynamic" context is created on demand
// and handed back once idle, so its multi-kilobyte workspace only occupies RAM
@@ -240,6 +243,18 @@ struct OtaContext {
// Apply the COMPLETE fetched .mota (platform dispatch) and arm the slot; sets apply_pending on success
// 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.
#if defined(OTA_BOOTLOADER_MIGRATION)
bool embeddedBootloaderIdle() const {
const auto state = manager.fetchState();
return (state == OtaManager::IDLE || state == OtaManager::COMPLETE || state == OtaManager::FAILED) &&
!apply_pending && !bootloader_apply_pending &&
!manager.pendingServeJobs() && !manager.pendingManifestJobs()
#if defined(OTA_SD_STORE)
&& !sdCacheFetching()
#endif
;
}
#endif
bool apply_fetched(char* msg) {
return apply_fetched_impl(nullptr, false, msg);
}
@@ -264,6 +279,11 @@ struct OtaContext {
}
bool apply_fetched_impl(const uint8_t* rescue_base_hash, bool trusted_auto, char* msg) {
#if defined(OTA_BOOTLOADER_MIGRATION)
if (bootloader_migration_pending()) {
strcpy(msg, "embedded bootloader update is already armed"); return false;
}
#endif
// The armed update owns its store and handoff until deferred reboot.
// Re-entering an applier can reset that handoff before a later read fails.
if (apply_pending || bootloader_apply_pending) {
@@ -398,6 +418,11 @@ struct OtaContext {
bool apply_fetched_bootloader(const uint8_t operator_mid[4],
const uint8_t operator_hash8[8], char* msg) {
#if defined(OTA_BOOTLOADER_MIGRATION)
if (bootloader_migration_pending()) {
strcpy(msg, "embedded bootloader update is already armed"); return false;
}
#endif
if (apply_pending || bootloader_apply_pending) {
strcpy(msg, "another update is already armed");
return false;