Update root shell preparation

This commit is contained in:
topjohnwu 2020-02-10 16:31:41 -08:00
parent 5fb071d80b
commit e85d5e54e2
5 changed files with 63 additions and 31 deletions

View File

@ -8,7 +8,7 @@ import com.topjohnwu.magisk.extensions.subscribeK
import com.topjohnwu.magisk.utils.CachedValue
import com.topjohnwu.magisk.utils.KObservableField
import com.topjohnwu.superuser.Shell
import com.topjohnwu.superuser.ShellUtils
import com.topjohnwu.superuser.ShellUtils.fastCmd
import java.io.FileInputStream
import java.io.IOException
@ -56,12 +56,11 @@ object Info {
}
}
private fun loadState() = runCatching {
val str = ShellUtils.fastCmd("magisk -v").split(":".toRegex())[0]
val code = ShellUtils.fastCmd("magisk -V").toInt()
val hide = Shell.su("magiskhide --status").exec().isSuccess
Env(str, code, hide)
}.getOrElse { Env() }
private fun loadState() = Env(
fastCmd("magisk -v").split(":".toRegex())[0],
runCatching { fastCmd("magisk -V").toInt() }.getOrDefault(-1),
Shell.su("magiskhide --status").exec().isSuccess
)
class Env(
val magiskVersionString: String = "",
@ -71,7 +70,7 @@ object Info {
val magiskHide get() = Config.magiskHide
val magiskVersionCode = when (code) {
in Int.MIN_VALUE..Const.Version.MIN_VERCODE -> -1
else -> if(Shell.rootAccess()) code else -1
else -> if (Shell.rootAccess()) code else -1
}
val isUnsupported = code > 0 && code < Const.Version.MIN_VERCODE
val isActive = magiskVersionCode >= 0

View File

@ -4,12 +4,12 @@ import android.app.Activity
import android.content.Context
import android.os.Bundle
import com.topjohnwu.magisk.BuildConfig
import com.topjohnwu.magisk.core.tasks.patchDTB
import com.topjohnwu.magisk.core.utils.Utils
import com.topjohnwu.magisk.core.view.Notifications
import com.topjohnwu.magisk.core.view.Shortcuts
import com.topjohnwu.magisk.model.navigation.Navigation
import com.topjohnwu.superuser.Shell
import com.topjohnwu.superuser.ShellUtils
open class SplashActivity : Activity() {
@ -22,7 +22,7 @@ open class SplashActivity : Activity() {
Shell.getShell { initAndStart() }
}
private fun initAndStart() {
private fun handleRepackage() {
val pkg = Config.suManager
if (Config.suManager.isNotEmpty() && packageName == BuildConfig.APPLICATION_ID) {
Config.suManager = ""
@ -35,31 +35,17 @@ open class SplashActivity : Activity() {
Shell.su("pm uninstall " + BuildConfig.APPLICATION_ID).submit()
}
}
}
Info.keepVerity = ShellUtils.fastCmd("echo \$KEEPVERITY").toBoolean()
Info.keepEnc = ShellUtils.fastCmd("echo \$KEEPFORCEENCRYPT").toBoolean()
Info.recovery = ShellUtils.fastCmd("echo \$RECOVERYMODE").toBoolean()
// Set default configs
private fun initAndStart() {
Config.initialize()
// Create notification channel on Android O
handleRepackage()
Notifications.setup(this)
// Schedule periodic update checks
Utils.scheduleUpdateCheck(this)
// Setup shortcuts
Shortcuts.setup(this)
if (Info.isNewReboot) {
val shell = Shell.newInstance()
shell.newJob().add("mm_patch_dtb").submit {
if (it.isSuccess)
Notifications.dtboPatched(this)
shell.close()
}
}
// Patch DTB partitions if needed
patchDTB(this)
DONE = true
Navigation.start(intent, this)

View File

@ -0,0 +1,37 @@
package com.topjohnwu.magisk.core.tasks
import android.content.Context
import android.content.ContextWrapper
import android.content.Intent
import android.content.IntentFilter
import com.topjohnwu.magisk.core.Const
import com.topjohnwu.magisk.core.Info
import com.topjohnwu.magisk.core.base.BaseReceiver
import com.topjohnwu.magisk.core.view.Notifications
import com.topjohnwu.superuser.Shell
import timber.log.Timber
private const val DTB_PATCH_RESULT = "dtb_result"
private const val DTB_PATCH_ACTION = "com.topjohnwu.magisk.DTBO_PATCH"
private class DTBPatchReceiver : BaseReceiver() {
override fun onReceive(context: ContextWrapper, intent: Intent?) {
intent?.also {
val result = it.getIntExtra(DTB_PATCH_RESULT, 1)
Timber.d("result=[$result]")
if (result == 0)
Notifications.dtboPatched(context)
}
context.unregisterReceiver(this)
}
}
fun patchDTB(context: Context) {
if (Info.isNewReboot) {
val c = context.applicationContext
c.registerReceiver(DTBPatchReceiver(), IntentFilter(DTB_PATCH_ACTION))
val broadcastCmd = "am broadcast --user ${Const.USER_ID} -p ${c.packageName} " +
"-a $DTB_PATCH_ACTION --ei $DTB_PATCH_RESULT \$result"
Shell.su("mm_patch_dtb '$broadcastCmd'").submit()
}
}

View File

@ -3,9 +3,11 @@ package com.topjohnwu.magisk.core.utils
import android.content.Context
import com.topjohnwu.magisk.R
import com.topjohnwu.magisk.core.Const
import com.topjohnwu.magisk.core.Info
import com.topjohnwu.magisk.core.wrap
import com.topjohnwu.magisk.extensions.rawResource
import com.topjohnwu.superuser.Shell
import com.topjohnwu.superuser.ShellUtils
import com.topjohnwu.superuser.io.SuFile
class RootInit : Shell.Initializer() {
@ -30,6 +32,12 @@ class RootInit : Shell.Initializer() {
"export BOOTMODE=true"
).exec()
fun getvar(name: String) = ShellUtils.fastCmd(shell, "echo \$$name").toBoolean()
Info.keepVerity = getvar("KEEPVERITY")
Info.keepEnc = getvar("KEEPFORCEENCRYPT")
Info.recovery = getvar("RECOVERYMODE")
return true
}
}

View File

@ -38,7 +38,7 @@ direct_install() {
}
mm_patch_dtb() {
local result=1
(local result=1
local PATCHED=$TMPDIR/dt.patched
for name in dtb dtbo; do
local IMAGE=`find_block $name$SLOT`
@ -55,7 +55,9 @@ mm_patch_dtb() {
fi
fi
done
return $result
# Run broadcast command passed from app
eval $1
)& >/dev/null 2>&1
}
restore_imgs() {