How to inject data in a .bin file in a post compilation script?

659 Views Asked by At

Purpose

I want my build system to produce one binary file that includes:

  • The bootloader
  • The application binary
  • The application header (for the bootloader)

Here's a small overview of the memory layout (nothing out of the ordinary here)

The wanted memory organization

The build system already concatenates the bootloader and the application in a post-compilation script.
In other words, only the header is missing.

Problem

What's the best way to generate and inject the application header in the memory?

Possible solutions

  • Create a .bin file just for the header and use cat to inject it in my final binary
  • Use linker file to hardcode the header (is this possible?)
  • Use a script to read the final binary and hardcode the header
  • Other?

What is the best solution for injecting data in memory in a post compilation script?

1

There are 1 best solutions below

2
On

SRecord is a great tool for doing all kinds of manipulation on binary and other file types used for embedded code images.

In this case, given a binary bootheader.bin to insert at offset 0x8000 in image.bin:

srec_cat bootheader.bin −binary −offset 0x8000 −o image.bin

The tool is somewhat arcane, but the documentaton includes numerous examples covering various common tasks.