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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /*
  2. * Copyright (C) 2014 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. * You can also choose to distribute this program under the terms of
  20. * the Unmodified Binary Distribution Licence (as given in the file
  21. * COPYING.UBDL), provided that you have satisfied its requirements.
  22. */
  23. FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
  24. #include <string.h>
  25. #include <errno.h>
  26. #include <ipxe/init.h>
  27. #include <ipxe/efi/efi.h>
  28. #include <ipxe/efi/efi_driver.h>
  29. #include <ipxe/efi/efi_utils.h>
  30. #include <ipxe/efi/Protocol/SimpleNetwork.h>
  31. #include <ipxe/efi/Protocol/NetworkInterfaceIdentifier.h>
  32. #include "snpnet.h"
  33. #include "nii.h"
  34. /** @file
  35. *
  36. * EFI chainloaded-device-only driver
  37. *
  38. */
  39. /** A chainloaded protocol */
  40. struct chained_protocol {
  41. /** Protocol GUID */
  42. EFI_GUID *protocol;
  43. /**
  44. * Protocol instance installed on the loaded image's device handle
  45. *
  46. * We match against the protocol instance (rather than simply
  47. * matching against the device handle itself) because some
  48. * systems load us via a child of the underlying device, with
  49. * a duplicate protocol installed on the child handle.
  50. */
  51. void *interface;
  52. };
  53. /** Chainloaded SNP protocol */
  54. static struct chained_protocol chained_snp = {
  55. .protocol = &efi_simple_network_protocol_guid,
  56. };
  57. /** Chainloaded NII protocol */
  58. static struct chained_protocol chained_nii = {
  59. .protocol = &efi_nii31_protocol_guid,
  60. };
  61. /**
  62. * Locate chainloaded protocol instance
  63. *
  64. * @v chained Chainloaded protocol
  65. * @ret rc Return status code
  66. */
  67. static int chained_locate ( struct chained_protocol *chained ) {
  68. EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
  69. EFI_HANDLE device = efi_loaded_image->DeviceHandle;
  70. EFI_HANDLE parent;
  71. EFI_STATUS efirc;
  72. int rc;
  73. /* Locate handle supporting this protocol */
  74. if ( ( rc = efi_locate_device ( device, chained->protocol,
  75. &parent ) ) != 0 ) {
  76. DBGC ( device, "CHAINED %s does not support %s: %s\n",
  77. efi_handle_name ( device ),
  78. efi_guid_ntoa ( chained->protocol ), strerror ( rc ) );
  79. goto err_locate_device;
  80. }
  81. DBGC ( device, "CHAINED %s found %s on ", efi_handle_name ( device ),
  82. efi_guid_ntoa ( chained->protocol ) );
  83. DBGC ( device, "%s\n", efi_handle_name ( parent ) );
  84. /* Get protocol instance */
  85. if ( ( efirc = bs->OpenProtocol ( parent, chained->protocol,
  86. &chained->interface, efi_image_handle,
  87. device,
  88. EFI_OPEN_PROTOCOL_GET_PROTOCOL ))!=0){
  89. rc = -EEFI ( efirc );
  90. DBGC ( device, "CHAINED %s could not open %s on ",
  91. efi_handle_name ( device ),
  92. efi_guid_ntoa ( chained->protocol ) );
  93. DBGC ( device, "%s: %s\n",
  94. efi_handle_name ( parent ), strerror ( rc ) );
  95. goto err_open_protocol;
  96. }
  97. err_locate_device:
  98. bs->CloseProtocol ( parent, chained->protocol, efi_image_handle,
  99. device );
  100. err_open_protocol:
  101. return rc;
  102. }
  103. /**
  104. * Check to see if driver supports a device
  105. *
  106. * @v device EFI device handle
  107. * @v chained Chainloaded protocol
  108. * @ret rc Return status code
  109. */
  110. static int chained_supported ( EFI_HANDLE device,
  111. struct chained_protocol *chained ) {
  112. EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
  113. EFI_STATUS efirc;
  114. void *interface;
  115. int rc;
  116. /* Get protocol */
  117. if ( ( efirc = bs->OpenProtocol ( device, chained->protocol, &interface,
  118. efi_image_handle, device,
  119. EFI_OPEN_PROTOCOL_GET_PROTOCOL ))!=0){
  120. rc = -EEFI ( efirc );
  121. DBGCP ( device, "CHAINED %s is not a %s device\n",
  122. efi_handle_name ( device ),
  123. efi_guid_ntoa ( chained->protocol ) );
  124. goto err_open_protocol;
  125. }
  126. /* Test for a match against the chainloading device */
  127. if ( interface != chained->interface ) {
  128. DBGC ( device, "CHAINED %s %p is not the chainloaded %s\n",
  129. efi_handle_name ( device ), interface,
  130. efi_guid_ntoa ( chained->protocol ) );
  131. rc = -ENOTTY;
  132. goto err_no_match;
  133. }
  134. /* Success */
  135. rc = 0;
  136. DBGC ( device, "CHAINED %s %p is the chainloaded %s\n",
  137. efi_handle_name ( device ), interface,
  138. efi_guid_ntoa ( chained->protocol ) );
  139. err_no_match:
  140. bs->CloseProtocol ( device, chained->protocol, efi_image_handle,
  141. device );
  142. err_open_protocol:
  143. return rc;
  144. }
  145. /**
  146. * Check to see if driver supports a device
  147. *
  148. * @v device EFI device handle
  149. * @ret rc Return status code
  150. */
  151. static int snponly_supported ( EFI_HANDLE device ) {
  152. return chained_supported ( device, &chained_snp );
  153. }
  154. /**
  155. * Check to see if driver supports a device
  156. *
  157. * @v device EFI device handle
  158. * @ret rc Return status code
  159. */
  160. static int niionly_supported ( EFI_HANDLE device ) {
  161. return chained_supported ( device, &chained_nii );
  162. }
  163. /** EFI SNP chainloading-device-only driver */
  164. struct efi_driver snponly_driver __efi_driver ( EFI_DRIVER_NORMAL ) = {
  165. .name = "SNPONLY",
  166. .supported = snponly_supported,
  167. .start = snpnet_start,
  168. .stop = snpnet_stop,
  169. };
  170. /** EFI NII chainloading-device-only driver */
  171. struct efi_driver niionly_driver __efi_driver ( EFI_DRIVER_NORMAL ) = {
  172. .name = "NIIONLY",
  173. .supported = niionly_supported,
  174. .start = nii_start,
  175. .stop = nii_stop,
  176. };
  177. /**
  178. * Initialise EFI chainloaded-device-only driver
  179. *
  180. */
  181. static void chained_init ( void ) {
  182. chained_locate ( &chained_snp );
  183. chained_locate ( &chained_nii );
  184. }
  185. /** EFI chainloaded-device-only initialisation function */
  186. struct init_fn chained_init_fn __init_fn ( INIT_LATE ) = {
  187. .initialise = chained_init,
  188. };