diff --git a/build.py b/build.py index 608caef80..3fa07c334 100755 --- a/build.py +++ b/build.py @@ -52,6 +52,7 @@ def build_all(args): build_binary(args) build_apk(args) zip_main(args) + zip_uninstaller(args) def build_binary(args): header('* Building Magisk binaries') @@ -197,6 +198,10 @@ def zip_uninstaller(args): zip_with_msg(zipf, source, target) source = os.path.join('scripts', 'magisk_uninstaller.sh') + target = 'magisk_uninstaller.sh' + zip_with_msg(zipf, source, target) + + source = os.path.join('scripts', 'uninstaller_loader.sh') target = os.path.join('META-INF', 'com', 'google', 'android', 'update-binary') zip_with_msg(zipf, source, target) @@ -232,7 +237,7 @@ parser = argparse.ArgumentParser(description='Magisk build script') parser.add_argument('--release', action='store_true', help='compile Magisk for release') subparsers = parser.add_subparsers(title='actions') -all_parser = subparsers.add_parser('all', help='build everything and create flashable zip') +all_parser = subparsers.add_parser('all', help='build everything and create flashable zip with uninstaller') all_parser.add_argument('versionString') all_parser.add_argument('versionCode', type=int) all_parser.set_defaults(func=build_all) diff --git a/jni/daemon/bootstages.c b/jni/daemon/bootstages.c index b05d43cbd..3531758dd 100644 --- a/jni/daemon/bootstages.c +++ b/jni/daemon/bootstages.c @@ -492,7 +492,7 @@ void post_fs_data(int client) { // uninstaller if (access(UNINSTALLER, F_OK) == 0) { close(open(UNBLOCKFILE, O_RDONLY | O_CREAT)); - char *const command[] = { "sh", UNBLOCKFILE, NULL }; + char *const command[] = { "sh", "-c", "BOOTMODE=true sh " UNBLOCKFILE, NULL }; run_command(NULL, "/system/bin/sh", command); return; } diff --git a/scripts/boot_patch.sh b/scripts/boot_patch.sh index 9b9bd5727..773952387 100644 --- a/scripts/boot_patch.sh +++ b/scripts/boot_patch.sh @@ -155,7 +155,7 @@ case $? in cp -af ramdisk.cpio ramdisk.cpio.orig ;; 2 ) # Other patched - ui_print_wrap "! Other patched boot detected!" + ui_print_wrap "! Boot image patched by other programs!" ui_print_wrap "! Please restore stock boot image" exit 1 ;; diff --git a/scripts/magisk_uninstaller.sh b/scripts/magisk_uninstaller.sh index 34dd1c5c5..eba918835 100644 --- a/scripts/magisk_uninstaller.sh +++ b/scripts/magisk_uninstaller.sh @@ -1,14 +1,32 @@ +#!/system/bin/sh +########################################################################################## +# +# Magisk Uninstaller +# by topjohnwu +# +# This script can be placed in /cache/magisk_uninstaller.sh +# 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 will try to do restoration in the following order: +# 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) +# 3. Remove added files in ramdisk, modified files are remained intact. By doing so, Magisk +# will not be started at boot, but not removed clean enough +# +# Finally, this uninstaller will remove all Magisk related files +# (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 -# This path should work in any cases -TMPDIR=/dev/tmp - -BOOTTMP=$TMPDIR/boottmp MAGISKBIN=/data/magisk CHROMEDIR=$MAGISKBIN/chromeos -SYSTEMLIB=/system/lib -[ -d /system/lib64 ] && SYSTEMLIB=/system/lib64 +[ -d /system/lib64 ] && SYSTEMLIB=/system/lib64 || SYSTEMLIB=/system/lib # Default permissions umask 022 @@ -56,70 +74,76 @@ fi ui_print_wrap "- Found Boot Image: $BOOTIMAGE" -rm -rf $BOOTTMP 2>/dev/null -mkdir -p $BOOTTMP -cd $BOOTTMP +cd $MAGISKBIN ui_print_wrap "- Unpacking boot image" -LD_LIBRARY_PATH=$SYSTEMLIB $MAGISKBIN/magiskboot --unpack $BOOTIMAGE +LD_LIBRARY_PATH=$SYSTEMLIB ./magiskboot --unpack $BOOTIMAGE if [ $? -ne 0 ]; then ui_print_wrap "! Unable to unpack boot image" exit 1 fi -# Update our previous backup to new format if exists +# Update our previous backups to new format if exists if [ -f /data/stock_boot.img ]; then - SHA1=`LD_LIBRARY_PATH=$SYSTEMLIB $MAGISKBIN/magiskboot --sha1 /data/stock_boot.img | tail -n 1` + SHA1=`LD_LIBRARY_PATH=$SYSTEMLIB ./magiskboot --sha1 /data/stock_boot.img | tail -n 1` STOCKDUMP=/data/stock_boot_${SHA1}.img mv /data/stock_boot.img $STOCKDUMP - LD_LIBRARY_PATH=$SYSTEMLIB $MAGISKBIN/magiskboot --compress $STOCKDUMP + LD_LIBRARY_PATH=$SYSTEMLIB ./magiskboot --compress $STOCKDUMP fi # Detect boot image state -LD_LIBRARY_PATH=$SYSTEMLIB $MAGISKBIN/magiskboot --cpio-test ramdisk.cpio +LD_LIBRARY_PATH=$SYSTEMLIB ./magiskboot --cpio-test ramdisk.cpio case $? in 0 ) + ui_print_wrap "- Stock boot image detected!" ui_print_wrap "! Magisk is not installed!" - ui_print_wrap "! Nothing to uninstall" exit ;; 1 ) + ui_print_wrap "- Magisk patched image detected!" # Find SHA1 of stock boot image if [ -z $SHA1 ]; then - LD_LIBRARY_PATH=$SYSTEMLIB $MAGISKBIN/magiskboot --cpio-extract ramdisk.cpio init.magisk.rc init.magisk.rc - SHA1=`grep_prop "# STOCKSHA1" init.magisk.rc` + LD_LIBRARY_PATH=$SYSTEMLIB ./magiskboot --cpio-extract ramdisk.cpio init.magisk.rc 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 + rm -f init.magisk.rc.old fi if [ -f ${STOCKDUMP}.gz ]; then ui_print_wrap "- Boot image backup found!" - LD_LIBRARY_PATH=$SYSTEMLIB $MAGISKBIN/magiskboot --decompress ${STOCKDUMP}.gz stock_boot.img + LD_LIBRARY_PATH=$SYSTEMLIB ./magiskboot --decompress ${STOCKDUMP}.gz stock_boot.img else ui_print_wrap "! Boot image backup unavailable" ui_print_wrap "- Restoring ramdisk with backup" - LD_LIBRARY_PATH=$SYSTEMLIB $MAGISKBIN/magiskboot --cpio-restore ramdisk.cpio - LD_LIBRARY_PATH=$SYSTEMLIB $MAGISKBIN/magiskboot --repack $BOOTIMAGE stock_boot.img + LD_LIBRARY_PATH=$SYSTEMLIB ./magiskboot --cpio-restore ramdisk.cpio + LD_LIBRARY_PATH=$SYSTEMLIB ./magiskboot --repack $BOOTIMAGE stock_boot.img fi ;; - 2 ) - ui_print_wrap "- SuperSU patched image detected" - LD_LIBRARY_PATH=$SYSTEMLIB $MAGISKBIN/magiskboot --cpio-restore ramdisk.cpio - LD_LIBRARY_PATH=$SYSTEMLIB $MAGISKBIN/magiskboot --repack $BOOTIMAGE stock_boot.img + 2 ) # Other patched + ui_print_wrap "! Boot image patched by other programs!" + ui_print_wrap "! Cannot uninstall with this uninstaller" + exit 1 ;; esac # Sign chromeos boot if [ -f chromeos ]; then - echo > config - echo > bootloader - LD_LIBRARY_PATH=$SYSTEMLIB $CHROMEDIR/futility vbutil_kernel --pack stock_boot.img.signed --keyblock $CHROMEDIR/kernel.keyblock --signprivate $CHROMEDIR/kernel_data_key.vbprivk --version 1 --vmlinuz stock_boot.img --config config --arch arm --bootloader bootloader --flags 0x1 - rm -f stock_boot.img + echo > empty + + LD_LIBRARY_PATH=$SYSTEMLIB $CHROMEDIR/futility vbutil_kernel --pack stock_boot.img.signed \ + --keyblock $CHROMEDIR/kernel.keyblock --signprivate $CHROMEDIR/kernel_data_key.vbprivk \ + --version 1 --vmlinuz stock_boot.img --config empty --arch arm --bootloader empty --flags 0x1 + + rm -f empty stock_boot.img mv stock_boot.img.signed stock_boot.img fi ui_print_wrap "- Flashing stock/reverted image" -[ ! -L "$BOOTIMAGE" ] && dd if=/dev/zero of=$BOOTIMAGE bs=4096 2>/dev/null -dd if=stock_boot.img of=$BOOTIMAGE bs=4096 +if [ -L $BOOTIMAGE ]; then + dd if=stock_boot.img of=$BOOTIMAGE bs=4096 +else + cat stock_boot.img /dev/zero | dd of=$BOOTIMAGE bs=4096 +fi +rm -f stock_boot.img ui_print_wrap "- Removing Magisk files" rm -rf /cache/magisk.log /cache/last_magisk.log /cache/magiskhide.log /cache/.disable_magisk \ diff --git a/scripts/uninstaller_loader.sh b/scripts/uninstaller_loader.sh new file mode 100644 index 000000000..95ff4a1cc --- /dev/null +++ b/scripts/uninstaller_loader.sh @@ -0,0 +1,133 @@ +#!/sbin/sh +########################################################################################## +# +# Magisk Uninstaller (used in recovery) +# by topjohnwu +# +# This script will load the real uninstaller in a flashable zip +# +########################################################################################## + +INSTALLER=/tmp/uninstall + +# Default permissions +umask 022 + +########################################################################################## +# Flashable update-binary preparation +########################################################################################## + +OUTFD=$2 +ZIP=$3 + +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 + +mkdir -p $INSTALLER +cd $INSTALLER +unzip -o "$ZIP" + +########################################################################################## +# Functions +########################################################################################## + +ui_print() { + echo -n -e "ui_print $1\n" >> /proc/self/fd/$OUTFD + echo -n -e "ui_print\n" >> /proc/self/fd/$OUTFD +} + +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 +} + +########################################################################################## +# Main +########################################################################################## + +ui_print "*****************************" +ui_print " Magisk Uninstaller " +ui_print "*****************************" + +if [ ! -d "$INSTALLER/arm" ]; then + ui_print "! Failed: Unable to extract zip file!" + exit 1 +fi + +ui_print "- Mounting /system(ro), /cache, /data" +mount -o ro /system 2>/dev/null +mount /cache 2>/dev/null +mount /data 2>/dev/null + +if [ ! -f '/system/build.prop' ]; then + ui_print "! Failed: /system could not be mounted!" + exit 1 +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` +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; + +ui_print "- Device platform: $ARCH" +CHROMEDIR=$INSTALLER/chromeos +BINDIR=$INSTALLER/$ARCH + +########################################################################################## +# Detection all done, start installing +########################################################################################## + +if (is_mounted /data); then + # Copy the binaries to /data/magisk, in case they do not exist + rm -rf /data/magisk 2>/dev/null + mkdir -p /data/magisk 2>/dev/null + cp -af $BINDIR/. $CHROMEDIR /data/magisk + . $INSTALLER/magisk_uninstaller.sh +else + ui_print "! Data unavailable" + ui_print "! Placing uninstall script to /cache" + ui_print "! The device might reboot multiple times" + cp -af $INSTALLER/magisk_uninstaller.sh /cache/magisk_uninstaller.sh + umount /system + ui_print "- Rebooting....." + sleep 5 + reboot +fi + +umount /system +ui_print "- Done" +exit 0