2016-09-14 10:31:13 +08:00
|
|
|
#!/sbin/sh
|
|
|
|
##########################################################################################
|
|
|
|
#
|
2017-06-03 20:19:01 +08:00
|
|
|
# Magisk Flash Script
|
2016-09-14 10:31:13 +08:00
|
|
|
# by topjohnwu
|
|
|
|
#
|
2017-06-03 20:19:01 +08:00
|
|
|
# This script will detect, construct the environment for Magisk
|
|
|
|
# It will then call boot_patch.sh to patch the boot image
|
2016-09-14 10:31:13 +08:00
|
|
|
#
|
|
|
|
##########################################################################################
|
|
|
|
|
2017-03-12 18:13:58 +08:00
|
|
|
# Detect whether in boot mode
|
|
|
|
ps | grep zygote | grep -v grep >/dev/null && BOOTMODE=true || BOOTMODE=false
|
2017-06-11 01:40:08 +08:00
|
|
|
$BOOTMODE || ps -A 2>/dev/null | grep zygote | grep -v grep >/dev/null && BOOTMODE=true
|
2016-09-14 10:31:13 +08:00
|
|
|
|
2017-03-12 18:13:58 +08:00
|
|
|
# This path should work in any cases
|
|
|
|
TMPDIR=/dev/tmp
|
2016-09-14 10:31:13 +08:00
|
|
|
|
2017-03-12 18:13:58 +08:00
|
|
|
INSTALLER=$TMPDIR/magisk
|
2017-06-16 15:27:28 +08:00
|
|
|
BUSYBOX=$TMPDIR/busybox
|
2017-03-15 19:32:25 +08:00
|
|
|
COMMONDIR=$INSTALLER/common
|
2016-11-29 04:16:01 +08:00
|
|
|
CHROMEDIR=$INSTALLER/chromeos
|
2017-06-11 01:40:08 +08:00
|
|
|
COREDIR=/magisk/.core
|
2016-11-29 04:16:01 +08:00
|
|
|
|
2016-11-13 16:58:43 +08:00
|
|
|
# Default permissions
|
|
|
|
umask 022
|
|
|
|
|
2016-09-14 10:31:13 +08:00
|
|
|
##########################################################################################
|
|
|
|
# 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
|
|
|
|
|
2017-06-16 15:27:28 +08:00
|
|
|
rm -rf $TMPDIR 2>/dev/null
|
|
|
|
mkdir -p $INSTALLER $BUSYBOX
|
|
|
|
unzip -o "$ZIP" -d $INSTALLER
|
2016-09-14 10:31:13 +08:00
|
|
|
|
|
|
|
##########################################################################################
|
|
|
|
# Detection
|
|
|
|
##########################################################################################
|
|
|
|
|
2017-06-19 00:15:44 +08:00
|
|
|
# Load all fuctions
|
|
|
|
. $COMMONDIR/util_functions.sh
|
|
|
|
|
2017-04-22 07:05:10 +08:00
|
|
|
ui_print "************************"
|
|
|
|
ui_print "* MAGISK_VERSION_STUB"
|
|
|
|
ui_print "************************"
|
2016-09-14 10:31:13 +08:00
|
|
|
|
2017-06-16 04:08:34 +08:00
|
|
|
[ -d "$COMMONDIR" ] || abort "! Unable to extract zip file!"
|
2016-09-14 10:31:13 +08:00
|
|
|
|
|
|
|
ui_print "- Mounting /system(ro), /cache, /data"
|
|
|
|
mount -o ro /system 2>/dev/null
|
|
|
|
mount /cache 2>/dev/null
|
|
|
|
mount /data 2>/dev/null
|
|
|
|
|
2017-06-16 04:08:34 +08:00
|
|
|
[ -f /system/build.prop ] || abort "! /system could not be mounted!"
|
2017-06-11 01:40:08 +08:00
|
|
|
|
2017-03-12 18:13:58 +08:00
|
|
|
# read override variables
|
|
|
|
getvar KEEPVERITY
|
|
|
|
getvar KEEPFORCEENCRYPT
|
|
|
|
getvar BOOTIMAGE
|
2016-09-14 10:31:13 +08:00
|
|
|
|
2017-01-02 03:00:03 +08:00
|
|
|
# Check if system root is installed and remove
|
|
|
|
remove_system_su
|
|
|
|
|
2017-03-12 18:13:58 +08:00
|
|
|
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`
|
2016-10-03 04:34:50 +08:00
|
|
|
|
2016-10-30 06:13:59 +08:00
|
|
|
ARCH=arm
|
2017-06-16 15:27:28 +08:00
|
|
|
BBPATH=armeabi-v7a
|
|
|
|
if [ "$ABI" = "x86" ]; then ARCH=x86; BBPATH=x86; fi;
|
|
|
|
if [ "$ABI2" = "x86" ]; then ARCH=x86; BBPATH=x86; fi;
|
|
|
|
if [ "$ABILONG" = "arm64-v8a" ]; then ARCH=arm64; fi;
|
|
|
|
if [ "$ABILONG" = "x86_64" ]; then ARCH=x64; BBPATH=x86; fi;
|
2016-10-03 04:34:50 +08:00
|
|
|
|
2017-06-16 04:08:34 +08:00
|
|
|
[ $API -lt 21 ] && abort "! Magisk is only for Lollipop 5.0+ (SDK 21+)"
|
2016-10-30 06:13:59 +08:00
|
|
|
|
|
|
|
ui_print "- Device platform: $ARCH"
|
|
|
|
|
|
|
|
BINDIR=$INSTALLER/$ARCH
|
2017-03-12 19:22:15 +08:00
|
|
|
chmod -R 755 $CHROMEDIR $BINDIR
|
2016-10-30 06:13:59 +08:00
|
|
|
|
|
|
|
find_boot_image
|
2017-06-16 04:08:34 +08:00
|
|
|
[ -z $BOOTIMAGE ] && abort "! Unable to detect boot image"
|
2016-10-03 04:34:50 +08:00
|
|
|
|
2016-11-13 16:58:43 +08:00
|
|
|
##########################################################################################
|
|
|
|
# Environment
|
|
|
|
##########################################################################################
|
|
|
|
|
2017-04-22 07:05:10 +08:00
|
|
|
ui_print "- Constructing environment"
|
2016-11-13 16:58:43 +08:00
|
|
|
|
2017-06-16 04:08:34 +08:00
|
|
|
if ! $BOOTMODE; then
|
2017-06-16 15:27:28 +08:00
|
|
|
# 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
|
2017-06-16 04:08:34 +08:00
|
|
|
# Clear out possible lib paths, let the binary find them itself
|
|
|
|
export LD_LIBRARY_PATH=
|
2017-06-16 15:27:28 +08:00
|
|
|
# Temporarily block out all custom recovery binaries/libs
|
2017-06-16 04:08:34 +08:00
|
|
|
mv /sbin /sbin_tmp
|
|
|
|
fi
|
|
|
|
|
2017-04-22 07:05:10 +08:00
|
|
|
is_mounted /data && MAGISKBIN=/data/magisk || MAGISKBIN=/cache/data_bin
|
2016-11-13 16:58:43 +08:00
|
|
|
|
2017-04-22 07:05:10 +08:00
|
|
|
# Copy required files
|
|
|
|
rm -rf $MAGISKBIN 2>/dev/null
|
|
|
|
mkdir -p $MAGISKBIN
|
2017-06-03 20:19:01 +08:00
|
|
|
cp -af $BINDIR/. $COMMONDIR/. $MAGISKBIN
|
2017-03-19 05:00:12 +08:00
|
|
|
|
2017-04-22 07:05:10 +08:00
|
|
|
chmod -R 755 $MAGISKBIN
|
2017-06-03 20:19:01 +08:00
|
|
|
chcon -hR u:object_r:system_file:s0 $MAGISKBIN
|
2017-02-04 18:44:07 +08:00
|
|
|
|
2017-06-19 00:15:44 +08:00
|
|
|
# addon.d
|
|
|
|
if [ -d /system/addon.d ]; then
|
|
|
|
ui_print "- Adding addon.d survival script"
|
|
|
|
mount -o rw,remount /system
|
|
|
|
cp $INSTALLER/addon.d/99-magisk.sh /system/addon.d/99-magisk.sh
|
|
|
|
chmod 755 /system/addon.d/99-magisk.sh
|
|
|
|
fi
|
|
|
|
|
2017-03-26 21:43:48 +08:00
|
|
|
##########################################################################################
|
|
|
|
# Magisk Image
|
|
|
|
##########################################################################################
|
|
|
|
|
2017-04-17 16:36:49 +08:00
|
|
|
# Fix SuperSU.....
|
2017-06-16 04:08:34 +08:00
|
|
|
$BOOTMODE && $BINDIR/magiskpolicy --live "allow fsck * * *"
|
2017-04-17 16:36:49 +08:00
|
|
|
|
|
|
|
if (is_mounted /data); then
|
|
|
|
IMG=/data/magisk.img
|
|
|
|
else
|
|
|
|
IMG=/cache/magisk.img
|
|
|
|
ui_print "- Data unavailable, use cache workaround"
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ -f $IMG ]; then
|
|
|
|
ui_print "- $IMG detected!"
|
|
|
|
else
|
|
|
|
ui_print "- Creating $IMG"
|
2017-06-16 04:08:34 +08:00
|
|
|
$BINDIR/magisk --createimg $IMG 64M
|
2017-04-17 16:36:49 +08:00
|
|
|
fi
|
|
|
|
|
|
|
|
mount_image $IMG /magisk
|
2017-06-16 04:08:34 +08:00
|
|
|
is_mounted /magisk || abort "! Magisk image mount failed..."
|
2017-04-17 16:36:49 +08:00
|
|
|
MAGISKLOOP=$LOOPDEVICE
|
|
|
|
|
2017-06-03 20:19:01 +08:00
|
|
|
# Core folders
|
|
|
|
mkdir -p $COREDIR/props $COREDIR/post-fs-data.d $COREDIR/service.d 2>/dev/null
|
2017-04-16 02:49:01 +08:00
|
|
|
|
2017-06-16 04:08:34 +08:00
|
|
|
chmod 755 $COREDIR/post-fs-data.d $COREDIR/service.d
|
|
|
|
chown 0.0 $COREDIR/post-fs-data.d $COREDIR/service.d
|
2017-06-03 20:19:01 +08:00
|
|
|
|
|
|
|
# Legacy cleanup
|
|
|
|
mv $COREDIR/magiskhide/hidelist $COREDIR/hidelist 2>/dev/null
|
|
|
|
rm -rf $COREDIR/magiskhide $COREDIR/bin
|
2017-03-26 21:43:48 +08:00
|
|
|
|
2017-03-12 18:13:58 +08:00
|
|
|
##########################################################################################
|
|
|
|
# Unpack boot
|
|
|
|
##########################################################################################
|
|
|
|
|
|
|
|
ui_print "- Found Boot Image: $BOOTIMAGE"
|
|
|
|
|
|
|
|
# Update our previous backup to new format if exists
|
|
|
|
if [ -f /data/stock_boot.img ]; then
|
2017-06-11 01:40:08 +08:00
|
|
|
SHA1=`$BINDIR/magiskboot --sha1 /data/stock_boot.img | tail -n 1`
|
2017-03-12 18:13:58 +08:00
|
|
|
STOCKDUMP=/data/stock_boot_${SHA1}.img
|
|
|
|
mv /data/stock_boot.img $STOCKDUMP
|
2017-06-16 04:08:34 +08:00
|
|
|
$BINDIR/magiskboot --compress $STOCKDUMP
|
2017-03-12 18:13:58 +08:00
|
|
|
fi
|
|
|
|
|
2017-06-03 20:19:01 +08:00
|
|
|
SOURCEDMODE=true
|
|
|
|
cd $MAGISKBIN
|
2017-02-04 18:44:07 +08:00
|
|
|
|
2017-06-03 20:19:01 +08:00
|
|
|
# Source the boot patcher
|
2017-06-11 01:40:08 +08:00
|
|
|
. $COMMONDIR/boot_patch.sh "$BOOTIMAGE"
|
2017-03-12 18:13:58 +08:00
|
|
|
|
2017-06-03 20:19:01 +08:00
|
|
|
# Sign chromeos boot
|
|
|
|
if [ -f chromeos ]; then
|
|
|
|
echo > empty
|
2017-03-31 06:25:22 +08:00
|
|
|
|
2017-06-16 04:08:34 +08:00
|
|
|
$CHROMEDIR/futility vbutil_kernel --pack new-boot.img.signed \
|
2017-06-03 20:19:01 +08:00
|
|
|
--keyblock $CHROMEDIR/kernel.keyblock --signprivate $CHROMEDIR/kernel_data_key.vbprivk \
|
|
|
|
--version 1 --vmlinuz new-boot.img --config empty --arch arm --bootloader empty --flags 0x1
|
2017-03-31 06:25:22 +08:00
|
|
|
|
2017-06-03 20:19:01 +08:00
|
|
|
rm -f empty new-boot.img
|
|
|
|
mv new-boot.img.signed new-boot.img
|
2017-03-12 18:13:58 +08:00
|
|
|
fi
|
|
|
|
|
2017-06-19 00:15:44 +08:00
|
|
|
[ -f stock_boot* ] && rm -f /data/stock_boot* 2>/dev/null
|
2016-09-14 10:31:13 +08:00
|
|
|
|
|
|
|
ui_print "- Flashing new boot image"
|
2017-06-11 01:40:08 +08:00
|
|
|
if [ -L "$BOOTIMAGE" ]; then
|
|
|
|
dd if=new-boot.img of="$BOOTIMAGE" bs=4096
|
2017-06-03 20:19:01 +08:00
|
|
|
else
|
2017-06-11 01:40:08 +08:00
|
|
|
cat new-boot.img /dev/zero | dd of="$BOOTIMAGE" bs=4096
|
2017-06-03 20:19:01 +08:00
|
|
|
fi
|
|
|
|
rm -f new-boot.img
|
2016-09-14 10:31:13 +08:00
|
|
|
|
2017-01-02 02:00:53 +08:00
|
|
|
cd /
|
|
|
|
|
2017-03-12 18:13:58 +08:00
|
|
|
if ! $BOOTMODE; then
|
2017-06-16 04:08:34 +08:00
|
|
|
mv /sbin_tmp /sbin
|
2016-10-03 04:34:50 +08:00
|
|
|
ui_print "- Unmounting partitions"
|
2016-09-14 10:31:13 +08:00
|
|
|
umount /magisk
|
2017-04-28 21:48:38 +08:00
|
|
|
losetup -d $MAGISKLOOP 2>/dev/null
|
2016-11-13 16:58:43 +08:00
|
|
|
rmdir /magisk
|
2017-06-11 01:40:08 +08:00
|
|
|
umount -l /system
|
2016-09-14 10:31:13 +08:00
|
|
|
fi
|
|
|
|
|
|
|
|
ui_print "- Done"
|
|
|
|
exit 0
|