From a58276abdde401bae19525c7c5895c6fbbfc22e2 Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Fri, 15 Feb 2019 10:38:49 +0100 Subject: [efi] Add very ugly hack to use HW RNG on EFI This is just added in the completely wrong spot, but makes very slow initialization on efi go away. --- src/interface/efi/efi_entropy.c | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) (limited to 'src/interface') 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 #include +#if defined(__i386) || defined(__x86_64) +#include +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 ) { -- cgit v1.2.3-55-g7522