2017-06-18 16:15:44 +00:00
|
|
|
##########################################################################################
|
2017-07-24 19:10:01 +00:00
|
|
|
#
|
2017-06-18 16:15:44 +00:00
|
|
|
# Magisk General Utility Functions
|
|
|
|
# by topjohnwu
|
2017-07-24 19:10:01 +00:00
|
|
|
#
|
2017-09-13 07:44:35 +00:00
|
|
|
# Used everywhere in Magisk
|
2017-07-24 19:10:01 +00:00
|
|
|
#
|
2017-06-18 16:15:44 +00:00
|
|
|
##########################################################################################
|
|
|
|
|
2017-12-20 19:36:18 +00:00
|
|
|
#MAGISK_VERSION_STUB
|
2017-07-10 17:54:11 +00:00
|
|
|
|
2018-01-01 08:46:28 +00:00
|
|
|
# Detect whether in boot mode
|
2018-09-20 20:21:22 +00:00
|
|
|
[ -z $BOOTMODE ] && BOOTMODE=false
|
|
|
|
$BOOTMODE || ps | grep zygote | grep -qv grep && BOOTMODE=true
|
2018-08-29 02:03:12 +00:00
|
|
|
$BOOTMODE || ps -A | grep zygote | grep -qv grep && BOOTMODE=true
|
2018-01-01 08:46:28 +00:00
|
|
|
|
2018-08-29 02:03:12 +00:00
|
|
|
# Presets
|
2018-11-15 06:36:03 +00:00
|
|
|
MAGISKTMP=/sbin/.magisk
|
2018-08-29 02:03:12 +00:00
|
|
|
[ -z $NVBASE ] && NVBASE=/data/adb
|
|
|
|
[ -z $MAGISKBIN ] && MAGISKBIN=$NVBASE/magisk
|
|
|
|
[ -z $IMG ] && IMG=$NVBASE/magisk.img
|
2017-09-27 16:54:01 +00:00
|
|
|
|
2018-09-20 19:37:59 +00:00
|
|
|
# Bootsigner related stuff
|
|
|
|
BOOTSIGNERCLASS=a.a
|
|
|
|
BOOTSIGNER="/system/bin/dalvikvm -Xnodex2oat -Xnoimage-dex2oat -cp \$APK \$BOOTSIGNERCLASS"
|
2017-10-31 09:05:24 +00:00
|
|
|
BOOTSIGNED=false
|
|
|
|
|
2018-08-29 02:03:12 +00:00
|
|
|
setup_flashable() {
|
2018-08-02 20:25:00 +00:00
|
|
|
$BOOTMODE && return
|
|
|
|
# Preserve environment varibles
|
|
|
|
OLD_PATH=$PATH
|
|
|
|
setup_bb
|
2018-07-07 09:48:05 +00:00
|
|
|
if [ -z $OUTFD ] || readlink /proc/$$/fd/$OUTFD | grep -q /tmp; then
|
2018-06-21 02:53:49 +00:00
|
|
|
# We will have to manually find out OUTFD
|
2018-07-07 09:48:05 +00:00
|
|
|
for FD in `ls /proc/$$/fd`; do
|
|
|
|
if readlink /proc/$$/fd/$FD | grep -q pipe; then
|
2018-06-21 02:53:49 +00:00
|
|
|
if ps | grep -v grep | grep -q " 3 $FD "; then
|
2017-07-09 16:17:34 +00:00
|
|
|
OUTFD=$FD
|
|
|
|
break
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2018-08-29 02:03:12 +00:00
|
|
|
# Backward compatibility
|
|
|
|
get_outfd() {
|
|
|
|
setup_flashable
|
|
|
|
}
|
|
|
|
|
2017-06-18 16:15:44 +00:00
|
|
|
ui_print() {
|
2017-10-07 14:08:10 +00:00
|
|
|
$BOOTMODE && echo "$1" || echo -e "ui_print $1\nui_print" >> /proc/self/fd/$OUTFD
|
2017-06-18 16:15:44 +00:00
|
|
|
}
|
|
|
|
|
2018-06-20 17:37:08 +00:00
|
|
|
toupper() {
|
|
|
|
echo "$@" | tr '[:lower:]' '[:upper:]'
|
|
|
|
}
|
|
|
|
|
|
|
|
find_block() {
|
2018-07-02 18:57:57 +00:00
|
|
|
for BLOCK in "$@"; do
|
|
|
|
DEVICE=`find /dev/block -type l -iname $BLOCK | head -n 1` 2>/dev/null
|
|
|
|
if [ ! -z $DEVICE ]; then
|
|
|
|
readlink -f $DEVICE
|
|
|
|
return 0
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
# Fallback by parsing sysfs uevents
|
2018-06-20 17:37:08 +00:00
|
|
|
for uevent in /sys/dev/block/*/uevent; do
|
|
|
|
local DEVNAME=`grep_prop DEVNAME $uevent`
|
|
|
|
local PARTNAME=`grep_prop PARTNAME $uevent`
|
2018-12-24 13:36:37 +00:00
|
|
|
for BLOCK in "$@"; do
|
|
|
|
if [ "`toupper $BLOCK`" = "`toupper $PARTNAME`" ]; then
|
2018-06-20 17:37:08 +00:00
|
|
|
echo /dev/block/$DEVNAME
|
|
|
|
return 0
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
done
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2017-09-12 20:07:25 +00:00
|
|
|
mount_partitions() {
|
|
|
|
# Check A/B slot
|
2018-06-20 17:37:08 +00:00
|
|
|
SLOT=`grep_cmdline androidboot.slot_suffix`
|
|
|
|
if [ -z $SLOT ]; then
|
|
|
|
SLOT=_`grep_cmdline androidboot.slot`
|
|
|
|
[ $SLOT = "_" ] && SLOT=
|
2017-11-10 17:33:50 +00:00
|
|
|
fi
|
2018-06-20 17:37:08 +00:00
|
|
|
[ -z $SLOT ] || ui_print "- Current boot slot: $SLOT"
|
2018-01-01 08:46:28 +00:00
|
|
|
|
2017-09-12 20:07:25 +00:00
|
|
|
ui_print "- Mounting /system, /vendor"
|
2018-06-17 09:59:24 +00:00
|
|
|
[ -f /system/build.prop ] || is_mounted /system || mount -o ro /system 2>/dev/null
|
2017-09-12 20:07:25 +00:00
|
|
|
if ! is_mounted /system && ! [ -f /system/build.prop ]; then
|
2018-06-20 17:37:08 +00:00
|
|
|
SYSTEMBLOCK=`find_block system$SLOT`
|
2019-02-02 01:28:24 +00:00
|
|
|
mount -o ro $SYSTEMBLOCK /system
|
2017-09-12 20:07:25 +00:00
|
|
|
fi
|
2018-06-17 09:59:24 +00:00
|
|
|
[ -f /system/build.prop ] || is_mounted /system || abort "! Cannot mount /system"
|
2018-10-20 21:10:35 +00:00
|
|
|
grep -qE '/dev/root|/system_root' /proc/mounts && SYSTEM_ROOT=true || SYSTEM_ROOT=false
|
2018-06-17 09:59:24 +00:00
|
|
|
if [ -f /system/init ]; then
|
|
|
|
SYSTEM_ROOT=true
|
2017-09-12 20:07:25 +00:00
|
|
|
mkdir /system_root 2>/dev/null
|
|
|
|
mount --move /system /system_root
|
|
|
|
mount -o bind /system_root/system /system
|
|
|
|
fi
|
|
|
|
if [ -L /system/vendor ]; then
|
|
|
|
# Seperate /vendor partition
|
|
|
|
is_mounted /vendor || mount -o ro /vendor 2>/dev/null
|
|
|
|
if ! is_mounted /vendor; then
|
2018-06-20 17:37:08 +00:00
|
|
|
VENDORBLOCK=`find_block vendor$SLOT`
|
2019-02-02 01:28:24 +00:00
|
|
|
mount -o ro $VENDORBLOCK /vendor
|
2017-09-12 20:07:25 +00:00
|
|
|
fi
|
|
|
|
is_mounted /vendor || abort "! Cannot mount /vendor"
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2018-06-17 17:40:56 +00:00
|
|
|
get_flags() {
|
|
|
|
# override variables
|
|
|
|
getvar KEEPVERITY
|
|
|
|
getvar KEEPFORCEENCRYPT
|
2018-12-24 17:08:46 +00:00
|
|
|
getvar RECOVERYMODE
|
2018-06-17 17:40:56 +00:00
|
|
|
if [ -z $KEEPVERITY ]; then
|
2018-08-29 04:40:14 +00:00
|
|
|
if $SYSTEM_ROOT; then
|
|
|
|
KEEPVERITY=true
|
|
|
|
ui_print "- Using system_root_image, keep dm/avb-verity"
|
|
|
|
else
|
|
|
|
KEEPVERITY=false
|
|
|
|
fi
|
2018-06-17 17:40:56 +00:00
|
|
|
fi
|
|
|
|
if [ -z $KEEPFORCEENCRYPT ]; then
|
2018-10-20 21:10:35 +00:00
|
|
|
grep ' /data ' /proc/mounts | grep -q 'dm-' && FDE=true || FDE=false
|
|
|
|
[ -d /data/unencrypted ] && FBE=true || FBE=false
|
|
|
|
# No data access means unable to decrypt in recovery
|
|
|
|
if $FDE || $FBE || ! $DATA; then
|
2018-06-17 17:40:56 +00:00
|
|
|
KEEPFORCEENCRYPT=true
|
|
|
|
ui_print "- Encrypted data detected, keep forceencrypt"
|
|
|
|
else
|
|
|
|
KEEPFORCEENCRYPT=false
|
|
|
|
fi
|
|
|
|
fi
|
2018-12-24 17:08:46 +00:00
|
|
|
[ -z $RECOVERYMODE ] && RECOVERYMODE=false
|
2018-06-17 17:40:56 +00:00
|
|
|
}
|
|
|
|
|
2018-06-17 09:59:24 +00:00
|
|
|
grep_cmdline() {
|
2018-06-20 17:37:08 +00:00
|
|
|
local REGEX="s/^$1=//p"
|
2018-12-06 01:07:15 +00:00
|
|
|
cat /proc/cmdline | tr '[:space:]' '\n' | sed -n "$REGEX" 2>/dev/null
|
2018-06-17 09:59:24 +00:00
|
|
|
}
|
|
|
|
|
2017-07-30 19:03:52 +00:00
|
|
|
grep_prop() {
|
2018-06-20 17:37:08 +00:00
|
|
|
local REGEX="s/^$1=//p"
|
2017-07-30 19:03:52 +00:00
|
|
|
shift
|
2018-06-20 17:37:08 +00:00
|
|
|
local FILES=$@
|
2017-07-30 19:03:52 +00:00
|
|
|
[ -z "$FILES" ] && FILES='/system/build.prop'
|
|
|
|
sed -n "$REGEX" $FILES 2>/dev/null | head -n 1
|
|
|
|
}
|
|
|
|
|
2017-06-18 16:15:44 +00:00
|
|
|
getvar() {
|
|
|
|
local VARNAME=$1
|
2018-02-09 19:34:13 +00:00
|
|
|
local VALUE=
|
2018-12-24 17:08:46 +00:00
|
|
|
VALUE=`grep_prop $VARNAME /sbin/.magisk/config /data/.magisk /cache/.magisk`
|
2018-02-09 19:34:13 +00:00
|
|
|
[ ! -z $VALUE ] && eval $VARNAME=\$VALUE
|
2017-09-26 12:21:43 +00:00
|
|
|
}
|
|
|
|
|
2018-01-01 08:46:28 +00:00
|
|
|
run_migrations() {
|
2017-09-06 08:13:23 +00:00
|
|
|
# Update the broken boot backup
|
|
|
|
if [ -f /data/stock_boot_.img.gz ]; then
|
2018-05-05 17:48:24 +00:00
|
|
|
$MAGISKBIN/magiskboot --decompress /data/stock_boot_.img.gz /data/stock_boot.img
|
2017-09-06 08:13:23 +00:00
|
|
|
fi
|
|
|
|
# Update our previous backup to new format if exists
|
|
|
|
if [ -f /data/stock_boot.img ]; then
|
|
|
|
ui_print "- Migrating boot image backup"
|
2018-05-05 17:48:24 +00:00
|
|
|
SHA1=`$MAGISKBIN/magiskboot --sha1 /data/stock_boot.img 2>/dev/null`
|
2017-09-06 08:13:23 +00:00
|
|
|
STOCKDUMP=/data/stock_boot_${SHA1}.img
|
|
|
|
mv /data/stock_boot.img $STOCKDUMP
|
2018-05-05 17:48:24 +00:00
|
|
|
$MAGISKBIN/magiskboot --compress $STOCKDUMP
|
2017-09-06 08:13:23 +00:00
|
|
|
fi
|
2018-01-01 08:46:28 +00:00
|
|
|
# Move the stock backups
|
|
|
|
if [ -f /data/magisk/stock_boot* ]; then
|
|
|
|
mv /data/magisk/stock_boot* /data 2>/dev/null
|
|
|
|
fi
|
|
|
|
if [ -f /data/adb/magisk/stock_boot* ]; then
|
|
|
|
mv /data/adb/magisk/stock_boot* /data 2>/dev/null
|
|
|
|
fi
|
|
|
|
# Remove old dbs
|
|
|
|
rm -f /data/user*/*/magisk.db
|
2018-02-09 19:34:13 +00:00
|
|
|
[ -L /data/magisk.img ] || mv /data/magisk.img /data/adb/magisk.img 2>/dev/null
|
2017-09-26 12:21:43 +00:00
|
|
|
}
|
|
|
|
|
2018-06-20 17:37:08 +00:00
|
|
|
find_boot_image() {
|
|
|
|
BOOTIMAGE=
|
|
|
|
if [ ! -z $SLOT ]; then
|
2018-12-24 13:36:37 +00:00
|
|
|
BOOTIMAGE=`find_block ramdisk$SLOT recovery_ramdisk$SLOT boot$SLOT`
|
2018-06-20 17:37:08 +00:00
|
|
|
else
|
2018-12-24 13:36:37 +00:00
|
|
|
BOOTIMAGE=`find_block ramdisk recovery_ramdisk boot boot_a kern-a android_boot kernel lnx bootimg`
|
2018-06-20 17:37:08 +00:00
|
|
|
fi
|
2018-07-03 10:28:44 +00:00
|
|
|
if [ -z $BOOTIMAGE ]; then
|
|
|
|
# Lets see what fstabs tells me
|
|
|
|
BOOTIMAGE=`grep -v '#' /etc/*fstab* | grep -E '/boot[^a-zA-Z]' | grep -oE '/dev/[a-zA-Z0-9_./-]*' | head -n 1`
|
|
|
|
fi
|
2018-06-20 17:37:08 +00:00
|
|
|
}
|
|
|
|
|
2018-08-10 10:59:14 +00:00
|
|
|
flash_image() {
|
2017-09-27 16:54:01 +00:00
|
|
|
# Make sure all blocks are writable
|
2018-05-05 17:48:24 +00:00
|
|
|
$MAGISKBIN/magisk --unlock-blocks 2>/dev/null
|
2017-09-26 12:21:43 +00:00
|
|
|
case "$1" in
|
2018-08-10 10:59:14 +00:00
|
|
|
*.gz) COM1="$MAGISKBIN/magiskboot --decompress '$1' - 2>/dev/null";;
|
|
|
|
*) COM1="cat '$1'";;
|
2017-09-26 12:21:43 +00:00
|
|
|
esac
|
2018-02-09 19:34:13 +00:00
|
|
|
if $BOOTSIGNED; then
|
2018-08-10 10:59:14 +00:00
|
|
|
COM2="$BOOTSIGNER -sign"
|
|
|
|
ui_print "- Sign image with test keys"
|
2018-02-09 19:34:13 +00:00
|
|
|
else
|
2018-08-10 10:59:14 +00:00
|
|
|
COM2="cat -"
|
|
|
|
fi
|
|
|
|
if [ -b "$2" ]; then
|
2018-08-11 07:56:12 +00:00
|
|
|
local s_size=`stat -c '%s' "$1"`
|
|
|
|
local t_size=`blockdev --getsize64 "$2"`
|
|
|
|
[ $s_size -gt $t_size ] && return 1
|
2018-08-10 10:59:14 +00:00
|
|
|
eval $COM1 | eval $COM2 | cat - /dev/zero > "$2" 2>/dev/null
|
|
|
|
else
|
|
|
|
ui_print "- Not block device, storing image"
|
|
|
|
eval $COM1 | eval $COM2 > "$2" 2>/dev/null
|
2018-02-09 19:34:13 +00:00
|
|
|
fi
|
2018-08-11 07:56:12 +00:00
|
|
|
return 0
|
2017-09-06 08:13:23 +00:00
|
|
|
}
|
|
|
|
|
2017-11-10 17:33:50 +00:00
|
|
|
find_dtbo_image() {
|
2018-06-20 17:37:08 +00:00
|
|
|
DTBOIMAGE=`find_block dtbo$SLOT`
|
2017-11-10 17:33:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
patch_dtbo_image() {
|
|
|
|
if [ ! -z $DTBOIMAGE ]; then
|
2018-05-05 17:48:24 +00:00
|
|
|
if $MAGISKBIN/magiskboot --dtb-test $DTBOIMAGE; then
|
2018-07-04 15:46:16 +00:00
|
|
|
ui_print "- Backing up stock DTBO image"
|
2018-05-05 17:48:24 +00:00
|
|
|
$MAGISKBIN/magiskboot --compress $DTBOIMAGE $MAGISKBIN/stock_dtbo.img.gz
|
2018-07-04 15:46:16 +00:00
|
|
|
ui_print "- Patching DTBO to remove avb-verity"
|
2018-05-05 17:48:24 +00:00
|
|
|
$MAGISKBIN/magiskboot --dtb-patch $DTBOIMAGE
|
2017-11-14 20:41:30 +00:00
|
|
|
return 0
|
2017-11-10 17:33:50 +00:00
|
|
|
fi
|
|
|
|
fi
|
2017-11-14 20:41:30 +00:00
|
|
|
return 1
|
2017-11-10 17:33:50 +00:00
|
|
|
}
|
|
|
|
|
2017-09-06 08:13:23 +00:00
|
|
|
sign_chromeos() {
|
2017-10-07 14:08:10 +00:00
|
|
|
ui_print "- Signing ChromeOS boot image"
|
2017-09-06 08:13:23 +00:00
|
|
|
|
2017-10-07 14:08:10 +00:00
|
|
|
echo > empty
|
2018-05-05 17:48:24 +00:00
|
|
|
./chromeos/futility vbutil_kernel --pack new-boot.img.signed \
|
2017-09-06 08:13:23 +00:00
|
|
|
--keyblock ./chromeos/kernel.keyblock --signprivate ./chromeos/kernel_data_key.vbprivk \
|
|
|
|
--version 1 --vmlinuz new-boot.img --config empty --arch arm --bootloader empty --flags 0x1
|
|
|
|
|
|
|
|
rm -f empty new-boot.img
|
|
|
|
mv new-boot.img.signed new-boot.img
|
|
|
|
}
|
|
|
|
|
2017-06-18 16:15:44 +00:00
|
|
|
is_mounted() {
|
2019-01-10 19:29:13 +00:00
|
|
|
grep -q " `readlink -f $1` " /proc/mounts 2>/dev/null
|
2017-06-18 16:15:44 +00:00
|
|
|
return $?
|
|
|
|
}
|
|
|
|
|
|
|
|
remove_system_su() {
|
|
|
|
if [ -f /system/bin/su -o -f /system/xbin/su ] && [ ! -f /su/bin/su ]; then
|
2018-08-29 02:03:12 +00:00
|
|
|
ui_print "- Removing system installed root"
|
2017-06-18 16:15:44 +00:00
|
|
|
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-07-02 13:36:09 +00:00
|
|
|
api_level_arch_detect() {
|
|
|
|
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`
|
|
|
|
|
|
|
|
ARCH=arm
|
2018-04-22 06:13:27 +00:00
|
|
|
ARCH32=arm
|
2017-07-02 13:36:09 +00:00
|
|
|
IS64BIT=false
|
2018-04-22 06:13:27 +00:00
|
|
|
if [ "$ABI" = "x86" ]; then ARCH=x86; ARCH32=x86; fi;
|
|
|
|
if [ "$ABI2" = "x86" ]; then ARCH=x86; ARCH32=x86; fi;
|
|
|
|
if [ "$ABILONG" = "arm64-v8a" ]; then ARCH=arm64; ARCH32=arm; IS64BIT=true; fi;
|
|
|
|
if [ "$ABILONG" = "x86_64" ]; then ARCH=x64; ARCH32=x86; IS64BIT=true; fi;
|
2017-07-02 13:36:09 +00:00
|
|
|
}
|
|
|
|
|
2018-06-26 14:41:03 +00:00
|
|
|
check_data() {
|
|
|
|
DATA=false
|
|
|
|
DATA_DE=false
|
|
|
|
if grep ' /data ' /proc/mounts | grep -vq 'tmpfs'; then
|
|
|
|
# Test if data is writable
|
|
|
|
touch /data/.rw && rm /data/.rw && DATA=true
|
|
|
|
# Test if DE storage is writable
|
|
|
|
$DATA && [ -d /data/adb ] && touch /data/adb/.rw && rm /data/adb/.rw && DATA_DE=true
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2018-06-21 02:53:49 +00:00
|
|
|
setup_bb() {
|
2018-11-15 06:36:03 +00:00
|
|
|
if [ -x $MAGISKTMP/busybox/busybox ]; then
|
2018-06-21 08:39:10 +00:00
|
|
|
# Make sure this path is in the front
|
2018-11-15 06:36:03 +00:00
|
|
|
echo $PATH | grep -q "^$MAGISKTMP/busybox" || export PATH=$MAGISKTMP/busybox:$PATH
|
2018-06-21 08:39:10 +00:00
|
|
|
elif [ -x $TMPDIR/bin/busybox ]; then
|
|
|
|
# Make sure this path is in the front
|
|
|
|
echo $PATH | grep -q "^$TMPDIR/bin" || export PATH=$TMPDIR/bin:$PATH
|
|
|
|
else
|
|
|
|
# Construct the PATH
|
2018-06-21 02:53:49 +00:00
|
|
|
mkdir -p $TMPDIR/bin
|
|
|
|
ln -s $MAGISKBIN/busybox $TMPDIR/bin/busybox
|
|
|
|
$MAGISKBIN/busybox --install -s $TMPDIR/bin
|
|
|
|
export PATH=$TMPDIR/bin:$PATH
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2017-08-12 08:44:58 +00:00
|
|
|
boot_actions() {
|
2018-11-15 06:36:03 +00:00
|
|
|
if [ ! -d $MAGISKTMP/mirror/bin ]; then
|
|
|
|
mkdir -p $MAGISKTMP/mirror/bin
|
|
|
|
mount -o bind $MAGISKBIN $MAGISKTMP/mirror/bin
|
2017-08-12 08:44:58 +00:00
|
|
|
fi
|
2018-11-15 06:36:03 +00:00
|
|
|
MAGISKBIN=$MAGISKTMP/mirror/bin
|
2018-06-21 02:53:49 +00:00
|
|
|
setup_bb
|
2017-08-12 08:44:58 +00:00
|
|
|
}
|
|
|
|
|
2017-06-24 14:38:20 +00:00
|
|
|
recovery_actions() {
|
|
|
|
# TWRP bug fix
|
|
|
|
mount -o bind /dev/urandom /dev/random
|
|
|
|
# Temporarily block out all custom recovery binaries/libs
|
|
|
|
mv /sbin /sbin_tmp
|
2018-05-05 17:48:24 +00:00
|
|
|
# Unset library paths
|
|
|
|
OLD_LD_LIB=$LD_LIBRARY_PATH
|
|
|
|
OLD_LD_PRE=$LD_PRELOAD
|
|
|
|
unset LD_LIBRARY_PATH
|
|
|
|
unset LD_PRELOAD
|
2017-07-02 13:36:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
recovery_cleanup() {
|
2017-07-30 19:03:52 +00:00
|
|
|
mv /sbin_tmp /sbin 2>/dev/null
|
|
|
|
[ -z $OLD_PATH ] || export PATH=$OLD_PATH
|
2018-05-05 17:48:24 +00:00
|
|
|
[ -z $OLD_LD_LIB ] || export LD_LIBRARY_PATH=$OLD_LD_LIB
|
|
|
|
[ -z $OLD_LD_PRE ] || export LD_PRELOAD=$OLD_LD_PRE
|
2017-07-02 13:36:09 +00:00
|
|
|
ui_print "- Unmounting partitions"
|
2017-09-12 20:07:25 +00:00
|
|
|
umount -l /system_root 2>/dev/null
|
2017-07-30 19:03:52 +00:00
|
|
|
umount -l /system 2>/dev/null
|
2017-07-02 13:36:09 +00:00
|
|
|
umount -l /vendor 2>/dev/null
|
2017-07-30 19:03:52 +00:00
|
|
|
umount -l /dev/random 2>/dev/null
|
2017-06-24 14:38:20 +00:00
|
|
|
}
|
|
|
|
|
2017-06-18 16:15:44 +00:00
|
|
|
abort() {
|
|
|
|
ui_print "$1"
|
2017-07-30 19:03:52 +00:00
|
|
|
$BOOTMODE || recovery_cleanup
|
2017-06-18 16:15:44 +00:00
|
|
|
exit 1
|
|
|
|
}
|
2017-07-09 16:17:34 +00:00
|
|
|
|
|
|
|
set_perm() {
|
2018-02-09 19:34:13 +00:00
|
|
|
chown $2:$3 $1 || return 1
|
|
|
|
chmod $4 $1 || return 1
|
|
|
|
[ -z $5 ] && chcon 'u:object_r:system_file:s0' $1 || chcon $5 $1 || return 1
|
2017-07-09 16:17:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
set_perm_recursive() {
|
|
|
|
find $1 -type d 2>/dev/null | while read dir; do
|
|
|
|
set_perm $dir $2 $3 $4 $6
|
|
|
|
done
|
2017-07-30 19:03:52 +00:00
|
|
|
find $1 -type f -o -type l 2>/dev/null | while read file; do
|
2017-07-09 16:17:34 +00:00
|
|
|
set_perm $file $2 $3 $5 $6
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
|
|
|
mktouch() {
|
2017-07-30 19:03:52 +00:00
|
|
|
mkdir -p ${1%/*} 2>/dev/null
|
|
|
|
[ -z $2 ] && touch $1 || echo $2 > $1
|
2017-07-09 16:17:34 +00:00
|
|
|
chmod 644 $1
|
|
|
|
}
|
|
|
|
|
|
|
|
request_size_check() {
|
2018-06-21 22:18:06 +00:00
|
|
|
reqSizeM=`du -ms $1 | cut -f1`
|
2017-07-09 16:17:34 +00:00
|
|
|
}
|
|
|
|
|
2017-08-12 15:15:39 +00:00
|
|
|
request_zip_size_check() {
|
2018-06-21 22:18:06 +00:00
|
|
|
reqSizeM=`unzip -l "$1" | tail -n 1 | awk '{ print int(($1 - 1) / 1048576 + 1) }'`
|
2017-08-12 15:15:39 +00:00
|
|
|
}
|
|
|
|
|
2018-06-21 22:18:06 +00:00
|
|
|
check_filesystem() {
|
|
|
|
curSizeM=`wc -c < $1`
|
|
|
|
curSizeM=$((curSizeM / 1048576))
|
2018-12-05 12:24:49 +00:00
|
|
|
local DF=`df -Pk $2 | grep $2`
|
2018-06-21 22:18:06 +00:00
|
|
|
curUsedM=`echo $DF | awk '{ print int($3 / 1024) }'`
|
|
|
|
curFreeM=`echo $DF | awk '{ print int($4 / 1024) }'`
|
|
|
|
}
|
|
|
|
|
|
|
|
mount_snippet() {
|
|
|
|
MAGISKLOOP=`$MAGISKBIN/magisk imgtool mount $IMG $MOUNTPATH`
|
|
|
|
is_mounted $MOUNTPATH || abort "! $IMG mount failed..."
|
2017-07-09 16:17:34 +00:00
|
|
|
}
|
|
|
|
|
2017-12-25 19:23:58 +00:00
|
|
|
mount_magisk_img() {
|
2019-01-15 02:58:18 +00:00
|
|
|
[ -z $reqSizeM ] && reqSizeM=0
|
2018-06-21 22:18:06 +00:00
|
|
|
mkdir -p $MOUNTPATH 2>/dev/null
|
2017-12-25 19:23:58 +00:00
|
|
|
if [ -f "$IMG" ]; then
|
|
|
|
ui_print "- Found $IMG"
|
2018-06-21 22:18:06 +00:00
|
|
|
mount_snippet
|
|
|
|
check_filesystem $IMG $MOUNTPATH
|
|
|
|
if [ $reqSizeM -gt $curFreeM ]; then
|
|
|
|
newSizeM=$(((curSizeM + reqSizeM - curFreeM) / 32 * 32 + 64))
|
2017-12-25 19:23:58 +00:00
|
|
|
ui_print "- Resizing $IMG to ${newSizeM}M"
|
2018-06-21 22:18:06 +00:00
|
|
|
$MAGISKBIN/magisk imgtool umount $MOUNTPATH $MAGISKLOOP
|
|
|
|
$MAGISKBIN/magisk imgtool resize $IMG $newSizeM >&2
|
|
|
|
mount_snippet
|
2017-12-25 19:23:58 +00:00
|
|
|
fi
|
2018-06-21 22:18:06 +00:00
|
|
|
ui_print "- Mount $IMG to $MOUNTPATH"
|
2017-12-25 19:23:58 +00:00
|
|
|
else
|
2018-06-21 22:18:06 +00:00
|
|
|
newSizeM=$((reqSizeM / 32 * 32 + 64))
|
2017-12-25 19:23:58 +00:00
|
|
|
ui_print "- Creating $IMG with size ${newSizeM}M"
|
2018-06-21 22:18:06 +00:00
|
|
|
$MAGISKBIN/magisk imgtool create $IMG $newSizeM >&2
|
|
|
|
mount_snippet
|
2017-12-25 19:23:58 +00:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
unmount_magisk_img() {
|
2018-06-21 22:18:06 +00:00
|
|
|
check_filesystem $IMG $MOUNTPATH
|
2017-12-25 19:23:58 +00:00
|
|
|
newSizeM=$((curUsedM / 32 * 32 + 64))
|
2018-06-21 22:18:06 +00:00
|
|
|
$MAGISKBIN/magisk imgtool umount $MOUNTPATH $MAGISKLOOP
|
2017-12-25 19:23:58 +00:00
|
|
|
if [ $curSizeM -gt $newSizeM ]; then
|
|
|
|
ui_print "- Shrinking $IMG to ${newSizeM}M"
|
2018-06-21 22:18:06 +00:00
|
|
|
$MAGISKBIN/magisk imgtool resize $IMG $newSizeM >&2
|
2017-12-25 19:23:58 +00:00
|
|
|
fi
|
|
|
|
}
|
2018-11-15 08:12:31 +00:00
|
|
|
|
|
|
|
find_manager_apk() {
|
|
|
|
APK=/data/adb/magisk.apk
|
|
|
|
[ -f $APK ] || APK=/data/magisk/magisk.apk
|
|
|
|
[ -f $APK ] || APK=/data/app/com.topjohnwu.magisk*/*.apk
|
|
|
|
if [ ! -f $APK ]; then
|
|
|
|
DBAPK=`magisk --sqlite "SELECT value FROM strings WHERE key='requester'" | cut -d= -f2`
|
|
|
|
[ -z "$DBAPK" ] || APK=/data/app/$DBAPK*/*.apk
|
|
|
|
fi
|
|
|
|
}
|