c - 用C程序写header?

标签 c linux

为了在嵌入式设备上启动 Linux 内核,我必须用特殊的 header 标记内核。用于标记内核的程序由设备制造商提供,仅作为 32 位二进制文​​件。这非常烦人,因为我不得不在我的 64 位系统上安装数百兆字节的库,只是为了用几个字节标记内核。这是内核的标记方式:

$./mkimage -f kernel.cfg -d zImage_without_header zImage

内核配置文件:

##########################################################
#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 二进制文件与可从例如网站获得的 mkimage 不同。 Debian 存储库,该存储库不适用于我的设备。我试图创建一个 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

内核应该总是用上面的 header 标记,因为我不需要更改任何东西。括号 [] 中的值似乎在文件大小发生变化时发生变化,但我不知道如何变化。

我认为同样的事情可以用一个小的 C 程序来完成,但我不确定从哪里开始以及如何开始?

欢迎提出任何建议或想法。

最佳答案

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

$ objdump -d ./mkimage

关于c - 用C程序写header?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10420408/

相关文章:

linux - 在 Linux RHEL 6.7 上安装没有 YUM 的 MongoDB

c++ - 奇怪的,float类型的参数在windows中传入函数

linux - 替换文件中的动态行

c - 连接超时在 select() 调用的上下文中意味着什么

c99 - 错误 : unknown type name ‘pid_t’

c - 需要将内存对齐到 2 的幂,并将程序中断对齐到 2 * getpagesize() 的倍数(C 中)

c - 如何使用 NativeCall 处理可变参数

c - 将许多参数传递给 C 函数

linux - 使用 crontab & nohup 将输出重定向到具有日期格式名称的文件

mysql从linux转储到windows