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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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/xferbuf.h>
  35. #include <ipxe/downloader.h>
  36. /** @file
  37. *
  38. * Image downloader
  39. *
  40. */
  41. /** A downloader */
  42. struct downloader {
  43. /** Reference count for this object */
  44. struct refcnt refcnt;
  45. /** Job control interface */
  46. struct interface job;
  47. /** Data transfer interface */
  48. struct interface xfer;
  49. /** Image to contain downloaded file */
  50. struct image *image;
  51. /** Data transfer buffer */
  52. struct xfer_buffer buffer;
  53. };
  54. /**
  55. * Free downloader object
  56. *
  57. * @v refcnt Downloader reference counter
  58. */
  59. static void downloader_free ( struct refcnt *refcnt ) {
  60. struct downloader *downloader =
  61. container_of ( refcnt, struct downloader, refcnt );
  62. image_put ( downloader->image );
  63. free ( downloader );
  64. }
  65. /**
  66. * Terminate download
  67. *
  68. * @v downloader Downloader
  69. * @v rc Reason for termination
  70. */
  71. static void downloader_finished ( struct downloader *downloader, int rc ) {
  72. /* Log download status */
  73. if ( rc == 0 ) {
  74. syslog ( LOG_NOTICE, "Downloaded \"%s\"\n",
  75. downloader->image->name );
  76. } else {
  77. syslog ( LOG_ERR, "Download of \"%s\" failed: %s\n",
  78. downloader->image->name, strerror ( rc ) );
  79. }
  80. /* Update image length */
  81. downloader->image->len = downloader->buffer.len;
  82. /* Shut down interfaces */
  83. intf_shutdown ( &downloader->xfer, rc );
  84. intf_shutdown ( &downloader->job, rc );
  85. }
  86. /****************************************************************************
  87. *
  88. * Job control interface
  89. *
  90. */
  91. /**
  92. * Report progress of download job
  93. *
  94. * @v downloader Downloader
  95. * @v progress Progress report to fill in
  96. * @ret ongoing_rc Ongoing job status code (if known)
  97. */
  98. static int downloader_progress ( struct downloader *downloader,
  99. struct job_progress *progress ) {
  100. int rc;
  101. /* Allow data transfer to provide an accurate description */
  102. if ( ( rc = job_progress ( &downloader->xfer, progress ) ) != 0 )
  103. return rc;
  104. /* This is not entirely accurate, since downloaded data may
  105. * arrive out of order (e.g. with multicast protocols), but
  106. * it's a reasonable first approximation.
  107. */
  108. if ( ! progress->total ) {
  109. progress->completed = downloader->buffer.pos;
  110. progress->total = downloader->buffer.len;
  111. }
  112. return 0;
  113. }
  114. /****************************************************************************
  115. *
  116. * Data transfer interface
  117. *
  118. */
  119. /**
  120. * Handle received data
  121. *
  122. * @v downloader Downloader
  123. * @v iobuf Datagram I/O buffer
  124. * @v meta Data transfer metadata
  125. * @ret rc Return status code
  126. */
  127. static int downloader_deliver ( struct downloader *downloader,
  128. struct io_buffer *iobuf,
  129. struct xfer_metadata *meta ) {
  130. int rc;
  131. /* Add data to buffer */
  132. if ( ( rc = xferbuf_deliver ( &downloader->buffer, iob_disown ( iobuf ),
  133. meta ) ) != 0 )
  134. goto err_deliver;
  135. return 0;
  136. err_deliver:
  137. downloader_finished ( downloader, rc );
  138. return rc;
  139. }
  140. /**
  141. * Get underlying data transfer buffer
  142. *
  143. * @v downloader Downloader
  144. * @ret xferbuf Data transfer buffer, or NULL on error
  145. */
  146. static struct xfer_buffer *
  147. downloader_buffer ( struct downloader *downloader ) {
  148. /* Provide direct access to underlying data transfer buffer */
  149. return &downloader->buffer;
  150. }
  151. /**
  152. * Redirect data transfer interface
  153. *
  154. * @v downloader Downloader
  155. * @v type New location type
  156. * @v args Remaining arguments depend upon location type
  157. * @ret rc Return status code
  158. */
  159. static int downloader_vredirect ( struct downloader *downloader, int type,
  160. va_list args ) {
  161. va_list tmp;
  162. struct uri *uri;
  163. int rc;
  164. /* Intercept redirects to a LOCATION_URI and update the image URI */
  165. if ( type == LOCATION_URI ) {
  166. /* Extract URI argument */
  167. va_copy ( tmp, args );
  168. uri = va_arg ( tmp, struct uri * );
  169. va_end ( tmp );
  170. /* Set image URI */
  171. if ( ( rc = image_set_uri ( downloader->image, uri ) ) != 0 )
  172. goto err;
  173. }
  174. /* Redirect to new location */
  175. if ( ( rc = xfer_vreopen ( &downloader->xfer, type, args ) ) != 0 )
  176. goto err;
  177. return 0;
  178. err:
  179. downloader_finished ( downloader, rc );
  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_deliver ),
  185. INTF_OP ( xfer_buffer, struct downloader *, downloader_buffer ),
  186. INTF_OP ( xfer_vredirect, struct downloader *, downloader_vredirect ),
  187. INTF_OP ( intf_close, struct downloader *, downloader_finished ),
  188. };
  189. /** Downloader data transfer interface descriptor */
  190. static struct interface_descriptor downloader_xfer_desc =
  191. INTF_DESC ( struct downloader, xfer, downloader_xfer_operations );
  192. /****************************************************************************
  193. *
  194. * Job control interface
  195. *
  196. */
  197. /** Downloader job control interface operations */
  198. static struct interface_operation downloader_job_op[] = {
  199. INTF_OP ( job_progress, struct downloader *, downloader_progress ),
  200. INTF_OP ( intf_close, struct downloader *, downloader_finished ),
  201. };
  202. /** Downloader job control interface descriptor */
  203. static struct interface_descriptor downloader_job_desc =
  204. INTF_DESC ( struct downloader, job, downloader_job_op );
  205. /****************************************************************************
  206. *
  207. * Instantiator
  208. *
  209. */
  210. /**
  211. * Instantiate a downloader
  212. *
  213. * @v job Job control interface
  214. * @v image Image to fill with downloaded file
  215. * @ret rc Return status code
  216. *
  217. * Instantiates a downloader object to download the content of the
  218. * specified image from its URI.
  219. */
  220. int create_downloader ( struct interface *job, struct image *image ) {
  221. struct downloader *downloader;
  222. int rc;
  223. /* Allocate and initialise structure */
  224. downloader = zalloc ( sizeof ( *downloader ) );
  225. if ( ! downloader )
  226. return -ENOMEM;
  227. ref_init ( &downloader->refcnt, downloader_free );
  228. intf_init ( &downloader->job, &downloader_job_desc,
  229. &downloader->refcnt );
  230. intf_init ( &downloader->xfer, &downloader_xfer_desc,
  231. &downloader->refcnt );
  232. downloader->image = image_get ( image );
  233. xferbuf_umalloc_init ( &downloader->buffer, &image->data );
  234. /* Instantiate child objects and attach to our interfaces */
  235. if ( ( rc = xfer_open_uri ( &downloader->xfer, image->uri ) ) != 0 )
  236. goto err;
  237. /* Attach parent interface, mortalise self, and return */
  238. intf_plug_plug ( &downloader->job, job );
  239. ref_put ( &downloader->refcnt );
  240. return 0;
  241. err:
  242. downloader_finished ( downloader, rc );
  243. ref_put ( &downloader->refcnt );
  244. return rc;
  245. }