diff --git a/scripts/addon.d.sh b/scripts/addon.d.sh index 0b21aaabe..6443232fa 100644 --- a/scripts/addon.d.sh +++ b/scripts/addon.d.sh @@ -20,6 +20,7 @@ main() { sleep 5 mount -o ro /system 2>/dev/null + mount -o ro /vendor 2>/dev/null mount /data 2>/dev/null if [ ! -d $MAGISKBIN ]; then @@ -36,14 +37,7 @@ main() { ui_print "* MAGISK_VERSION_STUB" ui_print "************************" - # Check if system root is installed and remove - remove_system_su - # Prefer bin in /system - export PATH=/system/bin:/system/xbin - # Clear out possible lib paths, let the binary find them itself - export LD_LIBRARY_PATH= - # Temporarily block out all custom recovery binaries/libs - mv /sbin /sbin_tmp + recovery_actions find_boot_image [ -z $BOOTIMAGE ] && abort "! Unable to detect boot image" @@ -53,7 +47,7 @@ main() { cd $MAGISKBIN # Source the boot patcher - . $MAGISKBIN/boot_patch.sh $BOOTIMAGE + . $MAGISKBIN/boot_patch.sh "$BOOTIMAGE" [ -f stock_boot* ] && rm -f /data/stock_boot* 2>/dev/null diff --git a/scripts/boot_patch.sh b/scripts/boot_patch.sh index c1a3cfa87..b182e9580 100644 --- a/scripts/boot_patch.sh +++ b/scripts/boot_patch.sh @@ -23,24 +23,9 @@ # image placed under /data we've created when previously installing # ########################################################################################## - -# Workaround for getting the full path of BOOTIMAGE -CWD=`pwd` -cd `dirname $1` -BOOTIMAGE="`pwd`/`basename $1`" -cd "$CWD" - -if [ -z "$BOOTIMAGE" ]; then - ui_print_wrap "This script requires a boot image as a parameter" - exit 1 -fi - -# Presets -[ -z $KEEPVERITY ] && KEEPVERITY=false -[ -z $KEEPFORCEENCRYPT ] && KEEPFORCEENCRYPT=false - -# Detect whether running as root -[ `id -u` -eq 0 ] && ROOT=true || ROOT=false +########################################################################################## +# Functions +########################################################################################## # Call ui_print_wrap if exists, or else simply use echo # Useful when wrapped in flashable zip @@ -60,6 +45,21 @@ abort_wrap() { fi } +# Pure bash dirname implementation +dirname_wrap() { + if echo $1 | grep "/" >/dev/null 2>&1; then + RES=${1%/*} + [ -z $RES ] && echo "/" || echo $RES + else + echo "." + fi +} + +# Pure bash basename implementation +basename_wrap() { + echo ${1##*/} +} + grep_prop() { REGEX="s/^$1=//p" shift @@ -86,13 +86,34 @@ cpio_mkdir() { } ########################################################################################## -# Prework +# Initialization ########################################################################################## +CWD=`pwd` +cd "`dirname_wrap $1`" +BOOTIMAGE="`pwd`/`basename_wrap $1`" +cd "$CWD" + +if [ -z "$BOOTIMAGE" ]; then + ui_print_wrap "This script requires a boot image as a parameter" + exit 1 +fi + +# Presets +[ -z $KEEPVERITY ] && KEEPVERITY=false +[ -z $KEEPFORCEENCRYPT ] && KEEPFORCEENCRYPT=false + +# Detect whether running as root +[ `id -u` -eq 0 ] && ROOT=true || ROOT=false + # Switch to the location of the script file -[ -z $SOURCEDMODE ] && cd "`dirname "${BASH_SOURCE:-$0}"`" +[ -z $SOURCEDMODE ] && cd "`dirname_wrap "${BASH_SOURCE:-$0}"`" chmod +x ./* +########################################################################################## +# Unpack +########################################################################################## + ui_print_wrap "- Unpacking boot image" ./magiskboot --unpack "$BOOTIMAGE" diff --git a/scripts/flash_script.sh b/scripts/flash_script.sh index 3c5bf72f3..02069e0a6 100644 --- a/scripts/flash_script.sh +++ b/scripts/flash_script.sh @@ -17,7 +17,6 @@ $BOOTMODE || ps -A 2>/dev/null | grep zygote | grep -v grep >/dev/null && BOOTMO TMPDIR=/dev/tmp INSTALLER=$TMPDIR/magisk -BUSYBOX=$TMPDIR/busybox COMMONDIR=$INSTALLER/common CHROMEDIR=$INSTALLER/chromeos COREDIR=/magisk/.core @@ -49,13 +48,18 @@ if [ "$?" -eq "0" ]; then fi rm -rf $TMPDIR 2>/dev/null -mkdir -p $INSTALLER $BUSYBOX +mkdir -p $INSTALLER unzip -o "$ZIP" -d $INSTALLER ########################################################################################## # Detection ########################################################################################## +if [ ! -d "$COMMONDIR" ]; then + echo "! Unable to extract zip file!" + exit 1 +fi + # Load all fuctions . $COMMONDIR/util_functions.sh @@ -63,10 +67,9 @@ ui_print "************************" ui_print "* MAGISK_VERSION_STUB" ui_print "************************" -[ -d "$COMMONDIR" ] || abort "! Unable to extract zip file!" - -ui_print "- Mounting /system(ro), /cache, /data" +ui_print "- Mounting /system(ro), /vendor(ro), /cache, /data" mount -o ro /system 2>/dev/null +mount -o ro /vendor 2>/dev/null mount /cache 2>/dev/null mount /data 2>/dev/null @@ -87,10 +90,10 @@ ABILONG=`grep_prop ro.product.cpu.abi` ARCH=arm BBPATH=armeabi-v7a -if [ "$ABI" = "x86" ]; then ARCH=x86; BBPATH=x86; fi; -if [ "$ABI2" = "x86" ]; then ARCH=x86; BBPATH=x86; fi; +if [ "$ABI" = "x86" ]; then ARCH=x86; fi; +if [ "$ABI2" = "x86" ]; then ARCH=x86; fi; if [ "$ABILONG" = "arm64-v8a" ]; then ARCH=arm64; fi; -if [ "$ABILONG" = "x86_64" ]; then ARCH=x64; BBPATH=x86; fi; +if [ "$ABILONG" = "x86_64" ]; then ARCH=x64; fi; [ $API -lt 21 ] && abort "! Magisk is only for Lollipop 5.0+ (SDK 21+)" @@ -108,22 +111,8 @@ find_boot_image ui_print "- Constructing environment" -if ! $BOOTMODE; then - # We are in custom recovery, don't trust anything in recovery, use those in /system - # Extract busybox from Magisk Manager, it's pure static binary so there will be no issues - unzip -o $COMMONDIR/magisk.apk lib/$BBPATH/libbusybox.so -d $BUSYBOX - mv $BUSYBOX/lib/*/libbusybox.so $BUSYBOX/busybox - rm -rf $BUSYBOX/lib - chmod +x $BUSYBOX/busybox - $BUSYBOX/busybox --install -s $BUSYBOX - # Prefer bin in /system, fallback to bundled busybox if a tool doesn't exist - export PATH=/system/bin:/system/xbin:$BUSYBOX - # Clear out possible lib paths, let the binary find them itself - export LD_LIBRARY_PATH= - # Temporarily block out all custom recovery binaries/libs - mv /sbin /sbin_tmp -fi - +$BOOTMODE || recovery_actions + is_mounted /data && MAGISKBIN=/data/magisk || MAGISKBIN=/cache/data_bin # Copy required files @@ -147,7 +136,7 @@ fi ########################################################################################## # Fix SuperSU..... -$BOOTMODE && $BINDIR/magiskpolicy --live "allow fsck * * *" +$BOOTMODE && $BINDIR/magisk magiskpolicy --live "allow fsck * * *" if (is_mounted /data); then IMG=/data/magisk.img @@ -163,9 +152,11 @@ else $BINDIR/magisk --createimg $IMG 64M fi -mount_image $IMG /magisk +if ! is_mounted /magisk; then + ui_print "- Mounting $IMG to /magisk" + MAGISKLOOP=`$BINDIR/magisk --mountimg $IMG /magisk` +fi is_mounted /magisk || abort "! Magisk image mount failed..." -MAGISKLOOP=$LOOPDEVICE # Core folders mkdir -p $COREDIR/props $COREDIR/post-fs-data.d $COREDIR/service.d 2>/dev/null @@ -222,12 +213,12 @@ rm -f new-boot.img cd / if ! $BOOTMODE; then - mv /sbin_tmp /sbin ui_print "- Unmounting partitions" - umount /magisk - losetup -d $MAGISKLOOP 2>/dev/null + $BINDIR/magisk --umountimg /magisk $MAGISKLOOP rmdir /magisk + mv /sbin_tmp /sbin umount -l /system + umount -l /vendor 2>/dev/null fi ui_print "- Done" diff --git a/scripts/util_functions.sh b/scripts/util_functions.sh index 95f1b5f04..1da1c5c8a 100644 --- a/scripts/util_functions.sh +++ b/scripts/util_functions.sh @@ -56,29 +56,6 @@ is_mounted() { return $? } -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 - [ -e $LOOPDEVICE ] || mknod $LOOPDEVICE b 7 $LOOP 2>/dev/null - losetup $LOOPDEVICE "$1" && mount -t ext4 -o loop $LOOPDEVICE "$2" - if is_mounted "$2"; then - ui_print "- Mounting $1 to $2" - break; - fi - fi - done - fi -} - grep_prop() { REGEX="s/^$1=//p" shift @@ -114,6 +91,15 @@ remove_system_su() { fi } +recovery_actions() { + # TWRP bug fix + mount -o bind /dev/urandom /dev/random + # Clear out possible lib paths, let the binaries find them itself + export LD_LIBRARY_PATH= + # Temporarily block out all custom recovery binaries/libs + mv /sbin /sbin_tmp +} + abort() { ui_print "$1" mv /sbin_tmp /sbin 2>/dev/null