mirror of
https://github.com/kvorschk/Magisk.git
synced 2026-12-05 23:14:03 -05:00
magiskboot: support building on host
Allow magiskboot to be built directly on host platforms (macOS and Linux) using Cargo: - Add a binary target to boot module with a no_main entry point that directly reuses the C main CLI implementation. - Compile native C and C++ sources (base, boot, cxx, and lz4) via cc in base and boot build.rs when target_os is not Android. - Platform gate Linux-specific functionality (SELinux xattrs, mount, namespaces) in base on Linux and Android, and provide macOS fallback for xsendfile. - Move MountInfo and mount parsing from files to mount module. - Scope Android-specific LTO rustflags in config.toml to Android targets. - Ensure build.py compiles Rust modules as static libraries (--lib). Assisted-by: Gemini 3.8 Flash
This commit is contained in:
+110
-57
@@ -2,7 +2,7 @@
|
||||
|
||||
use crate::cxx_extern::readlinkat;
|
||||
use crate::{Directory, LibcReturn, ResultExt, Utf8CStr, slice_from_ptr, slice_from_ptr_mut};
|
||||
use libc::{c_char, c_uint, c_ulong, c_void, dev_t, mode_t, off_t};
|
||||
use libc::{c_char, c_uint, c_void, dev_t, mode_t, off_t};
|
||||
use std::ffi::CStr;
|
||||
use std::fs::File;
|
||||
use std::io::{Read, Write};
|
||||
@@ -20,6 +20,70 @@ fn ptr_to_str<'a>(ptr: *const c_char) -> Option<&'a str> {
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(any(target_os = "linux", target_os = "android"))]
|
||||
mod linux {
|
||||
use crate::xwrap::ptr_to_str;
|
||||
use crate::{LibcReturn, ResultExt};
|
||||
use std::ffi::{c_char, c_void};
|
||||
use std::os::fd::RawFd;
|
||||
|
||||
#[unsafe(no_mangle)]
|
||||
extern "C" fn xsetns(fd: RawFd, nstype: i32) -> i32 {
|
||||
unsafe {
|
||||
libc::setns(fd, nstype)
|
||||
.into_os_result("setns", None, None)
|
||||
.log()
|
||||
.unwrap_or(-1)
|
||||
}
|
||||
}
|
||||
|
||||
#[unsafe(no_mangle)]
|
||||
extern "C" fn xunshare(flags: i32) -> i32 {
|
||||
unsafe {
|
||||
libc::unshare(flags)
|
||||
.into_os_result("unshare", None, None)
|
||||
.log()
|
||||
.unwrap_or(-1)
|
||||
}
|
||||
}
|
||||
|
||||
#[unsafe(no_mangle)]
|
||||
unsafe extern "C" fn xpipe2(fds: *mut i32, flags: i32) -> i32 {
|
||||
unsafe {
|
||||
libc::pipe2(fds, flags)
|
||||
.into_os_result("pipe2", None, None)
|
||||
.log()
|
||||
.unwrap_or(-1)
|
||||
}
|
||||
}
|
||||
|
||||
#[unsafe(no_mangle)]
|
||||
unsafe extern "C" fn xmount(
|
||||
src: *const c_char,
|
||||
target: *const c_char,
|
||||
fstype: *const c_char,
|
||||
flags: libc::c_ulong,
|
||||
data: *const c_void,
|
||||
) -> i32 {
|
||||
unsafe {
|
||||
libc::mount(src, target, fstype, flags, data)
|
||||
.into_os_result("mount", ptr_to_str(src), ptr_to_str(target))
|
||||
.log()
|
||||
.unwrap_or(-1)
|
||||
}
|
||||
}
|
||||
|
||||
#[unsafe(no_mangle)]
|
||||
unsafe extern "C" fn xumount2(target: *const c_char, flags: i32) -> i32 {
|
||||
unsafe {
|
||||
libc::umount2(target, flags)
|
||||
.into_os_result("umount2", ptr_to_str(target), None)
|
||||
.log()
|
||||
.unwrap_or(-1)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[unsafe(no_mangle)]
|
||||
unsafe extern "C" fn xreadlinkat(
|
||||
dirfd: RawFd,
|
||||
@@ -103,36 +167,6 @@ unsafe extern "C" fn xxread(fd: RawFd, buf: *mut u8, bufsz: usize) -> isize {
|
||||
.map_or(-1, |_| data.len() as isize)
|
||||
}
|
||||
|
||||
#[unsafe(no_mangle)]
|
||||
unsafe extern "C" fn xpipe2(fds: *mut i32, flags: i32) -> i32 {
|
||||
unsafe {
|
||||
libc::pipe2(fds, flags)
|
||||
.into_os_result("pipe2", None, None)
|
||||
.log()
|
||||
.unwrap_or(-1)
|
||||
}
|
||||
}
|
||||
|
||||
#[unsafe(no_mangle)]
|
||||
extern "C" fn xsetns(fd: RawFd, nstype: i32) -> i32 {
|
||||
unsafe {
|
||||
libc::setns(fd, nstype)
|
||||
.into_os_result("setns", None, None)
|
||||
.log()
|
||||
.unwrap_or(-1)
|
||||
}
|
||||
}
|
||||
|
||||
#[unsafe(no_mangle)]
|
||||
extern "C" fn xunshare(flags: i32) -> i32 {
|
||||
unsafe {
|
||||
libc::unshare(flags)
|
||||
.into_os_result("unshare", None, None)
|
||||
.log()
|
||||
.unwrap_or(-1)
|
||||
}
|
||||
}
|
||||
|
||||
#[unsafe(no_mangle)]
|
||||
unsafe extern "C" fn xopendir(path: *const c_char) -> *mut libc::DIR {
|
||||
unsafe {
|
||||
@@ -202,32 +236,6 @@ unsafe extern "C" fn xsymlink(target: *const c_char, linkpath: *const c_char) ->
|
||||
}
|
||||
}
|
||||
|
||||
#[unsafe(no_mangle)]
|
||||
unsafe extern "C" fn xmount(
|
||||
src: *const c_char,
|
||||
target: *const c_char,
|
||||
fstype: *const c_char,
|
||||
flags: c_ulong,
|
||||
data: *const c_void,
|
||||
) -> i32 {
|
||||
unsafe {
|
||||
libc::mount(src, target, fstype, flags, data)
|
||||
.into_os_result("mount", ptr_to_str(src), ptr_to_str(target))
|
||||
.log()
|
||||
.unwrap_or(-1)
|
||||
}
|
||||
}
|
||||
|
||||
#[unsafe(no_mangle)]
|
||||
unsafe extern "C" fn xumount2(target: *const c_char, flags: i32) -> i32 {
|
||||
unsafe {
|
||||
libc::umount2(target, flags)
|
||||
.into_os_result("umount2", ptr_to_str(target), None)
|
||||
.log()
|
||||
.unwrap_or(-1)
|
||||
}
|
||||
}
|
||||
|
||||
#[unsafe(no_mangle)]
|
||||
unsafe extern "C" fn xrename(oldname: *const c_char, newname: *const c_char) -> i32 {
|
||||
unsafe {
|
||||
@@ -265,12 +273,57 @@ unsafe extern "C" fn xsendfile(
|
||||
offset: *mut off_t,
|
||||
count: usize,
|
||||
) -> isize {
|
||||
#[cfg(any(target_os = "linux", target_os = "android"))]
|
||||
unsafe {
|
||||
libc::sendfile(out_fd, in_fd, offset, count)
|
||||
.into_os_result("sendfile", None, None)
|
||||
.log()
|
||||
.unwrap_or(-1)
|
||||
}
|
||||
#[cfg(not(any(target_os = "linux", target_os = "android")))]
|
||||
unsafe {
|
||||
let mut total = 0;
|
||||
let mut buf = [0u8; 65536];
|
||||
if !offset.is_null() {
|
||||
let cur = libc::lseek(in_fd, *offset, libc::SEEK_SET);
|
||||
if cur < 0 {
|
||||
return (-1isize)
|
||||
.into_os_result("lseek", None, None)
|
||||
.log()
|
||||
.unwrap_or(-1);
|
||||
}
|
||||
}
|
||||
while total < count {
|
||||
let to_read = std::cmp::min(buf.len(), count - total);
|
||||
let nread = libc::read(in_fd, buf.as_mut_ptr().cast(), to_read);
|
||||
if nread < 0 {
|
||||
return nread.into_os_result("read", None, None).log().unwrap_or(-1);
|
||||
}
|
||||
if nread == 0 {
|
||||
break;
|
||||
}
|
||||
let mut written = 0;
|
||||
while written < nread {
|
||||
let nwrite = libc::write(
|
||||
out_fd,
|
||||
buf.as_ptr().add(written as usize).cast(),
|
||||
(nread - written) as usize,
|
||||
);
|
||||
if nwrite < 0 {
|
||||
return nwrite
|
||||
.into_os_result("write", None, None)
|
||||
.log()
|
||||
.unwrap_or(-1);
|
||||
}
|
||||
written += nwrite;
|
||||
}
|
||||
total += nread as usize;
|
||||
}
|
||||
if !offset.is_null() {
|
||||
*offset += total as off_t;
|
||||
}
|
||||
total as isize
|
||||
}
|
||||
}
|
||||
|
||||
#[unsafe(no_mangle)]
|
||||
|
||||
Reference in New Issue
Block a user