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

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