iPXE
efi_settings.c File Reference

EFI variable settings. More...

#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <ipxe/settings.h>
#include <ipxe/init.h>
#include <ipxe/efi/efi.h>
#include <ipxe/efi/efi_strings.h>

Go to the source code of this file.

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
 FILE_SECBOOT (PERMITTED)
static int efivars_applies (struct settings *settings __unused, const struct setting *setting)
 Check applicability of EFI variable setting.
static int efivars_find (const CHAR16 *wname, EFI_GUID *guid)
 Find first matching EFI variable name.
static int efivars_fetch (struct settings *settings __unused, struct setting *setting, void *data, size_t len)
 Fetch value of EFI variable setting.
static void efivars_init (void)
 Initialise EFI variable settings.
struct init_fn efivars_init_fn __init_fn (INIT_NORMAL)
 EFI variable settings initialiser.

Variables

static const struct settings_scope efivars_scope
 EFI variable settings scope.
static struct settings efivars
 EFI variable settings.
static struct settings_operations efivars_operations
 EFI variable settings operations.

Detailed Description

EFI variable settings.

Definition in file efi_settings.c.

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL )

◆ FILE_SECBOOT()

FILE_SECBOOT ( PERMITTED )

◆ efivars_applies()

int efivars_applies ( struct settings *settings __unused,
const struct setting * setting )
static

Check applicability of EFI variable setting.

Parameters
settingsSettings block
settingSetting
Return values
appliesSetting applies within this settings block

Definition at line 55 of file efi_settings.c.

56 {
57
58 return ( setting->scope == &efivars_scope );
59}
static const struct settings_scope efivars_scope
EFI variable settings scope.
A setting.
Definition settings.h:24
const struct settings_scope * scope
Setting scope (or NULL)
Definition settings.h:50

References __unused, efivars_scope, and setting::scope.

◆ efivars_find()

int efivars_find ( const CHAR16 * wname,
EFI_GUID * guid )
static

Find first matching EFI variable name.

Parameters
wnameName
guidGUID to fill in
Return values
rcReturn status code

Definition at line 68 of file efi_settings.c.

68 {
69 EFI_RUNTIME_SERVICES *rs = efi_systab->RuntimeServices;
70 size_t wname_len = ( ( wcslen ( wname ) + 1 ) * sizeof ( wname[0] ) );
71 CHAR16 *buf;
72 CHAR16 *tmp;
73 UINTN size;
74 EFI_STATUS efirc;
75 int rc;
76
77 /* Allocate single wNUL for first call to GetNextVariableName() */
78 size = sizeof ( buf[0] );
79 buf = zalloc ( size );
80 if ( ! buf )
81 return -ENOMEM;
82
83 /* Iterate over all veriables */
84 while ( 1 ) {
85
86 /* Get next variable name */
87 efirc = rs->GetNextVariableName ( &size, buf, guid );
88 if ( efirc == EFI_BUFFER_TOO_SMALL ) {
89 tmp = realloc ( buf, size );
90 if ( ! tmp ) {
91 rc = -ENOMEM;
92 break;
93 }
94 buf = tmp;
95 efirc = rs->GetNextVariableName ( &size, buf, guid );
96 }
97 if ( efirc == EFI_NOT_FOUND ) {
98 rc = -ENOENT;
99 break;
100 }
101 if ( efirc != 0 ) {
102 rc = -EEFI ( efirc );
103 DBGC ( &efivars, "EFIVARS %s:%ls could not fetch next "
104 "variable name: %s\n",
105 efi_guid_ntoa ( guid ), buf, strerror ( rc ) );
106 break;
107 }
108 DBGC2 ( &efivars, "EFIVARS %s:%ls exists\n",
109 efi_guid_ntoa ( guid ), buf );
110
111 /* Check for matching variable name */
112 if ( memcmp ( wname, buf, wname_len ) == 0 ) {
113 rc = 0;
114 break;
115 }
116 }
117
118 /* Free temporary buffer */
119 free ( buf );
120
121 return rc;
122}
UINT64 UINTN
Unsigned value of native width.
unsigned short CHAR16
2-byte Character.
#define EFI_NOT_FOUND
Enumeration of EFI_STATUS.
RETURN_STATUS EFI_STATUS
Function return status for EFI API.
#define EFI_BUFFER_TOO_SMALL
Enumeration of EFI_STATUS.
struct arbelprm_rc_send_wqe rc
Definition arbel.h:3
uint64_t guid
GUID.
Definition edd.h:1
const char * efi_guid_ntoa(CONST EFI_GUID *guid)
Convert GUID to a printable string.
Definition efi_guid.c:726
static struct settings efivars
EFI variable settings.
#define DBGC2(...)
Definition compiler.h:522
#define DBGC(...)
Definition compiler.h:505
uint16_t size
Buffer size.
Definition dwmac.h:3
#define ENOENT
No such file or directory.
Definition errno.h:515
#define ENOMEM
Not enough space.
Definition errno.h:535
#define EEFI(efirc)
Convert an EFI status code to an iPXE status code.
Definition efi.h:175
EFI_SYSTEM_TABLE * efi_systab
unsigned long tmp
Definition linux_pci.h:65
void * realloc(void *old_ptr, size_t new_size)
Reallocate memory.
Definition malloc.c:607
void * zalloc(size_t size)
Allocate cleared memory.
Definition malloc.c:662
static void(* free)(struct refcnt *refcnt))
Definition refcnt.h:55
char * strerror(int errno)
Retrieve string representation of error number.
Definition strerror.c:79
int memcmp(const void *first, const void *second, size_t len)
Compare memory regions.
Definition string.c:115
EFI Runtime Services Table.
Definition UefiSpec.h:1880
EFI_GET_NEXT_VARIABLE_NAME GetNextVariableName
Definition UefiSpec.h:1904
size_t wcslen(const wchar_t *string)
Calculate length of wide-character string.
Definition wchar.c:57

