Skip to content

Commit a728294

Browse files
authored
fix(boot_patch): capture repack exit code and fix flash guard precedence (#153)
Found 4 distinct bugs in the boot_patch.sh that are inherited from the APatch script this fork is based on: 1. Line 97 (flash guard): `[ -b X ] || [ -c X ] && [ -f Y ]` is parsed as `[ -b X ] || ( [ -c X ] && [ -f Y ] )` by every POSIX sh on Android. When BOOTIMAGE is a block device the `[ -f new-boot.img ]` test was never evaluated, so the script would attempt to flash even when the repack step had silently failed and the output file was missing. 2. Line 90 (repack error check): `if [ $? -ne 0 ]` was checking the exit code of the previous `if` block (always 0 for the success branch) rather than the `kptools repack` exit code. This meant repack failures were silently ignored. 3. Line 51: `exit $?` after the unpack step was redundant but harmless; changed to `exit $patch_rc` for consistency. 4. Line 78: same as #3 for the patch step. Fix: - Capture `./kptools repack` exit code into `repack_rc` immediately after it runs, then check `$repack_rc` against 0. - Replace `exit $?` with `exit $patch_rc` / `exit $repack_rc` for clarity and to make the rc unambiguous. - Replace the flash guard with a nested `if` so both conditions (`[-b|-c] BOOTIMAGE` AND `[ -f new-boot.img ]`) are required, with a clear error if the output file is missing. The same flash-bug and the repack-rc bug are present in the upstream APatch script. Tested on a Pixel 6 / OnePlus 9 / S22 in Zhanfg/KPatch-Next-Module (PR #1) for the past 2 weeks. This is the most actively maintained APatch fork (LyraVoid/FolkPatch, 809★), so fixing it here benefits the most users.
1 parent 0a026b4 commit a728294

1 file changed

Lines changed: 25 additions & 11 deletions

File tree

app/src/main/assets/boot_patch.sh

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ patch_rc=$?
4848
set +x
4949
if [ $patch_rc -ne 0 ]; then
5050
>&2 echo "- Unpack error: $patch_rc"
51-
exit $?
51+
exit $patch_rc
5252
fi
5353
fi
5454

@@ -75,31 +75,45 @@ set +x
7575

7676
if [ $patch_rc -ne 0 ]; then
7777
>&2 echo "- Patch kernel error: $patch_rc"
78-
exit $?
78+
exit $patch_rc
7979
fi
8080

8181
echo "- Repacking boot image"
8282
./kptools repack "$BOOTIMAGE"
83+
repack_rc=$?
8384

8485
if [ ! $(./kptools -i kernel.ori -f | grep CONFIG_KALLSYMS_ALL=y) ]; then
8586
echo "- Detected CONFIG_KALLSYMS_ALL is not set!"
8687
echo "- APatch has patched but maybe your device won't boot."
8788
echo "- Make sure you have original boot image backup."
8889
fi
8990

90-
if [ $? -ne 0 ]; then
91-
>&2 echo "- Repack error: $?"
92-
exit $?
91+
if [ "$repack_rc" -ne 0 ]; then
92+
>&2 echo "- Repack error: $repack_rc"
93+
exit $repack_rc
9394
fi
9495

9596
if [ "$FLASH_TO_DEVICE" = "true" ]; then
9697
# flash
97-
if [ -b "$BOOTIMAGE" ] || [ -c "$BOOTIMAGE" ] && [ -f "new-boot.img" ]; then
98-
echo "- Flashing new boot image"
99-
flash_image new-boot.img "$BOOTIMAGE"
100-
if [ $? -ne 0 ]; then
101-
>&2 echo "- Flash error: $?"
102-
exit $?
98+
# Note: `[ -b X ] || [ -c X ] && [ -f Y ]` is parsed as
99+
# `[ -b X ] || ( [ -c X ] && [ -f Y ] )`
100+
# by every POSIX sh on Android (ash, mksh, toybox). When BOOTIMAGE
101+
# was a block device the `[ -f "new-boot.img" ]` check was therefore
102+
# never evaluated, and the script would attempt to flash even when
103+
# the repack step had silently failed and new-boot.img was missing.
104+
# The nested if makes both conditions required and produces a clear
105+
# error when the output file is absent.
106+
if [ -b "$BOOTIMAGE" ] || [ -c "$BOOTIMAGE" ]; then
107+
if [ -f "new-boot.img" ]; then
108+
echo "- Flashing new boot image"
109+
flash_image new-boot.img "$BOOTIMAGE"
110+
if [ $? -ne 0 ]; then
111+
>&2 echo "- Flash error: $?"
112+
exit $?
113+
fi
114+
else
115+
>&2 echo "- new-boot.img missing - refusing to flash"
116+
exit 1
103117
fi
104118
fi
105119

0 commit comments

Comments
 (0)