2022-01-19 13:12:11 +00:00
|
|
|
#####################################################################
|
|
|
|
# AVD MagiskInit Setup
|
|
|
|
#####################################################################
|
|
|
|
#
|
2023-08-11 16:46:45 +00:00
|
|
|
# Support API level: 23 - 34
|
2022-01-19 13:12:11 +00:00
|
|
|
#
|
|
|
|
# With an emulator booted and accessible via ADB, usage:
|
|
|
|
# ./build.py avd_patch path/to/booted/avd-image/ramdisk.img
|
|
|
|
#
|
|
|
|
# The purpose of this script is to patch AVD ramdisk.img and do a
|
|
|
|
# full integration test of magiskinit under several circumstances.
|
|
|
|
# After patching ramdisk.img, close the emulator, then select
|
|
|
|
# "Cold Boot Now" in AVD Manager to force a full reboot.
|
|
|
|
#
|
|
|
|
#####################################################################
|
|
|
|
# AVD Init Configurations:
|
|
|
|
#
|
|
|
|
# rootfs w/o early mount: API 23 - 25
|
|
|
|
# rootfs with early mount: API 26 - 27
|
|
|
|
# Legacy system-as-root: API 28
|
2023-08-11 16:46:45 +00:00
|
|
|
# 2 stage init: API 29 - 34
|
2022-01-19 13:12:11 +00:00
|
|
|
#####################################################################
|
|
|
|
|
|
|
|
if [ ! -f /system/build.prop ]; then
|
|
|
|
# Running on PC
|
|
|
|
echo 'Please run `./build.py avd_patch` instead of directly executing the script!'
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
cd /data/local/tmp
|
|
|
|
chmod 755 busybox
|
|
|
|
|
|
|
|
if [ -z "$FIRST_STAGE" ]; then
|
|
|
|
export FIRST_STAGE=1
|
|
|
|
export ASH_STANDALONE=1
|
|
|
|
# Re-exec script with busybox
|
2024-07-19 23:47:00 +00:00
|
|
|
exec ./busybox sh $0 "$@"
|
|
|
|
fi
|
|
|
|
|
|
|
|
TARGET_FILE="$1"
|
|
|
|
OUTPUT_FILE="$1.magisk"
|
|
|
|
|
|
|
|
if echo "$TARGET_FILE" | grep -q 'ramdisk'; then
|
|
|
|
IS_RAMDISK=true
|
|
|
|
else
|
|
|
|
IS_RAMDISK=false
|
2022-01-19 13:12:11 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
# Extract files from APK
|
2022-10-31 14:31:15 +00:00
|
|
|
unzip -oj magisk.apk 'assets/util_functions.sh' 'assets/stub.apk'
|
2022-01-19 13:12:11 +00:00
|
|
|
. ./util_functions.sh
|
|
|
|
|
|
|
|
api_level_arch_detect
|
|
|
|
|
2024-04-01 05:01:22 +00:00
|
|
|
unzip -oj magisk.apk "lib/$ABI/*" -x "lib/$ABI/libbusybox.so"
|
2022-01-19 13:12:11 +00:00
|
|
|
for file in lib*.so; do
|
|
|
|
chmod 755 $file
|
|
|
|
mv "$file" "${file:3:${#file}-6}"
|
|
|
|
done
|
|
|
|
|
2024-07-19 23:47:00 +00:00
|
|
|
if $IS_RAMDISK; then
|
|
|
|
./magiskboot decompress "$TARGET_FILE" ramdisk.cpio
|
|
|
|
else
|
|
|
|
./magiskboot unpack "$TARGET_FILE"
|
|
|
|
fi
|
2022-01-19 13:12:11 +00:00
|
|
|
cp ramdisk.cpio ramdisk.cpio.orig
|
|
|
|
|
2023-04-24 20:54:46 +00:00
|
|
|
export KEEPVERITY=true
|
2023-03-08 06:42:54 +00:00
|
|
|
export KEEPFORCEENCRYPT=true
|
2023-02-12 08:36:38 +00:00
|
|
|
|
2023-03-08 06:42:54 +00:00
|
|
|
echo "KEEPVERITY=$KEEPVERITY" > config
|
|
|
|
echo "KEEPFORCEENCRYPT=$KEEPFORCEENCRYPT" >> config
|
2024-04-01 05:01:22 +00:00
|
|
|
echo "PREINITDEVICE=$(./magisk --preinit-device)" >> config
|
2024-07-25 05:49:48 +00:00
|
|
|
# For API 28, we also manually disable SystemAsRoot
|
|
|
|
# Explicitly override skip_initramfs by setting RECOVERYMODE=true
|
2022-01-19 13:12:11 +00:00
|
|
|
[ $API = "28" ] && echo 'RECOVERYMODE=true' >> config
|
2023-04-24 20:54:46 +00:00
|
|
|
cat config
|
2022-01-19 13:12:11 +00:00
|
|
|
|
2024-04-01 05:01:22 +00:00
|
|
|
./magiskboot compress=xz magisk magisk.xz
|
2022-10-31 14:31:15 +00:00
|
|
|
./magiskboot compress=xz stub.apk stub.xz
|
2024-07-25 05:49:48 +00:00
|
|
|
./magiskboot compress=xz init-ld init-ld.xz
|
2022-01-19 13:12:11 +00:00
|
|
|
|
|
|
|
./magiskboot cpio ramdisk.cpio \
|
|
|
|
"add 0750 init magiskinit" \
|
|
|
|
"mkdir 0750 overlay.d" \
|
|
|
|
"mkdir 0750 overlay.d/sbin" \
|
2024-04-01 05:01:22 +00:00
|
|
|
"add 0644 overlay.d/sbin/magisk.xz magisk.xz" \
|
2022-10-31 14:31:15 +00:00
|
|
|
"add 0644 overlay.d/sbin/stub.xz stub.xz" \
|
2024-07-25 05:49:48 +00:00
|
|
|
"add 0644 overlay.d/sbin/init-ld.xz init-ld.xz" \
|
2022-01-19 13:12:11 +00:00
|
|
|
"patch" \
|
|
|
|
"backup ramdisk.cpio.orig" \
|
|
|
|
"mkdir 000 .backup" \
|
|
|
|
"add 000 .backup/.magisk config"
|
|
|
|
|
2024-07-25 05:49:48 +00:00
|
|
|
rm -f ramdisk.cpio.orig config *.xz
|
2024-07-19 23:47:00 +00:00
|
|
|
if $IS_RAMDISK; then
|
|
|
|
./magiskboot compress=gzip ramdisk.cpio "$OUTPUT_FILE"
|
|
|
|
else
|
|
|
|
./magiskboot repack "$TARGET_FILE" "$OUTPUT_FILE"
|
|
|
|
./magiskboot cleanup
|
|
|
|
fi
|