From 92f41b38c4414e7396c1d231ca82fd910b651fdb Mon Sep 17 00:00:00 2001 From: lordwelch Date: Sun, 18 Apr 2021 14:27:01 -0700 Subject: [PATCH] Update to latest magisk --- META-INF/com/google/android/update-binary | 152 +------------------ boot_patch.sh | 174 ++++++++++++++++++++++ customize.sh | 139 ++--------------- flash_script.sh | 60 ++++++++ module.prop | 4 +- overlay.d/ghid.rc | 1 - system/bin/ghid.sh | 1 + utils.sh | 63 ++++++++ 8 files changed, 316 insertions(+), 278 deletions(-) create mode 100644 boot_patch.sh create mode 100644 flash_script.sh create mode 100644 utils.sh diff --git a/META-INF/com/google/android/update-binary b/META-INF/com/google/android/update-binary index 29240cd..28b48e5 100644 --- a/META-INF/com/google/android/update-binary +++ b/META-INF/com/google/android/update-binary @@ -6,168 +6,28 @@ umask 022 -# Global vars -TMPDIR=/dev/tmp -PERSISTDIR=/sbin/.magisk/mirror/persist - -rm -rf $TMPDIR 2>/dev/null -mkdir -p $TMPDIR - # echo before loading util_functions ui_print() { echo "$1"; } require_new_magisk() { ui_print "*******************************" - ui_print " Please install Magisk v19.0+! " + ui_print " Please install Magisk v20.4+! " ui_print "*******************************" exit 1 } -is_legacy_script() { - unzip -l "$ZIPFILE" install.sh | grep -q install.sh - return $? -} - -print_modname() { - local len - len=`echo -n $MODNAME | wc -c` - len=$((len + 2)) - local pounds=`printf "%${len}s" | tr ' ' '*'` - ui_print "$pounds" - ui_print " $MODNAME " - ui_print "$pounds" - ui_print "*******************" - ui_print " Powered by Magisk " - ui_print "*******************" -} - -############## -# Environment -############## +######################### +# Load util_functions.sh +######################### OUTFD=$2 ZIPFILE=$3 mount /data 2>/dev/null -# Load utility functions [ -f /data/adb/magisk/util_functions.sh ] || require_new_magisk . /data/adb/magisk/util_functions.sh -[ $MAGISK_VER_CODE -gt 18100 ] || require_new_magisk +[ $MAGISK_VER_CODE -lt 20400 ] && require_new_magisk -# Preperation for flashable zips -setup_flashable - -# Mount partitions -mount_partitions - -# Detect version and architecture -api_level_arch_detect - -# Setup busybox and binaries -$BOOTMODE && boot_actions || recovery_actions - -############## -# Preparation -############## - -# Extract prop file -unzip -o "$ZIPFILE" module.prop -d $TMPDIR >&2 -[ ! -f $TMPDIR/module.prop ] && abort "! Unable to extract zip file!" - -$BOOTMODE && MODDIRNAME=modules_update || MODDIRNAME=modules -MODULEROOT=$NVBASE/$MODDIRNAME -MODID=`grep_prop id $TMPDIR/module.prop` -MODPATH=$MODULEROOT/$MODID -MODNAME=`grep_prop name $TMPDIR/module.prop` - -# Create mod paths -rm -rf $MODPATH 2>/dev/null -mkdir -p $MODPATH - -########## -# Install -########## - -if is_legacy_script; then - unzip -oj "$ZIPFILE" module.prop install.sh uninstall.sh 'common/*' -d $TMPDIR >&2 - - # Load install script - . $TMPDIR/install.sh - - # Callbacks - print_modname - on_install - - # Custom uninstaller - [ -f $TMPDIR/uninstall.sh ] && cp -af $TMPDIR/uninstall.sh $MODPATH/uninstall.sh - - # Skip mount - $SKIPMOUNT && touch $MODPATH/skip_mount - - # prop file - $PROPFILE && cp -af $TMPDIR/system.prop $MODPATH/system.prop - - # Module info - cp -af $TMPDIR/module.prop $MODPATH/module.prop - - # post-fs-data scripts - $POSTFSDATA && cp -af $TMPDIR/post-fs-data.sh $MODPATH/post-fs-data.sh - - # service scripts - $LATESTARTSERVICE && cp -af $TMPDIR/service.sh $MODPATH/service.sh - - ui_print "- Setting permissions" - set_permissions -else - print_modname - - unzip -o "$ZIPFILE" customize.sh -d $MODPATH >&2 - - if ! grep -q '^SKIPUNZIP=1$' $MODPATH/customize.sh 2>/dev/null; then - ui_print "- Extracting module files" - unzip -o "$ZIPFILE" -x 'META-INF/*' -d $MODPATH >&2 - - # Default permissions - set_perm_recursive $MODPATH 0 0 0755 0644 - fi - - # Load customization script - [ -f $MODPATH/customize.sh ] && . $MODPATH/customize.sh -fi - -# Handle replace folders -for TARGET in $REPLACE; do - ui_print "- Replace target: $TARGET" - mktouch $MODPATH$TARGET/.replace -done - -if $BOOTMODE; then - # Update info for Magisk Manager - mktouch $NVBASE/modules/$MODID/update - cp -af $MODPATH/module.prop $NVBASE/modules/$MODID/module.prop -fi - -# Copy over custom sepolicy rules -if [ -f $MODPATH/sepolicy.rule -a -e $PERSISTDIR ]; then - ui_print "- Installing custom sepolicy patch" - PERSISTMOD=$PERSISTDIR/magisk/$MODID - mkdir -p $PERSISTMOD - cp -af $MODPATH/sepolicy.rule $PERSISTMOD/sepolicy.rule -fi - -# Remove stuffs that don't belong to modules -rm -rf \ -$MODPATH/system/placeholder $MODPATH/customize.sh \ -$MODPATH/README.md $MODPATH/.git* 2>/dev/null - -############## -# Finalizing -############## - -cd / -$BOOTMODE || recovery_cleanup -rm -rf $TMPDIR - -ui_print "- Done" +install_module exit 0 diff --git a/boot_patch.sh b/boot_patch.sh new file mode 100644 index 0000000..fe35508 --- /dev/null +++ b/boot_patch.sh @@ -0,0 +1,174 @@ +#!/system/bin/sh +####################################################################################### +# Magisk Boot Image Patcher +####################################################################################### +# +# Usage: boot_patch.sh +# +# The following flags can be set in environment variables: +# KEEPVERITY, KEEPFORCEENCRYPT, RECOVERYMODE +# +# This script should be placed in a directory with the following files: +# +# File name Type Description +# +# boot_patch.sh script A script to patch boot image for Magisk. +# (this file) The script will use files in its same +# directory to complete the patching process +# util_functions.sh script A script which hosts all functions required +# for this script to work properly +# magiskinit binary The binary to replace /init +# magisk(32/64) binary The magisk binaries +# magiskboot binary A tool to manipulate boot images +# chromeos folder This folder includes the utility and keys to sign +# (optional) chromeos boot images. Only used for Pixel C. +# +####################################################################################### + +############ +# Functions +############ + +# Pure bash dirname implementation +getdir() { + case "$1" in + */*) + dir=${1%/*} + if [ -z $dir ]; then + echo "/" + else + echo $dir + fi + ;; + *) echo "." ;; + esac +} + +################# +# Initialization +################# + +[ -e "$BOOTIMAGE" ] || abort "$BOOTIMAGE does not exist!" + +# Dump image for MTD/NAND character device boot partitions +if [ -c "$BOOTIMAGE" ]; then + nanddump -f boot.img "$BOOTIMAGE" + BOOTNAND="$BOOTIMAGE" + BOOTIMAGE=boot.img +fi + +# Flags +[ -z $KEEPVERITY ] && KEEPVERITY=false +[ -z $KEEPFORCEENCRYPT ] && KEEPFORCEENCRYPT=false +[ -z $RECOVERYMODE ] && RECOVERYMODE=false +export KEEPVERITY +export KEEPFORCEENCRYPT + +chmod -R 755 . + +######### +# Unpack +######### + +CHROMEOS=false + +ui_print "- Unpacking boot image" +$MAGISKBIN/magiskboot unpack "$BOOTIMAGE" + +case $? in + 0 ) ;; + 1 ) + abort "! Unsupported/Unknown image format" + ;; + 2 ) + ui_print "- ChromeOS boot image detected" + CHROMEOS=true + ;; + * ) + abort "! Unable to unpack boot image" + ;; +esac + +[ -f recovery_dtbo ] && RECOVERYMODE=true + +################### +# Ramdisk Restores +################### + +# Test patch status and do restore +ui_print "- Checking ramdisk status" +if [ -e ramdisk.cpio ]; then + $MAGISKBIN/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 must be installed" + ;; + 1 ) # Magisk patched + ui_print "- Magisk patched boot image detected" + # Find SHA1 of stock boot image + [ -z $SHA1 ] && SHA1=$($MAGISKBIN/magiskboot cpio ramdisk.cpio sha1 2>/dev/null) + cp -af ramdisk.cpio ramdisk.cpio.stock.magisk + ;; + 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" + +echo "KEEPVERITY=$KEEPVERITY" > config +echo "KEEPFORCEENCRYPT=$KEEPFORCEENCRYPT" >> config +echo "RECOVERYMODE=$RECOVERYMODE" >> config +[ ! -z $SHA1 ] && echo "SHA1=$SHA1" >> config + +$MAGISKBIN/magiskboot cpio ramdisk.cpio "extract overlay.d/ghid.rc ghid.rc" 2>/dev/null +if ! diff $MODPATH/overlay.d/ghid.rc ghid.rc 2>/dev/null; then + ui_print "- Adding overlay.d/ghid.rc" + + $MAGISKBIN/magiskboot cpio ramdisk.cpio \ + "mkdir 755 overlay.d" \ + "add 755 overlay.d/ghid.rc $MODPATH/overlay.d/ghid.rc" + + rm -f ramdisk.cpio.orig config magisk*.xz + +################# +# Binary Patches +################# + + if [ $((STATUS & 4)) -ne 0 ]; then + ui_print "- Compressing ramdisk" + $MAGISKBIN/magiskboot cpio ramdisk.cpio compress + fi + + rm -f ramdisk.cpio.stock.magisk config ghid.rc +################# +# Repack & Flash +################# + + ui_print "- Repacking boot image" + $MAGISKBIN/magiskboot repack "$BOOTIMAGE" || abort "! Unable to repack boot image!" +# Sign chromeos boot +$CHROMEOS && sign_chromeos + + # Sign chromeos boot + $CHROMEOS && sign_chromeos +else + ui_print "- ghid.rc already installed. Skipping boot patching." + rm -f ramdisk.cpio.stock.magisk config ghid.rc +fi +# Restore the original boot partition path +[ -e "$BOOTNAND" ] && BOOTIMAGE="$BOOTNAND" + +# Reset any error code +true diff --git a/customize.sh b/customize.sh index 0b2c867..dcfdafc 100644 --- a/customize.sh +++ b/customize.sh @@ -1,134 +1,15 @@ #!/bin/sh +$BOOTMODE || abort "! must be installed in the running system" +if [[ -e /proc/config.gz ]]; then + zcat /proc/config.gz | grep -Eq 'CONFIG_USB_(CONFIGFS_)?F_HID=[my]' || abort "! kernel does not have the required modules" +else + [[ -d /config/usb_gadget ]] || abort "! kernel does not have the required modules" + mkdir /config/usb_gadget/g1 || abort "! kernel does not have the required modules" + mkdir /config/usb_gadget/g1/functions/hid.usb0 || abort "! kernel does not have the required modules" +fi 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" +. "$MODPATH/utils.sh" +. "$MODPATH/flash_script.sh" diff --git a/flash_script.sh b/flash_script.sh new file mode 100644 index 0000000..f16c315 --- /dev/null +++ b/flash_script.sh @@ -0,0 +1,60 @@ +#MAGISK +############################################ +# Magisk Flash Script (updater-script) +############################################ + +############## +# Preparation +############## + +find_magisk_apk +# Default permissions +umask 022 + +OUTFD=$2 +APK="$3" +COMMONDIR=$INSTALLER/assets +CHROMEDIR=$INSTALLER/assets/chromeos + +setup_flashable + +############ +# Detection +############ + +if echo $MAGISK_VER | grep -q '\.'; then + PRETTY_VER=$MAGISK_VER +else + PRETTY_VER="$MAGISK_VER($MAGISK_VER_CODE)" +fi +print_title "Magisk $PRETTY_VER Installer" + +is_mounted /data || mount /data || is_mounted /cache || mount /cache +mount_partitions +check_data +get_flags +find_boot_image + +[ -z $BOOTIMAGE ] && abort "! Unable to detect target image" +ui_print "- Target image: $BOOTIMAGE" + +# Detect version and architecture +api_level_arch_detect + +[ $API -lt 21 ] && abort "! Magisk only support Android 5.0 and above" + +ui_print "- Device platform: $ARCH" + + +################## +# Image Patching +################## + +install_magisk + +# Cleanups +$BOOTMODE || recovery_cleanup +rm -rf $TMPDIR + +ui_print "- Done" +exit 0 diff --git a/module.prop b/module.prop index fe79fbf..9d95e04 100644 --- a/module.prop +++ b/module.prop @@ -1,6 +1,6 @@ id=ghid name=ghid -version=0.9 -versionCode=30 +version=0.11 +versionCode=38 author=lordwelch description=creates ghid device files in /dev diff --git a/overlay.d/ghid.rc b/overlay.d/ghid.rc index 6033533..0839314 100644 --- a/overlay.d/ghid.rc +++ b/overlay.d/ghid.rc @@ -4,6 +4,5 @@ on property:sys.usb.ffs.ready=1 && property:sys.usb.config=* && property:sys.usb service ghid /system/bin/ghid.sh setup ${sys.usb.config} user root - seclabel u:r:magisk:s0 disabled oneshot diff --git a/system/bin/ghid.sh b/system/bin/ghid.sh index 99eb4bf..7ea75c5 100644 --- a/system/bin/ghid.sh +++ b/system/bin/ghid.sh @@ -5,6 +5,7 @@ moddir=[MODDIR] selected=keyboard-standard exec >>$moddir/ghid.log exec 2>>$moddir/ghid.log +date set -o xtrace set -o nohup set +o errexit diff --git a/utils.sh b/utils.sh new file mode 100644 index 0000000..570bc13 --- /dev/null +++ b/utils.sh @@ -0,0 +1,63 @@ +# get_flags() { +# # override variables +# getvar KEEPVERITY +# getvar KEEPFORCEENCRYPT +# getvar RECOVERYMODE +# if [ -z $KEEPVERITY ]; then +# if $SYSTEM_ROOT; then +# KEEPVERITY=true +# ui_print "- System-as-root, keep dm/avb-verity" +# else +# KEEPVERITY=false +# fi +# fi +# ISENCRYPTED=false +# grep ' /data ' /proc/mounts | grep -q 'dm-' && ISENCRYPTED=true +# [ "$(getprop ro.crypto.state)" = "encrypted" ] && ISENCRYPTED=true +# if [ -z $KEEPFORCEENCRYPT ]; then +# # No data access means unable to decrypt in recovery +# if $ISENCRYPTED || ! $DATA; then +# KEEPFORCEENCRYPT=true +# ui_print "- Encrypted data, keep forceencrypt" +# else +# KEEPFORCEENCRYPT=false +# fi +# fi +# [ -z $RECOVERYMODE ] && RECOVERYMODE=false +# } + +install_magisk() { + cd $TMPDIR + + mkdir boot_patching + cd boot_patching/ + + # Dump image for MTD/NAND character device boot partitions + if [ -c $BOOTIMAGE ]; then + nanddump -f boot.img $BOOTIMAGE + local BOOTNAND=$BOOTIMAGE + BOOTIMAGE=boot.img + fi + + if [ $API -ge 21 ]; then + eval $BOOTSIGNER -verify < $BOOTIMAGE && BOOTSIGNED=true + $BOOTSIGNED && ui_print "- Boot image is signed with AVB 1.0" + fi + + # Source the boot patcher + . "$MODPATH/boot_patch.sh" + + if [ -f new-boot.img ]; then + ui_print "- Flashing new boot image" + + # Restore the original boot partition path + [ "$BOOTNAND" ] && BOOTIMAGE=$BOOTNAND + flash_image new-boot.img "$BOOTIMAGE" || abort "! Insufficient partition size" + + rm -f new-boot.img + + run_migrations + fi + + $MAGISKBIN/magiskboot cleanup +}