8 * Copyright (C) 2004 Michael Brown <mbrown@fensystems.co.uk>.
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.
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.
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., 675 Mass Ave, Cambridge, MA 02139, USA.
28 #include <gpxe/uaccess.h>
30 #include <gpxe/tftp.h>
31 #include <gpxe/posix_io.h>
34 /** File descriptor for "single-file-only" PXE TFTP transfer */
35 static int pxe_single_fd = -1;
37 /** Block size for "single-file-only" PXE TFTP transfer */
38 static size_t pxe_single_blksize;
40 /** Current block index for "single-file-only" PXE TFTP transfer */
41 static unsigned int pxe_single_blkidx;
43 /** Length of a PXE-derived URI
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)
49 #define PXE_URI_LEN 256
52 * Build PXE URI string
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
60 * The URI string buffer must be at least @c PXE_URI_LEN bytes long.
62 static void pxe_tftp_build_uri ( char *uri_string,
63 uint32_t ipaddress, unsigned int port,
64 const unsigned char *filename,
66 struct in_addr address;
68 address.s_addr = ipaddress;
70 port = htons ( TFTP_PORT );
72 blksize = TFTP_MAX_BLKSIZE;
73 tftp_set_request_blksize ( blksize );
75 snprintf ( uri_string, PXE_URI_LEN, "tftp://%s:%d%s%s",
76 inet_ntoa ( address ), ntohs ( port ),
77 ( ( filename[0] == '/' ) ? "" : "/" ), filename );
83 * @v tftp_open Pointer to a struct s_PXENV_TFTP_OPEN
84 * @v s_PXENV_TFTP_OPEN::ServerIPAddress TFTP server IP address
85 * @v s_PXENV_TFTP_OPEN::GatewayIPAddress Relay agent IP address, or 0.0.0.0
86 * @v s_PXENV_TFTP_OPEN::FileName Name of file to open
87 * @v s_PXENV_TFTP_OPEN::TFTPPort TFTP server UDP port
88 * @v s_PXENV_TFTP_OPEN::PacketSize TFTP blksize option to request
89 * @ret #PXENV_EXIT_SUCCESS File was opened
90 * @ret #PXENV_EXIT_FAILURE File was not opened
91 * @ret s_PXENV_TFTP_OPEN::Status PXE status code
92 * @ret s_PXENV_TFTP_OPEN::PacketSize Negotiated blksize
93 * @err #PXENV_STATUS_TFTP_INVALID_PACKET_SIZE Requested blksize too small
95 * Opens a TFTP connection for downloading a file a block at a time
96 * using pxenv_tftp_read().
98 * If s_PXENV_TFTP_OPEN::GatewayIPAddress is 0.0.0.0, normal IP
99 * routing will take place. See the relevant
100 * @ref pxe_routing "implementation note" for more details.
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.
107 * On x86, you must set the s_PXE::StatusCallout field to a nonzero
108 * value before calling this function in protected mode. You cannot
109 * call this function with a 32-bit stack segment. (See the relevant
110 * @ref pxe_x86_pmode16 "implementation note" for more details.)
112 * @note According to the PXE specification version 2.1, this call
113 * "opens a file for reading/writing", though how writing is to be
114 * achieved without the existence of an API call %pxenv_tftp_write()
117 * @note Despite the existence of the numerous statements within the
118 * PXE specification of the form "...if a TFTP/MTFTP or UDP connection
119 * is active...", you cannot use pxenv_tftp_open() and
120 * pxenv_tftp_read() to read a file via MTFTP; only via plain old
121 * TFTP. If you want to use MTFTP, use pxenv_tftp_read_file()
122 * instead. Astute readers will note that, since
123 * pxenv_tftp_read_file() is an atomic operation from the point of
124 * view of the PXE API, it is conceptually impossible to issue any
125 * other PXE API call "if an MTFTP connection is active".
127 PXENV_EXIT_t pxenv_tftp_open ( struct s_PXENV_TFTP_OPEN *tftp_open ) {
128 char uri_string[PXE_URI_LEN];
130 DBG ( "PXENV_TFTP_OPEN" );
132 /* Guard against callers that fail to close before re-opening */
133 close ( pxe_single_fd );
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 );
143 pxe_single_fd = open ( uri_string );
144 if ( pxe_single_fd < 0 ) {
145 tftp_open->Status = PXENV_STATUS ( pxe_single_fd );
146 return PXENV_EXIT_FAILURE;
149 /* Record parameters for later use */
150 pxe_single_blksize = tftp_open->PacketSize;
151 pxe_single_blkidx = 0;
153 tftp_open->Status = PXENV_STATUS_SUCCESS;
154 return PXENV_EXIT_SUCCESS;
160 * @v tftp_close Pointer to a struct s_PXENV_TFTP_CLOSE
161 * @ret #PXENV_EXIT_SUCCESS File was closed successfully
162 * @ret #PXENV_EXIT_FAILURE File was not closed
163 * @ret s_PXENV_TFTP_CLOSE::Status PXE status code
166 * Close a connection previously opened with pxenv_tftp_open(). You
167 * must have previously opened a connection with pxenv_tftp_open().
169 * On x86, you must set the s_PXE::StatusCallout field to a nonzero
170 * value before calling this function in protected mode. You cannot
171 * call this function with a 32-bit stack segment. (See the relevant
172 * @ref pxe_x86_pmode16 "implementation note" for more details.)
174 PXENV_EXIT_t pxenv_tftp_close ( struct s_PXENV_TFTP_CLOSE *tftp_close ) {
175 DBG ( "PXENV_TFTP_CLOSE" );
177 close ( pxe_single_fd );
179 tftp_close->Status = PXENV_STATUS_SUCCESS;
180 return PXENV_EXIT_SUCCESS;
186 * @v tftp_read Pointer to a struct s_PXENV_TFTP_READ
187 * @v s_PXENV_TFTP_READ::Buffer Address of data buffer
188 * @ret #PXENV_EXIT_SUCCESS Data was read successfully
189 * @ret #PXENV_EXIT_FAILURE Data was not read
190 * @ret s_PXENV_TFTP_READ::Status PXE status code
191 * @ret s_PXENV_TFTP_READ::PacketNumber TFTP packet number
192 * @ret s_PXENV_TFTP_READ::BufferSize Length of data written into buffer
194 * Reads a single packet from a connection previously opened with
195 * pxenv_tftp_open() into the data buffer pointed to by
196 * s_PXENV_TFTP_READ::Buffer. You must have previously opened a
197 * connection with pxenv_tftp_open(). The data written into
198 * s_PXENV_TFTP_READ::Buffer is just the file data; the various
199 * network headers have already been removed.
201 * The buffer must be large enough to contain a packet of the size
202 * negotiated via the s_PXENV_TFTP_OPEN::PacketSize field in the
203 * pxenv_tftp_open() call. It is worth noting that the PXE
204 * specification does @b not require the caller to fill in
205 * s_PXENV_TFTP_READ::BufferSize before calling pxenv_tftp_read(), so
206 * the PXE stack is free to ignore whatever value the caller might
207 * place there and just assume that the buffer is large enough. That
208 * said, it may be worth the caller always filling in
209 * s_PXENV_TFTP_READ::BufferSize to guard against PXE stacks that
210 * mistake it for an input parameter.
212 * The length of the TFTP data packet will be returned via
213 * s_PXENV_TFTP_READ::BufferSize. If this length is less than the
214 * blksize negotiated via s_PXENV_TFTP_OPEN::PacketSize in the call to
215 * pxenv_tftp_open(), this indicates that the block is the last block
216 * in the file. Note that zero is a valid length for
217 * s_PXENV_TFTP_READ::BufferSize, and will occur when the length of
218 * the file is a multiple of the blksize.
220 * The PXE specification doesn't actually state that calls to
221 * pxenv_tftp_read() will return the data packets in strict sequential
222 * order, though most PXE stacks will probably do so. The sequence
223 * number of the packet will be returned in
224 * s_PXENV_TFTP_READ::PacketNumber. The first packet in the file has
225 * a sequence number of one, not zero.
227 * To guard against flawed PXE stacks, the caller should probably set
228 * s_PXENV_TFTP_READ::PacketNumber to one less than the expected
229 * returned value (i.e. set it to zero for the first call to
230 * pxenv_tftp_read() and then re-use the returned s_PXENV_TFTP_READ
231 * parameter block for subsequent calls without modifying
232 * s_PXENV_TFTP_READ::PacketNumber between calls). The caller should
233 * also guard against potential problems caused by flawed
234 * implementations returning the occasional duplicate packet, by
235 * checking that the value returned in s_PXENV_TFTP_READ::PacketNumber
236 * is as expected (i.e. one greater than that returned from the
237 * previous call to pxenv_tftp_read()).
239 * On x86, you must set the s_PXE::StatusCallout field to a nonzero
240 * value before calling this function in protected mode. You cannot
241 * call this function with a 32-bit stack segment. (See the relevant
242 * @ref pxe_x86_pmode16 "implementation note" for more details.)
244 PXENV_EXIT_t pxenv_tftp_read ( struct s_PXENV_TFTP_READ *tftp_read ) {
248 DBG ( "PXENV_TFTP_READ to %04x:%04x",
249 tftp_read->Buffer.segment, tftp_read->Buffer.offset );
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 );
255 tftp_read->Status = PXENV_STATUS ( len );
256 return PXENV_EXIT_FAILURE;
258 tftp_read->BufferSize = len;
259 tftp_read->PacketNumber = ++pxe_single_blkidx;
261 tftp_read->Status = PXENV_STATUS_SUCCESS;
262 return PXENV_EXIT_SUCCESS;
266 * TFTP/MTFTP read file
268 * @v tftp_read_file Pointer to a struct s_PXENV_TFTP_READ_FILE
269 * @v s_PXENV_TFTP_READ_FILE::FileName File name
270 * @v s_PXENV_TFTP_READ_FILE::BufferSize Size of the receive buffer
271 * @v s_PXENV_TFTP_READ_FILE::Buffer Address of the receive buffer
272 * @v s_PXENV_TFTP_READ_FILE::ServerIPAddress TFTP server IP address
273 * @v s_PXENV_TFTP_READ_FILE::GatewayIPAddress Relay agent IP address
274 * @v s_PXENV_TFTP_READ_FILE::McastIPAddress File's multicast IP address
275 * @v s_PXENV_TFTP_READ_FILE::TFTPClntPort Client multicast UDP port
276 * @v s_PXENV_TFTP_READ_FILE::TFTPSrvPort Server multicast UDP port
277 * @v s_PXENV_TFTP_READ_FILE::TFTPOpenTimeOut Time to wait for first packet
278 * @v s_PXENV_TFTP_READ_FILE::TFTPReopenDelay MTFTP inactivity timeout
279 * @ret #PXENV_EXIT_SUCCESS File downloaded successfully
280 * @ret #PXENV_EXIT_FAILURE File not downloaded
281 * @ret s_PXENV_TFTP_READ_FILE::Status PXE status code
282 * @ret s_PXENV_TFTP_READ_FILE::BufferSize Length of downloaded file
284 * Downloads an entire file via either TFTP or MTFTP into the buffer
285 * pointed to by s_PXENV_TFTP_READ_FILE::Buffer.
287 * The PXE specification does not make it clear how the caller
288 * requests that MTFTP be used rather than TFTP (or vice versa). One
289 * reasonable guess is that setting
290 * s_PXENV_TFTP_READ_FILE::McastIPAddress to 0.0.0.0 would cause TFTP
291 * to be used instead of MTFTP, though it is conceivable that some PXE
292 * stacks would interpret that as "use the DHCP-provided multicast IP
293 * address" instead. Some PXE stacks will not implement MTFTP at all,
294 * and will always use TFTP.
296 * It is not specified whether or not
297 * s_PXENV_TFTP_READ_FILE::TFTPSrvPort will be used as the TFTP server
298 * port for TFTP (rather than MTFTP) downloads. Callers should assume
299 * that the only way to access a TFTP server on a non-standard port is
300 * to use pxenv_tftp_open() and pxenv_tftp_read().
302 * If s_PXENV_TFTP_READ_FILE::GatewayIPAddress is 0.0.0.0, normal IP
303 * routing will take place. See the relevant
304 * @ref pxe_routing "implementation note" for more details.
306 * It is interesting to note that s_PXENV_TFTP_READ_FILE::Buffer is an
307 * #ADDR32_t type, i.e. nominally a flat physical address. Some PXE
308 * NBPs (e.g. NTLDR) are known to call pxenv_tftp_read_file() in real
309 * mode with s_PXENV_TFTP_READ_FILE::Buffer set to an address above
310 * 1MB. This means that PXE stacks must be prepared to write to areas
311 * outside base memory. Exactly how this is to be achieved is not
312 * specified, though using INT 15,87 is as close to a standard method
313 * as any, and should probably be used. Switching to protected-mode
314 * in order to access high memory will fail if pxenv_tftp_read_file()
315 * is called in V86 mode; it is reasonably to expect that a V86
316 * monitor would intercept the relatively well-defined INT 15,87 if it
317 * wants the PXE stack to be able to write to high memory.
319 * Things get even more interesting if pxenv_tftp_read_file() is
320 * called in protected mode, because there is then absolutely no way
321 * for the PXE stack to write to an absolute physical address. You
322 * can't even get around the problem by creating a special "access
323 * everything" segment in the s_PXE data structure, because the
324 * #SEGDESC_t descriptors are limited to 64kB in size.
326 * Previous versions of the PXE specification (e.g. WfM 1.1a) provide
327 * a separate API call, %pxenv_tftp_read_file_pmode(), specifically to
328 * work around this problem. The s_PXENV_TFTP_READ_FILE_PMODE
329 * parameter block splits s_PXENV_TFTP_READ_FILE::Buffer into
330 * s_PXENV_TFTP_READ_FILE_PMODE::BufferSelector and
331 * s_PXENV_TFTP_READ_FILE_PMODE::BufferOffset, i.e. it provides a
332 * protected-mode segment:offset address for the data buffer. This
333 * API call is no longer present in version 2.1 of the PXE
336 * Etherboot makes the assumption that s_PXENV_TFTP_READ_FILE::Buffer
337 * is an offset relative to the caller's data segment, when
338 * pxenv_tftp_read_file() is called in protected mode.
340 * On x86, you must set the s_PXE::StatusCallout field to a nonzero
341 * value before calling this function in protected mode. You cannot
342 * call this function with a 32-bit stack segment. (See the relevant
343 * @ref pxe_x86_pmode16 "implementation note" for more details.)
345 * @note Microsoft's NTLDR assumes that the filename passed in via
346 * s_PXENV_TFTP_READ_FILE::FileName will be stored in the "file" field
347 * of the stored DHCPACK packet, whence it will be returned via any
348 * subsequent calls to pxenv_get_cached_info(). Though this is
349 * essentially a bug in the Intel PXE implementation (not, for once,
350 * in the specification!), it is a bug that Microsoft relies upon, and
351 * so we implement this bug-for-bug compatibility by overwriting the
352 * filename stored DHCPACK packet with the filename passed in
353 * s_PXENV_TFTP_READ_FILE::FileName.
356 PXENV_EXIT_t pxenv_tftp_read_file ( struct s_PXENV_TFTP_READ_FILE
358 char uri_string[PXE_URI_LEN];
366 DBG ( "PXENV_TFTP_READ_FILE" );
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 );
374 DBG ( " to %08lx+%lx", tftp_read_file->Buffer,
375 tftp_read_file->BufferSize );
378 fd = open ( uri_string );
380 tftp_read_file->Status = PXENV_STATUS ( fd );
381 return PXENV_EXIT_FAILURE;
385 buffer = phys_to_user ( tftp_read_file->Buffer );
386 max_len = tftp_read_file->BufferSize;
388 frag_len = read_user ( fd, buffer, len, max_len );
389 if ( frag_len <= 0 ) {
398 tftp_read_file->BufferSize = len;
399 tftp_read_file->Status = PXENV_STATUS ( rc );
400 return ( rc ? PXENV_EXIT_FAILURE : PXENV_EXIT_SUCCESS );
406 * @v tftp_get_fsize Pointer to a struct s_PXENV_TFTP_GET_FSIZE
407 * @v s_PXENV_TFTP_GET_FSIZE::ServerIPAddress TFTP server IP address
408 * @v s_PXENV_TFTP_GET_FSIZE::GatewayIPAddress Relay agent IP address
409 * @v s_PXENV_TFTP_GET_FSIZE::FileName File name
410 * @ret #PXENV_EXIT_SUCCESS File size was determined successfully
411 * @ret #PXENV_EXIT_FAILURE File size was not determined
412 * @ret s_PXENV_TFTP_GET_FSIZE::Status PXE status code
413 * @ret s_PXENV_TFTP_GET_FSIZE::FileSize File size
415 * Determine the size of a file on a TFTP server. This uses the
416 * "tsize" TFTP option, and so will not work with a TFTP server that
417 * does not support TFTP options, or that does not support the "tsize"
420 * The PXE specification states that this API call will @b not open a
421 * TFTP connection for subsequent use with pxenv_tftp_read(). (This
422 * is somewhat daft, since the only way to obtain the file size via
423 * the "tsize" option involves issuing a TFTP open request, but that's
426 * You cannot call pxenv_tftp_get_fsize() while a TFTP or UDP
427 * connection is open.
429 * If s_PXENV_TFTP_GET_FSIZE::GatewayIPAddress is 0.0.0.0, normal IP
430 * routing will take place. See the relevant
431 * @ref pxe_routing "implementation note" for more details.
433 * On x86, you must set the s_PXE::StatusCallout field to a nonzero
434 * value before calling this function in protected mode. You cannot
435 * call this function with a 32-bit stack segment. (See the relevant
436 * @ref pxe_x86_pmode16 "implementation note" for more details.)
438 * @note There is no way to specify the TFTP server port with this API
439 * call. Though you can open a file using a non-standard TFTP server
440 * port (via s_PXENV_TFTP_OPEN::TFTPPort or, potentially,
441 * s_PXENV_TFTP_READ_FILE::TFTPSrvPort), you can only get the size of
442 * a file from a TFTP server listening on the standard TFTP port.
443 * "Consistency" is not a word in Intel's vocabulary.
445 PXENV_EXIT_t pxenv_tftp_get_fsize ( struct s_PXENV_TFTP_GET_FSIZE
447 char uri_string[PXE_URI_LEN];
451 DBG ( "PXENV_TFTP_GET_FSIZE" );
454 pxe_tftp_build_uri ( uri_string, tftp_get_fsize->ServerIPAddress,
455 0, tftp_get_fsize->FileName, 0 );
456 DBG ( " %s", uri_string );
459 fd = open ( uri_string );
461 tftp_get_fsize->Status = PXENV_STATUS ( fd );
462 return PXENV_EXIT_FAILURE;
469 tftp_get_fsize->Status = PXENV_STATUS ( size );
470 return PXENV_EXIT_FAILURE;
473 tftp_get_fsize->FileSize = size;
474 tftp_get_fsize->Status = PXENV_STATUS_SUCCESS;
475 return PXENV_EXIT_SUCCESS;