Skip to content

Commit f5fdce0

Browse files
authored
Implement Deref and DerefMut for ScopedProtocol (#434)
1 parent 00b56d8 commit f5fdce0

File tree

9 files changed

+32
-23
lines changed

9 files changed

+32
-23
lines changed

src/table/boot.rs

+26-6
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use core::cell::UnsafeCell;
1414
use core::ffi::c_void;
1515
use core::fmt::{Debug, Formatter};
1616
use core::mem::{self, MaybeUninit};
17+
use core::ops::{Deref, DerefMut};
1718
use core::ptr::NonNull;
1819
use core::{ptr, slice};
1920

@@ -884,6 +885,8 @@ impl BootServices {
884885
)
885886
.into_with_val(|| unsafe {
886887
let interface = P::mut_ptr_from_ffi(interface) as *const UnsafeCell<P>;
888+
889+
#[allow(deprecated)]
887890
ScopedProtocol {
888891
interface: &*interface,
889892
open_params: params,
@@ -1030,21 +1033,17 @@ impl BootServices {
10301033
},
10311034
OpenProtocolAttributes::Exclusive,
10321035
)?;
1033-
let loaded_image = unsafe { &*loaded_image.interface.get() };
1034-
1035-
let device_handle = loaded_image.device();
10361036

10371037
let device_path = self.open_protocol::<DevicePath>(
10381038
OpenProtocolParams {
1039-
handle: device_handle,
1039+
handle: loaded_image.device(),
10401040
agent: image_handle,
10411041
controller: None,
10421042
},
10431043
OpenProtocolAttributes::Exclusive,
10441044
)?;
1045-
let mut device_path = unsafe { &*device_path.interface.get() };
10461045

1047-
let device_handle = self.locate_device_path::<SimpleFileSystem>(&mut device_path)?;
1046+
let device_handle = self.locate_device_path::<SimpleFileSystem>(&mut &*device_path)?;
10481047

10491048
self.open_protocol::<SimpleFileSystem>(
10501049
OpenProtocolParams {
@@ -1330,6 +1329,7 @@ pub struct OpenProtocolParams {
13301329
/// protocol and why [`UnsafeCell`] is used.
13311330
pub struct ScopedProtocol<'a, P: Protocol + ?Sized> {
13321331
/// The protocol interface.
1332+
#[deprecated(since = "0.16.0", note = "use Deref and DerefMut instead")]
13331333
pub interface: &'a UnsafeCell<P>,
13341334

13351335
open_params: OpenProtocolParams,
@@ -1353,6 +1353,26 @@ impl<'a, P: Protocol + ?Sized> Drop for ScopedProtocol<'a, P> {
13531353
}
13541354
}
13551355

1356+
impl<'a, P: Protocol + ?Sized> Deref for ScopedProtocol<'a, P> {
1357+
type Target = P;
1358+
1359+
fn deref(&self) -> &Self::Target {
1360+
#[allow(deprecated)]
1361+
unsafe {
1362+
&*self.interface.get()
1363+
}
1364+
}
1365+
}
1366+
1367+
impl<'a, P: Protocol + ?Sized> DerefMut for ScopedProtocol<'a, P> {
1368+
fn deref_mut(&mut self) -> &mut Self::Target {
1369+
#[allow(deprecated)]
1370+
unsafe {
1371+
&mut *self.interface.get()
1372+
}
1373+
}
1374+
}
1375+
13561376
/// Type of allocation to perform.
13571377
#[derive(Debug, Copy, Clone)]
13581378
pub enum AllocateType {

uefi-test-runner/src/main.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ fn check_screenshot(image: Handle, bt: &BootServices, name: &str) {
8686
.get(1)
8787
.expect("Second serial device is missing");
8888

89-
let serial = bt
89+
let mut serial = bt
9090
.open_protocol::<Serial>(
9191
OpenProtocolParams {
9292
handle: serial_handle,
@@ -96,7 +96,6 @@ fn check_screenshot(image: Handle, bt: &BootServices, name: &str) {
9696
OpenProtocolAttributes::Exclusive,
9797
)
9898
.expect("Could not open serial protocol");
99-
let serial = unsafe { &mut *serial.interface.get() };
10099

101100
// Set a large timeout to avoid problems with Travis
102101
let mut io_mode = *serial.io_mode();

uefi-test-runner/src/proto/debug.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,14 @@ pub fn test(image: Handle, bt: &BootServices) {
77
info!("Running UEFI debug connection protocol test");
88
if let Ok(handles) = bt.find_handles::<DebugSupport>() {
99
for handle in handles {
10-
if let Ok(debug_support) = bt.open_protocol::<DebugSupport>(
10+
if let Ok(mut debug_support) = bt.open_protocol::<DebugSupport>(
1111
OpenProtocolParams {
1212
handle,
1313
agent: image,
1414
controller: None,
1515
},
1616
OpenProtocolAttributes::Exclusive,
1717
) {
18-
let debug_support = unsafe { &mut *debug_support.interface.get() };
19-
2018
// make sure that the max processor index is a sane value, i.e. it works
2119
let maximum_processor_index = debug_support.get_maximum_processor_index();
2220
assert_ne!(
@@ -94,7 +92,7 @@ pub fn test(image: Handle, bt: &BootServices) {
9492
_ => unreachable!(),
9593
}
9694

97-
test_invalidate_instruction_cache(debug_support);
95+
test_invalidate_instruction_cache(&mut *debug_support);
9896
}
9997
}
10098
} else {

uefi-test-runner/src/proto/device_path.rs

-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ pub fn test(image: Handle, bt: &BootServices) {
1616
OpenProtocolAttributes::Exclusive,
1717
)
1818
.expect("Failed to open LoadedImage protocol");
19-
let loaded_image = unsafe { &*loaded_image.interface.get() };
2019

2120
let device_path = bt
2221
.open_protocol::<DevicePath>(
@@ -28,7 +27,6 @@ pub fn test(image: Handle, bt: &BootServices) {
2827
OpenProtocolAttributes::Exclusive,
2928
)
3029
.expect("Failed to open DevicePath protocol");
31-
let device_path = unsafe { &*device_path.interface.get() };
3230

3331
let device_path_to_text = bt
3432
.locate_protocol::<DevicePathToText>()

uefi-test-runner/src/proto/loaded_image.rs

-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ pub fn test(image: Handle, bt: &BootServices) {
1515
OpenProtocolAttributes::Exclusive,
1616
)
1717
.expect("Failed to open LoadedImage protocol");
18-
let loaded_image = unsafe { &*loaded_image.interface.get() };
1918

2019
let load_options = loaded_image.load_options_as_bytes();
2120
info!("LoadedImage options: {:?}", load_options);

uefi-test-runner/src/proto/media/known_disk.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ pub fn test_known_disk(image: Handle, bt: &BootServices) {
154154

155155
let mut found_test_disk = false;
156156
for handle in handles {
157-
let sfs = bt
157+
let mut sfs = bt
158158
.open_protocol::<SimpleFileSystem>(
159159
OpenProtocolParams {
160160
handle,
@@ -164,7 +164,6 @@ pub fn test_known_disk(image: Handle, bt: &BootServices) {
164164
OpenProtocolAttributes::Exclusive,
165165
)
166166
.expect("Failed to get simple file system");
167-
let sfs = unsafe { &mut *sfs.interface.get() };
168167
let mut directory = sfs.open_volume().unwrap();
169168

170169
let mut fs_info_buf = vec![0; 128];

uefi-test-runner/src/proto/media/mod.rs

-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@ pub fn test(image: Handle, bt: &BootServices) {
7272
OpenProtocolAttributes::Exclusive,
7373
)
7474
.expect("Failed to get partition info");
75-
let pi = unsafe { &*pi.interface.get() };
7675

7776
if let Some(mbr) = pi.mbr_partition_record() {
7877
info!("MBR partition: {:?}", mbr);

uefi-test-runner/src/proto/network.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ pub fn test(image: Handle, bt: &BootServices) {
1313

1414
if let Ok(handles) = bt.find_handles::<BaseCode>() {
1515
for handle in handles {
16-
let base_code = bt
16+
let mut base_code = bt
1717
.open_protocol::<BaseCode>(
1818
OpenProtocolParams {
1919
handle,
@@ -24,8 +24,6 @@ pub fn test(image: Handle, bt: &BootServices) {
2424
)
2525
.unwrap();
2626

27-
let base_code = unsafe { &mut *base_code.interface.get() };
28-
2927
info!("Starting PXE Base Code");
3028
base_code
3129
.start(false)

uefi-test-runner/src/proto/rng.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ pub fn test(image: Handle, bt: &BootServices) {
1111
.first()
1212
.expect("No Rng handles");
1313

14-
let rng = bt
14+
let mut rng = bt
1515
.open_protocol::<Rng>(
1616
OpenProtocolParams {
1717
handle,
@@ -21,7 +21,6 @@ pub fn test(image: Handle, bt: &BootServices) {
2121
OpenProtocolAttributes::Exclusive,
2222
)
2323
.expect("Failed to open Rng protocol");
24-
let rng = unsafe { &mut *rng.interface.get() };
2524

2625
let mut list = [RngAlgorithmType::EMPTY_ALGORITHM; 4];
2726

0 commit comments

Comments
 (0)