Do not modify system-images contents when patching

This commit is contained in:
topjohnwu 2024-07-19 16:47:00 -07:00
parent 4931825912
commit 659dd09723
3 changed files with 48 additions and 44 deletions

View File

@ -602,46 +602,37 @@ def setup_avd(args):
error("avd_magisk.sh failed!")
def patch_avd_ramdisk(args):
def patch_avd_file(args):
if not args.skip:
args.release = False
build_all(args)
args.ramdisk = Path(args.ramdisk)
args.target = Path(args.target)
src_file = f"/data/local/tmp/{args.target.name}"
out_file = f"{src_file}.magisk"
if args.output:
args.output = Path(args.output)
else:
args.output = args.target.parent / f"{args.target.name}.magisk"
header("* Patching emulator ramdisk.img")
# Create a backup to prevent accidental overwrites
backup = args.ramdisk.parent / f"{args.ramdisk.name}.bak"
if not backup.exists():
cp(args.ramdisk, backup)
ini = args.ramdisk.parent / "advancedFeatures.ini"
with open(ini, "r") as f:
adv_ft = f.read()
# Need to turn off system as root
if "SystemAsRoot = on" in adv_ft:
# Create a backup
cp(ini, ini.parent / f"{ini.name}.bak")
adv_ft = adv_ft.replace("SystemAsRoot = on", "SystemAsRoot = off")
with open(ini, "w") as f:
f.write(adv_ft)
header(f"* Patching {args.target.name}")
push_files(args, Path("scripts", "avd_patch.sh"))
proc = execv([adb_path, "push", backup, "/data/local/tmp/ramdisk.cpio.tmp"])
proc = execv([adb_path, "push", args.target, "/data/local/tmp"])
if proc.returncode != 0:
error("adb push failed!")
proc = execv([adb_path, "shell", "sh", "/data/local/tmp/avd_patch.sh"])
proc = execv([adb_path, "shell", "sh", "/data/local/tmp/avd_patch.sh", src_file])
if proc.returncode != 0:
error("avd_patch.sh failed!")
proc = execv([adb_path, "pull", "/data/local/tmp/ramdisk.cpio.gz", args.ramdisk])
proc = execv([adb_path, "pull", out_file, args.output])
if proc.returncode != 0:
error("adb pull failed!")
header(f"Output: {args.output}")
def build_all(args):
build_binary(args)
@ -722,12 +713,15 @@ avd_parser.add_argument(
)
avd_parser.set_defaults(func=setup_avd)
avd_patch_parser = subparsers.add_parser("avd_patch", help="patch AVD ramdisk.img")
avd_patch_parser.add_argument("ramdisk", help="path to ramdisk.img")
avd_patch_parser = subparsers.add_parser(
"avd_patch", help="patch AVD ramdisk.img or init_boot.img"
)
avd_patch_parser.add_argument("target", help="path to ramdisk.img or init_boot.img")
avd_patch_parser.add_argument("output", help="optional output file name", nargs="?")
avd_patch_parser.add_argument(
"-s", "--skip", action="store_true", help="skip building binaries and the app"
)
avd_patch_parser.set_defaults(func=patch_avd_ramdisk)
avd_patch_parser.set_defaults(func=patch_avd_file)
clean_parser = subparsers.add_parser("clean", help="cleanup")
clean_parser.add_argument(

View File

@ -34,7 +34,16 @@ if [ -z "$FIRST_STAGE" ]; then
export FIRST_STAGE=1
export ASH_STANDALONE=1
# Re-exec script with busybox
exec ./busybox sh $0
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
fi
# Extract files from APK
@ -49,7 +58,11 @@ for file in lib*.so; do
mv "$file" "${file:3:${#file}-6}"
done
./magiskboot decompress ramdisk.cpio.tmp ramdisk.cpio
if $IS_RAMDISK; then
./magiskboot decompress "$TARGET_FILE" ramdisk.cpio
else
./magiskboot unpack "$TARGET_FILE"
fi
cp ramdisk.cpio ramdisk.cpio.orig
export KEEPVERITY=true
@ -78,4 +91,9 @@ cat config
"add 000 .backup/.magisk config"
rm -f ramdisk.cpio.orig config magisk*.xz stub.xz
./magiskboot compress=gzip ramdisk.cpio ramdisk.cpio.gz
if $IS_RAMDISK; then
./magiskboot compress=gzip ramdisk.cpio "$OUTPUT_FILE"
else
./magiskboot repack "$TARGET_FILE" "$OUTPUT_FILE"
./magiskboot cleanup
fi

View File

@ -26,9 +26,7 @@ print_error() {
cleanup() {
print_error "! An error occurred when testing $pkg"
find $ANDROID_SDK_ROOT/system-images -name 'ramdisk.img' -exec cp -v {}.bak {} \; 2>/dev/null
find $ANDROID_SDK_ROOT/system-images -name 'advancedFeatures.ini' -exec cp -v {}.bak {} \; 2>/dev/null
rm -f magisk_patched.img
"$avd" delete avd -n test
pkill -INT -P $$
wait
@ -62,12 +60,6 @@ wait_for_boot() {
done
}
restore_backup() {
if [ -f "${1}.bak" ]; then
cp "${1}.bak" "$1"
fi
}
wait_emu() {
local wait_fn=$1
local which_pid
@ -195,8 +187,6 @@ run_test() {
# Setup emulator
"$sdk" --channel=3 $pkg
echo no | "$avd" create avd -f -n test -k $pkg
restore_backup $ramdisk
restore_backup $features
# Launch stock emulator
print_title "* Launching $pkg"
@ -204,14 +194,17 @@ run_test() {
emu_pid=$!
wait_emu wait_for_bootanim
# Update arguments for Magisk runs
emu_args="$emu_args -ramdisk magisk_patched.img -feature -SystemAsRoot"
# Patch and test debug build
./build.py avd_patch -s "$ramdisk"
./build.py avd_patch -s "$ramdisk" magisk_patched.img
kill -INT $emu_pid
wait $emu_pid
test_emu debug $api
# Re-patch and test release build
./build.py -r avd_patch -s "$ramdisk"
./build.py -r avd_patch -s "$ramdisk" magisk_patched.img
kill -INT $emu_pid
wait $emu_pid
test_emu release $api
@ -219,8 +212,7 @@ run_test() {
# Cleanup
kill -INT $emu_pid
wait $emu_pid
restore_backup $ramdisk
restore_backup $features
rm -f magisk_patched.img
}
trap cleanup EXIT