tftp.c 20 KB

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