inux下 解包/打包 Android 映像文件 system.img, boot.img, ramdisk.img, userdata.img.

本文详细介绍了Android设备使用eMMC进行引导的过程,包括创建GPT分区表、修改.img文件、使用Fastboot进行分区闪存等步骤。同时,提供了如何在Linux环境下解包和打包Android映像文件的指导。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Android eMMC Booting

Contents

  [hide]

[edit]eMMC binaries

This is the efi partition table as exists on the emmc

Sector#    Size Name
    256     128K xloader
    512     256K bootloader
   2048       8M recovery                                                      
  18432       8M boot                                                          
  34816     512M system                                                        
1083392     256M cache                                                         
1607680     512M userdata                                                      
2656256    2183M media

[edit]Creating the GPT table

On Target
  1. Connect a USB cable to the OTG port on your platform
  2. Boot your platform up with a stock u-boot and MLO
  3. Once you platform is booted you will see the following:
Fastboot entered...
On Host Machine

locate fastboot in you android filesystem

cd $mydroid/out/host/linux-x86/bin/fastboot

Search for fastboot devices

fastboot devices

Create GPT table on eMMC/SD card

fastboot oem format

From the android build these are the binaries that go into each partition:

 Sector#    Size Name            Binary
    256     128K xloader         MLO
    512     256K bootloader      u-boot.bin
   2048       8M recovery        recovery.img                                                    
  18432       8M boot            boot.img                                  
  34816     512M system          system.img                                      
1083392     256M cache           cache.img                                    
1607680     512M userdata        userdata.img                                     
2656256    2183M media           none


File locations
  • MLO --> x-loader/MLO
  • u-boot --> u-boot/u-boot.bin
  • boot.img --> need to create using zImage + ramdisk.img
  • recovery.img ---> need to create using zImage + ramdisk-recovery.img
  • system.img --> $mydroid/out/target/product/<platform>/system.img
  • cache.img -->
  • userdata.img --> $mydroid/out/target/product/<platform>/userdata.img


All these partitions can be flashed with the given binary using fastboot.

 fastboot flash <name> <binary>

Example flashing of all partitions

 fastboot flash xloader     MLO
 fastboot flash bootloader  u-boot.bin
 fastboot flash recovery    recovery.img
 fastboot flash boot        boot.img
 fastboot flash system      system.img
 fastboot flash cache       cache.img
 fastboot flash userdata    userdata.img

[edit]Modifying .IMG Files

Typically when you want to modify any of the partitions, you would need to unzip-modify-rezip and then fastboot flash.

Following section talks about how to do that for each partition

BOOT.IMG

 boot.img = zImage + ramdisk.img
 zImage = kernel image
 ramdisk.img = out/target/product/blaze/root/
 %./out/host/linux-x86/bin/mkbootimg 
 --kernel zImage 
 --ramdisk ramdisk.img 
 --base 0x80000000 
 --cmdline "console=ttyO2,115200n8 mem=456M@0x80000000 mem=512M@0xA0000000 init=/init vram=10M omapfb.vram=0:4M androidboot.console=ttyO2" 
 --board omap4 
 -o boot.img.new
 Output: boot.img.new
 **Note: bootarg is passed to kernel via --cmdline option above
 **Note:For Pandaboard ES,--cmdline should be mmodified as "console=ttyO2,115200n8 mem=456M@0x80000000 mem=512M@0xA0000000 init=/init vram=32M omapfb.vram=0:16M androidboot.console=ttyO2"

To "just" boot boot.img (before flashing) you can use:

%fastboot boot boot.img

RAMDISK.IMG

 %mkdir root
 %cd root
 %gunzip -c ../ramdisk.img | cpio -i
 <make changes to root/ contents...>
 %./out/host/linux-x86/bin/mkbootfs root/ | ./out/host/linux-x86/bin/minigzip >ramdisk.img.new
 #output: ramdisk.img.new
 ** Note: any init.rc changes will need to use this method

RECOVERY.IMG

Is just like boot.img. 
recovery.img = zImage + ramdisk-recovery.img
*Follow the same steps as boot.img for packing/unpacking

SYSTEM.IMG

 #uncompress
 %./out/host/linux-x86/bin/simg2img system.img system.img.raw
 #mount to directory mnt-point/
 %mkdir mnt-point
 %sudo mount -t ext4 -o loop system.img.raw mnt-point/
 #modify any .so or apk in the mnt-point/ directory
 #rezip
 %sudo out/host/linux-x86/bin/make_ext4fs -s -l 512M -a system system.img.new mnt-point/
 %sudo umount mnt-point/
 Output: system.img.new

Instead of having to reflash the whole big system.img, one can selective update any binary in /system folder on running target

%adb remount
%adb push <local> <remote>
Eg: 
%adb remount
%adb push out/target/product/blaze/obj/lib/overlay.omap4.so /system/lib/hw/overlay.omap4.so
%adb sync

