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_path.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)
 
int efi_locate_device (EFI_HANDLE device, EFI_GUID *protocol, EFI_HANDLE *parent, unsigned int skip)
 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_device_info_pci (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_locate_device()

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

Locate parent device supporting a given protocol.

Parameters
deviceEFI device handle
protocolProtocol GUID
parentParent EFI device handle to fill in
skipNumber of protocol-supporting parent devices to skip
Return values
rcReturn status code

Definition at line 45 of file efi_utils.c.

46  {
48  EFI_DEVICE_PATH_PROTOCOL *devpath;
51  size_t len;
52  EFI_STATUS efirc;
53  int rc;
54 
55  /* Get device path */
57  &devpath ) ) != 0 ) {
58  DBGC ( device, "EFIDEV %s cannot open device path: %s\n",
59  efi_handle_name ( device ), strerror ( rc ) );
60  goto err_open_device_path;
61  }
62 
63  /* Create modifiable copy of device path */
64  len = ( efi_path_len ( devpath ) + sizeof ( *end ) );
65  path = malloc ( len );
66  if ( ! path ) {
67  rc = -ENOMEM;
68  goto err_alloc_path;
69  }
70  memcpy ( path, devpath, len );
71 
72  /* Locate parent device(s) */
73  while ( 1 ) {
74 
75  /* Check for presence of specified protocol */
76  end = path;
77  if ( ( efirc = bs->LocateDevicePath ( protocol, &end,
78  parent ) ) != 0 ) {
79  rc = -EEFI ( efirc );
80  DBGC ( device, "EFIDEV %s has no parent supporting "
81  "%s: %s\n", efi_devpath_text ( path ),
82  efi_guid_ntoa ( protocol ), strerror ( rc ) );
83  goto err_locate_protocol;
84  }
85 
86  /* Stop if we have skipped the requested number of devices */
87  if ( ! skip-- )
88  break;
89 
90  /* Trim device path */
92  end = efi_path_prev ( path, end );
94  }
95 
96  err_locate_protocol:
97  free ( path );
98  err_alloc_path:
99  err_open_device_path:
100  return rc;
101 }
EFI_BOOT_SERVICES * BootServices
A pointer to the EFI Boot Services Table.
Definition: UefiSpec.h:2098
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
#define DBGC(...)
Definition: compiler.h:505
size_t efi_path_len(EFI_DEVICE_PATH_PROTOCOL *path)
Find length of device path (excluding terminator)
Definition: efi_path.c:173
This protocol can be used on any device handle to obtain generic path/location information concerning...
Definition: DevicePath.h:45
static void efi_path_terminate(EFI_DEVICE_PATH_PROTOCOL *end)
Terminate device path.
Definition: efi_path.h:30
EFI_DEVICE_PATH_PROTOCOL * efi_path_prev(EFI_DEVICE_PATH_PROTOCOL *path, EFI_DEVICE_PATH_PROTOCOL *curr)
Find previous element of device path.
Definition: efi_path.c:144
#define ENOMEM
Not enough space.
Definition: errno.h:534
A hardware device.
Definition: device.h:76
void * memcpy(void *dest, const void *src, size_t len) __nonnull
ring len
Length.
Definition: dwmac.h:231
const char * efi_devpath_text(EFI_DEVICE_PATH_PROTOCOL *path)
Get textual representation of device path.
Definition: efi_debug.c:247
const char * efi_handle_name(EFI_HANDLE handle)
Get name of an EFI handle.
Definition: efi_debug.c:652
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:78
static void(* free)(struct refcnt *refcnt))
Definition: refcnt.h:54
EFI Boot Services Table.
Definition: UefiSpec.h:1930
#define efi_open(handle, protocol, interface)
Open protocol for ephemeral use.
Definition: efi.h:443
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
void * malloc(size_t size)
Allocate memory.
Definition: malloc.c:620
RETURN_STATUS EFI_STATUS
Function return status for EFI API.
Definition: UefiBaseType.h:31
uint32_t end
Ending offset.
Definition: netvsc.h:18
EFI_SYSTEM_TABLE * efi_systab
uint16_t protocol
Protocol ID.
Definition: stp.h:18
EFI_LOCATE_DEVICE_PATH LocateDevicePath
Definition: UefiSpec.h:1971

References EFI_SYSTEM_TABLE::BootServices, DBGC, EEFI, efi_device_path_protocol_guid, efi_devpath_text(), efi_guid_ntoa(), efi_handle_name(), efi_open, efi_path_len(), efi_path_prev(), efi_path_terminate(), efi_systab, end, ENOMEM, free, len, EFI_BOOT_SERVICES::LocateDevicePath, malloc(), memcpy(), protocol, rc, and strerror().

Referenced by chained_locate(), efi_autoexec_load(), efi_device_info_pci(), nii_pci_open(), and snpnet_supported().

◆ 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 110 of file efi_utils.c.

