Warning
This document is not updated for ESP32C5 yet, so some of the content may not be correct.
This warning was automatically inserted due to the source file being in the add_warnings_pages list.
NVS Partition Generator Utility
Introduction
The utility nvs_flash/nvs_partition_generator/nvs_partition_gen.py creates a binary file, compatible with the NVS architecture defined in Non-Volatile Storage Library, based on the key-value pairs provided in a CSV file.
This utility is ideally suited for generating a binary blob, containing data specific to ODM/OEM, which can be flashed externally at the time of device manufacturing. This allows manufacturers to generate many instances of the same application firmware with customized parameters for each device, such as a serial number.
Prerequisites
- To use this utility in encryption mode, install the following packages:
cryptography
All the required packages are included in requirements.txt in the root of the ESP-IDF directory.
CSV File Format
Each line of a CSV file should contain 4 parameters, separated by a comma. The table below describes each of these parameters.
No. |
Parameter |
Description |
Notes |
---|---|---|---|
1 |
Key |
Key of the data. The data can be accessed later from an application using this key. |
|
2 |
Type |
Supported values are |
|
3 |
Encoding |
Supported values are: |
As of now, for the |
4 |
Value |
Data value |
|
Note
The first line of the CSV file should always be the column header and it is not configurable.
Below is an example dump of such a CSV file:
key,type,encoding,value <-- column header
namespace_name,namespace,, <-- First entry should be of type "namespace"
key1,data,u8,1
key2,file,string,/path/to/file
Note
- Make sure there are no spaces:
before and after ','
at the end of each line in a CSV file
NVS Entry and Namespace Association
When a namespace entry is encountered in a CSV file, each following entry will be treated as part of that namespace until the next namespace entry is found. At this point, all the following entries will be treated as part of the new namespace.
Note
First entry in a CSV file should always be a namespace
entry.
Multipage Blob Support
By default, binary blobs are allowed to span over multiple pages and are written in the format mentioned in Section Structure of Entry. If you intend to use the older format, the utility provides an option to disable this feature.
Encryption-Decryption Support
The NVS Partition Generator utility also allows you to create an encrypted binary file and decrypt an encrypted one. The utility uses the XTS-AES encryption. Please refer to NVS Encryption for more details.
Running the Utility
Usage:
python nvs_partition_gen.py [-h] {generate,generate-key,encrypt,decrypt} ...
Optional Arguments:
No. |
Parameter |
Description |
---|---|---|
1 |
|
Show the help message and exit |
Commands:
Run
nvs_partition_gen.py {command} -h
for additional help
No. |
Parameter |
Description |
---|---|---|
1 |
|
Generate NVS partition |
2 |
|
Generate keys for encryption |
3 |
|
Generate NVS encrypted partition |
4 |
|
Decrypt NVS encrypted partition |
Generate NVS Partition (Default)
Usage:
python nvs_partition_gen.py generate [-h] [--version {1,2}] [--outdir OUTDIR] input output size
Positional Arguments:
Parameter |
Description |
---|---|
|
Path to CSV file to parse |
|
Path to output NVS binary file |
|
Size of NVS partition in bytes (must be multiple of 4096) |
Optional Arguments:
Parameter |
Description |
---|---|
|
Show the help message and exit |
|
Set multipage blob version (Default: Version 2) Version 1 - Multipage blob support disabled Version 2 - Multipage blob support enabled |
|
Output directory to store file created (Default: current directory) |
You can run the utility to generate NVS partition using the command below. A sample CSV file is provided with the utility:
python nvs_partition_gen.py generate sample_singlepage_blob.csv sample.bin 0x3000
Generate Encryption Keys Partition
Usage:
python nvs_partition_gen.py generate-key [-h] [--keyfile KEYFILE] [--outdir OUTDIR]
Optional Arguments:
Parameter |
Description |
---|---|
|
Show the help message and exit |
|
Path to output encryption keys file |
|
Output directory to store files created. (Default: current directory) |
You can run the utility to generate only the encryption key partition using the command below:
python nvs_partition_gen.py generate-key
Generate Encrypted NVS Partition
Usage:
python nvs_partition_gen.py encrypt [-h] [--version {1,2}] [--keygen]
[--keyfile KEYFILE] [--inputkey INPUTKEY] [--outdir OUTDIR]
input output size
Positional Arguments:
Parameter |
Description |
---|---|
|
Path to CSV file to parse |
|
Path to output NVS binary file |
|
Size of NVS partition in bytes (must be multiple of 4096) |
Optional Arguments:
Parameter |
Description |
---|---|
|
Show the help message and exit |
|
Set multipage blob version (Default: Version 2) Version 1 - Multipage blob support disabled Version 2 - Multipage blob support enabled |
|
Generates key for encrypting NVS partition |
|
Path to output encryption keys file |
|
File having key for encrypting NVS partition |
|
Output directory to store file created (Default: current directory) |
You can run the utility to encrypt NVS partition using the command below. A sample CSV file is provided with the utility:
Encrypt by allowing the utility to generate encryption keys:
python nvs_partition_gen.py encrypt sample_singlepage_blob.csv sample_encr.bin 0x3000 --keygen
Note
Encryption key of the format
<outdir>/keys/keys-<timestamp>.bin
is created.
Encrypt by allowing the utility to generate encryption keys and store it in provided custom filename:
python nvs_partition_gen.py encrypt sample_singlepage_blob.csv sample_encr.bin 0x3000 --keygen --keyfile sample_keys.bin
Note
Encryption key of the format
<outdir>/keys/sample_keys.bin
is created.This newly created file having encryption keys in
keys/
directory is compatible with NVS key-partition structure. Refer to NVS Key Partition for more details.
Encrypt by providing the encryption keys as input binary file:
python nvs_partition_gen.py encrypt sample_singlepage_blob.csv sample_encr.bin 0x3000 --inputkey sample_keys.bin
Decrypt Encrypted NVS Partition
Usage:
python nvs_partition_gen.py decrypt [-h] [--outdir OUTDIR] input key output
Positional Arguments:
Parameter |
Description |
---|---|
|
Path to encrypted NVS partition file to parse |
|
Path to file having keys for decryption |
|
Path to output decrypted binary file |
Optional Arguments:
Parameter |
Description |
---|---|
|
Show the help message and exit |
|
Output directory to store files created. (Default: current directory) |
You can run the utility to decrypt encrypted NVS partition using the command below:
python nvs_partition_gen.py decrypt sample_encr.bin sample_keys.bin sample_decr.bin
- You can also provide the format version number:
Multipage blob support disabled (Version 1)
Multipage blob support enabled (Version 2)
Multipage Blob Support Disabled (Version 1)
You can run the utility in this format by setting the version parameter to 1, as shown below. A sample CSV file for the same is provided with the utility:
python nvs_partition_gen.py generate sample_singlepage_blob.csv sample.bin 0x3000 --version 1
Multipage Blob Support Enabled (Version 2)
You can run the utility in this format by setting the version parameter to 2, as shown below. A sample CSV file for the same is provided with the utility:
python nvs_partition_gen.py generate sample_multipage_blob.csv sample.bin 0x4000 --version 2
Note
Minimum NVS Partition Size needed is 0x3000 bytes.
When flashing the binary onto the device, make sure it is consistent with the application's sdkconfig.
Caveats
Utility does not check for duplicate keys and will write data pertaining to both keys. You need to make sure that the keys are distinct.
Once a new page is created, no data will be written in the space left on the previous page. Fields in the CSV file need to be ordered in such a way as to optimize memory.
64-bit datatype is not yet supported.