Skip to content

Commit 49eee3f

Browse files
authored
Merge pull request #1340 from nicholasbishop/bishop-pt-conv-2
uefi: Drop BootServices arg from device path <-> text conversions
2 parents fa36299 + edb859d commit 49eee3f

File tree

5 files changed

+35
-49
lines changed

5 files changed

+35
-49
lines changed

uefi-test-runner/examples/loaded_image.rs

-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ fn print_image_path(boot_services: &BootServices) -> Result {
4949
loaded_image.file_path().expect("File path is not set");
5050
let image_device_path_text = device_path_to_text
5151
.convert_device_path_to_text(
52-
boot_services,
5352
image_device_path,
5453
DisplayOnly(true),
5554
AllowShortcuts(false),

uefi-test-runner/src/proto/device_path.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ pub fn test(bt: &BootServices) {
4343
);
4444

4545
let text = device_path_to_text
46-
.convert_device_node_to_text(bt, path, DisplayOnly(true), AllowShortcuts(false))
46+
.convert_device_node_to_text(path, DisplayOnly(true), AllowShortcuts(false))
4747
.expect("Failed to convert device path to text");
4848
let text = &*text;
4949
info!("path name: {text}");
@@ -81,7 +81,7 @@ pub fn test(bt: &BootServices) {
8181

8282
let path_components = device_path
8383
.node_iter()
84-
.map(|node| node.to_string(bt, DisplayOnly(false), AllowShortcuts(false)))
84+
.map(|node| node.to_string(DisplayOnly(false), AllowShortcuts(false)))
8585
.map(|str| str.unwrap().to_string())
8686
.collect::<Vec<_>>();
8787

@@ -107,7 +107,7 @@ pub fn test(bt: &BootServices) {
107107

108108
// Test that to_string works for device_paths
109109
let path = device_path
110-
.to_string(bt, DisplayOnly(false), AllowShortcuts(false))
110+
.to_string(DisplayOnly(false), AllowShortcuts(false))
111111
.unwrap()
112112
.to_string();
113113

uefi/CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ how to integrate the `uefi` crate into them.
77
## Added
88
- Added `Handle::new`
99

10+
## Changed
11+
- **Breaking:** The conversion functions between device paths and text no longer
12+
take a `BootServices` argument. The global system table is used instead.
13+
14+
1015
# uefi - 0.31.0 (2024-08-21)
1116

1217
See [Deprecating SystemTable/BootServices/RuntimeServices][funcmigrate] for

uefi/src/proto/device_path/mod.rs

+9-16
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,8 @@ use ptr_meta::Pointee;
9090

9191
#[cfg(feature = "alloc")]
9292
use {
93+
crate::boot::{self, OpenProtocolAttributes, OpenProtocolParams, ScopedProtocol, SearchType},
9394
crate::proto::device_path::text::{AllowShortcuts, DevicePathToText, DisplayOnly},
94-
crate::table::boot::{
95-
BootServices, OpenProtocolAttributes, OpenProtocolParams, ScopedProtocol, SearchType,
96-
},
9795
crate::{CString16, Identify},
9896
alloc::borrow::ToOwned,
9997
alloc::boxed::Box,
@@ -232,14 +230,13 @@ impl DevicePathNode {
232230
#[cfg(feature = "alloc")]
233231
pub fn to_string(
234232
&self,
235-
bs: &BootServices,
236233
display_only: DisplayOnly,
237234
allow_shortcuts: AllowShortcuts,
238235
) -> Result<CString16, DevicePathToTextError> {
239-
let to_text_protocol = open_text_protocol(bs)?;
236+
let to_text_protocol = open_text_protocol()?;
240237

241238
to_text_protocol
242-
.convert_device_node_to_text(bs, self, display_only, allow_shortcuts)
239+
.convert_device_node_to_text(self, display_only, allow_shortcuts)
243240
.map(|pool_string| {
244241
let cstr16 = &*pool_string;
245242
// Another allocation; pool string is dropped. This overhead
@@ -484,14 +481,13 @@ impl DevicePath {
484481
#[cfg(feature = "alloc")]
485482
pub fn to_string(
486483
&self,
487-
bs: &BootServices,
488484
display_only: DisplayOnly,
489485
allow_shortcuts: AllowShortcuts,
490486
) -> Result<CString16, DevicePathToTextError> {
491-
let to_text_protocol = open_text_protocol(bs)?;
487+
let to_text_protocol = open_text_protocol()?;
492488

493489
to_text_protocol
494-
.convert_device_path_to_text(bs, self, display_only, allow_shortcuts)
490+
.convert_device_path_to_text(self, display_only, allow_shortcuts)
495491
.map(|pool_string| {
496492
let cstr16 = &*pool_string;
497493
// Another allocation; pool string is dropped. This overhead
@@ -878,20 +874,17 @@ impl core::error::Error for DevicePathToTextError {
878874
/// Helper function to open the [`DevicePathToText`] protocol using the boot
879875
/// services.
880876
#[cfg(feature = "alloc")]
881-
fn open_text_protocol(
882-
bs: &BootServices,
883-
) -> Result<ScopedProtocol<DevicePathToText>, DevicePathToTextError> {
884-
let &handle = bs
885-
.locate_handle_buffer(SearchType::ByProtocol(&DevicePathToText::GUID))
877+
fn open_text_protocol() -> Result<ScopedProtocol<DevicePathToText>, DevicePathToTextError> {
878+
let &handle = boot::locate_handle_buffer(SearchType::ByProtocol(&DevicePathToText::GUID))
886879
.map_err(DevicePathToTextError::CantLocateHandleBuffer)?
887880
.first()
888881
.ok_or(DevicePathToTextError::NoHandle)?;
889882

890883
unsafe {
891-
bs.open_protocol::<DevicePathToText>(
884+
boot::open_protocol::<DevicePathToText>(
892885
OpenProtocolParams {
893886
handle,
894-
agent: bs.image_handle(),
887+
agent: boot::image_handle(),
895888
controller: None,
896889
},
897890
OpenProtocolAttributes::GetProtocol,

uefi/src/proto/device_path/text.rs

+18-29
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010

1111
use crate::proto::device_path::{DevicePath, DevicePathNode};
1212
use crate::proto::unsafe_protocol;
13-
use crate::table::boot::BootServices;
14-
use crate::{CStr16, Char16, Result, Status};
13+
use crate::{boot, CStr16, Char16, Result, Status};
1514
use core::ops::Deref;
15+
use core::ptr::NonNull;
1616
use uefi_raw::protocol::device_path::{DevicePathFromTextProtocol, DevicePathToTextProtocol};
1717

1818
/// This struct is a wrapper of `display_only` parameter
@@ -42,36 +42,27 @@ pub struct AllowShortcuts(pub bool);
4242
/// Wrapper for a string internally allocated from
4343
/// UEFI boot services memory.
4444
#[derive(Debug)]
45-
pub struct PoolString<'a> {
46-
boot_services: &'a BootServices,
47-
text: *const Char16,
48-
}
45+
pub struct PoolString(NonNull<Char16>);
4946

50-
impl<'a> PoolString<'a> {
51-
fn new(boot_services: &'a BootServices, text: *const Char16) -> Result<Self> {
52-
if text.is_null() {
53-
Err(Status::OUT_OF_RESOURCES.into())
54-
} else {
55-
Ok(Self {
56-
boot_services,
57-
text,
58-
})
59-
}
47+
impl PoolString {
48+
fn new(text: *const Char16) -> Result<Self> {
49+
NonNull::new(text.cast_mut())
50+
.map(Self)
51+
.ok_or(Status::OUT_OF_RESOURCES.into())
6052
}
6153
}
6254

63-
impl<'a> Deref for PoolString<'a> {
55+
impl Deref for PoolString {
6456
type Target = CStr16;
6557

6658
fn deref(&self) -> &Self::Target {
67-
unsafe { CStr16::from_ptr(self.text) }
59+
unsafe { CStr16::from_ptr(self.0.as_ptr()) }
6860
}
6961
}
7062

71-
impl Drop for PoolString<'_> {
63+
impl Drop for PoolString {
7264
fn drop(&mut self) {
73-
let addr = self.text as *mut u8;
74-
unsafe { self.boot_services.free_pool(addr) }.expect("Failed to free pool [{addr:#?}]");
65+
unsafe { boot::free_pool(self.0.cast()) }.expect("Failed to free pool [{addr:#?}]");
7566
}
7667
}
7768

@@ -91,21 +82,20 @@ impl DevicePathToText {
9182
/// memory for the conversion.
9283
///
9384
/// [`OUT_OF_RESOURCES`]: Status::OUT_OF_RESOURCES
94-
pub fn convert_device_node_to_text<'boot>(
85+
pub fn convert_device_node_to_text(
9586
&self,
96-
boot_services: &'boot BootServices,
9787
device_node: &DevicePathNode,
9888
display_only: DisplayOnly,
9989
allow_shortcuts: AllowShortcuts,
100-
) -> Result<PoolString<'boot>> {
90+
) -> Result<PoolString> {
10191
let text_device_node = unsafe {
10292
(self.0.convert_device_node_to_text)(
10393
device_node.as_ffi_ptr().cast(),
10494
display_only.0,
10595
allow_shortcuts.0,
10696
)
10797
};
108-
PoolString::new(boot_services, text_device_node.cast())
98+
PoolString::new(text_device_node.cast())
10999
}
110100

111101
/// Convert a device path to its text representation.
@@ -114,21 +104,20 @@ impl DevicePathToText {
114104
/// memory for the conversion.
115105
///
116106
/// [`OUT_OF_RESOURCES`]: Status::OUT_OF_RESOURCES
117-
pub fn convert_device_path_to_text<'boot>(
107+
pub fn convert_device_path_to_text(
118108
&self,
119-
boot_services: &'boot BootServices,
120109
device_path: &DevicePath,
121110
display_only: DisplayOnly,
122111
allow_shortcuts: AllowShortcuts,
123-
) -> Result<PoolString<'boot>> {
112+
) -> Result<PoolString> {
124113
let text_device_path = unsafe {
125114
(self.0.convert_device_path_to_text)(
126115
device_path.as_ffi_ptr().cast(),
127116
display_only.0,
128117
allow_shortcuts.0,
129118
)
130119
};
131-
PoolString::new(boot_services, text_device_path.cast())
120+
PoolString::new(text_device_path.cast())
132121
}
133122
}
134123

0 commit comments

Comments
 (0)