Refine cpio and dtb subcommands

This commit is contained in:
LoveSy 2025-02-09 18:11:07 +08:00 committed by John Wu
parent f4204c3509
commit 3c068a017e
3 changed files with 90 additions and 110 deletions

View File

@ -1,5 +1,5 @@
use crate::cpio::{cpio_commands, print_cpio_usage}; use crate::cpio::{cpio_commands, print_cpio_usage};
use crate::dtb::{dtb_commands, print_dtb_usage}; use crate::dtb::{dtb_commands, print_dtb_usage, DtbAction};
use crate::ffi::{ use crate::ffi::{
cleanup, compress, decompress_raw, formats, repack, sign, split_image_dtb, unpack, verify cleanup, compress, decompress_raw, formats, repack, sign, split_image_dtb, unpack, verify
}; };
@ -100,6 +100,8 @@ struct HexPatch {
#[derive(FromArgs)] #[derive(FromArgs)]
#[argh(subcommand, name = "cpio")] #[argh(subcommand, name = "cpio")]
struct Cpio { struct Cpio {
#[argh(positional)]
file: String,
#[argh(positional)] #[argh(positional)]
cmds: Vec<String>, cmds: Vec<String>,
} }
@ -108,7 +110,9 @@ struct Cpio {
#[argh(subcommand, name = "dtb")] #[argh(subcommand, name = "dtb")]
struct Dtb { struct Dtb {
#[argh(positional)] #[argh(positional)]
cmds: Vec<String>, file: String,
#[argh(subcommand)]
action: DtbAction,
} }
#[derive(FromArgs)] #[derive(FromArgs)]
@ -353,15 +357,27 @@ pub unsafe extern "C" fn main(
Err(log_err!("Failed to patch"))?; Err(log_err!("Failed to patch"))?;
} }
} }
Action::Cpio(Cpio { ref cmds }) => { Action::Cpio(Cpio {
return if cpio_commands(&cmds.iter().map(|x| x.as_str()).collect::<Vec<_>>()) { ref mut file,
ref mut cmds,
}) => {
return if cpio_commands(file, cmds)
.log_with_msg(|w| w.write_str("Failed to process cpio"))
.is_ok()
{
0 0
} else { } else {
1 1
} }
} }
Action::Dtb(Dtb { ref cmds }) => { Action::Dtb(Dtb {
return if dtb_commands(&cmds.iter().map(|x| x.as_str()).collect::<Vec<_>>()) { ref mut file,
ref action,
}) => {
return if dtb_commands(file, action)
.log_with_msg(|w| w.write_str("Failed to process dtb"))
.unwrap_or(false)
{
0 0
} else { } else {
1 1

View File

@ -28,14 +28,6 @@ use crate::check_env;
use crate::ffi::{unxz, xz}; use crate::ffi::{unxz, xz};
use crate::patch::{patch_encryption, patch_verity}; use crate::patch::{patch_encryption, patch_verity};
#[derive(FromArgs)]
struct CpioCli {
#[argh(positional)]
file: String,
#[argh(positional)]
commands: Vec<String>,
}
#[derive(FromArgs)] #[derive(FromArgs)]
struct CpioCommand { struct CpioCommand {
#[argh(subcommand)] #[argh(subcommand)]
@ -753,23 +745,19 @@ impl Display for CpioEntry {
} }
} }
pub fn cpio_commands(cmds: &Vec<&str>) -> bool { pub(crate) fn cpio_commands(file: &mut String, cmds: &mut Vec<String>) -> LoggedResult<bool> {
let res: LoggedResult<()> = try { let file = Utf8CStr::from_string(file);
let mut cli =
CpioCli::from_args(&["magiskboot", "cpio"], cmds).on_early_exit(print_cpio_usage);
let file = Utf8CStr::from_string(&mut cli.file);
let mut cpio = if FsPath::from(file).exists() { let mut cpio = if FsPath::from(file).exists() {
Cpio::load_from_file(file)? Cpio::load_from_file(file)?
} else { } else {
Cpio::new() Cpio::new()
}; };
for cmd in cli.commands { for cmd in cmds {
if cmd.starts_with('#') { if cmd.starts_with('#') {
continue; continue;
} }
let mut cli = CpioCommand::from_args( let mut cmd = CpioCommand::from_args(
&["magiskboot", "cpio", file], &["magiskboot", "cpio", file],
cmd.split(' ') cmd.split(' ')
.filter(|x| !x.is_empty()) .filter(|x| !x.is_empty())
@ -778,16 +766,12 @@ pub fn cpio_commands(cmds: &Vec<&str>) -> bool {
) )
.on_early_exit(print_cpio_usage); .on_early_exit(print_cpio_usage);
match &mut cli.action { match &mut cmd.action {
CpioAction::Test(_) => exit(cpio.test()), CpioAction::Test(_) => exit(cpio.test()),
CpioAction::Restore(_) => cpio.restore()?, CpioAction::Restore(_) => cpio.restore()?,
CpioAction::Patch(_) => cpio.patch(), CpioAction::Patch(_) => cpio.patch(),
CpioAction::Exists(Exists { path }) => { CpioAction::Exists(Exists { path }) => {
if cpio.exists(path) { return Ok(cpio.exists(path));
exit(0);
} else {
exit(1);
}
} }
CpioAction::Backup(Backup { CpioAction::Backup(Backup {
origin, origin,
@ -807,14 +791,12 @@ pub fn cpio_commands(cmds: &Vec<&str>) -> bool {
} }
CpioAction::List(List { path, recursive }) => { CpioAction::List(List { path, recursive }) => {
cpio.ls(path.as_str(), *recursive); cpio.ls(path.as_str(), *recursive);
exit(0); return Ok(true);
} }
}; };
} }
cpio.dump(file)?; cpio.dump(file)?;
}; Ok(true)
res.log_with_msg(|w| w.write_str("Failed to process cpio"))
.is_ok()
} }
fn x8u(x: &[u8; 8]) -> LoggedResult<u32> { fn x8u(x: &[u8; 8]) -> LoggedResult<u32> {

View File

@ -1,4 +1,4 @@
use std::{cell::UnsafeCell, process::exit}; use std::cell::UnsafeCell;
use argh::FromArgs; use argh::FromArgs;
use fdt::{ use fdt::{
@ -6,21 +6,14 @@ use fdt::{
Fdt, FdtError, Fdt, FdtError,
}; };
use base::{EarlyExitExt, LoggedResult, MappedFile, ResultExt, Utf8CStr}; use base::{LoggedResult, MappedFile, Utf8CStr};
use crate::{check_env, patch::patch_verity}; use crate::{check_env, patch::patch_verity};
#[derive(FromArgs)]
struct DtbCli {
#[argh(positional)]
file: String,
#[argh(subcommand)]
action: DtbAction,
}
#[derive(FromArgs)] #[derive(FromArgs)]
#[argh(subcommand)] #[argh(subcommand)]
enum DtbAction { #[allow(private_interfaces)]
pub(crate) enum DtbAction {
Print(Print), Print(Print),
Patch(Patch), Patch(Patch),
Test(Test), Test(Test),
@ -272,29 +265,18 @@ fn dtb_patch(file: &Utf8CStr) -> LoggedResult<bool> {
Ok(patched) Ok(patched)
} }
pub fn dtb_commands(cmds: &Vec<&str>) -> bool { pub(crate) fn dtb_commands(file: &mut String, action: &DtbAction) -> LoggedResult<bool> {
let res: LoggedResult<()> = try { let file = Utf8CStr::from_string(file);
let mut cli = match action {
DtbCli::from_args(&["magiskboot", "dtb"], &cmds).on_early_exit(print_dtb_usage);
let file = Utf8CStr::from_string(&mut cli.file);
match cli.action {
DtbAction::Print(Print { fstab }) => { DtbAction::Print(Print { fstab }) => {
dtb_print(file, fstab)?; dtb_print(file, *fstab)?;
Ok(true)
} }
DtbAction::Test(_) => { DtbAction::Test(_) => {
if !dtb_test(file)? { Ok(dtb_test(file)?)
exit(1);
}
} }
DtbAction::Patch(_) => { DtbAction::Patch(_) => {
if !dtb_patch(file)? { Ok(dtb_patch(file)?)
exit(1);
} }
} }
}
};
res.log_with_msg(|w| w.write_str("Failed to process dtb"))
.is_ok()
} }