iPXE
Functions | Variables
efi_init.c File Reference
#include <string.h>
#include <errno.h>
#include <ipxe/init.h>
#include <ipxe/efi/efi.h>
#include <ipxe/efi/efi_driver.h>
#include <ipxe/efi/Protocol/LoadedImage.h>

Go to the source code of this file.

Functions

 FILE_LICENCE (GPL2_OR_LATER)
 
EFI_SYSTEM_TABLE_C2 (PLATFORM, _systab)
 System table passed to entry point. More...
 
static EFI_STATUS EFIAPI efi_unload (EFI_HANDLE image_handle __unused)
 Shut down EFI environment. More...
 
static EFIAPI void efi_shutdown_hook (EFI_EVENT event __unused, void *context __unused)
 Shut down in preparation for booting an OS. More...
 
static void * efi_find_table (EFI_GUID *guid)
 Look up EFI configuration table. More...
 
unsigned long efi_stack_cookie (EFI_HANDLE handle)
 Construct a stack cookie value. More...
 
EFI_STATUS efi_init (EFI_HANDLE image_handle, EFI_SYSTEM_TABLE *systab)
 Initialise EFI environment. More...
 
void __stack_chk_fail (void)
 Abort on stack check failure. More...
 

Variables

EFI_HANDLE efi_image_handle
 Image handle passed to entry point. More...
 
EFI_LOADED_IMAGE_PROTOCOLefi_loaded_image
 Loaded image protocol for this image. More...
 
int efi_shutdown_in_progress
 EFI shutdown is in progress. More...
 
static EFI_EVENT efi_shutdown_event
 Event used to signal shutdown. More...
 
unsigned long __stack_chk_guard
 Stack cookie. More...
 
static EFI_EXIT efi_exit
 Exit function. More...
 

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER  )

◆ _C2()

EFI_SYSTEM_TABLE* _C2 ( PLATFORM  ,
_systab   
)

System table passed to entry point.

We construct the symbol name efi_systab via the PLATFORM macro. This ensures that the symbol is defined only in EFI builds, and so prevents EFI code from being incorrectly linked in to a non-EFI build.

◆ efi_unload()

static EFI_STATUS EFIAPI efi_unload ( EFI_HANDLE image_handle  __unused)
static

Shut down EFI environment.

Parameters
image_handleImage handle

Definition at line 257 of file efi_init.c.

257  {
259  EFI_SYSTEM_TABLE *systab = efi_systab;
260 
261  DBGC ( systab, "EFI image unloading\n" );
262 
263  /* Shut down */
264  shutdown_exit();
265 
266  /* Disconnect any remaining devices */
268 
269  /* Uninstall driver binding protocol */
271 
272  /* Uninstall exit boot services event */
274 
275  DBGC ( systab, "EFI image unloaded\n" );
276 
277  return 0;
278 }
void efi_driver_uninstall(void)
Uninstall EFI driver.
Definition: efi_driver.c:419
EFI_BOOT_SERVICES * BootServices
A pointer to the EFI Boot Services Table.
Definition: UefiSpec.h:2000
#define DBGC(...)
Definition: compiler.h:505
EFI_CLOSE_EVENT CloseEvent
Definition: UefiSpec.h:1864
static EFI_EVENT efi_shutdown_event
Event used to signal shutdown.
Definition: efi_init.c:48
static void shutdown_exit(void)
Shut down system for exit back to firmware.
Definition: init.h:84
EFI Boot Services Table.
Definition: UefiSpec.h:1836
void efi_driver_disconnect_all(void)
Disconnect EFI driver from all possible devices.
Definition: efi_driver.c:585
EFI System Table.
Definition: UefiSpec.h:1949
EFI_SYSTEM_TABLE * efi_systab

References EFI_SYSTEM_TABLE::BootServices, EFI_BOOT_SERVICES::CloseEvent, DBGC, efi_driver_disconnect_all(), efi_driver_uninstall(), efi_shutdown_event, efi_systab, and shutdown_exit().

Referenced by efi_init().

◆ efi_shutdown_hook()

static EFIAPI void efi_shutdown_hook ( EFI_EVENT event  __unused,
void *context  __unused 
)
static

