Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

efi_image.c 8.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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. /* Disambiguate the various error causes */
  35. #define EINFO_EEFI_LOAD \
  36. __einfo_uniqify ( EINFO_EPLATFORM, 0x01, \
  37. "Could not load image" )
  38. #define EINFO_EEFI_LOAD_PROHIBITED \
  39. __einfo_platformify ( EINFO_EEFI_LOAD, EFI_SECURITY_VIOLATION, \
  40. "Image prohibited by security policy" )
  41. #define EEFI_LOAD_PROHIBITED \
  42. __einfo_error ( EINFO_EEFI_LOAD_PROHIBITED )
  43. #define EEFI_LOAD( efirc ) EPLATFORM ( EINFO_EEFI_LOAD, efirc, \
  44. EEFI_LOAD_PROHIBITED )
  45. #define EINFO_EEFI_START \
  46. __einfo_uniqify ( EINFO_EPLATFORM, 0x02, \
  47. "Could not start image" )
  48. #define EEFI_START( efirc ) EPLATFORM ( EINFO_EEFI_START, efirc )
  49. /** EFI loaded image protocol GUID */
  50. static EFI_GUID efi_loaded_image_protocol_guid =
  51. EFI_LOADED_IMAGE_PROTOCOL_GUID;
  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. end = efi_devpath_end ( parent );
  72. prefix_len = ( ( void * ) end - ( void * ) parent );
  73. name_len = strlen ( image->name );
  74. filepath_len = ( SIZE_OF_FILEPATH_DEVICE_PATH +
  75. ( name_len + 1 /* NUL */ ) * sizeof ( wchar_t ) );
  76. len = ( prefix_len + filepath_len + sizeof ( *end ) );
  77. /* Allocate device path */
  78. path = zalloc ( len );
  79. if ( ! path )
  80. return NULL;
  81. /* Construct device path */
  82. memcpy ( path, parent, prefix_len );
  83. filepath = ( ( ( void * ) path ) + prefix_len );
  84. filepath->Header.Type = MEDIA_DEVICE_PATH;
  85. filepath->Header.SubType = MEDIA_FILEPATH_DP;
  86. filepath->Header.Length[0] = ( filepath_len & 0xff );
  87. filepath->Header.Length[1] = ( filepath_len >> 8 );
  88. efi_snprintf ( filepath->PathName, ( name_len + 1 /* NUL */ ),
  89. "%s", image->name );
  90. end = ( ( ( void * ) filepath ) + filepath_len );
  91. end->Type = END_DEVICE_PATH_TYPE;
  92. end->SubType = END_ENTIRE_DEVICE_PATH_SUBTYPE;
  93. end->Length[0] = sizeof ( *end );
  94. return path;
  95. }
  96. /**
  97. * Create command line for image
  98. *
  99. * @v image EFI image
  100. * @ret cmdline Command line, or NULL on failure
  101. */
  102. static wchar_t * efi_image_cmdline ( struct image *image ) {
  103. wchar_t *cmdline;
  104. size_t len;
  105. len = ( strlen ( image->name ) +
  106. ( image->cmdline ?
  107. ( 1 /* " " */ + strlen ( image->cmdline ) ) : 0 ) );
  108. cmdline = zalloc ( ( len + 1 /* NUL */ ) * sizeof ( wchar_t ) );
  109. if ( ! cmdline )
  110. return NULL;
  111. efi_snprintf ( cmdline, ( len + 1 /* NUL */ ), "%s%s%s",
  112. image->name,
  113. ( image->cmdline ? " " : "" ),
  114. ( image->cmdline ? image->cmdline : "" ) );
  115. return cmdline;
  116. }
  117. /**
  118. * Execute EFI image
  119. *
  120. * @v image EFI image
  121. * @ret rc Return status code
  122. */
  123. static int efi_image_exec ( struct image *image ) {
  124. EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
  125. struct efi_snp_device *snpdev;
  126. EFI_DEVICE_PATH_PROTOCOL *path;
  127. union {
  128. EFI_LOADED_IMAGE_PROTOCOL *image;
  129. void *interface;
  130. } loaded;
  131. EFI_HANDLE handle;
  132. wchar_t *cmdline;
  133. EFI_STATUS efirc;
  134. int rc;
  135. /* Find an appropriate device handle to use */
  136. snpdev = last_opened_snpdev();
  137. if ( ! snpdev ) {
  138. DBGC ( image, "EFIIMAGE %p could not identify SNP device\n",
  139. image );
  140. rc = -ENODEV;
  141. goto err_no_snpdev;
  142. }
  143. /* Install file I/O protocols */
  144. if ( ( rc = efi_file_install ( &snpdev->handle ) ) != 0 ) {
  145. DBGC ( image, "EFIIMAGE %p could not install file protocol: "
  146. "%s\n", image, strerror ( rc ) );
  147. goto err_file_install;
  148. }
  149. /* Install iPXE download protocol */
  150. if ( ( rc = efi_download_install ( &snpdev->handle ) ) != 0 ) {
  151. DBGC ( image, "EFIIMAGE %p could not install iPXE download "
  152. "protocol: %s\n", image, strerror ( rc ) );
  153. goto err_download_install;
  154. }
  155. /* Create device path for image */
  156. path = efi_image_path ( image, &snpdev->path );
  157. if ( ! path ) {
  158. DBGC ( image, "EFIIMAGE %p could not create device path\n",
  159. image );
  160. rc = -ENOMEM;
  161. goto err_image_path;
  162. }
  163. /* Create command line for image */
  164. cmdline = efi_image_cmdline ( image );
  165. if ( ! cmdline ) {
  166. DBGC ( image, "EFIIMAGE %p could not create command line\n",
  167. image );
  168. rc = -ENOMEM;
  169. goto err_cmdline;
  170. }
  171. /* Attempt loading image */
  172. if ( ( efirc = bs->LoadImage ( FALSE, efi_image_handle, path,
  173. user_to_virt ( image->data, 0 ),
  174. image->len, &handle ) ) != 0 ) {
  175. /* Not an EFI image */
  176. rc = -EEFI_LOAD ( efirc );
  177. DBGC ( image, "EFIIMAGE %p could not load: %s\n",
  178. image, strerror ( rc ) );
  179. goto err_load_image;
  180. }
  181. /* Get the loaded image protocol for the newly loaded image */
  182. efirc = bs->OpenProtocol ( handle, &efi_loaded_image_protocol_guid,
  183. &loaded.interface, efi_image_handle,
  184. NULL, EFI_OPEN_PROTOCOL_GET_PROTOCOL );
  185. if ( efirc ) {
  186. /* Should never happen */
  187. rc = -EEFI ( efirc );
  188. goto err_open_protocol;
  189. }
  190. /* Sanity checks */
  191. assert ( loaded.image->ParentHandle == efi_image_handle );
  192. assert ( loaded.image->DeviceHandle == snpdev->handle );
  193. assert ( loaded.image->LoadOptionsSize == 0 );
  194. assert ( loaded.image->LoadOptions == NULL );
  195. /* Set command line */
  196. loaded.image->LoadOptions = cmdline;
  197. loaded.image->LoadOptionsSize =
  198. ( ( wcslen ( cmdline ) + 1 /* NUL */ ) * sizeof ( wchar_t ) );
  199. /* Start the image */
  200. if ( ( efirc = bs->StartImage ( handle, NULL, NULL ) ) != 0 ) {
  201. rc = -EEFI_START ( efirc );
  202. DBGC ( image, "EFIIMAGE %p returned with status %s\n",
  203. image, strerror ( rc ) );
  204. goto err_start_image;
  205. }
  206. /* Success */
  207. rc = 0;
  208. err_start_image:
  209. err_open_protocol:
  210. /* Unload the image. We can't leave it loaded, because we
  211. * have no "unload" operation.
  212. */
  213. if ( ( efirc = bs->UnloadImage ( handle ) ) != 0 ) {
  214. rc = -EEFI ( efirc );
  215. DBGC ( image, "EFIIMAGE %p could not unload: %s\n",
  216. image, strerror ( rc ) );
  217. }
  218. err_load_image:
  219. free ( cmdline );
  220. err_cmdline:
  221. free ( path );
  222. err_image_path:
  223. efi_download_uninstall ( snpdev->handle );
  224. err_download_install:
  225. efi_file_uninstall ( snpdev->handle );
  226. err_file_install:
  227. err_no_snpdev:
  228. return rc;
  229. }
  230. /**
  231. * Probe EFI image
  232. *
  233. * @v image EFI file
  234. * @ret rc Return status code
  235. */
  236. static int efi_image_probe ( struct image *image ) {
  237. EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
  238. EFI_HANDLE handle;
  239. EFI_STATUS efirc;
  240. int rc;
  241. /* Attempt loading image */
  242. if ( ( efirc = bs->LoadImage ( FALSE, efi_image_handle, NULL,
  243. user_to_virt ( image->data, 0 ),
  244. image->len, &handle ) ) != 0 ) {
  245. /* Not an EFI image */
  246. rc = -EEFI_LOAD ( efirc );
  247. DBGC ( image, "EFIIMAGE %p could not load: %s\n",
  248. image, strerror ( rc ) );
  249. return rc;
  250. }
  251. /* Unload the image. We can't leave it loaded, because we
  252. * have no "unload" operation.
  253. */
  254. bs->UnloadImage ( handle );
  255. return 0;
  256. }
  257. /** EFI image type */
  258. struct image_type efi_image_type __image_type ( PROBE_NORMAL ) = {
  259. .name = "EFI",
  260. .probe = efi_image_probe,
  261. .exec = efi_image_exec,
  262. };