From ae34659b26fd96c63cf0c390ddfee72ec6edfa32 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=AE=8B=E9=A1=B5?= Date: Sat, 18 Feb 2023 07:00:14 +0800 Subject: [PATCH] No kernel repack if it isn't patched at all MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit It turns out that decompressing and recompressing the kernel is enough to break booting on many devices that use MT6763. Fix #5124, fix #6204, fix #6566 Co-authored-by: LoveSy Co-authored-by: 南宫雪珊 --- native/src/boot/bootimg.cpp | 3 +++ scripts/boot_patch.sh | 20 ++++++++++++++------ 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/native/src/boot/bootimg.cpp b/native/src/boot/bootimg.cpp index 0faf119d2..5b84e23ed 100644 --- a/native/src/boot/bootimg.cpp +++ b/native/src/boot/bootimg.cpp @@ -601,6 +601,9 @@ void repack(const char *src_img, const char *out_img, bool skip_comp) { // zImage size shall remain the same hdr->kernel_size() = boot.hdr->kernel_size(); } + } else if (boot.hdr->kernel_size() != 0) { + xwrite(fd, boot.kernel, boot.hdr->kernel_size()); + hdr->kernel_size() = boot.hdr->kernel_size(); } if (boot.flags[ZIMAGE_KERNEL]) { // Copy zImage tail and adjust size accordingly diff --git a/scripts/boot_patch.sh b/scripts/boot_patch.sh index 07e286f13..966a7c127 100644 --- a/scripts/boot_patch.sh +++ b/scripts/boot_patch.sh @@ -6,7 +6,7 @@ # Usage: boot_patch.sh # # The following flags can be set in environment variables: -# KEEPVERITY, KEEPFORCEENCRYPT, PATCHVBMETAFLAG, RECOVERYMODE +# KEEPVERITY, KEEPFORCEENCRYPT, PATCHVBMETAFLAG, RECOVERYMODE, SYSTEM_ROOT # # This script should be placed in a directory with the following files: # @@ -73,6 +73,7 @@ fi [ -z $KEEPFORCEENCRYPT ] && KEEPFORCEENCRYPT=false [ -z $PATCHVBMETAFLAG ] && PATCHVBMETAFLAG=false [ -z $RECOVERYMODE ] && RECOVERYMODE=false +[ -z $SYSTEM_ROOT ] && SYSTEM_ROOT=false [ -z $ISENCRYPTED ] && ISENCRYPTED=false export KEEPVERITY export KEEPFORCEENCRYPT @@ -203,21 +204,28 @@ for dt in dtb kernel_dtb extra; do done if [ -f kernel ]; then + PATCHEDKERNEL=false # Remove Samsung RKP ./magiskboot hexpatch kernel \ 49010054011440B93FA00F71E9000054010840B93FA00F7189000054001840B91FA00F7188010054 \ - A1020054011440B93FA00F7140020054010840B93FA00F71E0010054001840B91FA00F7181010054 + A1020054011440B93FA00F7140020054010840B93FA00F71E0010054001840B91FA00F7181010054 \ + && PATCHEDKERNEL=true # Remove Samsung defex # Before: [mov w2, #-221] (-__NR_execve) # After: [mov w2, #-32768] - ./magiskboot hexpatch kernel 821B8012 E2FF8F12 + ./magiskboot hexpatch kernel 821B8012 E2FF8F12 && PATCHEDKERNEL=true - # Force kernel to load rootfs + # Force kernel to load rootfs for legacy SAR devices # skip_initramfs -> want_initramfs - ./magiskboot hexpatch kernel \ + $SYSTEM_ROOT && ./magiskboot hexpatch kernel \ 736B69705F696E697472616D667300 \ - 77616E745F696E697472616D667300 + 77616E745F696E697472616D667300 \ + && PATCHEDKERNEL=true + + # If the kernel doesn't need to be patched at all, + # keep raw kernel to avoid bootloops on some weird devices + $PATCHEDKERNEL || rm -f kernel fi #################