iPXE
Macros | Functions | Variables
efi_entropy.c File Reference

EFI entropy source. More...

#include <errno.h>
#include <ipxe/entropy.h>
#include <ipxe/profile.h>
#include <ipxe/efi/efi.h>

Go to the source code of this file.

Macros

#define EFI_ENTROPY_TRIGGER_TIME   10
 Time (in 100ns units) to delay waiting for timer tick. More...
 

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
 
struct entropy_source efitick_entropy __entropy_source (ENTROPY_FALLBACK)
 EFI entropy source. More...
 
static int efi_entropy_enable (void)
 Enable entropy gathering. More...
 
static void efi_entropy_disable (void)
 Disable entropy gathering. More...
 
static int efi_entropy_tick (void)
 Wait for a timer tick. More...
 
static int efi_get_noise (noise_sample_t *noise)
 Get noise sample from timer ticks. More...
 

Variables

static EFI_EVENT tick
 Event used to wait for timer tick. More...
 

Detailed Description

EFI entropy source.

Definition in file efi_entropy.c.

Macro Definition Documentation

◆ EFI_ENTROPY_TRIGGER_TIME

#define EFI_ENTROPY_TRIGGER_TIME   10

Time (in 100ns units) to delay waiting for timer tick.

In theory, UEFI allows us to specify a trigger time of zero to simply wait for the next timer tick. In practice, specifying zero seems to often return immediately, which produces almost no entropy. Specify a delay of 1000ns to try to force an existent delay.

Definition at line 47 of file efi_entropy.c.

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL  )

◆ __entropy_source()

struct entropy_source efitick_entropy __entropy_source ( ENTROPY_FALLBACK  )
Initial value:
= {
.name = "efitick",
.enable = efi_entropy_enable,
.disable = efi_entropy_disable,
.get_noise = efi_get_noise,
}
static int efi_get_noise(noise_sample_t *noise)
Get noise sample from timer ticks.
Definition: efi_entropy.c:136
static void efi_entropy_disable(void)
Disable entropy gathering.
Definition: efi_entropy.c:87
static int efi_entropy_enable(void)
Enable entropy gathering.
Definition: efi_entropy.c:57

EFI entropy source.

◆ efi_entropy_enable()

static int efi_entropy_enable ( void  )
static

Enable entropy gathering.

Return values
rcReturn status code

Definition at line 57 of file efi_entropy.c.

57  {
59  EFI_STATUS efirc;
60  int rc;
61 
62  /* Drop to external TPL to allow timer tick event to take place */
64 
65  /* Create timer tick event */
66  if ( ( efirc = bs->CreateEvent ( EVT_TIMER, TPL_NOTIFY, NULL, NULL,
67  &tick ) ) != 0 ) {
68  rc = -EEFI ( efirc );
69  DBGC ( &tick, "ENTROPY could not create event: %s\n",
70  strerror ( rc ) );
71  return rc;
72  }
73 
74  /* We use essentially the same mechanism as for the BIOS
75  * RTC-based entropy source, and so assume the same
76  * min-entropy per sample.
77  */
78  entropy_init ( &efitick_entropy, MIN_ENTROPY ( 1.3 ) );
79 
80  return 0;
81 }
EFI_BOOT_SERVICES * BootServices
A pointer to the EFI Boot Services Table.
Definition: UefiSpec.h:2081
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
#define EEFI(efirc)
Convert an EFI status code to an iPXE status code.
Definition: efi.h:171
static EFI_EVENT tick
Event used to wait for timer tick.
Definition: efi_entropy.c:50
#define DBGC(...)
Definition: compiler.h:505
#define TPL_NOTIFY
Definition: UefiSpec.h:639
EFI_CREATE_EVENT CreateEvent
Definition: UefiSpec.h:1941
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:78
EFI Boot Services Table.
Definition: UefiSpec.h:1917
#define MIN_ENTROPY(bits)
Construct a min-entropy fixed-point value.
Definition: entropy.h:42
RETURN_STATUS EFI_STATUS
Function return status for EFI API.
Definition: UefiBaseType.h:31
EFI_SYSTEM_TABLE * efi_systab
EFI_RESTORE_TPL RestoreTPL
Definition: UefiSpec.h:1927
static void entropy_init(struct entropy_source *source, min_entropy_t min_entropy_per_sample)
Initialise entropy source.
Definition: entropy.h:489
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321
#define EVT_TIMER
Definition: UefiSpec.h:440
EFI_TPL efi_external_tpl
External task priority level.
Definition: efi_init.c:55

References EFI_SYSTEM_TABLE::BootServices, EFI_BOOT_SERVICES::CreateEvent, DBGC, EEFI, efi_external_tpl, efi_systab, entropy_init(), EVT_TIMER, MIN_ENTROPY, NULL, rc, EFI_BOOT_SERVICES::RestoreTPL, strerror(), tick, and TPL_NOTIFY.

◆ efi_entropy_disable()

static void efi_entropy_disable ( void  )
static

Disable entropy gathering.

Definition at line 87 of file efi_entropy.c.

