ksud: Fix name typo
This commit is contained in:
@@ -97,9 +97,9 @@ pub fn get_current_kmi() -> Result<String> {
|
|||||||
bail!("Unsupported platform")
|
bail!("Unsupported platform")
|
||||||
}
|
}
|
||||||
|
|
||||||
fn do_cpio_cmd(magiskboot: &Path, workding_dir: &Path, cmd: &str) -> Result<()> {
|
fn do_cpio_cmd(magiskboot: &Path, workdir: &Path, cmd: &str) -> Result<()> {
|
||||||
let status = Command::new(magiskboot)
|
let status = Command::new(magiskboot)
|
||||||
.current_dir(workding_dir)
|
.current_dir(workdir)
|
||||||
.stdout(Stdio::null())
|
.stdout(Stdio::null())
|
||||||
.stderr(Stdio::null())
|
.stderr(Stdio::null())
|
||||||
.arg("cpio")
|
.arg("cpio")
|
||||||
@@ -111,9 +111,9 @@ fn do_cpio_cmd(magiskboot: &Path, workding_dir: &Path, cmd: &str) -> Result<()>
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn is_magisk_patched(magiskboot: &Path, workding_dir: &Path) -> Result<bool> {
|
fn is_magisk_patched(magiskboot: &Path, workdir: &Path) -> Result<bool> {
|
||||||
let status = Command::new(magiskboot)
|
let status = Command::new(magiskboot)
|
||||||
.current_dir(workding_dir)
|
.current_dir(workdir)
|
||||||
.stdout(Stdio::null())
|
.stdout(Stdio::null())
|
||||||
.stderr(Stdio::null())
|
.stderr(Stdio::null())
|
||||||
.args(["cpio", "ramdisk.cpio", "test"])
|
.args(["cpio", "ramdisk.cpio", "test"])
|
||||||
@@ -123,9 +123,9 @@ fn is_magisk_patched(magiskboot: &Path, workding_dir: &Path) -> Result<bool> {
|
|||||||
Ok(status.code() == Some(1))
|
Ok(status.code() == Some(1))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn is_kernelsu_patched(magiskboot: &Path, workding_dir: &Path) -> Result<bool> {
|
fn is_kernelsu_patched(magiskboot: &Path, workdir: &Path) -> Result<bool> {
|
||||||
let status = Command::new(magiskboot)
|
let status = Command::new(magiskboot)
|
||||||
.current_dir(workding_dir)
|
.current_dir(workdir)
|
||||||
.stdout(Stdio::null())
|
.stdout(Stdio::null())
|
||||||
.stderr(Stdio::null())
|
.stderr(Stdio::null())
|
||||||
.args(["cpio", "ramdisk.cpio", "exists kernelsu.ko"])
|
.args(["cpio", "ramdisk.cpio", "exists kernelsu.ko"])
|
||||||
@@ -155,14 +155,15 @@ pub fn restore(
|
|||||||
magiskboot_path: Option<PathBuf>,
|
magiskboot_path: Option<PathBuf>,
|
||||||
flash: bool,
|
flash: bool,
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
let workding_dir = tempdir::TempDir::new("KernelSU").context("create temp dir failed")?;
|
let tmpdir = tempdir::TempDir::new("KernelSU").context("create temp dir failed")?;
|
||||||
let magiskboot = find_magiskboot(magiskboot_path, workding_dir.path())?;
|
let workdir = tmpdir.path();
|
||||||
|
let magiskboot = find_magiskboot(magiskboot_path, workdir)?;
|
||||||
|
|
||||||
let (bootimage, bootdevice) = find_boot_image(&image, false, false, workding_dir.path())?;
|
let (bootimage, bootdevice) = find_boot_image(&image, false, false, workdir)?;
|
||||||
|
|
||||||
println!("- Unpacking boot image");
|
println!("- Unpacking boot image");
|
||||||
let status = Command::new(&magiskboot)
|
let status = Command::new(&magiskboot)
|
||||||
.current_dir(workding_dir.path())
|
.current_dir(workdir)
|
||||||
.stdout(Stdio::null())
|
.stdout(Stdio::null())
|
||||||
.stderr(Stdio::null())
|
.stderr(Stdio::null())
|
||||||
.arg("unpack")
|
.arg("unpack")
|
||||||
@@ -170,26 +171,20 @@ pub fn restore(
|
|||||||
.status()?;
|
.status()?;
|
||||||
ensure!(status.success(), "magiskboot unpack failed");
|
ensure!(status.success(), "magiskboot unpack failed");
|
||||||
|
|
||||||
let is_kernelsu_patched = is_kernelsu_patched(&magiskboot, workding_dir.path())?;
|
let is_kernelsu_patched = is_kernelsu_patched(&magiskboot, workdir)?;
|
||||||
ensure!(is_kernelsu_patched, "boot image is not patched by KernelSU");
|
ensure!(is_kernelsu_patched, "boot image is not patched by KernelSU");
|
||||||
|
|
||||||
let mut new_boot = None;
|
let mut new_boot = None;
|
||||||
let mut from_backup = false;
|
let mut from_backup = false;
|
||||||
|
|
||||||
#[cfg(target_os = "android")]
|
#[cfg(target_os = "android")]
|
||||||
if do_cpio_cmd(
|
if do_cpio_cmd(&magiskboot, workdir, &format!("exists {BACKUP_FILENAME}")).is_ok() {
|
||||||
&magiskboot,
|
|
||||||
workding_dir.path(),
|
|
||||||
&format!("exists {}", BACKUP_FILENAME),
|
|
||||||
)
|
|
||||||
.is_ok()
|
|
||||||
{
|
|
||||||
do_cpio_cmd(
|
do_cpio_cmd(
|
||||||
&magiskboot,
|
&magiskboot,
|
||||||
workding_dir.path(),
|
workdir,
|
||||||
&format!("extract {0} {0}", BACKUP_FILENAME),
|
&format!("extract {0} {0}", BACKUP_FILENAME),
|
||||||
)?;
|
)?;
|
||||||
let sha = std::fs::read(workding_dir.path().join(BACKUP_FILENAME))?;
|
let sha = std::fs::read(workdir.join(BACKUP_FILENAME))?;
|
||||||
let sha = String::from_utf8(sha)?;
|
let sha = String::from_utf8(sha)?;
|
||||||
let sha = sha.trim();
|
let sha = sha.trim();
|
||||||
let backup_path =
|
let backup_path =
|
||||||
@@ -210,27 +205,27 @@ pub fn restore(
|
|||||||
|
|
||||||
if new_boot.is_none() {
|
if new_boot.is_none() {
|
||||||
// remove kernelsu.ko
|
// remove kernelsu.ko
|
||||||
do_cpio_cmd(&magiskboot, workding_dir.path(), "rm kernelsu.ko")?;
|
do_cpio_cmd(&magiskboot, workdir, "rm kernelsu.ko")?;
|
||||||
|
|
||||||
// if init.real exists, restore it
|
// if init.real exists, restore it
|
||||||
let status = do_cpio_cmd(&magiskboot, workding_dir.path(), "exists init.real").is_ok();
|
let status = do_cpio_cmd(&magiskboot, workdir, "exists init.real").is_ok();
|
||||||
if status {
|
if status {
|
||||||
do_cpio_cmd(&magiskboot, workding_dir.path(), "mv init.real init")?;
|
do_cpio_cmd(&magiskboot, workdir, "mv init.real init")?;
|
||||||
} else {
|
} else {
|
||||||
let ramdisk = workding_dir.path().join("ramdisk.cpio");
|
let ramdisk = workdir.join("ramdisk.cpio");
|
||||||
std::fs::remove_file(ramdisk)?;
|
std::fs::remove_file(ramdisk)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
println!("- Repacking boot image");
|
println!("- Repacking boot image");
|
||||||
let status = Command::new(&magiskboot)
|
let status = Command::new(&magiskboot)
|
||||||
.current_dir(workding_dir.path())
|
.current_dir(workdir)
|
||||||
.stdout(Stdio::null())
|
.stdout(Stdio::null())
|
||||||
.stderr(Stdio::null())
|
.stderr(Stdio::null())
|
||||||
.arg("repack")
|
.arg("repack")
|
||||||
.arg(bootimage.display().to_string())
|
.arg(bootimage.display().to_string())
|
||||||
.status()?;
|
.status()?;
|
||||||
ensure!(status.success(), "magiskboot repack failed");
|
ensure!(status.success(), "magiskboot repack failed");
|
||||||
new_boot = Some(workding_dir.path().join("new-boot.img"));
|
new_boot = Some(workdir.join("new-boot.img"));
|
||||||
}
|
}
|
||||||
|
|
||||||
let new_boot = new_boot.unwrap();
|
let new_boot = new_boot.unwrap();
|
||||||
@@ -311,10 +306,10 @@ fn do_patch(
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
let workding_dir = tempdir::TempDir::new("KernelSU").context("create temp dir failed")?;
|
let tmpdir = tempdir::TempDir::new("KernelSU").context("create temp dir failed")?;
|
||||||
|
let workdir = tmpdir.path();
|
||||||
|
|
||||||
let (bootimage, bootdevice) =
|
let (bootimage, bootdevice) = find_boot_image(&image, ota, is_replace_kernel, workdir)?;
|
||||||
find_boot_image(&image, ota, is_replace_kernel, workding_dir.path())?;
|
|
||||||
|
|
||||||
let bootimage = bootimage.display().to_string();
|
let bootimage = bootimage.display().to_string();
|
||||||
|
|
||||||
@@ -322,16 +317,15 @@ fn do_patch(
|
|||||||
let _ = assets::ensure_binaries(false);
|
let _ = assets::ensure_binaries(false);
|
||||||
|
|
||||||
// extract magiskboot
|
// extract magiskboot
|
||||||
let magiskboot = find_magiskboot(magiskboot_path, workding_dir.path())?;
|
let magiskboot = find_magiskboot(magiskboot_path, workdir)?;
|
||||||
|
|
||||||
if let Some(kernel) = kernel {
|
if let Some(kernel) = kernel {
|
||||||
std::fs::copy(kernel, workding_dir.path().join("kernel"))
|
std::fs::copy(kernel, workdir.join("kernel")).context("copy kernel from failed")?;
|
||||||
.context("copy kernel from failed")?;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
println!("- Preparing assets");
|
println!("- Preparing assets");
|
||||||
|
|
||||||
let kmod_file = workding_dir.path().join("kernelsu.ko");
|
let kmod_file = workdir.join("kernelsu.ko");
|
||||||
if let Some(kmod) = kmod {
|
if let Some(kmod) = kmod {
|
||||||
std::fs::copy(kmod, kmod_file).context("copy kernel module failed")?;
|
std::fs::copy(kmod, kmod_file).context("copy kernel module failed")?;
|
||||||
} else {
|
} else {
|
||||||
@@ -347,7 +341,7 @@ fn do_patch(
|
|||||||
.with_context(|| format!("Failed to copy {name}"))?;
|
.with_context(|| format!("Failed to copy {name}"))?;
|
||||||
};
|
};
|
||||||
|
|
||||||
let init_file = workding_dir.path().join("init");
|
let init_file = workdir.join("init");
|
||||||
if let Some(init) = init {
|
if let Some(init) = init {
|
||||||
std::fs::copy(init, init_file).context("copy init failed")?;
|
std::fs::copy(init, init_file).context("copy init failed")?;
|
||||||
} else {
|
} else {
|
||||||
@@ -361,7 +355,7 @@ fn do_patch(
|
|||||||
|
|
||||||
println!("- Unpacking boot image");
|
println!("- Unpacking boot image");
|
||||||
let status = Command::new(&magiskboot)
|
let status = Command::new(&magiskboot)
|
||||||
.current_dir(workding_dir.path())
|
.current_dir(workdir)
|
||||||
.stdout(Stdio::null())
|
.stdout(Stdio::null())
|
||||||
.stderr(Stdio::null())
|
.stderr(Stdio::null())
|
||||||
.arg("unpack")
|
.arg("unpack")
|
||||||
@@ -369,37 +363,33 @@ fn do_patch(
|
|||||||
.status()?;
|
.status()?;
|
||||||
ensure!(status.success(), "magiskboot unpack failed");
|
ensure!(status.success(), "magiskboot unpack failed");
|
||||||
|
|
||||||
let no_ramdisk = !workding_dir.path().join("ramdisk.cpio").exists();
|
let no_ramdisk = !workdir.join("ramdisk.cpio").exists();
|
||||||
let is_magisk_patched = is_magisk_patched(&magiskboot, workding_dir.path())?;
|
let is_magisk_patched = is_magisk_patched(&magiskboot, workdir)?;
|
||||||
ensure!(
|
ensure!(
|
||||||
no_ramdisk || !is_magisk_patched,
|
no_ramdisk || !is_magisk_patched,
|
||||||
"Cannot work with Magisk patched image"
|
"Cannot work with Magisk patched image"
|
||||||
);
|
);
|
||||||
|
|
||||||
println!("- Adding KernelSU LKM");
|
println!("- Adding KernelSU LKM");
|
||||||
let is_kernelsu_patched = is_kernelsu_patched(&magiskboot, workding_dir.path())?;
|
let is_kernelsu_patched = is_kernelsu_patched(&magiskboot, workdir)?;
|
||||||
|
|
||||||
let mut need_backup = false;
|
let mut need_backup = false;
|
||||||
if !is_kernelsu_patched {
|
if !is_kernelsu_patched {
|
||||||
// kernelsu.ko is not exist, backup init if necessary
|
// kernelsu.ko is not exist, backup init if necessary
|
||||||
let status = do_cpio_cmd(&magiskboot, workding_dir.path(), "exists init");
|
let status = do_cpio_cmd(&magiskboot, workdir, "exists init");
|
||||||
if status.is_ok() {
|
if status.is_ok() {
|
||||||
do_cpio_cmd(&magiskboot, workding_dir.path(), "mv init init.real")?;
|
do_cpio_cmd(&magiskboot, workdir, "mv init init.real")?;
|
||||||
}
|
}
|
||||||
|
|
||||||
need_backup = flash;
|
need_backup = flash;
|
||||||
}
|
}
|
||||||
|
|
||||||
do_cpio_cmd(&magiskboot, workding_dir.path(), "add 0755 init init")?;
|
do_cpio_cmd(&magiskboot, workdir, "add 0755 init init")?;
|
||||||
do_cpio_cmd(
|
do_cpio_cmd(&magiskboot, workdir, "add 0755 kernelsu.ko kernelsu.ko")?;
|
||||||
&magiskboot,
|
|
||||||
workding_dir.path(),
|
|
||||||
"add 0755 kernelsu.ko kernelsu.ko",
|
|
||||||
)?;
|
|
||||||
|
|
||||||
#[cfg(target_os = "android")]
|
#[cfg(target_os = "android")]
|
||||||
if need_backup {
|
if need_backup {
|
||||||
if let Err(e) = do_backup(&magiskboot, workding_dir.path(), &bootimage) {
|
if let Err(e) = do_backup(&magiskboot, workdir, &bootimage) {
|
||||||
println!("- Backup stock image failed: {e}");
|
println!("- Backup stock image failed: {e}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -407,14 +397,14 @@ fn do_patch(
|
|||||||
println!("- Repacking boot image");
|
println!("- Repacking boot image");
|
||||||
// magiskboot repack boot.img
|
// magiskboot repack boot.img
|
||||||
let status = Command::new(&magiskboot)
|
let status = Command::new(&magiskboot)
|
||||||
.current_dir(workding_dir.path())
|
.current_dir(workdir)
|
||||||
.stdout(Stdio::null())
|
.stdout(Stdio::null())
|
||||||
.stderr(Stdio::null())
|
.stderr(Stdio::null())
|
||||||
.arg("repack")
|
.arg("repack")
|
||||||
.arg(&bootimage)
|
.arg(&bootimage)
|
||||||
.status()?;
|
.status()?;
|
||||||
ensure!(status.success(), "magiskboot repack failed");
|
ensure!(status.success(), "magiskboot repack failed");
|
||||||
let new_boot = workding_dir.path().join("new-boot.img");
|
let new_boot = workdir.join("new-boot.img");
|
||||||
|
|
||||||
if patch_file {
|
if patch_file {
|
||||||
// if image is specified, write to output file
|
// if image is specified, write to output file
|
||||||
@@ -446,10 +436,10 @@ fn do_patch(
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(target_os = "android")]
|
#[cfg(target_os = "android")]
|
||||||
fn do_backup(magiskboot: &Path, workding_dir: &Path, image: &str) -> Result<()> {
|
fn do_backup(magiskboot: &Path, workdir: &Path, image: &str) -> Result<()> {
|
||||||
// calc boot sha1
|
// calc boot sha1
|
||||||
let output = Command::new(magiskboot)
|
let output = Command::new(magiskboot)
|
||||||
.current_dir(workding_dir)
|
.current_dir(workdir)
|
||||||
.arg("sha1")
|
.arg("sha1")
|
||||||
.arg(image)
|
.arg(image)
|
||||||
.output()?;
|
.output()?;
|
||||||
@@ -465,10 +455,10 @@ fn do_backup(magiskboot: &Path, workding_dir: &Path, image: &str) -> Result<()>
|
|||||||
// magiskboot cpio ramdisk.cpio 'add 0755 $BACKUP_FILENAME'
|
// magiskboot cpio ramdisk.cpio 'add 0755 $BACKUP_FILENAME'
|
||||||
let target = format!("{KSU_BACKUP_DIR}{filename}");
|
let target = format!("{KSU_BACKUP_DIR}{filename}");
|
||||||
std::fs::copy(image, &target).with_context(|| format!("backup to {target}"))?;
|
std::fs::copy(image, &target).with_context(|| format!("backup to {target}"))?;
|
||||||
std::fs::write(workding_dir.join(BACKUP_FILENAME), sha1.as_bytes()).context("write sha1")?;
|
std::fs::write(workdir.join(BACKUP_FILENAME), sha1.as_bytes()).context("write sha1")?;
|
||||||
do_cpio_cmd(
|
do_cpio_cmd(
|
||||||
magiskboot,
|
magiskboot,
|
||||||
workding_dir,
|
workdir,
|
||||||
&format!("add 0755 {0} {0}", BACKUP_FILENAME),
|
&format!("add 0755 {0} {0}", BACKUP_FILENAME),
|
||||||
)?;
|
)?;
|
||||||
println!("- Stock image has been backup to");
|
println!("- Stock image has been backup to");
|
||||||
@@ -512,7 +502,7 @@ fn flash_boot(bootdevice: &Option<String>, new_boot: PathBuf) -> Result<()> {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn find_magiskboot(magiskboot_path: Option<PathBuf>, workding_dir: &Path) -> Result<PathBuf> {
|
fn find_magiskboot(magiskboot_path: Option<PathBuf>, workdir: &Path) -> Result<PathBuf> {
|
||||||
let magiskboot = {
|
let magiskboot = {
|
||||||
if which("magiskboot").is_ok() {
|
if which("magiskboot").is_ok() {
|
||||||
let _ = assets::ensure_binaries(true);
|
let _ = assets::ensure_binaries(true);
|
||||||
@@ -522,7 +512,7 @@ fn find_magiskboot(magiskboot_path: Option<PathBuf>, workding_dir: &Path) -> Res
|
|||||||
let magiskboot = if let Some(magiskboot_path) = magiskboot_path {
|
let magiskboot = if let Some(magiskboot_path) = magiskboot_path {
|
||||||
std::fs::canonicalize(magiskboot_path)?
|
std::fs::canonicalize(magiskboot_path)?
|
||||||
} else {
|
} else {
|
||||||
let magiskboot_path = workding_dir.join("magiskboot");
|
let magiskboot_path = workdir.join("magiskboot");
|
||||||
assets::copy_assets_to_file("magiskboot", &magiskboot_path)
|
assets::copy_assets_to_file("magiskboot", &magiskboot_path)
|
||||||
.context("copy magiskboot failed")?;
|
.context("copy magiskboot failed")?;
|
||||||
magiskboot_path
|
magiskboot_path
|
||||||
@@ -540,7 +530,7 @@ fn find_boot_image(
|
|||||||
image: &Option<PathBuf>,
|
image: &Option<PathBuf>,
|
||||||
ota: bool,
|
ota: bool,
|
||||||
is_replace_kernel: bool,
|
is_replace_kernel: bool,
|
||||||
workding_dir: &Path,
|
workdir: &Path,
|
||||||
) -> Result<(PathBuf, Option<String>)> {
|
) -> Result<(PathBuf, Option<String>)> {
|
||||||
let bootimage;
|
let bootimage;
|
||||||
let mut bootdevice = None;
|
let mut bootdevice = None;
|
||||||
@@ -568,7 +558,7 @@ fn find_boot_image(
|
|||||||
};
|
};
|
||||||
|
|
||||||
println!("- Bootdevice: {boot_partition}");
|
println!("- Bootdevice: {boot_partition}");
|
||||||
let tmp_boot_path = workding_dir.join("boot.img");
|
let tmp_boot_path = workdir.join("boot.img");
|
||||||
|
|
||||||
dd(&boot_partition, &tmp_boot_path)?;
|
dd(&boot_partition, &tmp_boot_path)?;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user