Skip to content

Commit d188e89

Browse files
committed
add get_rng wrapper
1 parent 87f2b16 commit d188e89

File tree

2 files changed

+34
-16
lines changed

2 files changed

+34
-16
lines changed

src/proto/rng.rs

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
//! `Rng` protocol.
22
33
use crate::{data_types::Guid, proto::Protocol, unsafe_guid, Result, Status};
4-
use core::slice::SliceIndex;
4+
use core::ptr;
55

66
/// Contain a Rng algorithm Guid
77
#[repr(C)]
88
#[derive(Clone, Copy, Debug)]
99
pub struct RngAlgorithm(pub Guid);
1010

1111
impl RngAlgorithm {
12+
/// Get an empty `RngAlgorithm`
13+
///
14+
/// Used provide a buffer to `Rng.get_info`
1215
pub fn default() -> Self {
1316
Self(Guid::default())
1417
}
@@ -26,19 +29,40 @@ pub struct Rng {
2629
) -> Status,
2730
get_rng: extern "efiapi" fn(
2831
this: &Rng,
29-
algorithm: Option<RngAlgorithm>,
32+
algorithm: *const RngAlgorithm,
3033
value_length: usize,
3134
value: *mut u8,
3235
) -> Status,
3336
}
3437

3538
impl Rng {
39+
/// Returns information about the random number generation implementation.
40+
///
41+
/// Exemple :
42+
/// ```
43+
/// use uefi::proto::rng::RngAlgorithm;
44+
///
45+
/// let mut buffer = [RngAlgorithm::default(); 4];
46+
/// rng.get_info().unwrap_success();
47+
/// ```
3648
pub fn get_info(&mut self, algorithm_list: &mut [RngAlgorithm]) -> Result<usize> {
37-
let mut algorithm_list_size = (algorithm_list.len() * 16) as *mut usize;
49+
let algorithm_list_size = (algorithm_list.len() * 16) as *mut usize;
3850

3951
(self.get_info)(self, algorithm_list_size, algorithm_list.as_mut_ptr())
4052
.into_with_val(|| algorithm_list_size as usize / 16)
4153

4254
// TODO: Add AlgorithmType Enum for better visibility on algorithms
4355
}
56+
57+
/// Returns the next set of random numbers
58+
pub fn get_rng(&mut self, algorithm: Option<RngAlgorithm>, buffer: &mut [u8]) -> Result {
59+
let buffer_length = buffer.len();
60+
61+
let algo = match algorithm {
62+
None => ptr::null(),
63+
Some(algo) => &algo as *const RngAlgorithm,
64+
};
65+
66+
(self.get_rng)(self, algo, buffer_length, buffer.as_mut_ptr()).into()
67+
}
4468
}

uefi-test-runner/src/proto/rng.rs

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
1-
use core::mem;
2-
use core::mem::size_of_val;
31
use uefi::prelude::*;
42
use uefi::proto::rng::{Rng, RngAlgorithm};
53
use uefi::table::boot::{BootServices, OpenProtocolAttributes, OpenProtocolParams};
6-
use uefi::Guid;
74

85
pub fn test(image: Handle, bt: &BootServices) {
96
info!("Running rng protocol test");
@@ -28,14 +25,11 @@ pub fn test(image: Handle, bt: &BootServices) {
2825

2926
let mut list = [RngAlgorithm::default(); 4];
3027

31-
match rng.get_info(&mut list) {
32-
Ok(nb) => {
33-
for i in 0..nb.unwrap() {
34-
info!("OK {} : {}", nb.unwrap(), list[i].0)
35-
}
36-
}
37-
Err(e) => {
38-
error!("ERROR : {:#?}", e.status())
39-
}
40-
}
28+
rng.get_info(&mut list).unwrap_success();
29+
30+
let mut buf = [0u8; 4];
31+
32+
rng.get_rng(Some(list[0]), &mut buf).unwrap_success();
33+
34+
assert_ne!([0u8; 4], buf);
4135
}

0 commit comments

Comments
 (0)