繁体   English   中英

用C程序写标题?

[英]Write header with C program?

为了在嵌入式设备上启动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程序可以实现同样的目标,但我不知道从哪里开始以及如何开始?

欢迎任何建议或想法。

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

$ objdump -d ./mkimage

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM