From 6d5edbd1290653edcc197eff0caefaa251e40acb Mon Sep 17 00:00:00 2001 From: nift4 Date: Sat, 18 Jan 2025 17:08:31 +0100 Subject: [PATCH] some sdless work --- .../org/andbootmgr/app/BackupRestoreFlow.kt | 2 +- .../java/org/andbootmgr/app/CreatePartFlow.kt | 6 +-- .../java/org/andbootmgr/app/DeviceInfo.kt | 43 +++++++++++++------ .../java/org/andbootmgr/app/DeviceLogic.kt | 2 +- .../java/org/andbootmgr/app/DroidBootFlow.kt | 6 +-- app/src/main/java/org/andbootmgr/app/Start.kt | 10 ++--- .../java/org/andbootmgr/app/UpdateFlow.kt | 2 +- .../org/andbootmgr/app/themes/Simulator.kt | 2 +- .../java/org/andbootmgr/app/themes/Themes.kt | 7 ++- .../java/org/andbootmgr/app/util/SDUtils.kt | 6 +-- 10 files changed, 53 insertions(+), 33 deletions(-) diff --git a/app/src/main/java/org/andbootmgr/app/BackupRestoreFlow.kt b/app/src/main/java/org/andbootmgr/app/BackupRestoreFlow.kt index 622aa016..ac8dde46 100644 --- a/app/src/main/java/org/andbootmgr/app/BackupRestoreFlow.kt +++ b/app/src/main/java/org/andbootmgr/app/BackupRestoreFlow.kt @@ -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()) } } diff --git a/app/src/main/java/org/andbootmgr/app/CreatePartFlow.kt b/app/src/main/java/org/andbootmgr/app/CreatePartFlow.kt index 63df92a5..b0de89ab 100644 --- a/app/src/main/java/org/andbootmgr/app/CreatePartFlow.kt +++ b/app/src/main/java/org/andbootmgr/app/CreatePartFlow.kt @@ -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 @@ -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 = @@ -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 diff --git a/app/src/main/java/org/andbootmgr/app/DeviceInfo.kt b/app/src/main/java/org/andbootmgr/app/DeviceInfo.kt index 7e9069a2..cac3b595 100644 --- a/app/src/main/java/org/andbootmgr/app/DeviceInfo.kt +++ b/app/src/main/java/org/andbootmgr/app/DeviceInfo.kt @@ -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( @@ -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 -> @@ -84,7 +87,7 @@ abstract class SdLessDeviceInfo : DeviceInfo { } } -class JsonDeviceInfo( +class JsonMetaOnSdDeviceInfo( override val codename: String, override val blBlock: String, override val bdev: String, @@ -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 { @@ -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)) diff --git a/app/src/main/java/org/andbootmgr/app/DeviceLogic.kt b/app/src/main/java/org/andbootmgr/app/DeviceLogic.kt index 9cdd8b10..7ca93a28 100644 --- a/app/src/main/java/org/andbootmgr/app/DeviceLogic.kt +++ b/app/src/main/java/org/andbootmgr/app/DeviceLogic.kt @@ -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) diff --git a/app/src/main/java/org/andbootmgr/app/DroidBootFlow.kt b/app/src/main/java/org/andbootmgr/app/DroidBootFlow.kt index db8b9175..a78020a8 100644 --- a/app/src/main/java/org/andbootmgr/app/DroidBootFlow.kt +++ b/app/src/main/java/org/andbootmgr/app/DroidBootFlow.kt @@ -210,7 +210,7 @@ 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 @@ -218,13 +218,13 @@ private fun Flash(d: DroidBootFlowDataHolder) { 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 diff --git a/app/src/main/java/org/andbootmgr/app/Start.kt b/app/src/main/java/org/andbootmgr/app/Start.kt index e7fb2690..3f37afb6 100644 --- a/app/src/main/java/org/andbootmgr/app/Start.kt +++ b/app/src/main/java/org/andbootmgr/app/Start.kt @@ -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 @@ -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 @@ -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 } } @@ -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 = { @@ -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" } diff --git a/app/src/main/java/org/andbootmgr/app/UpdateFlow.kt b/app/src/main/java/org/andbootmgr/app/UpdateFlow.kt index f6bccd8b..92720b61 100644 --- a/app/src/main/java/org/andbootmgr/app/UpdateFlow.kt +++ b/app/src/main/java/org/andbootmgr/app/UpdateFlow.kt @@ -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 { diff --git a/app/src/main/java/org/andbootmgr/app/themes/Simulator.kt b/app/src/main/java/org/andbootmgr/app/themes/Simulator.kt index 579ab600..04ec5149 100644 --- a/app/src/main/java/org/andbootmgr/app/themes/Simulator.kt +++ b/app/src/main/java/org/andbootmgr/app/themes/Simulator.kt @@ -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 diff --git a/app/src/main/java/org/andbootmgr/app/themes/Themes.kt b/app/src/main/java/org/andbootmgr/app/themes/Themes.kt index 3fbbcb0e..d7d66b81 100644 --- a/app/src/main/java/org/andbootmgr/app/themes/Themes.kt +++ b/app/src/main/java/org/andbootmgr/app/themes/Themes.kt @@ -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 /* @@ -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) } ) } diff --git a/app/src/main/java/org/andbootmgr/app/util/SDUtils.kt b/app/src/main/java/org/andbootmgr/app/util/SDUtils.kt index da231a4e..7067243b 100644 --- a/app/src/main/java/org/andbootmgr/app/util/SDUtils.kt +++ b/app/src/main/java/org/andbootmgr/app/util/SDUtils.kt @@ -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 @@ -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")