From f37e8f4ca8b863ad9dd61d6e38b888a9c8f146dc Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Sun, 7 Feb 2021 01:54:08 -0800 Subject: [PATCH] Fix boot image patching --- .../magisk/core/tasks/MagiskInstaller.kt | 52 ++++++------------- .../topjohnwu/magisk/core/utils/ShellInit.kt | 12 +---- scripts/boot_patch.sh | 1 + 3 files changed, 17 insertions(+), 48 deletions(-) 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 e3283285c..e98ca54a4 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 @@ -9,8 +9,6 @@ import com.topjohnwu.magisk.BuildConfig import com.topjohnwu.magisk.DynAPK import com.topjohnwu.magisk.R import com.topjohnwu.magisk.core.* -import com.topjohnwu.magisk.core.utils.BusyBoxInit -import com.topjohnwu.magisk.core.utils.LightShellInit import com.topjohnwu.magisk.core.utils.MediaStoreUtils import com.topjohnwu.magisk.core.utils.MediaStoreUtils.inputStream import com.topjohnwu.magisk.core.utils.MediaStoreUtils.outputStream @@ -52,9 +50,7 @@ abstract class MagiskInstallImpl protected constructor( protected var installDir = File("xxx") private lateinit var srcBoot: File - private lateinit var shell: Shell - private var closeShell = false - + private val shell = Shell.getShell() private val service: NetworkService by inject() protected val context: Context by inject(Protected) @@ -298,7 +294,7 @@ abstract class MagiskInstallImpl protected constructor( val newBootImg: File if (inRootDir) { - // Migrate everything to tmpfs to workaround Samsung bullshit + // Move everything to tmpfs to workaround Samsung bullshit SuFile("${Const.TMPDIR}/install").also { arrayOf( "rm -rf $it", @@ -311,8 +307,9 @@ abstract class MagiskInstallImpl protected constructor( newBootImg = SuFile(installDir, "new-boot.img") } else { newBootImg = File(installDir, "new-boot.img") - // Create the output file before hand + // Create output files before hand newBootImg.createNewFile() + File(installDir, "stock_boot.img").createNewFile() } var isSigned = false @@ -330,12 +327,14 @@ abstract class MagiskInstallImpl protected constructor( } } - val flags = + val cmds = arrayOf( + "cd $installDir", "KEEPFORCEENCRYPT=${Config.keepEnc} " + "KEEPVERITY=${Config.keepVerity} " + - "RECOVERYMODE=${Config.recovery}" + "RECOVERYMODE=${Config.recovery} " + + "sh boot_patch.sh $srcBoot") - if (!"cd $installDir; $flags sh boot_patch.sh $srcBoot".sh().isSuccess) + if (!cmds.sh().isSuccess) return false val job = shell.newJob().add("./magiskboot cleanup", "cd /") @@ -383,47 +382,26 @@ abstract class MagiskInstallImpl protected constructor( return true } - private fun setupShell(forceNonRoot: Boolean = false): Boolean { - shell = Shell.getShell() - if (forceNonRoot && shell.isRoot) { - shell = Shell.Builder.create() - .setFlags(Shell.FLAG_NON_ROOT_SHELL) - .setInitializers(BusyBoxInit::class.java, LightShellInit::class.java) - .build() - closeShell = true - } - return true - } - private fun String.sh() = shell.newJob().add(this).to(console, logs).exec() private fun Array.sh() = shell.newJob().add(*this).to(console, logs).exec() private fun String.fsh() = ShellUtils.fastCmd(shell, this) private fun Array.fsh() = ShellUtils.fastCmd(shell, *this) - protected fun doPatchFile(patchFile: Uri) = - setupShell(true) && extractFiles() && handleFile(patchFile) + protected fun doPatchFile(patchFile: Uri) = extractFiles() && handleFile(patchFile) - protected fun direct() = - setupShell() && findImage() && extractFiles() && patchBoot() && flashBoot() + protected fun direct() = findImage() && extractFiles() && patchBoot() && flashBoot() protected suspend fun secondSlot() = - setupShell() && findSecondary() && extractFiles() && patchBoot() && flashBoot() && postOTA() + findSecondary() && extractFiles() && patchBoot() && flashBoot() && postOTA() - protected fun fixEnv() = - setupShell() && extractFiles() && "fix_env $installDir".sh().isSuccess + protected fun fixEnv() = extractFiles() && "fix_env $installDir".sh().isSuccess - protected fun uninstall() = - setupShell() && "run_uninstaller ${AssetHack.apk}".sh().isSuccess + protected fun uninstall() = "run_uninstaller ${AssetHack.apk}".sh().isSuccess @WorkerThread protected abstract suspend fun operations(): Boolean - open suspend fun exec() = withContext(Dispatchers.IO) { - val result = operations() - if (closeShell) - shell.close() - result - } + open suspend fun exec() = withContext(Dispatchers.IO) { operations() } } abstract class MagiskInstaller( 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 177ae2fa3..ec7fff286 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 @@ -34,6 +34,7 @@ class BusyBoxInit : BaseShellInit() { val jar = JarFile(DynAPK.current(context)) val bb = jar.getJarEntry("lib/${Const.CPU_ABI_32}/libbusybox.so") localBB = context.deviceProtectedContext.cachedFile("busybox") + localBB.delete() jar.getInputStream(bb).writeTo(localBB) localBB.setExecutable(true) } else { @@ -66,17 +67,6 @@ class BusyBoxInit : BaseShellInit() { } } -class LightShellInit : BaseShellInit() { - - override fun init(context: Context, shell: Shell): Boolean { - shell.newJob().apply { - add("export SDK_INT=${Build.VERSION.SDK_INT}") - add(context.rawResource(R.raw.manager)) - }.exec() - return true - } -} - class AppShellInit : BaseShellInit() { override fun init(context: Context, shell: Shell): Boolean { diff --git a/scripts/boot_patch.sh b/scripts/boot_patch.sh index 4c2c9bb22..e0daab2f0 100644 --- a/scripts/boot_patch.sh +++ b/scripts/boot_patch.sh @@ -123,6 +123,7 @@ case $((STATUS & 3)) in [ -z $SHA1 ] && SHA1=$(./magiskboot cpio ramdisk.cpio sha1 2>/dev/null) ./magiskboot cpio ramdisk.cpio restore cp -af ramdisk.cpio ramdisk.cpio.orig + rm -f stock_boot.img ;; 2 ) # Unsupported ui_print "! Boot image patched by unsupported programs"