]> git.proxmox.com Git - mirror_edk2.git/blame - SecurityPkg/RandomNumberGenerator/RngDxe/X64/GccRdRand.c
Add UEFI RNG Protocol support. The driver will leverage Intel Secure Key technology...
[mirror_edk2.git] / SecurityPkg / RandomNumberGenerator / RngDxe / X64 / GccRdRand.c
CommitLineData
3aa8dc6c
LQ
1/** @file\r
2 RDRAND Support Routines for GCC environment.\r
3\r
4Copyright (c) 2013, Intel Corporation. All rights reserved.<BR>\r
5This program and the accompanying materials\r
6are licensed and made available under the terms and conditions of the BSD License\r
7which accompanies this distribution. The full text of the license may be found at\r
8http://opensource.org/licenses/bsd-license.php\r
9\r
10THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
11WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
12\r
13**/\r
14\r
15/**\r
16 Generates a 16-bit random number through RDRAND instruction.\r
17\r
18 @param[out] Rand Buffer pointer to store the random result.\r
19\r
20 @retval TRUE RDRAND call was successful.\r
21 @retval FALSE Failed attempts to call RDRAND.\r
22\r
23**/\r
24BOOLEAN\r
25EFIAPI\r
26RdRand16Step (\r
27 OUT UINT16 *Rand\r
28 )\r
29{\r
30 UINT8 Carry;\r
31\r
32 //\r
33 // Uses byte code for RDRAND instruction,\r
34 // in case that GCC version has no direct support on RDRAND assembly.\r
35 //\r
36 __asm__ __volatile__ (\r
37 ".byte 0x66; .byte 0x0f; .byte 0xc7; .byte 0xf0; setc %1"\r
38 :"=a" (*Rand),\r
39 "=qm" (Carry)\r
40 ); \r
41\r
42 return (BOOLEAN) Carry;\r
43}\r
44\r
45/**\r
46 Generates a 32-bit random number through RDRAND instruction.\r
47\r
48 @param[out] Rand Buffer pointer to store the random result.\r
49\r
50 @retval TRUE RDRAND call was successful.\r
51 @retval FALSE Failed attempts to call RDRAND.\r
52\r
53**/\r
54BOOLEAN\r
55EFIAPI\r
56RdRand32Step (\r
57 OUT UINT32 *Rand\r
58 )\r
59{\r
60 UINT8 Carry;\r
61\r
62 __asm__ __volatile__ (\r
63 ".byte 0x0f; .byte 0xc7; .byte 0xf0; setc %1"\r
64 :"=a" (*Rand), \r
65 "=qm" (Carry)\r
66 );\r
67\r
68 return (BOOLEAN) Carry;\r
69}\r
70\r
71/**\r
72 Generates a 64-bit random number through RDRAND instruction.\r
73\r
74 @param[out] Rand Buffer pointer to store the random result.\r
75\r
76 @retval TRUE RDRAND call was successful.\r
77 @retval FALSE Failed attempts to call RDRAND.\r
78\r
79**/\r
80BOOLEAN\r
81EFIAPI\r
82RdRand64Step (\r
83 OUT UINT64 *Rand\r
84 )\r
85{\r
86 UINT8 Carry;\r
87\r
88 __asm__ __volatile__ (\r
89 ".byte 0x48; .byte 0x0f; .byte 0xc7; .byte 0xf0; setc %1"\r
90 :"=a" (*Rand), \r
91 "=qm" (Carry)\r
92 );\r
93 \r
94 return (BOOLEAN) Carry;\r
95}\r