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

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