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.

downloader.c 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /*
  2. * Copyright (C) 2007 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., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18. FILE_LICENCE ( GPL2_OR_LATER );
  19. #include <stdlib.h>
  20. #include <stdarg.h>
  21. #include <errno.h>
  22. #include <ipxe/iobuf.h>
  23. #include <ipxe/xfer.h>
  24. #include <ipxe/open.h>
  25. #include <ipxe/job.h>
  26. #include <ipxe/uaccess.h>
  27. #include <ipxe/umalloc.h>
  28. #include <ipxe/image.h>
  29. #include <ipxe/downloader.h>
  30. /** @file
  31. *
  32. * Image downloader
  33. *
  34. */
  35. /** A downloader */
  36. struct downloader {
  37. /** Reference count for this object */
  38. struct refcnt refcnt;
  39. /** Job control interface */
  40. struct interface job;
  41. /** Data transfer interface */
  42. struct interface xfer;
  43. /** Image to contain downloaded file */
  44. struct image *image;
  45. /** Current position within image buffer */
  46. size_t pos;
  47. };
  48. /**
  49. * Free downloader object
  50. *
  51. * @v refcnt Downloader reference counter
  52. */
  53. static void downloader_free ( struct refcnt *refcnt ) {
  54. struct downloader *downloader =
  55. container_of ( refcnt, struct downloader, refcnt );
  56. image_put ( downloader->image );
  57. free ( downloader );
  58. }
  59. /**
  60. * Terminate download
  61. *
  62. * @v downloader Downloader
  63. * @v rc Reason for termination
  64. */
  65. static void downloader_finished ( struct downloader *downloader, int rc ) {
  66. /* Shut down interfaces */
  67. intf_shutdown ( &downloader->xfer, rc );
  68. intf_shutdown ( &downloader->job, rc );
  69. }
  70. /**
  71. * Ensure that download buffer is large enough for the specified size
  72. *
  73. * @v downloader Downloader
  74. * @v len Required minimum size
  75. * @ret rc Return status code
  76. */
  77. static int downloader_ensure_size ( struct downloader *downloader,
  78. size_t len ) {
  79. userptr_t new_buffer;
  80. /* If buffer is already large enough, do nothing */
  81. if ( len <= downloader->image->len )
  82. return 0;
  83. DBGC ( downloader, "Downloader %p extending to %zd bytes\n",
  84. downloader, len );
  85. /* Extend buffer */
  86. new_buffer = urealloc ( downloader->image->data, len );
  87. if ( ! new_buffer ) {
  88. DBGC ( downloader, "Downloader %p could not extend buffer to "
  89. "%zd bytes\n", downloader, len );
  90. return -ENOBUFS;
  91. }
  92. downloader->image->data = new_buffer;
  93. downloader->image->len = len;
  94. return 0;
  95. }
  96. /****************************************************************************
  97. *
  98. * Job control interface
  99. *
  100. */
  101. /**
  102. * Report progress of download job
  103. *
  104. * @v downloader Downloader
  105. * @v progress Progress report to fill in
  106. */
  107. static void downloader_progress ( struct downloader *downloader,
  108. struct job_progress *progress ) {
  109. /* This is not entirely accurate, since downloaded data may
  110. * arrive out of order (e.g. with multicast protocols), but
  111. * it's a reasonable first approximation.
  112. */
  113. progress->completed = downloader->pos;
  114. progress->total = downloader->image->len;
  115. }
  116. /****************************************************************************
  117. *
  118. * Data transfer interface
  119. *
  120. */
  121. /**
  122. * Handle received data
  123. *
  124. * @v downloader Downloader
  125. * @v iobuf Datagram I/O buffer
  126. * @v meta Data transfer metadata
  127. * @ret rc Return status code
  128. */
  129. static int downloader_xfer_deliver ( struct downloader *downloader,
  130. struct io_buffer *iobuf,
  131. struct xfer_metadata *meta ) {
  132. size_t len;
  133. size_t max;
  134. int rc;
  135. /* Calculate new buffer position */
  136. if ( meta->flags & XFER_FL_ABS_OFFSET )
  137. downloader->pos = 0;
  138. downloader->pos += meta->offset;
  139. /* Ensure that we have enough buffer space for this data */
  140. len = iob_len ( iobuf );
  141. max = ( downloader->pos + len );
  142. if ( ( rc = downloader_ensure_size ( downloader, max ) ) != 0 )
  143. goto done;
  144. /* Copy data to buffer */
  145. copy_to_user ( downloader->image->data, downloader->pos,
  146. iobuf->data, len );
  147. /* Update current buffer position */
  148. downloader->pos += len;
  149. done:
  150. free_iob ( iobuf );
  151. return rc;
  152. }
  153. /** Downloader data transfer interface operations */
  154. static struct interface_operation downloader_xfer_operations[] = {
  155. INTF_OP ( xfer_deliver, struct downloader *, downloader_xfer_deliver ),
  156. INTF_OP ( intf_close, struct downloader *, downloader_finished ),
  157. };
  158. /** Downloader data transfer interface descriptor */
  159. static struct interface_descriptor downloader_xfer_desc =
  160. INTF_DESC ( struct downloader, xfer, downloader_xfer_operations );
  161. /****************************************************************************
  162. *
  163. * Job control interface
  164. *
  165. */
  166. /** Downloader job control interface operations */
  167. static struct interface_operation downloader_job_op[] = {
  168. INTF_OP ( job_progress, struct downloader *, downloader_progress ),
  169. INTF_OP ( intf_close, struct downloader *, downloader_finished ),
  170. };
  171. /** Downloader job control interface descriptor */
  172. static struct interface_descriptor downloader_job_desc =
  173. INTF_DESC ( struct downloader, job, downloader_job_op );
  174. /****************************************************************************
  175. *
  176. * Instantiator
  177. *
  178. */
  179. /**
  180. * Instantiate a downloader
  181. *
  182. * @v job Job control interface
  183. * @v image Image to fill with downloaded file
  184. * @v type Location type to pass to xfer_open()
  185. * @v ... Remaining arguments to pass to xfer_open()
  186. * @ret rc Return status code
  187. *
  188. * Instantiates a downloader object to download the specified URI into
  189. * the specified image object.
  190. */
  191. int create_downloader ( struct interface *job, struct image *image,
  192. int type, ... ) {
  193. struct downloader *downloader;
  194. va_list args;
  195. int rc;
  196. /* Allocate and initialise structure */
  197. downloader = zalloc ( sizeof ( *downloader ) );
  198. if ( ! downloader )
  199. return -ENOMEM;
  200. ref_init ( &downloader->refcnt, downloader_free );
  201. intf_init ( &downloader->job, &downloader_job_desc,
  202. &downloader->refcnt );
  203. intf_init ( &downloader->xfer, &downloader_xfer_desc,
  204. &downloader->refcnt );
  205. downloader->image = image_get ( image );
  206. va_start ( args, type );
  207. /* Instantiate child objects and attach to our interfaces */
  208. if ( ( rc = xfer_vopen ( &downloader->xfer, type, args ) ) != 0 )
  209. goto err;
  210. /* Attach parent interface, mortalise self, and return */
  211. intf_plug_plug ( &downloader->job, job );
  212. ref_put ( &downloader->refcnt );
  213. va_end ( args );
  214. return 0;
  215. err:
  216. downloader_finished ( downloader, rc );
  217. ref_put ( &downloader->refcnt );
  218. va_end ( args );
  219. return rc;
  220. }