Skip to content

Commit 1a70ac6

Browse files
uefi: Fix unsafe_op_in_unsafe_fn in boot module
1 parent 51e0d81 commit 1a70ac6

File tree

1 file changed

+57
-44
lines changed

1 file changed

+57
-44
lines changed

uefi/src/boot.rs

+57-44
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ pub unsafe fn raise_tpl(tpl: Tpl) -> TplGuard {
116116
let bt = unsafe { bt.as_ref() };
117117

118118
TplGuard {
119-
old_tpl: (bt.raise_tpl)(tpl),
119+
old_tpl: unsafe { (bt.raise_tpl)(tpl) },
120120
}
121121
}
122122

@@ -381,15 +381,17 @@ pub unsafe fn create_event(
381381

382382
// Safety: the argument types of the function pointers are defined
383383
// differently, but are compatible and can be safely transmuted.
384-
let notify_fn: Option<uefi_raw::table::boot::EventNotifyFn> = mem::transmute(notify_fn);
384+
let notify_fn: Option<uefi_raw::table::boot::EventNotifyFn> =
385+
unsafe { mem::transmute(notify_fn) };
385386

386387
let notify_ctx = opt_nonnull_to_ptr(notify_ctx);
387388

388389
// Now we're ready to call UEFI
389-
(bt.create_event)(event_ty, notify_tpl, notify_fn, notify_ctx, &mut event).to_result_with_val(
390-
// OK to unwrap: event is non-null for Status::SUCCESS.
391-
|| Event::from_ptr(event).unwrap(),
392-
)
390+
unsafe { (bt.create_event)(event_ty, notify_tpl, notify_fn, notify_ctx, &mut event) }
391+
.to_result_with_val(
392+
// OK to unwrap: event is non-null for Status::SUCCESS.
393+
|| unsafe { Event::from_ptr(event) }.unwrap(),
394+
)
393395
}
394396

395397
/// Creates an event in an event group.
@@ -451,19 +453,22 @@ pub unsafe fn create_event_ex(
451453

452454
// Safety: the argument types of the function pointers are defined
453455
// differently, but are compatible and can be safely transmuted.
454-
let notify_fn: Option<uefi_raw::table::boot::EventNotifyFn> = mem::transmute(notify_fn);
455-
456-
(bt.create_event_ex)(
457-
event_type,
458-
notify_tpl,
459-
notify_fn,
460-
opt_nonnull_to_ptr(notify_ctx),
461-
opt_nonnull_to_ptr(event_group),
462-
&mut event,
463-
)
456+
let notify_fn: Option<uefi_raw::table::boot::EventNotifyFn> =
457+
unsafe { mem::transmute(notify_fn) };
458+
459+
unsafe {
460+
(bt.create_event_ex)(
461+
event_type,
462+
notify_tpl,
463+
notify_fn,
464+
opt_nonnull_to_ptr(notify_ctx),
465+
opt_nonnull_to_ptr(event_group),
466+
&mut event,
467+
)
468+
}
464469
.to_result_with_val(
465470
// OK to unwrap: event is non-null for Status::SUCCESS.
466-
|| Event::from_ptr(event).unwrap(),
471+
|| unsafe { Event::from_ptr(event) }.unwrap(),
467472
)
468473
}
469474

@@ -696,13 +701,15 @@ pub unsafe fn install_protocol_interface(
696701
let bt = unsafe { bt.as_ref() };
697702

698703
let mut handle = Handle::opt_to_ptr(handle);
699-
((bt.install_protocol_interface)(
700-
&mut handle,
701-
protocol,
702-
InterfaceType::NATIVE_INTERFACE,
703-
interface,
704-
))
705-
.to_result_with_val(|| Handle::from_ptr(handle).unwrap())
704+
unsafe {
705+
(bt.install_protocol_interface)(
706+
&mut handle,
707+
protocol,
708+
InterfaceType::NATIVE_INTERFACE,
709+
interface,
710+
)
711+
}
712+
.to_result_with_val(|| unsafe { Handle::from_ptr(handle) }.unwrap())
706713
}
707714

