2020-02-10 11:36:28 +00:00
|
|
|
##################################
|
2021-03-21 22:25:56 +00:00
|
|
|
# Magisk app internal scripts
|
2020-02-10 11:36:28 +00:00
|
|
|
##################################
|
|
|
|
|
2020-08-23 05:49:03 +00:00
|
|
|
run_delay() {
|
|
|
|
(sleep $1; $2)&
|
|
|
|
}
|
|
|
|
|
2018-05-13 10:14:10 +00:00
|
|
|
env_check() {
|
2021-01-18 12:25:26 +00:00
|
|
|
for file in busybox magiskboot magiskinit util_functions.sh boot_patch.sh; do
|
2022-03-22 16:53:06 +00:00
|
|
|
[ -f "$MAGISKBIN/$file" ] || return 1
|
2018-05-13 10:14:10 +00:00
|
|
|
done
|
2022-06-13 08:21:24 +00:00
|
|
|
if [ "$2" -ge 25000 ]; then
|
2022-03-22 16:53:06 +00:00
|
|
|
[ -f "$MAGISKBIN/magiskpolicy" ] || return 1
|
|
|
|
fi
|
2023-03-08 06:43:41 +00:00
|
|
|
if [ "$2" -ge 25210 ]; then
|
2023-03-16 11:07:00 +00:00
|
|
|
[ -b "$MAGISKTMP/.magisk/block/preinit" ] || return 2
|
2023-03-08 06:43:41 +00: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 10:14:10 +00:00
|
|
|
return 0
|
|
|
|
}
|
2018-06-25 16:29:01 +00:00
|
|
|
|
2021-01-22 10:28:53 +00:00
|
|
|
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 09:15:26 +00:00
|
|
|
cd /
|
|
|
|
}
|
|
|
|
|
2021-01-22 10:28:53 +00:00
|
|
|
fix_env() {
|
|
|
|
# Cleanup and make dirs
|
|
|
|
rm -rf $MAGISKBIN/*
|
2019-02-24 07:11:11 +00:00
|
|
|
mkdir -p $MAGISKBIN 2>/dev/null
|
|
|
|
chmod 700 $NVBASE
|
2023-03-08 06:43:41 +00:00
|
|
|
rm $1/stub.apk 2>/dev/null
|
2021-01-29 13:15:22 +00:00
|
|
|
cp_readlink $1 $MAGISKBIN
|
2021-01-22 10:28:53 +00:00
|
|
|
rm -rf $1
|
|
|
|
chown -R 0:0 $MAGISKBIN
|
|
|
|
}
|
|
|
|
|
|
|
|
direct_install() {
|
2018-08-10 10:59:14 +00:00
|
|
|
echo "- Flashing new boot image"
|
2018-08-12 18:57:03 +00:00
|
|
|
flash_image $1/new-boot.img $2
|
2020-12-19 22:12:12 +00:00
|
|
|
case $? in
|
|
|
|
1)
|
|
|
|
echo "! Insufficient partition size"
|
|
|
|
return 1
|
|
|
|
;;
|
|
|
|
2)
|
|
|
|
echo "! $2 is read only"
|
|
|
|
return 2
|
|
|
|
;;
|
|
|
|
esac
|
2021-01-22 10:28:53 +00:00
|
|
|
|
|
|
|
rm -f $1/new-boot.img
|
|
|
|
fix_env $1
|
2021-01-29 13:15:22 +00:00
|
|
|
run_migrations
|
2023-03-16 11:07:00 +00:00
|
|
|
copy_preinit_files
|
2021-01-22 10:28:53 +00:00
|
|
|
|
2018-08-11 07:56:12 +00:00
|
|
|
return 0
|
2018-07-04 09:15:26 +00:00
|
|
|
}
|
|
|
|
|
2021-01-22 10:28:53 +00:00
|
|
|
run_uninstaller() {
|
|
|
|
rm -rf /dev/tmp
|
|
|
|
mkdir -p /dev/tmp/install
|
|
|
|
unzip -o "$1" "assets/*" "lib/*" -d /dev/tmp/install
|
2021-01-24 15:18:14 +00:00
|
|
|
INSTALLER=/dev/tmp/install sh /dev/tmp/install/assets/uninstaller.sh dummy 1 "$1"
|
2021-01-22 10:28:53 +00:00
|
|
|
}
|
|
|
|
|
2018-06-26 21:58:56 +00:00
|
|
|
restore_imgs() {
|
|
|
|
[ -z $SHA1 ] && return 1
|
2020-01-01 06:02:44 +00:00
|
|
|
local BACKUPDIR=/data/magisk_backup_$SHA1
|
|
|
|
[ -d $BACKUPDIR ] || return 1
|
2018-06-26 21:58:56 +00:00
|
|
|
|
2019-05-08 23:06:29 +00:00
|
|
|
get_flags
|
2018-06-26 21:58:56 +00:00
|
|
|
find_boot_image
|
|
|
|
|
2020-01-01 06:02:44 +00:00
|
|
|
for name in dtb dtbo; do
|
|
|
|
[ -f $BACKUPDIR/${name}.img.gz ] || continue
|
2020-09-23 11:40:44 +00:00
|
|
|
local IMAGE=$(find_block $name$SLOT)
|
2020-01-01 06:02:44 +00:00
|
|
|
[ -z $IMAGE ] && continue
|
|
|
|
flash_image $BACKUPDIR/${name}.img.gz $IMAGE
|
|
|
|
done
|
|
|
|
[ -f $BACKUPDIR/boot.img.gz ] || return 1
|
|
|
|
flash_image $BACKUPDIR/boot.img.gz $BOOTIMAGE
|
2018-06-26 21:58:56 +00:00
|
|
|
}
|
2018-07-20 17:59:28 +00:00
|
|
|
|
|
|
|
post_ota() {
|
2021-11-18 02:53:33 +00:00
|
|
|
cd $NVBASE
|
2021-01-29 13:15:22 +00:00
|
|
|
cp -f $1 bootctl
|
|
|
|
rm -f $1
|
2018-07-20 17:59:28 +00:00
|
|
|
chmod 755 bootctl
|
2018-12-13 11:05:19 +00:00
|
|
|
./bootctl hal-info || return
|
2022-06-12 12:56:03 +00:00
|
|
|
SLOT_NUM=0
|
|
|
|
[ $(./bootctl get-current-slot) -eq 0 ] && SLOT_NUM=1
|
2018-12-13 11:05:19 +00:00
|
|
|
./bootctl set-active-boot-slot $SLOT_NUM
|
2019-03-22 06:32:21 +00:00
|
|
|
cat << EOF > post-fs-data.d/post_ota.sh
|
2021-01-29 13:15:22 +00: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 06:32:21 +00:00
|
|
|
EOF
|
2018-07-20 17:59:28 +00:00
|
|
|
chmod 755 post-fs-data.d/post_ota.sh
|
|
|
|
cd /
|
|
|
|
}
|
2018-11-15 18:57:41 +00:00
|
|
|
|
|
|
|
add_hosts_module() {
|
|
|
|
# Do not touch existing hosts module
|
2020-04-05 08:27:07 +00:00
|
|
|
[ -d $MAGISKTMP/modules/hosts ] && return
|
|
|
|
cd $MAGISKTMP/modules
|
2018-11-15 18:57:41 +00:00
|
|
|
mkdir -p hosts/system/etc
|
|
|
|
cat << EOF > hosts/module.prop
|
|
|
|
id=hosts
|
|
|
|
name=Systemless Hosts
|
|
|
|
version=1.0
|
|
|
|
versionCode=1
|
2021-03-21 22:25:56 +00:00
|
|
|
author=Magisk
|
|
|
|
description=Magisk app built-in systemless hosts module
|
2018-11-15 18:57:41 +00:00
|
|
|
EOF
|
2020-04-05 08:27:07 +00:00
|
|
|
magisk --clone /system/etc/hosts hosts/system/etc/hosts
|
2018-11-15 18:57:41 +00:00
|
|
|
touch hosts/update
|
|
|
|
cd /
|
|
|
|
}
|
2019-10-24 16:23:03 +00:00
|
|
|
|
2020-08-15 02:58:30 +00:00
|
|
|
adb_pm_install() {
|
2020-10-17 10:40:43 +00:00
|
|
|
local tmp=/data/local/tmp/temp.apk
|
2020-08-15 02:58:30 +00:00
|
|
|
cp -f "$1" $tmp
|
|
|
|
chmod 644 $tmp
|
2022-08-23 12:09:50 +00:00
|
|
|
su 2000 -c pm install -g $tmp || pm install -g $tmp || su 1000 -c pm install -g $tmp
|
2020-08-15 02:58:30 +00:00
|
|
|
local res=$?
|
|
|
|
rm -f $tmp
|
2022-08-23 12:09:50 +00:00
|
|
|
if [ $res = 0 ]; then
|
|
|
|
appops set "$2" REQUEST_INSTALL_PACKAGES allow
|
|
|
|
fi
|
2020-08-15 02:58:30 +00:00
|
|
|
return $res
|
|
|
|
}
|
|
|
|
|
2020-02-11 00:33:58 +00:00
|
|
|
check_boot_ramdisk() {
|
|
|
|
# Create boolean ISAB
|
2022-06-12 12:56:03 +00:00
|
|
|
ISAB=true
|
|
|
|
[ -z $SLOT ] && ISAB=false
|
2020-02-11 00:33:58 +00:00
|
|
|
|
|
|
|
# If we are A/B, then we must have ramdisk
|
|
|
|
$ISAB && return 0
|
|
|
|
|
2020-09-23 11:40:44 +00:00
|
|
|
# If we are using legacy SAR, but not A/B, assume we do not have ramdisk
|
2020-02-11 00:33:58 +00:00
|
|
|
if grep ' / ' /proc/mounts | grep -q '/dev/root'; then
|
2022-06-12 07:32:34 +00:00
|
|
|
# Override recovery mode to true
|
|
|
|
RECOVERYMODE=true
|
2020-02-11 00:33:58 +00:00
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2020-09-23 11:40:44 +00: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)
|
2022-06-12 12:56:03 +00:00
|
|
|
CRYPTOTYPE="block"
|
|
|
|
grep -q ' /metadata ' /proc/mounts && CRYPTOTYPE="file"
|
2020-09-23 11:40:44 +00:00
|
|
|
fi
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
CRYPTOTYPE="N/A"
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2020-02-10 11:36:28 +00:00
|
|
|
##########################
|
|
|
|
# Non-root util_functions
|
|
|
|
##########################
|
|
|
|
|
|
|
|
mount_partitions() {
|
2020-09-23 11:40:44 +00:00
|
|
|
[ "$(getprop ro.build.ab_update)" = "true" ] && SLOT=$(getprop ro.boot.slot_suffix)
|
2020-02-10 11:36:28 +00:00
|
|
|
# Check whether non rootfs root dir exists
|
2022-06-12 12:56:03 +00:00
|
|
|
SYSTEM_ROOT=false
|
|
|
|
grep ' / ' /proc/mounts | grep -qv 'rootfs' && SYSTEM_ROOT=true
|
2020-02-10 11:36:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
get_flags() {
|
2020-09-23 11:40:44 +00:00
|
|
|
KEEPVERITY=$SYSTEM_ROOT
|
2022-06-12 12:56:03 +00:00
|
|
|
ISENCRYPTED=false
|
|
|
|
[ "$(getprop ro.crypto.state)" = "encrypted" ] && ISENCRYPTED=true
|
2020-09-23 11:40:44 +00:00
|
|
|
KEEPFORCEENCRYPT=$ISENCRYPTED
|
2022-01-12 10:21:26 +00:00
|
|
|
# Although this most certainly won't work without root, keep it just in case
|
|
|
|
if [ -e /dev/block/by-name/vbmeta_a ] || [ -e /dev/block/by-name/vbmeta ]; then
|
|
|
|
VBMETAEXIST=true
|
|
|
|
else
|
|
|
|
VBMETAEXIST=false
|
|
|
|
fi
|
|
|
|
# Preset PATCHVBMETAFLAG to false in the non-root case
|
|
|
|
PATCHVBMETAFLAG=false
|
2022-06-13 08:21:24 +00:00
|
|
|
# Make sure RECOVERYMODE has value
|
|
|
|
[ -z $RECOVERYMODE ] && RECOVERYMODE=false
|
2020-02-10 11:36:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
run_migrations() { return; }
|
2020-04-05 08:27:07 +00:00
|
|
|
|
|
|
|
grep_prop() { return; }
|
|
|
|
|
|
|
|
#############
|
|
|
|
# Initialize
|
|
|
|
#############
|
|
|
|
|
2021-01-29 13:15:22 +00:00
|
|
|
app_init() {
|
2020-04-05 08:27:07 +00:00
|
|
|
mount_partitions
|
2022-06-12 07:32:34 +00:00
|
|
|
RAMDISKEXIST=false
|
|
|
|
check_boot_ramdisk && RAMDISKEXIST=true
|
2020-04-05 08:27:07 +00:00
|
|
|
get_flags
|
|
|
|
run_migrations
|
|
|
|
SHA1=$(grep_prop SHA1 $MAGISKTMP/config)
|
2020-09-23 11:40:44 +00:00
|
|
|
check_encryption
|
2020-04-05 08:27:07 +00:00
|
|
|
}
|
2021-01-29 13:15:22 +00:00
|
|
|
|
|
|
|
export BOOTMODE=true
|