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...
 
EFI_STATUS efi_init (EFI_HANDLE image_handle, EFI_SYSTEM_TABLE *systab)
 Initialise EFI environment. 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...
 

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 221 of file efi_init.c.

221  {
223  EFI_SYSTEM_TABLE *systab = efi_systab;
224 
225  DBGC ( systab, "EFI image unloading\n" );
226 
227  /* Shut down */
228  shutdown_exit();
229 
230  /* Disconnect any remaining devices */
232 
233  /* Uninstall driver binding protocol */
235 
236  /* Uninstall exit boot services event */
238 
239  DBGC ( systab, "EFI image unloaded\n" );
240 
241  return 0;
242 }
void efi_driver_uninstall(void)
Uninstall EFI driver.
Definition: efi_driver.c:405
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:562
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 60 of file efi_init.c.

61  {
62 
63  /* Mark shutdown as being in progress, to indicate that large
64  * parts of the system (e.g. timers) are no longer functional.
65  */
67 
68  /* Shut down iPXE */
69  shutdown_boot();
70 }
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 78 of file efi_init.c.

78  {
79  unsigned int i;
80 
81  for ( i = 0 ; i < efi_systab->NumberOfTableEntries ; i++ ) {
83  guid, sizeof ( *guid ) ) == 0 )
85  }
86 
87  return NULL;
88 }
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_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 97 of file efi_init.c.

98  {
100  struct efi_protocol *prot;
101  struct efi_config_table *tab;
102  void *loaded_image;
103  EFI_STATUS efirc;
104  int rc;
105 
106  /* Store image handle and system table pointer for future use */
107  efi_image_handle = image_handle;
108  efi_systab = systab;
109 
110  /* Sanity checks */
111  if ( ! systab ) {
112  efirc = EFI_NOT_AVAILABLE_YET;
113  goto err_sanity;
114  }
115  if ( ! systab->ConOut ) {
116  efirc = EFI_NOT_AVAILABLE_YET;
117  goto err_sanity;
118  }
119  if ( ! systab->BootServices ) {
120  DBGC ( systab, "EFI provided no BootServices entry point\n" );
121  efirc = EFI_NOT_AVAILABLE_YET;
122  goto err_sanity;
123  }
124  if ( ! systab->RuntimeServices ) {
125  DBGC ( systab, "EFI provided no RuntimeServices entry "
126  "point\n" );
127  efirc = EFI_NOT_AVAILABLE_YET;
128  goto err_sanity;
129  }
130  DBGC ( systab, "EFI handle %p systab %p\n", image_handle, systab );
131  bs = systab->BootServices;
132 
133  /* Look up used protocols */
135  if ( ( efirc = bs->LocateProtocol ( &prot->guid, NULL,
136  prot->protocol ) ) == 0 ) {
137  DBGC ( systab, "EFI protocol %s is at %p\n",
138  efi_guid_ntoa ( &prot->guid ),
139  *(prot->protocol) );
140  } else {
141  DBGC ( systab, "EFI does not provide protocol %s\n",
142  efi_guid_ntoa ( &prot->guid ) );
143  /* Fail if protocol is required */
144  if ( prot->required )
145  goto err_missing_protocol;
146  }
147  }
148 
149  /* Look up used configuration tables */
151  if ( ( *(tab->table) = efi_find_table ( &tab->guid ) ) ) {
152  DBGC ( systab, "EFI configuration table %s is at %p\n",
153  efi_guid_ntoa ( &tab->guid ), *(tab->table) );
154  } else {
155  DBGC ( systab, "EFI does not provide configuration "
156  "table %s\n", efi_guid_ntoa ( &tab->guid ) );
157  if ( tab->required ) {
158  efirc = EFI_NOT_AVAILABLE_YET;
159  goto err_missing_table;
160  }
161  }
162  }
163 
164  /* Get loaded image protocol */
165  if ( ( efirc = bs->OpenProtocol ( image_handle,
167  &loaded_image, image_handle, NULL,
168  EFI_OPEN_PROTOCOL_GET_PROTOCOL ) ) != 0 ) {
169  rc = -EEFI ( efirc );
170  DBGC ( systab, "EFI could not get loaded image protocol: %s",
171  strerror ( rc ) );
172  goto err_no_loaded_image;
173  }
174  efi_loaded_image = loaded_image;
175  DBGC ( systab, "EFI image base address %p\n",
177 
178  /* EFI is perfectly capable of gracefully shutting down any
179  * loaded devices if it decides to fall back to a legacy boot.
180  * For no particularly comprehensible reason, it doesn't
181  * bother doing so when ExitBootServices() is called.
182  */
183  if ( ( efirc = bs->CreateEvent ( EVT_SIGNAL_EXIT_BOOT_SERVICES,
185  NULL, &efi_shutdown_event ) ) != 0 ) {
186  rc = -EEFI ( efirc );
187  DBGC ( systab, "EFI could not create ExitBootServices event: "
188  "%s\n", strerror ( rc ) );
189  goto err_create_event;
190  }
191 
192  /* Install driver binding protocol */
193  if ( ( rc = efi_driver_install() ) != 0 ) {
194  DBGC ( systab, "EFI could not install driver: %s\n",
195  strerror ( rc ) );
196  efirc = EFIRC ( rc );
197  goto err_driver_install;
198  }
199 
200  /* Install image unload method */
202 
203  return 0;
204 
206  err_driver_install:
208  err_create_event:
209  err_no_loaded_image:
210  err_missing_table:
211  err_missing_protocol:
212  err_sanity:
213  return efirc;
214 }
void efi_driver_uninstall(void)
Uninstall EFI driver.
Definition: efi_driver.c:405
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:74
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:157
EFI_LOCATE_PROTOCOL LocateProtocol
Definition: UefiSpec.h:1914
EFI_GUID guid
GUID.
Definition: efi.h:72
#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:80
#define EVT_SIGNAL_EXIT_BOOT_SERVICES
Definition: UefiSpec.h:396
#define EFI_CONFIG_TABLES
EFI configuration table table.
Definition: efi.h:124
An EFI configuration table used by iPXE.
Definition: efi.h:114
#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:60
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:78
#define EFI_OPEN_PROTOCOL_GET_PROTOCOL
Definition: UefiSpec.h:1271
An EFI protocol used by iPXE.
Definition: efi.h:70
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:118
int required
Protocol is required.
Definition: efi.h:76
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:116
RETURN_STATUS EFI_STATUS
Function return status for EFI API.
Definition: UefiBaseType.h:35
EFI_SYSTEM_TABLE * efi_systab
EFI_OPEN_PROTOCOL OpenProtocol
Definition: UefiSpec.h:1905
int required
Table is required for operation.
Definition: efi.h:120
static EFI_STATUS EFIAPI efi_unload(EFI_HANDLE image_handle)
Shut down EFI environment.
Definition: efi_init.c:221
#define NULL
NULL pointer (VOID *)
Definition: Base.h:362
int efi_driver_install(void)
Install EFI driver.
Definition: efi_driver.c:372
#define EFIRC(rc)
Convert an iPXE status code to an EFI status code.
Definition: efi.h:149

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_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, 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().

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