ksud: fmt
This commit is contained in:
@@ -168,7 +168,7 @@ fn do_cpio_cmd(magiskboot: &Path, workdir: &Path, cmd: &str) -> Result<()> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn do_vendor_init_boot_cpio_cmd(magiskboot: &Path, workdir: &Path, cmd: &str) -> Result<()> {
|
fn do_vendor_init_boot_cpio_cmd(magiskboot: &Path, workdir: &Path, cmd: &str) -> Result<()> {
|
||||||
let vendor_init_boot_cpio = workdir.join("vendor_ramdisk").join("init_boot.cpio");
|
let vendor_init_boot_cpio = workdir.join("vendor_ramdisk").join("init_boot.cpio");
|
||||||
let status = Command::new(magiskboot)
|
let status = Command::new(magiskboot)
|
||||||
.current_dir(workdir)
|
.current_dir(workdir)
|
||||||
.stdout(Stdio::null())
|
.stdout(Stdio::null())
|
||||||
@@ -200,11 +200,7 @@ fn is_magisk_patched_vendor_init_boot(magiskboot: &Path, workdir: &Path) -> Resu
|
|||||||
.current_dir(workdir)
|
.current_dir(workdir)
|
||||||
.stdout(Stdio::null())
|
.stdout(Stdio::null())
|
||||||
.stderr(Stdio::null())
|
.stderr(Stdio::null())
|
||||||
.args([
|
.args(["cpio", vendor_init_boot_cpio.to_str().unwrap(), "test"])
|
||||||
"cpio",
|
|
||||||
vendor_init_boot_cpio.to_str().unwrap(),
|
|
||||||
"test",
|
|
||||||
])
|
|
||||||
.status()?;
|
.status()?;
|
||||||
|
|
||||||
// 0: stock, 1: magisk
|
// 0: stock, 1: magisk
|
||||||
@@ -284,8 +280,12 @@ pub fn restore(
|
|||||||
|
|
||||||
let no_ramdisk = !workdir.join("ramdisk.cpio").exists();
|
let no_ramdisk = !workdir.join("ramdisk.cpio").exists();
|
||||||
let is_kernelsu_patched = is_kernelsu_patched(&magiskboot, workdir)?;
|
let is_kernelsu_patched = is_kernelsu_patched(&magiskboot, workdir)?;
|
||||||
let is_kernelsu_patched_vendor_init_boot = is_kernelsu_patched_vendor_init_boot(&magiskboot, workdir)?;
|
let is_kernelsu_patched_vendor_init_boot =
|
||||||
ensure!(is_kernelsu_patched || is_kernelsu_patched_vendor_init_boot, "boot image is not patched by KernelSU");
|
is_kernelsu_patched_vendor_init_boot(&magiskboot, workdir)?;
|
||||||
|
ensure!(
|
||||||
|
is_kernelsu_patched || is_kernelsu_patched_vendor_init_boot,
|
||||||
|
"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;
|
||||||
@@ -321,12 +321,13 @@ pub fn restore(
|
|||||||
// vendor init_boot restore
|
// vendor init_boot restore
|
||||||
do_vendor_init_boot_cpio_cmd(&magiskboot, workdir, "rm kernelsu.ko")?;
|
do_vendor_init_boot_cpio_cmd(&magiskboot, workdir, "rm kernelsu.ko")?;
|
||||||
// if init.real exists, restore it
|
// if init.real exists, restore it
|
||||||
let status = do_vendor_init_boot_cpio_cmd(&magiskboot, workdir, "exists init.real").is_ok();
|
let status =
|
||||||
|
do_vendor_init_boot_cpio_cmd(&magiskboot, workdir, "exists init.real").is_ok();
|
||||||
if status {
|
if status {
|
||||||
do_vendor_init_boot_cpio_cmd(&magiskboot, workdir, "mv init.real init")?;
|
do_vendor_init_boot_cpio_cmd(&magiskboot, workdir, "mv init.real init")?;
|
||||||
} else {
|
} else {
|
||||||
let vendor_init_boot = workdir.join("vendor_ramdisk").join("init_boot.cpio");
|
let vendor_init_boot = workdir.join("vendor_ramdisk").join("init_boot.cpio");
|
||||||
std::fs::remove_file(vendor_init_boot )?;
|
std::fs::remove_file(vendor_init_boot)?;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// remove kernelsu.ko
|
// remove kernelsu.ko
|
||||||
@@ -524,7 +525,8 @@ fn do_patch(
|
|||||||
bail!("No compatible ramdisk found.");
|
bail!("No compatible ramdisk found.");
|
||||||
}
|
}
|
||||||
let is_magisk_patched = is_magisk_patched(&magiskboot, workdir)?;
|
let is_magisk_patched = is_magisk_patched(&magiskboot, workdir)?;
|
||||||
let is_magisk_patched_vendor_init_boot = is_magisk_patched_vendor_init_boot(&magiskboot, workdir)?;
|
let is_magisk_patched_vendor_init_boot =
|
||||||
|
is_magisk_patched_vendor_init_boot(&magiskboot, workdir)?;
|
||||||
ensure!(
|
ensure!(
|
||||||
!is_magisk_patched || !is_magisk_patched_vendor_init_boot,
|
!is_magisk_patched || !is_magisk_patched_vendor_init_boot,
|
||||||
"Cannot work with Magisk patched image"
|
"Cannot work with Magisk patched image"
|
||||||
@@ -532,7 +534,8 @@ fn do_patch(
|
|||||||
|
|
||||||
println!("- Adding KernelSU LKM");
|
println!("- Adding KernelSU LKM");
|
||||||
let is_kernelsu_patched = is_kernelsu_patched(&magiskboot, workdir)?;
|
let is_kernelsu_patched = is_kernelsu_patched(&magiskboot, workdir)?;
|
||||||
let is_kernelsu_patched_vendor_init_boot = is_kernelsu_patched_vendor_init_boot(&magiskboot, workdir)?;
|
let is_kernelsu_patched_vendor_init_boot =
|
||||||
|
is_kernelsu_patched_vendor_init_boot(&magiskboot, workdir)?;
|
||||||
|
|
||||||
let mut need_backup = false;
|
let mut need_backup = false;
|
||||||
if !is_kernelsu_patched || (no_ramdisk && !is_kernelsu_patched_vendor_init_boot) {
|
if !is_kernelsu_patched || (no_ramdisk && !is_kernelsu_patched_vendor_init_boot) {
|
||||||
@@ -740,7 +743,6 @@ fn find_boot_image(
|
|||||||
Path::new(&format!("/dev/block/by-name/init_boot{slot_suffix}")).exists();
|
Path::new(&format!("/dev/block/by-name/init_boot{slot_suffix}")).exists();
|
||||||
let vendor_boot_exist =
|
let vendor_boot_exist =
|
||||||
Path::new(&format!("/dev/block/by-name/vendor_boot{slot_suffix}")).exists();
|
Path::new(&format!("/dev/block/by-name/vendor_boot{slot_suffix}")).exists();
|
||||||
|
|
||||||
let boot_partition = if !is_replace_kernel && init_boot_exist && !skip_init {
|
let boot_partition = if !is_replace_kernel && init_boot_exist && !skip_init {
|
||||||
format!("/dev/block/by-name/init_boot{slot_suffix}")
|
format!("/dev/block/by-name/init_boot{slot_suffix}")
|
||||||
} else if !is_replace_kernel && vendor_boot_exist && !skip_init {
|
} else if !is_replace_kernel && vendor_boot_exist && !skip_init {
|
||||||
|
|||||||
@@ -1,11 +1,11 @@
|
|||||||
use crate::defs::{KSU_MOUNT_SOURCE, NO_MOUNT_PATH, NO_TMPFS_PATH};
|
use crate::defs::{KSU_MOUNT_SOURCE, NO_MOUNT_PATH, NO_TMPFS_PATH};
|
||||||
|
use crate::kpm;
|
||||||
use crate::module::{handle_updated_modules, prune_modules};
|
use crate::module::{handle_updated_modules, prune_modules};
|
||||||
use crate::{assets, defs, ksucalls, restorecon, utils};
|
use crate::{assets, defs, ksucalls, restorecon, utils};
|
||||||
use anyhow::{Context, Result};
|
use anyhow::{Context, Result};
|
||||||
use log::{info, warn};
|
use log::{info, warn};
|
||||||
use rustix::fs::{MountFlags, mount};
|
use rustix::fs::{MountFlags, mount};
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
use crate::kpm;
|
|
||||||
|
|
||||||
pub fn on_post_data_fs() -> Result<()> {
|
pub fn on_post_data_fs() -> Result<()> {
|
||||||
ksucalls::report_post_fs_data();
|
ksucalls::report_post_fs_data();
|
||||||
@@ -72,7 +72,13 @@ pub fn on_post_data_fs() -> Result<()> {
|
|||||||
|
|
||||||
// mount temp dir
|
// mount temp dir
|
||||||
if !Path::new(NO_TMPFS_PATH).exists() {
|
if !Path::new(NO_TMPFS_PATH).exists() {
|
||||||
if let Err(e) = mount(KSU_MOUNT_SOURCE, utils::get_tmp_path(), "tmpfs", MountFlags::empty(), "") {
|
if let Err(e) = mount(
|
||||||
|
KSU_MOUNT_SOURCE,
|
||||||
|
utils::get_tmp_path(),
|
||||||
|
"tmpfs",
|
||||||
|
MountFlags::empty(),
|
||||||
|
"",
|
||||||
|
) {
|
||||||
warn!("do temp dir mount failed: {}", e);
|
warn!("do temp dir mount failed: {}", e);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -1,9 +1,9 @@
|
|||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use notify::{Watcher, RecursiveMode};
|
|
||||||
use std::path::Path;
|
|
||||||
use std::fs;
|
|
||||||
use anyhow::anyhow;
|
use anyhow::anyhow;
|
||||||
|
use notify::{RecursiveMode, Watcher};
|
||||||
use std::ffi::OsStr;
|
use std::ffi::OsStr;
|
||||||
|
use std::fs;
|
||||||
|
use std::path::Path;
|
||||||
|
|
||||||
pub const KPM_DIR: &str = "/data/adb/kpm";
|
pub const KPM_DIR: &str = "/data/adb/kpm";
|
||||||
pub const KPMMGR_PATH: &str = "/data/adb/ksu/bin/kpmmgr";
|
pub const KPMMGR_PATH: &str = "/data/adb/ksu/bin/kpmmgr";
|
||||||
@@ -28,11 +28,9 @@ pub fn start_kpm_watcher() -> Result<()> {
|
|||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut watcher = notify::recommended_watcher(|res| {
|
let mut watcher = notify::recommended_watcher(|res| match res {
|
||||||
match res {
|
Ok(event) => handle_kpm_event(event),
|
||||||
Ok(event) => handle_kpm_event(event),
|
Err(e) => log::error!("monitoring error: {:?}", e),
|
||||||
Err(e) => log::error!("monitoring error: {:?}", e),
|
|
||||||
}
|
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
watcher.watch(Path::new(KPM_DIR), RecursiveMode::NonRecursive)?;
|
watcher.watch(Path::new(KPM_DIR), RecursiveMode::NonRecursive)?;
|
||||||
@@ -80,7 +78,9 @@ fn handle_modify_event(paths: Vec<std::path::PathBuf>) {
|
|||||||
|
|
||||||
// 加载 KPM 模块
|
// 加载 KPM 模块
|
||||||
pub fn load_kpm(path: &Path) -> Result<()> {
|
pub fn load_kpm(path: &Path) -> Result<()> {
|
||||||
let path_str = path.to_str().ok_or_else(|| anyhow!("Invalid path: {}", path.display()))?;
|
let path_str = path
|
||||||
|
.to_str()
|
||||||
|
.ok_or_else(|| anyhow!("Invalid path: {}", path.display()))?;
|
||||||
let status = std::process::Command::new(KPMMGR_PATH)
|
let status = std::process::Command::new(KPMMGR_PATH)
|
||||||
.args(["load", path_str, ""])
|
.args(["load", path_str, ""])
|
||||||
.status()?;
|
.status()?;
|
||||||
@@ -168,7 +168,6 @@ pub fn load_kpm_modules() -> Result<()> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if path.extension().is_some_and(|ext| ext == "kpm") {
|
if path.extension().is_some_and(|ext| ext == "kpm") {
|
||||||
match load_kpm(&path) {
|
match load_kpm(&path) {
|
||||||
Ok(()) => log::info!("Successfully loaded KPM module: {}", path.display()),
|
Ok(()) => log::info!("Successfully loaded KPM module: {}", path.display()),
|
||||||
@@ -178,4 +177,4 @@ pub fn load_kpm_modules() -> Result<()> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,4 @@
|
|||||||
use crate::defs::{
|
use crate::defs::{DISABLE_FILE_NAME, KSU_MOUNT_SOURCE, MODULE_DIR, SKIP_MOUNT_FILE_NAME};
|
||||||
DISABLE_FILE_NAME, KSU_MOUNT_SOURCE, MODULE_DIR, SKIP_MOUNT_FILE_NAME,
|
|
||||||
};
|
|
||||||
use crate::magic_mount::NodeFileType::{Directory, RegularFile, Symlink, Whiteout};
|
use crate::magic_mount::NodeFileType::{Directory, RegularFile, Symlink, Whiteout};
|
||||||
use crate::restorecon::{lgetfilecon, lsetfilecon};
|
use crate::restorecon::{lgetfilecon, lsetfilecon};
|
||||||
use crate::utils::{ensure_dir_exists, get_work_dir};
|
use crate::utils::{ensure_dir_exists, get_work_dir};
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ mod cli;
|
|||||||
mod debug;
|
mod debug;
|
||||||
mod defs;
|
mod defs;
|
||||||
mod init_event;
|
mod init_event;
|
||||||
|
mod kpm;
|
||||||
mod ksucalls;
|
mod ksucalls;
|
||||||
#[cfg(target_os = "android")]
|
#[cfg(target_os = "android")]
|
||||||
mod magic_mount;
|
mod magic_mount;
|
||||||
@@ -14,7 +15,6 @@ mod restorecon;
|
|||||||
mod sepolicy;
|
mod sepolicy;
|
||||||
mod su;
|
mod su;
|
||||||
mod utils;
|
mod utils;
|
||||||
mod kpm;
|
|
||||||
|
|
||||||
fn main() -> anyhow::Result<()> {
|
fn main() -> anyhow::Result<()> {
|
||||||
cli::run()
|
cli::run()
|
||||||
|
|||||||
@@ -187,13 +187,7 @@ fn is_ok_empty(dir: &str) -> bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_tmp_path() -> String {
|
pub fn get_tmp_path() -> String {
|
||||||
let dirs = [
|
let dirs = ["/debug_ramdisk", "/patch_hw", "/oem", "/root", "/sbin"];
|
||||||
"/debug_ramdisk",
|
|
||||||
"/patch_hw",
|
|
||||||
"/oem",
|
|
||||||
"/root",
|
|
||||||
"/sbin",
|
|
||||||
];
|
|
||||||
|
|
||||||
// find empty directory
|
// find empty directory
|
||||||
for dir in dirs {
|
for dir in dirs {
|
||||||
@@ -205,8 +199,8 @@ pub fn get_tmp_path() -> String {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_work_dir() -> String {
|
pub fn get_work_dir() -> String {
|
||||||
let tmp_path = get_tmp_path();
|
let tmp_path = get_tmp_path();
|
||||||
format!("{}/workdir/", tmp_path)
|
format!("{}/workdir/", tmp_path)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(target_os = "android")]
|
#[cfg(target_os = "android")]
|
||||||
|
|||||||
Reference in New Issue
Block a user