-
Notifications
You must be signed in to change notification settings - Fork 159
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
uefi: add BootPolicy type #1326
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
dd87436
uefi: add BootPolicy type
phip1611 a7f0189
uefi: boot policy: reorder imports and modules
phip1611 19b20cf
uefi: LoadImageSource: use `BootPolicy`
phip1611 1d1138e
uefi: remove code duplication
phip1611 8f0a6b5
uefi: fix imports
phip1611 b3d031a
uefi: boot-policy: derive Default
phip1611 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
//! 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 {} | ||
|
||
/// 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. | ||
#[derive(Copy, Clone, Debug, Default, Eq, Ord, PartialEq, PartialOrd)] | ||
pub enum BootPolicy { | ||
/// Indicates that the request originates from the boot manager, and that | ||
/// the boot manager is attempting to load the provided `file_path` as a | ||
/// boot selection. | ||
/// | ||
/// Boot selection refers to what a user has chosen in the (GUI) boot menu. | ||
/// | ||
/// This corresponds to the `TRUE` value in the UEFI spec. | ||
BootSelection, | ||
/// The provided `file_path` must match an exact file to be loaded. | ||
/// | ||
/// This corresponds to the `FALSE` value in the UEFI spec. | ||
#[default] | ||
ExactMatch, | ||
} | ||
|
||
impl From<BootPolicy> for bool { | ||
fn from(value: BootPolicy) -> Self { | ||
match value { | ||
BootPolicy::BootSelection => true, | ||
BootPolicy::ExactMatch => false, | ||
} | ||
} | ||
} | ||
|
||
impl From<bool> for BootPolicy { | ||
fn from(value: bool) -> Self { | ||
match value { | ||
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)), | ||
} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[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)) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems like it could be made compatible: add
repr(u8)
andBootSelection = 1
,ExactMatch = 0
.(As long as this type is only passed into UEFI APIs, and not read from them; in that case
newtype_enum
might be better.)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we want to postpone that decision/discussion to #1307?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As long as the type is only being passed into the API, UB shouldn't be a concern -- we'll always initialize
BootPolicy
correctly.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Were you just adding thoughts to the discussion or do you want this doccomment to be removed before we merge it? That's not clear to me right now :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was thinking we should make changes before merging, but actually since there is no declared
repr
right now, it wouldn't be a breaking change to addrepr(u8)
in the future I think. So let's go ahead and merge.