Skip to content

nvs: also support sector_size of 64KB #91389

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

mjchen0
Copy link
Contributor

@mjchen0 mjchen0 commented Jun 11, 2025

Allows NVS to work with flash device configured to use only 64KB block erase.

@github-actions github-actions bot added area: File System size: XS A PR changing only a single line of code labels Jun 11, 2025
@github-actions github-actions bot requested review from de-nordic, Laczen and nashif June 11, 2025 01:13
@Laczen
Copy link
Contributor

Laczen commented Jun 11, 2025

Hi @mjchen0, the limitation of NVS to smaller sector sizes is an internal property. The proposed change does not enable the use of larger sectors.

@de-nordic de-nordic assigned Laczen and unassigned de-nordic Jun 11, 2025
@mjchen0
Copy link
Contributor Author

mjchen0 commented Jun 11, 2025

Hi @mjchen0, the limitation of NVS to smaller sector sizes is an internal property. The proposed change does not enable the use of larger sectors.

Sorry, my title wasn't correct (I've changed it). I wasn't trying to support arbitrarily large sectors. I was just trying to make NVS work with FLASH that has only 64KB erase units. NVS seems to have a max sector size of 64KB. It's not described in the documentation of nvs.rts, but appears due to the representation of address as described in nvs_priv.h:

/*                                                                                                                                 
 * MASKS AND SHIFT FOR ADDRESSES                                                                                                   
 * an address in nvs is an uint32_t where:                                                                                         
 *   high 2 bytes represent the sector number                                                                                      
 *   low 2 bytes represent the offset in a sector                                                                                  
 */                                                                                                                                
#define ADDR_SECT_MASK 0xFFFF0000                                                                                                  
#define ADDR_SECT_SHIFT 16                                                                                                         
#define ADDR_OFFS_MASK 0x0000FFFF 

With 16-bits for offset, a 64KB sector size is possible (offset 0 to 0xffff) except that the sector_size field itself is uint16_t so 64KB couldn't be stored as the size.

@Laczen
Copy link
Contributor

Laczen commented Jun 11, 2025

Hi @mjchen0, the limitation of NVS to smaller sector sizes is an internal property. The proposed change does not enable the use of larger sectors.

Sorry, my title wasn't correct (I've changed it). I wasn't trying to support arbitrarily large sectors. I was just trying to make NVS work with FLASH that has only 64KB erase units. NVS seems to have a max sector size of 64KB. It's not described in the documentation of nvs.rts, but appears due to the representation of address as described in nvs_priv.h:

/*                                                                                                                                 
 * MASKS AND SHIFT FOR ADDRESSES                                                                                                   
 * an address in nvs is an uint32_t where:                                                                                         
 *   high 2 bytes represent the sector number                                                                                      
 *   low 2 bytes represent the offset in a sector                                                                                  
 */                                                                                                                                
#define ADDR_SECT_MASK 0xFFFF0000                                                                                                  
#define ADDR_SECT_SHIFT 16                                                                                                         
#define ADDR_OFFS_MASK 0x0000FFFF 

With 16-bits for offset, a 64KB sector size is possible (offset 0 to 0xffff) except that the sector_size field itself is uint16_t so 64KB couldn't be stored as the size.

Ok, makes sense. Could you extend the PR with a check that sector_size is <= 64kB ? Also change the title to reflect "<=64kB".

@mjchen0 mjchen0 force-pushed the nvs_64kb_erase_blocks branch from 3a7f692 to 4734e23 Compare June 11, 2025 23:57
@mjchen0
Copy link
Contributor Author

mjchen0 commented Jun 11, 2025

I added the test in nvs_mount() that the sector_size isn't greater than 64KB.

I also added a new test case for native_sim with 64KB erase blocks, and a test that the nvs_mount() returns the expected error for a bad sector_size value.

Laczen
Laczen previously approved these changes Jun 12, 2025
Copy link
Contributor

@Laczen Laczen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks

@Laczen Laczen changed the title nvs: support sector_size > 64KB nvs: also support sector_size of 64KB Jun 12, 2025
@mjchen0 mjchen0 force-pushed the nvs_64kb_erase_blocks branch from 4734e23 to 8b325ed Compare June 16, 2025 18:09
Allows NVS to work with flash device configured to
use only 64KB block erase. Due to how addresses
are encoded internally in NVS, 64KB is the maximum
sector size. Add a test for this during mount.

Add a native_sim unit test case for 64kb erase block size

Signed-off-by: Mike J. Chen <mjchen@google.com>
@mjchen0 mjchen0 force-pushed the nvs_64kb_erase_blocks branch from 8b325ed to 9ab7231 Compare June 16, 2025 18:56
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: File System size: XS A PR changing only a single line of code
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants