iPXE
downloader.c File Reference

Image downloader. More...

#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <syslog.h>
#include <ipxe/iobuf.h>
#include <ipxe/xfer.h>
#include <ipxe/open.h>
#include <ipxe/job.h>
#include <ipxe/umalloc.h>
#include <ipxe/image.h>
#include <ipxe/xferbuf.h>
#include <ipxe/downloader.h>

Go to the source code of this file.

Data Structures

struct  downloader
 A downloader. More...

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
 FILE_SECBOOT (PERMITTED)
static void downloader_free (struct refcnt *refcnt)
 Free downloader object.
static void downloader_finished (struct downloader *downloader, int rc)
 Terminate download.
static int downloader_progress (struct downloader *downloader, struct job_progress *progress)
 Report progress of download job.
static int downloader_deliver (struct downloader *downloader, struct io_buffer *iobuf, struct xfer_metadata *meta)
 Handle received data.
static struct xfer_bufferdownloader_buffer (struct downloader *downloader)
 Get underlying data transfer buffer.
static int downloader_vredirect (struct downloader *downloader, int type, va_list args)
 Redirect data transfer interface.
int create_downloader (struct interface *job, struct image *image)
 Instantiate a downloader.

Variables

static struct interface_operation downloader_xfer_operations []
 Downloader data transfer interface operations.
static struct interface_descriptor downloader_xfer_desc
 Downloader data transfer interface descriptor.
static struct interface_operation downloader_job_op []
 Downloader job control interface operations.
static struct interface_descriptor downloader_job_desc
 Downloader job control interface descriptor.

Detailed Description

Image downloader.

Definition in file downloader.c.

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL )

◆ FILE_SECBOOT()

FILE_SECBOOT ( PERMITTED )

◆ downloader_free()

void downloader_free ( struct refcnt * refcnt)
static

Free downloader object.

Parameters
refcntDownloader reference counter

Definition at line 67 of file downloader.c.

67 {
68 struct downloader *downloader =
70
73 free ( downloader );
74}
static void image_put(struct image *image)
Decrement reference count on an image.
Definition image.h:250
static void(* free)(struct refcnt *refcnt))
Definition refcnt.h:55
#define container_of(ptr, type, field)
Get containing structure.
Definition stddef.h:36
A downloader.
Definition downloader.c:47
struct image * image
Image to contain downloaded file.
Definition downloader.c:57
struct xfer_buffer buffer
Data transfer buffer.
Definition downloader.c:59
A reference counter.
Definition refcnt.h:27
void xferbuf_free(struct xfer_buffer *xferbuf)
Free data transfer buffer.
Definition xferbuf.c:74

References downloader::buffer, container_of, free, downloader::image, image_put(), and xferbuf_free().

Referenced by create_downloader().

◆ downloader_finished()

void downloader_finished ( struct downloader * downloader,
int rc )
static

Terminate download.

Parameters
downloaderDownloader
rcReason for termination

Definition at line 82 of file downloader.c.

82 {
84 struct image *image = downloader->image;
85
86 /* Log download status */
87 if ( rc == 0 ) {
88 syslog ( LOG_NOTICE, "Downloaded \"%s\"\n", image->name );
89 } else {
90 syslog ( LOG_ERR, "Download of \"%s\" failed: %s\n",
91 image->name, strerror ( rc ) );
92 }
93
94 /* Transfer ownership from data transfer buffer to image */
95 image->data = buffer->data;
96 image->len = buffer->len;
98
99 /* Shut down interfaces */
102}
struct arbelprm_rc_send_wqe rc
Definition arbel.h:3
uint32_t buffer
Buffer index (or NETVSC_RNDIS_NO_BUFFER)
Definition netvsc.h:5
#define LOG_ERR
Error: error conditions.
Definition syslog.h:36
#define LOG_NOTICE
Notice: normal but significant conditions.
Definition syslog.h:42
void intf_shutdown(struct interface *intf, int rc)
Shut down an object interface.
Definition interface.c:279
char * strerror(int errno)
Retrieve string representation of error number.
Definition strerror.c:79
struct interface xfer
Data transfer interface.
Definition downloader.c:54
struct interface job
Job control interface.
Definition downloader.c:52
An executable image.
Definition image.h:24
const void * data
Read-only data.
Definition image.h:51
char * name
Name.
Definition image.h:38
size_t len
Length of raw file image.
Definition image.h:56
A data transfer buffer.
Definition xferbuf.h:19
#define syslog(priority, fmt,...)
Write message to system log.
Definition syslog.h:94
void xferbuf_detach(struct xfer_buffer *xferbuf)
Detach data from data transfer buffer.
Definition xferbuf.c:62

References buffer, downloader::buffer, image::data, downloader::image, intf_shutdown(), downloader::job, image::len, LOG_ERR, LOG_NOTICE, image::name, rc, strerror(), syslog, downloader::xfer, and xferbuf_detach().

