Skip to content

Commit 492f002

Browse files
committed
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
1 parent 89e8359 commit 492f002

File tree

5 files changed

+26
-52
lines changed

5 files changed

+26
-52
lines changed

uefi-raw/src/lib.rs

+13-36
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,8 @@ mod status;
3434
pub use status::Status;
3535
pub use uguid::{guid, Guid};
3636

37-
#[cfg(feature = "unstable")]
38-
use core::error::Error;
3937
use core::ffi::c_void;
40-
use core::fmt::{self, Debug, Display, Formatter};
38+
use core::fmt::{self, Debug, Formatter};
4139

4240
/// Handle to an event structure.
4341
pub type Event = *mut c_void;
@@ -68,26 +66,11 @@ pub type PhysicalAddress = u64;
6866
/// of target platform.
6967
pub type VirtualAddress = u64;
7068

71-
/// The provided [`Boolean`] can't be converted to [`bool`] as it is neither
72-
/// `0` nor `1`.
73-
#[derive(Debug, Copy, Clone, PartialEq, Ord, PartialOrd, Eq)]
74-
#[repr(transparent)]
75-
pub struct InvalidBooleanError(pub u8);
76-
77-
impl Display for InvalidBooleanError {
78-
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
79-
Debug::fmt(self, f)
80-
}
81-
}
82-
83-
#[cfg(feature = "unstable")]
84-
impl Error for InvalidBooleanError {}
85-
8669
/// ABI-compatible UEFI boolean.
8770
///
8871
/// Opaque 1-byte value holding either `0` for FALSE or a `1` for TRUE. This
8972
/// type can be converted from and to `bool` via corresponding [`From`]
90-
/// respectively [`TryFrom`] implementations.
73+
/// implementations.
9174
#[derive(Copy, Clone, Debug, Default, PartialEq, Ord, PartialOrd, Eq, Hash)]
9275
#[repr(transparent)]
9376
pub struct Boolean(pub u8);
@@ -109,14 +92,13 @@ impl From<bool> for Boolean {
10992
}
11093
}
11194

112-
impl TryFrom<Boolean> for bool {
113-
type Error = InvalidBooleanError;
114-
115-
fn try_from(value: Boolean) -> Result<Self, Self::Error> {
95+
impl From<Boolean> for bool {
96+
#[allow(clippy::match_like_matches_macro)]
97+
fn from(value: Boolean) -> Self {
98+
// We handle it as in C: Any bit-pattern != 0 equals true
11699
match value.0 {
117-
0 => Ok(false),
118-
1 => Ok(true),
119-
x => Err(InvalidBooleanError(x)),
100+
0 => false,
101+
_ => true,
120102
}
121103
}
122104
}
@@ -205,15 +187,10 @@ mod tests {
205187
assert_eq!(Boolean::from(false).0, 0);
206188
assert_eq!(Boolean::TRUE.0, 1);
207189
assert_eq!(Boolean::FALSE.0, 0);
208-
assert_eq!(bool::try_from(Boolean(0b0)), Ok(false));
209-
assert_eq!(bool::try_from(Boolean(0b1)), Ok(true));
210-
assert_eq!(
211-
bool::try_from(Boolean(0b11)),
212-
Err(InvalidBooleanError(0b11))
213-
);
214-
assert_eq!(
215-
bool::try_from(Boolean(0b10)),
216-
Err(InvalidBooleanError(0b10))
217-
);
190+
assert_eq!(bool::from(Boolean(0b0)), false);
191+
assert_eq!(bool::from(Boolean(0b1)), true);
192+
// We do it a C: Every bit pattern not 0 is equal to true
193+
assert_eq!(bool::from(Boolean(0b11111110)), true);
194+
assert_eq!(bool::from(Boolean(0b11111111)), true);
218195
}
219196
}

uefi/src/proto/boot_policy.rs

+6-9
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Module for the [`BootPolicy`] helper type.
22
3-
use uefi_raw::{Boolean, InvalidBooleanError};
3+
use uefi_raw::Boolean;
44

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

36-
impl TryFrom<Boolean> for BootPolicy {
37-
type Error = InvalidBooleanError;
38-
39-
fn try_from(value: Boolean) -> Result<Self, Self::Error> {
40-
let boolean: bool = value.try_into()?;
41-
let policy = match boolean {
36+
impl From<Boolean> for BootPolicy {
37+
fn from(value: Boolean) -> Self {
38+
let boolean: bool = value.into();
39+
match boolean {
4240
true => Self::BootSelection,
4341
false => Self::ExactMatch,
44-
};
45-
Ok(policy)
42+
}
4643
}
4744
}
4845

uefi/src/proto/console/text/output.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ impl Output {
118118
#[must_use]
119119
pub fn cursor_visible(&self) -> bool {
120120
// Panic: Misbehaving UEFI impls are so unlikely; just fail
121-
self.data().cursor_visible.try_into().unwrap()
121+
self.data().cursor_visible.into()
122122
}
123123

124124
/// Make the cursor visible or invisible.

uefi/src/proto/media/block.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -117,35 +117,35 @@ impl BlockIOMedia {
117117
#[must_use]
118118
pub fn is_removable_media(&self) -> bool {
119119
// Panic: Misbehaving UEFI impls are so unlikely; just fail
120-
self.0.removable_media.try_into().unwrap()
120+
self.0.removable_media.into()
121121
}
122122

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

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

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

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

151151
/// The intrinsic block size of the device.

uefi/src/proto/media/load_file.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ use crate::proto::unsafe_protocol;
44
#[cfg(all(feature = "alloc", feature = "unstable"))]
55
use alloc::alloc::Global;
66
use uefi_raw::protocol::media::{LoadFile2Protocol, LoadFileProtocol};
7-
use uefi_raw::Boolean;
87
#[cfg(feature = "alloc")]
98
use {
109
crate::{mem::make_boxed, proto::device_path::DevicePath, Result, StatusExt},
1110
alloc::boxed::Box,
1211
uefi::proto::BootPolicy,
12+
uefi_raw::Boolean,
1313
};
1414

1515
/// Load File Protocol.

0 commit comments

Comments
 (0)