Author Topic: Computing CRC-16 from .hex file  (Read 8529 times)

0 Members and 1 Guest are viewing this topic.

Offline fearlessTopic starter

  • Contributor
  • Posts: 20
  • Country: us
Computing CRC-16 from .hex file
« on: February 12, 2024, 12:18:14 am »
Context: After bootloading a .hex file into our microcontroller, I want to use a CRC to verify that the program memory was properly written.  Therefore, I need to compute the CRC on the host computer prior to downloading the .hex file into the microcontroller.

The goal: given a program on the host computer that takes as inputs:
* a well-defined CRC algorithm (the actual choice of CRC is not important for this question)
* a .hex file that contains binary data to be loaded into the microcontroller, with possibly discontinuous address segments
* a starting address and number of bytes over which to run the CRC algo
... compute the CRC for that memory range.

The question: Are there any techniques or utilities that will help me compute the CRC over that address range from the .hex file?

I initially thought this would be easy: just decode each data byte in the .hex file, if it is within the address range, pass it to the CRC algo.

But then I realized that .hex files can have discontinuous sections, and since the CRC algorithms are sensitive to the order in which bytes are fed to them, out-of-order sections would spoil the computation.

The best idea I've come up with is to allocate an array on the host computer, parse the .hex file and write only those bytes within the target address range into the array.  After the entire .hex file is read, then run the CRC algo other the array. 

How would you handle this?
 

Online ataradov

  • Super Contributor
  • ***
  • Posts: 11262
  • Country: us
    • Personal site
Re: Computing CRC-16 from .hex file
« Reply #1 on: February 12, 2024, 01:09:26 am »
"Unpacking" the whole file (or just the regions of interest) is the only reliable way. You will also need to synchronize the way you will be filling gaps in your programming algorithm  (likely just keep erased state 0xff).

But the best thing you can do is normalize the HEX file and make sure that it is linear and contiguous.  You can also add CRC in the binary itself at that point, so you don't have to transfer it separately from the file.

This is one of the reasons why I will never use HEX files for anything. They are annoying to parse securely and don't have a standard way to deal with gaps and out of order addresses. They also don't add anything useful, we are not in the 80s anymore and not transferring the files on paper printouts.
« Last Edit: February 12, 2024, 01:12:06 am by ataradov »
Alex
 

Online Ian.M

  • Super Contributor
  • ***
  • Posts: 12862
Re: Computing CRC-16 from .hex file
« Reply #2 on: February 12, 2024, 01:19:52 am »
Microchip Technology have a 'Swiss Army Knife' command line utility for manipulating IntelHEX hex files.  It can do CRC16 and insert the result into the hex file. See https://developerhelp.microchip.com/xwiki/bin/view/software-tools/xc8/crc-checksum/
It can also normalise hex files.
The Hexmate utility is bundled with their XC8 compiler (and IIRC also with the MPLAB X IDE).
« Last Edit: February 12, 2024, 01:23:20 am by Ian.M »
 
The following users thanked this post: oPossum

Online ataradov

  • Super Contributor
  • ***
  • Posts: 11262
  • Country: us
    • Personal site
Re: Computing CRC-16 from .hex file
« Reply #3 on: February 12, 2024, 01:23:56 am »
Hexmate is not that universal. It has not received many updates and fails on some files generated by XC32, for example. I don't know if it will handle well arbitrary chopped up files with many sections.

And it is hard to use if your file includes sections that are not a part of the main program space (like what is described in the original post).
Alex
 

Offline S. Petrukhin

  • Super Contributor
  • ***
  • Posts: 1146
  • Country: ru
Re: Computing CRC-16 from .hex file
« Reply #4 on: February 12, 2024, 06:58:15 pm »
one of the reasons why I will never use HEX files for anything
I've never studied hex/bin in detail, but I'm wondering now - can a firmware image with address gaps be saved in bin?
And sorry for my English.
 

Online ataradov

  • Super Contributor
  • ***
  • Posts: 11262
  • Country: us
    • Personal site
Re: Computing CRC-16 from .hex file
« Reply #5 on: February 12, 2024, 07:05:36 pm »
Only with a custom format. If you are adding CRC, then you are likely using some custom tool/script anyway, so if you really need multiple sections, just use a custom binary format.

But chances are that real applications don't need multiple sections. Or if they do, then non-main sections will usually have fixed or predictable size (assuming fuses or similar), so you don't really need to add any header information, just concatenate the binaries.
Alex
 

Offline S. Petrukhin

  • Super Contributor
  • ***
  • Posts: 1146
  • Country: ru
Re: Computing CRC-16 from .hex file
« Reply #6 on: February 12, 2024, 07:26:58 pm »
That is, you do not want to print hex on paper, but manually write down starting addresses of individual bin pieces of single firmware to paper?   :)

