tftp.c 24 KB

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