iPXE
Functions
efi_utils.c File Reference

EFI utilities. More...

#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <ipxe/efi/efi.h>
#include <ipxe/efi/efi_pci.h>
#include <ipxe/efi/efi_utils.h>

Go to the source code of this file.

Functions

 FILE_LICENCE (GPL2_OR_LATER)
 
EFI_DEVICE_PATH_PROTOCOLefi_devpath_end (EFI_DEVICE_PATH_PROTOCOL *path)
 Find end of device path. More...
 
size_t efi_devpath_len (EFI_DEVICE_PATH_PROTOCOL *path)
 Find length of device path (excluding terminator) More...
 
int efi_locate_device (EFI_HANDLE device, EFI_GUID *protocol, EFI_HANDLE *parent)
 Locate parent device supporting a given protocol. More...
 
int efi_child_add (EFI_HANDLE parent, EFI_HANDLE child)
 Add EFI device as child of another EFI device. More...
 
void efi_child_del (EFI_HANDLE parent, EFI_HANDLE child)
 Remove EFI device as child of another EFI device. More...
 
static int efi_pci_info (EFI_HANDLE device, const char *prefix, struct device *dev)
 Get underlying PCI device information. More...
 
void efi_device_info (EFI_HANDLE device, const char *prefix, struct device *dev)
 Get underlying device information. More...
 

Detailed Description

EFI utilities.

Definition in file efi_utils.c.

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER  )

◆ efi_devpath_end()

EFI_DEVICE_PATH_PROTOCOL* efi_devpath_end ( EFI_DEVICE_PATH_PROTOCOL path)

Find end of device path.

Parameters
pathPath to device
Return values
path_endEnd of device path

Definition at line 41 of file efi_utils.c.

41  {
42 
43  while ( path->Type != END_DEVICE_PATH_TYPE ) {
44  path = ( ( ( void * ) path ) +
45  /* There's this amazing new-fangled thing known as
46  * a UINT16, but who wants to use one of those? */
47  ( ( path->Length[1] << 8 ) | path->Length[0] ) );
48  }
49 
50  return path;
51 }
#define END_DEVICE_PATH_TYPE
Definition: DevicePath.h:1327
UINT8 Length[2]
Specific Device Path data.
Definition: DevicePath.h:64
UINT8 Type
0x01 Hardware Device Path.
Definition: DevicePath.h:52

References END_DEVICE_PATH_TYPE, EFI_DEVICE_PATH_PROTOCOL::Length, and EFI_DEVICE_PATH_PROTOCOL::Type.

Referenced by efi_block_boot_image(), efi_devpath_len(), and usbio_open().

◆ efi_devpath_len()

size_t efi_devpath_len ( EFI_DEVICE_PATH_PROTOCOL path)

Find length of device path (excluding terminator)

Parameters
pathPath to device
Return values
path_lenLength of device path

Definition at line 59 of file efi_utils.c.

59  {
61 
62  return ( ( ( void * ) end ) - ( ( void * ) path ) );
63 }
This protocol can be used on any device handle to obtain generic path/location information concerning...
Definition: DevicePath.h:51
uint32_t end
Ending offset.
Definition: netvsc.h:18
EFI_DEVICE_PATH_PROTOCOL * efi_devpath_end(EFI_DEVICE_PATH_PROTOCOL *path)
Find end of device path.
Definition: efi_utils.c:41

References efi_devpath_end(), and end.

Referenced by efi_block_boot_image(), efi_block_hook(), efi_devpath_text(), efi_driver_start(), efi_image_path(), efi_local_open_path(), efi_snp_hii_install(), efi_snp_probe(), efi_usb_install(), and usbio_path().

◆ efi_locate_device()

int efi_locate_device ( EFI_HANDLE  device,
EFI_GUID protocol,
EFI_HANDLE parent 
)

Locate parent device supporting a given protocol.

Parameters
deviceEFI device handle
protocolProtocol GUID
parentParent EFI device handle to fill in
Return values
rcReturn status code

Definition at line 73 of file efi_utils.c.

