Writing a single value to NVS from a PC

JolonBMT
Posts: 3
Joined: Wed Oct 08, 2025 4:41 am

Writing a single value to NVS from a PC

Postby JolonBMT » Wed Oct 08, 2025 8:11 pm

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.

vvb333007
Posts: 71
Joined: Wed Jul 31, 2024 5:53 am
Location: Thailand
Contact:

Re: Writing a single value to NVS from a PC

Postby vvb333007 » Thu Oct 09, 2025 7:44 am

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 :)
Thanks!
Slava.

JolonBMT
Posts: 3
Joined: Wed Oct 08, 2025 4:41 am

Re: Writing a single value to NVS from a PC

Postby JolonBMT » Thu Oct 09, 2025 10:01 pm

@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...

Sprite
Espressif staff
Espressif staff
Posts: 10612
Joined: Thu Nov 26, 2015 4:08 am

Re: Writing a single value to NVS from a PC

Postby Sprite » Sun Oct 12, 2025 2:04 am

Hopefully there is no CRC check :)
There is, so sorry, that won't work.

JolonBMT
Posts: 3
Joined: Wed Oct 08, 2025 4:41 am

Re: Writing a single value to NVS from a PC

Postby JolonBMT » Sun Oct 12, 2025 8:20 pm

There is, so sorry, that won't work.
@Sprite Is there a better way to modify one value in NVS?

Sprite
Espressif staff
Espressif staff
Posts: 10612
Joined: Thu Nov 26, 2015 4:08 am

Re: Writing a single value to NVS from a PC

Postby Sprite » Mon Oct 13, 2025 2:23 am

I mean, the obvious way is to have a feature in the firmware that allows for updating the serial number...

ESP_rrtandler
Posts: 53
Joined: Wed May 31, 2023 6:54 pm

Re: Writing a single value to NVS from a PC

Postby ESP_rrtandler » Mon Oct 13, 2025 11:11 am

@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

Who is online

Users browsing this forum: Semrush [Bot] and 3 guests