iPXE
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.

Functions

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

Variables

static EFI_EVENT tick
 Event used to wait for timer tick.

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 48 of file efi_entropy.c.

Referenced by efi_entropy_tick().

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL )

◆ FILE_SECBOOT()

FILE_SECBOOT ( PERMITTED )

◆ __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_entropy_enable(void)
Enable entropy gathering.
Definition efi_entropy.c:58
static int efi_get_noise(noise_sample_t *noise)
Get noise sample from timer ticks.
static void efi_entropy_disable(void)
Disable entropy gathering.
Definition efi_entropy.c:88

EFI entropy source.

References __entropy_source, and ENTROPY_FALLBACK.

◆ efi_entropy_enable()

int efi_entropy_enable ( void )
static

Enable entropy gathering.

Return values
rcReturn status code

Definition at line 58 of file efi_entropy.c.

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

References 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()

void efi_entropy_disable ( void )
static

Disable entropy gathering.

Definition at line 88 of file efi_entropy.c.

88 {
89 EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
90
91 /* Close timer tick event */
92 bs->CloseEvent ( tick );
93
94 /* Return to internal TPL */
96}
EFI_TPL efi_internal_tpl
Internal task priority level.
Definition efi_init.c:54
EFI_CLOSE_EVENT CloseEvent
Definition UefiSpec.h:1959
EFI_RAISE_TPL RaiseTPL
Definition UefiSpec.h:1940

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

◆ efi_entropy_tick()

int efi_entropy_tick ( void )
static

Wait for a timer tick.

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

Definition at line 103 of file efi_entropy.c.

103 {
104 EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
105 UINTN index;
107 EFI_STATUS efirc;
108 int rc;
109
110 /* Wait for next timer tick */
111 if ( ( efirc = bs->SetTimer ( tick, TimerRelative,
112 EFI_ENTROPY_TRIGGER_TIME ) ) != 0 ) {
113 rc = -EEFI ( efirc );
114 DBGC ( &tick, "ENTROPY could not set timer: %s\n",
115 strerror ( rc ) );
116 return rc;
117 }
118 if ( ( efirc = bs->WaitForEvent ( 1, &tick, &index ) ) != 0 ) {
119 rc = -EEFI ( efirc );
120 DBGC ( &tick, "ENTROPY could not wait for timer tick: %s\n",
121 strerror ( rc ) );
122 return rc;
123 }
124
125 /* Get current CPU profiling timestamp low-order bits */
127
128 return low;
129}
UINT64 UINTN
Unsigned value of native width.
@ TimerRelative
An event is to be signaled once at a specified interval from the current time.
Definition UefiSpec.h:552
unsigned short uint16_t
Definition stdint.h:11
long index
Definition bigint.h:65
#define EFI_ENTROPY_TRIGGER_TIME
Time (in 100ns units) to delay waiting for timer tick.
Definition efi_entropy.c:48
unsigned long profile_timestamp(void)
uint32_t low
Low 16 bits of address.
Definition myson.h:0
EFI_SET_TIMER SetTimer
Definition UefiSpec.h:1956
EFI_WAIT_FOR_EVENT WaitForEvent
Definition UefiSpec.h:1957

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

Referenced by efi_get_noise().

◆ efi_get_noise()

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 137 of file efi_entropy.c.

137 {
138 int before;
139 int after;
140 int rc;
141
142 /* Wait for a timer tick */
144 if ( before < 0 ) {
145 rc = before;
146 return rc;
147 }
148
149 /* Wait for another timer tick */
151 if ( after < 0 ) {
152 rc = after;
153 return rc;
154 }
155
156 /* Use TSC delta as noise sample */
157 *noise = ( after - before );
158
159 return 0;
160}
static int efi_entropy_tick(void)
Wait for a timer tick.
int32_t after
Final microcode version.
Definition ucode.h:7
int32_t before
Initial microcode version.
Definition ucode.h:5

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 51 of file efi_entropy.c.

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