Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

efi_init.c 8.2KB

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