tftp.c 25 KB

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