Skip to content

Commit dd87436

Browse files
committed
uefi: add BootPolicy type
This type is used in three functions of the UEFI spec. Having an enum instead of a boolean simplifies the interface as the variants can be properly documented.
1 parent 42dd1f0 commit dd87436

File tree

2 files changed

+111
-0
lines changed

2 files changed

+111
-0
lines changed

uefi/src/proto/boot_policy.rs

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
//! Module for the [`BootPolicy`] helper type.
2+
3+
use core::fmt::{Display, Formatter};
4+
5+
/// Errors that can happen when working with [`BootPolicy`].
6+
#[derive(Debug, Copy, Clone, PartialOrd, PartialEq, Eq, Ord)]
7+
pub enum BootPolicyError {
8+
/// Only `0` and `1` are valid integers, all other values are undefined.
9+
InvalidInteger(u8),
10+
}
11+
12+
impl Display for BootPolicyError {
13+
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
14+
let s = match self {
15+
Self::InvalidInteger(_) => {
16+
"Only `0` and `1` are valid integers, all other values are undefined."
17+
}
18+
};
19+
f.write_str(s)
20+
}
21+
}
22+
23+
#[cfg(feature = "unstable")]
24+
impl core::error::Error for BootPolicyError {}
25+
26+
/// The UEFI boot policy is a property that influences the behaviour of
27+
/// various UEFI functions that load files (typically UEFI images).
28+
///
29+
/// This type is not ABI compatible. On the ABI level, this is an UEFI
30+
/// boolean.
31+
#[derive(Debug, Copy, Clone, PartialOrd, PartialEq, Eq, Ord)]
32+
pub enum BootPolicy {
33+
/// Indicates that the request originates from the boot manager, and that
34+
/// the boot manager is attempting to load the provided `file_path` as a
35+
/// boot selection.
36+
///
37+
/// Boot selection refers to what a user has chosen in the (GUI) boot menu.
38+
///
39+
/// This corresponds to the `TRUE` value in the UEFI spec.
40+
BootSelection,
41+
/// The provided `file_path` must match an exact file to be loaded.
42+
///
43+
/// This corresponds to the `FALSE` value in the UEFI spec.
44+
ExactMatch,
45+
}
46+
47+
impl From<BootPolicy> for bool {
48+
fn from(value: BootPolicy) -> Self {
49+
match value {
50+
BootPolicy::BootSelection => true,
51+
BootPolicy::ExactMatch => false,
52+
}
53+
}
54+
}
55+
56+
impl From<bool> for BootPolicy {
57+
fn from(value: bool) -> Self {
58+
match value {
59+
true => Self::BootSelection,
60+
false => Self::ExactMatch,
61+
}
62+
}
63+
}
64+
65+
impl From<BootPolicy> for u8 {
66+
fn from(value: BootPolicy) -> Self {
67+
match value {
68+
BootPolicy::BootSelection => 1,
69+
BootPolicy::ExactMatch => 0,
70+
}
71+
}
72+
}
73+
74+
impl TryFrom<u8> for BootPolicy {
75+
type Error = BootPolicyError;
76+
fn try_from(value: u8) -> Result<Self, Self::Error> {
77+
match value {
78+
0 => Ok(Self::ExactMatch),
79+
1 => Ok(Self::BootSelection),
80+
err => Err(Self::Error::InvalidInteger(err)),
81+
}
82+
}
83+
}
84+
85+
#[cfg(test)]
86+
mod tests {
87+
use super::*;
88+
89+
#[test]
90+
fn boot_policy() {
91+
assert_eq!(bool::from(BootPolicy::ExactMatch), false);
92+
assert_eq!(bool::from(BootPolicy::BootSelection), true);
93+
94+
assert_eq!(BootPolicy::from(false), BootPolicy::ExactMatch);
95+
assert_eq!(BootPolicy::from(true), BootPolicy::BootSelection);
96+
97+
assert_eq!(u8::from(BootPolicy::ExactMatch), 0);
98+
assert_eq!(u8::from(BootPolicy::BootSelection), 1);
99+
100+
assert_eq!(BootPolicy::try_from(0), Ok(BootPolicy::ExactMatch));
101+
assert_eq!(BootPolicy::try_from(1), Ok(BootPolicy::BootSelection));
102+
assert_eq!(
103+
BootPolicy::try_from(2),
104+
Err(BootPolicyError::InvalidInteger(2))
105+
);
106+
}
107+
}

uefi/src/proto/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
//!
1010
//! [`BootServices`]: crate::table::boot::BootServices#accessing-protocols
1111
12+
pub use boot_policy::BootPolicy;
13+
1214
use crate::Identify;
1315
use core::ffi::c_void;
1416

@@ -81,3 +83,5 @@ pub mod shell_params;
8183
pub mod shim;
8284
pub mod string;
8385
pub mod tcg;
86+
87+
mod boot_policy;

0 commit comments

Comments
 (0)