cancel
Showing results for 
Search instead for 
Did you mean: 

Recognizing a project by reading the flash

chriskuku
Senior III

 

It happens to from time to time that I have a flashed project in a hardware, e.g. a bluepill, but I forgot which source project was belonging to it in the STM32CubeIDE's project treeview.

Is there a way to write the name of the project by means of some C-macro in text form into the program as kind of a signature or even more safely, compute a checksum of the flash binary and write that checksum into the program (yes I know, this may become tricky since the checksum alters the checksum itself :)

Is there a common technique for this?

3 REPLIES 3
Andrew Neil
Super User

@chriskuku wrote:

Is there a way to write the name of the project by means of some C-macro in text form into the program as kind of a signature 


Sure there is - no need for macros; eg,

const char signature[] = "This is my Signature string";

The string will be readable in flash.

Better yet, have your code write it out to a UART on startup.

 

PS:

You could locate that string at a specific address, to make it easier to find; eg,

Locate Constant Data in Flash at Fixed Address

https://stackoverflow.com/a/54128613

Defining Variables at Absolute Addresses with gcc

GCC Documentation: Specifying Attributes of Variables

A complex system that works is invariably found to have evolved from a simple system that worked.
A complex system designed from scratch never works and cannot be patched up to make it work.

The least I'm demanding is an automatism that gets the project name as a string. (similar to __DATE__,__TIME__, __LINE__,__FILE__ macros in C). Or some Makefile construct, that does something like "echo "const char signature =\" `echo  $(PROJECT)` \";" >project.h". 


@Andrew Neil wrote:

@chriskuku wrote:

Is there a way to write the name of the project by means of some C-macro in text form into the program as kind of a signature 


Sure there is - no need for macros; eg,

const char signature[] = "This is my Signature string";

The string will be readable in flash.

 


No, it will not unless it is referenced in the program code. Normally all the non-referenced objects are removed during linking.

The solution is to use custom section name for the object and define this section in the link script with KEEP(). it may be put at the end of .isr_vector output section.

Code:

__attribute__((section(".my_sig")))const char sig[] = "My signature";

Link script modification to put the signature in a fixed location, right after exception vector table:

  /* The startup code into "FLASH" Rom type memory */
  .isr_vector :
  {
    . = ALIGN(4);
    KEEP(*(.isr_vector)) /* Startup code */
    KEEP(*(.my_sig))
    . = ALIGN(4);
  } >FLASH

 

My STM32 stuff on github - compact USB device stack and more: https://github.com/gbm-ii/gbmUSBdevice