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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18. FILE_LICENCE ( GPL2_OR_LATER );
  19. #include <string.h>
  20. #include <ipxe/efi/efi.h>
  21. #include <ipxe/efi/Protocol/LoadedImage.h>
  22. #include <ipxe/uuid.h>
  23. #include <ipxe/init.h>
  24. /** Image handle passed to entry point */
  25. EFI_HANDLE efi_image_handle;
  26. /** Loaded image protocol for this image */
  27. EFI_LOADED_IMAGE_PROTOCOL *efi_loaded_image;
  28. /** System table passed to entry point */
  29. EFI_SYSTEM_TABLE *efi_systab;
  30. /** EFI loaded image protocol GUID */
  31. static EFI_GUID efi_loaded_image_protocol_guid
  32. = EFI_LOADED_IMAGE_PROTOCOL_GUID;
  33. /** Event used to signal shutdown */
  34. static EFI_EVENT efi_shutdown_event;
  35. /**
  36. * Shut down in preparation for booting an OS.
  37. *
  38. * This hook gets called at ExitBootServices time in order to make
  39. * sure that everything is properly shut down before the OS takes
  40. * over.
  41. */
  42. static EFIAPI void efi_shutdown_hook ( EFI_EVENT event __unused,
  43. void *context __unused ) {
  44. shutdown_boot();
  45. }
  46. /**
  47. * Look up EFI configuration table
  48. *
  49. * @v guid Configuration table GUID
  50. * @ret table Configuration table, or NULL
  51. */
  52. static void * efi_find_table ( EFI_GUID *guid ) {
  53. unsigned int i;
  54. for ( i = 0 ; i < efi_systab->NumberOfTableEntries ; i++ ) {
  55. if ( memcmp ( &efi_systab->ConfigurationTable[i].VendorGuid,
  56. guid, sizeof ( *guid ) ) == 0 )
  57. return efi_systab->ConfigurationTable[i].VendorTable;
  58. }
  59. return NULL;
  60. }
  61. /**
  62. * Initialise EFI environment
  63. *
  64. * @v image_handle Image handle
  65. * @v systab System table
  66. * @ret efirc EFI return status code
  67. */
  68. EFI_STATUS efi_init ( EFI_HANDLE image_handle,
  69. EFI_SYSTEM_TABLE *systab ) {
  70. EFI_BOOT_SERVICES *bs;
  71. struct efi_protocol *prot;
  72. struct efi_config_table *tab;
  73. EFI_STATUS efirc;
  74. void *loaded_image;
  75. /* Store image handle and system table pointer for future use */
  76. efi_image_handle = image_handle;
  77. efi_systab = systab;
  78. /* Sanity checks */
  79. if ( ! systab )
  80. return EFI_NOT_AVAILABLE_YET;
  81. if ( ! systab->ConOut )
  82. return EFI_NOT_AVAILABLE_YET;
  83. if ( ! systab->BootServices ) {
  84. DBGC ( systab, "EFI provided no BootServices entry point\n" );
  85. return EFI_NOT_AVAILABLE_YET;
  86. }
  87. if ( ! systab->RuntimeServices ) {
  88. DBGC ( systab, "EFI provided no RuntimeServices entry "
  89. "point\n" );
  90. return EFI_NOT_AVAILABLE_YET;
  91. }
  92. DBGC ( systab, "EFI handle %p systab %p\n", image_handle, systab );
  93. bs = systab->BootServices;
  94. efirc = bs->OpenProtocol ( image_handle,
  95. &efi_loaded_image_protocol_guid,
  96. &loaded_image, image_handle, NULL,
  97. EFI_OPEN_PROTOCOL_GET_PROTOCOL );
  98. if ( efirc ) {
  99. DBGC ( systab, "Could not get loaded image protocol" );
  100. return efirc;
  101. }
  102. efi_loaded_image = loaded_image;
  103. DBG ( "Image base address = %p\n", efi_loaded_image->ImageBase );
  104. /* Look up used protocols */
  105. for_each_table_entry ( prot, EFI_PROTOCOLS ) {
  106. if ( ( efirc = bs->LocateProtocol ( &prot->u.guid, NULL,
  107. prot->protocol ) ) == 0 ) {
  108. DBGC ( systab, "EFI protocol %s is at %p\n",
  109. uuid_ntoa ( &prot->u.uuid ), *(prot->protocol));
  110. } else {
  111. DBGC ( systab, "EFI does not provide protocol %s\n",
  112. uuid_ntoa ( &prot->u.uuid ) );
  113. /* All protocols are required */
  114. return efirc;
  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->u.guid ) ) ) {
  120. DBGC ( systab, "EFI configuration table %s is at %p\n",
  121. uuid_ntoa ( &tab->u.uuid ), *(tab->table) );
  122. } else {
  123. DBGC ( systab, "EFI does not provide configuration "
  124. "table %s\n", uuid_ntoa ( &tab->u.uuid ) );
  125. if ( tab->required )
  126. return EFI_NOT_AVAILABLE_YET;
  127. }
  128. }
  129. /* EFI is perfectly capable of gracefully shutting down any
  130. * loaded devices if it decides to fall back to a legacy boot.
  131. * For no particularly comprehensible reason, it doesn't
  132. * bother doing so when ExitBootServices() is called.
  133. */
  134. if ( ( efirc = bs->CreateEvent ( EVT_SIGNAL_EXIT_BOOT_SERVICES,
  135. TPL_CALLBACK, efi_shutdown_hook,
  136. NULL, &efi_shutdown_event ) ) != 0 ) {
  137. DBGC ( systab, "EFI could not create ExitBootServices event: "
  138. "%s\n", efi_strerror ( efirc ) );
  139. return efirc;
  140. }
  141. return 0;
  142. }