tftp.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966
  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. if (pos > tftp_tsize)
  224. pos = tftp_tsize;
  225. while (tftp_tsize_num_hash < pos * 50 / tftp_tsize) {
  226. putc('#');
  227. tftp_tsize_num_hash++;
  228. }
  229. } else
  230. #endif
  231. {
  232. if (((tftp_cur_block - 1) % 10) == 0)
  233. putc('#');
  234. else if ((tftp_cur_block % (10 * HASHES_PER_LINE)) == 0)
  235. puts("\n\t ");
  236. }
  237. }
  238. /**
  239. * restart the current transfer due to an error
  240. *
  241. * @param msg Message to print for user
  242. */
  243. static void restart(const char *msg)
  244. {
  245. printf("\n%s; starting again\n", msg);
  246. #ifdef CONFIG_MCAST_TFTP
  247. mcast_cleanup();
  248. #endif
  249. net_start_again();
  250. }
  251. /*
  252. * Check if the block number has wrapped, and update progress
  253. *
  254. * TODO: The egregious use of global variables in this file should be tidied.
  255. */
  256. static void update_block_number(void)
  257. {
  258. /*
  259. * RFC1350 specifies that the first data packet will
  260. * have sequence number 1. If we receive a sequence
  261. * number of 0 this means that there was a wrap
  262. * around of the (16 bit) counter.
  263. */
  264. if (tftp_cur_block == 0 && tftp_prev_block != 0) {
  265. tftp_block_wrap++;
  266. tftp_block_wrap_offset += tftp_block_size * TFTP_SEQUENCE_SIZE;
  267. timeout_count = 0; /* we've done well, reset the timeout */
  268. } else {
  269. show_block_marker();
  270. }
  271. }
  272. /* The TFTP get or put is complete */
  273. static void tftp_complete(void)
  274. {
  275. #ifdef CONFIG_TFTP_TSIZE
  276. /* Print hash marks for the last packet received */
  277. while (tftp_tsize && tftp_tsize_num_hash < 49) {
  278. putc('#');
  279. tftp_tsize_num_hash++;
  280. }
  281. puts(" ");
  282. print_size(tftp_tsize, "");
  283. #endif
  284. time_start = get_timer(time_start);
  285. if (time_start > 0) {
  286. puts("\n\t "); /* Line up with "Loading: " */
  287. print_size(net_boot_file_size /
  288. time_start * 1000, "/s");
  289. }
  290. puts("\ndone\n");
  291. net_set_state(NETLOOP_SUCCESS);
  292. }
  293. static void tftp_send(void)
  294. {
  295. uchar *pkt;
  296. uchar *xp;
  297. int len = 0;
  298. ushort *s;
  299. #ifdef CONFIG_MCAST_TFTP
  300. /* Multicast TFTP.. non-MasterClients do not ACK data. */
  301. if (tftp_mcast_active && tftp_state == STATE_DATA &&
  302. tftp_mcast_master_client == 0)
  303. return;
  304. #endif
  305. /*
  306. * We will always be sending some sort of packet, so
  307. * cobble together the packet headers now.
  308. */
  309. pkt = net_tx_packet + net_eth_hdr_size() + IP_UDP_HDR_SIZE;
  310. switch (tftp_state) {
  311. case STATE_SEND_RRQ:
  312. case STATE_SEND_WRQ:
  313. xp = pkt;
  314. s = (ushort *)pkt;
  315. #ifdef CONFIG_CMD_TFTPPUT
  316. *s++ = htons(tftp_state == STATE_SEND_RRQ ? TFTP_RRQ :
  317. TFTP_WRQ);
  318. #else
  319. *s++ = htons(TFTP_RRQ);
  320. #endif
  321. pkt = (uchar *)s;
  322. strcpy((char *)pkt, tftp_filename);
  323. pkt += strlen(tftp_filename) + 1;
  324. strcpy((char *)pkt, "octet");
  325. pkt += 5 /*strlen("octet")*/ + 1;
  326. strcpy((char *)pkt, "timeout");
  327. pkt += 7 /*strlen("timeout")*/ + 1;
  328. sprintf((char *)pkt, "%lu", timeout_ms / 1000);
  329. debug("send option \"timeout %s\"\n", (char *)pkt);
  330. pkt += strlen((char *)pkt) + 1;
  331. #ifdef CONFIG_TFTP_TSIZE
  332. pkt += sprintf((char *)pkt, "tsize%c%u%c",
  333. 0, net_boot_file_size, 0);
  334. #endif
  335. /* try for more effic. blk size */
  336. pkt += sprintf((char *)pkt, "blksize%c%d%c",
  337. 0, tftp_block_size_option, 0);
  338. #ifdef CONFIG_MCAST_TFTP
  339. /* Check all preconditions before even trying the option */
  340. if (!tftp_mcast_disabled) {
  341. tftp_mcast_bitmap = malloc(tftp_mcast_bitmap_size);
  342. if (tftp_mcast_bitmap && eth_get_dev()->mcast) {
  343. free(tftp_mcast_bitmap);
  344. tftp_mcast_bitmap = NULL;
  345. pkt += sprintf((char *)pkt, "multicast%c%c",
  346. 0, 0);
  347. }
  348. }
  349. #endif /* CONFIG_MCAST_TFTP */
  350. len = pkt - xp;
  351. break;
  352. case STATE_OACK:
  353. #ifdef CONFIG_MCAST_TFTP
  354. /* My turn! Start at where I need blocks I missed. */
  355. if (tftp_mcast_active)
  356. tftp_cur_block = ext2_find_next_zero_bit(
  357. tftp_mcast_bitmap,
  358. tftp_mcast_bitmap_size * 8, 0);
  359. /* fall through */
  360. #endif
  361. case STATE_RECV_WRQ:
  362. case STATE_DATA:
  363. xp = pkt;
  364. s = (ushort *)pkt;
  365. s[0] = htons(TFTP_ACK);
  366. s[1] = htons(tftp_cur_block);
  367. pkt = (uchar *)(s + 2);
  368. #ifdef CONFIG_CMD_TFTPPUT
  369. if (tftp_put_active) {
  370. int toload = tftp_block_size;
  371. int loaded = load_block(tftp_cur_block, pkt, toload);
  372. s[0] = htons(TFTP_DATA);
  373. pkt += loaded;
  374. tftp_put_final_block_sent = (loaded < toload);
  375. }
  376. #endif
  377. len = pkt - xp;
  378. break;
  379. case STATE_TOO_LARGE:
  380. xp = pkt;
  381. s = (ushort *)pkt;
  382. *s++ = htons(TFTP_ERROR);
  383. *s++ = htons(3);
  384. pkt = (uchar *)s;
  385. strcpy((char *)pkt, "File too large");
  386. pkt += 14 /*strlen("File too large")*/ + 1;
  387. len = pkt - xp;
  388. break;
  389. case STATE_BAD_MAGIC:
  390. xp = pkt;
  391. s = (ushort *)pkt;
  392. *s++ = htons(TFTP_ERROR);
  393. *s++ = htons(2);
  394. pkt = (uchar *)s;
  395. strcpy((char *)pkt, "File has bad magic");
  396. pkt += 18 /*strlen("File has bad magic")*/ + 1;
  397. len = pkt - xp;
  398. break;
  399. }
  400. net_send_udp_packet(net_server_ethaddr, tftp_remote_ip,
  401. tftp_remote_port, tftp_our_port, len);
  402. }
  403. #ifdef CONFIG_CMD_TFTPPUT
  404. static void icmp_handler(unsigned type, unsigned code, unsigned dest,
  405. struct in_addr sip, unsigned src, uchar *pkt,
  406. unsigned len)
  407. {
  408. if (type == ICMP_NOT_REACH && code == ICMP_NOT_REACH_PORT) {
  409. /* Oh dear the other end has gone away */
  410. restart("TFTP server died");
  411. }
  412. }
  413. #endif
  414. static void tftp_handler(uchar *pkt, unsigned dest, struct in_addr sip,
  415. unsigned src, unsigned len)
  416. {
  417. __be16 proto;
  418. __be16 *s;
  419. int i;
  420. if (dest != tftp_our_port) {
  421. #ifdef CONFIG_MCAST_TFTP
  422. if (tftp_mcast_active &&
  423. (!tftp_mcast_port || dest != tftp_mcast_port))
  424. #endif
  425. return;
  426. }
  427. if (tftp_state != STATE_SEND_RRQ && src != tftp_remote_port &&
  428. tftp_state != STATE_RECV_WRQ && tftp_state != STATE_SEND_WRQ)
  429. return;
  430. if (len < 2)
  431. return;
  432. len -= 2;
  433. /* warning: don't use increment (++) in ntohs() macros!! */
  434. s = (__be16 *)pkt;
  435. proto = *s++;
  436. pkt = (uchar *)s;
  437. switch (ntohs(proto)) {
  438. case TFTP_RRQ:
  439. break;
  440. case TFTP_ACK:
  441. #ifdef CONFIG_CMD_TFTPPUT
  442. if (tftp_put_active) {
  443. if (tftp_put_final_block_sent) {
  444. tftp_complete();
  445. } else {
  446. /*
  447. * Move to the next block. We want our block
  448. * count to wrap just like the other end!
  449. */
  450. int block = ntohs(*s);
  451. int ack_ok = (tftp_cur_block == block);
  452. tftp_cur_block = (unsigned short)(block + 1);
  453. update_block_number();
  454. if (ack_ok)
  455. tftp_send(); /* Send next data block */
  456. }
  457. }
  458. #endif
  459. break;
  460. default:
  461. break;
  462. #ifdef CONFIG_CMD_TFTPSRV
  463. case TFTP_WRQ:
  464. debug("Got WRQ\n");
  465. tftp_remote_ip = sip;
  466. tftp_remote_port = src;
  467. tftp_our_port = 1024 + (get_timer(0) % 3072);
  468. new_transfer();
  469. tftp_send(); /* Send ACK(0) */
  470. break;
  471. #endif
  472. case TFTP_OACK:
  473. debug("Got OACK: %s %s\n",
  474. pkt, pkt + strlen((char *)pkt) + 1);
  475. tftp_state = STATE_OACK;
  476. tftp_remote_port = src;
  477. /*
  478. * Check for 'blksize' option.
  479. * Careful: "i" is signed, "len" is unsigned, thus
  480. * something like "len-8" may give a *huge* number
  481. */
  482. for (i = 0; i+8 < len; i++) {
  483. if (strcmp((char *)pkt + i, "blksize") == 0) {
  484. tftp_block_size = (unsigned short)
  485. simple_strtoul((char *)pkt + i + 8,
  486. NULL, 10);
  487. debug("Blocksize ack: %s, %d\n",
  488. (char *)pkt + i + 8, tftp_block_size);
  489. }
  490. #ifdef CONFIG_TFTP_TSIZE
  491. if (strcmp((char *)pkt+i, "tsize") == 0) {
  492. tftp_tsize = simple_strtoul((char *)pkt + i + 6,
  493. NULL, 10);
  494. debug("size = %s, %d\n",
  495. (char *)pkt + i + 6, tftp_tsize);
  496. }
  497. #endif
  498. }
  499. #ifdef CONFIG_MCAST_TFTP
  500. parse_multicast_oack((char *)pkt, len - 1);
  501. if ((tftp_mcast_active) && (!tftp_mcast_master_client))
  502. tftp_state = STATE_DATA; /* passive.. */
  503. else
  504. #endif
  505. #ifdef CONFIG_CMD_TFTPPUT
  506. if (tftp_put_active) {
  507. /* Get ready to send the first block */
  508. tftp_state = STATE_DATA;
  509. tftp_cur_block++;
  510. }
  511. #endif
  512. tftp_send(); /* Send ACK or first data block */
  513. break;
  514. case TFTP_DATA:
  515. if (len < 2)
  516. return;
  517. len -= 2;
  518. tftp_cur_block = ntohs(*(__be16 *)pkt);
  519. update_block_number();
  520. if (tftp_state == STATE_SEND_RRQ)
  521. debug("Server did not acknowledge timeout option!\n");
  522. if (tftp_state == STATE_SEND_RRQ || tftp_state == STATE_OACK ||
  523. tftp_state == STATE_RECV_WRQ) {
  524. /* first block received */
  525. tftp_state = STATE_DATA;
  526. tftp_remote_port = src;
  527. new_transfer();
  528. #ifdef CONFIG_MCAST_TFTP
  529. if (tftp_mcast_active) { /* start!=1 common if mcast */
  530. tftp_prev_block = tftp_cur_block - 1;
  531. } else
  532. #endif
  533. if (tftp_cur_block != 1) { /* Assertion */
  534. puts("\nTFTP error: ");
  535. printf("First block is not block 1 (%ld)\n",
  536. tftp_cur_block);
  537. puts("Starting again\n\n");
  538. net_start_again();
  539. break;
  540. }
  541. }
  542. if (tftp_cur_block == tftp_prev_block) {
  543. /* Same block again; ignore it. */
  544. break;
  545. }
  546. tftp_prev_block = tftp_cur_block;
  547. timeout_count_max = TIMEOUT_COUNT;
  548. net_set_timeout_handler(timeout_ms, tftp_timeout_handler);
  549. store_block(tftp_cur_block - 1, pkt + 2, len);
  550. /*
  551. * Acknowledge the block just received, which will prompt
  552. * the remote for the next one.
  553. */
  554. #ifdef CONFIG_MCAST_TFTP
  555. /* if I am the MasterClient, actively calculate what my next
  556. * needed block is; else I'm passive; not ACKING
  557. */
  558. if (tftp_mcast_active) {
  559. if (len < tftp_block_size) {
  560. tftp_mcast_ending_block = tftp_cur_block;
  561. } else if (tftp_mcast_master_client) {
  562. tftp_mcast_prev_hole = ext2_find_next_zero_bit(
  563. tftp_mcast_bitmap,
  564. tftp_mcast_bitmap_size * 8,
  565. tftp_mcast_prev_hole);
  566. tftp_cur_block = tftp_mcast_prev_hole;
  567. if (tftp_cur_block >
  568. ((tftp_mcast_bitmap_size * 8) - 1)) {
  569. debug("tftpfile too big\n");
  570. /* try to double it and retry */
  571. tftp_mcast_bitmap_size <<= 1;
  572. mcast_cleanup();
  573. net_start_again();
  574. return;
  575. }
  576. tftp_prev_block = tftp_cur_block;
  577. }
  578. }
  579. #endif
  580. tftp_send();
  581. #ifdef CONFIG_MCAST_TFTP
  582. if (tftp_mcast_active) {
  583. if (tftp_mcast_master_client &&
  584. (tftp_cur_block >= tftp_mcast_ending_block)) {
  585. puts("\nMulticast tftp done\n");
  586. mcast_cleanup();
  587. net_set_state(NETLOOP_SUCCESS);
  588. }
  589. } else
  590. #endif
  591. if (len < tftp_block_size)
  592. tftp_complete();
  593. break;
  594. case TFTP_ERROR:
  595. printf("\nTFTP error: '%s' (%d)\n",
  596. pkt + 2, ntohs(*(__be16 *)pkt));
  597. switch (ntohs(*(__be16 *)pkt)) {
  598. case TFTP_ERR_FILE_NOT_FOUND:
  599. case TFTP_ERR_ACCESS_DENIED:
  600. puts("Not retrying...\n");
  601. eth_halt();
  602. net_set_state(NETLOOP_FAIL);
  603. break;
  604. case TFTP_ERR_UNDEFINED:
  605. case TFTP_ERR_DISK_FULL:
  606. case TFTP_ERR_UNEXPECTED_OPCODE:
  607. case TFTP_ERR_UNKNOWN_TRANSFER_ID:
  608. case TFTP_ERR_FILE_ALREADY_EXISTS:
  609. default:
  610. puts("Starting again\n\n");
  611. #ifdef CONFIG_MCAST_TFTP
  612. mcast_cleanup();
  613. #endif
  614. net_start_again();
  615. break;
  616. }
  617. break;
  618. }
  619. }
  620. static void tftp_timeout_handler(void)
  621. {
  622. if (++timeout_count > timeout_count_max) {
  623. restart("Retry count exceeded");
  624. } else {
  625. puts("T ");
  626. net_set_timeout_handler(timeout_ms, tftp_timeout_handler);
  627. if (tftp_state != STATE_RECV_WRQ)
  628. tftp_send();
  629. }
  630. }
  631. void tftp_start(enum proto_t protocol)
  632. {
  633. char *ep; /* Environment pointer */
  634. /*
  635. * Allow the user to choose TFTP blocksize and timeout.
  636. * TFTP protocol has a minimal timeout of 1 second.
  637. */
  638. ep = getenv("tftpblocksize");
  639. if (ep != NULL)
  640. tftp_block_size_option = simple_strtol(ep, NULL, 10);
  641. ep = getenv("tftptimeout");
  642. if (ep != NULL)
  643. timeout_ms = simple_strtol(ep, NULL, 10);
  644. if (timeout_ms < 1000) {
  645. printf("TFTP timeout (%ld ms) too low, set min = 1000 ms\n",
  646. timeout_ms);
  647. timeout_ms = 1000;
  648. }
  649. debug("TFTP blocksize = %i, timeout = %ld ms\n",
  650. tftp_block_size_option, timeout_ms);
  651. tftp_remote_ip = net_server_ip;
  652. if (net_boot_file_name[0] == '\0') {
  653. sprintf(default_filename, "%02X%02X%02X%02X.img",
  654. net_ip.s_addr & 0xFF,
  655. (net_ip.s_addr >> 8) & 0xFF,
  656. (net_ip.s_addr >> 16) & 0xFF,
  657. (net_ip.s_addr >> 24) & 0xFF);
  658. strncpy(tftp_filename, default_filename, MAX_LEN);
  659. tftp_filename[MAX_LEN - 1] = 0;
  660. printf("*** Warning: no boot file name; using '%s'\n",
  661. tftp_filename);
  662. } else {
  663. char *p = strchr(net_boot_file_name, ':');
  664. if (p == NULL) {
  665. strncpy(tftp_filename, net_boot_file_name, MAX_LEN);
  666. tftp_filename[MAX_LEN - 1] = 0;
  667. } else {
  668. tftp_remote_ip = string_to_ip(net_boot_file_name);
  669. strncpy(tftp_filename, p + 1, MAX_LEN);
  670. tftp_filename[MAX_LEN - 1] = 0;
  671. }
  672. }
  673. printf("Using %s device\n", eth_get_name());
  674. printf("TFTP %s server %pI4; our IP address is %pI4",
  675. #ifdef CONFIG_CMD_TFTPPUT
  676. protocol == TFTPPUT ? "to" : "from",
  677. #else
  678. "from",
  679. #endif
  680. &tftp_remote_ip, &net_ip);
  681. /* Check if we need to send across this subnet */
  682. if (net_gateway.s_addr && net_netmask.s_addr) {
  683. struct in_addr our_net;
  684. struct in_addr remote_net;
  685. our_net.s_addr = net_ip.s_addr & net_netmask.s_addr;
  686. remote_net.s_addr = tftp_remote_ip.s_addr & net_netmask.s_addr;
  687. if (our_net.s_addr != remote_net.s_addr)
  688. printf("; sending through gateway %pI4", &net_gateway);
  689. }
  690. putc('\n');
  691. printf("Filename '%s'.", tftp_filename);
  692. if (net_boot_file_expected_size_in_blocks) {
  693. printf(" Size is 0x%x Bytes = ",
  694. net_boot_file_expected_size_in_blocks << 9);
  695. print_size(net_boot_file_expected_size_in_blocks << 9, "");
  696. }
  697. putc('\n');
  698. #ifdef CONFIG_CMD_TFTPPUT
  699. tftp_put_active = (protocol == TFTPPUT);
  700. if (tftp_put_active) {
  701. printf("Save address: 0x%lx\n", save_addr);
  702. printf("Save size: 0x%lx\n", save_size);
  703. net_boot_file_size = save_size;
  704. puts("Saving: *\b");
  705. tftp_state = STATE_SEND_WRQ;
  706. new_transfer();
  707. } else
  708. #endif
  709. {
  710. printf("Load address: 0x%lx\n", load_addr);
  711. puts("Loading: *\b");
  712. tftp_state = STATE_SEND_RRQ;
  713. }
  714. time_start = get_timer(0);
  715. timeout_count_max = tftp_timeout_count_max;
  716. net_set_timeout_handler(timeout_ms, tftp_timeout_handler);
  717. net_set_udp_handler(tftp_handler);
  718. #ifdef CONFIG_CMD_TFTPPUT
  719. net_set_icmp_handler(icmp_handler);
  720. #endif
  721. tftp_remote_port = WELL_KNOWN_PORT;
  722. timeout_count = 0;
  723. /* Use a pseudo-random port unless a specific port is set */
  724. tftp_our_port = 1024 + (get_timer(0) % 3072);
  725. #ifdef CONFIG_TFTP_PORT
  726. ep = getenv("tftpdstp");
  727. if (ep != NULL)
  728. tftp_remote_port = simple_strtol(ep, NULL, 10);
  729. ep = getenv("tftpsrcp");
  730. if (ep != NULL)
  731. tftp_our_port = simple_strtol(ep, NULL, 10);
  732. #endif
  733. tftp_cur_block = 0;
  734. /* zero out server ether in case the server ip has changed */
  735. memset(net_server_ethaddr, 0, 6);
  736. /* Revert tftp_block_size to dflt */
  737. tftp_block_size = TFTP_BLOCK_SIZE;
  738. #ifdef CONFIG_MCAST_TFTP
  739. mcast_cleanup();
  740. #endif
  741. #ifdef CONFIG_TFTP_TSIZE
  742. tftp_tsize = 0;
  743. tftp_tsize_num_hash = 0;
  744. #endif
  745. tftp_send();
  746. }
  747. #ifdef CONFIG_CMD_TFTPSRV
  748. void tftp_start_server(void)
  749. {
  750. tftp_filename[0] = 0;
  751. printf("Using %s device\n", eth_get_name());
  752. printf("Listening for TFTP transfer on %pI4\n", &net_ip);
  753. printf("Load address: 0x%lx\n", load_addr);
  754. puts("Loading: *\b");
  755. timeout_count_max = TIMEOUT_COUNT;
  756. timeout_count = 0;
  757. timeout_ms = TIMEOUT;
  758. net_set_timeout_handler(timeout_ms, tftp_timeout_handler);
  759. /* Revert tftp_block_size to dflt */
  760. tftp_block_size = TFTP_BLOCK_SIZE;
  761. tftp_cur_block = 0;
  762. tftp_our_port = WELL_KNOWN_PORT;
  763. #ifdef CONFIG_TFTP_TSIZE
  764. tftp_tsize = 0;
  765. tftp_tsize_num_hash = 0;
  766. #endif
  767. tftp_state = STATE_RECV_WRQ;
  768. net_set_udp_handler(tftp_handler);
  769. /* zero out server ether in case the server ip has changed */
  770. memset(net_server_ethaddr, 0, 6);
  771. }
  772. #endif /* CONFIG_CMD_TFTPSRV */
  773. #ifdef CONFIG_MCAST_TFTP
  774. /*
  775. * Credits: atftp project.
  776. */
  777. /*
  778. * Pick up BcastAddr, Port, and whether I am [now] the master-client.
  779. * Frame:
  780. * +-------+-----------+---+-------~~-------+---+
  781. * | opc | multicast | 0 | addr, port, mc | 0 |
  782. * +-------+-----------+---+-------~~-------+---+
  783. * The multicast addr/port becomes what I listen to, and if 'mc' is '1' then
  784. * I am the new master-client so must send ACKs to DataBlocks. If I am not
  785. * master-client, I'm a passive client, gathering what DataBlocks I may and
  786. * making note of which ones I got in my bitmask.
  787. * In theory, I never go from master->passive..
  788. * .. this comes in with pkt already pointing just past opc
  789. */
  790. static void parse_multicast_oack(char *pkt, int len)
  791. {
  792. int i;
  793. struct in_addr addr;
  794. char *mc_adr;
  795. char *port;
  796. char *mc;
  797. mc_adr = NULL;
  798. port = NULL;
  799. mc = NULL;
  800. /* march along looking for 'multicast\0', which has to start at least
  801. * 14 bytes back from the end.
  802. */
  803. for (i = 0; i < len - 14; i++)
  804. if (strcmp(pkt + i, "multicast") == 0)
  805. break;
  806. if (i >= (len - 14)) /* non-Multicast OACK, ign. */
  807. return;
  808. i += 10; /* strlen multicast */
  809. mc_adr = pkt + i;
  810. for (; i < len; i++) {
  811. if (*(pkt + i) == ',') {
  812. *(pkt + i) = '\0';
  813. if (port) {
  814. mc = pkt + i + 1;
  815. break;
  816. } else {
  817. port = pkt + i + 1;
  818. }
  819. }
  820. }
  821. if (!port || !mc_adr || !mc)
  822. return;
  823. if (tftp_mcast_active && tftp_mcast_master_client) {
  824. printf("I got a OACK as master Client, WRONG!\n");
  825. return;
  826. }
  827. /* ..I now accept packets destined for this MCAST addr, port */
  828. if (!tftp_mcast_active) {
  829. if (tftp_mcast_bitmap) {
  830. printf("Internal failure! no mcast.\n");
  831. free(tftp_mcast_bitmap);
  832. tftp_mcast_bitmap = NULL;
  833. tftp_mcast_disabled = 1;
  834. return;
  835. }
  836. /* I malloc instead of pre-declare; so that if the file ends
  837. * up being too big for this bitmap I can retry
  838. */
  839. tftp_mcast_bitmap = malloc(tftp_mcast_bitmap_size);
  840. if (!tftp_mcast_bitmap) {
  841. printf("No bitmap, no multicast. Sorry.\n");
  842. tftp_mcast_disabled = 1;
  843. return;
  844. }
  845. memset(tftp_mcast_bitmap, 0, tftp_mcast_bitmap_size);
  846. tftp_mcast_prev_hole = 0;
  847. tftp_mcast_active = 1;
  848. }
  849. addr = string_to_ip(mc_adr);
  850. if (net_mcast_addr.s_addr != addr.s_addr) {
  851. if (net_mcast_addr.s_addr)
  852. eth_mcast_join(net_mcast_addr, 0);
  853. net_mcast_addr = addr;
  854. if (eth_mcast_join(net_mcast_addr, 1)) {
  855. printf("Fail to set mcast, revert to TFTP\n");
  856. tftp_mcast_disabled = 1;
  857. mcast_cleanup();
  858. net_start_again();
  859. }
  860. }
  861. tftp_mcast_master_client = simple_strtoul((char *)mc, NULL, 10);
  862. tftp_mcast_port = (unsigned short)simple_strtoul(port, NULL, 10);
  863. printf("Multicast: %s:%d [%d]\n", mc_adr, tftp_mcast_port,
  864. tftp_mcast_master_client);
  865. return;
  866. }
  867. #endif /* Multicast TFTP */