diff --git a/app/src/main/java/com/topjohnwu/magisk/tasks/MagiskInstaller.kt b/app/src/main/java/com/topjohnwu/magisk/tasks/MagiskInstaller.kt index e6de79548..19598e116 100644 --- a/app/src/main/java/com/topjohnwu/magisk/tasks/MagiskInstaller.kt +++ b/app/src/main/java/com/topjohnwu/magisk/tasks/MagiskInstaller.kt @@ -287,8 +287,10 @@ abstract class MagiskInstaller { protected fun flashBoot(): Boolean { if (!"direct_install $installDir $srcBoot".sh().isSuccess) return false - if (!Info.keepVerity) - "patch_dtbo_image".sh() + arrayOf( + "(KEEPVERITY=${Info.keepVerity} patch_dtb_partitions)", + "run_migrations" + ).sh() return true } diff --git a/app/src/main/java/com/topjohnwu/magisk/ui/SplashActivity.kt b/app/src/main/java/com/topjohnwu/magisk/ui/SplashActivity.kt index eabbd9c00..bdc63cff5 100644 --- a/app/src/main/java/com/topjohnwu/magisk/ui/SplashActivity.kt +++ b/app/src/main/java/com/topjohnwu/magisk/ui/SplashActivity.kt @@ -49,7 +49,7 @@ open class SplashActivity : Activity() { // Setup shortcuts Shortcuts.setup(this) - Shell.su("mm_patch_dtbo").submit { + Shell.su("mm_patch_dtb").submit { if (it.isSuccess) Notifications.dtboPatched(this) } diff --git a/app/src/main/java/com/topjohnwu/magisk/utils/RootInit.kt b/app/src/main/java/com/topjohnwu/magisk/utils/RootInit.kt index c7c01a1d1..bf06c33e4 100644 --- a/app/src/main/java/com/topjohnwu/magisk/utils/RootInit.kt +++ b/app/src/main/java/com/topjohnwu/magisk/utils/RootInit.kt @@ -29,11 +29,12 @@ class RootInit : Shell.Initializer() { job.add(context.rawResource(R.raw.nonroot_utils)) } - job.add("mount_partitions", - "get_flags", - "run_migrations", - "export BOOTMODE=true") - .exec() + job.add( + "mount_partitions", + "get_flags", + "run_migrations", + "export BOOTMODE=true" + ).exec() Info.keepVerity = ShellUtils.fastCmd("echo \$KEEPVERITY").toBoolean() Info.keepEnc = ShellUtils.fastCmd("echo \$KEEPFORCEENCRYPT").toBoolean() diff --git a/app/src/main/res/raw/utils.sh b/app/src/main/res/raw/utils.sh index 79ca207b4..6bbeb7b35 100644 --- a/app/src/main/res/raw/utils.sh +++ b/app/src/main/res/raw/utils.sh @@ -17,16 +17,6 @@ fix_env() { cd / } -run_migrations() { - # Move the stock backups - if [ -f /data/magisk/stock_boot* ]; then - mv /data/magisk/stock_boot* /data 2>/dev/null - fi - if [ -f /data/adb/magisk/stock_boot* ]; then - mv /data/adb/magisk/stock_boot* /data 2>/dev/null - fi -} - direct_install() { rm -rf $MAGISKBIN/* 2>/dev/null mkdir -p $MAGISKBIN 2>/dev/null @@ -43,30 +33,43 @@ direct_install() { return 0 } -mm_patch_dtbo() { - $KEEPVERITY && return 1 || patch_dtbo_image +mm_patch_dtb() { + local result=1 + local PATCHED=$TMPDIR/dt.patched + for name in dtb dtbo; do + local IMAGE=`find_block $name$SLOT` + if [ ! -z $IMAGE ]; then + if $MAGISKBIN/magiskboot dtb $IMAGE patch $PATCHED; then + result=0 + if [ ! -z $SHA1 ]; then + # Backup stuffs + mkdir /data/magisk_backup_${SHA1} 2>/dev/null + cat $IMAGE | gzip -9 > /data/magisk_backup_${SHA1}/${name}.img.gz + fi + cat $PATCHED /dev/zero > $IMAGE + rm -f $PATCHED + fi + fi + done + return $result } restore_imgs() { - local SHA1=`grep_prop SHA1 /sbin/.magisk/config` - [ -z $SHA1 ] && local SHA1=`cat /.backup/.sha1` [ -z $SHA1 ] && return 1 - local STOCKBOOT=/data/stock_boot_${SHA1}.img.gz - local STOCKDTBO=/data/stock_dtbo.img.gz - [ -f $STOCKBOOT ] || return 1 + local BACKUPDIR=/data/magisk_backup_$SHA1 + [ -d $BACKUPDIR ] || return 1 get_flags find_boot_image - find_dtbo_image - if [ -f $STOCKDTBO -a -b "$DTBOIMAGE" ]; then - flash_image $STOCKDTBO $DTBOIMAGE - fi - if [ -f $STOCKBOOT -a -b "$BOOTIMAGE" ]; then - flash_image $STOCKBOOT $BOOTIMAGE - return 0 - fi - return 1 + for name in dtb dtbo; do + [ -f $BACKUPDIR/${name}.img.gz ] || continue + local IMAGE=`find_block $name$SLOT` + [ -z $IMAGE ] && continue + flash_image $BACKUPDIR/${name}.img.gz $IMAGE + done + [ -f $BACKUPDIR/boot.img.gz ] || return 1 + flash_image $BACKUPDIR/boot.img.gz $BOOTIMAGE } post_ota() { @@ -119,3 +122,5 @@ force_pm_install() { [ "$VERIFY" -eq 1 ] && settings put global package_verifier_enable 1 return $res } + +SHA1=`grep_prop SHA1 /sbin/.magisk/config` diff --git a/scripts/addon.d.sh b/scripts/addon.d.sh index 8aae989b6..dc01b2d46 100644 --- a/scripts/addon.d.sh +++ b/scripts/addon.d.sh @@ -70,9 +70,8 @@ main() { remove_system_su find_manager_apk - patch_boot_image + install_magisk - cd / # Cleanups $BOOTMODE || recovery_cleanup rm -rf $TMPDIR diff --git a/scripts/boot_patch.sh b/scripts/boot_patch.sh index 148e22aca..b139aae05 100644 --- a/scripts/boot_patch.sh +++ b/scripts/boot_patch.sh @@ -13,20 +13,15 @@ # # File name Type Description # -# boot_patch.sh script A script to patch boot. Expect path to boot image as parameter. +# boot_patch.sh script A script to patch boot image for Magisk. # (this file) The script will use binaries and files in its same directory # to complete the patching process -# util_functions.sh script A script which hosts all functions requires for this script +# util_functions.sh script A script which hosts all functions required for this script # to work properly -# magiskinit binary The binary to replace /init, which has the magisk binary embedded -# magiskboot binary A tool to unpack boot image, decompress ramdisk, extract ramdisk, -# and patch the ramdisk for Magisk support -# chromeos folder This folder should store all the utilities and keys to sign -# (optional) a chromeos device. Used for Pixel C -# -# If the script is not running as root, then the input boot image should be a stock image -# or have a backup included in ramdisk internally, since we cannot access the stock boot -# image placed under /data we've created when previously installed +# magiskinit binary The binary to replace /init; magisk binary embedded +# magiskboot binary A tool to manipulate boot images +# chromeos folder This folder includes all the utilities and keys to sign +# (optional) chromeos boot images. Currently only used for Pixel C # ########################################################################################## ########################################################################################## @@ -104,10 +99,8 @@ fi case $((STATUS & 3)) in 0 ) # Stock boot ui_print "- Stock boot image detected" - ui_print "- Backing up stock boot image" SHA1=`./magiskboot sha1 "$BOOTIMAGE" 2>/dev/null` - STOCKDUMP=stock_boot_${SHA1}.img.gz - ./magiskboot compress "$BOOTIMAGE" $STOCKDUMP + cat $BOOTIMAGE > stock_boot.img cp -af ramdisk.cpio ramdisk.cpio.orig 2>/dev/null ;; 1 ) # Magisk patched @@ -158,7 +151,7 @@ rm -f ramdisk.cpio.orig config ########################################################################################## for dt in dtb kernel_dtb extra recovery_dtbo; do - [ -f $dt ] && ./magiskboot dtb $dt patch && ui_print "- Patching fstab in $dt" + [ -f $dt ] && ./magiskboot dtb $dt patch && ui_print "- Patch fstab in $dt" done if [ -f kernel ]; then diff --git a/scripts/flash_script.sh b/scripts/flash_script.sh index 6b8f6bbcc..0cdcfaf14 100644 --- a/scripts/flash_script.sh +++ b/scripts/flash_script.sh @@ -53,7 +53,7 @@ ui_print "- Target image: $BOOTIMAGE" # Detect version and architecture api_level_arch_detect -[ $API -lt 17 ] && abort "! Magisk is only for Android 4.2 and above" +[ $API -lt 17 ] && abort "! Magisk only support Android 4.2 and above" ui_print "- Device platform: $ARCH" @@ -108,9 +108,8 @@ $BOOTMODE || recovery_actions # Boot/DTBO Patching ########################################################################################## -patch_boot_image +install_magisk -cd / # Cleanups $BOOTMODE || recovery_cleanup rm -rf $TMPDIR diff --git a/scripts/magisk_uninstaller.sh b/scripts/magisk_uninstaller.sh index 0fb59b9ff..02b89edd6 100644 --- a/scripts/magisk_uninstaller.sh +++ b/scripts/magisk_uninstaller.sh @@ -15,6 +15,7 @@ TMPDIR=/dev/tmp INSTALLER=$TMPDIR/install CHROMEDIR=$INSTALLER/chromeos +PERSISTDIR=/sbin/.magisk/mirror/persist # Default permissions umask 022 @@ -50,6 +51,7 @@ chmod -R 755 $MAGISKBIN check_data $DATA_DE || abort "! Cannot access /data, please uninstall with Magisk Manager" $BOOTMODE || recovery_actions +run_migrations ########################################################################################## # Uninstall @@ -57,7 +59,6 @@ $BOOTMODE || recovery_actions get_flags find_boot_image -find_dtbo_image [ -e $BOOTIMAGE ] || abort "! Unable to detect boot image" ui_print "- Found target image: $BOOTIMAGE" @@ -96,16 +97,18 @@ case $((STATUS & 3)) in 1 ) # Magisk patched ui_print "- Magisk patched image detected" # Find SHA1 of stock boot image - [ -z $SHA1 ] && SHA1=`./magiskboot cpio ramdisk.cpio sha1 2>/dev/null` - STOCKBOOT=/data/stock_boot_${SHA1}.img.gz - STOCKDTBO=/data/stock_dtbo.img.gz - if [ -f $STOCKBOOT ]; then + SHA1=`./magiskboot cpio ramdisk.cpio sha1 2>/dev/null` + BACKUPDIR=/data/magisk_backup_$SHA1 + if [ -d $BACKUPDIR ]; then ui_print "- Restoring stock boot image" - flash_image $STOCKBOOT $BOOTIMAGE - if [ -f $STOCKDTBO -a -b "$DTBOIMAGE" ]; then - ui_print "- Restoring stock dtbo image" - flash_image $STOCKDTBO $DTBOIMAGE - fi + flash_image $BACKUPDIR/boot.img.gz $BOOTIMAGE + for name in dtb dtbo; do + [ -f $BACKUPDIR/${name}.img.gz ] || continue + IMAGE=`find_block $name$SLOT` + [ -z $IMAGE ] && continue + ui_print "- Restoring stock $name image" + flash_image $BACKUPDIR/${name}.img.gz $IMAGE + done else ui_print "! Boot image backup unavailable" ui_print "- Restoring ramdisk with internal backup" @@ -128,9 +131,10 @@ case $((STATUS & 3)) in esac ui_print "- Removing Magisk files" -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/adb/*magisk* \ - /data/adb/post-fs-data.d /data/adb/service.d /data/adb/modules* 2>/dev/null +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/adb/*magisk* \ +/data/adb/post-fs-data.d /data/adb/service.d /data/adb/modules* $PERSISTDIR/magisk 2>/dev/null if [ -f /system/addon.d/99-magisk.sh ]; then mount -o rw,remount /system diff --git a/scripts/util_functions.sh b/scripts/util_functions.sh index dd7e5610c..935c8aaca 100644 --- a/scripts/util_functions.sh +++ b/scripts/util_functions.sh @@ -110,8 +110,8 @@ recovery_actions() { recovery_cleanup() { ui_print "- Unmounting partitions" - umount -l /system_root 2>/dev/null umount -l /system 2>/dev/null + umount -l /system_root 2>/dev/null umount -l /vendor 2>/dev/null umount -l /dev/random 2>/dev/null export PATH=$OLD_PATH @@ -196,7 +196,7 @@ mount_partitions() { [ -L /system/vendor ] && mount_ro_ensure vendor $SYSTEM_ROOT && ui_print "- Device is system-as-root" - # Persist partitions for module install in recovery + # Mount persist partition in recovery if ! $BOOTMODE && [ ! -z $PERSISTDIR ]; then # Try to mount persist PERSISTDIR=/persist @@ -276,30 +276,29 @@ flash_image() { return 0 } -find_dtbo_image() { - DTBOIMAGE=`find_block dtbo$SLOT` -} - -patch_dtbo_image() { - find_dtbo_image - if [ ! -z $DTBOIMAGE ]; then - ui_print "- DTBO image: $DTBOIMAGE" - local PATCHED=$TMPDIR/dtbo - if $MAGISKBIN/magiskboot dtb $DTBOIMAGE patch $PATCHED; then - ui_print "- Backing up stock DTBO image" - $MAGISKBIN/magiskboot compress $DTBOIMAGE $MAGISKBIN/stock_dtbo.img.gz - ui_print "- Patching DTBO to remove avb-verity" - cat $PATCHED /dev/zero > $DTBOIMAGE - rm -f $PATCHED - return 0 +patch_dtb_partitions() { + local result=1 + cd $MAGISKBIN + for name in dtb dtbo; do + local IMAGE=`find_block $name$SLOT` + if [ ! -z $IMAGE ]; then + ui_print "- $name image: $IMAGE" + if ./magiskboot dtb $IMAGE patch dt.patched; then + result=0 + ui_print "- Backing up stock $name image" + cat $IMAGE > stock_${name}.img + ui_print "- Flashing patched $name" + cat dt.patched /dev/zero > $IMAGE + rm -f dt.patched + fi fi - fi - return 1 + done + cd / + return $result } # Common installation script for flash_script.sh and addon.d.sh -patch_boot_image() { - SOURCEDMODE=true +install_magisk() { cd $MAGISKBIN eval $BOOTSIGNER -verify < $BOOTIMAGE && BOOTSIGNED=true @@ -308,6 +307,7 @@ patch_boot_image() { $IS64BIT && mv -f magiskinit64 magiskinit 2>/dev/null || rm -f magiskinit64 # Source the boot patcher + SOURCEDMODE=true . ./boot_patch.sh "$BOOTIMAGE" ui_print "- Flashing new boot image" @@ -322,18 +322,8 @@ patch_boot_image() { ./magiskboot cleanup rm -f new-boot.img - if [ -f stock_boot* ]; then - rm -f /data/stock_boot* 2>/dev/null - $DATA && mv stock_boot* /data - fi - - # Patch DTBO together with boot image - $KEEPVERITY || patch_dtbo_image - - if [ -f stock_dtbo* ]; then - rm -f /data/stock_dtbo* 2>/dev/null - $DATA && mv stock_dtbo* /data - fi + patch_dtb_partitions + run_migrations } sign_chromeos() { @@ -415,6 +405,41 @@ find_manager_apk() { [ -f $APK ] || ui_print "! Unable to detect Magisk Manager APK for BootSigner" } +run_migrations() { + local LOCSHA1 + local TARGET + # Legacy app installation + local BACKUP=/data/adb/magisk/stock_boot*.gz + if [ -f $BACKUP ]; then + cp $BACKUP /data + rm -f $BACKUP + fi + + # Legacy backup + for gz in /data/stock_boot*.gz; do + [ -f $gz ] || break + LOCSHA1=`basename $gz | sed -e 's/stock_boot_//' -e 's/.img.gz//'` + [ -z $LOCSHA1 ] && break + mkdir /data/magisk_backup_${LOCSHA1} 2>/dev/null + mv $gz /data/magisk_backup_${LOCSHA1}/boot.img.gz + done + + # Stock backups + LOCSHA1=$SHA1 + for name in boot dtb dtbo; do + BACKUP=/data/adb/magisk/stock_${name}.img + [ -f $BACKUP ] || continue + if [ $name = 'boot' ]; then + LOCSHA1=`$MAGISKBIN/magiskboot sha1 $BACKUP` + mkdir /data/magisk_backup_${LOCSHA1} 2>/dev/null + fi + TARGET=/data/magisk_backup_${LOCSHA1}/${name}.img + cp $BACKUP $TARGET + rm -f $BACKUP + gzip -9f $TARGET + done +} + ################# # Module Related #################