-
Notifications
You must be signed in to change notification settings - Fork 215
Add efi_rng
opt-in backend
#570
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
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
e7eb146
Add `efi_rng` opt-in backend
newpavlov 8158b08
fix import
newpavlov c5c8845
Add Clippy job
newpavlov 07cf98b
Add inline attributes
newpavlov 5d879a5
Merge branch 'master' into efi_rng
newpavlov 1c01253
Update Cargo.lock and changelog
newpavlov 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 hidden or 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 hidden or 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 hidden or 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,125 @@ | ||
//! Implementation for UEFI using EFI_RNG_PROTOCOL | ||
use crate::Error; | ||
use core::{ | ||
mem::MaybeUninit, | ||
ptr::{self, null_mut, NonNull}, | ||
sync::atomic::{AtomicPtr, Ordering::Relaxed}, | ||
}; | ||
use r_efi::{ | ||
efi::{BootServices, Handle}, | ||
protocols::rng, | ||
}; | ||
|
||
extern crate std; | ||
|
||
pub use crate::util::{inner_u32, inner_u64}; | ||
|
||
#[cfg(not(target_os = "uefi"))] | ||
compile_error!("`efi_rng` backend can be enabled only for UEFI targets!"); | ||
|
||
static RNG_PROTOCOL: AtomicPtr<rng::Protocol> = AtomicPtr::new(null_mut()); | ||
|
||
#[cold] | ||
#[inline(never)] | ||
fn init() -> Result<NonNull<rng::Protocol>, Error> { | ||
const HANDLE_SIZE: usize = size_of::<Handle>(); | ||
|
||
let boot_services = std::os::uefi::env::boot_services() | ||
.ok_or(Error::BOOT_SERVICES_UNAVAILABLE)? | ||
.cast::<BootServices>(); | ||
|
||
let mut handles = [ptr::null_mut(); 16]; | ||
// `locate_handle` operates with length in bytes | ||
let mut buf_size = handles.len() * HANDLE_SIZE; | ||
let mut guid = rng::PROTOCOL_GUID; | ||
let ret = unsafe { | ||
((*boot_services.as_ptr()).locate_handle)( | ||
r_efi::efi::BY_PROTOCOL, | ||
&mut guid, | ||
null_mut(), | ||
&mut buf_size, | ||
handles.as_mut_ptr(), | ||
) | ||
}; | ||
|
||
if ret.is_error() { | ||
return Err(Error::TEMP_EFI_ERROR); | ||
} | ||
|
||
let handles_len = buf_size / HANDLE_SIZE; | ||
let handles = handles.get(..handles_len).ok_or(Error::UNEXPECTED)?; | ||
|
||
let system_handle = std::os::uefi::env::image_handle(); | ||
for &handle in handles { | ||
let mut protocol: MaybeUninit<*mut rng::Protocol> = MaybeUninit::uninit(); | ||
|
||
let mut protocol_guid = rng::PROTOCOL_GUID; | ||
let ret = unsafe { | ||
((*boot_services.as_ptr()).open_protocol)( | ||
handle, | ||
&mut protocol_guid, | ||
protocol.as_mut_ptr().cast(), | ||
system_handle.as_ptr(), | ||
ptr::null_mut(), | ||
r_efi::system::OPEN_PROTOCOL_GET_PROTOCOL, | ||
) | ||
}; | ||
|
||
let protocol = if ret.is_error() { | ||
continue; | ||
} else { | ||
let protocol = unsafe { protocol.assume_init() }; | ||
NonNull::new(protocol).ok_or(Error::UNEXPECTED)? | ||
}; | ||
|
||
// Try to use the acquired protocol handle | ||
let mut buf = [0u8; 8]; | ||
let mut alg_guid = rng::ALGORITHM_RAW; | ||
let ret = unsafe { | ||
((*protocol.as_ptr()).get_rng)( | ||
protocol.as_ptr(), | ||
&mut alg_guid, | ||
buf.len(), | ||
buf.as_mut_ptr(), | ||
) | ||
}; | ||
|
||
if ret.is_error() { | ||
continue; | ||
} | ||
|
||
RNG_PROTOCOL.store(protocol.as_ptr(), Relaxed); | ||
return Ok(protocol); | ||
} | ||
Err(Error::NO_RNG_HANDLE) | ||
} | ||
|
||
#[inline] | ||
pub fn fill_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> { | ||
let protocol = match NonNull::new(RNG_PROTOCOL.load(Relaxed)) { | ||
Some(p) => p, | ||
None => init()?, | ||
}; | ||
|
||
let mut alg_guid = rng::ALGORITHM_RAW; | ||
let ret = unsafe { | ||
((*protocol.as_ptr()).get_rng)( | ||
protocol.as_ptr(), | ||
&mut alg_guid, | ||
dest.len(), | ||
dest.as_mut_ptr().cast::<u8>(), | ||
) | ||
}; | ||
|
||
if ret.is_error() { | ||
Err(Error::TEMP_EFI_ERROR) | ||
} else { | ||
Ok(()) | ||
} | ||
} | ||
|
||
impl Error { | ||
pub(crate) const BOOT_SERVICES_UNAVAILABLE: Error = Self::new_internal(10); | ||
pub(crate) const NO_RNG_HANDLE: Error = Self::new_internal(11); | ||
pub(crate) const TEMP_EFI_ERROR: Error = Self::new_internal(12); | ||
} |
This file contains hidden or 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.
Do we need
TEMP_EFI_ERROR
now if we have #569 ?Uh oh!
There was an error while loading. Please reload this page.
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 I wrote in the OP, to encapsulate the UEFI status codes we need a wider state than 32-bits. Technically, on x86-64 we need 64 bits, but we probably can use
NonZeroU64
since status codes with prefixes0x1
,0x3
, and0x7
are used for "warning" codes.