iPXE
efi_init.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2008 Michael Brown <mbrown@fensystems.co.uk>.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of the
7  * License, or any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  * 02110-1301, USA.
18  */
19 
20 FILE_LICENCE ( GPL2_OR_LATER );
21 
22 #include <string.h>
23 #include <errno.h>
24 #include <endian.h>
25 #include <ipxe/init.h>
26 #include <ipxe/rotate.h>
27 #include <ipxe/efi/efi.h>
28 #include <ipxe/efi/efi_table.h>
29 #include <ipxe/efi/efi_driver.h>
30 #include <ipxe/efi/efi_path.h>
31 #include <ipxe/efi/efi_cmdline.h>
33 
34 /** Image handle passed to entry point */
36 
37 /** Loaded image protocol for this image */
39 
40 /** Device path for the loaded image's device handle */
42 
43 /** System table passed to entry point
44  *
45  * We construct the symbol name efi_systab via the PLATFORM macro.
46  * This ensures that the symbol is defined only in EFI builds, and so
47  * prevents EFI code from being incorrectly linked in to a non-EFI
48  * build.
49  */
50 EFI_SYSTEM_TABLE * _C2 ( PLATFORM, _systab );
51 
52 /** Internal task priority level */
54 
55 /** External task priority level */
57 
58 /** EFI shutdown is in progress */
60 
61 /** Event used to signal shutdown */
63 
64 /** Stack cookie */
65 unsigned long __stack_chk_guard;
66 
67 /** Exit function
68  *
69  * Cached to minimise external dependencies when a stack check
70  * failure is triggered.
71  */
73 
74 /* Forward declarations */
75 static EFI_STATUS EFIAPI efi_unload ( EFI_HANDLE image_handle );
76 
77 /**
78  * Shut down in preparation for booting an OS.
79  *
80  * This hook gets called at ExitBootServices time in order to make
81  * sure that everything is properly shut down before the OS takes
82  * over.
83  */
85  void *context __unused ) {
86 
87  /* This callback is invoked at TPL_NOTIFY in order to ensure
88  * that we have an opportunity to shut down cleanly before
89  * other shutdown hooks perform destructive operations such as
90  * disabling the IOMMU.
91  *
92  * Modify the internal task priority level so that no code
93  * attempts to raise from TPL_NOTIFY to TPL_CALLBACK (which
94  * would trigger a fatal exception).
95  */
97 
98  /* Mark shutdown as being in progress, to indicate that large
99  * parts of the system (e.g. timers) are no longer functional.
100  */
102 
103  /* Shut down iPXE */
104  shutdown_boot();
105 }
106 
107 /**
108  * Construct a stack cookie value
109  *
110  * @v handle Image handle
111  * @ret cookie Stack cookie
112  */
113 __attribute__ (( noinline )) unsigned long
115  unsigned long cookie = 0;
116  unsigned int rotation = ( 8 * sizeof ( cookie ) / 4 );
117 
118  /* There is no viable source of entropy available at this
119  * point. Construct a value that is at least likely to vary
120  * between platforms and invocations.
121  */
122  cookie ^= ( ( unsigned long ) handle );
123  cookie = roll ( cookie, rotation );
124  cookie ^= ( ( unsigned long ) &handle );
125  cookie = roll ( cookie, rotation );
126  cookie ^= profile_timestamp();
127  cookie = roll ( cookie, rotation );
128  cookie ^= build_id;
129 
130  /* Ensure that the value contains a NUL byte, to act as a
131  * runaway string terminator. Construct the NUL using a shift
132  * rather than a mask, to avoid losing valuable entropy in the
133  * lower-order bits.
134  */
135  cookie <<= 8;
136 
137  /* Ensure that the NUL byte is placed at the bottom of the
138  * stack cookie, to avoid potential disclosure via an
139  * unterminated string.
140  */
141  if ( __BYTE_ORDER == __BIG_ENDIAN )
142  cookie >>= 8;
143 
144  return cookie;
145 }
146 
147 /**
148  * Initialise EFI environment
149  *
150  * @v image_handle Image handle
151  * @v systab System table
152  * @ret efirc EFI return status code
153  */
155  EFI_SYSTEM_TABLE *systab ) {
156  EFI_BOOT_SERVICES *bs;
157  struct efi_protocol *prot;
158  struct efi_config_table *tab;
160  void *device_path_copy;
161  size_t device_path_len;
162  EFI_STATUS efirc;
163  int rc;
164 
165  /* Store image handle and system table pointer for future use */
166  efi_image_handle = image_handle;
167  efi_systab = systab;
168 
169  /* Sanity checks */
170  if ( ! systab ) {
171  efirc = EFI_NOT_AVAILABLE_YET;
172  goto err_sanity;
173  }
174  if ( ! systab->ConOut ) {
175  efirc = EFI_NOT_AVAILABLE_YET;
176  goto err_sanity;
177  }
178  if ( ! systab->BootServices ) {
179  DBGC ( systab, "EFI provided no BootServices entry point\n" );
180  efirc = EFI_NOT_AVAILABLE_YET;
181  goto err_sanity;
182  }
183  if ( ! systab->RuntimeServices ) {
184  DBGC ( systab, "EFI provided no RuntimeServices entry "
185  "point\n" );
186  efirc = EFI_NOT_AVAILABLE_YET;
187  goto err_sanity;
188  }
189  DBGC ( systab, "EFI handle %p systab %p\n", image_handle, systab );
190  bs = systab->BootServices;
191 
192  /* Store abort function pointer */
193  efi_exit = bs->Exit;
194 
195  /* Look up used protocols */
197  if ( ( efirc = bs->LocateProtocol ( &prot->guid, NULL,
198  prot->protocol ) ) == 0 ) {
199  DBGC ( systab, "EFI protocol %s is at %p\n",
200  efi_guid_ntoa ( &prot->guid ),
201  *(prot->protocol) );
202  } else {
203  DBGC ( systab, "EFI does not provide protocol %s\n",
204  efi_guid_ntoa ( &prot->guid ) );
205  /* Fail if protocol is required */
206  if ( prot->required )
207  goto err_missing_protocol;
208  }
209  }
210 
211  /* Look up used configuration tables */
213  if ( ( *(tab->table) = efi_find_table ( &tab->guid ) ) ) {
214  DBGC ( systab, "EFI configuration table %s is at %p\n",
215  efi_guid_ntoa ( &tab->guid ), *(tab->table) );
216  } else {
217  DBGC ( systab, "EFI does not provide configuration "
218  "table %s\n", efi_guid_ntoa ( &tab->guid ) );
219  if ( tab->required ) {
220  efirc = EFI_NOT_AVAILABLE_YET;
221  goto err_missing_table;
222  }
223  }
224  }
225 
226  /* Get loaded image protocol
227  *
228  * We assume that our loaded image protocol will not be
229  * uninstalled while our image code is still running.
230  */
231  if ( ( rc = efi_open_unsafe ( image_handle,
233  &efi_loaded_image ) ) != 0 ) {
234  DBGC ( systab, "EFI could not get loaded image protocol: %s",
235  strerror ( rc ) );
236  efirc = EFIRC ( rc );
237  goto err_no_loaded_image;
238  }
239  DBGC ( systab, "EFI image base address %p\n",
241 
242  /* Record command line */
245 
246  /* Get loaded image's device handle's device path */
249  &device_path ) ) != 0 ) {
250  DBGC ( systab, "EFI could not get loaded image's device path: "
251  "%s", strerror ( rc ) );
252  efirc = EFIRC ( rc );
253  goto err_no_device_path;
254  }
255 
256  /* Make a copy of the loaded image's device handle's device
257  * path, since the device handle itself may become invalidated
258  * when we load our own drivers.
259  */
260  device_path_len = ( efi_path_len ( device_path ) +
261  sizeof ( EFI_DEVICE_PATH_PROTOCOL ) );
262  if ( ( efirc = bs->AllocatePool ( EfiBootServicesData, device_path_len,
263  &device_path_copy ) ) != 0 ) {
264  rc = -EEFI ( efirc );
265  goto err_alloc_device_path;
266  }
267  memcpy ( device_path_copy, device_path, device_path_len );
268  efi_loaded_image_path = device_path_copy;
269  DBGC ( systab, "EFI image device path %s\n",
271 
272  /* EFI is perfectly capable of gracefully shutting down any
273  * loaded devices if it decides to fall back to a legacy boot.
274  * For no particularly comprehensible reason, it doesn't
275  * bother doing so when ExitBootServices() is called.
276  */
277  if ( ( efirc = bs->CreateEvent ( EVT_SIGNAL_EXIT_BOOT_SERVICES,
279  NULL, &efi_shutdown_event ) ) != 0 ) {
280  rc = -EEFI ( efirc );
281  DBGC ( systab, "EFI could not create ExitBootServices event: "
282  "%s\n", strerror ( rc ) );
283  goto err_create_event;
284  }
285 
286  /* Install driver binding protocol */
287  if ( ( rc = efi_driver_install() ) != 0 ) {
288  DBGC ( systab, "EFI could not install driver: %s\n",
289  strerror ( rc ) );
290  efirc = EFIRC ( rc );
291  goto err_driver_install;
292  }
293 
294  /* Install image unload method */
296 
297  return 0;
298 
300  err_driver_install:
302  err_create_event:
304  err_alloc_device_path:
305  err_no_device_path:
306  err_no_loaded_image:
307  err_missing_table:
308  err_missing_protocol:
309  err_sanity:
310  return efirc;
311 }
312 
313 /**
314  * Shut down EFI environment
315  *
316  * @v image_handle Image handle
317  */
318 static EFI_STATUS EFIAPI efi_unload ( EFI_HANDLE image_handle __unused ) {
320  EFI_SYSTEM_TABLE *systab = efi_systab;
321  struct efi_saved_tpl tpl;
322 
323  DBGC ( systab, "EFI image unloading\n" );
324 
325  /* Raise TPL */
326  efi_raise_tpl ( &tpl );
327 
328  /* Shut down */
329  shutdown_exit();
330 
331  /* Disconnect any remaining devices */
333 
334  /* Uninstall driver binding protocol */
336 
337  /* Uninstall exit boot services event */
339 
340  /* Free copy of loaded image's device handle's device path */
342 
343  DBGC ( systab, "EFI image unloaded\n" );
344 
345  /* Restore TPL */
346  efi_restore_tpl ( &tpl );
347 
348  return 0;
349 }
350 
351 /**
352  * Abort on stack check failure
353  *
354  */
355 __attribute__ (( noreturn )) void __stack_chk_fail ( void ) {
356  EFI_STATUS efirc;
357  int rc;
358 
359  /* Report failure (when debugging) */
360  DBGC ( efi_systab, "EFI stack check failed (cookie %#lx); aborting\n",
362 
363  /* Attempt to exit cleanly with an error status */
364  if ( efi_exit ) {
366  0, NULL );
367  rc = -EEFI ( efirc );
368  DBGC ( efi_systab, "EFI stack check exit failed: %s\n",
369  strerror ( rc ) );
370  }
371 
372  /* If the exit fails for any reason, lock the system */
373  while ( 1 ) {}
374 
375 }
376 
377 /**
378  * Raise task priority level to internal level
379  *
380  * @v tpl Saved TPL
381  */
382 void efi_raise_tpl ( struct efi_saved_tpl *tpl ) {
384 
385  /* Record current external TPL */
386  tpl->previous = efi_external_tpl;
387 
388  /* Raise TPL and record previous TPL as new external TPL */
389  tpl->current = bs->RaiseTPL ( efi_internal_tpl );
390  efi_external_tpl = tpl->current;
391 }
392 
393 /**
394  * Restore task priority level
395  *
396  * @v tpl Saved TPL
397  */
398 void efi_restore_tpl ( struct efi_saved_tpl *tpl ) {
400 
401  /* Restore external TPL */
402  efi_external_tpl = tpl->previous;
403 
404  /* Restore TPL */
405  bs->RestoreTPL ( tpl->current );
406 }
void efi_driver_uninstall(void)
Uninstall EFI driver.
Definition: efi_driver.c:420
union edd_device_path device_path
Device path.
Definition: edd.h:24
#define __attribute__(x)
Definition: compiler.h:10
EFI_BOOT_SERVICES * BootServices
A pointer to the EFI Boot Services Table.
Definition: UefiSpec.h:2098
void ** protocol
Variable containing pointer to protocol structure.
Definition: efi.h:91
EFI_LOADED_IMAGE_PROTOCOL * efi_loaded_image
Loaded image protocol for this image.
Definition: efi_init.c:38
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:174
EFI_RAISE_TPL RaiseTPL
Definition: UefiSpec.h:1939
EFI driver interface.
const wchar_t * efi_cmdline
EFI command line (may not be wNUL-terminated.
Definition: efi_cmdline.c:45
EFI_LOCATE_PROTOCOL LocateProtocol
Definition: UefiSpec.h:2008
Error codes.
Definition: efi.h:62
void efi_raise_tpl(struct efi_saved_tpl *tpl)
Raise task priority level to internal level.
Definition: efi_init.c:382
#define TPL_APPLICATION
Definition: UefiSpec.h:647
#define efi_open_unsafe(handle, protocol, interface)
Open protocol for unsafe persistent use.
Definition: efi.h:458
EFI_GUID guid
GUID.
Definition: efi.h:89
#define __BYTE_ORDER
Definition: endian.h:6
#define DBGC(...)
Definition: compiler.h:505
EFI_GUID efi_loaded_image_protocol_guid
Loaded image protocol GUID.
Definition: efi_guid.c:272
size_t efi_path_len(EFI_DEVICE_PATH_PROTOCOL *path)
Find length of device path (excluding terminator)
Definition: efi_path.c:173
EFI_CLOSE_EVENT CloseEvent
Definition: UefiSpec.h:1958
VOID * ImageBase
The base address at which the image was loaded.
Definition: LoadedImage.h:69
This protocol can be used on any device handle to obtain generic path/location information concerning...
Definition: DevicePath.h:45
EFI_TPL efi_internal_tpl
Internal task priority level.
Definition: efi_init.c:53
#define TPL_NOTIFY
Definition: UefiSpec.h:649
#define EFI_PROTOCOLS
EFI protocol table.
Definition: efi.h:97
FILE_LICENCE(GPL2_OR_LATER)
#define EVT_SIGNAL_EXIT_BOOT_SERVICES
Definition: UefiSpec.h:455
UEFI 2.0 Loaded image protocol definition.
#define EFI_CONFIG_TABLES
EFI configuration table table.
Definition: efi.h:141
An EFI configuration table used by iPXE.
Definition: efi.h:131
EFI_STATUS(EFIAPI * EFI_EXIT)(IN EFI_HANDLE ImageHandle, IN EFI_STATUS ExitStatus, IN UINTN ExitDataSize, IN CHAR16 *ExitData OPTIONAL)
Terminates a loaded EFI image and returns control to boot services.
Definition: UefiSpec.h:1005
EFI_DEVICE_PATH_PROTOCOL * efi_loaded_image_path
Device path for the loaded image's device handle.
Definition: efi_init.c:41
void * memcpy(void *dest, const void *src, size_t len) __nonnull
EFI configuration tables.
#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:56
Can be used on any image handle to obtain information about the loaded image.
Definition: LoadedImage.h:45
EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL * ConOut
A pointer to the EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL interface that is associated with ConsoleOutHandle.
Definition: UefiSpec.h:2079
static EFI_EVENT efi_shutdown_event
Event used to signal shutdown.
Definition: efi_init.c:62
unsigned long build_id
Build ID.
Definition: version.c:61
EFI_TPL current
Current external TPL.
Definition: efi.h:81
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:84
#define EFI_COMPROMISED_DATA
Enumeration of EFI_STATUS.
Definition: UefiBaseType.h:145
#define __unused
Declare a variable or data structure as unused.
Definition: compiler.h:573
EFI_CREATE_EVENT CreateEvent
Definition: UefiSpec.h:1954
static void shutdown_exit(void)
Shut down system for exit back to firmware.
Definition: init.h:85
const char * efi_devpath_text(EFI_DEVICE_PATH_PROTOCOL *path)
Get textual representation of device path.
Definition: efi_debug.c:247
VOID * LoadOptions
A pointer to the image's binary load options.
Definition: LoadedImage.h:64
#define __BIG_ENDIAN
Constant representing big-endian byte order.
Definition: endian.h:21
An EFI protocol used by iPXE.
Definition: efi.h:87
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:78
#define EFIAPI
EFI Boot Services Table.
Definition: UefiSpec.h:1930
EFI_HANDLE efi_image_handle
Image handle passed to entry point.
Definition: efi_init.c:35
EFI_IMAGE_UNLOAD Unload
Definition: LoadedImage.h:73
void efi_driver_disconnect_all(void)
Disconnect EFI driver from all possible devices.
Definition: efi_driver.c:647
EFI_TPL previous
Previous external TPL.
Definition: efi.h:83
#define for_each_table_entry(pointer, table)
Iterate through all entries within a linker table.
Definition: tables.h:385
#define TPL_CALLBACK
Definition: UefiSpec.h:648
#define efi_open(handle, protocol, interface)
Open protocol for ephemeral use.
Definition: efi.h:443
EFI device paths.
EFI System Table.
Definition: UefiSpec.h:2043
unsigned long __stack_chk_guard
Stack cookie.
Definition: efi_init.c:65
static unsigned int rotation
Definition: rotate.h:22
EFI_GUID efi_device_path_protocol_guid
Device path protocol GUID.
Definition: efi_guid.c:168
const char * efi_guid_ntoa(CONST EFI_GUID *guid)
Convert GUID to a printable string.
Definition: efi_guid.c:725
EFI_FREE_POOL FreePool
Definition: UefiSpec.h:1949
EFI API.
void ** table
Variable containing pointer to configuration table.
Definition: efi.h:135
UINT32 LoadOptionsSize
The size in bytes of LoadOptions.
Definition: LoadedImage.h:63
int required
Protocol is required.
Definition: efi.h:93
EFI_RUNTIME_SERVICES * RuntimeServices
A pointer to the EFI Runtime Services Table.
Definition: UefiSpec.h:2094
UINTN EFI_TPL
Task priority level.
Definition: UefiBaseType.h:43
An EFI saved task priority level.
Definition: efi.h:79
EFI_GUID guid
GUID.
Definition: efi.h:133
RETURN_STATUS EFI_STATUS
Function return status for EFI API.
Definition: UefiBaseType.h:31
EFI command line.
unsigned long profile_timestamp(void)
EFI_STATUS efi_init(EFI_HANDLE image_handle, EFI_SYSTEM_TABLE *systab)
Initialise EFI environment.
Definition: efi_init.c:154
static EFI_EXIT efi_exit
Exit function.
Definition: efi_init.c:72
EFI_SYSTEM_TABLE * efi_systab
EFI_SYSTEM_TABLE * _C2(PLATFORM, _systab)
System table passed to entry point.
size_t efi_cmdline_len
Length of EFI command line (in bytes)
Definition: efi_cmdline.c:48
EFI_RESTORE_TPL RestoreTPL
Definition: UefiSpec.h:1940
The data portions of a loaded Boot Serves Driver, and the default data allocation type used by a Boot...
unsigned long efi_stack_cookie(EFI_HANDLE handle)
Construct a stack cookie value.
Definition: efi_init.c:114
int required
Table is required for operation.
Definition: efi.h:137
static EFI_STATUS EFIAPI efi_unload(EFI_HANDLE image_handle)
Shut down EFI environment.
Definition: efi_init.c:318
void __stack_chk_fail(void)
Abort on stack check failure.
Definition: efi_init.c:355
static void shutdown_boot(void)
Shut down system for OS boot.
Definition: init.h:77
uint16_t handle
Handle.
Definition: smbios.h:16
void efi_restore_tpl(struct efi_saved_tpl *tpl)
Restore task priority level.
Definition: efi_init.c:398
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321
String functions.
void * efi_find_table(EFI_GUID *guid)
Look up EFI configuration table.
Definition: efi_table.c:44
int efi_driver_install(void)
Install EFI driver.
Definition: efi_driver.c:387
#define EFIRC(rc)
Convert an iPXE status code to an EFI status code.
Definition: efi.h:166
Definition: efi.h:61
EFI_HANDLE DeviceHandle
The device handle that the EFI Image was loaded from.
Definition: LoadedImage.h:55
EFI_ALLOCATE_POOL AllocatePool
Definition: UefiSpec.h:1948
Bit operations.
int efi_shutdown_in_progress
EFI shutdown is in progress.
Definition: efi_init.c:59
EFI_TPL efi_external_tpl
External task priority level.
Definition: efi_init.c:56