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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. /**
  26. * Execute EFI image
  27. *
  28. * @v image EFI image
  29. * @ret rc Return status code
  30. */
  31. static int efi_image_exec ( struct image *image ) {
  32. EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
  33. EFI_HANDLE handle;
  34. UINTN exit_data_size;
  35. CHAR16 *exit_data;
  36. EFI_STATUS efirc;
  37. int rc;
  38. /* Attempt loading image */
  39. if ( ( efirc = bs->LoadImage ( FALSE, efi_image_handle, NULL,
  40. user_to_virt ( image->data, 0 ),
  41. image->len, &handle ) ) != 0 ) {
  42. /* Not an EFI image */
  43. DBGC ( image, "EFIIMAGE %p could not load: %s\n",
  44. image, efi_strerror ( efirc ) );
  45. return -ENOEXEC;
  46. }
  47. /* Start the image */
  48. if ( ( efirc = bs->StartImage ( handle, &exit_data_size,
  49. &exit_data ) ) != 0 ) {
  50. DBGC ( image, "EFIIMAGE %p returned with status %s\n",
  51. image, efi_strerror ( efirc ) );
  52. }
  53. rc = EFIRC_TO_RC ( efirc );
  54. /* Unload the image. We can't leave it loaded, because we
  55. * have no "unload" operation.
  56. */
  57. bs->UnloadImage ( handle );
  58. return rc;
  59. }
  60. /**
  61. * Probe EFI image
  62. *
  63. * @v image EFI file
  64. * @ret rc Return status code
  65. */
  66. static int efi_image_probe ( struct image *image ) {
  67. EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
  68. EFI_HANDLE handle;
  69. EFI_STATUS efirc;
  70. /* Attempt loading image */
  71. if ( ( efirc = bs->LoadImage ( FALSE, efi_image_handle, NULL,
  72. user_to_virt ( image->data, 0 ),
  73. image->len, &handle ) ) != 0 ) {
  74. /* Not an EFI image */
  75. DBGC ( image, "EFIIMAGE %p could not load: %s\n",
  76. image, efi_strerror ( efirc ) );
  77. return -ENOEXEC;
  78. }
  79. /* Unload the image. We can't leave it loaded, because we
  80. * have no "unload" operation.
  81. */
  82. bs->UnloadImage ( handle );
  83. return 0;
  84. }
  85. /** EFI image type */
  86. struct image_type efi_image_type __image_type ( PROBE_NORMAL ) = {
  87. .name = "EFI",
  88. .probe = efi_image_probe,
  89. .exec = efi_image_exec,
  90. };