TShopping

 找回密碼
 註冊
搜索
查看: 1592|回復: 0
打印 上一主題 下一主題

[教學] 如何解包/編輯/打包android系統的boot.img文件(2)

[複製鏈接]
跳轉到指定樓層
1#
發表於 2013-9-6 19:38:45 | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式
 
Push to Facebook
recovery映像包含了一些額外的文件,例如一個叫做recovery的二進製程序,以及一些對該程序支持性的資源圖片文件(當你您按下home+power組合鍵的時候就會運行這個recovery程序)。
典型的文件列表如下:
./
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、對映像文件進行解包、編輯、打包的常規方法

(注意,下面我給你您介紹的是手工命令行方式進行解包以及重新打包的方法,但是我仍然創建了兩個perl腳本,這兩個腳本可以讓你您的解包和打包工作變得輕鬆許多。

如果你您很擅長使用16進制編輯器的話,你您可以打開boot.img或者recovery.img,然後跳過開始的2K的頭數據,然後尋找一大堆0的數據,在這一堆0的數據後面,緊跟著1F 8B這兩個數字(1F 8B是gzip格式的文件的結束標記)。從此文件開始的地方(跳過2K的頭),一大堆0後面緊跟著到1F 8B這兩個數字為止的全部數據(不包括1F 8B),就是gzip壓縮過的linux內核。從1F 8B開始一直到文件的結尾包含的全部數據,就是ramdisk內存盤的數據。你您可以把把內核和ramdisk兩個文件分別保存下來,在進行分別的修改和處理。我們可以通過un-cpio和un-gzip操作來讀取ramdisk文件中的數據,可以使用如下的命令來實現這個目的,以下操作會生成一個目錄,直接cd進去就可以看到ramdisk中的數據了:

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

此命令可以將ramdisk中的所有的文件解包到當前的工作目錄下面,然後就可以對它進行編輯了。

當需要重新打包ramdisk的時候,就需要re-cpio然後re-gzip這些數據和目錄,可以通過如下命令來實現:(cpio會把所有當前目錄下面的文件都打包進去,因此,在進行此步驟之前,請把不需要的文件都清除掉。)

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

最後一步就是通過 mkbootimg這個工具,把kernel和ramdisk打包在一起,生成一個boot.img:

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

這裡的mkbootimg工具會在編譯android的源代碼的時候會在~/android-src/out/host/linux-x86/bin目錄下面自動生成。

現在,如果不想背這些複雜的命令或者擺弄那個讓人眩暈的16進制編輯器的話,可以嘗試使用我編寫的用於解包和打包的perl腳本了。希望這些腳本能夠節約各位的鍵盤。
3.1、另一種解包、編輯、打包的方法
下載
split_bootimg.zip文件,在此zip文件中包含一個perl文件,split_bootimg.pl腳本,該腳本可以讀取boot.img頭(根據
Android源碼中的bootimg.h讀取)將kernel和ramdisk讀取出來,此腳本也會輸出內核命令行和板子名字。


(注意,不要使用從/dev/mtd/mtd2直接拷貝出來的boot.img,此映像可能在讀取過程遭到損壞。)
下面是一個從TC4-RC28更新中提取出來的boot.img進行解包操作:
%
./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.



解包ramdisk的命令如下:
% mkdir
ramdisk

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

% cd
..


解碼完畢後,就可以修改了(例如,在default.prop設置ro.secure=0等等)

使用mkbootfs工具(mkbootfs工具是編譯完畢Android源代碼以後,就會在~/android-src/out/host/linux-x86/bin自動生成)來重新創建ramdisk,可以使用如下命令來操作:
% mkbootfs ./ramdisk | gzip > ramdisk-new.gz

使用mkbootimg來重新創建boot.img,mkbootimg也可以在~/android-src/out/host/linux-x86/bin目錄中可以找到:
% mkbootimg --cmdline 'no_console_suspend=1
console=null'

--kernel boot.img-kernel --ramdisk
ramdisk-new.gz -o boot-new.img


(注意:console=null的命令行選現是從TC4-RC30的boot.img引入的,用以去掉root
shell)


4、將新的映像刷回到手機
可以將recovery.img拷貝到/system目錄下面,然後重新啟動手機,讓手機自動為你您刷寫到mtd裡面(工作原理在上面已經提過了)。對於boot.img可以通過將其拷貝到sd卡的根目錄,然後通過手機內的刷寫工具將此映像寫入到手機中。

例如,使用 adb工具( Android SDK中的一個工具)將boot.img拷貝到手機的sd卡的根目錄:
adb push ./mynewimage.img /sdcard

然後通過adb
shell登錄手機(獲取過 ROOT的)的shell交互模式,利用命令行進行交互:

# cat
/dev/zero >/dev/mtd/mtd2

   write: No
space left on device [this is ok, you can ignore]

#
flash_image boot /sdcard/mynewimage.img


然後重啟手機。

如果手機能夠正常啟動,那麼祝賀你您,你您的修改和替換已經成功了;如果不能夠順利啟動,則需要重新啟動進入recovery模式,並且使用update.zip來恢復



5、解包、編輯、打包為我們帶來了什麼
可以修改手機開機啟動時候的畫面

You can display a "splash" screen of sorts upon boot. You just have to have a file named initlogo.rle in your root directory. The trick is that the root directory is overwritten each time you boot with the contents of your boot image (mtd2). There are instructions elsewhere on the forums for updating this image.

You can prove to yourself that this works by just copying logo.rle to initlogo.rle in your updated boot image and you will see the "G1" screen twice.

The hurdle I'm running into is generating my own images. There is a program called [url=]to565[/url] that takes in an image and spits out the resulting image in the appropriate format (give it the -r flag to generate an rle) [edit, correct flag is -rle]. However, everything I generate with this just looks like random colored bars when I boot up (and sometimes at the bottom of the screen I see part of whatever was left in the framebuffer when I shut down). Supposedly to565 takes in a raw rgb888 file, and perhaps I'm not generating those properly. I tried saving to "raw image" from gimp but so far everything is just different variations on colored bars.

I may give up at this point, but I thought others might be interesting in picking up where I left off.




 

臉書網友討論
*滑块验证:
您需要登錄後才可以回帖 登錄 | 註冊 |

本版積分規則



Archiver|手機版|小黑屋|免責聲明|TShopping

GMT+8, 2024-4-25 22:45 , Processed in 0.098449 second(s), 22 queries .

本論壇言論純屬發表者個人意見,與 TShopping綜合論壇 立場無關 如有意見侵犯了您的權益 請寫信聯絡我們。

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回復 返回頂部 返回列表