8000 uefi-raw: unified boolean type by phip1611 · Pull Request #1307 · rust-osdev/uefi-rs · GitHub
[go: up one dir, main page]

Skip to content

uefi-raw: unified boolean type #1307

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

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
uefi-raw: make Boolean less restrictive, allow any bit pattern
This aligns the behaviour with r_efi [0].

[0] https://docs.rs/r-efi/5.1.0/src/r_efi/base.rs.html#488
  • Loading branch information
phip1611 committed Oct 6, 2024
commit 492f00258a9ec7e79317d9515be4c8ad7139da15
49 changes: 13 additions & 36 deletions uefi-raw/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,8 @@ mod status;
pub use status::Status;
pub use uguid::{guid, Guid};

#[cfg(feature = "unstable")]
use core::error::Error;
use core::ffi::c_void;
use core::fmt::{self, Debug, Display, Formatter};
use core::fmt::{self, Debug, Formatter};

/// Handle to an event structure.
pub type Event = *mut c_void;
Expand Down Expand Up @@ -68,26 +66,11 @@ pub type PhysicalAddress = u64;
/// of target platform.
pub type VirtualAddress = u64;

/// The provided [`Boolean`] can't be converted to [`bool`] as it is neither
/// `0` nor `1`.
#[derive(Debug, Copy, Clone, PartialEq, Ord, PartialOrd, Eq)]
#[repr(transparent)]
pub struct InvalidBooleanError(pub u8);

impl Display for InvalidBooleanError {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
Debug::fmt(self, f)
}
}

#[cfg(feature = "unstable")]
impl Error for InvalidBooleanError {}

/// ABI-compatible UEFI boolean.
///
/// Opaque 1-byte value holding either `0` for FALSE or a `1` for TRUE. This
/// type can be converted from and to `bool` via corresponding [`From`]
/// respectively [`TryFrom`] implementations.
/// implementations.
#[derive(Copy, Clone, Debug, Default, PartialEq, Ord, PartialOrd, Eq, Hash)]
#[repr(transparent)]
pub struct Boolean(pub u8);
Expand All @@ -109,14 +92,13 @@ impl From<bool> for Boolean {
}
}

impl TryFrom<Boolean> for bool {
type Error = InvalidBooleanError;

fn try_from(value: Boolean) -> Result<Self, Self::Error> {
impl From<Boolean> for bool {
#[allow(clippy::match_like_matches_macro)]
fn from(value: Boolean) -> Self {
// We handle it as in C: Any bit-pattern != 0 equals true
match value.0 {
0 => 8000 ; Ok(false),
1 => Ok(true),
x => Err(InvalidBooleanError(x)),
0 => false,
_ => true,
}
}
}
Expand Down Expand Up @@ -205,15 +187,10 @@ mod tests {
assert_eq!(Boolean::from(false).0, 0);
assert_eq!(Boolean::TRUE.0, 1);
assert_eq!(Boolean::FALSE.0, 0);
assert_eq!(bool::try_from(Boolean(0b0)), Ok(false));
assert_eq!(bool::try_from(Boolean(0b1)), Ok(true));
assert_eq!(
bool::try_from(Boolean(0b11)),
Err(InvalidBooleanError(0b11))
);
assert_eq!(
bool::try_from(Boolean(0b10)),
Err(InvalidBooleanError(0b10))
);
assert_eq!(bool::from(Boolean(0b0)), false);
assert_eq!(bool::from(Boolean(0b1)), true);
// We do it a C: Every bit pattern not 0 is equal to true
assert_eq!(bool::from(Boolean(0b11111110)), true);
assert_eq!(bool::from(Boolean(0b11111111)), true);
}
}
15 changes: 6 additions & 9 deletions uefi/src/proto/boot_policy.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Module for the [`BootPolicy`] helper type.

use uefi_raw::{Boolean, InvalidBooleanError};
use uefi_raw::Boolean;

/// The UEFI boot policy is a property that influences the behaviour of
/// various UEFI functions that load files (typically UEFI images).
Expand Down Expand Up @@ -33,16 +33,13 @@ impl From<BootPolicy> for Boolean {
}
}

impl TryFrom<Boolean> for BootPolicy {
type Error = InvalidBooleanError;

fn try_from(value: Boolean) -> Result<Self, Self::Error> {
let boolean: bool = value.try_into()?;
let policy = match boolean {
impl From<Boolean> for BootPolicy {
fn from(value: Boolean) -> Self {
let boolean: bool = value.into();
match boolean {
true => Self::BootSelection,
false => Self::ExactMatch,
};
Ok(policy)
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion uefi/src/proto/console/text/output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ impl Output {
#[must_use]
pub fn cursor_visible(&self) -> bool {
// Panic: Misbehaving UEFI impls are so unlikely; just fail
self.data().cursor_visible.try_into().unwrap()
self.data().cursor_visible.into()
}

/// Make the cursor visible or invisible.
Expand Down
10 changes: 5 additions & 5 deletions uefi/src/proto/media/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,35 +117,35 @@ impl BlockIOMedia {
#[must_use]
pub fn is_removable_media(&self) -> bool {
// Panic: Misbehaving UEFI impls are so unlikely; just fail
Copy link
Member

Choose a reason for hiding this comment

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

Can drop all the panic comments in this file too

self.0.removable_media.try_into().unwrap()
self.0.removable_media.into()
}

/// True if there is a media currently present in the device.
#[must_use]
pub fn is_media_present(&self) -> bool {
// Panic: Misbehaving UEFI impls are so unlikely; just fail
self.0.media_present.try_into().unwrap()
self.0.media_present.into()
}

/// True if block IO was produced to abstract partition structure.
#[must_use]
pub fn is_logical_partition(&self) -> bool {
// Panic: Misbehaving UEFI impls are so unlikely; just fail
self.0.logical_partition.try_into().unwrap()
self.0.logical_partition.into()
}

/// True if the media is marked read-only.
#[must_use]
pub fn is_read_only(&self) -> bool {
// Panic: Misbehaving UEFI impls are so unlikely; just fail
self.0.read_only.try_into().unwrap()
self.0.read_only.into()
}

/// True if `writeBlocks` function writes data.
#[must_use]
pub fn is_write_caching(&self) -> bool {
// Panic: Misbehaving UEFI impls are so unlikely; just fail
self.0.write_caching.try_into().unwrap()
self.0.write_caching.into()
}

/// The intrinsic block size of the device.
Expand Down
2 changes: 1 addition & 1 deletion uefi/src/proto/media/load_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ use crate::proto::unsafe_protocol;
#[cfg(all(feature = "alloc", feature = "unstable"))]
use alloc::alloc::Global;
use uefi_raw::protocol::media::{LoadFile2Protocol, LoadFileProtocol};
use uefi_raw::Boolean;
#[cfg(feature = "alloc")]
use {
crate::{mem::make_boxed, proto::device_path::DevicePath, Result, StatusExt},
alloc::boxed::Box,
uefi::proto::BootPolicy,
uefi_raw::Boolean,
};

/// Load File Protocol.
Expand Down
0