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

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