The application of CRC (Cyclic Redundancy Check) in the embedded field is very extensive. For example, in the field of communication, the CRC check value can be included as part of a data packet to check for bit errors during data transmission. If the CRC check fails, the receiver can notify the sender to request retransmission of the packet, greatly improving the reliability of data transmission. Additionally, CRC is widely used in application program integrity verification. Compared with parity checks, CRC has stronger error correction capabilities. Compared with signature verification, CRC has the advantage of speed. Therefore, CRC is a well-balanced integrity verification method in various aspects.
- ielftool command line tool
There are many command line gadgets in the IAR installation directory, these gadgets are actually the core of IAR. The IDE interface only provides human-computer interaction, and many of the functions behind it are realized by calling these command line gadgets. One of the functions used to achieve CRC checksum is contained in the ielftool.exe tool:
The ielftool.exe tool has a total of two parameter options related to CRC, one is --fill for filling and the other is --checksum for setting the algorithm, see \IAR Systems\Embedded Workbench 8.50.6\arm\doc\EWARM_ for details on the use of parameters. DevelopmentGuide.ENU manual in the Checksum calculation for verifying image integrity subsection.
Here is just a simple example, the following command indicates that the CRC result between the range __checksum_begin - __checksum_end is calculated in the source executable sourceFile.out, the final checksum value __checksum length is 4 bytes, the fixed CRC32 algorithm, the calculation unit is 1 byte, the specified CRC The initial value is 0xffffffff, the rest is set to default, and the result is placed in the target executable destinationFile.out.
ielftool --fill="0xFF;__checksum_begin–__checksum_end"
--checksum="__checksum:4,crc32:p,0xffffffff;__checksum_begin-__checksum_end"
sourceFile.out
destinationFile.out
As you should know from the above command, sourceFile.out is not an arbitrary executable, it must contain the necessary definitions of __checksum_begin, __checksum_end, and __checksum. Assuming we don't actually use CRC in our code, we need to set the following in the IAR project options of the generated sourceFile.out file:
Note: The four names __checksum_begin, __checksum_end symbol, __checksum variable, and .checksum segment are user-definable, and are named as such here mainly to unify with the default IAR definition.
Let's find a random embedded IAR project to generate the source hello_world.out file according to the above settings and execute the command once using the ielftool tool to see that the __checksum value is generated and the newly generated hello_world_1.out file contains the correct CRC checksum result.
- Add CRC checksum to the project
Under the project options Linker/Checksum, check Fill unused code memory and Generate checksum will enable CRC checksum, the two addresses in the blue box are used to set the CRC calculation range, the parameters in the green box are used to set the CRC algorithm details, the specific meaning of each configuration can be viewed \IAR Systems\Embedded Workbench 8.50.6\arm\doc\EWARM_DevelopmentGuide.ENU manual in the Checksum section
The following sample configuration has two points to explain: First, the algorithm selected CRC32, its polynomial coefficient is actually fixed to 0x04C11BD7; if you want to customize the CRC32 polynomial coefficient value, optional CRC polynomial; Second, because the address is set to 0x0 - 0x400, a total of 1025 bytes (including 0x400), so checksum unit size here can only choose 8-bit, because the IDE mandatory requirements of address alignment.
After setting it up and recompiling the project, we will get the error "ielftool error: The string '__checksum' was not found in the string table", because we don't have any CRC-related references in the program code at all. IAR will ignore the CRC function enabled in the interface, so we also need to force __checksum into the following option (note that __checksum is the default CRC check value symbol name defined in IAR).
At this point, you can compile the project again and see the CRC information in the generated.out and.map files. __checksum is randomly placed at 0x2844 by the linker, and __checksum_begin and __checksum_end are the symbolic variable names that IAR records the CRC calculation range by default.
If you want to determine the location of the __checksum link yourself, you can add to the project link file the placement of section.checksum, which corresponds to __checksum. for example, let's try placing this section at 0x1000.
Compile the project again and look at the map file, this time __checksum is placed at the 0x1000 location we specified.
Comments