Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

efi_driver.c 15KB

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