Update uninstallation

This commit is contained in:
topjohnwu 2017-07-11 00:50:27 +08:00
parent 493642eb38
commit 264c6a50b6
4 changed files with 255 additions and 70 deletions

View File

@ -8,120 +8,103 @@
# The Magisk main binary will pick up the script, and uninstall itself, following a reboot # The Magisk main binary will pick up the script, and uninstall itself, following a reboot
# This script can also be used in flashable zip with the uninstaller_loader.sh # This script can also be used in flashable zip with the uninstaller_loader.sh
# #
# This script will try to do restoration in the following order: # This script will try to do restoration with the following:
# 1. Find and restore the original stock boot image dump (OTA proof) # 1-1. Find and restore the original stock boot image dump (OTA proof)
# 2. Restore ramdisk from the internal backup (ramdisk fully restored, not OTA friendly) # 1-2. If 1-1 fails, restore ramdisk from the internal backup
# 3. Remove added files in ramdisk, modified files are remained intact. By doing so, Magisk # (ramdisk fully restored, not OTA friendly)
# will not be started at boot, but not removed clean enough # 1-3. If 1-2 fails, it will remove added files in ramdisk, however modified files
# # are remained modified, because we have no backups. By doing so, Magisk will
# Finally, this uninstaller will remove all Magisk related files # not be started at boot, but this isn't actually 100% cleaned up
# (The list is LARGE, most likely due to bad decision in early versions # 2. Remove all Magisk related files
# the latest versions has much less bloat to cleanup) # (The list is LARGE, most likely due to bad decision in early versions
# the latest versions has much less bloat to cleanup)
# #
########################################################################################## ##########################################################################################
[ -z $BOOTMODE ] && BOOTMODE=false
MAGISKBIN=/data/magisk
CHROMEDIR=$MAGISKBIN/chromeos
[ -d /system/lib64 ] && SYSTEMLIB=/system/lib64 || SYSTEMLIB=/system/lib
# Default permissions
umask 022
# Call ui_print_wrap if exists, or else simply use echo # Call ui_print_wrap if exists, or else simply use echo
# Useful when wrapped in flashable zip # Useful when wrapped in flashable zip
ui_print_wrap() { ui_print_wrap() {
type ui_print >/dev/null 2>&1 && ui_print "$1" || echo "$1" type ui_print >/dev/null 2>&1 && ui_print "$1" || echo "$1"
} }
grep_prop() { # Call abort if exists, or else show error message and exit
REGEX="s/^$1=//p" # Essential when wrapped in flashable zip
shift abort_wrap() {
FILES=$@ type abort >/dev/null 2>&1
if [ -z "$FILES" ]; then if [ $? -ne 0 ]; then
FILES='/system/build.prop' ui_print_wrap "$1"
fi exit 1
cat $FILES 2>/dev/null | sed -n "$REGEX" | head -n 1 else
} abort "$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 fi
} }
# Environments if [ ! -d $MAGISKBIN -o ! -f $MAGISKBIN/magiskboot -o ! -f $MAGISKBIN/util_functions.sh ]; then
# Set permissions ui_print_wrap "! Cannot find $MAGISKBIN"
chmod -R 755 $CHROMEDIR/futility $MAGISKBIN 2>/dev/null exit 1
fi
[ -z $BOOTMODE ] && BOOTMODE=false
MAGISKBIN=/data/magisk
CHROMEDIR=$MAGISKBIN/chromeos
# Default permissions
umask 022
# Load utility functions
. $MAGISKBIN/util_functions.sh
# Find the boot image # Find the boot image
find_boot_image find_boot_image
if [ -z "$BOOTIMAGE" ]; then [ -z $BOOTIMAGE ] && abort "! Unable to detect boot image"
ui_print_wrap "! Unable to detect boot image"
exit 1
fi
ui_print_wrap "- Found Boot Image: $BOOTIMAGE" ui_print_wrap "- Found Boot Image: $BOOTIMAGE"
cd $MAGISKBIN cd $MAGISKBIN
ui_print_wrap "- Unpacking boot image" ui_print_wrap "- Unpacking boot image"
LD_LIBRARY_PATH=$SYSTEMLIB ./magiskboot --unpack $BOOTIMAGE ./magiskboot --unpack "$BOOTIMAGE"
if [ $? -ne 0 ]; then [ $? -ne 0 ] && abort_wrap "! Unable to unpack boot image"
ui_print_wrap "! Unable to unpack boot image"
exit 1
fi
# Update our previous backups to new format if exists # Update our previous backup to new format if exists
if [ -f /data/stock_boot.img ]; then if [ -f /data/stock_boot.img ]; then
SHA1=`LD_LIBRARY_PATH=$SYSTEMLIB ./magiskboot --sha1 /data/stock_boot.img | tail -n 1` SHA1=`./magiskboot --sha1 /data/stock_boot.img | tail -n 1`
STOCKDUMP=/data/stock_boot_${SHA1}.img STOCKDUMP=/data/stock_boot_${SHA1}.img
mv /data/stock_boot.img $STOCKDUMP mv /data/stock_boot.img $STOCKDUMP
LD_LIBRARY_PATH=$SYSTEMLIB ./magiskboot --compress $STOCKDUMP ./magiskboot --compress $STOCKDUMP
fi fi
# Detect boot image state # Detect boot image state
LD_LIBRARY_PATH=$SYSTEMLIB ./magiskboot --cpio-test ramdisk.cpio ./magiskboot --cpio-test ramdisk.cpio
case $? in case $? in
0 ) 0 ) # Stock boot
ui_print_wrap "- Stock boot image detected!" ui_print_wrap "- Stock boot image detected!"
ui_print_wrap "! Magisk is not installed!" ui_print_wrap "! Magisk is not installed!"
exit exit
;; ;;
1 ) 1 ) # Magisk patched
ui_print_wrap "- Magisk patched image detected!" ui_print_wrap "- Magisk patched image detected!"
# Find SHA1 of stock boot image # Find SHA1 of stock boot image
if [ -z $SHA1 ]; then if [ -z $SHA1 ]; then
LD_LIBRARY_PATH=$SYSTEMLIB ./magiskboot --cpio-extract ramdisk.cpio init.magisk.rc init.magisk.rc.old ./magiskboot --cpio-extract ramdisk.cpio init.magisk.rc init.magisk.rc.old
SHA1=`grep_prop "# STOCKSHA1" init.magisk.rc.old` SHA1=`grep_prop "# STOCKSHA1" init.magisk.rc.old`
[ ! -z $SHA1 ] && STOCKDUMP=/data/stock_boot_${SHA1}.img
rm -f init.magisk.rc.old rm -f init.magisk.rc.old
fi fi
[ ! -z $SHA1 ] && STOCKDUMP=/data/stock_boot_${SHA1}.img
if [ -f ${STOCKDUMP}.gz ]; then if [ -f ${STOCKDUMP}.gz ]; then
ui_print_wrap "- Boot image backup found!" ui_print_wrap "- Boot image backup found!"
LD_LIBRARY_PATH=$SYSTEMLIB ./magiskboot --decompress ${STOCKDUMP}.gz stock_boot.img ./magiskboot --decompress ${STOCKDUMP}.gz stock_boot.img
else else
ui_print_wrap "! Boot image backup unavailable" ui_print_wrap "! Boot image backup unavailable"
ui_print_wrap "- Restoring ramdisk with backup" ui_print_wrap "- Restoring ramdisk with backup"
LD_LIBRARY_PATH=$SYSTEMLIB ./magiskboot --cpio-restore ramdisk.cpio ./magiskboot --cpio-restore ramdisk.cpio
LD_LIBRARY_PATH=$SYSTEMLIB ./magiskboot --repack $BOOTIMAGE stock_boot.img ./magiskboot --repack $BOOTIMAGE stock_boot.img
fi fi
;; ;;
2 ) # Other patched 2 ) # Other patched
ui_print_wrap "! Boot image patched by other programs!" ui_print_wrap "! Boot image patched by other programs!"
ui_print_wrap "! Cannot uninstall with this uninstaller" abort_wrap "! Cannot uninstall with this uninstaller"
exit 1
;; ;;
esac esac
@ -138,10 +121,10 @@ if [ -f chromeos ]; then
fi fi
ui_print_wrap "- Flashing stock/reverted image" ui_print_wrap "- Flashing stock/reverted image"
if [ -L $BOOTIMAGE ]; then if [ -L "$BOOTIMAGE" ]; then
dd if=stock_boot.img of=$BOOTIMAGE bs=4096 dd if=stock_boot.img of="$BOOTIMAGE" bs=4096
else else
cat stock_boot.img /dev/zero | dd of=$BOOTIMAGE bs=4096 cat stock_boot.img /dev/zero | dd of="$BOOTIMAGE" bs=4096 >/dev/null 2>&1
fi fi
rm -f stock_boot.img rm -f stock_boot.img

