11//! `Rng` protocol.
22
33use 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 ) ]
99pub struct RngAlgorithm ( pub Guid ) ;
1010
1111impl 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
3538impl 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}
0 commit comments