You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

pxe_tftp.c 21KB

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