|
@@ -22,10 +22,60 @@
|
22
|
22
|
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
23
|
23
|
*/
|
24
|
24
|
|
25
|
|
-#include "pxe.h"
|
|
25
|
+#include <stdlib.h>
|
|
26
|
+#include <stdio.h>
|
|
27
|
+#include <byteswap.h>
|
|
28
|
+#include <gpxe/uaccess.h>
|
|
29
|
+#include <gpxe/in.h>
|
|
30
|
+#include <gpxe/tftp.h>
|
|
31
|
+#include <gpxe/posix_io.h>
|
|
32
|
+#include <pxe.h>
|
26
|
33
|
|
27
|
|
-static int pxe_tftp_read_block ( unsigned char *data, unsigned int block,
|
28
|
|
- unsigned int len, int eof );
|
|
34
|
+/** File descriptor for "single-file-only" PXE TFTP transfer */
|
|
35
|
+static int pxe_single_fd = -1;
|
|
36
|
+
|
|
37
|
+/** Block size for "single-file-only" PXE TFTP transfer */
|
|
38
|
+static size_t pxe_single_blksize;
|
|
39
|
+
|
|
40
|
+/** Current block index for "single-file-only" PXE TFTP transfer */
|
|
41
|
+static unsigned int pxe_single_blkidx;
|
|
42
|
+
|
|
43
|
+/** Length of a PXE-derived URI
|
|
44
|
+ *
|
|
45
|
+ * The "single-file-only" API calls use a filename field of 128 bytes.
|
|
46
|
+ * 256 bytes provides plenty of space for constructing the (temporary)
|
|
47
|
+ * full URI.
|
|
48
|
+ */
|
|
49
|
+#define PXE_URI_LEN 256
|
|
50
|
+
|
|
51
|
+/**
|
|
52
|
+ * Build PXE URI string
|
|
53
|
+ *
|
|
54
|
+ * @v uri_string URI string to fill in
|
|
55
|
+ * @v ipaddress Server IP address (in network byte order)
|
|
56
|
+ * @v port Server port (in network byte order)
|
|
57
|
+ * @v filename File name
|
|
58
|
+ * @v blksize Requested block size, or 0
|
|
59
|
+ */
|
|
60
|
+static void pxe_tftp_build_uri ( char uri_string[PXE_URI_LEN],
|
|
61
|
+ uint32_t ipaddress, unsigned int port,
|
|
62
|
+ const unsigned char *filename,
|
|
63
|
+ int blksize ) {
|
|
64
|
+ struct in_addr address;
|
|
65
|
+
|
|
66
|
+ /* This is a fix to make Microsoft Remote Install Services work (RIS) */
|
|
67
|
+#warning "Overwrite DHCP filename"
|
|
68
|
+
|
|
69
|
+ address.s_addr = ipaddress;
|
|
70
|
+ if ( ! port )
|
|
71
|
+ port = htons ( TFTP_PORT );
|
|
72
|
+ if ( ! blksize )
|
|
73
|
+ blksize = TFTP_MAX_BLKSIZE;
|
|
74
|
+ snprintf ( uri_string, sizeof ( uri_string ),
|
|
75
|
+ "tftp://%s:%d%s%s?blksize=%d", inet_ntoa ( address ),
|
|
76
|
+ ntohs ( port ), ( ( filename[0] == '/' ) ? "" : "/" ),
|
|
77
|
+ filename, blksize );
|
|
78
|
+}
|
29
|
79
|
|
30
|
80
|
/**
|
31
|
81
|
* TFTP OPEN
|
|
@@ -49,48 +99,16 @@ static int pxe_tftp_read_block ( unsigned char *data, unsigned int block,
|
49
|
99
|
* routing will take place. See the relevant
|
50
|
100
|
* @ref pxe_routing "implementation note" for more details.
|
51
|
101
|
*
|
52
|
|
- * The blksize negotiated with the TFTP server will be returned in
|
53
|
|
- * s_PXENV_TFTP_OPEN::PacketSize, and will be the size of data blocks
|
54
|
|
- * returned by subsequent calls to pxenv_tftp_read(). The TFTP server
|
55
|
|
- * may negotiate a smaller blksize than the caller requested.
|
56
|
|
- *
|
57
|
|
- * Some TFTP servers do not support TFTP options, and will therefore
|
58
|
|
- * not be able to use anything other than a fixed 512-byte blksize.
|
59
|
|
- * The PXE specification version 2.1 requires that the caller must
|
60
|
|
- * pass in s_PXENV_TFTP_OPEN::PacketSize with a value of 512 or
|
61
|
|
- * greater.
|
62
|
|
- *
|
63
|
|
- * You can only have one TFTP connection open at a time, because the
|
64
|
|
- * PXE API requires the PXE stack to keep state (e.g. local and remote
|
65
|
|
- * port numbers, data block index) about the open TFTP connection,
|
66
|
|
- * rather than letting the caller do so.
|
67
|
|
- *
|
68
|
|
- * It is unclear precisely what constitutes a "TFTP open" operation.
|
69
|
|
- * Clearly, we must send the TFTP open request to the server. Since
|
70
|
|
- * we must know whether or not the open succeeded, we must wait for
|
71
|
|
- * the first reply packet from the TFTP server. If the TFTP server
|
72
|
|
- * supports options, the first reply packet will be an OACK; otherwise
|
73
|
|
- * it will be a DATA packet. In other words, we may only get to
|
74
|
|
- * discover whether or not the open succeeded when we receive the
|
75
|
|
- * first block of data. However, the pxenv_tftp_open() API provides
|
76
|
|
- * no way for us to return this block of data at this time. See the
|
77
|
|
- * relevant @ref pxe_note_tftp "implementation note" for Etherboot's
|
78
|
|
- * solution to this problem.
|
|
102
|
+ * Because we support arbitrary protocols, most of which have no
|
|
103
|
+ * notion of "block size" and will return data in arbitrary-sized
|
|
104
|
+ * chunks, we cheat and pretend to the caller that the blocksize is
|
|
105
|
+ * always accepted as-is.
|
79
|
106
|
*
|
80
|
107
|
* On x86, you must set the s_PXE::StatusCallout field to a nonzero
|
81
|
108
|
* value before calling this function in protected mode. You cannot
|
82
|
109
|
* call this function with a 32-bit stack segment. (See the relevant
|
83
|
110
|
* @ref pxe_x86_pmode16 "implementation note" for more details.)
|
84
|
111
|
*
|
85
|
|
- * @note If you pass in a value less than 512 for
|
86
|
|
- * s_PXENV_TFTP_OPEN::PacketSize, Etherboot will attempt to negotiate
|
87
|
|
- * this blksize with the TFTP server, even though such a value is not
|
88
|
|
- * permitted according to the PXE specification. If the TFTP server
|
89
|
|
- * ends up dictating a blksize larger than the value requested by the
|
90
|
|
- * caller (which is very probable in the case of a requested blksize
|
91
|
|
- * less than 512), then Etherboot will return the error
|
92
|
|
- * #PXENV_STATUS_TFTP_INVALID_PACKET_SIZE.
|
93
|
|
- *
|
94
|
112
|
* @note According to the PXE specification version 2.1, this call
|
95
|
113
|
* "opens a file for reading/writing", though how writing is to be
|
96
|
114
|
* achieved without the existence of an API call %pxenv_tftp_write()
|
|
@@ -107,40 +125,30 @@ static int pxe_tftp_read_block ( unsigned char *data, unsigned int block,
|
107
|
125
|
* other PXE API call "if an MTFTP connection is active".
|
108
|
126
|
*/
|
109
|
127
|
PXENV_EXIT_t pxenv_tftp_open ( struct s_PXENV_TFTP_OPEN *tftp_open ) {
|
|
128
|
+ char uri_string[PXE_URI_LEN];
|
|
129
|
+
|
110
|
130
|
DBG ( "PXENV_TFTP_OPEN" );
|
111
|
131
|
|
112
|
|
-#if 0
|
113
|
|
- /* Set server address and port */
|
114
|
|
- tftp_server.sin_addr.s_addr = tftp_open->ServerIPAddress
|
115
|
|
- ? tftp_open->ServerIPAddress
|
116
|
|
- : arptable[ARP_SERVER].ipaddr.s_addr;
|
117
|
|
- tftp_server.sin_port = ntohs ( tftp_open->TFTPPort );
|
118
|
|
-#ifdef WORK_AROUND_BPBATCH_BUG
|
119
|
|
- /* Force use of port 69; BpBatch tries to use port 4 for some
|
120
|
|
- * bizarre reason. */
|
121
|
|
- tftp_server.sin_port = TFTP_PORT;
|
122
|
|
-#endif
|
123
|
|
- /* Ignore gateway address; we can route properly */
|
124
|
|
- /* Fill in request structure */
|
125
|
|
- request.server = &tftp_server;
|
126
|
|
- request.name = tftp_open->FileName;
|
127
|
|
- request.blksize = tftp_open->PacketSize;
|
128
|
|
- DBG ( " %@:%d/%s (%d)", tftp_open->ServerIPAddress,
|
129
|
|
- tftp_open->TFTPPort, request.name, request.blksize );
|
130
|
|
- if ( !request.blksize ) request.blksize = TFTP_DEFAULT_BLKSIZE;
|
131
|
|
- /* Make request and get first packet */
|
132
|
|
- if ( !tftp_block ( &request, &block ) ) {
|
133
|
|
- tftp_open->Status = PXENV_STATUS_TFTP_FILE_NOT_FOUND;
|
|
132
|
+ /* Guard against callers that fail to close before re-opening */
|
|
133
|
+ close ( pxe_single_fd );
|
|
134
|
+ pxe_single_fd = -1;
|
|
135
|
+
|
|
136
|
+ /* Construct URI */
|
|
137
|
+ pxe_tftp_build_uri ( uri_string, tftp_open->ServerIPAddress,
|
|
138
|
+ tftp_open->TFTPPort, tftp_open->FileName,
|
|
139
|
+ tftp_open->PacketSize );
|
|
140
|
+ DBG ( " %s", uri_string );
|
|
141
|
+
|
|
142
|
+ /* Open URI */
|
|
143
|
+ pxe_single_fd = open ( uri_string );
|
|
144
|
+ if ( pxe_single_fd < 0 ) {
|
|
145
|
+ tftp_open->Status = PXENV_STATUS ( pxe_single_fd );
|
134
|
146
|
return PXENV_EXIT_FAILURE;
|
135
|
147
|
}
|
136
|
|
- /* Fill in PacketSize */
|
137
|
|
- tftp_open->PacketSize = request.blksize;
|
138
|
|
- /* Store first block for later retrieval by TFTP_READ */
|
139
|
|
- pxe_stack->tftpdata.magic_cookie = PXE_TFTP_MAGIC_COOKIE;
|
140
|
|
- pxe_stack->tftpdata.len = block.len;
|
141
|
|
- pxe_stack->tftpdata.eof = block.eof;
|
142
|
|
- memcpy ( pxe_stack->tftpdata.data, block.data, block.len );
|
143
|
|
-#endif
|
|
148
|
+
|
|
149
|
+ /* Record parameters for later use */
|
|
150
|
+ pxe_single_blksize = tftp_open->PacketSize;
|
|
151
|
+ pxe_single_blkidx = 0;
|
144
|
152
|
|
145
|
153
|
tftp_open->Status = PXENV_STATUS_SUCCESS;
|
146
|
154
|
return PXENV_EXIT_SUCCESS;
|
|
@@ -162,14 +170,12 @@ PXENV_EXIT_t pxenv_tftp_open ( struct s_PXENV_TFTP_OPEN *tftp_open ) {
|
162
|
170
|
* value before calling this function in protected mode. You cannot
|
163
|
171
|
* call this function with a 32-bit stack segment. (See the relevant
|
164
|
172
|
* @ref pxe_x86_pmode16 "implementation note" for more details.)
|
165
|
|
- *
|
166
|
|
- * @note Since TFTP runs over UDP, which is a connectionless protocol,
|
167
|
|
- * the concept of closing a file is somewhat meaningless. This call
|
168
|
|
- * is a no-op for Etherboot.
|
169
|
173
|
*/
|
170
|
174
|
PXENV_EXIT_t pxenv_tftp_close ( struct s_PXENV_TFTP_CLOSE *tftp_close ) {
|
171
|
175
|
DBG ( "PXENV_TFTP_CLOSE" );
|
172
|
176
|
|
|
177
|
+ close ( pxe_single_fd );
|
|
178
|
+ pxe_single_fd = -1;
|
173
|
179
|
tftp_close->Status = PXENV_STATUS_SUCCESS;
|
174
|
180
|
return PXENV_EXIT_SUCCESS;
|
175
|
181
|
}
|
|
@@ -230,42 +236,28 @@ PXENV_EXIT_t pxenv_tftp_close ( struct s_PXENV_TFTP_CLOSE *tftp_close ) {
|
230
|
236
|
* is as expected (i.e. one greater than that returned from the
|
231
|
237
|
* previous call to pxenv_tftp_read()).
|
232
|
238
|
*
|
233
|
|
- * Nothing in the PXE specification indicates when the TFTP
|
234
|
|
- * acknowledgement packets will be sent back to the server. See the
|
235
|
|
- * relevant @ref pxe_note_tftp "implementation note" for details on
|
236
|
|
- * when Etherboot chooses to send these packets.
|
237
|
|
- *
|
238
|
239
|
* On x86, you must set the s_PXE::StatusCallout field to a nonzero
|
239
|
240
|
* value before calling this function in protected mode. You cannot
|
240
|
241
|
* call this function with a 32-bit stack segment. (See the relevant
|
241
|
242
|
* @ref pxe_x86_pmode16 "implementation note" for more details.)
|
242
|
243
|
*/
|
243
|
244
|
PXENV_EXIT_t pxenv_tftp_read ( struct s_PXENV_TFTP_READ *tftp_read ) {
|
244
|
|
- DBG ( "PXENV_TFTP_READ" );
|
245
|
|
-
|
246
|
|
-#if 0
|
247
|
|
- /* Do we have a block pending */
|
248
|
|
- if ( pxe_stack->tftpdata.magic_cookie == PXE_TFTP_MAGIC_COOKIE ) {
|
249
|
|
- block.data = pxe_stack->tftpdata.data;
|
250
|
|
- block.len = pxe_stack->tftpdata.len;
|
251
|
|
- block.eof = pxe_stack->tftpdata.eof;
|
252
|
|
- block.block = 1; /* Will be the first block */
|
253
|
|
- pxe_stack->tftpdata.magic_cookie = 0;
|
254
|
|
- } else {
|
255
|
|
- if ( !tftp_block ( NULL, &block ) ) {
|
256
|
|
- tftp_read->Status = PXENV_STATUS_TFTP_FILE_NOT_FOUND;
|
257
|
|
- return PXENV_EXIT_FAILURE;
|
258
|
|
- }
|
|
245
|
+ userptr_t buffer;
|
|
246
|
+ ssize_t len;
|
|
247
|
+
|
|
248
|
+ DBG ( "PXENV_TFTP_READ to %04x:%04x",
|
|
249
|
+ tftp_read->Buffer.segment, tftp_read->Buffer.offset );
|
|
250
|
+
|
|
251
|
+ buffer = real_to_user ( tftp_read->Buffer.segment,
|
|
252
|
+ tftp_read->Buffer.offset );
|
|
253
|
+ len = read_user ( pxe_single_fd, buffer, 0, pxe_single_blksize );
|
|
254
|
+ if ( len < 0 ) {
|
|
255
|
+ tftp_read->Status = PXENV_STATUS ( len );
|
|
256
|
+ return PXENV_EXIT_FAILURE;
|
259
|
257
|
}
|
|
258
|
+ tftp_read->BufferSize = len;
|
|
259
|
+ tftp_read->PacketNumber = ++pxe_single_blkidx;
|
260
|
260
|
|
261
|
|
- /* Return data */
|
262
|
|
- tftp_read->PacketNumber = block.block;
|
263
|
|
- tftp_read->BufferSize = block.len;
|
264
|
|
- memcpy ( SEGOFF16_TO_PTR(tftp_read->Buffer), block.data, block.len );
|
265
|
|
- DBG ( " %d to %hx:%hx", block.len, tftp_read->Buffer.segment,
|
266
|
|
- tftp_read->Buffer.offset );
|
267
|
|
-#endif
|
268
|
|
-
|
269
|
261
|
tftp_read->Status = PXENV_STATUS_SUCCESS;
|
270
|
262
|
return PXENV_EXIT_SUCCESS;
|
271
|
263
|
}
|
|
@@ -363,53 +355,47 @@ PXENV_EXIT_t pxenv_tftp_read ( struct s_PXENV_TFTP_READ *tftp_read ) {
|
363
|
355
|
*/
|
364
|
356
|
PXENV_EXIT_t pxenv_tftp_read_file ( struct s_PXENV_TFTP_READ_FILE
|
365
|
357
|
*tftp_read_file ) {
|
366
|
|
- DBG ( "PXENV_TFTP_READ_FILE %s to [%x,%x)", tftp_read_file->FileName,
|
367
|
|
- tftp_read_file->Buffer,
|
368
|
|
- tftp_read_file->Buffer + tftp_read_file->BufferSize );
|
369
|
|
-
|
370
|
|
-#if 0
|
371
|
|
- /* inserted by Klaus Wittemeier */
|
372
|
|
- /* KERNEL_BUF stores the name of the last required file */
|
373
|
|
- /* This is a fix to make Microsoft Remote Install Services work (RIS) */
|
374
|
|
- memcpy(KERNEL_BUF, tftp_read_file->FileName, sizeof(KERNEL_BUF));
|
375
|
|
- /* end of insertion */
|
376
|
|
-
|
377
|
|
- /* Set server address and port */
|
378
|
|
- tftp_server.sin_addr.s_addr = tftp_read_file->ServerIPAddress
|
379
|
|
- ? tftp_read_file->ServerIPAddress
|
380
|
|
- : arptable[ARP_SERVER].ipaddr.s_addr;
|
381
|
|
- tftp_server.sin_port = ntohs ( tftp_read_file->TFTPSrvPort );
|
382
|
|
-
|
383
|
|
- pxe_stack->readfile.buffer = phys_to_virt ( tftp_read_file->Buffer );
|
384
|
|
- pxe_stack->readfile.bufferlen = tftp_read_file->BufferSize;
|
385
|
|
- pxe_stack->readfile.offset = 0;
|
386
|
|
-
|
387
|
|
- rc = tftp ( NULL, &tftp_server, tftp_read_file->FileName,
|
388
|
|
- pxe_tftp_read_block );
|
389
|
|
- if ( rc ) {
|
390
|
|
- tftp_read_file->Status = PXENV_STATUS_FAILURE;
|
|
358
|
+ char uri_string[PXE_URI_LEN];
|
|
359
|
+ int fd;
|
|
360
|
+ userptr_t buffer;
|
|
361
|
+ size_t max_len;
|
|
362
|
+ ssize_t frag_len;
|
|
363
|
+ size_t len = 0;
|
|
364
|
+ int rc = -ENOBUFS;
|
|
365
|
+
|
|
366
|
+ DBG ( "PXENV_TFTP_READ_FILE" );
|
|
367
|
+
|
|
368
|
+ /* Construct URI */
|
|
369
|
+ pxe_tftp_build_uri ( uri_string, tftp_read_file->ServerIPAddress,
|
|
370
|
+ tftp_read_file->TFTPSrvPort,
|
|
371
|
+ tftp_read_file->FileName, 0 );
|
|
372
|
+ DBG ( " %s", uri_string );
|
|
373
|
+
|
|
374
|
+ /* Open URI */
|
|
375
|
+ fd = open ( uri_string );
|
|
376
|
+ if ( fd < 0 ) {
|
|
377
|
+ tftp_read_file->Status = PXENV_STATUS ( fd );
|
391
|
378
|
return PXENV_EXIT_FAILURE;
|
392
|
379
|
}
|
393
|
|
-#endif
|
394
|
380
|
|
395
|
|
- tftp_read_file->Status = PXENV_STATUS_SUCCESS;
|
396
|
|
- return PXENV_EXIT_SUCCESS;
|
397
|
|
-}
|
398
|
|
-
|
399
|
|
-#if 0
|
400
|
|
-static int pxe_tftp_read_block ( unsigned char *data,
|
401
|
|
- unsigned int block __unused,
|
402
|
|
- unsigned int len, int eof ) {
|
403
|
|
- if ( pxe_stack->readfile.buffer ) {
|
404
|
|
- if ( pxe_stack->readfile.offset + len >=
|
405
|
|
- pxe_stack->readfile.bufferlen ) return -1;
|
406
|
|
- memcpy ( pxe_stack->readfile.buffer +
|
407
|
|
- pxe_stack->readfile.offset, data, len );
|
|
381
|
+ /* Read file */
|
|
382
|
+ buffer = phys_to_user ( tftp_read_file->Buffer );
|
|
383
|
+ max_len = tftp_read_file->BufferSize;
|
|
384
|
+ while ( max_len ) {
|
|
385
|
+ frag_len = read_user ( fd, buffer, len, max_len );
|
|
386
|
+ if ( frag_len <= 0 ) {
|
|
387
|
+ rc = frag_len;
|
|
388
|
+ break;
|
|
389
|
+ }
|
|
390
|
+ len += frag_len;
|
|
391
|
+ max_len -= frag_len;
|
408
|
392
|
}
|
409
|
|
- pxe_stack->readfile.offset += len;
|
410
|
|
- return eof ? 0 : 1;
|
|
393
|
+
|
|
394
|
+ close ( fd );
|
|
395
|
+ tftp_read_file->BufferSize = len;
|
|
396
|
+ tftp_read_file->Status = PXENV_STATUS ( rc );
|
|
397
|
+ return ( rc ? PXENV_EXIT_FAILURE : PXENV_EXIT_SUCCESS );
|
411
|
398
|
}
|
412
|
|
-#endif
|
413
|
399
|
|
414
|
400
|
/**
|
415
|
401
|
* TFTP GET FILE SIZE
|
|
@@ -455,168 +441,33 @@ static int pxe_tftp_read_block ( unsigned char *data,
|
455
|
441
|
*/
|
456
|
442
|
PXENV_EXIT_t pxenv_tftp_get_fsize ( struct s_PXENV_TFTP_GET_FSIZE
|
457
|
443
|
*tftp_get_fsize ) {
|
458
|
|
- int rc;
|
|
444
|
+ char uri_string[PXE_URI_LEN];
|
|
445
|
+ int fd;
|
|
446
|
+ ssize_t size;
|
459
|
447
|
|
460
|
448
|
DBG ( "PXENV_TFTP_GET_FSIZE" );
|
461
|
449
|
|
462
|
|
-#if 0
|
463
|
|
- pxe_stack->readfile.buffer = NULL;
|
464
|
|
- pxe_stack->readfile.bufferlen = 0;
|
465
|
|
- pxe_stack->readfile.offset = 0;
|
|
450
|
+ /* Construct URI */
|
|
451
|
+ pxe_tftp_build_uri ( uri_string, tftp_get_fsize->ServerIPAddress,
|
|
452
|
+ 0, tftp_get_fsize->FileName, 0 );
|
|
453
|
+ DBG ( " %s", uri_string );
|
|
454
|
+
|
|
455
|
+ /* Open URI */
|
|
456
|
+ fd = open ( uri_string );
|
|
457
|
+ if ( fd < 0 ) {
|
|
458
|
+ tftp_get_fsize->Status = PXENV_STATUS ( fd );
|
|
459
|
+ return PXENV_EXIT_FAILURE;
|
|
460
|
+ }
|
466
|
461
|
|
467
|
|
-#warning "Rewrite pxenv_tftp_get_fsize, please"
|
468
|
|
- if ( rc ) {
|
469
|
|
- tftp_get_fsize->FileSize = 0;
|
470
|
|
- tftp_get_fsize->Status = PXENV_STATUS_FAILURE;
|
|
462
|
+ /* Determine size */
|
|
463
|
+ size = fsize ( fd );
|
|
464
|
+ close ( fd );
|
|
465
|
+ if ( size < 0 ) {
|
|
466
|
+ tftp_get_fsize->Status = PXENV_STATUS ( size );
|
471
|
467
|
return PXENV_EXIT_FAILURE;
|
472
|
468
|
}
|
473
|
|
- tftp_get_fsize->FileSize = pxe_stack->readfile.offset;
|
474
|
|
-#endif
|
475
|
469
|
|
|
470
|
+ tftp_get_fsize->FileSize = size;
|
476
|
471
|
tftp_get_fsize->Status = PXENV_STATUS_SUCCESS;
|
477
|
472
|
return PXENV_EXIT_SUCCESS;
|
478
|
473
|
}
|
479
|
|
-
|
480
|
|
-/** @page pxe_notes Etherboot PXE implementation notes
|
481
|
|
-
|
482
|
|
-@section pxe_note_tftp Welding together the TFTP protocol and the PXE TFTP API
|
483
|
|
-
|
484
|
|
-The PXE TFTP API is fundamentally poorly designed; the TFTP protocol
|
485
|
|
-simply does not map well into "open file", "read file block", "close
|
486
|
|
-file" operations. The problem is the unreliable nature of UDP
|
487
|
|
-transmissions and the lock-step mechanism employed by TFTP to
|
488
|
|
-guarantee file transfer. The lock-step mechanism requires that if we
|
489
|
|
-time out waiting for a packet to arrive, we must trigger its
|
490
|
|
-retransmission by retransmitting our own previously transmitted
|
491
|
|
-packet.
|
492
|
|
-
|
493
|
|
-For example, suppose that pxenv_tftp_read() is called to read the
|
494
|
|
-first data block of a file from a server that does not support TFTP
|
495
|
|
-options, and that no data block is received within the timeout period.
|
496
|
|
-In order to trigger the retransmission of this data block,
|
497
|
|
-pxenv_tftp_read() must retransmit the TFTP open request. However, the
|
498
|
|
-information used to build the TFTP open request is not available at
|
499
|
|
-this time; it was provided only to the pxenv_tftp_open() call. Even
|
500
|
|
-if we were able to retransmit a TFTP open request, we would have to
|
501
|
|
-allocate a new local port number (and be prepared for data to arrive
|
502
|
|
-from a new remote port number) in order to avoid violating the TFTP
|
503
|
|
-protocol specification.
|
504
|
|
-
|
505
|
|
-The question of when to transmit the ACK packets is also awkward. At
|
506
|
|
-a first glance, it would seem to be fairly simple: acknowledge a
|
507
|
|
-packet immediately after receiving it. However, since the ACK packet
|
508
|
|
-may itself be lost, the next call to pxenv_tftp_read() must be
|
509
|
|
-prepared to retransmit the acknowledgement.
|
510
|
|
-
|
511
|
|
-Another problem to consider is that the pxenv_tftp_open() API call
|
512
|
|
-must return an indication of whether or not the TFTP open request
|
513
|
|
-succeeded. In the case of a TFTP server that doesn't support TFTP
|
514
|
|
-options, the only indication of a successful open is the reception of
|
515
|
|
-the first data block. However, the pxenv_tftp_open() API provides no
|
516
|
|
-way to return this data block at this time.
|
517
|
|
-
|
518
|
|
-At least some PXE stacks (e.g. NILO) solve this problem by violating
|
519
|
|
-the TFTP protocol and never bothering with retransmissions, relying on
|
520
|
|
-the TFTP server to retransmit when it times out waiting for an ACK.
|
521
|
|
-This approach is dubious at best; if, for example, the initial TFTP
|
522
|
|
-open request is lost then NILO will believe that it has opened the
|
523
|
|
-file and will eventually time out and give up while waiting for the
|
524
|
|
-first packet to arrive.
|
525
|
|
-
|
526
|
|
-The only viable solution seems to be to allocate a buffer for the
|
527
|
|
-storage of the first data packet returned by the TFTP server, since we
|
528
|
|
-may receive this packet during the pxenv_tftp_open() call but have to
|
529
|
|
-return it from the subsequent pxenv_tftp_read() call. This buffer
|
530
|
|
-must be statically allocated and must be dedicated to providing a
|
531
|
|
-temporary home for TFTP packets. There is nothing in the PXE
|
532
|
|
-specification that prevents a caller from calling
|
533
|
|
-e.g. pxenv_undi_transmit() between calls to the TFTP API, so we cannot
|
534
|
|
-use the normal transmit/receive buffer for this purpose.
|
535
|
|
-
|
536
|
|
-Having paid the storage penalty for this buffer, we can then gain some
|
537
|
|
-simplicity by exploiting it in full. There is at least one
|
538
|
|
-circumstance (pxenv_tftp_open() called to open a file on a server that
|
539
|
|
-does not support TFTP options) in which we will have to enter
|
540
|
|
-pxenv_tftp_read() knowing that our previous transmission (the open
|
541
|
|
-request, in this situation) has already been acknowledged.
|
542
|
|
-Implementation of pxenv_tftp_read() can be made simpler by making this
|
543
|
|
-condition an invariant. Specifically, on each call to
|
544
|
|
-pxenv_tftp_read(), we shall ensure that the following are true:
|
545
|
|
-
|
546
|
|
- - Our previous transmission has already been acknowledged. We
|
547
|
|
- therefore do not need to keep state about our previous
|
548
|
|
- transmission.
|
549
|
|
-
|
550
|
|
- - The next packet to read is already in a buffer in memory.
|
551
|
|
-
|
552
|
|
-In order to maintain these two conditions, pxenv_tftp_read() must do
|
553
|
|
-the following:
|
554
|
|
-
|
555
|
|
- - Copy the data packet from our buffer to the caller's buffer.
|
556
|
|
-
|
557
|
|
- - Acknowledge the data packet that we have just copied. This will
|
558
|
|
- trigger transmission of the next packet from the server.
|
559
|
|
-
|
560
|
|
- - Retransmit this acknowledgement packet until the next packet
|
561
|
|
- arrives.
|
562
|
|
-
|
563
|
|
- - Copy the packet into our internal buffer, ready for the next call
|
564
|
|
- to pxenv_tftp_read().
|
565
|
|
-
|
566
|
|
-It can be verified that this preserves the invariant condition, and it
|
567
|
|
-is clear that the resulting implementation of pxenv_tftp_read() can be
|
568
|
|
-relatively simple. (For the special case of the last data packet,
|
569
|
|
-pxenv_tftp_read() should return immediately after sending a single
|
570
|
|
-acknowledgement packet.)
|
571
|
|
-
|
572
|
|
-In order to set up this invariant condition for the first call to
|
573
|
|
-pxenv_tftp_read(), pxenv_tftp_open() must do the following:
|
574
|
|
-
|
575
|
|
- - Construct and transmit the TFTP open request.
|
576
|
|
-
|
577
|
|
- - Retransmit the TFTP open request (using a new local port number as
|
578
|
|
- necessary) until a response (DATA, OACK, or ERROR) is received.
|
579
|
|
-
|
580
|
|
- - If the response is an OACK, acknowledge the OACK and retransmit
|
581
|
|
- the acknowledgement until the first DATA packet arrives.
|
582
|
|
-
|
583
|
|
- - If we have a DATA packet, store it in a buffer ready for the first
|
584
|
|
- call to pxenv_tftp_read().
|
585
|
|
-
|
586
|
|
-This approach has the advantage of being fully compliant with both
|
587
|
|
-RFC1350 (TFTP) and RFC2347 (TFTP options). It avoids unnecessary
|
588
|
|
-retransmissions. The cost is approximately 1500 bytes of
|
589
|
|
-uninitialised storage. Since there is demonstrably no way to avoid
|
590
|
|
-paying this cost without either violating the protocol specifications
|
591
|
|
-or introducing unnecessary retransmissions, we deem this to be a cost
|
592
|
|
-worth paying.
|
593
|
|
-
|
594
|
|
-A small performance gain may be obtained by adding a single extra
|
595
|
|
-"send ACK" in both pxenv_tftp_open() and pxenv_tftp_read() immediately
|
596
|
|
-after receiving the DATA packet and copying it into the internal
|
597
|
|
-buffer. The sequence of events for pxenv_tftp_read() then becomes:
|
598
|
|
-
|
599
|
|
- - Copy the data packet from our buffer to the caller's buffer.
|
600
|
|
-
|
601
|
|
- - If this was the last data packet, return immediately.
|
602
|
|
-
|
603
|
|
- - Check to see if a TFTP data packet is waiting. If not, send an
|
604
|
|
- ACK for the data packet that we have just copied, and retransmit
|
605
|
|
- this ACK until the next data packet arrives.
|
606
|
|
-
|
607
|
|
- - Copy the packet into our internal buffer, ready for the next call
|
608
|
|
- to pxenv_tftp_read().
|
609
|
|
-
|
610
|
|
- - Send a single ACK for this data packet.
|
611
|
|
-
|
612
|
|
-Sending the ACK at this point allows the server to transmit the next
|
613
|
|
-data block while our caller is processing the current packet. If this
|
614
|
|
-ACK is lost, or the DATA packet it triggers is lost or is consumed by
|
615
|
|
-something other than pxenv_tftp_read() (e.g. by calls to
|
616
|
|
-pxenv_undi_isr()), then the next call to pxenv_tftp_read() will not
|
617
|
|
-find a TFTP data packet waiting and will retransmit the ACK anyway.
|
618
|
|
-
|
619
|
|
-Note to future API designers at Intel: try to understand the
|
620
|
|
-underlying network protocol first!
|
621
|
|
-
|
622
|
|
-*/
|