Skip to content

Commit

Permalink
cleaning up some legacy logic
Browse files Browse the repository at this point in the history
  • Loading branch information
nift4 committed Jul 30, 2024
1 parent 5e85a2c commit 412d369
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 167 deletions.
170 changes: 38 additions & 132 deletions app/src/main/java/org/andbootmgr/app/DeviceInfo.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,199 +16,105 @@ interface DeviceInfo {
val postInstallScript: Boolean
val havedtbo: Boolean
fun isInstalled(logic: DeviceLogic): Boolean
fun isBooted(logic: DeviceLogic): Boolean
@SuppressLint("PrivateApi")
fun isBooted(logic: DeviceLogic): Boolean {
try {
val c = Class.forName("android.os.SystemProperties")
val getBoolean: Method = c.getMethod(
"getBoolean",
String::class.java,
Boolean::class.javaPrimitiveType
)
if (getBoolean.invoke(c, "ro.boot.has_dualboot", false) as Boolean
|| getBoolean.invoke(c, "ro.boot.hasdualboot", false) as Boolean)
return true
} catch (e: Exception) {
e.printStackTrace()
}
val result = Shell.cmd(File(logic.assetDir, "Scripts/is_installed.sh").absolutePath).exec()
return result.isSuccess && result.out.join("\n").contains("ABM.bootloader=1")
}
fun isCorrupt(logic: DeviceLogic): Boolean
}

abstract class MetaOnSdDeviceInfo : DeviceInfo {
override val metaonsd: Boolean = true
override fun isInstalled(logic: DeviceLogic): Boolean {
return SuFile.open(bdev).exists() && run {
val meta: SDUtils.SDPartitionMeta? =
SDUtils.generateMeta(bdev, pbdev)
meta?.let { (meta.countPartitions() > 0) && (meta.dumpPartition(0).type == SDUtils.PartitionType.RESERVED) } == true
}
}
override fun isCorrupt(logic: DeviceLogic): Boolean {
return !SuFile.open(logic.abmDb, "db.conf").exists()
}
}

