ksud: refine boot patch, add --out-name arg to boot-patch and boot-restore command (#2982)

This commit is contained in:
5ec1cff
2025-11-24 02:04:27 +08:00
committed by ShirkNeko
parent f2de18bc26
commit c5d473c815
3 changed files with 304 additions and 312 deletions

View File

@@ -12,10 +12,9 @@ use which::which;
use crate::{
assets,
defs::{self, BACKUP_FILENAME, KSU_BACKUP_DIR, KSU_BACKUP_FILE_PREFIX},
defs::{BACKUP_FILENAME, KSU_BACKUP_DIR, KSU_BACKUP_FILE_PREFIX},
utils,
};
#[cfg(target_os = "android")]
fn ensure_gki_kernel() -> Result<()> {
let version = get_kernel_version()?;
@@ -81,7 +80,7 @@ fn parse_kmi_from_modules() -> Result<String> {
return parse_kmi(&line);
}
}
anyhow::bail!("Parse KMI from modules failed")
bail!("Parse KMI from modules failed")
}
#[cfg(target_os = "android")]
@@ -142,10 +141,7 @@ fn parse_kmi_from_boot(magiskboot: &Path, image: &PathBuf, workdir: &Path) -> Re
.context("Failed to execute magiskboot command")?;
if !status.success() {
bail!(
"magiskboot unpack failed with status: {:?}",
status.code().unwrap()
);
bail!("magiskboot unpack failed with status: {:?}", status);
}
parse_kmi_from_kernel(&image_path, workdir)
@@ -206,11 +202,33 @@ fn dd<P: AsRef<Path>, Q: AsRef<Path>>(ifile: P, ofile: Q) -> Result<()> {
Ok(())
}
pub fn restore(
image: Option<PathBuf>,
magiskboot_path: Option<PathBuf>,
flash: bool,
) -> Result<()> {
#[derive(clap::Args, Debug)]
pub struct BootRestoreArgs {
/// boot image path, if not specified, will try to find the boot image automatically
#[arg(short, long)]
pub boot: Option<PathBuf>,
/// Flash it to boot partition after restore
#[arg(short, long, default_value = "false")]
pub flash: bool,
/// magiskboot path, if not specified, will search from $PATH
#[arg(long, default_value = None)]
pub magiskboot: Option<PathBuf>,
/// File name of the output.
#[arg(long, default_value = None)]
pub out_name: Option<String>,
}
pub fn restore(args: BootRestoreArgs) -> Result<()> {
let BootRestoreArgs {
boot: image,
flash,
magiskboot: magiskboot_path,
out_name,
} = args;
let tmpdir = tempfile::Builder::new()
.prefix("KernelSU")
.tempdir()
@@ -283,7 +301,8 @@ pub fn restore(
println!("- Backup info is absent!");
}
if new_boot.is_none() {
let new_boot = new_boot.map_or_else(
|| -> Result<_> {
// remove kernelsu.ko
do_cpio_cmd(&magiskboot, workdir, ramdisk, "rm kernelsu.ko")?;
@@ -302,19 +321,19 @@ pub fn restore(
.arg(&bootimage)
.status()?;
ensure!(status.success(), "magiskboot repack failed");
new_boot = Some(workdir.join("new-boot.img"));
}
let new_boot = new_boot.unwrap();
Ok(workdir.join("new-boot.img"))
},
Ok,
)?;
if image.is_some() {
// if image is specified, write to output file
let output_dir = std::env::current_dir()?;
let name = out_name.unwrap_or_else(|| {
let now = chrono::Utc::now();
let output_image = output_dir.join(format!(
"kernelsu_restore_{}.img",
now.format("%Y%m%d_%H%M%S")
));
format!("kernelsu_restore_{}.img", now.format("%Y%m%d_%H%M%S"))
});
let output_image = output_dir.join(name);
if from_backup || std::fs::rename(&new_boot, &output_image).is_err() {
std::fs::copy(&new_boot, &output_image).context("copy out new boot failed")?;
@@ -334,41 +353,69 @@ pub fn restore(
Ok(())
}
#[allow(clippy::too_many_arguments)]
pub fn patch(
image: Option<PathBuf>,
kernel: Option<PathBuf>,
kmod: Option<PathBuf>,
init: Option<PathBuf>,
ota: bool,
flash: bool,
out: Option<PathBuf>,
magiskboot: Option<PathBuf>,
kmi: Option<String>,
partition: Option<String>,
) -> Result<()> {
let result = do_patch(
image, kernel, kmod, init, ota, flash, out, magiskboot, kmi, partition,
);
if let Err(ref e) = result {
println!("- Install Error: {e}");
}
result
#[derive(clap::Args, Debug)]
pub struct BootPatchArgs {
/// boot image path, if not specified, will try to find the boot image automatically
#[arg(short, long)]
pub boot: Option<PathBuf>,
/// kernel image path to replace
#[arg(short, long)]
pub kernel: Option<PathBuf>,
/// LKM module path to replace, if not specified, will use the builtin one
#[arg(short, long)]
pub module: Option<PathBuf>,
/// init to be replaced
#[arg(short, long, requires("module"))]
pub init: Option<PathBuf>,
/// will use another slot when boot image is not specified
#[arg(short = 'u', long, default_value = "false")]
pub ota: bool,
/// Flash it to boot partition after patch
#[arg(short, long, default_value = "false")]
pub flash: bool,
/// output path, if not specified, will use current directory
#[arg(short, long, default_value = None)]
pub out: Option<PathBuf>,
/// magiskboot path, if not specified, will search from $PATH
#[arg(long, default_value = None)]
pub magiskboot: Option<PathBuf>,
/// KMI version, if specified, will use the specified KMI
#[arg(long, default_value = None)]
pub kmi: Option<String>,
/// target partition override (init_boot | boot | vendor_boot)
#[arg(long, default_value = None)]
pub partition: Option<String>,
/// File name of the output.
#[arg(long, default_value = None)]
pub out_name: Option<String>,
}
#[allow(clippy::too_many_arguments, clippy::needless_pass_by_value)]
fn do_patch(
image: Option<PathBuf>,
kernel: Option<PathBuf>,
kmod: Option<PathBuf>,
init: Option<PathBuf>,
ota: bool,
flash: bool,
out: Option<PathBuf>,
magiskboot_path: Option<PathBuf>,
kmi: Option<String>,
partition: Option<String>,
) -> Result<()> {
pub fn patch(args: BootPatchArgs) -> Result<()> {
let inner = move || {
let BootPatchArgs {
boot: image,
init,
kernel,
module: kmod,
ota,
flash,
out,
magiskboot: magiskboot_path,
kmi,
partition,
out_name,
} = args;
println!(include_str!("banner"));
let patch_file = image.is_some();
@@ -396,31 +443,32 @@ fn do_patch(
// extract magiskboot
let magiskboot = find_magiskboot(magiskboot_path, workdir)?;
let kmi = if let Some(kmi) = kmi {
kmi
} else {
match get_current_kmi() {
let kmi = kmi.map_or_else(
|| -> Result<_> {
Ok(match get_current_kmi() {
Ok(value) => value,
Err(e) => {
println!("- {e}");
if let Some(image_path) = &image {
println!(
"- Trying to auto detect KMI version for {}",
image_path.to_str().unwrap()
image_path.display()
);
parse_kmi_from_boot(&magiskboot, image_path, tmpdir.path())?
} else if let Some(kernel_path) = &kernel {
println!(
"- Trying to auto detect KMI version for {}",
kernel_path.to_str().unwrap()
kernel_path.display()
);
parse_kmi_from_kernel(kernel_path, tmpdir.path())?
} else {
String::new()
}
}
}
};
})
},
Ok,
)?;
let (bootimage, bootdevice) =
find_boot_image(&image, &kmi, ota, is_replace_kernel, workdir, &partition)?;
@@ -521,12 +569,11 @@ fn do_patch(
if patch_file {
// if image is specified, write to output file
let output_dir = out.unwrap_or(std::env::current_dir()?);
let name = out_name.unwrap_or_else(|| {
let now = chrono::Utc::now();
let output_image = output_dir.join(format!(
"kernelsu_patched_{}.img",
now.format("%Y%m%d_%H%M%S")
));
format!("kernelsu_patched_{}.img", now.format("%Y%m%d_%H%M%S"))
});
let output_image = output_dir.join(name);
if std::fs::rename(&new_boot, &output_image).is_err() {
std::fs::copy(&new_boot, &output_image).context("copy out new boot failed")?;
}
@@ -545,6 +592,13 @@ fn do_patch(
println!("- Done!");
Ok(())
};
let result = inner();
if let Err(ref e) = result {
println!("- Patch Error: {e}");
}
result
}
#[cfg(target_os = "android")]
@@ -592,7 +646,7 @@ fn do_backup(magiskboot: &Path, workdir: &Path, cpio_path: &Path, image: &Path)
fn clean_backup(sha1: &str) -> Result<()> {
println!("- Clean up backup");
let backup_name = format!("{KSU_BACKUP_FILE_PREFIX}{sha1}");
let dir = std::fs::read_dir(defs::KSU_BACKUP_DIR)?;
let dir = std::fs::read_dir(KSU_BACKUP_DIR)?;
for entry in dir.flatten() {
let path = entry.path();
if !path.is_file() {
@@ -744,7 +798,7 @@ pub fn list_available_partitions() -> Vec<String> {
candidates
.into_iter()
.filter(|name| Path::new(&format!("/dev/block/by-name/{name}{slot_suffix}")).exists())
.map(std::string::ToString::to_string)
.map(ToString::to_string)
.collect()
}
@@ -773,7 +827,7 @@ fn post_ota() -> Result<()> {
.arg(format!("set-active-boot-slot {target_slot}"))
.status()?;
let post_fs_data = std::path::Path::new(ADB_DIR).join("post-fs-data.d");
let post_fs_data = Path::new(ADB_DIR).join("post-fs-data.d");
utils::ensure_dir_exists(&post_fs_data)?;
let post_ota_sh = post_fs_data.join("post_ota.sh");

View File

@@ -7,6 +7,7 @@ use android_logger::Config;
#[cfg(target_os = "android")]
use log::LevelFilter;
use crate::boot_patch::{BootPatchArgs, BootRestoreArgs};
use crate::{apk_sign, assets, debug, defs, init_event, ksucalls, module, module_config, utils};
/// KernelSU userspace cli
@@ -66,62 +67,10 @@ enum Commands {
},
/// Patch boot or init_boot images to apply KernelSU
BootPatch {
/// boot image path, if not specified, will try to find the boot image automatically
#[arg(short, long)]
boot: Option<PathBuf>,
/// kernel image path to replace
#[arg(short, long)]
kernel: Option<PathBuf>,
/// LKM module path to replace, if not specified, will use the builtin one
#[arg(short, long)]
module: Option<PathBuf>,
/// init to be replaced
#[arg(short, long, requires("module"))]
init: Option<PathBuf>,
/// will use another slot when boot image is not specified
#[arg(short = 'u', long, default_value = "false")]
ota: bool,
/// Flash it to boot partition after patch
#[arg(short, long, default_value = "false")]
flash: bool,
/// output path, if not specified, will use current directory
#[arg(short, long, default_value = None)]
out: Option<PathBuf>,
/// magiskboot path, if not specified, will search from $PATH
#[arg(long, default_value = None)]
magiskboot: Option<PathBuf>,
/// KMI version, if specified, will use the specified KMI
#[arg(long, default_value = None)]
kmi: Option<String>,
/// target partition override (init_boot | boot | vendor_boot)
#[arg(long, default_value = None)]
partition: Option<String>,
},
BootPatch(BootPatchArgs),
/// Restore boot or init_boot images patched by KernelSU
BootRestore {
/// boot image path, if not specified, will try to find the boot image automatically
#[arg(short, long)]
boot: Option<PathBuf>,
/// Flash it to boot partition after patch
#[arg(short, long, default_value = "false")]
flash: bool,
/// magiskboot path, if not specified, will search from $PATH
#[arg(long, default_value = None)]
magiskboot: Option<PathBuf>,
},
BootRestore(BootRestoreArgs),
/// Show boot information
BootInfo {
@@ -703,20 +652,7 @@ pub fn run() -> Result<()> {
},
},
Commands::BootPatch {
boot,
init,
kernel,
module,
ota,
flash,
out,
magiskboot,
kmi,
partition,
} => crate::boot_patch::patch(
boot, kernel, module, init, ota, flash, out, magiskboot, kmi, partition,
),
Commands::BootPatch(boot_patch) => crate::boot_patch::patch(boot_patch),
Commands::BootInfo { command } => match command {
BootInfo::CurrentKmi => {
@@ -758,11 +694,7 @@ pub fn run() -> Result<()> {
return Ok(());
}
},
Commands::BootRestore {
boot,
magiskboot,
flash,
} => crate::boot_patch::restore(boot, magiskboot, flash),
Commands::BootRestore(boot_restore) => crate::boot_patch::restore(boot_restore),
Commands::Kernel { command } => match command {
Kernel::NukeExt4Sysfs { mnt } => ksucalls::nuke_ext4_sysfs(&mnt),
Kernel::Umount { command } => match command {

View File

@@ -17,6 +17,7 @@ use std::os::unix::prelude::PermissionsExt;
use std::path::PathBuf;
use crate::boot_patch::BootRestoreArgs;
#[cfg(any(target_os = "linux", target_os = "android"))]
use rustix::{
process,
@@ -217,7 +218,12 @@ pub fn uninstall(magiskboot_path: Option<PathBuf>) -> Result<()> {
std::fs::remove_file(defs::DAEMON_PATH).ok();
std::fs::remove_dir_all(defs::MODULE_DIR).ok();
println!("- Restore boot image..");
boot_patch::restore(None, magiskboot_path, true)?;
boot_patch::restore(BootRestoreArgs {
boot: None,
flash: true,
magiskboot: magiskboot_path,
out_name: None,
})?;
println!("- Uninstall KernelSU manager..");
Command::new("pm")
.args(["uninstall", "com.sukisu.ultra"])