From d12d9e82f1b688215e3d33b356b24296623e64bb Mon Sep 17 00:00:00 2001 From: vvb2060 Date: Sun, 9 Jul 2023 23:53:59 +0800 Subject: [PATCH] Force kernel to load rootfs only for legacy SAR devices --- app/src/main/java/com/topjohnwu/magisk/core/Info.kt | 1 + .../topjohnwu/magisk/core/tasks/MagiskInstaller.kt | 2 +- .../java/com/topjohnwu/magisk/core/utils/ShellInit.kt | 1 + app/src/main/res/raw/manager.sh | 9 +++++++-- scripts/boot_patch.sh | 6 +++--- scripts/util_functions.sh | 11 ++++++++++- 6 files changed, 23 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/com/topjohnwu/magisk/core/Info.kt b/app/src/main/java/com/topjohnwu/magisk/core/Info.kt index 7441497a7..5168598b8 100644 --- a/app/src/main/java/com/topjohnwu/magisk/core/Info.kt +++ b/app/src/main/java/com/topjohnwu/magisk/core/Info.kt @@ -28,6 +28,7 @@ object Info { // Device state @JvmStatic val env by lazy { loadState() } @JvmField var isSAR = false + var legacySAR = false var isAB = false @JvmField val isZygiskEnabled = System.getenv("ZYGISK_ENABLED") == "1" @JvmStatic val isFDE get() = crypto == "block" diff --git a/app/src/main/java/com/topjohnwu/magisk/core/tasks/MagiskInstaller.kt b/app/src/main/java/com/topjohnwu/magisk/core/tasks/MagiskInstaller.kt index 314a46c4c..c1b1c1d86 100644 --- a/app/src/main/java/com/topjohnwu/magisk/core/tasks/MagiskInstaller.kt +++ b/app/src/main/java/com/topjohnwu/magisk/core/tasks/MagiskInstaller.kt @@ -492,7 +492,7 @@ abstract class MagiskInstallImpl protected constructor( "KEEPVERITY=${Config.keepVerity} " + "PATCHVBMETAFLAG=${Config.patchVbmeta} " + "RECOVERYMODE=${Config.recovery} " + - "SYSTEM_ROOT=${Info.isSAR} " + + "LEGACYSAR=${Info.legacySAR} " + "sh boot_patch.sh $srcBoot") val isSuccess = cmds.sh().isSuccess diff --git a/app/src/main/java/com/topjohnwu/magisk/core/utils/ShellInit.kt b/app/src/main/java/com/topjohnwu/magisk/core/utils/ShellInit.kt index f4c7b630e..0eb724959 100644 --- a/app/src/main/java/com/topjohnwu/magisk/core/utils/ShellInit.kt +++ b/app/src/main/java/com/topjohnwu/magisk/core/utils/ShellInit.kt @@ -78,6 +78,7 @@ class ShellInit : Shell.Initializer() { Info.vbmeta = getBool("VBMETAEXIST") Info.isAB = getBool("ISAB") Info.crypto = getVar("CRYPTOTYPE") + Info.legacySAR = getBool("LEGACYSAR") // Default presets Config.recovery = getBool("RECOVERYMODE") diff --git a/app/src/main/res/raw/manager.sh b/app/src/main/res/raw/manager.sh index 57f694d04..2b3104abe 100644 --- a/app/src/main/res/raw/manager.sh +++ b/app/src/main/res/raw/manager.sh @@ -154,7 +154,7 @@ check_boot_ramdisk() { $ISAB && return 0 # If we are using legacy SAR, but not A/B, assume we do not have ramdisk - if grep ' / ' /proc/mounts | grep -q '/dev/root'; then + if $LEGACYSAR; then # Override recovery mode to true RECOVERYMODE=true return 1 @@ -211,6 +211,11 @@ get_flags() { PATCHVBMETAFLAG=false # Make sure RECOVERYMODE has value [ -z $RECOVERYMODE ] && RECOVERYMODE=false + if grep ' / ' /proc/mounts | grep -q '/dev/root'; then + LEGACYSAR=true + else + LEGACYSAR=false + fi } run_migrations() { return; } @@ -223,9 +228,9 @@ grep_prop() { return; } app_init() { mount_partitions + get_flags RAMDISKEXIST=false check_boot_ramdisk && RAMDISKEXIST=true - get_flags run_migrations SHA1=$(grep_prop SHA1 $MAGISKTMP/.magisk/config) check_encryption diff --git a/scripts/boot_patch.sh b/scripts/boot_patch.sh index abeb78785..57fc3d4ee 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, SYSTEM_ROOT +# KEEPVERITY, KEEPFORCEENCRYPT, PATCHVBMETAFLAG, RECOVERYMODE, LEGACYSAR # # This script should be placed in a directory with the following files: # @@ -73,7 +73,7 @@ fi [ -z $KEEPFORCEENCRYPT ] && KEEPFORCEENCRYPT=false [ -z $PATCHVBMETAFLAG ] && PATCHVBMETAFLAG=false [ -z $RECOVERYMODE ] && RECOVERYMODE=false -[ -z $SYSTEM_ROOT ] && SYSTEM_ROOT=false +[ -z $LEGACYSAR ] && LEGACYSAR=false export KEEPVERITY export KEEPFORCEENCRYPT export PATCHVBMETAFLAG @@ -233,7 +233,7 @@ if [ -f kernel ]; then # Force kernel to load rootfs for legacy SAR devices # skip_initramfs -> want_initramfs - $SYSTEM_ROOT && ./magiskboot hexpatch kernel \ + $LEGACYSAR && ./magiskboot hexpatch kernel \ 736B69705F696E697472616D667300 \ 77616E745F696E697472616D667300 \ && PATCHEDKERNEL=true diff --git a/scripts/util_functions.sh b/scripts/util_functions.sh index 689f7177e..a453911c0 100644 --- a/scripts/util_functions.sh +++ b/scripts/util_functions.sh @@ -312,7 +312,7 @@ mount_partitions() { # After calling this method, the following variables will be set: # KEEPVERITY, KEEPFORCEENCRYPT, RECOVERYMODE, PATCHVBMETAFLAG, -# ISENCRYPTED, VBMETAEXIST +# ISENCRYPTED, VBMETAEXIST, LEGACYSAR get_flags() { getvar KEEPVERITY getvar KEEPFORCEENCRYPT @@ -350,6 +350,15 @@ get_flags() { fi fi [ -z $RECOVERYMODE ] && RECOVERYMODE=false + local IS_DYNAMIC=false + grep -q 'androidboot.super_partition' /proc/cmdline && IS_DYNAMIC=true + [ -b "/dev/block/by-name/super" ] && IS_DYNAMIC=true + if $SYSTEM_ROOT && ! $IS_DYNAMIC; then + LEGACYSAR=true + ui_print "- legacy SAR, force kernel to load rootfs" + else + LEGACYSAR=false + fi } find_boot_image() {