9

I'm currently working on a C program in the LPCXpresso (eclipse-based) tool-chain on Windows 7, an IDE with gcc targeting the an NXP Cortex M3 microprocessor. It provides a simple way to compile-link-program the microprocessor over JTAG. The result of a build is an AXF file (ELF format) that is loaded by a debug configuration.

The loaded program resides in Flash memory from 0x00000 to 0x3FFFB. I'd like to include a 4-byte CRC-32 at 0x3FFFC to validate the program at start-up. I added another section and use the gcc __attribute__ directive to access that memory location.

uint32_t crc32_build __attribute__ ((section(".text_MFlashCRC")));

To compute and store the CRC-32 value, my plan was to use SRecord with the following post-build steps:

arm-none-eabi-size "${BuildArtifactFileName}"
arm-none-eabi-objcopy -O binary "${BuildArtifactFileName}" "${BuildArtifactFileBaseName}.bin"
checksum -p ${TargetChip} -d "${BuildArtifactFileBaseName}.bin"
../util/srec_cat "${BuildArtifactFileBaseName}.bin" -binary -crop 0 0x3FFFC -fill 0xFF 0x00000 0x3FFFC -crc32-b-e 0x3FFFC -o "${BuildArtifactFileBaseName}.crc.bin" -binary
echo ""
echo "CRC32:"
../util/srec_cat "${BuildArtifactFileBaseName}.crc.bin" -binary -crop 0x3FFFC 0x40000 -o - -hex-dump

This creates a binary with a checksum (necessary for bootloader) and then computes the CRC over the used Flash memory, storing the CRC value at 0x3FFFC.

However, I don't think I can load the binary file using the debugger. There is a built in programming utility with LPCXpresso that can load the modified binary file, however, that doesn't let me debug. I believe I can then try to start a debugging session with the original AXF file using "attach-only" mode, however, this becomes cumbersome.

I've been able to use readelf to inspect the crc32_build variable in the AXF file. Is there a way to edit the variable in the AXF file? Is there an industry-standard approach to inserting a CRC as a post-build step?

4

1 回答 1

14

没有我知道的行业标准。有多种技术可以做到这一点。我建议您在“C”中使用crc32_buildasextern并通过链接描述文件定义它。例如,

  $ cat ld.script
  .text : {
    _start_crc_region = .;
    *(.text);
    crc32_build = .;
    LONG(CALC_CRC);
    _end_crc_region = .;
  }

您将值CALC_CRC作为零传递给第一次调用,然后与值集重新链接。例如,

 $ ld --defsym=CALC_CRC=0 -T ld.script *.o -o phony.elf
 $ objcopy -j sections phony.elf -o phony.bin # sections means checksum 'areas'
 $ ld --defsym=CALC_CRC=`crc32 phony.bin` -T ld.script *.o -o target.elf

我使用这种技术为图像添加数字签名;它应该同样适用于crc值。链接描述文件允许您定位变量,这对于 CRC 等完整性检查通常很重要,但对于简单的校验和则无关紧要。链接描述文件还允许您为区域的开始和结束定义符号。没有脚本,你需要一些精灵内省。

您当然可以扩展这个想法以包括初始化数据和其他分配的部分。在某些时候,您需要使用objcopy来提取部分并在构建时进行完整性检查。这些部分可能有各种对齐约束,在进行构建时间crc计算时,您需要在主机上模仿这一点(在上面的phony.bin中)。

作为奖励,当您生成srec文件时,一切都已经完成。

如果您遇到问题--defsym,您可以使用sedawkperlpython等预处理ld.script并用CALC_CRC所在的十六进制值替换文本。

于 2014-06-11T14:57:05.493 回答