Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

efi_image.c 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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 <wchar.h>
  23. #include <ipxe/efi/efi.h>
  24. #include <ipxe/efi/efi_snp.h>
  25. #include <ipxe/efi/efi_download.h>
  26. #include <ipxe/efi/efi_file.h>
  27. #include <ipxe/efi/efi_driver.h>
  28. #include <ipxe/efi/efi_strings.h>
  29. #include <ipxe/image.h>
  30. #include <ipxe/init.h>
  31. #include <ipxe/features.h>
  32. #include <ipxe/uri.h>
  33. FEATURE ( FEATURE_IMAGE, "EFI", DHCP_EB_FEATURE_EFI, 1 );
  34. /** EFI loaded image protocol GUID */
  35. static EFI_GUID efi_loaded_image_protocol_guid =
  36. EFI_LOADED_IMAGE_PROTOCOL_GUID;
  37. /**
  38. * Create device path for image
  39. *
  40. * @v image EFI image
  41. * @v parent Parent device path
  42. * @ret path Device path, or NULL on failure
  43. *
  44. * The caller must eventually free() the device path.
  45. */
  46. static EFI_DEVICE_PATH_PROTOCOL *
  47. efi_image_path ( struct image *image, EFI_DEVICE_PATH_PROTOCOL *parent ) {
  48. EFI_DEVICE_PATH_PROTOCOL *path;
  49. FILEPATH_DEVICE_PATH *filepath;
  50. EFI_DEVICE_PATH_PROTOCOL *end;
  51. size_t name_len;
  52. size_t prefix_len;
  53. size_t filepath_len;
  54. size_t len;
  55. /* Calculate device path lengths */
  56. end = efi_devpath_end ( parent );
  57. prefix_len = ( ( void * ) end - ( void * ) parent );
  58. name_len = strlen ( image->name );
  59. filepath_len = ( SIZE_OF_FILEPATH_DEVICE_PATH +
  60. ( name_len + 1 /* NUL */ ) * sizeof ( wchar_t ) );
  61. len = ( prefix_len + filepath_len + sizeof ( *end ) );
  62. /* Allocate device path */
  63. path = zalloc ( len );
  64. if ( ! path )
  65. return NULL;
  66. /* Construct device path */
  67. memcpy ( path, parent, prefix_len );
  68. filepath = ( ( ( void * ) path ) + prefix_len );
  69. filepath->Header.Type = MEDIA_DEVICE_PATH;
  70. filepath->Header.SubType = MEDIA_FILEPATH_DP;
  71. filepath->Header.Length[0] = ( filepath_len & 0xff );
  72. filepath->Header.Length[1] = ( filepath_len >> 8 );
  73. efi_snprintf ( filepath->PathName, ( name_len + 1 /* NUL */ ),
  74. "%s", image->name );
  75. end = ( ( ( void * ) filepath ) + filepath_len );
  76. end->Type = END_DEVICE_PATH_TYPE;
  77. end->SubType = END_ENTIRE_DEVICE_PATH_SUBTYPE;
  78. end->Length[0] = sizeof ( *end );
  79. return path;
  80. }
  81. /**
  82. * Create command line for image
  83. *
  84. * @v image EFI image
  85. * @ret cmdline Command line, or NULL on failure
  86. */
  87. static wchar_t * efi_image_cmdline ( struct image *image ) {
  88. wchar_t *cmdline;
  89. size_t len;
  90. len = ( strlen ( image->name ) +
  91. ( image->cmdline ?
  92. ( 1 /* " " */ + strlen ( image->cmdline ) ) : 0 ) );
  93. cmdline = zalloc ( ( len + 1 /* NUL */ ) * sizeof ( wchar_t ) );
  94. if ( ! cmdline )
  95. return NULL;
  96. efi_snprintf ( cmdline, ( len + 1 /* NUL */ ), "%s%s%s",
  97. image->name,
  98. ( image->cmdline ? " " : "" ),
  99. ( image->cmdline ? image->cmdline : "" ) );
  100. return cmdline;
  101. }
  102. /**
  103. * Execute EFI image
  104. *
  105. * @v image EFI image
  106. * @ret rc Return status code
  107. */
  108. static int efi_image_exec ( struct image *image ) {
  109. EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
  110. struct efi_snp_device *snpdev;
  111. EFI_DEVICE_PATH_PROTOCOL *path;
  112. union {
  113. EFI_LOADED_IMAGE_PROTOCOL *image;
  114. void *interface;
  115. } loaded;
  116. EFI_HANDLE handle;
  117. wchar_t *cmdline;
  118. EFI_STATUS efirc;
  119. int rc;
  120. /* Find an appropriate device handle to use */
  121. snpdev = last_opened_snpdev();
  122. if ( ! snpdev ) {
  123. DBGC ( image, "EFIIMAGE %p could not identify SNP device\n",
  124. image );
  125. rc = -ENODEV;
  126. goto err_no_snpdev;
  127. }
  128. /* Install file I/O protocols */
  129. if ( ( rc = efi_file_install ( &snpdev->handle ) ) != 0 ) {
  130. DBGC ( image, "EFIIMAGE %p could not install file protocol: "
  131. "%s\n", image, strerror ( rc ) );
  132. goto err_file_install;
  133. }
  134. /* Install iPXE download protocol */
  135. if ( ( rc = efi_download_install ( &snpdev->handle ) ) != 0 ) {
  136. DBGC ( image, "EFIIMAGE %p could not install iPXE download "
  137. "protocol: %s\n", image, strerror ( rc ) );
  138. goto err_download_install;
  139. }
  140. /* Create device path for image */
  141. path = efi_image_path ( image, &snpdev->path );
  142. if ( ! path ) {
  143. DBGC ( image, "EFIIMAGE %p could not create device path\n",
  144. image );
  145. rc = -ENOMEM;
  146. goto err_image_path;
  147. }
  148. /* Create command line for image */
  149. cmdline = efi_image_cmdline ( image );
  150. if ( ! cmdline ) {
  151. DBGC ( image, "EFIIMAGE %p could not create command line\n",
  152. image );
  153. rc = -ENOMEM;
  154. goto err_cmdline;
  155. }
  156. /* Attempt loading image */
  157. if ( ( efirc = bs->LoadImage ( FALSE, efi_image_handle, path,
  158. user_to_virt ( image->data, 0 ),
  159. image->len, &handle ) ) != 0 ) {
  160. /* Not an EFI image */
  161. DBGC ( image, "EFIIMAGE %p could not load: %s\n",
  162. image, efi_strerror ( efirc ) );
  163. rc = -ENOEXEC;
  164. goto err_load_image;
  165. }
  166. /* Get the loaded image protocol for the newly loaded image */
  167. efirc = bs->OpenProtocol ( handle, &efi_loaded_image_protocol_guid,
  168. &loaded.interface, efi_image_handle,
  169. NULL, EFI_OPEN_PROTOCOL_GET_PROTOCOL );
  170. if ( efirc ) {
  171. /* Should never happen */
  172. rc = EFIRC_TO_RC ( efirc );
  173. goto err_open_protocol;
  174. }
  175. /* Sanity checks */
  176. assert ( loaded.image->ParentHandle == efi_image_handle );
  177. assert ( loaded.image->DeviceHandle == snpdev->handle );
  178. assert ( loaded.image->LoadOptionsSize == 0 );
  179. assert ( loaded.image->LoadOptions == NULL );
  180. /* Set command line */
  181. loaded.image->LoadOptions = cmdline;
  182. loaded.image->LoadOptionsSize =
  183. ( ( wcslen ( cmdline ) + 1 /* NUL */ ) * sizeof ( wchar_t ) );
  184. /* Start the image */
  185. if ( ( efirc = bs->StartImage ( handle, NULL, NULL ) ) != 0 ) {
  186. DBGC ( image, "EFIIMAGE %p returned with status %s\n",
  187. image, efi_strerror ( efirc ) );
  188. rc = EFIRC_TO_RC ( efirc );
  189. goto err_start_image;
  190. }
  191. /* Success */
  192. rc = 0;
  193. err_start_image:
  194. err_open_protocol:
  195. /* Unload the image. We can't leave it loaded, because we
  196. * have no "unload" operation.
  197. */
  198. if ( ( efirc = bs->UnloadImage ( handle ) ) != 0 ) {
  199. DBGC ( image, "EFIIMAGE %p could not unload: %s\n",
  200. image, efi_strerror ( efirc ) );
  201. }
  202. err_load_image:
  203. free ( cmdline );
  204. err_cmdline:
  205. free ( path );
  206. err_image_path:
  207. efi_download_uninstall ( snpdev->handle );
  208. err_download_install:
  209. efi_file_uninstall ( snpdev->handle );
  210. err_file_install:
  211. err_no_snpdev:
  212. return rc;
  213. }
  214. /**
  215. * Probe EFI image
  216. *
  217. * @v image EFI file
  218. * @ret rc Return status code
  219. */
  220. static int efi_image_probe ( struct image *image ) {
  221. EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
  222. EFI_HANDLE handle;
  223. EFI_STATUS efirc;
  224. /* Attempt loading image */
  225. if ( ( efirc = bs->LoadImage ( FALSE, efi_image_handle, NULL,
  226. user_to_virt ( image->data, 0 ),
  227. image->len, &handle ) ) != 0 ) {
  228. /* Not an EFI image */
  229. DBGC ( image, "EFIIMAGE %p could not load: %s\n",
  230. image, efi_strerror ( efirc ) );
  231. return -ENOEXEC;
  232. }
  233. /* Unload the image. We can't leave it loaded, because we
  234. * have no "unload" operation.
  235. */
  236. bs->UnloadImage ( handle );
  237. return 0;
  238. }
  239. /** EFI image type */
  240. struct image_type efi_image_type __image_type ( PROBE_NORMAL ) = {
  241. .name = "EFI",
  242. .probe = efi_image_probe,
  243. .exec = efi_image_exec,
  244. };