References DBGC, DBGC2, EEFI, EFI_BUFFER_TOO_SMALL, efi_guid_ntoa(), EFI_NOT_FOUND, efi_systab, efivars, ENOENT, ENOMEM, free, EFI_RUNTIME_SERVICES::GetNextVariableName, guid, memcmp(), rc, realloc(), size, strerror(), tmp, wcslen(), and zalloc().

Referenced by efivars_fetch().

◆ efivars_fetch()

int efivars_fetch ( struct settings *settings __unused,
struct setting * setting,
void * data,
size_t len )
static

Fetch value of EFI variable setting.

Parameters
settingsSettings block
settingSetting to fetch
dataBuffer to fill with setting data
lenLength of buffer
Return values
lenLength of setting data, or negative error

Definition at line 133 of file efi_settings.c.

134 {
135 EFI_RUNTIME_SERVICES *rs = efi_systab->RuntimeServices;
136 size_t name_len = strlen ( setting->name );
137 CHAR16 wname[ name_len + 1 /* wNUL */ ];
139 UINT32 attrs;
140 UINTN size;
141 void *buf;
142 EFI_STATUS efirc;
143 int rc;
144
145 /* Convert name to UCS-2 */
146 efi_snprintf ( wname, sizeof ( wname ), "%s", setting->name );
147
148 /* Find variable GUID */
149 if ( ( rc = efivars_find ( wname, &guid ) ) != 0 )
150 goto err_find;
151
152 /* Get variable length */
153 size = 0;
154 if ( ( efirc = rs->GetVariable ( wname, &guid, &attrs, &size,
155 NULL ) != EFI_BUFFER_TOO_SMALL ) ) {
156 rc = -EEFI ( efirc );
157 DBGC ( &efivars, "EFIVARS %s:%ls could not get size: %s\n",
158 efi_guid_ntoa ( &guid ), wname, strerror ( rc ) );
159 goto err_len;
160 }
161
162 /* Allocate temporary buffer, since GetVariable() is not
163 * guaranteed to return partial data for an underlength
164 * buffer.
165 */
166 buf = malloc ( size );
167 if ( ! buf ) {
168 rc = -ENOMEM;
169 goto err_alloc;
170 }
171
172 /* Get variable value */
173 if ( ( efirc = rs->GetVariable ( wname, &guid, &attrs, &size,
174 buf ) ) != 0 ) {
175 rc = -EEFI ( efirc );
176 DBGC ( &efivars, "EFIVARS %s:%ls could not get %zd bytes: "
177 "%s\n", efi_guid_ntoa ( &guid ), wname,
178 ( ( size_t ) size ), strerror ( rc ) );
179 goto err_get;
180 }
181 DBGC ( &efivars, "EFIVARS %s:%ls:\n", efi_guid_ntoa ( &guid ), wname );
182 DBGC_HDA ( &efivars, 0, buf, size );
183
184 /* Return setting value */
185 if ( len > size )
186 len = size;
187 memcpy ( data, buf, len );
188 if ( ! setting->type )
189 setting->type = &setting_type_hex;
190
191 /* Free temporary buffer */
192 free ( buf );
193
194 return size;
195
196 err_get:
197 free ( buf );
198 err_alloc:
199 err_len:
200 err_find:
201 return rc;
202}
unsigned int UINT32
4-byte unsigned value.
#define NULL
NULL pointer (VOID *)
Definition Base.h:322
GUID EFI_GUID
128-bit buffer containing a unique identifier value.
ring len
Length.
Definition dwmac.h:226
static int efivars_find(const CHAR16 *wname, EFI_GUID *guid)
Find first matching EFI variable name.
int efi_snprintf(wchar_t *wbuf, size_t wsize, const char *fmt,...)
Write a formatted string to a buffer.
uint8_t data[48]
Additional event data.
Definition ena.h:11
#define DBGC_HDA(...)
Definition compiler.h:506
void * memcpy(void *dest, const void *src, size_t len) __nonnull
void * malloc(size_t size)
Allocate memory.
Definition malloc.c:621
size_t strlen(const char *src)
Get length of string.
Definition string.c:244
EFI_GET_VARIABLE GetVariable
Definition UefiSpec.h:1903
const char * name
Name.
Definition settings.h:29
const struct setting_type * type
Setting type.
Definition settings.h:37

