summaryrefslogtreecommitdiffstats
path: root/src/interface/efi
diff options
context:
space:
mode:
Diffstat (limited to 'src/interface/efi')
-rw-r--r--src/interface/efi/efi_entropy.c27
1 files changed, 27 insertions, 0 deletions
diff --git a/src/interface/efi/efi_entropy.c b/src/interface/efi/efi_entropy.c
index 2a2fc9054..2dd231b22 100644
--- a/src/interface/efi/efi_entropy.c
+++ b/src/interface/efi/efi_entropy.c
@@ -30,6 +30,11 @@ FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
#include <ipxe/efi/efi.h>
#include <ipxe/efi/Protocol/Rng.h>
+#if defined(__i386) || defined(__x86_64)
+#include <ipxe/cpuid.h>
+static char have_hwrnd = 0;
+#endif
+
/** @file
*
* EFI entropy source
@@ -120,6 +125,28 @@ static int efi_entropy_tick ( void ) {
EFI_STATUS efirc;
int rc;
+#if defined(__i386) || defined(__x86_64)
+ if ( have_hwrnd == 0 ) {
+ struct x86_features features;
+ x86_features( &features );
+ if ( features.intel.ecx & ( 1 << 30 ) ) {
+ have_hwrnd = 1;
+ } else {
+ have_hwrnd = 2;
+ }
+ DBGC( &tick, "Have RDRAND: %s\n", ( have_hwrnd == 1 ? "YES!" : "NO :-(" ) );
+ }
+ if ( have_hwrnd == 1 ) {
+ int ret, retries = 10;
+ char ok;
+ while ( --retries > 0 ) {
+ __asm__ volatile ( "rdrand %0; setc %1" : "=r" ( ret ), "=qm" ( ok ) );
+ if ( ok )
+ return ret & 0x7fffffff;
+ }
+ }
+#endif
+
/* Wait for next timer tick */
if ( ( efirc = bs->SetTimer ( tick, TimerRelative,
EFI_ENTROPY_TRIGGER_TIME ) ) != 0 ) {