Skip to content

Commit 95a3cab

Browse files
uefi: Use PxeBaseCodeProtocol to implement pxe::BaseCode
1 parent 60fce79 commit 95a3cab

File tree

1 file changed

+36
-122
lines changed

1 file changed

+36
-122
lines changed

uefi/src/proto/network/pxe.rs

Lines changed: 36 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,14 @@ use crate::proto::unsafe_protocol;
88
use crate::util::ptr_write_unaligned_and_add;
99
use crate::{CStr8, Result, Status, StatusExt};
1010
use bitflags::bitflags;
11-
use core::ffi::c_void;
1211
use core::fmt::{self, Debug, Display, Formatter};
1312
use core::iter::from_fn;
1413
use core::mem::MaybeUninit;
1514
use core::ptr::{self, null, null_mut};
1615
use ptr_meta::Pointee;
1716
use uefi_raw::protocol::network::pxe::{
1817
PxeBaseCodeDiscoverInfo, PxeBaseCodeIpFilter, PxeBaseCodeMtftpInfo, PxeBaseCodePacket,
19-
PxeBaseCodeTftpOpcode,
18+
PxeBaseCodeProtocol, PxeBaseCodeTftpOpcode,
2019
};
2120
use uefi_raw::{Boolean, Char8};
2221

