Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting from *.hex to *.bin for ARM on Linux

I want to upload program to my STM32F4 Discovery board using st-flash command. Problem is when I try to upload *.hex or *.elf file it is just not working. I tried many ways ( like using xxd ) of converting from *.elf or *.hex to *.bin but it is still not working when I upload it. And yes, I tried uploading hex file from other Windows computer and it works.

Sample ( first three lines, just to show you how it looks inside ) of hex file:

:020000040800F2
:100000000000022099020008A1020008A5020008D1
:10001000A9020008AD020008B102000800000000BB

My OS is Ubuntu 14.04 LTS.

Thanks for help!

like image 360
Jacajack Avatar asked Nov 16 '14 20:11

Jacajack


3 Answers

I assume you have linux and you have installed binutils, so you just do:

objcopy --input-target=ihex --output-target=binary code00.hex code00.bin
like image 150
A. Genchev Avatar answered Sep 30 '22 11:09

A. Genchev


Have you considered using arm-none-linux-gnueabi-objcopy (or similar) instead of xxd? This can be found in any ARM toolchain.

like image 43
Adashi Avatar answered Sep 30 '22 10:09

Adashi


.hex file format is documented on the web. You need a loader program capable to understand it, as it has several kinds of registers to control the loading process. Some of the registers control entry point address. Others are data to be loaded at some fixed address.

You can get information at the wikipedia (I have found it there) for Intel Hex format (that's how it is called). If all the data is on only one segment and no entry point is specified, theoretically you can convert it to binary data to be loaded, but that's improbable.

It is a text file made of lines beginning with ':' character, then comes a two field hex number representing the number of bytes of data this record has, then the address this data is to be loaded on, then the type of file, it can be one of:

  • 00 This value is for a bunch of data, normally 16 bytes (0x10)
  • 01 End of file. It has no data, so always is codified as :00000001FF
  • 02 Extended segment address, to allow addresses with more than 16bit.
  • 03 Start Entry point address, to register the initial CS:IP address in 0x86 architecture.
  • 04 Extended Linear Address, to specify 32bit addresses. This specifies the upper 16bit address part of 00 registers.
  • 05 Start Entry point Linear Address. This is the 32 bit linear entry point address.

Then comes n bytes (n is the value of the first field) of data (hex coded) to be loaded and finally a checksum byte (the sum in two's complement of all the record bytes from the colon up).

like image 35
Luis Colorado Avatar answered Sep 30 '22 11:09

Luis Colorado