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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
use crate::{
proto::device_path::{DevicePath, DevicePathNode, FfiDevicePath},
proto::Protocol,
table::boot::BootServices,
unsafe_guid, CStr16, Char16,
};
use core::ops::Deref;
#[derive(Clone, Copy)]
pub struct DisplayOnly(pub bool);
#[derive(Clone, Copy)]
pub struct AllowShortcuts(pub bool);
pub struct PoolString<'a> {
boot_services: &'a BootServices,
text: *const Char16,
}
impl<'a> PoolString<'a> {
fn new(boot_services: &'a BootServices, text: *const Char16) -> Option<Self> {
if text.is_null() {
None
} else {
Some(Self {
boot_services,
text,
})
}
}
}
impl<'a> Deref for PoolString<'a> {
type Target = CStr16;
fn deref(&self) -> &Self::Target {
unsafe { CStr16::from_ptr(self.text) }
}
}
impl Drop for PoolString<'_> {
fn drop(&mut self) {
let addr = self.text as *mut u8;
self.boot_services
.free_pool(addr)
.expect("Failed to free pool [{addr:#?}]");
}
}
#[repr(C)]
#[unsafe_guid("8b843e20-8132-4852-90cc-551a4e4a7f1c")]
#[derive(Protocol)]
pub struct DevicePathToText {
convert_device_node_to_text: unsafe extern "efiapi" fn(
device_node: *const FfiDevicePath,
display_only: bool,
allow_shortcuts: bool,
) -> *const Char16,
convert_device_path_to_text: unsafe extern "efiapi" fn(
device_path: *const FfiDevicePath,
display_only: bool,
allow_shortcuts: bool,
) -> *const Char16,
}
impl DevicePathToText {
pub fn convert_device_node_to_text<'boot>(
&self,
boot_services: &'boot BootServices,
device_node: &DevicePathNode,
display_only: DisplayOnly,
allow_shortcuts: AllowShortcuts,
) -> Option<PoolString<'boot>> {
let text_device_node = unsafe {
(self.convert_device_node_to_text)(
device_node.as_ffi_ptr(),
display_only.0,
allow_shortcuts.0,
)
};
PoolString::new(boot_services, text_device_node)
}
pub fn convert_device_path_to_text<'boot>(
&self,
boot_services: &'boot BootServices,
device_path: &DevicePath,
display_only: DisplayOnly,
allow_shortcuts: AllowShortcuts,
) -> Option<PoolString<'boot>> {
let text_device_path = unsafe {
(self.convert_device_path_to_text)(
device_path.as_ffi_ptr(),
display_only.0,
allow_shortcuts.0,
)
};
PoolString::new(boot_services, text_device_path)
}
}
#[repr(C)]
#[unsafe_guid("05c99a21-c70f-4ad2-8a5f-35df3343f51e")]
#[derive(Protocol)]
pub struct DevicePathFromText {
convert_text_to_device_node:
unsafe extern "efiapi" fn(text_device_node: *const Char16) -> *const FfiDevicePath,
convert_text_to_device_path:
unsafe extern "efiapi" fn(text_device_path: *const Char16) -> *const FfiDevicePath,
}
impl DevicePathFromText {
pub fn convert_text_to_device_node(
&self,
text_device_node: &CStr16,
) -> Option<&DevicePathNode> {
unsafe {
let ptr = (self.convert_text_to_device_node)(text_device_node.as_ptr());
if ptr.is_null() {
None
} else {
Some(DevicePathNode::from_ffi_ptr(ptr))
}
}
}
pub fn convert_text_to_device_path(&self, text_device_path: &CStr16) -> Option<&DevicePath> {
unsafe {
let ptr = (self.convert_text_to_device_path)(text_device_path.as_ptr());
if ptr.is_null() {
None
} else {
Some(DevicePath::from_ffi_ptr(ptr))
}
}
}
}