74  {
76  union {
78  void *interface;
79  } path;
80  EFI_DEVICE_PATH_PROTOCOL *devpath;
81  EFI_STATUS efirc;
82  int rc;
83 
84  /* Get device path */
85  if ( ( efirc = bs->OpenProtocol ( device,
87  &path.interface,
90  rc = -EEFI ( efirc );
91  DBGC ( device, "EFIDEV %s cannot open device path: %s\n",
92  efi_handle_name ( device ), strerror ( rc ) );
93  goto err_open_device_path;
94  }
95  devpath = path.path;
96 
97  /* Check for presence of specified protocol */
98  if ( ( efirc = bs->LocateDevicePath ( protocol, &devpath,
99  parent ) ) != 0 ) {
100  rc = -EEFI ( efirc );
101  DBGC ( device, "EFIDEV %s has no parent supporting %s: %s\n",
103  efi_guid_ntoa ( protocol ), strerror ( rc ) );
104  goto err_locate_protocol;
105  }
106 
107  /* Success */
108  rc = 0;
109 
110  err_locate_protocol:
113  err_open_device_path:
114  return rc;
115 }
EFI_BOOT_SERVICES * BootServices
A pointer to the EFI Boot Services Table.
Definition: UefiSpec.h:2000
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
This protocol can be used on any device handle to obtain generic path/location information concerning...
Definition: DevicePath.h:51
EFI_CLOSE_PROTOCOL CloseProtocol
Definition: UefiSpec.h:1906
A hardware device.
Definition: device.h:73
An object interface.
Definition: interface.h:109
#define EFI_OPEN_PROTOCOL_GET_PROTOCOL
Definition: UefiSpec.h:1271
const char * efi_handle_name(EFI_HANDLE handle)
Get name of an EFI handle.
Definition: efi_debug.c:713
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_GUID efi_device_path_protocol_guid
Device path protocol GUID.
Definition: efi_guid.c:132
const char * efi_guid_ntoa(EFI_GUID *guid)
Convert GUID to a printable string.
Definition: efi_debug.c:192
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
uint16_t protocol
Protocol ID.
Definition: stp.h:18
EFI_LOCATE_DEVICE_PATH LocateDevicePath
Definition: UefiSpec.h:1877

References EFI_SYSTEM_TABLE::BootServices, EFI_BOOT_SERVICES::CloseProtocol, DBGC, EEFI, efi_device_path_protocol_guid, efi_guid_ntoa(), efi_handle_name(), efi_image_handle, EFI_OPEN_PROTOCOL_GET_PROTOCOL, efi_systab, EFI_BOOT_SERVICES::LocateDevicePath, EFI_BOOT_SERVICES::OpenProtocol, protocol, rc, and strerror().

Referenced by chained_locate(), efi_pci_info(), and nii_pci_open().

◆ efi_child_add()

int efi_child_add ( EFI_HANDLE  parent,
EFI_HANDLE  child 
)

Add EFI device as child of another EFI device.

Parameters
parentEFI parent device handle
childEFI child device handle
Return values
rcReturn status code

Definition at line 124 of file efi_utils.c.

124  {
126  void *devpath;
127  EFI_STATUS efirc;
128  int rc;
129 
130  /* Re-open the device path protocol */
131  if ( ( efirc = bs->OpenProtocol ( parent,
133  &devpath,
134  efi_image_handle, child,
136  ) ) != 0 ) {
137  rc = -EEFI ( efirc );
138  DBGC ( parent, "EFIDEV %s could not add child",
139  efi_handle_name ( parent ) );
140  DBGC ( parent, " %s: %s\n",
141  efi_handle_name ( child ), strerror ( rc ) );
142  DBGC_EFI_OPENERS ( parent, parent,
144  return rc;
145  }
146 
147  DBGC2 ( parent, "EFIDEV %s added child", efi_handle_name ( parent ) );
148  DBGC2 ( parent, " %s\n", efi_handle_name ( child ) );
149  return 0;
150 }
EFI_BOOT_SERVICES * BootServices
A pointer to the EFI Boot Services Table.
Definition: UefiSpec.h:2000
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 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
Definition: UefiSpec.h:1273
#define DBGC(...)
Definition: compiler.h:505
#define DBGC_EFI_OPENERS(...)
Definition: efi.h:262
const char * efi_handle_name(EFI_HANDLE handle)
Get name of an EFI handle.
Definition: efi_debug.c:713
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_GUID efi_device_path_protocol_guid
Device path protocol GUID.
Definition: efi_guid.c:132
#define DBGC2(...)
Definition: compiler.h:522
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

References EFI_SYSTEM_TABLE::BootServices, DBGC, DBGC2, DBGC_EFI_OPENERS, EEFI, efi_device_path_protocol_guid, efi_handle_name(), efi_image_handle, EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER, efi_systab, EFI_BOOT_SERVICES::OpenProtocol, rc, and strerror().

Referenced by efi_snp_hii_install(), and efi_snp_probe().

◆ efi_child_del()

void efi_child_del ( EFI_HANDLE  parent,
EFI_HANDLE  child 
)

Remove EFI device as child of another EFI device.

Parameters
parentEFI parent device handle
childEFI child device handle

Definition at line 158 of file efi_utils.c.

158  {
160 
162  efi_image_handle, child );
163  DBGC2 ( parent, "EFIDEV %s removed child", efi_handle_name ( parent ) );
164  DBGC2 ( parent, " %s\n", efi_handle_name ( child ) );
165 }
EFI_BOOT_SERVICES * BootServices
A pointer to the EFI Boot Services Table.
Definition: UefiSpec.h:2000
EFI_CLOSE_PROTOCOL CloseProtocol
Definition: UefiSpec.h:1906
const char * efi_handle_name(EFI_HANDLE handle)
Get name of an EFI handle.
Definition: efi_debug.c:713
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_GUID efi_device_path_protocol_guid
Device path protocol GUID.
Definition: efi_guid.c:132
#define DBGC2(...)
Definition: compiler.h:522
EFI_SYSTEM_TABLE * efi_systab

References EFI_SYSTEM_TABLE::BootServices, EFI_BOOT_SERVICES::CloseProtocol, DBGC2, efi_device_path_protocol_guid, efi_handle_name(), efi_image_handle, and efi_systab.

Referenced by efi_snp_hii_install(), efi_snp_hii_uninstall(), efi_snp_probe(), and efi_snp_remove().

◆ efi_pci_info()

static int efi_pci_info ( EFI_HANDLE  device,
const char *  prefix,
struct device dev 
)
static

Get underlying PCI device information.

Parameters
deviceEFI device handle
prefixDevice name prefix
devGeneric device to fill in
Return values
rcReturn status code

Definition at line 175 of file efi_utils.c.

176  {
178  struct pci_device pci;
179  int rc;
180 
181  /* Find parent PCI device */
183  &pci_device ) ) != 0 ) {
184  DBGC ( device, "EFIDEV %s is not a PCI device: %s\n",
185  efi_handle_name ( device ), strerror ( rc ) );
186  return rc;
187  }
188 
189  /* Get PCI device information */
190  if ( ( rc = efipci_info ( pci_device, &pci ) ) != 0 ) {
191  DBGC ( device, "EFIDEV %s could not get PCI information: %s\n",
192  efi_handle_name ( device ), strerror ( rc ) );
193  return rc;
194  }
195 
196  /* Populate device information */
197  memcpy ( &dev->desc, &pci.dev.desc, sizeof ( dev->desc ) );
198  snprintf ( dev->name, sizeof ( dev->name ), "%s-%s",
199  prefix, pci.dev.name );
200 
201  return 0;
202 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
EFI_GUID efi_pci_io_protocol_guid
PCI I/O protocol GUID.
Definition: efi_guid.c:216
#define DBGC(...)
Definition: compiler.h:505
char name[40]
Name.
Definition: device.h:75
char prefix[4]
Definition: vmconsole.c:53
struct device dev
Generic device.
Definition: pci.h:189
A hardware device.
Definition: device.h:73
void * memcpy(void *dest, const void *src, size_t len) __nonnull
const char * efi_handle_name(EFI_HANDLE handle)
Get name of an EFI handle.
Definition: efi_debug.c:713
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:78
A PCI device.
Definition: pci.h:187
struct device_description desc
Device description.
Definition: device.h:79
int efi_locate_device(EFI_HANDLE device, EFI_GUID *protocol, EFI_HANDLE *parent)
Locate parent device supporting a given protocol.
Definition: efi_utils.c:73
int snprintf(char *buf, size_t size, const char *fmt,...)
Write a formatted string to a buffer.
Definition: vsprintf.c:382
int efipci_info(EFI_HANDLE device, struct pci_device *pci)
Get EFI PCI device information.
Definition: efi_pci.c:330
Definition: efi.h:55

