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.1KB

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