How to convert elf file to binary file that could boot from SD based on CycloneV and FreeRTOS

yycolin wrote on Friday, March 31, 2017:

Hello Everyone.

I am developing a project based on Altera Cyclone V SoC RTOS Demo(http://www.freertos.org/RTOS_Altera_SoC_ARM_Cortex-A9.html). That Demo has been ported successfully and debug normally. Then I try to convert elf file (RTOSDemo.elf) to binary image file which could be stored in a SD card and boot from it, but failed. Two steps I have tried.
Firstly, use tool “arm-altera-eabi-objcopy” as following.
arm-altera-eabi-objcopy -O binary RTOSDemo.elf RTOSDemo.bin
The size of file named RTOSDemo.elf is 1.85 MB, after converting,the size of RTOSDemo.bin is 3.99 GB.

Secondly, Try use tool “mkimage” as following.
mkimage -A arm -T standalone -C none -a 0x100040 -e 0 -n “Baremetal Image” -d RTOSDemo.bin main-mkimage.bin
But the shell return as follow.
D:\altera\15.0\embedded\host_tools\altera\mkimage\mkimage.exe: Can’t read RTOSDemo.bin: No error

Could you please give me some suggestion? Thank you very much.

rtel wrote on Friday, March 31, 2017:

Not really a FreeRTOS question as such (how do the docs for the Altera
tools tell you to do this?), but:

Firstly, use tool “arm-altera-eabi-objcopy” as following.
arm-altera-eabi-objcopy -O binary RTOSDemo.elf RTOSDemo.bin
The size of file named RTOSDemo.elf is 1.85 MB, after converting,the
size of RTOSDemo.bin is 3.99 GB.

I presume this is because you have a non-contiguous memory map (for
example, executable code far away from some RAM block), and the gaps
between the memory regions are just being filled with zeros.

yycolin wrote on Saturday, April 01, 2017:

Thank you for your reply.
If there is no need to use the Altera tools as above, could you please tell me how to convert the ELF file to the image file which could be stored in a SD card and boot FreeRTOS demo?

wwdjax wrote on Monday, September 09, 2019:

Just in case anyone is still looking for an answer here it is:
“in the embedded command shell for SoCEDS, using the command: arm-altera-eabi-readelf –sections RTOSDemo.elf
I can see that the section .text is located at 0x100000 and .cs.ocram with size 0xc800 is located at 0xffff0000. This explanes the huge bin-file when using the objcopy tool. The empty space is zero-padded. I used the command:
arm-altera-eabi-objcopy -R .cs3.ocram -O binary RTOSDemo.elf RTOSDemo.bin
to generate the binary file without the .cs3.ocram section. I can’t predict the effect of removing this section.
Next I generated the image to load from 0x100000 with the command:
mkimage -A arm -T standalone -C none -a 0x100000 -e 0 -n “baremetal image” -d RTOSDemo.bin RTOSDemo-mkimage.bin
This gave me an image file of 51kB for the blinky compilation. I copied the image file to the fat-portion of the SD-card, plugged it in and it booted.” -magnuslauritsen on June 14, 2017