Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/aienux/blob/main/vendor/uefi/src/proto/loaded_image.rs # aienux Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `aienux / vendor / uefi / src / proto / loaded_image.rs` 203 lines ยท 7372 bytes Raw Blame History 1 // SPDX-License-Identifier: MIT OR Apache-2.0 2 3 //! `LoadedImage` protocol. 4 5 use crate::data_types::FromSliceWithNulError; 6 use crate::mem::memory_map::MemoryType; 7 use crate::proto::device_path::DevicePath; 8 use crate::proto::unsafe_protocol; 9 use crate::util::usize_from_u32; 10 use crate::{CStr16, Handle, Status}; 11 use core::ffi::c_void; 12 use core::{mem, slice}; 13 use uefi_raw::protocol::loaded_image::LoadedImageProtocol; 14 15 /// The Loaded Image [`Protocol`]. 16 /// 17 /// This can be opened on any image handle using [`boot::open_protocol`], 18 /// for example. 19 /// 20 /// [`Protocol`]: uefi::proto::Protocol 21 /// [`boot::open_protocol`]: uefi::boot::open_protocol 22 #[derive(Debug)] 23 #[repr(transparent)] 24 #[unsafe_protocol(LoadedImageProtocol::GUID)] 25 pub struct LoadedImage(LoadedImageProtocol); 26 27 /// Errors that can be raised during parsing of the load options. 28 #[derive(Debug)] 29 pub enum LoadOptionsError { 30 /// Load options are not set. 31 NotSet, 32 33 /// The start and/or length of the load options is not [`u16`]-aligned. 34 NotAligned, 35 36 /// Not a valid null-terminated UCS-2 string. 37 InvalidString(FromSliceWithNulError), 38 } 39 40 impl LoadedImage { 41 /// Returns a handle to the storage device on which the image is located. 42 #[must_use] 43 pub fn device(&self) -> Option { 44 // SAFETY: The memory is valid. 45 unsafe { Handle::from_ptr(self.0.device_handle) } 46 } 47 48 /// Get a reference to the `file_path` portion of the DeviceHandle that the 49 /// EFI image was loaded from. 50 /// 51 /// For a full device path, consider using the [`LoadedImageDevicePath`] 52 /// protocol. 53 /// 54 /// Returns `None` if `file_path` is null. 55 /// 56 /// [`LoadedImageDevicePath`]: crate::proto::device_path::LoadedImageDevicePath 57 #[must_use] 58 pub fn file_path(&self) -> Option<&DevicePath> { 59 if self.0.file_path.is_null() { 60 None 61 } else { 62 // SAFETY: The memory is valid. 63 unsafe { Some(DevicePath::from_ffi_ptr(self.0.file_path.cast())) } 64 } 65 } 66 67 /// Get the load options of the image as a [`&CStr16`]. 68 /// 69 /// Load options are typically used to pass command-line options as 70 /// a null-terminated UCS-2 string. This format is not required 71 /// though; use [`load_options_as_bytes`] to access the raw bytes. 72 /// 73 /// [`&CStr16`]: `CStr16` 74 /// [`load_options_as_bytes`]: `Self::load_options_as_bytes` 75 pub fn load_options_as_cstr16(&self) -> Result<&CStr16, LoadOptionsError> { 76 let load_options_size = usize_from_u32(self.0.load_options_size); 77 78 if self.0.load_options.is_null() { 79 Err(LoadOptionsError::NotSet) 80 } else if !load_options_size.is_multiple_of(size_of::()) 81 || !(self.0.load_options as usize).is_multiple_of(align_of::()) 82 { 83 Err(LoadOptionsError::NotAligned) 84 } else { 85 // SAFETY: The memory is valid. 86 let s = unsafe { 87 slice::from_raw_parts( 88 self.0.load_options.cast::(), 89 load_options_size / size_of::(), 90 ) 91 }; 92 CStr16::from_u16_with_nul(s).map_err(LoadOptionsError::InvalidString) 93 } 94 } 95 96 /// Get the load options of the image as raw bytes. 97 /// 98 /// UEFI allows arbitrary binary data in load options, but typically 99 /// the data is a null-terminated UCS-2 string. Use 100 /// [`load_options_as_cstr16`] to more conveniently access the load 101 /// options as a string. 102 /// 103 /// Returns `None` if load options are not set. 104 /// 105 /// [`load_options_as_cstr16`]: `Self::load_options_as_cstr16` 106 #[must_use] 107 pub const fn load_options_as_bytes(&self) -> Option<&[u8]> { 108 if self.0.load_options.is_null() { 109 None 110 } else { 111 // SAFETY: The memory is valid. 112 unsafe { 113 Some(slice::from_raw_parts( 114 self.0.load_options.cast(), 115 usize_from_u32(self.0.load_options_size), 116 )) 117 } 118 } 119 } 120 121 /// Set the image data address and size. 122 /// 123 /// This is useful in the following scenario: 124 /// 1. Secure boot is enabled, so images loaded with `LoadImage` must be 125 /// signed with an appropriate key known to the firmware. 126 /// 2. The bootloader has its own key embedded, and uses that key to 127 /// verify the next stage. This key is not known to the firmware, so 128 /// the next stage's image can't be loaded with `LoadImage`. 129 /// 3. Since image handles are created by `LoadImage`, which we can't 130 /// call, we have to make use of an existing image handle -- the one 131 /// passed into the bootloader's entry function. By modifying that 132 /// image handle (after appropriately verifying the signature of the 133 /// new data), we can repurpose the image handle for the next stage. 134 /// 135 /// See [shim] for an example of this scenario. 136 /// 137 /// # Safety 138 /// 139 /// This function takes `data` as a raw pointer because the data is not 140 /// owned by `LoadedImage`. The caller must ensure that the memory lives 141 /// long enough. 142 /// 143 /// [shim]: https://github.com/rhboot/shim/blob/`4d64389c6c941d21548b06423b8131c872e3c3c7`/pe.c#L1143 144 pub const unsafe fn set_image(&mut self, data: *const c_void, size: u64) { 145 self.0.image_base = data; 146 self.0.image_size = size; 147 } 148 149 /// Registers a cleanup function that is called when [`boot::unload_image`] 150 /// is called. 151 /// 152 /// # Safety 153 /// 154 /// The registered function must reside in memory that is not freed until 155 /// after the image is unloaded. 156 /// 157 /// [`boot::unload_image`]: crate::boot::unload_image 158 pub unsafe fn set_unload( 159 &mut self, 160 unload: extern "efiapi" fn(image_handle: Handle) -> Status, 161 ) { 162 let unload: unsafe extern "efiapi" fn(image_handle: uefi_raw::Handle) -> uefi_raw::Status = 163 // SAFETY: The memory is valid. 164 unsafe { mem::transmute(unload) }; 165 self.0.unload = Some(unload); 166 } 167 168 /// Set the load options for the image. This can be used prior to 169 /// calling [`boot::start_image`] to control the command line 170 /// passed to the image. 171 /// 172 /// `size` is in bytes. 173 /// 174 /// # Safety 175 /// 176 /// This function takes `options` as a raw pointer because the 177 /// load options data is not owned by `LoadedImage`. The caller 178 /// must ensure that the memory lives long enough. 179 /// 180 /// [`boot::start_image`]: crate::boot::start_image 181 pub const unsafe fn set_load_options(&mut self, options: *const u8, size: u32) { 182 self.0.load_options = options.cast(); 183 self.0.load_options_size = size; 184 } 185 186 /// Returns the base address and the size in bytes of the loaded image. 187 #[must_use] 188 pub const fn info(&self) -> (*const c_void, u64) { 189 (self.0.image_base, self.0.image_size) 190 } 191 192 /// Returns the memory type that the image's code sections were loaded as. 193 #[must_use] 194 pub const fn code_type(&self) -> MemoryType { 195 self.0.image_code_type 196 } 197 198 /// Returns the memory type that the image's data sections were loaded as. 199 #[must_use] 200 pub const fn data_type(&self) -> MemoryType { 201 self.0.image_data_type 202 } 203 }