Merge remote-tracking branch 'origin/keymindCascade' into keymindCascade

This commit is contained in:
mikecarper committed 2026-10-23 18:08:25 -07:00
commit 170c1761d0
21 files changed
+1658 -41

No files matched your search

+3 -2
View File
@@ -481,13 +481,14 @@ void __attribute__((noinline)) Mesh::serviceLoopMaintenance() {
}
#if defined(ENABLE_OTA)
#if defined(OTA_BOOTLOADER_MIGRATION)
if (ota::bootloader_migration_pending()) {
if (ota::bootloader_migration_service_due()) {
// 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();
const bool ready = outbound_idle && ota_idle &&
(!ota::bootloader_migration_pending() || prepareForOtaReboot());
ota::bootloader_migration_tick(ready, ota_idle);
}
#endif
+156 -9
View File
@@ -24,10 +24,20 @@ static_assert(migration::ACL_READ_DENIED == ACL_ACL_PERM_READ_Msk, "ACL read per
static_assert(migration::ACL_WRITE_DENIED == ACL_ACL_PERM_WRITE_Msk, "ACL write permission bit");
namespace {
bool pending = false;
bool automatic_attempted = false;
bool automatic_blocked = false;
constexpr uint32_t AUTOMATIC_DELAY_MS = 70000u;
constexpr uint8_t ATTEMPT_MARKER = 0xD0;
uint32_t armed_at = 0;
uint8_t last_result = 0;
bool result_is_current = false;
bool already_installed = false;
FILESYSTEM *state_fs = nullptr;
uint8_t firmware_hash[32] = {};
MigrationAutoLock automatic_lock = MigrationAutoLock::Unavailable;
bool firmware_identified = false;
migration::InstalledVariant automatic_variant = migration::InstalledVariant::Unknown;
bool loader_excluded = 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
@@ -77,19 +87,67 @@ bool installed_up_to_date() {
migration_softdevice_fwid);
}
uint8_t retained_result() {
if (result_is_current) return last_result;
bool installed_allows_automatic() {
const uint8_t *boot = reinterpret_cast<const uint8_t *>(migration::BOOT_BASE);
OtaBootloaderIdentity installed;
const bool valid = ota_bootloader_identity_from_image(boot, migration::BOOT_SIZE, installed);
automatic_variant = migration::installed_variant(boot, migration::BOOT_SIZE,
valid ? installed.boot_version : 0);
if (automatic_variant == migration::InstalledVariant::Recovery) return false;
if (valid && automatic_variant == migration::InstalledVariant::Normal &&
migration::installed_matches_resource(installed, migration_board_id, migration_device_name,
mota_nrf52_app_base(), migration_softdevice_fwid))
return true;
// Only the explicitly pinned legacy bridge may omit modern identity/text.
if (!migration_has_legacy) return false;
uint8_t digest[32];
hash_region(migration::BOOT_BASE, migration::BOOT_SIZE, digest);
return memcmp(digest, migration_legacy_sha256, sizeof(digest)) == 0;
}
bool lock_automatic_for_firmware() {
if (!firmware_identified || !bootloader_migration_lock_auto(state_fs, firmware_hash)) {
automatic_lock = MigrationAutoLock::Unavailable;
return false;
}
automatic_lock = MigrationAutoLock::Locked;
return true;
}
bool read_reboot_marker(uint32_t &result) {
uint8_t enabled = 0;
uint32_t result = 0;
if (sd_softdevice_is_enabled(&enabled) != NRF_SUCCESS) return 0;
if (sd_softdevice_is_enabled(&enabled) != NRF_SUCCESS) return false;
if (enabled) {
if (sd_power_gpregret_get(0, &result) != NRF_SUCCESS) return 0;
if (sd_power_gpregret_get(0, &result) != NRF_SUCCESS) return false;
} else {
result = NRF_POWER->GPREGRET;
}
return true;
}
uint8_t retained_result() {
if (result_is_current) return last_result;
uint32_t result = 0;
if (!read_reboot_marker(result)) return 0;
return result >= 0xC0 && result <= 0xCF ? result & 15 : last_result;
}
bool write_reboot_marker(uint8_t marker) {
uint8_t enabled = 0;
if (sd_softdevice_is_enabled(&enabled) != NRF_SUCCESS) return false;
if (enabled) {
uint32_t current = 0;
if (sd_power_gpregret_get(0, &current) != NRF_SUCCESS) return false;
// Arming after the automatic image check must preserve its existing
// guard, rather than creating an unguarded gap between clear and set.
if (current == marker) return true;
if (sd_power_gpregret_clr(0, 0xFF) != NRF_SUCCESS) return false;
return marker == 0 || sd_power_gpregret_set(0, marker) == NRF_SUCCESS;
}
NRF_POWER->GPREGRET = marker;
return NRF_POWER->GPREGRET == marker;
}
// 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) {
@@ -174,7 +232,10 @@ void install() {
if (NRF_WDT->RREN & (1u << i)) NRF_WDT->RR[i] = 0x6E524635u;
}
}
NRF_POWER->GPREGRET = 0;
// The installed loader preserves our non-DFU marker. If COPY_BL or its
// preflight is interrupted, the next application boot will stay available
// instead of automatically trying the same operation again.
NRF_POWER->GPREGRET = ATTEMPT_MARKER;
NRF_POWER->GPREGRET2 = 0;
sd_mbr_command_t command = {};
command.command = SD_MBR_COMMAND_COPY_BL;
@@ -187,16 +248,40 @@ void install() {
}
} // namespace
void bootloader_migration_begin(FILESYSTEM *fs) {
state_fs = fs;
firmware_identified = false;
automatic_lock = MigrationAutoLock::Unavailable;
SelfFwInfo app;
if (fs && ota_self_firmware(app) && app.valid) {
hash_region(mota_nrf52_app_base(), app.image_len, firmware_hash);
firmware_identified = true;
automatic_lock = bootloader_migration_auto_lock(fs, firmware_hash);
}
automatic_attempted = automatic_lock == MigrationAutoLock::Locked ||
automatic_lock == MigrationAutoLock::Unavailable;
automatic_blocked = automatic_attempted;
}
bool bootloader_migration_pending() {
return pending;
}
bool bootloader_migration_service_due() {
return pending || (!automatic_attempted && millis() >= AUTOMATIC_DELAY_MS);
}
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,
snprintf(reply, capacity, "BL embedded 2.4.12: 40960 B; hash=%s; %s; last=%u; auto=%s", hash,
pending ? "armed" : already_installed ? "already installed" :
installed_up_to_date() ? "up to date" : "idle", retained_result());
installed_up_to_date() ? "up to date" : "idle", retained_result(),
loader_excluded ? automatic_variant == migration::InstalledVariant::Recovery ?
"recovery" : "unrecognized" :
automatic_lock == MigrationAutoLock::Locked ? "locked for this firmware" :
automatic_lock == MigrationAutoLock::Unavailable ? "storage unavailable" :
automatic_blocked ? "blocked" : automatic_attempted ? "checked" : "70s after boot");
}
bool bootloader_migration_request(const char *confirmation, char *reply, size_t capacity) {
@@ -206,6 +291,10 @@ bool bootloader_migration_request(const char *confirmation, char *reply, size_t
snprintf(reply, capacity, "ERR confirm exact image: ota bootloader embedded install %s", hash);
return false;
}
// A manual attempt also satisfies the startup check, including a cancelled
// or failed attempt. Only another explicit command may retry it this boot.
automatic_attempted = true;
automatic_blocked = false;
if (pending) {
snprintf(reply, capacity, "OK bootloader update already armed");
return true;
@@ -229,6 +318,18 @@ bool bootloader_migration_request(const char *confirmation, char *reply, size_t
return true;
}
already_installed = false;
if (!lock_automatic_for_firmware()) {
last_result = 18;
result_is_current = true;
snprintf(reply, capacity, "ERR cannot save bootloader automatic-update lock");
return false;
}
if (!write_reboot_marker(ATTEMPT_MARKER)) {
last_result = 17;
result_is_current = true;
snprintf(reply, capacity, "ERR bootloader reboot guard unavailable");
return false;
}
pending = true;
armed_at = millis();
last_result = 0;
@@ -258,7 +359,53 @@ void bootloader_migration_audit(char *reply, size_t capacity) {
}
void bootloader_migration_tick(bool outbound_idle, bool ota_idle) {
if (!pending) return;
if (!pending) {
if (!bootloader_migration_service_due() || !outbound_idle || !ota_idle) return;
// A failed MBR/preflight reset leaves this marker. Do not create a loop
// that retries and resets every 70 seconds; the CLI can retry explicitly.
uint32_t marker = 0;
if (!read_reboot_marker(marker) ||
(automatic_lock != MigrationAutoLock::PreviousImage &&
(marker == ATTEMPT_MARKER || (marker > 0xC0 && marker <= 0xCF)))) {
automatic_attempted = true;
automatic_blocked = true;
return;
}
automatic_attempted = true;
// Mark before reading either image too, so a reset in the version/hash
// check cannot turn into a recurring 70-second reboot loop.
if (!write_reboot_marker(ATTEMPT_MARKER)) {
last_result = 17;
result_is_current = true;
automatic_blocked = true;
return;
}
// Save the current image before inspecting the bootloader. A new firmware
// may ignore its predecessor's retained marker, so GPREGRET alone cannot
// protect a reset during that first check.
if (!lock_automatic_for_firmware()) {
last_result = 18;
result_is_current = true;
automatic_blocked = true;
return;
}
if (!installed_allows_automatic()) {
loader_excluded = true;
automatic_blocked = true;
if (!write_reboot_marker(0)) {
last_result = 17;
result_is_current = true;
}
return;
}
char reply[160];
bootloader_migration_request(nullptr, reply, sizeof(reply));
if (!pending && last_result == 0 && !write_reboot_marker(0)) {
last_result = 17;
result_is_current = true;
}
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.
+3
View File
@@ -2,10 +2,13 @@
#if defined(OTA_BOOTLOADER_MIGRATION)
#include <stddef.h>
#include "BootloaderMigrationStore.h"
namespace mesh {
namespace ota {
void bootloader_migration_begin(FILESYSTEM *fs);
bool bootloader_migration_pending();
bool bootloader_migration_service_due();
// nullptr selects the pinned embedded image; a supplied hash must match exactly.
bool bootloader_migration_request(const char *confirmation, char *reply, size_t capacity);
void bootloader_migration_status(char *reply, size_t capacity);
+60 -4
View File
@@ -2,6 +2,7 @@
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include "OtaBootloaderUpdate.h"
namespace mesh {
@@ -16,6 +17,55 @@ 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;
enum class InstalledVariant { Unknown, Normal, Recovery };
inline bool boot_text_matches(const uint8_t *text, size_t available,
const char *expected, size_t length) {
return available >= length && memcmp(text, expected, length) == 0;
}
// Recovery builds deliberately retain the normal board identity and BLM2
// flags. Their on-device version text is the only published discriminator.
// Older recovery images can omit that text entirely, so automatic installation
// also requires one complete, recognized normal INFO_UF2 version record.
// The caller must separately verify the image CRC, identity and platform.
inline InstalledVariant installed_variant(const uint8_t *image, size_t size,
uint32_t version) {
if (!image || size != BOOT_SIZE || !ota_bootloader_version_valid(version))
return InstalledVariant::Unknown;
static const char header[] = "UF2 Bootloader ";
static const char recovery[] = "R_0x";
static const char old_recovery[] = "-recovery-allow-all";
static const char recovery_header[] = "UF2 Bootloader R_";
for (size_t i = 0; i < size; ++i) {
const size_t left = size - i;
if (boot_text_matches(image + i, left, recovery, sizeof(recovery) - 1) ||
boot_text_matches(image + i, left, old_recovery, sizeof(old_recovery) - 1) ||
boot_text_matches(image + i, left, recovery_header, sizeof(recovery_header) - 1))
return InstalledVariant::Recovery;
}
char normal[48], packed[16];
const unsigned preview = version & 0xFFu;
snprintf(normal, sizeof(normal),
preview == 0xFFu ? "0.11.0-OTAFIX%u.%u.%u\r\n" :
"0.11.0-OTAFIX%u.%u.%u-preview.%u\r\n",
unsigned(version >> 24), unsigned((version >> 16) & 0xFFu),
unsigned((version >> 8) & 0xFFu), preview);
snprintf(packed, sizeof(packed), "BL_0x%08lX\r\n", (unsigned long)version);
unsigned records = 0;
for (size_t i = 0; i + sizeof(header) - 1 < size; ++i) {
if (!boot_text_matches(image + i, size - i, header, sizeof(header) - 1)) continue;
const uint8_t *label = image + i + sizeof(header) - 1;
size_t left = size - i - (sizeof(header) - 1);
bool matches = boot_text_matches(label, left, packed, strlen(packed));
if (*label == 'v') { ++label; --left; }
if (!matches && !boot_text_matches(label, left, normal, strlen(normal)))
return InstalledVariant::Unknown;
if (++records > 1) return InstalledVariant::Unknown;
}
return records == 1 ? InstalledVariant::Normal : InstalledVariant::Unknown;
}
struct AclRegion {
uint32_t address;
uint32_t size;
@@ -101,13 +151,19 @@ inline bool installed_can_migrate(const OtaBootloaderIdentity &installed,
140, fwid, app_base, 1) == OTA_BOOT_CONTINUITY_OK;
}
inline bool installed_up_to_date(const OtaBootloaderIdentity &installed,
uint32_t board_id, const char name[16],
uint32_t version, uint32_t app_base, uint16_t fwid) {
inline bool installed_matches_resource(const OtaBootloaderIdentity &installed,
uint32_t board_id, const char name[16],
uint32_t app_base, uint16_t fwid) {
return installed.present && installed.crc_ok &&
installed.image_start == BOOT_BASE && installed.image_size == BOOT_SIZE &&
installed.board_id == board_id && memcmp(installed.device_name, name, 16) == 0 &&
ota_bootloader_continuity_matches_runtime(installed, 140, fwid, app_base, 1) &&
ota_bootloader_continuity_matches_runtime(installed, 140, fwid, app_base, 1);
}
inline bool installed_up_to_date(const OtaBootloaderIdentity &installed,
uint32_t board_id, const char name[16],
uint32_t version, uint32_t app_base, uint16_t fwid) {
return installed_matches_resource(installed, board_id, name, app_base, fwid) &&
installed.boot_version >= version;
}
@@ -0,0 +1,57 @@
#if defined(OTA_BOOTLOADER_MIGRATION)
#include "BootloaderMigrationStore.h"
#include <helpers/AtomicFileWriter.h>
#include <helpers/FilePresence.h>
#include <helpers/PersistentStoreFormat.h>
namespace mesh { namespace ota {
namespace {
constexpr size_t RecordSize = 40;
const char* const Path = "/bl_auto";
const char* const Temp = "/bl_auto.tmp";
const uint8_t Magic[4] = {'B', 'A', 'L', 1};
bool readRecord(FILESYSTEM* fs, uint8_t record[RecordSize]) {
File file(*fs);
if (!file.open(Path, FILE_O_READ)) return false;
const bool complete = !file.isDirectory() && file.size() == RecordSize
&& file.read(record, RecordSize) == static_cast<int>(RecordSize);
file.close();
return complete
&& memcmp(record, Magic, sizeof(Magic)) == 0
&& storage::readLE32(record + RecordSize - 4)
== storage::updateCRC32(0xffffffffU, record, RecordSize - 4);
}
}
MigrationAutoLock bootloader_migration_auto_lock(
FILESYSTEM* fs, const uint8_t image_hash[32]) {
bool present = false;
if (!fs || !image_hash || !filePresence(fs, Temp, present) || present)
return MigrationAutoLock::Unavailable;
if (!filePresence(fs, Path, present)) return MigrationAutoLock::Unavailable;
if (!present) return MigrationAutoLock::Eligible;
uint8_t record[RecordSize];
if (!readRecord(fs, record)) return MigrationAutoLock::Unavailable;
return memcmp(record + sizeof(Magic), image_hash, 32) == 0
? MigrationAutoLock::Locked : MigrationAutoLock::PreviousImage;
}
bool bootloader_migration_lock_auto(FILESYSTEM* fs, const uint8_t image_hash[32]) {
const auto state = bootloader_migration_auto_lock(fs, image_hash);
if (state == MigrationAutoLock::Locked) return true;
if (state == MigrationAutoLock::Unavailable) return false;
uint8_t record[RecordSize], verify[RecordSize];
memcpy(record, Magic, sizeof(Magic));
memcpy(record + sizeof(Magic), image_hash, 32);
storage::writeLE32(record + RecordSize - 4,
storage::updateCRC32(0xffffffffU, record, RecordSize - 4));
AtomicFileWriter writer(fs, Path);
return writer && writer.write(record, sizeof(record)) == sizeof(record)
&& writer.commit() && readRecord(fs, verify)
&& memcmp(record, verify, sizeof(record)) == 0;
}
} } // namespace mesh::ota
#endif
@@ -0,0 +1,20 @@
#pragma once
#if defined(OTA_BOOTLOADER_MIGRATION)
#include <helpers/IdentityStore.h>
namespace mesh { namespace ota {
enum class MigrationAutoLock : uint8_t { Eligible, PreviousImage, Locked, Unavailable };
// image_hash is SHA-256 of the complete, verified running application. Pass
// nullptr as the filesystem when the role is using volatile fallback storage.
MigrationAutoLock bootloader_migration_auto_lock(
FILESYSTEM* fs, const uint8_t image_hash[32]);
// Record an attempt before inspecting the bootloader. A matching durable record is
// already sufficient; a different image may atomically replace its predecessor.
bool bootloader_migration_lock_auto(FILESYSTEM* fs, const uint8_t image_hash[32]);
} } // namespace mesh::ota
#endif