Shut down in preparation for booting an OS.

This hook gets called at ExitBootServices time in order to make sure that everything is properly shut down before the OS takes over.

Definition at line 70 of file efi_init.c.

71  {
72 
73  /* Mark shutdown as being in progress, to indicate that large
74  * parts of the system (e.g. timers) are no longer functional.
75  */
77 
78  /* Shut down iPXE */
79  shutdown_boot();
80 }
static void shutdown_boot(void)
Shut down system for OS boot.
Definition: init.h:76
int efi_shutdown_in_progress
EFI shutdown is in progress.
Definition: efi_init.c:45

References efi_shutdown_in_progress, and shutdown_boot().

Referenced by efi_init().

◆ efi_find_table()

static void* efi_find_table ( EFI_GUID guid)
static

Look up EFI configuration table.

Parameters
guidConfiguration table GUID
Return values
tableConfiguration table, or NULL

Definition at line 88 of file efi_init.c.

88  {
89  unsigned int i;
90 
91  for ( i = 0 ; i < efi_systab->NumberOfTableEntries ; i++ ) {
93  guid, sizeof ( *guid ) ) == 0 )
95  }
96 
97  return NULL;
98 }
EFI_GUID VendorGuid
The 128-bit GUID value that uniquely identifies the system configuration table.
Definition: UefiSpec.h:1939
uint64_t guid
GUID.
Definition: edd.h:30
UINTN NumberOfTableEntries
The number of system configuration tables in the buffer ConfigurationTable.
Definition: UefiSpec.h:2004
EFI_SYSTEM_TABLE * efi_systab
int memcmp(const void *first, const void *second, size_t len)
Compare memory regions.
Definition: string.c:98
#define NULL
NULL pointer (VOID *)
Definition: Base.h:362
EFI_CONFIGURATION_TABLE * ConfigurationTable
A pointer to the system configuration tables.
Definition: UefiSpec.h:2009
VOID * VendorTable
A pointer to the table associated with VendorGuid.
Definition: UefiSpec.h:1943

References EFI_SYSTEM_TABLE::ConfigurationTable, efi_systab, guid, memcmp(), NULL, EFI_SYSTEM_TABLE::NumberOfTableEntries, EFI_CONFIGURATION_TABLE::VendorGuid, and EFI_CONFIGURATION_TABLE::VendorTable.

Referenced by efi_init().

◆ efi_stack_cookie()

unsigned long efi_stack_cookie ( EFI_HANDLE  handle)

Construct a stack cookie value.

Parameters
handleImage handle
Return values
cookieStack cookie

Definition at line 107 of file efi_init.c.

107  {
108 
109  /* There is no viable source of entropy available at this
110  * point. Construct a value that is at least likely to vary
111  * between platforms and invocations.
112  *
113  * Ensure that the value contains a NUL byte, to act as a
114  * runaway string terminator. Construct the NUL using a shift
115  * rather than a mask, to avoid losing valuable entropy in the
116  * low-order bits.
117  */
118  return ( ( ( ( unsigned long ) handle ) ^
119  ( ( unsigned long ) &handle ) ^
120  profile_timestamp() ^ build_id ) << 8 );
121 }
unsigned long build_id
Build ID.
Definition: version.c:60
uint16_t handle
Handle.
Definition: smbios.h:16

References build_id, and handle.

Referenced by efi_init_stack_guard().

◆ efi_init()

EFI_STATUS efi_init ( EFI_HANDLE  image_handle,
EFI_SYSTEM_TABLE systab 
)

Initialise EFI environment.

Parameters
image_handleImage handle
systabSystem table
Return values
efircEFI return status code

Definition at line 130 of file efi_init.c.