USERDATA.IMG

 #uncompress
 %./out/host/linux-x86/bin/simg2img userdata.img userdata.img.raw
 #mount to directory mnt-point/
 %mkdir mnt-point
 %sudo mount -t ext4 -o loop userdata.img.raw mnt-point/
 #modify any .so or apk in the mnt-point/ directory
 #rezip
 #%sudo ./out/host/linux-x86/bin/make_ext4fs -s -l 512M -a userdata userdata.img.new mnt-point/
 # Above command won't work on GB/HC. For GB/HC, please use the following updated command
 %sudo ./out/host/linux-x86/bin/make_ext4fs -s -l 512M -a data userdata.img.new mnt-point/
 %sudo umount mnt-point/
 Output: userdata.img.new

CACHE.IMG

 #This is empty ext4 fs image
 %mkdir mnt-point/
 %sudo ./make_ext4fs -s -l 256M -a cache cache.img mnt-point/
 Output: cache.img

[edit]TI Android build setup

Copy kernel zImage, u-boot.bin and MLO for your board in folder device/ti/blaze/boot/.

Rename as:

 %mv MLO MLO_es2.2_emu 
 or 
 %mv MLO MLO_es2.2_gp 
 (based on your board being GP or EMU)

Next start standard android build and all img files are generated in:

