Skip to content

Commit

Permalink
some sdless work
Browse files Browse the repository at this point in the history
  • Loading branch information
nift4 committed Jan 18, 2025
1 parent 31f4823 commit 6d5edbd
Show file tree
Hide file tree
Showing 10 changed files with 53 additions and 33 deletions.
2 changes: 1 addition & 1 deletion app/src/main/java/org/andbootmgr/app/BackupRestoreFlow.kt
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ private class CreateBackupDataHolder(val vm: WizardState, val pi: Int?, val part
private fun ChooseAction(c: CreateBackupDataHolder) {
if (c.vm.deviceInfo.metaonsd) {
LaunchedEffect(Unit) {
c.meta = SDUtils.generateMeta(c.vm.deviceInfo)
c.meta = SDUtils.generateMeta(c.vm.deviceInfo.asMetaOnSdDeviceInfo())
}
}

Expand Down
6 changes: 3 additions & 3 deletions app/src/main/java/org/andbootmgr/app/CreatePartFlow.kt
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ private fun Start(c: CreatePartDataHolder) {
LaunchedEffect(Unit) {
if (c.meta == null) {
withContext(Dispatchers.IO) {
val meta = SDUtils.generateMeta(c.vm.deviceInfo)!! // TODO !metaonsd
val meta = SDUtils.generateMeta(c.vm.deviceInfo.asMetaOnSdDeviceInfo())!! // TODO !metaonsd
c.p =
meta.s.find { c.desiredStartSector == it.startSector } as SDUtils.Partition.FreeSpace
c.meta = meta
Expand Down Expand Up @@ -650,7 +650,7 @@ private fun Flash(c: CreatePartDataHolder) {
terminal.add(vm.activity.getString(R.string.term_reboot_asap))
}
createdParts.add(Pair(part, c.meta!!.nid))
c.meta = SDUtils.generateMeta(c.vm.deviceInfo)
c.meta = SDUtils.generateMeta(c.vm.deviceInfo.asMetaOnSdDeviceInfo())
// do not assert there is leftover space if we just created the last partition we want to create
if (index < c.parts.size - 1) {
c.p =
Expand All @@ -665,7 +665,7 @@ private fun Flash(c: CreatePartDataHolder) {
}
terminal.add(vm.activity.getString(R.string.term_created_pt))
vm.logic.mountBootset(vm.deviceInfo)
val meta = SDUtils.generateMeta(vm.deviceInfo)
val meta = SDUtils.generateMeta(vm.deviceInfo.asMetaOnSdDeviceInfo())
if (meta == null) {
terminal.add(vm.activity.getString(R.string.term_cant_get_meta))
return@WizardTerminalWork
Expand Down
43 changes: 30 additions & 13 deletions app/src/main/java/org/andbootmgr/app/DeviceInfo.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,13 @@ import java.net.URL
interface DeviceInfo {
val codename: String
val blBlock: String
val bdev: String
val pbdev: String
val metaonsd: Boolean
val postInstallScript: Boolean
val havedtbo: Boolean
fun isInstalled(logic: DeviceLogic): Boolean
@SuppressLint("PrivateApi")
fun isBooted(logic: DeviceLogic): Boolean {
// TODO migrate all modern BL to one of these three variants
try {
val c = Class.forName("android.os.SystemProperties")
val getBoolean: Method = c.getMethod(
Expand All @@ -48,7 +47,11 @@ interface DeviceInfo {
fun getAbmSettings(logic: DeviceLogic): String?
}

fun DeviceInfo.asMetaOnSdDeviceInfo() = this as MetaOnSdDeviceInfo

abstract class MetaOnSdDeviceInfo : DeviceInfo {
abstract val bdev: String
abstract val pbdev: String
override val metaonsd = true
override fun isInstalled(logic: DeviceLogic): Boolean {
return SuFile.open(bdev).exists() && SDUtils.generateMeta(this)?.let { meta ->
Expand Down Expand Up @@ -84,7 +87,7 @@ abstract class SdLessDeviceInfo : DeviceInfo {
}
}

class JsonDeviceInfo(
class JsonMetaOnSdDeviceInfo(
override val codename: String,
override val blBlock: String,
override val bdev: String,
Expand All @@ -93,6 +96,13 @@ class JsonDeviceInfo(
override val havedtbo: Boolean
) : MetaOnSdDeviceInfo()

class JsonSdLessDeviceInfo(
override val codename: String,
override val blBlock: String,
override val postInstallScript: Boolean,
override val havedtbo: Boolean
) : SdLessDeviceInfo()

class JsonDeviceInfoFactory(private val ctx: Context) {
suspend fun get(codename: String): DeviceInfo? {
return try {
Expand Down Expand Up @@ -124,16 +134,23 @@ class JsonDeviceInfoFactory(private val ctx: Context) {
File(ctx.filesDir, "abm_dd_cache.json").writeText(newRoot.toString())
}
}
if (!json.getBoolean("metaOnSd"))
throw IllegalArgumentException("sd less currently not implemented")
JsonDeviceInfo(
json.getString("codename"),
json.getString("blBlock"),
json.getString("sdBlock"),
json.getString("sdBlockP"),
json.getBoolean("postInstallScript"),
json.getBoolean("haveDtbo")
)
if (json.getBoolean("metaOnSd")) {
JsonMetaOnSdDeviceInfo(
json.getString("codename"),
json.getString("blBlock"),
json.getString("sdBlock"),
json.getString("sdBlockP"),
json.getBoolean("postInstallScript"),
json.getBoolean("haveDtbo")
)
} else {
JsonSdLessDeviceInfo(
json.getString("codename"),
json.getString("blBlock"),
json.getBoolean("postInstallScript"),
json.getBoolean("haveDtbo")
)
}
}
} catch (e: Exception) {
Log.e("ABM device info", Log.getStackTraceString(e))
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/org/andbootmgr/app/DeviceLogic.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class DeviceLogic(private val ctx: Context) {
val abmSdLessBootset = File("/data/abm")
val abmSdLessBootsetImg = File(abmSdLessBootset, "bootset.img")
private val metadata = File("/metadata")
val metadataMap = File(metadata, "bootset.map")
val metadataMap = File(metadata, "abm_settings.map")
val dmBase = File("/dev/block/mapper")
val dmName = "abmbootset"
val dmPath = File(dmBase, dmName)
Expand Down
6 changes: 3 additions & 3 deletions app/src/main/java/org/andbootmgr/app/DroidBootFlow.kt
Original file line number Diff line number Diff line change
Expand Up @@ -210,21 +210,21 @@ private fun Flash(d: DroidBootFlowDataHolder) {
}

if (vm.deviceInfo.metaonsd) {
var meta = SDUtils.generateMeta(vm.deviceInfo)
var meta = SDUtils.generateMeta(vm.deviceInfo.asMetaOnSdDeviceInfo())
if (meta == null) {
terminal.add(vm.activity.getString(R.string.term_cant_get_meta))
return@WizardTerminalWork
}
if (!Shell.cmd(SDUtils.umsd(meta)).to(terminal).exec().isSuccess) {
terminal.add(vm.activity.getString(R.string.term_failed_umount_drive))
}
if (!Shell.cmd("sgdisk --mbrtogpt --clear ${vm.deviceInfo.bdev}").to(terminal)
if (!Shell.cmd("sgdisk --mbrtogpt --clear ${vm.deviceInfo.asMetaOnSdDeviceInfo().bdev}").to(terminal)
.exec().isSuccess
) {
terminal.add(vm.activity.getString(R.string.term_failed_create_pt))
return@WizardTerminalWork
}
meta = SDUtils.generateMeta(vm.deviceInfo)
meta = SDUtils.generateMeta(vm.deviceInfo.asMetaOnSdDeviceInfo())
if (meta == null) {
terminal.add(vm.activity.getString(R.string.term_cant_get_meta))
return@WizardTerminalWork
Expand Down
10 changes: 5 additions & 5 deletions app/src/main/java/org/andbootmgr/app/Start.kt
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ fun Start(vm: MainActivityState) {
corrupt = remember { vm.deviceInfo!!.isCorrupt(vm.logic!!) }
mounted = vm.logic!!.mounted
metaOnSd = vm.deviceInfo!!.metaonsd
sdPresent = if (metaOnSd) remember { SuFile.open(vm.deviceInfo!!.bdev).exists() } else false
sdPresent = if (metaOnSd) remember { SuFile.open(vm.deviceInfo!!.asMetaOnSdDeviceInfo().bdev).exists() } else false
} else {
installed = false
booted = false
Expand Down Expand Up @@ -193,7 +193,7 @@ private fun PartTool(vm: MainActivityState) {
)
) { filter = it }

var parts by remember { mutableStateOf(SDUtils.generateMeta(vm.deviceInfo!!)) }
var parts by remember { mutableStateOf(SDUtils.generateMeta(vm.deviceInfo!!.asMetaOnSdDeviceInfo())) }
if (parts == null) {
Text(stringResource(R.string.part_wizard_err))
return
Expand Down Expand Up @@ -325,7 +325,7 @@ private fun PartTool(vm: MainActivityState) {
vm, editPartID!!, simplified = filterUnifiedView,
onClose = { editPartID = null }, onPtChanged = {
// TODO don't call generateMeta on main thread
parts = SDUtils.generateMeta(vm.deviceInfo!!)
parts = SDUtils.generateMeta(vm.deviceInfo!!.asMetaOnSdDeviceInfo())
editPartID = if (it) null else
parts?.s!!.findLast { it.id == editPartID!!.id }
}
Expand All @@ -347,7 +347,7 @@ private fun PartTool(vm: MainActivityState) {
if (it) {
entries!!.remove(editEntryID!!.also { editEntryID = null })
// TODO don't call generateMeta on main thread
parts = SDUtils.generateMeta(vm.deviceInfo!!)
parts = SDUtils.generateMeta(vm.deviceInfo!!.asMetaOnSdDeviceInfo())
} else
editEntryID = null
}, onOpenUpdater = {
Expand Down Expand Up @@ -429,7 +429,7 @@ private fun OsEditor(vm: MainActivityState, parts: SDUtils.SDPartitionMeta?, e:
vm.unmountBootset()
for (p in allp) { // Do not chain, but regenerate meta and unmount every time. Thanks vold
p.meta = if (parts != null) parts.also { parts = null }
else SDUtils.generateMeta(vm.deviceInfo!!)!!
else SDUtils.generateMeta(vm.deviceInfo!!.asMetaOnSdDeviceInfo())!!
val r = vm.logic!!.delete(p).exec()
tresult += r.out.joinToString("\n") + r.err.joinToString("\n") + "\n"
}
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/org/andbootmgr/app/UpdateFlow.kt
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ private fun Flash(u: UpdateFlowDataHolder) {
u.vm.logic.extractToolkit(terminal)
u.vm.downloadRemainingFiles(terminal)
val sp = u.e!!["xpart"]!!.split(":")
val meta = SDUtils.generateMeta(u.vm.deviceInfo)!! // TODO !metaonsd
val meta = SDUtils.generateMeta(u.vm.deviceInfo.asMetaOnSdDeviceInfo())!! // TODO !metaonsd
Shell.cmd(SDUtils.umsd(meta)).exec()
val tmpFile = if (u.vm.idNeeded.contains("_install.sh_")) {
u.vm.chosen["_install.sh_"]!!.toFile(u.vm).also {
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/org/andbootmgr/app/themes/Simulator.kt
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class Simulator : AppCompatActivity() {
stop()
}
})
f = File(intent.getStringExtra("sdCardBlock")!!)
f = File(intent.getStringExtra("sdCardBlock")!!) // TODO support sd-less with "bootsetBlock"
val l = LinearLayout(this)
v = object : View(this) {
private var firstTime = true
Expand Down
7 changes: 6 additions & 1 deletion app/src/main/java/org/andbootmgr/app/themes/Themes.kt
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ import kotlinx.coroutines.launch
import org.andbootmgr.app.AppContent
import org.andbootmgr.app.MainActivityState
import org.andbootmgr.app.R
import org.andbootmgr.app.asMetaOnSdDeviceInfo
import org.andbootmgr.app.util.AbmTheme

/*
Expand Down Expand Up @@ -130,7 +131,11 @@ fun Themes(vm: ThemeViewModel) {
vm.mvm.activity,
Simulator::class.java
).apply {
putExtra("sdCardBlock", vm.mvm.deviceInfo!!.bdev)
if (vm.mvm.deviceInfo!!.metaonsd)
putExtra("sdCardBlock",
vm.mvm.deviceInfo!!.asMetaOnSdDeviceInfo().bdev)
else
putExtra("bootsetBlock", vm.mvm.logic!!.abmSdLessBootsetImg)
}
)
}
Expand Down
6 changes: 2 additions & 4 deletions app/src/main/java/org/andbootmgr/app/util/SDUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package org.andbootmgr.app.util

import android.util.Log
import com.topjohnwu.superuser.Shell
import org.andbootmgr.app.DeviceInfo
import org.andbootmgr.app.MetaOnSdDeviceInfo
import java.util.*
import java.util.stream.Collectors
import kotlin.jvm.optionals.getOrElse
Expand All @@ -17,9 +17,7 @@ object SDUtils {
return if (e.isEmpty()) "true" else e.substring(0, e.length - 4)
}

fun generateMeta(deviceInfo: DeviceInfo): SDPartitionMeta? {
if (!deviceInfo.metaonsd)
throw IllegalStateException("App bug: generateMeta should _never_ be called on sd-less ports")
fun generateMeta(deviceInfo: MetaOnSdDeviceInfo): SDPartitionMeta? {
val meta: SDPartitionMeta
val r =
Shell.cmd("printf \"mm:%d:%d\\n\" `stat -c '0x%t 0x%T' ${deviceInfo.bdev}` && sgdisk ${deviceInfo.bdev} --print")
Expand Down

0 comments on commit 6d5edbd

Please sign in to comment.