android boot.img的生成

[Android]构建boot.img(三):boot.img的生成与结构


1 #build/core/Makefile
2  
3 INTERNAL_BOOTIMAGE_ARGS := \
4  
5             --kernel $(INSTALLED_KERNEL_TARGET) \
6  
7             --ramdisk $(INSTALLED_RAMDISK_TARGET)

显然,boot.img中包含了Image和ramdisk.img文件,但boot.img中的内容远不只这么多,本文将介绍

boot.img中的其它参数,boot.img的生成以及最终boot.img的组成格式.

INTERNAL_BOOTIMAGE_ARGS还包含以下内容:

1.附加的内核命令行(cmdline): BOARD_KERNEL_CMDLINE

同样在build/core/Makefile中,有以下一段内容(strip起到去除空格的作用):

1 BOARD_KERNEL_CMDLINE := $(strip $(BOARD_KERNEL_CMDLINE)
2  
3 ifdef BOARD_KERNEL_CMDLINE
4  
5     INTERNAL_BOOTIMAGE_ARGS += --cmdline "$(BOARD_KERNEL_CMDLINE)"
6  
7 #endif

而BOARD_KERNEL_CMDLINE则在文件device/telechips/tcc88xx-common/BoardConfigCommon.mk中定义:

1 BOARD_KERNEL_CMDLINE := console=ttyTCC, 115200n8

2.内核加载的基地址,BOARD_KERNEL_BASE

同样在build/core/Makefile中,有以下一段内容:

1 BOARD_KERNEL_BASE := $(strip $(BOARD_KERNEL_BASE))
2  
3 ifdef BOARD_KERNEL_BASE
4  
5     INTERNAL_BOOTIMAGE_ARGS += --base $(BOARD_KERNEL_BASE)
6  
7 endif

而BOARD_KERNEL_BASE也在device/telechips/tcc88xx-common/BoardConfigCommon.mk中定义。

1 BOARD_KERNEL_BASE := 0x40000000

3.映像的页面大小:BOARD_KERNEL_PAGESIZE

同样在build/core/Makefile中,有以下一段内容:

1 BOARD_KERNEL_PAGESIZE:= $(strip $(BOARD_KERNEL_PAGESIZE))
2  
3 ifdef BOARD_KERNEL_PAGESIZE
4  
5     INTERNAL_BOOTIMAGE_ARGS += --pagesize $(BOARD_KERNEL_PAGESIZE)
6  
7 endif

BOARD_KERNEL_PAGESIZE 却device/telechips/tcc8800/BoardConfig.mk中定义:

1 BOARD_KERNEL_PAGESIZE := 8192

剩下的内容就是生成boot.img的关键语句,在 build/core/Makefile中,内容如下:

1 INSTALLED_BOOTIMAGE_TARGET := $(PRODUCT_OUT)/boot.img
2  
3 $(INTALLED_BOOTIMAGE_TARGET) : $(MKBOOTIMG) $(INTERNAL_BOOTIMAGE_FILE
4  
5       $(hide) $(MKBOOTIMG) $(INTERNAL_BOOTIMAGE_ARGS)  --output $@

到此,我们可以知道 INTERNAL_BOOTIMAGE_ARGS的内容是:

1 --kernel     out/target/product/tcc8800/kernel
2 --ramdisk   out/target/product/tcc8800/ramdisk.img
3 --cmdline   console=ttyTCC,115200n8
4 --base 0x40000000  --pagesize 8192

而预知boot.img的格式,必须查看MKBOOTIMG这个程序,其实就是out/host/linux-x86/bin/mkbootimg中的mkbootimg程序。

mkbootimg程序由system/core/mkbootimg工程生成得到,为此我们来看看其中的mkbootimg.c文件,其中有这样一段:

01 //信息头部分
02 if(write(fd,&hdr,sizeof(hdr)) != sizeof(hdr)) goto fail;
03 if(write_padding(fd,pagesize,sizeof(hdr))) goto fail;
04  
05 //内核部分
06 if(write(fd,&kernel_data, hdr.kernel_size)!= hdr.kernel_size)
07    goto fail;
08 if(write_padding(fd,pagesize,hdr.kernel_size)) goto fail;
09  
10 //文件系统部分
11 if(write(fd,&ramdisk_data,hdr.ramdisk_size)!= hdr.ramdisk_size)
12    goto fail;
13 if(wirte_padding(fd,pagesize,hdr.ramdisk_size)) goto fail;

可见boot.img是由文件头信息,内核数据以及文件系统数据组成,它们之间非页面对齐部分用0填充(可以

查看write_padding的代码),文件头信息的具体结构可以在system/core/mkbootimg/bootimg.h中看到:

01 struct boot_img_hdr
02  
03 {
04  
05     unsigned char magic[BOOT_MAGIC_SIZE];
06  
07     unsigned  kernel_size;
08  
09     unsigned  kernel_addr;
10  
11     unsigned  ramdisk_size;
12  
13     unsigned  ramdisk_addr;
14  
15     unsigned  second_size;
16  
17     unsigned  second_addr;
18  
19     unsigned  tags_addr;
20  
21     unsigned  page_size;
22  
23     unsigned  unused[2];
24  
25     unsigned  char  name[BOOT_NAME_SIZE]
26  
27     unsigned  char cmdline[BOOT_ARGS_SIZE]
28  
29     unsigned  id[8]; //存放时间戳,校验和,SHA加密等内容
30  
31 }

其它成员也很明了,由此可知boot.img的大致组成结构了。
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值