Search code examples
stm32bootloaderlinker-scripts

STM32 link crc value of firmware in the final binary


I'm attempting to write a bootloader + application for a custom STM32F401RE board, and I have some questions.

Currently, I have the booloader lives at 0x08000000, and application lives 0x08020000. The idea is to have the bootloader perform a CRC check before jumping to the application.

I used a linker script to create a section called .fw_crc right after .isr_vector , and before .text. In the application code, I can write to this directly to this address in the flash. But this is where I got stuck.

If I understand it correctly, I should...

  1. Default the CRC value to 0 in the application.
  2. Build the binary.
  3. Generate the CRC of this binary.
  4. Use some hex tool to overwrite this section with the computed CRC value.
  5. Regenerate the binary again for final flashing.

Is the above assumption correct? Doesn't regenerating the binary a second time with the updated CRC value ultimately change the resulting CRC of the final binary?

Also, inside the bootloader, do I perform the CRC check from the start of the application_address to application_address + binary_size?


Solution

  • Is the above assumption correct? Doesn't regenerating the binary a second time with the updated CRC value ultimately change the resulting CRC of the final binary?

    • The default value of the CRC might be set to anything you like. It will be overwritten by your post-build process anyway.

    • In your post-build step, you must calculate the CRC over the entire application binary except for the .fw_crc, and write the CRC result into that specific memory region. However, since the .fw_crc is defined in between the .isr_vector and .text regions, you should consider calculating the CRC for those two parts separately and combining them. A suggestion would be to re-arrange your memory areas such that .fw_crc lies at the start. For this to work, you have to relocate the ISR vector table in flash with the necessary offset. Then you will be able to calculate the application binary CRC more easily.

    Also, inside the bootloader, do I perform the CRC check from the start of the application_address to application_address + binary_size?

    As mentioned earlier, according to your current memory arrangement, you will have to calculate the CRC over two memory regions i.e. .isr_vector and .text. Of course, you will need to perform the same CRC calculation and comparison inside the bootloader to verify the validity of your application binray.