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 22KB

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