Skip to content

Commit

Permalink
uefi: streamline BootPolicy and Boolean types
Browse files Browse the repository at this point in the history
  • Loading branch information
phip1611 committed Oct 1, 2024
1 parent fc640ce commit b8b13af
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 68 deletions.
2 changes: 1 addition & 1 deletion uefi/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ details of the deprecated items that were removed in this release.
- **Breaking:** `FileSystem` no longer has a lifetime parameter, and the
deprecated conversion from `uefi::table::boot::ScopedProtocol` has been
removed.
- **Breaking:** Removed `BootPolicyError` as `BootPolicy`
- Fixed `boot::open_protocol` to properly handle a null interface pointer.


# uefi - 0.32.0 (2024-09-09)

See [Deprecating SystemTable/BootServices/RuntimeServices][funcmigrate] for
Expand Down
90 changes: 24 additions & 66 deletions uefi/src/proto/boot_policy.rs
Original file line number Diff line number Diff line change
@@ -1,33 +1,12 @@
//! Module for the [`BootPolicy`] helper type.
use core::fmt::{Display, Formatter};

/// Errors that can happen when working with [`BootPolicy`].
#[derive(Debug, Copy, Clone, PartialOrd, PartialEq, Eq, Ord)]
pub enum BootPolicyError {
/// Only `0` and `1` are valid integers, all other values are undefined.
InvalidInteger(u8),
}

impl Display for BootPolicyError {
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
let s = match self {
Self::InvalidInteger(_) => {
"Only `0` and `1` are valid integers, all other values are undefined."
}
};
f.write_str(s)
}
}

#[cfg(feature = "unstable")]
impl core::error::Error for BootPolicyError {}
use uefi_raw::{Boolean, InvalidBooleanError};

/// The UEFI boot policy is a property that influences the behaviour of
/// various UEFI functions that load files (typically UEFI images).
///
/// This type is not ABI compatible. On the ABI level, this is an UEFI
/// boolean.
/// This type is not ABI compatible. On the ABI level, this corresponds to
/// a [`Boolean`].
#[derive(Copy, Clone, Debug, Default, Eq, Ord, PartialEq, PartialOrd)]
pub enum BootPolicy {
/// Indicates that the request originates from the boot manager, and that
Expand All @@ -36,50 +15,34 @@ pub enum BootPolicy {
///
/// Boot selection refers to what a user has chosen in the (GUI) boot menu.
///
/// This corresponds to the `TRUE` value in the UEFI spec.
/// This corresponds to the underlying [`Boolean`] being `true`.
BootSelection,
/// The provided `file_path` must match an exact file to be loaded.
///
/// This corresponds to the `FALSE` value in the UEFI spec.
/// This corresponds to the underlying [`Boolean`] being `false`.
#[default]
ExactMatch,
}

impl From<BootPolicy> for bool {
impl From<BootPolicy> for Boolean {
fn from(value: BootPolicy) -> Self {
match value {
BootPolicy::BootSelection => true,
BootPolicy::ExactMatch => false,
BootPolicy::BootSelection => true.into(),
BootPolicy::ExactMatch => false.into(),
}
}
}

impl From<bool> for BootPolicy {
fn from(value: bool) -> Self {
match value {
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 {
true => Self::BootSelection,
false => Self::ExactMatch,
}
}
}

impl From<BootPolicy> for u8 {
fn from(value: BootPolicy) -> Self {
match value {
BootPolicy::BootSelection => 1,
BootPolicy::ExactMatch => 0,
}
}
}

impl TryFrom<u8> for BootPolicy {
type Error = BootPolicyError;
fn try_from(value: u8) -> Result<Self, Self::Error> {
match value {
0 => Ok(Self::ExactMatch),
1 => Ok(Self::BootSelection),
err => Err(Self::Error::InvalidInteger(err)),
}
};
Ok(policy)
}
}

Expand All @@ -89,20 +52,15 @@ mod tests {

#[test]
fn boot_policy() {
assert_eq!(bool::from(BootPolicy::ExactMatch), false);
assert_eq!(bool::from(BootPolicy::BootSelection), true);

assert_eq!(BootPolicy::from(false), BootPolicy::ExactMatch);
assert_eq!(BootPolicy::from(true), BootPolicy::BootSelection);

assert_eq!(u8::from(BootPolicy::ExactMatch), 0);
assert_eq!(u8::from(BootPolicy::BootSelection), 1);

assert_eq!(BootPolicy::try_from(0), Ok(BootPolicy::ExactMatch));
assert_eq!(BootPolicy::try_from(1), Ok(BootPolicy::BootSelection));
assert_eq!(
BootPolicy::try_from(2),
Err(BootPolicyError::InvalidInteger(2))
BootPolicy::try_from(Boolean::TRUE).unwrap(),
BootPolicy::BootSelection
);
assert_eq!(
BootPolicy::try_from(Boolean::FALSE).unwrap(),
BootPolicy::ExactMatch
);
assert_eq!(Boolean::from(BootPolicy::BootSelection), Boolean::TRUE);
assert_eq!(Boolean::from(BootPolicy::ExactMatch), Boolean::FALSE);
}
}
2 changes: 1 addition & 1 deletion uefi/src/proto/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pub mod tcg;

mod boot_policy;

pub use boot_policy::{BootPolicy, BootPolicyError};
pub use boot_policy::BootPolicy;
pub use uefi_macros::unsafe_protocol;

use crate::Identify;
Expand Down

0 comments on commit b8b13af

Please sign in to comment.