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