iPXE
efi_utils.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2011 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 <stdio.h>
23 #include <string.h>
24 #include <errno.h>
25 #include <ipxe/efi/efi.h>
26 #include <ipxe/efi/efi_path.h>
27 #include <ipxe/efi/efi_pci.h>
28 #include <ipxe/efi/efi_utils.h>
29 
30 /** @file
31  *
32  * EFI utilities
33  *
34  */
35 
36 /**
37  * Locate parent device supporting a given protocol
38  *
39  * @v device EFI device handle
40  * @v protocol Protocol GUID
41  * @v parent Parent EFI device handle to fill in
42  * @v skip Number of protocol-supporting parent devices to skip
43  * @ret rc Return status code
44  */
46  EFI_HANDLE *parent, unsigned int skip ) {
48  union {
50  void *interface;
51  } u;
54  size_t len;
55  EFI_STATUS efirc;
56  int rc;
57 
58  /* Get device path */
59  if ( ( efirc = bs->OpenProtocol ( device,
61  &u.interface,
64  rc = -EEFI ( efirc );
65  DBGC ( device, "EFIDEV %s cannot open device path: %s\n",
66  efi_handle_name ( device ), strerror ( rc ) );
67  goto err_open_device_path;
68  }
69 
70  /* Create modifiable copy of device path */
71  len = ( efi_path_len ( u.path ) + sizeof ( EFI_DEVICE_PATH_PROTOCOL ));
72  path = malloc ( len );
73  if ( ! path ) {
74  rc = -ENOMEM;
75  goto err_alloc_path;
76  }
77  memcpy ( path, u.path, len );
78 
79  /* Locate parent device(s) */
80  while ( 1 ) {
81 
82  /* Check for presence of specified protocol */
83  end = path;
84  if ( ( efirc = bs->LocateDevicePath ( protocol, &end,
85  parent ) ) != 0 ) {
86  rc = -EEFI ( efirc );
87  DBGC ( device, "EFIDEV %s has no parent supporting "
88  "%s: %s\n", efi_devpath_text ( path ),
89  efi_guid_ntoa ( protocol ), strerror ( rc ) );
90  goto err_locate_protocol;
91  }
92 
93  /* Stop if we have skipped the requested number of devices */
94  if ( ! skip-- )
95  break;
96 
97  /* Trim device path */
99  end = efi_path_prev ( path, end );
101  }
102 
103  /* Success */
104  rc = 0;
105 
106  err_locate_protocol:
107  free ( path );
108  err_alloc_path:
111  err_open_device_path:
112  return rc;
113 }
114 
115 /**
116  * Add EFI device as child of another EFI device
117  *
118  * @v parent EFI parent device handle
119  * @v child EFI child device handle
120  * @ret rc Return status code
121  */
122 int efi_child_add ( EFI_HANDLE parent, EFI_HANDLE child ) {
124  void *devpath;
125  EFI_STATUS efirc;
126  int rc;
127 
128  /* Re-open the device path protocol */
129  if ( ( efirc = bs->OpenProtocol ( parent,
131  &devpath,
132  efi_image_handle, child,
134  ) ) != 0 ) {
135  rc = -EEFI ( efirc );
136  DBGC ( parent, "EFIDEV %s could not add child",
137  efi_handle_name ( parent ) );
138  DBGC ( parent, " %s: %s\n",
139  efi_handle_name ( child ), strerror ( rc ) );
140  DBGC_EFI_OPENERS ( parent, parent,
142  return rc;
143  }
144 
145  DBGC2 ( parent, "EFIDEV %s added child", efi_handle_name ( parent ) );
146  DBGC2 ( parent, " %s\n", efi_handle_name ( child ) );
147  return 0;
148 }
149 
150 /**
151  * Remove EFI device as child of another EFI device
152  *
153  * @v parent EFI parent device handle
154  * @v child EFI child device handle
155  */
156 void efi_child_del ( EFI_HANDLE parent, EFI_HANDLE child ) {
158 
160  efi_image_handle, child );
161  DBGC2 ( parent, "EFIDEV %s removed child", efi_handle_name ( parent ) );
162  DBGC2 ( parent, " %s\n", efi_handle_name ( child ) );
163 }
164 
165 /**
166  * Get underlying PCI device information
167  *
168  * @v device EFI device handle
169  * @v prefix Device name prefix
170  * @v dev Generic device to fill in
171  * @ret rc Return status code
172  */
173 static int efi_pci_info ( EFI_HANDLE device, const char *prefix,
174  struct device *dev ) {
176  struct efi_pci_device efipci;
177  int rc;
178 
179  /* Find parent PCI device */
181  &pci_device, 0 ) ) != 0 ) {
182  DBGC ( device, "EFIDEV %s is not a PCI device: %s\n",
183  efi_handle_name ( device ), strerror ( rc ) );
184  return rc;
185  }
186 
187  /* Get PCI device information */
188  if ( ( rc = efipci_info ( pci_device, &efipci ) ) != 0 ) {
189  DBGC ( device, "EFIDEV %s could not get PCI information: %s\n",
190  efi_handle_name ( device ), strerror ( rc ) );
191  return rc;
192  }
193 
194  /* Populate device information */
195  memcpy ( &dev->desc, &efipci.pci.dev.desc, sizeof ( dev->desc ) );
196  snprintf ( dev->name, sizeof ( dev->name ), "%s-%s",
197  prefix, efipci.pci.dev.name );
198 
199  return 0;
200 }
201 
202 /**
203  * Get underlying device information
204  *
205  * @v device EFI device handle
206  * @v prefix Device name prefix
207  * @v dev Generic device to fill in
208  */
210  struct device *dev ) {
211  int rc;
212 
213  /* Try getting underlying PCI device information */
214  if ( ( rc = efi_pci_info ( device, prefix, dev ) ) == 0 )
215  return;
216 
217  /* If we cannot get any underlying device information, fall
218  * back to providing information about the EFI handle.
219  */
220  DBGC ( device, "EFIDEV %s could not get underlying device "
221  "information\n", efi_handle_name ( device ) );
222  dev->desc.bus_type = BUS_TYPE_EFI;
223  snprintf ( dev->name, sizeof ( dev->name ), "%s-%p", prefix, device );
224 }
EFI_BOOT_SERVICES * BootServices
A pointer to the EFI Boot Services Table.
Definition: UefiSpec.h:2081
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:171
int efipci_info(EFI_HANDLE device, struct efi_pci_device *efipci)
Get EFI PCI device information.
Definition: efi_pci.c:759
#define EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
Definition: UefiSpec.h:1346
struct pci_device pci
PCI device.
Definition: efi_pci.h:23
128 bit buffer containing a unique identifier value.
Definition: Base.h:215
Error codes.
EFI_GUID efi_pci_io_protocol_guid
PCI I/O protocol GUID.
Definition: efi_guid.c:283
#define DBGC(...)
Definition: compiler.h:505
char name[40]
Name.
Definition: device.h:75
size_t efi_path_len(EFI_DEVICE_PATH_PROTOCOL *path)
Find length of device path (excluding terminator)
Definition: efi_path.c:144
char prefix[4]
Definition: vmconsole.c:53
#define DBGC_EFI_OPENERS(...)
Definition: efi.h:321
This protocol can be used on any device handle to obtain generic path/location information concerning...
Definition: DevicePath.h:45
int efi_child_add(EFI_HANDLE parent, EFI_HANDLE child)
Add EFI device as child of another EFI device.
Definition: efi_utils.c:122
EFI_CLOSE_PROTOCOL CloseProtocol
Definition: UefiSpec.h:1987
struct device dev
Generic device.
Definition: pci.h:208
EFI utilities.
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:115
void efi_child_del(EFI_HANDLE parent, EFI_HANDLE child)
Remove EFI device as child of another EFI device.
Definition: efi_utils.c:156
static int efi_pci_info(EFI_HANDLE device, const char *prefix, struct device *dev)
Get underlying PCI device information.
Definition: efi_utils.c:173
FILE_LICENCE(GPL2_OR_LATER)
#define ENOMEM
Not enough space.
Definition: errno.h:534
A hardware device.
Definition: device.h:73
void * memcpy(void *dest, const void *src, size_t len) __nonnull
An object interface.
Definition: interface.h:124
const char * efi_devpath_text(EFI_DEVICE_PATH_PROTOCOL *path)
Get textual representation of device path.
Definition: efi_debug.c:461
#define EFI_OPEN_PROTOCOL_GET_PROTOCOL
Definition: UefiSpec.h:1344
const char * efi_handle_name(EFI_HANDLE handle)
Get name of an EFI handle.
Definition: efi_debug.c:808
const char * efi_guid_ntoa(CONST EFI_GUID *guid)
Convert GUID to a printable string.
Definition: efi_debug.c:254
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:1917
EFI_HANDLE efi_image_handle
Image handle passed to entry point.
Definition: efi_init.c:34
A PCI device.
Definition: pci.h:206
EFI device paths.
EFI_GUID efi_device_path_protocol_guid
Device path protocol GUID.
Definition: efi_guid.c:143
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
#define BUS_TYPE_EFI
EFI bus type.
Definition: device.h:61
void * malloc(size_t size)
Allocate memory.
Definition: malloc.c:583
EFI API.
An EFI PCI device.
Definition: efi_pci.h:21
unsigned int bus_type
Bus type.
Definition: device.h:24
uint32_t len
Length.
Definition: ena.h:14
#define DBGC2(...)
Definition: compiler.h:522
RETURN_STATUS EFI_STATUS
Function return status for EFI API.
Definition: UefiBaseType.h:31
EFI driver interface.
union @17 u
uint32_t end
Ending offset.
Definition: netvsc.h:18
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
EFI_SYSTEM_TABLE * efi_systab
EFI_OPEN_PROTOCOL OpenProtocol
Definition: UefiSpec.h:1986
uint16_t protocol
Protocol ID.
Definition: stp.h:18
String functions.
Definition: efi.h:59
EFI_LOCATE_DEVICE_PATH LocateDevicePath
Definition: UefiSpec.h:1958
void efi_device_info(EFI_HANDLE device, const char *prefix, struct device *dev)
Get underlying device information.
Definition: efi_utils.c:209