Skip to content

fs: nvs: fix invalid block compare when data CRC is enabled #90311

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

yolaval
Copy link
Contributor

@yolaval yolaval commented May 22, 2025

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 (flash write is skipped and 0 is returned if matching). 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 passed to the compare function does not already have the CRC part added, while the data on flash does and the size to be compared includes the 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.

Tested locally on my NUCLEO-G491RE board.

@github-actions github-actions bot added area: File System size: XS A PR changing only a single line of code labels May 22, 2025
@github-actions github-actions bot requested review from de-nordic, Laczen and nashif May 22, 2025 05:52
@yolaval yolaval force-pushed the fix-nvs-crc-wrong-write-compare-check branch from 304591e to 8bd42c3 Compare May 22, 2025 05:57
@yolaval yolaval changed the title subsys: fs: nvs: fix invalid block compare when data CRC is enabled fs: nvs: fix invalid block compare when data CRC is enabled May 22, 2025
@de-nordic de-nordic requested a review from rghaddab May 22, 2025 11:52
@de-nordic de-nordic assigned Laczen and unassigned de-nordic May 22, 2025
@de-nordic de-nordic added the bug The issue is a bug, or the PR is fixing a bug label May 22, 2025
@de-nordic
Copy link
Collaborator

@yolaval I think this should be considered a bug. Would it be possible for you to log Zephyr issue for this and mark this PR as a fix?

@Laczen
Copy link
Collaborator

Laczen commented May 22, 2025

@yolaval, thanks for the catch.

I wouldn't consider this a bug as it merely uses flash in a less optimal way and only in case a rewrite of the same data is done (and who does that...).

@RICCIARDI-Adrien could you have a look.

@RICCIARDI-Adrien
Copy link
Contributor

Indeed, the data CRC is added in nvs_flash_data_wrt(), which is called after nvs_flash_block_cmp() has been called.
Good catch @yolaval, and thanks for the fix !

@yolaval yolaval requested a review from RICCIARDI-Adrien May 23, 2025 03:38
@yolaval yolaval force-pushed the fix-nvs-crc-wrong-write-compare-check branch from 6739cb4 to e6d35dc Compare May 23, 2025 03:47
Laczen
Laczen previously approved these changes May 23, 2025
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 <yonas.alizadeh@alfalaval.com>
@yolaval yolaval force-pushed the fix-nvs-crc-wrong-write-compare-check branch from e6d35dc to 28cf85b Compare May 23, 2025 03:55
@yolaval
Copy link
Contributor Author

yolaval commented May 23, 2025

@Laczen sorry for new force push after approval. Had to fix a trailing whitespace that had sneaked in.

Copy link

@yolaval yolaval requested a review from Laczen May 23, 2025 04:25
@de-nordic de-nordic added this to the v4.2.0 milestone May 23, 2025
@de-nordic
Copy link
Collaborator

Indeed, the data CRC is added in nvs_flash_data_wrt(), which is called after nvs_flash_block_cmp() has been called. Good catch @yolaval, and thanks for the fix !

Should there be issue logged in Zephyr for that?

@kartben kartben merged commit 440de0d into zephyrproject-rtos:main May 26, 2025
26 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: File System bug The issue is a bug, or the PR is fixing a bug size: XS A PR changing only a single line of code
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants