Skip to content

std: sys: net: uefi: tcp4: Implement write #141532

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions library/std/src/sys/net/connection/uefi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::time::Duration;
mod tcp;
pub(crate) mod tcp4;

pub struct TcpStream(#[expect(dead_code)] tcp::Tcp);
pub struct TcpStream(tcp::Tcp);

impl TcpStream {
pub fn connect(addr: io::Result<&SocketAddr>) -> io::Result<TcpStream> {
Expand Down Expand Up @@ -54,12 +54,13 @@ impl TcpStream {
false
}

pub fn write(&self, _: &[u8]) -> io::Result<usize> {
unsupported()
pub fn write(&self, buf: &[u8]) -> io::Result<usize> {
self.0.write(buf)
}

pub fn write_vectored(&self, _: &[IoSlice<'_>]) -> io::Result<usize> {
unsupported()
pub fn write_vectored(&self, buf: &[IoSlice<'_>]) -> io::Result<usize> {
// FIXME: UEFI does support vectored write, so implment that.
crate::io::default_write_vectored(|b| self.write(b), buf)
}

pub fn is_write_vectored(&self) -> bool {
Expand Down
8 changes: 7 additions & 1 deletion library/std/src/sys/net/connection/uefi/tcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::io;
use crate::net::SocketAddr;

pub(crate) enum Tcp {
V4(#[expect(dead_code)] tcp4::Tcp4),
V4(tcp4::Tcp4),
}

impl Tcp {
Expand All @@ -18,4 +18,10 @@ impl Tcp {
SocketAddr::V6(_) => todo!(),
}
}

pub(crate) fn write(&self, buf: &[u8]) -> io::Result<usize> {
match self {
Self::V4(client) => client.write(buf),
}
}
}
38 changes: 38 additions & 0 deletions library/std/src/sys/net/connection/uefi/tcp4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,44 @@ impl Tcp4 {
}
}

pub(crate) fn write(&self, buf: &[u8]) -> io::Result<usize> {
let evt = unsafe { self.create_evt() }?;
let completion_token =
tcp4::CompletionToken { event: evt.as_ptr(), status: Status::SUCCESS };
let data_len = crate::cmp::min(u32::MAX as u64, buf.len() as u64) as u32;

let fragment = tcp4::FragmentData {
fragment_length: data_len,
fragment_buffer: buf.as_ptr() as *mut _,
};
let mut tx_data = tcp4::TransmitData {
push: r_efi::efi::Boolean::FALSE,
urgent: r_efi::efi::Boolean::FALSE,
data_length: data_len,
fragment_count: 1,
fragment_table: [fragment],
};

let protocol = self.protocol.as_ptr();
let mut token = tcp4::IoToken {
completion_token,
packet: tcp4::IoTokenPacket { tx_data: &mut tx_data as *mut _ as *mut _ },
};

let r = unsafe { ((*protocol).transmit)(protocol, &mut token) };
if r.is_error() {
return Err(io::Error::from_raw_os_error(r.as_usize()));
}

self.wait_for_flag();

if completion_token.status.is_error() {
Err(io::Error::from_raw_os_error(completion_token.status.as_usize()))
} else {
Ok(data_len as usize)
}
}

unsafe fn create_evt(&self) -> io::Result<helpers::OwnedEvent> {
self.flag.store(false, Ordering::Relaxed);
helpers::OwnedEvent::new(
Expand Down
Loading