以msm8226为例,讲解android源码编译生成boot.img的结构。boot.img包括boot.img header、kernel以及ramdisk文件系统。下面是对boot.img的结构进行解读。
boot.img 文件头信息的具体结构可以在system/core/mkbootimg/bootimg.h中看到:
typedef struct boot_img_hdr boot_img_hdr;
#define BOOT_MAGIC "ANDROID!"
#define BOOT_MAGIC_SIZE 8
#define BOOT_NAME_SIZE 16
#define BOOT_ARGS_SIZE 512
struct boot_img_hdr
{
unsigned char magic[BOOT_MAGIC_SIZE];
unsigned kernel_size; /* size in bytes */
unsigned kernel_addr; /* physical load addr */
unsigned ramdisk_size; /* size in bytes */
unsigned ramdisk_addr; /* physical load addr */
unsigned second_size; /* size in bytes */
unsigned second_addr; /* physical load addr */
unsigned tags_addr; /* physical addr for kernel tags */
unsigned page_size; /* flash page size we assume */
unsigned dt_size; /* device tree in bytes */
unsigned unused; /* future expansion: should be 0 */
unsigned char name[BOOT_NAME_SIZE]; /* asciiz product name */
unsigned char cmdline[BOOT_ARGS_SIZE];
unsigned id[8]; /* timestamp / checksum / sha1 / etc */
};
boot,img文件跳过2k的文件头之后,包括两个 gz包,一个是boot.img-kernel.gz:Linux内核,一个是boot.img-ramdisk.cpio.gz
大概的组成结构如下:
** +-----------------+
** | boot header | 1 page
** +-----------------+
** | kernel | n pages
** +-----------------+
** | ramdisk | m pages
** +-----------------+
** | second stage | o pages
** +-----------------+
** | device tree | p pages
** +-----------------+
** n = (kernel_size + page_size - 1) / page_size
** m = (ramdisk_size + page_size - 1) / page_size
** o = (second_size + page_size - 1) / page_size
** p = (dt_size + page_size - 1) / page_size
图一是boot.img头文件结构解析:
圈一:安卓的boot.img标准的文件头格式:ANDROID!(8字节)
圈二:kernel_size(4字节)
圈三:kernel_addr(4字节)
圈四:ramdisk_size(4字节)
圈五:ramdisk_addr(4字节)
圈六:second_stage_size(4字节)
圈七:second_stage_addr(4字节)
图八:tags_addr(4字节)
圈九:page_size(4字节)
boot.img头文件信息,kernel_addr,ramdisk_addr,second_stage_addr在bootable\bootloader\lk\target\msm8226\Rules.mk中设置:
PLATFORM := msm8226
MEMBASE := 0x0FF00000 # SDRAM
MEMSIZE := 0x00500000 # 5MB
BASE_ADDR := 0x00000
TAGS_ADDR := BASE_ADDR+0x00000100
KERNEL_ADDR := BASE_ADDR+0x00008000
RAMDISK_ADDR := BASE_ADDR+0x01000000
SCRATCH_ADDR := 0x10400000
page是页长,为1k(1024)的整数倍。此处为0x00000800,十进制为2048。
结合实际的boot.img文件,进行验证。如图二,是msm8226 boot.img
解析msm8226 boot.img内容,可以得到:
base=0x00000000
kernel_size=0x005ff6f0
kernel_addr=0x00008000+base=0x00008000
ramdisk_size=0x00195de2
ramdisk_addr=0x01000000+base=0x01000000
sencond_stage_size=0x00000000
sencond_stage_addr=0x00f00000+base=0x00f00000
tag_size=0x00000100
page_size=0x00000800(2KB)
详情查看:boot格式文件结构解析实例