2 * Copyright (C) 2011 Michael Brown <mbrown@fensystems.co.uk>.
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.
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.
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
20 FILE_LICENCE ( GPL2_OR_LATER );
25 #include <ipxe/efi/efi.h>
26 #include <ipxe/efi/efi_pci.h>
27 #include <ipxe/efi/efi_utils.h>
36 * Find end of device path
38 * @v path Path to device
39 * @ret path_end End of device path
41 EFI_DEVICE_PATH_PROTOCOL * efi_devpath_end ( EFI_DEVICE_PATH_PROTOCOL *path ) {
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] ) );
54 * Locate parent device supporting a given protocol
56 * @v device EFI device handle
57 * @v protocol Protocol GUID
58 * @v parent Parent EFI device handle to fill in
59 * @ret rc Return status code
61 int efi_locate_device ( EFI_HANDLE device, EFI_GUID *protocol,
62 EFI_HANDLE *parent ) {
63 EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
65 EFI_DEVICE_PATH_PROTOCOL *path;
68 EFI_DEVICE_PATH_PROTOCOL *devpath;
73 if ( ( efirc = bs->OpenProtocol ( device,
74 &efi_device_path_protocol_guid,
76 efi_image_handle, device,
77 EFI_OPEN_PROTOCOL_GET_PROTOCOL ))!=0){
79 DBGC ( device, "EFIDEV %p %s cannot open device path: %s\n",
80 device, efi_handle_name ( device ), strerror ( rc ) );
81 goto err_open_device_path;
85 /* Check for presence of specified protocol */
86 if ( ( efirc = bs->LocateDevicePath ( protocol, &devpath,
89 DBGC ( device, "EFIDEV %p %s has no parent supporting %s: %s\n",
90 device, efi_handle_name ( device ),
91 efi_guid_ntoa ( protocol ), strerror ( rc ) );
92 goto err_locate_protocol;
99 bs->CloseProtocol ( device, &efi_device_path_protocol_guid,
100 efi_image_handle, device );
101 err_open_device_path:
106 * Add EFI device as child of another EFI device
108 * @v parent EFI parent device handle
109 * @v child EFI child device handle
110 * @ret rc Return status code
112 int efi_child_add ( EFI_HANDLE parent, EFI_HANDLE child ) {
113 EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
118 /* Re-open the device path protocol */
119 if ( ( efirc = bs->OpenProtocol ( parent,
120 &efi_device_path_protocol_guid,
122 efi_image_handle, child,
123 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
125 rc = -EEFI ( efirc );
126 DBGC ( parent, "EFIDEV %p %s could not add child",
127 parent, efi_handle_name ( parent ) );
128 DBGC ( parent, " %p %s: %s\n", child,
129 efi_handle_name ( child ), strerror ( rc ) );
130 DBGC_EFI_OPENERS ( parent, parent,
131 &efi_device_path_protocol_guid );
135 DBGC2 ( parent, "EFIDEV %p %s added child",
136 parent, efi_handle_name ( parent ) );
137 DBGC2 ( parent, " %p %s\n", child, efi_handle_name ( child ) );
142 * Remove EFI device as child of another EFI device
144 * @v parent EFI parent device handle
145 * @v child EFI child device handle
147 void efi_child_del ( EFI_HANDLE parent, EFI_HANDLE child ) {
148 EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
150 bs->CloseProtocol ( parent, &efi_device_path_protocol_guid,
151 efi_image_handle, child );
152 DBGC2 ( parent, "EFIDEV %p %s removed child",
153 parent, efi_handle_name ( parent ) );
154 DBGC2 ( parent, " %p %s\n",
155 child, efi_handle_name ( child ) );
159 * Get underlying PCI device information
161 * @v device EFI device handle
162 * @v prefix Device name prefix
163 * @v dev Generic device to fill in
164 * @ret rc Return status code
166 static int efi_pci_info ( EFI_HANDLE device, const char *prefix,
167 struct device *dev ) {
168 EFI_HANDLE pci_device;
169 struct pci_device pci;
172 /* Find parent PCI device */
173 if ( ( rc = efi_locate_device ( device, &efi_pci_io_protocol_guid,
174 &pci_device ) ) != 0 ) {
175 DBGC ( device, "EFIDEV %p %s is not a PCI device: %s\n",
176 device, efi_handle_name ( device ), strerror ( rc ) );
180 /* Get PCI device information */
181 if ( ( rc = efipci_info ( pci_device, &pci ) ) != 0 ) {
182 DBGC ( device, "EFIDEV %p %s could not get PCI information: "
183 "%s\n", device, efi_handle_name ( device ),
188 /* Populate device information */
189 memcpy ( &dev->desc, &pci.dev.desc, sizeof ( dev->desc ) );
190 snprintf ( dev->name, sizeof ( dev->name ), "%s-%s",
191 prefix, pci.dev.name );
197 * Get underlying device information
199 * @v device EFI device handle
200 * @v prefix Device name prefix
201 * @v dev Generic device to fill in
203 void efi_device_info ( EFI_HANDLE device, const char *prefix,
204 struct device *dev ) {
207 /* Try getting underlying PCI device information */
208 if ( ( rc = efi_pci_info ( device, prefix, dev ) ) == 0 )
211 /* If we cannot get any underlying device information, fall
212 * back to providing information about the EFI handle.
214 DBGC ( device, "EFIDEV %p %s could not get underlying device "
215 "information\n", device, efi_handle_name ( device ) );
216 dev->desc.bus_type = BUS_TYPE_EFI;
217 snprintf ( dev->name, sizeof ( dev->name ), "%s-%p", prefix, device );