-
Notifications
You must be signed in to change notification settings - Fork 3k
Add NVStore (A.K.A SOTP) feature #5900
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
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
# NVStore | ||
|
||
NVStore is a lightweight module that stores data by keys in the internal flash for security purposes. | ||
|
||
## Description | ||
|
||
NVStore provides the ability to store a minimal set of system critical items in the internal flash. | ||
For each item key, the NVStore module provides the ability to set the item data or get it. | ||
Newly added values are added to the end of the existing data, superseding the previous value that was there for the same key. | ||
The NVStore module ensures that power failures don't harm existing data during any operation. | ||
The full interface can be found under `nvstore.h`. | ||
|
||
### Flash structure | ||
NVStore uses two Flash areas, active and nonactive. Each area must consist of at least one erasable unit (sector). | ||
Data is written to the active area until it becomes full. When it does, garbage collection is invoked. | ||
This compacts items from the active area to the nonactive one and switches activity between areas. | ||
Each item is kept in an entry containing a header and data, where the header holds the item key, size and CRC. | ||
|
||
### APIs | ||
- init: Initialize NVStore (also lazily called by get, set, set_once and remove APIs). | ||
- deinit: Deinitialize NVStore. | ||
- get: Get the value of an item, given key. | ||
- set: Set the value of an item, given key and value. | ||
- set_once: Like set, but allows only a one time setting of this item (and disables deleting of this item). | ||
- remove: Remove an item, given key. | ||
- get_item_size: Get the item value size (in bytes). | ||
- set_max_keys: Set maximal value of unique keys. Overriding the default of NVSTORE_MAX_KEYS. This affects RAM consumption, | ||
as NVStore consumes 4 bytes per unique key. Reinitializes the module. | ||
|
||
|
||
## Usage | ||
|
||
### Enabling NVStore and configuring it for your board | ||
NVStore is enabled by default for all devices with the internal flash driver (have "FLASH" in the device_has attribute). | ||
One can disable it by setting its "enabled" attribute to false. | ||
Unless specifically configured by the user, NVStore selects the last two flash sectors as its areas, with the mininum size of 4KBs, | ||
meaning that if the sectors are smaller, few continuous ones will be used for each area. | ||
The user can override this by setting the addresses and sizes of both areas in` mbed_lib.json` on a per board basis. | ||
In this case, all following four attributes need to be set: | ||
- area_1_address | ||
- area_1_size | ||
- area_2_address | ||
- area_2_size | ||
|
||
In addition, the `num_keys` value should be modified to change the default number of different keys. | ||
|
||
### Using NVStore | ||
NVStore is a singleton class, meaning that the system can have only a single instance of it. | ||
To instantiate NVStore, one needs to call its get_instance member function as following: | ||
``` c++ | ||
NVStore &nvstore = NVStore::get_instance(); | ||
``` | ||
After the NVStore instantiation, one can call the init API, but it is not necessary because all | ||
NVStore APIs (get, set and so on) perform a "lazy initialization". | ||
|
||
### Testing NVStore | ||
Run the NVStore functionality test with the `mbed` command as following: | ||
```mbed test -n features-nvstore-tests-nvstore-functionality``` |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks like a great start for the docs! Is there a copy in the handbook or is this best placed there?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks. Regarding your question about documentation in the handbook - yes, there is such copy. It is currently handled in a separate PR. There's also a new example repo (https://github.com/ARMmbed/mbed-os-example-nvstore). Note that both can't be finalized until this PR is merged in master.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you link or reference the docs PR to this one? Would like to make sure they go in together.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here it is.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
depends on ARMmbed/mbed-os-5-docs#399ARMmbed/mbed-os-5-docs#399
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isn't making this PR depend on the documentation PR problematic? As far as I managed to understand from @AnotherButler , I can only complete the Handbook PR after publishing my example code via the online compiler. I have an example repo, which points to this branch, but will only be able to build it in the online compiler after the NVStore gets into the master branch (as the online compiler doesn't see the NVStore code). So we have the chicken and egg situation here. What am I missing?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll edit the comment. I was trying to get github to make a nice link graphic but it didn't work.