]> git.proxmox.com Git - mirror_edk2.git/blame - MdePkg/Library/BaseRngLib/AArch64/Rndr.c
MdePkg: Apply uncrustify changes
[mirror_edk2.git] / MdePkg / Library / BaseRngLib / AArch64 / Rndr.c
CommitLineData
9301e564
RC
1/** @file\r
2 Random number generator service that uses the RNDR instruction\r
3 to provide pseudorandom numbers.\r
4\r
5 Copyright (c) 2021, NUVIA Inc. All rights reserved.<BR>\r
6 Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>\r
7\r
8 SPDX-License-Identifier: BSD-2-Clause-Patent\r
9\r
10**/\r
11\r
12#include <Uefi.h>\r
13#include <Library/BaseLib.h>\r
14#include <Library/DebugLib.h>\r
15#include <Library/RngLib.h>\r
16\r
17#include "ArmRng.h"\r
18#include "BaseRngLibInternals.h"\r
19\r
2f88bd3a 20STATIC BOOLEAN mRndrSupported;\r
9301e564
RC
21\r
22//\r
23// Bit mask used to determine if RNDR instruction is supported.\r
24//\r
2f88bd3a 25#define RNDR_MASK ((UINT64)MAX_UINT16 << 60U)\r
9301e564
RC
26\r
27/**\r
28 The constructor function checks whether or not RNDR instruction is supported\r
29 by the host hardware.\r
30\r
31 The constructor function checks whether or not RNDR instruction is supported.\r
32 It will ASSERT() if RNDR instruction is not supported.\r
33 It will always return EFI_SUCCESS.\r
34\r
35 @retval EFI_SUCCESS The constructor always returns EFI_SUCCESS.\r
36\r
37**/\r
38EFI_STATUS\r
39EFIAPI\r
40BaseRngLibConstructor (\r
41 VOID\r
42 )\r
43{\r
2f88bd3a
MK
44 UINT64 Isar0;\r
45\r
9301e564
RC
46 //\r
47 // Determine RNDR support by examining bits 63:60 of the ISAR0 register returned by\r
48 // MSR. A non-zero value indicates that the processor supports the RNDR instruction.\r
49 //\r
50 Isar0 = ArmReadIdIsar0 ();\r
51 ASSERT ((Isar0 & RNDR_MASK) != 0);\r
52\r
53 mRndrSupported = ((Isar0 & RNDR_MASK) != 0);\r
54\r
55 return EFI_SUCCESS;\r
56}\r
57\r
58/**\r
59 Generates a 16-bit random number.\r
60\r
61 @param[out] Rand Buffer pointer to store the 16-bit random value.\r
62\r
63 @retval TRUE Random number generated successfully.\r
64 @retval FALSE Failed to generate the random number.\r
65\r
66**/\r
67BOOLEAN\r
68EFIAPI\r
69ArchGetRandomNumber16 (\r
2f88bd3a 70 OUT UINT16 *Rand\r
9301e564
RC
71 )\r
72{\r
2f88bd3a 73 UINT64 Rand64;\r
9301e564
RC
74\r
75 if (ArchGetRandomNumber64 (&Rand64)) {\r
76 *Rand = Rand64 & MAX_UINT16;\r
77 return TRUE;\r
78 }\r
79\r
80 return FALSE;\r
81}\r
82\r
83/**\r
84 Generates a 32-bit random number.\r
85\r
86 @param[out] Rand Buffer pointer to store the 32-bit random value.\r
87\r
88 @retval TRUE Random number generated successfully.\r
89 @retval FALSE Failed to generate the random number.\r
90\r
91**/\r
92BOOLEAN\r
93EFIAPI\r
94ArchGetRandomNumber32 (\r
2f88bd3a 95 OUT UINT32 *Rand\r
9301e564
RC
96 )\r
97{\r
2f88bd3a 98 UINT64 Rand64;\r
9301e564
RC
99\r
100 if (ArchGetRandomNumber64 (&Rand64)) {\r
101 *Rand = Rand64 & MAX_UINT32;\r
102 return TRUE;\r
103 }\r
104\r
105 return FALSE;\r
106}\r
107\r
108/**\r
109 Generates a 64-bit random number.\r
110\r
111 @param[out] Rand Buffer pointer to store the 64-bit random value.\r
112\r
113 @retval TRUE Random number generated successfully.\r
114 @retval FALSE Failed to generate the random number.\r
115\r
116**/\r
117BOOLEAN\r
118EFIAPI\r
119ArchGetRandomNumber64 (\r
2f88bd3a 120 OUT UINT64 *Rand\r
9301e564
RC
121 )\r
122{\r
123 return ArmRndr (Rand);\r
124}\r
125\r
126/**\r
127 Checks whether RNDR is supported.\r
128\r
129 @retval TRUE RNDR is supported.\r
130 @retval FALSE RNDR is not supported.\r
131\r
132**/\r
133BOOLEAN\r
134EFIAPI\r
135ArchIsRngSupported (\r
136 VOID\r
137 )\r
138{\r
139 return mRndrSupported;\r
140}\r