c - How to insert data into compiled binary for MCU -


i trying insert md5 hash of part of binary binary, keeping track of mcu fw version.

i have approached this: in link script have split flash in 2 sections

memory                                                                                                   {                                                                                                        flash0 (rx)      : origin = 0x8000000, length = 64k - 16                                                 flash1 (r)       : origin = 0x800fff0, length = 16                                                       ram (xrw)      : origin = 0x20000000, length = 8k                                                        }  

then have specified output section so:

  .fw_version :                                                                                            {                                                                                                          keep(*(.fw_version))                                                                                   } >flash1 

next have firmware_version.c file containing only:

#define fw_version_size 16                                                                                const unsigned char fw_version[fw_version_size]                                                            __attribute__((section(".fw_version"), used)) = {0}; 

then after binary compiled , objcopy has been used create .bin file have 65536 b large file, split file @ 65520 bytes, md5 checksum of first part , insert second part (16 b). lastly cat parta partb > final.bin.

when examine binary hexdump can see md5 checksum indeed @ end. using objdump -h get:

... 8 .fw_version   00000010  0800fff0  0800fff0  00017ff0  2**2 ... 

and objdump -t gives:

... 0800fff0 g     o .fw_version    00000010 fw_version ... 

i thought meant use fw_version[i] part of md5 checksum within mcu fw when examine memory in gdb it's zeroed out never changed.

what missing here?

[edit] device stm32f030c8t6 arm cortex m0 programmed through gdb.

like commented under question found (one) reason not working while manipulating .bin file while loaded .elf file when programming gdb. should (could) have worked if used programmer or bootloader download .bin file target.

i found better (i think) way of doing though.

  1. compile sources in project .o files.
  2. cat *.o > /tmp/tmp.something_unique. used $(shell mktemp) in makefile
  3. openssl dgst -md5 -binary /tmp/tmp.something_unique > version_file
  4. objcopy -i binary -o elf32-littlearm -b arm version_file v_file.o
  5. linkscript has section .fw_version : { keep(v_file.o(.data)) } >flash1
  6. link application
  7. in application address of version number doing extern unsigned char _binary_version_file_start; uint8_t *fw_version = &_binary_version_file_start; const size_t fw_version_size = (size_t) &_binary_version_file_size;. note uses of & correct.

this result in checksum being taken on objects compiled source , checksum linked binary flashed in target.


Comments