概述
Android 产品中,内核格式是Linux标准的zImage,根文件系统采用ramdisk格式。这两者在Android下是直接合并在一起取名为boot.img,会放在一个独立分区当中。这个分区格式是Android自行制定的格式。
Android开发时,最标准的做法是重新编译于内核和根文件系统,然后调用Android给的命令行文件mkbootimg来打包。
常用的就是打包和解包工具:unpackbootimg 和 mkbootimg
bootimg格式
源码包中的bootimg.h
有很多的版本可参考,下面的是version 1
/* When a boot header is of version 1, the structure of boot image is as | |
* follows: | |
* | |
* +---------------------+ | |
* | boot header | 1 page | |
* +---------------------+ | |
* | kernel | n pages | |
* +---------------------+ | |
* | ramdisk | m pages | |
* +---------------------+ | |
* | second stage | o pages | |
* +---------------------+ | |
* | recovery dtbo/acpio | 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 = (recovery_dtbo_size + page_size - 1) / page_size | |
* | |
* 0. all entities are page_size aligned in flash | |
* 1. kernel and ramdisk are required (size != 0) | |
* 2. recovery_dtbo/recovery_acpio is required for recovery.img in non-A/B | |
* devices(recovery_dtbo_size != 0) | |
* 3. second is optional (second_size == 0 -> no second) | |
* 4. load each element (kernel, ramdisk, second) at | |
* the specified physical address (kernel_addr, etc) | |
* 5. If booting to recovery mode in a non-A/B device, extract recovery | |
* dtbo/acpio and apply the correct set of overlays on the base device tree | |
* depending on the hardware/product revision. | |
* 6. set up registers for kernel entry as required by your architecture | |
* 7. if second_size != 0: jump to second_addr | |
* else: jump to kernel_addr | |
*/ | |
来源:platform/system/tools/mkbootimg - Git at Google
打包工具
boot.img可以打包任何操作系统内核,包括但不限于Linux, 格式也可以根据需求进行修改。解/打包工具都可以自己实现,源码中包含mkbootimg.py
, repack_bootimg.py
和unpack_bootimg.py
等可供参考。
安卓mkbootimg源码归档位置:
git clone https://gitee.com/whilewell/mkbootimg
打包ramdisk
$ cd ramdisk | |
$ find . | cpio -o -H newc | gzip > ../newramdisk.img |
打包bootimg
常用打包工具可以是安卓自带的mkbootimg,也可以是abootimg
mkbootimg
./mkbootimg --cmdline 'no_console_suspend=1 console=null' --kernel zImage --ramdisk boot/boot.img-ramdisk.gz -o boot.img --base 02e00000 |
abootimg
abootimg --create boot.img -k zImage -r newramdisk.img -c "pagesize=2048" -c "kerneladdr=0x80208000" -c "ramdiskaddr=0x82200000" -c "tagsaddr=0x80200100" -c "cmdline=console=ttyHSL0,115200n8 ..." |
解包工具
解压bootimg
$ abootimg -x boot.img | |
$ ls | |
bootimg.cfg # boot header | |
zImage # kernel | |
initrd.img # ramdisk |
解压 ramdisk
$ mkdir ramdisk | |
$ cd ramdisk | |
$ gunzip -c ../initrd.img | cpio -i | |
$ ls | |
data | |
dev | |
proc | |
... |