1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
//! File system support protocols.

use super::file::{Directory, FileHandle};
use crate::proto::unsafe_protocol;
use crate::{Result, StatusExt};
use core::ptr;
use uefi_raw::protocol::file_system::SimpleFileSystemProtocol;

/// Allows access to a FAT-12/16/32 file system.
///
/// This interface is implemented by some storage devices
/// to allow file access to the contained file systems.
///
/// # Accessing `SimpleFileSystem` protocol
///
/// Use [`BootServices::get_image_file_system`] to retrieve the `SimpleFileSystem`
/// protocol associated with a given image handle.
///
/// See the [`BootServices`] documentation for more details of how to open a protocol.
///
/// [`BootServices::get_image_file_system`]: crate::table::boot::BootServices::get_image_file_system
/// [`BootServices`]: crate::table::boot::BootServices#accessing-protocols
#[derive(Debug)]
#[repr(transparent)]
#[unsafe_protocol(SimpleFileSystemProtocol::GUID)]
pub struct SimpleFileSystem(SimpleFileSystemProtocol);

impl SimpleFileSystem {
    /// Open the root directory on a volume.
    ///
    /// # Errors
    ///
    /// See section `EFI_SIMPLE_FILE_SYSTEM_PROTOCOL.OpenVolume()` in the UEFI Specification
    /// for more details.
    ///
    /// If you can't find the function definition, try searching for
    /// `EFI_SIMPLE_FILE SYSTEM_PROTOCOL.OpenVolume()` (this has a space in between FILE and
    /// SYSTEM; it could be a typo in the UEFI spec).
    ///
    /// * [`uefi::Status::UNSUPPORTED`]
    /// * [`uefi::Status::NO_MEDIA`]
    /// * [`uefi::Status::DEVICE_ERROR`]
    /// * [`uefi::Status::VOLUME_CORRUPTED`]
    /// * [`uefi::Status::ACCESS_DENIED`]
    /// * [`uefi::Status::OUT_OF_RESOURCES`]
    /// * [`uefi::Status::MEDIA_CHANGED`]
    pub fn open_volume(&mut self) -> Result<Directory> {
        let mut ptr = ptr::null_mut();
        unsafe { (self.0.open_volume)(&mut self.0, &mut ptr) }
            .to_result_with_val(|| unsafe { Directory::new(FileHandle::new(ptr.cast())) })
    }
}