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

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