tftp.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806
  1. /*
  2. * Copyright 1994, 1995, 2000 Neil Russell.
  3. * (See License)
  4. * Copyright 2000, 2001 DENX Software Engineering, Wolfgang Denk, wd@denx.de
  5. * Copyright 2011 Comelit Group SpA,
  6. * Luca Ceresoli <luca.ceresoli@comelit.it>
  7. */
  8. #include <common.h>
  9. #include <command.h>
  10. #include <efi_loader.h>
  11. #include <env.h>
  12. #include <mapmem.h>
  13. #include <net.h>
  14. #include <net/tftp.h>
  15. #include "bootp.h"
  16. #ifdef CONFIG_SYS_DIRECT_FLASH_TFTP
  17. #include <flash.h>
  18. #endif
  19. DECLARE_GLOBAL_DATA_PTR;
  20. /* Well known TFTP port # */
  21. #define WELL_KNOWN_PORT 69
  22. /* Millisecs to timeout for lost pkt */
  23. #define TIMEOUT 5000UL
  24. #ifndef CONFIG_NET_RETRY_COUNT
  25. /* # of timeouts before giving up */
  26. # define TIMEOUT_COUNT 10
  27. #else
  28. # define TIMEOUT_COUNT (CONFIG_NET_RETRY_COUNT * 2)
  29. #endif
  30. /* Number of "loading" hashes per line (for checking the image size) */
  31. #define HASHES_PER_LINE 65
  32. /*
  33. * TFTP operations.
  34. */
  35. #define TFTP_RRQ 1
  36. #define TFTP_WRQ 2
  37. #define TFTP_DATA 3
  38. #define TFTP_ACK 4
  39. #define TFTP_ERROR 5
  40. #define TFTP_OACK 6
  41. static ulong timeout_ms = TIMEOUT;
  42. static int timeout_count_max = TIMEOUT_COUNT;
  43. static ulong time_start; /* Record time we started tftp */
  44. /*
  45. * These globals govern the timeout behavior when attempting a connection to a
  46. * TFTP server. tftp_timeout_ms specifies the number of milliseconds to
  47. * wait for the server to respond to initial connection. Second global,
  48. * tftp_timeout_count_max, gives the number of such connection retries.
  49. * tftp_timeout_count_max must be non-negative and tftp_timeout_ms must be
  50. * positive. The globals are meant to be set (and restored) by code needing
  51. * non-standard timeout behavior when initiating a TFTP transfer.
  52. */
  53. ulong tftp_timeout_ms = TIMEOUT;
  54. int tftp_timeout_count_max = TIMEOUT_COUNT;
  55. enum {
  56. TFTP_ERR_UNDEFINED = 0,
  57. TFTP_ERR_FILE_NOT_FOUND = 1,
  58. TFTP_ERR_ACCESS_DENIED = 2,
  59. TFTP_ERR_DISK_FULL = 3,
  60. TFTP_ERR_UNEXPECTED_OPCODE = 4,
  61. TFTP_ERR_UNKNOWN_TRANSFER_ID = 5,
  62. TFTP_ERR_FILE_ALREADY_EXISTS = 6,
  63. };
  64. static struct in_addr tftp_remote_ip;
  65. /* The UDP port at their end */
  66. static int tftp_remote_port;
  67. /* The UDP port at our end */
  68. static int tftp_our_port;
  69. static int timeout_count;
  70. /* packet sequence number */
  71. static ulong tftp_cur_block;
  72. /* last packet sequence number received */
  73. static ulong tftp_prev_block;
  74. /* count of sequence number wraparounds */
  75. static ulong tftp_block_wrap;
  76. /* memory offset due to wrapping */
  77. static ulong tftp_block_wrap_offset;
  78. static int tftp_state;
  79. static ulong tftp_load_addr;
  80. #ifdef CONFIG_LMB
  81. static ulong tftp_load_size;
  82. #endif
  83. #ifdef CONFIG_TFTP_TSIZE
  84. /* The file size reported by the server */
  85. static int tftp_tsize;
  86. /* The number of hashes we printed */
  87. static short tftp_tsize_num_hash;
  88. #endif
  89. #ifdef CONFIG_CMD_TFTPPUT
  90. /* 1 if writing, else 0 */
  91. static int tftp_put_active;
  92. /* 1 if we have sent the last block */
  93. static int tftp_put_final_block_sent;
  94. #else
  95. #define tftp_put_active 0
  96. #endif
  97. #define STATE_SEND_RRQ 1
  98. #define STATE_DATA 2
  99. #define STATE_TOO_LARGE 3
  100. #define STATE_BAD_MAGIC 4
  101. #define STATE_OACK 5
  102. #define STATE_RECV_WRQ 6
  103. #define STATE_SEND_WRQ 7
  104. /* default TFTP block size */
  105. #define TFTP_BLOCK_SIZE 512
  106. /* sequence number is 16 bit */
  107. #define TFTP_SEQUENCE_SIZE ((ulong)(1<<16))
  108. #define DEFAULT_NAME_LEN (8 + 4 + 1)
  109. static char default_filename[DEFAULT_NAME_LEN];
  110. #ifndef CONFIG_TFTP_FILE_NAME_MAX_LEN
  111. #define MAX_LEN 128
  112. #else
  113. #define MAX_LEN CONFIG_TFTP_FILE_NAME_MAX_LEN
  114. #endif
  115. static char tftp_filename[MAX_LEN];
  116. /* 512 is poor choice for ethernet, MTU is typically 1500.
  117. * Minus eth.hdrs thats 1468. Can get 2x better throughput with
  118. * almost-MTU block sizes. At least try... fall back to 512 if need be.
  119. * (but those using CONFIG_IP_DEFRAG may want to set a larger block in cfg file)
  120. */
  121. #ifdef CONFIG_TFTP_BLOCKSIZE
  122. #define TFTP_MTU_BLOCKSIZE CONFIG_TFTP_BLOCKSIZE
  123. #else
  124. #define TFTP_MTU_BLOCKSIZE 1468
  125. #endif
  126. static unsigned short tftp_block_size = TFTP_BLOCK_SIZE;
  127. static unsigned short tftp_block_size_option = TFTP_MTU_BLOCKSIZE;
  128. static inline int store_block(int block, uchar *src, unsigned int len)
  129. {
  130. ulong offset = block * tftp_block_size + tftp_block_wrap_offset;
  131. ulong newsize = offset + len;
  132. ulong store_addr = tftp_load_addr + offset;
  133. #ifdef CONFIG_SYS_DIRECT_FLASH_TFTP
  134. int i, rc = 0;
  135. for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; i++) {
  136. /* start address in flash? */
  137. if (flash_info[i].flash_id == FLASH_UNKNOWN)
  138. continue;
  139. if (store_addr >= flash_info[i].start[0]) {
  140. rc = 1;
  141. break;
  142. }
  143. }
  144. if (rc) { /* Flash is destination for this packet */
  145. rc = flash_write((char *)src, store_addr, len);
  146. if (rc) {
  147. flash_perror(rc);
  148. return rc;
  149. }
  150. } else
  151. #endif /* CONFIG_SYS_DIRECT_FLASH_TFTP */
  152. {
  153. void *ptr;
  154. #ifdef CONFIG_LMB
  155. if (store_addr < tftp_load_addr ||
  156. store_addr + len > tftp_load_addr + tftp_load_size) {
  157. puts("\nTFTP error: ");
  158. puts("trying to overwrite reserved memory...\n");
  159. return -1;
  160. }
  161. #endif
  162. ptr = map_sysmem(store_addr, len);
  163. memcpy(ptr, src, len);
  164. unmap_sysmem(ptr);
  165. }
  166. if (net_boot_file_size < newsize)
  167. net_boot_file_size = newsize;
  168. return 0;
  169. }
  170. /* Clear our state ready for a new transfer */
  171. static void new_transfer(void)
  172. {
  173. tftp_prev_block = 0;
  174. tftp_block_wrap = 0;
  175. tftp_block_wrap_offset = 0;
  176. #ifdef CONFIG_CMD_TFTPPUT
  177. tftp_put_final_block_sent = 0;
  178. #endif
  179. }
  180. #ifdef CONFIG_CMD_TFTPPUT
  181. /**
  182. * Load the next block from memory to be sent over tftp.
  183. *
  184. * @param block Block number to send
  185. * @param dst Destination buffer for data
  186. * @param len Number of bytes in block (this one and every other)
  187. * @return number of bytes loaded
  188. */
  189. static int load_block(unsigned block, uchar *dst, unsigned len)
  190. {
  191. /* We may want to get the final block from the previous set */
  192. ulong offset = ((int)block - 1) * len + tftp_block_wrap_offset;
  193. ulong tosend = len;
  194. tosend = min(net_boot_file_size - offset, tosend);
  195. (void)memcpy(dst, (void *)(save_addr + offset), tosend);
  196. debug("%s: block=%d, offset=%ld, len=%d, tosend=%ld\n", __func__,
  197. block, offset, len, tosend);
  198. return tosend;
  199. }
  200. #endif
  201. static void tftp_send(void);
  202. static void tftp_timeout_handler(void);
  203. /**********************************************************************/
  204. static void show_block_marker(void)
  205. {
  206. #ifdef CONFIG_TFTP_TSIZE
  207. if (tftp_tsize) {
  208. ulong pos = tftp_cur_block * tftp_block_size +
  209. tftp_block_wrap_offset;
  210. if (pos > tftp_tsize)
  211. pos = tftp_tsize;
  212. while (tftp_tsize_num_hash < pos * 50 / tftp_tsize) {
  213. putc('#');
  214. tftp_tsize_num_hash++;
  215. }
  216. } else
  217. #endif
  218. {
  219. if (((tftp_cur_block - 1) % 10) == 0)
  220. putc('#');
  221. else if ((tftp_cur_block % (10 * HASHES_PER_LINE)) == 0)
  222. puts("\n\t ");
  223. }
  224. }
  225. /**
  226. * restart the current transfer due to an error
  227. *
  228. * @param msg Message to print for user
  229. */
  230. static void restart(const char *msg)
  231. {
  232. printf("\n%s; starting again\n", msg);
  233. net_start_again();
  234. }
  235. /*
  236. * Check if the block number has wrapped, and update progress
  237. *
  238. * TODO: The egregious use of global variables in this file should be tidied.
  239. */
  240. static void update_block_number(void)
  241. {
  242. /*
  243. * RFC1350 specifies that the first data packet will
  244. * have sequence number 1. If we receive a sequence
  245. * number of 0 this means that there was a wrap
  246. * around of the (16 bit) counter.
  247. */
  248. if (tftp_cur_block == 0 && tftp_prev_block != 0) {
  249. tftp_block_wrap++;
  250. tftp_block_wrap_offset += tftp_block_size * TFTP_SEQUENCE_SIZE;
  251. timeout_count = 0; /* we've done well, reset the timeout */
  252. } else {
  253. show_block_marker();
  254. }
  255. }
  256. /* The TFTP get or put is complete */
  257. static void tftp_complete(void)
  258. {
  259. #ifdef CONFIG_TFTP_TSIZE
  260. /* Print hash marks for the last packet received */
  261. while (tftp_tsize && tftp_tsize_num_hash < 49) {
  262. putc('#');
  263. tftp_tsize_num_hash++;
  264. }
  265. puts(" ");
  266. print_size(tftp_tsize, "");
  267. #endif
  268. time_start = get_timer(time_start);
  269. if (time_start > 0) {
  270. puts("\n\t "); /* Line up with "Loading: " */
  271. print_size(net_boot_file_size /
  272. time_start * 1000, "/s");
  273. }
  274. puts("\ndone\n");
  275. net_set_state(NETLOOP_SUCCESS);
  276. }
  277. static void tftp_send(void)
  278. {
  279. uchar *pkt;
  280. uchar *xp;
  281. int len = 0;
  282. ushort *s;
  283. /*
  284. * We will always be sending some sort of packet, so
  285. * cobble together the packet headers now.
  286. */
  287. pkt = net_tx_packet + net_eth_hdr_size() + IP_UDP_HDR_SIZE;
  288. switch (tftp_state) {
  289. case STATE_SEND_RRQ:
  290. case STATE_SEND_WRQ:
  291. xp = pkt;
  292. s = (ushort *)pkt;
  293. #ifdef CONFIG_CMD_TFTPPUT
  294. *s++ = htons(tftp_state == STATE_SEND_RRQ ? TFTP_RRQ :
  295. TFTP_WRQ);
  296. #else
  297. *s++ = htons(TFTP_RRQ);
  298. #endif
  299. pkt = (uchar *)s;
  300. strcpy((char *)pkt, tftp_filename);
  301. pkt += strlen(tftp_filename) + 1;
  302. strcpy((char *)pkt, "octet");
  303. pkt += 5 /*strlen("octet")*/ + 1;
  304. strcpy((char *)pkt, "timeout");
  305. pkt += 7 /*strlen("timeout")*/ + 1;
  306. sprintf((char *)pkt, "%lu", timeout_ms / 1000);
  307. debug("send option \"timeout %s\"\n", (char *)pkt);
  308. pkt += strlen((char *)pkt) + 1;
  309. #ifdef CONFIG_TFTP_TSIZE
  310. pkt += sprintf((char *)pkt, "tsize%c%u%c",
  311. 0, net_boot_file_size, 0);
  312. #endif
  313. /* try for more effic. blk size */
  314. pkt += sprintf((char *)pkt, "blksize%c%d%c",
  315. 0, tftp_block_size_option, 0);
  316. len = pkt - xp;
  317. break;
  318. case STATE_OACK:
  319. case STATE_RECV_WRQ:
  320. case STATE_DATA:
  321. xp = pkt;
  322. s = (ushort *)pkt;
  323. s[0] = htons(TFTP_ACK);
  324. s[1] = htons(tftp_cur_block);
  325. pkt = (uchar *)(s + 2);
  326. #ifdef CONFIG_CMD_TFTPPUT
  327. if (tftp_put_active) {
  328. int toload = tftp_block_size;
  329. int loaded = load_block(tftp_cur_block, pkt, toload);
  330. s[0] = htons(TFTP_DATA);
  331. pkt += loaded;
  332. tftp_put_final_block_sent = (loaded < toload);
  333. }
  334. #endif
  335. len = pkt - xp;
  336. break;
  337. case STATE_TOO_LARGE:
  338. xp = pkt;
  339. s = (ushort *)pkt;
  340. *s++ = htons(TFTP_ERROR);
  341. *s++ = htons(3);
  342. pkt = (uchar *)s;
  343. strcpy((char *)pkt, "File too large");
  344. pkt += 14 /*strlen("File too large")*/ + 1;
  345. len = pkt - xp;
  346. break;
  347. case STATE_BAD_MAGIC:
  348. xp = pkt;
  349. s = (ushort *)pkt;
  350. *s++ = htons(TFTP_ERROR);
  351. *s++ = htons(2);
  352. pkt = (uchar *)s;
  353. strcpy((char *)pkt, "File has bad magic");
  354. pkt += 18 /*strlen("File has bad magic")*/ + 1;
  355. len = pkt - xp;
  356. break;
  357. }
  358. net_send_udp_packet(net_server_ethaddr, tftp_remote_ip,
  359. tftp_remote_port, tftp_our_port, len);
  360. }
  361. #ifdef CONFIG_CMD_TFTPPUT
  362. static void icmp_handler(unsigned type, unsigned code, unsigned dest,
  363. struct in_addr sip, unsigned src, uchar *pkt,
  364. unsigned len)
  365. {
  366. if (type == ICMP_NOT_REACH && code == ICMP_NOT_REACH_PORT) {
  367. /* Oh dear the other end has gone away */
  368. restart("TFTP server died");
  369. }
  370. }
  371. #endif
  372. static void tftp_handler(uchar *pkt, unsigned dest, struct in_addr sip,
  373. unsigned src, unsigned len)
  374. {
  375. __be16 proto;
  376. __be16 *s;
  377. int i;
  378. if (dest != tftp_our_port) {
  379. return;
  380. }
  381. if (tftp_state != STATE_SEND_RRQ && src != tftp_remote_port &&
  382. tftp_state != STATE_RECV_WRQ && tftp_state != STATE_SEND_WRQ)
  383. return;
  384. if (len < 2)
  385. return;
  386. len -= 2;
  387. /* warning: don't use increment (++) in ntohs() macros!! */
  388. s = (__be16 *)pkt;
  389. proto = *s++;
  390. pkt = (uchar *)s;
  391. switch (ntohs(proto)) {
  392. case TFTP_RRQ:
  393. break;
  394. case TFTP_ACK:
  395. #ifdef CONFIG_CMD_TFTPPUT
  396. if (tftp_put_active) {
  397. if (tftp_put_final_block_sent) {
  398. tftp_complete();
  399. } else {
  400. /*
  401. * Move to the next block. We want our block
  402. * count to wrap just like the other end!
  403. */
  404. int block = ntohs(*s);
  405. int ack_ok = (tftp_cur_block == block);
  406. tftp_cur_block = (unsigned short)(block + 1);
  407. update_block_number();
  408. if (ack_ok)
  409. tftp_send(); /* Send next data block */
  410. }
  411. }
  412. #endif
  413. break;
  414. default:
  415. break;
  416. #ifdef CONFIG_CMD_TFTPSRV
  417. case TFTP_WRQ:
  418. debug("Got WRQ\n");
  419. tftp_remote_ip = sip;
  420. tftp_remote_port = src;
  421. tftp_our_port = 1024 + (get_timer(0) % 3072);
  422. new_transfer();
  423. tftp_send(); /* Send ACK(0) */
  424. break;
  425. #endif
  426. case TFTP_OACK:
  427. debug("Got OACK: %s %s\n",
  428. pkt, pkt + strlen((char *)pkt) + 1);
  429. tftp_state = STATE_OACK;
  430. tftp_remote_port = src;
  431. /*
  432. * Check for 'blksize' option.
  433. * Careful: "i" is signed, "len" is unsigned, thus
  434. * something like "len-8" may give a *huge* number
  435. */
  436. for (i = 0; i+8 < len; i++) {
  437. if (strcmp((char *)pkt + i, "blksize") == 0) {
  438. tftp_block_size = (unsigned short)
  439. simple_strtoul((char *)pkt + i + 8,
  440. NULL, 10);
  441. debug("Blocksize ack: %s, %d\n",
  442. (char *)pkt + i + 8, tftp_block_size);
  443. }
  444. #ifdef CONFIG_TFTP_TSIZE
  445. if (strcmp((char *)pkt+i, "tsize") == 0) {
  446. tftp_tsize = simple_strtoul((char *)pkt + i + 6,
  447. NULL, 10);
  448. debug("size = %s, %d\n",
  449. (char *)pkt + i + 6, tftp_tsize);
  450. }
  451. #endif
  452. }
  453. #ifdef CONFIG_CMD_TFTPPUT
  454. if (tftp_put_active) {
  455. /* Get ready to send the first block */
  456. tftp_state = STATE_DATA;
  457. tftp_cur_block++;
  458. }
  459. #endif
  460. tftp_send(); /* Send ACK or first data block */
  461. break;
  462. case TFTP_DATA:
  463. if (len < 2)
  464. return;
  465. len -= 2;
  466. tftp_cur_block = ntohs(*(__be16 *)pkt);
  467. update_block_number();
  468. if (tftp_state == STATE_SEND_RRQ)
  469. debug("Server did not acknowledge timeout option!\n");
  470. if (tftp_state == STATE_SEND_RRQ || tftp_state == STATE_OACK ||
  471. tftp_state == STATE_RECV_WRQ) {
  472. /* first block received */
  473. tftp_state = STATE_DATA;
  474. tftp_remote_port = src;
  475. new_transfer();
  476. if (tftp_cur_block != 1) { /* Assertion */
  477. puts("\nTFTP error: ");
  478. printf("First block is not block 1 (%ld)\n",
  479. tftp_cur_block);
  480. puts("Starting again\n\n");
  481. net_start_again();
  482. break;
  483. }
  484. }
  485. if (tftp_cur_block == tftp_prev_block) {
  486. /* Same block again; ignore it. */
  487. break;
  488. }
  489. tftp_prev_block = tftp_cur_block;
  490. timeout_count_max = tftp_timeout_count_max;
  491. net_set_timeout_handler(timeout_ms, tftp_timeout_handler);
  492. if (store_block(tftp_cur_block - 1, pkt + 2, len)) {
  493. eth_halt();
  494. net_set_state(NETLOOP_FAIL);
  495. break;
  496. }
  497. /*
  498. * Acknowledge the block just received, which will prompt
  499. * the remote for the next one.
  500. */
  501. tftp_send();
  502. if (len < tftp_block_size)
  503. tftp_complete();
  504. break;
  505. case TFTP_ERROR:
  506. printf("\nTFTP error: '%s' (%d)\n",
  507. pkt + 2, ntohs(*(__be16 *)pkt));
  508. switch (ntohs(*(__be16 *)pkt)) {
  509. case TFTP_ERR_FILE_NOT_FOUND:
  510. case TFTP_ERR_ACCESS_DENIED:
  511. puts("Not retrying...\n");
  512. eth_halt();
  513. net_set_state(NETLOOP_FAIL);
  514. break;
  515. case TFTP_ERR_UNDEFINED:
  516. case TFTP_ERR_DISK_FULL:
  517. case TFTP_ERR_UNEXPECTED_OPCODE:
  518. case TFTP_ERR_UNKNOWN_TRANSFER_ID:
  519. case TFTP_ERR_FILE_ALREADY_EXISTS:
  520. default:
  521. puts("Starting again\n\n");
  522. net_start_again();
  523. break;
  524. }
  525. break;
  526. }
  527. }
  528. static void tftp_timeout_handler(void)
  529. {
  530. if (++timeout_count > timeout_count_max) {
  531. restart("Retry count exceeded");
  532. } else {
  533. puts("T ");
  534. net_set_timeout_handler(timeout_ms, tftp_timeout_handler);
  535. if (tftp_state != STATE_RECV_WRQ)
  536. tftp_send();
  537. }
  538. }
  539. /* Initialize tftp_load_addr and tftp_load_size from load_addr and lmb */
  540. static int tftp_init_load_addr(void)
  541. {
  542. #ifdef CONFIG_LMB
  543. struct lmb lmb;
  544. phys_size_t max_size;
  545. lmb_init_and_reserve(&lmb, gd->bd, (void *)gd->fdt_blob);
  546. max_size = lmb_get_free_size(&lmb, load_addr);
  547. if (!max_size)
  548. return -1;
  549. tftp_load_size = max_size;
  550. #endif
  551. tftp_load_addr = load_addr;
  552. return 0;
  553. }
  554. void tftp_start(enum proto_t protocol)
  555. {
  556. #if CONFIG_NET_TFTP_VARS
  557. char *ep; /* Environment pointer */
  558. /*
  559. * Allow the user to choose TFTP blocksize and timeout.
  560. * TFTP protocol has a minimal timeout of 1 second.
  561. */
  562. ep = env_get("tftpblocksize");
  563. if (ep != NULL)
  564. tftp_block_size_option = simple_strtol(ep, NULL, 10);
  565. ep = env_get("tftptimeout");
  566. if (ep != NULL)
  567. timeout_ms = simple_strtol(ep, NULL, 10);
  568. if (timeout_ms < 1000) {
  569. printf("TFTP timeout (%ld ms) too low, set min = 1000 ms\n",
  570. timeout_ms);
  571. timeout_ms = 1000;
  572. }
  573. ep = env_get("tftptimeoutcountmax");
  574. if (ep != NULL)
  575. tftp_timeout_count_max = simple_strtol(ep, NULL, 10);
  576. if (tftp_timeout_count_max < 0) {
  577. printf("TFTP timeout count max (%d ms) negative, set to 0\n",
  578. tftp_timeout_count_max);
  579. tftp_timeout_count_max = 0;
  580. }
  581. #endif
  582. debug("TFTP blocksize = %i, timeout = %ld ms\n",
  583. tftp_block_size_option, timeout_ms);
  584. tftp_remote_ip = net_server_ip;
  585. if (!net_parse_bootfile(&tftp_remote_ip, tftp_filename, MAX_LEN)) {
  586. sprintf(default_filename, "%02X%02X%02X%02X.img",
  587. net_ip.s_addr & 0xFF,
  588. (net_ip.s_addr >> 8) & 0xFF,
  589. (net_ip.s_addr >> 16) & 0xFF,
  590. (net_ip.s_addr >> 24) & 0xFF);
  591. strncpy(tftp_filename, default_filename, DEFAULT_NAME_LEN);
  592. tftp_filename[DEFAULT_NAME_LEN - 1] = 0;
  593. printf("*** Warning: no boot file name; using '%s'\n",
  594. tftp_filename);
  595. }
  596. printf("Using %s device\n", eth_get_name());
  597. printf("TFTP %s server %pI4; our IP address is %pI4",
  598. #ifdef CONFIG_CMD_TFTPPUT
  599. protocol == TFTPPUT ? "to" : "from",
  600. #else
  601. "from",
  602. #endif
  603. &tftp_remote_ip, &net_ip);
  604. /* Check if we need to send across this subnet */
  605. if (net_gateway.s_addr && net_netmask.s_addr) {
  606. struct in_addr our_net;
  607. struct in_addr remote_net;
  608. our_net.s_addr = net_ip.s_addr & net_netmask.s_addr;
  609. remote_net.s_addr = tftp_remote_ip.s_addr & net_netmask.s_addr;
  610. if (our_net.s_addr != remote_net.s_addr)
  611. printf("; sending through gateway %pI4", &net_gateway);
  612. }
  613. putc('\n');
  614. printf("Filename '%s'.", tftp_filename);
  615. if (net_boot_file_expected_size_in_blocks) {
  616. printf(" Size is 0x%x Bytes = ",
  617. net_boot_file_expected_size_in_blocks << 9);
  618. print_size(net_boot_file_expected_size_in_blocks << 9, "");
  619. }
  620. putc('\n');
  621. #ifdef CONFIG_CMD_TFTPPUT
  622. tftp_put_active = (protocol == TFTPPUT);
  623. if (tftp_put_active) {
  624. printf("Save address: 0x%lx\n", save_addr);
  625. printf("Save size: 0x%lx\n", save_size);
  626. net_boot_file_size = save_size;
  627. puts("Saving: *\b");
  628. tftp_state = STATE_SEND_WRQ;
  629. new_transfer();
  630. } else
  631. #endif
  632. {
  633. if (tftp_init_load_addr()) {
  634. eth_halt();
  635. net_set_state(NETLOOP_FAIL);
  636. puts("\nTFTP error: ");
  637. puts("trying to overwrite reserved memory...\n");
  638. return;
  639. }
  640. printf("Load address: 0x%lx\n", tftp_load_addr);
  641. puts("Loading: *\b");
  642. tftp_state = STATE_SEND_RRQ;
  643. #ifdef CONFIG_CMD_BOOTEFI
  644. efi_set_bootdev("Net", "", tftp_filename);
  645. #endif
  646. }
  647. time_start = get_timer(0);
  648. timeout_count_max = tftp_timeout_count_max;
  649. net_set_timeout_handler(timeout_ms, tftp_timeout_handler);
  650. net_set_udp_handler(tftp_handler);
  651. #ifdef CONFIG_CMD_TFTPPUT
  652. net_set_icmp_handler(icmp_handler);
  653. #endif
  654. tftp_remote_port = WELL_KNOWN_PORT;
  655. timeout_count = 0;
  656. /* Use a pseudo-random port unless a specific port is set */
  657. tftp_our_port = 1024 + (get_timer(0) % 3072);
  658. #ifdef CONFIG_TFTP_PORT
  659. ep = env_get("tftpdstp");
  660. if (ep != NULL)
  661. tftp_remote_port = simple_strtol(ep, NULL, 10);
  662. ep = env_get("tftpsrcp");
  663. if (ep != NULL)
  664. tftp_our_port = simple_strtol(ep, NULL, 10);
  665. #endif
  666. tftp_cur_block = 0;
  667. /* zero out server ether in case the server ip has changed */
  668. memset(net_server_ethaddr, 0, 6);
  669. /* Revert tftp_block_size to dflt */
  670. tftp_block_size = TFTP_BLOCK_SIZE;
  671. #ifdef CONFIG_TFTP_TSIZE
  672. tftp_tsize = 0;
  673. tftp_tsize_num_hash = 0;
  674. #endif
  675. tftp_send();
  676. }
  677. #ifdef CONFIG_CMD_TFTPSRV
  678. void tftp_start_server(void)
  679. {
  680. tftp_filename[0] = 0;
  681. if (tftp_init_load_addr()) {
  682. eth_halt();
  683. net_set_state(NETLOOP_FAIL);
  684. puts("\nTFTP error: trying to overwrite reserved memory...\n");
  685. return;
  686. }
  687. printf("Using %s device\n", eth_get_name());
  688. printf("Listening for TFTP transfer on %pI4\n", &net_ip);
  689. printf("Load address: 0x%lx\n", tftp_load_addr);
  690. puts("Loading: *\b");
  691. timeout_count_max = tftp_timeout_count_max;
  692. timeout_count = 0;
  693. timeout_ms = TIMEOUT;
  694. net_set_timeout_handler(timeout_ms, tftp_timeout_handler);
  695. /* Revert tftp_block_size to dflt */
  696. tftp_block_size = TFTP_BLOCK_SIZE;
  697. tftp_cur_block = 0;
  698. tftp_our_port = WELL_KNOWN_PORT;
  699. #ifdef CONFIG_TFTP_TSIZE
  700. tftp_tsize = 0;
  701. tftp_tsize_num_hash = 0;
  702. #endif
  703. tftp_state = STATE_RECV_WRQ;
  704. net_set_udp_handler(tftp_handler);
  705. /* zero out server ether in case the server ip has changed */
  706. memset(net_server_ethaddr, 0, 6);
  707. }
  708. #endif /* CONFIG_CMD_TFTPSRV */