Referenced by create_downloader(), downloader_deliver(), and downloader_vredirect().

◆ downloader_progress()

int downloader_progress ( struct downloader * downloader,
struct job_progress * progress )
static

Report progress of download job.

Parameters
downloaderDownloader
progressProgress report to fill in
Return values
ongoing_rcOngoing job status code (if known)

Definition at line 117 of file downloader.c.

118 {
119 int rc;
120
121 /* Allow data transfer to provide an accurate description */
122 if ( ( rc = job_progress ( &downloader->xfer, progress ) ) != 0 )
123 return rc;
124
125 /* This is not entirely accurate, since downloaded data may
126 * arrive out of order (e.g. with multicast protocols), but
127 * it's a reasonable first approximation.
128 */
129 if ( ! progress->total ) {
130 progress->completed = downloader->buffer.pos;
131 progress->total = downloader->buffer.len;
132 }
133
134 return 0;
135}
Job progress.
Definition job.h:16
unsigned long total
Total operation size.
Definition job.h:31
unsigned long completed
Amount of operation completed so far.
Definition job.h:24
size_t len
Size of data.
Definition xferbuf.h:23
size_t pos
Current offset within data.
Definition xferbuf.h:25

References downloader::buffer, job_progress::completed, xfer_buffer::len, xfer_buffer::pos, rc, job_progress::total, and downloader::xfer.

◆ downloader_deliver()

int downloader_deliver ( struct downloader * downloader,
struct io_buffer * iobuf,
struct xfer_metadata * meta )
static

Handle received data.

Parameters
downloaderDownloader
iobufDatagram I/O buffer
metaData transfer metadata
Return values
rcReturn status code

Definition at line 151 of file downloader.c.

153 {
154 int rc;
155
156 /* Add data to buffer */
157 if ( ( rc = xferbuf_deliver ( &downloader->buffer, iob_disown ( iobuf ),
158 meta ) ) != 0 )
159 goto err_deliver;
160
161 return 0;
162
163 err_deliver:
165 return rc;
166}
static void downloader_finished(struct downloader *downloader, int rc)
Terminate download.
Definition downloader.c:82
uint8_t meta
Metadata flags.
Definition ena.h:3
#define iob_disown(iobuf)
Disown an I/O buffer.
Definition iobuf.h:217
int xferbuf_deliver(struct xfer_buffer *xferbuf, struct io_buffer *iobuf, struct xfer_metadata *meta)
Add received data to data transfer buffer.
Definition xferbuf.c:175

References downloader::buffer, downloader_finished(), iob_disown, meta, rc, and xferbuf_deliver().

◆ downloader_buffer()

struct xfer_buffer * downloader_buffer ( struct downloader * downloader)
static

Get underlying data transfer buffer.

Parameters
downloaderDownloader
Return values
xferbufData transfer buffer, or NULL on error

Definition at line 175 of file downloader.c.

175 {
176
177 /* Provide direct access to underlying data transfer buffer */
178 return &downloader->buffer;
179}

References downloader::buffer.

◆ downloader_vredirect()

int downloader_vredirect ( struct downloader * downloader,
int type,
va_list args )
static

Redirect data transfer interface.

Parameters
downloaderDownloader
typeNew location type
argsRemaining arguments depend upon location type
Return values
rcReturn status code

Definition at line 189 of file downloader.c.

190 {
191 va_list tmp;
192 struct uri *uri;
193 int rc;
194
195 /* Intercept redirects to a LOCATION_URI and update the image URI */
196 if ( type == LOCATION_URI ) {
197
198 /* Extract URI argument */
199 va_copy ( tmp, args );
200 uri = va_arg ( tmp, struct uri * );
201 va_end ( tmp );
202
203 /* Set image URI */
204 if ( ( rc = image_set_uri ( downloader->image, uri ) ) != 0 )
205 goto err;
206 }
207
208 /* Redirect to new location */
209 if ( ( rc = xfer_vreopen ( &downloader->xfer, type, args ) ) != 0 )
210 goto err;
211
212 return 0;
213
214 err:
216 return rc;
217}
uint32_t type
Operating system type.
Definition ena.h:1
int image_set_uri(struct image *image, struct uri *uri)
Set image URI.
Definition image.c:153
unsigned long tmp
Definition linux_pci.h:65
int xfer_vreopen(struct interface *intf, int type, va_list args)
Reopen location.
Definition open.c:225
@ LOCATION_URI
Location is a URI.
Definition open.h:28
#define va_copy(dest, src)
Definition stdarg.h:11
#define va_arg(ap, type)
Definition stdarg.h:9
#define va_end(ap)
Definition stdarg.h:10
__builtin_va_list va_list
Definition stdarg.h:7
A Uniform Resource Identifier.
Definition uri.h:65

References downloader_finished(), downloader::image, image_set_uri(), LOCATION_URI, rc, tmp, type, va_arg, va_copy, va_end, downloader::xfer, and xfer_vreopen().

