Skip to content

Commit b215a92

Browse files
authored
Merge pull request #857 from nicholasbishop/bishop-bs-unsafe
uefi: Make BootServices fn ptrs unsafe
2 parents a743ac1 + 55f819b commit b215a92

File tree

1 file changed

+47
-36
lines changed

1 file changed

+47
-36
lines changed

uefi/src/table/boot.rs

Lines changed: 47 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -98,23 +98,26 @@ pub struct BootServices {
9898
restore_tpl: unsafe extern "efiapi" fn(old_tpl: Tpl),
9999

100100
// Memory allocation functions
101-
allocate_pages: extern "efiapi" fn(
101+
allocate_pages: unsafe extern "efiapi" fn(
102102
alloc_ty: u32,
103103
mem_ty: MemoryType,
104104
count: usize,
105105
addr: &mut PhysicalAddress,
106106
) -> Status,
107-
free_pages: extern "efiapi" fn(addr: PhysicalAddress, pages: usize) -> Status,
107+
free_pages: unsafe extern "efiapi" fn(addr: PhysicalAddress, pages: usize) -> Status,
108108
get_memory_map: unsafe extern "efiapi" fn(
109109
size: &mut usize,
110110
map: *mut MemoryDescriptor,
111111
key: &mut MemoryMapKey,
112112
desc_size: &mut usize,
113113
desc_version: &mut u32,
114114
) -> Status,
115-
allocate_pool:
116-
extern "efiapi" fn(pool_type: MemoryType, size: usize, buffer: &mut *mut u8) -> Status,
117-
free_pool: extern "efiapi" fn(buffer: *mut u8) -> Status,
115+
allocate_pool: unsafe extern "efiapi" fn(
116+
pool_type: MemoryType,
117+
size: usize,
118+
buffer: &mut *mut u8,
119+
) -> Status,
120+
free_pool: unsafe extern "efiapi" fn(buffer: *mut u8) -> Status,
118121

119122
// Event & timer functions
120123
create_event: unsafe extern "efiapi" fn(
@@ -130,7 +133,7 @@ pub struct BootServices {
130133
events: *mut Event,
131134
out_index: *mut usize,
132135
) -> Status,
133-
signal_event: extern "efiapi" fn(event: Event) -> Status,
136+
signal_event: unsafe extern "efiapi" fn(event: Event) -> Status,
134137
close_event: unsafe extern "efiapi" fn(event: Event) -> Status,
135138
check_event: unsafe extern "efiapi" fn(event: Event) -> Status,
136139

@@ -153,10 +156,13 @@ pub struct BootServices {
153156
interface: *mut c_void,
154157
) -> Status,
155158
#[deprecated = "open_protocol and open_protocol_exclusive are better alternatives and available since EFI 1.10 (2002)"]
156-
handle_protocol:
157-
extern "efiapi" fn(handle: Handle, proto: &Guid, out_proto: &mut *mut c_void) -> Status,
159+
handle_protocol: unsafe extern "efiapi" fn(
160+
handle: Handle,
161+
proto: &Guid,
162+
out_proto: &mut *mut c_void,
163+
) -> Status,
158164
_reserved: usize,
159-
register_protocol_notify: extern "efiapi" fn(
165+
register_protocol_notify: unsafe extern "efiapi" fn(
160166
protocol: &Guid,
161167
event: Event,
162168
registration: *mut Option<ProtocolSearchKey>,
@@ -174,7 +180,7 @@ pub struct BootServices {
174180
out_handle: &mut Option<Handle>,
175181
) -> Status,
176182
install_configuration_table:
177-
extern "efiapi" fn(guid_entry: &Guid, table_ptr: *const c_void) -> Status,
183+
unsafe extern "efiapi" fn(guid_entry: &Guid, table_ptr: *const c_void) -> Status,
178184

179185
// Image services
180186
load_image: unsafe extern "efiapi" fn(
@@ -190,19 +196,19 @@ pub struct BootServices {
190196
exit_data_size: *mut usize,
191197
exit_data: &mut *mut Char16,
192198
) -> Status,
193-
exit: extern "efiapi" fn(
199+
exit: unsafe extern "efiapi" fn(
194200
image_handle: Handle,
195201
exit_status: Status,
196202
exit_data_size: usize,
197203
exit_data: *mut Char16,
198204
) -> !,
199-
unload_image: extern "efiapi" fn(image_handle: Handle) -> Status,
205+
unload_image: unsafe extern "efiapi" fn(image_handle: Handle) -> Status,
200206
exit_boot_services:
201207
unsafe extern "efiapi" fn(image_handle: Handle, map_key: MemoryMapKey) -> Status,
202208

203209
// Misc services
204210
get_next_monotonic_count: usize,
205-
stall: extern "efiapi" fn(microseconds: usize) -> Status,
211+
stall: unsafe extern "efiapi" fn(microseconds: usize) -> Status,
206212
set_watchdog_timer: unsafe extern "efiapi" fn(
207213
timeout: usize,
208214
watchdog_code: u64,
@@ -224,15 +230,15 @@ pub struct BootServices {
224230
) -> Status,
225231

226232
// Protocol open / close services
227-
open_protocol: extern "efiapi" fn(
233+
open_protocol: unsafe extern "efiapi" fn(
228234
handle: Handle,
229235
protocol: &Guid,
230236
interface: &mut *mut c_void,
231237
agent_handle: Handle,
232238
controller_handle: Option<Handle>,
233239
attributes: u32,
234240
) -> Status,
235-
close_protocol: extern "efiapi" fn(
241+
close_protocol: unsafe extern "efiapi" fn(
236242
handle: Handle,
237243
protocol: &Guid,
238244
agent_handle: Handle,
@@ -254,7 +260,7 @@ pub struct BootServices {
254260
buf: &mut *mut Handle,
255261
) -> Status,
256262
#[deprecated = "open_protocol and open_protocol_exclusive are better alternatives and available since EFI 1.10 (2002)"]
257-
locate_protocol: extern "efiapi" fn(
263+
locate_protocol: unsafe extern "efiapi" fn(
258264
proto: &Guid,
259265
registration: *mut c_void,
260266
out_proto: &mut *mut c_void,
@@ -367,7 +373,7 @@ impl BootServices {
367373
AllocateType::MaxAddress(addr) => (1, addr),
368374
AllocateType::Address(addr) => (2, addr),
369375
};
370-
(self.allocate_pages)(ty, mem_ty, count, &mut addr).to_result_with_val(|| addr)
376+
unsafe { (self.allocate_pages)(ty, mem_ty, count, &mut addr) }.to_result_with_val(|| addr)
371377
}
372378

373379
/// Frees memory pages allocated by UEFI.
@@ -379,7 +385,7 @@ impl BootServices {
379385
/// * [`uefi::Status::NOT_FOUND`]
380386
/// * [`uefi::Status::INVALID_PARAMETER`]
381387
pub fn free_pages(&self, addr: PhysicalAddress, count: usize) -> Result {
382-
(self.free_pages)(addr, count).to_result()
388+
unsafe { (self.free_pages)(addr, count) }.to_result()
383389
}
384390

385391
/// Returns struct which contains the size of a single memory descriptor
@@ -476,7 +482,7 @@ impl BootServices {
476482
/// * [`uefi::Status::INVALID_PARAMETER`]
477483
pub fn allocate_pool(&self, mem_ty: MemoryType, size: usize) -> Result<*mut u8> {
478484
let mut buffer = ptr::null_mut();
479-
(self.allocate_pool)(mem_ty, size, &mut buffer).to_result_with_val(|| buffer)
485+
unsafe { (self.allocate_pool)(mem_ty, size, &mut buffer) }.to_result_with_val(|| buffer)
480486
}
481487

482488
/// Frees memory allocated from a pool.
@@ -486,8 +492,9 @@ impl BootServices {
486492
/// See section `EFI_BOOT_SERVICES.FreePool()` in the UEFI Specification for more details.
487493
///
488494
/// * [`uefi::Status::INVALID_PARAMETER`]
495+
#[allow(clippy::not_unsafe_ptr_arg_deref)]
489496
pub fn free_pool(&self, addr: *mut u8) -> Result {
490-
(self.free_pool)(addr).to_result()
497+
unsafe { (self.free_pool)(addr) }.to_result()
491498
}
492499

493500
/// Creates an event
@@ -1068,7 +1075,7 @@ impl BootServices {
10681075
/// * [`uefi::Status::UNSUPPORTED`]
10691076
/// * [`uefi::Status::INVALID_PARAMETER`]
10701077
pub fn unload_image(&self, image_handle: Handle) -> Result {
1071-
(self.unload_image)(image_handle).to_result()
1078+
unsafe { (self.unload_image)(image_handle) }.to_result()
10721079
}
10731080

10741081
/// Transfer control to a loaded image's entry point.
@@ -1139,7 +1146,7 @@ impl BootServices {
11391146
///
11401147
/// The time is in microseconds.
11411148
pub fn stall(&self, time: usize) {
1142-
assert_eq!((self.stall)(time), Status::SUCCESS);
1149+
assert_eq!(unsafe { (self.stall)(time) }, Status::SUCCESS);
11431150
}
11441151

11451152
/// Adds, updates, or removes a configuration table entry
@@ -1389,14 +1396,16 @@ impl BootServices {
13891396
) -> Result<()> {
13901397
const TEST_PROTOCOL: u32 = 0x04;
13911398
let mut interface = ptr::null_mut();
1392-
(self.open_protocol)(
1393-
params.handle,
1394-
&P::GUID,
1395-
&mut interface,
1396-
params.agent,
1397-
params.controller,
1398-
TEST_PROTOCOL,
1399-
)
1399+
unsafe {
1400+
(self.open_protocol)(
1401+
params.handle,
1402+
&P::GUID,
1403+
&mut interface,
1404+
params.agent,
1405+
params.controller,
1406+
TEST_PROTOCOL,
1407+
)
1408+
}
14001409
.to_result_with_val(|| ())
14011410
}
14021411

@@ -1818,12 +1827,14 @@ pub struct ScopedProtocol<'a, P: Protocol + ?Sized> {
18181827

18191828
impl<'a, P: Protocol + ?Sized> Drop for ScopedProtocol<'a, P> {
18201829
fn drop(&mut self) {
1821-
let status = (self.boot_services.close_protocol)(
1822-
self.open_params.handle,
1823-
&P::GUID,
1824-
self.open_params.agent,
1825-
self.open_params.controller,
1826-
);
1830+
let status = unsafe {
1831+
(self.boot_services.close_protocol)(
1832+
self.open_params.handle,
1833+
&P::GUID,
1834+
self.open_params.agent,
1835+
self.open_params.controller,
1836+
)
1837+
};
18271838
// All of the error cases for close_protocol boil down to
18281839
// calling it with a different set of parameters than what was
18291840
// passed to open_protocol. The public API prevents such errors,

0 commit comments

Comments
 (0)