############################################ # Magisk General Utility Functions ############################################ MAGISK_VER='23.0' MAGISK_VER_CODE=23000 ################### # Helper Functions ################### ui_print() { echo "$1" } toupper() { echo "$@" | tr '[:lower:]' '[:upper:]' } grep_cmdline() { local REGEX="s/^$1=//p" cat /proc/cmdline | tr '[:space:]' '\n' | sed -n "$REGEX" 2>/dev/null } grep_prop() { local REGEX="s/^$1=//p" shift local FILES=$@ [ -z "$FILES" ] && FILES='/system/build.prop' cat $FILES 2>/dev/null | dos2unix | sed -n "$REGEX" | head -n 1 } grep_get_prop() { local result=$(grep_prop $@) if [ -z "$result" ]; then # Fallback to getprop adb shell getprop "$1" else echo $result fi } getvar() { local VARNAME=$1 local VALUE local PROPPATH='/data/.magisk /cache/.magisk' [ ! -z $MAGISKTMP ] && PROPPATH="$MAGISKTMP/config $PROPPATH" VALUE=$(grep_prop $VARNAME $PROPPATH) [ ! -z $VALUE ] && eval $VARNAME=\$VALUE } is_mounted() { grep -q " $(readlink -f $1) " /proc/mounts 2>/dev/null return $? } abort() { ui_print "$1" $BOOTMODE || recovery_cleanup [ ! -z $MODPATH ] && rm -rf $MODPATH rm -rf $TMPDIR exit 1 } resolve_vars() { MAGISKBIN=$NVBASE/magisk POSTFSDATAD=$NVBASE/post-fs-data.d SERVICED=$NVBASE/service.d } print_title() { local len line1len line2len pounds line1len=$(echo -n $1 | wc -c) line2len=$(echo -n $2 | wc -c) len=$line2len [ $line1len -gt $line2len ] && len=$line1len len=$((len + 2)) pounds=$(printf "%${len}s" | tr ' ' '*') ui_print "$pounds" ui_print " $1 " [ "$2" ] && ui_print " $2 " ui_print "$pounds" } ###################### # Environment Related ###################### setup_flashable() { ensure_bb $BOOTMODE && return if [ -z $OUTFD ] || readlink /proc/$$/fd/$OUTFD | grep -q /tmp; then # We will have to manually find out OUTFD for FD in `ls /proc/$$/fd`; do if readlink /proc/$$/fd/$FD | grep -q pipe; then if ps | grep -v grep | grep -qE " 3 $FD |status_fd=$FD"; then OUTFD=$FD break fi fi done fi recovery_actions } ensure_bb() { if set -o | grep -q standalone; then # We are definitely in busybox ash set -o standalone return fi # Find our busybox binary local bb if [ -f $TMPDIR/busybox ]; then bb=$TMPDIR/busybox elif [ -f $MAGISKBIN/busybox ]; then bb=$MAGISKBIN/busybox else abort "! Cannot find BusyBox" fi chmod 755 $bb # Busybox could be a script, make sure /system/bin/sh exists if [ ! -f /system/bin/sh ]; then umount -l /system 2>/dev/null mkdir -p /system/bin ln -s $(command -v sh) /system/bin/sh fi export ASH_STANDALONE=1 # Find our current arguments # Run in busybox environment to ensure consistent results # /proc//cmdline shall be