Update several scripts

- Update backup format as we might be patching multiple partitions
- Update uninstaller to remove files in persist (sepolicy.rule)
- Better handling for dtb/dtbo partition patching
This commit is contained in:
topjohnwu 2020-01-01 14:02:44 +08:00
parent 2db1e5cb74
commit 3049a81c3b
9 changed files with 129 additions and 101 deletions

View File

@ -287,8 +287,10 @@ abstract class MagiskInstaller {
protected fun flashBoot(): Boolean { protected fun flashBoot(): Boolean {
if (!"direct_install $installDir $srcBoot".sh().isSuccess) if (!"direct_install $installDir $srcBoot".sh().isSuccess)
return false return false
if (!Info.keepVerity) arrayOf(
"patch_dtbo_image".sh() "(KEEPVERITY=${Info.keepVerity} patch_dtb_partitions)",
"run_migrations"
).sh()
return true return true
} }

View File

@ -49,7 +49,7 @@ open class SplashActivity : Activity() {
// Setup shortcuts // Setup shortcuts
Shortcuts.setup(this) Shortcuts.setup(this)
Shell.su("mm_patch_dtbo").submit { Shell.su("mm_patch_dtb").submit {
if (it.isSuccess) if (it.isSuccess)
Notifications.dtboPatched(this) Notifications.dtboPatched(this)
} }

View File

@ -29,11 +29,12 @@ class RootInit : Shell.Initializer() {
job.add(context.rawResource(R.raw.nonroot_utils)) job.add(context.rawResource(R.raw.nonroot_utils))
} }
job.add("mount_partitions", job.add(
"get_flags", "mount_partitions",
"run_migrations", "get_flags",
"export BOOTMODE=true") "run_migrations",
.exec() "export BOOTMODE=true"
).exec()
Info.keepVerity = ShellUtils.fastCmd("echo \$KEEPVERITY").toBoolean() Info.keepVerity = ShellUtils.fastCmd("echo \$KEEPVERITY").toBoolean()
Info.keepEnc = ShellUtils.fastCmd("echo \$KEEPFORCEENCRYPT").toBoolean() Info.keepEnc = ShellUtils.fastCmd("echo \$KEEPFORCEENCRYPT").toBoolean()

View File

@ -17,16 +17,6 @@ fix_env() {
cd / cd /
} }
run_migrations() {
# Move the stock backups
if [ -f /data/magisk/stock_boot* ]; then
mv /data/magisk/stock_boot* /data 2>/dev/null
fi
if [ -f /data/adb/magisk/stock_boot* ]; then
mv /data/adb/magisk/stock_boot* /data 2>/dev/null
fi
}
direct_install() { direct_install() {
rm -rf $MAGISKBIN/* 2>/dev/null rm -rf $MAGISKBIN/* 2>/dev/null
mkdir -p $MAGISKBIN 2>/dev/null mkdir -p $MAGISKBIN 2>/dev/null
@ -43,30 +33,43 @@ direct_install() {
return 0 return 0
} }
mm_patch_dtbo() { mm_patch_dtb() {
$KEEPVERITY && return 1 || patch_dtbo_image local result=1
local PATCHED=$TMPDIR/dt.patched
for name in dtb dtbo; do
local IMAGE=`find_block $name$SLOT`
if [ ! -z $IMAGE ]; then
if $MAGISKBIN/magiskboot dtb $IMAGE patch $PATCHED; then
result=0
if [ ! -z $SHA1 ]; then
# Backup stuffs
mkdir /data/magisk_backup_${SHA1} 2>/dev/null
cat $IMAGE | gzip -9 > /data/magisk_backup_${SHA1}/${name}.img.gz
fi
cat $PATCHED /dev/zero > $IMAGE
rm -f $PATCHED
fi
fi
done
return $result
} }
restore_imgs() { restore_imgs() {
local SHA1=`grep_prop SHA1 /sbin/.magisk/config`
[ -z $SHA1 ] && local SHA1=`cat /.backup/.sha1`
[ -z $SHA1 ] && return 1 [ -z $SHA1 ] && return 1
local STOCKBOOT=/data/stock_boot_${SHA1}.img.gz local BACKUPDIR=/data/magisk_backup_$SHA1
local STOCKDTBO=/data/stock_dtbo.img.gz [ -d $BACKUPDIR ] || return 1
[ -f $STOCKBOOT ] || return 1
get_flags get_flags
find_boot_image find_boot_image
find_dtbo_image
if [ -f $STOCKDTBO -a -b "$DTBOIMAGE" ]; then for name in dtb dtbo; do
flash_image $STOCKDTBO $DTBOIMAGE [ -f $BACKUPDIR/${name}.img.gz ] || continue
fi local IMAGE=`find_block $name$SLOT`
if [ -f $STOCKBOOT -a -b "$BOOTIMAGE" ]; then [ -z $IMAGE ] && continue
flash_image $STOCKBOOT $BOOTIMAGE flash_image $BACKUPDIR/${name}.img.gz $IMAGE
return 0 done
fi [ -f $BACKUPDIR/boot.img.gz ] || return 1
return 1 flash_image $BACKUPDIR/boot.img.gz $BOOTIMAGE
} }
post_ota() { post_ota() {
@ -119,3 +122,5 @@ force_pm_install() {
[ "$VERIFY" -eq 1 ] && settings put global package_verifier_enable 1 [ "$VERIFY" -eq 1 ] && settings put global package_verifier_enable 1
return $res return $res
} }
SHA1=`grep_prop SHA1 /sbin/.magisk/config`

View File

@ -70,9 +70,8 @@ main() {
remove_system_su remove_system_su
find_manager_apk find_manager_apk
patch_boot_image install_magisk
cd /
# Cleanups # Cleanups
$BOOTMODE || recovery_cleanup $BOOTMODE || recovery_cleanup
rm -rf $TMPDIR rm -rf $TMPDIR

View File

@ -13,20 +13,15 @@
# #
# File name Type Description # File name Type Description
# #
# boot_patch.sh script A script to patch boot. Expect path to boot image as parameter. # boot_patch.sh script A script to patch boot image for Magisk.
# (this file) The script will use binaries and files in its same directory # (this file) The script will use binaries and files in its same directory
# to complete the patching process # to complete the patching process
# util_functions.sh script A script which hosts all functions requires for this script # util_functions.sh script A script which hosts all functions required for this script
# to work properly # to work properly
# magiskinit binary The binary to replace /init, which has the magisk binary embedded # magiskinit binary The binary to replace /init; magisk binary embedded
# magiskboot binary A tool to unpack boot image, decompress ramdisk, extract ramdisk, # magiskboot binary A tool to manipulate boot images
# and patch the ramdisk for Magisk support # chromeos folder This folder includes all the utilities and keys to sign
# chromeos folder This folder should store all the utilities and keys to sign # (optional) chromeos boot images. Currently only used for Pixel C
# (optional) a chromeos device. Used for Pixel C
#
# If the script is not running as root, then the input boot image should be a stock image
# or have a backup included in ramdisk internally, since we cannot access the stock boot
# image placed under /data we've created when previously installed
# #
########################################################################################## ##########################################################################################
########################################################################################## ##########################################################################################
@ -104,10 +99,8 @@ fi
case $((STATUS & 3)) in case $((STATUS & 3)) in
0 ) # Stock boot 0 ) # Stock boot
ui_print "- Stock boot image detected" ui_print "- Stock boot image detected"
ui_print "- Backing up stock boot image"
SHA1=`./magiskboot sha1 "$BOOTIMAGE" 2>/dev/null` SHA1=`./magiskboot sha1 "$BOOTIMAGE" 2>/dev/null`
STOCKDUMP=stock_boot_${SHA1}.img.gz cat $BOOTIMAGE > stock_boot.img
./magiskboot compress "$BOOTIMAGE" $STOCKDUMP
cp -af ramdisk.cpio ramdisk.cpio.orig 2>/dev/null cp -af ramdisk.cpio ramdisk.cpio.orig 2>/dev/null
;; ;;
1 ) # Magisk patched 1 ) # Magisk patched
@ -158,7 +151,7 @@ rm -f ramdisk.cpio.orig config
########################################################################################## ##########################################################################################
for dt in dtb kernel_dtb extra recovery_dtbo; do for dt in dtb kernel_dtb extra recovery_dtbo; do
[ -f $dt ] && ./magiskboot dtb $dt patch && ui_print "- Patching fstab in $dt" [ -f $dt ] && ./magiskboot dtb $dt patch && ui_print "- Patch fstab in $dt"
done done
if [ -f kernel ]; then if [ -f kernel ]; then

View File

@ -53,7 +53,7 @@ ui_print "- Target image: $BOOTIMAGE"
# Detect version and architecture # Detect version and architecture
api_level_arch_detect api_level_arch_detect
[ $API -lt 17 ] && abort "! Magisk is only for Android 4.2 and above" [ $API -lt 17 ] && abort "! Magisk only support Android 4.2 and above"
ui_print "- Device platform: $ARCH" ui_print "- Device platform: $ARCH"
@ -108,9 +108,8 @@ $BOOTMODE || recovery_actions
# Boot/DTBO Patching # Boot/DTBO Patching
########################################################################################## ##########################################################################################
patch_boot_image install_magisk
cd /
# Cleanups # Cleanups
$BOOTMODE || recovery_cleanup $BOOTMODE || recovery_cleanup
rm -rf $TMPDIR rm -rf $TMPDIR

View File

@ -15,6 +15,7 @@ TMPDIR=/dev/tmp
INSTALLER=$TMPDIR/install INSTALLER=$TMPDIR/install
CHROMEDIR=$INSTALLER/chromeos CHROMEDIR=$INSTALLER/chromeos
PERSISTDIR=/sbin/.magisk/mirror/persist
# Default permissions # Default permissions
umask 022 umask 022
@ -50,6 +51,7 @@ chmod -R 755 $MAGISKBIN
check_data check_data
$DATA_DE || abort "! Cannot access /data, please uninstall with Magisk Manager" $DATA_DE || abort "! Cannot access /data, please uninstall with Magisk Manager"
$BOOTMODE || recovery_actions $BOOTMODE || recovery_actions
run_migrations
########################################################################################## ##########################################################################################
# Uninstall # Uninstall
@ -57,7 +59,6 @@ $BOOTMODE || recovery_actions
get_flags get_flags
find_boot_image find_boot_image
find_dtbo_image
[ -e $BOOTIMAGE ] || abort "! Unable to detect boot image" [ -e $BOOTIMAGE ] || abort "! Unable to detect boot image"
ui_print "- Found target image: $BOOTIMAGE" ui_print "- Found target image: $BOOTIMAGE"
@ -96,16 +97,18 @@ case $((STATUS & 3)) in
1 ) # Magisk patched 1 ) # Magisk patched
ui_print "- Magisk patched image detected" ui_print "- Magisk patched image detected"
# Find SHA1 of stock boot image # Find SHA1 of stock boot image
[ -z $SHA1 ] && SHA1=`./magiskboot cpio ramdisk.cpio sha1 2>/dev/null` SHA1=`./magiskboot cpio ramdisk.cpio sha1 2>/dev/null`
STOCKBOOT=/data/stock_boot_${SHA1}.img.gz BACKUPDIR=/data/magisk_backup_$SHA1
STOCKDTBO=/data/stock_dtbo.img.gz if [ -d $BACKUPDIR ]; then
if [ -f $STOCKBOOT ]; then
ui_print "- Restoring stock boot image" ui_print "- Restoring stock boot image"
flash_image $STOCKBOOT $BOOTIMAGE flash_image $BACKUPDIR/boot.img.gz $BOOTIMAGE
if [ -f $STOCKDTBO -a -b "$DTBOIMAGE" ]; then for name in dtb dtbo; do
ui_print "- Restoring stock dtbo image" [ -f $BACKUPDIR/${name}.img.gz ] || continue
flash_image $STOCKDTBO $DTBOIMAGE IMAGE=`find_block $name$SLOT`
fi [ -z $IMAGE ] && continue
ui_print "- Restoring stock $name image"
flash_image $BACKUPDIR/${name}.img.gz $IMAGE
done
else else
ui_print "! Boot image backup unavailable" ui_print "! Boot image backup unavailable"
ui_print "- Restoring ramdisk with internal backup" ui_print "- Restoring ramdisk with internal backup"
@ -128,9 +131,10 @@ case $((STATUS & 3)) in
esac esac
ui_print "- Removing Magisk files" ui_print "- Removing Magisk files"
rm -rf /cache/*magisk* /cache/unblock /data/*magisk* /data/cache/*magisk* /data/property/*magisk* \ rm -rf \
/data/Magisk.apk /data/busybox /data/custom_ramdisk_patch.sh /data/adb/*magisk* \ /cache/*magisk* /cache/unblock /data/*magisk* /data/cache/*magisk* /data/property/*magisk* \
/data/adb/post-fs-data.d /data/adb/service.d /data/adb/modules* 2>/dev/null /data/Magisk.apk /data/busybox /data/custom_ramdisk_patch.sh /data/adb/*magisk* \
/data/adb/post-fs-data.d /data/adb/service.d /data/adb/modules* $PERSISTDIR/magisk 2>/dev/null
if [ -f /system/addon.d/99-magisk.sh ]; then if [ -f /system/addon.d/99-magisk.sh ]; then
mount -o rw,remount /system mount -o rw,remount /system

View File

@ -110,8 +110,8 @@ recovery_actions() {
recovery_cleanup() { recovery_cleanup() {
ui_print "- Unmounting partitions" ui_print "- Unmounting partitions"
umount -l /system_root 2>/dev/null
umount -l /system 2>/dev/null umount -l /system 2>/dev/null
umount -l /system_root 2>/dev/null
umount -l /vendor 2>/dev/null umount -l /vendor 2>/dev/null
umount -l /dev/random 2>/dev/null umount -l /dev/random 2>/dev/null
export PATH=$OLD_PATH export PATH=$OLD_PATH
@ -196,7 +196,7 @@ mount_partitions() {
[ -L /system/vendor ] && mount_ro_ensure vendor [ -L /system/vendor ] && mount_ro_ensure vendor
$SYSTEM_ROOT && ui_print "- Device is system-as-root" $SYSTEM_ROOT && ui_print "- Device is system-as-root"
# Persist partitions for module install in recovery # Mount persist partition in recovery
if ! $BOOTMODE && [ ! -z $PERSISTDIR ]; then if ! $BOOTMODE && [ ! -z $PERSISTDIR ]; then
# Try to mount persist # Try to mount persist
PERSISTDIR=/persist PERSISTDIR=/persist
@ -276,30 +276,29 @@ flash_image() {
return 0 return 0
} }
find_dtbo_image() { patch_dtb_partitions() {
DTBOIMAGE=`find_block dtbo$SLOT` local result=1
} cd $MAGISKBIN
for name in dtb dtbo; do
patch_dtbo_image() { local IMAGE=`find_block $name$SLOT`
find_dtbo_image if [ ! -z $IMAGE ]; then
if [ ! -z $DTBOIMAGE ]; then ui_print "- $name image: $IMAGE"
ui_print "- DTBO image: $DTBOIMAGE" if ./magiskboot dtb $IMAGE patch dt.patched; then
local PATCHED=$TMPDIR/dtbo result=0
if $MAGISKBIN/magiskboot dtb $DTBOIMAGE patch $PATCHED; then ui_print "- Backing up stock $name image"
ui_print "- Backing up stock DTBO image" cat $IMAGE > stock_${name}.img
$MAGISKBIN/magiskboot compress $DTBOIMAGE $MAGISKBIN/stock_dtbo.img.gz ui_print "- Flashing patched $name"
ui_print "- Patching DTBO to remove avb-verity" cat dt.patched /dev/zero > $IMAGE
cat $PATCHED /dev/zero > $DTBOIMAGE rm -f dt.patched
rm -f $PATCHED fi
return 0
fi fi
fi done
return 1 cd /
return $result
} }
# Common installation script for flash_script.sh and addon.d.sh # Common installation script for flash_script.sh and addon.d.sh
patch_boot_image() { install_magisk() {
SOURCEDMODE=true
cd $MAGISKBIN cd $MAGISKBIN
eval $BOOTSIGNER -verify < $BOOTIMAGE && BOOTSIGNED=true eval $BOOTSIGNER -verify < $BOOTIMAGE && BOOTSIGNED=true
@ -308,6 +307,7 @@ patch_boot_image() {
$IS64BIT && mv -f magiskinit64 magiskinit 2>/dev/null || rm -f magiskinit64 $IS64BIT && mv -f magiskinit64 magiskinit 2>/dev/null || rm -f magiskinit64
# Source the boot patcher # Source the boot patcher
SOURCEDMODE=true
. ./boot_patch.sh "$BOOTIMAGE" . ./boot_patch.sh "$BOOTIMAGE"
ui_print "- Flashing new boot image" ui_print "- Flashing new boot image"
@ -322,18 +322,8 @@ patch_boot_image() {
./magiskboot cleanup ./magiskboot cleanup
rm -f new-boot.img rm -f new-boot.img
if [ -f stock_boot* ]; then patch_dtb_partitions
rm -f /data/stock_boot* 2>/dev/null run_migrations
$DATA && mv stock_boot* /data
fi
# Patch DTBO together with boot image
$KEEPVERITY || patch_dtbo_image
if [ -f stock_dtbo* ]; then
rm -f /data/stock_dtbo* 2>/dev/null
$DATA && mv stock_dtbo* /data
fi
} }
sign_chromeos() { sign_chromeos() {
@ -415,6 +405,41 @@ find_manager_apk() {
[ -f $APK ] || ui_print "! Unable to detect Magisk Manager APK for BootSigner" [ -f $APK ] || ui_print "! Unable to detect Magisk Manager APK for BootSigner"
} }
run_migrations() {
local LOCSHA1
local TARGET
# Legacy app installation
local BACKUP=/data/adb/magisk/stock_boot*.gz
if [ -f $BACKUP ]; then
cp $BACKUP /data
rm -f $BACKUP
fi
# Legacy backup
for gz in /data/stock_boot*.gz; do
[ -f $gz ] || break
LOCSHA1=`basename $gz | sed -e 's/stock_boot_//' -e 's/.img.gz//'`
[ -z $LOCSHA1 ] && break
mkdir /data/magisk_backup_${LOCSHA1} 2>/dev/null
mv $gz /data/magisk_backup_${LOCSHA1}/boot.img.gz
done
# Stock backups
LOCSHA1=$SHA1
for name in boot dtb dtbo; do
BACKUP=/data/adb/magisk/stock_${name}.img
[ -f $BACKUP ] || continue
if [ $name = 'boot' ]; then
LOCSHA1=`$MAGISKBIN/magiskboot sha1 $BACKUP`
mkdir /data/magisk_backup_${LOCSHA1} 2>/dev/null
fi
TARGET=/data/magisk_backup_${LOCSHA1}/${name}.img
cp $BACKUP $TARGET
rm -f $BACKUP
gzip -9f $TARGET
done
}
################# #################
# Module Related # Module Related
################# #################