Add LoRa partition migration recipes for ESP32 infrastructure boards

This commit is contained in:
mikecarper committed 2026-09-24 10:40:32 -07:00
1 parent e7f6d529a0
commit c0ef07a968
10 files changed
+288 -86

No files matched your search

+26 -14
View File
@@ -267,14 +267,17 @@ inline bool canMigrateGeneric(uint32_t flash_bytes,
&& !sameGeometry(geometry, plan->layout);
}
// A LoRa migration keeps the old OTA-capable application alive while the
// bridge restores SPIFFS identity. Legacy app0 stays at its address; legacy
// app1 is copied to the new app1 before the table is published. The bridge
// boots first, then hands off to whichever slot contains the old application.
// On 8/16 MiB flash the bridge can copy legacy app1 to its new address,
// reboot, restore the staged identity, and hand off to the old receiver.
// On 4 MiB flash that copy would overwrite legacy app1. The only safe LoRa
// route there starts with the old receiver in app0 and the bridge in app1;
// the new Full application must restore the NVS-staged identity on first boot.
struct LoRaResumePlan {
bool valid;
uint8_t bridge_slot;
uint8_t resume_slot;
bool copy_app1;
bool restore_identity_in_full;
};
constexpr bool overlaps(uint32_t a, uint32_t a_size,
@@ -288,19 +291,28 @@ inline LoRaResumePlan planLoRaResume(const PartitionGeometry& source,
uint32_t running_address) {
const bool in_app0 = running_address == source.app0_address;
const bool in_app1 = running_address == source.app1_address;
const bool common = (in_app0 || in_app1)
&& source.app0_address == target.app0_address
&& source.app0_size <= target.app0_size
&& source.app1_size <= target.app1_size
&& !overlaps(source.app0_address, source.app0_size,
source.app1_address, source.app1_size)
&& !overlaps(source.app0_address, source.app0_size,
target.app1_address, target.app1_size);
const bool can_copy = common
&& !overlaps(source.app1_address, source.app1_size,
target.app1_address, source.app1_size);
const bool direct_4mb = common && in_app1
&& target.app1_address == kExpanded4MBLayout.app1_address
&& target.app1_size == kExpanded4MBLayout.app1_size
&& !overlaps(source.app0_address, source.app0_size,
target.spiffs_address, target.spiffs_size);
return {
(in_app0 || in_app1)
&& source.app0_address == target.app0_address
&& source.app0_size <= target.app0_size
&& source.app1_size <= target.app1_size
&& !overlaps(source.app0_address, source.app0_size,
source.app1_address, source.app1_size)
&& !overlaps(source.app0_address, source.app0_size,
target.app1_address, source.app1_size)
&& !overlaps(source.app1_address, source.app1_size,
target.app1_address, source.app1_size),
can_copy || direct_4mb,
static_cast<uint8_t>(in_app0 ? 0 : 1),
static_cast<uint8_t>(in_app0 ? 1 : 0),
can_copy,
direct_4mb && !can_copy,
};
}
+44
View File
@@ -1,6 +1,9 @@
#include "IdentityStore.h"
#include "FilePresence.h"
#if defined(ESP32_PLATFORM)
#include <Preferences.h>
#endif
#if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM)
#include "AtomicFileWriter.h"
#else
@@ -43,6 +46,47 @@ IdentityLoadResult IdentityStore::loadResult(
char filename[40];
if (snprintf(filename, sizeof(filename), "%s/%s.id", _dir, name)
>= (int)sizeof(filename)) return IdentityLoadResult::Unreadable;
#if defined(ESP32_PLATFORM)
// A 4 MiB LoRa partition migration must boot the old app0 receiver before
// the new Full image can run. The bridge staged the original key in stable
// NVS; that old receiver may have formatted the newly sized SPIFFS and
// generated a temporary key. Restore the original before normal startup
// accepts either that temporary key or a missing identity.
if (strcmp(_dir, "/identity") == 0 && strcmp(name, "_main") == 0) {
Preferences migration_nvs;
if (migration_nvs.begin("mesh-pt-migrate", true)) {
const bool pending = migration_nvs.getBool("id-pending", false);
uint8_t file_bytes[PUB_KEY_SIZE + PRV_KEY_SIZE];
const size_t staged = pending
? migration_nvs.getBytes("identity", file_bytes, sizeof(file_bytes)) : 0;
migration_nvs.end();
if (pending) {
if (staged != sizeof(file_bytes)) return IdentityLoadResult::Unreadable;
uint8_t export_bytes[sizeof(file_bytes)];
memcpy(export_bytes, file_bytes + PUB_KEY_SIZE, PRV_KEY_SIZE);
memcpy(export_bytes + PRV_KEY_SIZE, file_bytes, PUB_KEY_SIZE);
mesh::LocalIdentity original;
original.readFrom(export_bytes, sizeof(export_bytes));
if (!saveWithRetry(name, original)) return IdentityLoadResult::Unreadable;
File restored = _fs->open(filename, "r");
uint8_t verified[sizeof(file_bytes)];
const bool matches = restored
&& restored.read(verified, sizeof(verified)) == sizeof(verified)
&& memcmp(file_bytes, verified, sizeof(file_bytes)) == 0;
if (restored) restored.close();
if (!matches) return IdentityLoadResult::Unreadable;
if (!migration_nvs.begin("mesh-pt-migrate", false))
return IdentityLoadResult::Unreadable;
// Clear the pending flag first: a power cut cannot then leave a
// pending flag pointing at already removed key bytes.
const bool cleared = migration_nvs.remove("id-pending");
if (cleared) migration_nvs.remove("identity");
migration_nvs.end();
if (!cleared) return IdentityLoadResult::Unreadable;
}
}
}
#endif
for (unsigned attempt = 0; attempt < IO_ATTEMPTS; ++attempt) {
if (!recover(name)) continue;
bool present = false;
+3 -1
View File
@@ -62,7 +62,9 @@ typedef bool (*ServeDeflateReadFn)(void* ctx, uint16_t block, uint8_t* dst,
uint16_t dst_cap, uint16_t* dst_len);
#ifndef OTA_PROOFGEN_SCRATCH
#if defined(OTA_SD_STORE)
#if defined(ESP32_PLATFORM)
#define OTA_PROOFGEN_SCRATCH 16384 // heap-backed; can seed <=4096 blocks (8 MiB at 2 KiB/block)
#elif defined(OTA_SD_STORE)
#define OTA_PROOFGEN_SCRATCH 8192 // SD archive can seed <=2048 blocks (about 4 MiB at 2 KiB/block)
#else
#define OTA_PROOFGEN_SCRATCH 4096 // server proof-gen working buffer (supports up to 1024 blocks)