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_download.c 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /*
  2. * Copyright (C) 2010 VMware, Inc. All Rights Reserved.
  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 St, Fifth Floor, Boston, MA 02110-1301 USA.
  17. */
  18. FILE_LICENCE ( GPL2_OR_LATER );
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <ipxe/open.h>
  22. #include <ipxe/process.h>
  23. #include <ipxe/iobuf.h>
  24. #include <ipxe/xfer.h>
  25. #include <ipxe/efi/efi.h>
  26. #include <ipxe/efi/ipxe_download.h>
  27. /** iPXE download protocol GUID */
  28. static EFI_GUID ipxe_download_protocol_guid
  29. = IPXE_DOWNLOAD_PROTOCOL_GUID;
  30. /** A single in-progress file */
  31. struct efi_download_file {
  32. /** Data transfer interface that provides downloaded data */
  33. struct interface xfer;
  34. /** Current file position */
  35. size_t pos;
  36. /** Data callback */
  37. IPXE_DOWNLOAD_DATA_CALLBACK data_callback;
  38. /** Finish callback */
  39. IPXE_DOWNLOAD_FINISH_CALLBACK finish_callback;
  40. /** Callback context */
  41. void *context;
  42. };
  43. /* xfer interface */
  44. /**
  45. * Transfer finished or was aborted
  46. *
  47. * @v file Data transfer file
  48. * @v rc Reason for close
  49. */
  50. static void efi_download_close ( struct efi_download_file *file, int rc ) {
  51. file->finish_callback ( file->context, RC_TO_EFIRC ( rc ) );
  52. intf_shutdown ( &file->xfer, rc );
  53. }
  54. /**
  55. * Process received data
  56. *
  57. * @v file Data transfer file
  58. * @v iobuf I/O buffer
  59. * @v meta Data transfer metadata
  60. * @ret rc Return status code
  61. */
  62. static int efi_download_deliver_iob ( struct efi_download_file *file,
  63. struct io_buffer *iobuf,
  64. struct xfer_metadata *meta ) {
  65. EFI_STATUS efirc;
  66. size_t len = iob_len ( iobuf );
  67. /* Calculate new buffer position */
  68. if ( meta->flags & XFER_FL_ABS_OFFSET )
  69. file->pos = 0;
  70. file->pos += meta->offset;
  71. /* Call out to the data handler */
  72. efirc = file->data_callback ( file->context, iobuf->data,
  73. len, file->pos );
  74. /* Update current buffer position */
  75. file->pos += len;
  76. free_iob ( iobuf );
  77. return EFIRC_TO_RC ( efirc );
  78. }
  79. /** Data transfer interface operations */
  80. static struct interface_operation efi_xfer_operations[] = {
  81. INTF_OP ( xfer_deliver, struct efi_download_file *, efi_download_deliver_iob ),
  82. INTF_OP ( intf_close, struct efi_download_file *, efi_download_close ),
  83. };
  84. /** EFI download data transfer interface descriptor */
  85. static struct interface_descriptor efi_download_file_xfer_desc =
  86. INTF_DESC ( struct efi_download_file, xfer, efi_xfer_operations );
  87. /**
  88. * Start downloading a file, and register callback functions to handle the
  89. * download.
  90. *
  91. * @v This iPXE Download Protocol instance
  92. * @v Url URL to download from
  93. * @v DataCallback Callback that will be invoked when data arrives
  94. * @v FinishCallback Callback that will be invoked when the download ends
  95. * @v Context Context passed to the Data and Finish callbacks
  96. * @v File Token that can be used to abort the download
  97. * @ret Status EFI status code
  98. */
  99. static EFI_STATUS EFIAPI
  100. efi_download_start ( IPXE_DOWNLOAD_PROTOCOL *This __unused,
  101. CHAR8 *Url,
  102. IPXE_DOWNLOAD_DATA_CALLBACK DataCallback,
  103. IPXE_DOWNLOAD_FINISH_CALLBACK FinishCallback,
  104. VOID *Context,
  105. IPXE_DOWNLOAD_FILE *File ) {
  106. struct efi_download_file *file;
  107. int rc;
  108. file = malloc ( sizeof ( struct efi_download_file ) );
  109. if ( file == NULL ) {
  110. return EFI_OUT_OF_RESOURCES;
  111. }
  112. intf_init ( &file->xfer, &efi_download_file_xfer_desc, NULL );
  113. rc = xfer_open ( &file->xfer, LOCATION_URI_STRING, Url );
  114. if ( rc ) {
  115. free ( file );
  116. return RC_TO_EFIRC ( rc );
  117. }
  118. file->pos = 0;
  119. file->data_callback = DataCallback;
  120. file->finish_callback = FinishCallback;
  121. file->context = Context;
  122. *File = file;
  123. return EFI_SUCCESS;
  124. }
  125. /**
  126. * Forcibly abort downloading a file that is currently in progress.
  127. *
  128. * It is not safe to call this function after the Finish callback has executed.
  129. *
  130. * @v This iPXE Download Protocol instance
  131. * @v File Token obtained from Start
  132. * @v Status Reason for aborting the download
  133. * @ret Status EFI status code
  134. */
  135. static EFI_STATUS EFIAPI
  136. efi_download_abort ( IPXE_DOWNLOAD_PROTOCOL *This __unused,
  137. IPXE_DOWNLOAD_FILE File,
  138. EFI_STATUS Status ) {
  139. struct efi_download_file *file = File;
  140. efi_download_close ( file, EFIRC_TO_RC ( Status ) );
  141. return EFI_SUCCESS;
  142. }
  143. /**
  144. * Poll for more data from iPXE. This function will invoke the registered
  145. * callbacks if data is available or if downloads complete.
  146. *
  147. * @v This iPXE Download Protocol instance
  148. * @ret Status EFI status code
  149. */
  150. static EFI_STATUS EFIAPI
  151. efi_download_poll ( IPXE_DOWNLOAD_PROTOCOL *This __unused ) {
  152. step();
  153. return EFI_SUCCESS;
  154. }
  155. /** Publicly exposed iPXE download protocol */
  156. static IPXE_DOWNLOAD_PROTOCOL ipxe_download_protocol_interface = {
  157. .Start = efi_download_start,
  158. .Abort = efi_download_abort,
  159. .Poll = efi_download_poll
  160. };
  161. /**
  162. * Create a new device handle with a iPXE download protocol attached to it.
  163. *
  164. * @v device_handle Newly created device handle (output)
  165. * @ret rc Return status code
  166. */
  167. int efi_download_install ( EFI_HANDLE *device_handle ) {
  168. EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
  169. EFI_STATUS efirc;
  170. EFI_HANDLE handle = NULL;
  171. if (efi_loaded_image->DeviceHandle) { /* TODO: ensure handle is the NIC (maybe efi_image has a better way to indicate the handle doing SNP?) */
  172. handle = efi_loaded_image->DeviceHandle;
  173. }
  174. DBG ( "Installing ipxe protocol interface (%p)... ",
  175. &ipxe_download_protocol_interface );
  176. efirc = bs->InstallMultipleProtocolInterfaces (
  177. &handle,
  178. &ipxe_download_protocol_guid,
  179. &ipxe_download_protocol_interface,
  180. NULL );
  181. if ( efirc ) {
  182. DBG ( "failed (%s)\n", efi_strerror ( efirc ) );
  183. return EFIRC_TO_RC ( efirc );
  184. }
  185. DBG ( "success (%p)\n", handle );
  186. *device_handle = handle;
  187. return 0;
  188. }
  189. /**
  190. * Remove the iPXE download protocol from the given handle, and if nothing
  191. * else is attached, destroy the handle.
  192. *
  193. * @v device_handle EFI device handle to remove from
  194. */
  195. void efi_download_uninstall ( EFI_HANDLE device_handle ) {
  196. EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
  197. bs->UninstallMultipleProtocolInterfaces (
  198. device_handle,
  199. ipxe_download_protocol_guid,
  200. ipxe_download_protocol_interface );
  201. }