Skip to content
140 changes: 80 additions & 60 deletions scripts/boot_patch.sh
Original file line number Diff line number Diff line change
Expand Up @@ -110,38 +110,50 @@ esac

# Test patch status and do restore
ui_print "- Checking ramdisk status"

if [ -e ramdisk.cpio ]; then
./magiskboot cpio ramdisk.cpio test
STATUS=$?
SKIP_BACKUP=""
else
# Stock A only legacy SAR, or some Android 13 GKIs
STATUS=0
SKIP_BACKUP="#"
RAMDISK_EXISTS=1
case $STATUS in
0 )
# Stock boot
ui_print "- Stock boot image detected"
SHA1=$(./magiskboot sha1 "$BOOTIMAGE" 2>/dev/null)
cat $BOOTIMAGE > stock_boot.img
cp -af ramdisk.cpio ramdisk.cpio.orig 2>/dev/null
;;
1 )
# Magisk patched
ui_print "- Magisk patched boot image detected"
./magiskboot cpio ramdisk.cpio \
"extract .backup/.magisk config.orig" \
"restore"
cp -af ramdisk.cpio ramdisk.cpio.orig
rm -f stock_boot.img
;;
2 )
# Unsupported
ui_print "! Boot image patched by unsupported programs"
abort "! Please restore back to stock boot image"
;;
esac
else
#checking if other cpio exist in the boot image
if find . -name "*.cpio" | grep -vF "./ramdisk.cpio" >null; then NOTSUPPORTED=1; fi #searching for any cpio file other than ./ramdisk.cpio
if [ $NOTSUPPORTED -eq 1 ]; then
ui_print "- Information"
ui_print "- This boot image contains the following not supported cpio:"
find . -name "*.cpio" | grep -vF "./ramdisk.cpio" > tmp.log
while read p; do
ui_print "- $p"
done <tmp.log
rm tmp.log
fi
ui_print "- No ramdisk file found in the root directory"
ui_print "- Skipping ramdisk patching"
RAMDISK_EXISTS=0
fi
case $STATUS in
0 )
# Stock boot
ui_print "- Stock boot image detected"
SHA1=$(./magiskboot sha1 "$BOOTIMAGE" 2>/dev/null)
cat $BOOTIMAGE > stock_boot.img
cp -af ramdisk.cpio ramdisk.cpio.orig 2>/dev/null
;;
1 )
# Magisk patched
ui_print "- Magisk patched boot image detected"
./magiskboot cpio ramdisk.cpio \
"extract .backup/.magisk config.orig" \
"restore"
cp -af ramdisk.cpio ramdisk.cpio.orig
rm -f stock_boot.img
;;
2 )
# Unsupported
ui_print "! Boot image patched by unsupported programs"
abort "! Please restore back to stock boot image"
;;
esac

if [ -f config.orig ]; then
# Read existing configs
Expand All @@ -158,38 +170,42 @@ fi
# Ramdisk Patches
##################

ui_print "- Patching ramdisk"

$BOOTMODE && [ -z "$PREINITDEVICE" ] && PREINITDEVICE=$(./magisk --preinit-device)

# Compress to save precious ramdisk space
./magiskboot compress=xz magisk magisk.xz
./magiskboot compress=xz stub.apk stub.xz
./magiskboot compress=xz init-ld init-ld.xz

echo "KEEPVERITY=$KEEPVERITY" > config
echo "KEEPFORCEENCRYPT=$KEEPFORCEENCRYPT" >> config
echo "RECOVERYMODE=$RECOVERYMODE" >> config
if [ -n "$PREINITDEVICE" ]; then
ui_print "- Pre-init storage partition: $PREINITDEVICE"
echo "PREINITDEVICE=$PREINITDEVICE" >> config
if [ $RAMDISK_EXISTS -eq 1 ]; then

ui_print "- Patching ramdisk"

$BOOTMODE && [ -z "$PREINITDEVICE" ] && PREINITDEVICE=$(./magisk --preinit-device)

# Compress to save precious ramdisk space
./magiskboot compress=xz magisk magisk.xz
./magiskboot compress=xz stub.apk stub.xz
./magiskboot compress=xz init-ld init-ld.xz

echo "KEEPVERITY=$KEEPVERITY" > config
echo "KEEPFORCEENCRYPT=$KEEPFORCEENCRYPT" >> config
echo "RECOVERYMODE=$RECOVERYMODE" >> config
if [ -n "$PREINITDEVICE" ]; then
ui_print "- Pre-init storage partition: $PREINITDEVICE"
echo "PREINITDEVICE=$PREINITDEVICE" >> config
fi
[ -n "$SHA1" ] && echo "SHA1=$SHA1" >> config

./magiskboot cpio ramdisk.cpio \
"add 0750 init magiskinit" \
"mkdir 0750 overlay.d" \
"mkdir 0750 overlay.d/sbin" \
"add 0644 overlay.d/sbin/magisk.xz magisk.xz" \
"add 0644 overlay.d/sbin/stub.xz stub.xz" \
"add 0644 overlay.d/sbin/init-ld.xz init-ld.xz" \
"patch" \
"backup ramdisk.cpio.orig" \
"mkdir 000 .backup" \
"add 000 .backup/.magisk config" \
|| abort "! Unable to patch ramdisk"

rm -f ramdisk.cpio.orig config *.xz

fi
[ -n "$SHA1" ] && echo "SHA1=$SHA1" >> config

./magiskboot cpio ramdisk.cpio \
"add 0750 init magiskinit" \
"mkdir 0750 overlay.d" \
"mkdir 0750 overlay.d/sbin" \
"add 0644 overlay.d/sbin/magisk.xz magisk.xz" \
"add 0644 overlay.d/sbin/stub.xz stub.xz" \
"add 0644 overlay.d/sbin/init-ld.xz init-ld.xz" \
"patch" \
"$SKIP_BACKUP backup ramdisk.cpio.orig" \
"mkdir 000 .backup" \
"add 000 .backup/.magisk config" \
|| abort "! Unable to patch ramdisk"

rm -f ramdisk.cpio.orig config *.xz

#################
# Binary Patches
Expand All @@ -209,6 +225,7 @@ done

if [ -f kernel ]; then
PATCHEDKERNEL=false
ui_print "- Patching kernel"
# Remove Samsung RKP
./magiskboot hexpatch kernel \
49010054011440B93FA00F71E9000054010840B93FA00F7189000054001840B91FA00F7188010054 \
Expand Down Expand Up @@ -237,6 +254,9 @@ if [ -f kernel ]; then
# 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

elif [ $RAMDISK_EXISTS -eq 0 ]; then
abort "! Selected boot image does not contain anything to patch"
fi

#################
Expand All @@ -245,7 +265,7 @@ fi

ui_print "- Repacking boot image"
./magiskboot repack "$BOOTIMAGE" || abort "! Unable to repack boot image"

# Sign chromeos boot
$CHROMEOS && sign_chromeos

Expand Down