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

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