Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Deref and DerefMut for ScopedProtocol #434

Merged
merged 3 commits into from
May 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 26 additions & 6 deletions src/table/boot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use core::cell::UnsafeCell;
use core::ffi::c_void;
use core::fmt::{Debug, Formatter};
use core::mem::{self, MaybeUninit};
use core::ops::{Deref, DerefMut};
use core::ptr::NonNull;
use core::{ptr, slice};

Expand Down Expand Up @@ -884,6 +885,8 @@ impl BootServices {
)
.into_with_val(|| unsafe {
let interface = P::mut_ptr_from_ffi(interface) as *const UnsafeCell<P>;

#[allow(deprecated)]
ScopedProtocol {
interface: &*interface,
open_params: params,
Expand Down Expand Up @@ -1030,21 +1033,17 @@ impl BootServices {
},
OpenProtocolAttributes::Exclusive,
)?;
let loaded_image = unsafe { &*loaded_image.interface.get() };

let device_handle = loaded_image.device();

let device_path = self.open_protocol::<DevicePath>(
OpenProtocolParams {
handle: device_handle,
handle: loaded_image.device(),
agent: image_handle,
controller: None,
},
OpenProtocolAttributes::Exclusive,
)?;
let mut device_path = unsafe { &*device_path.interface.get() };

let device_handle = self.locate_device_path::<SimpleFileSystem>(&mut device_path)?;
let device_handle = self.locate_device_path::<SimpleFileSystem>(&mut &*device_path)?;

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

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

impl<'a, P: Protocol + ?Sized> Deref for ScopedProtocol<'a, P> {
type Target = P;

fn deref(&self) -> &Self::Target {
#[allow(deprecated)]
unsafe {
&*self.interface.get()
}
}
}

impl<'a, P: Protocol + ?Sized> DerefMut for ScopedProtocol<'a, P> {
fn deref_mut(&mut self) -> &mut Self::Target {
#[allow(deprecated)]
unsafe {
&mut *self.interface.get()
}
}
}

/// Type of allocation to perform.
#[derive(Debug, Copy, Clone)]
pub enum AllocateType {
Expand Down
3 changes: 1 addition & 2 deletions uefi-test-runner/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ fn check_screenshot(image: Handle, bt: &BootServices, name: &str) {
.get(1)
.expect("Second serial device is missing");

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

// Set a large timeout to avoid problems with Travis
let mut io_mode = *serial.io_mode();
Expand Down
6 changes: 2 additions & 4 deletions uefi-test-runner/src/proto/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,14 @@ pub fn test(image: Handle, bt: &BootServices) {
info!("Running UEFI debug connection protocol test");
if let Ok(handles) = bt.find_handles::<DebugSupport>() {
for handle in handles {
if let Ok(debug_support) = bt.open_protocol::<DebugSupport>(
if let Ok(mut debug_support) = bt.open_protocol::<DebugSupport>(
OpenProtocolParams {
handle,
agent: image,
controller: None,
},
OpenProtocolAttributes::Exclusive,
) {
let debug_support = unsafe { &mut *debug_support.interface.get() };

// make sure that the max processor index is a sane value, i.e. it works
let maximum_processor_index = debug_support.get_maximum_processor_index();
assert_ne!(
Expand Down Expand Up @@ -94,7 +92,7 @@ pub fn test(image: Handle, bt: &BootServices) {
_ => unreachable!(),
}

test_invalidate_instruction_cache(debug_support);
test_invalidate_instruction_cache(&mut *debug_support);
}
}
} else {
Expand Down
2 changes: 0 additions & 2 deletions uefi-test-runner/src/proto/device_path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ pub fn test(image: Handle, bt: &BootServices) {
OpenProtocolAttributes::Exclusive,
)
.expect("Failed to open LoadedImage protocol");
let loaded_image = unsafe { &*loaded_image.interface.get() };

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

let device_path_to_text = bt
.locate_protocol::<DevicePathToText>()
Expand Down
1 change: 0 additions & 1 deletion uefi-test-runner/src/proto/loaded_image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ pub fn test(image: Handle, bt: &BootServices) {
OpenProtocolAttributes::Exclusive,
)
.expect("Failed to open LoadedImage protocol");
let loaded_image = unsafe { &*loaded_image.interface.get() };

let load_options = loaded_image.load_options_as_bytes();
info!("LoadedImage options: {:?}", load_options);
Expand Down
3 changes: 1 addition & 2 deletions uefi-test-runner/src/proto/media/known_disk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ pub fn test_known_disk(image: Handle, bt: &BootServices) {

let mut found_test_disk = false;
for handle in handles {
let sfs = bt
let mut sfs = bt
.open_protocol::<SimpleFileSystem>(
OpenProtocolParams {
handle,
Expand All @@ -164,7 +164,6 @@ pub fn test_known_disk(image: Handle, bt: &BootServices) {
OpenProtocolAttributes::Exclusive,
)
.expect("Failed to get simple file system");
let sfs = unsafe { &mut *sfs.interface.get() };
let mut directory = sfs.open_volume().unwrap();

let mut fs_info_buf = vec![0; 128];
Expand Down
1 change: 0 additions & 1 deletion uefi-test-runner/src/proto/media/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ pub fn test(image: Handle, bt: &BootServices) {
OpenProtocolAttributes::Exclusive,
)
.expect("Failed to get partition info");
let pi = unsafe { &*pi.interface.get() };

if let Some(mbr) = pi.mbr_partition_record() {
info!("MBR partition: {:?}", mbr);
Expand Down
4 changes: 1 addition & 3 deletions uefi-test-runner/src/proto/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub fn test(image: Handle, bt: &BootServices) {

if let Ok(handles) = bt.find_handles::<BaseCode>() {
for handle in handles {
let base_code = bt
let mut base_code = bt
.open_protocol::<BaseCode>(
OpenProtocolParams {
handle,
Expand All @@ -24,8 +24,6 @@ pub fn test(image: Handle, bt: &BootServices) {
)
.unwrap();

let base_code = unsafe { &mut *base_code.interface.get() };

info!("Starting PXE Base Code");
base_code
.start(false)
Expand Down
3 changes: 1 addition & 2 deletions uefi-test-runner/src/proto/rng.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub fn test(image: Handle, bt: &BootServices) {
.first()
.expect("No Rng handles");

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

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

Expand Down