Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

efi_download.c 6.2KB

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