Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

efi_image.c 9.6KB

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