0

I'm designing an application for the ESP8266 that stores certain information to a config file in SPIFFS. However, I'd like to store some information in a battery-backed NVRAM chip attached to it (to reduce flash wear for some data that changes quite frequently).

Is there some simple way to determine what version of the firmware wrote the config data in the NVRAM (56 bytes max, most of which is used by program data) so that it can tell if reading the data would be corrupted by a structure change? Ideally, the check data, whatever it will be, can be only a byte or two in size.

I have been using the __DATE__ macro and similar, but this doesn't always update if the firmware is recompiled (which may be a good thing, since we only care if the serializer code is changed, which would affect the date stored there). However, I'd prefer if the data is rebuilt each time a set of firmware is uploaded to the device.

I need some way to either store something that automatically changes with each build of the firmware (hash of the program section of flash? How?) or a trigger that runs only on the first run of the program/upload (needs to cover OTA and serial both).


The underlying issue is that, since it's embedded, I'm working on a limited set of things I can actually control or modify here. Ideally, there would be some sort of unique code in the firmware that is created and updated along with the one in the config file. However, the firmware is read-only (and wiped every time the program is flashed) so I must somehow create the firmware key off of features that already exist in it, since I can't set it after the compile/upload. If I just add magicnumber=42; EVERY compiled version will always have the value of 42, so later updates will still see the matching key. The firmware also can't set some bit to say that it has been changed, since it's deleted in its entirety without being able to run an uninstaller or something like a program could on a normal computer.

As stated above, I thus need some code (system library or compiler directive == better than low-level direct access, but both are acceptable) that is capable of somehow determining when it was compiled or creating a value that is (statistically likely to be) unique to its own binary.

  • 1
    I'm already talking about adding metadata to the config file. The problem I have is that, within an embedded architecture, how to tell if someone has replaced the system that reads the file. Obviously it will look for the metadata, and if it doesn't match or is wrong (offset changed, etc) it will reset it. The issue is that the firmware doesn't know its own name save for what's in the config... which may be wrong. I need some way for the code to identify itself uniquely based on some characteristic of the binary rather than whatever (possibly wrong) data was created and stored later. – RDragonrydr Dec 28 '19 at 4:20
  • Why not compile the name and version into the binary? And if you're using a version control system, most of them have a way to automate that. – JRobert Dec 28 '19 at 17:50
  • Unfortunately, VCS is one of those things on my "to learn about" list. And I was trying to automate it with the compiler or some API option of the ESP8266, like reading the flash or something... – RDragonrydr Dec 30 '19 at 0:42
  • Until then, there are the predefined macros __DATE__ and __TIME__ which you can compile into printable string. – JRobert Dec 30 '19 at 17:24
  • I was using that already, but am running into the annoyance of their size and the fact that they are not always updated when I build a new firmware. It also may be an implementation-defined thing, but I suspect that it also stores a copy of those strings for every time I use them, rather than one copy for the whole firmware, which is a potential memory issue. I'm already using a lot of the RAM... – RDragonrydr Dec 30 '19 at 21:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.