From 440de0dde08d48e0e6a829511d78d623cea2f1fa Mon Sep 17 00:00:00 2001 From: Yonas Alizadeh Date: Fri, 23 May 2025 05:47:24 +0200 Subject: [PATCH] fs: nvs: fix invalid block compare when data CRC is enabled When nvs_write is called, the nvs_flash_block_cmp is used to check if the new data to be written matches the data already on flash. This check always fail when CONFIG_NVS_DATA_CRC is enabled, caused by the NVS_DATA_CRC_SIZE being added to the len parameter. The pointer to the new data does not already have the CRC part added, while the data on flash does, and the size to be compared includes CRC section. By removing the addition of NVS_DATA_CRC_SIZE to the compare size, only the data without CRC is compared, which will make the compare work in both cases. Signed-off-by: Yonas Alizadeh --- subsys/fs/nvs/nvs.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/subsys/fs/nvs/nvs.c b/subsys/fs/nvs/nvs.c index 2c7c4ff5606..2e553fdbf55 100644 --- a/subsys/fs/nvs/nvs.c +++ b/subsys/fs/nvs/nvs.c @@ -1136,7 +1136,10 @@ no_cached_entry: } else if (len + NVS_DATA_CRC_SIZE == wlk_ate.len) { /* do not try to compare if lengths are not equal */ /* compare the data and if equal return 0 */ - rc = nvs_flash_block_cmp(fs, rd_addr, data, len + NVS_DATA_CRC_SIZE); + /* note: data CRC is not taken into account here, as it has not yet been + * appended to the data buffer + */ + rc = nvs_flash_block_cmp(fs, rd_addr, data, len); if (rc <= 0) { return rc; }