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

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