You've already forked Magisk
mirror of
https://github.com/topjohnwu/Magisk.git
synced 2025-09-06 06:36:58 +00:00
Cleanup magiskboot main function
This commit is contained in:
@@ -296,15 +296,11 @@ fn sign_cmd(
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
#[unsafe(no_mangle)]
|
fn boot_main(cmds: CmdArgs) -> LoggedResult<i32> {
|
||||||
pub extern "C" fn main(argc: i32, argv: *const *const c_char, _envp: *const *const c_char) -> i32 {
|
let mut cmds = cmds.0;
|
||||||
cmdline_logging();
|
if cmds.len() < 2 {
|
||||||
unsafe { umask(0) };
|
|
||||||
let res: LoggedResult<()> = try {
|
|
||||||
let mut cmds = CmdArgs::new(argc, argv).0;
|
|
||||||
if argc < 2 {
|
|
||||||
print_usage(cmds.first().unwrap_or(&"magiskboot"));
|
print_usage(cmds.first().unwrap_or(&"magiskboot"));
|
||||||
return 1;
|
return log_err!();
|
||||||
}
|
}
|
||||||
|
|
||||||
if cmds[1].starts_with("--") {
|
if cmds[1].starts_with("--") {
|
||||||
@@ -327,27 +323,31 @@ pub extern "C" fn main(argc: i32, argv: *const *const c_char, _envp: *const *con
|
|||||||
no_decompress,
|
no_decompress,
|
||||||
dump_header,
|
dump_header,
|
||||||
ref mut img,
|
ref mut img,
|
||||||
}) => return unpack(Utf8CStr::from_string(img), no_decompress, dump_header),
|
}) => {
|
||||||
|
return Ok(unpack(
|
||||||
|
Utf8CStr::from_string(img),
|
||||||
|
no_decompress,
|
||||||
|
dump_header,
|
||||||
|
));
|
||||||
|
}
|
||||||
Action::Repack(Repack {
|
Action::Repack(Repack {
|
||||||
no_compress,
|
no_compress,
|
||||||
ref mut img,
|
mut img,
|
||||||
ref mut out,
|
mut out,
|
||||||
}) => {
|
}) => {
|
||||||
repack(
|
repack(
|
||||||
Utf8CStr::from_string(img),
|
Utf8CStr::from_string(&mut img),
|
||||||
Utf8CStr::from_string(out),
|
Utf8CStr::from_string(&mut out),
|
||||||
no_compress,
|
no_compress,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
Action::Verify(Verify { mut img, mut cert }) => {
|
Action::Verify(Verify { mut img, mut cert }) => {
|
||||||
return if verify_cmd(
|
if !verify_cmd(
|
||||||
Utf8CStr::from_string(&mut img),
|
Utf8CStr::from_string(&mut img),
|
||||||
cert.as_mut().map(Utf8CStr::from_string),
|
cert.as_mut().map(Utf8CStr::from_string),
|
||||||
) {
|
) {
|
||||||
0
|
return log_err!();
|
||||||
} else {
|
}
|
||||||
1
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
Action::Sign(Sign { mut img, mut args }) => {
|
Action::Sign(Sign { mut img, mut args }) => {
|
||||||
let mut iter = args.iter_mut();
|
let mut iter = args.iter_mut();
|
||||||
@@ -358,65 +358,50 @@ pub extern "C" fn main(argc: i32, argv: *const *const c_char, _envp: *const *con
|
|||||||
iter.next().map(Utf8CStr::from_string),
|
iter.next().map(Utf8CStr::from_string),
|
||||||
)?;
|
)?;
|
||||||
}
|
}
|
||||||
Action::Extract(Extract {
|
Action::Extract(Extract { payload, args }) => {
|
||||||
ref payload,
|
|
||||||
ref args,
|
|
||||||
}) => {
|
|
||||||
if args.len() > 2 {
|
if args.len() > 2 {
|
||||||
log_err!("Too many arguments")?;
|
log_err!("Too many arguments")?;
|
||||||
}
|
}
|
||||||
extract_boot_from_payload(
|
extract_boot_from_payload(
|
||||||
payload,
|
&payload,
|
||||||
args.first().map(|x| x.as_str()),
|
args.first().map(|x| x.as_str()),
|
||||||
args.get(1).map(|x| x.as_str()),
|
args.get(1).map(|x| x.as_str()),
|
||||||
)
|
)
|
||||||
.log_with_msg(|w| w.write_str("Failed to extract from payload"))?;
|
.log_with_msg(|w| w.write_str("Failed to extract from payload"))?;
|
||||||
}
|
}
|
||||||
Action::HexPatch(HexPatch {
|
Action::HexPatch(HexPatch {
|
||||||
ref mut file,
|
mut file,
|
||||||
ref mut src,
|
mut src,
|
||||||
ref mut dest,
|
mut dest,
|
||||||
}) => {
|
}) => {
|
||||||
if !hexpatch(
|
if !hexpatch(
|
||||||
file,
|
&mut file,
|
||||||
Utf8CStr::from_string(src),
|
Utf8CStr::from_string(&mut src),
|
||||||
Utf8CStr::from_string(dest),
|
Utf8CStr::from_string(&mut dest),
|
||||||
) {
|
) {
|
||||||
log_err!("Failed to patch")?;
|
log_err!("Failed to patch")?;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Action::Cpio(Cpio {
|
Action::Cpio(Cpio { mut file, mut cmds }) => {
|
||||||
ref mut file,
|
cpio_commands(Utf8CStr::from_string(&mut file), &mut cmds)
|
||||||
ref mut cmds,
|
.log_with_msg(|w| w.write_str("Failed to process cpio"))?;
|
||||||
}) => {
|
|
||||||
return if cpio_commands(file, cmds)
|
|
||||||
.log_with_msg(|w| w.write_str("Failed to process cpio"))?
|
|
||||||
{
|
|
||||||
0
|
|
||||||
} else {
|
|
||||||
1
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
Action::Dtb(Dtb {
|
Action::Dtb(Dtb { mut file, action }) => {
|
||||||
ref mut file,
|
return dtb_commands(Utf8CStr::from_string(&mut file), &action)
|
||||||
ref action,
|
.map(|b| if b { 0 } else { 1 })
|
||||||
}) => {
|
.log_with_msg(|w| w.write_str("Failed to process dtb"));
|
||||||
return if dtb_commands(file, action)
|
|
||||||
.log_with_msg(|w| w.write_str("Failed to process dtb"))?
|
|
||||||
{
|
|
||||||
0
|
|
||||||
} else {
|
|
||||||
1
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
Action::Split(Split {
|
Action::Split(Split {
|
||||||
no_decompress,
|
no_decompress,
|
||||||
ref mut file,
|
mut file,
|
||||||
}) => {
|
}) => {
|
||||||
return split_image_dtb(Utf8CStr::from_string(file), no_decompress);
|
return Ok(split_image_dtb(
|
||||||
|
Utf8CStr::from_string(&mut file),
|
||||||
|
no_decompress,
|
||||||
|
));
|
||||||
}
|
}
|
||||||
Action::Sha1(Sha1 { ref mut file }) => {
|
Action::Sha1(Sha1 { mut file }) => {
|
||||||
let file = MappedFile::open(Utf8CStr::from_string(file))?;
|
let file = MappedFile::open(Utf8CStr::from_string(&mut file))?;
|
||||||
let mut sha1 = [0u8; 20];
|
let mut sha1 = [0u8; 20];
|
||||||
sha1_hash(file.as_ref(), &mut sha1);
|
sha1_hash(file.as_ref(), &mut sha1);
|
||||||
for byte in &sha1 {
|
for byte in &sha1 {
|
||||||
@@ -428,11 +413,8 @@ pub extern "C" fn main(argc: i32, argv: *const *const c_char, _envp: *const *con
|
|||||||
eprintln!("Cleaning up...");
|
eprintln!("Cleaning up...");
|
||||||
cleanup();
|
cleanup();
|
||||||
}
|
}
|
||||||
Action::Decompress(Decompress {
|
Action::Decompress(Decompress { mut file, mut out }) => {
|
||||||
ref mut file,
|
decompress(&mut file, out.as_mut())?;
|
||||||
ref mut out,
|
|
||||||
}) => {
|
|
||||||
decompress(file, out.as_mut())?;
|
|
||||||
}
|
}
|
||||||
Action::Compress(Compress {
|
Action::Compress(Compress {
|
||||||
ref mut file,
|
ref mut file,
|
||||||
@@ -446,6 +428,13 @@ pub extern "C" fn main(argc: i32, argv: *const *const c_char, _envp: *const *con
|
|||||||
)?;
|
)?;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
Ok(0)
|
||||||
if res.is_ok() { 0 } else { 1 }
|
}
|
||||||
|
|
||||||
|
#[unsafe(no_mangle)]
|
||||||
|
pub extern "C" fn main(argc: i32, argv: *const *const c_char, _envp: *const *const c_char) -> i32 {
|
||||||
|
cmdline_logging();
|
||||||
|
unsafe { umask(0) };
|
||||||
|
let cmds = CmdArgs::new(argc, argv);
|
||||||
|
boot_main(cmds).unwrap_or(1)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -753,8 +753,7 @@ impl Display for CpioEntry {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn cpio_commands(file: &mut String, cmds: &mut Vec<String>) -> LoggedResult<bool> {
|
pub(crate) fn cpio_commands(file: &Utf8CStr, cmds: &mut Vec<String>) -> LoggedResult<()> {
|
||||||
let file = Utf8CStr::from_string(file);
|
|
||||||
let mut cpio = if file.exists() {
|
let mut cpio = if file.exists() {
|
||||||
Cpio::load_from_file(file)?
|
Cpio::load_from_file(file)?
|
||||||
} else {
|
} else {
|
||||||
@@ -779,7 +778,11 @@ pub(crate) fn cpio_commands(file: &mut String, cmds: &mut Vec<String>) -> Logged
|
|||||||
CpioAction::Restore(_) => cpio.restore()?,
|
CpioAction::Restore(_) => cpio.restore()?,
|
||||||
CpioAction::Patch(_) => cpio.patch(),
|
CpioAction::Patch(_) => cpio.patch(),
|
||||||
CpioAction::Exists(Exists { path }) => {
|
CpioAction::Exists(Exists { path }) => {
|
||||||
return Ok(cpio.exists(path));
|
return if cpio.exists(path) {
|
||||||
|
Ok(())
|
||||||
|
} else {
|
||||||
|
log_err!()
|
||||||
|
};
|
||||||
}
|
}
|
||||||
CpioAction::Backup(Backup {
|
CpioAction::Backup(Backup {
|
||||||
origin,
|
origin,
|
||||||
@@ -799,12 +802,12 @@ pub(crate) fn cpio_commands(file: &mut String, cmds: &mut Vec<String>) -> Logged
|
|||||||
}
|
}
|
||||||
CpioAction::List(List { path, recursive }) => {
|
CpioAction::List(List { path, recursive }) => {
|
||||||
cpio.ls(path.as_str(), *recursive);
|
cpio.ls(path.as_str(), *recursive);
|
||||||
return Ok(true);
|
return Ok(());
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
cpio.dump(file)?;
|
cpio.dump(file)?;
|
||||||
Ok(true)
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn x8u(x: &[u8; 8]) -> LoggedResult<u32> {
|
fn x8u(x: &[u8; 8]) -> LoggedResult<u32> {
|
||||||
|
|||||||
@@ -264,8 +264,7 @@ fn dtb_patch(file: &Utf8CStr) -> LoggedResult<bool> {
|
|||||||
Ok(patched)
|
Ok(patched)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn dtb_commands(file: &mut String, action: &DtbAction) -> LoggedResult<bool> {
|
pub(crate) fn dtb_commands(file: &Utf8CStr, action: &DtbAction) -> LoggedResult<bool> {
|
||||||
let file = Utf8CStr::from_string(file);
|
|
||||||
match action {
|
match action {
|
||||||
DtbAction::Print(Print { fstab }) => {
|
DtbAction::Print(Print { fstab }) => {
|
||||||
dtb_print(file, *fstab)?;
|
dtb_print(file, *fstab)?;
|
||||||
|
|||||||
Reference in New Issue
Block a user