将 CRC 存储到 AXF/ELF 文件中 [英] Storing CRC into an AXF/ELF file

查看:30
本文介绍了将 CRC 存储到 AXF/ELF 文件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在 Windows 7 上的 LPCXpresso(基于 Eclipse) 工具链中开发 C 程序,一个带有 gcc 的 IDE,针对 NXP Cortex M3 微处理器.它提供了一种通过 JTAG 编译-链接-编程微处理器的简单方法.构建的结果是由调试配置加载的 AXF 文件(ELF 格式).

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.

加载的程序驻留在闪存中,从 0x00000 到 0x3FFFB.我想在 0x3FFFC 处包含一个 4 字节的 CRC-32 以在启动时验证程序.我添加了另一个部分并使用 gcc __attribute__ 指令访问该内存位置.

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")));

为了计算和存储 CRC-32 值,我的计划是使用 SRecord 和以下构建后步骤:>

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

这会创建一个带有校验和的二进制文件(引导加载程序所必需的),然后通过使用的闪存计算 CRC,将 CRC 值存储在 0x3FFFC.

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.

但是,我不认为我可以使用调试器加载二进制文件.LPCXpresso 有一个内置的编程实用程序,可以加载修改后的二进制文件,但是,这不允许我进行调试.我相信我可以尝试使用仅附加"模式使用原始 AXF 文件启动调试会话,但是,这变得很麻烦.

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.

我已经能够使用 readelf 检查 AXF 文件中的 crc32_build 变量.有没有办法编辑 AXF 文件中的变量?是否有行业标准的方法来插入 CRC 作为构建后的步骤?

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?

推荐答案

没有我所知道的行业标准.有多种技术可以做到这一点.我建议您使用 crc32_build 作为 'C' 中的 extern 并通过链接器脚本定义它.例如,

There is no industry standard that I am aware of. There are various techniques to do this. I would suggest that you use the crc32_build as an extern in 'C' and define it via a linker script. For instance,

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

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

You pass the value CALC_CRC as zero for a first invocation and then relink with the value set. For instance,

 $ 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 这样的完整性检查通常很重要,但对于简单的校验和则无关紧要.链接描述文件还允许您为区域的开始和结束定义符号.没有脚本,你需要一些elf自省.

I use this technique to add digital signing to images; it should apply equally well to crc values. The linker script allows you to position the variable, which is often important for integrity checks like a CRC, but wouldn't matter for a simple checksum. A linker script also allows you to define symbols for both the start and end of the region. Without a script, you need some elf introspection.

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

You can of course extend the idea to include init data and other allocated sections. At some point you need to use objcopy to extract the sections and do the integrity check at build time. The sections may have various alignment constraints and you need to mimic this (in phony.bin above) on the host when doing the build time crc calculation.

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

As a bonus, everything is already done when you generate an srec file.

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

If you have trouble with --defsym, you can just pre-process the ld.script with sed, awk, perl, python, etc and substitute text with a hex value where CALC_CRC is.

这篇关于将 CRC 存储到 AXF/ELF 文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