You can pre-generate the NVS data partition and flash it together with firmware. ESP IDF provides the NVS Partition Generator Utility specifically for for that purpose. Here's an overview of the process.
- First you create a CSV file (let's name it
mfgdata.csv
) with your pre-generated data, e.g. serial number, product ID, keypair, whatever. Assume the NVS namespace is "mfgdata_ns".
key,type,encoding,value
mfgdata_ns,namespace,,
serial,data,string,"ABC1234"
private_key,file,string,/path/key.pri
- Then you generate the binary partition with NVS data on it (named
mfgdata.bin
) from this CSV file. Assume the NVS partition starts at address 0x3000.
$IDF_PATH/components/nvs_flash/nvs_partition_generator/nvs_partition_gen.py generate mfgdata.csv mfgdata.bin 0x3000
- Finally you flash the NVS partition together with your firmware. Here's a sample flashing command for the NVS partition alone (assuming the partition is named "mfgdata_part" in your partition table).
$ $IDF_PATH/components/partition_table/parttool.py -p /dev/ttyUSB0 -b 921600 write_partition --partition-name=mfgdata_part --input=mfgdata.bin
- Firmware starts up, loads the NVS namespace and finds all values that you specified in
mfgdata.csv
.
You have to be careful when creating the CSV file. Some 2 years ago they were using the python CSV module to parse this file and it worked perfectly, as expected. Then some not very bright person decided to ditch the python module and replace it by splitting each line on comma character, silently ignoring all parsing problems. I don't know if they've unf*cked it yet.