@@ -2,6 +2,7 @@ use alloc::string::ToString;
22use core:: ffi:: c_void;
33use core:: ptr:: NonNull ;
44use uefi:: prelude:: * ;
5+ use uefi:: proto:: media:: block:: BlockIO ;
56use uefi:: proto:: media:: disk:: { DiskIo , DiskIo2 , DiskIo2Token } ;
67use uefi:: proto:: media:: file:: {
78 Directory , File , FileAttribute , FileInfo , FileMode , FileSystemInfo ,
@@ -147,6 +148,18 @@ fn test_create_file(directory: &mut Directory) {
147148fn test_raw_disk_io ( handle : Handle , image : Handle , bt : & BootServices ) {
148149 info ! ( "Testing raw disk I/O" ) ;
149150
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+
150163 // Open the disk I/O protocol on the input handle
151164 let disk_io = bt
152165 . open_protocol :: < DiskIo > (
@@ -159,26 +172,17 @@ fn test_raw_disk_io(handle: Handle, image: Handle, bt: &BootServices) {
159172 )
160173 . expect ( "Failed to get disk I/O protocol" ) ;
161174
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-
171175 // Read from the first sector of the disk into the buffer
176+ let mut buf = vec ! [ 0 ; 512 ] ;
172177 disk_io
173- . read_disk ( 0 , 0 , slice )
178+ . read_disk ( block_io . media ( ) . media_id ( ) , 0 , & mut buf )
174179 . expect ( "Failed to read from disk" ) ;
175180
176181 // 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 ) ;
179184
180185 info ! ( "Raw disk I/O succeeded" ) ;
181- bt. free_pool ( buf) . unwrap ( ) ;
182186}
183187
184188/// Asynchronous disk I/O task context
@@ -221,6 +225,18 @@ fn test_raw_disk_io2(handle: Handle, image: Handle, bt: &BootServices) {
221225 } ,
222226 OpenProtocolAttributes :: GetProtocol ,
223227 ) {
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+
224240 // Allocate the task context structure
225241 let task = bt
226242 . 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) {
248264
249265 // Initiate the asynchronous read operation
250266 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+ )
252274 . expect ( "Failed to initiate asynchronous disk I/O read" ) ;
253275 }
254276
@@ -272,10 +294,6 @@ pub fn test_known_disk(image: Handle, bt: &BootServices) {
272294
273295 let mut found_test_disk = false ;
274296 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-
279297 let mut sfs = bt
280298 . open_protocol :: < SimpleFileSystem > (
281299 OpenProtocolParams {
@@ -300,6 +318,10 @@ pub fn test_known_disk(image: Handle, bt: &BootServices) {
300318 continue ;
301319 }
302320
321+ // Test raw disk I/O first
322+ test_raw_disk_io ( handle, image, bt) ;
323+ test_raw_disk_io2 ( handle, image, bt) ;
324+
303325 assert ! ( !fs_info. read_only( ) ) ;
304326 assert_eq ! ( fs_info. volume_size( ) , 512 * 1192 ) ;
305327 assert_eq ! ( fs_info. free_space( ) , 512 * 1190 ) ;
0 commit comments