Skip to content

Commit 004caf0

Browse files
Howard20181topjohnwu
authored andcommitted
Support patch boot on device using build.py
1 parent b92626c commit 004caf0

2 files changed

Lines changed: 86 additions & 0 deletions

File tree

build.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -672,6 +672,31 @@ def patch_avd_file():
672672

673673
header(f"Output: {output}")
674674

675+
def patch_device_file():
676+
input = Path(args.image)
677+
output = Path(args.output)
678+
679+
header(f"* Patching {input.name}")
680+
681+
push_files(Path("scripts", "patch_boot.sh"))
682+
683+
proc = execv([adb_path, "push", input, "/data/local/tmp"])
684+
if proc.returncode != 0:
685+
error("adb push failed!")
686+
687+
src_file = f"/data/local/tmp/{input.name}"
688+
out_file = "/data/local/tmp/new-boot.img"
689+
690+
proc = execv([adb_path, "shell", "sh", "/data/local/tmp/patch_boot.sh", src_file])
691+
if proc.returncode != 0:
692+
error("patch_boot.sh failed!")
693+
694+
proc = execv([adb_path, "pull", out_file, output])
695+
if proc.returncode != 0:
696+
error("adb pull failed!")
697+
698+
header(f"Output: {output}")
699+
execv([adb_path, "shell", "rm", "/data/local/tmp/new-boot.img"])
675700

676701
##########################
677702
# Config, paths, argparse
@@ -832,6 +857,16 @@ def parse_args():
832857
"-b", "--build", action="store_true", help="build before patching"
833858
)
834859

860+
device_patch_parser = subparsers.add_parser(
861+
"device_patch", help="patch device boot.img or init_boot.img"
862+
)
863+
device_patch_parser.add_argument("image", help="path to boot.img or init_boot.img")
864+
device_patch_parser.add_argument("output", help="output file name, default: new-boot.img")
865+
device_patch_parser.add_argument("--apk", help="a Magisk APK to use")
866+
device_patch_parser.add_argument(
867+
"-b", "--build", action="store_true", help="build before patching"
868+
)
869+
835870
cargo_parser = subparsers.add_parser(
836871
"cargo", help="call 'cargo' commands against the project"
837872
)
@@ -859,6 +894,7 @@ def parse_args():
859894
test_parser.set_defaults(func=build_test)
860895
emu_parser.set_defaults(func=setup_avd)
861896
avd_patch_parser.set_defaults(func=patch_avd_file)
897+
device_patch_parser.set_defaults(func=patch_device_file)
862898
clean_parser.set_defaults(func=cleanup)
863899
ndk_parser.set_defaults(func=setup_ndk)
864900

scripts/patch_boot.sh

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#!/bin/sh
2+
INSTALL_DIR=/data/local/tmp/magisk
3+
MAGISK_APK=/data/local/tmp/magisk.apk
4+
mkdir -p $INSTALL_DIR
5+
cd $INSTALL_DIR || exit 1
6+
MAGISKBIN=/data/adb/magisk
7+
MAGISKTMP=/sbin
8+
[ -d /sbin ] || MAGISKTMP=/debug_ramdisk
9+
if [ ! -f "$MAGISK_APK" ]; then
10+
echo "No Magisk apk found"
11+
exit 1
12+
fi
13+
ABI=$(getprop ro.product.cpu.abi)
14+
for file in busybox magiskpolicy magiskboot magiskinit magisk init-ld; do
15+
rm -f "$INSTALL_DIR/$file"
16+
unzip -d $INSTALL_DIR -oj "$MAGISK_APK" "lib/$ABI/lib$file.so"
17+
mv $INSTALL_DIR/lib$file.so $INSTALL_DIR/$file
18+
chmod 755 "$INSTALL_DIR/$file"
19+
done
20+
for file in util_functions.sh boot_patch.sh stub.apk; do
21+
rm -f "$INSTALL_DIR/$file"
22+
unzip -d $INSTALL_DIR -oj "$MAGISK_APK" "assets/$file"
23+
chmod 755 "$INSTALL_DIR/$file"
24+
done
25+
rm -rf "${INSTALL_DIR:?}/chromeos"
26+
mkdir -p "$INSTALL_DIR/chromeos"
27+
for file in futility kernel_data_key.vbprivk kernel.keyblock; do
28+
unzip -d "$INSTALL_DIR/chromeos" -oj "$MAGISK_APK" "assets/chromeos/$file"
29+
chmod 755 "$INSTALL_DIR/chromeos/$file"
30+
done
31+
rm "$MAGISK_APK"
32+
. ./util_functions.sh
33+
get_flags
34+
api_level_arch_detect
35+
if [ -n "$(getprop ro.boot.vbmeta.device)" ] || [ -n "$(getprop ro.boot.vbmeta.size)" ]; then
36+
PATCHVBMETAFLAG=false
37+
elif getprop ro.product.ab_ota_partitions | grep -wq vbmeta; then
38+
PATCHVBMETAFLAG=false
39+
else
40+
PATCHVBMETAFLAG=true
41+
fi
42+
LEGACYSAR=false
43+
grep ' / ' /proc/mounts | grep -q '/dev/root' && LEGACYSAR=true
44+
SOURCEDMODE=true
45+
. ./boot_patch.sh
46+
mv ./new-boot.img /data/local/tmp/
47+
./magiskboot cleanup
48+
cd /data/local/tmp
49+
rm "$1" "$0" /data/local/tmp/busybox
50+
rm -rf "${INSTALL_DIR:?}"

0 commit comments

Comments
 (0)