Add qemu 2.4.0
[kvmfornfv.git] / qemu / roms / ipxe / src / arch / i386 / interface / pxe / pxe_tftp.c
1 /** @file
2  *
3  * PXE TFTP API
4  *
5  */
6
7 /*
8  * Copyright (C) 2004 Michael Brown <mbrown@fensystems.co.uk>.
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License as
12  * published by the Free Software Foundation; either version 2 of the
13  * License, or any later version.
14  *
15  * This program is distributed in the hope that it will be useful, but
16  * WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23  * 02110-1301, USA.
24  */
25
26 FILE_LICENCE ( GPL2_OR_LATER );
27
28 #include <stdlib.h>
29 #include <stdio.h>
30 #include <errno.h>
31 #include <byteswap.h>
32 #include <ipxe/uaccess.h>
33 #include <ipxe/in.h>
34 #include <ipxe/tftp.h>
35 #include <ipxe/iobuf.h>
36 #include <ipxe/xfer.h>
37 #include <ipxe/open.h>
38 #include <ipxe/process.h>
39 #include <pxe.h>
40
41 /** A PXE TFTP connection */
42 struct pxe_tftp_connection {
43         /** Data transfer interface */
44         struct interface xfer;
45         /** Data buffer */
46         userptr_t buffer;
47         /** Size of data buffer */
48         size_t size;
49         /** Starting offset of data buffer */
50         size_t start;
51         /** File position */
52         size_t offset;
53         /** Maximum file position */
54         size_t max_offset;
55         /** Block size */
56         size_t blksize;
57         /** Block index */
58         unsigned int blkidx;
59         /** Overall return status code */
60         int rc;
61 };
62
63 /**
64  * Close PXE TFTP connection
65  *
66  * @v pxe_tftp          PXE TFTP connection
67  * @v rc                Final status code
68  */
69 static void pxe_tftp_close ( struct pxe_tftp_connection *pxe_tftp, int rc ) {
70         intf_shutdown ( &pxe_tftp->xfer, rc );
71         pxe_tftp->rc = rc;
72 }
73
74 /**
75  * Check flow control window
76  *
77  * @v pxe_tftp          PXE TFTP connection
78  * @ret len             Length of window
79  */
80 static size_t pxe_tftp_xfer_window ( struct pxe_tftp_connection *pxe_tftp ) {
81
82         return pxe_tftp->blksize;
83 }
84
85 /**
86  * Receive new data
87  *
88  * @v pxe_tftp          PXE TFTP connection
89  * @v iobuf             I/O buffer
90  * @v meta              Transfer metadata
91  * @ret rc              Return status code
92  */
93 static int pxe_tftp_xfer_deliver ( struct pxe_tftp_connection *pxe_tftp,
94                                    struct io_buffer *iobuf,
95                                    struct xfer_metadata *meta ) {
96         size_t len = iob_len ( iobuf );
97         int rc = 0;
98
99         /* Calculate new buffer position */
100         if ( meta->flags & XFER_FL_ABS_OFFSET )
101                 pxe_tftp->offset = 0;
102         pxe_tftp->offset += meta->offset;
103
104         /* Copy data block to buffer */
105         if ( len == 0 ) {
106                 /* No data (pure seek); treat as success */
107         } else if ( pxe_tftp->offset < pxe_tftp->start ) {
108                 DBG ( " buffer underrun at %zx (min %zx)",
109                       pxe_tftp->offset, pxe_tftp->start );
110                 rc = -ENOBUFS;
111         } else if ( ( pxe_tftp->offset + len ) >
112                     ( pxe_tftp->start + pxe_tftp->size ) ) {
113                 DBG ( " buffer overrun at %zx (max %zx)",
114                       ( pxe_tftp->offset + len ),
115                       ( pxe_tftp->start + pxe_tftp->size ) );
116                 rc = -ENOBUFS;
117         } else {
118                 copy_to_user ( pxe_tftp->buffer,
119                                ( pxe_tftp->offset - pxe_tftp->start ),
120                                iobuf->data, len );
121         }
122
123         /* Calculate new buffer position */
124         pxe_tftp->offset += len;
125
126         /* Record maximum offset as the file size */
127         if ( pxe_tftp->max_offset < pxe_tftp->offset )
128                 pxe_tftp->max_offset = pxe_tftp->offset;
129
130         /* Terminate transfer on error */
131         if ( rc != 0 )
132                 pxe_tftp_close ( pxe_tftp, rc );
133
134         free_iob ( iobuf );
135         return rc;
136 }
137
138 /** PXE TFTP connection interface operations */
139 static struct interface_operation pxe_tftp_xfer_ops[] = {
140         INTF_OP ( xfer_deliver, struct pxe_tftp_connection *,
141                   pxe_tftp_xfer_deliver ),
142         INTF_OP ( xfer_window, struct pxe_tftp_connection *,
143                   pxe_tftp_xfer_window ),
144         INTF_OP ( intf_close, struct pxe_tftp_connection *, pxe_tftp_close ),
145 };
146
147 /** PXE TFTP connection interface descriptor */
148 static struct interface_descriptor pxe_tftp_xfer_desc =
149         INTF_DESC ( struct pxe_tftp_connection, xfer, pxe_tftp_xfer_ops );
150
151 /** The PXE TFTP connection */
152 static struct pxe_tftp_connection pxe_tftp = {
153         .xfer = INTF_INIT ( pxe_tftp_xfer_desc ),
154 };
155
156 /**
157  * Maximum length of a PXE TFTP URI
158  *
159  * The PXE TFTP API provides 128 characters for the filename; the
160  * extra 128 bytes allow for the remainder of the URI.
161  */
162 #define PXE_TFTP_URI_LEN 256
163
164 /**
165  * Open PXE TFTP connection
166  *
167  * @v ipaddress         IP address
168  * @v port              TFTP server port
169  * @v filename          File name
170  * @v blksize           Requested block size
171  * @ret rc              Return status code
172  */
173 static int pxe_tftp_open ( uint32_t ipaddress, unsigned int port,
174                            const unsigned char *filename, size_t blksize,
175                            int sizeonly ) {
176         char uri_string[PXE_TFTP_URI_LEN];
177         struct in_addr address;
178         int rc;
179
180         /* Reset PXE TFTP connection structure */
181         memset ( &pxe_tftp, 0, sizeof ( pxe_tftp ) );
182         intf_init ( &pxe_tftp.xfer, &pxe_tftp_xfer_desc, NULL );
183         if ( blksize < TFTP_DEFAULT_BLKSIZE )
184                 blksize = TFTP_DEFAULT_BLKSIZE;
185         pxe_tftp.blksize = blksize;
186         pxe_tftp.rc = -EINPROGRESS;
187
188         /* Construct URI string */
189         address.s_addr = ipaddress;
190         if ( ! port )
191                 port = htons ( TFTP_PORT );
192         snprintf ( uri_string, sizeof ( uri_string ), "tftp%s://%s:%d%s%s",
193                    sizeonly ? "size" : "", inet_ntoa ( address ),
194                    ntohs ( port ), ( ( filename[0] == '/' ) ? "" : "/" ),
195                    filename );
196         DBG ( " %s", uri_string );
197
198         /* Open PXE TFTP connection */
199         if ( ( rc = xfer_open_uri_string ( &pxe_tftp.xfer,
200                                            uri_string ) ) != 0 ) {
201                 DBG ( " could not open (%s)\n", strerror ( rc ) );
202                 return rc;
203         }
204
205         return 0;
206 }
207
208 /**
209  * TFTP OPEN
210  *
211  * @v tftp_open                         Pointer to a struct s_PXENV_TFTP_OPEN
212  * @v s_PXENV_TFTP_OPEN::ServerIPAddress TFTP server IP address
213  * @v s_PXENV_TFTP_OPEN::GatewayIPAddress Relay agent IP address, or 0.0.0.0
214  * @v s_PXENV_TFTP_OPEN::FileName       Name of file to open
215  * @v s_PXENV_TFTP_OPEN::TFTPPort       TFTP server UDP port
216  * @v s_PXENV_TFTP_OPEN::PacketSize     TFTP blksize option to request
217  * @ret #PXENV_EXIT_SUCCESS             File was opened
218  * @ret #PXENV_EXIT_FAILURE             File was not opened
219  * @ret s_PXENV_TFTP_OPEN::Status       PXE status code
220  * @ret s_PXENV_TFTP_OPEN::PacketSize   Negotiated blksize
221  * @err #PXENV_STATUS_TFTP_INVALID_PACKET_SIZE Requested blksize too small
222  *
223  * Opens a TFTP connection for downloading a file a block at a time
224  * using pxenv_tftp_read().
225  *
226  * If s_PXENV_TFTP_OPEN::GatewayIPAddress is 0.0.0.0, normal IP
227  * routing will take place.  See the relevant
228  * @ref pxe_routing "implementation note" for more details.
229  *
230  * On x86, you must set the s_PXE::StatusCallout field to a nonzero
231  * value before calling this function in protected mode.  You cannot
232  * call this function with a 32-bit stack segment.  (See the relevant
233  * @ref pxe_x86_pmode16 "implementation note" for more details.)
234  * 
235  * @note According to the PXE specification version 2.1, this call
236  * "opens a file for reading/writing", though how writing is to be
237  * achieved without the existence of an API call %pxenv_tftp_write()
238  * is not made clear.
239  *
240  * @note Despite the existence of the numerous statements within the
241  * PXE specification of the form "...if a TFTP/MTFTP or UDP connection
242  * is active...", you cannot use pxenv_tftp_open() and
243  * pxenv_tftp_read() to read a file via MTFTP; only via plain old
244  * TFTP.  If you want to use MTFTP, use pxenv_tftp_read_file()
245  * instead.  Astute readers will note that, since
246  * pxenv_tftp_read_file() is an atomic operation from the point of
247  * view of the PXE API, it is conceptually impossible to issue any
248  * other PXE API call "if an MTFTP connection is active".
249  */
250 static PXENV_EXIT_t pxenv_tftp_open ( struct s_PXENV_TFTP_OPEN *tftp_open ) {
251         int rc;
252
253         DBG ( "PXENV_TFTP_OPEN" );
254
255         /* Guard against callers that fail to close before re-opening */
256         pxe_tftp_close ( &pxe_tftp, 0 );
257
258         /* Open connection */
259         if ( ( rc = pxe_tftp_open ( tftp_open->ServerIPAddress,
260                                     tftp_open->TFTPPort,
261                                     tftp_open->FileName,
262                                     tftp_open->PacketSize,
263                                     0) ) != 0 ) {
264                 tftp_open->Status = PXENV_STATUS ( rc );
265                 return PXENV_EXIT_FAILURE;
266         }
267
268         /* Wait for OACK to arrive so that we have the block size */
269         while ( ( ( rc = pxe_tftp.rc ) == -EINPROGRESS ) &&
270                 ( pxe_tftp.max_offset == 0 ) ) {
271                 step();
272         }
273         pxe_tftp.blksize = xfer_window ( &pxe_tftp.xfer );
274         tftp_open->PacketSize = pxe_tftp.blksize;
275         DBG ( " blksize=%d", tftp_open->PacketSize );
276
277         /* EINPROGRESS is normal; we don't wait for the whole transfer */
278         if ( rc == -EINPROGRESS )
279                 rc = 0;
280
281         tftp_open->Status = PXENV_STATUS ( rc );
282         return ( rc ? PXENV_EXIT_FAILURE : PXENV_EXIT_SUCCESS );
283 }
284
285 /**
286  * TFTP CLOSE
287  *
288  * @v tftp_close                        Pointer to a struct s_PXENV_TFTP_CLOSE
289  * @ret #PXENV_EXIT_SUCCESS             File was closed successfully
290  * @ret #PXENV_EXIT_FAILURE             File was not closed
291  * @ret s_PXENV_TFTP_CLOSE::Status      PXE status code
292  * @err None                            -
293  *
294  * Close a connection previously opened with pxenv_tftp_open().  You
295  * must have previously opened a connection with pxenv_tftp_open().
296  *
297  * On x86, you must set the s_PXE::StatusCallout field to a nonzero
298  * value before calling this function in protected mode.  You cannot
299  * call this function with a 32-bit stack segment.  (See the relevant
300  * @ref pxe_x86_pmode16 "implementation note" for more details.)
301  */
302 static PXENV_EXIT_t pxenv_tftp_close ( struct s_PXENV_TFTP_CLOSE *tftp_close ) {
303         DBG ( "PXENV_TFTP_CLOSE" );
304
305         pxe_tftp_close ( &pxe_tftp, 0 );
306         tftp_close->Status = PXENV_STATUS_SUCCESS;
307         return PXENV_EXIT_SUCCESS;
308 }
309
310 /**
311  * TFTP READ
312  *
313  * @v tftp_read                         Pointer to a struct s_PXENV_TFTP_READ
314  * @v s_PXENV_TFTP_READ::Buffer         Address of data buffer
315  * @ret #PXENV_EXIT_SUCCESS             Data was read successfully
316  * @ret #PXENV_EXIT_FAILURE             Data was not read
317  * @ret s_PXENV_TFTP_READ::Status       PXE status code
318  * @ret s_PXENV_TFTP_READ::PacketNumber TFTP packet number
319  * @ret s_PXENV_TFTP_READ::BufferSize   Length of data written into buffer
320  *
321  * Reads a single packet from a connection previously opened with
322  * pxenv_tftp_open() into the data buffer pointed to by
323  * s_PXENV_TFTP_READ::Buffer.  You must have previously opened a
324  * connection with pxenv_tftp_open().  The data written into
325  * s_PXENV_TFTP_READ::Buffer is just the file data; the various
326  * network headers have already been removed.
327  *
328  * The buffer must be large enough to contain a packet of the size
329  * negotiated via the s_PXENV_TFTP_OPEN::PacketSize field in the
330  * pxenv_tftp_open() call.  It is worth noting that the PXE
331  * specification does @b not require the caller to fill in
332  * s_PXENV_TFTP_READ::BufferSize before calling pxenv_tftp_read(), so
333  * the PXE stack is free to ignore whatever value the caller might
334  * place there and just assume that the buffer is large enough.  That
335  * said, it may be worth the caller always filling in
336  * s_PXENV_TFTP_READ::BufferSize to guard against PXE stacks that
337  * mistake it for an input parameter.
338  *
339  * The length of the TFTP data packet will be returned via
340  * s_PXENV_TFTP_READ::BufferSize.  If this length is less than the
341  * blksize negotiated via s_PXENV_TFTP_OPEN::PacketSize in the call to
342  * pxenv_tftp_open(), this indicates that the block is the last block
343  * in the file.  Note that zero is a valid length for
344  * s_PXENV_TFTP_READ::BufferSize, and will occur when the length of
345  * the file is a multiple of the blksize.
346  *
347  * The PXE specification doesn't actually state that calls to
348  * pxenv_tftp_read() will return the data packets in strict sequential
349  * order, though most PXE stacks will probably do so.  The sequence
350  * number of the packet will be returned in
351  * s_PXENV_TFTP_READ::PacketNumber.  The first packet in the file has
352  * a sequence number of one, not zero.
353  *
354  * To guard against flawed PXE stacks, the caller should probably set
355  * s_PXENV_TFTP_READ::PacketNumber to one less than the expected
356  * returned value (i.e. set it to zero for the first call to
357  * pxenv_tftp_read() and then re-use the returned s_PXENV_TFTP_READ
358  * parameter block for subsequent calls without modifying
359  * s_PXENV_TFTP_READ::PacketNumber between calls).  The caller should
360  * also guard against potential problems caused by flawed
361  * implementations returning the occasional duplicate packet, by
362  * checking that the value returned in s_PXENV_TFTP_READ::PacketNumber
363  * is as expected (i.e. one greater than that returned from the
364  * previous call to pxenv_tftp_read()).
365  *
366  * On x86, you must set the s_PXE::StatusCallout field to a nonzero
367  * value before calling this function in protected mode.  You cannot
368  * call this function with a 32-bit stack segment.  (See the relevant
369  * @ref pxe_x86_pmode16 "implementation note" for more details.)
370  */
371 static PXENV_EXIT_t pxenv_tftp_read ( struct s_PXENV_TFTP_READ *tftp_read ) {
372         int rc;
373
374         DBG ( "PXENV_TFTP_READ to %04x:%04x",
375               tftp_read->Buffer.segment, tftp_read->Buffer.offset );
376
377         /* Read single block into buffer */
378         pxe_tftp.buffer = real_to_user ( tftp_read->Buffer.segment,
379                                          tftp_read->Buffer.offset );
380         pxe_tftp.size = pxe_tftp.blksize;
381         pxe_tftp.start = pxe_tftp.offset;
382         while ( ( ( rc = pxe_tftp.rc ) == -EINPROGRESS ) &&
383                 ( pxe_tftp.offset == pxe_tftp.start ) )
384                 step();
385         pxe_tftp.buffer = UNULL;
386         tftp_read->BufferSize = ( pxe_tftp.offset - pxe_tftp.start );
387         tftp_read->PacketNumber = ++pxe_tftp.blkidx;
388
389         /* EINPROGRESS is normal if we haven't reached EOF yet */
390         if ( rc == -EINPROGRESS )
391                 rc = 0;
392
393         tftp_read->Status = PXENV_STATUS ( rc );
394         return ( rc ? PXENV_EXIT_FAILURE : PXENV_EXIT_SUCCESS );
395 }
396
397 /**
398  * TFTP/MTFTP read file
399  *
400  * @v tftp_read_file                 Pointer to a struct s_PXENV_TFTP_READ_FILE
401  * @v s_PXENV_TFTP_READ_FILE::FileName          File name
402  * @v s_PXENV_TFTP_READ_FILE::BufferSize        Size of the receive buffer
403  * @v s_PXENV_TFTP_READ_FILE::Buffer            Address of the receive buffer
404  * @v s_PXENV_TFTP_READ_FILE::ServerIPAddress   TFTP server IP address
405  * @v s_PXENV_TFTP_READ_FILE::GatewayIPAddress  Relay agent IP address
406  * @v s_PXENV_TFTP_READ_FILE::McastIPAddress    File's multicast IP address
407  * @v s_PXENV_TFTP_READ_FILE::TFTPClntPort      Client multicast UDP port
408  * @v s_PXENV_TFTP_READ_FILE::TFTPSrvPort       Server multicast UDP port
409  * @v s_PXENV_TFTP_READ_FILE::TFTPOpenTimeOut   Time to wait for first packet
410  * @v s_PXENV_TFTP_READ_FILE::TFTPReopenDelay   MTFTP inactivity timeout
411  * @ret #PXENV_EXIT_SUCCESS                     File downloaded successfully
412  * @ret #PXENV_EXIT_FAILURE                     File not downloaded
413  * @ret s_PXENV_TFTP_READ_FILE::Status          PXE status code
414  * @ret s_PXENV_TFTP_READ_FILE::BufferSize      Length of downloaded file
415  *
416  * Downloads an entire file via either TFTP or MTFTP into the buffer
417  * pointed to by s_PXENV_TFTP_READ_FILE::Buffer.
418  *
419  * The PXE specification does not make it clear how the caller
420  * requests that MTFTP be used rather than TFTP (or vice versa).  One
421  * reasonable guess is that setting
422  * s_PXENV_TFTP_READ_FILE::McastIPAddress to 0.0.0.0 would cause TFTP
423  * to be used instead of MTFTP, though it is conceivable that some PXE
424  * stacks would interpret that as "use the DHCP-provided multicast IP
425  * address" instead.  Some PXE stacks will not implement MTFTP at all,
426  * and will always use TFTP.
427  *
428  * It is not specified whether or not
429  * s_PXENV_TFTP_READ_FILE::TFTPSrvPort will be used as the TFTP server
430  * port for TFTP (rather than MTFTP) downloads.  Callers should assume
431  * that the only way to access a TFTP server on a non-standard port is
432  * to use pxenv_tftp_open() and pxenv_tftp_read().
433  *
434  * If s_PXENV_TFTP_READ_FILE::GatewayIPAddress is 0.0.0.0, normal IP
435  * routing will take place.  See the relevant
436  * @ref pxe_routing "implementation note" for more details.
437  *
438  * It is interesting to note that s_PXENV_TFTP_READ_FILE::Buffer is an
439  * #ADDR32_t type, i.e. nominally a flat physical address.  Some PXE
440  * NBPs (e.g. NTLDR) are known to call pxenv_tftp_read_file() in real
441  * mode with s_PXENV_TFTP_READ_FILE::Buffer set to an address above
442  * 1MB.  This means that PXE stacks must be prepared to write to areas
443  * outside base memory.  Exactly how this is to be achieved is not
444  * specified, though using INT 15,87 is as close to a standard method
445  * as any, and should probably be used.  Switching to protected-mode
446  * in order to access high memory will fail if pxenv_tftp_read_file()
447  * is called in V86 mode; it is reasonably to expect that a V86
448  * monitor would intercept the relatively well-defined INT 15,87 if it
449  * wants the PXE stack to be able to write to high memory.
450  *
451  * Things get even more interesting if pxenv_tftp_read_file() is
452  * called in protected mode, because there is then absolutely no way
453  * for the PXE stack to write to an absolute physical address.  You
454  * can't even get around the problem by creating a special "access
455  * everything" segment in the s_PXE data structure, because the
456  * #SEGDESC_t descriptors are limited to 64kB in size.
457  *
458  * Previous versions of the PXE specification (e.g. WfM 1.1a) provide
459  * a separate API call, %pxenv_tftp_read_file_pmode(), specifically to
460  * work around this problem.  The s_PXENV_TFTP_READ_FILE_PMODE
461  * parameter block splits s_PXENV_TFTP_READ_FILE::Buffer into
462  * s_PXENV_TFTP_READ_FILE_PMODE::BufferSelector and
463  * s_PXENV_TFTP_READ_FILE_PMODE::BufferOffset, i.e. it provides a
464  * protected-mode segment:offset address for the data buffer.  This
465  * API call is no longer present in version 2.1 of the PXE
466  * specification.
467  *
468  * Etherboot makes the assumption that s_PXENV_TFTP_READ_FILE::Buffer
469  * is an offset relative to the caller's data segment, when
470  * pxenv_tftp_read_file() is called in protected mode.
471  *
472  * On x86, you must set the s_PXE::StatusCallout field to a nonzero
473  * value before calling this function in protected mode.  You cannot
474  * call this function with a 32-bit stack segment.  (See the relevant
475  * @ref pxe_x86_pmode16 "implementation note" for more details.)
476  */
477 PXENV_EXIT_t pxenv_tftp_read_file ( struct s_PXENV_TFTP_READ_FILE
478                                     *tftp_read_file ) {
479         int rc;
480
481         DBG ( "PXENV_TFTP_READ_FILE to %08x+%x", tftp_read_file->Buffer,
482               tftp_read_file->BufferSize );
483
484         /* Open TFTP file */
485         if ( ( rc = pxe_tftp_open ( tftp_read_file->ServerIPAddress, 0,
486                                     tftp_read_file->FileName, 0, 0 ) ) != 0 ) {
487                 tftp_read_file->Status = PXENV_STATUS ( rc );
488                 return PXENV_EXIT_FAILURE;
489         }
490
491         /* Read entire file */
492         pxe_tftp.buffer = phys_to_user ( tftp_read_file->Buffer );
493         pxe_tftp.size = tftp_read_file->BufferSize;
494         while ( ( rc = pxe_tftp.rc ) == -EINPROGRESS )
495                 step();
496         pxe_tftp.buffer = UNULL;
497         tftp_read_file->BufferSize = pxe_tftp.max_offset;
498
499         /* Close TFTP file */
500         pxe_tftp_close ( &pxe_tftp, rc );
501
502         tftp_read_file->Status = PXENV_STATUS ( rc );
503         return ( rc ? PXENV_EXIT_FAILURE : PXENV_EXIT_SUCCESS );
504 }
505
506 /**
507  * TFTP GET FILE SIZE
508  *
509  * @v tftp_get_fsize                 Pointer to a struct s_PXENV_TFTP_GET_FSIZE
510  * @v s_PXENV_TFTP_GET_FSIZE::ServerIPAddress   TFTP server IP address
511  * @v s_PXENV_TFTP_GET_FSIZE::GatewayIPAddress  Relay agent IP address
512  * @v s_PXENV_TFTP_GET_FSIZE::FileName  File name
513  * @ret #PXENV_EXIT_SUCCESS             File size was determined successfully
514  * @ret #PXENV_EXIT_FAILURE             File size was not determined
515  * @ret s_PXENV_TFTP_GET_FSIZE::Status  PXE status code
516  * @ret s_PXENV_TFTP_GET_FSIZE::FileSize        File size
517  *
518  * Determine the size of a file on a TFTP server.  This uses the
519  * "tsize" TFTP option, and so will not work with a TFTP server that
520  * does not support TFTP options, or that does not support the "tsize"
521  * option.
522  *
523  * The PXE specification states that this API call will @b not open a
524  * TFTP connection for subsequent use with pxenv_tftp_read().  (This
525  * is somewhat daft, since the only way to obtain the file size via
526  * the "tsize" option involves issuing a TFTP open request, but that's
527  * life.)
528  *
529  * You cannot call pxenv_tftp_get_fsize() while a TFTP or UDP
530  * connection is open.
531  *
532  * If s_PXENV_TFTP_GET_FSIZE::GatewayIPAddress is 0.0.0.0, normal IP
533  * routing will take place.  See the relevant
534  * @ref pxe_routing "implementation note" for more details.
535  *
536  * On x86, you must set the s_PXE::StatusCallout field to a nonzero
537  * value before calling this function in protected mode.  You cannot
538  * call this function with a 32-bit stack segment.  (See the relevant
539  * @ref pxe_x86_pmode16 "implementation note" for more details.)
540  * 
541  * @note There is no way to specify the TFTP server port with this API
542  * call.  Though you can open a file using a non-standard TFTP server
543  * port (via s_PXENV_TFTP_OPEN::TFTPPort or, potentially,
544  * s_PXENV_TFTP_READ_FILE::TFTPSrvPort), you can only get the size of
545  * a file from a TFTP server listening on the standard TFTP port.
546  * "Consistency" is not a word in Intel's vocabulary.
547  */
548 static PXENV_EXIT_t pxenv_tftp_get_fsize ( struct s_PXENV_TFTP_GET_FSIZE
549                                            *tftp_get_fsize ) {
550         int rc;
551
552         DBG ( "PXENV_TFTP_GET_FSIZE" );
553
554         /* Open TFTP file */
555         if ( ( rc = pxe_tftp_open ( tftp_get_fsize->ServerIPAddress, 0,
556                                     tftp_get_fsize->FileName, 0, 1 ) ) != 0 ) {
557                 tftp_get_fsize->Status = PXENV_STATUS ( rc );
558                 return PXENV_EXIT_FAILURE;
559         }
560
561         /* Wait for initial seek to arrive, and record size */
562         while ( ( ( rc = pxe_tftp.rc ) == -EINPROGRESS ) &&
563                 ( pxe_tftp.max_offset == 0 ) ) {
564                 step();
565         }
566         tftp_get_fsize->FileSize = pxe_tftp.max_offset;
567         DBG ( " fsize=%d", tftp_get_fsize->FileSize );
568
569         /* EINPROGRESS is normal; we don't wait for the whole transfer */
570         if ( rc == -EINPROGRESS )
571                 rc = 0;
572
573         /* Close TFTP file */
574         pxe_tftp_close ( &pxe_tftp, rc );
575
576         tftp_get_fsize->Status = PXENV_STATUS ( rc );
577         return ( rc ? PXENV_EXIT_FAILURE : PXENV_EXIT_SUCCESS );
578 }
579
580 /** PXE TFTP API */
581 struct pxe_api_call pxe_tftp_api[] __pxe_api_call = {
582         PXE_API_CALL ( PXENV_TFTP_OPEN, pxenv_tftp_open,
583                        struct s_PXENV_TFTP_OPEN ),
584         PXE_API_CALL ( PXENV_TFTP_CLOSE, pxenv_tftp_close,
585                        struct s_PXENV_TFTP_CLOSE ),
586         PXE_API_CALL ( PXENV_TFTP_READ, pxenv_tftp_read,
587                        struct s_PXENV_TFTP_READ ),
588         PXE_API_CALL ( PXENV_TFTP_READ_FILE, pxenv_tftp_read_file,
589                        struct s_PXENV_TFTP_READ_FILE ),
590         PXE_API_CALL ( PXENV_TFTP_GET_FSIZE, pxenv_tftp_get_fsize,
591                        struct s_PXENV_TFTP_GET_FSIZE ),
592 };