131  {
132  EFI_BOOT_SERVICES *bs;
133  struct efi_protocol *prot;
134  struct efi_config_table *tab;
135  void *loaded_image;
136  EFI_STATUS efirc;
137  int rc;
138 
139  /* Store image handle and system table pointer for future use */
140  efi_image_handle = image_handle;
141  efi_systab = systab;
142 
143  /* Sanity checks */
144  if ( ! systab ) {
145  efirc = EFI_NOT_AVAILABLE_YET;
146  goto err_sanity;
147  }
148  if ( ! systab->ConOut ) {
149  efirc = EFI_NOT_AVAILABLE_YET;
150  goto err_sanity;
151  }
152  if ( ! systab->BootServices ) {
153  DBGC ( systab, "EFI provided no BootServices entry point\n" );
154  efirc = EFI_NOT_AVAILABLE_YET;
155  goto err_sanity;
156  }
157  if ( ! systab->RuntimeServices ) {
158  DBGC ( systab, "EFI provided no RuntimeServices entry "
159  "point\n" );
160  efirc = EFI_NOT_AVAILABLE_YET;
161  goto err_sanity;
162  }
163  DBGC ( systab, "EFI handle %p systab %p\n", image_handle, systab );
164  bs = systab->BootServices;
165 
166  /* Store abort function pointer */
167  efi_exit = bs->Exit;
168 
169  /* Look up used protocols */
171  if ( ( efirc = bs->LocateProtocol ( &prot->guid, NULL,
172  prot->protocol ) ) == 0 ) {
173  DBGC ( systab, "EFI protocol %s is at %p\n",
174  efi_guid_ntoa ( &prot->guid ),
175  *(prot->protocol) );
176  } else {
177  DBGC ( systab, "EFI does not provide protocol %s\n",
178  efi_guid_ntoa ( &prot->guid ) );
179  /* Fail if protocol is required */
180  if ( prot->required )
181  goto err_missing_protocol;
182  }
183  }
184 
185  /* Look up used configuration tables */
187  if ( ( *(tab->table) = efi_find_table ( &tab->guid ) ) ) {
188  DBGC ( systab, "EFI configuration table %s is at %p\n",
189  efi_guid_ntoa ( &tab->guid ), *(tab->table) );
190  } else {
191  DBGC ( systab, "EFI does not provide configuration "
192  "table %s\n", efi_guid_ntoa ( &tab->guid ) );
193  if ( tab->required ) {
194  efirc = EFI_NOT_AVAILABLE_YET;
195  goto err_missing_table;
196  }
197  }
198  }
199 
200  /* Get loaded image protocol */
201  if ( ( efirc = bs->OpenProtocol ( image_handle,
203  &loaded_image, image_handle, NULL,
204  EFI_OPEN_PROTOCOL_GET_PROTOCOL ) ) != 0 ) {
205  rc = -EEFI ( efirc );
206  DBGC ( systab, "EFI could not get loaded image protocol: %s",
207  strerror ( rc ) );
208  goto err_no_loaded_image;
209  }
210  efi_loaded_image = loaded_image;
211  DBGC ( systab, "EFI image base address %p\n",
213 
214  /* EFI is perfectly capable of gracefully shutting down any
215  * loaded devices if it decides to fall back to a legacy boot.
216  * For no particularly comprehensible reason, it doesn't
217  * bother doing so when ExitBootServices() is called.
218  */
219  if ( ( efirc = bs->CreateEvent ( EVT_SIGNAL_EXIT_BOOT_SERVICES,
221  NULL, &efi_shutdown_event ) ) != 0 ) {
222  rc = -EEFI ( efirc );
223  DBGC ( systab, "EFI could not create ExitBootServices event: "
224  "%s\n", strerror ( rc ) );
225  goto err_create_event;
226  }
227 
228  /* Install driver binding protocol */
229  if ( ( rc = efi_driver_install() ) != 0 ) {
230  DBGC ( systab, "EFI could not install driver: %s\n",
231  strerror ( rc ) );
232  efirc = EFIRC ( rc );
233  goto err_driver_install;
234  }
235 
236  /* Install image unload method */
238 
239  return 0;
240 
242  err_driver_install:
244  err_create_event:
245  err_no_loaded_image:
246  err_missing_table:
247  err_missing_protocol:
248  err_sanity:
249  return efirc;
250 }
void efi_driver_uninstall(void)
Uninstall EFI driver.
Definition: efi_driver.c:419
EFI_BOOT_SERVICES * BootServices
A pointer to the EFI Boot Services Table.
Definition: UefiSpec.h:2000
void ** protocol
Variable containing pointer to protocol structure.
Definition: efi.h:76
EFI_LOADED_IMAGE_PROTOCOL * efi_loaded_image
Loaded image protocol for this image.
Definition: efi_init.c:33
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:159
EFI_LOCATE_PROTOCOL LocateProtocol
Definition: UefiSpec.h:1914
EFI_GUID guid
GUID.
Definition: efi.h:74
#define DBGC(...)
Definition: compiler.h:505
EFI_GUID efi_loaded_image_protocol_guid
Loaded image protocol GUID.
Definition: efi_guid.c:184
EFI_CLOSE_EVENT CloseEvent
Definition: UefiSpec.h:1864
VOID * ImageBase
The base address at which the image was loaded.
Definition: LoadedImage.h:75
#define EFI_PROTOCOLS
EFI protocol table.
Definition: efi.h:82
#define EVT_SIGNAL_EXIT_BOOT_SERVICES
Definition: UefiSpec.h:396
#define EFI_CONFIG_TABLES
EFI configuration table table.
Definition: efi.h:126
An EFI configuration table used by iPXE.
Definition: efi.h:116
#define EFI_NOT_AVAILABLE_YET
If this value is returned by an API, it means the capability is not yet installed/available/ready to ...
Definition: PiMultiPhase.h:55
EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL * ConOut
A pointer to the EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL interface that is associated with ConsoleOutHandle.
Definition: UefiSpec.h:1982
static EFI_EVENT efi_shutdown_event
Event used to signal shutdown.
Definition: efi_init.c:48
static EFIAPI void efi_shutdown_hook(EFI_EVENT event __unused, void *context __unused)
Shut down in preparation for booting an OS.
Definition: efi_init.c:70
EFI_CREATE_EVENT CreateEvent
Definition: UefiSpec.h:1860
static void * efi_find_table(EFI_GUID *guid)
Look up EFI configuration table.
Definition: efi_init.c:88
#define EFI_OPEN_PROTOCOL_GET_PROTOCOL
Definition: UefiSpec.h:1271
An EFI protocol used by iPXE.
Definition: efi.h:72
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:78
EFI Boot Services Table.
Definition: UefiSpec.h:1836
EFI_HANDLE efi_image_handle
Image handle passed to entry point.
Definition: efi_init.c:30
EFI_IMAGE_UNLOAD Unload
Definition: LoadedImage.h:79
#define for_each_table_entry(pointer, table)
Iterate through all entries within a linker table.
Definition: tables.h:358
#define TPL_CALLBACK
Definition: UefiSpec.h:591
void ** table
Variable containing pointer to configuration table.
Definition: efi.h:120
int required
Protocol is required.
Definition: efi.h:78
const char * efi_guid_ntoa(EFI_GUID *guid)
Convert GUID to a printable string.
Definition: efi_debug.c:192
EFI_RUNTIME_SERVICES * RuntimeServices
A pointer to the EFI Runtime Services Table.
Definition: UefiSpec.h:1996
EFI_GUID guid
GUID.
Definition: efi.h:118
RETURN_STATUS EFI_STATUS
Function return status for EFI API.
Definition: UefiBaseType.h:35
static EFI_EXIT efi_exit
Exit function.
Definition: efi_init.c:58
EFI_SYSTEM_TABLE * efi_systab
EFI_OPEN_PROTOCOL OpenProtocol
Definition: UefiSpec.h:1905
int required
Table is required for operation.
Definition: efi.h:122
static EFI_STATUS EFIAPI efi_unload(EFI_HANDLE image_handle)
Shut down EFI environment.
Definition: efi_init.c:257
#define NULL
NULL pointer (VOID *)
Definition: Base.h:362
int efi_driver_install(void)
Install EFI driver.
Definition: efi_driver.c:386
#define EFIRC(rc)
Convert an iPXE status code to an EFI status code.
Definition: efi.h:151

