You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

efi_driver.c 16KB

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