1
0
mirror of https://github.com/topjohnwu/Magisk.git synced 2024-12-24 08:47:51 +00:00
Magisk/scripts/flash_script.sh

151 lines
3.9 KiB
Bash
Raw Permalink Normal View History

#MAGISK
2016-09-14 10:31:13 +08:00
##########################################################################################
#
2017-06-03 20:19:01 +08:00
# Magisk Flash Script
2016-09-14 10:31:13 +08:00
# by topjohnwu
2017-07-25 02:02:19 +08:00
#
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-07-10 00:17:34 +08:00
##########################################################################################
# Preparation
##########################################################################################
2017-03-15 19:32:25 +08:00
COMMONDIR=$INSTALLER/common
2017-10-31 17:05:24 +08:00
APK=$COMMONDIR/magisk.apk
2016-11-29 04:16:01 +08:00
CHROMEDIR=$INSTALLER/chromeos
2016-11-13 16:58:43 +08:00
# Default permissions
umask 022
2016-09-14 10:31:13 +08:00
OUTFD=$2
ZIP=$3
2017-09-16 03:48:58 +08:00
if [ ! -f $COMMONDIR/util_functions.sh ]; then
2017-06-24 23:38:20 +09:00
echo "! Unable to extract zip file!"
exit 1
fi
2017-07-10 00:17:34 +08:00
# Load utility fuctions
2017-06-19 00:15:44 +08:00
. $COMMONDIR/util_functions.sh
2018-08-28 22:03:12 -04:00
setup_flashable
2017-07-10 00:17:34 +08:00
##########################################################################################
# Detection
##########################################################################################
2017-04-22 07:05:10 +08:00
ui_print "************************"
2017-07-31 03:03:52 +08:00
ui_print "* Magisk v$MAGISK_VER Installer"
2017-04-22 07:05:10 +08:00
ui_print "************************"
2016-09-14 10:31:13 +08:00
2019-04-06 13:04:17 -04:00
is_mounted /data || mount /data || is_mounted /cache || mount /cache
2017-09-13 04:07:25 +08:00
mount_partitions
check_data
get_flags
2019-04-05 05:13:57 +08:00
find_boot_image
2018-07-04 23:46:16 +08:00
[ -z $BOOTIMAGE ] && abort "! Unable to detect target image"
ui_print "- Target image: $BOOTIMAGE"
2017-07-02 21:36:09 +08:00
# Detect version and architecture
api_level_arch_detect
2016-10-03 04:34:50 +08:00
2019-02-03 16:42:16 -05:00
[ $API -lt 17 ] && abort "! Magisk is only for Android 4.2 and above"
2016-10-30 06:13:59 +08:00
ui_print "- Device platform: $ARCH"
2018-04-22 14:13:27 +08:00
BINDIR=$INSTALLER/$ARCH32
2017-03-12 19:22:15 +08:00
chmod -R 755 $CHROMEDIR $BINDIR
2016-10-30 06:13:59 +08:00
# Check if system root is installed and remove
remove_system_su
2016-11-13 16:58:43 +08:00
##########################################################################################
# Environment
##########################################################################################
2017-04-22 07:05:10 +08:00
ui_print "- Constructing environment"
2017-07-25 02:02:19 +08:00
2017-04-22 07:05:10 +08:00
# Copy required files
rm -rf $MAGISKBIN/* 2>/dev/null
mkdir -p $MAGISKBIN 2>/dev/null
2019-02-24 02:11:11 -05:00
cp -af $BINDIR/. $COMMONDIR/. $CHROMEDIR $BBDIR/busybox $MAGISKBIN
2017-04-22 07:05:10 +08:00
chmod -R 755 $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
2018-08-03 22:40:49 +08:00
ADDOND=/system/addon.d/99-magisk.sh
cat <<EOF > $ADDOND
#!/sbin/sh
# ADDOND_VERSION=2
if [ -f /data/adb/magisk/addon.d.sh ]; then
exec sh /data/adb/magisk/addon.d.sh "\$@"
else
2019-04-30 16:35:56 -03:00
OUTFD=\$(ps | grep -v 'grep' | grep -oE 'update(.*)' | cut -d" " -f3)
ui_print() { echo -e "ui_print \$1\nui_print" >> /proc/self/fd/\$OUTFD; }
ui_print "************************"
ui_print "* Magisk addon.d failed"
ui_print "************************"
ui_print "! Cannot find Magisk binaries - was data wiped or not decrypted?"
ui_print "! Reflash OTA from decrypted recovery or reflash Magisk"
fi
EOF
2018-08-03 22:40:49 +08:00
chmod 755 $ADDOND
2017-06-19 00:15:44 +08:00
fi
$BOOTMODE || recovery_actions
2017-04-17 16:36:49 +08:00
2017-03-12 18:13:58 +08:00
##########################################################################################
2017-09-05 21:44:22 +08:00
# Boot patching
2017-03-12 18:13:58 +08:00
##########################################################################################
2017-10-31 17:05:24 +08:00
eval $BOOTSIGNER -verify < $BOOTIMAGE && BOOTSIGNED=true
$BOOTSIGNED && ui_print "- Boot image is signed with AVB 1.0"
2017-03-12 18:13:58 +08:00
2017-06-03 20:19:01 +08:00
SOURCEDMODE=true
cd $MAGISKBIN
2017-02-04 18:44:07 +08:00
$IS64BIT && mv -f magiskinit64 magiskinit || rm -f magiskinit64
2017-06-03 20:19:01 +08:00
# Source the boot patcher
. ./boot_patch.sh "$BOOTIMAGE"
2017-03-12 18:13:58 +08:00
2018-08-10 18:59:14 +08:00
ui_print "- Flashing new boot image"
2019-02-24 20:39:01 -05:00
if ! flash_image new-boot.img "$BOOTIMAGE"; then
ui_print "- Compressing ramdisk to fit in partition"
./magiskboot cpio ramdisk.cpio compress
./magiskboot repack "$BOOTIMAGE"
2019-02-24 20:39:01 -05:00
flash_image new-boot.img "$BOOTIMAGE" || abort "! Insufficient partition size"
fi
./magiskboot cleanup
2017-11-11 01:33:50 +08:00
rm -f new-boot.img
if [ -f stock_boot* ]; then
rm -f /data/stock_boot* 2>/dev/null
$DATA && mv stock_boot* /data
fi
2017-03-12 18:13:58 +08:00
$KEEPVERITY || patch_dtbo_image
2017-11-11 01:33:50 +08:00
if [ -f stock_dtbo* ]; then
rm -f /data/stock_dtbo* 2>/dev/null
$DATA && mv stock_dtbo* /data
2017-11-11 01:33:50 +08:00
fi
2016-09-14 10:31:13 +08:00
2017-01-02 02:00:53 +08:00
cd /
2017-09-05 21:44:22 +08:00
# Cleanups
$BOOTMODE || recovery_cleanup
rm -rf $TMPDIR
2017-07-31 03:03:52 +08:00
2016-09-14 10:31:13 +08:00
ui_print "- Done"
exit 0