Skip to main content
LGree.3
Associate
November 26, 2021
Solved

Combining Bootloader and Application Code via Batch script

  • November 26, 2021
  • 4 replies
  • 5409 views

Hello,

I am building a project using a batch script which is calling stm32cubeidec.exe to build the separate entities. The project contains a bootloader as well as main application code. I am wondering the best way to combine the two binary files in the batch script into one complete binary for flashing.

I have a specific bootloader size and I know where the application code will be flashed. I just need to figure out the best way to combine them into one binary file.

This topic has been closed for replies.
Best answer by TDK

Here is a batch file that will do it. Rename to combine_hex_files.bat and put somewhere accessible.

:: Usage: combine_hex_files source1.hex source2.hex combined.hex
 
@ECHO OFF
 
IF "%1"=="" GOTO BAD_ARGUMENTS
IF "%2"=="" GOTO BAD_ARGUMENTS
IF "%3"=="" GOTO BAD_ARGUMENTS
 
FINDSTR /R /V "^:00000001FF" "%1" > "%3"
TYPE "%2" >> "%3"
GOTO COMPLETE
 
:BAD_ARGUMENTS
ECHO ERROR: incorrect number of arguments
ECHO.
ECHO Usage:
ECHO combine_hex_files.bat source1.hex source2.hex combined.hex
 
:COMPLETE

4 replies

TDK
Super User
November 26, 2021

A HEX file will be easier, otherwise you need to pad the binary with data between the two sections. Concatenate the files and remove the EOL line from the first file and you're done.

Or, if your bootloader BIN is the exact size of the bootloader, you can just concatenate the two files.

"If you feel a post has answered your question, please click ""Accept as Solution""."
LGree.3
LGree.3Author
Associate
November 26, 2021

Thank you, how would one do this via batch for the hex files?

It is not the exact size and will require padding.

TDK
TDKBest answer
Super User
November 26, 2021

Here is a batch file that will do it. Rename to combine_hex_files.bat and put somewhere accessible.

:: Usage: combine_hex_files source1.hex source2.hex combined.hex
 
@ECHO OFF
 
IF "%1"=="" GOTO BAD_ARGUMENTS
IF "%2"=="" GOTO BAD_ARGUMENTS
IF "%3"=="" GOTO BAD_ARGUMENTS
 
FINDSTR /R /V "^:00000001FF" "%1" > "%3"
TYPE "%2" >> "%3"
GOTO COMPLETE
 
:BAD_ARGUMENTS
ECHO ERROR: incorrect number of arguments
ECHO.
ECHO Usage:
ECHO combine_hex_files.bat source1.hex source2.hex combined.hex
 
:COMPLETE

"If you feel a post has answered your question, please click ""Accept as Solution""."
Tesla DeLorean
Guru
November 26, 2021

If you're a C programmer, you could presumably code something with a handful of STDIO file handling functions.

Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..
MM..1
Chief III
November 28, 2021

How utility you use for flash or for what you need one file ???

In normal i runs sstlink cli twice and flash boot and app...