Skip to content

Commit

Permalink
uefi: Use raw disk protocols
Browse files Browse the repository at this point in the history
  • Loading branch information
nicholasbishop authored and phip1611 committed Jul 16, 2023
1 parent 6827099 commit cac3a3b
Showing 1 changed file with 44 additions and 53 deletions.
97 changes: 44 additions & 53 deletions uefi/src/proto/media/disk.rs
Original file line number Diff line number Diff line change
@@ -1,34 +1,20 @@
//! Disk I/O protocols.

use crate::proto::unsafe_protocol;
use crate::util::opt_nonnull_to_ptr;
use crate::{Event, Result, Status, StatusExt};
use core::ptr::NonNull;
use uefi_raw::protocol::disk::{DiskIo2Protocol, DiskIoProtocol};

/// The disk I/O protocol.
///
/// This protocol is used to abstract the block accesses of the block I/O
/// protocol to a more general offset-length protocol. Firmware is
/// responsible for adding this protocol to any block I/O interface that
/// appears in the system that does not already have a disk I/O protocol.
#[repr(C)]
#[unsafe_protocol("ce345171-ba0b-11d2-8e4f-00a0c969723b")]
pub struct DiskIo {
revision: u64,
read_disk: unsafe extern "efiapi" fn(
this: &DiskIo,
media_id: u32,
offset: u64,
len: usize,
buffer: *mut u8,
) -> Status,
write_disk: unsafe extern "efiapi" fn(
this: &mut DiskIo,
media_id: u32,
offset: u64,
len: usize,
buffer: *const u8,
) -> Status,
}
#[repr(transparent)]
#[unsafe_protocol(DiskIoProtocol::GUID)]
pub struct DiskIo(DiskIoProtocol);

impl DiskIo {
/// Reads bytes from the disk device.
Expand All @@ -46,8 +32,16 @@ impl DiskIo {
/// * `uefi::status::NO_MEDIA` There is no medium in the device.
/// * `uefi::status::MEDIA_CHANGED` `media_id` is not for the current medium.
pub fn read_disk(&self, media_id: u32, offset: u64, buffer: &mut [u8]) -> Result {
unsafe { (self.read_disk)(self, media_id, offset, buffer.len(), buffer.as_mut_ptr()) }
.to_result()
unsafe {
(self.0.read_disk)(
&self.0,
media_id,
offset,
buffer.len(),
buffer.as_mut_ptr().cast(),
)
}
.to_result()
}

/// Writes bytes to the disk device.
Expand All @@ -66,8 +60,16 @@ impl DiskIo {
/// * `uefi::status::MEDIA_CHANGED` `media_id` is not for the current medium.
/// * `uefi::status::WRITE_PROTECTED` The device cannot be written to.
pub fn write_disk(&mut self, media_id: u32, offset: u64, buffer: &[u8]) -> Result {
unsafe { (self.write_disk)(self, media_id, offset, buffer.len(), buffer.as_ptr()) }
.to_result()
unsafe {
(self.0.write_disk)(
&mut self.0,
media_id,
offset,
buffer.len(),
buffer.as_ptr().cast(),
)
}
.to_result()
}
}

Expand All @@ -85,32 +87,9 @@ pub struct DiskIo2Token {
///
/// This protocol provides an extension to the disk I/O protocol to enable
/// non-blocking / asynchronous byte-oriented disk operation.
#[repr(C)]
#[unsafe_protocol("151c8eae-7f2c-472c-9e54-9828194f6a88")]
pub struct DiskIo2 {
revision: u64,
cancel: unsafe extern "efiapi" fn(this: &mut DiskIo2) -> Status,
read_disk_ex: unsafe extern "efiapi" fn(
this: &DiskIo2,
media_id: u32,
offset: u64,
token: Option<NonNull<DiskIo2Token>>,
len: usize,
buffer: *mut u8,
) -> Status,
write_disk_ex: unsafe extern "efiapi" fn(
this: &mut DiskIo2,
media_id: u32,
offset: u64,
token: Option<NonNull<DiskIo2Token>>,
len: usize,
buffer: *const u8,
) -> Status,
flush_disk_ex: unsafe extern "efiapi" fn(
this: &mut DiskIo2,
token: Option<NonNull<DiskIo2Token>>,
) -> Status,
}
#[repr(transparent)]
#[unsafe_protocol(DiskIo2Protocol::GUID)]
pub struct DiskIo2(DiskIo2Protocol);

impl DiskIo2 {
/// Terminates outstanding asynchronous requests to the device.
Expand All @@ -119,7 +98,7 @@ impl DiskIo2 {
/// * `uefi::status::DEVICE_ERROR` The device reported an error while performing
/// the cancel operation.
pub fn cancel(&mut self) -> Result {
unsafe { (self.cancel)(self) }.to_result()
unsafe { (self.0.cancel)(&mut self.0) }.to_result()
}

/// Reads bytes from the disk device.
Expand Down Expand Up @@ -153,7 +132,9 @@ impl DiskIo2 {
len: usize,
buffer: *mut u8,
) -> Result {
(self.read_disk_ex)(self, media_id, offset, token, len, buffer).to_result()
let token = opt_nonnull_to_ptr(token);
(self.0.read_disk_ex)(&self.0, media_id, offset, token.cast(), len, buffer.cast())
.to_result()
}

/// Writes bytes to the disk device.
Expand Down Expand Up @@ -188,7 +169,16 @@ impl DiskIo2 {
len: usize,
buffer: *const u8,
) -> Result {
(self.write_disk_ex)(self, media_id, offset, token, len, buffer).to_result()
let token = opt_nonnull_to_ptr(token);
(self.0.write_disk_ex)(
&mut self.0,
media_id,
offset,
token.cast(),
len,
buffer.cast(),
)
.to_result()
}

/// Flushes all modified data to the physical device.
Expand All @@ -206,6 +196,7 @@ impl DiskIo2 {
/// the flush operation.
/// * `uefi::status::WRITE_PROTECTED` The device cannot be written to.
pub fn flush_disk(&mut self, token: Option<NonNull<DiskIo2Token>>) -> Result {
unsafe { (self.flush_disk_ex)(self, token) }.to_result()
let token = opt_nonnull_to_ptr(token);
unsafe { (self.0.flush_disk_ex)(&mut self.0, token.cast()) }.to_result()
}
}

0 comments on commit cac3a3b

Please sign in to comment.