Skip to content

Commit 40800ac

Browse files
authored
Merge pull request #1624 from JarlEvanson/pool-string
uefi: Move PoolString to enable additional use
2 parents 53dd8b3 + 9d97973 commit 40800ac

File tree

4 files changed

+46
-31
lines changed

4 files changed

+46
-31
lines changed

uefi/src/boot.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1318,7 +1318,7 @@ unsafe fn get_memory_map_and_exit_boot_services(buf: &mut [u8]) -> Result<Memory
13181318
///
13191319
/// [`helpers`]: crate::helpers
13201320
/// [`Output`]: crate::proto::console::text::Output
1321-
/// [`PoolString`]: crate::proto::device_path::text::PoolString
1321+
/// [`PoolString`]: crate::data_types::PoolString
13221322
#[must_use]
13231323
pub unsafe fn exit_boot_services(custom_memory_type: Option<MemoryType>) -> MemoryMapOwned {
13241324
// LOADER_DATA is the default and also used by the Linux kernel:

uefi/src/data_types/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,8 @@ mod opaque;
169169

170170
mod strs;
171171
pub use strs::{
172-
CStr16, CStr8, EqStrUntilNul, FromSliceWithNulError, FromStrWithBufError, UnalignedCStr16Error,
172+
CStr16, CStr8, EqStrUntilNul, FromSliceWithNulError, FromStrWithBufError, PoolString,
173+
UnalignedCStr16Error,
173174
};
174175

175176
/// These functions are used in the implementation of the [`cstr8`] macro.

uefi/src/data_types/strs.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
// SPDX-License-Identifier: MIT OR Apache-2.0
22

3+
use uefi_raw::Status;
4+
35
use super::chars::{Char16, Char8, NUL_16, NUL_8};
46
use super::UnalignedSlice;
7+
use crate::mem::PoolAllocation;
58
use crate::polyfill::maybe_uninit_slice_assume_init_ref;
69
use core::borrow::Borrow;
710
use core::ffi::CStr;
811
use core::fmt::{self, Display, Formatter};
912
use core::mem::MaybeUninit;
13+
use core::ops::Deref;
14+
use core::ptr::NonNull;
1015
use core::{ptr, slice};
1116

1217
#[cfg(feature = "alloc")]
@@ -718,6 +723,40 @@ impl PartialEq<CString16> for &CStr16 {
718723
}
719724
}
720725

726+
/// UCS-2 string allocated from UEFI pool memory.
727+
///
728+
/// This is similar to a [`CString16`], but used for memory that was allocated
729+
/// internally by UEFI rather than the Rust allocator.
730+
///
731+
/// [`CString16`]: crate::CString16
732+
#[derive(Debug)]
733+
pub struct PoolString(PoolAllocation);
734+
735+
impl PoolString {
736+
/// Create a [`PoolString`] from a [`CStr16`] residing in a buffer allocated
737+
/// using [`allocate_pool()`][cbap].
738+
///
739+
/// # Safety
740+
///
741+
/// The caller must ensure that the buffer points to a valid [`CStr16`] and
742+
/// resides in a buffer allocated using [`allocate_pool()`][cbap]
743+
///
744+
/// [cbap]: crate::boot::allocate_pool()
745+
pub unsafe fn new(text: *const Char16) -> crate::Result<Self> {
746+
NonNull::new(text.cast_mut())
747+
.map(|p| Self(PoolAllocation::new(p.cast())))
748+
.ok_or(Status::OUT_OF_RESOURCES.into())
749+
}
750+
}
751+
752+
impl Deref for PoolString {
753+
type Target = CStr16;
754+
755+
fn deref(&self) -> &Self::Target {
756+
unsafe { CStr16::from_ptr(self.0.as_ptr().as_ptr().cast()) }
757+
}
758+
}
759+
721760
impl UnalignedSlice<'_, u16> {
722761
/// Create a [`CStr16`] from an [`UnalignedSlice`] using an aligned
723762
/// buffer for storage. The lifetime of the output is tied to `buf`,

uefi/src/proto/device_path/text.rs

Lines changed: 4 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@
1010
// if there is insufficient memory. So we treat any NULL output as an
1111
// `OUT_OF_RESOURCES` error.
1212

13+
use crate::data_types::PoolString;
1314
use crate::mem::PoolAllocation;
1415
use crate::proto::device_path::{DevicePath, DevicePathNode};
1516
use crate::proto::unsafe_protocol;
16-
use crate::{CStr16, Char16, Result, Status};
17-
use core::ops::Deref;
17+
use crate::{CStr16, Result, Status};
1818
use core::ptr::NonNull;
1919
use uefi_raw::protocol::device_path::{DevicePathFromTextProtocol, DevicePathToTextProtocol};
2020

@@ -47,31 +47,6 @@ pub struct DisplayOnly(pub bool);
4747
#[derive(Clone, Copy, Debug)]
4848
pub struct AllowShortcuts(pub bool);
4949

50-
/// UCS-2 string allocated from UEFI pool memory.
51-
///
52-
/// This is similar to a [`CString16`], but used for memory that was allocated
53-
/// internally by UEFI rather than the Rust allocator.
54-
///
55-
/// [`CString16`]: crate::CString16
56-
#[derive(Debug)]
57-
pub struct PoolString(PoolAllocation);
58-
59-
impl PoolString {
60-
fn new(text: *const Char16) -> Result<Self> {
61-
NonNull::new(text.cast_mut())
62-
.map(|p| Self(PoolAllocation::new(p.cast())))
63-
.ok_or(Status::OUT_OF_RESOURCES.into())
64-
}
65-
}
66-
67-
impl Deref for PoolString {
68-
type Target = CStr16;
69-
70-
fn deref(&self) -> &Self::Target {
71-
unsafe { CStr16::from_ptr(self.0.as_ptr().as_ptr().cast()) }
72-
}
73-
}
74-
7550
/// Protocol for converting a [`DevicePath`] or `DevicePathNode`] to a string.
7651
#[derive(Debug)]
7752
#[repr(transparent)]
@@ -98,7 +73,7 @@ impl DevicePathToText {
9873
allow_shortcuts.0.into(),
9974
)
10075
};
101-
PoolString::new(text.cast())
76+
unsafe { PoolString::new(text.cast()) }
10277
}
10378

10479
/// Convert a [`DevicePath`] to a [`PoolString`].
@@ -120,7 +95,7 @@ impl DevicePathToText {
12095
allow_shortcuts.0.into(),
12196
)
12297
};
123-
PoolString::new(text.cast())
98+
unsafe { PoolString::new(text.cast()) }
12499
}
125100
}
126101

0 commit comments

Comments
 (0)