708715
/// Reinstalls a protocol interface on a device handle. `old_interface` is replaced with `new_interface`.
@@ -730,8 +737,10 @@ pub unsafe fn reinstall_protocol_interface(
730737
let bt = boot_services_raw_panicking();
731738
let bt = unsafe { bt.as_ref() };
732739

733-
(bt.reinstall_protocol_interface)(handle.as_ptr(), protocol, old_interface, new_interface)
734-
.to_result()
740+
unsafe {
741+
(bt.reinstall_protocol_interface)(handle.as_ptr(), protocol, old_interface, new_interface)
742+
}
743+
.to_result()
735744
}
736745

737746
/// Removes a protocol interface from a device handle.
@@ -757,7 +766,7 @@ pub unsafe fn uninstall_protocol_interface(
757766
let bt = boot_services_raw_panicking();
758767
let bt = unsafe { bt.as_ref() };
759768

760-
(bt.uninstall_protocol_interface)(handle.as_ptr(), protocol, interface).to_result()
769+
unsafe { (bt.uninstall_protocol_interface)(handle.as_ptr(), protocol, interface).to_result() }
761770
}
762771

763772
/// Registers `event` to be signaled whenever a protocol interface is registered for
@@ -1035,19 +1044,21 @@ pub unsafe fn open_protocol<P: ProtocolPointer + ?Sized>(
10351044
let bt = unsafe { bt.as_ref() };
10361045

10371046
let mut interface = ptr::null_mut();
1038-
(bt.open_protocol)(
1039-
params.handle.as_ptr(),
1040-
&P::GUID,
1041-
&mut interface,
1042-
params.agent.as_ptr(),
1043-
Handle::opt_to_ptr(params.controller),
1044-
attributes as u32,
1045-
)
1047+
unsafe {
1048+
(bt.open_protocol)(
1049+
params.handle.as_ptr(),
1050+
&P::GUID,
1051+
&mut interface,
1052+
params.agent.as_ptr(),
1053+
Handle::opt_to_ptr(params.controller),
1054+
attributes as u32,
1055+
)
1056+
}
10461057
.to_result_with_val(|| {
10471058
let interface = if interface.is_null() {
10481059
None
10491060
} else {
1050-
NonNull::new(P::mut_ptr_from_ffi(interface))
1061+
NonNull::new(unsafe { P::mut_ptr_from_ffi(interface) })
10511062
};
10521063
ScopedProtocol {
10531064
interface,
@@ -1220,12 +1231,14 @@ pub unsafe fn exit(
12201231
let bt = boot_services_raw_panicking();
12211232
let bt = unsafe { bt.as_ref() };
12221233

1223-
(bt.exit)(
1224-
image_handle.as_ptr(),
1225-
exit_status,
1226-
exit_data_size,
1227-
exit_data.cast(),
1228-
)
1234+
unsafe {
1235+
(bt.exit)(
1236+
image_handle.as_ptr(),
1237+
exit_status,
1238+
exit_data_size,
1239+
exit_data.cast(),
1240+
)
1241+
}
12291242
}
12301243

12311244
/// Get the current memory map and exit boot services.
@@ -1241,7 +1254,7 @@ unsafe fn get_memory_map_and_exit_boot_services(buf: &mut [u8]) -> Result<Memory
12411254
// what boot services functions can be called. In UEFI 2.8 and earlier,
12421255
// only `get_memory_map` and `exit_boot_services` are allowed. Starting
12431256
// in UEFI 2.9 other memory allocation functions may also be called.
1244-
(bt.exit_boot_services)(image_handle().as_ptr(), memory_map.map_key.0)
1257+
unsafe { (bt.exit_boot_services)(image_handle().as_ptr(), memory_map.map_key.0) }
12451258
.to_result_with_val(|| memory_map)
12461259
}
12471260

@@ -1344,7 +1357,7 @@ pub unsafe fn install_configuration_table(
13441357
let bt = boot_services_raw_panicking();
13451358
let bt = unsafe { bt.as_ref() };
13461359

1347-
(bt.install_configuration_table)(guid_entry, table_ptr).to_result()
1360+
unsafe { (bt.install_configuration_table)(guid_entry, table_ptr) }.to_result()
13481361
}
13491362

13501363
/// Sets the watchdog timer.

0 commit comments

Comments
 (0)