◆ create_downloader()

int create_downloader ( struct interface * job,
struct image * image )

Instantiate a downloader.

Parameters
jobJob control interface
imageImage to fill with downloaded file
Return values
rcReturn status code

Instantiates a downloader object to download the content of the specified image from its URI.

Definition at line 263 of file downloader.c.

263 {
264 struct downloader *downloader;
265 int rc;
266
267 /* Allocate and initialise structure */
268 downloader = zalloc ( sizeof ( *downloader ) );
269 if ( ! downloader )
270 return -ENOMEM;
273 &downloader->refcnt );
275 &downloader->refcnt );
278
279 /* Instantiate child objects and attach to our interfaces */
280 if ( ( rc = xfer_open_uri ( &downloader->xfer, image->uri ) ) != 0 )
281 goto err;
282
283 /* Attach parent interface, mortalise self, and return */
286 return 0;
287
288 err:
291 return rc;
292}
static struct interface_descriptor downloader_job_desc
Downloader job control interface descriptor.
Definition downloader.c:244
static void downloader_free(struct refcnt *refcnt)
Free downloader object.
Definition downloader.c:67
static struct interface_descriptor downloader_xfer_desc
Downloader data transfer interface descriptor.
Definition downloader.c:228
#define ENOMEM
Not enough space.
Definition errno.h:535
static struct image * image_get(struct image *image)
Increment reference count on an image.
Definition image.h:240
void intf_plug_plug(struct interface *a, struct interface *b)
Plug two object interfaces together.
Definition interface.c:108
static void intf_init(struct interface *intf, struct interface_descriptor *desc, struct refcnt *refcnt)
Initialise an object interface.
Definition interface.h:204
void * zalloc(size_t size)
Allocate cleared memory.
Definition malloc.c:662
int xfer_open_uri(struct interface *intf, struct uri *uri)
Open URI.
Definition open.c:68
#define ref_put(refcnt)
Drop reference to object.
Definition refcnt.h:107
#define ref_init(refcnt, free)
Initialise a reference counter.
Definition refcnt.h:65
struct refcnt refcnt
Reference count for this object.
Definition downloader.c:49
struct uri * uri
URI of image.
Definition image.h:32
static void xferbuf_umalloc_init(struct xfer_buffer *xferbuf)
Initialise umalloc()-based data transfer buffer.
Definition xferbuf.h:67

References downloader::buffer, downloader_finished(), downloader_free(), downloader_job_desc, downloader_xfer_desc, ENOMEM, downloader::image, image_get(), intf_init(), intf_plug_plug(), downloader::job, rc, ref_init, ref_put, downloader::refcnt, image::uri, downloader::xfer, xfer_open_uri(), xferbuf_umalloc_init(), and zalloc().

Referenced by imgdownload().

Variable Documentation

◆ downloader_xfer_operations

struct interface_operation downloader_xfer_operations[]
static
Initial value:
= {
}
static struct xfer_buffer * downloader_buffer(struct downloader *downloader)
Get underlying data transfer buffer.
Definition downloader.c:175
static int downloader_deliver(struct downloader *downloader, struct io_buffer *iobuf, struct xfer_metadata *meta)
Handle received data.
Definition downloader.c:151
static int downloader_vredirect(struct downloader *downloader, int type, va_list args)
Redirect data transfer interface.
Definition downloader.c:189
void intf_close(struct interface *intf, int rc)
Close an object interface.
Definition interface.c:250
#define INTF_OP(op_type, object_type, op_func)
Define an object interface operation.
Definition interface.h:33
int xfer_deliver(struct interface *intf, struct io_buffer *iobuf, struct xfer_metadata *meta)
Deliver datagram.
Definition xfer.c:195
int xfer_vredirect(struct interface *intf, int type, va_list args)
Send redirection event.
Definition xfer.c:63

Downloader data transfer interface operations.

Definition at line 220 of file downloader.c.

◆ downloader_xfer_desc

struct interface_descriptor downloader_xfer_desc
static
Initial value:
=
static struct interface_operation downloader_xfer_operations[]
Downloader data transfer interface operations.
Definition downloader.c:220
#define INTF_DESC(object_type, intf, operations)
Define an object interface descriptor.
Definition interface.h:81

Downloader data transfer interface descriptor.

Definition at line 228 of file downloader.c.

Referenced by create_downloader().

◆ downloader_job_op

struct interface_operation downloader_job_op[]
static
Initial value:
= {
}
static int downloader_progress(struct downloader *downloader, struct job_progress *progress)
Report progress of download job.
Definition downloader.c:117

Downloader job control interface operations.

Definition at line 238 of file downloader.c.

◆ downloader_job_desc

struct interface_descriptor downloader_job_desc
static
Initial value:
=
static struct interface_operation downloader_job_op[]
Downloader job control interface operations.
Definition downloader.c:238

Downloader job control interface descriptor.

Definition at line 244 of file downloader.c.

Referenced by create_downloader().