1
1
//! Disk I/O protocols.
2
2
3
3
use crate :: proto:: unsafe_protocol;
4
+ use crate :: util:: opt_nonnull_to_ptr;
4
5
use crate :: { Event , Result , Status , StatusExt } ;
5
6
use core:: ptr:: NonNull ;
7
+ use uefi_raw:: protocol:: disk:: { DiskIo2Protocol , DiskIoProtocol } ;
6
8
7
9
/// The disk I/O protocol.
8
10
///
9
11
/// This protocol is used to abstract the block accesses of the block I/O
10
12
/// protocol to a more general offset-length protocol. Firmware is
11
13
/// responsible for adding this protocol to any block I/O interface that
12
14
/// appears in the system that does not already have a disk I/O protocol.
13
- #[ repr( C ) ]
14
- #[ unsafe_protocol( "ce345171-ba0b-11d2-8e4f-00a0c969723b" ) ]
15
- pub struct DiskIo {
16
- revision : u64 ,
17
- read_disk : extern "efiapi" fn (
18
- this : & DiskIo ,
19
- media_id : u32 ,
20
- offset : u64 ,
21
- len : usize ,
22
- buffer : * mut u8 ,
23
- ) -> Status ,
24
- write_disk : extern "efiapi" fn (
25
- this : & mut DiskIo ,
26
- media_id : u32 ,
27
- offset : u64 ,
28
- len : usize ,
29
- buffer : * const u8 ,
30
- ) -> Status ,
31
- }
15
+ #[ repr( transparent) ]
16
+ #[ unsafe_protocol( DiskIoProtocol :: GUID ) ]
17
+ pub struct DiskIo ( DiskIoProtocol ) ;
32
18
33
19
impl DiskIo {
34
20
/// Reads bytes from the disk device.
@@ -46,7 +32,16 @@ impl DiskIo {
46
32
/// * `uefi::status::NO_MEDIA` There is no medium in the device.
47
33
/// * `uefi::status::MEDIA_CHANGED` `media_id` is not for the current medium.
48
34
pub fn read_disk ( & self , media_id : u32 , offset : u64 , buffer : & mut [ u8 ] ) -> Result {
49
- ( self . read_disk ) ( self , media_id, offset, buffer. len ( ) , buffer. as_mut_ptr ( ) ) . to_result ( )
35
+ unsafe {
36
+ ( self . 0 . read_disk ) (
37
+ & self . 0 ,
38
+ media_id,
39
+ offset,
40
+ buffer. len ( ) ,
41
+ buffer. as_mut_ptr ( ) . cast ( ) ,
42
+ )
43
+ }
44
+ . to_result ( )
50
45
}
51
46
52
47
/// Writes bytes to the disk device.
@@ -65,7 +60,16 @@ impl DiskIo {
65
60
/// * `uefi::status::MEDIA_CHANGED` `media_id` is not for the current medium.
66
61
/// * `uefi::status::WRITE_PROTECTED` The device cannot be written to.
67
62
pub fn write_disk ( & mut self , media_id : u32 , offset : u64 , buffer : & [ u8 ] ) -> Result {
68
- ( self . write_disk ) ( self , media_id, offset, buffer. len ( ) , buffer. as_ptr ( ) ) . to_result ( )
63
+ unsafe {
64
+ ( self . 0 . write_disk ) (
65
+ & mut self . 0 ,
66
+ media_id,
67
+ offset,
68
+ buffer. len ( ) ,
69
+ buffer. as_ptr ( ) . cast ( ) ,
70
+ )
71
+ }
72
+ . to_result ( )
69
73
}
70
74
}
71
75
@@ -83,30 +87,9 @@ pub struct DiskIo2Token {
83
87
///
84
88
/// This protocol provides an extension to the disk I/O protocol to enable
85
89
/// non-blocking / asynchronous byte-oriented disk operation.
86
- #[ repr( C ) ]
87
- #[ unsafe_protocol( "151c8eae-7f2c-472c-9e54-9828194f6a88" ) ]
88
- pub struct DiskIo2 {
89
- revision : u64 ,
90
- cancel : extern "efiapi" fn ( this : & mut DiskIo2 ) -> Status ,
91
- read_disk_ex : extern "efiapi" fn (
92
- this : & DiskIo2 ,
93
- media_id : u32 ,
94
- offset : u64 ,
95
- token : Option < NonNull < DiskIo2Token > > ,
96
- len : usize ,
97
- buffer : * mut u8 ,
98
- ) -> Status ,
99
- write_disk_ex : extern "efiapi" fn (
100
- this : & mut DiskIo2 ,
101
- media_id : u32 ,
102
- offset : u64 ,
103
- token : Option < NonNull < DiskIo2Token > > ,
104
- len : usize ,
105
- buffer : * const u8 ,
106
- ) -> Status ,
107
- flush_disk_ex :
108
- extern "efiapi" fn ( this : & mut DiskIo2 , token : Option < NonNull < DiskIo2Token > > ) -> Status ,
109
- }
90
+ #[ repr( transparent) ]
91
+ #[ unsafe_protocol( DiskIo2Protocol :: GUID ) ]
92
+ pub struct DiskIo2 ( DiskIo2Protocol ) ;
110
93
111
94
impl DiskIo2 {
112
95
/// Terminates outstanding asynchronous requests to the device.
@@ -115,7 +98,7 @@ impl DiskIo2 {
115
98
/// * `uefi::status::DEVICE_ERROR` The device reported an error while performing
116
99
/// the cancel operation.
117
100
pub fn cancel ( & mut self ) -> Result {
118
- ( self . cancel ) ( self ) . to_result ( )
101
+ unsafe { ( self . 0 . cancel ) ( & mut self . 0 ) } . to_result ( )
119
102
}
120
103
121
104
/// Reads bytes from the disk device.
@@ -149,7 +132,9 @@ impl DiskIo2 {
149
132
len : usize ,
150
133
buffer : * mut u8 ,
151
134
) -> Result {
152
- ( self . read_disk_ex ) ( self , media_id, offset, token, len, buffer) . to_result ( )
135
+ let token = opt_nonnull_to_ptr ( token) ;
136
+ ( self . 0 . read_disk_ex ) ( & self . 0 , media_id, offset, token. cast ( ) , len, buffer. cast ( ) )
137
+ . to_result ( )
153
138
}
154
139
155
140
/// Writes bytes to the disk device.
@@ -184,7 +169,16 @@ impl DiskIo2 {
184
169
len : usize ,
185
170
buffer : * const u8 ,
186
171
) -> Result {
187
- ( self . write_disk_ex ) ( self , media_id, offset, token, len, buffer) . to_result ( )
172
+ let token = opt_nonnull_to_ptr ( token) ;
173
+ ( self . 0 . write_disk_ex ) (
174
+ & mut self . 0 ,
175
+ media_id,
176
+ offset,
177
+ token. cast ( ) ,
178
+ len,
179
+ buffer. cast ( ) ,
180
+ )
181
+ . to_result ( )
188
182
}
189
183
190
184
/// Flushes all modified data to the physical device.
@@ -202,6 +196,7 @@ impl DiskIo2 {
202
196
/// the flush operation.
203
197
/// * `uefi::status::WRITE_PROTECTED` The device cannot be written to.
204
198
pub fn flush_disk ( & mut self , token : Option < NonNull < DiskIo2Token > > ) -> Result {
205
- ( self . flush_disk_ex ) ( self , token) . to_result ( )
199
+ let token = opt_nonnull_to_ptr ( token) ;
200
+ unsafe { ( self . 0 . flush_disk_ex ) ( & mut self . 0 , token. cast ( ) ) } . to_result ( )
206
201
}
207
202
}
0 commit comments