Research on the Development of the Android platform for Refresh (2)

Source: Internet
Author: User
Tags perl script

The following is an article about editing and modifying boot. IMG and recovery. IMG. I hope to save some time to read documents for interested friends. Thanks to the authors of this article: alansj, darkriftx, ryebrye, Will, try Op9, tonyb133, timmmm, lxrose, and many unknown authors who have made unremitting efforts on Wiki.

How to unpackage/edit/large package boot. imgfile

Many people use their own methods to solve boot. I was asked to write an article about the file structure of the boot and recovery images and how to edit them, so we have the following article.

Directory
1. Background Knowledge
2. File structure of the boot and recovery Images
3. General methods for unpackaging, editing, and packaging image files
3.1. Another method of unpacking, editing, and packaging
4. Brush the new image back to the mobile phone
5. What does unpacking, editing, and packaging bring to us?
6. The content in this article is not the same as update.zip.

Body
1. Background Knowledge

The file system of the Android mobile phone consists of a lot of memory, which is output below the ADB shell:
# 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: 04ac00000 00020000 "userdata"
Note that the order of different mobile phones on the above storage devices may be different! Make sure to check your mobile phone and select the correct device number in the following operations (mtdx, this X number must be checked clearly ).

In this wizard, we mainly describe how to operate the storage devices of "recovery" and "Boot; the "system" storage device stores all the data in the Android system directory (after the system is started, it will be mounted to the "System/" Directory ); the "userdata" storage device stores all the data in the android data directory (after the system is started, it will be mounted to the "Data/" directory, there will be a lot of application data and user configuration data such as preference ).

From the above output, we can see that the recovery and boot partitions correspond to/dev/MTD/mtd1 and/dev/MTD/mtd2. You must do two things before you start making any changes, first, you must back up the two partitions first.
You can use the following command to back up data:
# Cat/dev/MTD/mtd1>/sdcard/recovery. img
# Cat/dev/MTD/mtd2>/sdcard/boot. img
(Note that added by lxros can execute the preceding backup command only after the mobile phone obtains the root permission)

Second, you should fl your latest update.zip file to the root directory of your SD card. In this way, even if you have encountered a problem in subsequent operations, you can start the recovery in the recovery mode.

Another important file you need to know is/system/recovery. IMG In the Android system directory, which is a full copy of the mtd1 storage device. This file is automatically written back to the mtd1 storage device every time it is shut down.

