Automatically recover internal Companion ExtraFS on boot

This commit is contained in:
mikecarper committed 2026-09-03 15:15:23 -07:00
1 parent 1c91f82ec0
commit 73f10f3fc3
7 files changed
+379 -70

No files matched your search

+55 -3
View File
@@ -105,13 +105,18 @@ void DataStore::begin() {
_fsExtra = nullptr;
return;
}
#if defined(EXTRAFS) && !defined(QSPIFLASH)
// Validate primary first: automatic secondary recovery must never hide a
// primary/identity fault. Retry or rebuild before loading any RAM state.
recoverInternalExtraFSOnBoot();
#else
if (_fsExtra != nullptr && !validateLfsFilesystem(_fsExtra)) {
// A traversal-failed filesystem may still be recoverable offline. Keep
// both removable QSPI and managed on-chip ExtraFS intact until an explicit
// local repair/factory-reset operation authorizes erasing it.
// Automatic destructive recovery is restricted to reserved internal
// ExtraFS. Removable/external QSPI still requires an explicit repair.
MESH_DEBUG_PRINTLN("DataStore: secondary LittleFS metadata is corrupt; preserving it and using primary storage");
disableSecondaryFS(true);
}
#endif
if (primary_ready) cleanupAtomicTempFiles(_fs);
if (_fsExtra != nullptr) cleanupAtomicTempFiles(_fsExtra);
#endif
@@ -210,6 +215,53 @@ static void cleanupAtomicTempFiles(FILESYSTEM* fs) {
}
#if defined(EXTRAFS) && !defined(QSPIFLASH)
bool DataStore::recoverInternalExtraFSOnBoot() {
if (_configuredFsExtra == nullptr) return false;
CustomLFS* extra = static_cast<CustomLFS*>(_configuredFsExtra);
if (_primary_storage_unavailable
|| !mesh::storage::isExpectedInternalExtraFsGeometry(
extra->getFlashAddr(), extra->getFlashSize(), extra->getBlockSize())
|| !mesh::storage::isInternalExtraFsReservedByApplication(
(uint32_t)(uintptr_t)__flash_arduino_end)) {
disableSecondaryFS(true);
MESH_DEBUG_PRINTLN("DataStore: refusing automatic ExtraFS recovery outside reserved 100 KiB region");
return false;
}
const mesh::storage::InternalSecondaryFsRecoveryResult result =
mesh::storage::recoverInternalSecondaryFilesystem(
[this]() -> bool {
return _fsExtra == _configuredFsExtra
&& validateLfsFilesystem(_fsExtra);
},
[this, extra]() -> bool {
MESH_DEBUG_PRINTLN("DataStore: retrying internal ExtraFS mount before recovery");
_fsExtra = nullptr;
extra->end();
if (!extra->Adafruit_LittleFS::begin()) return false;
_fsExtra = _configuredFsExtra;
return true;
},
[this]() -> bool {
MESH_DEBUG_PRINTLN("DataStore: internal ExtraFS remains unusable; rebuilding 100 KiB (secondary-only data may be lost)");
return reinitializeInternalExtraFS();
});
if (result == mesh::storage::InternalSecondaryFsRecoveryResult::Failed) {
disableSecondaryFS(true);
return false;
}
// This runs only before migration and user-data loading. Clear the initial
// mount quarantine, not errors from a later incomplete contact/prefs load.
// Migration below establishes authority again and can re-latch any error.
_secondary_authority_unknown = false;
_contact_load_incomplete = false;
_identity_creation_blocked = false;
_prefs_load_incomplete = false;
return true;
}
bool DataStore::reinitializeInternalExtraFS() {
if (_configuredFsExtra == nullptr) return false;
+3 -2
View File
@@ -19,8 +19,8 @@ class DataStore {
FILESYSTEM* _fs;
FILESYSTEM* _fsExtra;
// Keep the configured secondary even when normal I/O falls back to the
// primary filesystem. An explicit local repair/factory reset must still be
// able to address the inactive on-chip ExtraFS.
// primary filesystem. Boot recovery and explicit local repair must still
// be able to address the inactive on-chip ExtraFS.
FILESYSTEM* _configuredFsExtra;
mesh::RTCClock* _clock;
IdentityStore identity_store;
@@ -47,6 +47,7 @@ class DataStore {
bool truncateLegacyContacts(uint16_t remaining_contacts);
void resetContactPageState(bool clear_incomplete = false);
#if defined(EXTRAFS) && !defined(QSPIFLASH)
bool recoverInternalExtraFSOnBoot();
bool reinitializeInternalExtraFS();
#endif
#endif
+5 -35
View File
@@ -2147,46 +2147,16 @@ void setup() {
ExtraFS.getBlockSize())
&& mesh::storage::isInternalExtraFsReservedByApplication(
(uint32_t)(uintptr_t)__flash_arduino_end);
mesh::storage::InternalSecondaryFsBootResult extra_fs_boot =
mesh::storage::InternalSecondaryFsBootResult::InitializationFailed;
if (extra_fs_geometry_valid) {
extra_fs_boot = mesh::storage::prepareInternalSecondaryFilesystem(
[]() -> bool {
// Bypass CustomLFS::begin(), whose mount-failure path erases
// nonblank media before DataStore can quarantine it.
return ExtraFS.Adafruit_LittleFS::begin();
},
[]() -> bool {
const volatile uint32_t* word =
reinterpret_cast<const volatile uint32_t*>(
mesh::storage::INTERNAL_EXTRAFS_START);
for (uint32_t offset = 0;
offset < mesh::storage::INTERNAL_EXTRAFS_SIZE;
offset += sizeof(uint32_t), word++) {
if (*word != 0xFFFFFFFFUL) return false;
}
return true;
},
[]() -> bool {
ExtraFS.end();
return ExtraFS.format();
});
}
// Initial mount is always non-destructive, even for blank media.
// DataStore validates primary first, then retries/rebuilds ExtraFS.
if (!extra_fs_geometry_valid
|| extra_fs_boot == mesh::storage::InternalSecondaryFsBootResult::PreservedNonBlank
|| extra_fs_boot == mesh::storage::InternalSecondaryFsBootResult::InitializationFailed) {
|| !ExtraFS.Adafruit_LittleFS::begin()) {
if (!extra_fs_geometry_valid) {
MESH_DEBUG_PRINTLN("CustomLFS: internal ExtraFS geometry is not 100 KiB; refusing to mount or erase it");
} else if (extra_fs_boot
== mesh::storage::InternalSecondaryFsBootResult::PreservedNonBlank) {
MESH_DEBUG_PRINTLN("CustomLFS: internal ExtraFS mount failed; preserving nonblank media and using primary storage");
} else {
MESH_DEBUG_PRINTLN("CustomLFS: blank internal ExtraFS initialization failed; using primary storage");
MESH_DEBUG_PRINTLN("CustomLFS: internal ExtraFS mount failed; deferring automatic recovery until primary validation");
}
const bool secondary_authority_unknown = !extra_fs_geometry_valid
|| extra_fs_boot
== mesh::storage::InternalSecondaryFsBootResult::PreservedNonBlank;
store.disableSecondaryFS(secondary_authority_unknown);
store.disableSecondaryFS(true);
}
#endif
#endif