Skip to main content
YHass.14
Associate III
August 1, 2019
Question

How can I calculate CRC of image in post build?

  • August 1, 2019
  • 9 replies
  • 8460 views

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.

This topic has been closed for replies.

9 replies

waclawek.jan
Super User
August 1, 2019

srecord is one of the tools which can be used for this purpose.

JW

YHass.14
YHass.14Author
Associate III
August 1, 2019

Hello,

JW: Can you please detail?

Yacob Hassidim.

Tesla DeLorean
Guru
August 1, 2019

Ok, so what's the problem here? You've described the steps

Doesn't IELFTOOL provide documentation for the command line expectations?

Does CubeIDE provide for a method to run scripts, or add lines to the makefile?

If you convert to a binary you could presumably write your own tools to read the file and add the CRC to the end. I've posted examples.

You can embedded image size data via linker symbols so the STM32 side code knows where the end of the code and copied statics falls.

https://community.st.com/s/question/0D50X0000Awa25qSQA/generated-binary-file-size-in-stm32l4

Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..
YHass.14
YHass.14Author
Associate III
August 1, 2019

@Community member​: Thanks for the link. It helped me.

I should try your suggestions from the link.

Yacob Hassidim.

Ethan HUANG
ST Employee
August 12, 2019

Hi Yacob,

Here is a step-by-step for your reference:

https://github.com/ethanhuanginst/STM32CubeIDE-Workshop-2019/tree/master/hands-on/06_F746-DISCO-CRC

Please note that this step-by-step is made for STM32CubeIDE v.1.0.0 which has bug of the order of post-build so an workaround (using a batch file checksum.bat in post-build command) is used. This bug has been fixed after STM32CubeIDE v1.0.2 so we can simply use the following without using the batch file:

  1. Add the directory of srec_cat in PATH of Windows Environment Variables.
  2. Enable "Conver to Intel Hex file (-O ihex) in MCU Post build outputs in Tool Settings tab.
  3. Use the following in Post-build steps in Build Steps tab: srec_cat.exe 06_F746-DISCO-CRC.hex -Intel -fill 0xFF 0x08000000 0x08007FFC -STM32 0x08007FFC -o 06_F746-DISCO-CRC_SRECORD.hex -Intel

Regards,

Ethan

Pavel A.
Super User
August 12, 2019

Interesting, a custom switch for srec-cat (--STM32).

But adding it to the PATH... Does CubeIDE provide a built-in variable for the toolkit binary path, like other Eclipse based environments?

-- pa

waclawek.jan
Super User
August 18, 2019

> How can I calculate the CRC of actual code, i.e. end of the range will not be constant "0x08007FFC" but as actual end of the code?

srec_cat without the -fill switch will do exactly this. The problem is then, where to put the checksum, as srec_cat requires an address.

Play with srec_cat to understand how it works. Read the srecord manual; I know that it's not written very well and it's not an easy reading.

Or, as Clive said above, write your own program to add the checksum - it will then work exactly as you want.

JW

YHass.14
YHass.14Author
Associate III
August 18, 2019

Hello @Community member​ 

Thank you for your advice.

Yacob Hassidim.

HMcKi
Associate III
February 12, 2020

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
YHass.14Author
Associate III
February 13, 2020

Hello @HMcKi​ 

Thank you for your response.

I will try it later.

Yacob Hassidim.

asala.19
Associate III
January 6, 2021

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

Tesla DeLorean
Guru
January 6, 2021

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 VenmoUp vote any posts that you find helpful, it shows what's working..