87  {
89 
90  /* Close timer tick event */
91  bs->CloseEvent ( tick );
92 
93  /* Return to internal TPL */
94  bs->RaiseTPL ( efi_internal_tpl );
95 }
EFI_BOOT_SERVICES * BootServices
A pointer to the EFI Boot Services Table.
Definition: UefiSpec.h:2081
EFI_RAISE_TPL RaiseTPL
Definition: UefiSpec.h:1926
static EFI_EVENT tick
Event used to wait for timer tick.
Definition: efi_entropy.c:50
EFI_CLOSE_EVENT CloseEvent
Definition: UefiSpec.h:1945
EFI_TPL efi_internal_tpl
Internal task priority level.
Definition: efi_init.c:52
EFI Boot Services Table.
Definition: UefiSpec.h:1917
EFI_SYSTEM_TABLE * efi_systab

References EFI_SYSTEM_TABLE::BootServices, EFI_BOOT_SERVICES::CloseEvent, efi_internal_tpl, efi_systab, EFI_BOOT_SERVICES::RaiseTPL, and tick.

◆ efi_entropy_tick()

static int efi_entropy_tick ( void  )
static

Wait for a timer tick.

Return values
lowCPU profiling low-order bits, or negative error

Definition at line 102 of file efi_entropy.c.

102  {
104  UINTN index;
105  uint16_t low;
106  EFI_STATUS efirc;
107  int rc;
108 
109  /* Wait for next timer tick */
110  if ( ( efirc = bs->SetTimer ( tick, TimerRelative,
111  EFI_ENTROPY_TRIGGER_TIME ) ) != 0 ) {
112  rc = -EEFI ( efirc );
113  DBGC ( &tick, "ENTROPY could not set timer: %s\n",
114  strerror ( rc ) );
115  return rc;
116  }
117  if ( ( efirc = bs->WaitForEvent ( 1, &tick, &index ) ) != 0 ) {
118  rc = -EEFI ( efirc );
119  DBGC ( &tick, "ENTROPY could not wait for timer tick: %s\n",
120  strerror ( rc ) );
121  return rc;
122  }
123 
124  /* Get current CPU profiling timestamp low-order bits */
125  low = profile_timestamp();
126 
127  return low;
128 }
EFI_BOOT_SERVICES * BootServices
A pointer to the EFI Boot Services Table.
Definition: UefiSpec.h:2081
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
unsigned short uint16_t
Definition: stdint.h:11
uint32_t low
Low 16 bits of address.
Definition: myson.h:19
#define EEFI(efirc)
Convert an EFI status code to an iPXE status code.
Definition: efi.h:171
An event is to be signaled once at a specified interval from the current time.
Definition: UefiSpec.h:541
static EFI_EVENT tick
Event used to wait for timer tick.
Definition: efi_entropy.c:50
#define DBGC(...)
Definition: compiler.h:505
EFI_SET_TIMER SetTimer
Definition: UefiSpec.h:1942
#define EFI_ENTROPY_TRIGGER_TIME
Time (in 100ns units) to delay waiting for timer tick.
Definition: efi_entropy.c:47
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:78
EFI Boot Services Table.
Definition: UefiSpec.h:1917
UINT64 UINTN
Unsigned value of native width.
EFI_WAIT_FOR_EVENT WaitForEvent
Definition: UefiSpec.h:1943
RETURN_STATUS EFI_STATUS
Function return status for EFI API.
Definition: UefiBaseType.h:31
EFI_SYSTEM_TABLE * efi_systab
uint64_t index
Index of the first segment within the content.
Definition: pccrc.h:21

References EFI_SYSTEM_TABLE::BootServices, DBGC, EEFI, EFI_ENTROPY_TRIGGER_TIME, efi_systab, index, low, rc, EFI_BOOT_SERVICES::SetTimer, strerror(), tick, TimerRelative, and EFI_BOOT_SERVICES::WaitForEvent.

Referenced by efi_get_noise().

◆ efi_get_noise()

static int efi_get_noise ( noise_sample_t noise)
static

Get noise sample from timer ticks.

Return values
noiseNoise sample
rcReturn status code

Definition at line 136 of file efi_entropy.c.

136  {
137  int before;
138  int after;
139  int rc;
140 
141  /* Wait for a timer tick */
143  if ( before < 0 ) {
144  rc = before;
145  return rc;
146  }
147 
148  /* Wait for another timer tick */
150  if ( after < 0 ) {
151  rc = after;
152  return rc;
153  }
154 
155  /* Use TSC delta as noise sample */
156  *noise = ( after - before );
157 
158  return 0;
159 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
static int efi_entropy_tick(void)
Wait for a timer tick.
Definition: efi_entropy.c:102
int32_t before
Initial microcode version.
Definition: ucode.h:16
int32_t after
Final microcode version.
Definition: ucode.h:18

References after, before, efi_entropy_tick(), and rc.

Variable Documentation

◆ tick

EFI_EVENT tick
static

Event used to wait for timer tick.

Definition at line 50 of file efi_entropy.c.

Referenced by efi_entropy_disable(), efi_entropy_enable(), efi_entropy_tick(), and fiber_autoneg().