ksud: add boot-info cli

This commit is contained in:
weishu
2024-03-23 10:46:16 +08:00
parent b2d0de325f
commit ef92c32729
3 changed files with 53 additions and 7 deletions

View File

@@ -37,3 +37,13 @@ pub fn copy_assets_to_file(name: &str, dst: impl AsRef<Path>) -> Result<()> {
std::fs::write(dst, asset.data)?; std::fs::write(dst, asset.data)?;
Ok(()) Ok(())
} }
pub fn list_supported_kmi() -> Result<Vec<String>> {
let mut list = Vec::new();
for file in Asset::iter() {
if let Some(kmi) = file.strip_suffix(".ko") {
list.push(kmi.to_string());
}
}
Ok(list)
}

View File

@@ -53,7 +53,7 @@ fn parse_kmi(version: &str) -> Result<String> {
let re = Regex::new(r"(.* )?(\d+\.\d+)(\S+)?(android\d+)(.*)")?; let re = Regex::new(r"(.* )?(\d+\.\d+)(\S+)?(android\d+)(.*)")?;
let cap = re let cap = re
.captures(version) .captures(version)
.ok_or_else(|| anyhow::anyhow!("Unknown KMI, please choose manually."))?; .ok_or_else(|| anyhow::anyhow!("Failed to get KMI from boot/modules"))?;
let android_version = cap.get(4).map_or("", |m| m.as_str()); let android_version = cap.get(4).map_or("", |m| m.as_str());
let kernel_version = cap.get(2).map_or("", |m| m.as_str()); let kernel_version = cap.get(2).map_or("", |m| m.as_str());
Ok(format!("{android_version}-{kernel_version}")) Ok(format!("{android_version}-{kernel_version}"))
@@ -85,13 +85,13 @@ fn parse_kmi_from_modules() -> Result<String> {
} }
#[cfg(target_os = "android")] #[cfg(target_os = "android")]
fn get_kmi() -> Result<String> { pub fn get_current_kmi() -> Result<String> {
parse_kmi_from_uname().or_else(|_| parse_kmi_from_modules()) parse_kmi_from_uname().or_else(|_| parse_kmi_from_modules())
} }
#[cfg(not(target_os = "android"))] #[cfg(not(target_os = "android"))]
fn get_kmi() -> Result<String> { pub fn get_current_kmi() -> Result<String> {
bail!("Unknown KMI, try use --kmi to specify it.") bail!("Unsupported platform")
} }
fn do_cpio_cmd(magiskboot: &Path, workding_dir: &Path, cmd: &str) -> Result<()> { fn do_cpio_cmd(magiskboot: &Path, workding_dir: &Path, cmd: &str) -> Result<()> {
@@ -261,7 +261,11 @@ fn do_patch(
std::fs::copy(kmod, kmod_file).with_context(|| "copy kernel module failed".to_string())?; std::fs::copy(kmod, kmod_file).with_context(|| "copy kernel module failed".to_string())?;
} else { } else {
// If kmod is not specified, extract from assets // 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 {
get_current_kmi().with_context(|| "Unknown KMI, please choose LKM manually")?
};
println!("- KMI: {kmi}"); println!("- KMI: {kmi}");
let name = format!("{kmi}_kernelsu.ko"); let name = format!("{kmi}_kernelsu.ko");
assets::copy_assets_to_file(&name, kmod_file) assets::copy_assets_to_file(&name, kmod_file)
@@ -331,8 +335,10 @@ fn do_patch(
// if image is specified, write to output file // if image is specified, write to output file
let output_dir = out.unwrap_or(std::env::current_dir()?); let output_dir = out.unwrap_or(std::env::current_dir()?);
let now = chrono::Utc::now(); let now = chrono::Utc::now();
let output_image = let output_image = output_dir.join(format!(
output_dir.join(format!("kernelsu_patched_{}.img", now.format("%Y%m%d_%H%M%S"))); "kernelsu_patched_{}.img",
now.format("%Y%m%d_%H%M%S")
));
if std::fs::rename(&new_boot, &output_image).is_err() { if std::fs::rename(&new_boot, &output_image).is_err() {
std::fs::copy(&new_boot, &output_image) std::fs::copy(&new_boot, &output_image)

View File

@@ -87,12 +87,28 @@ enum Commands {
#[arg(long, default_value = None)] #[arg(long, default_value = None)]
kmi: Option<String>, kmi: Option<String>,
}, },
/// Show boot information
BootInfo {
#[command(subcommand)]
command: BootInfo,
},
/// For developers /// For developers
Debug { Debug {
#[command(subcommand)] #[command(subcommand)]
command: Debug, command: Debug,
}, },
} }
#[derive(clap::Subcommand, Debug)]
enum BootInfo {
/// show current kmi version
CurrentKmi,
/// show supported kmi versions
SupportedKmi,
}
#[derive(clap::Subcommand, Debug)] #[derive(clap::Subcommand, Debug)]
enum Debug { enum Debug {
/// Set the manager app, kernel CONFIG_KSU_DEBUG should be enabled. /// Set the manager app, kernel CONFIG_KSU_DEBUG should be enabled.
@@ -322,6 +338,20 @@ pub fn run() -> Result<()> {
magiskboot, magiskboot,
kmi, kmi,
} => crate::boot_patch::patch(boot, kernel, module, init, ota, flash, out, magiskboot, kmi), } => crate::boot_patch::patch(boot, kernel, module, init, ota, flash, out, magiskboot, kmi),
Commands::BootInfo { command } => match command {
BootInfo::CurrentKmi => {
let kmi = crate::boot_patch::get_current_kmi()?;
println!("{}", kmi);
// return here to avoid printing the error message
return Ok(());
}
BootInfo::SupportedKmi => {
let kmi = crate::assets::list_supported_kmi()?;
kmi.iter().for_each(|kmi| println!("{}", kmi));
return Ok(());
}
},
}; };
if let Err(e) = &result { if let Err(e) = &result {