handle subcommand help

This commit is contained in:
LoveSy 2025-02-06 19:57:11 +08:00 committed by John Wu
parent 8ae7641e8a
commit f4204c3509
3 changed files with 52 additions and 31 deletions

View File

@ -1,12 +1,17 @@
use crate::payload::extract_boot_from_payload;
use crate::sign::{sha1_hash, verify_boot_image};
use argh::FromArgs;
use base::{cmdline_logging, libc::umask, log_err, map_args, raw_cstr, EarlyExitExt, LoggedResult, MappedFile, ResultExt, Utf8CStr};
use std::ffi::c_char;
use crate::cpio::cpio_commands;
use crate::dtb::dtb_commands;
use crate::ffi::{cleanup, compress, decompress_raw, formats, repack, sign, split_image_dtb, unpack, verify};
use crate::cpio::{cpio_commands, print_cpio_usage};
use crate::dtb::{dtb_commands, print_dtb_usage};
use crate::ffi::{
cleanup, compress, decompress_raw, formats, repack, sign, split_image_dtb, unpack, verify
};
use crate::patch::hexpatch;
use crate::payload::extract_boot_from_payload;
use crate::sign::sha1_hash;
use argh::FromArgs;
use base::{
cmdline_logging, libc::umask, log_err, map_args, raw_cstr, EarlyExitExt, LoggedResult,
MappedFile, ResultExt, Utf8CStr,
};
use std::ffi::c_char;
#[derive(FromArgs)]
struct Cli {
@ -245,7 +250,8 @@ Supported actions:
{1}
"#,
cmd, formats()
cmd,
formats()
);
}
@ -274,14 +280,22 @@ pub unsafe extern "C" fn main(
cmds[3] = fmt;
}
let mut cli = Cli::from_args(&[cmds[0]], &cmds[1..]).on_early_exit(|| print_usage(cmds[0]));
let mut cli = Cli::from_args(&[cmds[0]], &cmds[1..]).on_early_exit(|| match cmds.get(1) {
Some(&"dtb") => print_dtb_usage(),
Some(&"cpio") => print_cpio_usage(),
_ => print_usage(cmds[0]),
});
match cli.action {
Action::Unpack(Unpack {
no_decompress,
dump_header,
ref mut img,
}) => {
return unpack(Utf8CStr::from_string(img).as_ptr(), no_decompress, dump_header);
return unpack(
Utf8CStr::from_string(img).as_ptr(),
no_decompress,
dump_header,
);
}
Action::Repack(Repack {
no_compress,
@ -310,7 +324,9 @@ pub unsafe extern "C" fn main(
};
return sign(
Utf8CStr::from_string(img).as_ptr(),
args.first_mut().map(|x| Utf8CStr::from_string(x).as_ptr()).unwrap_or(raw_cstr!("/boot")),
args.first_mut()
.map(|x| Utf8CStr::from_string(x).as_ptr())
.unwrap_or(raw_cstr!("/boot")),
pem, pk8
)
}
@ -337,18 +353,14 @@ pub unsafe extern "C" fn main(
Err(log_err!("Failed to patch"))?;
}
}
Action::Cpio(Cpio {
ref cmds,
}) => {
Action::Cpio(Cpio { ref cmds }) => {
return if cpio_commands(&cmds.iter().map(|x| x.as_str()).collect::<Vec<_>>()) {
0
} else {
1
}
}
Action::Dtb(Dtb {
ref cmds,
}) => {
Action::Dtb(Dtb { ref cmds }) => {
return if dtb_commands(&cmds.iter().map(|x| x.as_str()).collect::<Vec<_>>()) {
0
} else {
@ -378,14 +390,25 @@ pub unsafe extern "C" fn main(
ref mut file,
ref mut out,
}) => {
decompress_raw(Utf8CStr::from_string(file).as_mut_ptr(), out.as_mut().map(|x| Utf8CStr::from_string(x).as_ptr()).unwrap_or(std::ptr::null()));
decompress_raw(
Utf8CStr::from_string(file).as_mut_ptr(),
out.as_mut()
.map(|x| Utf8CStr::from_string(x).as_ptr())
.unwrap_or(std::ptr::null()),
);
}
Action::Compress(Compress {
ref mut file,
ref mut format,
ref mut out,
}) => {
compress(Utf8CStr::from_string(format).as_ptr(), Utf8CStr::from_string(file).as_ptr(), out.as_mut().map(|x| Utf8CStr::from_string(x).as_ptr()).unwrap_or(std::ptr::null()));
compress(
Utf8CStr::from_string(format).as_ptr(),
Utf8CStr::from_string(file).as_ptr(),
out.as_mut()
.map(|x| Utf8CStr::from_string(x).as_ptr())
.unwrap_or(std::ptr::null()),
);
}
}
};

View File

@ -15,13 +15,13 @@ use num_traits::cast::AsPrimitive;
use size::{Base, Size, Style};
use base::libc::{
dev_t, gid_t, major, makedev, minor, mknod, mode_t, uid_t, O_CLOEXEC, O_CREAT,
O_RDONLY, O_TRUNC, O_WRONLY, S_IFBLK, S_IFCHR, S_IFDIR, S_IFLNK, S_IFMT, S_IFREG, S_IRGRP,
S_IROTH, S_IRUSR, S_IWGRP, S_IWOTH, S_IWUSR, S_IXGRP, S_IXOTH, S_IXUSR,
dev_t, gid_t, major, makedev, minor, mknod, mode_t, uid_t, O_CLOEXEC, O_CREAT, O_RDONLY,
O_TRUNC, O_WRONLY, S_IFBLK, S_IFCHR, S_IFDIR, S_IFLNK, S_IFMT, S_IFREG, S_IRGRP, S_IROTH,
S_IRUSR, S_IWGRP, S_IWOTH, S_IWUSR, S_IXGRP, S_IXOTH, S_IXUSR,
};
use base::{
cstr_buf, log_err, BytesExt, EarlyExitExt, FsPath, LoggedResult, MappedFile,
ResultExt, Utf8CStr, Utf8CStrBuf, WriteExt,
cstr_buf, log_err, BytesExt, EarlyExitExt, FsPath, LoggedResult, MappedFile, ResultExt, Utf8CStr,
Utf8CStrBuf, WriteExt,
};
use crate::check_env;
@ -150,7 +150,7 @@ struct List {
recursive: bool,
}
fn print_cpio_usage() {
pub(crate) fn print_cpio_usage() {
eprintln!(
r#"Usage: magiskboot cpio <incpio> [commands...]
@ -756,7 +756,7 @@ impl Display for CpioEntry {
pub fn cpio_commands(cmds: &Vec<&str>) -> bool {
let res: LoggedResult<()> = try {
let mut cli =
CpioCli::from_args(&["magiskboot", "cpio"], &cmds).on_early_exit(print_cpio_usage);
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() {

View File

@ -6,9 +6,7 @@ use fdt::{
Fdt, FdtError,
};
use base::{
EarlyExitExt, LoggedResult, MappedFile, ResultExt, Utf8CStr,
};
use base::{EarlyExitExt, LoggedResult, MappedFile, ResultExt, Utf8CStr};
use crate::{check_env, patch::patch_verity};
@ -43,7 +41,7 @@ struct Patch {}
#[argh(subcommand, name = "test")]
struct Test {}
fn print_dtb_usage() {
pub(crate) fn print_dtb_usage() {
eprintln!(
r#"Usage: magiskboot dtb <file> <action> [args...]
Do dtb related actions to <file>.