tftp.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809
  1. /*
  2. * Copyright 1994, 1995, 2000 Neil Russell.
  3. * (See License)
  4. * Copyright 2000, 2001 DENX Software Engineering, Wolfgang Denk, wd@denx.de
  5. * Copyright 2011 Comelit Group SpA,
  6. * Luca Ceresoli <luca.ceresoli@comelit.it>
  7. */
  8. #include <common.h>
  9. #include <command.h>
  10. #include <net.h>
  11. #include "tftp.h"
  12. #include "bootp.h"
  13. /* Well known TFTP port # */
  14. #define WELL_KNOWN_PORT 69
  15. /* Millisecs to timeout for lost pkt */
  16. #define TIMEOUT 5000UL
  17. #ifndef CONFIG_NET_RETRY_COUNT
  18. /* # of timeouts before giving up */
  19. # define TIMEOUT_COUNT 10
  20. #else
  21. # define TIMEOUT_COUNT (CONFIG_NET_RETRY_COUNT * 2)
  22. #endif
  23. /* Number of "loading" hashes per line (for checking the image size) */
  24. #define HASHES_PER_LINE 65
  25. /*
  26. * TFTP operations.
  27. */
  28. #define TFTP_RRQ 1
  29. #define TFTP_WRQ 2
  30. #define TFTP_DATA 3
  31. #define TFTP_ACK 4
  32. #define TFTP_ERROR 5
  33. #define TFTP_OACK 6
  34. static ulong TftpTimeoutMSecs = TIMEOUT;
  35. static int TftpTimeoutCountMax = TIMEOUT_COUNT;
  36. /*
  37. * These globals govern the timeout behavior when attempting a connection to a
  38. * TFTP server. TftpRRQTimeoutMSecs specifies the number of milliseconds to
  39. * wait for the server to respond to initial connection. Second global,
  40. * TftpRRQTimeoutCountMax, gives the number of such connection retries.
  41. * TftpRRQTimeoutCountMax must be non-negative and TftpRRQTimeoutMSecs must be
  42. * positive. The globals are meant to be set (and restored) by code needing
  43. * non-standard timeout behavior when initiating a TFTP transfer.
  44. */
  45. ulong TftpRRQTimeoutMSecs = TIMEOUT;
  46. int TftpRRQTimeoutCountMax = TIMEOUT_COUNT;
  47. enum {
  48. TFTP_ERR_UNDEFINED = 0,
  49. TFTP_ERR_FILE_NOT_FOUND = 1,
  50. TFTP_ERR_ACCESS_DENIED = 2,
  51. TFTP_ERR_DISK_FULL = 3,
  52. TFTP_ERR_UNEXPECTED_OPCODE = 4,
  53. TFTP_ERR_UNKNOWN_TRANSFER_ID = 5,
  54. TFTP_ERR_FILE_ALREADY_EXISTS = 6,
  55. };
  56. static IPaddr_t TftpRemoteIP;
  57. /* The UDP port at their end */
  58. static int TftpRemotePort;
  59. /* The UDP port at our end */
  60. static int TftpOurPort;
  61. static int TftpTimeoutCount;
  62. /* packet sequence number */
  63. static ulong TftpBlock;
  64. /* last packet sequence number received */
  65. static ulong TftpLastBlock;
  66. /* count of sequence number wraparounds */
  67. static ulong TftpBlockWrap;
  68. /* memory offset due to wrapping */
  69. static ulong TftpBlockWrapOffset;
  70. static int TftpState;
  71. #ifdef CONFIG_TFTP_TSIZE
  72. /* The file size reported by the server */
  73. static int TftpTsize;
  74. /* The number of hashes we printed */
  75. static short TftpNumchars;
  76. #endif
  77. #define STATE_SEND_RRQ 1
  78. #define STATE_DATA 2
  79. #define STATE_TOO_LARGE 3
  80. #define STATE_BAD_MAGIC 4
  81. #define STATE_OACK 5
  82. #define STATE_RECV_WRQ 6
  83. /* default TFTP block size */
  84. #define TFTP_BLOCK_SIZE 512
  85. /* sequence number is 16 bit */
  86. #define TFTP_SEQUENCE_SIZE ((ulong)(1<<16))
  87. #define DEFAULT_NAME_LEN (8 + 4 + 1)
  88. static char default_filename[DEFAULT_NAME_LEN];
  89. #ifndef CONFIG_TFTP_FILE_NAME_MAX_LEN
  90. #define MAX_LEN 128
  91. #else
  92. #define MAX_LEN CONFIG_TFTP_FILE_NAME_MAX_LEN
  93. #endif
  94. static char tftp_filename[MAX_LEN];
  95. #ifdef CONFIG_SYS_DIRECT_FLASH_TFTP
  96. extern flash_info_t flash_info[];
  97. #endif
  98. /* 512 is poor choice for ethernet, MTU is typically 1500.
  99. * Minus eth.hdrs thats 1468. Can get 2x better throughput with
  100. * almost-MTU block sizes. At least try... fall back to 512 if need be.
  101. * (but those using CONFIG_IP_DEFRAG may want to set a larger block in cfg file)
  102. */
  103. #ifdef CONFIG_TFTP_BLOCKSIZE
  104. #define TFTP_MTU_BLOCKSIZE CONFIG_TFTP_BLOCKSIZE
  105. #else
  106. #define TFTP_MTU_BLOCKSIZE 1468
  107. #endif
  108. static unsigned short TftpBlkSize = TFTP_BLOCK_SIZE;
  109. static unsigned short TftpBlkSizeOption = TFTP_MTU_BLOCKSIZE;
  110. #ifdef CONFIG_MCAST_TFTP
  111. #include <malloc.h>
  112. #define MTFTP_BITMAPSIZE 0x1000
  113. static unsigned *Bitmap;
  114. static int PrevBitmapHole, Mapsize = MTFTP_BITMAPSIZE;
  115. static uchar ProhibitMcast, MasterClient;
  116. static uchar Multicast;
  117. extern IPaddr_t Mcast_addr;
  118. static int Mcast_port;
  119. static ulong TftpEndingBlock; /* can get 'last' block before done..*/
  120. static void parse_multicast_oack(char *pkt, int len);
  121. static void
  122. mcast_cleanup(void)
  123. {
  124. if (Mcast_addr)
  125. eth_mcast_join(Mcast_addr, 0);
  126. if (Bitmap)
  127. free(Bitmap);
  128. Bitmap = NULL;
  129. Mcast_addr = Multicast = Mcast_port = 0;
  130. TftpEndingBlock = -1;
  131. }
  132. #endif /* CONFIG_MCAST_TFTP */
  133. static __inline__ void
  134. store_block(unsigned block, uchar *src, unsigned len)
  135. {
  136. ulong offset = block * TftpBlkSize + TftpBlockWrapOffset;
  137. ulong newsize = offset + len;
  138. #ifdef CONFIG_SYS_DIRECT_FLASH_TFTP
  139. int i, rc = 0;
  140. for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; i++) {
  141. /* start address in flash? */
  142. if (flash_info[i].flash_id == FLASH_UNKNOWN)
  143. continue;
  144. if (load_addr + offset >= flash_info[i].start[0]) {
  145. rc = 1;
  146. break;
  147. }
  148. }
  149. if (rc) { /* Flash is destination for this packet */
  150. rc = flash_write((char *)src, (ulong)(load_addr+offset), len);
  151. if (rc) {
  152. flash_perror(rc);
  153. NetState = NETLOOP_FAIL;
  154. return;
  155. }
  156. }
  157. else
  158. #endif /* CONFIG_SYS_DIRECT_FLASH_TFTP */
  159. {
  160. (void)memcpy((void *)(load_addr + offset), src, len);
  161. }
  162. #ifdef CONFIG_MCAST_TFTP
  163. if (Multicast)
  164. ext2_set_bit(block, Bitmap);
  165. #endif
  166. if (NetBootFileXferSize < newsize)
  167. NetBootFileXferSize = newsize;
  168. }
  169. static void TftpSend(void);
  170. static void TftpTimeout(void);
  171. /**********************************************************************/
  172. static void
  173. TftpSend(void)
  174. {
  175. volatile uchar *pkt;
  176. volatile uchar *xp;
  177. int len = 0;
  178. volatile ushort *s;
  179. #ifdef CONFIG_MCAST_TFTP
  180. /* Multicast TFTP.. non-MasterClients do not ACK data. */
  181. if (Multicast
  182. && (TftpState == STATE_DATA)
  183. && (MasterClient == 0))
  184. return;
  185. #endif
  186. /*
  187. * We will always be sending some sort of packet, so
  188. * cobble together the packet headers now.
  189. */
  190. pkt = NetTxPacket + NetEthHdrSize() + IP_HDR_SIZE;
  191. switch (TftpState) {
  192. case STATE_SEND_RRQ:
  193. xp = pkt;
  194. s = (ushort *)pkt;
  195. *s++ = htons(TFTP_RRQ);
  196. pkt = (uchar *)s;
  197. strcpy((char *)pkt, tftp_filename);
  198. pkt += strlen(tftp_filename) + 1;
  199. strcpy((char *)pkt, "octet");
  200. pkt += 5 /*strlen("octet")*/ + 1;
  201. strcpy((char *)pkt, "timeout");
  202. pkt += 7 /*strlen("timeout")*/ + 1;
  203. sprintf((char *)pkt, "%lu", TftpTimeoutMSecs / 1000);
  204. debug("send option \"timeout %s\"\n", (char *)pkt);
  205. pkt += strlen((char *)pkt) + 1;
  206. #ifdef CONFIG_TFTP_TSIZE
  207. memcpy((char *)pkt, "tsize\0000\0", 8);
  208. pkt += 8;
  209. #endif
  210. /* try for more effic. blk size */
  211. pkt += sprintf((char *)pkt, "blksize%c%d%c",
  212. 0, TftpBlkSizeOption, 0);
  213. #ifdef CONFIG_MCAST_TFTP
  214. /* Check all preconditions before even trying the option */
  215. if (!ProhibitMcast
  216. && (Bitmap = malloc(Mapsize))
  217. && eth_get_dev()->mcast) {
  218. free(Bitmap);
  219. Bitmap = NULL;
  220. pkt += sprintf((char *)pkt, "multicast%c%c", 0, 0);
  221. }
  222. #endif /* CONFIG_MCAST_TFTP */
  223. len = pkt - xp;
  224. break;
  225. case STATE_OACK:
  226. #ifdef CONFIG_MCAST_TFTP
  227. /* My turn! Start at where I need blocks I missed.*/
  228. if (Multicast)
  229. TftpBlock = ext2_find_next_zero_bit(Bitmap,
  230. (Mapsize*8), 0);
  231. /*..falling..*/
  232. #endif
  233. case STATE_RECV_WRQ:
  234. case STATE_DATA:
  235. xp = pkt;
  236. s = (ushort *)pkt;
  237. *s++ = htons(TFTP_ACK);
  238. *s++ = htons(TftpBlock);
  239. pkt = (uchar *)s;
  240. len = pkt - xp;
  241. break;
  242. case STATE_TOO_LARGE:
  243. xp = pkt;
  244. s = (ushort *)pkt;
  245. *s++ = htons(TFTP_ERROR);
  246. *s++ = htons(3);
  247. pkt = (uchar *)s;
  248. strcpy((char *)pkt, "File too large");
  249. pkt += 14 /*strlen("File too large")*/ + 1;
  250. len = pkt - xp;
  251. break;
  252. case STATE_BAD_MAGIC:
  253. xp = pkt;
  254. s = (ushort *)pkt;
  255. *s++ = htons(TFTP_ERROR);
  256. *s++ = htons(2);
  257. pkt = (uchar *)s;
  258. strcpy((char *)pkt, "File has bad magic");
  259. pkt += 18 /*strlen("File has bad magic")*/ + 1;
  260. len = pkt - xp;
  261. break;
  262. }
  263. NetSendUDPPacket(NetServerEther, TftpRemoteIP, TftpRemotePort,
  264. TftpOurPort, len);
  265. }
  266. static void
  267. TftpHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src,
  268. unsigned len)
  269. {
  270. ushort proto;
  271. ushort *s;
  272. int i;
  273. if (dest != TftpOurPort) {
  274. #ifdef CONFIG_MCAST_TFTP
  275. if (Multicast
  276. && (!Mcast_port || (dest != Mcast_port)))
  277. #endif
  278. return;
  279. }
  280. if (TftpState != STATE_SEND_RRQ && src != TftpRemotePort &&
  281. TftpState != STATE_RECV_WRQ)
  282. return;
  283. if (len < 2)
  284. return;
  285. len -= 2;
  286. /* warning: don't use increment (++) in ntohs() macros!! */
  287. s = (ushort *)pkt;
  288. proto = *s++;
  289. pkt = (uchar *)s;
  290. switch (ntohs(proto)) {
  291. case TFTP_RRQ:
  292. case TFTP_ACK:
  293. break;
  294. default:
  295. break;
  296. #ifdef CONFIG_CMD_TFTPSRV
  297. case TFTP_WRQ:
  298. debug("Got WRQ\n");
  299. TftpRemoteIP = sip;
  300. TftpRemotePort = src;
  301. TftpOurPort = 1024 + (get_timer(0) % 3072);
  302. TftpLastBlock = 0;
  303. TftpBlockWrap = 0;
  304. TftpBlockWrapOffset = 0;
  305. TftpSend(); /* Send ACK(0) */
  306. break;
  307. #endif
  308. case TFTP_OACK:
  309. debug("Got OACK: %s %s\n",
  310. pkt,
  311. pkt + strlen((char *)pkt) + 1);
  312. TftpState = STATE_OACK;
  313. TftpRemotePort = src;
  314. /*
  315. * Check for 'blksize' option.
  316. * Careful: "i" is signed, "len" is unsigned, thus
  317. * something like "len-8" may give a *huge* number
  318. */
  319. for (i = 0; i+8 < len; i++) {
  320. if (strcmp((char *)pkt+i, "blksize") == 0) {
  321. TftpBlkSize = (unsigned short)
  322. simple_strtoul((char *)pkt+i+8, NULL,
  323. 10);
  324. debug("Blocksize ack: %s, %d\n",
  325. (char *)pkt+i+8, TftpBlkSize);
  326. }
  327. #ifdef CONFIG_TFTP_TSIZE
  328. if (strcmp((char *)pkt+i, "tsize") == 0) {
  329. TftpTsize = simple_strtoul((char *)pkt+i+6,
  330. NULL, 10);
  331. debug("size = %s, %d\n",
  332. (char *)pkt+i+6, TftpTsize);
  333. }
  334. #endif
  335. }
  336. #ifdef CONFIG_MCAST_TFTP
  337. parse_multicast_oack((char *)pkt, len-1);
  338. if ((Multicast) && (!MasterClient))
  339. TftpState = STATE_DATA; /* passive.. */
  340. else
  341. #endif
  342. TftpSend(); /* Send ACK */
  343. break;
  344. case TFTP_DATA:
  345. if (len < 2)
  346. return;
  347. len -= 2;
  348. TftpBlock = ntohs(*(ushort *)pkt);
  349. /*
  350. * RFC1350 specifies that the first data packet will
  351. * have sequence number 1. If we receive a sequence
  352. * number of 0 this means that there was a wrap
  353. * around of the (16 bit) counter.
  354. */
  355. if (TftpBlock == 0) {
  356. TftpBlockWrap++;
  357. TftpBlockWrapOffset +=
  358. TftpBlkSize * TFTP_SEQUENCE_SIZE;
  359. printf("\n\t %lu MB received\n\t ",
  360. TftpBlockWrapOffset>>20);
  361. }
  362. #ifdef CONFIG_TFTP_TSIZE
  363. else if (TftpTsize) {
  364. while (TftpNumchars <
  365. NetBootFileXferSize * 50 / TftpTsize) {
  366. putc('#');
  367. TftpNumchars++;
  368. }
  369. }
  370. #endif
  371. else {
  372. if (((TftpBlock - 1) % 10) == 0)
  373. putc('#');
  374. else if ((TftpBlock % (10 * HASHES_PER_LINE)) == 0)
  375. puts("\n\t ");
  376. }
  377. if (TftpState == STATE_SEND_RRQ)
  378. debug("Server did not acknowledge timeout option!\n");
  379. if (TftpState == STATE_SEND_RRQ || TftpState == STATE_OACK ||
  380. TftpState == STATE_RECV_WRQ) {
  381. /* first block received */
  382. TftpState = STATE_DATA;
  383. TftpRemotePort = src;
  384. TftpLastBlock = 0;
  385. TftpBlockWrap = 0;
  386. TftpBlockWrapOffset = 0;
  387. #ifdef CONFIG_MCAST_TFTP
  388. if (Multicast) { /* start!=1 common if mcast */
  389. TftpLastBlock = TftpBlock - 1;
  390. } else
  391. #endif
  392. if (TftpBlock != 1) { /* Assertion */
  393. printf("\nTFTP error: "
  394. "First block is not block 1 (%ld)\n"
  395. "Starting again\n\n",
  396. TftpBlock);
  397. NetStartAgain();
  398. break;
  399. }
  400. }
  401. if (TftpBlock == TftpLastBlock) {
  402. /*
  403. * Same block again; ignore it.
  404. */
  405. break;
  406. }
  407. TftpLastBlock = TftpBlock;
  408. TftpTimeoutCountMax = TIMEOUT_COUNT;
  409. NetSetTimeout(TftpTimeoutMSecs, TftpTimeout);
  410. store_block(TftpBlock - 1, pkt + 2, len);
  411. /*
  412. * Acknowledge the block just received, which will prompt
  413. * the remote for the next one.
  414. */
  415. #ifdef CONFIG_MCAST_TFTP
  416. /* if I am the MasterClient, actively calculate what my next
  417. * needed block is; else I'm passive; not ACKING
  418. */
  419. if (Multicast) {
  420. if (len < TftpBlkSize) {
  421. TftpEndingBlock = TftpBlock;
  422. } else if (MasterClient) {
  423. TftpBlock = PrevBitmapHole =
  424. ext2_find_next_zero_bit(
  425. Bitmap,
  426. (Mapsize*8),
  427. PrevBitmapHole);
  428. if (TftpBlock > ((Mapsize*8) - 1)) {
  429. printf("tftpfile too big\n");
  430. /* try to double it and retry */
  431. Mapsize <<= 1;
  432. mcast_cleanup();
  433. NetStartAgain();
  434. return;
  435. }
  436. TftpLastBlock = TftpBlock;
  437. }
  438. }
  439. #endif
  440. TftpSend();
  441. #ifdef CONFIG_MCAST_TFTP
  442. if (Multicast) {
  443. if (MasterClient && (TftpBlock >= TftpEndingBlock)) {
  444. puts("\nMulticast tftp done\n");
  445. mcast_cleanup();
  446. NetState = NETLOOP_SUCCESS;
  447. }
  448. }
  449. else
  450. #endif
  451. if (len < TftpBlkSize) {
  452. /*
  453. * We received the whole thing. Try to
  454. * run it.
  455. */
  456. #ifdef CONFIG_TFTP_TSIZE
  457. /* Print hash marks for the last packet received */
  458. while (TftpTsize && TftpNumchars < 49) {
  459. putc('#');
  460. TftpNumchars++;
  461. }
  462. #endif
  463. puts("\ndone\n");
  464. NetState = NETLOOP_SUCCESS;
  465. }
  466. break;
  467. case TFTP_ERROR:
  468. printf("\nTFTP error: '%s' (%d)\n",
  469. pkt + 2, ntohs(*(ushort *)pkt));
  470. switch (ntohs(*(ushort *)pkt)) {
  471. case TFTP_ERR_FILE_NOT_FOUND:
  472. case TFTP_ERR_ACCESS_DENIED:
  473. puts("Not retrying...\n");
  474. eth_halt();
  475. NetState = NETLOOP_FAIL;
  476. break;
  477. case TFTP_ERR_UNDEFINED:
  478. case TFTP_ERR_DISK_FULL:
  479. case TFTP_ERR_UNEXPECTED_OPCODE:
  480. case TFTP_ERR_UNKNOWN_TRANSFER_ID:
  481. case TFTP_ERR_FILE_ALREADY_EXISTS:
  482. default:
  483. puts("Starting again\n\n");
  484. #ifdef CONFIG_MCAST_TFTP
  485. mcast_cleanup();
  486. #endif
  487. NetStartAgain();
  488. break;
  489. }
  490. break;
  491. }
  492. }
  493. static void
  494. TftpTimeout(void)
  495. {
  496. if (++TftpTimeoutCount > TftpTimeoutCountMax) {
  497. puts("\nRetry count exceeded; starting again\n");
  498. #ifdef CONFIG_MCAST_TFTP
  499. mcast_cleanup();
  500. #endif
  501. NetStartAgain();
  502. } else {
  503. puts("T ");
  504. NetSetTimeout(TftpTimeoutMSecs, TftpTimeout);
  505. if (TftpState != STATE_RECV_WRQ)
  506. TftpSend();
  507. }
  508. }
  509. void
  510. TftpStart(void)
  511. {
  512. char *ep; /* Environment pointer */
  513. /*
  514. * Allow the user to choose TFTP blocksize and timeout.
  515. * TFTP protocol has a minimal timeout of 1 second.
  516. */
  517. ep = getenv("tftpblocksize");
  518. if (ep != NULL)
  519. TftpBlkSizeOption = simple_strtol(ep, NULL, 10);
  520. ep = getenv("tftptimeout");
  521. if (ep != NULL)
  522. TftpTimeoutMSecs = simple_strtol(ep, NULL, 10);
  523. if (TftpTimeoutMSecs < 1000) {
  524. printf("TFTP timeout (%ld ms) too low, "
  525. "set minimum = 1000 ms\n",
  526. TftpTimeoutMSecs);
  527. TftpTimeoutMSecs = 1000;
  528. }
  529. debug("TFTP blocksize = %i, timeout = %ld ms\n",
  530. TftpBlkSizeOption, TftpTimeoutMSecs);
  531. TftpRemoteIP = NetServerIP;
  532. if (BootFile[0] == '\0') {
  533. sprintf(default_filename, "%02lX%02lX%02lX%02lX.img",
  534. NetOurIP & 0xFF,
  535. (NetOurIP >> 8) & 0xFF,
  536. (NetOurIP >> 16) & 0xFF,
  537. (NetOurIP >> 24) & 0xFF);
  538. strncpy(tftp_filename, default_filename, MAX_LEN);
  539. tftp_filename[MAX_LEN-1] = 0;
  540. printf("*** Warning: no boot file name; using '%s'\n",
  541. tftp_filename);
  542. } else {
  543. char *p = strchr(BootFile, ':');
  544. if (p == NULL) {
  545. strncpy(tftp_filename, BootFile, MAX_LEN);
  546. tftp_filename[MAX_LEN-1] = 0;
  547. } else {
  548. TftpRemoteIP = string_to_ip(BootFile);
  549. strncpy(tftp_filename, p + 1, MAX_LEN);
  550. tftp_filename[MAX_LEN-1] = 0;
  551. }
  552. }
  553. #if defined(CONFIG_NET_MULTI)
  554. printf("Using %s device\n", eth_get_name());
  555. #endif
  556. printf("TFTP from server %pI4"
  557. "; our IP address is %pI4", &TftpRemoteIP, &NetOurIP);
  558. /* Check if we need to send across this subnet */
  559. if (NetOurGatewayIP && NetOurSubnetMask) {
  560. IPaddr_t OurNet = NetOurIP & NetOurSubnetMask;
  561. IPaddr_t RemoteNet = TftpRemoteIP & NetOurSubnetMask;
  562. if (OurNet != RemoteNet)
  563. printf("; sending through gateway %pI4",
  564. &NetOurGatewayIP);
  565. }
  566. putc('\n');
  567. printf("Filename '%s'.", tftp_filename);
  568. if (NetBootFileSize) {
  569. printf(" Size is 0x%x Bytes = ", NetBootFileSize<<9);
  570. print_size(NetBootFileSize<<9, "");
  571. }
  572. putc('\n');
  573. printf("Load address: 0x%lx\n", load_addr);
  574. puts("Loading: *\b");
  575. TftpTimeoutCountMax = TftpRRQTimeoutCountMax;
  576. NetSetTimeout(TftpTimeoutMSecs, TftpTimeout);
  577. NetSetHandler(TftpHandler);
  578. TftpRemotePort = WELL_KNOWN_PORT;
  579. TftpTimeoutCount = 0;
  580. TftpState = STATE_SEND_RRQ;
  581. /* Use a pseudo-random port unless a specific port is set */
  582. TftpOurPort = 1024 + (get_timer(0) % 3072);
  583. #ifdef CONFIG_TFTP_PORT
  584. ep = getenv("tftpdstp");
  585. if (ep != NULL)
  586. TftpRemotePort = simple_strtol(ep, NULL, 10);
  587. ep = getenv("tftpsrcp");
  588. if (ep != NULL)
  589. TftpOurPort = simple_strtol(ep, NULL, 10);
  590. #endif
  591. TftpBlock = 0;
  592. /* zero out server ether in case the server ip has changed */
  593. memset(NetServerEther, 0, 6);
  594. /* Revert TftpBlkSize to dflt */
  595. TftpBlkSize = TFTP_BLOCK_SIZE;
  596. #ifdef CONFIG_MCAST_TFTP
  597. mcast_cleanup();
  598. #endif
  599. #ifdef CONFIG_TFTP_TSIZE
  600. TftpTsize = 0;
  601. TftpNumchars = 0;
  602. #endif
  603. TftpSend();
  604. }
  605. #ifdef CONFIG_CMD_TFTPSRV
  606. void
  607. TftpStartServer(void)
  608. {
  609. tftp_filename[0] = 0;
  610. #if defined(CONFIG_NET_MULTI)
  611. printf("Using %s device\n", eth_get_name());
  612. #endif
  613. printf("Listening for TFTP transfer on %pI4\n", &NetOurIP);
  614. printf("Load address: 0x%lx\n", load_addr);
  615. puts("Loading: *\b");
  616. TftpTimeoutCountMax = TIMEOUT_COUNT;
  617. TftpTimeoutCount = 0;
  618. TftpTimeoutMSecs = TIMEOUT;
  619. NetSetTimeout(TftpTimeoutMSecs, TftpTimeout);
  620. /* Revert TftpBlkSize to dflt */
  621. TftpBlkSize = TFTP_BLOCK_SIZE;
  622. TftpBlock = 0;
  623. TftpOurPort = WELL_KNOWN_PORT;
  624. #ifdef CONFIG_TFTP_TSIZE
  625. TftpTsize = 0;
  626. TftpNumchars = 0;
  627. #endif
  628. TftpState = STATE_RECV_WRQ;
  629. NetSetHandler(TftpHandler);
  630. }
  631. #endif /* CONFIG_CMD_TFTPSRV */
  632. #ifdef CONFIG_MCAST_TFTP
  633. /* Credits: atftp project.
  634. */
  635. /* pick up BcastAddr, Port, and whether I am [now] the master-client. *
  636. * Frame:
  637. * +-------+-----------+---+-------~~-------+---+
  638. * | opc | multicast | 0 | addr, port, mc | 0 |
  639. * +-------+-----------+---+-------~~-------+---+
  640. * The multicast addr/port becomes what I listen to, and if 'mc' is '1' then
  641. * I am the new master-client so must send ACKs to DataBlocks. If I am not
  642. * master-client, I'm a passive client, gathering what DataBlocks I may and
  643. * making note of which ones I got in my bitmask.
  644. * In theory, I never go from master->passive..
  645. * .. this comes in with pkt already pointing just past opc
  646. */
  647. static void parse_multicast_oack(char *pkt, int len)
  648. {
  649. int i;
  650. IPaddr_t addr;
  651. char *mc_adr, *port, *mc;
  652. mc_adr = port = mc = NULL;
  653. /* march along looking for 'multicast\0', which has to start at least
  654. * 14 bytes back from the end.
  655. */
  656. for (i = 0; i < len-14; i++)
  657. if (strcmp(pkt+i, "multicast") == 0)
  658. break;
  659. if (i >= (len-14)) /* non-Multicast OACK, ign. */
  660. return;
  661. i += 10; /* strlen multicast */
  662. mc_adr = pkt+i;
  663. for (; i < len; i++) {
  664. if (*(pkt+i) == ',') {
  665. *(pkt+i) = '\0';
  666. if (port) {
  667. mc = pkt+i+1;
  668. break;
  669. } else {
  670. port = pkt+i+1;
  671. }
  672. }
  673. }
  674. if (!port || !mc_adr || !mc)
  675. return;
  676. if (Multicast && MasterClient) {
  677. printf("I got a OACK as master Client, WRONG!\n");
  678. return;
  679. }
  680. /* ..I now accept packets destined for this MCAST addr, port */
  681. if (!Multicast) {
  682. if (Bitmap) {
  683. printf("Internal failure! no mcast.\n");
  684. free(Bitmap);
  685. Bitmap = NULL;
  686. ProhibitMcast = 1;
  687. return ;
  688. }
  689. /* I malloc instead of pre-declare; so that if the file ends
  690. * up being too big for this bitmap I can retry
  691. */
  692. Bitmap = malloc(Mapsize);
  693. if (!Bitmap) {
  694. printf("No Bitmap, no multicast. Sorry.\n");
  695. ProhibitMcast = 1;
  696. return;
  697. }
  698. memset(Bitmap, 0, Mapsize);
  699. PrevBitmapHole = 0;
  700. Multicast = 1;
  701. }
  702. addr = string_to_ip(mc_adr);
  703. if (Mcast_addr != addr) {
  704. if (Mcast_addr)
  705. eth_mcast_join(Mcast_addr, 0);
  706. Mcast_addr = addr;
  707. if (eth_mcast_join(Mcast_addr, 1)) {
  708. printf("Fail to set mcast, revert to TFTP\n");
  709. ProhibitMcast = 1;
  710. mcast_cleanup();
  711. NetStartAgain();
  712. }
  713. }
  714. MasterClient = (unsigned char)simple_strtoul((char *)mc, NULL, 10);
  715. Mcast_port = (unsigned short)simple_strtoul(port, NULL, 10);
  716. printf("Multicast: %s:%d [%d]\n", mc_adr, Mcast_port, MasterClient);
  717. return;
  718. }
  719. #endif /* Multicast TFTP */