Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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