Combining HEX Files

Go To Last Post
9 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I'm referring to the Bootloader FAQ document, which tells we can combine 2 hex files (e.g. app.hex and boot.hex) by these 2 ways:

 

1:

I think the easiest way to combine hex files is with the srec_cat command-line tool. This tools is part of the srecord utilities. It comes with WinAVR and is very easy to install on a Unix like OS. This is the command you would use:

 

srec_cat app.hex -I boot.hex -I -o combined.hex -I 

 2:

Cliff explains that you can also manually combined the app.hex and boot.hex files with a small edit: “Every hex file has one last record in it which effectively says “the file ends here”. So after doing a [manual merge] one needs to edit the combined hex file and locate the termination record from the end of app.hex (now somewhere in the middle of the file) and remove it. The record has a type 01 – the type byte of an Intel hex record is the 4th hex byte so the record actually looks like “:00000001FF”. The whole line (the one in the middle, not the one at the end of the file) should be removed.”

I tried both ways. The seconds way seems to work for me, I don't know why but sometimes I get a verify error if I combine hex files by this way. So I wanted to use the srec utilities option which seems safer. But combined files for these 2 ways don't look like the same. 

 

 

So I wanted to be sure whether it is true or not. So I tried using the srec_cmp to compare them. But I guess I couldn't write the command properly. I either get "misplaced "-Intel" option " error or "file contains no data" error. I attached 2 combined.hex files; combined1 is the file which has been combined by the first way and the combined2 for the second way. Can we say these are same hex files?

 

 

srec_cmp -I ihex combined1.hex -O ihex combined2.hex

 

Attachment(s): 

This topic has a solution.
Last Edited: Mon. May 23, 2016 - 01:35 PM
This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
srec_cmp -I ihex combined1.hex -O ihex combined2.hex

that is not the way srec tools take the format specifcation. In GNU objcopy you do indeed use parameters like:

avr-objcopy -I ihex -O binary avr.hex avr.bin

(which converts a .hex to a bin by the way and can be quite useful) but the srec tools do this in a different way:

srec_cat avr.hex -intel avr.bin -binary

I forget the exact syntax but the fact is that you give a filename then you FOLLOW it with "-intel", "-binary" or whatever.

 

Anyway if I want to compare two .hex files the way I would actually do it is to convert both to binary first (so using what you just learned above):

avr-objcopy -I ihex -O binary file1.hex file1.bin
avr-objcopy -I ihex -O binary file2.hex file2.bin

Converting to binary in this way effectively "flattens" the files - they are no longer dependent on specific Intel Hex layouts and line lengths etc. Instead each out is just a pure block of binary. So even if the two .hex were different ways to express the same binary pattern the two bin files will be identical. Then just use your favourite binary file comparison tool to compare file1.bin and file2.bin. Personally I like vbindiff in Linux. But even plain old diff should be able to tell you if they are binary equal or not. Another quick way to compare binaries is to run md5sum on both and see if they have the same MD5 value (only binary equal files will have the same MD5).

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

clawson wrote:

srec_cmp -I ihex combined1.hex -O ihex combined2.hex

that is not the way srec tools take the format specifcation. In GNU objcopy you do indeed use parameters like:

avr-objcopy -I ihex -O binary avr.hex avr.bin

(which converts a .hex to a bin by the way and can be quite useful) but the srec tools do this in a different way:

srec_cat avr.hex -intel avr.bin -binary

I forget the exact syntax but the fact is that you give a filename then you FOLLOW it with "-intel", "-binary" or whatever.

 

Anyway if I want to compare two .hex files the way I would actually do it is to convert both to binary first (so using what you just learned above):

avr-objcopy -I ihex -O binary file1.hex file1.bin
avr-objcopy -I ihex -O binary file2.hex file2.bin

Converting to binary in this way effectively "flattens" the files - they are no longer dependent on specific Intel Hex layouts and line lengths etc. Instead each out is just a pure block of binary. So even if the two .hex were different ways to express the same binary pattern the two bin files will be identical. Then just use your favourite binary file comparison tool to compare file1.bin and file2.bin. Personally I like vbindiff in Linux. But even plain old diff should be able to tell you if they are binary equal or not. Another quick way to compare binaries is to run md5sum on both and see if they have the same MD5 value (only binary equal files will have the same MD5).

 

