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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /*
  2. * Copyright (C) 2008 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 <string.h>
  21. #include <errno.h>
  22. #include <ipxe/init.h>
  23. #include <ipxe/efi/efi.h>
  24. #include <ipxe/efi/efi_driver.h>
  25. #include <ipxe/efi/Protocol/LoadedImage.h>
  26. /** Image handle passed to entry point */
  27. EFI_HANDLE efi_image_handle;
  28. /** Loaded image protocol for this image */
  29. EFI_LOADED_IMAGE_PROTOCOL *efi_loaded_image;
  30. /** System table passed to entry point */
  31. EFI_SYSTEM_TABLE *efi_systab;
  32. /** Event used to signal shutdown */
  33. static EFI_EVENT efi_shutdown_event;
  34. /* Forward declarations */
  35. static EFI_STATUS EFIAPI efi_unload ( EFI_HANDLE image_handle );
  36. /**
  37. * Shut down in preparation for booting an OS.
  38. *
  39. * This hook gets called at ExitBootServices time in order to make
  40. * sure that everything is properly shut down before the OS takes
  41. * over.
  42. */
  43. static EFIAPI void efi_shutdown_hook ( EFI_EVENT event __unused,
  44. void *context __unused ) {
  45. shutdown_boot();
  46. }
  47. /**
  48. * Look up EFI configuration table
  49. *
  50. * @v guid Configuration table GUID
  51. * @ret table Configuration table, or NULL
  52. */
  53. static void * efi_find_table ( EFI_GUID *guid ) {
  54. unsigned int i;
  55. for ( i = 0 ; i < efi_systab->NumberOfTableEntries ; i++ ) {
  56. if ( memcmp ( &efi_systab->ConfigurationTable[i].VendorGuid,
  57. guid, sizeof ( *guid ) ) == 0 )
  58. return efi_systab->ConfigurationTable[i].VendorTable;
  59. }
  60. return NULL;
  61. }
  62. /**
  63. * Initialise EFI environment
  64. *
  65. * @v image_handle Image handle
  66. * @v systab System table
  67. * @ret efirc EFI return status code
  68. */
  69. EFI_STATUS efi_init ( EFI_HANDLE image_handle,
  70. EFI_SYSTEM_TABLE *systab ) {
  71. EFI_BOOT_SERVICES *bs;
  72. struct efi_protocol *prot;
  73. struct efi_config_table *tab;
  74. void *loaded_image;
  75. EFI_STATUS efirc;
  76. int rc;
  77. /* Store image handle and system table pointer for future use */
  78. efi_image_handle = image_handle;
  79. efi_systab = systab;
  80. /* Sanity checks */
  81. if ( ! systab ) {
  82. efirc = EFI_NOT_AVAILABLE_YET;
  83. goto err_sanity;
  84. }
  85. if ( ! systab->ConOut ) {
  86. efirc = EFI_NOT_AVAILABLE_YET;
  87. goto err_sanity;
  88. }
  89. if ( ! systab->BootServices ) {
  90. DBGC ( systab, "EFI provided no BootServices entry point\n" );
  91. efirc = EFI_NOT_AVAILABLE_YET;
  92. goto err_sanity;
  93. }
  94. if ( ! systab->RuntimeServices ) {
  95. DBGC ( systab, "EFI provided no RuntimeServices entry "
  96. "point\n" );
  97. efirc = EFI_NOT_AVAILABLE_YET;
  98. goto err_sanity;
  99. }
  100. DBGC ( systab, "EFI handle %p systab %p\n", image_handle, systab );
  101. bs = systab->BootServices;
  102. /* Look up used protocols */
  103. for_each_table_entry ( prot, EFI_PROTOCOLS ) {
  104. if ( ( efirc = bs->LocateProtocol ( &prot->guid, NULL,
  105. prot->protocol ) ) == 0 ) {
  106. DBGC ( systab, "EFI protocol %s is at %p\n",
  107. efi_guid_ntoa ( &prot->guid ),
  108. *(prot->protocol) );
  109. } else {
  110. DBGC ( systab, "EFI does not provide protocol %s\n",
  111. efi_guid_ntoa ( &prot->guid ) );
  112. /* Fail if protocol is required */
  113. if ( prot->required )
  114. goto err_missing_protocol;
  115. }
  116. }
  117. /* Look up used configuration tables */
  118. for_each_table_entry ( tab, EFI_CONFIG_TABLES ) {
  119. if ( ( *(tab->table) = efi_find_table ( &tab->guid ) ) ) {
  120. DBGC ( systab, "EFI configuration table %s is at %p\n",
  121. efi_guid_ntoa ( &tab->guid ), *(tab->table) );
  122. } else {
  123. DBGC ( systab, "EFI does not provide configuration "
  124. "table %s\n", efi_guid_ntoa ( &tab->guid ) );
  125. if ( tab->required ) {
  126. efirc = EFI_NOT_AVAILABLE_YET;
  127. goto err_missing_table;
  128. }
  129. }
  130. }
  131. /* Get loaded image protocol */
  132. if ( ( efirc = bs->OpenProtocol ( image_handle,
  133. &efi_loaded_image_protocol_guid,
  134. &loaded_image, image_handle, NULL,
  135. EFI_OPEN_PROTOCOL_GET_PROTOCOL ) ) != 0 ) {
  136. rc = -EEFI ( efirc );
  137. DBGC ( systab, "EFI could not get loaded image protocol: %s",
  138. strerror ( rc ) );
  139. goto err_no_loaded_image;
  140. }
  141. efi_loaded_image = loaded_image;
  142. DBGC ( systab, "EFI image base address %p\n",
  143. efi_loaded_image->ImageBase );
  144. /* EFI is perfectly capable of gracefully shutting down any
  145. * loaded devices if it decides to fall back to a legacy boot.
  146. * For no particularly comprehensible reason, it doesn't
  147. * bother doing so when ExitBootServices() is called.
  148. */
  149. if ( ( efirc = bs->CreateEvent ( EVT_SIGNAL_EXIT_BOOT_SERVICES,
  150. TPL_CALLBACK, efi_shutdown_hook,
  151. NULL, &efi_shutdown_event ) ) != 0 ) {
  152. rc = -EEFI ( efirc );
  153. DBGC ( systab, "EFI could not create ExitBootServices event: "
  154. "%s\n", strerror ( rc ) );
  155. goto err_create_event;
  156. }
  157. /* Install driver binding protocol */
  158. if ( ( rc = efi_driver_install() ) != 0 ) {
  159. DBGC ( systab, "EFI could not install driver: %s\n",
  160. strerror ( rc ) );
  161. efirc = EFIRC ( rc );
  162. goto err_driver_install;
  163. }
  164. /* Install image unload method */
  165. efi_loaded_image->Unload = efi_unload;
  166. return 0;
  167. efi_driver_uninstall();
  168. err_driver_install:
  169. bs->CloseEvent ( efi_shutdown_event );
  170. err_create_event:
  171. err_no_loaded_image:
  172. err_missing_table:
  173. err_missing_protocol:
  174. err_sanity:
  175. return efirc;
  176. }
  177. /**
  178. * Shut down EFI environment
  179. *
  180. * @v image_handle Image handle
  181. */
  182. static EFI_STATUS EFIAPI efi_unload ( EFI_HANDLE image_handle __unused ) {
  183. EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
  184. EFI_SYSTEM_TABLE *systab = efi_systab;
  185. DBGC ( systab, "EFI image unloading\n" );
  186. /* Shut down */
  187. shutdown_exit();
  188. /* Disconnect any remaining devices */
  189. efi_driver_disconnect_all();
  190. /* Uninstall driver binding protocol */
  191. efi_driver_uninstall();
  192. /* Uninstall exit boot services event */
  193. bs->CloseEvent ( efi_shutdown_event );
  194. DBGC ( systab, "EFI image unloaded\n" );
  195. return 0;
  196. }