From 492f00258a9ec7e79317d9515be4c8ad7139da15 Mon Sep 17 00:00:00 2001 From: Philipp Schuster Date: Wed, 2 Oct 2024 11:24:26 +0200 Subject: [PATCH] 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 --- uefi-raw/src/lib.rs | 49 +++++++-------------------- uefi/src/proto/boot_policy.rs | 15 ++++---- uefi/src/proto/console/text/output.rs | 2 +- uefi/src/proto/media/block.rs | 10 +++--- uefi/src/proto/media/load_file.rs | 2 +- 5 files changed, 26 insertions(+), 52 deletions(-) diff --git a/uefi-raw/src/lib.rs b/uefi-raw/src/lib.rs index 101efb2b5..8bdbf7139 100644 --- a/uefi-raw/src/lib.rs +++ b/uefi-raw/src/lib.rs @@ -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; @@ -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); @@ -109,14 +92,13 @@ impl From for Boolean { } } -impl TryFrom for bool { - type Error = InvalidBooleanError; - - fn try_from(value: Boolean) -> Result { +impl From 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 => Ok(false), - 1 => Ok(true), - x => Err(InvalidBooleanError(x)), + 0 => false, + _ => true, } } } @@ -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); } } diff --git a/uefi/src/proto/boot_policy.rs b/uefi/src/proto/boot_policy.rs index 05aaed5d9..316f3716c 100644 --- a/uefi/src/proto/boot_policy.rs +++ b/uefi/src/proto/boot_policy.rs @@ -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). @@ -33,16 +33,13 @@ impl From for Boolean { } } -impl TryFrom for BootPolicy { - type Error = InvalidBooleanError; - - fn try_from(value: Boolean) -> Result { - let boolean: bool = value.try_into()?; - let policy = match boolean { +impl From for BootPolicy { + fn from(value: Boolean) -> Self { + let boolean: bool = value.into(); + match boolean { true => Self::BootSelection, false => Self::ExactMatch, - }; - Ok(policy) + } } } diff --git a/uefi/src/proto/console/text/output.rs b/uefi/src/proto/console/text/output.rs index 6e394c0e6..1ee956e6a 100644 --- a/uefi/src/proto/console/text/output.rs +++ b/uefi/src/proto/console/text/output.rs @@ -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. diff --git a/uefi/src/proto/media/block.rs b/uefi/src/proto/media/block.rs index ce80d8e6f..aee87dffc 100644 --- a/uefi/src/proto/media/block.rs +++ b/uefi/src/proto/media/block.rs @@ -117,35 +117,35 @@ impl BlockIOMedia { #[must_use] pub fn is_removable_media(&self) -> bool { // Panic: Misbehaving UEFI impls are so unlikely; just fail - 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. diff --git a/uefi/src/proto/media/load_file.rs b/uefi/src/proto/media/load_file.rs index 826139636..b57913c1c 100644 --- a/uefi/src/proto/media/load_file.rs +++ b/uefi/src/proto/media/load_file.rs @@ -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.