Thanks.

I run the command again after your suggestion, like this:

 

srec_cmp combined.hex -intel combined2.hex -intel

If I change the content of these files and try to compare them, it detects the error and outputs it. If I compare them with their default states, it does not output anything. So can I assume that they are same? Or should I debug the application and ensure the combined application is true?

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

As I say I would convert each to binary and use an unequivocal binary compare.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

clawson wrote:

As I say I would convert each to binary and use an unequivocal binary compare.

 

Oh, I couldn't understand that well first though. You are right, they must be the same if I convert them to binary as I can even check it with my eyes :D Thanks!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I added this to the pre-build event command line:

 

"$(ToolchainDir)\avr-objcopy.exe" -I ihex -O binary "$(MSBuildProjectDirectory)\bootloader\Bootloader.hex" bootloader.bin
"$(ToolchainDir)\avr-objcopy.exe" --rename-section .data=.boot,contents,alloc,load,readonly,data -I binary -B avr:106 -O elf32-avr bootloader.bin bootloader.o

 

Then add this to the linker command line:

 

bootloader.o -Wl,--undefined=_binary_bootloader_bin_start

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Issue:
Solution with "srec_cat" described here not work for me. I get this error message, when I try to open with CVAVR Chip Programmer:
"Invalid data in file" (I tried more intel format types, but without result...)
I have result with following steps and I hope to help others too.

Combine avr *.hex files
It works for me :)
-------------------------
The scope is to combine (merge) "app.hex" with "bootloader.hex" to new file "result.hex"

* Tools: Notepad++ or other text editor

1. Copy content of "app.hex" to file "result.hex")

2. Remove this line from end of "result.hex": ":00000001FF"

3. Copy content of "bootloader.hex" to end of "result.hex" file

4. Save and test your hex file...

Note:
I used the following files:
- "chip45boot2_atmega328p_uart0_v2.9Q.hex" (as "bootloader.hex") it was address range: 3C00...3F98
  Downloaded from: https://www.chip45.com/avr_bootl...
- "TDAQ_XP3.hex" (as "app.hex") it was address range: 0...23E4
  Created with CodeVision AVR V3.25 by SPECTRA for Internet Thermometer System, see here: https://www.facebook.com/spectra...
- "comb_TDAQ_XP3.hex" ("result.hex") file produced address range: 0...3F98

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

szabogyozo wrote:

Issue:
Solution with "srec_cat" described here not work for me. I get this error message, when I try to open with CVAVR Chip Programmer:
"Invalid data in file" (I tried more intel format types, but without result...)
I have result with following steps and I hope to help others too.

Combine avr *.hex files
It works for me :)
-------------------------
The scope is to combine (merge) "app.hex" with "bootloader.hex" to new file "result.hex"

* Tools: Notepad++ or other text editor

1. Copy content of "app.hex" to file "result.hex")

2. Remove this line from end of "result.hex": ":00000001FF"

3. Copy content of "bootloader.hex" to end of "result.hex" file

4. Save and test your hex file...

Note:
I used the following files:
- "chip45boot2_atmega328p_uart0_v2.9Q.hex" (as "bootloader.hex") it was address range: 3C00...3F98
  Downloaded from: https://www.chip45.com/avr_bootl...
- "TDAQ_XP3.hex" (as "app.hex") it was address range: 0...23E4
  Created with CodeVision AVR V3.25 by SPECTRA for Internet Thermometer System, see here: https://www.facebook.com/spectra...
- "comb_TDAQ_XP3.hex" ("result.hex") file produced address range: 0...3F98

 

 

 * DO NOT CROSS POST. I have deleted the other post. Moderator. *

Ross McKenzie ValuSoft Melbourne Australia

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

szabogyozo wrote:
Solution with "srec_cat" described here not work for me.

Show the srec_cat command you were trying to use. Also, if possible, post the two input and the joined output .hex file and I'll tell you whether it's valid Intel Hex or not and, if not, what's wrong with it.