Skip to content

Commit b8d1460

Browse files
uefi: Remove use of Option in definition of pxe::BaseCode
This is an intermediate step towards using the uefi-raw definition of the protocol.
1 parent df20a84 commit b8d1460

File tree

1 file changed

+35
-25
lines changed

1 file changed

+35
-25
lines changed

uefi/src/proto/network/pxe.rs

Lines changed: 35 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ pub struct BaseCode {
4848
buffer: *mut c_void,
4949
overwrite: Boolean,
5050
buffer_size: &mut u64,
51-
block_size: Option<&usize>,
51+
block_size: *const usize,
5252
server_ip: *const uefi_raw::IpAddress,
5353
filename: *const Char8,
5454
info: *const PxeBaseCodeMtftpInfo,
@@ -61,8 +61,8 @@ pub struct BaseCode {
6161
dest_port: &u16,
6262
gateway_ip: *const uefi_raw::IpAddress,
6363
src_ip: *const uefi_raw::IpAddress,
64-
src_port: Option<&mut u16>,
65-
header_size: Option<&usize>,
64+
src_port: *mut u16,
65+
header_size: *const usize,
6666
header_ptr: *const c_void,
6767
buffer_size: &usize,
6868
buffer_ptr: *const c_void,
@@ -71,10 +71,10 @@ pub struct BaseCode {
7171
this: &Self,
7272
op_flags: UdpOpFlags,
7373
dest_ip: *mut uefi_raw::IpAddress,
74-
dest_port: Option<&mut u16>,
74+
dest_port: *mut u16,
7575
src_ip: *mut uefi_raw::IpAddress,
76-
src_port: Option<&mut u16>,
77-
header_size: Option<&usize>,
76+
src_port: *mut u16,
77+
header_size: *const usize,
7878
header_ptr: *mut c_void,
7979
buffer_size: &mut usize,
8080
buffer_ptr: *mut c_void,
@@ -84,14 +84,14 @@ pub struct BaseCode {
8484
arp: unsafe extern "efiapi" fn(
8585
this: &Self,
8686
ip_addr: *const uefi_raw::IpAddress,
87-
mac_addr: Option<&mut MacAddress>,
87+
mac_addr: *mut MacAddress,
8888
) -> Status,
8989
set_parameters: unsafe extern "efiapi" fn(
9090
this: &Self,
9191
new_auto_arp: *const Boolean,
9292
new_send_guid: *const Boolean,
93-
new_ttl: Option<&u8>,
94-
new_tos: Option<&u8>,
93+
new_ttl: *const u8,
94+
new_tos: *const u8,
9595
new_make_callback: *const Boolean,
9696
) -> Status,
9797
set_station_ip: unsafe extern "efiapi" fn(
@@ -161,7 +161,7 @@ impl BaseCode {
161161
null_mut(),
162162
Boolean::FALSE,
163163
&mut buffer_size,
164-
None,
164+
null(),
165165
server_ip.as_raw_ptr(),
166166
cstr8_to_ptr(filename),
167167
null(),
@@ -192,7 +192,7 @@ impl BaseCode {
192192
buffer_ptr,
193193
Boolean::FALSE,
194194
&mut buffer_size,
195-
None,
195+
null(),
196196
server_ip.as_raw_ptr(),
197197
cstr8_to_ptr(filename),
198198
null(),
@@ -220,7 +220,7 @@ impl BaseCode {
220220
buffer_ptr,
221221
overwrite.into(),
222222
&mut buffer_size,
223-
None,
223+
null(),
224224
server_ip.as_raw_ptr(),
225225
cstr8_to_ptr(filename),
226226
null(),
@@ -248,7 +248,7 @@ impl BaseCode {
248248
buffer_ptr,
249249
Boolean::FALSE,
250250
&mut buffer_size,
251-
None,
251+
null(),
252252
server_ip.as_raw_ptr(),
253253
cstr8_to_ptr(directory_name),
254254
null(),
@@ -321,7 +321,7 @@ impl BaseCode {
321321
null_mut(),
322322
Boolean::FALSE,
323323
&mut buffer_size,
324-
None,
324+
null(),
325325
server_ip.as_raw_ptr(),
326326
cstr8_to_ptr(filename),
327327
info.as_raw_ptr(),
@@ -353,7 +353,7 @@ impl BaseCode {
353353
buffer_ptr,
354354
Boolean::FALSE,
355355
&mut buffer_size,
356-
None,
356+
null(),
357357
server_ip.as_raw_ptr(),
358358
cstr8_to_ptr(filename),
359359
info.as_raw_ptr(),
@@ -381,7 +381,7 @@ impl BaseCode {
381381
buffer_ptr,
382382
Boolean::FALSE,
383383
&mut buffer_size,
384-
None,
384+
null(),
385385
server_ip.as_raw_ptr(),
386386
null_mut(),
387387
info.as_raw_ptr(),
@@ -484,8 +484,8 @@ impl BaseCode {
484484
&dest_port,
485485
opt_ip_addr_to_ptr(gateway_ip),
486486
opt_ip_addr_to_ptr(src_ip),
487-
src_port,
488-
header_size,
487+
opt_mut_to_ptr(src_port),
488+
opt_ref_to_ptr(header_size),
489489
header_ptr,
490490
&buffer.len(),
491491
buffer.as_ptr().cast(),
@@ -509,9 +509,9 @@ impl BaseCode {
509509
let header_size_tmp;
510510
let (header_size, header_ptr) = if let Some(header) = header {
511511
header_size_tmp = header.len();
512-
(Some(&header_size_tmp), header.as_mut_ptr().cast())
512+
(ptr::from_ref(&header_size_tmp), header.as_mut_ptr().cast())
513513
} else {
514-
(None, null_mut())
514+
(null(), null_mut())
515515
};
516516

517517
let mut buffer_size = buffer.len();
@@ -521,9 +521,9 @@ impl BaseCode {
521521
self,
522522
op_flags,
523523
opt_ip_addr_to_ptr_mut(dest_ip),
524-
dest_port,
524+
opt_mut_to_ptr(dest_port),
525525
opt_ip_addr_to_ptr_mut(src_ip),
526-
src_port,
526+
opt_mut_to_ptr(src_port),
527527
header_size,
528528
header_ptr,
529529
&mut buffer_size,
@@ -542,7 +542,7 @@ impl BaseCode {
542542

543543
/// Uses the ARP protocol to resolve a MAC address.
544544
pub fn arp(&mut self, ip_addr: &IpAddress, mac_addr: Option<&mut MacAddress>) -> Result {
545-
unsafe { (self.arp)(self, ip_addr.as_raw_ptr(), mac_addr) }.to_result()
545+
unsafe { (self.arp)(self, ip_addr.as_raw_ptr(), opt_mut_to_ptr(mac_addr)) }.to_result()
546546
}
547547

548548
/// Updates the parameters that affect the operation of the PXE Base Code
@@ -560,8 +560,8 @@ impl BaseCode {
560560
self,
561561
opt_bool_to_ptr(&new_auto_arp),
562562
opt_bool_to_ptr(&new_send_guid),
563-
new_ttl.as_ref(),
564-
new_tos.as_ref(),
563+
opt_ref_to_ptr(new_ttl.as_ref()),
564+
opt_ref_to_ptr(new_tos.as_ref()),
565565
opt_bool_to_ptr(&new_make_callback),
566566
)
567567
}
@@ -658,6 +658,16 @@ fn opt_packet_to_ptr(arg: Option<&Packet>) -> *const PxeBaseCodePacket {
658658
arg.map(|p| ptr::from_ref(p).cast()).unwrap_or_else(null)
659659
}
660660

661+
/// Convert an `Option<&T>` to a `*const T`.
662+
fn opt_ref_to_ptr<T>(opt: Option<&T>) -> *const T {
663+
opt.map(ptr::from_ref).unwrap_or(null())
664+
}
665+
666+
/// Convert an `Option<&mut T>` to a `*mut T`.
667+
fn opt_mut_to_ptr<T>(opt: Option<&mut T>) -> *mut T {
668+
opt.map(ptr::from_mut).unwrap_or(null_mut())
669+
}
670+
661671
opaque_type! {
662672
/// Opaque type that should be used to represent a pointer to a [`DiscoverInfo`] in
663673
/// foreign function interfaces. This type produces a thin pointer, unlike

0 commit comments

Comments
 (0)