iPXE
Functions | Variables
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. More...
 
static int efivars_find (const CHAR16 *wname, EFI_GUID *guid)
 Find first matching EFI variable name. More...
 
static int efivars_fetch (struct settings *settings __unused, struct setting *setting, void *data, size_t len)
 Fetch value of EFI variable setting. More...
 
static void efivars_init (void)
 Initialise EFI variable settings. More...
 
struct init_fn efivars_init_fn __init_fn (INIT_NORMAL)
 EFI variable settings initialiser. More...
 

Variables

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

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

static 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 }
A setting.
Definition: settings.h:24
static const struct settings_scope efivars_scope
EFI variable settings scope.
Definition: efi_settings.c:43
const struct settings_scope * scope
Setting scope (or NULL)
Definition: settings.h:50

References efivars_scope, and setting::scope.

◆ efivars_find()

static 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  {
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 }
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:175
uint16_t size
Buffer size.
Definition: dwmac.h:14
#define DBGC(...)
Definition: compiler.h:505
#define ENOENT
No such file or directory.
Definition: errno.h:515
struct xfer_buffer buf
Data transfer buffer.
Definition: efi_pxe.c:95
unsigned short CHAR16
#define EFI_BUFFER_TOO_SMALL
Enumeration of EFI_STATUS.
Definition: UefiBaseType.h:120
size_t wcslen(const wchar_t *string)
Calculate length of wide-character string.
Definition: wchar.c:57
unsigned long tmp
Definition: linux_pci.h:65
#define ENOMEM
Not enough space.
Definition: errno.h:535
static struct settings efivars
EFI variable settings.
Definition: efi_settings.c:46
EFI Runtime Services Table.
Definition: UefiSpec.h:1880
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:79
static void(* free)(struct refcnt *refcnt))
Definition: refcnt.h:55
void * zalloc(size_t size)
Allocate cleared memory.
Definition: malloc.c:662
UINT64 UINTN
Unsigned value of native width.
const char * efi_guid_ntoa(CONST EFI_GUID *guid)
Convert GUID to a printable string.
Definition: efi_guid.c:726
uint64_t guid
GUID.
Definition: edd.h:31
EFI_RUNTIME_SERVICES * RuntimeServices
A pointer to the EFI Runtime Services Table.
Definition: UefiSpec.h:2095
#define EFI_NOT_FOUND
Enumeration of EFI_STATUS.
Definition: UefiBaseType.h:129
#define DBGC2(...)
Definition: compiler.h:522
RETURN_STATUS EFI_STATUS
Function return status for EFI API.
Definition: UefiBaseType.h:32
EFI_GET_NEXT_VARIABLE_NAME GetNextVariableName
Definition: UefiSpec.h:1904
void * realloc(void *old_ptr, size_t new_size)
Reallocate memory.
Definition: malloc.c:607
EFI_SYSTEM_TABLE * efi_systab
int memcmp(const void *first, const void *second, size_t len)
Compare memory regions.
Definition: string.c:115

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(), EFI_SYSTEM_TABLE::RuntimeServices, size, strerror(), tmp, wcslen(), and zalloc().

Referenced by efivars_fetch().

◆ efivars_fetch()

static 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  {
136  size_t name_len = strlen ( setting->name );
137  CHAR16 wname[ name_len + 1 /* wNUL */ ];
138  EFI_GUID guid;
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 }
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:175
128 bit buffer containing a unique identifier value.
Definition: Base.h:216
uint16_t size
Buffer size.
Definition: dwmac.h:14
#define DBGC(...)
Definition: compiler.h:505
unsigned int UINT32
Definition: ProcessorBind.h:99
struct xfer_buffer buf
Data transfer buffer.
Definition: efi_pxe.c:95
unsigned short CHAR16
#define EFI_BUFFER_TOO_SMALL
Enumeration of EFI_STATUS.
Definition: UefiBaseType.h:120
static int efivars_find(const CHAR16 *wname, EFI_GUID *guid)
Find first matching EFI variable name.
Definition: efi_settings.c:68
const char * name
Name.
Definition: settings.h:29
#define ENOMEM
Not enough space.
Definition: errno.h:535
void * memcpy(void *dest, const void *src, size_t len) __nonnull
#define DBGC_HDA(...)
Definition: compiler.h:506
static struct settings efivars
EFI variable settings.
Definition: efi_settings.c:46
ring len
Length.
Definition: dwmac.h:231
int efi_snprintf(wchar_t *wbuf, size_t wsize, const char *fmt,...)
Write a formatted string to a buffer.
Definition: efi_strings.c:107
const struct setting_type * type
Setting type.
Definition: settings.h:37
EFI_GET_VARIABLE GetVariable
Definition: UefiSpec.h:1903
EFI Runtime Services Table.
Definition: UefiSpec.h:1880
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:79
static void(* free)(struct refcnt *refcnt))
Definition: refcnt.h:55
size_t strlen(const char *src)
Get length of string.
Definition: string.c:244
UINT64 UINTN
Unsigned value of native width.
const char * efi_guid_ntoa(CONST EFI_GUID *guid)
Convert GUID to a printable string.
Definition: efi_guid.c:726
void * malloc(size_t size)
Allocate memory.
Definition: malloc.c:621
A setting.
Definition: settings.h:24
uint64_t guid
GUID.
Definition: edd.h:31
EFI_RUNTIME_SERVICES * RuntimeServices
A pointer to the EFI Runtime Services Table.
Definition: UefiSpec.h:2095
RETURN_STATUS EFI_STATUS
Function return status for EFI API.
Definition: UefiBaseType.h:32
uint8_t data[48]
Additional event data.
Definition: ena.h:22
EFI_SYSTEM_TABLE * efi_systab
#define NULL
NULL pointer (VOID *)
Definition: Base.h:322

References 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, EFI_SYSTEM_TABLE::RuntimeServices, size, strerror(), strlen(), and setting::type.

◆ efivars_init()

static 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 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
#define DBGC(...)
Definition: compiler.h:505
static struct settings efivars
EFI variable settings.
Definition: efi_settings.c:46
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:79
int register_settings(struct settings *settings, struct settings *parent, const char *name)
Register settings block.
Definition: settings.c:476
#define NULL
NULL pointer (VOID *)
Definition: Base.h:322

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

◆ __init_fn()

struct init_fn efivars_init_fn __init_fn ( INIT_NORMAL  )

EFI variable settings initialiser.

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

static 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.
Definition: efi_settings.c:205
static struct settings efivars
EFI variable settings.
Definition: efi_settings.c:46
struct list_head siblings
Sibling settings blocks.
Definition: settings.h:141
static const struct settings_scope efivars_scope
EFI variable settings scope.
Definition: efi_settings.c:43
struct list_head children
Child settings blocks.
Definition: settings.h:143
#define LIST_HEAD_INIT(list)
Initialise a static list head.
Definition: list.h:31
#define NULL
NULL pointer (VOID *)
Definition: Base.h:322

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.
Definition: efi_settings.c:133
static int efivars_applies(struct settings *settings __unused, const struct setting *setting)
Check applicability of EFI variable setting.
Definition: efi_settings.c:55

EFI variable settings operations.

Definition at line 205 of file efi_settings.c.