110  {
111  EFI_DEVICE_PATH_PROTOCOL *devpath;
112  int rc;
113 
114  /* Re-open the device path protocol */
116  child, &devpath ) ) != 0 ) {
117  DBGC ( parent, "EFIDEV %s could not add child",
118  efi_handle_name ( parent ) );
119  DBGC ( parent, " %s: %s\n",
120  efi_handle_name ( child ), strerror ( rc ) );
121  DBGC_EFI_OPENERS ( parent, parent,
123  return rc;
124  }
125 
126  DBGC2 ( parent, "EFIDEV %s added child", efi_handle_name ( parent ) );
127  DBGC2 ( parent, " %s\n", efi_handle_name ( child ) );
128  return 0;
129 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
#define DBGC(...)
Definition: compiler.h:505
#define DBGC_EFI_OPENERS(...)
Definition: efi.h:344
This protocol can be used on any device handle to obtain generic path/location information concerning...
Definition: DevicePath.h:45
#define efi_open_by_child(handle, protocol, child, interface)
Open protocol for persistent use by a child controller.
Definition: efi.h:489
const char * efi_handle_name(EFI_HANDLE handle)
Get name of an EFI handle.
Definition: efi_debug.c:652
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:78
EFI_GUID efi_device_path_protocol_guid
Device path protocol GUID.
Definition: efi_guid.c:168
#define DBGC2(...)
Definition: compiler.h:522

References DBGC, DBGC2, DBGC_EFI_OPENERS, efi_device_path_protocol_guid, efi_handle_name(), efi_open_by_child, 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 137 of file efi_utils.c.

137  {
138 
140  DBGC2 ( parent, "EFIDEV %s removed child", efi_handle_name ( parent ) );
141  DBGC2 ( parent, " %s\n", efi_handle_name ( child ) );
142 }
void efi_close_by_child(EFI_HANDLE handle, EFI_GUID *protocol, EFI_HANDLE child)
Close protocol opened for persistent use by a child controller.
Definition: efi_open.c:343
const char * efi_handle_name(EFI_HANDLE handle)
Get name of an EFI handle.
Definition: efi_debug.c:652
EFI_GUID efi_device_path_protocol_guid
Device path protocol GUID.
Definition: efi_guid.c:168
#define DBGC2(...)
Definition: compiler.h:522

References DBGC2, efi_close_by_child(), efi_device_path_protocol_guid, and efi_handle_name().

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

◆ efi_device_info_pci()

static int efi_device_info_pci ( 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 152 of file efi_utils.c.

153  {
155  struct efi_pci_device efipci;
156  int rc;
157 
158  /* Find parent PCI device */
160  &pci_device, 0 ) ) != 0 ) {
161  DBGC ( device, "EFIDEV %s is not a PCI device: %s\n",
162  efi_handle_name ( device ), strerror ( rc ) );
163  return rc;
164  }
165 
166  /* Get PCI device information */
167  if ( ( rc = efipci_info ( pci_device, &efipci ) ) != 0 ) {
168  DBGC ( device, "EFIDEV %s could not get PCI information: %s\n",
169  efi_handle_name ( device ), strerror ( rc ) );
170  return rc;
171  }
172 
173  /* Populate device information */
174  memcpy ( &dev->desc, &efipci.pci.dev.desc, sizeof ( dev->desc ) );
175  snprintf ( dev->name, sizeof ( dev->name ), "%s-%s",
176  prefix, efipci.pci.dev.name );
177 
178  return 0;
179 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
int efipci_info(EFI_HANDLE device, struct efi_pci_device *efipci)
Get EFI PCI device information.
Definition: efi_pci.c:704
EFI_GUID efi_pci_io_protocol_guid
PCI I/O protocol GUID.
Definition: efi_guid.c:312
#define DBGC(...)
Definition: compiler.h:505
char name[40]
Name.
Definition: device.h:78
char prefix[4]
Definition: vmconsole.c:53
A hardware device.
Definition: device.h:76
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:652
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:78
A PCI device.
Definition: pci.h:210
int efi_locate_device(EFI_HANDLE device, EFI_GUID *protocol, EFI_HANDLE *parent, unsigned int skip)
Locate parent device supporting a given protocol.
Definition: efi_utils.c:45
An EFI PCI device.
Definition: efi_pci.h:21
struct device_description desc
Device description.
Definition: device.h:82
int snprintf(char *buf, size_t size, const char *fmt,...)
Write a formatted string to a buffer.
Definition: vsprintf.c:382
Definition: efi.h:61

References DBGC, device::desc, pci_device::dev, efi_handle_name(), efi_locate_device(), efi_pci_io_protocol_guid, efipci_info(), memcpy(), device::name, efi_pci_device::pci, 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 188 of file efi_utils.c.

189  {
190  int rc;
191 
192  /* Try getting underlying PCI device information */
193  if ( ( rc = efi_device_info_pci ( device, prefix, dev ) ) == 0 )
194  return;
195 
196  /* If we cannot get any underlying device information, fall
197  * back to providing information about the EFI handle.
198  */
199  DBGC ( device, "EFIDEV %s could not get underlying device "
200  "information\n", efi_handle_name ( device ) );
201  dev->desc.bus_type = BUS_TYPE_EFI;
202  snprintf ( dev->name, sizeof ( dev->name ), "%s-%p", prefix, device );
203 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
#define DBGC(...)
Definition: compiler.h:505
char name[40]
Name.
Definition: device.h:78
char prefix[4]
Definition: vmconsole.c:53
A hardware device.
Definition: device.h:76
const char * efi_handle_name(EFI_HANDLE handle)
Get name of an EFI handle.
Definition: efi_debug.c:652
#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:82
int snprintf(char *buf, size_t size, const char *fmt,...)
Write a formatted string to a buffer.
Definition: vsprintf.c:382
static int efi_device_info_pci(EFI_HANDLE device, const char *prefix, struct device *dev)
Get underlying PCI device information.
Definition: efi_utils.c:152

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

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