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

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