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.
static struct efi_dropped_tpl efi_entropy_tpl
 Dropped TPL.

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:61
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:91

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

61 {
62 EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
63 EFI_STATUS efirc;
64 int rc;
65
66 /* Drop to external TPL to allow timer tick event to take place */
68
69 /* Create timer tick event */
70 if ( ( efirc = bs->CreateEvent ( EVT_TIMER, TPL_NOTIFY, NULL, NULL,
71 &tick ) ) != 0 ) {
72 rc = -EEFI ( efirc );
73 DBGC ( &tick, "ENTROPY could not create event: %s\n",
74 strerror ( rc ) );
75 return rc;
76 }
77
78 /* We use essentially the same mechanism as for the BIOS
79 * RTC-based entropy source, and so assume the same
80 * min-entropy per sample.
81 */
82 entropy_init ( &efitick_entropy, MIN_ENTROPY ( 1.3 ) );
83
84 return 0;
85}
#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
static struct efi_dropped_tpl efi_entropy_tpl
Dropped TPL.
Definition efi_entropy.c:54
void efi_drop_tpl(struct efi_dropped_tpl *tpl)
Drop task priority level temporarily to external level.
Definition efi_init.c:414
#define DBGC(...)
Definition compiler.h:505
#define EEFI(efirc)
Convert an EFI status code to an iPXE status code.
Definition efi.h:181
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_CREATE_EVENT CreateEvent
Definition UefiSpec.h:1955

References EFI_BOOT_SERVICES::CreateEvent, DBGC, EEFI, efi_drop_tpl(), efi_entropy_tpl, efi_systab, entropy_init(), EVT_TIMER, MIN_ENTROPY, NULL, rc, strerror(), tick, and TPL_NOTIFY.

◆ efi_entropy_disable()

void efi_entropy_disable ( void )
static

Disable entropy gathering.

Definition at line 91 of file efi_entropy.c.

91 {
92 EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
93
94 /* Close timer tick event */
95 bs->CloseEvent ( tick );
96
97 /* Return to internal TPL */
99}
void efi_undrop_tpl(struct efi_dropped_tpl *tpl)
Restore dropped task priority level.
Definition efi_init.c:430
EFI_CLOSE_EVENT CloseEvent
Definition UefiSpec.h:1959

References EFI_BOOT_SERVICES::CloseEvent, efi_entropy_tpl, efi_systab, efi_undrop_tpl(), 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 106 of file efi_entropy.c.

106 {
107 EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
108 UINTN index;
110 EFI_STATUS efirc;
111 int rc;
112
113 /* Wait for next timer tick */
114 if ( ( efirc = bs->SetTimer ( tick, TimerRelative,
115 EFI_ENTROPY_TRIGGER_TIME ) ) != 0 ) {
116 rc = -EEFI ( efirc );
117 DBGC ( &tick, "ENTROPY could not set timer: %s\n",
118 strerror ( rc ) );
119 return rc;
120 }
121 if ( ( efirc = bs->WaitForEvent ( 1, &tick, &index ) ) != 0 ) {
122 rc = -EEFI ( efirc );
123 DBGC ( &tick, "ENTROPY could not wait for timer tick: %s\n",
124 strerror ( rc ) );
125 return rc;
126 }
127
128 /* Get current CPU profiling timestamp low-order bits */
130
131 return low;
132}
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 140 of file efi_entropy.c.

140 {
141 int before;
142 int after;
143 int rc;
144
145 /* Wait for a timer tick */
147 if ( before < 0 ) {
148 rc = before;
149 return rc;
150 }
151
152 /* Wait for another timer tick */
154 if ( after < 0 ) {
155 rc = after;
156 return rc;
157 }
158
159 /* Use TSC delta as noise sample */
160 *noise = ( after - before );
161
162 return 0;
163}
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().

◆ efi_entropy_tpl

struct efi_dropped_tpl efi_entropy_tpl
static

Dropped TPL.

Definition at line 54 of file efi_entropy.c.

Referenced by efi_entropy_disable(), and efi_entropy_enable().