Skip to content

Commit 3ccdf54

Browse files
uefi: BootServices: Make remaining handle args compatible with uefi_raw
1 parent b66076f commit 3ccdf54

File tree

2 files changed

+38
-28
lines changed

2 files changed

+38
-28
lines changed

uefi/src/data_types/mod.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
//! This module defines the basic data types that are used throughout uefi-rs
44
55
use core::ffi::c_void;
6-
use core::ptr::NonNull;
6+
use core::ptr::{self, NonNull};
77

88
/// Opaque handle to an UEFI entity (protocol, image...), guaranteed to be non-null.
99
///
@@ -42,6 +42,10 @@ impl Handle {
4242
pub fn as_ptr(&self) -> *mut c_void {
4343
self.0.as_ptr()
4444
}
45+
46+
pub(crate) fn opt_to_ptr(handle: Option<Self>) -> *mut c_void {
47+
handle.map(|h| h.0.as_ptr()).unwrap_or(ptr::null_mut())
48+
}
4549
}
4650

4751
/// Handle to an event structure, guaranteed to be non-null.

uefi/src/table/boot.rs

Lines changed: 33 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ struct BootServicesInternal {
9494

9595
// Protocol handlers
9696
install_protocol_interface: unsafe extern "efiapi" fn(
97-
handle: &mut Option<Handle>,
97+
handle: *mut uefi_raw::Handle,
9898
guid: &Guid,
9999
interface_type: InterfaceType,
100100
interface: *mut c_void,
@@ -127,12 +127,12 @@ struct BootServicesInternal {
127127
proto: Option<&Guid>,
128128
key: Option<ProtocolSearchKey>,
129129
buf_sz: &mut usize,
130-
buf: *mut MaybeUninit<Handle>,
130+
buf: *mut uefi_raw::Handle,
131131
) -> Status,
132132
locate_device_path: unsafe extern "efiapi" fn(
133133
proto: &Guid,
134134
device_path: *mut *const uefi_raw::protocol::device_path::DevicePathProtocol,
135-
out_handle: &mut Option<Handle>,
135+
out_handle: *mut uefi_raw::Handle,
136136
) -> Status,
137137
install_configuration_table:
138138
unsafe extern "efiapi" fn(guid_entry: &Guid, table_ptr: *const c_void) -> Status,
@@ -144,7 +144,7 @@ struct BootServicesInternal {
144144
device_path: *const uefi_raw::protocol::device_path::DevicePathProtocol,
145145
source_buffer: *const u8,
146146
source_size: usize,
147-
image_handle: &mut Option<Handle>,
147+
image_handle: *mut uefi_raw::Handle,
148148
) -> Status,
149149
start_image: unsafe extern "efiapi" fn(
150150
image_handle: uefi_raw::Handle,
@@ -174,14 +174,14 @@ struct BootServicesInternal {
174174
// Driver support services
175175
connect_controller: unsafe extern "efiapi" fn(
176176
controller: uefi_raw::Handle,
177-
driver_image: Option<Handle>,
177+
driver_image: uefi_raw::Handle,
178178
remaining_device_path: *const uefi_raw::protocol::device_path::DevicePathProtocol,
179179
recursive: bool,
180180
) -> Status,
181181
disconnect_controller: unsafe extern "efiapi" fn(
182182
controller: uefi_raw::Handle,
183-
driver_image: Option<Handle>,
184-
child: Option<Handle>,
183+
driver_image: uefi_raw::Handle,
184+
child: uefi_raw::Handle,
185185
) -> Status,
186186

187187
// Protocol open / close services
@@ -190,14 +190,14 @@ struct BootServicesInternal {
190190
protocol: &Guid,
191191
interface: &mut *mut c_void,
192192
agent_handle: uefi_raw::Handle,
193-
controller_handle: Option<Handle>,
193+
controller_handle: uefi_raw::Handle,
194194
attributes: u32,
195195
) -> Status,
196196
close_protocol: unsafe extern "efiapi" fn(
197197
handle: uefi_raw::Handle,
198198
protocol: &Guid,
199199
agent_handle: uefi_raw::Handle,
200-
controller_handle: Option<Handle>,
200+
controller_handle: uefi_raw::Handle,
201201
) -> Status,
202202
open_protocol_information: usize,
203203

@@ -212,7 +212,7 @@ struct BootServicesInternal {
212212
proto: Option<&Guid>,
213213
key: Option<ProtocolSearchKey>,
214214
no_handles: &mut usize,
215-
buf: &mut *mut Handle,
215+
buf: *mut *mut uefi_raw::Handle,
216216
) -> Status,
217217
#[deprecated = "open_protocol and open_protocol_exclusive are better alternatives and available since EFI 1.10 (2002)"]
218218
locate_protocol: unsafe extern "efiapi" fn(
@@ -756,19 +756,18 @@ impl BootServices {
756756
/// * [`uefi::Status::INVALID_PARAMETER`]
757757
pub unsafe fn install_protocol_interface(
758758
&self,
759-
mut handle: Option<Handle>,
759+
handle: Option<Handle>,
760760
protocol: &Guid,
761761
interface: *mut c_void,
762762
) -> Result<Handle> {
763+
let mut handle = Handle::opt_to_ptr(handle);
763764
((self.0.install_protocol_interface)(
764765
&mut handle,
765766
protocol,
766767
InterfaceType::NATIVE_INTERFACE,
767768
interface,
768769
))
769-
// this `unwrapped_unchecked` is safe, `handle` is guaranteed to be Some() if this call is
770-
// successful
771-
.to_result_with_val(|| handle.unwrap_unchecked())
770+
.to_result_with_val(|| Handle::from_ptr(handle).unwrap())
772771
}
773772

774773
/// Reinstalls a protocol interface on a device handle. `old_interface` is replaced with `new_interface`.
@@ -903,7 +902,8 @@ impl BootServices {
903902
SearchType::ByProtocol(guid) => (2, Some(guid), None),
904903
};
905904

906-
let status = unsafe { (self.0.locate_handle)(ty, guid, key, &mut buffer_size, buffer) };
905+
let status =
906+
unsafe { (self.0.locate_handle)(ty, guid, key, &mut buffer_size, buffer.cast()) };
907907

908908
// Must convert the returned size (in bytes) to length (number of elements).
909909
let buffer_len = buffer_size / handle_size;
@@ -935,15 +935,15 @@ impl BootServices {
935935
&self,
936936
device_path: &mut &DevicePath,
937937
) -> Result<Handle> {
938-
let mut handle = None;
938+
let mut handle = ptr::null_mut();
939939
let mut device_path_ptr: *const uefi_raw::protocol::device_path::DevicePathProtocol =
940940
device_path.as_ffi_ptr().cast();
941941
unsafe {
942942
(self.0.locate_device_path)(&P::GUID, &mut device_path_ptr, &mut handle)
943943
.to_result_with_val(|| {
944944
*device_path = DevicePath::from_ffi_ptr(device_path_ptr.cast());
945945
// OK to unwrap: handle is non-null for Status::SUCCESS.
946-
handle.unwrap()
946+
Handle::from_ptr(handle).unwrap()
947947
})
948948
}
949949
}
@@ -1056,7 +1056,7 @@ impl BootServices {
10561056
}
10571057
};
10581058

1059-
let mut image_handle = None;
1059+
let mut image_handle = ptr::null_mut();
10601060
unsafe {
10611061
(self.0.load_image)(
10621062
boot_policy,
@@ -1068,7 +1068,7 @@ impl BootServices {
10681068
)
10691069
.to_result_with_val(
10701070
// OK to unwrap: image handle is non-null for Status::SUCCESS.
1071-
|| image_handle.unwrap(),
1071+
|| Handle::from_ptr(image_handle).unwrap(),
10721072
)
10731073
}
10741074
}
@@ -1268,7 +1268,7 @@ impl BootServices {
12681268
unsafe {
12691269
(self.0.connect_controller)(
12701270
controller.as_ptr(),
1271-
driver_image,
1271+
Handle::opt_to_ptr(driver_image),
12721272
remaining_device_path
12731273
.map(|dp| dp.as_ffi_ptr())
12741274
.unwrap_or(ptr::null())
@@ -1296,8 +1296,14 @@ impl BootServices {
12961296
driver_image: Option<Handle>,
12971297
child: Option<Handle>,
12981298
) -> Result {
1299-
unsafe { (self.0.disconnect_controller)(controller.as_ptr(), driver_image, child) }
1300-
.to_result_with_err(|_| ())
1299+
unsafe {
1300+
(self.0.disconnect_controller)(
1301+
controller.as_ptr(),
1302+
Handle::opt_to_ptr(driver_image),
1303+
Handle::opt_to_ptr(child),
1304+
)
1305+
}
1306+
.to_result_with_err(|_| ())
13011307
}
13021308

13031309
/// Open a protocol interface for a handle.
@@ -1353,7 +1359,7 @@ impl BootServices {
13531359
&P::GUID,
13541360
&mut interface,
13551361
params.agent.as_ptr(),
1356-
params.controller,
1362+
Handle::opt_to_ptr(params.controller),
13571363
attributes as u32,
13581364
)
13591365
.to_result_with_val(|| {
@@ -1424,7 +1430,7 @@ impl BootServices {
14241430
&P::GUID,
14251431
&mut interface,
14261432
params.agent.as_ptr(),
1427-
params.controller,
1433+
Handle::opt_to_ptr(params.controller),
14281434
TEST_PROTOCOL,
14291435
)
14301436
}
@@ -1479,7 +1485,7 @@ impl BootServices {
14791485
/// * [`uefi::Status::OUT_OF_RESOURCES`]
14801486
pub fn locate_handle_buffer(&self, search_ty: SearchType) -> Result<HandleBuffer> {
14811487
let mut num_handles: usize = 0;
1482-
let mut buffer: *mut Handle = ptr::null_mut();
1488+
let mut buffer: *mut uefi_raw::Handle = ptr::null_mut();
14831489

14841490
// Obtain the needed data from the parameters.
14851491
let (ty, guid, key) = match search_ty {
@@ -1492,7 +1498,7 @@ impl BootServices {
14921498
.to_result_with_val(|| HandleBuffer {
14931499
boot_services: self,
14941500
count: num_handles,
1495-
buffer,
1501+
buffer: buffer.cast(),
14961502
})
14971503
}
14981504

@@ -1872,7 +1878,7 @@ impl<'a, P: Protocol + ?Sized> Drop for ScopedProtocol<'a, P> {
18721878
self.open_params.handle.as_ptr(),
18731879
&P::GUID,
18741880
self.open_params.agent.as_ptr(),
1875-
self.open_params.controller,
1881+
Handle::opt_to_ptr(self.open_params.controller),
18761882
)
18771883
};
18781884
// All of the error cases for close_protocol boil down to

0 commit comments

Comments
 (0)