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

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