From 11826ca46ce135473d84e2abeff83916526301e2 Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Fri, 12 Apr 2019 11:35:24 +0200 Subject: [efi] Put rdrand workaround in sep. function --- src/interface/efi/efi_entropy.c | 57 +++++++++++++++++++++++++---------------- 1 file changed, 35 insertions(+), 22 deletions(-) (limited to 'src/interface') diff --git a/src/interface/efi/efi_entropy.c b/src/interface/efi/efi_entropy.c index 2dd231b22..a1dfaaa79 100644 --- a/src/interface/efi/efi_entropy.c +++ b/src/interface/efi/efi_entropy.c @@ -125,28 +125,6 @@ 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 ) { @@ -168,6 +146,40 @@ static int efi_entropy_tick ( void ) { return low; } +/** + * Get noise sample from rdrand + * + * @ret noise Noise sample + * @ret rc Return status code + */ +static int efi_get_noise_rdrand ( noise_sample_t *noise ) { +#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 ) { + *noise = ret; + return 0; + } + } + return -EBUSY; + } +#endif + return -ENOTSUP; +} + /** * Get noise sample from timer ticks * @@ -244,6 +256,7 @@ static int efi_get_noise ( noise_sample_t *noise ) { /* Try RNG first, falling back to timer ticks */ if ( ( ( rc = efi_get_noise_rng ( noise ) ) != 0 ) && + ( ( rc = efi_get_noise_rdrand ( noise ) ) != 0 ) && ( ( rc = efi_get_noise_ticks ( noise ) ) != 0 ) ) return rc; -- cgit v1.2.3-55-g7522