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-07-09 16:17:34 +00:00
|
|
|
# Used in flash_script.sh, addon.d.sh, magisk module installers, and uninstaller
|
2017-07-24 19:10:01 +00:00
|
|
|
#
|
2017-06-18 16:15:44 +00:00
|
|
|
##########################################################################################
|
|
|
|
|
2017-07-10 17:54:11 +00:00
|
|
|
MAGISK_VERSION_STUB
|
2017-07-30 19:03:52 +00:00
|
|
|
SCRIPT_VERSION=$MAGISK_VER_CODE
|
2017-07-10 17:54:11 +00:00
|
|
|
|
2017-07-09 16:17:34 +00:00
|
|
|
get_outfd() {
|
|
|
|
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-18 16:15:44 +00:00
|
|
|
ui_print() {
|
|
|
|
if $BOOTMODE; then
|
|
|
|
echo "$1"
|
2017-07-24 19:10:01 +00:00
|
|
|
else
|
2017-06-18 16:15:44 +00:00
|
|
|
echo -n -e "ui_print $1\n" >> /proc/self/fd/$OUTFD
|
|
|
|
echo -n -e "ui_print\n" >> /proc/self/fd/$OUTFD
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2017-07-30 19:03:52 +00:00
|
|
|
grep_prop() {
|
|
|
|
REGEX="s/^$1=//p"
|
|
|
|
shift
|
|
|
|
FILES=$@
|
|
|
|
[ -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
|
2017-08-04 15:49:22 +00:00
|
|
|
local VALUE=$(eval echo \$$VARNAME)
|
2017-07-30 19:03:52 +00:00
|
|
|
[ ! -z $VALUE ] && return
|
|
|
|
for DIR in /dev /data /cache /system; do
|
|
|
|
VALUE=`grep_prop $VARNAME $DIR/.magisk`
|
|
|
|
[ ! -z $VALUE ] && break;
|
2017-06-18 16:15:44 +00:00
|
|
|
done
|
|
|
|
eval $VARNAME=\$VALUE
|
|
|
|
}
|
|
|
|
|
|
|
|
find_boot_image() {
|
|
|
|
if [ -z "$BOOTIMAGE" ]; then
|
2017-07-30 19:03:52 +00:00
|
|
|
for BLOCK in boot_a kern-a android_boot kernel boot lnx; do
|
|
|
|
BOOTIMAGE=`find /dev/block -iname $BLOCK | head -n 1` 2>/dev/null
|
2017-06-18 16:15:44 +00:00
|
|
|
[ ! -z $BOOTIMAGE ] && break
|
|
|
|
done
|
|
|
|
fi
|
|
|
|
# Recovery fallback
|
|
|
|
if [ -z "$BOOTIMAGE" ]; then
|
|
|
|
for FSTAB in /etc/*fstab*; do
|
2017-07-30 19:03:52 +00:00
|
|
|
BOOTIMAGE=`grep -v '#' $FSTAB | grep -E '\b/boot\b' | grep -oE '/dev/[a-zA-Z0-9_./-]*'`
|
2017-06-18 16:15:44 +00:00
|
|
|
[ ! -z $BOOTIMAGE ] && break
|
|
|
|
done
|
|
|
|
fi
|
|
|
|
[ -L "$BOOTIMAGE" ] && BOOTIMAGE=`readlink $BOOTIMAGE`
|
|
|
|
}
|
|
|
|
|
|
|
|
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 $?
|
|
|
|
}
|
|
|
|
|
|
|
|
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-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
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2017-08-12 08:44:58 +00:00
|
|
|
boot_actions() {
|
|
|
|
if [ ! -d /dev/magisk/mirror/bin ]; then
|
|
|
|
mkdir -p /dev/magisk/mirror/bin
|
|
|
|
mount -o bind $MAGISKBIN /dev/magisk/mirror/bin
|
|
|
|
fi
|
|
|
|
MAGISKBIN=/dev/magisk/mirror/bin
|
|
|
|
$MAGISKBIN/magisk magiskpolicy --live "allow fsck * * *"
|
|
|
|
}
|
|
|
|
|
2017-06-24 14:38:20 +00:00
|
|
|
recovery_actions() {
|
|
|
|
# TWRP bug fix
|
|
|
|
mount -o bind /dev/urandom /dev/random
|
2017-07-24 19:10:01 +00:00
|
|
|
# Preserve environment varibles
|
|
|
|
OLD_PATH=$PATH
|
|
|
|
OLD_LD_PATH=$LD_LIBRARY_PATH
|
2017-08-01 18:21:50 +00:00
|
|
|
if [ ! -d $TMPDIR/bin ]; then
|
|
|
|
# Add busybox to PATH
|
|
|
|
mkdir -p $TMPDIR/bin
|
|
|
|
ln -s $MAGISKBIN/busybox $TMPDIR/bin/busybox
|
|
|
|
$MAGISKBIN/busybox --install -s $TMPDIR/bin
|
|
|
|
export PATH=$TMPDIR/bin:$PATH
|
|
|
|
fi
|
2017-06-24 14:38:20 +00:00
|
|
|
# Temporarily block out all custom recovery binaries/libs
|
|
|
|
mv /sbin /sbin_tmp
|
2017-07-02 13:36:09 +00:00
|
|
|
# Add all possible library paths
|
|
|
|
$IS64BIT && export LD_LIBRARY_PATH=/system/lib64:/system/vendor/lib64 || export LD_LIBRARY_PATH=/system/lib:/system/vendor/lib
|
|
|
|
}
|
|
|
|
|
|
|
|
recovery_cleanup() {
|
2017-07-30 19:03:52 +00:00
|
|
|
mv /sbin_tmp /sbin 2>/dev/null
|
2017-07-02 13:36:09 +00:00
|
|
|
export LD_LIBRARY_PATH=$OLD_LD_PATH
|
2017-07-30 19:03:52 +00:00
|
|
|
[ -z $OLD_PATH ] || export PATH=$OLD_PATH
|
2017-07-02 13:36:09 +00:00
|
|
|
ui_print "- Unmounting partitions"
|
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() {
|
|
|
|
chown $2:$3 $1 || exit 1
|
|
|
|
chmod $4 $1 || exit 1
|
2017-07-30 19:03:52 +00:00
|
|
|
[ -z $5 ] && chcon -h 'u:object_r:system_file:s0' $1 || chcon -h $5 $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() {
|
|
|
|
reqSizeM=`du -s $1 | cut -f1`
|
|
|
|
reqSizeM=$((reqSizeM / 1024 + 1))
|
|
|
|
}
|
|
|
|
|
2017-08-12 15:15:39 +00:00
|
|
|
request_zip_size_check() {
|
|
|
|
reqSizeM=`unzip -l "$1" | tail -n 1 | awk '{ print int($1 / 1048567 + 1) }'`
|
|
|
|
}
|
|
|
|
|
2017-07-09 16:17:34 +00:00
|
|
|
image_size_check() {
|
|
|
|
SIZE="`$MAGISKBIN/magisk --imgsize $IMG`"
|
|
|
|
curUsedM=`echo "$SIZE" | cut -d" " -f1`
|
|
|
|
curSizeM=`echo "$SIZE" | cut -d" " -f2`
|
|
|
|
curFreeM=$((curSizeM - curUsedM))
|
|
|
|
}
|
|
|
|
|