2016-09-14 04:31:13 +02:00
|
|
|
#!/sbin/sh
|
|
|
|
##########################################################################################
|
|
|
|
#
|
2017-06-03 14:19:01 +02:00
|
|
|
# Magisk Flash Script
|
2016-09-14 04:31:13 +02:00
|
|
|
# by topjohnwu
|
|
|
|
#
|
2017-06-03 14:19:01 +02: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 04:31:13 +02:00
|
|
|
#
|
|
|
|
##########################################################################################
|
|
|
|
|
2017-03-12 11:13:58 +01:00
|
|
|
# Detect whether in boot mode
|
|
|
|
ps | grep zygote | grep -v grep >/dev/null && BOOTMODE=true || BOOTMODE=false
|
2017-06-10 19:40:08 +02:00
|
|
|
$BOOTMODE || ps -A 2>/dev/null | grep zygote | grep -v grep >/dev/null && BOOTMODE=true
|
2016-09-14 04:31:13 +02:00
|
|
|
|
2017-03-12 11:13:58 +01:00
|
|
|
# This path should work in any cases
|
|
|
|
TMPDIR=/dev/tmp
|
2016-09-14 04:31:13 +02:00
|
|
|
|
2017-03-12 11:13:58 +01:00
|
|
|
INSTALLER=$TMPDIR/magisk
|
2017-03-15 12:32:25 +01:00
|
|
|
COMMONDIR=$INSTALLER/common
|
2016-11-28 21:16:01 +01:00
|
|
|
CHROMEDIR=$INSTALLER/chromeos
|
2017-06-10 19:40:08 +02:00
|
|
|
COREDIR=/magisk/.core
|
2016-11-28 21:16:01 +01:00
|
|
|
|
2016-11-13 09:58:43 +01:00
|
|
|
# Default permissions
|
|
|
|
umask 022
|
|
|
|
|
2016-09-14 04:31:13 +02: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
|
|
|
|
|
|
|
|
mkdir -p $INSTALLER
|
|
|
|
cd $INSTALLER
|
|
|
|
unzip -o "$ZIP"
|
|
|
|
|
|
|
|
##########################################################################################
|
|
|
|
# Functions
|
|
|
|
##########################################################################################
|
|
|
|
|
|
|
|
ui_print() {
|
2017-03-12 11:13:58 +01:00
|
|
|
if $BOOTMODE; then
|
2016-09-14 04:31:13 +02:00
|
|
|
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");
|
2016-10-30 00:13:59 +02:00
|
|
|
for FILE in /dev/.magisk /data/.magisk /cache/.magisk /system/.magisk; do
|
2016-09-14 04:31:13 +02:00
|
|
|
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
|
2017-06-10 19:40:08 +02:00
|
|
|
for PARTITION in kern-a android_boot kernel boot lnx; do
|
2017-06-12 18:46:20 +02:00
|
|
|
BOOTIMAGE=`find /dev/block -iname "$PARTITION" | head -n 1`
|
2017-06-10 19:40:08 +02:00
|
|
|
[ ! -z $BOOTIMAGE ] && break
|
2016-09-14 04:31:13 +02:00
|
|
|
done
|
|
|
|
fi
|
2017-06-10 19:40:08 +02:00
|
|
|
# Recovery fallback
|
2016-10-02 22:34:50 +02:00
|
|
|
if [ -z "$BOOTIMAGE" ]; then
|
2017-06-10 19:40:08 +02:00
|
|
|
for FSTAB in /etc/*fstab*; do
|
|
|
|
BOOTIMAGE=`grep -E '\b/boot\b' $FSTAB | grep -oE '/dev/[a-zA-Z0-9_./-]*'`
|
|
|
|
[ ! -z $BOOTIMAGE ] && break
|
|
|
|
done
|
2016-10-02 22:34:50 +02:00
|
|
|
fi
|
2017-06-10 19:40:08 +02:00
|
|
|
[ -L "$BOOTIMAGE" ] && BOOTIMAGE=`readlink $BOOTIMAGE`
|
2016-09-14 04:31:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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 $?
|
|
|
|
}
|
|
|
|
|
2016-10-02 22:34:50 +02:00
|
|
|
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
|
|
|
|
if [ ! -f "$LOOPDEVICE" ]; then
|
2016-11-28 21:16:01 +01:00
|
|
|
mknod $LOOPDEVICE b 7 $LOOP 2>/dev/null
|
2016-10-02 22:34:50 +02:00
|
|
|
fi
|
|
|
|
losetup $LOOPDEVICE $1
|
|
|
|
if [ "$?" -eq "0" ]; then
|
|
|
|
mount -t ext4 -o loop $LOOPDEVICE $2
|
|
|
|
if (! is_mounted $2); then
|
|
|
|
/system/bin/toolbox mount -t ext4 -o loop $LOOPDEVICE $2
|
|
|
|
fi
|
|
|
|
if (! is_mounted $2); then
|
|
|
|
/system/bin/toybox mount -t ext4 -o loop $LOOPDEVICE $2
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
if (is_mounted $2); then
|
|
|
|
ui_print "- Mounting $1 to $2"
|
|
|
|
break;
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2016-09-14 04:31:13 +02:00
|
|
|
grep_prop() {
|
|
|
|
REGEX="s/^$1=//p"
|
|
|
|
shift
|
|
|
|
FILES=$@
|
|
|
|
if [ -z "$FILES" ]; then
|
|
|
|
FILES='/system/build.prop'
|
|
|
|
fi
|
2017-03-12 11:13:58 +01:00
|
|
|
cat $FILES 2>/dev/null | sed -n "$REGEX" | head -n 1
|
2016-09-14 04:31:13 +02:00
|
|
|
}
|
|
|
|
|
2017-01-01 20:00:03 +01:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2017-06-15 22:08:34 +02:00
|
|
|
abort() {
|
|
|
|
ui_print "$1"
|
|
|
|
mv /sbin_tmp /sbin 2>/dev/null
|
|
|
|
exit 1
|
|
|
|
}
|
|
|
|
|
2016-09-14 04:31:13 +02:00
|
|
|
##########################################################################################
|
|
|
|
# Detection
|
|
|
|
##########################################################################################
|
|
|
|
|
2017-04-22 01:05:10 +02:00
|
|
|
ui_print "************************"
|
|
|
|
ui_print "* MAGISK_VERSION_STUB"
|
|
|
|
ui_print "************************"
|
2016-09-14 04:31:13 +02:00
|
|
|
|
2017-06-15 22:08:34 +02:00
|
|
|
[ -d "$COMMONDIR" ] || abort "! Unable to extract zip file!"
|
2016-09-14 04:31:13 +02: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-15 22:08:34 +02:00
|
|
|
[ -f /system/build.prop ] || abort "! /system could not be mounted!"
|
2017-06-10 19:40:08 +02:00
|
|
|
|
2017-03-12 11:13:58 +01:00
|
|
|
# read override variables
|
|
|
|
getvar KEEPVERITY
|
|
|
|
getvar KEEPFORCEENCRYPT
|
|
|
|
getvar BOOTIMAGE
|
2016-09-14 04:31:13 +02:00
|
|
|
|
2017-01-01 20:00:03 +01:00
|
|
|
# Check if system root is installed and remove
|
|
|
|
remove_system_su
|
|
|
|
|
2017-03-12 11:13:58 +01: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-02 22:34:50 +02:00
|
|
|
|
2016-10-30 00:13:59 +02:00
|
|
|
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;
|
2016-10-02 22:34:50 +02:00
|
|
|
|
2017-06-15 22:08:34 +02:00
|
|
|
[ $API -lt 21 ] && abort "! Magisk is only for Lollipop 5.0+ (SDK 21+)"
|
2016-10-30 00:13:59 +02:00
|
|
|
|
|
|
|
ui_print "- Device platform: $ARCH"
|
|
|
|
|
|
|
|
BINDIR=$INSTALLER/$ARCH
|
2017-03-12 12:22:15 +01:00
|
|
|
chmod -R 755 $CHROMEDIR $BINDIR
|
2016-10-30 00:13:59 +02:00
|
|
|
|
|
|
|
find_boot_image
|
2017-06-15 22:08:34 +02:00
|
|
|
[ -z $BOOTIMAGE ] && abort "! Unable to detect boot image"
|
2016-10-02 22:34:50 +02:00
|
|
|
|
2016-11-13 09:58:43 +01:00
|
|
|
##########################################################################################
|
|
|
|
# Environment
|
|
|
|
##########################################################################################
|
|
|
|
|
2017-04-22 01:05:10 +02:00
|
|
|
ui_print "- Constructing environment"
|
2016-11-13 09:58:43 +01:00
|
|
|
|
2017-06-15 22:08:34 +02:00
|
|
|
if ! $BOOTMODE; then
|
|
|
|
# Completely use /system components
|
|
|
|
[ -e /vendor ] || ln -s /system/vendor /vendor
|
|
|
|
export PATH=/system/bin:/system/xbin:/vendor/bin
|
|
|
|
# Clear out possible lib paths, let the binary find them itself
|
|
|
|
export LD_LIBRARY_PATH=
|
|
|
|
# Completely block out all custom recovery binaries/libs
|
|
|
|
mv /sbin /sbin_tmp
|
|
|
|
fi
|
|
|
|
|
2017-04-22 01:05:10 +02:00
|
|
|
is_mounted /data && MAGISKBIN=/data/magisk || MAGISKBIN=/cache/data_bin
|
2016-11-13 09:58:43 +01:00
|
|
|
|
2017-04-22 01:05:10 +02:00
|
|
|
# Copy required files
|
|
|
|
rm -rf $MAGISKBIN 2>/dev/null
|
|
|
|
mkdir -p $MAGISKBIN
|
2017-06-03 14:19:01 +02:00
|
|
|
cp -af $BINDIR/. $COMMONDIR/. $MAGISKBIN
|
2017-03-18 22:00:12 +01:00
|
|
|
|
2017-04-22 01:05:10 +02:00
|
|
|
chmod -R 755 $MAGISKBIN
|
2017-06-03 14:19:01 +02:00
|
|
|
chcon -hR u:object_r:system_file:s0 $MAGISKBIN
|
2017-02-04 11:44:07 +01:00
|
|
|
|
2017-03-26 15:43:48 +02:00
|
|
|
##########################################################################################
|
|
|
|
# Magisk Image
|
|
|
|
##########################################################################################
|
|
|
|
|
2017-04-17 10:36:49 +02:00
|
|
|
# Fix SuperSU.....
|
2017-06-15 22:08:34 +02:00
|
|
|
$BOOTMODE && $BINDIR/magiskpolicy --live "allow fsck * * *"
|
2017-04-17 10:36:49 +02: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-15 22:08:34 +02:00
|
|
|
$BINDIR/magisk --createimg $IMG 64M
|
2017-04-17 10:36:49 +02:00
|
|
|
fi
|
|
|
|
|
|
|
|
mount_image $IMG /magisk
|
2017-06-15 22:08:34 +02:00
|
|
|
is_mounted /magisk || abort "! Magisk image mount failed..."
|
2017-04-17 10:36:49 +02:00
|
|
|
MAGISKLOOP=$LOOPDEVICE
|
|
|
|
|
2017-06-03 14:19:01 +02:00
|
|
|
# Core folders
|
|
|
|
mkdir -p $COREDIR/props $COREDIR/post-fs-data.d $COREDIR/service.d 2>/dev/null
|
2017-04-15 20:49:01 +02:00
|
|
|
|
2017-06-15 22:08:34 +02: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 14:19:01 +02:00
|
|
|
|
|
|
|
# Legacy cleanup
|
|
|
|
mv $COREDIR/magiskhide/hidelist $COREDIR/hidelist 2>/dev/null
|
|
|
|
rm -rf $COREDIR/magiskhide $COREDIR/bin
|
2017-03-26 15:43:48 +02:00
|
|
|
|
2017-03-12 11:13:58 +01: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-10 19:40:08 +02:00
|
|
|
SHA1=`$BINDIR/magiskboot --sha1 /data/stock_boot.img | tail -n 1`
|
2017-03-12 11:13:58 +01:00
|
|
|
STOCKDUMP=/data/stock_boot_${SHA1}.img
|
|
|
|
mv /data/stock_boot.img $STOCKDUMP
|
2017-06-15 22:08:34 +02:00
|
|
|
$BINDIR/magiskboot --compress $STOCKDUMP
|
2017-03-12 11:13:58 +01:00
|
|
|
fi
|
|
|
|
|
2017-06-03 14:19:01 +02:00
|
|
|
SOURCEDMODE=true
|
|
|
|
cd $MAGISKBIN
|
2017-02-04 11:44:07 +01:00
|
|
|
|
2017-06-03 14:19:01 +02:00
|
|
|
# Source the boot patcher
|
2017-06-10 19:40:08 +02:00
|
|
|
. $COMMONDIR/boot_patch.sh "$BOOTIMAGE"
|
2017-03-12 11:13:58 +01:00
|
|
|
|
2017-06-03 14:19:01 +02:00
|
|
|
# Sign chromeos boot
|
|
|
|
if [ -f chromeos ]; then
|
|
|
|
echo > empty
|
2017-03-31 00:25:22 +02:00
|
|
|
|
2017-06-15 22:08:34 +02:00
|
|
|
$CHROMEDIR/futility vbutil_kernel --pack new-boot.img.signed \
|
2017-06-03 14:19:01 +02: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 00:25:22 +02:00
|
|
|
|
2017-06-03 14:19:01 +02:00
|
|
|
rm -f empty new-boot.img
|
|
|
|
mv new-boot.img.signed new-boot.img
|
2017-03-12 11:13:58 +01:00
|
|
|
fi
|
|
|
|
|
2017-06-03 14:19:01 +02:00
|
|
|
if is_mounted /data; then
|
2017-06-10 19:40:08 +02:00
|
|
|
rm -f /data/stock_boot* 2>/dev/null
|
|
|
|
mv stock_boot* /data 2>/dev/null
|
2016-09-14 04:31:13 +02:00
|
|
|
fi
|
|
|
|
|
|
|
|
ui_print "- Flashing new boot image"
|
2017-06-10 19:40:08 +02:00
|
|
|
if [ -L "$BOOTIMAGE" ]; then
|
|
|
|
dd if=new-boot.img of="$BOOTIMAGE" bs=4096
|
2017-06-03 14:19:01 +02:00
|
|
|
else
|
2017-06-10 19:40:08 +02:00
|
|
|
cat new-boot.img /dev/zero | dd of="$BOOTIMAGE" bs=4096
|
2017-06-03 14:19:01 +02:00
|
|
|
fi
|
|
|
|
rm -f new-boot.img
|
2016-09-14 04:31:13 +02:00
|
|
|
|
2017-01-01 19:00:53 +01:00
|
|
|
cd /
|
|
|
|
|
2017-03-12 11:13:58 +01:00
|
|
|
if ! $BOOTMODE; then
|
2017-06-15 22:08:34 +02:00
|
|
|
mv /sbin_tmp /sbin
|
2016-10-02 22:34:50 +02:00
|
|
|
ui_print "- Unmounting partitions"
|
2016-09-14 04:31:13 +02:00
|
|
|
umount /magisk
|
2017-04-28 15:48:38 +02:00
|
|
|
losetup -d $MAGISKLOOP 2>/dev/null
|
2016-11-13 09:58:43 +01:00
|
|
|
rmdir /magisk
|
2017-06-10 19:40:08 +02:00
|
|
|
umount -l /system
|
2016-09-14 04:31:13 +02:00
|
|
|
fi
|
|
|
|
|
|
|
|
ui_print "- Done"
|
|
|
|
exit 0
|