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 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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 <ipxe/efi/efi.h>
  22. #include <ipxe/efi/Protocol/LoadedImage.h>
  23. #include <ipxe/efi/Protocol/DevicePath.h>
  24. #include <ipxe/uuid.h>
  25. #include <ipxe/init.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. /** Loaded image protocol device path for this image */
  31. EFI_DEVICE_PATH_PROTOCOL *efi_loaded_image_path;
  32. /** System table passed to entry point */
  33. EFI_SYSTEM_TABLE *efi_systab;
  34. /** EFI loaded image protocol GUID */
  35. static EFI_GUID efi_loaded_image_protocol_guid
  36. = EFI_LOADED_IMAGE_PROTOCOL_GUID;
  37. /** EFI loaded image device path protocol GUID */
  38. static EFI_GUID efi_loaded_image_device_path_protocol_guid
  39. = EFI_LOADED_IMAGE_DEVICE_PATH_PROTOCOL_GUID;
  40. /** Event used to signal shutdown */
  41. static EFI_EVENT efi_shutdown_event;
  42. /**
  43. * Shut down in preparation for booting an OS.
  44. *
  45. * This hook gets called at ExitBootServices time in order to make
  46. * sure that everything is properly shut down before the OS takes
  47. * over.
  48. */
  49. static EFIAPI void efi_shutdown_hook ( EFI_EVENT event __unused,
  50. void *context __unused ) {
  51. shutdown_boot();
  52. }
  53. /**
  54. * Look up EFI configuration table
  55. *
  56. * @v guid Configuration table GUID
  57. * @ret table Configuration table, or NULL
  58. */
  59. static void * efi_find_table ( EFI_GUID *guid ) {
  60. unsigned int i;
  61. for ( i = 0 ; i < efi_systab->NumberOfTableEntries ; i++ ) {
  62. if ( memcmp ( &efi_systab->ConfigurationTable[i].VendorGuid,
  63. guid, sizeof ( *guid ) ) == 0 )
  64. return efi_systab->ConfigurationTable[i].VendorTable;
  65. }
  66. return NULL;
  67. }
  68. /**
  69. * Initialise EFI environment
  70. *
  71. * @v image_handle Image handle
  72. * @v systab System table
  73. * @ret efirc EFI return status code
  74. */
  75. EFI_STATUS efi_init ( EFI_HANDLE image_handle,
  76. EFI_SYSTEM_TABLE *systab ) {
  77. EFI_BOOT_SERVICES *bs;
  78. struct efi_protocol *prot;
  79. struct efi_config_table *tab;
  80. void *loaded_image;
  81. void *loaded_image_path;
  82. EFI_STATUS efirc;
  83. /* Store image handle and system table pointer for future use */
  84. efi_image_handle = image_handle;
  85. efi_systab = systab;
  86. /* Sanity checks */
  87. if ( ! systab )
  88. return EFI_NOT_AVAILABLE_YET;
  89. if ( ! systab->ConOut )
  90. return EFI_NOT_AVAILABLE_YET;
  91. if ( ! systab->BootServices ) {
  92. DBGC ( systab, "EFI provided no BootServices entry point\n" );
  93. return EFI_NOT_AVAILABLE_YET;
  94. }
  95. if ( ! systab->RuntimeServices ) {
  96. DBGC ( systab, "EFI provided no RuntimeServices entry "
  97. "point\n" );
  98. return EFI_NOT_AVAILABLE_YET;
  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. /* All protocols are required */
  113. return efirc;
  114. }
  115. }
  116. /* Look up used configuration tables */
  117. for_each_table_entry ( tab, EFI_CONFIG_TABLES ) {
  118. if ( ( *(tab->table) = efi_find_table ( &tab->guid ) ) ) {
  119. DBGC ( systab, "EFI configuration table %s is at %p\n",
  120. efi_guid_ntoa ( &tab->guid ), *(tab->table) );
  121. } else {
  122. DBGC ( systab, "EFI does not provide configuration "
  123. "table %s\n", efi_guid_ntoa ( &tab->guid ) );
  124. if ( tab->required )
  125. return EFI_NOT_AVAILABLE_YET;
  126. }
  127. }
  128. /* Get loaded image protocol */
  129. if ( ( efirc = bs->OpenProtocol ( image_handle,
  130. &efi_loaded_image_protocol_guid,
  131. &loaded_image, image_handle, NULL,
  132. EFI_OPEN_PROTOCOL_GET_PROTOCOL ) ) != 0 ) {
  133. DBGC ( systab, "EFI could not get loaded image protocol: %s",
  134. efi_strerror ( efirc ) );
  135. return efirc;
  136. }
  137. efi_loaded_image = loaded_image;
  138. DBGC ( systab, "EFI image base address %p\n",
  139. efi_loaded_image->ImageBase );
  140. /* Get loaded image device path protocol */
  141. if ( ( efirc = bs->OpenProtocol ( image_handle,
  142. &efi_loaded_image_device_path_protocol_guid,
  143. &loaded_image_path, image_handle, NULL,
  144. EFI_OPEN_PROTOCOL_GET_PROTOCOL ) ) != 0 ) {
  145. DBGC ( systab, "EFI could not get loaded image device path "
  146. "protocol: %s", efi_strerror ( efirc ) );
  147. return efirc;
  148. }
  149. efi_loaded_image_path = loaded_image_path;
  150. DBGC ( systab, "EFI image device path " );
  151. DBGC_EFI_DEVPATH ( systab, efi_loaded_image_path );
  152. DBGC ( systab, "\n" );
  153. /* EFI is perfectly capable of gracefully shutting down any
  154. * loaded devices if it decides to fall back to a legacy boot.
  155. * For no particularly comprehensible reason, it doesn't
  156. * bother doing so when ExitBootServices() is called.
  157. */
  158. if ( ( efirc = bs->CreateEvent ( EVT_SIGNAL_EXIT_BOOT_SERVICES,
  159. TPL_CALLBACK, efi_shutdown_hook,
  160. NULL, &efi_shutdown_event ) ) != 0 ) {
  161. DBGC ( systab, "EFI could not create ExitBootServices event: "
  162. "%s\n", efi_strerror ( efirc ) );
  163. return efirc;
  164. }
  165. return 0;
  166. }