1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
|
/*
* Copyright (C) 2007 Michael Brown <mbrown@fensystems.co.uk>.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <stdlib.h>
#include <stdarg.h>
#include <errno.h>
#include <gpxe/xfer.h>
#include <gpxe/open.h>
#include <gpxe/job.h>
#include <gpxe/uaccess.h>
#include <gpxe/umalloc.h>
#include <gpxe/image.h>
#include <gpxe/downloader.h>
/** @file
*
* Image downloader
*
*/
/** A downloader */
struct downloader {
/** Reference count for this object */
struct refcnt refcnt;
/** Job control interface */
struct job_interface job;
/** Data transfer interface */
struct xfer_interface xfer;
/** Image to contain downloaded file */
struct image *image;
/** Current position within image buffer */
size_t pos;
/** Image registration routine */
int ( * register_image ) ( struct image *image );
};
/**
* Free downloader object
*
* @v refcnt Downloader reference counter
*/
static void downloader_free ( struct refcnt *refcnt ) {
struct downloader *downloader =
container_of ( refcnt, struct downloader, refcnt );
image_put ( downloader->image );
free ( downloader );
}
/**
* Terminate download
*
* @v downloader Downloader
* @v rc Reason for termination
*/
static void downloader_finished ( struct downloader *downloader, int rc ) {
/* Block further incoming messages */
job_nullify ( &downloader->job );
xfer_nullify ( &downloader->xfer );
/* Free resources and close interfaces */
xfer_close ( &downloader->xfer, rc );
job_done ( &downloader->job, rc );
}
/**
* Ensure that download buffer is large enough for the specified size
*
* @v downloader Downloader
* @v len Required minimum size
* @ret rc Return status code
*/
static int downloader_ensure_size ( struct downloader *downloader,
size_t len ) {
userptr_t new_buffer;
/* If buffer is already large enough, do nothing */
if ( len <= downloader->image->len )
return 0;
/* Extend buffer */
new_buffer = urealloc ( downloader->image->data, len );
if ( ! new_buffer ) {
DBGC ( downloader, "Downloader %p could not extend buffer to "
"%zd bytes\n", downloader, len );
return -ENOBUFS;
}
downloader->image->data = new_buffer;
downloader->image->len = len;
return 0;
}
/****************************************************************************
*
* Job control interface
*
*/
/**
* Handle kill() event received via job control interface
*
* @v job Downloader job control interface
*/
static void downloader_job_kill ( struct job_interface *job ) {
struct downloader *downloader =
container_of ( job, struct downloader, job );
/* Terminate download */
downloader_finished ( downloader, -ECANCELED );
}
/** Downloader job control interface operations */
static struct job_interface_operations downloader_job_operations = {
.start = ignore_job_start,
.done = ignore_job_done,
.kill = downloader_job_kill,
.progress = ignore_job_progress,
};
/****************************************************************************
*
* Data transfer interface
*
*/
/**
* Handle seek() event received via data transfer interface
*
* @v xfer Downloader data transfer interface
* @v pos New position
* @ret rc Return status code
*/
static int downloader_xfer_seek ( struct xfer_interface *xfer, off_t offset,
int whence ) {
struct downloader *downloader =
container_of ( xfer, struct downloader, xfer );
off_t new_pos;
int rc;
/* Calculate new buffer position */
switch ( whence ) {
case SEEK_SET:
new_pos = offset;
break;
case SEEK_CUR:
new_pos = ( downloader->pos + offset );
break;
default:
assert ( 0 );
return -EINVAL;
}
/* Ensure that we have enough buffer space for this buffer position */
if ( ( rc = downloader_ensure_size ( downloader, new_pos ) ) != 0 )
return rc;
downloader->pos = new_pos;
return 0;
}
/**
* Handle deliver_raw() event received via data transfer interface
*
* @v xfer Downloader data transfer interface
* @v data Data buffer
* @v len Length of data buffer
* @ret rc Return status code
*/
static int downloader_xfer_deliver_raw ( struct xfer_interface *xfer,
const void *data, size_t len ) {
struct downloader *downloader =
container_of ( xfer, struct downloader, xfer );
size_t max;
int rc;
/* Ensure that we have enough buffer space for this data */
max = ( downloader->pos + len );
if ( ( rc = downloader_ensure_size ( downloader, max ) ) != 0 )
return rc;
/* Copy data to buffer */
copy_to_user ( downloader->image->data, downloader->pos, data, len );
/* Update current buffer position */
downloader->pos += len;
return 0;
}
/**
* Handle close() event received via data transfer interface
*
* @v xfer Downloader data transfer interface
* @v rc Reason for close
*/
static void downloader_xfer_close ( struct xfer_interface *xfer, int rc ) {
struct downloader *downloader =
container_of ( xfer, struct downloader, xfer );
/* Register image if download was successful */
if ( rc == 0 )
rc = downloader->register_image ( downloader->image );
/* Terminate download */
downloader_finished ( downloader, rc );
}
/** Downloader data transfer interface operations */
static struct xfer_interface_operations downloader_xfer_operations = {
.close = downloader_xfer_close,
.vredirect = xfer_vopen,
.request = ignore_xfer_request,
.seek = downloader_xfer_seek,
.deliver_iob = xfer_deliver_as_raw,
.deliver_raw = downloader_xfer_deliver_raw,
};
/****************************************************************************
*
* Instantiator
*
*/
/**
* Instantiate a downloader
*
* @v job Job control interface
* @v image Image to fill with downloaded file
* @v register_image Image registration routine
* @v type Location type to pass to xfer_open()
* @v ... Remaining arguments to pass to xfer_open()
* @ret rc Return status code
*
* Instantiates a downloader object to download the specified URI into
* the specified image object. If the download is successful, the
* image registration routine @c register_image() will be called.
*/
int create_downloader ( struct job_interface *job, struct image *image,
int ( * register_image ) ( struct image *image ),
int type, ... ) {
struct downloader *downloader;
va_list args;
int rc;
/* Allocate and initialise structure */
downloader = malloc ( sizeof ( *downloader ) );
if ( ! downloader )
return -ENOMEM;
memset ( downloader, 0, sizeof ( *downloader ) );
downloader->refcnt.free = downloader_free;
job_init ( &downloader->job, &downloader_job_operations,
&downloader->refcnt );
xfer_init ( &downloader->xfer, &downloader_xfer_operations,
&downloader->refcnt );
downloader->image = image_get ( image );
downloader->register_image = register_image;
va_start ( args, type );
/* Instantiate child objects and attach to our interfaces */
if ( ( rc = xfer_vopen ( &downloader->xfer, type, args ) ) != 0 )
goto err;
/* Attach parent interface, mortalise self, and return */
job_plug_plug ( &downloader->job, job );
ref_put ( &downloader->refcnt );
va_end ( args );
return 0;
err:
downloader_finished ( downloader, rc );
ref_put ( &downloader->refcnt );
va_end ( args );
return rc;
}
|