tftp.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813
  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 show_block_marker(void)
  173. {
  174. #ifdef CONFIG_TFTP_TSIZE
  175. if (TftpTsize) {
  176. ulong pos = TftpBlock * TftpBlkSize + TftpBlockWrapOffset;
  177. while (TftpNumchars < pos * 50 / TftpTsize) {
  178. putc('#');
  179. TftpNumchars++;
  180. }
  181. }
  182. #endif
  183. else {
  184. if (((TftpBlock - 1) % 10) == 0)
  185. putc('#');
  186. else if ((TftpBlock % (10 * HASHES_PER_LINE)) == 0)
  187. puts("\n\t ");
  188. }
  189. }
  190. /* The TFTP get or put is complete */
  191. static void tftp_complete(void)
  192. {
  193. #ifdef CONFIG_TFTP_TSIZE
  194. /* Print hash marks for the last packet received */
  195. while (TftpTsize && TftpNumchars < 49) {
  196. putc('#');
  197. TftpNumchars++;
  198. }
  199. #endif
  200. puts("\ndone\n");
  201. NetState = NETLOOP_SUCCESS;
  202. }
  203. static void
  204. TftpSend(void)
  205. {
  206. volatile uchar *pkt;
  207. volatile uchar *xp;
  208. int len = 0;
  209. volatile ushort *s;
  210. #ifdef CONFIG_MCAST_TFTP
  211. /* Multicast TFTP.. non-MasterClients do not ACK data. */
  212. if (Multicast
  213. && (TftpState == STATE_DATA)
  214. && (MasterClient == 0))
  215. return;
  216. #endif
  217. /*
  218. * We will always be sending some sort of packet, so
  219. * cobble together the packet headers now.
  220. */
  221. pkt = NetTxPacket + NetEthHdrSize() + IP_HDR_SIZE;
  222. switch (TftpState) {
  223. case STATE_SEND_RRQ:
  224. xp = pkt;
  225. s = (ushort *)pkt;
  226. *s++ = htons(TFTP_RRQ);
  227. pkt = (uchar *)s;
  228. strcpy((char *)pkt, tftp_filename);
  229. pkt += strlen(tftp_filename) + 1;
  230. strcpy((char *)pkt, "octet");
  231. pkt += 5 /*strlen("octet")*/ + 1;
  232. strcpy((char *)pkt, "timeout");
  233. pkt += 7 /*strlen("timeout")*/ + 1;
  234. sprintf((char *)pkt, "%lu", TftpTimeoutMSecs / 1000);
  235. debug("send option \"timeout %s\"\n", (char *)pkt);
  236. pkt += strlen((char *)pkt) + 1;
  237. #ifdef CONFIG_TFTP_TSIZE
  238. memcpy((char *)pkt, "tsize\0000\0", 8);
  239. pkt += 8;
  240. #endif
  241. /* try for more effic. blk size */
  242. pkt += sprintf((char *)pkt, "blksize%c%d%c",
  243. 0, TftpBlkSizeOption, 0);
  244. #ifdef CONFIG_MCAST_TFTP
  245. /* Check all preconditions before even trying the option */
  246. if (!ProhibitMcast
  247. && (Bitmap = malloc(Mapsize))
  248. && eth_get_dev()->mcast) {
  249. free(Bitmap);
  250. Bitmap = NULL;
  251. pkt += sprintf((char *)pkt, "multicast%c%c", 0, 0);
  252. }
  253. #endif /* CONFIG_MCAST_TFTP */
  254. len = pkt - xp;
  255. break;
  256. case STATE_OACK:
  257. #ifdef CONFIG_MCAST_TFTP
  258. /* My turn! Start at where I need blocks I missed.*/
  259. if (Multicast)
  260. TftpBlock = ext2_find_next_zero_bit(Bitmap,
  261. (Mapsize*8), 0);
  262. /*..falling..*/
  263. #endif
  264. case STATE_RECV_WRQ:
  265. case STATE_DATA:
  266. xp = pkt;
  267. s = (ushort *)pkt;
  268. *s++ = htons(TFTP_ACK);
  269. *s++ = htons(TftpBlock);
  270. pkt = (uchar *)s;
  271. len = pkt - xp;
  272. break;
  273. case STATE_TOO_LARGE:
  274. xp = pkt;
  275. s = (ushort *)pkt;
  276. *s++ = htons(TFTP_ERROR);
  277. *s++ = htons(3);
  278. pkt = (uchar *)s;
  279. strcpy((char *)pkt, "File too large");
  280. pkt += 14 /*strlen("File too large")*/ + 1;
  281. len = pkt - xp;
  282. break;
  283. case STATE_BAD_MAGIC:
  284. xp = pkt;
  285. s = (ushort *)pkt;
  286. *s++ = htons(TFTP_ERROR);
  287. *s++ = htons(2);
  288. pkt = (uchar *)s;
  289. strcpy((char *)pkt, "File has bad magic");
  290. pkt += 18 /*strlen("File has bad magic")*/ + 1;
  291. len = pkt - xp;
  292. break;
  293. }
  294. NetSendUDPPacket(NetServerEther, TftpRemoteIP, TftpRemotePort,
  295. TftpOurPort, len);
  296. }
  297. static void
  298. TftpHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src,
  299. unsigned len)
  300. {
  301. ushort proto;
  302. ushort *s;
  303. int i;
  304. if (dest != TftpOurPort) {
  305. #ifdef CONFIG_MCAST_TFTP
  306. if (Multicast
  307. && (!Mcast_port || (dest != Mcast_port)))
  308. #endif
  309. return;
  310. }
  311. if (TftpState != STATE_SEND_RRQ && src != TftpRemotePort &&
  312. TftpState != STATE_RECV_WRQ)
  313. return;
  314. if (len < 2)
  315. return;
  316. len -= 2;
  317. /* warning: don't use increment (++) in ntohs() macros!! */
  318. s = (ushort *)pkt;
  319. proto = *s++;
  320. pkt = (uchar *)s;
  321. switch (ntohs(proto)) {
  322. case TFTP_RRQ:
  323. case TFTP_ACK:
  324. break;
  325. default:
  326. break;
  327. #ifdef CONFIG_CMD_TFTPSRV
  328. case TFTP_WRQ:
  329. debug("Got WRQ\n");
  330. TftpRemoteIP = sip;
  331. TftpRemotePort = src;
  332. TftpOurPort = 1024 + (get_timer(0) % 3072);
  333. TftpLastBlock = 0;
  334. TftpBlockWrap = 0;
  335. TftpBlockWrapOffset = 0;
  336. TftpSend(); /* Send ACK(0) */
  337. break;
  338. #endif
  339. case TFTP_OACK:
  340. debug("Got OACK: %s %s\n",
  341. pkt,
  342. pkt + strlen((char *)pkt) + 1);
  343. TftpState = STATE_OACK;
  344. TftpRemotePort = src;
  345. /*
  346. * Check for 'blksize' option.
  347. * Careful: "i" is signed, "len" is unsigned, thus
  348. * something like "len-8" may give a *huge* number
  349. */
  350. for (i = 0; i+8 < len; i++) {
  351. if (strcmp((char *)pkt+i, "blksize") == 0) {
  352. TftpBlkSize = (unsigned short)
  353. simple_strtoul((char *)pkt+i+8, NULL,
  354. 10);
  355. debug("Blocksize ack: %s, %d\n",
  356. (char *)pkt+i+8, TftpBlkSize);
  357. }
  358. #ifdef CONFIG_TFTP_TSIZE
  359. if (strcmp((char *)pkt+i, "tsize") == 0) {
  360. TftpTsize = simple_strtoul((char *)pkt+i+6,
  361. NULL, 10);
  362. debug("size = %s, %d\n",
  363. (char *)pkt+i+6, TftpTsize);
  364. }
  365. #endif
  366. }
  367. #ifdef CONFIG_MCAST_TFTP
  368. parse_multicast_oack((char *)pkt, len-1);
  369. if ((Multicast) && (!MasterClient))
  370. TftpState = STATE_DATA; /* passive.. */
  371. else
  372. #endif
  373. TftpSend(); /* Send ACK */
  374. break;
  375. case TFTP_DATA:
  376. if (len < 2)
  377. return;
  378. len -= 2;
  379. TftpBlock = ntohs(*(ushort *)pkt);
  380. /*
  381. * RFC1350 specifies that the first data packet will
  382. * have sequence number 1. If we receive a sequence
  383. * number of 0 this means that there was a wrap
  384. * around of the (16 bit) counter.
  385. */
  386. if (TftpBlock == 0) {
  387. TftpBlockWrap++;
  388. TftpBlockWrapOffset +=
  389. TftpBlkSize * TFTP_SEQUENCE_SIZE;
  390. printf("\n\t %lu MB received\n\t ",
  391. TftpBlockWrapOffset>>20);
  392. } else {
  393. show_block_marker();
  394. }
  395. if (TftpState == STATE_SEND_RRQ)
  396. debug("Server did not acknowledge timeout option!\n");
  397. if (TftpState == STATE_SEND_RRQ || TftpState == STATE_OACK ||
  398. TftpState == STATE_RECV_WRQ) {
  399. /* first block received */
  400. TftpState = STATE_DATA;
  401. TftpRemotePort = src;
  402. TftpLastBlock = 0;
  403. TftpBlockWrap = 0;
  404. TftpBlockWrapOffset = 0;
  405. #ifdef CONFIG_MCAST_TFTP
  406. if (Multicast) { /* start!=1 common if mcast */
  407. TftpLastBlock = TftpBlock - 1;
  408. } else
  409. #endif
  410. if (TftpBlock != 1) { /* Assertion */
  411. printf("\nTFTP error: "
  412. "First block is not block 1 (%ld)\n"
  413. "Starting again\n\n",
  414. TftpBlock);
  415. NetStartAgain();
  416. break;
  417. }
  418. }
  419. if (TftpBlock == TftpLastBlock) {
  420. /*
  421. * Same block again; ignore it.
  422. */
  423. break;
  424. }
  425. TftpLastBlock = TftpBlock;
  426. TftpTimeoutCountMax = TIMEOUT_COUNT;
  427. NetSetTimeout(TftpTimeoutMSecs, TftpTimeout);
  428. store_block(TftpBlock - 1, pkt + 2, len);
  429. /*
  430. * Acknowledge the block just received, which will prompt
  431. * the remote for the next one.
  432. */
  433. #ifdef CONFIG_MCAST_TFTP
  434. /* if I am the MasterClient, actively calculate what my next
  435. * needed block is; else I'm passive; not ACKING
  436. */
  437. if (Multicast) {
  438. if (len < TftpBlkSize) {
  439. TftpEndingBlock = TftpBlock;
  440. } else if (MasterClient) {
  441. TftpBlock = PrevBitmapHole =
  442. ext2_find_next_zero_bit(
  443. Bitmap,
  444. (Mapsize*8),
  445. PrevBitmapHole);
  446. if (TftpBlock > ((Mapsize*8) - 1)) {
  447. printf("tftpfile too big\n");
  448. /* try to double it and retry */
  449. Mapsize <<= 1;
  450. mcast_cleanup();
  451. NetStartAgain();
  452. return;
  453. }
  454. TftpLastBlock = TftpBlock;
  455. }
  456. }
  457. #endif
  458. TftpSend();
  459. #ifdef CONFIG_MCAST_TFTP
  460. if (Multicast) {
  461. if (MasterClient && (TftpBlock >= TftpEndingBlock)) {
  462. puts("\nMulticast tftp done\n");
  463. mcast_cleanup();
  464. NetState = NETLOOP_SUCCESS;
  465. }
  466. }
  467. else
  468. #endif
  469. if (len < TftpBlkSize)
  470. tftp_complete();
  471. break;
  472. case TFTP_ERROR:
  473. printf("\nTFTP error: '%s' (%d)\n",
  474. pkt + 2, ntohs(*(ushort *)pkt));
  475. switch (ntohs(*(ushort *)pkt)) {
  476. case TFTP_ERR_FILE_NOT_FOUND:
  477. case TFTP_ERR_ACCESS_DENIED:
  478. puts("Not retrying...\n");
  479. eth_halt();
  480. NetState = NETLOOP_FAIL;
  481. break;
  482. case TFTP_ERR_UNDEFINED:
  483. case TFTP_ERR_DISK_FULL:
  484. case TFTP_ERR_UNEXPECTED_OPCODE:
  485. case TFTP_ERR_UNKNOWN_TRANSFER_ID:
  486. case TFTP_ERR_FILE_ALREADY_EXISTS:
  487. default:
  488. puts("Starting again\n\n");
  489. #ifdef CONFIG_MCAST_TFTP
  490. mcast_cleanup();
  491. #endif
  492. NetStartAgain();
  493. break;
  494. }
  495. break;
  496. }
  497. }
  498. static void
  499. TftpTimeout(void)
  500. {
  501. if (++TftpTimeoutCount > TftpTimeoutCountMax) {
  502. puts("\nRetry count exceeded; starting again\n");
  503. #ifdef CONFIG_MCAST_TFTP
  504. mcast_cleanup();
  505. #endif
  506. NetStartAgain();
  507. } else {
  508. puts("T ");
  509. NetSetTimeout(TftpTimeoutMSecs, TftpTimeout);
  510. if (TftpState != STATE_RECV_WRQ)
  511. TftpSend();
  512. }
  513. }
  514. void
  515. TftpStart(void)
  516. {
  517. char *ep; /* Environment pointer */
  518. /*
  519. * Allow the user to choose TFTP blocksize and timeout.
  520. * TFTP protocol has a minimal timeout of 1 second.
  521. */
  522. ep = getenv("tftpblocksize");
  523. if (ep != NULL)
  524. TftpBlkSizeOption = simple_strtol(ep, NULL, 10);
  525. ep = getenv("tftptimeout");
  526. if (ep != NULL)
  527. TftpTimeoutMSecs = simple_strtol(ep, NULL, 10);
  528. if (TftpTimeoutMSecs < 1000) {
  529. printf("TFTP timeout (%ld ms) too low, "
  530. "set minimum = 1000 ms\n",
  531. TftpTimeoutMSecs);
  532. TftpTimeoutMSecs = 1000;
  533. }
  534. debug("TFTP blocksize = %i, timeout = %ld ms\n",
  535. TftpBlkSizeOption, TftpTimeoutMSecs);
  536. TftpRemoteIP = NetServerIP;
  537. if (BootFile[0] == '\0') {
  538. sprintf(default_filename, "%02lX%02lX%02lX%02lX.img",
  539. NetOurIP & 0xFF,
  540. (NetOurIP >> 8) & 0xFF,
  541. (NetOurIP >> 16) & 0xFF,
  542. (NetOurIP >> 24) & 0xFF);
  543. strncpy(tftp_filename, default_filename, MAX_LEN);
  544. tftp_filename[MAX_LEN-1] = 0;
  545. printf("*** Warning: no boot file name; using '%s'\n",
  546. tftp_filename);
  547. } else {
  548. char *p = strchr(BootFile, ':');
  549. if (p == NULL) {
  550. strncpy(tftp_filename, BootFile, MAX_LEN);
  551. tftp_filename[MAX_LEN-1] = 0;
  552. } else {
  553. TftpRemoteIP = string_to_ip(BootFile);
  554. strncpy(tftp_filename, p + 1, MAX_LEN);
  555. tftp_filename[MAX_LEN-1] = 0;
  556. }
  557. }
  558. printf("Using %s device\n", eth_get_name());
  559. printf("TFTP from server %pI4"
  560. "; our IP address is %pI4", &TftpRemoteIP, &NetOurIP);
  561. /* Check if we need to send across this subnet */
  562. if (NetOurGatewayIP && NetOurSubnetMask) {
  563. IPaddr_t OurNet = NetOurIP & NetOurSubnetMask;
  564. IPaddr_t RemoteNet = TftpRemoteIP & NetOurSubnetMask;
  565. if (OurNet != RemoteNet)
  566. printf("; sending through gateway %pI4",
  567. &NetOurGatewayIP);
  568. }
  569. putc('\n');
  570. printf("Filename '%s'.", tftp_filename);
  571. if (NetBootFileSize) {
  572. printf(" Size is 0x%x Bytes = ", NetBootFileSize<<9);
  573. print_size(NetBootFileSize<<9, "");
  574. }
  575. putc('\n');
  576. printf("Load address: 0x%lx\n", load_addr);
  577. puts("Loading: *\b");
  578. TftpTimeoutCountMax = TftpRRQTimeoutCountMax;
  579. NetSetTimeout(TftpTimeoutMSecs, TftpTimeout);
  580. NetSetHandler(TftpHandler);
  581. TftpRemotePort = WELL_KNOWN_PORT;
  582. TftpTimeoutCount = 0;
  583. TftpState = STATE_SEND_RRQ;
  584. /* Use a pseudo-random port unless a specific port is set */
  585. TftpOurPort = 1024 + (get_timer(0) % 3072);
  586. #ifdef CONFIG_TFTP_PORT
  587. ep = getenv("tftpdstp");
  588. if (ep != NULL)
  589. TftpRemotePort = simple_strtol(ep, NULL, 10);
  590. ep = getenv("tftpsrcp");
  591. if (ep != NULL)
  592. TftpOurPort = simple_strtol(ep, NULL, 10);
  593. #endif
  594. TftpBlock = 0;
  595. /* zero out server ether in case the server ip has changed */
  596. memset(NetServerEther, 0, 6);
  597. /* Revert TftpBlkSize to dflt */
  598. TftpBlkSize = TFTP_BLOCK_SIZE;
  599. #ifdef CONFIG_MCAST_TFTP
  600. mcast_cleanup();
  601. #endif
  602. #ifdef CONFIG_TFTP_TSIZE
  603. TftpTsize = 0;
  604. TftpNumchars = 0;
  605. #endif
  606. TftpSend();
  607. }
  608. #ifdef CONFIG_CMD_TFTPSRV
  609. void
  610. TftpStartServer(void)
  611. {
  612. tftp_filename[0] = 0;
  613. printf("Using %s device\n", eth_get_name());
  614. printf("Listening for TFTP transfer on %pI4\n", &NetOurIP);
  615. printf("Load address: 0x%lx\n", load_addr);
  616. puts("Loading: *\b");
  617. TftpTimeoutCountMax = TIMEOUT_COUNT;
  618. TftpTimeoutCount = 0;
  619. TftpTimeoutMSecs = TIMEOUT;
  620. NetSetTimeout(TftpTimeoutMSecs, TftpTimeout);
  621. /* Revert TftpBlkSize to dflt */
  622. TftpBlkSize = TFTP_BLOCK_SIZE;
  623. TftpBlock = 0;
  624. TftpOurPort = WELL_KNOWN_PORT;
  625. #ifdef CONFIG_TFTP_TSIZE
  626. TftpTsize = 0;
  627. TftpNumchars = 0;
  628. #endif
  629. TftpState = STATE_RECV_WRQ;
  630. NetSetHandler(TftpHandler);
  631. }
  632. #endif /* CONFIG_CMD_TFTPSRV */
  633. #ifdef CONFIG_MCAST_TFTP
  634. /* Credits: atftp project.
  635. */
  636. /* pick up BcastAddr, Port, and whether I am [now] the master-client. *
  637. * Frame:
  638. * +-------+-----------+---+-------~~-------+---+
  639. * | opc | multicast | 0 | addr, port, mc | 0 |
  640. * +-------+-----------+---+-------~~-------+---+
  641. * The multicast addr/port becomes what I listen to, and if 'mc' is '1' then
  642. * I am the new master-client so must send ACKs to DataBlocks. If I am not
  643. * master-client, I'm a passive client, gathering what DataBlocks I may and
  644. * making note of which ones I got in my bitmask.
  645. * In theory, I never go from master->passive..
  646. * .. this comes in with pkt already pointing just past opc
  647. */
  648. static void parse_multicast_oack(char *pkt, int len)
  649. {
  650. int i;
  651. IPaddr_t addr;
  652. char *mc_adr, *port, *mc;
  653. mc_adr = port = mc = NULL;
  654. /* march along looking for 'multicast\0', which has to start at least
  655. * 14 bytes back from the end.
  656. */
  657. for (i = 0; i < len-14; i++)
  658. if (strcmp(pkt+i, "multicast") == 0)
  659. break;
  660. if (i >= (len-14)) /* non-Multicast OACK, ign. */
  661. return;
  662. i += 10; /* strlen multicast */
  663. mc_adr = pkt+i;
  664. for (; i < len; i++) {
  665. if (*(pkt+i) == ',') {
  666. *(pkt+i) = '\0';
  667. if (port) {
  668. mc = pkt+i+1;
  669. break;
  670. } else {
  671. port = pkt+i+1;
  672. }
  673. }
  674. }
  675. if (!port || !mc_adr || !mc)
  676. return;
  677. if (Multicast && MasterClient) {
  678. printf("I got a OACK as master Client, WRONG!\n");
  679. return;
  680. }
  681. /* ..I now accept packets destined for this MCAST addr, port */
  682. if (!Multicast) {
  683. if (Bitmap) {
  684. printf("Internal failure! no mcast.\n");
  685. free(Bitmap);
  686. Bitmap = NULL;
  687. ProhibitMcast = 1;
  688. return ;
  689. }
  690. /* I malloc instead of pre-declare; so that if the file ends
  691. * up being too big for this bitmap I can retry
  692. */
  693. Bitmap = malloc(Mapsize);
  694. if (!Bitmap) {
  695. printf("No Bitmap, no multicast. Sorry.\n");
  696. ProhibitMcast = 1;
  697. return;
  698. }
  699. memset(Bitmap, 0, Mapsize);
  700. PrevBitmapHole = 0;
  701. Multicast = 1;
  702. }
  703. addr = string_to_ip(mc_adr);
  704. if (Mcast_addr != addr) {
  705. if (Mcast_addr)
  706. eth_mcast_join(Mcast_addr, 0);
  707. Mcast_addr = addr;
  708. if (eth_mcast_join(Mcast_addr, 1)) {
  709. printf("Fail to set mcast, revert to TFTP\n");
  710. ProhibitMcast = 1;
  711. mcast_cleanup();
  712. NetStartAgain();
  713. }
  714. }
  715. MasterClient = (unsigned char)simple_strtoul((char *)mc, NULL, 10);
  716. Mcast_port = (unsigned short)simple_strtoul(port, NULL, 10);
  717. printf("Multicast: %s:%d [%d]\n", mc_adr, Mcast_port, MasterClient);
  718. return;
  719. }
  720. #endif /* Multicast TFTP */