magisk-ghid/customize.sh

135 lines
3.8 KiB
Bash
Raw Normal View History

#!/bin/sh
chmod 755 "$MODPATH/system/bin/ghid.sh"
sed "s@\[MODDIR\]@$MODPATH@g" "$MODPATH/system/bin/ghid.sh" | sed 's/modules_update/modules/g' >"$TMPDIR/ghid.sh"
cp "$TMPDIR/ghid.sh" "$MODPATH/system/bin/ghid.sh"
rpwd="$PWD"
##########################################################################################
# Initialization
##########################################################################################
cd "$MAGISKBIN"
get_flags
find_boot_image
[ -e "$BOOTIMAGE" ] || abort "$BOOTIMAGE does not exist!"
# Flags
[ -z "$KEEPVERITY" ] && KEEPVERITY=false
[ -z "$KEEPFORCEENCRYPT" ] && KEEPFORCEENCRYPT=false
[ -z "$RECOVERYMODE" ] && RECOVERYMODE=false
export KEEPVERITY
export KEEPFORCEENCRYPT
chmod -R 755 .
eval "$BOOTSIGNER" -verify < "$BOOTIMAGE" && BOOTSIGNED=true
"$BOOTSIGNED" && ui_print "- Boot image is signed with AVB 1.0"
##########################################################################################
# Start bootpatch
##########################################################################################
##########################################################################################
# Unpack
##########################################################################################
CHROMEOS=false
ui_print "- Unpacking boot image"
./magiskboot unpack "$BOOTIMAGE"
case $? in
1 )
abort "! Unsupported/Unknown image format"
;;
2 )
ui_print "- ChromeOS boot image detected"
CHROMEOS=true
;;
esac
[ -f recovery_dtbo ] && RECOVERYMODE=true
##########################################################################################
# Ramdisk restores
##########################################################################################
# Test patch status and do restore
ui_print "- Checking ramdisk status"
if [ -e ramdisk.cpio ]; then
./magiskboot cpio ramdisk.cpio test
STATUS=$?
else
# Stock A only system-as-root
STATUS=0
fi
case $((STATUS & 3)) in
0 ) # Stock boot
ui_print "- Stock boot image detected"
abort "Magisk not installed????"
;;
1 ) # Magisk patched
ui_print "- Magisk patched boot image detected"
;;
2 ) # Unsupported
ui_print "! Boot image patched by unsupported programs"
abort "! Please restore back to stock boot image"
;;
esac
##########################################################################################
# Ramdisk patches
##########################################################################################
ui_print "- Patching ramdisk"
./magiskboot cpio ramdisk.cpio extract /overlay.d/ghid.rc ghid.rc
if ! diff $MODPATH/overlay.d/ghid.rc ghid.rc; then
rm ghid.rc
ui_print "- Adding overlay.d/ghid.rc"
./magiskboot cpio ramdisk.cpio \
"mkdir 755 overlay.d" \
"add 755 overlay.d/ghid.rc $MODPATH/overlay.d/ghid.rc"
if [ $((STATUS & 4)) -ne 0 ]; then
ui_print "- Compressing ramdisk"
./magiskboot cpio ramdisk.cpio compress
fi
rm -f ramdisk.cpio.orig config
ui_print "- Repacking boot image"
./magiskboot repack "$BOOTIMAGE" || abort "! Unable to repack boot image!"
# Sign chromeos boot
$CHROMEOS && sign_chromeos
##########################################################################################
# End bootpatch
##########################################################################################
ui_print "- Flashing new boot image"
if ! flash_image new-boot.img "$BOOTIMAGE"; then
ui_print "- Compressing ramdisk to fit in partition"
./magiskboot cpio ramdisk.cpio compress
./magiskboot repack "$BOOTIMAGE"
flash_image new-boot.img "$BOOTIMAGE" || abort "! Insufficient partition size"
fi
else
rm ghid.rc
ui_print "- overlay.d/ghid.rc already added"
fi
./magiskboot cleanup
rm -f new-boot.img
patch_dtb_partitions
run_migrations
cd "$rpwd"