@@ -2,6 +2,7 @@ use alloc::string::ToString;
2
2
use core:: ffi:: c_void;
3
3
use core:: ptr:: NonNull ;
4
4
use uefi:: prelude:: * ;
5
+ use uefi:: proto:: media:: block:: BlockIO ;
5
6
use uefi:: proto:: media:: disk:: { DiskIo , DiskIo2 , DiskIo2Token } ;
6
7
use uefi:: proto:: media:: file:: {
7
8
Directory , File , FileAttribute , FileInfo , FileMode , FileSystemInfo ,
@@ -147,6 +148,18 @@ fn test_create_file(directory: &mut Directory) {
147
148
fn test_raw_disk_io ( handle : Handle , image : Handle , bt : & BootServices ) {
148
149
info ! ( "Testing raw disk I/O" ) ;
149
150
151
+ // Open the block I/O protocol on the handle
152
+ let block_io = bt
153
+ . open_protocol :: < BlockIO > (
154
+ OpenProtocolParams {
155
+ handle,
156
+ agent : image,
157
+ controller : None ,
158
+ } ,
159
+ OpenProtocolAttributes :: GetProtocol ,
160
+ )
161
+ . expect ( "Failed to get block I/O protocol" ) ;
162
+
150
163
// Open the disk I/O protocol on the input handle
151
164
let disk_io = bt
152
165
. open_protocol :: < DiskIo > (
@@ -159,26 +172,17 @@ fn test_raw_disk_io(handle: Handle, image: Handle, bt: &BootServices) {
159
172
)
160
173
. expect ( "Failed to get disk I/O protocol" ) ;
161
174
162
- // Allocate a temporary buffer to read into
163
- const SIZE : usize = 512 ;
164
- let buf = bt
165
- . allocate_pool ( MemoryType :: LOADER_DATA , SIZE )
166
- . expect ( "Failed to allocate temporary buffer" ) ;
167
-
168
- // SAFETY: A valid buffer of `SIZE` bytes was allocated above
169
- let slice = unsafe { core:: slice:: from_raw_parts_mut ( buf, SIZE ) } ;
170
-
171
175
// Read from the first sector of the disk into the buffer
176
+ let mut buf = vec ! [ 0 ; 512 ] ;
172
177
disk_io
173
- . read_disk ( 0 , 0 , slice )
178
+ . read_disk ( block_io . media ( ) . media_id ( ) , 0 , & mut buf )
174
179
. expect ( "Failed to read from disk" ) ;
175
180
176
181
// Verify that the disk's MBR signature is correct
177
- assert_eq ! ( slice [ 510 ] , 0x55 ) ;
178
- assert_eq ! ( slice [ 511 ] , 0xaa ) ;
182
+ assert_eq ! ( buf [ 510 ] , 0x55 ) ;
183
+ assert_eq ! ( buf [ 511 ] , 0xaa ) ;
179
184
180
185
info ! ( "Raw disk I/O succeeded" ) ;
181
- bt. free_pool ( buf) . unwrap ( ) ;
182
186
}
183
187
184
188
/// Asynchronous disk I/O task context
@@ -221,6 +225,18 @@ fn test_raw_disk_io2(handle: Handle, image: Handle, bt: &BootServices) {
221
225
} ,
222
226
OpenProtocolAttributes :: GetProtocol ,
223
227
) {
228
+ // Open the block I/O protocol on the handle
229
+ let block_io = bt
230
+ . open_protocol :: < BlockIO > (
231
+ OpenProtocolParams {
232
+ handle,
233
+ agent : image,
234
+ controller : None ,
235
+ } ,
236
+ OpenProtocolAttributes :: GetProtocol ,
237
+ )
238
+ . expect ( "Failed to get block I/O protocol" ) ;
239
+
224
240
// Allocate the task context structure
225
241
let task = bt
226
242
. allocate_pool ( MemoryType :: LOADER_DATA , core:: mem:: size_of :: < DiskIoTask > ( ) )
@@ -248,7 +264,13 @@ fn test_raw_disk_io2(handle: Handle, image: Handle, bt: &BootServices) {
248
264
249
265
// Initiate the asynchronous read operation
250
266
disk_io2
251
- . read_disk_raw ( 0 , 0 , & mut ( * task) . token , SIZE_TO_READ , ( * task) . buffer )
267
+ . read_disk_raw (
268
+ block_io. media ( ) . media_id ( ) ,
269
+ 0 ,
270
+ & mut ( * task) . token ,
271
+ SIZE_TO_READ ,
272
+ ( * task) . buffer ,
273
+ )
252
274
. expect ( "Failed to initiate asynchronous disk I/O read" ) ;
253
275
}
254
276
@@ -272,10 +294,6 @@ pub fn test_known_disk(image: Handle, bt: &BootServices) {
272
294
273
295
let mut found_test_disk = false ;
274
296
for handle in handles {
275
- // Test raw disk I/O first
276
- test_raw_disk_io ( handle, image, bt) ;
277
- test_raw_disk_io2 ( handle, image, bt) ;
278
-
279
297
let mut sfs = bt
280
298
. open_protocol :: < SimpleFileSystem > (
281
299
OpenProtocolParams {
@@ -300,6 +318,10 @@ pub fn test_known_disk(image: Handle, bt: &BootServices) {
300
318
continue ;
301
319
}
302
320
321
+ // Test raw disk I/O first
322
+ test_raw_disk_io ( handle, image, bt) ;
323
+ test_raw_disk_io2 ( handle, image, bt) ;
324
+
303
325
assert ! ( !fs_info. read_only( ) ) ;
304
326
assert_eq ! ( fs_info. volume_size( ) , 512 * 1192 ) ;
305
327
assert_eq ! ( fs_info. free_space( ) , 512 * 1190 ) ;
0 commit comments