用C语言编写头文件?

3
为了在嵌入式设备上启动Linux内核,我需要使用特殊的头文件标记内核。用于标记内核的程序由设备制造商提供,仅为32位二进制文件。这非常麻烦,因为我必须在我的64位系统上安装数百兆字节的库,仅仅是为了给几个字节的内核打标记。以下是如何标记内核的方法:
$./mkimage -f kernel.cfg -d zImage_without_header zImage

kernel.cfg:

##########################################################
#ENCINFO.CFG
# 
# information and command for encode the Linux zImage
##########################################################

# Magic number for the ImageHeader, use this to seach start of the Image Header
#
MAGIC_NUMBER 0x27051956

#operation system type
OS_TYPE  linux

#cpu architecture type
CPU_ARCH  arm

#image type
IMAGE_TYPE  kernel

#compress type
COMPRESS_TYPE   gzip

#
DATALOAD_ADDRESS   0x00008000

#
ENTRY_ADDRESS   0x00008000

#image name string
IMAGE_NAME   kernel.img 

#model name string
MODEL_NAME   DNS-313

# version string
VERSION  1.00b18

# mac address string
MAC_ADDRESS          FF-FF-FF-FF-FF-FF

#the beginning offset of writing header
START_OFFSET         0x00

#the end offset of writing header
END_OFFSET           0xFF

#whether overwrite
OVERWRITE            n

mkimage二进制文件与例如Debian存储库中提供的mkimage不同,那个无法在我的设备上工作。我尝试创建一个1MB的文件并标记它以显示头部:

$dd if=/dev/zero bs=1k count=1024 of=zImage_without_header
$./mkimage -f kernel.cfg -d zImage_without_header zImage

上一个命令的输出结果:
Magic Number:   27051956
Image Name:   kernel.img
Created:      Wed May  2 17:40:43 2012
Image Type:   ARM Linux Kernel Image (gzip compressed)
Data Size:    1048576 Bytes = 1024.00 kB = 1.00 MB
Load Address: 0x00008000
Entry Point:  0x00008000
Model Name:   DNS-313
Version   :   1.00b18
Mac Address:  ff:ff:ff:ff:ff:ff  

$hexdump -C zImage

上一个命令的输出:

00000000  27 05 19 56 [2c 83 53 d5] 4f a1 [55 7b 00 10 00 00] |'..V,.S.O.U{....|
00000010  00 00 80 00 00 00 80 00  [a7 38 ea 1c] 05 02 02 01  |.........8......|
00000020  6b 65 72 6e 65 6c 2e 69  6d 67 00 00 00 00 00 00  |kernel.img......|
00000030  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00000040  44 4e 53 2d 33 31 33 00  00 00 00 00 00 00 00 00  |DNS-313.........|
00000050  31 2e 30 30 62 31 38 00  00 00 00 00 00 00 00 00  |1.00b18.........|
00000060  ff ff ff ff ff ff 00 00  00 00 00 00 00 00 00 00  |................|
00000070  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
00100060

核心应该始终标记为像上面的标题,因为我不需要改变任何东西。方括号[]中的值似乎随文件大小而变化,但我不知道原因。
我认为用一个小型C程序也可以完成同样的事情,但我不确定从何开始和如何开始?
欢迎任何建议或想法。

2
请求制造商提供mkimage的源代码。虽然他们理论上可以拒绝,但这对他们来说是一个非常糟糕的主意。 - Joshua
看起来很可能有一些或所有不同的值都是校验和。在你自己创建头之前,你需要知道它们是什么。 - Kristof Provost
1
4f a1 55 7b 是一个时间戳(5月2日 @ 15:40:43 GMT)。其中两个“未知数”是校验和。如果您的引导加载程序是u-boot,则应该是这些字段:http://www.linux-m32r.org/public/codefestweek2008/takata/qemu-0.9.1/html/d1/d14/structuboot__image__header__t.html。 - indiv
1个回答

0

这可能是一个冒险,但如果您没有访问“mkimage”源代码,您可以尝试使用objdump反汇编它并尝试弄清楚发生了什么:

$ objdump -d ./mkimage

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接