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 );
27 #include <ipxe/version.h>
28 #include <ipxe/efi/efi.h>
29 #include <ipxe/efi/Protocol/DriverBinding.h>
30 #include <ipxe/efi/Protocol/ComponentName2.h>
31 #include <ipxe/efi/Protocol/DevicePath.h>
32 #include <ipxe/efi/efi_strings.h>
33 #include <ipxe/efi/efi_driver.h>
37 * EFI driver interface
41 static EFI_DRIVER_BINDING_PROTOCOL efi_driver_binding;
43 /** List of controlled EFI devices */
44 static LIST_HEAD ( efi_devices );
49 * @v device EFI device handle
50 * @ret efidev EFI device, or NULL if not found
52 static struct efi_device * efidev_find ( EFI_HANDLE device ) {
53 struct efi_device *efidev;
55 /* Look for an existing EFI device */
56 list_for_each_entry ( efidev, &efi_devices, dev.siblings ) {
57 if ( efidev->device == device )
65 * Get parent EFI device
67 * @v dev Generic device
68 * @ret efidev Parent EFI device, or NULL
70 struct efi_device * efidev_parent ( struct device *dev ) {
71 struct device *parent = dev->parent;
72 struct efi_device *efidev;
74 /* Check that parent exists and is an EFI device */
77 if ( parent->desc.bus_type != BUS_TYPE_EFI )
80 /* Get containing EFI device */
81 efidev = container_of ( parent, struct efi_device, dev );
86 * Check to see if driver supports a device
88 * @v driver EFI driver
89 * @v device EFI device
90 * @v child Path to child device, if any
91 * @ret efirc EFI status code
93 static EFI_STATUS EFIAPI
94 efi_driver_supported ( EFI_DRIVER_BINDING_PROTOCOL *driver __unused,
95 EFI_HANDLE device, EFI_DEVICE_PATH_PROTOCOL *child ) {
96 struct efi_driver *efidrv;
99 DBGCP ( device, "EFIDRV %p %s DRIVER_SUPPORTED",
100 device, efi_handle_name ( device ) );
102 DBGCP ( device, " (child %s)", efi_devpath_text ( child ) );
103 DBGCP ( device, "\n" );
105 /* Do nothing if we are already driving this device */
106 if ( efidev_find ( device ) != NULL ) {
107 DBGCP ( device, "EFIDRV %p %s is already started\n",
108 device, efi_handle_name ( device ) );
109 return EFI_ALREADY_STARTED;
112 /* Look for a driver claiming to support this device */
113 for_each_table_entry ( efidrv, EFI_DRIVERS ) {
114 if ( ( rc = efidrv->supported ( device ) ) == 0 ) {
115 DBGC ( device, "EFIDRV %p %s has driver \"%s\"\n",
116 device, efi_handle_name ( device ),
121 DBGCP ( device, "EFIDRV %p %s has no driver\n",
122 device, efi_handle_name ( device ) );
124 return EFI_UNSUPPORTED;
128 * Attach driver to device
130 * @v driver EFI driver
131 * @v device EFI device
132 * @v child Path to child device, if any
133 * @ret efirc EFI status code
135 static EFI_STATUS EFIAPI
136 efi_driver_start ( EFI_DRIVER_BINDING_PROTOCOL *driver __unused,
137 EFI_HANDLE device, EFI_DEVICE_PATH_PROTOCOL *child ) {
138 struct efi_driver *efidrv;
139 struct efi_device *efidev;
143 DBGC ( device, "EFIDRV %p %s DRIVER_START",
144 device, efi_handle_name ( device ) );
146 DBGC ( device, " (child %s)", efi_devpath_text ( child ) );
147 DBGC ( device, "\n" );
149 /* Do nothing if we are already driving this device */
150 efidev = efidev_find ( device );
152 DBGCP ( device, "EFIDRV %p %s is already started\n",
153 device, efi_handle_name ( device ) );
154 efirc = EFI_ALREADY_STARTED;
155 goto err_already_started;
158 /* Allocate and initialise structure */
159 efidev = zalloc ( sizeof ( *efidev ) );
161 efirc = EFI_OUT_OF_RESOURCES;
164 efidev->device = device;
165 efidev->dev.desc.bus_type = BUS_TYPE_EFI;
166 INIT_LIST_HEAD ( &efidev->dev.children );
167 list_add ( &efidev->dev.siblings, &efi_devices );
169 /* Try to start this device */
170 for_each_table_entry ( efidrv, EFI_DRIVERS ) {
171 if ( ( rc = efidrv->supported ( device ) ) != 0 ) {
172 DBGC ( device, "EFIDRV %p %s is not supported by "
173 "driver \"%s\": %s\n", device,
174 efi_handle_name ( device ), efidrv->name,
178 if ( ( rc = efidrv->start ( efidev ) ) == 0 ) {
179 efidev->driver = efidrv;
180 DBGC ( device, "EFIDRV %p %s using driver \"%s\"\n",
181 device, efi_handle_name ( device ),
182 efidev->driver->name );
185 DBGC ( device, "EFIDRV %p %s could not start driver \"%s\": "
186 "%s\n", device, efi_handle_name ( device ),
187 efidrv->name, strerror ( rc ) );
189 efirc = EFI_UNSUPPORTED;
191 list_del ( &efidev->dev.siblings );
199 * Detach driver from device
201 * @v driver EFI driver
202 * @v device EFI device
204 * @v num_children Number of child devices
205 * @v children List of child devices
206 * @ret efirc EFI status code
208 static EFI_STATUS EFIAPI
209 efi_driver_stop ( EFI_DRIVER_BINDING_PROTOCOL *driver __unused,
210 EFI_HANDLE device, UINTN num_children,
211 EFI_HANDLE *children ) {
212 struct efi_driver *efidrv;
213 struct efi_device *efidev;
216 DBGC ( device, "EFIDRV %p %s DRIVER_STOP",
217 device, efi_handle_name ( device ) );
218 for ( i = 0 ; i < num_children ; i++ ) {
219 DBGC ( device, "%s%p %s", ( i ? ", " : " child " ),
220 children[i], efi_handle_name ( children[i] ) );
222 DBGC ( device, "\n" );
224 /* Do nothing unless we are driving this device */
225 efidev = efidev_find ( device );
227 DBGCP ( device, "EFIDRV %p %s is not started\n",
228 device, efi_handle_name ( device ) );
232 /* Stop this device */
233 efidrv = efidev->driver;
234 assert ( efidrv != NULL );
235 efidrv->stop ( efidev );
236 list_del ( &efidev->dev.siblings );
242 /** EFI driver binding protocol */
243 static EFI_DRIVER_BINDING_PROTOCOL efi_driver_binding = {
244 .Supported = efi_driver_supported,
245 .Start = efi_driver_start,
246 .Stop = efi_driver_stop,
250 * Look up driver name
252 * @v wtf Component name protocol
253 * @v language Language to use
254 * @v driver_name Driver name to fill in
255 * @ret efirc EFI status code
257 static EFI_STATUS EFIAPI
258 efi_driver_name ( EFI_COMPONENT_NAME2_PROTOCOL *wtf __unused,
259 CHAR8 *language __unused, CHAR16 **driver_name ) {
262 name = ( product_wname[0] ? product_wname : build_wname );
263 *driver_name = ( ( wchar_t * ) name );
268 * Look up controller name
270 * @v wtf Component name protocol
272 * @v child Child device, or NULL
273 * @v language Language to use
274 * @v driver_name Device name to fill in
275 * @ret efirc EFI status code
277 static EFI_STATUS EFIAPI
278 efi_driver_controller_name ( EFI_COMPONENT_NAME2_PROTOCOL *wtf __unused,
279 EFI_HANDLE device, EFI_HANDLE child,
280 CHAR8 *language, CHAR16 **controller_name ) {
281 EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
283 EFI_COMPONENT_NAME2_PROTOCOL *name2;
288 /* Delegate to the EFI_COMPONENT_NAME2_PROTOCOL instance
289 * installed on child handle, if present.
291 if ( ( child != NULL ) &&
292 ( ( efirc = bs->OpenProtocol (
293 child, &efi_component_name2_protocol_guid,
294 &name2.interface, NULL, NULL,
295 EFI_OPEN_PROTOCOL_GET_PROTOCOL ) ) == 0 ) ) {
296 return name2.name2->GetControllerName ( name2.name2, device,
301 /* Otherwise, let EFI use the default Device Path Name */
302 return EFI_UNSUPPORTED;
305 /** EFI component name protocol */
306 static EFI_COMPONENT_NAME2_PROTOCOL efi_wtf = {
307 .GetDriverName = efi_driver_name,
308 .GetControllerName = efi_driver_controller_name,
309 .SupportedLanguages = "en",
315 * @ret rc Return status code
317 int efi_driver_install ( void ) {
318 EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
322 /* Calculate driver version number. We use the build
323 * timestamp (in seconds since the Epoch) shifted right by six
324 * bits: this gives us an approximately one-minute resolution
325 * and a scheme which will last until the year 10680.
327 efi_driver_binding.Version = ( build_timestamp >> 6 );
329 /* Install protocols on image handle */
330 efi_driver_binding.ImageHandle = efi_image_handle;
331 efi_driver_binding.DriverBindingHandle = efi_image_handle;
332 if ( ( efirc = bs->InstallMultipleProtocolInterfaces (
334 &efi_driver_binding_protocol_guid, &efi_driver_binding,
335 &efi_component_name2_protocol_guid, &efi_wtf,
337 rc = -EEFI ( efirc );
338 DBGC ( &efi_driver_binding, "EFIDRV could not install "
339 "protocols: %s\n", strerror ( rc ) );
347 * Uninstall EFI driver
350 void efi_driver_uninstall ( void ) {
351 EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
353 /* Uninstall protocols */
354 bs->UninstallMultipleProtocolInterfaces (
356 &efi_driver_binding_protocol_guid, &efi_driver_binding,
357 &efi_component_name2_protocol_guid, &efi_wtf, NULL );
361 * Try to connect EFI driver
363 * @v device EFI device
364 * @ret rc Return status code
366 static int efi_driver_connect ( EFI_HANDLE device ) {
367 EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
368 EFI_HANDLE drivers[2] =
369 { efi_driver_binding.DriverBindingHandle, NULL };
373 /* Check if we want to drive this device */
374 if ( ( efirc = efi_driver_supported ( &efi_driver_binding, device,
376 /* Not supported; not an error */
380 /* Disconnect any existing drivers */
381 DBGC2 ( device, "EFIDRV %p %s before disconnecting:\n",
382 device, efi_handle_name ( device ) );
383 DBGC2_EFI_PROTOCOLS ( device, device );
384 DBGC ( device, "EFIDRV %p %s disconnecting existing drivers\n",
385 device, efi_handle_name ( device ) );
386 if ( ( efirc = bs->DisconnectController ( device, NULL,
388 rc = -EEFI ( efirc );
389 DBGC ( device, "EFIDRV %p %s could not disconnect existing "
390 "drivers: %s\n", device, efi_handle_name ( device ),
392 /* Ignore the error and attempt to connect our drivers */
394 DBGC2 ( device, "EFIDRV %p %s after disconnecting:\n",
395 device, efi_handle_name ( device ) );
396 DBGC2_EFI_PROTOCOLS ( device, device );
398 /* Connect our driver */
399 DBGC ( device, "EFIDRV %p %s connecting new drivers\n",
400 device, efi_handle_name ( device ) );
401 if ( ( efirc = bs->ConnectController ( device, drivers, NULL,
403 rc = -EEFI ( efirc );
404 DBGC ( device, "EFIDRV %p %s could not connect new drivers: "
405 "%s\n", device, efi_handle_name ( device ),
409 DBGC2 ( device, "EFIDRV %p %s after connecting:\n",
410 device, efi_handle_name ( device ) );
411 DBGC2_EFI_PROTOCOLS ( device, device );
417 * Try to disconnect EFI driver
419 * @v device EFI device
420 * @ret rc Return status code
422 static int efi_driver_disconnect ( EFI_HANDLE device ) {
423 EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
425 /* Disconnect our driver */
426 bs->DisconnectController ( device,
427 efi_driver_binding.DriverBindingHandle,
433 * Reconnect original EFI driver
435 * @v device EFI device
436 * @ret rc Return status code
438 static int efi_driver_reconnect ( EFI_HANDLE device ) {
439 EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
441 /* Reconnect any available driver */
442 bs->ConnectController ( device, NULL, NULL, FALSE );
448 * Connect/disconnect EFI driver from all handles
450 * @v method Connect/disconnect method
451 * @ret rc Return status code
453 static int efi_driver_handles ( int ( * method ) ( EFI_HANDLE handle ) ) {
454 EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
461 /* Enumerate all handles */
462 if ( ( efirc = bs->LocateHandleBuffer ( AllHandles, NULL, NULL,
464 &handles ) ) != 0 ) {
465 rc = -EEFI ( efirc );
466 DBGC ( &efi_driver_binding, "EFIDRV could not list handles: "
467 "%s\n", strerror ( rc ) );
471 /* Connect/disconnect driver from all handles */
472 for ( i = 0 ; i < num_handles ; i++ ) {
473 if ( ( rc = method ( handles[i] ) ) != 0 )
481 bs->FreePool ( handles );
487 * Connect EFI driver to all possible devices
489 * @ret rc Return status code
491 int efi_driver_connect_all ( void ) {
493 DBGC ( &efi_driver_binding, "EFIDRV connecting our drivers\n" );
494 return efi_driver_handles ( efi_driver_connect );
498 * Disconnect EFI driver from all possible devices
500 * @ret rc Return status code
502 void efi_driver_disconnect_all ( void ) {
504 DBGC ( &efi_driver_binding, "EFIDRV disconnecting our drivers\n" );
505 efi_driver_handles ( efi_driver_disconnect );
509 * Reconnect original EFI drivers to all possible devices
511 * @ret rc Return status code
513 void efi_driver_reconnect_all ( void ) {
515 DBGC ( &efi_driver_binding, "EFIDRV reconnecting old drivers\n" );
516 efi_driver_handles ( efi_driver_reconnect );