@@ -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