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 13KB

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