Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions build.py
Original file line number Diff line number Diff line change
Expand Up @@ -671,6 +671,31 @@ def patch_avd_file():

header(f"Output: {output}")

def patch_device_file():
input = Path(args.image)
output = Path(args.output)

header(f"* Patching {input.name}")

push_files(Path("scripts", "patch_boot.sh"))

proc = execv([adb_path, "push", input, "/data/local/tmp"])
if proc.returncode != 0:
error("adb push failed!")

src_file = f"/data/local/tmp/{input.name}"
out_file = "/data/local/tmp/new-boot.img"

proc = execv([adb_path, "shell", "sh", "/data/local/tmp/patch_boot.sh", src_file])
if proc.returncode != 0:
error("patch_boot.sh failed!")

proc = execv([adb_path, "pull", out_file, output])
if proc.returncode != 0:
error("adb pull failed!")

header(f"Output: {output}")
execv([adb_path, "shell", "rm", "/data/local/tmp/new-boot.img"])

##########################
# Config, paths, argparse
Expand Down Expand Up @@ -831,6 +856,16 @@ def parse_args():
"-b", "--build", action="store_true", help="build before patching"
)

device_patch_parser = subparsers.add_parser(
"device_patch", help="patch device boot.img or init_boot.img"
)
device_patch_parser.add_argument("image", help="path to boot.img or init_boot.img")
device_patch_parser.add_argument("output", help="output file name, default: new-boot.img")
device_patch_parser.add_argument("--apk", help="a Magisk APK to use")
device_patch_parser.add_argument(
"-b", "--build", action="store_true", help="build before patching"
)

cargo_parser = subparsers.add_parser(
"cargo", help="call 'cargo' commands against the project"
)
Expand Down Expand Up @@ -858,6 +893,7 @@ def parse_args():
test_parser.set_defaults(func=build_test)
emu_parser.set_defaults(func=setup_avd)
avd_patch_parser.set_defaults(func=patch_avd_file)
device_patch_parser.set_defaults(func=patch_device_file)
clean_parser.set_defaults(func=cleanup)
ndk_parser.set_defaults(func=setup_ndk)

Expand Down
50 changes: 50 additions & 0 deletions scripts/patch_boot.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#!/bin/sh
INSTALL_DIR=/data/local/tmp/magisk
MAGISK_APK=/data/local/tmp/magisk.apk
mkdir -p $INSTALL_DIR
cd $INSTALL_DIR || exit 1
MAGISKBIN=/data/adb/magisk
MAGISKTMP=/sbin
[ -d /sbin ] || MAGISKTMP=/debug_ramdisk
if [ ! -f "$MAGISK_APK" ]; then
echo "No Magisk apk found"
exit 1
fi
ABI=$(getprop ro.product.cpu.abi)
for file in busybox magiskpolicy magiskboot magiskinit magisk init-ld; do
rm -f "$INSTALL_DIR/$file"
unzip -d $INSTALL_DIR -oj "$MAGISK_APK" "lib/$ABI/lib$file.so"
mv $INSTALL_DIR/lib$file.so $INSTALL_DIR/$file
chmod 755 "$INSTALL_DIR/$file"
done
for file in util_functions.sh boot_patch.sh stub.apk; do
rm -f "$INSTALL_DIR/$file"
unzip -d $INSTALL_DIR -oj "$MAGISK_APK" "assets/$file"
chmod 755 "$INSTALL_DIR/$file"
done
rm -rf "${INSTALL_DIR:?}/chromeos"
mkdir -p "$INSTALL_DIR/chromeos"
for file in futility kernel_data_key.vbprivk kernel.keyblock; do
unzip -d "$INSTALL_DIR/chromeos" -oj "$MAGISK_APK" "assets/chromeos/$file"
chmod 755 "$INSTALL_DIR/chromeos/$file"
done
rm "$MAGISK_APK"
. ./util_functions.sh
get_flags
api_level_arch_detect
if [ -n "$(getprop ro.boot.vbmeta.device)" ] || [ -n "$(getprop ro.boot.vbmeta.size)" ]; then
PATCHVBMETAFLAG=false
elif getprop ro.product.ab_ota_partitions | grep -wq vbmeta; then
PATCHVBMETAFLAG=false
else
PATCHVBMETAFLAG=true
fi
LEGACYSAR=false
grep ' / ' /proc/mounts | grep -q '/dev/root' && LEGACYSAR=true
SOURCEDMODE=true
. ./boot_patch.sh
mv ./new-boot.img /data/local/tmp/
./magiskboot cleanup
cd /data/local/tmp
rm "$1" "$0" /data/local/tmp/busybox
rm -rf "${INSTALL_DIR:?}"
Loading