View File

@ -0,0 +1,192 @@
##########################################################################################
#
# Magisk General Utility Functions
# by topjohnwu
#
# Used in flash_script.sh, addon.d.sh, magisk module installers, and uninstaller
#
##########################################################################################
get_outfd() {
readlink /proc/$$/fd/$OUTFD 2>/dev/null | grep /tmp >/dev/null
if [ "$?" -eq "0" ]; then
OUTFD=0
for FD in `ls /proc/$$/fd`; do
readlink /proc/$$/fd/$FD 2>/dev/null | grep pipe >/dev/null
if [ "$?" -eq "0" ]; then
ps | grep " 3 $FD " | grep -v grep >/dev/null
if [ "$?" -eq "0" ]; then
OUTFD=$FD
break
fi
fi
done
fi
}
ui_print() {
if $BOOTMODE; then
echo "$1"
else
echo -n -e "ui_print $1\n" >> /proc/self/fd/$OUTFD
echo -n -e "ui_print\n" >> /proc/self/fd/$OUTFD
fi
}
getvar() {
local VARNAME=$1
local VALUE=$(eval echo \$"$VARNAME");
for FILE in /dev/.magisk /data/.magisk /cache/.magisk /system/.magisk; do
if [ -z "$VALUE" ]; then
LINE=$(cat $FILE 2>/dev/null | grep "$VARNAME=")
if [ ! -z "$LINE" ]; then
VALUE=${LINE#*=}
fi
fi
done
eval $VARNAME=\$VALUE
}
find_boot_image() {
if [ -z "$BOOTIMAGE" ]; then
for BLOCK in boot_a BOOT_A kern-a KERN-A android_boot ANDROID_BOOT kernel KERNEL boot BOOT lnx LNX; do
BOOTIMAGE=`ls /dev/block/by-name/$BLOCK || ls /dev/block/platform/*/by-name/$BLOCK || ls /dev/block/platform/*/*/by-name/$BLOCK` 2>/dev/null
[ ! -z $BOOTIMAGE ] && break
done
fi
# Recovery fallback
if [ -z "$BOOTIMAGE" ]; then
for FSTAB in /etc/*fstab*; do
BOOTIMAGE=`grep -E '\b/boot\b' $FSTAB | grep -v "#" | grep -oE '/dev/[a-zA-Z0-9_./-]*'`
[ ! -z $BOOTIMAGE ] && break
done
fi
[ -L "$BOOTIMAGE" ] && BOOTIMAGE=`readlink $BOOTIMAGE`
}
is_mounted() {
if [ ! -z "$2" ]; then
cat /proc/mounts | grep $1 | grep $2, >/dev/null
else
cat /proc/mounts | grep $1 >/dev/null
fi
return $?
}
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
}
remove_system_su() {
if [ -f /system/bin/su -o -f /system/xbin/su ] && [ ! -f /su/bin/su ]; then
ui_print "! System installed root detected, mount rw :("
mount -o rw,remount /system
# SuperSU
if [ -e /system/bin/.ext/.su ]; then
mv -f /system/bin/app_process32_original /system/bin/app_process32 2>/dev/null
mv -f /system/bin/app_process64_original /system/bin/app_process64 2>/dev/null
mv -f /system/bin/install-recovery_original.sh /system/bin/install-recovery.sh 2>/dev/null
cd /system/bin
if [ -e app_process64 ]; then
ln -sf app_process64 app_process
else
ln -sf app_process32 app_process
fi
fi
rm -rf /system/.pin /system/bin/.ext /system/etc/.installed_su_daemon /system/etc/.has_su_daemon \
/system/xbin/daemonsu /system/xbin/su /system/xbin/sugote /system/xbin/sugote-mksh /system/xbin/supolicy \
/system/bin/app_process_init /system/bin/su /cache/su /system/lib/libsupol.so /system/lib64/libsupol.so \
/system/su.d /system/etc/install-recovery.sh /system/etc/init.d/99SuperSUDaemon /cache/install-recovery.sh \
/system/.supersu /cache/.supersu /data/.supersu \
/system/app/Superuser.apk /system/app/SuperSU /cache/Superuser.apk 2>/dev/null
fi
}
api_level_arch_detect() {
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`
ABILONG=`grep_prop ro.product.cpu.abi`
ARCH=arm
IS64BIT=false
if [ "$ABI" = "x86" ]; then ARCH=x86; fi;
if [ "$ABI2" = "x86" ]; then ARCH=x86; fi;
if [ "$ABILONG" = "arm64-v8a" ]; then ARCH=arm64; IS64BIT=true; fi;
if [ "$ABILONG" = "x86_64" ]; then ARCH=x64; IS64BIT=true; fi;
}
recovery_actions() {
# TWRP bug fix
mount -o bind /dev/urandom /dev/random
# Temporarily block out all custom recovery binaries/libs
mv /sbin /sbin_tmp
# Add all possible library paths
OLD_LD_PATH=$LD_LIBRARY_PATH
$IS64BIT && export LD_LIBRARY_PATH=/system/lib64:/system/vendor/lib64 || export LD_LIBRARY_PATH=/system/lib:/system/vendor/lib
}
recovery_cleanup() {
mv /sbin_tmp /sbin
# Clear LD_LIBRARY_PATH
export LD_LIBRARY_PATH=$OLD_LD_PATH
ui_print "- Unmounting partitions"
umount -l /system
umount -l /vendor 2>/dev/null
umount -l /dev/random
}
abort() {
ui_print "$1"
mv /sbin_tmp /sbin 2>/dev/null
exit 1
}
set_perm() {
chown $2:$3 $1 || exit 1
chmod $4 $1 || exit 1
if [ ! -z $5 ]; then
chcon $5 $1 2>/dev/null
else
chcon 'u:object_r:system_file:s0' $1 2>/dev/null
fi
}
set_perm_recursive() {
find $1 -type d 2>/dev/null | while read dir; do
set_perm $dir $2 $3 $4 $6
done
find $1 -type f 2>/dev/null | while read file; do
set_perm $file $2 $3 $5 $6
done
}
mktouch() {
mkdir -p ${1%/*}
if [ -z "$2" ]; then
touch $1
else
echo $2 > $1
fi
chmod 644 $1
}
request_size_check() {
reqSizeM=`du -s $1 | cut -f1`
reqSizeM=$((reqSizeM / 1024 + 1))
}
image_size_check() {
SIZE="`$MAGISKBIN/magisk --imgsize $IMG`"
curUsedM=`echo "$SIZE" | cut -d" " -f1`
curSizeM=`echo "$SIZE" | cut -d" " -f2`
curFreeM=$((curSizeM - curUsedM))
}

View File

@ -176,6 +176,14 @@ public class MagiskFragment extends Fragment
} }
in.close(); in.close();
out.close(); out.close();
in = magiskManager.getAssets().open(MagiskManager.UTIL_FUNCTIONS);
File utils = new File(magiskManager.getCacheDir(), MagiskManager.UTIL_FUNCTIONS);
out = new FileOutputStream(utils);
while ((read = in.read(bytes)) != -1) {
out.write(bytes, 0, read);
}
in.close();
out.close();
ProgressDialog progress = new ProgressDialog(getActivity()); ProgressDialog progress = new ProgressDialog(getActivity());
progress.setTitle(R.string.reboot); progress.setTitle(R.string.reboot);
progress.show(); progress.show();
@ -190,6 +198,7 @@ public class MagiskFragment extends Fragment
progress.setMessage(getString(R.string.reboot_countdown, 0)); progress.setMessage(getString(R.string.reboot_countdown, 0));
Shell.su(true, Shell.su(true,
"mv -f " + uninstaller + " /cache/" + MagiskManager.UNINSTALLER, "mv -f " + uninstaller + " /cache/" + MagiskManager.UNINSTALLER,
"mv -f " + utils + " /data/magisk/" + MagiskManager.UTIL_FUNCTIONS,
"reboot" "reboot"
); );
} }

View File

@ -26,6 +26,7 @@ public class MagiskManager extends Application {
public static final String TMP_FOLDER_PATH = "/dev/tmp"; public static final String TMP_FOLDER_PATH = "/dev/tmp";
public static final String MAGISK_PATH = "/magisk"; public static final String MAGISK_PATH = "/magisk";
public static final String UNINSTALLER = "magisk_uninstaller.sh"; public static final String UNINSTALLER = "magisk_uninstaller.sh";
public static final String UTIL_FUNCTIONS= "util_functions.sh";
public static final String INTENT_SECTION = "section"; public static final String INTENT_SECTION = "section";
public static final String BUSYBOX_VERSION = "1.26.2"; public static final String BUSYBOX_VERSION = "1.26.2";
public static final String MAGISKHIDE_PROP = "persist.magisk.hide"; public static final String MAGISKHIDE_PROP = "persist.magisk.hide";