tftp.c 20 KB

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