Support compatible RAK bootloader updates with adaptive app storage

This commit is contained in:
mikecarper committed 2026-09-29 03:08:53 -07:00
1 parent 793531e16b
commit 346cf2fd1f
12 files changed
+430 -22

No files matched your search

+15 -4
View File
@@ -1257,9 +1257,7 @@ bool ota_prepare_bootloader_update_nrf52(OtaStoreFlashNrf52& store,
}
const OtaBlCaps current_caps = ota_bootloader_update_caps();
if (!ota_bootloader_self_update_caps_valid(current_caps) ||
current_caps.storage_flags != (OTA_BL_STORAGE_STAGE_CEILING |
OTA_BL_STORAGE_BOOT_UPDATE)) {
if (!ota_bootloader_self_update_caps_valid(current_caps)) {
strcpy(msg, "installed bootloader cannot safely self-update from internal flash"); return false;
}
@@ -1303,10 +1301,23 @@ bool ota_prepare_bootloader_update_nrf52(OtaStoreFlashNrf52& store,
!ota_bootloader_identity_matches(installed, candidate) ||
!ota_bootloader_caps_from_image(
image, OTA_BOOT_IMAGE_SIZE,
OTA_BL_STORAGE_STAGE_CEILING | OTA_BL_STORAGE_BOOT_UPDATE,
current_caps.storage_flags,
candidate_caps)) {
strcpy(msg, "candidate bootloader identity/capability/CRC mismatch"); return false;
}
if (current_caps.storage_flags == OTA_BL_PROFILE_RAK_AUTO_BOOT_UPDATE &&
(!ota_bootloader_supports_hybrid(ota_ram_caps_scan_aligned(image, OTA_BOOT_IMAGE_SIZE)) ||
ota_boot_rd32(image) > MOTA_NRF52_HYBRID_RAM_START)) {
strcpy(msg, "candidate adaptive bootloader must retain the hybrid RAM arena"); return false;
}
#if defined(OTA_RAK_AUTO_STORE)
const uint8_t optional_storage = ota_bl_optional_app_storage(
(const uint8_t*)(uintptr_t)MOTA_NRF52_BL_START, OTA_BOOT_IMAGE_SIZE);
if (optional_storage != 0 &&
ota_bl_optional_app_storage(image, OTA_BOOT_IMAGE_SIZE) != optional_storage) {
strcpy(msg, "candidate must retain optional RAK application storage"); return false;
}
#endif
const OtaBootloaderContinuityGate continuity = ota_bootloader_continuity_gate(
installed, candidate, m.fw_version, OTA_BOOT_CONTINUITY_FAMILY_S140,
ota_runtime_softdevice_fwid(), mota_nrf52_app_base(),
+39 -2
View File
@@ -28,6 +28,7 @@ struct OtaBlCaps {
uint16_t apply_abi = 0; // max .mota format_ver the bootloader can apply
uint16_t codec_mask = 0; // bit i set => can apply codec_id i (in-place delta = bit 2)
uint8_t storage_flags = 0; // OTA_BL_STORAGE_* capability bits
uint8_t optional_app_storage = 0; // separate MOTASTOR record, never a boot-update backend
};
// Independent marker for the reset-retained hybrid source. Keep this separate
@@ -93,6 +94,8 @@ static const uint8_t OTA_BL_PROFILE_INTERNAL_BOOT_UPDATE =
static const uint8_t OTA_BL_PROFILE_QSPI_BOOT_UPDATE =
OTA_BL_STORAGE_STAGE_CEILING | OTA_BL_STORAGE_QSPI |
OTA_BL_STORAGE_BOOT_UPDATE;
static const uint8_t OTA_BL_PROFILE_RAK_AUTO_BOOT_UPDATE =
OTA_BL_PROFILE_QSPI_BOOT_UPDATE | OTA_BL_STORAGE_HEADER_W25;
// A successor must retain both application update paths used by qualified
// external stores: bit 0 CODEC_FULL and bit 2 CODEC_DETOOLS_INPLACE.
static const uint16_t OTA_BL_REQUIRED_APP_CODEC_MASK = 0x0005u;
@@ -116,10 +119,15 @@ inline uint8_t ota_bootloader_update_storage_flags() {
inline bool ota_bootloader_self_update_caps_valid(const OtaBlCaps& c) {
const uint8_t required = ota_bootloader_update_storage_flags();
const bool profile_ok = c.storage_flags == required
#if defined(OTA_INTERNAL_BOOTLOADER_UPDATE) && defined(OTA_RAK_AUTO_STORE)
|| c.storage_flags == OTA_BL_PROFILE_RAK_AUTO_BOOT_UPDATE
#endif
;
return required != 0 && c.present && c.apply_abi >= 3u &&
(c.codec_mask & OTA_BL_REQUIRED_APP_CODEC_MASK) ==
OTA_BL_REQUIRED_APP_CODEC_MASK &&
c.storage_flags == required;
profile_ok;
}
// Prefer a continuity-capable marker over a numerically newer legacy-looking candidate. Bootloader
@@ -209,8 +217,30 @@ inline OtaBlCaps ota_bl_legacy_app_caps_scan_halfword(const uint8_t* bytes,
return c;
}
// Unified RAK loaders preserve the deployed board's privileged MOTABLDR=0x0A
// contract. A separate record advertises optional APPLICATION storage. It is
// adjacent to the unchanged, unique 64 KiB RAM marker and grants no new
// bootloader staging backend.
inline uint8_t ota_bl_optional_app_storage(const uint8_t* bytes, size_t len) {
static const uint8_t record[32] = {
'M', 'O', 'T', 'A', 'R', 'A', 'M', 'A', 1, 0, 72, 0, 0, 0, 1, 0,
'M', 'O', 'T', 'A', 'S', 'T', 'O', 'R', 1, 0, 16, 0, 0x14, 0, 0, 0,
};
if (!bytes || !ota_bootloader_supports_hybrid(ota_ram_caps_scan_aligned(bytes, len))) return 0;
unsigned matches = 0;
for (size_t off = 0; off + sizeof(record) <= len; off += 4) {
if (memcmp(bytes + off, record, sizeof(record)) == 0 && ++matches > 1) return 0;
}
return matches == 1 ? OTA_BL_STORAGE_QSPI | OTA_BL_STORAGE_HEADER_W25 : 0;
}
inline OtaBlCaps ota_bl_app_caps_scan(const uint8_t* bytes, size_t len) {
const OtaBlCaps aligned = ota_bl_caps_scan_aligned(bytes, len, false);
OtaBlCaps aligned = ota_bl_caps_scan_aligned(bytes, len, false);
if (aligned.present && aligned.apply_abi >= 3 &&
aligned.storage_flags == OTA_BL_PROFILE_INTERNAL_BOOT_UPDATE) {
aligned.optional_app_storage = ota_bl_optional_app_storage(bytes, len);
aligned.storage_flags |= aligned.optional_app_storage;
}
if (aligned.present) return aligned;
return ota_bl_legacy_app_caps_scan_halfword(bytes, len);
}
@@ -255,8 +285,15 @@ inline OtaBlCaps ota_bootloader_update_caps() {
defined(OTA_SD_BOOTLOADER_UPDATE))
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;
#if defined(OTA_INTERNAL_BOOTLOADER_UPDATE) && defined(OTA_RAK_AUTO_STORE)
// Unified RAK applications support the existing internal board identity as
// well as the adaptive successor. Both require one privileged marker.
const OtaBlCaps caps = ota_bl_caps_scan_aligned(lo, (size_t)(hi - lo), true);
return ota_bootloader_self_update_caps_valid(caps) ? caps : OtaBlCaps();
#else
return ota_bl_update_caps_scan_aligned(
lo, (size_t)(hi - lo), ota_bootloader_update_storage_flags());
#endif
#else
return OtaBlCaps();
#endif
+2 -2
View File
@@ -360,7 +360,7 @@ struct OtaContext {
const OtaBootloaderIdentity& installed = bootloaderIdentity();
#if defined(OTA_RAK_AUTO_STORE)
if (!fetch_store.usesInternal()) {
strncpy(msg, "bootloader LoRa update needs the internal OTAFIX profile", 96);
strncpy(msg, "bootloader package must be staged in internal flash", 96);
msg[95] = 0; return false;
}
bool ok = ota_prepare_bootloader_update_nrf52(
@@ -704,7 +704,7 @@ struct OtaContext {
(defined(OTA_QSPI_BOOTLOADER_UPDATE) || defined(OTA_INTERNAL_BOOTLOADER_UPDATE) || \
defined(OTA_SD_BOOTLOADER_UPDATE))
#if defined(OTA_RAK_AUTO_STORE)
manager.set_accept_bootloader(fetch_store.usesInternal() &&
manager.set_accept_bootloader(
ota_bootloader_self_update_caps_valid(ota_bootloader_update_caps()));
#else
manager.set_accept_bootloader(true);
+5 -2
View File
@@ -15,11 +15,14 @@ inline RakStorageChoice rak_storage_choice(uint8_t detected,
bool qspi_bootloader,
bool identity_valid,
const char* device_name,
bool rak3401) {
bool rak3401,
bool optional_app_storage = false) {
if (detected > 3u || detected == 3u) return RakStorageChoice::Unsafe;
const char* auto_name = rak3401 ? "3401_AUTO_DFU" : "4631_AUTO_DFU";
const char* board_name = rak3401 ? "3401_DFU" : "4631_DFU";
const bool merged_bootloader = identity_valid && device_name &&
strcmp(device_name, auto_name) == 0;
(strcmp(device_name, auto_name) == 0 ||
(optional_app_storage && strcmp(device_name, board_name) == 0));
if (detected == 0u) return !qspi_bootloader || merged_bootloader
? RakStorageChoice::Internal : RakStorageChoice::Unsafe;
if (!qspi_bootloader) return RakStorageChoice::Internal;
+47 -5
View File
@@ -14,12 +14,14 @@ namespace mesh {
namespace ota {
// One RAK application image supports the board's internal OTA store and its
// explicitly matched external NOR. Selection is fixed for the lifetime of an
// app boot; no staged container is ever moved between backends.
// explicitly matched external NOR. Application placement follows the detected
// hardware. Bootloader packages always use internal flash; select the backend
// before begin(), never move a partially fetched container between stores.
class OtaStoreAdaptiveNrf52 : public OtaStore {
enum Mode : uint8_t { UNSELECTED, INTERNAL_MODE, QSPI_MODE, UNSAFE };
mutable Mode _mode = UNSELECTED;
mutable const char* _reason = "not probed";
bool _bootloader_store = false;
// Only one backend is usable for a given board/bootloader pairing. Sharing
// their two flash-page buffers saves 8 KiB of scarce nRF52840 runtime RAM.
union StoreStorage {
@@ -39,6 +41,26 @@ class OtaStoreAdaptiveNrf52 : public OtaStore {
_mode = QSPI_MODE;
}
bool selectBootloader() {
if (!ota_bootloader_self_update_caps_valid(ota_bootloader_update_caps())) return false;
if (_mode != INTERNAL_MODE) {
if (_mode == QSPI_MODE) _storage.external.~OtaStoreQspiNrf52();
activateInternal();
}
_bootloader_store = true;
_reason = "bootloader package in internal flash";
return true;
}
void selectApplication() {
if (_bootloader_store) {
_storage.internal.~OtaStoreFlashNrf52();
_mode = UNSELECTED;
_bootloader_store = false;
}
select();
}
void select() const {
if (_mode != UNSELECTED) return;
const uint8_t detected = OtaStoreQspiNrf52::autoDetect();
@@ -66,7 +88,8 @@ class OtaStoreAdaptiveNrf52 : public OtaStore {
#endif
const RakStorageChoice choice = rak_storage_choice(
detected, qspi_bootloader, identity_valid,
identity_valid ? identity.device_name : nullptr, rak3401);
identity_valid ? identity.device_name : nullptr, rak3401,
caps.optional_app_storage == (OTA_BL_STORAGE_QSPI | OTA_BL_STORAGE_HEADER_W25));
if (choice == RakStorageChoice::Internal) {
activateInternal();
_reason = detected == 0u ? "no external NOR" :
@@ -138,12 +161,31 @@ public:
}
bool finalize() override { OtaStore* s = active(); return s && s->finalize(); }
void checkpoint() override { OtaStore* s = active(); if (s) s->checkpoint(); }
bool reopen() override { OtaStore* s = active(); return s && s->reopen(); }
bool reopen() override { return reopenFor(nullptr, 0u); }
bool reopenFor(const uint8_t* mid, uint32_t target) override {
OtaStore* s = active(); return s && s->reopenFor(mid, target);
selectApplication();
OtaStore* s = active();
if (s && s->reopenFor(mid, target)) return true;
// Only an explicit MID pull may resume privileged data. Automatic resume
// stays on the application backend. Validate kind before adopting this
// alternative store; the manager repeats all manifest and block checks.
if (!mid || _mode == INTERNAL_MODE || !selectBootloader()) return false;
uint8_t raw[MOTA_MFL];
MotaManifest manifest;
if (_storage.internal.reopenFor(mid, target) &&
_storage.internal.read(8u, raw, sizeof(raw)) &&
mota_parse_manifest(raw, sizeof(raw), manifest) && manifest.is_bootloader())
return true;
selectApplication();
return false;
}
bool plan_layout(bool full, uint32_t image, uint32_t payload_off,
uint32_t payload_size, bool bootloader) override {
if (bootloader) {
if (!selectBootloader()) return false;
} else {
selectApplication();
}
OtaStore* s = active();
return s && s->plan_layout(full, image, payload_off, payload_size, bootloader);
}