|
| 1 | +//! Implementation for UEFI using EFI_RNG_PROTOCOL |
| 2 | +use crate::Error; |
| 3 | +use core::{ |
| 4 | + mem::MaybeUninit, |
| 5 | + ptr::{self, null_mut, NonNull}, |
| 6 | + sync::atomic::{AtomicPtr, Ordering::Relaxed}, |
| 7 | +}; |
| 8 | +use r_efi::{ |
| 9 | + efi::{BootServices, Handle}, |
| 10 | + protocols::rng, |
| 11 | +}; |
| 12 | + |
| 13 | +extern crate std; |
| 14 | + |
| 15 | +pub use crate::util::{inner_u32, inner_u64}; |
| 16 | + |
| 17 | +#[cfg(not(target_os = "uefi"))] |
| 18 | +compile_error!("`efi_rng` backend can be enabled only for UEFI targets!"); |
| 19 | + |
| 20 | +static RNG_PROTOCOL: AtomicPtr<rng::Protocol> = AtomicPtr::new(null_mut()); |
| 21 | + |
| 22 | +#[cold] |
| 23 | +#[inline(never)] |
| 24 | +fn init() -> Result<NonNull<rng::Protocol>, Error> { |
| 25 | + const HANDLE_SIZE: usize = size_of::<Handle>(); |
| 26 | + |
| 27 | + let boot_services = std::os::uefi::env::boot_services() |
| 28 | + .ok_or(Error::BOOT_SERVICES_UNAVAILABLE)? |
| 29 | + .cast::<BootServices>(); |
| 30 | + |
| 31 | + let mut handles = [ptr::null_mut(); 16]; |
| 32 | + // `locate_handle` operates with length in bytes |
| 33 | + let mut buf_size = handles.len() * HANDLE_SIZE; |
| 34 | + let mut guid = rng::PROTOCOL_GUID; |
| 35 | + let ret = unsafe { |
| 36 | + ((*boot_services.as_ptr()).locate_handle)( |
| 37 | + r_efi::efi::BY_PROTOCOL, |
| 38 | + &mut guid, |
| 39 | + null_mut(), |
| 40 | + &mut buf_size, |
| 41 | + handles.as_mut_ptr(), |
| 42 | + ) |
| 43 | + }; |
| 44 | + |
| 45 | + if ret.is_error() { |
| 46 | + return Err(Error::TEMP_EFI_ERROR); |
| 47 | + } |
| 48 | + |
| 49 | + let handles_len = buf_size / HANDLE_SIZE; |
| 50 | + let handles = handles.get(..handles_len).ok_or(Error::UNEXPECTED)?; |
| 51 | + |
| 52 | + let system_handle = std::os::uefi::env::image_handle(); |
| 53 | + for &handle in handles { |
| 54 | + let mut protocol: MaybeUninit<*mut rng::Protocol> = MaybeUninit::uninit(); |
| 55 | + |
| 56 | + let mut protocol_guid = rng::PROTOCOL_GUID; |
| 57 | + let ret = unsafe { |
| 58 | + ((*boot_services.as_ptr()).open_protocol)( |
| 59 | + handle, |
| 60 | + &mut protocol_guid, |
| 61 | + protocol.as_mut_ptr().cast(), |
| 62 | + system_handle.as_ptr(), |
| 63 | + ptr::null_mut(), |
| 64 | + r_efi::system::OPEN_PROTOCOL_GET_PROTOCOL, |
| 65 | + ) |
| 66 | + }; |
| 67 | + |
| 68 | + let protocol = if ret.is_error() { |
| 69 | + continue; |
| 70 | + } else { |
| 71 | + let protocol = unsafe { protocol.assume_init() }; |
| 72 | + NonNull::new(protocol).ok_or(Error::UNEXPECTED)? |
| 73 | + }; |
| 74 | + |
| 75 | + // Try to use the acquired protocol handle |
| 76 | + let mut buf = [0u8; 8]; |
| 77 | + let mut alg_guid = rng::ALGORITHM_RAW; |
| 78 | + let ret = unsafe { |
| 79 | + ((*protocol.as_ptr()).get_rng)( |
| 80 | + protocol.as_ptr(), |
| 81 | + &mut alg_guid, |
| 82 | + buf.len(), |
| 83 | + buf.as_mut_ptr(), |
| 84 | + ) |
| 85 | + }; |
| 86 | + |
| 87 | + if ret.is_error() { |
| 88 | + continue; |
| 89 | + } |
| 90 | + |
| 91 | + RNG_PROTOCOL.store(protocol.as_ptr(), Relaxed); |
| 92 | + return Ok(protocol); |
| 93 | + } |
| 94 | + Err(Error::NO_RNG_HANDLE) |
| 95 | +} |
| 96 | + |
| 97 | +#[inline] |
| 98 | +pub fn fill_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> { |
| 99 | + let protocol = match NonNull::new(RNG_PROTOCOL.load(Relaxed)) { |
| 100 | + Some(p) => p, |
| 101 | + None => init()?, |
| 102 | + }; |
| 103 | + |
| 104 | + let mut alg_guid = rng::ALGORITHM_RAW; |
| 105 | + let ret = unsafe { |
| 106 | + ((*protocol.as_ptr()).get_rng)( |
| 107 | + protocol.as_ptr(), |
| 108 | + &mut alg_guid, |
| 109 | + dest.len(), |
| 110 | + dest.as_mut_ptr().cast::<u8>(), |
| 111 | + ) |
| 112 | + }; |
| 113 | + |
| 114 | + if ret.is_error() { |
| 115 | + Err(Error::TEMP_EFI_ERROR) |
| 116 | + } else { |
| 117 | + Ok(()) |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +impl Error { |
| 122 | + pub(crate) const BOOT_SERVICES_UNAVAILABLE: Error = Self::new_internal(10); |
| 123 | + pub(crate) const NO_RNG_HANDLE: Error = Self::new_internal(11); |
| 124 | + pub(crate) const TEMP_EFI_ERROR: Error = Self::new_internal(12); |
| 125 | +} |
0 commit comments