Page 1 of 1

Writing a single value to NVS from a PC

Posted: Wed Oct 08, 2025 8:11 pm
by JolonBMT
I have an ESP32-S3 and I've set up the partition table to have a 24K NVS partition.
One of the values in NVS is called `serialno`. It is a 32-bit serial number that should be unique to each device.
There are other values stored in NVS. Some of them are constant, but others may be set during operation of the device (such as calibration data).

If I never need to update the serial number at a later date, is there a way I can do this without affecting the other data already stored in NVS?
I've tried using

Code: Select all

parttool.py write_partition
, but that overwrites the entire partition. My ideal solution would allow me to modify just one value.

I'm thinking that it may be possible to achieve by doing something like

Code: Select all

parttool.py read_partition -n nvs --output nvs.bin
# Do something here to modify nvs.bin to have new serial number
parttool.py write_partition -n nvs --input nvs.bin
but I don't know what to do between the parttool steps. As far as I can tell, there is no way to convert nvs.bin back into a CSV which can then be easily modified with a Python script. I understand that I can convert it into JSON, but I'm also unaware of any way to convert it back to a .bin file after modifying it.

Re: Writing a single value to NVS from a PC

Posted: Thu Oct 09, 2025 7:44 am
by vvb333007
is there a way I can do this without affecting the other data already stored in NVS?
if your serialno has fixed size and some start/end markers, for example:

const char *serialno = "#313:45-19#";

then you can download NVS partition, open it in a hex editor, find your "#XXXXXX#" string and change its value. Any hex editor would do the job. Then save this binary and write it back to the ESP. This technically overwrites other values, but overwrites with their original values, so nothing changes.

Hopefully there is no CRC check :)

Re: Writing a single value to NVS from a PC

Posted: Thu Oct 09, 2025 10:01 pm
by JolonBMT
@vvb333007 That's a good idea. My preference would've been to store the serial number as an integer. I could possibly make it a uint64 and use the start and end bytes as markers.

I wish there was a way to unpack a nvs binary file back to a csv for easy modification. Perhaps I could write a tool that converts the JSON file generated by nvs_tool.py into a CSV, modify it, and then convert the CSV back into a .bin file.

I also found this tool. It has some code for generating a CRC, so maybe it's not as simple as modifying the binary file. I'll have to give this a go. I wish there was an officially supported way to do it though...

Re: Writing a single value to NVS from a PC

Posted: Sun Oct 12, 2025 2:04 am
by Sprite
Hopefully there is no CRC check :)
There is, so sorry, that won't work.

Re: Writing a single value to NVS from a PC

Posted: Sun Oct 12, 2025 8:20 pm
by JolonBMT
There is, so sorry, that won't work.
@Sprite Is there a better way to modify one value in NVS?

Re: Writing a single value to NVS from a PC

Posted: Mon Oct 13, 2025 2:23 am
by Sprite
I mean, the obvious way is to have a feature in the firmware that allows for updating the serial number...

Re: Writing a single value to NVS from a PC

Posted: Mon Oct 13, 2025 11:11 am
by ESP_rrtandler
@JolonBMT

You can upload the advanced console example to your chip, set the value of respective NVS key via console and then re-upload your application. The NVS binary will be all the time in your chip.

You can find the example in the IDF folder

examples/system/console/advanced
https://github.com/espressif/esp-idf/tr ... e/advanced