Objcopy elf to bin file
Asked Answered
V

2

9

I have STM32F404 board and I am trying to flash it. I am following this tutorial.

In the project Makefile

$(PROJ_NAME).elf: $(SRCS)
    $(CC) $(CFLAGS) $^ -o $@ 
    $(OBJCOPY) -O ihex $(PROJ_NAME).elf $(PROJ_NAME).hex
    $(OBJCOPY) -O binary $(PROJ_NAME).elf $(PROJ_NAME).bin

burn: proj
    $(STLINK)/st-flash write $(PROJ_NAME).bin 0x8000000

The bin file is generated using OBJCOPYand then flashed using the Make target burn

My questions :

Question 1: What does OBJCOPY=arm-none-eabi-objcopy in this case. I opened the man but I didn't fully undrestand can anyone explain it simply ?

Question 2: Flashing the bin file gives the expected result (Leds blinking) However the leds are not blinking by flashing the elf file $(STLINK)/st-flash write $(PROJ_NAME).elf 0x8000000 so why ?

Vindictive answered 5/4, 2018 at 19:47 Comment(0)
Y
14

Question 1: What does OBJCOPY=arm-none-eabi-objcopy in this case. I opened the man but I didn't fully undrestand can anyone explain it simply ?

It assigns value arm-none-eabi-objcopy to make variable OBJCOPY.

When make executes this command:

$(OBJCOPY) -O binary $(PROJ_NAME).elf $(PROJ_NAME).bin

the actual command that runs is

arm-none-eabi-objcopy -O binary tim_time_base.elf tim_time_base.bin

Question 2: Flashing the bin file gives the expected result (Leds blinking) However the leds are not blinking by flashing the elf file $(STLINK)/st-flash write $(PROJ_NAME).elf 0x8000000 so why?

The tim_time_base.elf is an ELF file -- it has metadata associated with it. Run arm-none-eabi-readelf -h tim_time_base.elf to see what some of this metadata are.

But when you processor jumps to location 0x8000000 after reset, it is expecting to find executable instructions, not metadata. When it finds "garbage" it doesn't understand, it probably just halts. It certainly doesn't find instructions to blink the lights.

Yachting answered 6/4, 2018 at 4:2 Comment(4)
Thank you @Employed Russian, for the Question 1, the purpose of the question is to explain more how objcopy worksVindictive
How does objcopy translates this metadata in the bin ?Vindictive
@Vindictive generally it doesn't. it isn't nessesary for program execution, so it just discards itPagas
This worked great for me! Here is the link to download objcopy if you are using Windows: pemicro.com/blog/index.cfm?post_id=35Karlkarla
X
1

In case someone wants to use the DFU ("Do a Firmware Upgrade") function, this tutorial teaches how to use the binary file to be loaded via USB, when the STM32 is operating with USB Host (or maybe OTG):

STM32 USB training - 11.3 USB MSC DFU host labs

This tutorial is part of a series of videos that are highly recommended for the programmer to watch, to understand a little better how the STM32 USB ports work and use (videos provided by the STM32 manufacturer itself, I recommend that the programmer watch all the videos on this channel):

MOOC - STM32 USB training

Notes: The example code from the STM32 tutorials are available in the descriptions of the videos themselves.

The binary file (*.bin) can be obtained with the help of the command that the colleague above explained (Employed Russian), and it (command) can also be adapted to produce a file containing the comparison value for CRC usage, as can be seen some details in these following posts:

Hands-on: CRC Checksum Generation

Srec_cat could be used to generate CRC checksum and put it into HEX file. To simplify the process, please put srec_cat.exe into the root of project folder.

Some tips and solutions about this CRC usage (Windows/Linux)

Unfortunately the amount of code is too big to post here directly, but I leave the code related to the other answer below:

arm-none-eabi-objcopy -O ihex "${BuildArtifactFileBaseName}.elf" "${BuildArtifactFileBaseName}.hex" && ..\checksum.bat ${BuildArtifactFileBaseName}.hex

Contents of the checksum.bat file:

#!/bin/bash

# Windows [Dos comment: REM]:
#..\srec_cat.exe %1 -Intel -fill 0xFF 0x08000000 0x080FFFFC -STM32 0x080FFFFC -o ROM.hex -Intel

# Linux [Linux comment: #]:
srec_cat $1 -Intel -fill 0xFF 0x08000000 0x080FFFFC -STM32 0x080FFFFC -o ROM.hex -Intel

Note: In this case, the file to be written is ROM.hex (you will need to configure the STM32CubeIDE to be able to do this operation, the IDE uses the *.elf file, see how to do it in the tips above)

This other tutorial deals with using the file with *.DFU extension: DFU - DfuSe

The key benefits of the DFU Boatloader are: No specific tools such us JTAG, ST-LINK or USB-to-UART cable are needed. The ability to program an "empty" STM32 device in a newly-assembled board via USB. And easy upgrade the STM32 firmware during development or pre-production.

This need to use a HEX file facilitates the operation of the implementation of the ROM.hex file generated with the CRC value, being practically a continuity:

You must generate a .DFU file from an .HEX or .S19 file, for do this use the DFU File Manager.

But it seems that using the *.DFU file is not as standalone as using the *.BIN file, so I found this other code that converts the HEX file (generated with CRC) to the *.BIN file, which can be used with a USB stick, as per the tutorial cited at the beginning of this answer (11.3 USB MSC DFU host):

objcopy --input-target=ihex --output-target=binary code00.hex code00.bin

Source

It sounds a little confusing, but we have these steps:

1- The STM32CubeIDE generates the *.elf file.

2- After compilation, the *.elf file is converted to *.hex.

3- CRC value is added in *.hex file via srec_cat application.

4- Now the *.hex file is converted to *.bin.

5- The BIN file is then stored on a USB flash drive.

6- STM32 updates firmware using USB flash drive file.

To use the *.BIN file it is necessary that the STM32 is already programmed to load the BIN file. If it is not programmed (the STM32 is empty, virgin or the program was not made to load the BIN file), it will be necessary to use St-Link or another programmer, or perhaps making use of the DFU method described in the tutorial above (DFU - DfuSe).

Xerosis answered 23/5, 2022 at 23:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.