Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

pxe_tftp.c 21KB

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