zephyrproject-rtos / zephyr

Primary Git Repository for the Zephyr Project. Zephyr is a new generation, scalable, optimized, secure RTOS for multiple hardware architectures.
https://docs.zephyrproject.org
Apache License 2.0
10.48k stars 6.41k forks source link

Add a way to place given variable at a given address in flash at linker time #48417

Open apoussartFW opened 2 years ago

apoussartFW commented 2 years ago

Is your feature request related to a problem? Please describe. Impossibility right now to have two variables placed at the beginning (fixed address) and the end of the zephyr app code.

Describe the solution you'd like The idea would be that a variable in the beginning of the program and another at the end would be used to verify its integrity when being sent to a device through an unreliable channel. If the first variable points towards an address that correctly contains a fixed magic word that is located at the very end of the image, then the app was sent in its entirety.

I would like to be able to place C variables at given addresses in the Linker at compile time so that the generated .hex already has those addresses with the correct values.

More specifically, I'd like to be able to do something like this in my C code:

PLACE_IN_SECTION("APP_END")   const uint32_t my_magic_value = 0x1234abcd;
PLACE_IN_SECTION("APP_START") const uint32_t my_magic_value_address = (uint32_t) &my_magic_value;

APP_END and APP_START would be defined in a linker or using a specific structure as follows:

SECTIONS
{
    .start_region 0x08000300:
  {
    KEEP(*(APP_START))
    . = ALIGN(4);
  } >FLASH
    .end_region :
  {
    KEEP(*(APP_END))
    . = ALIGN(4);
  } >FLASH
}

where APP_START would be forced at 0x08000300 (example fixed address at the beginning of the app) and APP_END would naturally go at the end of the app.

Describe alternatives you've considered I tried to use the Code Relocation (https://docs.zephyrproject.org/latest/kernel/code-relocation.html) feature but with no success for a single 32-bits word of data to be put at the very beginning of the program space.

henrikbrixandersen commented 2 years ago

Have you seen how e.g. the NXP Kinetis series handles this? It needs a specific variable for configuration to be stored at a fixed address in flash: https://github.com/zephyrproject-rtos/zephyr/blob/main/soc/arm/nxp_kinetis/flash_configuration.c