@@ -27,111 +26,25 @@ pub use uefi_raw::protocol::network::pxe::{
2726

2827
/// PXE Base Code protocol
2928
#[derive(Debug)]
30-
#[repr(C)]
31-
#[unsafe_protocol("03c4e603-ac28-11d3-9a2d-0090273fc14d")]
32-
#[allow(clippy::type_complexity)]
33-
pub struct BaseCode {
34-
revision: u64,
35-
start: unsafe extern "efiapi" fn(this: &Self, use_ipv6: Boolean) -> Status,
36-
stop: unsafe extern "efiapi" fn(this: &Self) -> Status,
37-
dhcp: unsafe extern "efiapi" fn(this: &Self, sort_offers: Boolean) -> Status,
38-
discover: unsafe extern "efiapi" fn(
39-
this: &Self,
40-
ty: BootstrapType,
41-
layer: &mut u16,
42-
use_bis: Boolean,
43-
info: *const PxeBaseCodeDiscoverInfo,
44-
) -> Status,
45-
mtftp: unsafe extern "efiapi" fn(
46-
this: &Self,
47-
operation: PxeBaseCodeTftpOpcode,
48-
buffer: *mut c_void,
49-
overwrite: Boolean,
50-
buffer_size: &mut u64,
51-
block_size: *const usize,
52-
server_ip: *const uefi_raw::IpAddress,
53-
filename: *const Char8,
54-
info: *const PxeBaseCodeMtftpInfo,
55-
dont_use_buffer: Boolean,
56-
) -> Status,
57-
udp_write: unsafe extern "efiapi" fn(
58-
this: &Self,
59-
op_flags: UdpOpFlags,
60-
dest_ip: *const uefi_raw::IpAddress,
61-
dest_port: &u16,
62-
gateway_ip: *const uefi_raw::IpAddress,
63-
src_ip: *const uefi_raw::IpAddress,
64-
src_port: *mut u16,
65-
header_size: *const usize,
66-
header_ptr: *const c_void,
67-
buffer_size: &usize,
68-
buffer_ptr: *const c_void,
69-
) -> Status,
70-
udp_read: unsafe extern "efiapi" fn(
71-
this: &Self,
72-
op_flags: UdpOpFlags,
73-
dest_ip: *mut uefi_raw::IpAddress,
74-
dest_port: *mut u16,
75-
src_ip: *mut uefi_raw::IpAddress,
76-
src_port: *mut u16,
77-
header_size: *const usize,
78-
header_ptr: *mut c_void,
79-
buffer_size: &mut usize,
80-
buffer_ptr: *mut c_void,
81-
) -> Status,
82-
set_ip_filter:
83-
unsafe extern "efiapi" fn(this: &Self, new_filter: *const PxeBaseCodeIpFilter) -> Status,
84-
arp: unsafe extern "efiapi" fn(
85-
this: &Self,
86-
ip_addr: *const uefi_raw::IpAddress,
87-
mac_addr: *mut MacAddress,
88-
) -> Status,
89-
set_parameters: unsafe extern "efiapi" fn(
90-
this: &Self,
91-
new_auto_arp: *const Boolean,
92-
new_send_guid: *const Boolean,
93-
new_ttl: *const u8,
94-
new_tos: *const u8,
95-
new_make_callback: *const Boolean,
96-
) -> Status,
97-
set_station_ip: unsafe extern "efiapi" fn(
98-
this: &Self,
99-
new_station_ip: *const uefi_raw::IpAddress,
100-
new_subnet_mask: *const uefi_raw::IpAddress,
101-
) -> Status,
102-
set_packets: unsafe extern "efiapi" fn(
103-
this: &Self,
104-
new_dhcp_discover_valid: *const Boolean,
105-
new_dhcp_ack_received: *const Boolean,
106-
new_proxy_offer_received: *const Boolean,
107-
new_pxe_discover_valid: *const Boolean,
108-
new_pxe_reply_received: *const Boolean,
109-
new_pxe_bis_reply_received: *const Boolean,
110-
new_dhcp_discover: *const PxeBaseCodePacket,
111-
new_dhcp_ack: *const PxeBaseCodePacket,
112-
new_proxy_offer: *const PxeBaseCodePacket,
113-
new_pxe_discover: *const PxeBaseCodePacket,
114-
new_pxe_reply: *const PxeBaseCodePacket,
115-
new_pxe_bis_reply: *const PxeBaseCodePacket,
116-
) -> Status,
117-
mode: *const Mode,
118-
}
29+
#[repr(transparent)]
30+
#[unsafe_protocol(PxeBaseCodeProtocol::GUID)]
31+
pub struct BaseCode(PxeBaseCodeProtocol);
11932

12033
impl BaseCode {
12134
/// Enables the use of the PXE Base Code Protocol functions.
12235
pub fn start(&mut self, use_ipv6: bool) -> Result {
123-
unsafe { (self.start)(self, use_ipv6.into()) }.to_result()
36+
unsafe { (self.0.start)(&mut self.0, use_ipv6.into()) }.to_result()
12437
}
12538

12639
/// Disables the use of the PXE Base Code Protocol functions.
12740
pub fn stop(&mut self) -> Result {
128-
unsafe { (self.stop)(self) }.to_result()
41+
unsafe { (self.0.stop)(&mut self.0) }.to_result()
12942
}
13043

13144
/// Attempts to complete a DHCPv4 D.O.R.A. (discover / offer / request /
13245
/// acknowledge) or DHCPv6 S.A.R.R (solicit / advertise / request / reply) sequence.
13346
pub fn dhcp(&mut self, sort_offers: bool) -> Result {
134-
unsafe { (self.dhcp)(self, sort_offers.into()) }.to_result()
47+
unsafe { (self.0.dhcp)(&mut self.0, sort_offers.into()) }.to_result()
13548
}
13649

13750
/// Attempts to complete the PXE Boot Server and/or boot image discovery
@@ -147,16 +60,16 @@ impl BaseCode {
14760
.map(|info| ptr::from_ref(info).cast())
14861
.unwrap_or(null());
14962

150-
unsafe { (self.discover)(self, ty, layer, use_bis.into(), info) }.to_result()
63+
unsafe { (self.0.discover)(&mut self.0, ty, layer, use_bis.into(), info) }.to_result()
15164
}
15265

15366
/// Returns the size of a file located on a TFTP server.
15467
pub fn tftp_get_file_size(&mut self, server_ip: &IpAddress, filename: &CStr8) -> Result<u64> {
15568
let mut buffer_size = 0;
15669

15770
let status = unsafe {
158-
(self.mtftp)(
159-
self,
71+
(self.0.mtftp)(
72+
&mut self.0,
16073
PxeBaseCodeTftpOpcode::TFTP_GET_FILE_SIZE,
16174
null_mut(),
16275
Boolean::FALSE,
@@ -186,8 +99,8 @@ impl BaseCode {
18699
};
187100

188101
let status = unsafe {
189-
(self.mtftp)(
190-
self,
102+
(self.0.mtftp)(
103+
&mut self.0,
191104
PxeBaseCodeTftpOpcode::TFTP_READ_FILE,
192105
buffer_ptr,
193106
Boolean::FALSE,
@@ -214,8 +127,8 @@ impl BaseCode {
214127
let mut buffer_size = u64::try_from(buffer.len()).expect("buffer length should fit in u64");
215128

216129
unsafe {
217-
(self.mtftp)(
218-
self,
130+
(self.0.mtftp)(
131+
&mut self.0,
219132
PxeBaseCodeTftpOpcode::TFTP_WRITE_FILE,
220133
buffer_ptr,
221134
overwrite.into(),
@@ -242,8 +155,8 @@ impl BaseCode {
242155
let mut buffer_size = u64::try_from(buffer.len()).expect("buffer length should fit in u64");
243156

244157
let status = unsafe {
245-
(self.mtftp)(
246-
self,
158+
(self.0.mtftp)(
159+
&mut self.0,
247160
PxeBaseCodeTftpOpcode::TFTP_READ_DIRECTORY,
248161
buffer_ptr,
249162
Boolean::FALSE,
@@ -315,8 +228,8 @@ impl BaseCode {
315228
let mut buffer_size = 0;
316229

317230
let status = unsafe {
318-
(self.mtftp)(
319-
self,
231+
(self.0.mtftp)(
232+
&mut self.0,
320233
PxeBaseCodeTftpOpcode::MTFTP_GET_FILE_SIZE,
321234
null_mut(),
322235
Boolean::FALSE,
@@ -347,8 +260,8 @@ impl BaseCode {
347260
};
348261

349262
let status = unsafe {
350-
(self.mtftp)(
351-
self,
263+
(self.0.mtftp)(
264+
&mut self.0,
352265
PxeBaseCodeTftpOpcode::MTFTP_READ_FILE,
353266
buffer_ptr,
354267
Boolean::FALSE,
@@ -375,8 +288,8 @@ impl BaseCode {
375288
let mut buffer_size = u64::try_from(buffer.len()).expect("buffer length should fit in u64");
376289

377290
let status = unsafe {
378-
(self.mtftp)(
379-
self,
291+
(self.0.mtftp)(
292+
&mut self.0,
380293
PxeBaseCodeTftpOpcode::MTFTP_READ_DIRECTORY,
381294
buffer_ptr,
382295
Boolean::FALSE,
@@ -477,8 +390,8 @@ impl BaseCode {
477390
};
478391

479392
unsafe {
480-
(self.udp_write)(
481-
self,
393+
(self.0.udp_write)(
394+
&mut self.0,
482395
op_flags,
483396
dest_ip.as_raw_ptr(),
484397
&dest_port,
@@ -517,8 +430,8 @@ impl BaseCode {
517430
let mut buffer_size = buffer.len();
518431

519432
let status = unsafe {
520-
(self.udp_read)(
521-
self,
433+
(self.0.udp_read)(
434+
&mut self.0,
522435
op_flags,
523436
opt_ip_addr_to_ptr_mut(dest_ip),
524437
opt_mut_to_ptr(dest_port),
@@ -537,12 +450,13 @@ impl BaseCode {
537450
/// filtering.
538451
pub fn set_ip_filter(&mut self, new_filter: &IpFilter) -> Result {
539452
let new_filter: *const PxeBaseCodeIpFilter = ptr::from_ref(new_filter).cast();
540-
unsafe { (self.set_ip_filter)(self, new_filter) }.to_result()
453+
unsafe { (self.0.set_ip_filter)(&mut self.0, new_filter) }.to_result()
541454
}
542455

543456
/// Uses the ARP protocol to resolve a MAC address.
544457
pub fn arp(&mut self, ip_addr: &IpAddress, mac_addr: Option<&mut MacAddress>) -> Result {
545-
unsafe { (self.arp)(self, ip_addr.as_raw_ptr(), opt_mut_to_ptr(mac_addr)) }.to_result()
458+
unsafe { (self.0.arp)(&mut self.0, ip_addr.as_raw_ptr(), opt_mut_to_ptr(mac_addr)) }
459+
.to_result()
546460
}
547461

548462
/// Updates the parameters that affect the operation of the PXE Base Code
@@ -556,8 +470,8 @@ impl BaseCode {
556470
new_make_callback: Option<bool>,
557471
) -> Result {
558472
unsafe {
559-
(self.set_parameters)(
560-
self,
473+
(self.0.set_parameters)(
474+
&mut self.0,
561475
opt_bool_to_ptr(&new_auto_arp),
562476
opt_bool_to_ptr(&new_send_guid),
563477
opt_ref_to_ptr(new_ttl.as_ref()),
@@ -576,8 +490,8 @@ impl BaseCode {
576490
new_subnet_mask: Option<&IpAddress>,
577491
) -> Result {
578492
unsafe {
579-
(self.set_station_ip)(
580-
self,
493+
(self.0.set_station_ip)(
494+
&mut self.0,
581495
opt_ip_addr_to_ptr(new_station_ip),
582496
opt_ip_addr_to_ptr(new_subnet_mask),
583497
)
@@ -603,8 +517,8 @@ impl BaseCode {
603517
new_pxe_bis_reply: Option<&Packet>,
604518
) -> Result {
605519
unsafe {
606-
(self.set_packets)(
607-
self,
520+
(self.0.set_packets)(
521+
&mut self.0,
608522
opt_bool_to_ptr(&new_dhcp_discover_valid),
609523
opt_bool_to_ptr(&new_dhcp_ack_received),
610524
opt_bool_to_ptr(&new_proxy_offer_received),
@@ -625,7 +539,7 @@ impl BaseCode {
625539
/// Returns a reference to the `Mode` struct.
626540
#[must_use]
627541
pub const fn mode(&self) -> &Mode {
628-
unsafe { &*self.mode }
542+
unsafe { &*(self.0.mode.cast()) }
629543
}
630544
}
631545

0 commit comments

Comments
 (0)