kernel: Rewrite kernle version code management

Co-authored-by: lamadaemon <i@lama.icu>
Signed-off-by: ShirkNeko <109797057+ShirkNeko@users.noreply.github.com>
This commit is contained in:
ShirkNeko
2025-06-24 23:16:16 +08:00
parent f57fe79c5d
commit 2c2698f6bc
53 changed files with 234 additions and 122 deletions

View File

@@ -17,6 +17,7 @@ object Natives {
// 10977: change groups_count and groups to avoid overflow write
// 11071: Fix the issue of failing to set a custom SELinux type.
const val MINIMAL_SUPPORTED_KERNEL = 11071
const val MINIMAL_SUPPORTED_KERNEL_FULL = "v3.1.5"
// 11640: Support query working mode, LKM or GKI
// when MINIMAL_SUPPORTED_KERNEL > 11640, we can remove this constant.
@@ -31,6 +32,23 @@ object Natives {
const val ROOT_UID = 0
const val ROOT_GID = 0
external fun getFullVersion(): String
fun getSimpleVersionFull(): String {
val fullVersion = getFullVersion()
val startIndex = fullVersion.indexOf('v')
if (startIndex < 0) {
return fullVersion
}
val endIndex = fullVersion.indexOf('-', startIndex)
val versionStr = if (endIndex > startIndex) {
fullVersion.substring(startIndex, endIndex)
} else {
fullVersion.substring(startIndex)
}
return "v" + (Regex("""\d+(\.\d+)*""").find(versionStr)?.value ?: versionStr)
}
init {
System.loadLibrary("zako")
}
@@ -98,7 +116,14 @@ object Natives {
}
fun requireNewKernel(): Boolean {
return version < MINIMAL_SUPPORTED_KERNEL
if (version < MINIMAL_SUPPORTED_KERNEL) {
return true
}
val simpleVersionFull = getSimpleVersionFull()
if (simpleVersionFull.isEmpty()) {
return false
}
return simpleVersionFull < MINIMAL_SUPPORTED_KERNEL_FULL
}
@Immutable
@@ -120,27 +145,7 @@ object Natives {
val statusMagicMount: Boolean = false,
val statusOverlayfsAutoKstat: Boolean = false,
val statusSusSu: Boolean = false
) : Parcelable {
fun toMap(): Map<String, Boolean> {
return mapOf(
"CONFIG_KSU_SUSFS_SUS_PATH" to statusSusPath,
"CONFIG_KSU_SUSFS_SUS_MOUNT" to statusSusMount,
"CONFIG_KSU_SUSFS_AUTO_ADD_SUS_KSU_DEFAULT_MOUNT" to statusAutoDefaultMount,
"CONFIG_KSU_SUSFS_AUTO_ADD_SUS_BIND_MOUNT" to statusAutoBindMount,
"CONFIG_KSU_SUSFS_SUS_KSTAT" to statusSusKstat,
"CONFIG_KSU_SUSFS_TRY_UMOUNT" to statusTryUmount,
"CONFIG_KSU_SUSFS_AUTO_ADD_TRY_UMOUNT_FOR_BIND_MOUNT" to statusAutoTryUmountBind,
"CONFIG_KSU_SUSFS_SPOOF_UNAME" to statusSpoofUname,
"CONFIG_KSU_SUSFS_ENABLE_LOG" to statusEnableLog,
"CONFIG_KSU_SUSFS_HIDE_KSU_SUSFS_SYMBOLS" to statusHideSymbols,
"CONFIG_KSU_SUSFS_SPOOF_CMDLINE_OR_BOOTCONFIG" to statusSpoofCmdline,
"CONFIG_KSU_SUSFS_OPEN_REDIRECT" to statusOpenRedirect,
"CONFIG_KSU_SUSFS_HAS_MAGIC_MOUNT" to statusMagicMount,
"CONFIG_KSU_SUSFS_SUS_OVERLAYFS" to statusOverlayfsAutoKstat,
"CONFIG_KSU_SUSFS_SUS_SU" to statusSusSu
)
}
}
) : Parcelable
@Immutable

View File

@@ -170,8 +170,8 @@ fun HomeScreen(navigator: DestinationsNavigator) {
if (viewModel.systemStatus.requireNewKernel) {
WarningCard(
stringResource(id = R.string.require_kernel_version).format(
viewModel.systemStatus.ksuVersion,
Natives.MINIMAL_SUPPORTED_KERNEL
Natives.getSimpleVersionFull(),
Natives.MINIMAL_SUPPORTED_KERNEL_FULL
)
)
}
@@ -438,11 +438,13 @@ private fun StatusCard(
if (!isHideVersion) {
Spacer(Modifier.height(4.dp))
Text(
text = stringResource(R.string.home_working_version, systemStatus.ksuVersion),
style = MaterialTheme.typography.bodyMedium,
color = MaterialTheme.colorScheme.secondary,
)
systemStatus.ksuFullVersion?.let {
Text(
text = stringResource(R.string.home_working_version, it),
style = MaterialTheme.typography.bodyMedium,
color = MaterialTheme.colorScheme.secondary,
)
}
}
}
}
@@ -725,7 +727,7 @@ private fun InfoCard(
if (!isSimpleMode) {
// 根据showKpmInfo决定是否显示KPM信息
if (lkmMode != true && !showKpmInfo && Natives.version >= Natives.MINIMAL_SUPPORTED_KPM) {
if (lkmMode != true && !showKpmInfo) {
val displayVersion = if (systemInfo.kpmVersion.isEmpty() || systemInfo.kpmVersion.startsWith("Error")) {
val statusText = if (Natives.isKPMEnabled()) {
stringResource(R.string.kernel_patched)

View File

@@ -37,6 +37,7 @@ class HomeViewModel : ViewModel() {
data class SystemStatus(
val isManager: Boolean = false,
val ksuVersion: Int? = null,
val ksuFullVersion : String? = null,
val lkmMode: Boolean? = null,
val kernelVersion: KernelVersion = getKernelVersion(),
val isRootAvailable: Boolean = false,
@@ -76,6 +77,8 @@ class HomeViewModel : ViewModel() {
var isSimpleMode by mutableStateOf(false)
private set
var isKernelSimpleMode by mutableStateOf(false)
private set
var isHideVersion by mutableStateOf(false)
private set
var isHideOtherInfo by mutableStateOf(false)
@@ -91,6 +94,7 @@ class HomeViewModel : ViewModel() {
viewModelScope.launch(Dispatchers.IO) {
val prefs = context.getSharedPreferences("settings", Context.MODE_PRIVATE)
isSimpleMode = prefs.getBoolean("is_simple_mode", false)
isKernelSimpleMode = prefs.getBoolean("is_kernel_simple_mode", false)
isHideVersion = prefs.getBoolean("is_hide_version", false)
isHideOtherInfo = prefs.getBoolean("is_hide_other_info", false)
isHideSusfsStatus = prefs.getBoolean("is_hide_susfs_status", false)
@@ -167,6 +171,25 @@ class HomeViewModel : ViewModel() {
val kernelVersion = getKernelVersion()
val isManager = Natives.becomeManager(ksuApp.packageName)
val ksuVersion = if (isManager) Natives.version else null
val fullVersion = Natives.getFullVersion()
val ksuFullVersion = if (isKernelSimpleMode) {
val startIndex = fullVersion.indexOf('v')
if (startIndex >= 0) {
val endIndex = fullVersion.indexOf('-', startIndex)
val versionStr = if (endIndex > startIndex) {
fullVersion.substring(startIndex, endIndex)
} else {
fullVersion.substring(startIndex)
}
val numericVersion = "v" + (Regex("""\d+(\.\d+)*""").find(versionStr)?.value ?: versionStr)
numericVersion
} else {
fullVersion
}
} else {
fullVersion
}
val lkmMode = ksuVersion?.let {
if (it >= Natives.MINIMAL_SUPPORTED_KERNEL_LKM && kernelVersion.isGKI()) Natives.isLkmMode else null
}
@@ -174,6 +197,7 @@ class HomeViewModel : ViewModel() {
systemStatus = SystemStatus(
isManager = isManager,
ksuVersion = ksuVersion,
ksuFullVersion = ksuFullVersion,
lkmMode = lkmMode,
kernelVersion = kernelVersion,
isRootAvailable = rootAvailable(),