Skip to content

Commit eca0dba

Browse files
committed
bumped to 1.0.0-alpha05
lowered minSdk to 30 added vbmeta to raw partition backups fixed vendor_dlkm map and mount added early exception handling
1 parent ae15383 commit eca0dba

File tree

4 files changed

+80
-31
lines changed

4 files changed

+80
-31
lines changed

app/build.gradle

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ android {
88

99
defaultConfig {
1010
applicationId "com.github.capntrips.kernelflasher"
11-
minSdk 31
11+
minSdk 30
1212
targetSdk 32
13-
versionCode 4
14-
versionName "1.0.0-alpha04"
13+
versionCode 5
14+
versionName "1.0.0-alpha05"
1515

1616
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
1717
vectorDrawables {

app/src/main/java/com/github/capntrips/kernelflasher/ui/screens/backups/BackupsViewModel.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ class BackupsViewModel(
167167
restoreLogicalPartition(context, backupDir, "vendor_dlkm", slotSuffix)
168168
restorePhysicalPartition(context, backupDir, "vendor_boot", slotSuffix)
169169
restorePhysicalPartition(context, backupDir, "dtbo", slotSuffix)
170+
restorePhysicalPartition(context, backupDir, "vbmeta", slotSuffix)
170171
log(context, "Backup restored successfully")
171172
wasRestored = true
172173
}

app/src/main/java/com/github/capntrips/kernelflasher/ui/screens/main/MainViewModel.kt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class MainViewModel(
4242
val hasError: Boolean
4343
get() = _error != null
4444
val error: String
45-
get() = _error ?: "Unknown Error"
45+
get() = _error!!
4646

4747
init {
4848
val bootA = File("/dev/block/by-name/boot_a")
@@ -51,7 +51,13 @@ class MainViewModel(
5151
slotSuffix = Shell.cmd("getprop ro.boot.slot_suffix").exec().out[0]
5252
backups = BackupsViewModel(context, _isRefreshing, navController)
5353
slotA = SlotViewModel(context, slotSuffix == "_a", "_a", bootA, _isRefreshing, navController, backups = backups.backups)
54+
if (slotA.hasError) {
55+
_error = slotA.error
56+
}
5457
slotB = SlotViewModel(context, slotSuffix == "_b", "_b", bootB, _isRefreshing, navController, backups = backups.backups)
58+
if (slotB.hasError) {
59+
_error = slotB.error
60+
}
5561

5662
val ramoops = SuFile("/sys/fs/pstore/console-ramoops-0")
5763
hasRamoops = ramoops.exists()

app/src/main/java/com/github/capntrips/kernelflasher/ui/screens/slot/SlotViewModel.kt

Lines changed: 69 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class SlotViewModel(
3939
const val TAG: String = "KernelFlasher/SlotState"
4040
}
4141

42-
lateinit var sha1: String
42+
var _sha1: String? = null
4343
var kernelVersion: String? = null
4444
var hasVendorDlkm: Boolean = false
4545
var isVendorDlkmMounted: Boolean = false
@@ -48,13 +48,21 @@ class SlotViewModel(
4848
private val _wasFlashSuccess: MutableState<Boolean?> = mutableStateOf(null)
4949
private var wasSlotReset: Boolean = false
5050
private var isFlashing: Boolean = false
51+
private var inInit = true
52+
private var _error: String? = null
5153

54+
val sha1: String
55+
get() = _sha1!!
5256
val flashOutput: List<String>
5357
get() = _flashOutput
5458
val wasFlashSuccess: Boolean?
5559
get() = _wasFlashSuccess.value
5660
val isRefreshing: Boolean
5761
get() = _isRefreshing.value
62+
val hasError: Boolean
63+
get() = _error != null
64+
val error: String
65+
get() = _error!!
5866

5967
init {
6068
refresh(context)
@@ -66,32 +74,36 @@ class SlotViewModel(
6674
val ramdisk = File(context.filesDir, "ramdisk.cpio")
6775

6876
val mapperDir = "/dev/block/mapper"
69-
val vendorDlkm = SuFile(mapperDir, "vendor_dlkm$slotSuffix")
77+
var vendorDlkm = SuFile(mapperDir, "vendor_dlkm$slotSuffix")
7078
hasVendorDlkm = vendorDlkm.exists()
7179
if (hasVendorDlkm) {
72-
val dmPath = Shell.cmd("readlink -f $vendorDlkm").exec().out[0]
73-
var mounts = Shell.cmd("mount | grep $dmPath").exec().out
74-
if (mounts.isNotEmpty()) {
75-
isVendorDlkmMounted = true
76-
} else {
77-
mounts = Shell.cmd("mount | grep vendor_dlkm-verity").exec().out
78-
isVendorDlkmMounted = mounts.isNotEmpty()
80+
isVendorDlkmMounted = isPartitionMounted(vendorDlkm)
81+
if (!isVendorDlkmMounted) {
82+
vendorDlkm = SuFile(mapperDir, "vendor_dlkm-verity")
83+
isVendorDlkmMounted = isPartitionMounted(vendorDlkm)
7984
}
8085
} else {
8186
isVendorDlkmMounted = false
8287
}
8388

84-
if (!isImage || ramdisk.exists()) {
85-
when (Shell.cmd("/data/adb/magisk/magiskboot cpio ramdisk.cpio test").exec().code) {
86-
0 -> sha1 = Shell.cmd("/data/adb/magisk/magiskboot sha1 $boot").exec().out[0]
87-
1 -> sha1 = Shell.cmd("/data/adb/magisk/magiskboot cpio ramdisk.cpio sha1").exec().out[0]
88-
else -> log(context, "Invalid boot.img", shouldThrow = true)
89+
val magiskboot = SuFile("/data/adb/magisk/magiskboot")
90+
if (magiskboot.exists()) {
91+
if (!isImage || ramdisk.exists()) {
92+
when (Shell.cmd("/data/adb/magisk/magiskboot cpio ramdisk.cpio test").exec().code) {
93+
0 -> _sha1 = Shell.cmd("/data/adb/magisk/magiskboot sha1 $boot").exec().out[0]
94+
1 -> _sha1 = Shell.cmd("/data/adb/magisk/magiskboot cpio ramdisk.cpio sha1").exec().out[0]
95+
else -> log(context, "Invalid boot.img", shouldThrow = true)
96+
}
97+
} else {
98+
log(context, "Invalid boot.img", shouldThrow = true)
8999
}
100+
Shell.cmd("/data/adb/magisk/magiskboot cleanup").exec()
90101
} else {
91-
log(context, "Invalid boot.img", shouldThrow = true)
102+
log(context, "magiskboot is missing", shouldThrow = true)
92103
}
104+
93105
kernelVersion = null
94-
Shell.cmd("/data/adb/magisk/magiskboot cleanup").exec()
106+
inInit = false
95107
}
96108

97109
private fun launch(block: suspend () -> Unit) {
@@ -119,7 +131,11 @@ class SlotViewModel(
119131
Toast.makeText(context, message, Toast.LENGTH_SHORT).show()
120132
}
121133
} else {
122-
throw Exception(message)
134+
if (inInit) {
135+
_error = message
136+
} else {
137+
throw Exception(message)
138+
}
123139
}
124140
}
125141

@@ -192,18 +208,34 @@ class SlotViewModel(
192208
}
193209
}
194210

211+
fun isPartitionMounted(partition: File) : Boolean {
212+
@Suppress("LiftReturnOrAssignment")
213+
if (partition.exists()) {
214+
val dmPath = Shell.cmd("readlink -f $partition").exec().out[0]
215+
val mounts = Shell.cmd("mount | grep $dmPath").exec().out
216+
return mounts.isNotEmpty();
217+
} else {
218+
return false;
219+
}
220+
}
221+
222+
fun unmountPartition(partition: File) {
223+
val dmPath = Shell.cmd("readlink -f $partition").exec().out[0]
224+
Shell.cmd("umount $dmPath").exec()
225+
}
226+
195227
fun unmountVendorDlkm(context: Context) {
196228
launch {
197229
val mapperDir = "/dev/block/mapper"
198-
val vendorDlkm = SuFile(mapperDir, "vendor_dlkm$slotSuffix")
199-
val dmPath = Shell.cmd("readlink -f $vendorDlkm").exec().out[0]
200-
var mounts = Shell.cmd("mount | grep $dmPath").exec().out
201-
if (mounts.isNotEmpty()) {
202-
Shell.cmd("umount $dmPath").exec()
203-
} else {
204-
mounts = Shell.cmd("mount | grep vendor_dlkm-verity").exec().out
205-
if (mounts.isNotEmpty()) {
206-
Shell.cmd("umount /vendor_dlkm-verity").exec()
230+
var vendorDlkm = SuFile(mapperDir, "vendor_dlkm$slotSuffix")
231+
if (vendorDlkm.exists()) {
232+
if (isPartitionMounted(vendorDlkm)) {
233+
unmountPartition(vendorDlkm)
234+
} else {
235+
vendorDlkm = SuFile(mapperDir, "vendor_dlkm-verity")
236+
if (isPartitionMounted(vendorDlkm)) {
237+
unmountPartition(vendorDlkm)
238+
}
207239
}
208240
}
209241
refresh(context)
@@ -223,7 +255,16 @@ class SlotViewModel(
223255
fun unmapVendorDlkm(context: Context) {
224256
launch {
225257
val lptools = File(context.filesDir, "lptools")
226-
Shell.cmd("$lptools unmap vendor_dlkm$slotSuffix").exec()
258+
val mapperDir = "/dev/block/mapper"
259+
val vendorDlkm = SuFile(mapperDir, "vendor_dlkm$slotSuffix")
260+
if (vendorDlkm.exists()) {
261+
val vendorDlkmVerity = SuFile(mapperDir, "vendor_dlkm-verity")
262+
if (vendorDlkmVerity.exists()) {
263+
Shell.cmd("$lptools unmap vendor_dlkm-verity").exec()
264+
} else {
265+
Shell.cmd("$lptools unmap vendor_dlkm$slotSuffix").exec()
266+
}
267+
}
227268
refresh(context)
228269
}
229270
}
@@ -302,6 +343,7 @@ class SlotViewModel(
302343
backupLogicalPartition(context, "vendor_dlkm", backupDir, true)
303344
backupPhysicalPartition(context, "vendor_boot", backupDir)
304345
backupPhysicalPartition(context, "dtbo", backupDir)
346+
backupPhysicalPartition(context, "vbmeta", backupDir)
305347
backups?.put(now, props)
306348
withContext (Dispatchers.Main) {
307349
callback.invoke()

0 commit comments

Comments
 (0)