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_utils.c 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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 <stdio.h>
  21. #include <string.h>
  22. #include <errno.h>
  23. #include <ipxe/efi/efi.h>
  24. #include <ipxe/efi/efi_pci.h>
  25. #include <ipxe/efi/efi_utils.h>
  26. /** @file
  27. *
  28. * EFI utilities
  29. *
  30. */
  31. /**
  32. * Find end of device path
  33. *
  34. * @v path Path to device
  35. * @ret path_end End of device path
  36. */
  37. EFI_DEVICE_PATH_PROTOCOL * efi_devpath_end ( EFI_DEVICE_PATH_PROTOCOL *path ) {
  38. while ( path->Type != END_DEVICE_PATH_TYPE ) {
  39. path = ( ( ( void * ) path ) +
  40. /* There's this amazing new-fangled thing known as
  41. * a UINT16, but who wants to use one of those? */
  42. ( ( path->Length[1] << 8 ) | path->Length[0] ) );
  43. }
  44. return path;
  45. }
  46. /**
  47. * Locate parent device supporting a given protocol
  48. *
  49. * @v device EFI device handle
  50. * @v protocol Protocol GUID
  51. * @v parent Parent EFI device handle to fill in
  52. * @ret rc Return status code
  53. */
  54. int efi_locate_device ( EFI_HANDLE device, EFI_GUID *protocol,
  55. EFI_HANDLE *parent ) {
  56. EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
  57. union {
  58. EFI_DEVICE_PATH_PROTOCOL *path;
  59. void *interface;
  60. } path;
  61. EFI_DEVICE_PATH_PROTOCOL *devpath;
  62. EFI_STATUS efirc;
  63. int rc;
  64. /* Get device path */
  65. if ( ( efirc = bs->OpenProtocol ( device,
  66. &efi_device_path_protocol_guid,
  67. &path.interface,
  68. efi_image_handle, device,
  69. EFI_OPEN_PROTOCOL_GET_PROTOCOL ))!=0){
  70. rc = -EEFI ( efirc );
  71. DBGC ( device, "EFIDEV %p %s cannot open device path: %s\n",
  72. device, efi_handle_name ( device ), strerror ( rc ) );
  73. goto err_open_device_path;
  74. }
  75. devpath = path.path;
  76. /* Check for presence of specified protocol */
  77. if ( ( efirc = bs->LocateDevicePath ( protocol, &devpath,
  78. parent ) ) != 0 ) {
  79. rc = -EEFI ( efirc );
  80. DBGC ( device, "EFIDEV %p %s has no parent supporting %s: %s\n",
  81. device, efi_handle_name ( device ),
  82. efi_guid_ntoa ( protocol ), strerror ( rc ) );
  83. goto err_locate_protocol;
  84. }
  85. /* Success */
  86. rc = 0;
  87. err_locate_protocol:
  88. bs->CloseProtocol ( device, &efi_device_path_protocol_guid,
  89. efi_image_handle, device );
  90. err_open_device_path:
  91. return rc;
  92. }
  93. /**
  94. * Add EFI device as child of another EFI device
  95. *
  96. * @v parent EFI parent device handle
  97. * @v child EFI child device handle
  98. * @ret rc Return status code
  99. */
  100. int efi_child_add ( EFI_HANDLE parent, EFI_HANDLE child ) {
  101. EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
  102. void *devpath;
  103. EFI_STATUS efirc;
  104. int rc;
  105. /* Re-open the device path protocol */
  106. if ( ( efirc = bs->OpenProtocol ( parent,
  107. &efi_device_path_protocol_guid,
  108. &devpath,
  109. efi_image_handle, child,
  110. EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
  111. ) ) != 0 ) {
  112. rc = -EEFI ( efirc );
  113. DBGC ( parent, "EFIDEV %p %s could not add child",
  114. parent, efi_handle_name ( parent ) );
  115. DBGC ( parent, " %p %s: %s\n", child,
  116. efi_handle_name ( child ), strerror ( rc ) );
  117. DBGC_EFI_OPENERS ( parent, parent,
  118. &efi_device_path_protocol_guid );
  119. return rc;
  120. }
  121. DBGC2 ( parent, "EFIDEV %p %s added child",
  122. parent, efi_handle_name ( parent ) );
  123. DBGC2 ( parent, " %p %s\n", child, efi_handle_name ( child ) );
  124. return 0;
  125. }
  126. /**
  127. * Remove EFI device as child of another EFI device
  128. *
  129. * @v parent EFI parent device handle
  130. * @v child EFI child device handle
  131. */
  132. void efi_child_del ( EFI_HANDLE parent, EFI_HANDLE child ) {
  133. EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
  134. bs->CloseProtocol ( parent, &efi_device_path_protocol_guid,
  135. efi_image_handle, child );
  136. DBGC2 ( parent, "EFIDEV %p %s removed child",
  137. parent, efi_handle_name ( parent ) );
  138. DBGC2 ( parent, " %p %s\n",
  139. child, efi_handle_name ( child ) );
  140. }
  141. /**
  142. * Get underlying PCI device information
  143. *
  144. * @v device EFI device handle
  145. * @v prefix Device name prefix
  146. * @v dev Generic device to fill in
  147. * @ret rc Return status code
  148. */
  149. static int efi_pci_info ( EFI_HANDLE device, const char *prefix,
  150. struct device *dev ) {
  151. EFI_HANDLE pci_device;
  152. struct pci_device pci;
  153. int rc;
  154. /* Find parent PCI device */
  155. if ( ( rc = efi_locate_device ( device, &efi_pci_io_protocol_guid,
  156. &pci_device ) ) != 0 ) {
  157. DBGC ( device, "EFIDEV %p %s is not a PCI device: %s\n",
  158. device, efi_handle_name ( device ), strerror ( rc ) );
  159. return rc;
  160. }
  161. /* Get PCI device information */
  162. if ( ( rc = efipci_info ( pci_device, &pci ) ) != 0 ) {
  163. DBGC ( device, "EFIDEV %p %s could not get PCI information: "
  164. "%s\n", device, efi_handle_name ( device ),
  165. strerror ( rc ) );
  166. return rc;
  167. }
  168. /* Populate device information */
  169. memcpy ( &dev->desc, &pci.dev.desc, sizeof ( dev->desc ) );
  170. snprintf ( dev->name, sizeof ( dev->name ), "%s-%s",
  171. prefix, pci.dev.name );
  172. return 0;
  173. }
  174. /**
  175. * Get underlying device information
  176. *
  177. * @v device EFI device handle
  178. * @v prefix Device name prefix
  179. * @v dev Generic device to fill in
  180. */
  181. void efi_device_info ( EFI_HANDLE device, const char *prefix,
  182. struct device *dev ) {
  183. int rc;
  184. /* Try getting underlying PCI device information */
  185. if ( ( rc = efi_pci_info ( device, prefix, dev ) ) == 0 )
  186. return;
  187. /* If we cannot get any underlying device information, fall
  188. * back to providing information about the EFI handle.
  189. */
  190. DBGC ( device, "EFIDEV %p %s could not get underlying device "
  191. "information\n", device, efi_handle_name ( device ) );
  192. dev->desc.bus_type = BUS_TYPE_EFI;
  193. snprintf ( dev->name, sizeof ( dev->name ), "%s-%p", prefix, device );
  194. }