It seems to me that hex is not so bad.
It indicates which addresses to write and data.
This allows you to record without first erasing, without disturbing the data in other addresses.

After all, it allows you to transfer your firmware to be downloaded to your device by another person and nothing else.
You do not store libraries and modules in hex, they do not need to be loaded into arbitrary devices.
There is no urgent need for standardization and global compliance.

Or do you dislike creative freedom and want to live only by standards?  :)
And sorry for my English.
 

Online ataradov

  • Super Contributor
  • ***
  • Posts: 11262
  • Country: us
    • Personal site
Re: Computing CRC-16 from .hex file
« Reply #7 on: February 12, 2024, 07:41:19 pm »
manually write down starting addresses of individual bin pieces of single firmware to paper?   :)
What are you talking about? Why would you need to write anything down? The use here is your own bootloader and update process. You know what format the data is in.

This allows you to record without first erasing, without disturbing the data in other addresses.
All flash devices have erase granularity and often minimum write size requirements. So, you will have to reassemble the binary before committing it into the flash. Having chopped up HEX file is the worst nightmare of any firmware update process.
Alex
 

Offline S. Petrukhin

  • Super Contributor
  • ***
  • Posts: 1146
  • Country: ru
Re: Computing CRC-16 from .hex file
« Reply #8 on: February 12, 2024, 07:55:58 pm »
HEX is not always used for flash and not always for the operation of a standalone programmer.

I use it to transfer the firmware update image to the embedded programmer. More precisely, the transmitter.
I get HEX from the compilator and pass pairs of address:word. I couldn't process BIN like that. And you know why.  :)
At the same time, updates are recorded in the second half of the flash without touching the first half, where the default firmware is located, to which you can return.

Of course, it is necessary to take into account the alignment and distribution of flash blocks.
And sorry for my English.
 

Online ataradov

  • Super Contributor
  • ***
  • Posts: 11262
  • Country: us
    • Personal site
Re: Computing CRC-16 from .hex file
« Reply #9 on: February 12, 2024, 08:43:12 pm »
HEX is not always used for flash
You should read the OP. It very specifically talks about bootloaders and flash programming.

I'm not here to talk philosophy. From the bootlaaoding point of view, HEX is one of the worst formats.

Alex
 

Offline DavidAlfa

  • Super Contributor
  • ***
  • Posts: 5912
  • Country: es
Re: Computing CRC-16 from .hex file
« Reply #10 on: February 12, 2024, 08:55:21 pm »
The HEX file already has a checksum for each line, isn't that enough?
Code: [Select]
:100000A59F2CC0C03C4039403640334030402D4045Sum all bytes in (100000A59F2CC0C03C4039403640334030402D401 = 0xBB) and make 2-complement: 0x45.
This should match last byte.
« Last Edit: February 12, 2024, 08:58:29 pm by DavidAlfa »
Hantek DSO2x1x            Drive        FAQ          DON'T BUY HANTEK! (Aka HALF-MADE)
Stm32 Soldering FW      Forum      Github      Donate
 

Online ataradov

  • Super Contributor
  • ***
  • Posts: 11262
  • Country: us
    • Personal site
Re: Computing CRC-16 from .hex file
« Reply #11 on: February 12, 2024, 09:13:09 pm »
It would be enough to verify that HEX is not damaged, but once the firmware is written into the flash, all those checksum are gone. You can do verification, but if you are writing segmented chunks and have to do read-modify-write operations, it becomes trickier. It is still possible, but making bootloader code more complicated is never a good idea.

Alex
 

Offline HwAoRrDk

  • Super Contributor
  • ***
  • Posts: 1478
  • Country: gb
Re: Computing CRC-16 from .hex file
« Reply #12 on: February 13, 2024, 12:35:51 am »
The SRecord utilities might be able to do what you want.

The srec_cat utility can do things like read in a hex file, fill holes (i.e. gaps in addresses) in the data with certain byte value, calculate a CRC (various algorithms supported) and insert that checksum into the data, then output again as hex format.
 

Offline fearlessTopic starter

  • Contributor
  • Posts: 20
  • Country: us
Re: Computing CRC-16 from .hex file
« Reply #13 on: February 15, 2024, 12:49:20 am »
I'm conversant with srecord suite, including how to compute a CRC and insert it into a specific address in the .hex file -- for example see

   https://community.nxp.com/t5/Kinetis-Design-Studio/Generating-CRC32-from-srec-files/m-p/709801#M9264

What I HAD forgotten about was normalizing the .hex file -- that does just what I need.   

(In the meantime, I wrote code that allocated a virtual memory buffer, filled it with 0xff bytes, then parsed the .hex file and wrote into the virtual memory buffer, then ran the checksum.  That works also...)

Case solved!

 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf