Appending a checksum for application self‐checking - iarsystems/cmake-tutorial GitHub Wiki
This interactive example describes how to append a checksum for application self-checking.
add_custom_target()- Checksum calculation for verifying image integrity Arm | RH850 | RISC-V | RL78 | RX
A CMake project example is provided at examples/checksum:
| Project files |
|---|
CMakeLists.txt |
crc32.c |
crc32.h |
generic_cortex.icf |
main.c |
The project links the executable target using the linker configuration from generic_cortex.icf for a Generic Arm Cortex device.
In the linker configuration, a block is defined to hold all the read-only data followed by a a section named .checksum:
// This if() block is the relevant change for ielftool's checksum placement
if (!isempty(ROM_region))
{
// Application block with ielftool checksum placeholder at end
define block APP with fixed order {
readonly,
ro section .checksum
};
place in ROM_region { block APP };
}So that the checksum symbol will be placed right after the application ends.
The application uses the crc32() function from crc32.c to perform self-checking at initialization.
The .checksum placeholder should then be filled at build-time by the IAR ELF Tool (${CMAKE_IAR_ELFTOOL}) so that it can be downloaded into the target alongside the application. The interactive exercise will be to insert the IAR ELF Tool command to generate the checksum at build-time so that the application can perform self-check during the runtime.
- Perform the following task on
CMakeLists.txt(click to show/hide answers):
TODO 1: Add a post build command for placing a checksum at the end of the application
add_custom_target(checksummed ALL
DEPENDS checksum
COMMAND ${CMAKE_IAR_ELFTOOL}
--silent #--verbose
--fill="0xFF\;0x0000'0000-0x0007'FFFF"
--checksum="__checksum:4,crc32:Li,0xFFFFFFFF\;0x0000'0000-__checksum"
$<TARGET_FILE:checksum>
$<TARGET_FILE:checksum>
)-
Build and test the project. Refer to the tutorial for more information.
-
Perform the following task on
main.c(click to show/hide answers):
TODO 2: Change the data vector size or its contents
__root const uint8_t data[16384] = { 0, 1 }; - Rebuild and test the project. Changing the data vector contents modified the application, hence a new checksum should be calculated, and yet, the consistency should be kept between the checksum etched to the application image at build time and the value yielded at runtime.
Note
This application was designed for the IAR C/C++ Toolchain for Arm on a simulated Arm Cortex-M4 device. It can be re-targeted to a specific device by changing the linker configuration, and updating the CMakeLists.txt accordingly.