From b7986a351c584f869320eb18c547ff2e5277f9a2 Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Tue, 25 Jul 2017 03:10:01 +0800 Subject: [PATCH] Overcome some quirks in Android Lollipop --- jni/magiskboot/cpio.c | 23 +++++++++++++++++++++++ jni/magiskboot/magiskboot.h | 3 ++- jni/magiskboot/main.c | 19 ++++++++++--------- scripts/boot_patch.sh | 9 ++------- scripts/flash_script.sh | 1 + scripts/magisk_uninstaller.sh | 6 +----- scripts/util_functions.sh | 22 +++++++++++++++++----- 7 files changed, 56 insertions(+), 27 deletions(-) diff --git a/jni/magiskboot/cpio.c b/jni/magiskboot/cpio.c index cbc743f1f..7975a8c30 100644 --- a/jni/magiskboot/cpio.c +++ b/jni/magiskboot/cpio.c @@ -470,6 +470,24 @@ static int cpio_restore(struct vector *v) { return ret; } +static void cpio_stocksha1(struct vector *v) { + cpio_file *f; + char sha1[41]; + vec_for_each(v, f) { + if (strcmp(f->filename, "init.magisk.rc") == 0) { + for (char *pos = f->data; pos < f->data + f->filesize; pos = strchr(pos + 1, '\n') + 1) { + if (memcmp(pos, "# STOCKSHA1=", 12) == 0) { + pos += 12; + memcpy(sha1, pos, 40); + sha1[40] = '\0'; + printf("%s\n", sha1); + return; + } + } + } + } +} + int cpio_commands(const char *command, int argc, char *argv[]) { int recursive = 0, ret = 0; command_t cmd; @@ -480,6 +498,8 @@ int cpio_commands(const char *command, int argc, char *argv[]) { cmd = TEST; } else if (strcmp(command, "restore") == 0) { cmd = RESTORE; + } else if (strcmp(command, "stocksha1") == 0) { + cmd = STOCKSHA1; } else if (argc == 1 && strcmp(command, "backup") == 0) { cmd = BACKUP; } else if (argc > 0 && strcmp(command, "rm") == 0) { @@ -510,6 +530,9 @@ int cpio_commands(const char *command, int argc, char *argv[]) { case RESTORE: ret = cpio_restore(&v); break; + case STOCKSHA1: + cpio_stocksha1(&v); + return 0; case BACKUP: cpio_backup(argv[0], &v); case RM: diff --git a/jni/magiskboot/magiskboot.h b/jni/magiskboot/magiskboot.h index 172e1b7a6..caa4a7c3f 100644 --- a/jni/magiskboot/magiskboot.h +++ b/jni/magiskboot/magiskboot.h @@ -55,7 +55,8 @@ typedef enum { TEST, PATCH, BACKUP, - RESTORE + RESTORE, + STOCKSHA1 } command_t; extern char *SUP_LIST[]; diff --git a/jni/magiskboot/main.c b/jni/magiskboot/main.c index 9a00ba3d4..3157d8cad 100644 --- a/jni/magiskboot/main.c +++ b/jni/magiskboot/main.c @@ -20,15 +20,16 @@ static void usage(char *arg0) { " Search in , and replace with \n" "\n" "%s --cpio- [flags...] [params...]\n" - " Do cpio related cmds to (modifications are done directly)\n Supported commands:\n" - " --cpio-rm [-r] \n Remove entry from cpio, flag -r to remove recursively\n" - " --cpio-mkdir \n Create directory as an \n" - " --cpio-add \n Add as an ; replaces if already exists\n" - " --cpio-extract \n Extract to \n" - " --cpio-test \n Return value: 0/not patched 1/Magisk 2/Other (e.g. phh, SuperSU)\n" - " --cpio-patch \n Patch cpio for Magisk. KEEP**** are true/false values\n" - " --cpio-backup \n Create ramdisk backups into from \n" - " --cpio-restore \n Restore ramdisk from ramdisk backup within \n" + " Do cpio related cmds to (modifications are done directly)\n Supported commands and params:\n" + " -rm [-r] \n Remove entry from , flag -r to remove recursively\n" + " -mkdir \n Create directory as an \n" + " -add \n Add as an ; replaces if already exists\n" + " -extract \n Extract to \n" + " -test \n Return value: 0/not patched 1/Magisk 2/Other (e.g. phh, SuperSU)\n" + " -patch \n Patch cpio for Magisk. KEEP**** are true/false values\n" + " -backup \n Create ramdisk backups into from \n" + " -restore\n Restore ramdisk from ramdisk backup within \n" + " -stocksha1\n Get stock boot SHA1 recorded within \n" "\n" "%s --compress[=method] [outfile]\n" " Compress with [method] (default: gzip), optionally to [outfile]\n Supported methods: " diff --git a/scripts/boot_patch.sh b/scripts/boot_patch.sh index 6fcb0a18a..ceb2266f3 100644 --- a/scripts/boot_patch.sh +++ b/scripts/boot_patch.sh @@ -106,7 +106,7 @@ fi [ -z $KEEPFORCEENCRYPT ] && KEEPFORCEENCRYPT=false # Detect whether running as root -[ `id -u` -eq 0 ] && ROOT=true || ROOT=false +id | grep "uid=0" >/dev/null 2>&1 && ROOT=true || ROOT=false # Switch to the location of the script file [ -z $SOURCEDMODE ] && cd "`dirname_wrap "${BASH_SOURCE:-$0}"`" @@ -156,12 +156,7 @@ case $? in 1 ) # Magisk patched ui_print_wrap "- Magisk patched image detected!" # Find SHA1 of stock boot image - if [ -z $SHA1 ]; then - ./magiskboot --cpio-extract ramdisk.cpio init.magisk.rc init.magisk.rc.old - SHA1=`grep_prop "# STOCKSHA1" init.magisk.rc.old` - rm -f init.magisk.rc.old - fi - + [ -z $SHA1 ] && SHA1=`./magiskboot --cpio-stocksha1 ramdisk.cpio` OK=false ./magiskboot --cpio-restore ramdisk.cpio if [ $? -eq 0 ]; then diff --git a/scripts/flash_script.sh b/scripts/flash_script.sh index a5153cf8d..bedba300f 100644 --- a/scripts/flash_script.sh +++ b/scripts/flash_script.sh @@ -183,6 +183,7 @@ if ! $BOOTMODE; then $MAGISKBIN/magisk --umountimg /magisk $MAGISKLOOP rmdir /magisk recovery_cleanup + rm -rf $TMPDIR fi ui_print "- Done" diff --git a/scripts/magisk_uninstaller.sh b/scripts/magisk_uninstaller.sh index 1eb3c700c..f63a33aef 100644 --- a/scripts/magisk_uninstaller.sh +++ b/scripts/magisk_uninstaller.sh @@ -101,11 +101,7 @@ case $? in 1 ) # Magisk patched ui_print_wrap "- Magisk patched image detected!" # Find SHA1 of stock boot image - if [ -z $SHA1 ]; then - ./magiskboot --cpio-extract ramdisk.cpio init.magisk.rc init.magisk.rc.old - SHA1=`grep_prop "# STOCKSHA1" init.magisk.rc.old` - rm -f init.magisk.rc.old - fi + [ -z $SHA1 ] && SHA1=`./magiskboot --cpio-stocksha1 ramdisk.cpio` [ ! -z $SHA1 ] && STOCKDUMP=/data/stock_boot_${SHA1}.img if [ -f ${STOCKDUMP}.gz ]; then ui_print_wrap "- Boot image backup found!" diff --git a/scripts/util_functions.sh b/scripts/util_functions.sh index 120843e67..54e777869 100644 --- a/scripts/util_functions.sh +++ b/scripts/util_functions.sh @@ -1,10 +1,10 @@ ########################################################################################## -# +# # Magisk General Utility Functions # by topjohnwu -# +# # Used in flash_script.sh, addon.d.sh, magisk module installers, and uninstaller -# +# ########################################################################################## MAGISK_VERSION_STUB @@ -30,7 +30,7 @@ get_outfd() { ui_print() { if $BOOTMODE; then echo "$1" - else + else echo -n -e "ui_print $1\n" >> /proc/self/fd/$OUTFD echo -n -e "ui_print\n" >> /proc/self/fd/$OUTFD fi @@ -128,10 +128,21 @@ api_level_arch_detect() { recovery_actions() { # TWRP bug fix mount -o bind /dev/urandom /dev/random + # Preserve environment varibles + OLD_PATH=$PATH + OLD_LD_PATH=$LD_LIBRARY_PATH + # Extract busybox from Magisk Manager + if [ -f $INSTALLER/common/magisk.apk ]; then + mkdir -p $TMPDIR/bin + [ $ARCH = "arm" -o $ARCH = "arm64" ] && BBPATH=lib/armeabi-v7a || BBPATH=lib/x86 + unzip -p $INSTALLER/common/magisk.apk $BBPATH/libbusybox.so > $TMPDIR/bin/busybox 2>/dev/null + chmod 755 $TMPDIR/bin/busybox + $TMPDIR/bin/busybox --install -s $TMPDIR/bin + export PATH=$PATH:$TMPDIR/bin + fi # 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 } @@ -139,6 +150,7 @@ recovery_cleanup() { mv /sbin_tmp /sbin # Clear LD_LIBRARY_PATH export LD_LIBRARY_PATH=$OLD_LD_PATH + export PATH=$OLD_PATH ui_print "- Unmounting partitions" umount -l /system umount -l /vendor 2>/dev/null