References DBGC, device::desc, pci_device::dev, efi_handle_name(), efi_locate_device(), efi_pci_io_protocol_guid, efipci_info(), memcpy(), device::name, prefix, rc, snprintf(), and strerror().

Referenced by efi_device_info().

◆ efi_device_info()

void efi_device_info ( EFI_HANDLE  device,
const char *  prefix,
struct device dev 
)

Get underlying device information.

Parameters
deviceEFI device handle
prefixDevice name prefix
devGeneric device to fill in

Definition at line 211 of file efi_utils.c.

212  {
213  int rc;
214 
215  /* Try getting underlying PCI device information */
216  if ( ( rc = efi_pci_info ( device, prefix, dev ) ) == 0 )
217  return;
218 
219  /* If we cannot get any underlying device information, fall
220  * back to providing information about the EFI handle.
221  */
222  DBGC ( device, "EFIDEV %s could not get underlying device "
223  "information\n", efi_handle_name ( device ) );
225  snprintf ( dev->name, sizeof ( dev->name ), "%s-%p", prefix, device );
226 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
#define DBGC(...)
Definition: compiler.h:505
char name[40]
Name.
Definition: device.h:75
char prefix[4]
Definition: vmconsole.c:53
struct device dev
Generic device.
Definition: pci.h:189
static int efi_pci_info(EFI_HANDLE device, const char *prefix, struct device *dev)
Get underlying PCI device information.
Definition: efi_utils.c:175
A hardware device.
Definition: device.h:73
const char * efi_handle_name(EFI_HANDLE handle)
Get name of an EFI handle.
Definition: efi_debug.c:713
#define BUS_TYPE_EFI
EFI bus type.
Definition: device.h:61
unsigned int bus_type
Bus type.
Definition: device.h:24
struct device_description desc
Device description.
Definition: device.h:79
int snprintf(char *buf, size_t size, const char *fmt,...)
Write a formatted string to a buffer.
Definition: vsprintf.c:382

References device_description::bus_type, BUS_TYPE_EFI, DBGC, device::desc, pci_device::dev, efi_handle_name(), efi_pci_info(), device::name, prefix, rc, and snprintf().

Referenced by nii_start(), snpnet_start(), and usbio_start().