References __unused, data, DBGC, DBGC_HDA, EEFI, EFI_BUFFER_TOO_SMALL, efi_guid_ntoa(), efi_snprintf(), efi_systab, efivars, efivars_find(), ENOMEM, free, EFI_RUNTIME_SERVICES::GetVariable, guid, len, malloc(), memcpy(), setting::name, NULL, rc, size, strerror(), strlen(), and setting::type.

◆ efivars_init()

void efivars_init ( void )
static

Initialise EFI variable settings.

Definition at line 223 of file efi_settings.c.

223 {
224 int rc;
225
226 /* Register settings block */
227 if ( ( rc = register_settings ( &efivars, NULL, "efi" ) ) != 0 ) {
228 DBGC ( &efivars, "EFIVARS could not register: %s\n",
229 strerror ( rc ) );
230 return;
231 }
232}
int register_settings(struct settings *settings, struct settings *parent, const char *name)
Register settings block.
Definition settings.c:476

References DBGC, efivars, NULL, rc, register_settings(), and strerror().

Referenced by __init_fn().

◆ __init_fn()

struct init_fn efivars_init_fn __init_fn ( INIT_NORMAL )

EFI variable settings initialiser.

References __init_fn, efivars_init(), and INIT_NORMAL.

Variable Documentation

◆ efivars_scope

const struct settings_scope efivars_scope
static

EFI variable settings scope.

Definition at line 43 of file efi_settings.c.

Referenced by efivars_applies().

◆ efivars

struct settings efivars
static
Initial value:
= {
.refcnt = NULL,
.siblings = LIST_HEAD_INIT ( efivars.siblings ),
.children = LIST_HEAD_INIT ( efivars.children ),
.default_scope = &efivars_scope,
}
static struct settings_operations efivars_operations
EFI variable settings operations.
#define LIST_HEAD_INIT(list)
Initialise a static list head.
Definition list.h:31

EFI variable settings.

Definition at line 46 of file efi_settings.c.

Referenced by efivars_fetch(), efivars_find(), and efivars_init().

◆ efivars_operations

struct settings_operations efivars_operations
static
Initial value:
= {
.applies = efivars_applies,
.fetch = efivars_fetch,
}
static int efivars_fetch(struct settings *settings __unused, struct setting *setting, void *data, size_t len)
Fetch value of EFI variable setting.
static int efivars_applies(struct settings *settings __unused, const struct setting *setting)
Check applicability of EFI variable setting.

EFI variable settings operations.

Definition at line 205 of file efi_settings.c.

205 {
206 .applies = efivars_applies,
207 .fetch = efivars_fetch,
208};