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_pci.h>
27 #include <ipxe/efi/efi_utils.h>
28 
29 /** @file
30  *
31  * EFI utilities
32  *
33  */
34 
35 /**
36  * Find end of device path
37  *
38  * @v path Path to device
39  * @ret path_end End of device path
40  */
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 }
52 
53 /**
54  * Find length of device path (excluding terminator)
55  *
56  * @v path Path to device
57  * @ret path_len Length of device path
58  */
61 
62  return ( ( ( void * ) end ) - ( ( void * ) path ) );
63 }
64 
65 /**
66  * Locate parent device supporting a given protocol
67  *
68  * @v device EFI device handle
69  * @v protocol Protocol GUID
70  * @v parent Parent EFI device handle to fill in
71  * @ret rc Return status code
72  */
74  EFI_HANDLE *parent ) {
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 }
116 
117 /**
118  * Add EFI device as child of another EFI device
119  *
120  * @v parent EFI parent device handle
121  * @v child EFI child device handle
122  * @ret rc Return status code
123  */
124 int efi_child_add ( EFI_HANDLE parent, EFI_HANDLE child ) {
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 }
151 
152 /**
153  * Remove EFI device as child of another EFI device
154  *
155  * @v parent EFI parent device handle
156  * @v child EFI child device handle
157  */
158 void efi_child_del ( EFI_HANDLE parent, EFI_HANDLE child ) {
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 }
166 
167 /**
168  * Get underlying PCI device information
169  *
170  * @v device EFI device handle
171  * @v prefix Device name prefix
172  * @v dev Generic device to fill in
173  * @ret rc Return status code
174  */
175 static int efi_pci_info ( EFI_HANDLE device, const char *prefix,
176  struct device *dev ) {
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 }
203 
204 /**
205  * Get underlying device information
206  *
207  * @v device EFI device handle
208  * @v prefix Device name prefix
209  * @v dev Generic device to fill in
210  */
212  struct device *dev ) {
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 }
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:157
#define EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
Definition: UefiSpec.h:1273
#define END_DEVICE_PATH_TYPE
Definition: DevicePath.h:1327
128 bit buffer containing a unique identifier value.
Definition: Base.h:263
Error codes.
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
size_t efi_devpath_len(EFI_DEVICE_PATH_PROTOCOL *path)
Find length of device path (excluding terminator)
Definition: efi_utils.c:59
#define DBGC_EFI_OPENERS(...)
Definition: efi.h:260
This protocol can be used on any device handle to obtain generic path/location information concerning...
Definition: DevicePath.h:51
int efi_child_add(EFI_HANDLE parent, EFI_HANDLE child)
Add EFI device as child of another EFI device.
Definition: efi_utils.c:124
EFI_CLOSE_PROTOCOL CloseProtocol
Definition: UefiSpec.h:1906
struct device dev
Generic device.
Definition: pci.h:189
EFI utilities.
void efi_child_del(EFI_HANDLE parent, EFI_HANDLE child)
Remove EFI device as child of another EFI device.
Definition: efi_utils.c:158
static int efi_pci_info(EFI_HANDLE device, const char *prefix, struct device *dev)
Get underlying PCI device information.
Definition: efi_utils.c:175
FILE_LICENCE(GPL2_OR_LATER)
A hardware device.
Definition: device.h:73
void * memcpy(void *dest, const void *src, size_t len) __nonnull
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
A PCI device.
Definition: pci.h:187
EFI_GUID efi_device_path_protocol_guid
Device path protocol GUID.
Definition: efi_guid.c:132
#define BUS_TYPE_EFI
EFI bus type.
Definition: device.h:61
EFI API.
const char * efi_guid_ntoa(EFI_GUID *guid)
Convert GUID to a printable string.
Definition: efi_debug.c:192
unsigned int bus_type
Bus type.
Definition: device.h:24
UINT8 Length[2]
Specific Device Path data.
Definition: DevicePath.h:64
#define DBGC2(...)
Definition: compiler.h:522
RETURN_STATUS EFI_STATUS
Function return status for EFI API.
Definition: UefiBaseType.h:35
EFI driver interface.
uint32_t end
Ending offset.
Definition: netvsc.h:18
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
UINT8 Type
0x01 Hardware Device Path.
Definition: DevicePath.h:52
EFI_SYSTEM_TABLE * efi_systab
EFI_OPEN_PROTOCOL OpenProtocol
Definition: UefiSpec.h:1905
uint16_t protocol
Protocol ID.
Definition: stp.h:18
int efipci_info(EFI_HANDLE device, struct pci_device *pci)
Get EFI PCI device information.
Definition: efi_pci.c:330
String functions.
Definition: efi.h:55
EFI_LOCATE_DEVICE_PATH LocateDevicePath
Definition: UefiSpec.h:1877
EFI_DEVICE_PATH_PROTOCOL * efi_devpath_end(EFI_DEVICE_PATH_PROTOCOL *path)
Find end of device path.
Definition: efi_utils.c:41
void efi_device_info(EFI_HANDLE device, const char *prefix, struct device *dev)
Get underlying device information.
Definition: efi_utils.c:211