[dns] Use all configured DNS servers
[ipxe.git] / src / interface / efi / efi_driver.c
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 <stddef.h>
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <string.h>
26 #include <errno.h>
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_utils.h>
34 #include <ipxe/efi/efi_driver.h>
35
36 /** @file
37 *
38 * EFI driver interface
39 *
40 */
41
42 /* Disambiguate the various error causes */
43 #define EINFO_EEFI_CONNECT \
44 __einfo_uniqify ( EINFO_EPLATFORM, 0x01, \
45 "Could not connect controllers" )
46 #define EINFO_EEFI_CONNECT_PROHIBITED \
47 __einfo_platformify ( EINFO_EEFI_CONNECT, \
48 EFI_SECURITY_VIOLATION, \
49 "Connecting controllers prohibited by " \
50 "security policy" )
51 #define EEFI_CONNECT_PROHIBITED \
52 __einfo_error ( EINFO_EEFI_CONNECT_PROHIBITED )
53 #define EEFI_CONNECT( efirc ) EPLATFORM ( EINFO_EEFI_CONNECT, efirc, \
54 EEFI_CONNECT_PROHIBITED )
55
56 static EFI_DRIVER_BINDING_PROTOCOL efi_driver_binding;
57
58 /** List of controlled EFI devices */
59 static LIST_HEAD ( efi_devices );
60
61 /** We are currently disconnecting drivers */
62 static int efi_driver_disconnecting;
63
64 /**
65 * Find EFI device
66 *
67 * @v device EFI device handle
68 * @ret efidev EFI device, or NULL if not found
69 */
70 static struct efi_device * efidev_find ( EFI_HANDLE device ) {
71 struct efi_device *efidev;
72
73 /* Look for an existing EFI device */
74 list_for_each_entry ( efidev, &efi_devices, dev.siblings ) {
75 if ( efidev->device == device )
76 return efidev;
77 }
78
79 return NULL;
80 }
81
82 /**
83 * Get parent EFI device
84 *
85 * @v dev Generic device
86 * @ret efidev Parent EFI device, or NULL
87 */
88 struct efi_device * efidev_parent ( struct device *dev ) {
89 struct device *parent;
90 struct efi_device *efidev;
91
92 /* Walk upwards until we find a registered EFI device */
93 while ( ( parent = dev->parent ) ) {
94 list_for_each_entry ( efidev, &efi_devices, dev.siblings ) {
95 if ( parent == &efidev->dev )
96 return efidev;
97 }
98 dev = parent;
99 }
100
101 return NULL;
102 }
103
104 /**
105 * Check to see if driver supports a device
106 *
107 * @v driver EFI driver
108 * @v device EFI device
109 * @v child Path to child device, if any
110 * @ret efirc EFI status code
111 */
112 static EFI_STATUS EFIAPI
113 efi_driver_supported ( EFI_DRIVER_BINDING_PROTOCOL *driver __unused,
114 EFI_HANDLE device, EFI_DEVICE_PATH_PROTOCOL *child ) {
115 struct efi_driver *efidrv;
116 int rc;
117
118 DBGCP ( device, "EFIDRV %s DRIVER_SUPPORTED",
119 efi_handle_name ( device ) );
120 if ( child )
121 DBGCP ( device, " (child %s)", efi_devpath_text ( child ) );
122 DBGCP ( device, "\n" );
123
124 /* Do nothing if we are already driving this device */
125 if ( efidev_find ( device ) != NULL ) {
126 DBGCP ( device, "EFIDRV %s is already started\n",
127 efi_handle_name ( device ) );
128 return EFI_ALREADY_STARTED;
129 }
130
131 /* Look for a driver claiming to support this device */
132 for_each_table_entry ( efidrv, EFI_DRIVERS ) {
133 if ( ( rc = efidrv->supported ( device ) ) == 0 ) {
134 DBGC ( device, "EFIDRV %s has driver \"%s\"\n",
135 efi_handle_name ( device ), efidrv->name );
136 return 0;
137 }
138 }
139 DBGCP ( device, "EFIDRV %s has no driver\n",
140 efi_handle_name ( device ) );
141
142 return EFI_UNSUPPORTED;
143 }
144
145 /**
146 * Attach driver to device
147 *
148 * @v driver EFI driver
149 * @v device EFI device
150 * @v child Path to child device, if any
151 * @ret efirc EFI status code
152 */
153 static EFI_STATUS EFIAPI
154 efi_driver_start ( EFI_DRIVER_BINDING_PROTOCOL *driver __unused,
155 EFI_HANDLE device, EFI_DEVICE_PATH_PROTOCOL *child ) {
156 EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
157 struct efi_driver *efidrv;
158 struct efi_device *efidev;
159 union {
160 EFI_DEVICE_PATH_PROTOCOL *path;
161 void *interface;
162 } path;
163 EFI_DEVICE_PATH_PROTOCOL *path_end;
164 size_t path_len;
165 EFI_TPL saved_tpl;
166 EFI_STATUS efirc;
167 int rc;
168
169 DBGC ( device, "EFIDRV %s DRIVER_START", efi_handle_name ( device ) );
170 if ( child )
171 DBGC ( device, " (child %s)", efi_devpath_text ( child ) );
172 DBGC ( device, "\n" );
173
174 /* Do nothing if we are already driving this device */
175 efidev = efidev_find ( device );
176 if ( efidev ) {
177 DBGCP ( device, "EFIDRV %s is already started\n",
178 efi_handle_name ( device ) );
179 efirc = EFI_ALREADY_STARTED;
180 goto err_already_started;
181 }
182
183 /* Raise TPL */
184 saved_tpl = bs->RaiseTPL ( TPL_CALLBACK );
185
186 /* Do nothing if we are currently disconnecting drivers */
187 if ( efi_driver_disconnecting ) {
188 DBGC ( device, "EFIDRV %s refusing to start during "
189 "disconnection\n", efi_handle_name ( device ) );
190 efirc = EFI_NOT_READY;
191 goto err_disconnecting;
192 }
193
194 /* Open device path */
195 if ( ( efirc = bs->OpenProtocol ( device,
196 &efi_device_path_protocol_guid,
197 &path.interface, efi_image_handle,
198 device,
199 EFI_OPEN_PROTOCOL_GET_PROTOCOL ))!=0){
200 rc = -EEFI ( efirc );
201 DBGC ( device, "EFIDRV %s could not open device path: %s\n",
202 efi_handle_name ( device ), strerror ( rc ) );
203 goto err_open_path;
204 }
205 path_len = ( efi_devpath_len ( path.path ) + sizeof ( *path_end ) );
206
207 /* Allocate and initialise structure */
208 efidev = zalloc ( sizeof ( *efidev ) + path_len );
209 if ( ! efidev ) {
210 efirc = EFI_OUT_OF_RESOURCES;
211 goto err_alloc;
212 }
213 efidev->device = device;
214 efidev->dev.desc.bus_type = BUS_TYPE_EFI;
215 efidev->path = ( ( ( void * ) efidev ) + sizeof ( *efidev ) );
216 memcpy ( efidev->path, path.path, path_len );
217 INIT_LIST_HEAD ( &efidev->dev.children );
218 list_add ( &efidev->dev.siblings, &efi_devices );
219
220 /* Close device path */
221 bs->CloseProtocol ( device, &efi_device_path_protocol_guid,
222 efi_image_handle, device );
223 path.path = NULL;
224
225 /* Try to start this device */
226 for_each_table_entry ( efidrv, EFI_DRIVERS ) {
227 if ( ( rc = efidrv->supported ( device ) ) != 0 ) {
228 DBGC ( device, "EFIDRV %s is not supported by driver "
229 "\"%s\": %s\n", efi_handle_name ( device ),
230 efidrv->name,
231 strerror ( rc ) );
232 continue;
233 }
234 if ( ( rc = efidrv->start ( efidev ) ) == 0 ) {
235 efidev->driver = efidrv;
236 DBGC ( device, "EFIDRV %s using driver \"%s\"\n",
237 efi_handle_name ( device ),
238 efidev->driver->name );
239 bs->RestoreTPL ( saved_tpl );
240 return 0;
241 }
242 DBGC ( device, "EFIDRV %s could not start driver \"%s\": %s\n",
243 efi_handle_name ( device ), efidrv->name,
244 strerror ( rc ) );
245 }
246 efirc = EFI_UNSUPPORTED;
247
248 list_del ( &efidev->dev.siblings );
249 free ( efidev );
250 err_alloc:
251 if ( path.path ) {
252 bs->CloseProtocol ( device, &efi_device_path_protocol_guid,
253 efi_image_handle, device );
254 }
255 err_open_path:
256 err_disconnecting:
257 bs->RestoreTPL ( saved_tpl );
258 err_already_started:
259 return efirc;
260 }
261
262 /**
263 * Detach driver from device
264 *
265 * @v driver EFI driver
266 * @v device EFI device
267 * @v pci PCI device
268 * @v num_children Number of child devices
269 * @v children List of child devices
270 * @ret efirc EFI status code
271 */
272 static EFI_STATUS EFIAPI
273 efi_driver_stop ( EFI_DRIVER_BINDING_PROTOCOL *driver __unused,
274 EFI_HANDLE device, UINTN num_children,
275 EFI_HANDLE *children ) {
276 EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
277 struct efi_driver *efidrv;
278 struct efi_device *efidev;
279 EFI_TPL saved_tpl;
280 UINTN i;
281
282 DBGC ( device, "EFIDRV %s DRIVER_STOP", efi_handle_name ( device ) );
283 for ( i = 0 ; i < num_children ; i++ ) {
284 DBGC ( device, "%s%s", ( i ? ", " : " child " ),
285 efi_handle_name ( children[i] ) );
286 }
287 DBGC ( device, "\n" );
288
289 /* Do nothing unless we are driving this device */
290 efidev = efidev_find ( device );
291 if ( ! efidev ) {
292 DBGCP ( device, "EFIDRV %s is not started\n",
293 efi_handle_name ( device ) );
294 return EFI_DEVICE_ERROR;
295 }
296
297 /* Raise TPL */
298 saved_tpl = bs->RaiseTPL ( TPL_CALLBACK );
299
300 /* Stop this device */
301 efidrv = efidev->driver;
302 assert ( efidrv != NULL );
303 efidrv->stop ( efidev );
304 list_del ( &efidev->dev.siblings );
305 free ( efidev );
306
307 bs->RestoreTPL ( saved_tpl );
308 return 0;
309 }
310
311 /** EFI driver binding protocol */
312 static EFI_DRIVER_BINDING_PROTOCOL efi_driver_binding = {
313 .Supported = efi_driver_supported,
314 .Start = efi_driver_start,
315 .Stop = efi_driver_stop,
316 };
317
318 /**
319 * Look up driver name
320 *
321 * @v wtf Component name protocol
322 * @v language Language to use
323 * @v driver_name Driver name to fill in
324 * @ret efirc EFI status code
325 */
326 static EFI_STATUS EFIAPI
327 efi_driver_name ( EFI_COMPONENT_NAME2_PROTOCOL *wtf __unused,
328 CHAR8 *language __unused, CHAR16 **driver_name ) {
329 const wchar_t *name;
330
331 name = ( product_wname[0] ? product_wname : build_wname );
332 *driver_name = ( ( wchar_t * ) name );
333 return 0;
334 }
335
336 /**
337 * Look up controller name
338 *
339 * @v wtf Component name protocol
340 * @v device Device
341 * @v child Child device, or NULL
342 * @v language Language to use
343 * @v driver_name Device name to fill in
344 * @ret efirc EFI status code
345 */
346 static EFI_STATUS EFIAPI
347 efi_driver_controller_name ( EFI_COMPONENT_NAME2_PROTOCOL *wtf __unused,
348 EFI_HANDLE device, EFI_HANDLE child,
349 CHAR8 *language, CHAR16 **controller_name ) {
350 EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
351 union {
352 EFI_COMPONENT_NAME2_PROTOCOL *name2;
353 void *interface;
354 } name2;
355 EFI_STATUS efirc;
356
357 /* Delegate to the EFI_COMPONENT_NAME2_PROTOCOL instance
358 * installed on child handle, if present.
359 */
360 if ( ( child != NULL ) &&
361 ( ( efirc = bs->OpenProtocol (
362 child, &efi_component_name2_protocol_guid,
363 &name2.interface, NULL, NULL,
364 EFI_OPEN_PROTOCOL_GET_PROTOCOL ) ) == 0 ) ) {
365 return name2.name2->GetControllerName ( name2.name2, device,
366 child, language,
367 controller_name );
368 }
369
370 /* Otherwise, let EFI use the default Device Path Name */
371 return EFI_UNSUPPORTED;
372 }
373
374 /** EFI component name protocol */
375 static EFI_COMPONENT_NAME2_PROTOCOL efi_wtf = {
376 .GetDriverName = efi_driver_name,
377 .GetControllerName = efi_driver_controller_name,
378 .SupportedLanguages = "en",
379 };
380
381 /**
382 * Install EFI driver
383 *
384 * @ret rc Return status code
385 */
386 int efi_driver_install ( void ) {
387 EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
388 EFI_STATUS efirc;
389 int rc;
390
391 /* Calculate driver version number. We use the build
392 * timestamp (in seconds since the Epoch) shifted right by six
393 * bits: this gives us an approximately one-minute resolution
394 * and a scheme which will last until the year 10680.
395 */
396 efi_driver_binding.Version = ( build_timestamp >> 6 );
397
398 /* Install protocols on image handle */
399 efi_driver_binding.ImageHandle = efi_image_handle;
400 efi_driver_binding.DriverBindingHandle = efi_image_handle;
401 if ( ( efirc = bs->InstallMultipleProtocolInterfaces (
402 &efi_image_handle,
403 &efi_driver_binding_protocol_guid, &efi_driver_binding,
404 &efi_component_name2_protocol_guid, &efi_wtf,
405 NULL ) ) != 0 ) {
406 rc = -EEFI ( efirc );
407 DBGC ( &efi_driver_binding, "EFIDRV could not install "
408 "protocols: %s\n", strerror ( rc ) );
409 return rc;
410 }
411
412 return 0;
413 }
414
415 /**
416 * Uninstall EFI driver
417 *
418 */
419 void efi_driver_uninstall ( void ) {
420 EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
421
422 /* Uninstall protocols */
423 bs->UninstallMultipleProtocolInterfaces (
424 efi_image_handle,
425 &efi_driver_binding_protocol_guid, &efi_driver_binding,
426 &efi_component_name2_protocol_guid, &efi_wtf, NULL );
427 }
428
429 /**
430 * Try to connect EFI driver
431 *
432 * @v device EFI device
433 * @ret rc Return status code
434 */
435 static int efi_driver_connect ( EFI_HANDLE device ) {
436 EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
437 EFI_HANDLE drivers[2] =
438 { efi_driver_binding.DriverBindingHandle, NULL };
439 EFI_STATUS efirc;
440 int rc;
441
442 /* Check if we want to drive this device */
443 if ( ( efirc = efi_driver_supported ( &efi_driver_binding, device,
444 NULL ) ) != 0 ) {
445 /* Not supported; not an error */
446 return 0;
447 }
448
449 /* Disconnect any existing drivers */
450 DBGC2 ( device, "EFIDRV %s before disconnecting:\n",
451 efi_handle_name ( device ) );
452 DBGC2_EFI_PROTOCOLS ( device, device );
453 DBGC ( device, "EFIDRV %s disconnecting existing drivers\n",
454 efi_handle_name ( device ) );
455 efi_driver_disconnecting = 1;
456 if ( ( efirc = bs->DisconnectController ( device, NULL,
457 NULL ) ) != 0 ) {
458 rc = -EEFI ( efirc );
459 DBGC ( device, "EFIDRV %s could not disconnect existing "
460 "drivers: %s\n", efi_handle_name ( device ),
461 strerror ( rc ) );
462 /* Ignore the error and attempt to connect our drivers */
463 }
464 efi_driver_disconnecting = 0;
465 DBGC2 ( device, "EFIDRV %s after disconnecting:\n",
466 efi_handle_name ( device ) );
467 DBGC2_EFI_PROTOCOLS ( device, device );
468
469 /* Connect our driver */
470 DBGC ( device, "EFIDRV %s connecting new drivers\n",
471 efi_handle_name ( device ) );
472 if ( ( efirc = bs->ConnectController ( device, drivers, NULL,
473 FALSE ) ) != 0 ) {
474 rc = -EEFI_CONNECT ( efirc );
475 DBGC ( device, "EFIDRV %s could not connect new drivers: "
476 "%s\n", efi_handle_name ( device ), strerror ( rc ) );
477 DBGC ( device, "EFIDRV %s connecting driver directly\n",
478 efi_handle_name ( device ) );
479 if ( ( efirc = efi_driver_start ( &efi_driver_binding, device,
480 NULL ) ) != 0 ) {
481 rc = -EEFI_CONNECT ( efirc );
482 DBGC ( device, "EFIDRV %s could not connect driver "
483 "directly: %s\n", efi_handle_name ( device ),
484 strerror ( rc ) );
485 return rc;
486 }
487 }
488 DBGC2 ( device, "EFIDRV %s after connecting:\n",
489 efi_handle_name ( device ) );
490 DBGC2_EFI_PROTOCOLS ( device, device );
491
492 return 0;
493 }
494
495 /**
496 * Try to disconnect EFI driver
497 *
498 * @v device EFI device
499 * @ret rc Return status code
500 */
501 static int efi_driver_disconnect ( EFI_HANDLE device ) {
502 EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
503
504 /* Disconnect our driver */
505 efi_driver_disconnecting = 1;
506 bs->DisconnectController ( device,
507 efi_driver_binding.DriverBindingHandle,
508 NULL );
509 efi_driver_disconnecting = 0;
510 return 0;
511 }
512
513 /**
514 * Reconnect original EFI driver
515 *
516 * @v device EFI device
517 * @ret rc Return status code
518 */
519 static int efi_driver_reconnect ( EFI_HANDLE device ) {
520 EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
521
522 /* Reconnect any available driver */
523 bs->ConnectController ( device, NULL, NULL, FALSE );
524
525 return 0;
526 }
527
528 /**
529 * Connect/disconnect EFI driver from all handles
530 *
531 * @v method Connect/disconnect method
532 * @ret rc Return status code
533 */
534 static int efi_driver_handles ( int ( * method ) ( EFI_HANDLE handle ) ) {
535 EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
536 EFI_HANDLE *handles;
537 UINTN num_handles;
538 EFI_STATUS efirc;
539 UINTN i;
540 int rc;
541
542 /* Enumerate all handles */
543 if ( ( efirc = bs->LocateHandleBuffer ( AllHandles, NULL, NULL,
544 &num_handles,
545 &handles ) ) != 0 ) {
546 rc = -EEFI ( efirc );
547 DBGC ( &efi_driver_binding, "EFIDRV could not list handles: "
548 "%s\n", strerror ( rc ) );
549 goto err_locate;
550 }
551
552 /* Connect/disconnect driver from all handles */
553 for ( i = 0 ; i < num_handles ; i++ ) {
554 if ( ( rc = method ( handles[i] ) ) != 0 ) {
555 /* Ignore errors and continue to process
556 * remaining handles.
557 */
558 }
559 }
560
561 /* Success */
562 rc = 0;
563
564 bs->FreePool ( handles );
565 err_locate:
566 return rc;
567 }
568
569 /**
570 * Connect EFI driver to all possible devices
571 *
572 * @ret rc Return status code
573 */
574 int efi_driver_connect_all ( void ) {
575
576 DBGC ( &efi_driver_binding, "EFIDRV connecting our drivers\n" );
577 return efi_driver_handles ( efi_driver_connect );
578 }
579
580 /**
581 * Disconnect EFI driver from all possible devices
582 *
583 * @ret rc Return status code
584 */
585 void efi_driver_disconnect_all ( void ) {
586
587 DBGC ( &efi_driver_binding, "EFIDRV disconnecting our drivers\n" );
588 efi_driver_handles ( efi_driver_disconnect );
589 }
590
591 /**
592 * Reconnect original EFI drivers to all possible devices
593 *
594 * @ret rc Return status code
595 */
596 void efi_driver_reconnect_all ( void ) {
597
598 DBGC ( &efi_driver_binding, "EFIDRV reconnecting old drivers\n" );
599 efi_driver_handles ( efi_driver_reconnect );
600 }