Migrate to rust 2024

This commit is contained in:
LoveSy 2025-03-08 00:21:22 +08:00 committed by John Wu
parent 3c068a017e
commit ae78b2e8b0

View File

@ -1,15 +1,15 @@
use crate::cpio::{cpio_commands, print_cpio_usage}; use crate::cpio::{cpio_commands, print_cpio_usage};
use crate::dtb::{dtb_commands, print_dtb_usage, DtbAction}; use crate::dtb::{DtbAction, dtb_commands, print_dtb_usage};
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,
}; };
use crate::patch::hexpatch; use crate::patch::hexpatch;
use crate::payload::extract_boot_from_payload; use crate::payload::extract_boot_from_payload;
use crate::sign::sha1_hash; use crate::sign::sha1_hash;
use argh::FromArgs; use argh::FromArgs;
use base::{ use base::{
cmdline_logging, libc::umask, log_err, map_args, raw_cstr, EarlyExitExt, LoggedResult, EarlyExitExt, LoggedResult, MappedFile, ResultExt, Utf8CStr, cmdline_logging, libc::umask,
MappedFile, ResultExt, Utf8CStr, log_err, map_args, raw_cstr,
}; };
use std::ffi::c_char; use std::ffi::c_char;
@ -259,14 +259,10 @@ Supported actions:
); );
} }
#[no_mangle] #[unsafe(no_mangle)]
pub unsafe extern "C" fn main( pub extern "C" fn main(argc: i32, argv: *const *const c_char, _envp: *const *const c_char) -> i32 {
argc: i32,
argv: *const *const c_char,
_envp: *const *const c_char,
) -> i32 {
cmdline_logging(); cmdline_logging();
umask(0); unsafe { umask(0) };
let res: LoggedResult<()> = try { let res: LoggedResult<()> = try {
let mut cmds = map_args(argc, argv)?; let mut cmds = map_args(argc, argv)?;
if argc < 2 { if argc < 2 {
@ -295,44 +291,61 @@ pub unsafe extern "C" fn main(
dump_header, dump_header,
ref mut img, ref mut img,
}) => { }) => {
return unpack( return unsafe {
unpack(
Utf8CStr::from_string(img).as_ptr(), Utf8CStr::from_string(img).as_ptr(),
no_decompress, no_decompress,
dump_header, dump_header,
); )
};
} }
Action::Repack(Repack { Action::Repack(Repack {
no_compress, no_compress,
ref mut img, ref mut img,
ref mut out, ref mut out,
}) => { }) => {
unsafe {
repack( repack(
Utf8CStr::from_string(img).as_ptr(), Utf8CStr::from_string(img).as_ptr(),
Utf8CStr::from_string(out).as_ptr(), Utf8CStr::from_string(out).as_ptr(),
no_compress, no_compress,
); )
};
} }
Action::Verify(Verify { Action::Verify(Verify {
ref mut img, ref mut img,
ref mut cert, ref mut cert,
}) => { }) => {
return verify(Utf8CStr::from_string(img).as_ptr(), cert.as_mut().map(|x| Utf8CStr::from_string(x).as_ptr()).unwrap_or(std::ptr::null())); return unsafe {
verify(
Utf8CStr::from_string(img).as_ptr(),
cert.as_mut()
.map(|x| Utf8CStr::from_string(x).as_ptr())
.unwrap_or(std::ptr::null()),
)
};
} }
Action::Sign(Sign { Action::Sign(Sign {
ref mut img, ref mut img,
ref mut args, ref mut args,
}) => { }) => {
let (pem, pk8) = match args.get_mut(1..=2) { let (pem, pk8) = match args.get_mut(1..=2) {
Some([pem,pk8]) => (Utf8CStr::from_string(pem).as_ptr(), Utf8CStr::from_string(pk8).as_ptr()), Some([pem, pk8]) => (
Utf8CStr::from_string(pem).as_ptr(),
Utf8CStr::from_string(pk8).as_ptr(),
),
_ => (std::ptr::null(), std::ptr::null()), _ => (std::ptr::null(), std::ptr::null()),
}; };
return sign( return unsafe {
sign(
Utf8CStr::from_string(img).as_ptr(), Utf8CStr::from_string(img).as_ptr(),
args.first_mut() args.first_mut()
.map(|x| Utf8CStr::from_string(x).as_ptr()) .map(|x| Utf8CStr::from_string(x).as_ptr())
.unwrap_or(raw_cstr!("/boot")), .unwrap_or(raw_cstr!("/boot")),
pem, pk8 pem,
pk8,
) )
};
} }
Action::Extract(Extract { Action::Extract(Extract {
ref payload, ref payload,
@ -353,7 +366,11 @@ pub unsafe extern "C" fn main(
ref mut src, ref mut src,
ref mut dest, ref mut dest,
}) => { }) => {
if !hexpatch(file, Utf8CStr::from_string(src), Utf8CStr::from_string(dest)) { if !hexpatch(
file,
Utf8CStr::from_string(src),
Utf8CStr::from_string(dest),
) {
Err(log_err!("Failed to patch"))?; Err(log_err!("Failed to patch"))?;
} }
} }
@ -368,7 +385,7 @@ pub unsafe extern "C" fn main(
0 0
} else { } else {
1 1
} };
} }
Action::Dtb(Dtb { Action::Dtb(Dtb {
ref mut file, ref mut file,
@ -381,13 +398,15 @@ pub unsafe extern "C" fn main(
0 0
} else { } else {
1 1
} };
} }
Action::Split(Split { Action::Split(Split {
no_decompress, no_decompress,
ref mut file, ref mut file,
}) => { }) => {
return split_image_dtb(Utf8CStr::from_string(file).as_ptr(), no_decompress); return unsafe {
split_image_dtb(Utf8CStr::from_string(file).as_ptr(), no_decompress)
};
} }
Action::Sha1(Sha1 { ref mut file }) => { Action::Sha1(Sha1 { ref mut file }) => {
let file = MappedFile::open(Utf8CStr::from_string(file))?; let file = MappedFile::open(Utf8CStr::from_string(file))?;
@ -406,31 +425,31 @@ pub unsafe extern "C" fn main(
ref mut file, ref mut file,
ref mut out, ref mut out,
}) => { }) => {
unsafe {
decompress_raw( decompress_raw(
Utf8CStr::from_string(file).as_mut_ptr(), Utf8CStr::from_string(file).as_mut_ptr(),
out.as_mut() out.as_mut()
.map(|x| Utf8CStr::from_string(x).as_ptr()) .map(|x| Utf8CStr::from_string(x).as_ptr())
.unwrap_or(std::ptr::null()), .unwrap_or(std::ptr::null()),
); )
};
} }
Action::Compress(Compress { Action::Compress(Compress {
ref mut file, ref mut file,
ref mut format, ref mut format,
ref mut out, ref mut out,
}) => { }) => {
unsafe {
compress( compress(
Utf8CStr::from_string(format).as_ptr(), Utf8CStr::from_string(format).as_ptr(),
Utf8CStr::from_string(file).as_ptr(), Utf8CStr::from_string(file).as_ptr(),
out.as_mut() out.as_mut()
.map(|x| Utf8CStr::from_string(x).as_ptr()) .map(|x| Utf8CStr::from_string(x).as_ptr())
.unwrap_or(std::ptr::null()), .unwrap_or(std::ptr::null()),
); )
};
} }
} }
}; };
if res.is_ok() { if res.is_ok() { 0 } else { 1 }
0
} else {
1
}
} }