Forum | Documentation | Website | Blog

Skip to content
Snippets Groups Projects
Verified Commit 17bdfd4d authored by Ayush Singh's avatar Ayush Singh
Browse files

bb-imager: Macos sd card flashing support


- Follow rpi-imager implementation as close as possible.
- Unmount disk before opening authentication

Changelog: fixed
Signed-off-by: Zain Siddavatam's avatarZain Siddavatam <beingzainsv@gmail.com>
Signed-off-by: Ayush Singh's avatarAyush Singh <ayush@beagleboard.org>
parent 2e4ceaf6
No related merge requests found
......@@ -478,8 +478,10 @@ dependencies = [
"fscommon",
"futures",
"hidapi",
"libc",
"liblzma",
"mbrman",
"nix 0.29.0",
"plist",
"reqwest",
"rs-drivelist",
......@@ -2613,9 +2615,9 @@ checksum = "03087c2bad5e1034e8cace5926dec053fb3790248370865f5117a7d0213354c8"
[[package]]
name = "libc"
version = "0.2.159"
version = "0.2.161"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "561d97a539a36e26a9a5fad1ea11a3039a67714694aaa379433e580854bc3dc5"
checksum = "8e9489c2807c139ffd9c1794f4af0ebe86a828db53ecdc7fea2111d0fed085d1"
[[package]]
name = "libfuzzer-sys"
......
......@@ -42,3 +42,5 @@ windows = { version = "0.58.0", features = ["Win32", "Win32_Storage", "Win32_Sto
security-framework = "3.0.0"
plist = "1.7.0"
anyhow = "1.0.91"
nix = { version = "0.29.0", features = ["socket", "uio"] }
libc = "0.2.161"
use std::{
io::Write,
os::fd::FromRawFd,
io::{IoSliceMut, Write},
os::{
fd::{AsRawFd, FromRawFd, OwnedFd, RawFd},
unix::net::UnixStream,
},
process::{Command, Stdio},
};
use nix::cmsg_space;
use nix::sys::socket::{ControlMessageOwned, MsgFlags};
use security_framework::authorization::{Authorization, AuthorizationItemSetBuilder, Flags};
use tokio::{
fs::File,
io::{AsyncReadExt, AsyncWriteExt},
};
use tokio::fs::File;
impl crate::common::Destination {
pub async fn open(&self) -> crate::error::Result<File> {
......@@ -37,12 +39,17 @@ fn open_auth(path: String) -> crate::error::Result<File> {
.unwrap();
let form = auth.make_external_form().unwrap();
let (pipe0, pipe1) = UnixStream::pair().unwrap();
let _ = Command::new("diskutil")
.args(["unmountDisk", &path])
.output()
.unwrap();
let mut cmd = Command::new("/usr/libexec/authopen")
.args(["-stdoutpipe", "-extauth", "-o", "2", &path])
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.stdout(OwnedFd::from(pipe1))
.spawn()
.unwrap();
......@@ -51,13 +58,40 @@ fn open_auth(path: String) -> crate::error::Result<File> {
stdin.write_all(&form_bytes).unwrap();
drop(stdin);
let output = cmd.wait_with_output().unwrap();
const IOV_BUF_SIZE: usize =
unsafe { libc::CMSG_SPACE(std::mem::size_of::<std::ffi::c_int>() as u32) } as usize;
let mut iov_buf = [0u8; IOV_BUF_SIZE];
let mut iov = [IoSliceMut::new(&mut iov_buf)];
let mut cmsg = cmsg_space!([RawFd; 1]);
match nix::sys::socket::recvmsg::<()>(
pipe0.as_raw_fd(),
&mut iov,
Some(&mut cmsg),
MsgFlags::empty(),
) {
Ok(result) => {
tracing::info!("Result: {:#?}", result);
for msg in result.cmsgs().unwrap() {
if let ControlMessageOwned::ScmRights(scm_rights) = msg {
for fd in scm_rights {
tracing::debug!("receive file descriptor: {fd}");
return Ok(unsafe { tokio::fs::File::from_raw_fd(fd) });
}
}
}
}
Err(e) => {
tracing::error!("Macos Error: {}", e);
}
}
tracing::info!("Raw output: {output:#?}");
tracing::info!("String output: {}", String::from_utf8_lossy(&output.stdout));
let status = cmd.wait().unwrap();
tracing::debug!("Exit Status: {:?}", status);
let fd = i32::from_ne_bytes(output.stdout.try_into().unwrap());
Ok(unsafe { tokio::fs::File::from_raw_fd(fd) })
panic!("fd not found");
}
/// TODO: Remove once a new version of rs_drivelist is published to crates.io
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment