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_image.c 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 <errno.h>
  20. #include <ipxe/efi/efi.h>
  21. #include <ipxe/image.h>
  22. #include <ipxe/init.h>
  23. #include <ipxe/features.h>
  24. FEATURE ( FEATURE_IMAGE, "EFI", DHCP_EB_FEATURE_EFI, 1 );
  25. struct image_type efi_image_type __image_type ( PROBE_NORMAL );
  26. /** Event used to signal shutdown */
  27. static EFI_EVENT efi_shutdown_event;
  28. /**
  29. * Shut down in preparation for booting an OS.
  30. *
  31. * This hook gets called at ExitBootServices time in order to make sure that
  32. * the network cards are properly shut down before the OS takes over.
  33. */
  34. static EFIAPI void efi_shutdown_hook ( EFI_EVENT event __unused,
  35. void *context __unused ) {
  36. shutdown ( SHUTDOWN_BOOT );
  37. }
  38. /**
  39. * Execute EFI image
  40. *
  41. * @v image EFI image
  42. * @ret rc Return status code
  43. */
  44. static int efi_image_exec ( struct image *image ) {
  45. EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
  46. EFI_HANDLE handle;
  47. UINTN exit_data_size;
  48. CHAR16 *exit_data;
  49. EFI_STATUS efirc;
  50. int rc;
  51. /* Attempt loading image */
  52. if ( ( efirc = bs->LoadImage ( FALSE, efi_image_handle, NULL,
  53. user_to_virt ( image->data, 0 ),
  54. image->len, &handle ) ) != 0 ) {
  55. /* Not an EFI image */
  56. DBGC ( image, "EFIIMAGE %p could not load: %s\n",
  57. image, efi_strerror ( efirc ) );
  58. return -ENOEXEC;
  59. }
  60. /* Be sure to shut down the NIC at ExitBootServices time, or else
  61. * DMA from the card can corrupt the OS.
  62. */
  63. efirc = bs->CreateEvent ( EVT_SIGNAL_EXIT_BOOT_SERVICES,
  64. TPL_CALLBACK, efi_shutdown_hook,
  65. NULL, &efi_shutdown_event );
  66. if ( efirc ) {
  67. rc = EFIRC_TO_RC ( efirc );
  68. goto done;
  69. }
  70. /* Start the image */
  71. if ( ( efirc = bs->StartImage ( handle, &exit_data_size,
  72. &exit_data ) ) != 0 ) {
  73. DBGC ( image, "EFIIMAGE %p returned with status %s\n",
  74. image, efi_strerror ( efirc ) );
  75. }
  76. rc = EFIRC_TO_RC ( efirc );
  77. /* Remove the shutdown hook */
  78. bs->CloseEvent ( efi_shutdown_event );
  79. done:
  80. /* Unload the image. We can't leave it loaded, because we
  81. * have no "unload" operation.
  82. */
  83. bs->UnloadImage ( handle );
  84. return rc;
  85. }
  86. /**
  87. * Load EFI image into memory
  88. *
  89. * @v image EFI file
  90. * @ret rc Return status code
  91. */
  92. static int efi_image_load ( struct image *image ) {
  93. EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
  94. EFI_HANDLE handle;
  95. EFI_STATUS efirc;
  96. /* Attempt loading image */
  97. if ( ( efirc = bs->LoadImage ( FALSE, efi_image_handle, NULL,
  98. user_to_virt ( image->data, 0 ),
  99. image->len, &handle ) ) != 0 ) {
  100. /* Not an EFI image */
  101. DBGC ( image, "EFIIMAGE %p could not load: %s\n",
  102. image, efi_strerror ( efirc ) );
  103. return -ENOEXEC;
  104. }
  105. /* This is an EFI image */
  106. if ( ! image->type )
  107. image->type = &efi_image_type;
  108. /* Unload the image. We can't leave it loaded, because we
  109. * have no "unload" operation.
  110. */
  111. bs->UnloadImage ( handle );
  112. return 0;
  113. }
  114. /** EFI image type */
  115. struct image_type efi_image_type __image_type ( PROBE_NORMAL ) = {
  116. .name = "EFI",
  117. .load = efi_image_load,
  118. .exec = efi_image_exec,
  119. };