############################################ # Magisk General Utility Functions ############################################ #MAGISK_VERSION_STUB ################### # Global Variables ################### # True if the script is running on booted Android, not something like recovery # BOOTMODE= # The path to store temporary files that don't need to persist # TMPDIR= # The non-volatile path where magisk executables are stored # MAGISKBIN= ################### # Helper Functions ################### ui_print() { if $BOOTMODE; then echo "$1" else echo -e "ui_print $1\nui_print" >> /proc/self/fd/$OUTFD fi } toupper() { echo "$@" | tr '[:lower:]' '[:upper:]' } grep_cmdline() { local REGEX="s/^$1=//p" { echo $(cat /proc/cmdline)$(sed -e 's/[^"]//g' -e 's/""//g' /proc/cmdline) | xargs -n 1; \ sed -e 's/ = /=/g' -e 's/, /,/g' -e 's/"//g' /proc/bootconfig; \ } 2>/dev/null | sed -n "$REGEX" } 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 getprop "$1" else echo $result fi } getvar() { local VARNAME=$1 local VALUE local PROPPATH='/data/.magisk /cache/.magisk' [ ! -z $MAGISKTMP ] && PROPPATH="$MAGISKTMP/.magisk/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 } print_title() { local len line1len line2len bar line1len=$(echo -n $1 | wc -c) line2len=$(echo -n $2 | wc -c) len=$line2len [ $line1len -gt $line2len ] && len=$line1len len=$((len + 2)) bar=$(printf "%${len}s" | tr ' ' '*') ui_print "$bar" ui_print " $1 " [ "$2" ] && ui_print " $2 " ui_print "$bar" } ###################### # 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