Skip to content

Commit

Permalink
post-ota
Browse files Browse the repository at this point in the history
  • Loading branch information
tiann committed Mar 13, 2024
1 parent f411018 commit 53238da
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 0 deletions.
Binary file modified userspace/ksud/bin/aarch64/ksuinit
Binary file not shown.
Binary file modified userspace/ksud/bin/x86_64/ksuinit
Binary file not shown.
1 change: 1 addition & 0 deletions userspace/ksud/src/assets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use crate::{defs::BINARY_DIR, utils};

pub const RESETPROP_PATH: &str = concatcp!(BINARY_DIR, "resetprop");
pub const BUSYBOX_PATH: &str = concatcp!(BINARY_DIR, "busybox");
pub const BOOTCTL_PATH: &str = concatcp!(BINARY_DIR, "bootctl");

#[cfg(target_arch = "aarch64")]
#[derive(RustEmbed)]
Expand Down
46 changes: 46 additions & 0 deletions userspace/ksud/src/boot_patch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,9 @@ fn do_patch(
bootdevice = Some(boot_partition);
};

// try extract magiskboot/bootctl
let _ = assets::ensure_binaries(false);

// extract magiskboot
let magiskboot = {
if which("magiskboot").is_ok() {
Expand Down Expand Up @@ -279,8 +282,51 @@ fn do_patch(
ensure!(status.success(), "set boot device rw failed");

dd(&new_boot, &bootdevice).with_context(|| "flash boot failed")?;

if ota {
post_ota()?;
}
}

println!("- Done!");
Ok(())
}

fn post_ota() -> Result<()> {
use crate::defs::ADB_DIR;
use assets::BOOTCTL_PATH;
let status = Command::new(BOOTCTL_PATH).arg("hal-info").status()?;
if !status.success() {
return Ok(());
}

let current_slot = Command::new(BOOTCTL_PATH)
.arg("get-current-slot")
.output()?
.stdout;
let current_slot = String::from_utf8(current_slot)?;
let current_slot = current_slot.trim();
let target_slot = if current_slot == "0" { 1 } else { 0 };

Command::new(BOOTCTL_PATH)
.arg(format!("set-active-boot-slot {target_slot}"))
.status()?;

let post_ota_sh = std::path::Path::new(ADB_DIR)
.join("post-fs-data.d")
.join("post_ota.sh");

let sh_content = format!(
r###"
{BOOTCTL_PATH} mark-boot-successful
rm -f {BOOTCTL_PATH}
rm -f /data/adb/post-fs-data.d/post_ota.sh
"###
);

std::fs::write(&post_ota_sh, sh_content)?;
#[cfg(unix)]
std::fs::set_permissions(post_ota_sh, std::fs::Permissions::from_mode(0o755))?;

Ok(())
}

0 comments on commit 53238da

Please sign in to comment.