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.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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/features.h>
  23. FEATURE ( FEATURE_IMAGE, "EFI", DHCP_EB_FEATURE_EFI, 1 );
  24. struct image_type efi_image_type __image_type ( PROBE_NORMAL );
  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. /* Attempt loading image */
  38. if ( ( efirc = bs->LoadImage ( FALSE, efi_image_handle, NULL,
  39. user_to_virt ( image->data, 0 ),
  40. image->len, &handle ) ) != 0 ) {
  41. /* Not an EFI image */
  42. DBGC ( image, "EFIIMAGE %p could not load: %s\n",
  43. image, efi_strerror ( efirc ) );
  44. return -ENOEXEC;
  45. }
  46. /* Start the image */
  47. if ( ( efirc = bs->StartImage ( handle, &exit_data_size,
  48. &exit_data ) ) != 0 ) {
  49. DBGC ( image, "EFIIMAGE %p returned with status %s\n",
  50. image, efi_strerror ( efirc ) );
  51. goto done;
  52. }
  53. done:
  54. /* Unload the image. We can't leave it loaded, because we
  55. * have no "unload" operation.
  56. */
  57. bs->UnloadImage ( handle );
  58. return EFIRC_TO_RC ( efirc );
  59. }
  60. /**
  61. * Load EFI image into memory
  62. *
  63. * @v image EFI file
  64. * @ret rc Return status code
  65. */
  66. static int efi_image_load ( 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. /* This is an EFI image */
  80. if ( ! image->type )
  81. image->type = &efi_image_type;
  82. /* Unload the image. We can't leave it loaded, because we
  83. * have no "unload" operation.
  84. */
  85. bs->UnloadImage ( handle );
  86. return 0;
  87. }
  88. /** EFI image type */
  89. struct image_type efi_image_type __image_type ( PROBE_NORMAL ) = {
  90. .name = "EFI",
  91. .load = efi_image_load,
  92. .exec = efi_image_exec,
  93. };