Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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 <errno.h>
  21. #include <stdlib.h>
  22. #include <ipxe/efi/efi.h>
  23. #include <ipxe/image.h>
  24. #include <ipxe/init.h>
  25. #include <ipxe/features.h>
  26. #include <ipxe/uri.h>
  27. FEATURE ( FEATURE_IMAGE, "EFI", DHCP_EB_FEATURE_EFI, 1 );
  28. /** EFI loaded image protocol GUID */
  29. static EFI_GUID efi_loaded_image_protocol_guid =
  30. EFI_LOADED_IMAGE_PROTOCOL_GUID;
  31. /**
  32. * Create a Unicode command line for the image
  33. *
  34. * @v image EFI image
  35. * @v devpath_out Device path to pass to image (output)
  36. * @v cmdline_out Unicode command line (output)
  37. * @v cmdline_len_out Length of command line in bytes (output)
  38. * @ret rc Return status code
  39. */
  40. static int efi_image_make_cmdline ( struct image *image,
  41. EFI_DEVICE_PATH **devpath_out,
  42. VOID **cmdline_out,
  43. UINT32 *cmdline_len_out ) {
  44. char *uri;
  45. size_t uri_len;
  46. FILEPATH_DEVICE_PATH *devpath;
  47. EFI_DEVICE_PATH *endpath;
  48. size_t devpath_len;
  49. CHAR16 *cmdline;
  50. UINT32 cmdline_len;
  51. size_t args_len = 0;
  52. UINT32 i;
  53. /* Get the URI string of the image */
  54. uri_len = unparse_uri ( NULL, 0, image->uri, URI_ALL ) + 1;
  55. /* Compute final command line length */
  56. if ( image->cmdline ) {
  57. args_len = strlen ( image->cmdline ) + 1;
  58. }
  59. cmdline_len = args_len + uri_len;
  60. /* Allocate space for the uri, final command line and device path */
  61. cmdline = malloc ( cmdline_len * sizeof ( CHAR16 ) + uri_len
  62. + SIZE_OF_FILEPATH_DEVICE_PATH
  63. + uri_len * sizeof ( CHAR16 )
  64. + sizeof ( EFI_DEVICE_PATH ) );
  65. if ( ! cmdline )
  66. return -ENOMEM;
  67. uri = (char *) ( cmdline + cmdline_len );
  68. devpath = (FILEPATH_DEVICE_PATH *) ( uri + uri_len );
  69. endpath = (EFI_DEVICE_PATH *) ( (char *) devpath
  70. + SIZE_OF_FILEPATH_DEVICE_PATH
  71. + uri_len * sizeof ( CHAR16 ) );
  72. /* Build the iPXE device path */
  73. devpath->Header.Type = MEDIA_DEVICE_PATH;
  74. devpath->Header.SubType = MEDIA_FILEPATH_DP;
  75. devpath_len = SIZE_OF_FILEPATH_DEVICE_PATH
  76. + uri_len * sizeof ( CHAR16 );
  77. devpath->Header.Length[0] = devpath_len & 0xFF;
  78. devpath->Header.Length[1] = devpath_len >> 8;
  79. endpath->Type = END_DEVICE_PATH_TYPE;
  80. endpath->SubType = END_ENTIRE_DEVICE_PATH_SUBTYPE;
  81. endpath->Length[0] = 4;
  82. endpath->Length[1] = 0;
  83. unparse_uri ( uri, uri_len, image->uri, URI_ALL );
  84. /* Convert to Unicode */
  85. for ( i = 0 ; i < uri_len ; i++ ) {
  86. cmdline[i] = uri[i];
  87. devpath->PathName[i] = uri[i];
  88. }
  89. if ( image->cmdline ) {
  90. cmdline[uri_len - 1] = ' ';
  91. }
  92. for ( i = 0 ; i < args_len ; i++ ) {
  93. cmdline[i + uri_len] = image->cmdline[i];
  94. }
  95. *devpath_out = &devpath->Header;
  96. *cmdline_out = cmdline;
  97. *cmdline_len_out = cmdline_len * sizeof ( CHAR16 );
  98. return 0;
  99. }
  100. /**
  101. * Execute EFI image
  102. *
  103. * @v image EFI image
  104. * @ret rc Return status code
  105. */
  106. static int efi_image_exec ( struct image *image ) {
  107. EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
  108. union {
  109. EFI_LOADED_IMAGE_PROTOCOL *image;
  110. void *interface;
  111. } loaded;
  112. EFI_HANDLE handle;
  113. EFI_HANDLE device_handle = NULL;
  114. UINTN exit_data_size;
  115. CHAR16 *exit_data;
  116. EFI_STATUS efirc;
  117. int rc;
  118. /* Attempt loading image */
  119. if ( ( efirc = bs->LoadImage ( FALSE, efi_image_handle, NULL,
  120. user_to_virt ( image->data, 0 ),
  121. image->len, &handle ) ) != 0 ) {
  122. /* Not an EFI image */
  123. DBGC ( image, "EFIIMAGE %p could not load: %s\n",
  124. image, efi_strerror ( efirc ) );
  125. rc = -ENOEXEC;
  126. goto err_load_image;
  127. }
  128. /* Get the loaded image protocol for the newly loaded image */
  129. efirc = bs->OpenProtocol ( handle, &efi_loaded_image_protocol_guid,
  130. &loaded.interface, efi_image_handle,
  131. NULL, EFI_OPEN_PROTOCOL_GET_PROTOCOL );
  132. if ( efirc ) {
  133. /* Should never happen */
  134. rc = EFIRC_TO_RC ( efirc );
  135. goto err_open_protocol;
  136. }
  137. /* Pass an iPXE download protocol to the image */
  138. if ( ( rc = efi_download_install ( &device_handle ) ) != 0 ) {
  139. DBGC ( image, "EFIIMAGE %p could not install iPXE download "
  140. "protocol: %s\n", image, strerror ( rc ) );
  141. goto err_download_install;
  142. }
  143. loaded.image->DeviceHandle = device_handle;
  144. loaded.image->ParentHandle = efi_loaded_image;
  145. if ( ( rc = efi_image_make_cmdline ( image, &loaded.image->FilePath,
  146. &loaded.image->LoadOptions,
  147. &loaded.image->LoadOptionsSize ) ) != 0 )
  148. goto err_make_cmdline;
  149. /* Start the image */
  150. if ( ( efirc = bs->StartImage ( handle, &exit_data_size,
  151. &exit_data ) ) != 0 ) {
  152. DBGC ( image, "EFIIMAGE %p returned with status %s\n",
  153. image, efi_strerror ( efirc ) );
  154. rc = EFIRC_TO_RC ( efirc );
  155. goto err_start_image;
  156. }
  157. /* Success */
  158. rc = 0;
  159. err_start_image:
  160. free ( loaded.image->LoadOptions );
  161. err_make_cmdline:
  162. efi_download_uninstall ( device_handle );
  163. err_download_install:
  164. err_open_protocol:
  165. /* Unload the image. We can't leave it loaded, because we
  166. * have no "unload" operation.
  167. */
  168. bs->UnloadImage ( handle );
  169. err_load_image:
  170. return rc;
  171. }
  172. /**
  173. * Probe EFI image
  174. *
  175. * @v image EFI file
  176. * @ret rc Return status code
  177. */
  178. static int efi_image_probe ( struct image *image ) {
  179. EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
  180. EFI_HANDLE handle;
  181. EFI_STATUS efirc;
  182. /* Attempt loading image */
  183. if ( ( efirc = bs->LoadImage ( FALSE, efi_image_handle, NULL,
  184. user_to_virt ( image->data, 0 ),
  185. image->len, &handle ) ) != 0 ) {
  186. /* Not an EFI image */
  187. DBGC ( image, "EFIIMAGE %p could not load: %s\n",
  188. image, efi_strerror ( efirc ) );
  189. return -ENOEXEC;
  190. }
  191. /* Unload the image. We can't leave it loaded, because we
  192. * have no "unload" operation.
  193. */
  194. bs->UnloadImage ( handle );
  195. return 0;
  196. }
  197. /** EFI image type */
  198. struct image_type efi_image_type __image_type ( PROBE_NORMAL ) = {
  199. .name = "EFI",
  200. .probe = efi_image_probe,
  201. .exec = efi_image_exec,
  202. };