Magisk/scripts/app_functions.sh

264 lines
5.7 KiB
Bash
Raw Normal View History

2020-02-10 03:36:28 -08:00
##################################
# Magisk app internal scripts
2020-02-10 03:36:28 -08:00
##################################
# $1 = delay
# $2 = command
2020-08-22 22:49:03 -07:00
run_delay() {
(sleep $1; $2)&
}
# $1 = version string
# $2 = version code
2018-05-13 18:14:10 +08:00
env_check() {
2021-01-18 04:25:26 -08:00
for file in busybox magiskboot magiskinit util_functions.sh boot_patch.sh; do
2022-03-23 00:53:06 +08:00
[ -f "$MAGISKBIN/$file" ] || return 1
2018-05-13 18:14:10 +08:00
done
2022-06-13 01:21:24 -07:00
if [ "$2" -ge 25000 ]; then
2022-03-23 00:53:06 +08:00
[ -f "$MAGISKBIN/magiskpolicy" ] || return 1
fi
2023-03-08 14:43:41 +08:00
if [ "$2" -ge 25210 ]; then
2024-01-05 01:47:15 +08:00
[ -b "$MAGISKTMP/.magisk/device/preinit" ] || [ -b "$MAGISKTMP/.magisk/block/preinit" ] || return 2
2023-03-08 14:43:41 +08:00
fi
grep -xqF "MAGISK_VER='$1'" "$MAGISKBIN/util_functions.sh" || return 3
grep -xqF "MAGISK_VER_CODE=$2" "$MAGISKBIN/util_functions.sh" || return 3
2018-05-13 18:14:10 +08:00
return 0
}
2018-06-26 00:29:01 +08:00
# $1 = dir to copy
# $2 = destination (optional)
cp_readlink() {
if [ -z $2 ]; then
cd $1
else
cp -af $1/. $2
cd $2
fi
for file in *; do
if [ -L $file ]; then
local full=$(readlink -f $file)
rm $file
cp -af $full $file
fi
done
chmod -R 755 .
2018-07-04 17:15:26 +08:00
cd /
}
# $1 = install dir
fix_env() {
# Cleanup and make dirs
rm -rf $MAGISKBIN/*
2019-02-24 02:11:11 -05:00
mkdir -p $MAGISKBIN 2>/dev/null
chmod 700 /data/adb
2021-01-29 05:15:22 -08:00
cp_readlink $1 $MAGISKBIN
rm -rf $1
chown -R 0:0 $MAGISKBIN
}
# $1 = install dir
# $2 = boot partition
direct_install() {
2018-08-10 18:59:14 +08:00
echo "- Flashing new boot image"
2018-08-13 02:57:03 +08:00
flash_image $1/new-boot.img $2
case $? in
1)
echo "! Insufficient partition size"
return 1
;;
2)
echo "! $2 is read only"
return 2
;;
esac
rm -f $1/new-boot.img
fix_env $1
2021-01-29 05:15:22 -08:00
run_migrations
return 0
2018-07-04 17:15:26 +08:00
}
# $1 = uninstaller zip
run_uninstaller() {
rm -rf /dev/tmp
mkdir -p /dev/tmp/install
unzip -o "$1" "assets/*" "lib/*" -d /dev/tmp/install
2021-01-24 07:18:14 -08:00
INSTALLER=/dev/tmp/install sh /dev/tmp/install/assets/uninstaller.sh dummy 1 "$1"
}
# $1 = boot partition
2018-06-27 05:58:56 +08:00
restore_imgs() {
2024-08-20 02:23:20 -07:00
local SHA1=$(grep_prop SHA1 $MAGISKTMP/.magisk/config)
local BACKUPDIR=/data/magisk_backup_$SHA1
[ -d $BACKUPDIR ] || return 1
[ -f $BACKUPDIR/boot.img.gz ] || return 1
flash_image $BACKUPDIR/boot.img.gz $1
2018-06-27 05:58:56 +08:00
}
# $1 = path to bootctl executable
post_ota() {
cd /data/adb
2021-01-29 05:15:22 -08:00
cp -f $1 bootctl
rm -f $1
chmod 755 bootctl
2018-12-13 06:05:19 -05:00
./bootctl hal-info || return
SLOT_NUM=0
[ $(./bootctl get-current-slot) -eq 0 ] && SLOT_NUM=1
2018-12-13 06:05:19 -05:00
./bootctl set-active-boot-slot $SLOT_NUM
2019-03-22 02:32:21 -04:00
cat << EOF > post-fs-data.d/post_ota.sh
2021-01-29 05:15:22 -08:00
/data/adb/bootctl mark-boot-successful
rm -f /data/adb/bootctl
rm -f /data/adb/post-fs-data.d/post_ota.sh
2019-03-22 02:32:21 -04:00
EOF
chmod 755 post-fs-data.d/post_ota.sh
cd /
}
add_hosts_module() {
# Do not touch existing hosts module
[ -d /data/adb/modules/hosts ] && return
cd /data/adb/modules
mkdir -p hosts/system/etc
cat << EOF > hosts/module.prop
id=hosts
name=Systemless Hosts
version=1.0
versionCode=1
author=Magisk
description=Magisk app built-in systemless hosts module
EOF
magisk --clone /system/etc/hosts hosts/system/etc/hosts
touch hosts/update
cd /
}
# $1 = APK
# $2 = package name
2020-08-15 10:58:30 +08:00
adb_pm_install() {
2020-10-17 03:40:43 -07:00
local tmp=/data/local/tmp/temp.apk
2020-08-15 10:58:30 +08:00
cp -f "$1" $tmp
chmod 644 $tmp
2022-08-23 05:09:50 -07:00
su 2000 -c pm install -g $tmp || pm install -g $tmp || su 1000 -c pm install -g $tmp
2020-08-15 10:58:30 +08:00
local res=$?
rm -f $tmp
2022-08-23 05:09:50 -07:00
if [ $res = 0 ]; then
appops set "$2" REQUEST_INSTALL_PACKAGES allow
fi
2020-08-15 10:58:30 +08:00
return $res
}
2020-02-10 16:33:58 -08:00
check_boot_ramdisk() {
# Create boolean ISAB
ISAB=true
[ -z $SLOT ] && ISAB=false
2020-02-10 16:33:58 -08:00
# If we are A/B, then we must have ramdisk
$ISAB && return 0
2020-09-23 04:40:44 -07:00
# If we are using legacy SAR, but not A/B, assume we do not have ramdisk
if $LEGACYSAR; then
# Override recovery mode to true
RECOVERYMODE=true
2020-02-10 16:33:58 -08:00
return 1
fi
return 0
}
2020-09-23 04:40:44 -07:00
check_encryption() {
if $ISENCRYPTED; then
if [ $SDK_INT -lt 24 ]; then
CRYPTOTYPE="block"
else
# First see what the system tells us
CRYPTOTYPE=$(getprop ro.crypto.type)
if [ -z $CRYPTOTYPE ]; then
# If not mounting through device mapper, we are FBE
if grep ' /data ' /proc/mounts | grep -qv 'dm-'; then
CRYPTOTYPE="file"
else
# We are either FDE or metadata encryption (which is also FBE)
CRYPTOTYPE="block"
grep -q ' /metadata ' /proc/mounts && CRYPTOTYPE="file"
2020-09-23 04:40:44 -07:00
fi
fi
fi
else
CRYPTOTYPE="N/A"
fi
}
2024-07-01 18:20:21 -07:00
printvar() {
eval echo $1=\$$1
}
run_action() {
local MODID="$1"
cd "/data/adb/modules/$MODID"
2024-09-01 23:55:27 +08:00
sh ./action.sh
local RES=$?
cd /
2024-09-01 23:55:27 +08:00
return $RES
}
2020-02-10 03:36:28 -08:00
##########################
# Non-root util_functions
##########################
mount_partitions() {
2020-09-23 04:40:44 -07:00
[ "$(getprop ro.build.ab_update)" = "true" ] && SLOT=$(getprop ro.boot.slot_suffix)
2020-02-10 03:36:28 -08:00
# Check whether non rootfs root dir exists
2023-08-28 22:13:24 -07:00
SYSTEM_AS_ROOT=false
grep ' / ' /proc/mounts | grep -qv 'rootfs' && SYSTEM_AS_ROOT=true
LEGACYSAR=false
grep ' / ' /proc/mounts | grep -q '/dev/root' && LEGACYSAR=true
2020-02-10 03:36:28 -08:00
}
get_flags() {
2023-08-28 22:13:24 -07:00
KEEPVERITY=$SYSTEM_AS_ROOT
ISENCRYPTED=false
[ "$(getprop ro.crypto.state)" = "encrypted" ] && ISENCRYPTED=true
2020-09-23 04:40:44 -07:00
KEEPFORCEENCRYPT=$ISENCRYPTED
if [ -n "$(getprop ro.boot.vbmeta.device)" -o -n "$(getprop ro.boot.vbmeta.size)" ]; then
2023-08-28 22:13:24 -07:00
PATCHVBMETAFLAG=false
elif getprop ro.product.ab_ota_partitions | grep -wq vbmeta; then
2023-08-28 22:13:24 -07:00
PATCHVBMETAFLAG=false
else
PATCHVBMETAFLAG=true
2022-01-12 02:21:26 -08:00
fi
2022-06-13 01:21:24 -07:00
[ -z $RECOVERYMODE ] && RECOVERYMODE=false
2020-02-10 03:36:28 -08:00
}
run_migrations() { return; }
grep_prop() { return; }
#############
# Initialize
#############
2021-01-29 05:15:22 -08:00
app_init() {
2024-07-01 18:20:21 -07:00
mount_partitions >/dev/null
RAMDISKEXIST=false
check_boot_ramdisk && RAMDISKEXIST=true
2024-07-01 18:20:21 -07:00
get_flags >/dev/null
run_migrations >/dev/null
2020-09-23 04:40:44 -07:00
check_encryption
2024-07-01 18:20:21 -07:00
# Dump variables
printvar SLOT
printvar SYSTEM_AS_ROOT
printvar RAMDISKEXIST
printvar ISAB
printvar CRYPTOTYPE
printvar PATCHVBMETAFLAG
printvar LEGACYSAR
printvar RECOVERYMODE
printvar KEEPVERITY
printvar KEEPFORCEENCRYPT
}
2021-01-29 05:15:22 -08:00
export BOOTMODE=true