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

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