Skip to content

Commit 88c615c

Browse files
committed
Implement hashmap_random_keys()
It uses EFI_RNG_PROTOCOL if present. Else just returns (1, 2) Signed-off-by: Ayush <ayushsingh1325@gmail.com>
1 parent 176361a commit 88c615c

File tree

4 files changed

+33
-6
lines changed

4 files changed

+33
-6
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2949,7 +2949,7 @@ dependencies = [
29492949
[[package]]
29502950
name = "r-efi"
29512951
version = "4.0.0"
2952-
source = "git+https://github.com/r-efi/r-efi#cfa10bdfe46f2f283d2a93f179c6feb699c4b58a"
2952+
source = "git+https://github.com/r-efi/r-efi#30e0e8b8552179e1cc7f054a8b64f243db7c9bee"
29532953
dependencies = [
29542954
"compiler_builtins",
29552955
"rustc-std-workspace-core",

library/std/src/os/uefi/env.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ pub fn get_runtime_services() -> Option<NonNull<RuntimeServices>> {
4848
NonNull::new(runtime_services)
4949
}
5050

51-
pub(crate) fn open_protocol<T>(
51+
#[unstable(feature = "uefi_std", issue = "none")]
52+
pub fn open_protocol<T>(
5253
handle: NonNull<c_void>,
5354
mut protocol_guid: Guid,
5455
) -> io::Result<NonNull<T>> {
@@ -77,7 +78,8 @@ pub(crate) fn open_protocol<T>(
7778
}
7879
}
7980

80-
pub(crate) fn locate_handles(mut guid: Guid) -> io::Result<Vec<NonNull<c_void>>> {
81+
#[unstable(feature = "uefi_std", issue = "none")]
82+
pub fn locate_handles(mut guid: Guid) -> io::Result<Vec<NonNull<c_void>>> {
8183
fn inner(
8284
guid: &mut Guid,
8385
boot_services: NonNull<BootServices>,

library/std/src/os/uefi/raw.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! This module just re-exports stuff from r-efi crate
22
3-
pub use r_efi::efi::{BootServices, RuntimeServices, SystemTable};
3+
pub use r_efi::efi::{BootServices, Guid, RuntimeServices, SystemTable};
44

55
use crate::alloc::{Allocator, Global, Layout};
66
use crate::io;

library/std/src/sys/uefi/mod.rs

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,34 @@ pub extern "C" fn __rust_abort() {
117117
abort_internal();
118118
}
119119

120-
// FIXME: Use EFI_RNG_PROTOCOL
121120
pub fn hashmap_random_keys() -> (u64, u64) {
122-
(1, 2)
121+
unsafe { (get_random().unwrap_or(1), get_random().unwrap_or(2)) }
122+
}
123+
124+
unsafe fn get_random() -> Option<u64> {
125+
use r_efi::protocols::rng;
126+
127+
let mut buf = [0u8; 8];
128+
let handles = uefi::env::locate_handles(rng::PROTOCOL_GUID).ok()?;
129+
for handle in handles {
130+
if let Ok(protocol) = uefi::env::open_protocol::<rng::Protocol>(handle, rng::PROTOCOL_GUID)
131+
{
132+
let r = unsafe {
133+
((*protocol.as_ptr()).get_rng)(
134+
protocol.as_ptr(),
135+
crate::ptr::null_mut(),
136+
buf.len(),
137+
buf.as_mut_ptr(),
138+
)
139+
};
140+
if r.is_error() {
141+
continue;
142+
} else {
143+
return Some(u64::from_le_bytes(buf));
144+
}
145+
}
146+
}
147+
None
123148
}
124149

125150
extern "C" {

0 commit comments

Comments
 (0)