esp-idf/components/nvs_flash/nvs_partition_generator/README.rst

318 lines
16 KiB
ReStructuredText
Raw Normal View History

NVS Partition Generator Utility
===============================
:link_to_translation:`zh_CN:[中文]`
Introduction
------------
2022-07-07 05:30:16 -04:00
The utility :component_file:`nvs_flash/nvs_partition_generator/nvs_partition_gen.py` creates a binary file based on key-value pairs provided in a CSV file. The binary file is compatible with NVS architecture defined in :doc:`Non-Volatile Storage </api-reference/storage/nvs_flash>`. 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.
2019-06-17 02:23:52 -04:00
Prerequisites
-------------
2022-07-07 05:30:16 -04:00
2019-06-17 02:23:52 -04:00
To use this utility in encryption mode, install the following packages:
- cryptography package
2019-06-17 02:23:52 -04:00
All the required packages are included in `requirements.txt` in the root of the esp-idf directory.
2022-07-07 05:30:16 -04:00
CSV File Format
---------------
2022-07-07 05:30:16 -04:00
Each line of a CSV file should contain 4 parameters, separated by a comma. The table below provides the description for each of these parameters.
.. list-table::
:widths: 10 20 35 35
:header-rows: 1
* - No.
- Parameter
- Description
- Notes
* - 1
- Key
- Key of the data. The data can be accessed later from an application using this key.
-
2022-07-07 05:30:16 -04:00
* - 2
- Type
- Supported values are ``file``, ``data``, and ``namespace``.
-
2022-07-07 05:30:16 -04:00
* - 3
- Encoding
- Supported values are: ``u8``, ``i8``, ``u16``, ``i16``, ``u32``, ``i32``, ``u64``, ``i64``, ``string``, ``hex2bin``, ``base64``, and ``binary``. This specifies how actual data values are encoded in the resulting binary file. The difference between the ``string`` and ``binary`` encoding is that ``string`` data is terminated with a NULL character, whereas ``binary`` data is not.
- As of now, for the ``file`` type, only ``hex2bin``, ``base64``, ``string``, and ``binary`` encoding is supported.
* - 4
- Value
- Data value
- ``Encoding`` and ``Value`` cells for the ``namespace`` field type should be empty. ``Encoding`` and ``Value`` of ``namespace`` are fixed and are not configurable. Any values in these cells are ignored.
.. note:: The first line of the CSV file should always be the column header and it is not configurable.
2019-06-17 02:23:52 -04:00
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
2019-06-17 02:23:52 -04:00
.. note::
Make sure there are **no spaces**:
- before and after ','
- at the end of each line in a CSV file
2022-07-07 05:30:16 -04:00
NVS Entry and Namespace Association
-----------------------------------
2019-06-17 02:23:52 -04:00
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
----------------------
2022-07-07 05:30:16 -04:00
By default, binary blobs are allowed to span over multiple pages and are written in the format mentioned in Section :ref:`structure_of_entry`. If you intend to use an older format, the utility provides an option to disable this feature.
2019-06-17 02:23:52 -04:00
Encryption Support
-------------------
2019-06-17 02:23:52 -04:00
The NVS Partition Generator utility also allows you to create an encrypted binary file. The utility uses the XTS-AES encryption. Please refer to :ref:`nvs_encryption` for more details.
2019-06-17 02:23:52 -04:00
2019-05-07 06:06:02 -04:00
Decryption Support
-------------------
This utility allows you to decrypt an encrypted NVS binary file. The utility uses an NVS binary file encrypted using XTS-AES encryption. Please refer to :ref:`nvs_encryption` for more details.
2019-05-07 06:06:02 -04:00
2022-07-07 05:30:16 -04:00
Running the Utility
-------------------
**Usage**::
2022-07-07 05:30:16 -04:00
python nvs_partition_gen.py [-h] {generate,generate-key,encrypt,decrypt} ...
**Optional Arguments**:
+-----+------------+----------------------------------------------------------------------+
| No. | Parameter | Description |
+=====+============+======================================================================+
| 1 | -h, --help | Show this help message and exit |
2022-07-07 05:30:16 -04:00
+-----+------------+----------------------------------------------------------------------+
**Commands**::
Run nvs_partition_gen.py {command} -h for additional help
2022-07-07 05:30:16 -04:00
+-----+--------------+--------------------------------------------------------------------+
| No. | Parameter | Description |
+=====+==============+====================================================================+
| 1 | generate | Generate NVS partition |
2022-07-07 05:30:16 -04:00
+-----+--------------+--------------------------------------------------------------------+
| 2 | generate-key | Generate keys for encryption |
2022-07-07 05:30:16 -04:00
+-----+--------------+--------------------------------------------------------------------+
| 3 | encrypt | Generate NVS encrypted partition |
2022-07-07 05:30:16 -04:00
+-----+--------------+--------------------------------------------------------------------+
| 4 | decrypt | Decrypt NVS encrypted partition |
+-----+--------------+--------------------------------------------------------------------+
2022-07-07 05:30:16 -04:00
To Generate NVS Partition (Default):
2019-05-07 06:06:02 -04:00
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2022-07-07 05:30:16 -04:00
**Usage**::
python nvs_partition_gen.py generate [-h] [--version {1,2}] [--outdir OUTDIR]
2019-05-07 06:06:02 -04:00
input output size
2022-07-07 05:30:16 -04:00
**Positional Arguments**:
2022-07-07 05:30:16 -04:00
+--------------+----------------------------------------------------------------------+
| Parameter | Description |
+==============+======================================================================+
| input | Path to CSV file to parse |
2022-07-07 05:30:16 -04:00
+--------------+----------------------------------------------------------------------+
| output | Path to output NVS binary file |
2022-07-07 05:30:16 -04:00
+--------------+----------------------------------------------------------------------+
| size | Size of NVS partition in bytes (must be multiple of 4096) |
2022-07-07 05:30:16 -04:00
+--------------+----------------------------------------------------------------------+
**Optional Arguments**:
+-----------------+--------------------------------------------------------------------+
| Parameter | Description |
+=================+====================================================================+
| -h, --help | Show this help message and exit |
2022-07-07 05:30:16 -04:00
+-----------------+--------------------------------------------------------------------+
| --version {1,2} | Set multipage blob version |
| | Version 1 - Multipage blob support disabled |
| | Version 2 - Multipage blob support enabled |
| | Default: Version 2 |
| | |
2022-07-07 05:30:16 -04:00
+-----------------+--------------------------------------------------------------------+
| --outdir OUTDIR | Output directory to store files created |
| | (Default: current directory) |
2022-07-07 05:30:16 -04:00
+-----------------+--------------------------------------------------------------------+
You can run the utility to generate NVS partition using the command below. A sample CSV file is provided with the utility::
2019-05-07 06:06:02 -04:00
python nvs_partition_gen.py generate sample_singlepage_blob.csv sample.bin 0x3000
2022-07-07 05:30:16 -04:00
To Generate Only Encryption Key Partition:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
**Usage**::
2019-05-07 06:06:02 -04:00
2022-07-07 05:30:16 -04:00
python nvs_partition_gen.py generate-key [-h] [--keyfile KEYFILE]
2019-05-07 06:06:02 -04:00
[--outdir OUTDIR]
2022-07-07 05:30:16 -04:00
**Optional Arguments**:
.. list-table::
:widths: 30 70
:header-rows: 1
2022-07-07 05:30:16 -04:00
* - Parameter
- Description
2022-07-07 05:30:16 -04:00
* - -h, --help
- Show this help message and exit
* - --keyfile KEYFILE
- Path to output encryption key partition file
* - --outdir OUTDIR
- Output directory to store file created (Default: current directory)
2019-05-07 06:06:02 -04:00
You can run the utility to generate only the encryption key partition using the command below::
2019-05-07 06:06:02 -04:00
python nvs_partition_gen.py generate-key
2022-07-07 05:30:16 -04:00
To Generate Encrypted NVS Partition:
2019-05-07 06:06:02 -04:00
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2022-07-07 05:30:16 -04:00
**Usage**::
2019-05-07 06:06:02 -04:00
python nvs_partition_gen.py encrypt [-h] [--version {1,2}] [--keygen]
[--keyfile KEYFILE] [--inputkey INPUTKEY]
[--outdir OUTDIR]
input output size
2022-07-07 05:30:16 -04:00
**Positional Arguments**:
+--------------+----------------------------------------------------------------------+
| Parameter | Description |
+==============+======================================================================+
| input | Path to CSV file to parse |
2022-07-07 05:30:16 -04:00
+--------------+----------------------------------------------------------------------+
| output | Path to output NVS binary file |
2022-07-07 05:30:16 -04:00
+--------------+----------------------------------------------------------------------+
| size | Size of NVS partition in bytes (must be multiple of 4096) |
2022-07-07 05:30:16 -04:00
+--------------+----------------------------------------------------------------------+
**Optional Arguments**:
+---------------------+--------------------------------------------------------------------+
| Parameter | Description |
+=====================+====================================================================+
| -h, --help | Show this help message and exit |
| | |
2022-07-07 05:30:16 -04:00
+---------------------+--------------------------------------------------------------------+
| --version {1,2} | Set multipage blob version |
| | Version 1 - Multipage blob support disabled |
| | Version 2 - Multipage blob support enabled |
| | Default: Version 2 |
+---------------------+--------------------------------------------------------------------+
| --keygen | Generates key for encrypting NVS partition |
+---------------------+--------------------------------------------------------------------+
| --keyfile KEYFILE | Path to output encryption keys file |
+---------------------+--------------------------------------------------------------------+
| --inputkey INPUTKEY | File having key for encrypting NVS partition |
+---------------------+--------------------------------------------------------------------+
| --outdir OUTDIR | Output directory to store files created |
| | (Default: current directory) |
2022-07-07 05:30:16 -04:00
+---------------------+--------------------------------------------------------------------+
You can run the utility to encrypt NVS partition using the command below. A sample CSV file is provided with the utility:
2019-05-07 06:06:02 -04:00
- 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 following format ``<outdir>/keys/keys-<timestamp>.bin`` is created.
2019-05-07 06:06:02 -04:00
- 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 following format ``<outdir>/keys/sample_keys.bin`` is created.
2019-05-07 06:06:02 -04:00
.. note:: This newly created file having encryption keys in ``keys/`` directory is compatible with NVS key-partition structure. Refer to :ref:`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
2019-05-07 06:06:02 -04:00
2022-07-07 05:30:16 -04:00
To Decrypt Encrypted NVS Partition:
2019-05-07 06:06:02 -04:00
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2022-07-07 05:30:16 -04:00
**Usage**::
2019-05-07 06:06:02 -04:00
2022-07-07 05:30:16 -04:00
python nvs_partition_gen.py decrypt [-h] [--outdir OUTDIR] input key output
2022-07-07 05:30:16 -04:00
**Positional Arguments**:
+--------------+----------------------------------------------------------------------+
| Parameter | Description |
+==============+======================================================================+
| input | Path to encrypted NVS partition file to parse |
2022-07-07 05:30:16 -04:00
+--------------+----------------------------------------------------------------------+
| key | Path to file having keys for decryption |
2022-07-07 05:30:16 -04:00
+--------------+----------------------------------------------------------------------+
| output | Path to output decrypted binary file |
2022-07-07 05:30:16 -04:00
+--------------+----------------------------------------------------------------------+
**Optional Arguments**:
+---------------------+--------------------------------------------------------------------+
| Parameter | Description |
+=====================+====================================================================+
| -h, --help | Show this help message and exit |
+---------------------+--------------------------------------------------------------------+
| --outdir OUTDIR | Output directory to store files created |
| | (Default: current directory) |
+---------------------+--------------------------------------------------------------------+
2019-05-07 06:06:02 -04:00
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):
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2022-07-07 05:30:16 -04:00
You can run the utility in this format by setting the version parameter to 1, as shown below. A sample CSV file is provided with the utility::
python nvs_partition_gen.py generate sample_singlepage_blob.csv sample.bin 0x3000 --version 1
2019-05-07 06:06:02 -04:00
Multipage Blob Support Enabled (Version 2):
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2022-07-07 05:30:16 -04:00
You can run the utility in this format by setting the version parameter to 2, as shown below. A sample CSV file 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.*
.. note:: *When flashing the binary onto the device, make sure it is consistent with the application's sdkconfig.*
2019-05-07 06:06:02 -04:00
Caveats
-------
2022-07-07 05:30:16 -04:00
2019-06-17 02:23:52 -04:00
- 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.