Add dtbo.img patch support

This commit is contained in:
topjohnwu 2017-11-11 01:33:50 +08:00
parent e9d0f615ba
commit 912c188b53
5 changed files with 88 additions and 37 deletions

View File

@ -43,7 +43,7 @@ main() {
find_boot_image find_boot_image
[ -z $BOOTIMAGE ] && abort "! Unable to detect boot image" [ -z $BOOTIMAGE ] && abort "! Unable to detect boot image"
ui_print "- Found Boot Image: $BOOTIMAGE" ui_print "- Found boot image: $BOOTIMAGE"
SOURCEDMODE=true SOURCEDMODE=true
cd $MAGISKBIN cd $MAGISKBIN
@ -51,13 +51,20 @@ main() {
# Source the boot patcher # Source the boot patcher
. $MAGISKBIN/boot_patch.sh "$BOOTIMAGE" . $MAGISKBIN/boot_patch.sh "$BOOTIMAGE"
flash_boot_image new-boot.img "$BOOTIMAGE"
rm -f new-boot.img
if [ -f stock_boot* ]; then if [ -f stock_boot* ]; then
rm -f /data/stock_boot* 2>/dev/null rm -f /data/stock_boot* 2>/dev/null
mv stock_boot* /data is_mounted /data && mv stock_boot* /data
fi fi
flash_boot_image new-boot.img $BOOTIMAGE patch_dtbo_image
rm -f new-boot.img
if [ -f stock_dtbo* ]; then
rm -f /data/stock_dtbo* 2>/dev/null
is_mounted /data && mv stock_dtbo* /data
fi
cd / cd /
recovery_cleanup recovery_cleanup

View File

@ -110,9 +110,8 @@ case $? in
ui_print "- Stock boot image detected!" ui_print "- Stock boot image detected!"
ui_print "- Backing up stock boot image" ui_print "- Backing up stock boot image"
SHA1=`./magiskboot --sha1 "$BOOTIMAGE" 2>/dev/null` SHA1=`./magiskboot --sha1 "$BOOTIMAGE" 2>/dev/null`
STOCKDUMP=stock_boot_${SHA1}.img STOCKDUMP=stock_boot_${SHA1}.img.gz
dd if="$BOOTIMAGE" of=$STOCKDUMP ./magiskboot --compress "$BOOTIMAGE" $STOCKDUMP
./magiskboot --compress $STOCKDUMP
cp -af ramdisk.cpio ramdisk.cpio.orig cp -af ramdisk.cpio ramdisk.cpio.orig
;; ;;
1 ) # Magisk patched 1 ) # Magisk patched
@ -129,10 +128,10 @@ case $? in
ui_print "! Cannot restore from internal backup" ui_print "! Cannot restore from internal backup"
# If we are root and SHA1 known, we try to find the stock backup # If we are root and SHA1 known, we try to find the stock backup
if [ ! -z $SHA1 ]; then if [ ! -z $SHA1 ]; then
STOCKDUMP=/data/stock_boot_${SHA1}.img STOCKDUMP=/data/stock_boot_${SHA1}.img.gz
if [ -f ${STOCKDUMP}.gz ]; then if [ -f $STOCKDUMP ]; then
ui_print "- Stock boot image backup found" ui_print "- Stock boot image backup found"
./magiskboot --decompress ${STOCKDUMP}.gz stock_boot.img ./magiskboot --decompress $STOCKDUMP stock_boot.img
./magiskboot --unpack stock_boot.img ./magiskboot --unpack stock_boot.img
rm -f stock_boot.img rm -f stock_boot.img
OK=true OK=true
@ -163,17 +162,15 @@ ui_print "- Patching ramdisk"
# Create ramdisk backups # Create ramdisk backups
./magiskboot --cpio-backup ramdisk.cpio ramdisk.cpio.orig $SHA1 ./magiskboot --cpio-backup ramdisk.cpio ramdisk.cpio.orig $SHA1
if ! $KEEPVERITY && [ -f dtb ]; then
./magiskboot --dtb-patch dtb && ui_print "- Patching fstab in dtb to remove dm-verity"
fi
rm -f ramdisk.cpio.orig rm -f ramdisk.cpio.orig
########################################################################################## ##########################################################################################
# Repack and flash # Binary patches
########################################################################################## ##########################################################################################
# Hexpatches if ! $KEEPVERITY && [ -f dtb ]; then
./magiskboot --dtb-patch dtb && ui_print "- Patching fstab in dtb to remove dm-verity"
fi
# Remove Samsung RKP in stock kernel # Remove Samsung RKP in stock kernel
./magiskboot --hexpatch kernel \ ./magiskboot --hexpatch kernel \
@ -185,6 +182,10 @@ A1020054011440B93FA00F7140020054010840B93FA00F71E0010054001840B91FA00F7181010054
736B69705F696E697472616D6673 \ 736B69705F696E697472616D6673 \
77616E745F696E697472616D6673 77616E745F696E697472616D6673
##########################################################################################
# Repack and flash
##########################################################################################
ui_print "- Repacking boot image" ui_print "- Repacking boot image"
./magiskboot --repack "$BOOTIMAGE" || abort "! Unable to repack boot image!" ./magiskboot --repack "$BOOTIMAGE" || abort "! Unable to repack boot image!"

View File

@ -115,14 +115,21 @@ cd $MAGISKBIN
# Source the boot patcher # Source the boot patcher
. $COMMONDIR/boot_patch.sh "$BOOTIMAGE" . $COMMONDIR/boot_patch.sh "$BOOTIMAGE"
if [ -f stock_boot* ]; then
rm -f /data/stock_boot* 2>/dev/null
mv stock_boot* /data
fi
flash_boot_image new-boot.img "$BOOTIMAGE" flash_boot_image new-boot.img "$BOOTIMAGE"
rm -f new-boot.img rm -f new-boot.img
if [ -f stock_boot* ]; then
rm -f /data/stock_boot* 2>/dev/null
is_mounted /data && mv stock_boot* /data
fi
patch_dtbo_image
if [ -f stock_dtbo* ]; then
rm -f /data/stock_dtbo* 2>/dev/null
is_mounted /data && mv stock_dtbo* /data
fi
cd / cd /
# Cleanups # Cleanups
$BOOTMODE || recovery_cleanup $BOOTMODE || recovery_cleanup

View File

@ -79,17 +79,16 @@ case $? in
ui_print "- Magisk patched image detected!" ui_print "- Magisk patched image detected!"
# Find SHA1 of stock boot image # Find SHA1 of stock boot image
[ -z $SHA1 ] && SHA1=`./magiskboot --cpio-stocksha1 ramdisk.cpio 2>/dev/null` [ -z $SHA1 ] && SHA1=`./magiskboot --cpio-stocksha1 ramdisk.cpio 2>/dev/null`
[ ! -z $SHA1 ] && STOCKBOOT=/data/stock_boot_${SHA1}.img.gz OK=false
if [ -f "$STOCKBOOT" ]; then [ ! -z $SHA1 ] && restore_imgs $SHA1 && OK=true
ui_print "- Boot image backup found!" if ! $OK; then
else
ui_print "! Boot image backup unavailable" ui_print "! Boot image backup unavailable"
ui_print "- Restoring ramdisk with internal backup" ui_print "- Restoring ramdisk with internal backup"
./magiskboot --cpio-restore ramdisk.cpio ./magiskboot --cpio-restore ramdisk.cpio
./magiskboot --repack $BOOTIMAGE ./magiskboot --repack $BOOTIMAGE
# Sign chromeos boot # Sign chromeos boot
$CHROMEOS && sign_chromeos $CHROMEOS && sign_chromeos
STOCKBOOT=new-boot.img flash_boot_image new-boot.img "$BOOTIMAGE"
fi fi
;; ;;
2 ) # Other patched 2 ) # Other patched
@ -98,13 +97,11 @@ case $? in
;; ;;
esac esac
flash_boot_image $STOCKBOOT "$BOOTIMAGE"
cd / cd /
ui_print "- Removing Magisk files" ui_print "- Removing Magisk files"
rm -rf /cache/*magisk* /cache/unblock /data/*magisk* /data/cache/*magisk* /data/property/*magisk* \ rm -rf /cache/*magisk* /cache/unblock /data/*magisk* /data/cache/*magisk* /data/property/*magisk* \
/data/Magisk.apk /data/busybox /data/custom_ramdisk_patch.sh \ /data/Magisk.apk /data/busybox /data/custom_ramdisk_patch.sh /data/app/com.topjohnwu.magisk* \
/data/app/com.topjohnwu.magisk* /data/user*/*/com.topjohnwu.magisk 2>/dev/null /data/user*/*/magisk.db /data/user*/*/com.topjohnwu.magisk 2>/dev/null
$BOOTMODE && reboot $BOOTMODE && reboot