References EFI_SYSTEM_TABLE::BootServices, EFI_BOOT_SERVICES::CloseEvent, EFI_SYSTEM_TABLE::ConOut, EFI_BOOT_SERVICES::CreateEvent, DBGC, EEFI, EFI_CONFIG_TABLES, efi_driver_install(), efi_driver_uninstall(), efi_exit, efi_find_table(), efi_guid_ntoa(), efi_image_handle, efi_loaded_image, efi_loaded_image_protocol_guid, EFI_NOT_AVAILABLE_YET, EFI_OPEN_PROTOCOL_GET_PROTOCOL, EFI_PROTOCOLS, efi_shutdown_event, efi_shutdown_hook(), efi_systab, efi_unload(), EFIRC, EVT_SIGNAL_EXIT_BOOT_SERVICES, EFI_BOOT_SERVICES::Exit, for_each_table_entry, efi_protocol::guid, efi_config_table::guid, EFI_LOADED_IMAGE_PROTOCOL::ImageBase, EFI_BOOT_SERVICES::LocateProtocol, NULL, EFI_BOOT_SERVICES::OpenProtocol, efi_protocol::protocol, rc, efi_protocol::required, efi_config_table::required, EFI_SYSTEM_TABLE::RuntimeServices, strerror(), efi_config_table::table, TPL_CALLBACK, and EFI_LOADED_IMAGE_PROTOCOL::Unload.

