Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

pxe_tftp.c 21KB

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