`
accuya
  • 浏览: 12719 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
最近访客 更多访客>>
社区版块
存档分类
最新评论

从android image中提取文件

 
阅读更多

HOWTO: Unpack, Edit, and Re-Pack Boot Images

Several people have already figured out the details on their own, but I have gotten requests to do a more comprehensive tutorial on how the boot and recovery images are structured, and how you can edit them.

Contents

[hide ]

Background

Your phone has several devices which hold different parts of the filesystem:

#cat /proc/mtd
dev:    size   erasesize  name
mtd0: 00040000 00020000 "misc"
mtd1: 00500000 00020000 "recovery"
mtd2: 00280000 00020000 "boot"
mtd3: 04380000 00020000 "system"
mtd4: 04380000 00020000 "cache"
mtd5: 04ac0000 00020000 "userdata"

Note that the order is different for different phones! Check yours to make sure you use the right device.

In this tutorial, we will deal with "recovery" and "boot". "system" holds everything that gets mounted in your system/ directory, and userdata/ is everything that shows up in data/ (this is all the apps you've installed, your preferences, etc).

The recovery and boot partitions are at /dev/mtd/mtd1 and /dev/mtd/mtd2, and before you do anything else you should back these up:

# cat /dev/mtd/mtd1 > /sdcard/mtd1.img
# cat /dev/mtd/mtd2 > /sdcard/mtd2.img

(Note added by lxrose: These commands don't work if your phone is not rooted and the permissions are not set.)

The other thing you should do is put your favorite update.zip file into the root directory of your sd card so that if you screw up your boot partition you can boot into recovery mode and re-apply the update. You probably want one of the pre-rooted recovery images found elsewhere on the forums.

There is also another important file you should know about. In /system/recovery.img there is a full copy of everything that is loaded on mtd1. This file is automatically flashed onto mtd1 every time you shut down. That means two things: 1. Any changes you make directly to /dev/mtd/mtd1 get blown away on reboot and 2. If you want to change /dev/mtd/mtd1 you're probably better off just sticking the image in /system/recovery.img and rebooting. When creating your own custom update.zip files (especially when adapting the stock images), you can get tripped up if you forget to replace /system/recovery.img and it ends up overwriting /dev/mtd/mtd1 unbeknownst to you. Watch out.

Structure of boot and recovery images

The boot and recovery images are not proper filesystems. Instead, they are a custom android format consisting of a 2k header, followed by a gzipped kernel, followed by a ramdisk, followed by a second stage loader (optional, we have not seen these in the wild yet - except on Sanyo Zio). This structure is outlined in mkbootimg.h :

+-----------------+ 
| boot header     | 1 page
+-----------------+
| kernel          | n pages  
+-----------------+
| ramdisk         | m pages  
+-----------------+
| second stage    | o 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

0. all entities are page_size aligned in flash
1. kernel and ramdisk are required (size != 0)
2. second is optional (second_size == 0 -> no second)

A ramdisk is basically a small filesystem containing the core files needed to initialize the system. It includes the critical init process, as well as init.rc, which is where you can set many system-wide properties. If you really want to know more about it, here is the documentation . Here's a list of files on a typical ramdisk:

./init.trout.rc
./default.prop
./proc
./dev
./init.rc
./init
./sys
./init.goldfish.rc
./sbin
./sbin/adbd
./system
./data

The recovery image typically has a few extra files, which constitute the recovery binary and supporting files (the application that gets run if you hold down home+power when rebooting). These files are:

./res
./res/images
./res/images/progress_bar_empty_left_round.bmp
./res/images/icon_firmware_install.bmp
./res/images/indeterminate3.bmp
./res/images/progress_bar_fill.bmp
./res/images/progress_bar_left_round.bmp
./res/images/icon_error.bmp
./res/images/indeterminate1.bmp
./res/images/progress_bar_empty_right_round.bmp
./res/images/icon_firmware_error.bmp
./res/images/progress_bar_right_round.bmp
./res/images/indeterminate4.bmp
./res/images/indeterminate5.bmp
./res/images/indeterminate6.bmp
./res/images/progress_bar_empty.bmp
./res/images/indeterminate2.bmp
./res/images/icon_unpacking.bmp
./res/images/icon_installing.bmp
./sbin/recovery

Unpacking, Editing, and Re-Packing the images

Note: below I give you the details for unpacking and repacking manually, but I created two perl scripts that do most of this for you (unpack-bootimg.pl , repack-bootimg.pl ).

If you are good with a hex editor, you can open up any of these images and strip off the first 2k of data. Then, look for a bunch of zeroes followed by the hex 1F 8B (which is the magic number of a gzip file). Copy everything from the first line of the file, through the zeroes, and stopping at the 1F 8B. That is the kernel. Everything from the 1F 8B through the end is the ramdisk. You could save each of these files separately. In order to see the contents of the ramdisk, you need to un-gzip it and then un-cpio it. You could use a command like this (ideally after creating a new directory and cd'ing into it):

gunzip -c ../your-ramdisk-file | cpio -i

That will place all of the files from the ramdisk in your working directory. You can now edit them.

In order to re-create the ramdisk, you need to re-cpio them and re-gzip those files, with a command like the following (remember, cpio will include everything in the current working directory, so you probably want to remove any other cruft you might have in there):

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

The final step is to combine the kernel and your new ramdisk into the full image, using the mkbootimg program (which you should download and compile from the git repository):

mkbootimg --cmdline 'no_console_suspend=1 console=null' --kernel your-kernel-file --ramdisk newramdisk.cpio.gz -o mynewimage.img

Now, there's a lot of hassle in pulling apart files in hex editors and remembering all of these commands, so I wrote unpack and repack perl scripts for you. Hooray.

Alternative Method

Download split_bootimg.zip . This Zip file contains one Perl file, split_bootimg.pl, which reads the boot.img header (according to the bootimg.h of the Android source code ) to extract the kernel and ramdisk. The script also outputs the kernel command line and board name (if specified).

(Note: Do not use a boot.img image extracted directly from /dev/mtd/mtd2 . This image may become corrupted during the read process.)

The following example uses the boot.img from the full TC4-RC28 update:

% ./split_bootimg.pl boot.img 
Page size: 2048 (0x00000800)
Kernel size: 1388548 (0x00153004)
Ramdisk size: 141518 (0x000228ce)
Second size: 0 (0x00000000)
Board name: 
Command line: no_console_suspend=1
Writing boot.img-kernel ... complete.
Writing boot.img-ramdisk.gz ... complete.

Extract the ramdisk.

% mkdir ramdisk
% cd ramdisk
% gzip -dc ../boot.img-ramdisk.gz | cpio -i
% cd ..

Make any changes necessary (e.g., set ro.secure=0 in default.prop ).

Recreate the cpio archive using the mkbootfs binary produced from building the Android source code (The cpio utility in OS X does not recognize the newc format, therefore mkbootfs is the best option for OS X users).

% mkbootfs ./ramdisk | gzip > ramdisk-new.gz

Recreate the image file using the mkbootimg binary produced from building the Android source code.

% mkbootimg --cmdline 'no_console_suspend=1 console=null' --kernel boot.img-kernel --ramdisk ramdisk-new.gz -o boot-new.img

For Nexus One  : Add --base 0x20000000 to mkbootimg command-line.


(Note: the console=null command line option was introduced in the TC4-RC30 boot images to remove the root shell (TODO: add link))

Flashing your new image back onto the phone

You will probably only ever be flashing boot images directly to the phone, given the fact that /system/recovery.img automatically flashes the recovery device for you (as noted above). If you have created a new recovery image, just stick it in /system/recovery.img and reboot. If you are flashing a boot image, stick it on your phone via adb (a tool included in the Android SDK ):

adb push ./mynewimage.img /sdcard

Then, open a shell to your phone via 'adb shell', get root , and do the following two commands to flash your new boot image:

# cat /dev/zero > /dev/mtd/mtd2
   write: No space left on device [this is ok, you can ignore]
# flash_image boot /sdcard/mynewimage.img

Reboot.

If your phone starts all the way up, congratulations. If not, you did something wrong and you'll need to boot into recovery mode and apply your update.zip file (reboot while holding down home+power, when you get the recovery screen press alt+L and then alt+S).

Something fun to do with your new found power

If you place a file titled initlogo.rle in the root directory of your boot image, the phone will display this image upon boot (after the "G1" image and before the Android animation). In order to create this file, you need to create a 320x480 image in Photoshop or Gimp and save it as a "raw image" file. You then need to compress that image with the program to565. More details on that here .

This is not the same thing as applying an update.zip

You will see other places on the forums that describe how to create customized update.zip files, as well as update.zip files that people are sharing. For example, there is a recent update.zip which is a modified version of rc30 (with the anti-root aspects disabled). The update.zip files include new boot images, recovery images, and typically replacements for the entire system/ directory as well as other updates. If you are creating a custom boot or recovery image, it is typically a good idea to start with the image distributed with the most recent update you have applied (flashing an image from an older release could have unintended consequences).


Questions?

分享到:
评论

相关推荐

    image文件提取文件

    在Android开发过程中,有时我们需要从System Image中提取特定的apk文件和对应的jar包。System Image是Android模拟器或设备上运行的操作系统镜像,包含了Android系统的各种预装应用和库。这里我们将详细讨论如何进行...

    AndroidRom之system.img内容提取工具

    可用附件中的simg2img.exe来转化一下原始的system.img,然后再用其中的ext2Explorer来浏览提取system.img中的内容。 用法:1、将system.img改名为system.img.ext4。2、命令行cd到simg2img.exe目录下,执行命令“simg...

    Android_boot_image_editor,用于解析和打包android boot.img/vbmeta.img的工具,支持android q预览.zip

    Android Boot Image Editor是一款专为Android系统开发者和爱好者设计的开源工具,主要功能是解析和打包Android系统的boot.img和vbmeta.img文件。这个工具对于理解Android启动流程、定制系统或者进行安全研究具有重要...

    最好用的ROM中img格式文件打开工具

    2. **提取文件**:如果你需要从img文件中单独提取某个应用或者驱动,WinImage提供了这一功能。开发者可以将特定的APK文件或其他重要文件提取出来,用于移植到其他设备或者进一步分析。 3. **编辑内容**:在某些情况...

    Android-ImageRecognition图片识别比较两张图片的匹配度

    在该仓库中,开发者可能包含了Android Studio项目的所有文件,如Java或Kotlin源码、资源文件、依赖库等。通过查看项目源码,我们可以更具体地了解图片识别和匹配的实现细节,包括如何初始化OpenCV,如何捕获和处理...

    unyaffs_src.zip_ image2yaffs_android_linux unyaffs_linux unyaff

    在标题"unyaffs_src.zip_image2yaffs_android_linux unyaffs_linux unyaffs"中,我们看到的是一个关于YAFFS2文件系统源码的压缩包,包含了一系列与从YAFFS image中提取文件相关的工具和库。"unyaffs_src"很可能就是...

    Android文件上传+服务器

    在Android开发中,文件上传是一项常见的任务,尤其是在构建应用程序时涉及到用户数据共享、云存储或者社交媒体集成时。本文将深入探讨Android如何实现文件上传到服务器,并结合提供的`UploadTest`和`UploadExample`...

    创建 Android 文件系统

    在Android系统中,文件系统是其核心组成部分,用于组织和管理设备上的数据和程序。创建Android文件系统主要包括构建root文件系统、system分区、userdata分区以及ramdisk。以下将详细介绍这两种创建方法。 **第一种...

    android动态加载jar文件中的图片

    步骤3:从jar文件中提取图片 一旦获取到`InputStream`,我们可以使用`ZipInputStream`来遍历jar文件的内容。对于每个条目,检查其是否为图片文件(如`.jpg`, `.png`等)。如果是,我们可以使用`BufferedImage`(需要...

    Android Tif文件内部打开

    在Android平台上,开发人员经常需要处理各种类型的文件,其中就包括TIFF(Tagged Image File Format)文件。TIFF是一种广泛用于存储高分辨率图像的格式,尤其在专业图像处理领域非常常见。Android Tif文件内部打开的...

    android-cropimage 裁剪图片源码

    6. **图像处理**:裁剪后,需要将选中的部分从原始图片中提取出来。源码中会包含关于图像截取和重采样的代码,这通常涉及到`Bitmap`对象的使用,如`Bitmap.createBitmap()`和`Bitmap.createBitmapRegionDecoder()`。...

    Android-Image-Tools-windows.zip_android_android 解包_android-img-t

    "Android-Image-Tools-windows.zip" 是一个专为Windows用户设计的工具包,它包含了处理Android系统镜像文件所需的各种工具。这个压缩包主要用于帮助用户对Android系统的BOOT和RECOVERY分区进行修改和定制,从而实现...

    mkyaffs2image_mkyaffs2image_

    这个文件可能是一个使用`unyaffs`工具从YAFFS2 image文件中提取出的原始文件和目录结构。`unyaffs`工具可以将.yaffs2文件解压回普通的文件系统格式,方便开发者查看或编辑image文件中的内容。 总结来说,`mkyaffs2...

    android 文件系统制作

    1. **构建文件系统源码**:构建Android文件系统需要先从Android源码中编译出所需的文件系统镜像,包括`system.img`、`ramdisk.img`和`userdata.img`等。 - `system.img`包含了Android系统的基本组件和应用程序。 -...

    Android kitchen

    1. **反编译**: Android Kitchen 可以将APK文件中的Java字节码反编译为可读的Smali代码。Smali是一种针对Dalvik虚拟机的汇编语言,与Java类似,但更底层。通过反编译,开发者可以查看和修改应用的源代码逻辑。 2. *...

    提取原生android系统图片裁剪源码

    "提取原生Android系统图片裁剪源码" 提供了一种直接可使用的裁剪插件代码库,允许开发者轻松集成到自己的应用中。下面将详细解释这个源码库的核心知识点及其在Android开发中的应用。 首先,Android的图片裁剪通常...

    system image解包打包工具

    这类工具可以用于提取系统映像中的文件,修改它们,然后重新打包成一个新的system image,这对于自定义ROM的制作至关重要。自定义ROM允许用户根据自己的需求定制操作系统,添加或移除功能,优化性能等。 在描述中...

    android 根据uri获取真实路径

    在Android开发过程中,处理文件路径时经常需要将一个`Uri`转换成真实的文件路径。尤其是在处理用户通过文件选择器选取的图片或文件时,通常会得到一个`Uri`,而这个`Uri`并不直接对应我们熟悉的文件系统路径。本文将...

    APK Image Extractor 2.0老朽痴拙汉化版(Akp图标提取)

    所有 PNG 和 JPEG 图像从 Android 应用程序的包 (APK 文件)。 所有按钮、 启动器图标,操作栏图标、 通知和状态栏图标, 菜单图标、 对话框和列表视图图标、 上下文图标 可以在按钮的单击提取徽标、 启动屏幕和...

    Android ZBar条码/二维码扫描,包含相册图片解析

    总之,通过ZBar库,Android开发者可以轻松地实现条码和二维码的扫描与解析,无论是从实时相机流还是从相册图片中提取数据。这个过程涉及相机的管理和图像处理,理解这些原理对于提升应用的用户体验至关重要。

Global site tag (gtag.js) - Google Analytics