Referenced by _efi_start(), and _efidrv_start().

◆ __stack_chk_fail()

void __stack_chk_fail ( void  )

Abort on stack check failure.

Definition at line 284 of file efi_init.c.

284  {
285  EFI_STATUS efirc;
286  int rc;
287 
288  /* Report failure (when debugging) */
289  DBGC ( efi_systab, "EFI stack check failed (cookie %#lx); aborting\n",
291 
292  /* Attempt to exit cleanly with an error status */
293  if ( efi_exit ) {
295  0, NULL );
296  rc = -EEFI ( efirc );
297  DBGC ( efi_systab, "EFI stack check exit failed: %s\n",
298  strerror ( rc ) );
299  }
300 
301  /* If the exit fails for any reason, lock the system */
302  while ( 1 ) {}
303 
304 }
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:159
#define DBGC(...)
Definition: compiler.h:505
#define EFI_COMPROMISED_DATA
Enumeration of EFI_STATUS.
Definition: UefiBaseType.h:151
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:78
EFI_HANDLE efi_image_handle
Image handle passed to entry point.
Definition: efi_init.c:30
unsigned long __stack_chk_guard
Stack cookie.
Definition: efi_init.c:51
RETURN_STATUS EFI_STATUS
Function return status for EFI API.
Definition: UefiBaseType.h:35
static EFI_EXIT efi_exit
Exit function.
Definition: efi_init.c:58
EFI_SYSTEM_TABLE * efi_systab
#define NULL
NULL pointer (VOID *)
Definition: Base.h:362

References __stack_chk_guard, DBGC, EEFI, EFI_COMPROMISED_DATA, efi_exit, efi_image_handle, efi_systab, NULL, rc, and strerror().

Variable Documentation

◆ efi_image_handle

EFI_HANDLE efi_image_handle

◆ efi_loaded_image

EFI_LOADED_IMAGE_PROTOCOL* efi_loaded_image

Loaded image protocol for this image.

Definition at line 33 of file efi_init.c.

Referenced by _efi_start(), chained_locate(), efi_init(), efi_local_open_path(), efi_local_open_volume(), and efi_set_autoboot().

◆ efi_shutdown_in_progress

int efi_shutdown_in_progress

EFI shutdown is in progress.

Definition at line 45 of file efi_init.c.

Referenced by efi_currticks(), efi_shutdown_hook(), usbio_endpoint_enqueue(), and usbio_endpoint_poll().

◆ efi_shutdown_event

EFI_EVENT efi_shutdown_event
static

Event used to signal shutdown.

Definition at line 48 of file efi_init.c.

Referenced by efi_init(), and efi_unload().

◆ __stack_chk_guard

unsigned long __stack_chk_guard

Stack cookie.

Definition at line 51 of file efi_init.c.

Referenced by __stack_chk_fail(), and efi_init_stack_guard().

◆ efi_exit

EFI_EXIT efi_exit
static

Exit function.

Cached to minimise external dependencies when a stack check failure is triggered.

Definition at line 58 of file efi_init.c.

Referenced by __stack_chk_fail(), and efi_init().