Magisk/scripts/flash_script.sh

190 lines
5.0 KiB
Bash
Raw Normal View History

2016-09-14 02:31:13 +00:00
#!/sbin/sh
##########################################################################################
#
2017-06-03 12:19:01 +00:00
# Magisk Flash Script
2016-09-14 02:31:13 +00:00
# by topjohnwu
#
2017-06-03 12:19:01 +00: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 02:31:13 +00:00
#
##########################################################################################
2017-07-09 16:17:34 +00:00
##########################################################################################
# Preparation
##########################################################################################
2017-03-12 10:13:58 +00:00
# Detect whether in boot mode
ps | grep zygote | grep -v grep >/dev/null && BOOTMODE=true || BOOTMODE=false
2017-06-10 17:40:08 +00:00
$BOOTMODE || ps -A 2>/dev/null | grep zygote | grep -v grep >/dev/null && BOOTMODE=true
2016-09-14 02:31:13 +00:00
2017-03-12 10:13:58 +00:00
# This path should work in any cases
TMPDIR=/dev/tmp
2016-09-14 02:31:13 +00:00
2017-07-09 16:17:34 +00:00
INSTALLER=$TMPDIR/install
2017-03-15 11:32:25 +00:00
COMMONDIR=$INSTALLER/common
2016-11-28 20:16:01 +00:00
CHROMEDIR=$INSTALLER/chromeos
2017-06-10 17:40:08 +00:00
COREDIR=/magisk/.core
2016-11-28 20:16:01 +00:00
2016-11-13 08:58:43 +00:00
# Default permissions
umask 022
2016-09-14 02:31:13 +00:00
OUTFD=$2
ZIP=$3
2017-06-16 07:27:28 +00:00
rm -rf $TMPDIR 2>/dev/null
2017-06-24 14:38:20 +00:00
mkdir -p $INSTALLER
2017-07-09 16:17:34 +00:00
unzip -o "$ZIP" -d $INSTALLER 2>/dev/null
2016-09-14 02:31:13 +00:00
2017-06-24 14:38:20 +00:00
if [ ! -d "$COMMONDIR" ]; then
echo "! Unable to extract zip file!"
exit 1
fi
2017-07-09 16:17:34 +00:00
# Load utility fuctions
2017-06-18 16:15:44 +00:00
. $COMMONDIR/util_functions.sh
2017-07-09 16:17:34 +00:00
get_outfd
##########################################################################################
# Detection
##########################################################################################
2017-04-21 23:05:10 +00:00
ui_print "************************"
ui_print "* MAGISK_VERSION_STUB"
ui_print "************************"
2016-09-14 02:31:13 +00:00
2017-07-02 13:36:09 +00:00
ui_print "- Mounting /system, /vendor, /cache, /data"
2016-09-14 02:31:13 +00:00
mount -o ro /system 2>/dev/null
2017-06-24 14:38:20 +00:00
mount -o ro /vendor 2>/dev/null
2016-09-14 02:31:13 +00:00
mount /cache 2>/dev/null
mount /data 2>/dev/null
2017-06-15 20:08:34 +00:00
[ -f /system/build.prop ] || abort "! /system could not be mounted!"
2017-06-10 17:40:08 +00:00
2017-03-12 10:13:58 +00:00
# read override variables
getvar KEEPVERITY
getvar KEEPFORCEENCRYPT
getvar BOOTIMAGE
2016-09-14 02:31:13 +00:00
2017-01-01 19:00:03 +00:00
# Check if system root is installed and remove
remove_system_su
2017-07-02 13:36:09 +00:00
# Detect version and architecture
api_level_arch_detect
2016-10-02 20:34:50 +00:00
2017-06-15 20:08:34 +00:00
[ $API -lt 21 ] && abort "! Magisk is only for Lollipop 5.0+ (SDK 21+)"
2016-10-29 22:13:59 +00:00
ui_print "- Device platform: $ARCH"
BINDIR=$INSTALLER/$ARCH
2017-03-12 11:22:15 +00:00
chmod -R 755 $CHROMEDIR $BINDIR
2016-10-29 22:13:59 +00:00
find_boot_image
2017-06-15 20:08:34 +00:00
[ -z $BOOTIMAGE ] && abort "! Unable to detect boot image"
2016-10-02 20:34:50 +00:00
2016-11-13 08:58:43 +00:00
##########################################################################################
# Environment
##########################################################################################
2017-04-21 23:05:10 +00:00
ui_print "- Constructing environment"
2017-06-24 14:38:20 +00:00
2017-04-21 23:05:10 +00:00
is_mounted /data && MAGISKBIN=/data/magisk || MAGISKBIN=/cache/data_bin
2016-11-13 08:58:43 +00:00
2017-04-21 23:05:10 +00:00
# Copy required files
rm -rf $MAGISKBIN 2>/dev/null
mkdir -p $MAGISKBIN
2017-06-03 12:19:01 +00:00
cp -af $BINDIR/. $COMMONDIR/. $MAGISKBIN
2017-07-09 16:17:34 +00:00
cp -af $CHROMEDIR $MAGISKBIN
2017-04-21 23:05:10 +00:00
chmod -R 755 $MAGISKBIN
2017-02-04 10:44:07 +00:00
2017-06-18 16:15:44 +00: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 13:43:48 +00:00
##########################################################################################
# Magisk Image
##########################################################################################
2017-07-02 13:36:09 +00:00
$BOOTMODE || recovery_actions
2017-04-17 08:36:49 +00:00
# Fix SuperSU.....
2017-07-09 16:17:34 +00:00
$BOOTMODE && $MAGISKBIN/magisk magiskpolicy --live "allow fsck * * *"
2017-04-17 08:36:49 +00: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-07-09 16:17:34 +00:00
$MAGISKBIN/magisk --createimg $IMG 64M
2017-04-17 08:36:49 +00:00
fi
2017-06-24 14:38:20 +00:00
if ! is_mounted /magisk; then
ui_print "- Mounting $IMG to /magisk"
2017-07-09 16:17:34 +00:00
MAGISKLOOP=`$MAGISKBIN/magisk --mountimg $IMG /magisk`
2017-06-24 14:38:20 +00:00
fi
2017-06-15 20:08:34 +00:00
is_mounted /magisk || abort "! Magisk image mount failed..."
2017-04-17 08:36:49 +00:00
2017-06-03 12:19:01 +00:00
# Core folders
mkdir -p $COREDIR/props $COREDIR/post-fs-data.d $COREDIR/service.d 2>/dev/null
2017-06-15 20:08:34 +00: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 12:19:01 +00:00
# Legacy cleanup
mv $COREDIR/magiskhide/hidelist $COREDIR/hidelist 2>/dev/null
rm -rf $COREDIR/magiskhide $COREDIR/bin
2017-03-26 13:43:48 +00:00
2017-03-12 10:13:58 +00: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-07-09 16:17:34 +00:00
SHA1=`$MAGISKBIN/magiskboot --sha1 /data/stock_boot.img | tail -n 1`
2017-03-12 10:13:58 +00:00
STOCKDUMP=/data/stock_boot_${SHA1}.img
mv /data/stock_boot.img $STOCKDUMP
2017-07-09 16:17:34 +00:00
$MAGISKBIN/magiskboot --compress $STOCKDUMP
2017-03-12 10:13:58 +00:00
fi
2017-06-03 12:19:01 +00:00
SOURCEDMODE=true
cd $MAGISKBIN
2017-02-04 10:44:07 +00:00
2017-06-03 12:19:01 +00:00
# Source the boot patcher
2017-06-10 17:40:08 +00:00
. $COMMONDIR/boot_patch.sh "$BOOTIMAGE"
2017-03-12 10:13:58 +00:00
2017-07-09 16:17:34 +00:00
if [ -f stock_boot* ]; then
rm -f /data/stock_boot* 2>/dev/null
mv stock_boot* /data
2017-03-12 10:13:58 +00:00
fi
2016-09-14 02:31:13 +00:00
ui_print "- Flashing new boot image"
2017-06-10 17:40:08 +00:00
if [ -L "$BOOTIMAGE" ]; then
dd if=new-boot.img of="$BOOTIMAGE" bs=4096
2017-06-03 12:19:01 +00:00
else
2017-07-02 13:36:09 +00:00
cat new-boot.img /dev/zero | dd of="$BOOTIMAGE" bs=4096 >/dev/null 2>&1
2017-06-03 12:19:01 +00:00
fi
rm -f new-boot.img
2016-09-14 02:31:13 +00:00
2017-01-01 18:00:53 +00:00
cd /
2017-03-12 10:13:58 +00:00
if ! $BOOTMODE; then
2017-07-09 16:17:34 +00:00
$MAGISKBIN/magisk --umountimg /magisk $MAGISKLOOP
2016-11-13 08:58:43 +00:00
rmdir /magisk
2017-07-02 13:36:09 +00:00
recovery_cleanup
2016-09-14 02:31:13 +00:00
fi
ui_print "- Done"
exit 0