TianoCore EDK2 master
Loading...
Searching...
No Matches
RngLibTimer.c
Go to the documentation of this file.
1
9#include <Base.h>
10#include <Library/BaseLib.h>
11#include <Library/DebugLib.h>
12#include <Library/TimerLib.h>
13
14#define DEFAULT_DELAY_TIME_IN_MICROSECONDS 10
15
23RETURN_STATUS
24EFIAPI
26 VOID
27 )
28{
29 DEBUG ((
30 DEBUG_WARN,
31 "Warning: This BaseRngTimerLib implementation will be deprecated. "
32 "Please use the MdeModulePkg implementation equivalent.\n"
33 ));
34
35 return RETURN_SUCCESS;
36}
37
46UINT32
48 VOID
49 )
50{
51 UINT64 CounterHz;
52
53 // Get the counter properties
55 // Make sure we won't divide by zero
56 if (CounterHz == 0) {
57 ASSERT (CounterHz != 0); // Assert so the developer knows something is wrong
58 return DEFAULT_DELAY_TIME_IN_MICROSECONDS;
59 }
60
61 // Calculate the minimum delay based on 1.5 microseconds divided by the hertz.
62 // We calculate the length of a cycle (1/CounterHz) and multiply it by 1.5 microseconds
63 // This ensures that the performance counter has increased by at least one
64 return (UINT32)(MAX (DivU64x64Remainder (1500000, CounterHz, NULL), 1));
65}
66
78BOOLEAN
79EFIAPI
81 OUT UINT16 *Rand
82 )
83{
84 UINT32 Index;
85 UINT8 *RandPtr;
86 UINT32 DelayInMicroSeconds;
87
88 ASSERT (Rand != NULL);
89
90 if (Rand == NULL) {
91 return FALSE;
92 }
93
94 DelayInMicroSeconds = CalculateMinimumDecentDelayInMicroseconds ();
95 RandPtr = (UINT8 *)Rand;
96 // Get 2 bytes of random ish data
97 for (Index = 0; Index < sizeof (UINT16); Index++) {
98 *RandPtr = (UINT8)(GetPerformanceCounter () & 0xFF);
99 // Delay to give the performance counter a chance to change
100 MicroSecondDelay (DelayInMicroSeconds);
101 RandPtr++;
102 }
103
104 return TRUE;
105}
106
118BOOLEAN
119EFIAPI
121 OUT UINT32 *Rand
122 )
123{
124 UINT32 Index;
125 UINT8 *RandPtr;
126 UINT32 DelayInMicroSeconds;
127
128 ASSERT (Rand != NULL);
129
130 if (NULL == Rand) {
131 return FALSE;
132 }
133
134 RandPtr = (UINT8 *)Rand;
135 DelayInMicroSeconds = CalculateMinimumDecentDelayInMicroseconds ();
136 // Get 4 bytes of random ish data
137 for (Index = 0; Index < sizeof (UINT32); Index++) {
138 *RandPtr = (UINT8)(GetPerformanceCounter () & 0xFF);
139 // Delay to give the performance counter a chance to change
140 MicroSecondDelay (DelayInMicroSeconds);
141 RandPtr++;
142 }
143
144 return TRUE;
145}
146
158BOOLEAN
159EFIAPI
161 OUT UINT64 *Rand
162 )
163{
164 UINT32 Index;
165 UINT8 *RandPtr;
166 UINT32 DelayInMicroSeconds;
167
168 ASSERT (Rand != NULL);
169
170 if (NULL == Rand) {
171 return FALSE;
172 }
173
174 RandPtr = (UINT8 *)Rand;
175 DelayInMicroSeconds = CalculateMinimumDecentDelayInMicroseconds ();
176 // Get 8 bytes of random ish data
177 for (Index = 0; Index < sizeof (UINT64); Index++) {
178 *RandPtr = (UINT8)(GetPerformanceCounter () & 0xFF);
179 // Delay to give the performance counter a chance to change
180 MicroSecondDelay (DelayInMicroSeconds);
181 RandPtr++;
182 }
183
184 return TRUE;
185}
186
198BOOLEAN
199EFIAPI
201 OUT UINT64 *Rand
202 )
203{
204 ASSERT (Rand != NULL);
205 // This should take around 80ms
206
207 // Read first 64 bits
208 if (!GetRandomNumber64 (Rand)) {
209 return FALSE;
210 }
211
212 // Read second 64 bits
213 return GetRandomNumber64 (++Rand);
214}
215
226RETURN_STATUS
227EFIAPI
229 GUID *RngGuid
230 )
231{
232 /* This implementation is to be replaced by its MdeModulePkg copy.
233 * The cause being that some GUIDs (gEdkiiRngAlgorithmUnSafe) cannot
234 * be defined in the MdePkg.
235 */
236 return RETURN_UNSUPPORTED;
237}
UINT64 EFIAPI GetPerformanceCounterProperties(OUT UINT64 *StartValue OPTIONAL, OUT UINT64 *EndValue OPTIONAL)
UINT64 EFIAPI GetPerformanceCounter(VOID)
UINTN EFIAPI MicroSecondDelay(IN UINTN MicroSeconds)
UINT64 EFIAPI DivU64x64Remainder(IN UINT64 Dividend, IN UINT64 Divisor, OUT UINT64 *Remainder OPTIONAL)
STATIC UINT32 CalculateMinimumDecentDelayInMicroseconds(VOID)
Definition: RngLibTimer.c:29
BOOLEAN EFIAPI GetRandomNumber16(OUT UINT16 *Rand)
Definition: RngLibTimer.c:62
EFI_STATUS EFIAPI GetRngGuid(GUID *RngGuid)
Definition: RngLibTimer.c:210
BOOLEAN EFIAPI GetRandomNumber32(OUT UINT32 *Rand)
Definition: RngLibTimer.c:102
BOOLEAN EFIAPI GetRandomNumber128(OUT UINT64 *Rand)
Definition: RngLibTimer.c:182
BOOLEAN EFIAPI GetRandomNumber64(OUT UINT64 *Rand)
Definition: RngLibTimer.c:142
#define NULL
Definition: Base.h:319
#define STATIC
Definition: Base.h:264
#define RETURN_UNSUPPORTED
Definition: Base.h:1081
#define RETURN_SUCCESS
Definition: Base.h:1066
#define TRUE
Definition: Base.h:301
#define FALSE
Definition: Base.h:307
#define OUT
Definition: Base.h:284
#define MAX(a, b)
Definition: Base.h:992
#define DEBUG(Expression)
Definition: DebugLib.h:434
RETURN_STATUS EFIAPI BaseRngLibTimerConstructor(VOID)
Definition: RngLibTimer.c:25
UINTN Rand(VOID)
Definition: Support.c:39
Definition: Base.h:213