tftp.c 20 KB

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