2020-03-20 10:44:08 +00:00
|
|
|
#####################################################################
|
2019-09-03 21:06:14 +00:00
|
|
|
# AVD Magisk Setup
|
2020-03-20 10:44:08 +00:00
|
|
|
#####################################################################
|
2019-09-03 21:06:14 +00:00
|
|
|
#
|
2023-08-11 16:46:45 +00:00
|
|
|
# Support API level: 23 - 34
|
2019-09-03 21:06:14 +00:00
|
|
|
#
|
2022-01-19 13:12:11 +00:00
|
|
|
# With an emulator booted and accessible via ADB, usage:
|
|
|
|
# ./build.py emulator
|
|
|
|
#
|
2021-08-26 10:09:56 +00:00
|
|
|
# This script will stop zygote, simulate the Magisk start up process
|
|
|
|
# that would've happened before zygote was started, and finally
|
|
|
|
# restart zygote. This is useful for setting up the emulator for
|
|
|
|
# developing Magisk, testing modules, and developing root apps using
|
|
|
|
# the official Android emulator (AVD) instead of a real device.
|
2019-09-03 21:06:14 +00:00
|
|
|
#
|
2022-01-19 13:12:11 +00:00
|
|
|
# This only covers the "core" features of Magisk. For testing
|
2022-01-20 04:28:01 +00:00
|
|
|
# magiskinit, please checkout avd_patch.sh.
|
2021-08-26 10:09:56 +00:00
|
|
|
#
|
2020-03-20 10:44:08 +00:00
|
|
|
#####################################################################
|
2019-09-03 21:06:14 +00:00
|
|
|
|
2019-09-04 15:12:09 +00:00
|
|
|
mount_sbin() {
|
2023-02-28 06:54:33 +00:00
|
|
|
mount -t tmpfs -o 'mode=0755' magisk /sbin
|
2021-08-18 10:44:32 +00:00
|
|
|
chcon u:object_r:rootfs:s0 /sbin
|
2019-09-04 15:12:09 +00:00
|
|
|
}
|
|
|
|
|
2019-09-03 21:06:14 +00:00
|
|
|
if [ ! -f /system/build.prop ]; then
|
|
|
|
# Running on PC
|
2021-09-07 10:03:02 +00:00
|
|
|
echo 'Please run `./build.py emulator` instead of directly executing the script!'
|
|
|
|
exit 1
|
2019-09-03 21:06:14 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
cd /data/local/tmp
|
2021-08-26 10:09:56 +00:00
|
|
|
chmod 755 busybox
|
2019-09-03 21:06:14 +00:00
|
|
|
|
2020-07-27 10:32:54 +00:00
|
|
|
if [ -z "$FIRST_STAGE" ]; then
|
|
|
|
export FIRST_STAGE=1
|
|
|
|
export ASH_STANDALONE=1
|
2021-09-07 10:03:02 +00:00
|
|
|
if [ $(./busybox id -u) -ne 0 ]; then
|
2020-07-27 10:32:54 +00:00
|
|
|
# Re-exec script with root
|
|
|
|
exec /system/xbin/su 0 ./busybox sh $0
|
|
|
|
else
|
|
|
|
# Re-exec script with busybox
|
|
|
|
exec ./busybox sh $0
|
|
|
|
fi
|
2019-10-21 17:58:57 +00:00
|
|
|
fi
|
2019-09-03 21:06:14 +00:00
|
|
|
|
2023-10-19 12:15:53 +00:00
|
|
|
pm install -r -g $(pwd)/magisk.apk
|
2021-08-26 10:09:56 +00:00
|
|
|
|
|
|
|
# Extract files from APK
|
2022-12-26 23:59:56 +00:00
|
|
|
unzip -oj magisk.apk 'assets/util_functions.sh' 'assets/stub.apk'
|
2021-09-07 10:03:02 +00:00
|
|
|
. ./util_functions.sh
|
|
|
|
|
|
|
|
api_level_arch_detect
|
|
|
|
|
2022-08-02 10:37:04 +00:00
|
|
|
unzip -oj magisk.apk "lib/$ABI/*" "lib/$ABI32/libmagisk32.so" -x "lib/$ABI/libbusybox.so"
|
2021-08-26 10:09:56 +00:00
|
|
|
for file in lib*.so; do
|
|
|
|
chmod 755 $file
|
|
|
|
mv "$file" "${file:3:${#file}-6}"
|
|
|
|
done
|
|
|
|
|
|
|
|
# Stop zygote (and previous setup if exists)
|
|
|
|
magisk --stop 2>/dev/null
|
|
|
|
stop
|
2023-10-29 07:47:28 +00:00
|
|
|
if [ -d /debug_ramdisk ]; then
|
|
|
|
umount -l /debug_ramdisk 2>/dev/null
|
2021-08-18 10:44:32 +00:00
|
|
|
fi
|
2019-10-21 17:58:57 +00:00
|
|
|
|
2023-09-28 22:37:11 +00:00
|
|
|
# Make sure boot completed props are not set to 1
|
|
|
|
setprop sys.boot_completed 0
|
|
|
|
|
2023-04-04 09:04:49 +00:00
|
|
|
# Mount /cache if not already mounted
|
|
|
|
if ! grep -q ' /cache ' /proc/mounts; then
|
|
|
|
mount -t tmpfs -o 'mode=0755' tmpfs /cache
|
2021-08-26 10:09:56 +00:00
|
|
|
fi
|
2019-09-03 21:06:14 +00:00
|
|
|
|
2021-08-26 10:09:56 +00:00
|
|
|
MAGISKTMP=/sbin
|
2020-07-27 10:32:54 +00:00
|
|
|
|
|
|
|
# Setup bin overlay
|
2019-09-04 15:12:09 +00:00
|
|
|
if mount | grep -q rootfs; then
|
2020-07-27 10:32:54 +00:00
|
|
|
# Legacy rootfs
|
2019-09-04 15:12:09 +00:00
|
|
|
mount -o rw,remount /
|
|
|
|
rm -rf /root
|
|
|
|
mkdir /root
|
|
|
|
chmod 750 /root
|
|
|
|
ln /sbin/* /root
|
|
|
|
mount -o ro,remount /
|
|
|
|
mount_sbin
|
|
|
|
ln -s /root/* /sbin
|
2020-07-27 10:32:54 +00:00
|
|
|
elif [ -e /sbin ]; then
|
|
|
|
# Legacy SAR
|
2019-09-04 15:12:09 +00:00
|
|
|
mount_sbin
|
2021-08-26 10:09:56 +00:00
|
|
|
mkdir -p /dev/sysroot
|
|
|
|
block=$(mount | grep ' / ' | awk '{ print $1 }')
|
2023-11-01 16:15:17 +00:00
|
|
|
[ $block = "/dev/root" ] && block=/dev/block/vda1
|
2021-08-26 10:09:56 +00:00
|
|
|
mount -o ro $block /dev/sysroot
|
|
|
|
for file in /dev/sysroot/sbin/*; do
|
2019-09-04 15:12:09 +00:00
|
|
|
[ ! -e $file ] && break
|
|
|
|
if [ -L $file ]; then
|
|
|
|
cp -af $file /sbin
|
|
|
|
else
|
2021-04-10 04:29:42 +00:00
|
|
|
sfile=/sbin/$(basename $file)
|
2019-09-04 15:12:09 +00:00
|
|
|
touch $sfile
|
|
|
|
mount -o bind $file $sfile
|
|
|
|
fi
|
|
|
|
done
|
2021-08-26 10:09:56 +00:00
|
|
|
umount -l /dev/sysroot
|
|
|
|
rm -rf /dev/sysroot
|
2020-07-27 10:32:54 +00:00
|
|
|
else
|
2021-08-26 10:09:56 +00:00
|
|
|
# Android Q+ without sbin
|
2023-10-29 07:47:28 +00:00
|
|
|
MAGISKTMP=/debug_ramdisk
|
2023-05-10 00:33:20 +00:00
|
|
|
# If a file name 'magisk' is in current directory, mount will fail
|
2023-08-02 09:49:18 +00:00
|
|
|
rm -f magisk
|
2023-10-29 07:47:28 +00:00
|
|
|
mount -t tmpfs -o 'mode=0755' magisk /debug_ramdisk
|
2019-09-04 15:12:09 +00:00
|
|
|
fi
|
|
|
|
|
2021-11-18 02:53:33 +00:00
|
|
|
# Magisk stuff
|
|
|
|
mkdir -p $MAGISKBIN 2>/dev/null
|
2022-08-02 10:37:04 +00:00
|
|
|
unzip -oj magisk.apk 'assets/*.sh' -d $MAGISKBIN
|
2021-11-18 02:53:33 +00:00
|
|
|
mkdir $NVBASE/modules 2>/dev/null
|
2023-07-17 23:07:16 +00:00
|
|
|
mkdir $NVBASE/post-fs-data.d 2>/dev/null
|
|
|
|
mkdir $NVBASE/service.d 2>/dev/null
|
2021-08-26 10:09:56 +00:00
|
|
|
|
2022-12-26 23:59:56 +00:00
|
|
|
for file in magisk32 magisk64 magiskpolicy stub.apk; do
|
2021-08-26 10:09:56 +00:00
|
|
|
chmod 755 ./$file
|
|
|
|
cp -af ./$file $MAGISKTMP/$file
|
2021-11-18 02:53:33 +00:00
|
|
|
cp -af ./$file $MAGISKBIN/$file
|
2021-08-26 10:09:56 +00:00
|
|
|
done
|
2021-11-18 02:53:33 +00:00
|
|
|
cp -af ./magiskboot $MAGISKBIN/magiskboot
|
2022-03-17 10:15:39 +00:00
|
|
|
cp -af ./magiskinit $MAGISKBIN/magiskinit
|
2021-11-18 02:53:33 +00:00
|
|
|
cp -af ./busybox $MAGISKBIN/busybox
|
2021-08-26 10:09:56 +00:00
|
|
|
|
2022-08-02 10:37:04 +00:00
|
|
|
if $IS64BIT; then
|
2022-07-21 08:55:27 +00:00
|
|
|
ln -s ./magisk64 $MAGISKTMP/magisk
|
|
|
|
else
|
|
|
|
ln -s ./magisk32 $MAGISKTMP/magisk
|
|
|
|
fi
|
2021-08-26 10:09:56 +00:00
|
|
|
ln -s ./magisk $MAGISKTMP/su
|
|
|
|
ln -s ./magisk $MAGISKTMP/resetprop
|
2022-03-17 10:15:39 +00:00
|
|
|
ln -s ./magiskpolicy $MAGISKTMP/supolicy
|
2021-08-26 10:09:56 +00:00
|
|
|
|
2024-01-04 17:47:15 +00:00
|
|
|
mkdir -p $MAGISKTMP/.magisk/device
|
2021-08-26 10:09:56 +00:00
|
|
|
touch $MAGISKTMP/.magisk/config
|
|
|
|
|
2023-04-01 00:20:32 +00:00
|
|
|
export MAGISKTMP
|
2023-04-04 09:04:49 +00:00
|
|
|
MAKEDEV=1 $MAGISKTMP/magisk --preinit-device 2>&1
|
|
|
|
|
|
|
|
RULESCMD=""
|
|
|
|
for r in $MAGISKTMP/.magisk/preinit/*/sepolicy.rule; do
|
|
|
|
[ -f "$r" ] || continue
|
|
|
|
RULESCMD="$RULESCMD --apply $r"
|
|
|
|
done
|
|
|
|
|
|
|
|
# SELinux stuffs
|
|
|
|
if [ -d /sys/fs/selinux ]; then
|
|
|
|
if [ -f /vendor/etc/selinux/precompiled_sepolicy ]; then
|
|
|
|
./magiskpolicy --load /vendor/etc/selinux/precompiled_sepolicy --live --magisk $RULESCMD 2>&1
|
|
|
|
elif [ -f /sepolicy ]; then
|
|
|
|
./magiskpolicy --load /sepolicy --live --magisk $RULESCMD 2>&1
|
|
|
|
else
|
|
|
|
./magiskpolicy --live --magisk $RULESCMD 2>&1
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Boot up
|
2021-08-26 10:09:56 +00:00
|
|
|
$MAGISKTMP/magisk --post-fs-data
|
|
|
|
start
|
|
|
|
$MAGISKTMP/magisk --service
|