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 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301, USA.
  18. *
  19. * You can also choose to distribute this program under the terms of
  20. * the Unmodified Binary Distribution Licence (as given in the file
  21. * COPYING.UBDL), provided that you have satisfied its requirements.
  22. */
  23. FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
  24. #include <stdlib.h>
  25. #include <errno.h>
  26. #include <syslog.h>
  27. #include <ipxe/iobuf.h>
  28. #include <ipxe/xfer.h>
  29. #include <ipxe/open.h>
  30. #include <ipxe/job.h>
  31. #include <ipxe/uaccess.h>
  32. #include <ipxe/umalloc.h>
  33. #include <ipxe/image.h>
  34. #include <ipxe/profile.h>
  35. #include <ipxe/downloader.h>
  36. /** @file
  37. *
  38. * Image downloader
  39. *
  40. */
  41. /** Receive profiler */
  42. static struct profiler downloader_rx_profiler __profiler =
  43. { .name = "downloader.rx" };
  44. /** Data copy profiler */
  45. static struct profiler downloader_copy_profiler __profiler =
  46. { .name = "downloader.copy" };
  47. /** A downloader */
  48. struct downloader {
  49. /** Reference count for this object */
  50. struct refcnt refcnt;
  51. /** Job control interface */
  52. struct interface job;
  53. /** Data transfer interface */
  54. struct interface xfer;
  55. /** Image to contain downloaded file */
  56. struct image *image;
  57. /** Current position within image buffer */
  58. size_t pos;
  59. };
  60. /**
  61. * Free downloader object
  62. *
  63. * @v refcnt Downloader reference counter
  64. */
  65. static void downloader_free ( struct refcnt *refcnt ) {
  66. struct downloader *downloader =
  67. container_of ( refcnt, struct downloader, refcnt );
  68. image_put ( downloader->image );
  69. free ( downloader );
  70. }
  71. /**
  72. * Terminate download
  73. *
  74. * @v downloader Downloader
  75. * @v rc Reason for termination
  76. */
  77. static void downloader_finished ( struct downloader *downloader, int rc ) {
  78. /* Log download status */
  79. if ( rc == 0 ) {
  80. syslog ( LOG_NOTICE, "Downloaded \"%s\"\n",
  81. downloader->image->name );
  82. } else {
  83. syslog ( LOG_ERR, "Download of \"%s\" failed: %s\n",
  84. downloader->image->name, strerror ( rc ) );
  85. }
  86. /* Shut down interfaces */
  87. intf_shutdown ( &downloader->xfer, rc );
  88. intf_shutdown ( &downloader->job, rc );
  89. }
  90. /**
  91. * Ensure that download buffer is large enough for the specified size
  92. *
  93. * @v downloader Downloader
  94. * @v len Required minimum size
  95. * @ret rc Return status code
  96. */
  97. static int downloader_ensure_size ( struct downloader *downloader,
  98. size_t len ) {
  99. userptr_t new_buffer;
  100. /* If buffer is already large enough, do nothing */
  101. if ( len <= downloader->image->len )
  102. return 0;
  103. DBGC ( downloader, "Downloader %p extending to %zd bytes\n",
  104. downloader, len );
  105. /* Extend buffer */
  106. new_buffer = urealloc ( downloader->image->data, len );
  107. if ( ! new_buffer ) {
  108. DBGC ( downloader, "Downloader %p could not extend buffer to "
  109. "%zd bytes\n", downloader, len );
  110. return -ENOSPC;
  111. }
  112. downloader->image->data = new_buffer;
  113. downloader->image->len = len;
  114. return 0;
  115. }
  116. /****************************************************************************
  117. *
  118. * Job control interface
  119. *
  120. */
  121. /**
  122. * Report progress of download job
  123. *
  124. * @v downloader Downloader
  125. * @v progress Progress report to fill in
  126. * @ret ongoing_rc Ongoing job status code (if known)
  127. */
  128. static int downloader_progress ( struct downloader *downloader,
  129. struct job_progress *progress ) {
  130. /* This is not entirely accurate, since downloaded data may
  131. * arrive out of order (e.g. with multicast protocols), but
  132. * it's a reasonable first approximation.
  133. */
  134. progress->completed = downloader->pos;
  135. progress->total = downloader->image->len;
  136. return 0;
  137. }
  138. /****************************************************************************
  139. *
  140. * Data transfer interface
  141. *
  142. */
  143. /**
  144. * Handle received data
  145. *
  146. * @v downloader Downloader
  147. * @v iobuf Datagram I/O buffer
  148. * @v meta Data transfer metadata
  149. * @ret rc Return status code
  150. */
  151. static int downloader_xfer_deliver ( struct downloader *downloader,
  152. struct io_buffer *iobuf,
  153. struct xfer_metadata *meta ) {
  154. size_t len;
  155. size_t max;
  156. int rc;
  157. /* Start profiling */
  158. profile_start ( &downloader_rx_profiler );
  159. /* Calculate new buffer position */
  160. if ( meta->flags & XFER_FL_ABS_OFFSET )
  161. downloader->pos = 0;
  162. downloader->pos += meta->offset;
  163. /* Ensure that we have enough buffer space for this data */
  164. len = iob_len ( iobuf );
  165. max = ( downloader->pos + len );
  166. if ( ( rc = downloader_ensure_size ( downloader, max ) ) != 0 )
  167. goto done;
  168. /* Copy data to buffer */
  169. profile_start ( &downloader_copy_profiler );
  170. copy_to_user ( downloader->image->data, downloader->pos,
  171. iobuf->data, len );
  172. profile_stop ( &downloader_copy_profiler );
  173. /* Update current buffer position */
  174. downloader->pos += len;
  175. done:
  176. free_iob ( iobuf );
  177. if ( rc != 0 )
  178. downloader_finished ( downloader, rc );
  179. profile_stop ( &downloader_rx_profiler );
  180. return rc;
  181. }
  182. /** Downloader data transfer interface operations */
  183. static struct interface_operation downloader_xfer_operations[] = {
  184. INTF_OP ( xfer_deliver, struct downloader *, downloader_xfer_deliver ),
  185. INTF_OP ( intf_close, struct downloader *, downloader_finished ),
  186. };
  187. /** Downloader data transfer interface descriptor */
  188. static struct interface_descriptor downloader_xfer_desc =
  189. INTF_DESC ( struct downloader, xfer, downloader_xfer_operations );
  190. /****************************************************************************
  191. *
  192. * Job control interface
  193. *
  194. */
  195. /** Downloader job control interface operations */
  196. static struct interface_operation downloader_job_op[] = {
  197. INTF_OP ( job_progress, struct downloader *, downloader_progress ),
  198. INTF_OP ( intf_close, struct downloader *, downloader_finished ),
  199. };
  200. /** Downloader job control interface descriptor */
  201. static struct interface_descriptor downloader_job_desc =
  202. INTF_DESC ( struct downloader, job, downloader_job_op );
  203. /****************************************************************************
  204. *
  205. * Instantiator
  206. *
  207. */
  208. /**
  209. * Instantiate a downloader
  210. *
  211. * @v job Job control interface
  212. * @v image Image to fill with downloaded file
  213. * @ret rc Return status code
  214. *
  215. * Instantiates a downloader object to download the content of the
  216. * specified image from its URI.
  217. */
  218. int create_downloader ( struct interface *job, struct image *image ) {
  219. struct downloader *downloader;
  220. int rc;
  221. /* Allocate and initialise structure */
  222. downloader = zalloc ( sizeof ( *downloader ) );
  223. if ( ! downloader )
  224. return -ENOMEM;
  225. ref_init ( &downloader->refcnt, downloader_free );
  226. intf_init ( &downloader->job, &downloader_job_desc,
  227. &downloader->refcnt );
  228. intf_init ( &downloader->xfer, &downloader_xfer_desc,
  229. &downloader->refcnt );
  230. downloader->image = image_get ( image );
  231. /* Instantiate child objects and attach to our interfaces */
  232. if ( ( rc = xfer_open_uri ( &downloader->xfer, image->uri ) ) != 0 )
  233. goto err;
  234. /* Attach parent interface, mortalise self, and return */
  235. intf_plug_plug ( &downloader->job, job );
  236. ref_put ( &downloader->refcnt );
  237. return 0;
  238. err:
  239. downloader_finished ( downloader, rc );
  240. ref_put ( &downloader->refcnt );
  241. return rc;
  242. }