It seems the ELF is much more used than the HEX file and I believe (I actually remembered before asking!) I've had the discussion before about trying to embed a crc16 into the elf and the answer was that there is no tool to do this. Something else I like about the elf is that you can embed the fuses and lock bits into it and have a single file to configure/flash a chip.
So, if there is a way to do this already (crc16 in elf), please let me know before I do the work.
What I am planning is this:
1. Embed a string in the program code using PROGMEM like this "cRcLxxxx". The "cRcL" must be unique and not appear anywhere else in the flash code.
2. Write a tool that opens the elf, reads the headers, and determines where the flash area is.
3. Once it knows where the flash area is, it looks for the cRcL signature to verify that it is found only once.
4. In case the 4 bytes following cRcL have been changed, they are rewritten with "xxxx".
5. Calculate a crc16 on the flash area.
6. Write the size into the first two bytes of the "xxxx" and write the crc into the last two bytes of the "xxxx"
You can then flash the ELF.
In the beginning of program code, it will scan the flash looking for the cRcL signature.
It will read the size and crc16.
It will then read the flash to calculate the crc16.
When it gets to the cRcL signature, it must use "xxxx" for the crc16 calculation so it will be correct.
Finally it compares the crc16 from the cRcL entry against what it calculated to see if all is well.
I can make a single utility called elfcrc16 that does this.
The advantages I can think of for this method are:
Advantages of the elf file including all things (flash, eeprom, fuse bits, lock bits) together.
You don't have to put the crc16 at the end making the flash section larger than the actual code length.
I think it would be compatible with debugging.
Any thoughts about how it could be done better? Why it might or might not work? If there are other tools that can do this already?