cancel
Showing results for 
Search instead for 
Did you mean: 

How can I calculate CRC of image in post build?

YHass.14
Senior

Hello,

I want to calculate the CRC (32 bit) of image at post build and insert it in a known location in the image.

In order to do it I should perform the following steps:

a) Definition of placeholder in linker script file

b) Post build steps:

b.1) Calculation of image CRC (32 bit)

b.2) Insert it into the placeholder

The reason of 32 bit is to use the STM32 HW CRC calculation for checking the CRC.

The post build steps can be performed by IELFTOOL.

Can anyone help?

Yacob Hassidim.

16 REPLIES 16

Hi Yacob,

As mentioned by waclawek, srec_cat without -fill switch will do what you want. But, it brings another problem: where to put the checksum as srec_cat requires an address. Here is a possible option (simple but not generic) for your reference if your code only stays in single partition of FLASH and you want to put CRC right next to your code:

  1. Build binary as well.
  2. Check the file size of the binary.
  3. The number of the size = the address of CRC

Regards,

Ethan

Hi @Pavel A.​ 

YES, CubeIDE provides Build Variable and Environment Variable. I've known those from TrueSTUDIO but didn't have chance to play those until read your question and decided to test them. Please check my record of my testing in the attached PDF.

Thanks for giving me the opportunity to play with them :)

Regards,

Ethan

Hello @Ethan HUANG​ ,

Thank you for your explanations.

Yacob Hassidim.

HMcKi
Associate III

Hi Guys,

I was able to use srec_cat.exe to append the CRC to the end of a binary file. It worked using the -maxiumum-address flag. Took me a while to work out how to get it to work as the examples were a little confusing. Below are a couple of examples that worked for me:

srec_cat.exe firmware_nocrc.bin -Binary -crc32-l-e -maximum-address firmware_nocrc.bin -Binary -o firmware_crc.bin -Binary

srec_cat.exe firmware_nocrc.bin -Binary -STM32_Little_Endian -maximum-address firmware_nocrc.bin -Binary -o firmware_crc.bin -Binary

I have been successfully able to validate the -crc32-l-e in both JS and a C on the micro. The STM32 one doesn't seem to match the hardware CRC with default settings like I was hoping it would. But that's a story for another forum post if anyone can help there.

Cheers,

Hamish.

YHass.14
Senior

Hello @HMcKi​ 

Thank you for your response.

I will try it later.

Yacob Hassidim.

asala.19
Associate III

Hello YHass.14  , Ethan HUANG 

Can anyone please tell me here :-

If i want to store generated CRC just after code memory address or end of the code automatically , flexible with changing code size , how can i implement it ? any explaining site link will be very helpful here.

Regards,

Arjun Salariya

Need to be familiar with C file functions, fopen, fread, fseek, etc.

You probably want the tools to output a binary to work with, processing a .ELF/AXF or .HEX is possible, but we're here.

The size can be inferred from the binary. You can however use linker symbols to write this size into a location in/after the vector table. The exact method differs for Keil vs GNU/GCC, but has been covered on multiple occasions.

https://community.st.com/s/question/0D53W0000037DOESA2/does-cubeide-support-launching-executables-that-arent-elf

The ST CRC works on 32-bit values, so align suitably.

https://community.st.com/s/question/0D50X0000AIeYIbSQN/stm32f4-crc32-algorithm-headache

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..