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

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