This means two things:
(1) Any direct modification to data in/dev/MTD/mtd1 will disappear after the next restart.
(2) If you want to modify/dev/MTD/mtd1, replace/system/recovery. IMG with your own rediscovery. IMG. If you forget to replace this/system/recovery when you create an update.zip image for your subscription (especially when the subscription is adapted. IMG, this recovery. IMG will be burned and written to mtd1 during shutdown, which may become bricks. Be sure to pay attention to this!

(For the translator's words, the/system/recovery. imgfile is not found on Android 2.1. Maybe this mechanism is out ?! Or do I have a deep understanding of this passage ?! Hope you can understand it)

2. File structure of the boot and recovery Images
Boot and recovery images are not a complete file system. They are a custom Android file format, which includes 2 K file headers, followed by the gzip compressed kernel, followed by a ramdisk memory disk, followed by the second-stage loader program (this loader is optional, this part may not exist in some images ). The definition of such files can be found from the source code Android-src/system/CORE/mkbootimg a file called bootimg. h.

(The original article is a file named mkbootimg. h, but from the android 2.1 code, the file name should be changed to bootimg. 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)
** 3. Load each element (kernel, ramdisk, second)
** The specified physical address (kernel_addr, etc)
** 4. Prepare tags at tag_addr. kernel_args [] is
** Appended to the kernel CommandLine In the tags.
** 5. R0 = 0, R1 = machine_type, R2 = tags_addr
** 6. If second_size! = 0: Jump to second_addr
** Else: Jump to kernel_addr
*/

The ramdisk image is a basic small file system. It includes all the core files required for initializing the system, such as initializing the INIT process and init. RC (can be used to set many system parameters) and other files. For more information about this file, see the following website:
Http://git.source.android.com /? P = kernel/common. Git; A = blob; F = documentation/filesystems/ramfs-rootfs-initramfs.txt
The following is a list of files contained in 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 contains some additional files, such as a binary program called recovery, and some resource image files that support this program (this recovery program runs when you press the home + power combination key ).
A typical file list is as follows:
./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

3. General methods for unpackaging, editing, and packaging image files
(Note: The following describes how to unpack and repackage a package using the command line method. However, I still created two Perl scripts, these two scripts make it much easier for you to unpack and package. Refer to the attachment unpack-bootimg.zipand repack-bootimg.zip in this document)

If you are good at using the hexadecimal editor, you can open boot. IMG or recovery. IMG, Then skip the first 2 k header data, and then look for a large pile of 0 data, after this pile of 0 data, follow the two numbers 1f 8B (1f 8B is the end mark of a GZIP file ). From the beginning of the file (skip the 2 k header), a lot of data after 0 is followed by the two numbers 1f 8B, that is, the Linux kernel compressed by gzip. From the data that follows 1f 8B to all the data contained at the end of the file, it is the data of the ramdisk memory disk. You can save the two files, kernel and ramdisk, and modify and process them separately. We can use the un-cpio and un-gzip operations to read data in the ramdisk file. You can use the following command to achieve this purpose. The following operations will generate a directory, you can view the data in ramdisk by entering CD directly:

Gunzip-C ../your-ramdisk-file | cpio-I

This command can unpackage all the files in ramdisk to the current working directory, and then edit it.

When you need to re-package ramdisk, you need re-cpio and re-gzip the data and directories, you can run the following command: (cpio will package all the files in the current directory. Therefore, clear unnecessary files before performing this step .)

Find. | cpio-o-h NEWC | gzip> ../newramdisk.cpio.gz

The last step is to use the mkbootimg tool to package the kernel and ramdisk together to generate a boot. IMG:

Mkbootimg -- cmdline 'no _ lele_suspend = 1 Console = null' -- kernel your-kernel-file -- ramdisk newramdisk.cpio.gz-O mynewimage. img

Here, the mkbootimg tool will compile the android source code in ~ /Android-src/out/host/linux-x86/bin directory is automatically generated.
:
Http://git.source.android.com /? P = platform/system/CORE. Git; A = tree; F = mkbootimg

Now, if you don't want to back up these complicated commands or play with the dizzy hex editor, you can try to use the Perl script I have compiled for unpacking and packaging. We hope that these scripts will save you on the keyboard.

3.1. Another method of unpacking, editing, and packaging
Download the split_bootimg.zip file (provided in the attachment to this article). The zip file contains a perl file and a split_bootimg.pl script, which can read boot. IMG header (according to bootimg in Android source code. h) read the kernel and ramdisk. The script also outputs the kernel command line and board name.

(Note: Do not use boot. IMG directly copied from/dev/MTD/mtd2. This image may be corrupted during the reading process .)
The following is a boot. IMG extracted from the TC4-RC28 update for unpacking:
%./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.

The command to unpackage ramdisk is as follows:
% Mkdir ramdisk
% Cd ramdisk
% Gzip-DC./boot.img-ramdisk.gz | cpio-I
% Cd ..

After decoding, you can modify it (for example, set Ro. Secure = 0 in default. Prop)

Use the mkbootfs tool (after the mkbootfs tool compiles the android source code ~ /Android-src/out/host/linux-x86/bin automatically generated) to re-create ramdisk, you can use the following command to operate:
% Mkbootfs./ramdisk | gzip> ramdisk-new.gz

You can use mkbootimg to re-create boot. IMG ~ /Android-src/out/host/linux-x86/bin directory can be found:
% Mkbootimg -- cmdline 'no _ cmdle_suspend = 1 Console = null' -- kernel boot. IMG-kernel -- ramdisk ramdisk-new.gz-O boot-new.img
(Note: The console = NULL command line is introduced from the boot. img of the TC4-RC30 to remove the root shell)

4. Brush the new image back to the mobile phone
You can copy recovery. IMG to the/system directory, and then restart the mobile phone so that the mobile phone will automatically refresh you to MTD (the working principle has been mentioned above ). For boot. IMG, you can copy the image to the root directory of the SD card, and then write the image to the mobile phone using the mobile phone flushing tool.

For example, use the ADB tool to copy boot. IMG to the root directory of the SD card of the mobile phone:
ADB push./mynewimage. img/sdcard
Then, use the ADB shell to log on to the shell interaction mode of the mobile phone and use the command line for interaction:
# Cat/dev/Zero>/dev/MTD/mtd2
Write: no space left on device [This is OK, you can ignore]
# Flash_image boot/sdcard/mynewimage. img
Then restart the phone.

Restore.

5. What does unpacking, editing, and packaging bring to us?
You can modify the screen when the phone is started. The specific operation address is:
Http://forum.xda-developers.com/showthread.php? T = 443431

6. The content in this article is not the same as update.zip.
You can easily see how to customize update.zip in other forums, or download a lot of self-made image farming tools shared on the Internet. For example, most recent refresh packages come from modifications and adjustments to the rc30 package. In update.zip, the new boot. IMG, recovery. IMG, and several files in the entire system/directory will be replaced and updated. If you want to make your own boot. IMG and recovery. IMG, we recommend that you select a newer version. (If older images are used, compatibility issues may occur ).

Code:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.