From faab79b41aaee345c627c946023b2153e3d68541 Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Mon, 6 Feb 2017 03:22:37 +0800 Subject: [PATCH] Update uninstaller --- .gitignore | 1 + build.cmd | 2 + build.sh | 2 + scripts/flash_script.sh | 5 +- scripts/magic_mask.sh | 10 +- scripts/magisk_uninstaller.sh | 150 ++++++++++++ .../META-INF/com/google/android/update-binary | 213 +----------------- 7 files changed, 179 insertions(+), 204 deletions(-) create mode 100644 scripts/magisk_uninstaller.sh diff --git a/.gitignore b/.gitignore index 5cccaa97d..424a4d902 100644 --- a/.gitignore +++ b/.gitignore @@ -14,6 +14,7 @@ uninstaller/x64/* ziptools/zipadjust # Generated scripts +uninstaller/common/ zip_static/common/magic_mask.sh zip_static/common/magisksu.sh zip_static/common/init.magisk.rc diff --git a/build.cmd b/build.cmd index 24164f764..2f2c58c6f 100644 --- a/build.cmd +++ b/build.cmd @@ -78,6 +78,7 @@ EXIT /B %ERRORLEVEL% 2>NUL DEL zip_static\common\magisksu.sh 2>NUL DEL zip_static\common\init.magisk.rc 2>NUL DEL zip_static\common\magic_mask.sh + 2>NUL RMDIR /S /Q uninstaller\common 2>NUL RMDIR /S /Q uninstaller\arm 2>NUL RMDIR /S /Q uninstaller\arm64 2>NUL RMDIR /S /Q uninstaller\x86 @@ -118,6 +119,7 @@ EXIT /B %ERRORLEVEL% ECHO ************************ ECHO * Zipping uninstaller ECHO ************************ + CALL :mkcp scripts\magisk_uninstaller.sh uninstaller\common FOR /F "tokens=* USEBACKQ" %%F IN (`ziptools\win_bin\date "+%%Y%%m%%d"`) DO (set timestamp=%%F) CD uninstaller 2>NUL DEL "../Magisk-uninstaller-%timestamp%.zip" diff --git a/build.sh b/build.sh index a72a57317..f6b4647b9 100755 --- a/build.sh +++ b/build.sh @@ -29,6 +29,7 @@ cleanup() { rm -rfv zip_static/common/magisksu.sh rm -rfv zip_static/common/init.magisk.rc rm -rfv zip_static/common/magic_mask.sh + rm -rfv uninstaller/common rm -rfv uninstaller/arm rm -rfv uninstaller/arm64 rm -rfv uninstaller/x86 @@ -91,6 +92,7 @@ zip_uninstaller() { echo "************************" echo "* Zipping uninstaller" echo "************************" + mkcp scripts/magisk_uninstaller.sh uninstaller/common cd uninstaller find . -type f -exec chmod 644 {} \; find . -type d -exec chmod 755 {} \; diff --git a/scripts/flash_script.sh b/scripts/flash_script.sh index 1500ca303..9dd3ff1e7 100644 --- a/scripts/flash_script.sh +++ b/scripts/flash_script.sh @@ -175,6 +175,7 @@ repack_boot() { cd $UNPACKDIR LD_LIBRARY_PATH=$SYSTEMLIB $BINDIR/bootimgtools --repack $BOOTIMAGE if [ -f chromeos ]; then + cp -af $CHROMEDIR /data/magisk echo " " > config echo " " > bootloader LD_LIBRARY_PATH=$SYSTEMLIB $CHROMEDIR/futility vbutil_kernel --pack new-boot.img.signed --keyblock $CHROMEDIR/kernel.keyblock --signprivate $CHROMEDIR/kernel_data_key.vbprivk --version 1 --vmlinuz new-boot.img --config config --arch arm --bootloader bootloader --flags 0x1 @@ -322,7 +323,7 @@ ui_print "- Constructing environment" if (is_mounted /data); then rm -rf /data/magisk 2>/dev/null mkdir -p /data/magisk - cp -af $BINDIR/busybox $BINDIR/sepolicy-inject $BINDIR/resetprop \ + cp -af $BINDIR/busybox $BINDIR/sepolicy-inject $BINDIR/resetprop $BINDIR/bootimgtools \ $INSTALLER/common/init.magisk.rc $INSTALLER/common/magic_mask.sh /data/magisk cp -af $INSTALLER/common/magisk.apk /data/magisk.apk chmod -R 755 /data/magisk @@ -331,7 +332,7 @@ if (is_mounted /data); then else rm -rf /cache/data_bin 2>/dev/null mkdir -p /cache/data_bin - cp -af $BINDIR/busybox $BINDIR/sepolicy-inject $BINDIR/resetprop \ + cp -af $BINDIR/busybox $BINDIR/sepolicy-inject $BINDIR/resetprop $BINDIR/bootimgtools \ $INSTALLER/common/custom_ramdisk_patch.sh $INSTALLER/common/init.magisk.rc \ $INSTALLER/common/magic_mask.sh /cache/data_bin cp -af $INSTALLER/common/magisk.apk /cache/magisk.apk diff --git a/scripts/magic_mask.sh b/scripts/magic_mask.sh index 792878fbc..3cf033f2b 100644 --- a/scripts/magic_mask.sh +++ b/scripts/magic_mask.sh @@ -2,6 +2,7 @@ LOGFILE=/cache/magisk.log DISABLEFILE=/cache/.disable_magisk +UNINSTALLER=/cache/magisk_uninstaller.sh IMG=/data/magisk.img WHITELIST="/system/bin" @@ -262,7 +263,7 @@ case $1 in # Cleanup legacy stuffs... rm -rf /cache/magisk /cache/magisk_merge /cache/magiskhide.log - [ -f $DISABLEFILE ] && unblock + [ -f $DISABLEFILE -o -f $UNINSTALLER ] && unblock if [ -d /cache/magisk_mount ]; then log_print "* Mounting cache files" @@ -303,6 +304,13 @@ case $1 in # Live patch sepolicy $BINPATH/sepolicy-inject --live + if [ -f $UNINSTALLER ]; then + touch /dev/.magisk.unblock + chcon u:object_r:device:s0 /dev/.magisk.unblock + BOOTMODE=true sh $UNINSTALLER + exit + fi + # Set up environment mkdir -p $TOOLPATH $BINPATH/busybox --install -s $TOOLPATH diff --git a/scripts/magisk_uninstaller.sh b/scripts/magisk_uninstaller.sh new file mode 100644 index 000000000..8b205079a --- /dev/null +++ b/scripts/magisk_uninstaller.sh @@ -0,0 +1,150 @@ +#!/system/bin/sh + +[ -z $BOOTMODE ] && BOOTMODE=false +TMPDIR=/tmp +($BOOTMODE) && TMPDIR=/dev/tmp + +BINDIR=/data/magisk +CHROMEDIR=$BINDIR/chromeos + +NEWBOOT=$TMPDIR/boottmp/new-boot.img +UNPACKDIR=$TMPDIR/boottmp/bootunpack +RAMDISK=$TMPDIR/boottmp/ramdisk + +SYSTEMLIB=/system/lib +[ -d /system/lib64 ] && SYSTEMLIB=/system/lib64 + +ui_print() { + echo "$1" +} + +grep_prop() { + REGEX="s/^$1=//p" + shift + FILES=$@ + if [ -z "$FILES" ]; then + FILES='/system/build.prop' + fi + cat $FILES 2>/dev/null | sed -n $REGEX | head -n 1 +} + +find_boot_image() { + if [ -z "$BOOTIMAGE" ]; then + for PARTITION in kern-a KERN-A android_boot ANDROID_BOOT kernel KERNEL boot BOOT lnx LNX; do + BOOTIMAGE=`readlink /dev/block/by-name/$PARTITION || readlink /dev/block/platform/*/by-name/$PARTITION || readlink /dev/block/platform/*/*/by-name/$PARTITION` + if [ ! -z "$BOOTIMAGE" ]; then break; fi + done + fi + if [ -z "$BOOTIMAGE" ]; then + FSTAB="/etc/recovery.fstab" + [ ! -f "$FSTAB" ] && FSTAB="/etc/recovery.fstab.bak" + [ -f "$FSTAB" ] && BOOTIMAGE=`grep -E '\b/boot\b' "$FSTAB" | grep -oE '/dev/[a-zA-Z0-9_./-]*'` + fi +} + +unpack_boot() { + rm -rf $UNPACKDIR $RAMDISK 2>/dev/null + mkdir -p $UNPACKDIR + mkdir -p $RAMDISK + cd $UNPACKDIR + LD_LIBRARY_PATH=$SYSTEMLIB $BINDIR/bootimgtools --extract $1 + + cd $RAMDISK + $BINDIR/busybox gunzip -c < $UNPACKDIR/ramdisk.gz | cpio -i +} + +repack_boot() { + cd $RAMDISK + find . | cpio -o -H newc 2>/dev/null | gzip -9 > $UNPACKDIR/ramdisk.gz + cd $UNPACKDIR + LD_LIBRARY_PATH=$SYSTEMLIB $BINDIR/bootimgtools --repack $BOOTIMAGE + if [ -f chromeos ]; then + echo " " > config + echo " " > bootloader + LD_LIBRARY_PATH=$SYSTEMLIB $CHROMEDIR/futility vbutil_kernel --pack new-boot.img.signed --keyblock $CHROMEDIR/kernel.keyblock --signprivate $CHROMEDIR/kernel_data_key.vbprivk --version 1 --vmlinuz new-boot.img --config config --arch arm --bootloader bootloader --flags 0x1 + rm -f new-boot.img + mv new-boot.img.signed new-boot.img + fi + if ($SAMSUNG); then + SAMSUNG_CHECK=$(cat new-boot.img | grep SEANDROIDENFORCE) + if [ $? -ne 0 ]; then + echo -n "SEANDROIDENFORCE" >> new-boot.img + fi + fi + if ($LGE_G); then + # Prevent secure boot error on LG G2/G3. + # Just for know, It's a pattern which bootloader verifies at boot. Thanks to LG hackers. + echo -n -e "\x41\xa9\xe4\x67\x74\x4d\x1d\x1b\xa4\x29\xf2\xec\xea\x65\x52\x79" >> new-boot.img + fi + mv new-boot.img $NEWBOOT +} + +# Set permissions +chmod -R 755 $CHROMEDIR/futility $BINDIR + +# Find the boot image +find_boot_image +if [ -z "$BOOTIMAGE" ]; then + ui_print "! Unable to detect boot image" + exit 1 +fi + +ui_print "- Found Boot Image: $BOOTIMAGE" + +# Detect special vendors +SAMSUNG=false +SAMSUNG_CHECK=$(cat /system/build.prop | grep "ro.build.fingerprint=" | grep -i "samsung") +if [ $? -eq 0 ]; then + SAMSUNG=true +fi +LGE_G=false +RBRAND=$(grep_prop ro.product.brand) +RMODEL=$(grep_prop ro.product.device) +if [ "$RBRAND" = "lge" ] || [ "$RBRAND" = "LGE" ]; then + if [ "$RMODEL" = "*D80*" ] || + [ "$RMODEL" = "*S98*" ] || + [ "$RMODEL" = "*D85*" ] || + [ "$RMODEL" = "*F40*" ]; then + LGE_G=true + ui_print "! Bump device detected" + fi +fi + +# First unpack the boot image +unpack_boot $BOOTIMAGE + +SUPERSU=false +[ -f sbin/launch_daemonsu.sh ] && SUPERSU=true + +if ($SUPERSU); then + ui_print "- SuperSU patched image detected" + rm -f magisk sbin/init.magisk.rc sbin/magic_mask.sh + repack_boot +else + if [ -f /data/stock_boot.img ]; then + ui_print "- Boot image backup found!" + NEWBOOT=/data/stock_boot.img + else + ui_print "! Boot image backup unavailable" + if [ -d ".backup" ]; then + ui_print "- Restoring ramdisk with backup" + cp -af .backup/. . + fi + rm -f magisk sbin/init.magisk.rc sbin/magic_mask.sh + repack_boot + fi +fi + +chmod 644 $NEWBOOT + +ui_print "- Flashing stock/reverted image" +[ ! -L "$BOOTIMAGE" ] && dd if=/dev/zero of=$BOOTIMAGE bs=4096 2>/dev/null +dd if=$NEWBOOT of=$BOOTIMAGE bs=4096 + +ui_print "- Removing Magisk files" +rm -rf /cache/magisk.log /cache/last_magisk.log /cache/magiskhide.log /cache/.disable_magisk \ + /cache/magisk /cache/magisk_merge /cache/magisk_mount /cache/unblock /cache/magisk_uninstaller.sh \ + /data/Magisk.apk /data/magisk.apk /data/magisk.img /data/magisk_merge.img \ + /data/busybox /data/magisk /data/custom_ramdisk_patch.sh 2>/dev/null + +($BOOTMODE) && reboot diff --git a/uninstaller/META-INF/com/google/android/update-binary b/uninstaller/META-INF/com/google/android/update-binary index d1290198f..72342cc0a 100644 --- a/uninstaller/META-INF/com/google/android/update-binary +++ b/uninstaller/META-INF/com/google/android/update-binary @@ -10,12 +10,6 @@ INSTALLER=/tmp/uninstall -# Boot Image Variables -CHROMEDIR=$INSTALLER/chromeos -NEWBOOT=$TMPDIR/boottmp/new-boot.img -UNPACKDIR=$TMPDIR/boottmp/bootunpack -RAMDISK=$TMPDIR/boottmp/ramdisk - # Default permissions umask 022 @@ -69,20 +63,6 @@ getvar() { eval $VARNAME=\$VALUE } -find_boot_image() { - if [ -z "$BOOTIMAGE" ]; then - for PARTITION in kern-a KERN-A android_boot ANDROID_BOOT kernel KERNEL boot BOOT lnx LNX; do - BOOTIMAGE=`readlink /dev/block/by-name/$PARTITION || readlink /dev/block/platform/*/by-name/$PARTITION || readlink /dev/block/platform/*/*/by-name/$PARTITION` - if [ ! -z "$BOOTIMAGE" ]; then break; fi - done - fi - if [ -z "$BOOTIMAGE" ]; then - FSTAB="/etc/recovery.fstab" - [ ! -f "$FSTAB" ] && FSTAB="/etc/recovery.fstab.bak" - [ -f "$FSTAB" ] && BOOTIMAGE=`grep -E '\b/boot\b' "$FSTAB" | grep -oE '/dev/[a-zA-Z0-9_./-]*'` - fi -} - is_mounted() { if [ ! -z "$2" ]; then cat /proc/mounts | grep $1 | grep $2, >/dev/null @@ -102,122 +82,6 @@ grep_prop() { cat $FILES 2>/dev/null | sed -n $REGEX | head -n 1 } -mount_image() { - if [ ! -d "$2" ]; then - mount -o rw,remount rootfs / - mkdir -p $2 2>/dev/null - ($BOOTMODE) && mount -o ro,remount rootfs / - [ ! -d "$2" ] && return 1 - fi - if (! is_mounted $2); then - LOOPDEVICE= - for LOOP in 0 1 2 3 4 5 6 7; do - if (! is_mounted $2); then - LOOPDEVICE=/dev/block/loop$LOOP - if [ ! -f "$LOOPDEVICE" ]; then - mknod $LOOPDEVICE b 7 $LOOP 2>/dev/null - fi - losetup $LOOPDEVICE $1 - if [ "$?" -eq "0" ]; then - mount -t ext4 -o loop $LOOPDEVICE $2 - if (! is_mounted $2); then - /system/bin/toolbox mount -t ext4 -o loop $LOOPDEVICE $2 - fi - if (! is_mounted $2); then - /system/bin/toybox mount -t ext4 -o loop $LOOPDEVICE $2 - fi - fi - if (is_mounted $2); then - ui_print "- Mounting $1 to $2" - break; - fi - fi - done - fi -} - -unpack_boot() { - rm -rf $UNPACKDIR $RAMDISK 2>/dev/null - mkdir -p $UNPACKDIR - mkdir -p $RAMDISK - cd $UNPACKDIR - LD_LIBRARY_PATH=$SYSTEMLIB $BINDIR/bootimgtools --extract $1 - - cd $RAMDISK - gunzip -c < $UNPACKDIR/ramdisk.gz | cpio -i -} - -repack_boot() { - cd $RAMDISK - find . | cpio -o -H newc 2>/dev/null | gzip -9 > $UNPACKDIR/ramdisk.gz - cd $UNPACKDIR - LD_LIBRARY_PATH=$SYSTEMLIB $BINDIR/bootimgtools --repack $BOOTIMAGE - if [ -f chromeos ]; then - echo " " > config - echo " " > bootloader - LD_LIBRARY_PATH=$SYSTEMLIB $CHROMEDIR/futility vbutil_kernel --pack new-boot.img.signed --keyblock $CHROMEDIR/kernel.keyblock --signprivate $CHROMEDIR/kernel_data_key.vbprivk --version 1 --vmlinuz new-boot.img --config config --arch arm --bootloader bootloader --flags 0x1 - rm -f new-boot.img - mv new-boot.img.signed new-boot.img - fi - if ($SAMSUNG); then - SAMSUNG_CHECK=$(cat new-boot.img | grep SEANDROIDENFORCE) - if [ $? -ne 0 ]; then - echo -n "SEANDROIDENFORCE" >> new-boot.img - fi - fi - if ($LGE_G); then - # Prevent secure boot error on LG G2/G3. - # Just for know, It's a pattern which bootloader verifies at boot. Thanks to LG hackers. - echo -n -e "\x41\xa9\xe4\x67\x74\x4d\x1d\x1b\xa4\x29\xf2\xec\xea\x65\x52\x79" >> new-boot.img - fi - mv new-boot.img $NEWBOOT -} - -revert_boot() { - rm -rf $TMPDIR/boottmp 2>/dev/null - mkdir -p $TMPDIR/boottmp - - ui_print "- Unpacking boot image" - unpack_boot $BOOTIMAGE - - SUPERSU=false - [ -f sbin/launch_daemonsu.sh ] && SUPERSU=true - - if ($SUPERSU); then - ui_print "- SuperSU patched boot detected!" - SUIMG=/data/su.img - mount_image $SUIMG /su - if (is_mounted /su); then - SUPERSULOOP=$LOOPDEVICE - gunzip -c < $UNPACKDIR/ramdisk.gz > $UNPACKDIR/ramdisk - ui_print "- Using sukernel to restore ramdisk" - # Restore ramdisk - LD_LIBRARY_PATH=$SYSTEMLIB /su/bin/sukernel --cpio-restore $UNPACKDIR/ramdisk $UNPACKDIR/ramdisk - if [ $? -ne 0 ]; then - ui_print "! Unable to restore ramdisk" - ui_print "! Will still remove Magisk additions" - fi - rm -rf $RAMDISK - mkdir -p $RAMDISK - cd $RAMDISK - cpio -i < $UNPACKDIR/ramdisk - rm -f $UNPACKDIR/ramdisk - fi - elif [ -d ".backup" ]; then - ui_print "- Restoring ramdisk with backup" - cp -af .backup/. . - else - ui_print "! No ramdisk backup found" - ui_print "! Will still remove Magisk additions" - fi - - # Remove possible boot modifications - rm -rf magisk init.magisk.rc sbin/magic_mask.sh .backup 2>/dev/null - - ui_print "- Repacking boot image" - repack_boot -} - ########################################################################################## # Main ########################################################################################## @@ -241,25 +105,6 @@ if [ ! -f '/system/build.prop' ]; then exit 1 fi -SAMSUNG=false -SAMSUNG_CHECK=$(cat /system/build.prop | grep "ro.build.fingerprint=" | grep -i "samsung") -if [ $? -eq 0 ]; then - SAMSUNG=true -fi - -LGE_G=false -RBRAND=$(grep_prop ro.product.brand) -RMODEL=$(grep_prop ro.product.device) -if [ "$RBRAND" = "lge" ] || [ "$RBRAND" = "LGE" ]; then - if [ "$RMODEL" = "*D80*" ] || - [ "$RMODEL" = "*S98*" ] || - [ "$RMODEL" = "*D85*" ] || - [ "$RMODEL" = "*F40*" ]; then - LGE_G=true - ui_print "! Bump device detected" - fi -fi - API=$(grep_prop ro.build.version.sdk) ABI=$(grep_prop ro.product.cpu.abi | cut -c-3) ABI2=$(grep_prop ro.product.cpu.abi2 | cut -c-3) @@ -273,18 +118,12 @@ if [ "$ABILONG" = "arm64-v8a" ]; then ARCH=arm64; IS64BIT=true; fi; if [ "$ABILONG" = "x86_64" ]; then ARCH=x64; IS64BIT=true; fi; ui_print "- Device platform: $ARCH" - +CHROMEDIR=$INSTALLER/chromeos BINDIR=$INSTALLER/$ARCH -chmod -R 755 $CHROMEDIR/futility $BINDIR -SYSTEMLIB=/system/lib -($IS64BIT) && SYSTEMLIB=/system/lib64 - -find_boot_image -if [ -z "$BOOTIMAGE" ]; then - ui_print "! Unable to detect boot image" - exit 1 -fi +# Copy the binaries to /data/magisk +mkdir -p /data/magisk 2>/dev/null +cp -af $BINDIR/bootimgtools $CHROMEDIR /data/magisk ########################################################################################## # Detection all done, start installing @@ -293,48 +132,20 @@ fi ui_print "- Found Boot Image: $BOOTIMAGE" if (is_mounted /data); then - PATH=/data/busybox:$PATH - cp -f /data/stock_boot_*.gz /data/stock_boot.img.gz 2>/dev/null - gunzip -d < /data/stock_boot.img.gz > /data/stock_boot.img 2>/dev/null - rm -f /data/stock_boot.img.gz 2>/dev/null - if [ -f /data/stock_boot.img ]; then - ui_print "- Boot image backup found!" - NEWBOOT=/data/stock_boot.img - else - ui_print "! Boot image backup unavalible, try using ramdisk backup" - revert_boot - fi - ui_print "- Removing Magisk files" - rm -rf /cache/magisk.log /cache/last_magisk.log /cache/magiskhide.log \ - /cache/magisk /cache/magisk_merge /cache/magisk_mount /cache/unblock \ - /data/Magisk.apk /data/magisk.apk /data/magisk.img /data/magisk_merge.img \ - /data/busybox /data/magisk /data/custom_ramdisk_patch.sh 2>/dev/null + ui_print "- Running uninstaller scripts" + sh $INSTALLER/common/magisk_uninstaller.sh else ui_print "! Data unavailable" - ui_print "! Impossible to restore original boot image" - ui_print "! Try using ramdisk backup" - revert_boot - ui_print "- Removing Magisk files" - rm -rf /cache/magisk.log /cache/last_magisk.log /cache/magiskhide.log \ - /cache/magisk /cache/magisk_merge /cache/magisk_mount /cache/unblock 2>/dev/null - ui_print "*****************************************" - ui_print " Magisk is not fully removed yet " - ui_print " Please manually remove /data/magisk.img " - ui_print "*****************************************" + ui_print "! Placing uninstall script to /cache" + ui_print "! The device will reboot multiple times" + cp -af $INSTALLER/common/magisk_uninstaller.sh /cache/magisk_uninstaller.sh + umount /system + sleep 5 + reboot fi chmod 644 $NEWBOOT -ui_print "- Flashing reverted image" -[ ! -L "$BOOTIMAGE" ] && dd if=/dev/zero of=$BOOTIMAGE bs=4096 2>/dev/null -dd if=$NEWBOOT of=$BOOTIMAGE bs=4096 - umount /system -if [ ! -z $SUPERSU ]; then - umount /su - losetup -d $SUPERSULOOP - rmdir /su -fi - ui_print "- Done" exit 0