object HardcodedDeviceInfoFactory {
private fun getYggdrasil(): DeviceInfo {
return object : DeviceInfo {
return object : MetaOnSdDeviceInfo() {
override val codename: String = "yggdrasil"
override val blBlock: String = "/dev/block/by-name/lk"
override val bdev: String = "/dev/block/mmcblk1"
override val pbdev: String = bdev + "p"
override val metaonsd: Boolean = false
override val postInstallScript: Boolean = false
override val havedtbo: Boolean = false
override fun isInstalled(logic: DeviceLogic): Boolean {
return SuFile.open(logic.abmDir, "codename.cfg").exists()
}
override fun isBooted(logic: DeviceLogic): Boolean {
val result = Shell.cmd(File(logic.assetDir, "Scripts/is_installed.sh").absolutePath).exec()
return result.isSuccess && result.out.join("\n").contains("ABM.bootloader=1")
}
override fun isCorrupt(logic: DeviceLogic): Boolean {
return !SuFile.open(logic.abmDb, "db.conf").exists()
}
}
}

private fun getCedric(): DeviceInfo {
return object : DeviceInfo {
return object : MetaOnSdDeviceInfo() {
override val codename: String = "cedric"
override val blBlock: String = "/dev/block/bootdevice/by-name/boot"
override val bdev: String = "/dev/block/mmcblk1"
override val pbdev: String = bdev + "p"
override val metaonsd: Boolean = true
override val postInstallScript: Boolean = true
override val havedtbo: Boolean = false
override fun isInstalled(logic: DeviceLogic): Boolean {
return SuFile.open(bdev).exists() && run {
val meta: SDUtils.SDPartitionMeta? =
SDUtils.generateMeta(bdev, pbdev)
meta?.let { (meta.countPartitions() > 0) && (meta.dumpPartition(0).type == SDUtils.PartitionType.RESERVED) } == true
}
}
override fun isBooted(logic: DeviceLogic): Boolean {
val result = Shell.cmd(File(logic.assetDir, "Scripts/is_installed.sh").absolutePath).exec()
return result.isSuccess && result.out.join("\n").contains("ABM.bootloader=1")
}
override fun isCorrupt(logic: DeviceLogic): Boolean {
return !SuFile.open(logic.abmDb, "db.conf").exists()
}
}
}

private fun getMimameid(): DeviceInfo {
return object : DeviceInfo {
return object : MetaOnSdDeviceInfo() {
override val codename: String = "mimameid"
override val blBlock: String = "/dev/block/by-name/lk"
override val bdev: String = "/dev/block/mmcblk1"
override val pbdev: String = bdev + "p"
override val metaonsd: Boolean = true
override val postInstallScript: Boolean = true
override val havedtbo: Boolean = false
override fun isInstalled(logic: DeviceLogic): Boolean {
return SuFile.open(bdev).exists() && run {
val meta: SDUtils.SDPartitionMeta? =
SDUtils.generateMeta(bdev, pbdev)
meta?.let { (meta.countPartitions() > 0) && (meta.dumpPartition(0).type == SDUtils.PartitionType.RESERVED) } == true
}
}
@SuppressLint("PrivateApi")
override fun isBooted(logic: DeviceLogic): Boolean {
var hasABM = false
try {
val c = Class.forName("android.os.SystemProperties")
val getBoolean: Method = c.getMethod(
"getBoolean",
String::class.java,
Boolean::class.javaPrimitiveType
)
hasABM = getBoolean.invoke(c, "ro.boot.has_dualboot", false) as Boolean
|| getBoolean.invoke(c, "ro.boot.hasdualboot", false) as Boolean
} catch (e: Exception) {
e.printStackTrace()
}
return hasABM
}
override fun isCorrupt(logic: DeviceLogic): Boolean {
return !SuFile.open(logic.abmDb, "db.conf").exists()
}
}
}

private fun getYggdrasilx(): DeviceInfo {
return object : DeviceInfo {
return object : MetaOnSdDeviceInfo() {
override val codename: String = "yggdrasilx"
override val blBlock: String = "/dev/block/by-name/lk"
override val bdev: String = "/dev/block/mmcblk1"
override val pbdev: String = bdev + "p"
override val metaonsd: Boolean = true
override val postInstallScript: Boolean = true
override val havedtbo: Boolean = false
override fun isInstalled(logic: DeviceLogic): Boolean {
return SuFile.open(bdev).exists() && run {
val meta: SDUtils.SDPartitionMeta? =
SDUtils.generateMeta(bdev, pbdev)
meta?.let { (meta.countPartitions() > 0) && (meta.dumpPartition(0).type == SDUtils.PartitionType.RESERVED) } == true
}
}
@SuppressLint("PrivateApi")
override fun isBooted(logic: DeviceLogic): Boolean {
var hasABM = false
try {
val c = Class.forName("android.os.SystemProperties")
val getBoolean: Method = c.getMethod(
"getBoolean",
String::class.java,
Boolean::class.javaPrimitiveType
)
hasABM = getBoolean.invoke(c, "ro.boot.has_dualboot", false) as Boolean
|| getBoolean.invoke(c, "ro.boot.hasdualboot", false) as Boolean
} catch (e: Exception) {
e.printStackTrace()
}
return hasABM
}
override fun isCorrupt(logic: DeviceLogic): Boolean {
return !SuFile.open(logic.abmDb, "db.conf").exists()
}
}
}

private fun getVidofnir(): DeviceInfo {
return object : DeviceInfo {
return object : MetaOnSdDeviceInfo() {
override val codename: String = "vidofnir"
override val blBlock: String = "/dev/block/by-name/lk"
override val bdev: String = "/dev/block/mmcblk0"
override val pbdev: String = bdev + "p"
override val metaonsd: Boolean = true
override val postInstallScript: Boolean = false
override val havedtbo: Boolean = false
override fun isInstalled(logic: DeviceLogic): Boolean {
return SuFile.open(bdev).exists() && run {
val meta: SDUtils.SDPartitionMeta? =
SDUtils.generateMeta(bdev, pbdev)
meta?.let { (meta.countPartitions() > 0) && (meta.dumpPartition(0).type == SDUtils.PartitionType.RESERVED) } == true
}
}
@SuppressLint("PrivateApi")
override fun isBooted(logic: DeviceLogic): Boolean {
var hasABM = false
try {
val c = Class.forName("android.os.SystemProperties")
val getBoolean: Method = c.getMethod(
"getBoolean",
String::class.java,
Boolean::class.javaPrimitiveType
)
hasABM = getBoolean.invoke(c, "ro.boot.hasdualboot", false) as Boolean
} catch (e: Exception) {
e.printStackTrace()
}
return hasABM
}

override fun isCorrupt(logic: DeviceLogic): Boolean {
return !SuFile.open(logic.abmDb, "db.conf").exists()
}
}
}

private fun getVayu(): DeviceInfo {
return object : DeviceInfo {
return object : MetaOnSdDeviceInfo() {
override val codename: String = "vayu"
override val blBlock: String = "/dev/block/by-name/boot"
override val bdev: String = "/dev/block/mmcblk0"
override val pbdev: String = bdev + "p"
override val metaonsd: Boolean = true
override val postInstallScript: Boolean = true
override val havedtbo: Boolean = true
override fun isInstalled(logic: DeviceLogic): Boolean {
return SuFile.open(bdev).exists() && run {
val meta: SDUtils.SDPartitionMeta? =
SDUtils.generateMeta(bdev, pbdev)
meta?.let { (meta.countPartitions() > 0) && (meta.dumpPartition(0).type == SDUtils.PartitionType.RESERVED) } == true
}
}
override fun isBooted(logic: DeviceLogic): Boolean {
val result = Shell.cmd(File(logic.assetDir, "Scripts/is_installed.sh").absolutePath).exec()
return result.isSuccess && result.out.join("\n").contains("ABM.bootloader=1")
}
override fun isCorrupt(logic: DeviceLogic): Boolean {
return !SuFile.open(logic.abmDb, "db.conf").exists()
}
}
}

Expand Down
15 changes: 8 additions & 7 deletions app/src/main/java/org/andbootmgr/app/DroidBootFlow.kt
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,13 @@ private fun Start(vm: WizardActivityState) {
) {
Text(stringResource(R.string.welcome_text))
Text(
if (vm.deviceInfo!!.isBooted(vm.logic)){
if (vm.deviceInfo!!.isBooted(vm.logic)) {
stringResource(R.string.install_abm)
} else {
stringResource(R.string.install_abm_dboot)
}
)
if (vm.deviceInfo.metaonsd){
if (vm.deviceInfo.metaonsd) {
Text(stringResource(R.string.sd_erase1))
Text(stringResource(R.string.sd_erase2))
}
Expand Down Expand Up @@ -224,6 +224,8 @@ private fun Flash(vm: WizardActivityState) {
terminal.add(vm.activity.getString(R.string.term_failed_create_meta))
return@Terminal
}
} else {
// TODO provision for sdless
}

if (!vm.logic.mount(vm.deviceInfo)) {
Expand All @@ -249,13 +251,10 @@ private fun Flash(vm: WizardActivityState) {
return@Terminal
}
}
val o = SuFileOutputStream.open(File(vm.logic.abmDir, "codename.cfg"))
o.write(vm.deviceInfo.codename.toByteArray())
o.flush()
o.close()

terminal.add(vm.activity.getString(R.string.term_building_cfg))
val db = ConfigFile()
// TODO was this ever used? is it used by vbm maybe?
db["default"] = "Entry 01"
db["timeout"] = "5"
db.exportToFile(File(vm.logic.abmDb, "db.conf"))
Expand All @@ -264,7 +263,7 @@ private fun Flash(vm: WizardActivityState) {
entry["linux"] = "null"
entry["initrd"] = "null"
entry["dtb"] = "null"
if(vm.deviceInfo.havedtbo)
if (vm.deviceInfo.havedtbo)
entry["dtbo"] = "null"
entry["options"] = "null"
entry["xtype"] = "droid"
Expand Down Expand Up @@ -302,6 +301,8 @@ private fun Flash(vm: WizardActivityState) {
vm.onNext.value = {
if (vm.deviceInfo.isBooted(vm.logic)) {
it.startActivity(Intent(it, MainActivity::class.java))
} else {
// TODO prompt user to reboot?
}
it.finish()
}
Expand Down
32 changes: 4 additions & 28 deletions app/src/main/java/org/andbootmgr/app/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -147,24 +147,7 @@ class MainActivity : ComponentActivity() {
if (!fail) {
Shell.getShell { shell ->
vm.root = shell.isRoot
val codename: String
var b: ByteArray? = null
if (vm.root) {
try {
val f = SuFile.open(vm.logic!!.abmDir, "codename.cfg")
val s = SuFileInputStream.open(f)
b = s.readBytes()
s.close()
} catch (e: IOException) {
Log.e("ABM_GetCodeName", Log.getStackTraceString(e))
}
}
codename = if (b != null) {
String(b).trim()
} else {
Build.DEVICE
}
vm.deviceInfo = HardcodedDeviceInfoFactory.get(codename)
vm.deviceInfo = HardcodedDeviceInfoFactory.get(Build.DEVICE)
if (vm.deviceInfo != null && vm.deviceInfo!!.isInstalled(vm.logic!!)) {
vm.logic!!.mount(vm.deviceInfo!!)
}
Expand Down Expand Up @@ -390,31 +373,24 @@ private fun Start(vm: MainActivityState) {
}
}
}
if (Shell.isAppGrantedRoot() == false) {
if (Shell.isAppGrantedRoot() != true) {
Text(
stringResource(R.string.need_root),
textAlign = TextAlign.Center
)
} else if (metaonsd && !sdpresent) {
Text(stringResource(R.string.need_sd), textAlign = TextAlign.Center)
} else if (!installed) {
} else if (!installed && !mounted) {
Button(onClick = { vm.startFlow("droidboot") }) {
Text(stringResource(if (metaonsd) R.string.setup_sd else R.string.install))
}
} else if (!booted) {
} else if (!booted && mounted) {
Text(stringResource(R.string.installed_not_booted), textAlign = TextAlign.Center)
Button(onClick = {
vm.startFlow("fix_droidboot")
}) {
Text(stringResource(R.string.repair_droidboot))
}
} else if (!metaonsd && mounted && corrupt) {
Text(stringResource(R.string.missing_cfg), textAlign = TextAlign.Center)
Button(onClick = {
//vm.startFlow("repair_cfg") TODO:Implement this
}) {
Text(stringResource(R.string.repair_cfg))
}
} else if (!mounted) {
Text(stringResource(R.string.cannot_mount), textAlign = TextAlign.Center)
} else if (vm.isOk) {
Expand Down

0 comments on commit 412d369

Please sign in to comment.