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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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/efi/efi_wrap.h>
  30. #include <ipxe/image.h>
  31. #include <ipxe/init.h>
  32. #include <ipxe/features.h>
  33. #include <ipxe/uri.h>
  34. FEATURE ( FEATURE_IMAGE, "EFI", DHCP_EB_FEATURE_EFI, 1 );
  35. /* Disambiguate the various error causes */
  36. #define EINFO_EEFI_LOAD \
  37. __einfo_uniqify ( EINFO_EPLATFORM, 0x01, \
  38. "Could not load image" )
  39. #define EINFO_EEFI_LOAD_PROHIBITED \
  40. __einfo_platformify ( EINFO_EEFI_LOAD, EFI_SECURITY_VIOLATION, \
  41. "Image prohibited by security policy" )
  42. #define EEFI_LOAD_PROHIBITED \
  43. __einfo_error ( EINFO_EEFI_LOAD_PROHIBITED )
  44. #define EEFI_LOAD( efirc ) EPLATFORM ( EINFO_EEFI_LOAD, efirc, \
  45. EEFI_LOAD_PROHIBITED )
  46. #define EINFO_EEFI_START \
  47. __einfo_uniqify ( EINFO_EPLATFORM, 0x02, \
  48. "Could not start image" )
  49. #define EEFI_START( efirc ) EPLATFORM ( EINFO_EEFI_START, efirc )
  50. /** EFI loaded image protocol GUID */
  51. static EFI_GUID efi_loaded_image_protocol_guid =
  52. EFI_LOADED_IMAGE_PROTOCOL_GUID;
  53. /**
  54. * Create device path for image
  55. *
  56. * @v image EFI image
  57. * @v parent Parent device path
  58. * @ret path Device path, or NULL on failure
  59. *
  60. * The caller must eventually free() the device path.
  61. */
  62. static EFI_DEVICE_PATH_PROTOCOL *
  63. efi_image_path ( struct image *image, EFI_DEVICE_PATH_PROTOCOL *parent ) {
  64. EFI_DEVICE_PATH_PROTOCOL *path;
  65. FILEPATH_DEVICE_PATH *filepath;
  66. EFI_DEVICE_PATH_PROTOCOL *end;
  67. size_t name_len;
  68. size_t prefix_len;
  69. size_t filepath_len;
  70. size_t len;
  71. /* Calculate device path lengths */
  72. end = efi_devpath_end ( parent );
  73. prefix_len = ( ( void * ) end - ( void * ) parent );
  74. name_len = strlen ( image->name );
  75. filepath_len = ( SIZE_OF_FILEPATH_DEVICE_PATH +
  76. ( name_len + 1 /* NUL */ ) * sizeof ( wchar_t ) );
  77. len = ( prefix_len + filepath_len + sizeof ( *end ) );
  78. /* Allocate device path */
  79. path = zalloc ( len );
  80. if ( ! path )
  81. return NULL;
  82. /* Construct device path */
  83. memcpy ( path, parent, prefix_len );
  84. filepath = ( ( ( void * ) path ) + prefix_len );
  85. filepath->Header.Type = MEDIA_DEVICE_PATH;
  86. filepath->Header.SubType = MEDIA_FILEPATH_DP;
  87. filepath->Header.Length[0] = ( filepath_len & 0xff );
  88. filepath->Header.Length[1] = ( filepath_len >> 8 );
  89. efi_snprintf ( filepath->PathName, ( name_len + 1 /* NUL */ ),
  90. "%s", image->name );
  91. end = ( ( ( void * ) filepath ) + filepath_len );
  92. end->Type = END_DEVICE_PATH_TYPE;
  93. end->SubType = END_ENTIRE_DEVICE_PATH_SUBTYPE;
  94. end->Length[0] = sizeof ( *end );
  95. return path;
  96. }
  97. /**
  98. * Create command line for image
  99. *
  100. * @v image EFI image
  101. * @ret cmdline Command line, or NULL on failure
  102. */
  103. static wchar_t * efi_image_cmdline ( struct image *image ) {
  104. wchar_t *cmdline;
  105. size_t len;
  106. len = ( strlen ( image->name ) +
  107. ( image->cmdline ?
  108. ( 1 /* " " */ + strlen ( image->cmdline ) ) : 0 ) );
  109. cmdline = zalloc ( ( len + 1 /* NUL */ ) * sizeof ( wchar_t ) );
  110. if ( ! cmdline )
  111. return NULL;
  112. efi_snprintf ( cmdline, ( len + 1 /* NUL */ ), "%s%s%s",
  113. image->name,
  114. ( image->cmdline ? " " : "" ),
  115. ( image->cmdline ? image->cmdline : "" ) );
  116. return cmdline;
  117. }
  118. /**
  119. * Execute EFI image
  120. *
  121. * @v image EFI image
  122. * @ret rc Return status code
  123. */
  124. static int efi_image_exec ( struct image *image ) {
  125. EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
  126. struct efi_snp_device *snpdev;
  127. EFI_DEVICE_PATH_PROTOCOL *path;
  128. union {
  129. EFI_LOADED_IMAGE_PROTOCOL *image;
  130. void *interface;
  131. } loaded;
  132. EFI_HANDLE handle;
  133. wchar_t *cmdline;
  134. EFI_STATUS efirc;
  135. int rc;
  136. /* Find an appropriate device handle to use */
  137. snpdev = last_opened_snpdev();
  138. if ( ! snpdev ) {
  139. DBGC ( image, "EFIIMAGE %p could not identify SNP device\n",
  140. image );
  141. rc = -ENODEV;
  142. goto err_no_snpdev;
  143. }
  144. /* Install file I/O protocols */
  145. if ( ( rc = efi_file_install ( &snpdev->handle ) ) != 0 ) {
  146. DBGC ( image, "EFIIMAGE %p could not install file protocol: "
  147. "%s\n", image, strerror ( rc ) );
  148. goto err_file_install;
  149. }
  150. /* Install iPXE download protocol */
  151. if ( ( rc = efi_download_install ( &snpdev->handle ) ) != 0 ) {
  152. DBGC ( image, "EFIIMAGE %p could not install iPXE download "
  153. "protocol: %s\n", image, strerror ( rc ) );
  154. goto err_download_install;
  155. }
  156. /* Create device path for image */
  157. path = efi_image_path ( image, &snpdev->path );
  158. if ( ! path ) {
  159. DBGC ( image, "EFIIMAGE %p could not create device path\n",
  160. image );
  161. rc = -ENOMEM;
  162. goto err_image_path;
  163. }
  164. /* Create command line for image */
  165. cmdline = efi_image_cmdline ( image );
  166. if ( ! cmdline ) {
  167. DBGC ( image, "EFIIMAGE %p could not create command line\n",
  168. image );
  169. rc = -ENOMEM;
  170. goto err_cmdline;
  171. }
  172. /* Attempt loading image */
  173. if ( ( efirc = bs->LoadImage ( FALSE, efi_image_handle, path,
  174. user_to_virt ( image->data, 0 ),
  175. image->len, &handle ) ) != 0 ) {
  176. /* Not an EFI image */
  177. rc = -EEFI_LOAD ( efirc );
  178. DBGC ( image, "EFIIMAGE %p could not load: %s\n",
  179. image, strerror ( rc ) );
  180. goto err_load_image;
  181. }
  182. /* Get the loaded image protocol for the newly loaded image */
  183. efirc = bs->OpenProtocol ( handle, &efi_loaded_image_protocol_guid,
  184. &loaded.interface, efi_image_handle,
  185. NULL, EFI_OPEN_PROTOCOL_GET_PROTOCOL );
  186. if ( efirc ) {
  187. /* Should never happen */
  188. rc = -EEFI ( efirc );
  189. goto err_open_protocol;
  190. }
  191. /* Sanity checks */
  192. assert ( loaded.image->ParentHandle == efi_image_handle );
  193. assert ( loaded.image->DeviceHandle == snpdev->handle );
  194. assert ( loaded.image->LoadOptionsSize == 0 );
  195. assert ( loaded.image->LoadOptions == NULL );
  196. /* Set command line */
  197. loaded.image->LoadOptions = cmdline;
  198. loaded.image->LoadOptionsSize =
  199. ( ( wcslen ( cmdline ) + 1 /* NUL */ ) * sizeof ( wchar_t ) );
  200. /* Release network devices for use via SNP */
  201. efi_snp_release();
  202. /* Wrap calls made by the loaded image (for debugging) */
  203. efi_wrap ( handle, loaded.image );
  204. /* Start the image */
  205. if ( ( efirc = bs->StartImage ( handle, NULL, NULL ) ) != 0 ) {
  206. rc = -EEFI_START ( efirc );
  207. DBGC ( image, "EFIIMAGE %p could not start (or returned with "
  208. "error): %s\n", image, strerror ( rc ) );
  209. goto err_start_image;
  210. }
  211. /* Success */
  212. rc = 0;
  213. err_start_image:
  214. efi_snp_claim();
  215. err_open_protocol:
  216. /* If there was no error, then the image must have been
  217. * started and returned successfully. It either unloaded
  218. * itself, or it intended to remain loaded (e.g. it was a
  219. * driver). We therefore do not unload successful images.
  220. *
  221. * If there was an error, attempt to unload the image. This
  222. * may not work. In particular, there is no way to tell
  223. * whether an error returned from StartImage() was due to
  224. * being unable to start the image (in which case we probably
  225. * should call UnloadImage()), or due to the image itself
  226. * returning an error (in which case we probably should not
  227. * call UnloadImage()). We therefore ignore any failures from
  228. * the UnloadImage() call itself.
  229. */
  230. if ( rc != 0 )
  231. bs->UnloadImage ( handle );
  232. err_load_image:
  233. free ( cmdline );
  234. err_cmdline:
  235. free ( path );
  236. err_image_path:
  237. efi_download_uninstall ( snpdev->handle );
  238. err_download_install:
  239. efi_file_uninstall ( snpdev->handle );
  240. err_file_install:
  241. err_no_snpdev:
  242. return rc;
  243. }
  244. /**
  245. * Probe EFI image
  246. *
  247. * @v image EFI file
  248. * @ret rc Return status code
  249. */
  250. static int efi_image_probe ( struct image *image ) {
  251. EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
  252. EFI_HANDLE handle;
  253. EFI_STATUS efirc;
  254. int rc;
  255. /* Attempt loading image */
  256. if ( ( efirc = bs->LoadImage ( FALSE, efi_image_handle, NULL,
  257. user_to_virt ( image->data, 0 ),
  258. image->len, &handle ) ) != 0 ) {
  259. /* Not an EFI image */
  260. rc = -EEFI_LOAD ( efirc );
  261. DBGC ( image, "EFIIMAGE %p could not load: %s\n",
  262. image, strerror ( rc ) );
  263. return rc;
  264. }
  265. /* Unload the image. We can't leave it loaded, because we
  266. * have no "unload" operation.
  267. */
  268. bs->UnloadImage ( handle );
  269. return 0;
  270. }
  271. /** EFI image type */
  272. struct image_type efi_image_type __image_type ( PROBE_NORMAL ) = {
  273. .name = "EFI",
  274. .probe = efi_image_probe,
  275. .exec = efi_image_exec,
  276. };