Skip to content

Commit 601d5ef

Browse files
de-nordicrghaddab
authored andcommitted
[nrf fromtree] fs/nvs: Switch to use flash_flatten instead of flash_erase
The NVS currently requires explict erase capability of a device to work, so usage of flash_erase has been replaced with flash_flatten. There has been additional LOG_WRN added to warn user that NVS may not efficiently work with device that do not really have erase. Currently NVS relies on devices that require erase. Signed-off-by: Dominik Ermel <[email protected]> (cherry picked from commit bf7d251)
1 parent 51246fa commit 601d5ef

File tree

3 files changed

+15
-1
lines changed

3 files changed

+15
-1
lines changed

subsys/fs/nvs/nvs.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,7 @@ static int nvs_flash_erase_sector(struct nvs_fs *fs, uint32_t addr)
390390
#ifdef CONFIG_NVS_LOOKUP_CACHE
391391
nvs_lookup_cache_invalidate(fs, addr >> ADDR_SECT_SHIFT);
392392
#endif
393-
rc = flash_erase(fs->flash_device, offset, fs->sector_size);
393+
rc = flash_flatten(fs->flash_device, offset, fs->sector_size);
394394

395395
if (rc) {
396396
return rc;

tests/subsys/fs/nvs/src/main.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,18 @@ ZTEST_F(nvs, test_nvs_corrupted_write)
207207
&flash_max_write_calls);
208208
stats_walk(fixture->sim_stats, flash_sim_write_calls_find, &flash_write_stat);
209209

210+
#if defined(CONFIG_FLASH_SIMULATOR_EXPLICIT_ERASE)
210211
*flash_max_write_calls = *flash_write_stat - 1;
212+
#else
213+
/* When there is no explicit erase, erase is done with write, which means
214+
* that there are more writes needed. The nvs_write here will cause erase
215+
* to be called, which in turn calls the flash_fill; flash_fill will
216+
* overwrite data using buffer of size CONFIG_FLASH_FILL_BUFFER_SIZE,
217+
* and then two additional real writes are allowed.
218+
*/
219+
*flash_max_write_calls = (fixture->fs.sector_size /
220+
CONFIG_FLASH_FILL_BUFFER_SIZE) + 2;
221+
#endif
211222
*flash_write_stat = 0;
212223

213224
/* Flash simulator will lose part of the data at the end of this write.

tests/subsys/fs/nvs/testcase.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ tests:
66
filesystem.nvs.0x00:
77
extra_args: DTC_OVERLAY_FILE=boards/qemu_x86_ev_0x00.overlay
88
platform_allow: qemu_x86
9+
filesystem.nvs.sim.no_erase:
10+
extra_args: CONFIG_FLASH_SIMULATOR_EXPLICIT_ERASE=n
11+
platform_allow: qemu_x86
912
filesystem.nvs.cache:
1013
extra_args:
1114
- CONFIG_NVS_LOOKUP_CACHE=y

0 commit comments

Comments
 (0)