Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

efi_init.c 6.7KB

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