Skip to main content
Visitor II
November 28, 2024
Question

Firmware size

  • November 28, 2024
  • 2 replies
  • 1521 views

Dear Sirs,

Is there a method to calculate the firmware size?

I am using STM32F103R8T6

My flash memory is divided in three blocks:

- Custom boot loader block

- Main application block

- Data block

When the main application is running, it needs to calculate its own size in terms of quantity of bytes in flash.

This is needed in order to calculate the CRC of the firmware.

Some pages at the end of main application block range might not have been erased, so I think I cannot count on 0xFFFFFF presence. Furthermore the firmware might finish with 0xFF.

Maybe the .hex file starts with the program counter initialization, followed by some useful information??

 

Thanks,

Marco

    This topic has been closed for replies.

    2 replies

    Graduate II
    November 28, 2024

    Can't you use some Linker Script symbols to scope the image size?

    Covered on the forum previously. Can also place size in one of the slack vectors in the table so tools can recover. 

    Can determine also from object file structures, ie LOADBITS and via what the .HEX touches

    sirpakAuthor
    Visitor II
    November 28, 2024

    I need the application to calculate itself or being able to find it somewhere.

    I couldn't find previous covering inside the forum.

    Super User
    November 28, 2024

    As @Tesla DeLorean wrote, this is easy with help of the linker script.

    For example, append this at the end of the script after the last section:

     

     

     

    MEMORY
    {
     FLASH (rx) : ORIGIN = 0x8000000, LENGTH = 2048K
    .........
    }
    
    SECTIONS
    {
    ................
     .flash_end (NOLOAD) : {
     . = ALIGN(4);
     } >FLASH
    
     my_FLASH_SIZE = (LOADADDR(.flash_end) - ORIGIN(FLASH)) ;
    }

     

     

    Then, in some .c file:

     

     

    extern char my_FLASH_SIZE; // exported by linker script
    #define MY_FLASH_SIZE ((size_t)&my_FLASH_SIZE)

     

     

     

    Maybe the .hex file starts with the program counter initialization, followed by some useful information??

    No chance. Standard hex files carry no such things.

    Super User
    November 28, 2024

    @sirpak wrote:

    This is needed in order to calculate the CRC of the firmware


    Why?

    Why not just calculate the CRC over the entire region of memory dedicated to the application?

    sirpakAuthor
    Visitor II
    November 28, 2024

    Because when a firmware is programmed, it just erase and program its own pages, not the whole Main application block memory space.

    Doing so, there might be leftovers in other pages, if a smaller firmware overwrites a bigger firmware. Those leftovers might differ from device to device.

    Super User
    November 28, 2024

    So why not make sure  that it does erase the whole area?