From ca0315a1d35c34a3903df882c0e5e1d8c3433adf Mon Sep 17 00:00:00 2001 From: Nicholas Bishop Date: Mon, 17 Feb 2025 13:59:23 -0500 Subject: [PATCH 01/11] uefi: Use uefi_raw::Boolean in pxe::BaseCode This is an intermediate step towards using the uefi-raw definition of the protocol. --- uefi/src/proto/network/pxe.rs | 96 +++++++++++++++++++---------------- 1 file changed, 53 insertions(+), 43 deletions(-) diff --git a/uefi/src/proto/network/pxe.rs b/uefi/src/proto/network/pxe.rs index 1b42a5505..0b4a23594 100644 --- a/uefi/src/proto/network/pxe.rs +++ b/uefi/src/proto/network/pxe.rs @@ -12,9 +12,10 @@ use core::ffi::c_void; use core::fmt::{self, Debug, Display, Formatter}; use core::iter::from_fn; use core::mem::MaybeUninit; -use core::ptr::{null, null_mut}; +use core::ptr::{self, null, null_mut}; use ptr_meta::Pointee; use uefi_raw::protocol::network::pxe::PxeBaseCodeTftpOpcode; +use uefi_raw::Boolean; pub use uefi_raw::protocol::network::pxe::{ PxeBaseCodeBootType as BootstrapType, PxeBaseCodeIpFilterFlags as IpFilters, @@ -28,27 +29,27 @@ pub use uefi_raw::protocol::network::pxe::{ #[allow(clippy::type_complexity)] pub struct BaseCode { revision: u64, - start: unsafe extern "efiapi" fn(this: &Self, use_ipv6: bool) -> Status, + start: unsafe extern "efiapi" fn(this: &Self, use_ipv6: Boolean) -> Status, stop: unsafe extern "efiapi" fn(this: &Self) -> Status, - dhcp: unsafe extern "efiapi" fn(this: &Self, sort_offers: bool) -> Status, + dhcp: unsafe extern "efiapi" fn(this: &Self, sort_offers: Boolean) -> Status, discover: unsafe extern "efiapi" fn( this: &Self, ty: BootstrapType, layer: &mut u16, - use_bis: bool, + use_bis: Boolean, info: *const FfiDiscoverInfo, ) -> Status, mtftp: unsafe extern "efiapi" fn( this: &Self, operation: PxeBaseCodeTftpOpcode, buffer: *mut c_void, - overwrite: bool, + overwrite: Boolean, buffer_size: &mut u64, block_size: Option<&usize>, server_ip: &IpAddress, filename: *const Char8, info: Option<&MtftpInfo>, - dont_use_buffer: bool, + dont_use_buffer: Boolean, ) -> Status, udp_write: unsafe extern "efiapi" fn( this: &Self, @@ -83,11 +84,11 @@ pub struct BaseCode { ) -> Status, set_parameters: unsafe extern "efiapi" fn( this: &Self, - new_auto_arp: Option<&bool>, - new_send_guid: Option<&bool>, + new_auto_arp: *const Boolean, + new_send_guid: *const Boolean, new_ttl: Option<&u8>, new_tos: Option<&u8>, - new_make_callback: Option<&bool>, + new_make_callback: *const Boolean, ) -> Status, set_station_ip: unsafe extern "efiapi" fn( this: &Self, @@ -96,12 +97,12 @@ pub struct BaseCode { ) -> Status, set_packets: unsafe extern "efiapi" fn( this: &Self, - new_dhcp_discover_valid: Option<&bool>, - new_dhcp_ack_received: Option<&bool>, - new_proxy_offer_received: Option<&bool>, - new_pxe_discover_valid: Option<&bool>, - new_pxe_reply_received: Option<&bool>, - new_pxe_bis_reply_received: Option<&bool>, + new_dhcp_discover_valid: *const Boolean, + new_dhcp_ack_received: *const Boolean, + new_proxy_offer_received: *const Boolean, + new_pxe_discover_valid: *const Boolean, + new_pxe_reply_received: *const Boolean, + new_pxe_bis_reply_received: *const Boolean, new_dhcp_discover: Option<&Packet>, new_dhcp_ack: Option<&Packet>, new_proxy_offer: Option<&Packet>, @@ -115,7 +116,7 @@ pub struct BaseCode { impl BaseCode { /// Enables the use of the PXE Base Code Protocol functions. pub fn start(&mut self, use_ipv6: bool) -> Result { - unsafe { (self.start)(self, use_ipv6) }.to_result() + unsafe { (self.start)(self, use_ipv6.into()) }.to_result() } /// Disables the use of the PXE Base Code Protocol functions. @@ -126,7 +127,7 @@ impl BaseCode { /// Attempts to complete a DHCPv4 D.O.R.A. (discover / offer / request / /// acknowledge) or DHCPv6 S.A.R.R (solicit / advertise / request / reply) sequence. pub fn dhcp(&mut self, sort_offers: bool) -> Result { - unsafe { (self.dhcp)(self, sort_offers) }.to_result() + unsafe { (self.dhcp)(self, sort_offers.into()) }.to_result() } /// Attempts to complete the PXE Boot Server and/or boot image discovery @@ -145,7 +146,7 @@ impl BaseCode { }) .unwrap_or(null()); - unsafe { (self.discover)(self, ty, layer, use_bis, info) }.to_result() + unsafe { (self.discover)(self, ty, layer, use_bis.into(), info) }.to_result() } /// Returns the size of a file located on a TFTP server. @@ -157,13 +158,13 @@ impl BaseCode { self, PxeBaseCodeTftpOpcode::TFTP_GET_FILE_SIZE, null_mut(), - false, + Boolean::FALSE, &mut buffer_size, None, server_ip, filename.as_ptr(), None, - false, + Boolean::FALSE, ) }; status.to_result_with_val(|| buffer_size) @@ -178,9 +179,9 @@ impl BaseCode { ) -> Result { let (buffer_ptr, mut buffer_size, dont_use_buffer) = if let Some(buffer) = buffer { let buffer_size = u64::try_from(buffer.len()).unwrap(); - (buffer.as_mut_ptr().cast(), buffer_size, false) + (buffer.as_mut_ptr().cast(), buffer_size, Boolean::FALSE) } else { - (null_mut(), 0, true) + (null_mut(), 0, Boolean::TRUE) }; let status = unsafe { @@ -188,7 +189,7 @@ impl BaseCode { self, PxeBaseCodeTftpOpcode::TFTP_READ_FILE, buffer_ptr, - false, + Boolean::FALSE, &mut buffer_size, None, server_ip, @@ -216,13 +217,13 @@ impl BaseCode { self, PxeBaseCodeTftpOpcode::TFTP_WRITE_FILE, buffer_ptr, - overwrite, + overwrite.into(), &mut buffer_size, None, server_ip, filename.as_ptr(), None, - false, + Boolean::FALSE, ) } .to_result() @@ -244,13 +245,13 @@ impl BaseCode { self, PxeBaseCodeTftpOpcode::TFTP_READ_DIRECTORY, buffer_ptr, - false, + Boolean::FALSE, &mut buffer_size, None, server_ip, directory_name.as_ptr(), None, - false, + Boolean::FALSE, ) }; status.to_result()?; @@ -317,13 +318,13 @@ impl BaseCode { self, PxeBaseCodeTftpOpcode::MTFTP_GET_FILE_SIZE, null_mut(), - false, + Boolean::FALSE, &mut buffer_size, None, server_ip, filename.as_ptr(), Some(info), - false, + Boolean::FALSE, ) }; status.to_result_with_val(|| buffer_size) @@ -339,9 +340,9 @@ impl BaseCode { ) -> Result { let (buffer_ptr, mut buffer_size, dont_use_buffer) = if let Some(buffer) = buffer { let buffer_size = u64::try_from(buffer.len()).unwrap(); - (buffer.as_mut_ptr().cast(), buffer_size, false) + (buffer.as_mut_ptr().cast(), buffer_size, Boolean::FALSE) } else { - (null_mut(), 0, true) + (null_mut(), 0, Boolean::TRUE) }; let status = unsafe { @@ -349,7 +350,7 @@ impl BaseCode { self, PxeBaseCodeTftpOpcode::MTFTP_READ_FILE, buffer_ptr, - false, + Boolean::FALSE, &mut buffer_size, None, server_ip, @@ -377,13 +378,13 @@ impl BaseCode { self, PxeBaseCodeTftpOpcode::MTFTP_READ_DIRECTORY, buffer_ptr, - false, + Boolean::FALSE, &mut buffer_size, None, server_ip, null_mut(), Some(info), - false, + Boolean::FALSE, ) }; status.to_result()?; @@ -555,11 +556,11 @@ impl BaseCode { unsafe { (self.set_parameters)( self, - new_auto_arp.as_ref(), - new_send_guid.as_ref(), + opt_bool_to_ptr(&new_auto_arp), + opt_bool_to_ptr(&new_send_guid), new_ttl.as_ref(), new_tos.as_ref(), - new_make_callback.as_ref(), + opt_bool_to_ptr(&new_make_callback), ) } .to_result() @@ -595,12 +596,12 @@ impl BaseCode { unsafe { (self.set_packets)( self, - new_dhcp_discover_valid.as_ref(), - new_dhcp_ack_received.as_ref(), - new_proxy_offer_received.as_ref(), - new_pxe_discover_valid.as_ref(), - new_pxe_reply_received.as_ref(), - new_pxe_bis_reply_received.as_ref(), + opt_bool_to_ptr(&new_dhcp_discover_valid), + opt_bool_to_ptr(&new_dhcp_ack_received), + opt_bool_to_ptr(&new_proxy_offer_received), + opt_bool_to_ptr(&new_pxe_discover_valid), + opt_bool_to_ptr(&new_pxe_reply_received), + opt_bool_to_ptr(&new_pxe_bis_reply_received), new_dhcp_discover, new_dhcp_ack, new_proxy_offer, @@ -619,6 +620,15 @@ impl BaseCode { } } +/// Convert an `&Option` to a `*const Boolean`. +/// +/// This is always a valid conversion; `bool` is an 8-bit `0` or `1`. +fn opt_bool_to_ptr(arg: &Option) -> *const Boolean { + arg.as_ref() + .map(|arg| ptr::from_ref(arg).cast::()) + .unwrap_or_else(null) +} + opaque_type! { /// Opaque type that should be used to represent a pointer to a [`DiscoverInfo`] in /// foreign function interfaces. This type produces a thin pointer, unlike From f76595c77433cfb1d9941d1cec1bf5974cb8915a Mon Sep 17 00:00:00 2001 From: Nicholas Bishop Date: Sun, 16 Mar 2025 14:28:02 -0400 Subject: [PATCH 02/11] uefi: Convert some uses of IpAddress to uefi_raw::IpAddress in pxe::BaseCode This is an intermediate step towards using the uefi-raw definition of the protocol. --- uefi/src/proto/network/mod.rs | 7 +++++++ uefi/src/proto/network/pxe.rs | 24 ++++++++++++------------ 2 files changed, 19 insertions(+), 12 deletions(-) diff --git a/uefi/src/proto/network/mod.rs b/uefi/src/proto/network/mod.rs index ffce22303..72483f448 100644 --- a/uefi/src/proto/network/mod.rs +++ b/uefi/src/proto/network/mod.rs @@ -33,6 +33,13 @@ impl IpAddress { pub const fn new_v6(ip_addr: [u8; 16]) -> Self { Self(ip_addr) } + + #[must_use] + const fn as_raw_ptr(&self) -> *const uefi_raw::IpAddress { + // The uefi-raw type is defined differently, but the layout is + // compatible. + self.0.as_ptr().cast() + } } impl From for IpAddress { diff --git a/uefi/src/proto/network/pxe.rs b/uefi/src/proto/network/pxe.rs index 0b4a23594..20b6528a0 100644 --- a/uefi/src/proto/network/pxe.rs +++ b/uefi/src/proto/network/pxe.rs @@ -46,7 +46,7 @@ pub struct BaseCode { overwrite: Boolean, buffer_size: &mut u64, block_size: Option<&usize>, - server_ip: &IpAddress, + server_ip: *const uefi_raw::IpAddress, filename: *const Char8, info: Option<&MtftpInfo>, dont_use_buffer: Boolean, @@ -54,7 +54,7 @@ pub struct BaseCode { udp_write: unsafe extern "efiapi" fn( this: &Self, op_flags: UdpOpFlags, - dest_ip: &IpAddress, + dest_ip: *const uefi_raw::IpAddress, dest_port: &u16, gateway_ip: Option<&IpAddress>, src_ip: Option<&IpAddress>, @@ -79,7 +79,7 @@ pub struct BaseCode { set_ip_filter: unsafe extern "efiapi" fn(this: &Self, new_filter: &IpFilter) -> Status, arp: unsafe extern "efiapi" fn( this: &Self, - ip_addr: &IpAddress, + ip_addr: *const uefi_raw::IpAddress, mac_addr: Option<&mut MacAddress>, ) -> Status, set_parameters: unsafe extern "efiapi" fn( @@ -161,7 +161,7 @@ impl BaseCode { Boolean::FALSE, &mut buffer_size, None, - server_ip, + server_ip.as_raw_ptr(), filename.as_ptr(), None, Boolean::FALSE, @@ -192,7 +192,7 @@ impl BaseCode { Boolean::FALSE, &mut buffer_size, None, - server_ip, + server_ip.as_raw_ptr(), filename.as_ptr(), None, dont_use_buffer, @@ -220,7 +220,7 @@ impl BaseCode { overwrite.into(), &mut buffer_size, None, - server_ip, + server_ip.as_raw_ptr(), filename.as_ptr(), None, Boolean::FALSE, @@ -248,7 +248,7 @@ impl BaseCode { Boolean::FALSE, &mut buffer_size, None, - server_ip, + server_ip.as_raw_ptr(), directory_name.as_ptr(), None, Boolean::FALSE, @@ -321,7 +321,7 @@ impl BaseCode { Boolean::FALSE, &mut buffer_size, None, - server_ip, + server_ip.as_raw_ptr(), filename.as_ptr(), Some(info), Boolean::FALSE, @@ -353,7 +353,7 @@ impl BaseCode { Boolean::FALSE, &mut buffer_size, None, - server_ip, + server_ip.as_raw_ptr(), filename.as_ptr(), Some(info), dont_use_buffer, @@ -381,7 +381,7 @@ impl BaseCode { Boolean::FALSE, &mut buffer_size, None, - server_ip, + server_ip.as_raw_ptr(), null_mut(), Some(info), Boolean::FALSE, @@ -479,7 +479,7 @@ impl BaseCode { (self.udp_write)( self, op_flags, - dest_ip, + dest_ip.as_raw_ptr(), &dest_port, gateway_ip, src_ip, @@ -540,7 +540,7 @@ impl BaseCode { /// Uses the ARP protocol to resolve a MAC address. pub fn arp(&mut self, ip_addr: &IpAddress, mac_addr: Option<&mut MacAddress>) -> Result { - unsafe { (self.arp)(self, ip_addr, mac_addr) }.to_result() + unsafe { (self.arp)(self, ip_addr.as_raw_ptr(), mac_addr) }.to_result() } /// Updates the parameters that affect the operation of the PXE Base Code From aac4efef5fb2db80b9cd84fd360a11453b825d10 Mon Sep 17 00:00:00 2001 From: Nicholas Bishop Date: Sun, 16 Mar 2025 14:35:06 -0400 Subject: [PATCH 03/11] uefi: Convert more uses of IpAddress to uefi_raw::IpAddress in pxe::BaseCode This is an intermediate step towards using the uefi-raw definition of the protocol. --- uefi/src/proto/network/pxe.rs | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/uefi/src/proto/network/pxe.rs b/uefi/src/proto/network/pxe.rs index 20b6528a0..d50542525 100644 --- a/uefi/src/proto/network/pxe.rs +++ b/uefi/src/proto/network/pxe.rs @@ -56,8 +56,8 @@ pub struct BaseCode { op_flags: UdpOpFlags, dest_ip: *const uefi_raw::IpAddress, dest_port: &u16, - gateway_ip: Option<&IpAddress>, - src_ip: Option<&IpAddress>, + gateway_ip: *const uefi_raw::IpAddress, + src_ip: *const uefi_raw::IpAddress, src_port: Option<&mut u16>, header_size: Option<&usize>, header_ptr: *const c_void, @@ -92,8 +92,8 @@ pub struct BaseCode { ) -> Status, set_station_ip: unsafe extern "efiapi" fn( this: &Self, - new_station_ip: Option<&IpAddress>, - new_subnet_mask: Option<&IpAddress>, + new_station_ip: *const uefi_raw::IpAddress, + new_subnet_mask: *const uefi_raw::IpAddress, ) -> Status, set_packets: unsafe extern "efiapi" fn( this: &Self, @@ -481,8 +481,8 @@ impl BaseCode { op_flags, dest_ip.as_raw_ptr(), &dest_port, - gateway_ip, - src_ip, + opt_ip_addr_to_ptr(gateway_ip), + opt_ip_addr_to_ptr(src_ip), src_port, header_size, header_ptr, @@ -573,7 +573,14 @@ impl BaseCode { new_station_ip: Option<&IpAddress>, new_subnet_mask: Option<&IpAddress>, ) -> Result { - unsafe { (self.set_station_ip)(self, new_station_ip, new_subnet_mask) }.to_result() + unsafe { + (self.set_station_ip)( + self, + opt_ip_addr_to_ptr(new_station_ip), + opt_ip_addr_to_ptr(new_subnet_mask), + ) + } + .to_result() } /// Updates the contents of the cached DHCP and Discover packets. @@ -629,6 +636,11 @@ fn opt_bool_to_ptr(arg: &Option) -> *const Boolean { .unwrap_or_else(null) } +/// Convert an `Option<&IpAddress>` to a `*const uefi_raw::IpAddress`. +fn opt_ip_addr_to_ptr(arg: Option<&IpAddress>) -> *const uefi_raw::IpAddress { + arg.map(|arg| arg.as_raw_ptr()).unwrap_or_else(null) +} + opaque_type! { /// Opaque type that should be used to represent a pointer to a [`DiscoverInfo`] in /// foreign function interfaces. This type produces a thin pointer, unlike From 26acc20e68f92f07cab65502c4cdf03e14c09845 Mon Sep 17 00:00:00 2001 From: Nicholas Bishop Date: Sun, 16 Mar 2025 15:57:22 -0400 Subject: [PATCH 04/11] uefi: Convert remaining uses of IpAddress to uefi_raw::IpAddress in pxe::BaseCode This is an intermediate step towards using the uefi-raw definition of the protocol. --- uefi/src/proto/network/mod.rs | 7 +++++++ uefi/src/proto/network/pxe.rs | 13 +++++++++---- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/uefi/src/proto/network/mod.rs b/uefi/src/proto/network/mod.rs index 72483f448..7d93f03cf 100644 --- a/uefi/src/proto/network/mod.rs +++ b/uefi/src/proto/network/mod.rs @@ -40,6 +40,13 @@ impl IpAddress { // compatible. self.0.as_ptr().cast() } + + #[must_use] + fn as_raw_ptr_mut(&mut self) -> *mut uefi_raw::IpAddress { + // The uefi-raw type is defined differently, but the layout is + // compatible. + self.0.as_mut_ptr().cast() + } } impl From for IpAddress { diff --git a/uefi/src/proto/network/pxe.rs b/uefi/src/proto/network/pxe.rs index d50542525..05e79cbc7 100644 --- a/uefi/src/proto/network/pxe.rs +++ b/uefi/src/proto/network/pxe.rs @@ -67,9 +67,9 @@ pub struct BaseCode { udp_read: unsafe extern "efiapi" fn( this: &Self, op_flags: UdpOpFlags, - dest_ip: Option<&mut IpAddress>, + dest_ip: *mut uefi_raw::IpAddress, dest_port: Option<&mut u16>, - src_ip: Option<&mut IpAddress>, + src_ip: *mut uefi_raw::IpAddress, src_port: Option<&mut u16>, header_size: Option<&usize>, header_ptr: *mut c_void, @@ -519,9 +519,9 @@ impl BaseCode { (self.udp_read)( self, op_flags, - dest_ip, + opt_ip_addr_to_ptr_mut(dest_ip), dest_port, - src_ip, + opt_ip_addr_to_ptr_mut(src_ip), src_port, header_size, header_ptr, @@ -641,6 +641,11 @@ fn opt_ip_addr_to_ptr(arg: Option<&IpAddress>) -> *const uefi_raw::IpAddress { arg.map(|arg| arg.as_raw_ptr()).unwrap_or_else(null) } +/// Convert an `Option<&mut IpAddress>` to a `*mut uefi_raw::IpAddress`. +fn opt_ip_addr_to_ptr_mut(arg: Option<&mut IpAddress>) -> *mut uefi_raw::IpAddress { + arg.map(|arg| arg.as_raw_ptr_mut()).unwrap_or_else(null_mut) +} + opaque_type! { /// Opaque type that should be used to represent a pointer to a [`DiscoverInfo`] in /// foreign function interfaces. This type produces a thin pointer, unlike From 17c6e6b9881fd756865e58599690c5c4fb17e8f2 Mon Sep 17 00:00:00 2001 From: Nicholas Bishop Date: Sun, 16 Mar 2025 15:05:46 -0400 Subject: [PATCH 05/11] uefi: Convert uses of Char8 to uefi_raw::Char8 in pxe::BaseCode This is an intermediate step towards using the uefi-raw definition of the protocol. --- uefi/src/proto/network/pxe.rs | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/uefi/src/proto/network/pxe.rs b/uefi/src/proto/network/pxe.rs index 05e79cbc7..5c18c7f4c 100644 --- a/uefi/src/proto/network/pxe.rs +++ b/uefi/src/proto/network/pxe.rs @@ -6,7 +6,7 @@ use super::{IpAddress, MacAddress}; use crate::polyfill::maybe_uninit_slice_as_mut_ptr; use crate::proto::unsafe_protocol; use crate::util::ptr_write_unaligned_and_add; -use crate::{CStr8, Char8, Result, Status, StatusExt}; +use crate::{CStr8, Result, Status, StatusExt}; use bitflags::bitflags; use core::ffi::c_void; use core::fmt::{self, Debug, Display, Formatter}; @@ -15,7 +15,7 @@ use core::mem::MaybeUninit; use core::ptr::{self, null, null_mut}; use ptr_meta::Pointee; use uefi_raw::protocol::network::pxe::PxeBaseCodeTftpOpcode; -use uefi_raw::Boolean; +use uefi_raw::{Boolean, Char8}; pub use uefi_raw::protocol::network::pxe::{ PxeBaseCodeBootType as BootstrapType, PxeBaseCodeIpFilterFlags as IpFilters, @@ -162,7 +162,7 @@ impl BaseCode { &mut buffer_size, None, server_ip.as_raw_ptr(), - filename.as_ptr(), + cstr8_to_ptr(filename), None, Boolean::FALSE, ) @@ -193,7 +193,7 @@ impl BaseCode { &mut buffer_size, None, server_ip.as_raw_ptr(), - filename.as_ptr(), + cstr8_to_ptr(filename), None, dont_use_buffer, ) @@ -221,7 +221,7 @@ impl BaseCode { &mut buffer_size, None, server_ip.as_raw_ptr(), - filename.as_ptr(), + cstr8_to_ptr(filename), None, Boolean::FALSE, ) @@ -249,7 +249,7 @@ impl BaseCode { &mut buffer_size, None, server_ip.as_raw_ptr(), - directory_name.as_ptr(), + cstr8_to_ptr(directory_name), None, Boolean::FALSE, ) @@ -322,7 +322,7 @@ impl BaseCode { &mut buffer_size, None, server_ip.as_raw_ptr(), - filename.as_ptr(), + cstr8_to_ptr(filename), Some(info), Boolean::FALSE, ) @@ -354,7 +354,7 @@ impl BaseCode { &mut buffer_size, None, server_ip.as_raw_ptr(), - filename.as_ptr(), + cstr8_to_ptr(filename), Some(info), dont_use_buffer, ) @@ -627,6 +627,11 @@ impl BaseCode { } } +/// Convert a `&CStr8` to a `*const uefi_raw::Char8`. +const fn cstr8_to_ptr(s: &CStr8) -> *const Char8 { + s.as_ptr().cast() +} + /// Convert an `&Option` to a `*const Boolean`. /// /// This is always a valid conversion; `bool` is an 8-bit `0` or `1`. From c866f4adea78c3db7d831b6c3e0fbbd354d9ae4e Mon Sep 17 00:00:00 2001 From: Nicholas Bishop Date: Sun, 16 Mar 2025 15:42:01 -0400 Subject: [PATCH 06/11] uefi: Convert uses of Packet to PxeBaseCodePacket in pxe::BaseCode This is an intermediate step towards using the uefi-raw definition of the protocol. --- uefi/src/proto/network/pxe.rs | 31 ++++++++++++++++++------------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/uefi/src/proto/network/pxe.rs b/uefi/src/proto/network/pxe.rs index 5c18c7f4c..fd68e7b72 100644 --- a/uefi/src/proto/network/pxe.rs +++ b/uefi/src/proto/network/pxe.rs @@ -14,7 +14,7 @@ use core::iter::from_fn; use core::mem::MaybeUninit; use core::ptr::{self, null, null_mut}; use ptr_meta::Pointee; -use uefi_raw::protocol::network::pxe::PxeBaseCodeTftpOpcode; +use uefi_raw::protocol::network::pxe::{PxeBaseCodePacket, PxeBaseCodeTftpOpcode}; use uefi_raw::{Boolean, Char8}; pub use uefi_raw::protocol::network::pxe::{ @@ -103,12 +103,12 @@ pub struct BaseCode { new_pxe_discover_valid: *const Boolean, new_pxe_reply_received: *const Boolean, new_pxe_bis_reply_received: *const Boolean, - new_dhcp_discover: Option<&Packet>, - new_dhcp_ack: Option<&Packet>, - new_proxy_offer: Option<&Packet>, - new_pxe_discover: Option<&Packet>, - new_pxe_reply: Option<&Packet>, - new_pxe_bis_reply: Option<&Packet>, + new_dhcp_discover: *const PxeBaseCodePacket, + new_dhcp_ack: *const PxeBaseCodePacket, + new_proxy_offer: *const PxeBaseCodePacket, + new_pxe_discover: *const PxeBaseCodePacket, + new_pxe_reply: *const PxeBaseCodePacket, + new_pxe_bis_reply: *const PxeBaseCodePacket, ) -> Status, mode: *const Mode, } @@ -609,12 +609,12 @@ impl BaseCode { opt_bool_to_ptr(&new_pxe_discover_valid), opt_bool_to_ptr(&new_pxe_reply_received), opt_bool_to_ptr(&new_pxe_bis_reply_received), - new_dhcp_discover, - new_dhcp_ack, - new_proxy_offer, - new_pxe_discover, - new_pxe_reply, - new_pxe_bis_reply, + opt_packet_to_ptr(new_dhcp_discover), + opt_packet_to_ptr(new_dhcp_ack), + opt_packet_to_ptr(new_proxy_offer), + opt_packet_to_ptr(new_pxe_discover), + opt_packet_to_ptr(new_pxe_reply), + opt_packet_to_ptr(new_pxe_bis_reply), ) } .to_result() @@ -651,6 +651,11 @@ fn opt_ip_addr_to_ptr_mut(arg: Option<&mut IpAddress>) -> *mut uefi_raw::IpAddre arg.map(|arg| arg.as_raw_ptr_mut()).unwrap_or_else(null_mut) } +/// Convert an `Option<&Packet>` to a `*const PxeBaseCodePacket`. +fn opt_packet_to_ptr(arg: Option<&Packet>) -> *const PxeBaseCodePacket { + arg.map(|p| ptr::from_ref(p).cast()).unwrap_or_else(null) +} + opaque_type! { /// Opaque type that should be used to represent a pointer to a [`DiscoverInfo`] in /// foreign function interfaces. This type produces a thin pointer, unlike From e43f9bb4b0e8ab41740dade004074a4d547ea753 Mon Sep 17 00:00:00 2001 From: Nicholas Bishop Date: Sun, 16 Mar 2025 15:52:25 -0400 Subject: [PATCH 07/11] uefi: Convert uses of MtftpInfo to PxeBaseCodeMtftpInfo in pxe::BaseCode This is an intermediate step towards using the uefi-raw definition of the protocol. --- uefi/src/proto/network/pxe.rs | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/uefi/src/proto/network/pxe.rs b/uefi/src/proto/network/pxe.rs index fd68e7b72..7abb459dc 100644 --- a/uefi/src/proto/network/pxe.rs +++ b/uefi/src/proto/network/pxe.rs @@ -14,7 +14,9 @@ use core::iter::from_fn; use core::mem::MaybeUninit; use core::ptr::{self, null, null_mut}; use ptr_meta::Pointee; -use uefi_raw::protocol::network::pxe::{PxeBaseCodePacket, PxeBaseCodeTftpOpcode}; +use uefi_raw::protocol::network::pxe::{ + PxeBaseCodeMtftpInfo, PxeBaseCodePacket, PxeBaseCodeTftpOpcode, +}; use uefi_raw::{Boolean, Char8}; pub use uefi_raw::protocol::network::pxe::{ @@ -48,7 +50,7 @@ pub struct BaseCode { block_size: Option<&usize>, server_ip: *const uefi_raw::IpAddress, filename: *const Char8, - info: Option<&MtftpInfo>, + info: *const PxeBaseCodeMtftpInfo, dont_use_buffer: Boolean, ) -> Status, udp_write: unsafe extern "efiapi" fn( @@ -163,7 +165,7 @@ impl BaseCode { None, server_ip.as_raw_ptr(), cstr8_to_ptr(filename), - None, + null(), Boolean::FALSE, ) }; @@ -194,7 +196,7 @@ impl BaseCode { None, server_ip.as_raw_ptr(), cstr8_to_ptr(filename), - None, + null(), dont_use_buffer, ) }; @@ -222,7 +224,7 @@ impl BaseCode { None, server_ip.as_raw_ptr(), cstr8_to_ptr(filename), - None, + null(), Boolean::FALSE, ) } @@ -250,7 +252,7 @@ impl BaseCode { None, server_ip.as_raw_ptr(), cstr8_to_ptr(directory_name), - None, + null(), Boolean::FALSE, ) }; @@ -323,7 +325,7 @@ impl BaseCode { None, server_ip.as_raw_ptr(), cstr8_to_ptr(filename), - Some(info), + info.as_raw_ptr(), Boolean::FALSE, ) }; @@ -355,7 +357,7 @@ impl BaseCode { None, server_ip.as_raw_ptr(), cstr8_to_ptr(filename), - Some(info), + info.as_raw_ptr(), dont_use_buffer, ) }; @@ -383,7 +385,7 @@ impl BaseCode { None, server_ip.as_raw_ptr(), null_mut(), - Some(info), + info.as_raw_ptr(), Boolean::FALSE, ) }; @@ -829,6 +831,12 @@ pub struct MtftpInfo { pub transmit_timeout: u16, } +impl MtftpInfo { + const fn as_raw_ptr(&self) -> *const PxeBaseCodeMtftpInfo { + ptr::from_ref(self).cast() + } +} + /// IP receive filter settings /// /// Corresponds to the `EFI_PXE_BASE_CODE_IP_FILTER` type in the C API. From 2a02be01c35366194eafacf4e5f5359d115c0410 Mon Sep 17 00:00:00 2001 From: Nicholas Bishop Date: Sun, 16 Mar 2025 16:10:04 -0400 Subject: [PATCH 08/11] uefi: Convert use of DiscoverInfo to PxeBaseCodeDiscoverInfo in pxe::BaseCode This is an intermediate step towards using the uefi-raw definition of the protocol. --- uefi/src/proto/network/pxe.rs | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/uefi/src/proto/network/pxe.rs b/uefi/src/proto/network/pxe.rs index 7abb459dc..3f305aee3 100644 --- a/uefi/src/proto/network/pxe.rs +++ b/uefi/src/proto/network/pxe.rs @@ -15,7 +15,7 @@ use core::mem::MaybeUninit; use core::ptr::{self, null, null_mut}; use ptr_meta::Pointee; use uefi_raw::protocol::network::pxe::{ - PxeBaseCodeMtftpInfo, PxeBaseCodePacket, PxeBaseCodeTftpOpcode, + PxeBaseCodeDiscoverInfo, PxeBaseCodeMtftpInfo, PxeBaseCodePacket, PxeBaseCodeTftpOpcode, }; use uefi_raw::{Boolean, Char8}; @@ -39,7 +39,7 @@ pub struct BaseCode { ty: BootstrapType, layer: &mut u16, use_bis: Boolean, - info: *const FfiDiscoverInfo, + info: *const PxeBaseCodeDiscoverInfo, ) -> Status, mtftp: unsafe extern "efiapi" fn( this: &Self, @@ -141,11 +141,8 @@ impl BaseCode { use_bis: bool, info: Option<&DiscoverInfo>, ) -> Result { - let info: *const FfiDiscoverInfo = info - .map(|info| { - let info_ptr: *const DiscoverInfo = info; - info_ptr.cast() - }) + let info: *const PxeBaseCodeDiscoverInfo = info + .map(|info| ptr::from_ref(info).cast()) .unwrap_or(null()); unsafe { (self.discover)(self, ty, layer, use_bis.into(), info) }.to_result() From 56735b05838a6ea57b79186456e27b25de85156a Mon Sep 17 00:00:00 2001 From: Nicholas Bishop Date: Sun, 16 Mar 2025 16:13:55 -0400 Subject: [PATCH 09/11] uefi: Convert use of IpFilter to PxeBaseCodeIpFilter in pxe::BaseCode This is an intermediate step towards using the uefi-raw definition of the protocol. --- uefi/src/proto/network/pxe.rs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/uefi/src/proto/network/pxe.rs b/uefi/src/proto/network/pxe.rs index 3f305aee3..6c4a2fb0f 100644 --- a/uefi/src/proto/network/pxe.rs +++ b/uefi/src/proto/network/pxe.rs @@ -15,7 +15,8 @@ use core::mem::MaybeUninit; use core::ptr::{self, null, null_mut}; use ptr_meta::Pointee; use uefi_raw::protocol::network::pxe::{ - PxeBaseCodeDiscoverInfo, PxeBaseCodeMtftpInfo, PxeBaseCodePacket, PxeBaseCodeTftpOpcode, + PxeBaseCodeDiscoverInfo, PxeBaseCodeIpFilter, PxeBaseCodeMtftpInfo, PxeBaseCodePacket, + PxeBaseCodeTftpOpcode, }; use uefi_raw::{Boolean, Char8}; @@ -78,7 +79,8 @@ pub struct BaseCode { buffer_size: &mut usize, buffer_ptr: *mut c_void, ) -> Status, - set_ip_filter: unsafe extern "efiapi" fn(this: &Self, new_filter: &IpFilter) -> Status, + set_ip_filter: + unsafe extern "efiapi" fn(this: &Self, new_filter: *const PxeBaseCodeIpFilter) -> Status, arp: unsafe extern "efiapi" fn( this: &Self, ip_addr: *const uefi_raw::IpAddress, @@ -534,6 +536,7 @@ impl BaseCode { /// Updates the IP receive filters of a network device and enables software /// filtering. pub fn set_ip_filter(&mut self, new_filter: &IpFilter) -> Result { + let new_filter: *const PxeBaseCodeIpFilter = ptr::from_ref(new_filter).cast(); unsafe { (self.set_ip_filter)(self, new_filter) }.to_result() } From 60fce7907f8c0789f51fe8e38c5f5d6813b8e1d8 Mon Sep 17 00:00:00 2001 From: Nicholas Bishop Date: Mon, 17 Feb 2025 15:41:52 -0500 Subject: [PATCH 10/11] uefi: Remove use of Option in definition of pxe::BaseCode This is an intermediate step towards using the uefi-raw definition of the protocol. --- uefi/src/proto/network/pxe.rs | 60 ++++++++++++++++++++--------------- 1 file changed, 35 insertions(+), 25 deletions(-) diff --git a/uefi/src/proto/network/pxe.rs b/uefi/src/proto/network/pxe.rs index 6c4a2fb0f..1a3904601 100644 --- a/uefi/src/proto/network/pxe.rs +++ b/uefi/src/proto/network/pxe.rs @@ -48,7 +48,7 @@ pub struct BaseCode { buffer: *mut c_void, overwrite: Boolean, buffer_size: &mut u64, - block_size: Option<&usize>, + block_size: *const usize, server_ip: *const uefi_raw::IpAddress, filename: *const Char8, info: *const PxeBaseCodeMtftpInfo, @@ -61,8 +61,8 @@ pub struct BaseCode { dest_port: &u16, gateway_ip: *const uefi_raw::IpAddress, src_ip: *const uefi_raw::IpAddress, - src_port: Option<&mut u16>, - header_size: Option<&usize>, + src_port: *mut u16, + header_size: *const usize, header_ptr: *const c_void, buffer_size: &usize, buffer_ptr: *const c_void, @@ -71,10 +71,10 @@ pub struct BaseCode { this: &Self, op_flags: UdpOpFlags, dest_ip: *mut uefi_raw::IpAddress, - dest_port: Option<&mut u16>, + dest_port: *mut u16, src_ip: *mut uefi_raw::IpAddress, - src_port: Option<&mut u16>, - header_size: Option<&usize>, + src_port: *mut u16, + header_size: *const usize, header_ptr: *mut c_void, buffer_size: &mut usize, buffer_ptr: *mut c_void, @@ -84,14 +84,14 @@ pub struct BaseCode { arp: unsafe extern "efiapi" fn( this: &Self, ip_addr: *const uefi_raw::IpAddress, - mac_addr: Option<&mut MacAddress>, + mac_addr: *mut MacAddress, ) -> Status, set_parameters: unsafe extern "efiapi" fn( this: &Self, new_auto_arp: *const Boolean, new_send_guid: *const Boolean, - new_ttl: Option<&u8>, - new_tos: Option<&u8>, + new_ttl: *const u8, + new_tos: *const u8, new_make_callback: *const Boolean, ) -> Status, set_station_ip: unsafe extern "efiapi" fn( @@ -161,7 +161,7 @@ impl BaseCode { null_mut(), Boolean::FALSE, &mut buffer_size, - None, + null(), server_ip.as_raw_ptr(), cstr8_to_ptr(filename), null(), @@ -192,7 +192,7 @@ impl BaseCode { buffer_ptr, Boolean::FALSE, &mut buffer_size, - None, + null(), server_ip.as_raw_ptr(), cstr8_to_ptr(filename), null(), @@ -220,7 +220,7 @@ impl BaseCode { buffer_ptr, overwrite.into(), &mut buffer_size, - None, + null(), server_ip.as_raw_ptr(), cstr8_to_ptr(filename), null(), @@ -248,7 +248,7 @@ impl BaseCode { buffer_ptr, Boolean::FALSE, &mut buffer_size, - None, + null(), server_ip.as_raw_ptr(), cstr8_to_ptr(directory_name), null(), @@ -321,7 +321,7 @@ impl BaseCode { null_mut(), Boolean::FALSE, &mut buffer_size, - None, + null(), server_ip.as_raw_ptr(), cstr8_to_ptr(filename), info.as_raw_ptr(), @@ -353,7 +353,7 @@ impl BaseCode { buffer_ptr, Boolean::FALSE, &mut buffer_size, - None, + null(), server_ip.as_raw_ptr(), cstr8_to_ptr(filename), info.as_raw_ptr(), @@ -381,7 +381,7 @@ impl BaseCode { buffer_ptr, Boolean::FALSE, &mut buffer_size, - None, + null(), server_ip.as_raw_ptr(), null_mut(), info.as_raw_ptr(), @@ -484,8 +484,8 @@ impl BaseCode { &dest_port, opt_ip_addr_to_ptr(gateway_ip), opt_ip_addr_to_ptr(src_ip), - src_port, - header_size, + opt_mut_to_ptr(src_port), + opt_ref_to_ptr(header_size), header_ptr, &buffer.len(), buffer.as_ptr().cast(), @@ -509,9 +509,9 @@ impl BaseCode { let header_size_tmp; let (header_size, header_ptr) = if let Some(header) = header { header_size_tmp = header.len(); - (Some(&header_size_tmp), header.as_mut_ptr().cast()) + (ptr::from_ref(&header_size_tmp), header.as_mut_ptr().cast()) } else { - (None, null_mut()) + (null(), null_mut()) }; let mut buffer_size = buffer.len(); @@ -521,9 +521,9 @@ impl BaseCode { self, op_flags, opt_ip_addr_to_ptr_mut(dest_ip), - dest_port, + opt_mut_to_ptr(dest_port), opt_ip_addr_to_ptr_mut(src_ip), - src_port, + opt_mut_to_ptr(src_port), header_size, header_ptr, &mut buffer_size, @@ -542,7 +542,7 @@ impl BaseCode { /// Uses the ARP protocol to resolve a MAC address. pub fn arp(&mut self, ip_addr: &IpAddress, mac_addr: Option<&mut MacAddress>) -> Result { - unsafe { (self.arp)(self, ip_addr.as_raw_ptr(), mac_addr) }.to_result() + unsafe { (self.arp)(self, ip_addr.as_raw_ptr(), opt_mut_to_ptr(mac_addr)) }.to_result() } /// Updates the parameters that affect the operation of the PXE Base Code @@ -560,8 +560,8 @@ impl BaseCode { self, opt_bool_to_ptr(&new_auto_arp), opt_bool_to_ptr(&new_send_guid), - new_ttl.as_ref(), - new_tos.as_ref(), + opt_ref_to_ptr(new_ttl.as_ref()), + opt_ref_to_ptr(new_tos.as_ref()), opt_bool_to_ptr(&new_make_callback), ) } @@ -658,6 +658,16 @@ fn opt_packet_to_ptr(arg: Option<&Packet>) -> *const PxeBaseCodePacket { arg.map(|p| ptr::from_ref(p).cast()).unwrap_or_else(null) } +/// Convert an `Option<&T>` to a `*const T`. +fn opt_ref_to_ptr(opt: Option<&T>) -> *const T { + opt.map(ptr::from_ref).unwrap_or(null()) +} + +/// Convert an `Option<&mut T>` to a `*mut T`. +fn opt_mut_to_ptr(opt: Option<&mut T>) -> *mut T { + opt.map(ptr::from_mut).unwrap_or(null_mut()) +} + opaque_type! { /// Opaque type that should be used to represent a pointer to a [`DiscoverInfo`] in /// foreign function interfaces. This type produces a thin pointer, unlike From 95a3cab2992f518e83b0b9baaddb4e67dc984492 Mon Sep 17 00:00:00 2001 From: Nicholas Bishop Date: Mon, 17 Feb 2025 01:59:02 -0500 Subject: [PATCH 11/11] uefi: Use PxeBaseCodeProtocol to implement pxe::BaseCode --- uefi/src/proto/network/pxe.rs | 158 ++++++++-------------------------- 1 file changed, 36 insertions(+), 122 deletions(-) diff --git a/uefi/src/proto/network/pxe.rs b/uefi/src/proto/network/pxe.rs index 1a3904601..73df8223e 100644 --- a/uefi/src/proto/network/pxe.rs +++ b/uefi/src/proto/network/pxe.rs @@ -8,7 +8,6 @@ use crate::proto::unsafe_protocol; use crate::util::ptr_write_unaligned_and_add; use crate::{CStr8, Result, Status, StatusExt}; use bitflags::bitflags; -use core::ffi::c_void; use core::fmt::{self, Debug, Display, Formatter}; use core::iter::from_fn; use core::mem::MaybeUninit; @@ -16,7 +15,7 @@ use core::ptr::{self, null, null_mut}; use ptr_meta::Pointee; use uefi_raw::protocol::network::pxe::{ PxeBaseCodeDiscoverInfo, PxeBaseCodeIpFilter, PxeBaseCodeMtftpInfo, PxeBaseCodePacket, - PxeBaseCodeTftpOpcode, + PxeBaseCodeProtocol, PxeBaseCodeTftpOpcode, }; use uefi_raw::{Boolean, Char8}; @@ -27,111 +26,25 @@ pub use uefi_raw::protocol::network::pxe::{ /// PXE Base Code protocol #[derive(Debug)] -#[repr(C)] -#[unsafe_protocol("03c4e603-ac28-11d3-9a2d-0090273fc14d")] -#[allow(clippy::type_complexity)] -pub struct BaseCode { - revision: u64, - start: unsafe extern "efiapi" fn(this: &Self, use_ipv6: Boolean) -> Status, - stop: unsafe extern "efiapi" fn(this: &Self) -> Status, - dhcp: unsafe extern "efiapi" fn(this: &Self, sort_offers: Boolean) -> Status, - discover: unsafe extern "efiapi" fn( - this: &Self, - ty: BootstrapType, - layer: &mut u16, - use_bis: Boolean, - info: *const PxeBaseCodeDiscoverInfo, - ) -> Status, - mtftp: unsafe extern "efiapi" fn( - this: &Self, - operation: PxeBaseCodeTftpOpcode, - buffer: *mut c_void, - overwrite: Boolean, - buffer_size: &mut u64, - block_size: *const usize, - server_ip: *const uefi_raw::IpAddress, - filename: *const Char8, - info: *const PxeBaseCodeMtftpInfo, - dont_use_buffer: Boolean, - ) -> Status, - udp_write: unsafe extern "efiapi" fn( - this: &Self, - op_flags: UdpOpFlags, - dest_ip: *const uefi_raw::IpAddress, - dest_port: &u16, - gateway_ip: *const uefi_raw::IpAddress, - src_ip: *const uefi_raw::IpAddress, - src_port: *mut u16, - header_size: *const usize, - header_ptr: *const c_void, - buffer_size: &usize, - buffer_ptr: *const c_void, - ) -> Status, - udp_read: unsafe extern "efiapi" fn( - this: &Self, - op_flags: UdpOpFlags, - dest_ip: *mut uefi_raw::IpAddress, - dest_port: *mut u16, - src_ip: *mut uefi_raw::IpAddress, - src_port: *mut u16, - header_size: *const usize, - header_ptr: *mut c_void, - buffer_size: &mut usize, - buffer_ptr: *mut c_void, - ) -> Status, - set_ip_filter: - unsafe extern "efiapi" fn(this: &Self, new_filter: *const PxeBaseCodeIpFilter) -> Status, - arp: unsafe extern "efiapi" fn( - this: &Self, - ip_addr: *const uefi_raw::IpAddress, - mac_addr: *mut MacAddress, - ) -> Status, - set_parameters: unsafe extern "efiapi" fn( - this: &Self, - new_auto_arp: *const Boolean, - new_send_guid: *const Boolean, - new_ttl: *const u8, - new_tos: *const u8, - new_make_callback: *const Boolean, - ) -> Status, - set_station_ip: unsafe extern "efiapi" fn( - this: &Self, - new_station_ip: *const uefi_raw::IpAddress, - new_subnet_mask: *const uefi_raw::IpAddress, - ) -> Status, - set_packets: unsafe extern "efiapi" fn( - this: &Self, - new_dhcp_discover_valid: *const Boolean, - new_dhcp_ack_received: *const Boolean, - new_proxy_offer_received: *const Boolean, - new_pxe_discover_valid: *const Boolean, - new_pxe_reply_received: *const Boolean, - new_pxe_bis_reply_received: *const Boolean, - new_dhcp_discover: *const PxeBaseCodePacket, - new_dhcp_ack: *const PxeBaseCodePacket, - new_proxy_offer: *const PxeBaseCodePacket, - new_pxe_discover: *const PxeBaseCodePacket, - new_pxe_reply: *const PxeBaseCodePacket, - new_pxe_bis_reply: *const PxeBaseCodePacket, - ) -> Status, - mode: *const Mode, -} +#[repr(transparent)] +#[unsafe_protocol(PxeBaseCodeProtocol::GUID)] +pub struct BaseCode(PxeBaseCodeProtocol); impl BaseCode { /// Enables the use of the PXE Base Code Protocol functions. pub fn start(&mut self, use_ipv6: bool) -> Result { - unsafe { (self.start)(self, use_ipv6.into()) }.to_result() + unsafe { (self.0.start)(&mut self.0, use_ipv6.into()) }.to_result() } /// Disables the use of the PXE Base Code Protocol functions. pub fn stop(&mut self) -> Result { - unsafe { (self.stop)(self) }.to_result() + unsafe { (self.0.stop)(&mut self.0) }.to_result() } /// Attempts to complete a DHCPv4 D.O.R.A. (discover / offer / request / /// acknowledge) or DHCPv6 S.A.R.R (solicit / advertise / request / reply) sequence. pub fn dhcp(&mut self, sort_offers: bool) -> Result { - unsafe { (self.dhcp)(self, sort_offers.into()) }.to_result() + unsafe { (self.0.dhcp)(&mut self.0, sort_offers.into()) }.to_result() } /// Attempts to complete the PXE Boot Server and/or boot image discovery @@ -147,7 +60,7 @@ impl BaseCode { .map(|info| ptr::from_ref(info).cast()) .unwrap_or(null()); - unsafe { (self.discover)(self, ty, layer, use_bis.into(), info) }.to_result() + unsafe { (self.0.discover)(&mut self.0, ty, layer, use_bis.into(), info) }.to_result() } /// Returns the size of a file located on a TFTP server. @@ -155,8 +68,8 @@ impl BaseCode { let mut buffer_size = 0; let status = unsafe { - (self.mtftp)( - self, + (self.0.mtftp)( + &mut self.0, PxeBaseCodeTftpOpcode::TFTP_GET_FILE_SIZE, null_mut(), Boolean::FALSE, @@ -186,8 +99,8 @@ impl BaseCode { }; let status = unsafe { - (self.mtftp)( - self, + (self.0.mtftp)( + &mut self.0, PxeBaseCodeTftpOpcode::TFTP_READ_FILE, buffer_ptr, Boolean::FALSE, @@ -214,8 +127,8 @@ impl BaseCode { let mut buffer_size = u64::try_from(buffer.len()).expect("buffer length should fit in u64"); unsafe { - (self.mtftp)( - self, + (self.0.mtftp)( + &mut self.0, PxeBaseCodeTftpOpcode::TFTP_WRITE_FILE, buffer_ptr, overwrite.into(), @@ -242,8 +155,8 @@ impl BaseCode { let mut buffer_size = u64::try_from(buffer.len()).expect("buffer length should fit in u64"); let status = unsafe { - (self.mtftp)( - self, + (self.0.mtftp)( + &mut self.0, PxeBaseCodeTftpOpcode::TFTP_READ_DIRECTORY, buffer_ptr, Boolean::FALSE, @@ -315,8 +228,8 @@ impl BaseCode { let mut buffer_size = 0; let status = unsafe { - (self.mtftp)( - self, + (self.0.mtftp)( + &mut self.0, PxeBaseCodeTftpOpcode::MTFTP_GET_FILE_SIZE, null_mut(), Boolean::FALSE, @@ -347,8 +260,8 @@ impl BaseCode { }; let status = unsafe { - (self.mtftp)( - self, + (self.0.mtftp)( + &mut self.0, PxeBaseCodeTftpOpcode::MTFTP_READ_FILE, buffer_ptr, Boolean::FALSE, @@ -375,8 +288,8 @@ impl BaseCode { let mut buffer_size = u64::try_from(buffer.len()).expect("buffer length should fit in u64"); let status = unsafe { - (self.mtftp)( - self, + (self.0.mtftp)( + &mut self.0, PxeBaseCodeTftpOpcode::MTFTP_READ_DIRECTORY, buffer_ptr, Boolean::FALSE, @@ -477,8 +390,8 @@ impl BaseCode { }; unsafe { - (self.udp_write)( - self, + (self.0.udp_write)( + &mut self.0, op_flags, dest_ip.as_raw_ptr(), &dest_port, @@ -517,8 +430,8 @@ impl BaseCode { let mut buffer_size = buffer.len(); let status = unsafe { - (self.udp_read)( - self, + (self.0.udp_read)( + &mut self.0, op_flags, opt_ip_addr_to_ptr_mut(dest_ip), opt_mut_to_ptr(dest_port), @@ -537,12 +450,13 @@ impl BaseCode { /// filtering. pub fn set_ip_filter(&mut self, new_filter: &IpFilter) -> Result { let new_filter: *const PxeBaseCodeIpFilter = ptr::from_ref(new_filter).cast(); - unsafe { (self.set_ip_filter)(self, new_filter) }.to_result() + unsafe { (self.0.set_ip_filter)(&mut self.0, new_filter) }.to_result() } /// Uses the ARP protocol to resolve a MAC address. pub fn arp(&mut self, ip_addr: &IpAddress, mac_addr: Option<&mut MacAddress>) -> Result { - unsafe { (self.arp)(self, ip_addr.as_raw_ptr(), opt_mut_to_ptr(mac_addr)) }.to_result() + unsafe { (self.0.arp)(&mut self.0, ip_addr.as_raw_ptr(), opt_mut_to_ptr(mac_addr)) } + .to_result() } /// Updates the parameters that affect the operation of the PXE Base Code @@ -556,8 +470,8 @@ impl BaseCode { new_make_callback: Option, ) -> Result { unsafe { - (self.set_parameters)( - self, + (self.0.set_parameters)( + &mut self.0, opt_bool_to_ptr(&new_auto_arp), opt_bool_to_ptr(&new_send_guid), opt_ref_to_ptr(new_ttl.as_ref()), @@ -576,8 +490,8 @@ impl BaseCode { new_subnet_mask: Option<&IpAddress>, ) -> Result { unsafe { - (self.set_station_ip)( - self, + (self.0.set_station_ip)( + &mut self.0, opt_ip_addr_to_ptr(new_station_ip), opt_ip_addr_to_ptr(new_subnet_mask), ) @@ -603,8 +517,8 @@ impl BaseCode { new_pxe_bis_reply: Option<&Packet>, ) -> Result { unsafe { - (self.set_packets)( - self, + (self.0.set_packets)( + &mut self.0, opt_bool_to_ptr(&new_dhcp_discover_valid), opt_bool_to_ptr(&new_dhcp_ack_received), opt_bool_to_ptr(&new_proxy_offer_received), @@ -625,7 +539,7 @@ impl BaseCode { /// Returns a reference to the `Mode` struct. #[must_use] pub const fn mode(&self) -> &Mode { - unsafe { &*self.mode } + unsafe { &*(self.0.mode.cast()) } } }