Skip to content

uefi: Move PoolString to enable additional use #1624

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion uefi/src/boot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1318,7 +1318,7 @@ unsafe fn get_memory_map_and_exit_boot_services(buf: &mut [u8]) -> Result<Memory
///
/// [`helpers`]: crate::helpers
/// [`Output`]: crate::proto::console::text::Output
/// [`PoolString`]: crate::proto::device_path::text::PoolString
/// [`PoolString`]: crate::data_types::PoolString
#[must_use]
pub unsafe fn exit_boot_services(custom_memory_type: Option<MemoryType>) -> MemoryMapOwned {
// LOADER_DATA is the default and also used by the Linux kernel:
Expand Down
3 changes: 2 additions & 1 deletion uefi/src/data_types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,8 @@ mod opaque;

mod strs;
pub use strs::{
CStr16, CStr8, EqStrUntilNul, FromSliceWithNulError, FromStrWithBufError, UnalignedCStr16Error,
CStr16, CStr8, EqStrUntilNul, FromSliceWithNulError, FromStrWithBufError, PoolString,
UnalignedCStr16Error,
};

/// These functions are used in the implementation of the [`cstr8`] macro.
Expand Down
39 changes: 39 additions & 0 deletions uefi/src/data_types/strs.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
// SPDX-License-Identifier: MIT OR Apache-2.0

use uefi_raw::Status;

use super::chars::{Char16, Char8, NUL_16, NUL_8};
use super::UnalignedSlice;
use crate::mem::PoolAllocation;
use crate::polyfill::maybe_uninit_slice_assume_init_ref;
use core::borrow::Borrow;
use core::ffi::CStr;
use core::fmt::{self, Display, Formatter};
use core::mem::MaybeUninit;
use core::ops::Deref;
use core::ptr::NonNull;
use core::{ptr, slice};

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

/// UCS-2 string allocated from UEFI pool memory.
///
/// This is similar to a [`CString16`], but used for memory that was allocated
/// internally by UEFI rather than the Rust allocator.
///
/// [`CString16`]: crate::CString16
#[derive(Debug)]
pub struct PoolString(PoolAllocation);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wait. Isn't there a Drop implementation for this type? 🤔 Or am I missing something in the Gitlab UI (on my smartphone)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Drop implementation is taken care of by the PoolAllocation inside.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahh yes, thanks


impl PoolString {
/// Create a [`PoolString`] from a [`CStr16`] residing in a buffer allocated
/// using [`allocate_pool()`][cbap].
///
/// # Safety
///
/// The caller must ensure that the buffer points to a valid [`CStr16`] and
/// resides in a buffer allocated using [`allocate_pool()`][cbap]
///
/// [cbap]: crate::boot::allocate_pool()
pub unsafe fn new(text: *const Char16) -> crate::Result<Self> {
NonNull::new(text.cast_mut())
.map(|p| Self(PoolAllocation::new(p.cast())))
.ok_or(Status::OUT_OF_RESOURCES.into())
}
}

impl Deref for PoolString {
type Target = CStr16;

fn deref(&self) -> &Self::Target {
unsafe { CStr16::from_ptr(self.0.as_ptr().as_ptr().cast()) }
}
}

impl UnalignedSlice<'_, u16> {
/// Create a [`CStr16`] from an [`UnalignedSlice`] using an aligned
/// buffer for storage. The lifetime of the output is tied to `buf`,
Expand Down
33 changes: 4 additions & 29 deletions uefi/src/proto/device_path/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
// if there is insufficient memory. So we treat any NULL output as an
// `OUT_OF_RESOURCES` error.

use crate::data_types::PoolString;
use crate::mem::PoolAllocation;
use crate::proto::device_path::{DevicePath, DevicePathNode};
use crate::proto::unsafe_protocol;
use crate::{CStr16, Char16, Result, Status};
use core::ops::Deref;
use crate::{CStr16, Result, Status};
use core::ptr::NonNull;
use uefi_raw::protocol::device_path::{DevicePathFromTextProtocol, DevicePathToTextProtocol};

Expand Down Expand Up @@ -47,31 +47,6 @@ pub struct DisplayOnly(pub bool);
#[derive(Clone, Copy, Debug)]
pub struct AllowShortcuts(pub bool);

/// UCS-2 string allocated from UEFI pool memory.
///
/// This is similar to a [`CString16`], but used for memory that was allocated
/// internally by UEFI rather than the Rust allocator.
///
/// [`CString16`]: crate::CString16
#[derive(Debug)]
pub struct PoolString(PoolAllocation);

impl PoolString {
fn new(text: *const Char16) -> Result<Self> {
NonNull::new(text.cast_mut())
.map(|p| Self(PoolAllocation::new(p.cast())))
.ok_or(Status::OUT_OF_RESOURCES.into())
}
}

impl Deref for PoolString {
type Target = CStr16;

fn deref(&self) -> &Self::Target {
unsafe { CStr16::from_ptr(self.0.as_ptr().as_ptr().cast()) }
}
}

/// Protocol for converting a [`DevicePath`] or `DevicePathNode`] to a string.
#[derive(Debug)]
#[repr(transparent)]
Expand All @@ -98,7 +73,7 @@ impl DevicePathToText {
allow_shortcuts.0.into(),
)
};
PoolString::new(text.cast())
unsafe { PoolString::new(text.cast()) }
}

/// Convert a [`DevicePath`] to a [`PoolString`].
Expand All @@ -120,7 +95,7 @@ impl DevicePathToText {
allow_shortcuts.0.into(),
)
};
PoolString::new(text.cast())
unsafe { PoolString::new(text.cast()) }
}
}

Expand Down