Skip to content

Commit

Permalink
fs: nvs: Add recovery path for corrupted NVS
Browse files Browse the repository at this point in the history
When all sectors of the settings partition have some data that is not 0xFF, NVS will fail to initialize settings. Add a recovery path to erase the whole settings partition and recover. Should not happen unless there's some serious flash corruption, or possibly interrupted garbage collection. Verified by recreating the scenario with flash CLIs.

Signed-off-by: Cory Andrew Mayer <[email protected]>
  • Loading branch information
corymayer committed Dec 17, 2024
1 parent 798122e commit ed01058
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions subsys/fs/nvs/nvs.c
Original file line number Diff line number Diff line change
Expand Up @@ -781,10 +781,16 @@ static int nvs_startup(struct nvs_fs *fs)
}
}
}
/* all sectors are closed, this is not a nvs fs */
/* all sectors are closed, irreparably corrupted, erase and start over */
if (closed_sectors == fs->sector_count) {
rc = -EDEADLK;
goto end;
LOG_WRN("All sectors closed, erasing all settings...");
addr = 0U;
rc = flash_erase(fs->flash_device, 0, fs->sector_size * fs->sector_count);
if (rc) {
rc = -EIO;
goto end;
}
i = 0;
}

if (i == fs->sector_count) {
Expand Down

0 comments on commit ed01058

Please sign in to comment.