|  | 
|  | 1 | +use core::convert::TryFrom; | 
| 1 | 2 | use uefi::prelude::*; | 
|  | 3 | +use uefi::proto::media::file::{Directory, File, FileAttribute, FileMode, FileType}; | 
| 2 | 4 | use uefi::proto::media::fs::SimpleFileSystem; | 
| 3 | 5 | use uefi::proto::media::partition::PartitionInfo; | 
| 4 | 6 | use uefi::table::boot::{OpenProtocolAttributes, OpenProtocolParams}; | 
|  | 7 | +use uefi::CString16; | 
|  | 8 | + | 
|  | 9 | +/// Open and read a test file in the boot directory. | 
|  | 10 | +pub fn test_open_and_read(directory: &mut Directory) { | 
|  | 11 | + let test_input_path = CString16::try_from("EFI\\BOOT\\test_input.txt").unwrap(); | 
|  | 12 | + match directory.open(&test_input_path, FileMode::Read, FileAttribute::empty()) { | 
|  | 13 | + Ok(file) => { | 
|  | 14 | + let file = file.unwrap().into_type().unwrap_success(); | 
|  | 15 | + if let FileType::Regular(mut file) = file { | 
|  | 16 | + let mut buffer = vec![0; 128]; | 
|  | 17 | + let size = file | 
|  | 18 | + .read(&mut buffer) | 
|  | 19 | + .expect_success(&format!("failed to read {}", test_input_path)); | 
|  | 20 | + let buffer = &buffer[..size]; | 
|  | 21 | + info!("Successfully read {}", test_input_path); | 
|  | 22 | + assert_eq!(buffer, b"test input data"); | 
|  | 23 | + } else { | 
|  | 24 | + panic!("{} is not a regular file", test_input_path); | 
|  | 25 | + } | 
|  | 26 | + } | 
|  | 27 | + Err(err) => { | 
|  | 28 | + let msg = format!("Failed to open {}: {:?}", test_input_path, err); | 
|  | 29 | + // The file might reasonably not be present when running on real | 
|  | 30 | + // hardware, so only panic on failure under qemu. | 
|  | 31 | + if cfg!(feature = "qemu") { | 
|  | 32 | + panic!("{}", msg); | 
|  | 33 | + } else { | 
|  | 34 | + warn!("{}", msg); | 
|  | 35 | + } | 
|  | 36 | + } | 
|  | 37 | + } | 
|  | 38 | +} | 
| 5 | 39 | 
 | 
| 6 | 40 | pub fn test(image: Handle, bt: &BootServices) { | 
| 7 | 41 |  info!("Testing Media Access protocols"); | 
| @@ -31,6 +65,8 @@ pub fn test(image: Handle, bt: &BootServices) { | 
| 31 | 65 |  info!("Root directory entry: {:?}", file_info); | 
| 32 | 66 |  } | 
| 33 | 67 |  directory.reset_entry_readout().unwrap().unwrap(); | 
|  | 68 | + | 
|  | 69 | + test_open_and_read(&mut directory); | 
| 34 | 70 |  } else { | 
| 35 | 71 |  warn!("`SimpleFileSystem` protocol is not available"); | 
| 36 | 72 |  } | 
|  | 
0 commit comments