Skip to content

Commit

Permalink
Revert "ksud: try parse kmi from gki modules if uname is modified"
Browse files Browse the repository at this point in the history
This reverts commit f41d73f.
  • Loading branch information
xiangfeidexiaohuo committed Apr 5, 2024
1 parent 0c368dd commit 511411d
Showing 1 changed file with 10 additions and 30 deletions.
40 changes: 10 additions & 30 deletions userspace/ksud/src/boot_patch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,45 +48,21 @@ pub fn get_kernel_version() -> Result<(i32, i32, i32)> {
}

#[cfg(target_os = "android")]
fn parse_kmi(version: &str) -> Result<String> {
fn parse_kmi() -> Result<String> {
use regex::Regex;
let uname = rustix::system::uname();
let version = uname.release().to_string_lossy();
let re = Regex::new(r"(.* )?(\d+\.\d+)(\S+)?(android\d+)(.*)")?;
let cap = re
.captures(version)
.captures(&version)
.ok_or_else(|| anyhow::anyhow!("No match found"))?;
let android_version = cap.get(4).map_or("", |m| m.as_str());
let kernel_version = cap.get(2).map_or("", |m| m.as_str());
Ok(format!("{android_version}-{kernel_version}"))
}

#[cfg(target_os = "android")]
fn parse_kmi_from_uname() -> Result<String> {
let uname = rustix::system::uname();
let version = uname.release().to_string_lossy();
parse_kmi(&version)
}

#[cfg(target_os = "android")]
fn parse_kmi_from_modules() -> Result<String> {
use std::io::BufRead;
let output = Command::new("modinfo")
.arg("/vendor/lib/modules/fips140.ko")
.output()?;
for line in output.stdout.lines().flatten() {
if line.starts_with("vermagic") {
return parse_kmi(&line);
}
}
anyhow::bail!("Unknown KMI, try use --kmi to specify it.")
}

#[cfg(target_os = "android")]
fn get_kmi() -> Result<String> {
parse_kmi_from_uname().or_else(|_| parse_kmi_from_modules())
}

#[cfg(not(target_os = "android"))]
fn get_kmi() -> Result<String> {
fn parse_kmi() -> Result<String> {
bail!("Unknown KMI, try use --kmi to specify it.")
}

Expand Down Expand Up @@ -257,7 +233,11 @@ fn do_patch(
std::fs::copy(kmod, kmod_file).with_context(|| "copy kernel module failed".to_string())?;
} else {
// If kmod is not specified, extract from assets
let kmi = if let Some(kmi) = kmi { kmi } else { get_kmi()? };
let kmi = if let Some(kmi) = kmi {
kmi
} else {
parse_kmi()?
};
println!("- KMI: {kmi}");
let name = format!("{kmi}_kernelsu.ko");
assets::copy_assets_to_file(&name, kmod_file)
Expand Down

0 comments on commit 511411d

Please sign in to comment.