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