Refactor adb patching and emulator setup

Add support for patching boot images directly via adb
with the build.py script.
This commit is contained in:
topjohnwu
2026-09-21 01:40:07 -07:00
parent 89d3100ffc
commit aed0261c3d
8 changed files with 123 additions and 91 deletions
+37
View File
@@ -0,0 +1,37 @@
#####################################################################
# ADB Magisk Boot Image Patcher
#####################################################################
#
# With a device accessible via ADB, usage:
# ./build.py patch path/to/boot.img
#
#####################################################################
if [ ! -f /system/build.prop ]; then
# Running on PC
echo 'Please run `./build.py patch` instead of directly executing the script!'
exit 1
fi
cd /data/local/tmp
chmod 755 busybox magisk*
if [ -z "$FIRST_STAGE" ]; then
export FIRST_STAGE=1
export ASH_STANDALONE=1
# Re-exec script with busybox
exec ./busybox sh $0 "$@"
fi
TARGET_FILE="$1"
OUTPUT_FILE="$1.magisk"
# Fetch env flags using non-root functions in app_functions
. ./app_functions.sh
mount_partitions
get_flags
# Patch the boot image to the target path
sh boot_patch.sh $TARGET_FILE
mv new-boot.img $OUTPUT_FILE
./magiskboot cleanup
+1 -1
View File
@@ -249,7 +249,7 @@ test_main() {
local images=()
for apk in "${apks[@]}"; do
images+=("magisk-$(basename $apk .apk).img")
./build.py -v avd_patch --apk "$apk" "$ramdisk" "${images[-1]}"
./build.py -v patch --avd --apk "$apk" "$ramdisk" "${images[-1]}"
done
adb emu kill
+7 -33
View File
@@ -5,7 +5,7 @@
# Support API level: 23 - 36
#
# With an emulator booted and accessible via ADB, usage:
# ./build.py avd_patch path/to/booted/avd-image/ramdisk.img
# ./build.py patch --avd 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.
@@ -23,12 +23,12 @@
if [ ! -f /system/build.prop ]; then
# Running on PC
echo 'Please run `./build.py avd_patch` instead of directly executing the script!'
echo 'Please run `./build.py patch --avd` instead of directly executing the script!'
exit 1
fi
cd /data/local/tmp
chmod 755 busybox
chmod 755 busybox magisk*
if [ -z "$FIRST_STAGE" ]; then
export FIRST_STAGE=1
@@ -40,29 +40,7 @@ fi
TARGET_FILE="$1"
OUTPUT_FILE="$1.magisk"
if echo "$TARGET_FILE" | grep -q 'ramdisk'; then
IS_RAMDISK=true
else
IS_RAMDISK=false
fi
# Extract files from APK
unzip -oj magisk.apk 'assets/util_functions.sh' 'assets/stub.apk'
. ./util_functions.sh
api_level_arch_detect
unzip -oj magisk.apk "lib/$ABI/*" -x "lib/$ABI/libbusybox.so"
for file in lib*.so; do
chmod 755 $file
mv "$file" "${file:3:${#file}-6}"
done
if $IS_RAMDISK; then
./magiskboot decompress "$TARGET_FILE" ramdisk.cpio
else
./magiskboot unpack "$TARGET_FILE"
fi
./magiskboot decompress "$TARGET_FILE" ramdisk.cpio
cp ramdisk.cpio ramdisk.cpio.orig
export KEEPVERITY=true
@@ -73,7 +51,8 @@ echo "KEEPFORCEENCRYPT=$KEEPFORCEENCRYPT" >> config
echo "PREINITDEVICE=$(./magisk --preinit-device)" >> config
# For API 28, we also manually disable SystemAsRoot
# Explicitly override skip_initramfs by setting RECOVERYMODE=true
[ $API = "28" ] && echo 'RECOVERYMODE=true' >> config
API=$(getprop ro.build.version.sdk)
[ "$API" = "28" ] && echo 'RECOVERYMODE=true' >> config
cat config
./magiskboot compress=xz magisk magisk.xz
@@ -93,9 +72,4 @@ cat config
"add 000 .backup/.magisk config"
rm -f ramdisk.cpio.orig config *.xz
if $IS_RAMDISK; then
./magiskboot compress=gzip ramdisk.cpio "$OUTPUT_FILE"
else
./magiskboot repack "$TARGET_FILE" "$OUTPUT_FILE"
./magiskboot cleanup
fi
./magiskboot compress=gzip ramdisk.cpio "$OUTPUT_FILE"
+8 -21
View File
@@ -37,7 +37,7 @@ if [ ! -f /system/build.prop ]; then
fi
cd /data/local/tmp
chmod 755 busybox
chmod 755 busybox magisk*
if [ -z "$FIRST_STAGE" ]; then
export FIRST_STAGE=1
@@ -53,24 +53,6 @@ fi
pm install -r -g $(pwd)/magisk.apk
# Extract files from APK
unzip -oj magisk.apk 'assets/util_functions.sh' 'assets/stub.apk'
. ./util_functions.sh
api_level_arch_detect
unzip -oj magisk.apk "lib/$ABI/*" -x "lib/$ABI/libbusybox.so"
for file in lib*.so; do
chmod 755 $file
mv "$file" "${file:3:${#file}-6}"
done
if $IS64BIT && [ -e "/system/bin/linker" ]; then
unzip -oj magisk.apk "lib/$ABI32/libmagisk.so"
mv libmagisk.so magisk32
chmod 755 magisk32
fi
# Stop zygote (and previous setup if exists)
magisk --stop 2>/dev/null
stop
@@ -125,14 +107,19 @@ else
fi
# Magisk stuff
MAGISKBIN=/data/adb/magisk
mkdir -p $MAGISKBIN 2>/dev/null
unzip -oj magisk.apk 'assets/*.sh' -d $MAGISKBIN
mkdir /data/adb/modules 2>/dev/null
mkdir /data/adb/post-fs-data.d 2>/dev/null
mkdir /data/adb/service.d 2>/dev/null
for file in magisk magisk32 magiskpolicy stub.apk; do
if [ ! -e $file ]; then continue; fi
MAGISK_FILES="magisk magiskpolicy stub.apk"
if [ -e /system/bin/linker ] && [ -e /system/bin/linker64 ]; then
MAGISK_FILES="$MAGISK_FILES magisk32"
fi
for file in $MAGISK_FILES; do
chmod 755 ./$file
cp -af ./$file $MAGISKTMP/$file
cp -af ./$file $MAGISKBIN/$file
-2
View File
@@ -55,8 +55,6 @@ if [ -z $SOURCEDMODE ]; then
cd "$(getdir "${BASH_SOURCE:-$0}")"
# Load utility functions
. ./util_functions.sh
# Check if 64-bit
api_level_arch_detect
fi
BOOTIMAGE="$1"
+1 -1
View File
@@ -87,7 +87,7 @@ test_main() {
local images=()
for apk in "${apks[@]}"; do
images+=("magisk-$(basename $apk .apk).img")
./build.py -v avd_patch --apk "$apk" "$CF_HOME/init_boot.img" "${images[-1]}"
./build.py -v patch --apk "$apk" "$CF_HOME/init_boot.img" "${images[-1]}"
done
for i in "${!apks[@]}"; do