aienux
Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.
Brought by drakestapleton
aienux / vendor / uefi / src / proto / media / load_file.rs
| 1 | // SPDX-License-Identifier: MIT OR Apache-2.0 |
| 2 | |
| 3 | //! LoadFile and LoadFile2 protocols. |
| 4 | |
| 5 | #[cfg(doc)] |
| 6 | use crate::Status; |
| 7 | use crate::proto::unsafe_protocol; |
| 8 | use uefi_raw::protocol::media::{LoadFile2Protocol, LoadFileProtocol}; |
| 9 | #[cfg(feature = "alloc")] |
| 10 | use { |
| 11 | crate::{Result, StatusExt, mem::make_boxed, proto::device_path::DevicePath}, |
| 12 | alloc::boxed::Box, |
| 13 | uefi::proto::BootPolicy, |
| 14 | uefi_raw::Boolean, |
| 15 | }; |
| 16 | |
| 17 | /// Load File [`Protocol`]. |
| 18 | /// |
| 19 | /// Used to obtain files, that are primarily boot options, from arbitrary |
| 20 | /// devices. |
| 21 | /// |
| 22 | /// # UEFI Specification |
| 23 | /// The EFI_LOAD_FILE_PROTOCOL is a simple protocol used to obtain files from |
| 24 | /// arbitrary devices. |
| 25 | /// |
| 26 | /// When the firmware is attempting to load a file, it first attempts to use the |
| 27 | /// device’s Simple File System protocol to read the file. If the file system |
| 28 | /// protocol is found, the firmware implements the policy of interpreting the |
| 29 | /// File Path value of the file being loaded. If the device does not support the |
| 30 | /// file system protocol, the firmware then attempts to read the file via the |
| 31 | /// EFI_LOAD_FILE_PROTOCOL and the LoadFile() function. In this case the |
| 32 | /// LoadFile() function implements the policy of interpreting the File Path |
| 33 | /// value. |
| 34 | /// |
| 35 | /// [`Protocol`]: uefi::proto::Protocol |
| 36 | #[derive(Debug)] |
| 37 | #[repr(transparent)] |
| 38 | #[unsafe_protocol(LoadFileProtocol::GUID)] |
| 39 | pub struct LoadFile(LoadFileProtocol); |
| 40 | |
| 41 | impl LoadFile { |
| 42 | /// Causes the driver to load a specified file. |
| 43 | /// |
| 44 | /// # Arguments |
| 45 | /// - `file_path` The device specific path of the file to load. |
| 46 | /// - `boot_policy` The [`BootPolicy`] to use. |
| 47 | /// |
| 48 | /// # Errors |
| 49 | /// - [`Status::SUCCESS`] The file was loaded. |
| 50 | /// - [`Status::UNSUPPORTED`] The device does not support the |
| 51 | /// provided BootPolicy. |
| 52 | /// - [`Status::INVALID_PARAMETER`] FilePath is not a valid device |
| 53 | /// path, or BufferSize is NULL. |
| 54 | /// - [`Status::NO_MEDIA`] No medium was present to load the file. |
| 55 | /// - [`Status::DEVICE_ERROR`] The file was not loaded due to a |
| 56 | /// device error. |
| 57 | /// - [`Status::NO_RESPONSE`] The remote system did not respond. |
| 58 | /// - [`Status::NOT_FOUND`] The file was not found. |
| 59 | /// - [`Status::ABORTED`] The file load process was manually |
| 60 | /// cancelled. |
| 61 | /// - [`Status::BUFFER_TOO_SMALL`] The BufferSize is too small to |
| 62 | /// read the current directory entry. BufferSize has been updated with the |
| 63 | /// size needed to complete the request. |
| 64 | /// - [`Status::WARN_FILE_SYSTEM`] The resulting Buffer contains |
| 65 | /// UEFI-compliant file system. |
| 66 | /// |
| 67 | /// [`BootPolicy`]: uefi::proto::BootPolicy |
| 68 | #[cfg(feature = "alloc")] |
| 69 | #[expect(clippy::extra_unused_lifetimes)] // false positive, it is used |
| 70 | pub fn load_file<'a>( |
| 71 | &mut self, |
| 72 | file_path: &DevicePath, |
| 73 | boot_policy: BootPolicy, |
| 74 | ) -> Result<Box<[u8]>> { |
| 75 | let fetch_data_fn = |buf: &'a mut [u8]| { |
| 76 | let mut size = buf.len(); |
| 77 | // SAFETY: The memory is valid. |
| 78 | let status = unsafe { |
| 79 | (self.0.load_file)( |
| 80 | &raw mut self.0, |
| 81 | file_path.as_ffi_ptr().cast(), |
| 82 | boot_policy.into(), |
| 83 | &mut size, |
| 84 | buf.as_mut_ptr().cast(), |
| 85 | ) |
| 86 | }; |
| 87 | // The firmware may write less than it announced, so only return |
| 88 | // the part it actually filled. |
| 89 | status |
| 90 | .to_result_with_err(|_| Some(size)) |
| 91 | .map(|_| &mut buf[..size]) |
| 92 | }; |
| 93 | |
| 94 | let file: Box<[u8]> = make_boxed::<[u8], _>(fetch_data_fn)?; |
| 95 | Ok(file) |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | /// Load File2 [`Protocol`]. |
| 100 | /// |
| 101 | /// The Load File2 protocol is used to obtain files from arbitrary devices that |
| 102 | /// are not boot options. |
| 103 | /// |
| 104 | /// # UEFI Specification |
| 105 | /// |
| 106 | /// The EFI_LOAD_FILE2_PROTOCOL is a simple protocol used to obtain files from |
| 107 | /// arbitrary devices that are not boot options. It is used by LoadImage() when |
| 108 | /// its BootOption parameter is FALSE and the FilePath does not have an instance |
| 109 | /// of the EFI_SIMPLE_FILE_SYSTEM_PROTOCOL. |
| 110 | /// |
| 111 | /// [`Protocol`]: uefi::proto::Protocol |
| 112 | #[derive(Debug)] |
| 113 | #[repr(transparent)] |
| 114 | #[unsafe_protocol(LoadFile2Protocol::GUID)] |
| 115 | pub struct LoadFile2(LoadFile2Protocol); |
| 116 | |
| 117 | impl LoadFile2 { |
| 118 | /// Causes the driver to load a specified file. |
| 119 | /// |
| 120 | /// # Arguments |
| 121 | /// - `file_path` The device specific path of the file to load. |
| 122 | /// |
| 123 | /// # Errors |
| 124 | /// - [`Status::SUCCESS`] The file was loaded. |
| 125 | /// - [`Status::UNSUPPORTED`] BootPolicy is TRUE. |
| 126 | /// - [`Status::INVALID_PARAMETER`] FilePath is not a valid device |
| 127 | /// path, or BufferSize is NULL. |
| 128 | /// - [`Status::NO_MEDIA`] No medium was present to load the file. |
| 129 | /// - [`Status::DEVICE_ERROR`] The file was not loaded due to a |
| 130 | /// device error. |
| 131 | /// - [`Status::NO_RESPONSE`] The remote system did not respond. |
| 132 | /// - [`Status::NOT_FOUND`] The file was not found. |
| 133 | /// - [`Status::ABORTED`] The file load process was manually |
| 134 | /// cancelled. |
| 135 | /// - [`Status::BUFFER_TOO_SMALL`] The BufferSize is too small to |
| 136 | /// read the current directory entry. BufferSize has been updated with the |
| 137 | /// size needed to complete the request. |
| 138 | #[cfg(feature = "alloc")] |
| 139 | #[expect(clippy::extra_unused_lifetimes)] // false positive, it is used |
| 140 | pub fn load_file<'a>(&mut self, file_path: &DevicePath) -> Result<Box<[u8]>> { |
| 141 | let fetch_data_fn = |buf: &'a mut [u8]| { |
| 142 | let mut size = buf.len(); |
| 143 | // SAFETY: The memory is valid. |
| 144 | let status = unsafe { |
| 145 | (self.0.load_file)( |
| 146 | &raw mut self.0, |
| 147 | file_path.as_ffi_ptr().cast(), |
| 148 | Boolean::FALSE, /* always false - see spec */ |
| 149 | &mut size, |
| 150 | buf.as_mut_ptr().cast(), |
| 151 | ) |
| 152 | }; |
| 153 | // The firmware may write less than it announced, so only return |
| 154 | // the part it actually filled. |
| 155 | status |
| 156 | .to_result_with_err(|_| Some(size)) |
| 157 | .map(|_| &mut buf[..size]) |
| 158 | }; |
| 159 | |
| 160 | let file: Box<[u8]> = make_boxed::<[u8], _>(fetch_data_fn)?; |
| 161 | |
| 162 | Ok(file) |
| 163 | } |
| 164 | } |