1#------------------------------------------------------------------------------ ;
2# Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>
3# This program and the accompanying materials
4# are licensed and made available under the terms and conditions of the BSD License
5# which accompanies this distribution.  The full text of the license may be found at
6# http://opensource.org/licenses/bsd-license.php.
7#
8# THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
9# WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
10#
11# Module Name:
12#
13#   RdRand.S
14#
15# Abstract:
16#
17#   Generates random number through CPU RdRand instruction under 32-bit platform.
18#
19# Notes:
20#
21#------------------------------------------------------------------------------
22
23//------------------------------------------------------------------------------
24//  Generates a 16 bit random number through RDRAND instruction.
25//  Return TRUE if Rand generated successfully, or FALSE if not.
26//
27//  BOOLEAN EFIAPI AsmRdRand16 (UINT16 *Rand);
28//------------------------------------------------------------------------------
29ASM_GLOBAL ASM_PFX(AsmRdRand16)
30ASM_PFX(AsmRdRand16):
31    .byte  0x0f, 0xc7, 0xf0        // rdrand r16: "0f c7 /6  ModRM:r/m(w)"
32    jc     rn16_ok                 // jmp if CF=1
33    xor    %eax, %eax              // reg=0 if CF=0
34    ret                            // return with failure status
35rn16_ok:
36    mov    0x4(%esp), %edx
37    mov    %ax, (%edx)
38    mov    $0x1, %eax
39    ret
40
41//------------------------------------------------------------------------------
42//  Generates a 32 bit random number through RDRAND instruction.
43//  Return TRUE if Rand generated successfully, or FALSE if not.
44//
45//  BOOLEAN EFIAPI AsmRdRand32 (UINT32 *Rand);
46//------------------------------------------------------------------------------
47ASM_GLOBAL ASM_PFX(AsmRdRand32)
48ASM_PFX(AsmRdRand32):
49    .byte  0x0f, 0xc7, 0xf0        // rdrand r32: "0f c7 /6  ModRM:r/m(w)"
50    jc     rn32_ok                 // jmp if CF=1
51    xor    %eax, %eax              // reg=0 if CF=0
52    ret                            // return with failure status
53rn32_ok:
54    mov    0x4(%esp), %edx
55    mov    %eax, (%edx)
56    mov    $0x1, %eax
57    ret
58
59//------------------------------------------------------------------------------
60//  Generates a 64 bit random number through RDRAND instruction.
61//  Return TRUE if Rand generated successfully, or FALSE if not.
62//
63//  BOOLEAN EFIAPI AsmRdRand64 (UINT64 *Rand);
64//------------------------------------------------------------------------------
65ASM_GLOBAL ASM_PFX(AsmRdRand64)
66ASM_PFX(AsmRdRand64):
67    .byte  0x0f, 0xc7, 0xf0        // rdrand r32: "0f c7 /6  ModRM:r/m(w)"
68    jnc    rn64_ret                // jmp if CF=0
69    mov    0x4(%esp), %edx
70    mov    %eax, (%edx)
71
72    .byte  0x0f, 0xc7, 0xf0        // generate another 32 bit RN
73    jnc    rn64_ret                // jmp if CF=0
74    mov    %eax, 0x4(%edx)
75
76    mov    $0x1, %eax
77    ret
78rn64_ret:
79    xor    %eax, %eax
80    ret                            // return with failure status
81