View File

@ -41,8 +41,11 @@ ui_print() {
mount_partitions() { mount_partitions() {
# Check A/B slot # Check A/B slot
SLOT=`getprop ro.boot.slot_suffix` SLOT=`getprop ro.boot.slot_suffix`
[ -z $SLOT ] && SLOT=_`getprop ro.boot.slot` if [ -z $SLOT ]; then
[ $SLOT = "_" ] || ui_print "- A/B partition detected, current slot: $SLOT" SLOT=_`getprop ro.boot.slot`
[ $SLOT = "_" ] && SLOT=
fi
[ -z $SLOT ] || ui_print "- A/B partition detected, current slot: $SLOT"
ui_print "- Mounting /system, /vendor" ui_print "- Mounting /system, /vendor"
is_mounted /system || [ -f /system/build.prop ] || mount -o ro /system 2>/dev/null is_mounted /system || [ -f /system/build.prop ] || mount -o ro /system 2>/dev/null
if ! is_mounted /system && ! [ -f /system/build.prop ]; then if ! is_mounted /system && ! [ -f /system/build.prop ]; then
@ -136,24 +139,60 @@ migrate_boot_backup() {
flash_boot_image() { flash_boot_image() {
# Make sure all blocks are writable # Make sure all blocks are writable
$MAGISKBIN/magisk --unlock-blocks $MAGISKBIN/magisk --unlock-blocks 2>/dev/null
case "$1" in case "$1" in
*.gz) COMMAND="gzip -d < \"$1\"";; *.gz) COMMAND="gzip -d < '$1'";;
*) COMMAND="cat \"$1\"";; *) COMMAND="cat '$1'";;
esac esac
$BOOTSIGNED && SIGNCOM="$BOOTSIGNER -sign" || SIGNCOM="cat -" $BOOTSIGNED && SIGNCOM="$BOOTSIGNER -sign" || SIGNCOM="cat -"
case "$2" in case "$2" in
/dev/block/*) /dev/block/*)
ui_print "- Flashing new boot image" ui_print "- Flashing new boot image"
eval $COMMAND | eval $SIGNCOM | cat - /dev/zero | dd of="$2" bs=4096 >/dev/null 2>&1 eval $COMMAND | eval $SIGNCOM | cat - /dev/zero 2>/dev/null | dd of="$2" bs=4096 2>/dev/null
;; ;;
*) *)
ui_print "- Storing new boot image" ui_print "- Storing new boot image"
eval $COMMAND | eval $SIGNCOM | dd of="$2" bs=4096 >/dev/null 2>&1 eval $COMMAND | eval $SIGNCOM | dd of="$2" bs=4096 2>/dev/null
;; ;;
esac esac
} }
find_dtbo_image() {
DTBOIMAGE=`find /dev/block -iname dtbo$SLOT | head -n 1` 2>/dev/null
[ ! -z $DTBOIMAGE ] && DTBOIMAGE=`resolve_link $DTBOIMAGE`
}
patch_dtbo_image() {
find_dtbo_image
if [ ! -z $DTBOIMAGE ]; then
ui_print "- Found dtbo image: $DTBOIMAGE"
if $MAGISKBIN/magiskboot --dtb-test $DTBOIMAGE; then
ui_print "- Backing up stock dtbo image"
$MAGISKBIN/magiskboot --compress $DTBOIMAGE $MAGISKBIN/stock_dtbo.img.gz
ui_print "- Patching fstab in dtbo to remove avb-verity"
$MAGISKBIN/magiskboot --dtb-patch $DTBOIMAGE
fi
fi
}
restore_imgs() {
STOCKBOOT=/data/stock_boot_${1}.img.gz
STOCKDTBO=/data/stock_dtbo.img.gz
find_dtbo_image
if [ ! -z "$DTBOIMAGE" -a -f "$STOCKDTBO" ]; then
ui_print "- Restoring stock dtbo image"
gzip -d < $STOCKDTBO | dd of=$DTBOIMAGE
fi
BOOTSIGNED=false
find_boot_image
if [ ! -z "$BOOTIMAGE" -a -f "$STOCKBOOT" ]; then
ui_print "- Restoring stock boot image"
flash_boot_image $STOCKBOOT "$BOOTIMAGE"
return 0
fi
return 1
}
sign_chromeos() { sign_chromeos() {
ui_print "- Signing ChromeOS boot image" ui_print "- Signing ChromeOS boot image"