Skip to content

Commit e1f5410

Browse files
committed
Support patch boot on device using build.py
1 parent 6ba1685 commit e1f5410

File tree

2 files changed

+86
-0
lines changed

2 files changed

+86
-0
lines changed

build.py

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

634634
header(f"Output: {output}")
635635

636+
def patch_device_file():
637+
input = Path(args.image)
638+
output = Path(args.output)
639+
640+
header(f"* Patching {input.name}")
641+
642+
push_files(Path("scripts", "patch_boot.sh"))
643+
644+
proc = execv([adb_path, "push", input, "/data/local/tmp"])
645+
if proc.returncode != 0:
646+
error("adb push failed!")
647+
648+
src_file = f"/data/local/tmp/{input.name}"
649+
out_file = "/data/local/tmp/new-boot.img"
650+
651+
proc = execv([adb_path, "shell", "sh", "/data/local/tmp/patch_boot.sh", src_file])
652+
if proc.returncode != 0:
653+
error("patch_boot.sh failed!")
654+
655+
proc = execv([adb_path, "pull", out_file, output])
656+
if proc.returncode != 0:
657+
error("adb pull failed!")
658+
659+
header(f"Output: {output}")
660+
execv([adb_path, "shell", "rm", "/data/local/tmp/new-boot.img"])
636661

637662
##########################
638663
# Config, paths, argparse
@@ -789,6 +814,16 @@ def parse_args():
789814
"-b", "--build", action="store_true", help="build before patching"
790815
)
791816

817+
device_patch_parser = subparsers.add_parser(
818+
"device_patch", help="patch device boot.img or init_boot.img"
819+
)
820+
device_patch_parser.add_argument("image", help="path to boot.img or init_boot.img")
821+
device_patch_parser.add_argument("output", help="output file name, default: new-boot.img")
822+
device_patch_parser.add_argument("--apk", help="a Magisk APK to use")
823+
device_patch_parser.add_argument(
824+
"-b", "--build", action="store_true", help="build before patching"
825+
)
826+
792827
cargo_parser = subparsers.add_parser(
793828
"cargo", help="call 'cargo' commands against the project"
794829
)
@@ -812,6 +847,7 @@ def parse_args():
812847
test_parser.set_defaults(func=build_test)
813848
emu_parser.set_defaults(func=setup_avd)
814849
avd_patch_parser.set_defaults(func=patch_avd_file)
850+
device_patch_parser.set_defaults(func=patch_device_file)
815851
clean_parser.set_defaults(func=cleanup)
816852
ndk_parser.set_defaults(func=setup_ndk)
817853

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)