Skip to content

Commit a0e9238

Browse files
Ayush1325gitbot
authored and
gitbot
committed
uefi: helpers: Introduce OwnedDevicePath
This PR is split off from rust-lang#135368 to reduce noise. Rename DevicePath to OwnedDevicePath. This is to allow a non-owning version of DevicePath in the future to work with UEFI shell APIs which provide const pointers to device paths for UEFI shell fs mapping. Also implement Debug for OwnedDevicePath for some quality of life improvements. Signed-off-by: Ayush Singh <ayush@beagleboard.org>
1 parent 5534aa6 commit a0e9238

File tree

2 files changed

+16
-7
lines changed

2 files changed

+16
-7
lines changed

std/src/sys/pal/uefi/helpers.rs

+15-6
Original file line numberDiff line numberDiff line change
@@ -222,14 +222,14 @@ pub(crate) fn runtime_services() -> Option<NonNull<r_efi::efi::RuntimeServices>>
222222
NonNull::new(runtime_services)
223223
}
224224

225-
pub(crate) struct DevicePath(NonNull<r_efi::protocols::device_path::Protocol>);
225+
pub(crate) struct OwnedDevicePath(NonNull<r_efi::protocols::device_path::Protocol>);
226226

227-
impl DevicePath {
227+
impl OwnedDevicePath {
228228
pub(crate) fn from_text(p: &OsStr) -> io::Result<Self> {
229229
fn inner(
230230
p: &OsStr,
231231
protocol: NonNull<r_efi::protocols::device_path_from_text::Protocol>,
232-
) -> io::Result<DevicePath> {
232+
) -> io::Result<OwnedDevicePath> {
233233
let path_vec = p.encode_wide().chain(Some(0)).collect::<Vec<u16>>();
234234
if path_vec[..path_vec.len() - 1].contains(&0) {
235235
return Err(const_error!(
@@ -242,7 +242,7 @@ impl DevicePath {
242242
unsafe { ((*protocol.as_ptr()).convert_text_to_device_path)(path_vec.as_ptr()) };
243243

244244
NonNull::new(path)
245-
.map(DevicePath)
245+
.map(OwnedDevicePath)
246246
.ok_or_else(|| const_error!(io::ErrorKind::InvalidFilename, "Invalid Device Path"))
247247
}
248248

@@ -275,12 +275,12 @@ impl DevicePath {
275275
))
276276
}
277277

278-
pub(crate) fn as_ptr(&self) -> *mut r_efi::protocols::device_path::Protocol {
278+
pub(crate) const fn as_ptr(&self) -> *mut r_efi::protocols::device_path::Protocol {
279279
self.0.as_ptr()
280280
}
281281
}
282282

283-
impl Drop for DevicePath {
283+
impl Drop for OwnedDevicePath {
284284
fn drop(&mut self) {
285285
if let Some(bt) = boot_services() {
286286
let bt: NonNull<r_efi::efi::BootServices> = bt.cast();
@@ -291,6 +291,15 @@ impl Drop for DevicePath {
291291
}
292292
}
293293

294+
impl crate::fmt::Debug for OwnedDevicePath {
295+
fn fmt(&self, f: &mut crate::fmt::Formatter<'_>) -> crate::fmt::Result {
296+
match device_path_to_text(self.0) {
297+
Ok(p) => p.fmt(f),
298+
Err(_) => f.debug_struct("OwnedDevicePath").finish_non_exhaustive(),
299+
}
300+
}
301+
}
302+
294303
pub(crate) struct OwnedProtocol<T> {
295304
guid: r_efi::efi::Guid,
296305
handle: NonNull<crate::ffi::c_void>,

std/src/sys/pal/uefi/process.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ mod uefi_command_internal {
326326

327327
impl Image {
328328
pub fn load_image(p: &OsStr) -> io::Result<Self> {
329-
let path = helpers::DevicePath::from_text(p)?;
329+
let path = helpers::OwnedDevicePath::from_text(p)?;
330330
let boot_services: NonNull<r_efi::efi::BootServices> = boot_services()
331331
.ok_or_else(|| const_error!(io::ErrorKind::NotFound, "Boot Services not found"))?
332332
.cast();

0 commit comments

Comments
 (0)