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_init.c 6.4KB

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