Skip to content

Commit 8a62ad4

Browse files
Add BlockIo protocl to uefi-raw
1 parent 9e88900 commit 8a62ad4

File tree

2 files changed

+56
-29
lines changed

2 files changed

+56
-29
lines changed

uefi-raw/src/protocol/block.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
use crate::{guid, Guid, Status};
2+
use core::ffi::c_void;
3+
14
/// Logical block address.
25
pub type Lba = u64;
36

@@ -22,3 +25,29 @@ pub struct BlockIoMedia {
2225
// Added in revision 3.
2326
pub optimal_transfer_length_granularity: u32,
2427
}
28+
29+
#[repr(C)]
30+
pub struct BlockIo {
31+
pub revision: u64,
32+
pub media: *const BlockIoMedia,
33+
pub reset: unsafe extern "efiapi" fn(this: *mut Self, extended_verification: bool) -> Status,
34+
pub read_blocks: unsafe extern "efiapi" fn(
35+
this: *const Self,
36+
media_id: u32,
37+
lba: Lba,
38+
buffer_size: usize,
39+
buffer: *mut c_void,
40+
) -> Status,
41+
pub write_blocks: unsafe extern "efiapi" fn(
42+
this: *mut Self,
43+
media_id: u32,
44+
lba: Lba,
45+
buffer_size: usize,
46+
buffer: *const c_void,
47+
) -> Status,
48+
pub flush_blocks: unsafe extern "efiapi" fn(this: *mut Self) -> Status,
49+
}
50+
51+
impl BlockIo {
52+
pub const GUID: Guid = guid!("964e5b21-6459-11d2-8e39-00a0c969723b");
53+
}

uefi/src/proto/media/block.rs

Lines changed: 27 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,20 @@
11
//! Block I/O protocols.
22
33
use crate::proto::unsafe_protocol;
4-
use crate::{Result, Status, StatusExt};
4+
use crate::{Result, StatusExt};
55

66
pub use uefi_raw::protocol::block::Lba;
77

88
/// The Block I/O protocol.
9-
#[repr(C)]
10-
#[unsafe_protocol("964e5b21-6459-11d2-8e39-00a0c969723b")]
11-
pub struct BlockIO {
12-
revision: u64,
13-
media: *const BlockIOMedia,
14-
15-
reset: extern "efiapi" fn(this: &BlockIO, extended_verification: bool) -> Status,
16-
read_blocks: extern "efiapi" fn(
17-
this: &BlockIO,
18-
media_id: u32,
19-
lba: Lba,
20-
buffer_size: usize,
21-
buffer: *mut u8,
22-
) -> Status,
23-
write_blocks: extern "efiapi" fn(
24-
this: &BlockIO,
25-
media_id: u32,
26-
lba: Lba,
27-
buffer_size: usize,
28-
buffer: *const u8,
29-
) -> Status,
30-
flush_blocks: extern "efiapi" fn(this: &BlockIO) -> Status,
31-
}
9+
#[repr(transparent)]
10+
#[unsafe_protocol(uefi_raw::protocol::block::BlockIo::GUID)]
11+
pub struct BlockIO(uefi_raw::protocol::block::BlockIo);
3212

3313
impl BlockIO {
3414
/// Pointer for block IO media.
3515
#[must_use]
3616
pub const fn media(&self) -> &BlockIOMedia {
37-
unsafe { &*self.media }
17+
unsafe { &*self.0.media.cast::<BlockIOMedia>() }
3818
}
3919

4020
/// Resets the block device hardware.
@@ -46,7 +26,7 @@ impl BlockIO {
4626
/// # Errors
4727
/// * `uefi::Status::DEVICE_ERROR` The block device is not functioning correctly and could not be reset.
4828
pub fn reset(&mut self, extended_verification: bool) -> Result {
49-
(self.reset)(self, extended_verification).to_result()
29+
unsafe { (self.0.reset)(&mut self.0, extended_verification) }.to_result()
5030
}
5131

5232
/// Read the requested number of blocks from the device.
@@ -67,7 +47,16 @@ impl BlockIO {
6747
/// proper alignment.
6848
pub fn read_blocks(&self, media_id: u32, lba: Lba, buffer: &mut [u8]) -> Result {
6949
let buffer_size = buffer.len();
70-
(self.read_blocks)(self, media_id, lba, buffer_size, buffer.as_mut_ptr()).to_result()
50+
unsafe {
51+
(self.0.read_blocks)(
52+
&self.0,
53+
media_id,
54+
lba,
55+
buffer_size,
56+
buffer.as_mut_ptr().cast(),
57+
)
58+
}
59+
.to_result()
7160
}
7261

7362
/// Writes the requested number of blocks to the device.
@@ -89,7 +78,16 @@ impl BlockIO {
8978
/// on proper alignment.
9079
pub fn write_blocks(&mut self, media_id: u32, lba: Lba, buffer: &[u8]) -> Result {
9180
let buffer_size = buffer.len();
92-
(self.write_blocks)(self, media_id, lba, buffer_size, buffer.as_ptr()).to_result()
81+
unsafe {
82+
(self.0.write_blocks)(
83+
&mut self.0,
84+
media_id,
85+
lba,
86+
buffer_size,
87+
buffer.as_ptr().cast(),
88+
)
89+
}
90+
.to_result()
9391
}
9492

9593
/// Flushes all modified data to a physical block device.
@@ -98,7 +96,7 @@ impl BlockIO {
9896
/// * `uefi::Status::DEVICE_ERROR` The device reported an error while attempting to write data.
9997
/// * `uefi::Status::NO_MEDIA` There is no media in the device.
10098
pub fn flush_blocks(&mut self) -> Result {
101-
(self.flush_blocks)(self).to_result()
99+
unsafe { (self.0.flush_blocks)(&mut self.0) }.to_result()
102100
}
103101
}
104102

0 commit comments

Comments
 (0)