tftp.c 20 KB

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