tftp.c 17 KB

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