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.
29 #include <gpxe/uaccess.h>
31 #include <gpxe/tftp.h>
32 #include <gpxe/posix_io.h>
35 /** File descriptor for "single-file-only" PXE TFTP transfer */
36 static int pxe_single_fd = -1;
38 /** Block size for "single-file-only" PXE TFTP transfer */
39 static size_t pxe_single_blksize;
41 /** Current block index for "single-file-only" PXE TFTP transfer */
42 static unsigned int pxe_single_blkidx;
44 /** Length of a PXE-derived URI
46 * The "single-file-only" API calls use a filename field of 128 bytes.
47 * 256 bytes provides plenty of space for constructing the (temporary)
50 #define PXE_URI_LEN 256
53 * Build PXE URI string
55 * @v uri_string URI string to fill in
56 * @v ipaddress Server IP address (in network byte order)
57 * @v port Server port (in network byte order)
58 * @v filename File name
59 * @v blksize Requested block size, or 0
61 * The URI string buffer must be at least @c PXE_URI_LEN bytes long.
63 static void pxe_tftp_build_uri ( char *uri_string,
64 uint32_t ipaddress, unsigned int port,
65 const unsigned char *filename,
67 struct in_addr address;
69 address.s_addr = ipaddress;
71 port = htons ( TFTP_PORT );
73 blksize = TFTP_MAX_BLKSIZE;
74 tftp_set_request_blksize ( blksize );
76 snprintf ( uri_string, PXE_URI_LEN, "tftp://%s:%d%s%s",
77 inet_ntoa ( address ), ntohs ( port ),
78 ( ( filename[0] == '/' ) ? "" : "/" ), filename );
82 * Read as much as possible from file
84 * @v fd File descriptor
85 * @v buffer Data buffer
86 * @v max_len Maximum length to read
87 * @ret len Actual length read, or negative error
89 static ssize_t pxe_tftp_read_all ( int fd, userptr_t buffer,
98 FD_SET ( fd, &fdset );
99 ready = select ( &fdset, 1 );
102 len = read_user ( fd, buffer, offset, max_len );
107 } while ( max_len && len );
115 * @v tftp_open Pointer to a struct s_PXENV_TFTP_OPEN
116 * @v s_PXENV_TFTP_OPEN::ServerIPAddress TFTP server IP address
117 * @v s_PXENV_TFTP_OPEN::GatewayIPAddress Relay agent IP address, or 0.0.0.0
118 * @v s_PXENV_TFTP_OPEN::FileName Name of file to open
119 * @v s_PXENV_TFTP_OPEN::TFTPPort TFTP server UDP port
120 * @v s_PXENV_TFTP_OPEN::PacketSize TFTP blksize option to request
121 * @ret #PXENV_EXIT_SUCCESS File was opened
122 * @ret #PXENV_EXIT_FAILURE File was not opened
123 * @ret s_PXENV_TFTP_OPEN::Status PXE status code
124 * @ret s_PXENV_TFTP_OPEN::PacketSize Negotiated blksize
125 * @err #PXENV_STATUS_TFTP_INVALID_PACKET_SIZE Requested blksize too small
127 * Opens a TFTP connection for downloading a file a block at a time
128 * using pxenv_tftp_read().
130 * If s_PXENV_TFTP_OPEN::GatewayIPAddress is 0.0.0.0, normal IP
131 * routing will take place. See the relevant
132 * @ref pxe_routing "implementation note" for more details.
134 * Because we support arbitrary protocols, most of which have no
135 * notion of "block size" and will return data in arbitrary-sized
136 * chunks, we cheat and pretend to the caller that the blocksize is
137 * always accepted as-is.
139 * On x86, you must set the s_PXE::StatusCallout field to a nonzero
140 * value before calling this function in protected mode. You cannot
141 * call this function with a 32-bit stack segment. (See the relevant
142 * @ref pxe_x86_pmode16 "implementation note" for more details.)
144 * @note According to the PXE specification version 2.1, this call
145 * "opens a file for reading/writing", though how writing is to be
146 * achieved without the existence of an API call %pxenv_tftp_write()
149 * @note Despite the existence of the numerous statements within the
150 * PXE specification of the form "...if a TFTP/MTFTP or UDP connection
151 * is active...", you cannot use pxenv_tftp_open() and
152 * pxenv_tftp_read() to read a file via MTFTP; only via plain old
153 * TFTP. If you want to use MTFTP, use pxenv_tftp_read_file()
154 * instead. Astute readers will note that, since
155 * pxenv_tftp_read_file() is an atomic operation from the point of
156 * view of the PXE API, it is conceptually impossible to issue any
157 * other PXE API call "if an MTFTP connection is active".
159 PXENV_EXIT_t pxenv_tftp_open ( struct s_PXENV_TFTP_OPEN *tftp_open ) {
160 char uri_string[PXE_URI_LEN];
162 DBG ( "PXENV_TFTP_OPEN" );
164 /* Guard against callers that fail to close before re-opening */
165 close ( pxe_single_fd );
169 pxe_tftp_build_uri ( uri_string, tftp_open->ServerIPAddress,
170 tftp_open->TFTPPort, tftp_open->FileName,
171 tftp_open->PacketSize );
172 DBG ( " %s", uri_string );
175 pxe_single_fd = open ( uri_string );
176 if ( pxe_single_fd < 0 ) {
177 tftp_open->Status = PXENV_STATUS ( pxe_single_fd );
178 return PXENV_EXIT_FAILURE;
181 /* Record parameters for later use */
182 pxe_single_blksize = tftp_open->PacketSize;
183 pxe_single_blkidx = 0;
185 tftp_open->Status = PXENV_STATUS_SUCCESS;
186 return PXENV_EXIT_SUCCESS;
192 * @v tftp_close Pointer to a struct s_PXENV_TFTP_CLOSE
193 * @ret #PXENV_EXIT_SUCCESS File was closed successfully
194 * @ret #PXENV_EXIT_FAILURE File was not closed
195 * @ret s_PXENV_TFTP_CLOSE::Status PXE status code
198 * Close a connection previously opened with pxenv_tftp_open(). You
199 * must have previously opened a connection with pxenv_tftp_open().
201 * On x86, you must set the s_PXE::StatusCallout field to a nonzero
202 * value before calling this function in protected mode. You cannot
203 * call this function with a 32-bit stack segment. (See the relevant
204 * @ref pxe_x86_pmode16 "implementation note" for more details.)
206 PXENV_EXIT_t pxenv_tftp_close ( struct s_PXENV_TFTP_CLOSE *tftp_close ) {
207 DBG ( "PXENV_TFTP_CLOSE" );
209 close ( pxe_single_fd );
211 tftp_close->Status = PXENV_STATUS_SUCCESS;
212 return PXENV_EXIT_SUCCESS;
218 * @v tftp_read Pointer to a struct s_PXENV_TFTP_READ
219 * @v s_PXENV_TFTP_READ::Buffer Address of data buffer
220 * @ret #PXENV_EXIT_SUCCESS Data was read successfully
221 * @ret #PXENV_EXIT_FAILURE Data was not read
222 * @ret s_PXENV_TFTP_READ::Status PXE status code
223 * @ret s_PXENV_TFTP_READ::PacketNumber TFTP packet number
224 * @ret s_PXENV_TFTP_READ::BufferSize Length of data written into buffer
226 * Reads a single packet from a connection previously opened with
227 * pxenv_tftp_open() into the data buffer pointed to by
228 * s_PXENV_TFTP_READ::Buffer. You must have previously opened a
229 * connection with pxenv_tftp_open(). The data written into
230 * s_PXENV_TFTP_READ::Buffer is just the file data; the various
231 * network headers have already been removed.
233 * The buffer must be large enough to contain a packet of the size
234 * negotiated via the s_PXENV_TFTP_OPEN::PacketSize field in the
235 * pxenv_tftp_open() call. It is worth noting that the PXE
236 * specification does @b not require the caller to fill in
237 * s_PXENV_TFTP_READ::BufferSize before calling pxenv_tftp_read(), so
238 * the PXE stack is free to ignore whatever value the caller might
239 * place there and just assume that the buffer is large enough. That
240 * said, it may be worth the caller always filling in
241 * s_PXENV_TFTP_READ::BufferSize to guard against PXE stacks that
242 * mistake it for an input parameter.
244 * The length of the TFTP data packet will be returned via
245 * s_PXENV_TFTP_READ::BufferSize. If this length is less than the
246 * blksize negotiated via s_PXENV_TFTP_OPEN::PacketSize in the call to
247 * pxenv_tftp_open(), this indicates that the block is the last block
248 * in the file. Note that zero is a valid length for
249 * s_PXENV_TFTP_READ::BufferSize, and will occur when the length of
250 * the file is a multiple of the blksize.
252 * The PXE specification doesn't actually state that calls to
253 * pxenv_tftp_read() will return the data packets in strict sequential
254 * order, though most PXE stacks will probably do so. The sequence
255 * number of the packet will be returned in
256 * s_PXENV_TFTP_READ::PacketNumber. The first packet in the file has
257 * a sequence number of one, not zero.
259 * To guard against flawed PXE stacks, the caller should probably set
260 * s_PXENV_TFTP_READ::PacketNumber to one less than the expected
261 * returned value (i.e. set it to zero for the first call to
262 * pxenv_tftp_read() and then re-use the returned s_PXENV_TFTP_READ
263 * parameter block for subsequent calls without modifying
264 * s_PXENV_TFTP_READ::PacketNumber between calls). The caller should
265 * also guard against potential problems caused by flawed
266 * implementations returning the occasional duplicate packet, by
267 * checking that the value returned in s_PXENV_TFTP_READ::PacketNumber
268 * is as expected (i.e. one greater than that returned from the
269 * previous call to pxenv_tftp_read()).
271 * On x86, you must set the s_PXE::StatusCallout field to a nonzero
272 * value before calling this function in protected mode. You cannot
273 * call this function with a 32-bit stack segment. (See the relevant
274 * @ref pxe_x86_pmode16 "implementation note" for more details.)
276 PXENV_EXIT_t pxenv_tftp_read ( struct s_PXENV_TFTP_READ *tftp_read ) {
280 DBG ( "PXENV_TFTP_READ to %04x:%04x",
281 tftp_read->Buffer.segment, tftp_read->Buffer.offset );
283 buffer = real_to_user ( tftp_read->Buffer.segment,
284 tftp_read->Buffer.offset );
285 len = pxe_tftp_read_all ( pxe_single_fd, buffer, pxe_single_blksize );
287 tftp_read->Status = PXENV_STATUS ( len );
288 return PXENV_EXIT_FAILURE;
291 tftp_read->BufferSize = len;
292 tftp_read->PacketNumber = ++pxe_single_blkidx;
294 tftp_read->Status = PXENV_STATUS_SUCCESS;
295 return PXENV_EXIT_SUCCESS;
299 * TFTP/MTFTP read file
301 * @v tftp_read_file Pointer to a struct s_PXENV_TFTP_READ_FILE
302 * @v s_PXENV_TFTP_READ_FILE::FileName File name
303 * @v s_PXENV_TFTP_READ_FILE::BufferSize Size of the receive buffer
304 * @v s_PXENV_TFTP_READ_FILE::Buffer Address of the receive buffer
305 * @v s_PXENV_TFTP_READ_FILE::ServerIPAddress TFTP server IP address
306 * @v s_PXENV_TFTP_READ_FILE::GatewayIPAddress Relay agent IP address
307 * @v s_PXENV_TFTP_READ_FILE::McastIPAddress File's multicast IP address
308 * @v s_PXENV_TFTP_READ_FILE::TFTPClntPort Client multicast UDP port
309 * @v s_PXENV_TFTP_READ_FILE::TFTPSrvPort Server multicast UDP port
310 * @v s_PXENV_TFTP_READ_FILE::TFTPOpenTimeOut Time to wait for first packet
311 * @v s_PXENV_TFTP_READ_FILE::TFTPReopenDelay MTFTP inactivity timeout
312 * @ret #PXENV_EXIT_SUCCESS File downloaded successfully
313 * @ret #PXENV_EXIT_FAILURE File not downloaded
314 * @ret s_PXENV_TFTP_READ_FILE::Status PXE status code
315 * @ret s_PXENV_TFTP_READ_FILE::BufferSize Length of downloaded file
317 * Downloads an entire file via either TFTP or MTFTP into the buffer
318 * pointed to by s_PXENV_TFTP_READ_FILE::Buffer.
320 * The PXE specification does not make it clear how the caller
321 * requests that MTFTP be used rather than TFTP (or vice versa). One
322 * reasonable guess is that setting
323 * s_PXENV_TFTP_READ_FILE::McastIPAddress to 0.0.0.0 would cause TFTP
324 * to be used instead of MTFTP, though it is conceivable that some PXE
325 * stacks would interpret that as "use the DHCP-provided multicast IP
326 * address" instead. Some PXE stacks will not implement MTFTP at all,
327 * and will always use TFTP.
329 * It is not specified whether or not
330 * s_PXENV_TFTP_READ_FILE::TFTPSrvPort will be used as the TFTP server
331 * port for TFTP (rather than MTFTP) downloads. Callers should assume
332 * that the only way to access a TFTP server on a non-standard port is
333 * to use pxenv_tftp_open() and pxenv_tftp_read().
335 * If s_PXENV_TFTP_READ_FILE::GatewayIPAddress is 0.0.0.0, normal IP
336 * routing will take place. See the relevant
337 * @ref pxe_routing "implementation note" for more details.
339 * It is interesting to note that s_PXENV_TFTP_READ_FILE::Buffer is an
340 * #ADDR32_t type, i.e. nominally a flat physical address. Some PXE
341 * NBPs (e.g. NTLDR) are known to call pxenv_tftp_read_file() in real
342 * mode with s_PXENV_TFTP_READ_FILE::Buffer set to an address above
343 * 1MB. This means that PXE stacks must be prepared to write to areas
344 * outside base memory. Exactly how this is to be achieved is not
345 * specified, though using INT 15,87 is as close to a standard method
346 * as any, and should probably be used. Switching to protected-mode
347 * in order to access high memory will fail if pxenv_tftp_read_file()
348 * is called in V86 mode; it is reasonably to expect that a V86
349 * monitor would intercept the relatively well-defined INT 15,87 if it
350 * wants the PXE stack to be able to write to high memory.
352 * Things get even more interesting if pxenv_tftp_read_file() is
353 * called in protected mode, because there is then absolutely no way
354 * for the PXE stack to write to an absolute physical address. You
355 * can't even get around the problem by creating a special "access
356 * everything" segment in the s_PXE data structure, because the
357 * #SEGDESC_t descriptors are limited to 64kB in size.
359 * Previous versions of the PXE specification (e.g. WfM 1.1a) provide
360 * a separate API call, %pxenv_tftp_read_file_pmode(), specifically to
361 * work around this problem. The s_PXENV_TFTP_READ_FILE_PMODE
362 * parameter block splits s_PXENV_TFTP_READ_FILE::Buffer into
363 * s_PXENV_TFTP_READ_FILE_PMODE::BufferSelector and
364 * s_PXENV_TFTP_READ_FILE_PMODE::BufferOffset, i.e. it provides a
365 * protected-mode segment:offset address for the data buffer. This
366 * API call is no longer present in version 2.1 of the PXE
369 * Etherboot makes the assumption that s_PXENV_TFTP_READ_FILE::Buffer
370 * is an offset relative to the caller's data segment, when
371 * pxenv_tftp_read_file() is called in protected mode.
373 * On x86, you must set the s_PXE::StatusCallout field to a nonzero
374 * value before calling this function in protected mode. You cannot
375 * call this function with a 32-bit stack segment. (See the relevant
376 * @ref pxe_x86_pmode16 "implementation note" for more details.)
378 * @note Microsoft's NTLDR assumes that the filename passed in via
379 * s_PXENV_TFTP_READ_FILE::FileName will be stored in the "file" field
380 * of the stored DHCPACK packet, whence it will be returned via any
381 * subsequent calls to pxenv_get_cached_info(). Though this is
382 * essentially a bug in the Intel PXE implementation (not, for once,
383 * in the specification!), it is a bug that Microsoft relies upon, and
384 * so we implement this bug-for-bug compatibility by overwriting the
385 * filename stored DHCPACK packet with the filename passed in
386 * s_PXENV_TFTP_READ_FILE::FileName.
389 PXENV_EXIT_t pxenv_tftp_read_file ( struct s_PXENV_TFTP_READ_FILE
391 char uri_string[PXE_URI_LEN];
397 DBG ( "PXENV_TFTP_READ_FILE" );
400 pxe_tftp_build_uri ( uri_string, tftp_read_file->ServerIPAddress,
401 tftp_read_file->TFTPSrvPort,
402 tftp_read_file->FileName, 0 );
403 DBG ( " %s", uri_string );
405 DBG ( " to %08lx+%lx", tftp_read_file->Buffer,
406 tftp_read_file->BufferSize );
409 fd = open ( uri_string );
411 tftp_read_file->Status = PXENV_STATUS ( fd );
412 return PXENV_EXIT_FAILURE;
416 buffer = phys_to_user ( tftp_read_file->Buffer );
417 len = pxe_tftp_read_all ( fd, buffer, tftp_read_file->BufferSize );
422 tftp_read_file->BufferSize = len;
423 tftp_read_file->Status = PXENV_STATUS ( rc );
424 return ( rc ? PXENV_EXIT_FAILURE : PXENV_EXIT_SUCCESS );
430 * @v tftp_get_fsize Pointer to a struct s_PXENV_TFTP_GET_FSIZE
431 * @v s_PXENV_TFTP_GET_FSIZE::ServerIPAddress TFTP server IP address
432 * @v s_PXENV_TFTP_GET_FSIZE::GatewayIPAddress Relay agent IP address
433 * @v s_PXENV_TFTP_GET_FSIZE::FileName File name
434 * @ret #PXENV_EXIT_SUCCESS File size was determined successfully
435 * @ret #PXENV_EXIT_FAILURE File size was not determined
436 * @ret s_PXENV_TFTP_GET_FSIZE::Status PXE status code
437 * @ret s_PXENV_TFTP_GET_FSIZE::FileSize File size
439 * Determine the size of a file on a TFTP server. This uses the
440 * "tsize" TFTP option, and so will not work with a TFTP server that
441 * does not support TFTP options, or that does not support the "tsize"
444 * The PXE specification states that this API call will @b not open a
445 * TFTP connection for subsequent use with pxenv_tftp_read(). (This
446 * is somewhat daft, since the only way to obtain the file size via
447 * the "tsize" option involves issuing a TFTP open request, but that's
450 * You cannot call pxenv_tftp_get_fsize() while a TFTP or UDP
451 * connection is open.
453 * If s_PXENV_TFTP_GET_FSIZE::GatewayIPAddress is 0.0.0.0, normal IP
454 * routing will take place. See the relevant
455 * @ref pxe_routing "implementation note" for more details.
457 * On x86, you must set the s_PXE::StatusCallout field to a nonzero
458 * value before calling this function in protected mode. You cannot
459 * call this function with a 32-bit stack segment. (See the relevant
460 * @ref pxe_x86_pmode16 "implementation note" for more details.)
462 * @note There is no way to specify the TFTP server port with this API
463 * call. Though you can open a file using a non-standard TFTP server
464 * port (via s_PXENV_TFTP_OPEN::TFTPPort or, potentially,
465 * s_PXENV_TFTP_READ_FILE::TFTPSrvPort), you can only get the size of
466 * a file from a TFTP server listening on the standard TFTP port.
467 * "Consistency" is not a word in Intel's vocabulary.
469 PXENV_EXIT_t pxenv_tftp_get_fsize ( struct s_PXENV_TFTP_GET_FSIZE
471 char uri_string[PXE_URI_LEN];
475 DBG ( "PXENV_TFTP_GET_FSIZE" );
478 pxe_tftp_build_uri ( uri_string, tftp_get_fsize->ServerIPAddress,
479 0, tftp_get_fsize->FileName, 0 );
480 DBG ( " %s", uri_string );
483 fd = open ( uri_string );
485 tftp_get_fsize->Status = PXENV_STATUS ( fd );
486 return PXENV_EXIT_FAILURE;
493 tftp_get_fsize->Status = PXENV_STATUS ( size );
494 return PXENV_EXIT_FAILURE;
497 tftp_get_fsize->FileSize = size;
498 tftp_get_fsize->Status = PXENV_STATUS_SUCCESS;
499 return PXENV_EXIT_SUCCESS;