1

I need to modify a byte of the Hex data block coming from an EEPROM and write it back to the EEPROM. Now the problem is if I simply do that, the CRC changes. Is there any tool in Linux for manually editing intel .hex files safely?

3 Answers3

2

Intel hex files do not feature a CRC.

Here's how you calculate the line checksum

Janka
  • 13,636
  • 1
  • 19
  • 33
2

Convert it to binary, edit the binary in a hex editor, save it, convert back to hex file if the burner does not accept binary. The srecord package should be able to handle this.

Justme
  • 127,425
  • 3
  • 97
  • 261
  • 1
    "editing intel .hex files" Not srecord. They calculate checksums somewhat differently. Also you don't need a hex editor. Intel hex is an ASCII format. – Lundin Jun 07 '21 at 06:30
  • 1
    @Lundin You did not understand what I said. The Linux package called "srecord" can work with at least 40 different file formats and convert between them, including Intel HEX format. And my suggestion was how I would do it fastest - convert HEX to binary and for that you need a hex editor and then convert back to HEX. That is not the only way to do it, most likely a raw hex text format exists that can be edited as text without checksums and convert back to Intel HEX format but it would take more time (for me at least) to find such a method. If you want to edit Intel HEX directly it is possible. – Justme Jun 07 '21 at 06:51
  • 1
    Okay, sounds cumbersome. Unless the line is really long, you can just do it manually with a calculator in a minute or so... – Lundin Jun 07 '21 at 07:00
1

You can do this with any text editor. Intel hex (and Motorola s-record) is an ASCII format.

  • At the start of the line there's a : which you ignore. (On s-record there's a Sx instead, where the x is a digit.)
  • After that follows the address. Find the relevant line that you wish to change.
  • Edit the part in the data section that you wish to change by typing in new upper case hex digits to replace the old ones.
  • The checksum is the last two digits of the line. It's not a CRC but a simplistic version based on addition.
  • Calculate checksum by adding hex digit pairs, ignoring the start of the line token. For example if you have 100F then that's 10h + 0Fh = 1Fh = 31 decimal. Keep adding everything but the start of line token and the checksum.
  • Discard everything but the lowest significant byte of the sum. Then invert this byte according to two's complement (one's complement on s-record). Replace the old checksum with what you got instead.
Lundin
  • 17,577
  • 1
  • 24
  • 67