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.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. * Find length of device path (excluding terminator)
  48. *
  49. * @v path Path to device
  50. * @ret path_len Length of device path
  51. */
  52. size_t efi_devpath_len ( EFI_DEVICE_PATH_PROTOCOL *path ) {
  53. EFI_DEVICE_PATH_PROTOCOL *end = efi_devpath_end ( path );
  54. return ( ( ( void * ) end ) - ( ( void * ) path ) );
  55. }
  56. /**
  57. * Locate parent device supporting a given protocol
  58. *
  59. * @v device EFI device handle
  60. * @v protocol Protocol GUID
  61. * @v parent Parent EFI device handle to fill in
  62. * @ret rc Return status code
  63. */
  64. int efi_locate_device ( EFI_HANDLE device, EFI_GUID *protocol,
  65. EFI_HANDLE *parent ) {
  66. EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
  67. union {
  68. EFI_DEVICE_PATH_PROTOCOL *path;
  69. void *interface;
  70. } path;
  71. EFI_DEVICE_PATH_PROTOCOL *devpath;
  72. EFI_STATUS efirc;
  73. int rc;
  74. /* Get device path */
  75. if ( ( efirc = bs->OpenProtocol ( device,
  76. &efi_device_path_protocol_guid,
  77. &path.interface,
  78. efi_image_handle, device,
  79. EFI_OPEN_PROTOCOL_GET_PROTOCOL ))!=0){
  80. rc = -EEFI ( efirc );
  81. DBGC ( device, "EFIDEV %s cannot open device path: %s\n",
  82. efi_handle_name ( device ), strerror ( rc ) );
  83. goto err_open_device_path;
  84. }
  85. devpath = path.path;
  86. /* Check for presence of specified protocol */
  87. if ( ( efirc = bs->LocateDevicePath ( protocol, &devpath,
  88. parent ) ) != 0 ) {
  89. rc = -EEFI ( efirc );
  90. DBGC ( device, "EFIDEV %s has no parent supporting %s: %s\n",
  91. efi_handle_name ( device ),
  92. efi_guid_ntoa ( protocol ), strerror ( rc ) );
  93. goto err_locate_protocol;
  94. }
  95. /* Success */
  96. rc = 0;
  97. err_locate_protocol:
  98. bs->CloseProtocol ( device, &efi_device_path_protocol_guid,
  99. efi_image_handle, device );
  100. err_open_device_path:
  101. return rc;
  102. }
  103. /**
  104. * Add EFI device as child of another EFI device
  105. *
  106. * @v parent EFI parent device handle
  107. * @v child EFI child device handle
  108. * @ret rc Return status code
  109. */
  110. int efi_child_add ( EFI_HANDLE parent, EFI_HANDLE child ) {
  111. EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
  112. void *devpath;
  113. EFI_STATUS efirc;
  114. int rc;
  115. /* Re-open the device path protocol */
  116. if ( ( efirc = bs->OpenProtocol ( parent,
  117. &efi_device_path_protocol_guid,
  118. &devpath,
  119. efi_image_handle, child,
  120. EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
  121. ) ) != 0 ) {
  122. rc = -EEFI ( efirc );
  123. DBGC ( parent, "EFIDEV %s could not add child",
  124. efi_handle_name ( parent ) );
  125. DBGC ( parent, " %s: %s\n",
  126. efi_handle_name ( child ), strerror ( rc ) );
  127. DBGC_EFI_OPENERS ( parent, parent,
  128. &efi_device_path_protocol_guid );
  129. return rc;
  130. }
  131. DBGC2 ( parent, "EFIDEV %s added child", efi_handle_name ( parent ) );
  132. DBGC2 ( parent, " %s\n", efi_handle_name ( child ) );
  133. return 0;
  134. }
  135. /**
  136. * Remove EFI device as child of another EFI device
  137. *
  138. * @v parent EFI parent device handle
  139. * @v child EFI child device handle
  140. */
  141. void efi_child_del ( EFI_HANDLE parent, EFI_HANDLE child ) {
  142. EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
  143. bs->CloseProtocol ( parent, &efi_device_path_protocol_guid,
  144. efi_image_handle, child );
  145. DBGC2 ( parent, "EFIDEV %s removed child", efi_handle_name ( parent ) );
  146. DBGC2 ( parent, " %s\n", efi_handle_name ( child ) );
  147. }
  148. /**
  149. * Get underlying PCI device information
  150. *
  151. * @v device EFI device handle
  152. * @v prefix Device name prefix
  153. * @v dev Generic device to fill in
  154. * @ret rc Return status code
  155. */
  156. static int efi_pci_info ( EFI_HANDLE device, const char *prefix,
  157. struct device *dev ) {
  158. EFI_HANDLE pci_device;
  159. struct pci_device pci;
  160. int rc;
  161. /* Find parent PCI device */
  162. if ( ( rc = efi_locate_device ( device, &efi_pci_io_protocol_guid,
  163. &pci_device ) ) != 0 ) {
  164. DBGC ( device, "EFIDEV %s is not a PCI device: %s\n",
  165. efi_handle_name ( device ), strerror ( rc ) );
  166. return rc;
  167. }
  168. /* Get PCI device information */
  169. if ( ( rc = efipci_info ( pci_device, &pci ) ) != 0 ) {
  170. DBGC ( device, "EFIDEV %s could not get PCI information: %s\n",
  171. efi_handle_name ( device ), strerror ( rc ) );
  172. return rc;
  173. }
  174. /* Populate device information */
  175. memcpy ( &dev->desc, &pci.dev.desc, sizeof ( dev->desc ) );
  176. snprintf ( dev->name, sizeof ( dev->name ), "%s-%s",
  177. prefix, pci.dev.name );
  178. return 0;
  179. }
  180. /**
  181. * Get underlying device information
  182. *
  183. * @v device EFI device handle
  184. * @v prefix Device name prefix
  185. * @v dev Generic device to fill in
  186. */
  187. void efi_device_info ( EFI_HANDLE device, const char *prefix,
  188. struct device *dev ) {
  189. int rc;
  190. /* Try getting underlying PCI device information */
  191. if ( ( rc = efi_pci_info ( device, prefix, dev ) ) == 0 )
  192. return;
  193. /* If we cannot get any underlying device information, fall
  194. * back to providing information about the EFI handle.
  195. */
  196. DBGC ( device, "EFIDEV %s could not get underlying device "
  197. "information\n", efi_handle_name ( device ) );
  198. dev->desc.bus_type = BUS_TYPE_EFI;
  199. snprintf ( dev->name, sizeof ( dev->name ), "%s-%p", prefix, device );
  200. }