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.