out/target/product/blaze/*.img

A script is introduced in TI Android release to make this flashing process easier: device/ti/blaze/boot/fastboot.sh

Usage:
cd device/ti/blaze/boot/
%fastboot.sh --emu
or
%fastboot.sh --gp

Running this script will flash whole android system on your board.

http://omappedia.org/wiki/Android_eMMC_Booting#Modifying_.IMG_Files



inux下 解包/打包 Android 映像文件 system.img, boot.img, ramdisk.img, userdata.img.

Linux下 解包/打包 Android 映像文件 system.img, boot.img, ramdisk.img, userdata.img.

2014年10月20日 ⁄ 计算机视觉 ⁄ 共 1372字⁄ ⁄ 暂无评论

转自: http://blog.youkuaiyun.com/yulix/article/details/12968705

Android源码编译成功后会输出映像文件:system.img,boot.img, ramdisk.img,userdata.img等等。有时我们需要修改里面的内容,下面列出在Linux下如何解包/打包这些映像文件。

ramdisk.img

ramdisk.img是经cpio打包、并用gzip压缩的文件。

解包: 新建一个工作目录,把当前目录更改为该工作目录,执行下面命令(注意: img文件位置可能不同).

[plain] view plaincopy

  1. gunzip -c  $HOME/img/ramdisk.img | cpio -i

打包:在工作目录下,把该目录下的所有内容打包

[plain] view plaincopy

  1. find . | cpio -o -H newc | gzip > ../newramdisk.img

参考文档:  http://android-dls.com/wiki/index.php?title=HOWTO:_Unpack%2C_Edit%2C_and_Re-Pack_Boot_Images

boot.img

boot.img包含2K字节头部,后面跟着的是zImage格式内核和和ramdisk格式的根文件系统。

解包工具: Android自带的unpackbootimg,以及一些脚本工具比如split_bootimg.pl

打包工具: Android自带的mkbootimg。

参考资料 :

中文请看: http://blog.youkuaiyun.com/wh_19910525/article/details/8200372

  英文请看:  http://android-dls.com/wiki/index.php?title=HOWTO:_Unpack%2C_Edit%2C_and_Re-Pack_Boot_Images
system.img (EXT4)

system.img 是 sparse image格式文件,现有的mount命令无法直接处理。

我们得把sparse image格式转变为普通的img格式,Android源码中带的ext4_utils可以做这个,没有Android源码也不用担心,该工具的源代码已被剥离出来,可以自行下载编译,地址是:http://forum.xda-developers.com/showthread.php?t=1081239

我们得到工具有: simg2img,make_ext4fs等等:

解包:

[plain] view plaincopy

  1. simg2img system.img system.img.ext4
  2. mkdir mnt_dir
  3. sudo mount -t ext4 -o loop system.img.ext4 mnt_dir

打包:

[plain] view plaincopy

  1. sudo make_ext4fs -s -l 512M -a system system_new.img mnt_dir

注意:在我的机器上必须用root权限执行make_ext4fs,否则新生成的image文件无法使用。

userdata.img (EXT4)

和system.img(EXT4) 一样处理

 

 

( file_context_open: Error getting file context handle (No such file or directory) 
No such file or directory )

参考下面链接:

http://forum.xda-developers.com/galaxy-s2/general/ref-unpacking-repacking-stock-rom-img-t1081239/page23

the problem is that this version requires a file_contexts file. This can be extracted from boot.img. The filename is file_contexts 
1. extract boot.img (i used AndroidImageKitchen for this) 
2. call make_ext4fs with -S parameter 
i.e.

Code:

make_ext4fs -l -s 2690M -a system -S <PATH_TO_FILE_CONTEXTS_FILE> system.img.ext4 <WHEREEVER_YOU_MOUNTED_SYSTEM_TO>

btw: the name of the mountpoint doesnt matter. So calling your mountpoint sys was no problem

If you have extracted boot.img the file_contexts-file should be located in the ramdisk folder. 
Don't panic if there is no such file. In this case you just have to use an earlier version of make_ext4fs.

The file_contexts file is used by versions of android which use SELinux. I only found this on android versions >= 4.3.

 

 

所以最终的命令是:

./make_ext4fs -s -l 550M -a system -S ./file_contexts system_new.img mnt_dir

 

make_ext4fs [ -l <len> ] [ -j <journal size> ] [ -b <block_size> ] 
    [ -g <blocks per group> ] [ -i <inodes> ] [ -I <inode size> ] 
    [ -L <label> ] [ -f ] [ -a <android mountpoint> ] 
    [ -S file_contexts ] 
    [ -z | -s ] [ -w ] [ -c ] [ -J ] [ -v ] 
    <filename> [<directory>]

 

参数解析

#make_ext4fs -s -l 512M -a root -L linux  ./rootfs_qt.img  ./root

执行之后即会将root文件打包成  rootfs_qt.img 文件系统镜像。

-l 512M"是分区大小,i9100的system分区是512M; 
-s就是生成ext4的S模式制作;

-s  就是生成ext4的S模式制作;

-l 512M 是分区大小;

-a root 是指这个img用于Linux系统若为-a system即表示为android系统,挂载点即是/system。使用这个参数,make_ext4fs会根据private/android_filesystem_config.h里定义好的权限来给文件夹里的所有文件重新设置权限,如果你刷机以后发现有文件权限不对,可以手工修改android_filesystem_config.h来添加权限,重新编译make_ext4fs,也可以不使用 “-a system”参数,这样就会使用文件的默认权限

./rootfs_qt.img 表示在当前目录下生成镜像文件。

./root 指定源路径


使用方法:http://blog.csdn.net/asmcvc/article/details/11770851 工具: unyaffs,mkyaffs2image 其中unyaffs有windows版本和linux版本,mkyaffs2image只有linux版本。 windows版本的unyaffs用法: 把system.img复制到unyaffs的相同目录下,cmd命令下cd到unyaffs的目录下,然后执行命令:unyaffs system.img unyaffs会把system.img解压到其目录下。 linux版本的unyaffs用法: 把unyaffs复制到/usr/bin目录下,并修改权限为可执行。 然后cd到system.img目录下(假定目录为system目录),执行命令:unyaffs system.img 然后对system目录下的文件进行修改。 注意:修改完后的文件要修改一下权限,尽量和其他文件的权限保持一致。例如:chmod 644 framework-res.apk mkyaffs2image用法: 复制到/usr/bin目录下,并修改权限为可执行。 这里以打包system目录为system.img为例,执行命令: mkyaffs2image system system.img 然后把新生成的system.img复制替换掉原:adt-bundle-windows-x86\sdk\system-images\android-17\armeabi-v7a\system.img 执行bat批处理命令启动模拟器: D:\adt-bundle-windows-x86\sdk\tools\emulator-arm.exe -avd AndroidVM -partition-size 128 这里以修改android系统启动画面为例: 打开解包目录下的\framework\framework-res.apk 替换图片:framework-res.apk\assets\images\android-logo-mask.png为下图: 然后对\framework\framework-res.apk文件重新签名,复制到linux下后修改文件权限和原来一致。 然后mkyaffs2image system system.img打包生成新的system.img,替换原来的system.img,并启动模拟器,效果图如下: 修改代码: 工具:odextools(参考:《一键odex批量合并工具odextools的重新整理与使用》)、dexopt-wrapper 其中odextools.bat的代码: 批处理有一处bug:每打包一次会把odex文件删除掉,导致在后面的打包过程中会出现找不到:system/framework/core.odex类似的错误,因此只需要在打包完后不删除odex文件即可,找到del /f !apkx!.odex 1>nul 2>nul改为:::del /f !apkx!.odex 1>nul 2>nul,也就是注释掉这一行代码。 具体使用方法(操作在windows下): 在odextools\romdir目录下创建文件夹:system 利用unyaffs解包system.img后,把所有文件复制到system目录下。 然后运行odextools.bat,如图: 选择一个需要整合odex的目录选项即可。odextools.bat会自动设置环境变量,使用baksmali.jar来反编译odex为smali,然后再调用smali.jar打包为classes.dex, 然后再打包到相应的apk包(framework目录下对应的是jar后缀的,实际上也是个apk包),最后再重新签名。 如果要修改代码,则需要把上面重新打包生成的apk文件,利用常规方法反编译后修改smali代码,例如插桩输入log信息。然后再回编译并重新签名。 最后一步:因为system.img中的apk是优化过的,apk主目录下是没有classes.dex文件的,而是一个被优化过的odex文件,用于优化启动速度。 因此需要将修改后的apk包再用dexopt-wrapper优化apk包后生成出odex文件,然后删除apk包里的classes.dex,并在相同目录下放置与apk包同名的odex文件。 按照原system目录的文件结构组织好后,目录复制到linux环境下使用mkyaffs2image重新打包system.img
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值