Fix nRF52 contact persistence and ExtraFS recovery

This commit is contained in:
mikecarper committed 2026-09-02 11:41:38 -07:00
1 parent 66ad8c52df
commit cb27fa8539
23 files changed
+1591 -113

No files matched your search

+19 -2
View File
@@ -277,6 +277,14 @@ void Mesh::loop() {
#endif
}
bool Mesh::hasPendingOtaApply() const {
#if defined(ENABLE_OTA) && !defined(OTA_SEEDER_ONLY)
return ota::ota_ctx().apply_pending;
#else
return false;
#endif
}
void __attribute__((noinline)) Mesh::serviceLoopMaintenance() {
if (_waiting_direct_retry_count != 0
&& millisHasNowPassed(_next_direct_retry_timeout)) {
@@ -335,8 +343,17 @@ void __attribute__((noinline)) Mesh::serviceLoopMaintenance() {
oc.apply_hard = futureMillis(15000);
} else if (millisHasNowPassed(oc.apply_at) &&
(_mgr->getOutboundTotal() == 0 || millisHasNowPassed(oc.apply_hard))) {
if (oc.bootloader_apply_pending) ota::ota_reboot_to_bootloader_update();
else ota::ota_reboot_to_apply();
if (!prepareForOtaReboot()) {
// A role-specific persistence flush failed or is in bounded
// backoff. Keep the verified apply armed without hammering storage
// on every pass through the event loop.
oc.apply_at = futureMillis(1000);
oc.apply_hard = futureMillis(15000);
} else if (oc.bootloader_apply_pending) {
ota::ota_reboot_to_bootloader_update();
} else {
ota::ota_reboot_to_apply();
}
}
}
}
+7
View File
@@ -190,6 +190,13 @@ class Mesh : public Dispatcher {
DispatcherAction forwardMultipartDirect(Packet* pkt);
protected:
// Roles with lazily persisted state can veto an OTA reset until that state
// is durable. The OTA apply remains armed and is retried later.
virtual bool prepareForOtaReboot() { return true; }
// Power-saving roles must stay runnable while the deferred OTA reboot grace
// period (or a persistence retry) is pending. On nRF52, board.sleep() has no
// timer wake and could otherwise strand an already-approved update.
bool hasPendingOtaApply() const;
DispatcherAction onRecvPacket(Packet* pkt) override;
void onTracePacketQueuedForSend(Packet* packet) override;
void onSendComplete(Packet* packet) override;
+5 -1
View File
@@ -1143,10 +1143,14 @@ void WebConfigServer::tick(uint32_t now) {
}
if (WebConfigBatch::rebootDue(_reboot_at, now)) {
// rebootNow() can return when its pre-reboot persistence flush fails.
// Consume this request first so a full filesystem cannot turn tick() into
// a tight loop of repeated flush attempts.
_reboot_at = 0;
mesh::usbLoggingPort().printf(
"WC: rebooting now (%s)\n",
_batch_reboot_armed ? "confirmed" : "fallback");
_cb->rebootNow(); // does not return
_cb->rebootNow();
}
if ((int32_t)(_diag_until - now) > 0 && (now - _diag_last) >= 1000) {
@@ -0,0 +1,64 @@
#pragma once
#include <stdint.h>
namespace mesh {
namespace storage {
static const uint32_t INTERNAL_EXTRAFS_START = 0xD4000UL;
static const uint32_t INTERNAL_EXTRAFS_SIZE = 0x19000UL;
static const uint32_t INTERNAL_EXTRAFS_BLOCK_SIZE = 128UL;
inline bool isExpectedInternalExtraFsGeometry(uint32_t start, uint32_t size,
uint32_t block_size) {
return start == INTERNAL_EXTRAFS_START
&& size == INTERNAL_EXTRAFS_SIZE
&& block_size == INTERNAL_EXTRAFS_BLOCK_SIZE;
}
inline bool isInternalExtraFsReservedByApplication(uint32_t application_end) {
return application_end <= INTERNAL_EXTRAFS_START;
}
enum class InternalSecondaryFsBootResult {
Mounted,
InitializedBlank,
PreservedNonBlank,
InitializationFailed,
};
// CustomLFS::begin() automatically formats after a failed mount. Use the base
// non-formatting mount through this policy instead: an erased factory region
// may be initialized, while nonblank media is preserved for explicit repair.
template <typename Mount, typename IsBlank, typename Format>
InternalSecondaryFsBootResult prepareInternalSecondaryFilesystem(
Mount mount, IsBlank is_blank, Format format) {
if (mount()) return InternalSecondaryFsBootResult::Mounted;
if (!is_blank()) return InternalSecondaryFsBootResult::PreservedNonBlank;
if (!format() || !mount()) {
return InternalSecondaryFsBootResult::InitializationFailed;
}
return InternalSecondaryFsBootResult::InitializedBlank;
}
enum class InternalSecondaryFsRepairResult {
Repaired,
FormatFailed,
MountFailed,
ValidationFailed,
};
// Repair is deliberately separate from the normal boot path. A filesystem
// which mounts but fails a full metadata traversal may still be forensically
// recoverable, so erasing it requires an explicit local operator action.
template <typename Format, typename Mount, typename Validate>
InternalSecondaryFsRepairResult repairInternalSecondaryFilesystem(
Format format, Mount mount, Validate validate) {
if (!format()) return InternalSecondaryFsRepairResult::FormatFailed;
if (!mount()) return InternalSecondaryFsRepairResult::MountFailed;
if (!validate()) return InternalSecondaryFsRepairResult::ValidationFailed;
return InternalSecondaryFsRepairResult::Repaired;
}
} // namespace storage
} // namespace mesh
+4 -6
View File
@@ -87,12 +87,10 @@ bool ota_apply_verify_slot(ApplyState& st) {
return st.slot_ok;
}
bool ota_apply_commit() {
bool ota_apply_arm() {
const esp_partition_t* p = esp_ota_get_next_update_partition(nullptr);
if (!p) return false;
if (esp_ota_set_boot_partition(p) != ESP_OK) return false;
esp_restart(); // does not return
return true;
return esp_ota_set_boot_partition(p) == ESP_OK;
}
// --- detools callback context -----------------------------------------------------------------
@@ -457,7 +455,7 @@ static bool parse_inplace_patch_dims(const uint8_t* payload, uint32_t payload_le
bool ota_apply_slot_info(uint32_t*, uint32_t*) { return false; }
bool ota_apply_set_manifest(const uint8_t*, uint32_t, const SignerAllowlist&, ApplyState& st) { st = ApplyState(); return false; }
bool ota_apply_verify_slot(ApplyState&) { return false; }
bool ota_apply_commit() { return false; }
bool ota_apply_arm() { return false; }
bool ota_apply_detools_mota(const uint8_t*, uint32_t, const SignerAllowlist&, ApplyState& st, char* msg) { st = ApplyState(); strcpy(msg, "use ota_apply_mota_nrf52"); return false; }
void ota_reboot_to_apply() { // public: set the apply magic + reset (does not return)
@@ -1092,7 +1090,7 @@ bool ota_prepare_bootloader_update_nrf52(OtaStoreFlashNrf52& store,
bool ota_apply_slot_info(uint32_t*, uint32_t*) { return false; }
bool ota_apply_set_manifest(const uint8_t*, uint32_t, const SignerAllowlist&, ApplyState& st) { st = ApplyState(); return false; }
bool ota_apply_verify_slot(ApplyState&) { return false; }
bool ota_apply_commit() { return false; }
bool ota_apply_arm() { return false; }
bool ota_apply_detools_mota(const uint8_t*, uint32_t, const SignerAllowlist&, ApplyState& st, char* msg) { st = ApplyState(); strcpy(msg, "unsupported"); return false; }
bool ota_apply_mota_nrf52(const uint8_t*, uint32_t, const SignerAllowlist&, ApplyState& st, char* msg) { st = ApplyState(); strcpy(msg, "unsupported"); return false; }
bool ota_rescue_mota_nrf52(const uint8_t*, uint32_t, const SignerAllowlist&, const uint8_t*,
+4 -1
View File
@@ -54,7 +54,10 @@ bool ota_apply_slot_info(uint32_t* addr, uint32_t* size); // the
bool ota_apply_set_manifest(const uint8_t* mf, uint32_t len,
const SignerAllowlist& allow, ApplyState& st); // parse + verify signature
bool ota_apply_verify_slot(ApplyState& st); // hash the slot vs image_hash
bool ota_apply_commit(); // set-boot + reboot (no return)
// Select the verified A/B slot for the next boot without rebooting inline. The
// caller must arm OtaContext::apply_pending so Mesh can drain the command reply
// and run its role-specific persistence guard before ota_reboot_to_apply().
bool ota_apply_arm();
// Apply an ESP32 A/B `.mota` (CODEC_DETOOLS_SEQUENTIAL delta or CODEC_FULL image) and arm the inactive
// slot; the caller reboots after the confirmation reply. `msg` (>=80 bytes) receives a human-readable
+9 -2
View File
@@ -1160,8 +1160,15 @@ static bool handle_dev(const char* d, char* reply, OtaContext& c) {
sprintf(reply, "slot image_hash %s (size=%u)", ok ? "MATCH" : "MISMATCH", (unsigned)c.apply_st.image_size);
} else if (strncmp(sub, "commit", 6) == 0) {
if (!c.apply_st.slot_ok) { strcpy(reply, "ERR run 'ota dev apply verify' first (slot must match)"); return true; }
ota_apply_commit(); // set boot partition + reboot; no return
strcpy(reply, "ERR commit failed (no A/B slot?)");
if (c.apply_pending) { strcpy(reply, "ERR update is already armed; reboot is pending"); return true; }
if (!ota_apply_arm()) { strcpy(reply, "ERR commit failed (no A/B slot?)"); return true; }
// Use the same deferred path as ordinary installs: it lets this reply
// drain, then Mesh invokes prepareForOtaReboot() before the reset.
c.bootloader_apply_pending = false;
c.apply_at = 0;
c.apply_hard = 0;
c.apply_pending = true;
strcpy(reply, "OK slot armed; guarded reboot pending");
} else {
strcpy(reply, "ERR ota dev apply (slot|manifest|verify|commit)");
}