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

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