interplane.Get Omega
Read in your language

aienux

Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.

Brought by drakestapleton

branch main Branches Tags History

aienux / vendor / uefi / src / proto / device_path / text.rs

159 lines · 5806 bytes Raw Blame History
1// SPDX-License-Identifier: MIT OR Apache-2.0
2
3//! Protocols for converting between UEFI strings and [`DevicePath`]/[`DevicePathNode`].
4
5// Note on return types: the specification of the conversion functions
6// is a little unusual in that they return a pointer rather than
7// `EFI_STATUS`. A NULL pointer is used to indicate an error, and the
8// spec says that will only happen if the input pointer is null (which
9// can't happen here since we use references as input, not pointers), or
10// if there is insufficient memory. So we treat any NULL output as an
11// `OUT_OF_RESOURCES` error.
12
13use crate::data_types::PoolString;
14use crate::mem::PoolAllocation;
15use crate::proto::device_path::{DevicePath, DevicePathNode};
16use crate::proto::unsafe_protocol;
17use crate::{CStr16, Result, Status};
18use core::ptr::NonNull;
19use uefi_raw::protocol::device_path::{DevicePathFromTextProtocol, DevicePathToTextProtocol};
20
21use super::{PoolDevicePath, PoolDevicePathNode};
22
23/// Parameter for [`DevicePathToText`] that alters the output format.
24///
25/// * `DisplayOnly(false)` produces parseable output.
26/// * `DisplayOnly(true)` produces output that _may_ be shorter and not
27/// parseable.
28///
29/// Example of how a node's text representation may be altered by this
30/// parameter:
31/// * `DisplayOnly(false)`: `Ata(Primary,Master,0x1)`
32/// * `DisplayOnly(true)`: `Ata(0x1)`
33#[derive(Clone, Copy, Debug)]
34pub struct DisplayOnly(pub bool);
35
36/// Parameter for [`DevicePathToText`] that alters the output format.
37///
38/// * `AllowShortcuts(false)`: node names are based only on the node's type and
39/// subtype.
40/// * `AllowShortcuts(true)` _may_ alter the node name based on other fields
41/// within the node.
42///
43/// Example of how a node's text representation may be altered by this
44/// parameter:
45/// * `AllowShortcuts(false)`: `VenMsg(E0C14753-F9BE-11D2-9A0C-0090273FC14D)`
46/// * `AllowShortcuts(true)`: `VenPcAnsi()`
47#[derive(Clone, Copy, Debug)]
48pub struct AllowShortcuts(pub bool);
49
50/// Device Path to Text [`Protocol`].
51///
52/// Protocol for converting a [`DevicePath`] or `DevicePathNode`] to a string.
53///
54/// [`Protocol`]: uefi::proto::Protocol
55#[derive(Debug)]
56#[repr(transparent)]
57#[unsafe_protocol(DevicePathToTextProtocol::GUID)]
58pub struct DevicePathToText(DevicePathToTextProtocol);
59
60impl DevicePathToText {
61 /// Convert a [`DevicePathNode`] to a [`PoolString`].
62 ///
63 /// Returns an [`OUT_OF_RESOURCES`] error if there is insufficient
64 /// memory for the conversion.
65 ///
66 /// [`OUT_OF_RESOURCES`]: Status::OUT_OF_RESOURCES
67 pub fn convert_device_node_to_text(
68 &self,
69 device_node: &DevicePathNode,
70 display_only: DisplayOnly,
71 allow_shortcuts: AllowShortcuts,
72 ) -> Result<PoolString> {
73 // SAFETY: The memory is valid.
74 let text = unsafe {
75 (self.0.convert_device_node_to_text)(
76 device_node.as_ffi_ptr().cast(),
77 display_only.0.into(),
78 allow_shortcuts.0.into(),
79 )
80 };
81 // SAFETY: The memory is valid.
82 unsafe { PoolString::new(text.cast()) }
83 }
84
85 /// Convert a [`DevicePath`] to a [`PoolString`].
86 ///
87 /// Returns an [`OUT_OF_RESOURCES`] error if there is insufficient
88 /// memory for the conversion.
89 ///
90 /// [`OUT_OF_RESOURCES`]: Status::OUT_OF_RESOURCES
91 pub fn convert_device_path_to_text(
92 &self,
93 device_path: &DevicePath,
94 display_only: DisplayOnly,
95 allow_shortcuts: AllowShortcuts,
96 ) -> Result<PoolString> {
97 // SAFETY: The memory is valid.
98 let text = unsafe {
99 (self.0.convert_device_path_to_text)(
100 device_path.as_ffi_ptr().cast(),
101 display_only.0.into(),
102 allow_shortcuts.0.into(),
103 )
104 };
105 // SAFETY: The memory is valid.
106 unsafe { PoolString::new(text.cast()) }
107 }
108}
109
110/// Device Path from Text [`Protocol`].
111///
112/// Protocol for converting a string to a [`DevicePath`] or `DevicePathNode`].
113///
114/// [`Protocol`]: uefi::proto::Protocol
115#[derive(Debug)]
116#[repr(transparent)]
117#[unsafe_protocol("05c99a21-c70f-4ad2-8a5f-35df3343f51e")]
118pub struct DevicePathFromText(DevicePathFromTextProtocol);
119
120impl DevicePathFromText {
121 /// Convert a [`CStr16`] to a [`DevicePathNode`].
122 ///
123 /// If a non-device-node character is encountered, the rest of the string is ignored.
124 ///
125 /// Returns an [`OUT_OF_RESOURCES`] error if there is insufficient
126 /// memory for the conversion.
127 ///
128 /// [`OUT_OF_RESOURCES`]: Status::OUT_OF_RESOURCES
129 pub fn convert_text_to_device_node(
130 &self,
131 text_device_node: &CStr16,
132 ) -> Result<PoolDevicePathNode> {
133 // SAFETY: The memory is valid.
134 unsafe {
135 let ptr = (self.0.convert_text_to_device_node)(text_device_node.as_ptr().cast());
136 NonNull::new(ptr)
137 .map(|p| PoolDevicePathNode(PoolAllocation::new(p.cast())))
138 .ok_or_else(|| Status::OUT_OF_RESOURCES.into())
139 }
140 }
141
142 /// Convert a [`CStr16`] to a [`DevicePath`].
143 ///
144 /// If a non-device-node character is encountered, the rest of the string is ignored.
145 ///
146 /// Returns an [`OUT_OF_RESOURCES`] error if there is insufficient
147 /// memory for the conversion.
148 ///
149 /// [`OUT_OF_RESOURCES`]: Status::OUT_OF_RESOURCES
150 pub fn convert_text_to_device_path(&self, text_device_path: &CStr16) -> Result<PoolDevicePath> {
151 // SAFETY: The memory is valid.
152 unsafe {
153 let ptr = (self.0.convert_text_to_device_path)(text_device_path.as_ptr().cast());
154 NonNull::new(ptr)
155 .map(|p| PoolDevicePath(PoolAllocation::new(p.cast())))
156 .ok_or_else(|| Status::OUT_OF_RESOURCES.into())
157 }
158 }
159}