安卓的開源使其具有很強的可定制性,對於用戶來說很具有可玩性。玩機一般來說就是解鎖BootLoader刷入第三方recovery,利用第三方recovery刷第三方ROM,刷supersu獲取root權限,當然少不了鼎鼎大名的xposed框架……
但是很多廠商是鎖定BootLoader且不提供官方解鎖的,比如魅族、錘子、360……熱門機型還好,或許有大神關注,破解BootLoader,從而刷入第三方recovery。如果使用了小眾機型,或是官方對漏洞控制的很好,無法破解BootLoader,進而無法刷入第三方recovery呢?所以就出現了所謂的外掛recovery。
之所以稱為外掛recovery,是因為我們並沒有把一個recovery鏡像(.img)刷入手機的recovery分區,而是利用安卓開機流程中會執行特定路徑下的shell腳本,來執行自定義shell代碼啟動recovery。比如安卓4.4.4時的system/etc/,再如安卓5.0之后的system/su.d/(發現這一點其實是當時找關閉selinux方法時看的一些帖子)。
我手里用的是魅族的魅藍note,因為一次系統升級時摔了手機一下,變磚了,無法進入recovery,只能進fastboot模式,可惜bootloader是鎖定的。因為急需手機的數據,郵寄太慢,打車往返100多公里去售后刷機,刷機的人特多,從早上等到下午4點多,午飯都沒吃,最后手機數據全清了。如果bootloader可以解鎖,直接flash boot和recovery就不用清除手機數據。為此特別氣憤,為什么官方不提供BootLoader解鎖,甚至線刷包都刷不進去,需要加密狗。為了所謂的安全?后來在淘寶發現,解屏幕鎖15塊錢,線刷救磚30塊錢左右,全部可遠程操作,甚至很多手機維修點也提供服務……
所以,搞起了外掛recovery,現在把要點記錄一下。
- 首先手機要能 root,最好是supersu授權;
- 編譯機型的recovery,也可以解包同配置其他手機的recovery試試;
- 解包recovery得到ramdisk里的etc,res,sbin,twrps四個文件夾;
- 利用shell腳本或apk程序把這些目錄拷貝到手機system分區,把啟動recovery的shell腳本拷貝到system/su.d/;
- 開機時判斷、拷貝etc,res,sbin,twrps到根目錄並啟動recovery
下面是我為魅藍note做的一個外掛recovery,方法適用於安卓5.0以上版本已root的手機。首先編譯了魅藍note的twrp,再利用apk安裝,apk提供“安裝”,“重啟”兩個button,“安裝”主要是文件拷貝,apk里assets目錄下是recovery目錄,recovery目錄下有busybox、install-recovery.sh、recovery.img(實為zip),recovery.img里有etc,res,sbin(空目錄),twrps四個目錄,sbin目錄打包成sbin.tar.gz(直接拷貝sbin目錄可能因權限問題導致某些文件拷貝失敗),三個shell腳本,“重啟”執行重啟手機並啟動recovery,主要用到三個shell腳本。
- install-recovery.sh
#!/system/bin/sh
# recoverydir,apk程序會把assets/目錄下的文件拷貝到recoverydir
# targetdir,這個腳本會把etc,res,sbin,twrps拷貝到targetdir
# busybox,busybox的路徑
recoverydir=/data/data/com.meizu.recoveryinstaller/files/recovery
targetdir=/system/etc/recovery
busybox=$recoverydir/busybox
mount -o rw,remount /system
chmod 777 $busybox
if [ ! -f /system/xbin/busybox ];then
$busybox cp $recoverydir/busybox /system/xbin/busybox
chown 0.0 /system/xbin/busybox
chmod 777 /system/xbin/busybox
fi
if [ ! -e /system/xbin/unzip ]; then
chmod 777 /system/xbin/busybox
/system/xbin/busybox --install -s /system/xbin
fi
if [ -d $targetdir ];then
rm -rf $targetdir
fi
mkdir -p $targetdir
#解壓recovery.img(實為zip)
unzip $recoverydir/recovery.img -d $targetdir
chmod -R 777 $targetdir
#解壓sbin.tar.gz
tar xvzpf $targetdir/sbin.tar.gz -C $targetdir/sbin
rm -rf $targetdir/sbin.tar.gz
if [ ! -d /system/su.d ]; then
mkdir /system/su.d
chmod 777 /system/su.d
fi
cat /system/etc/recovery/start-recovery.sh > /system/su.d/start-recovery.sh
chmod 777 /system/su.d/start-recovery.sh
mount -o ro,remount /system
2.reboot-recovery.sh
#!/system/bin/sh
if [ -d /cache/recovery ]; then
rm -rf /cache/recovery
mkdir /cache/recovery
chmod 755 /cache/recovery
else
mkdir /cache/recovery
fi
touch /cache/recovery/command
chmod 755 /cache/recovery/command
reboot
3.start-recovery.sh
#!/system/bin/sh
#掛載可讀寫
mountr() {
mount -o remount,rw /
mount -o remount,rw /system
mount -o remount,rw /custom
mount -o remount,rw /data
mount -o remount,rw /cache
}
mountr
stop
kill -9 $!
#只有執行過reboot-recovery.sh之后-e /cache/recovery/command才為真
if [ -e /cache/recovery/command ];then
mountr
rm -rf /cache/keycache
rm -rf /cache/recovery/command
chmod -R 777 /system/etc/recovery
#拷貝etc,res,sbin,twrps到根目錄
cp -rf /system/etc/recovery/etc /
cp -rf /system/etc/recovery/sbin /
cp -rf /system/etc/recovery/res /
cp -rf /system/etc/recovery/twres /
chmod -R 777 /sbin
chmod -R 777 /twres
chmod -R 777 /res
chmod -R 777 /etc
mkdir /tmp
chmod -R 777 /tmp
setenforce 0
mountr
#綁定/data/media/0 /sdcard
rm -rf /sdcard
mkdir /sdcard
mount -o remount,rw /sdcard
mount --bind /data/media/0 /sdcard
runcon u:r:recovery:s0
busybox killall cploadserver
/sbin/recovery
else
start
fi
