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

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