diff --git a/userspace/ksud/Cargo.lock b/userspace/ksud/Cargo.lock index 3d5d8e86da1e..b8c5cc36527a 100644 --- a/userspace/ksud/Cargo.lock +++ b/userspace/ksud/Cargo.lock @@ -814,6 +814,7 @@ dependencies = [ "rustix 0.38.30", "serde", "serde_json", + "sha1", "sha256", "tempdir", "which", diff --git a/userspace/ksud/Cargo.toml b/userspace/ksud/Cargo.toml index 19bf0c8db994..7e8ce1841baa 100644 --- a/userspace/ksud/Cargo.toml +++ b/userspace/ksud/Cargo.toml @@ -34,6 +34,7 @@ rust-embed = { version = "8", features = [ which = "6" getopts = "0.2" sha256 = "1" +sha1 = "0.10" tempdir = "0.3" chrono = "0.4" hole-punch = { git = "https://github.com/tiann/hole-punch" } diff --git a/userspace/ksud/src/boot_patch.rs b/userspace/ksud/src/boot_patch.rs index 8b3a9c04559d..4ed3db82112c 100644 --- a/userspace/ksud/src/boot_patch.rs +++ b/userspace/ksud/src/boot_patch.rs @@ -435,20 +435,27 @@ fn do_patch( Ok(()) } +#[cfg(target_os = "android")] +fn calculate_sha1(file_path: impl AsRef) -> io::Result { + let mut file = File::open(file_path.as_ref())?; + let mut hasher = Sha1::new(); + let mut buffer = [0; 1024]; + + loop { + let n = file.read(&mut buffer)?; + if n == 0 { + break; + } + hasher.update(&buffer[..n]); + } + + let result = hasher.finalize(); + Ok(format!("{:x}", result)) +} + #[cfg(target_os = "android")] fn do_backup(magiskboot: &Path, workdir: &Path, image: &str) -> Result<()> { - // calc boot sha1 - let output = Command::new(magiskboot) - .current_dir(workdir) - .arg("sha1") - .arg(image) - .output()?; - ensure!( - output.status.success(), - "Cannot calculate sha1 of original boot!" - ); - let output = String::from_utf8(output.stdout)?; - let sha1 = output.trim(); + let sha1 = calculate_sha1(image)?; let filename = format!("{KSU_BACKUP_FILE_PREFIX}{sha1}"); println!("- Backup stock boot image");