您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

efi_driver.c 14KB

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