Magisk/scripts/util_functions.sh

390 lines
12 KiB
Bash
Raw Normal View History

2017-06-18 16:15:44 +00:00
##########################################################################################
#
2017-06-18 16:15:44 +00:00
# Magisk General Utility Functions
# by topjohnwu
#
2017-09-13 07:44:35 +00:00
# Used everywhere in Magisk
#
2017-06-18 16:15:44 +00:00
##########################################################################################
2017-12-20 19:36:18 +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
2018-01-01 08:46:28 +00:00
# Detect whether in boot mode
ps | grep zygote | grep -v grep >/dev/null && BOOTMODE=true || BOOTMODE=false
$BOOTMODE || ps -A 2>/dev/null | grep zygote | grep -v grep >/dev/null && BOOTMODE=true
$BOOTMODE || id | grep -q 'uid=0' || BOOTMODE=true
# Default location, will override if needed
2017-12-25 19:23:58 +00:00
MAGISKBIN=/data/adb/magisk
2017-12-28 20:25:30 +00:00
[ -z $MOUNTPATH ] && MOUNTPATH=/sbin/.core/img
2017-12-25 19:23:58 +00:00
[ -z $IMG ] && IMG=/data/adb/magisk.img
2018-04-29 06:04:18 +00:00
BOOTSIGNER="eval \$LIBPFX /system/bin/dalvikvm -Xnodex2oat -Xnoimage-dex2oat -cp \$APK com.topjohnwu.magisk.utils.BootSigner"
2017-10-31 09:05:24 +00:00
BOOTSIGNED=false
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() {
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
}
2017-09-12 20:07:25 +00:00
mount_partitions() {
# Check A/B slot
SLOT=`getprop ro.boot.slot_suffix`
2017-11-10 17:33:50 +00:00
if [ -z $SLOT ]; then
SLOT=_`getprop ro.boot.slot`
[ $SLOT = "_" ] && SLOT=
fi
2018-01-01 08:46:28 +00:00
# Check the boot image to make sure the slot actually make sense
find_boot_image
find_dtbo_image
2017-11-10 17:33:50 +00:00
[ -z $SLOT ] || ui_print "- A/B partition detected, current slot: $SLOT"
2018-01-01 08:46:28 +00:00
2017-09-12 20:07:25 +00:00
ui_print "- Mounting /system, /vendor"
is_mounted /system || [ -f /system/build.prop ] || mount -o ro /system 2>/dev/null
if ! is_mounted /system && ! [ -f /system/build.prop ]; then
SYSTEMBLOCK=`find /dev/block -iname system$SLOT | head -n 1`
mount -t ext4 -o ro $SYSTEMBLOCK /system
fi
is_mounted /system || [ -f /system/build.prop ] || abort "! Cannot mount /system"
2017-09-13 07:44:35 +00:00
cat /proc/mounts | grep -E '/dev/root|/system_root' >/dev/null && SKIP_INITRAMFS=true || SKIP_INITRAMFS=false
2017-09-12 20:07:25 +00:00
if [ -f /system/init.rc ]; then
SKIP_INITRAMFS=true
mkdir /system_root 2>/dev/null
mount --move /system /system_root
mount -o bind /system_root/system /system
fi
$SKIP_INITRAMFS && ui_print "- Device skip_initramfs detected"
if [ -L /system/vendor ]; then
# Seperate /vendor partition
is_mounted /vendor || mount -o ro /vendor 2>/dev/null
if ! is_mounted /vendor; then
VENDORBLOCK=`find /dev/block -iname vendor$SLOT | head -n 1`
mount -t ext4 -o ro $VENDORBLOCK /vendor
fi
is_mounted /vendor || abort "! Cannot mount /vendor"
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
2018-02-09 19:34:13 +00:00
local VALUE=
2017-12-28 20:25:30 +00:00
for DIR in /.backup /dev /data /cache /system; do
2017-07-30 19:03:52 +00:00
VALUE=`grep_prop $VARNAME $DIR/.magisk`
[ ! -z $VALUE ] && break;
2017-06-18 16:15:44 +00:00
done
2018-02-09 19:34:13 +00:00
[ ! -z $VALUE ] && eval $VARNAME=\$VALUE
2017-09-26 12:21:43 +00:00
}
2017-06-18 16:15:44 +00:00
find_boot_image() {
BOOTIMAGE=
if [ ! -z $SLOT ]; then
BOOTIMAGE=`find /dev/block -iname boot$SLOT | head -n 1` 2>/dev/null
2018-01-01 08:46:28 +00:00
fi
2018-02-09 19:34:13 +00:00
if [ -z $BOOTIMAGE ]; then
2018-01-01 08:46:28 +00:00
# The slot info is incorrect...
SLOT=
2018-02-09 19:34:13 +00:00
for BLOCK in ramdisk boot_a kern-a android_boot kernel boot lnx bootimg; do
BOOTIMAGE=`find /dev/block -iname $BLOCK | head -n 1` 2>/dev/null
[ ! -z $BOOTIMAGE ] && break
done
2017-06-18 16:15:44 +00:00
fi
# Recovery fallback
2018-02-09 19:34:13 +00:00
if [ -z $BOOTIMAGE ]; then
2017-06-18 16:15:44 +00:00
for FSTAB in /etc/*fstab*; do
2017-09-26 12:21:43 +00:00
BOOTIMAGE=`grep -v '#' $FSTAB | grep -E '/boot[^a-zA-Z]' | grep -oE '/dev/[a-zA-Z0-9_./-]*'`
2017-06-18 16:15:44 +00:00
[ ! -z $BOOTIMAGE ] && break
done
fi
2018-02-09 19:34:13 +00:00
[ ! -z $BOOTIMAGE ] && BOOTIMAGE=`readlink -f $BOOTIMAGE`
2017-06-18 16:15:44 +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-04-29 06:04:18 +00:00
eval $LIB32PFX $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-04-29 06:04:18 +00:00
SHA1=`eval $LIB32PFX $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-04-29 06:04:18 +00:00
eval $LIB32PFX $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
rm -rf /data/stock_boot*
mv /data/magisk/stock_boot* /data 2>/dev/null
fi
if [ -f /data/adb/magisk/stock_boot* ]; then
rm -rf /data/stock_boot*
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
}
flash_boot_image() {
# Make sure all blocks are writable
2018-04-29 06:04:18 +00:00
eval $LIB32PFX $MAGISKBIN/magisk --unlock-blocks 2>/dev/null
2017-09-26 12:21:43 +00:00
case "$1" in
2017-11-10 17:33:50 +00:00
*.gz) COMMAND="gzip -d < '$1'";;
*) COMMAND="cat '$1'";;
2017-09-26 12:21:43 +00:00
esac
2018-02-09 19:34:13 +00:00
if $BOOTSIGNED; then
SIGNCOM="$BOOTSIGNER -sign"
ui_print "- Sign boot image with test keys"
else
SIGNCOM="cat -"
fi
2017-09-26 12:21:43 +00:00
case "$2" in
/dev/block/*)
ui_print "- Flashing new boot image"
2017-11-10 17:33:50 +00:00
eval $COMMAND | eval $SIGNCOM | cat - /dev/zero 2>/dev/null | dd of="$2" bs=4096 2>/dev/null
2017-09-26 12:21:43 +00:00
;;
*)
ui_print "- Storing new boot image"
2017-11-10 17:33:50 +00:00
eval $COMMAND | eval $SIGNCOM | dd of="$2" bs=4096 2>/dev/null
2017-09-26 12:21:43 +00:00
;;
esac
2017-09-06 08:13:23 +00:00
}
2017-11-10 17:33:50 +00:00
find_dtbo_image() {
DTBOIMAGE=`find /dev/block -iname dtbo$SLOT | head -n 1` 2>/dev/null
2018-02-09 19:34:13 +00:00
[ ! -z $DTBOIMAGE ] && DTBOIMAGE=`readlink -f $DTBOIMAGE`
2017-11-10 17:33:50 +00:00
}
patch_dtbo_image() {
if [ ! -z $DTBOIMAGE ]; then
2018-04-29 06:04:18 +00:00
if eval $LIB32PFX $MAGISKBIN/magiskboot --dtb-test $DTBOIMAGE; then
2017-11-10 17:33:50 +00:00
ui_print "- Backing up stock dtbo image"
2018-04-29 06:04:18 +00:00
eval $LIB32PFX $MAGISKBIN/magiskboot --compress $DTBOIMAGE $MAGISKBIN/stock_dtbo.img.gz
2017-11-10 17:33:50 +00:00
ui_print "- Patching fstab in dtbo to remove avb-verity"
2018-04-29 06:04:18 +00:00
eval $LIB32PFX $MAGISKBIN/magiskboot --dtb-patch $DTBOIMAGE
return 0
2017-11-10 17:33:50 +00:00
fi
fi
return 1
2017-11-10 17:33:50 +00:00
}
restore_imgs() {
STOCKBOOT=/data/stock_boot_${1}.img.gz
STOCKDTBO=/data/stock_dtbo.img.gz
# Make sure all blocks are writable
2018-04-29 06:04:18 +00:00
eval $LIB32PFX $MAGISKBIN/magisk --unlock-blocks 2>/dev/null
2017-11-10 17:33:50 +00:00
find_dtbo_image
if [ ! -z "$DTBOIMAGE" -a -f "$STOCKDTBO" ]; then
ui_print "- Restoring stock dtbo image"
gzip -d < $STOCKDTBO | dd of=$DTBOIMAGE
fi
BOOTSIGNED=false
find_boot_image
if [ ! -z "$BOOTIMAGE" -a -f "$STOCKBOOT" ]; then
ui_print "- Restoring stock boot image"
gzip -d < $STOCKBOOT | cat - /dev/zero 2>/dev/null | dd of="$BOOTIMAGE" bs=4096 2>/dev/null
2017-11-10 17:33:50 +00:00
return 0
fi
return 1
}
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-04-29 06:04:18 +00:00
eval $LIBPFX ./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() {
2018-02-09 19:34:13 +00:00
TARGET="`readlink -f $1`"
2017-11-22 14:22:29 +00:00
cat /proc/mounts | grep " $TARGET " >/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
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
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
}
2017-08-12 08:44:58 +00:00
boot_actions() {
2017-11-22 08:04:24 +00:00
if [ ! -d /sbin/.core/mirror/bin ]; then
mkdir -p /sbin/.core/mirror/bin
mount -o bind $MAGISKBIN /sbin/.core/mirror/bin
2017-08-12 08:44:58 +00:00
fi
2017-11-22 08:04:24 +00:00
MAGISKBIN=/sbin/.core/mirror/bin
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
# Preserve environment varibles
OLD_PATH=$PATH
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
2018-04-29 06:04:18 +00:00
# Set library paths
$IS64BIT && LIBPFX="LD_LIBRARY_PATH=/system/lib64:/system/vendor/lib64" || LIBPFX="LD_LIBRARY_PATH=/system/lib:/system/vendor/lib"
LIB32PFX="LD_LIBRARY_PATH=/system/lib:/system/vendor/lib"
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
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() {
reqSizeM=`du -s $1 | cut -f1`
reqSizeM=$((reqSizeM / 1024 + 1))
}
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() {
2018-04-29 06:04:18 +00:00
SIZE="`eval $LIB32PFX $MAGISKBIN/magisk --imgsize $IMG`"
2017-07-09 16:17:34 +00:00
curUsedM=`echo "$SIZE" | cut -d" " -f1`
curSizeM=`echo "$SIZE" | cut -d" " -f2`
curFreeM=$((curSizeM - curUsedM))
}
2017-12-25 19:23:58 +00:00
mount_magisk_img() {
[ -z reqSizeM ] && reqSizeM=0
if [ -f "$IMG" ]; then
ui_print "- Found $IMG"
image_size_check $IMG
if [ "$reqSizeM" -gt "$curFreeM" ]; then
newSizeM=$(((reqSizeM + curUsedM) / 32 * 32 + 64))
ui_print "- Resizing $IMG to ${newSizeM}M"
2018-04-29 06:04:18 +00:00
eval $LIB32PFX $MAGISKBIN/magisk --resizeimg $IMG $newSizeM >&2
2017-12-25 19:23:58 +00:00
fi
else
newSizeM=$((reqSizeM / 32 * 32 + 64));
ui_print "- Creating $IMG with size ${newSizeM}M"
2018-04-29 06:04:18 +00:00
eval $LIB32PFX $MAGISKBIN/magisk --createimg $IMG $newSizeM >&2
2017-12-25 19:23:58 +00:00
fi
ui_print "- Mounting $IMG to $MOUNTPATH"
2018-04-29 06:04:18 +00:00
MAGISKLOOP=`eval $LIB32PFX $MAGISKBIN/magisk --mountimg $IMG $MOUNTPATH`
2017-12-25 19:23:58 +00:00
is_mounted $MOUNTPATH || abort "! $IMG mount failed..."
}
unmount_magisk_img() {
2018-04-29 06:04:18 +00:00
eval $LIB32PFX $MAGISKBIN/magisk --umountimg $MOUNTPATH $MAGISKLOOP
2017-12-25 19:23:58 +00:00
# Shrink the image if possible
image_size_check $IMG
newSizeM=$((curUsedM / 32 * 32 + 64))
if [ $curSizeM -gt $newSizeM ]; then
ui_print "- Shrinking $IMG to ${newSizeM}M"
2018-04-29 06:04:18 +00:00
eval $LIB32PFX $MAGISKBIN/magisk --resizeimg $IMG $newSizeM
2017-12-25 19:23:58 +00:00
fi
}