vendredi 8 mai 2015

How to insert data into compiled binary for MCU

I am trying to insert a md5 hash of part of my binary into the binary, for keeping track of MCU FW version.

I have approached it like this: in the link script I have split the flash in two sections

MEMORY                                                                                                  
{                                                                                                       
FLASH0 (rx)      : ORIGIN = 0x8000000, LENGTH = 64K - 16                                                
FLASH1 (r)       : ORIGIN = 0x800FFF0, LENGTH = 16                                                      
RAM (xrw)      : ORIGIN = 0x20000000, LENGTH = 8K                                                       
} 

Then I have specified a output section like so:

  .fw_version :                                                                                         
  {                                                                                                     
    KEEP(*(.fw_version))                                                                                
  } >FLASH1

Next I have my 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 the binary is compiled and objcopy has been used to create a .bin file I have a 65536 B large file, I split that file at 65520 bytes, do a md5 checksum of the first part and insert that into the second part (16 B). Lastly I do cat parta partb > final.bin.

When i examine this binary with hexdump I can see that the md5 checksum is indeed at the end. Using objdump -h I get:

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

and objdump -t gives:

...
0800fff0 g     O .fw_version    00000010 FW_VERSION
...

I thought that this meant that I could just use FW_VERSION[i] to get part i of the md5 checksum from within the mcu fw but when I examine the memory in gdb I get that it's all zeroed out like it was never changed.

What am I missing here?

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

Aucun commentaire:

Enregistrer un commentaire