|
| 1 | +//! `DevicePathToText` and `DevicePathFromText` Protocol |
| 2 | +
|
| 3 | +use crate::{ |
| 4 | + proto::{device_path::DevicePath, Protocol}, |
| 5 | + table::boot::BootServices, |
| 6 | + unsafe_guid, CStr16, Char16, |
| 7 | +}; |
| 8 | +use core::ops::Deref; |
| 9 | + |
| 10 | +/// This struct is a wrapper of `display_only` parameter |
| 11 | +/// used by Device Path to Text protocol. |
| 12 | +/// |
| 13 | +/// The `display_only` parameter controls whether the longer |
| 14 | +/// (parseable) or shorter (display-only) form of the conversion |
| 15 | +/// is used. If `display_only` is TRUE, then the shorter text |
| 16 | +/// representation of the display node is used, where applicable. |
| 17 | +/// If `display_only` is FALSE, then the longer text representation |
| 18 | +/// of the display node is used. |
| 19 | +#[derive(Clone, Copy)] |
| 20 | +pub struct DisplayOnly(pub bool); |
| 21 | + |
| 22 | +/// This struct is a wrapper of `allow_shortcuts` parameter |
| 23 | +/// used by Device Path to Text protocol. |
| 24 | +/// |
| 25 | +/// The `allow_shortcuts` is FALSE, then the shortcut forms of |
| 26 | +/// text representation for a device node cannot be used. A |
| 27 | +/// shortcut form is one which uses information other than the |
| 28 | +/// type or subtype. If `allow_shortcuts is TRUE, then the |
| 29 | +/// shortcut forms of text representation for a device node |
| 30 | +/// can be used, where applicable. |
| 31 | +#[derive(Clone, Copy)] |
| 32 | +pub struct AllowShortcuts(pub bool); |
| 33 | + |
| 34 | +/// Wrapper for a string internally allocated from |
| 35 | +/// UEFI boot services memory. |
| 36 | +pub struct PoolString<'a> { |
| 37 | + boot_services: &'a BootServices, |
| 38 | + text: *const Char16, |
| 39 | +} |
| 40 | + |
| 41 | +impl<'a> PoolString<'a> { |
| 42 | + fn new(boot_services: &'a BootServices, text: *const Char16) -> Option<Self> { |
| 43 | + if text.is_null() { |
| 44 | + None |
| 45 | + } else { |
| 46 | + Some(Self { |
| 47 | + boot_services, |
| 48 | + text, |
| 49 | + }) |
| 50 | + } |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +impl<'a> Deref for PoolString<'a> { |
| 55 | + type Target = CStr16; |
| 56 | + |
| 57 | + fn deref(&self) -> &Self::Target { |
| 58 | + unsafe { CStr16::from_ptr(self.text) } |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +impl Drop for PoolString<'_> { |
| 63 | + fn drop(&mut self) { |
| 64 | + let addr = self.text as *mut u8; |
| 65 | + self.boot_services |
| 66 | + .free_pool(addr) |
| 67 | + .expect("Failed to free pool [{addr:#?}]"); |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +/// Device Path to Text protocol. |
| 72 | +/// |
| 73 | +/// This protocol provides common utility functions for converting device |
| 74 | +/// nodes and device paths to a text representation. |
| 75 | +#[repr(C)] |
| 76 | +#[unsafe_guid("8b843e20-8132-4852-90cc-551a4e4a7f1c")] |
| 77 | +#[derive(Protocol)] |
| 78 | +pub struct DevicePathToText { |
| 79 | + convert_device_node_to_text: unsafe extern "efiapi" fn( |
| 80 | + device_node: *const DevicePath, |
| 81 | + display_only: bool, |
| 82 | + allow_shortcuts: bool, |
| 83 | + ) -> *const Char16, |
| 84 | + convert_device_path_to_text: unsafe extern "efiapi" fn( |
| 85 | + device_path: *const DevicePath, |
| 86 | + display_only: bool, |
| 87 | + allow_shortcuts: bool, |
| 88 | + ) -> *const Char16, |
| 89 | +} |
| 90 | + |
| 91 | +impl DevicePathToText { |
| 92 | + /// Convert a device node to its text representation. |
| 93 | + /// |
| 94 | + /// Returns `None` if `device_node` was NULL or there was |
| 95 | + /// insufficient memory. |
| 96 | + pub fn convert_device_node_to_text<'boot>( |
| 97 | + &self, |
| 98 | + boot_services: &'boot BootServices, |
| 99 | + device_node: &DevicePath, |
| 100 | + display_only: DisplayOnly, |
| 101 | + allow_shortcuts: AllowShortcuts, |
| 102 | + ) -> Option<PoolString<'boot>> { |
| 103 | + let text_device_node = unsafe { |
| 104 | + (self.convert_device_node_to_text)(device_node, display_only.0, allow_shortcuts.0) |
| 105 | + }; |
| 106 | + PoolString::new(boot_services, text_device_node) |
| 107 | + } |
| 108 | + |
| 109 | + /// Convert a device path to its text representation. |
| 110 | + /// |
| 111 | + /// Returns `None` if `device_path` was NULL or there was |
| 112 | + /// insufficient memory. |
| 113 | + pub fn convert_device_path_to_text<'boot>( |
| 114 | + &self, |
| 115 | + boot_services: &'boot BootServices, |
| 116 | + device_path: &DevicePath, |
| 117 | + display_only: DisplayOnly, |
| 118 | + allow_shortcuts: AllowShortcuts, |
| 119 | + ) -> Option<PoolString<'boot>> { |
| 120 | + let text_device_path = unsafe { |
| 121 | + (self.convert_device_path_to_text)(device_path, display_only.0, allow_shortcuts.0) |
| 122 | + }; |
| 123 | + PoolString::new(boot_services, text_device_path) |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +/// Device Path from Text protocol. |
| 128 | +/// |
| 129 | +/// This protocol provides common utilities for converting text to |
| 130 | +/// device paths and device nodes. |
| 131 | +#[repr(C)] |
| 132 | +#[unsafe_guid("05c99a21-c70f-4ad2-8a5f-35df3343f51e")] |
| 133 | +#[derive(Protocol)] |
| 134 | +pub struct DevicePathFromText { |
| 135 | + convert_text_to_device_node: |
| 136 | + unsafe extern "efiapi" fn(text_device_node: *const Char16) -> *const DevicePath, |
| 137 | + convert_text_to_device_path: |
| 138 | + unsafe extern "efiapi" fn(text_device_path: *const Char16) -> *const DevicePath, |
| 139 | +} |
| 140 | + |
| 141 | +impl DevicePathFromText { |
| 142 | + /// Convert text to the binary representation of a device node. |
| 143 | + /// |
| 144 | + /// `text_device_node` is the text representation of a device node. |
| 145 | + /// Conversion starts with the first character and continues until |
| 146 | + /// the first non-device node character. |
| 147 | + /// |
| 148 | + /// Returns `None` if `text_device_node` was NULL or there was |
| 149 | + /// insufficient memory. |
| 150 | + pub fn convert_text_to_device_node(&self, text_device_node: &CStr16) -> Option<&DevicePath> { |
| 151 | + unsafe { (self.convert_text_to_device_node)(text_device_node.as_ptr()).as_ref() } |
| 152 | + } |
| 153 | + |
| 154 | + /// Convert a text to its binary device path representation. |
| 155 | + /// |
| 156 | + /// `text_device_path` is the text representation of a device path. |
| 157 | + /// Conversion starts with the first character and continues until |
| 158 | + /// the first non-device path character. |
| 159 | + /// |
| 160 | + /// Returns `None` if `text_device_path` was NULL or there was |
| 161 | + /// insufficient memory. |
| 162 | + pub fn convert_text_to_device_path(&self, text_device_path: &CStr16) -> Option<&DevicePath> { |
| 163 | + unsafe { (self.convert_text_to_device_path)(text_device_path.as_ptr()).as_ref() } |
| 164 | + } |
| 165 | +} |
0 commit comments