tftp.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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. #undef ET_DEBUG
  12. #if (CONFIG_COMMANDS & CFG_CMD_NET)
  13. #define WELL_KNOWN_PORT 69 /* Well known TFTP port # */
  14. #define TIMEOUT 5 /* Seconds to timeout for a lost pkt */
  15. #ifndef CONFIG_NET_RETRY_COUNT
  16. # define TIMEOUT_COUNT 10 /* # of timeouts before giving up */
  17. #else
  18. # define TIMEOUT_COUNT (CONFIG_NET_RETRY_COUNT * 2)
  19. #endif
  20. /* (for checking the image size) */
  21. #define HASHES_PER_LINE 65 /* Number of "loading" hashes per line */
  22. /*
  23. * TFTP operations.
  24. */
  25. #define TFTP_RRQ 1
  26. #define TFTP_WRQ 2
  27. #define TFTP_DATA 3
  28. #define TFTP_ACK 4
  29. #define TFTP_ERROR 5
  30. #define TFTP_OACK 6
  31. static int TftpServerPort; /* The UDP port at their end */
  32. static int TftpOurPort; /* The UDP port at our end */
  33. static int TftpTimeoutCount;
  34. static ulong TftpBlock; /* packet sequence number */
  35. static ulong TftpLastBlock; /* last packet sequence number received */
  36. static ulong TftpBlockWrap; /* count of sequence number wraparounds */
  37. static ulong TftpBlockWrapOffset; /* memory offset due to wrapping */
  38. static int TftpState;
  39. #define STATE_RRQ 1
  40. #define STATE_DATA 2
  41. #define STATE_TOO_LARGE 3
  42. #define STATE_BAD_MAGIC 4
  43. #define STATE_OACK 5
  44. #define TFTP_BLOCK_SIZE 512 /* default TFTP block size */
  45. #define TFTP_SEQUENCE_SIZE ((ulong)(1<<16)) /* sequence number is 16 bit */
  46. #define DEFAULT_NAME_LEN (8 + 4 + 1)
  47. static char default_filename[DEFAULT_NAME_LEN];
  48. static char *tftp_filename;
  49. #ifdef CFG_DIRECT_FLASH_TFTP
  50. extern flash_info_t flash_info[CFG_MAX_FLASH_BANKS];
  51. #endif
  52. static __inline__ void
  53. store_block (unsigned block, uchar * src, unsigned len)
  54. {
  55. ulong offset = block * TFTP_BLOCK_SIZE + TftpBlockWrapOffset;
  56. ulong newsize = offset + len;
  57. #ifdef CFG_DIRECT_FLASH_TFTP
  58. int i, rc = 0;
  59. for (i=0; i<CFG_MAX_FLASH_BANKS; i++) {
  60. /* start address in flash? */
  61. if (load_addr + offset >= flash_info[i].start[0]) {
  62. rc = 1;
  63. break;
  64. }
  65. }
  66. if (rc) { /* Flash is destination for this packet */
  67. rc = flash_write ((uchar *)src, (ulong)(load_addr+offset), len);
  68. if (rc) {
  69. flash_perror (rc);
  70. NetState = NETLOOP_FAIL;
  71. return;
  72. }
  73. }
  74. else
  75. #endif /* CFG_DIRECT_FLASH_TFTP */
  76. {
  77. (void)memcpy((void *)(load_addr + offset), src, len);
  78. }
  79. if (NetBootFileXferSize < newsize)
  80. NetBootFileXferSize = newsize;
  81. }
  82. static void TftpSend (void);
  83. static void TftpTimeout (void);
  84. /**********************************************************************/
  85. static void
  86. TftpSend (void)
  87. {
  88. volatile uchar * pkt;
  89. volatile uchar * xp;
  90. int len = 0;
  91. /*
  92. * We will always be sending some sort of packet, so
  93. * cobble together the packet headers now.
  94. */
  95. pkt = NetTxPacket + NetEthHdrSize() + IP_HDR_SIZE;
  96. switch (TftpState) {
  97. case STATE_RRQ:
  98. xp = pkt;
  99. *((ushort *)pkt)++ = htons(TFTP_RRQ);
  100. strcpy ((char *)pkt, tftp_filename);
  101. pkt += strlen(tftp_filename) + 1;
  102. strcpy ((char *)pkt, "octet");
  103. pkt += 5 /*strlen("octet")*/ + 1;
  104. strcpy ((char *)pkt, "timeout");
  105. pkt += 7 /*strlen("timeout")*/ + 1;
  106. sprintf((char *)pkt, "%d", TIMEOUT);
  107. #ifdef ET_DEBUG
  108. printf("send option \"timeout %s\"\n", (char *)pkt);
  109. #endif
  110. pkt += strlen((char *)pkt) + 1;
  111. len = pkt - xp;
  112. break;
  113. case STATE_DATA:
  114. case STATE_OACK:
  115. xp = pkt;
  116. *((ushort *)pkt)++ = htons(TFTP_ACK);
  117. *((ushort *)pkt)++ = htons(TftpBlock);
  118. len = pkt - xp;
  119. break;
  120. case STATE_TOO_LARGE:
  121. xp = pkt;
  122. *((ushort *)pkt)++ = htons(TFTP_ERROR);
  123. *((ushort *)pkt)++ = htons(3);
  124. strcpy ((char *)pkt, "File too large");
  125. pkt += 14 /*strlen("File too large")*/ + 1;
  126. len = pkt - xp;
  127. break;
  128. case STATE_BAD_MAGIC:
  129. xp = pkt;
  130. *((ushort *)pkt)++ = htons(TFTP_ERROR);
  131. *((ushort *)pkt)++ = htons(2);
  132. strcpy ((char *)pkt, "File has bad magic");
  133. pkt += 18 /*strlen("File has bad magic")*/ + 1;
  134. len = pkt - xp;
  135. break;
  136. }
  137. NetSendUDPPacket(NetServerEther, NetServerIP, TftpServerPort, TftpOurPort, len);
  138. }
  139. static void
  140. TftpHandler (uchar * pkt, unsigned dest, unsigned src, unsigned len)
  141. {
  142. ushort proto;
  143. if (dest != TftpOurPort) {
  144. return;
  145. }
  146. if (TftpState != STATE_RRQ && src != TftpServerPort) {
  147. return;
  148. }
  149. if (len < 2) {
  150. return;
  151. }
  152. len -= 2;
  153. /* warning: don't use increment (++) in ntohs() macros!! */
  154. proto = *((ushort *)pkt)++;
  155. switch (ntohs(proto)) {
  156. case TFTP_RRQ:
  157. case TFTP_WRQ:
  158. case TFTP_ACK:
  159. break;
  160. default:
  161. break;
  162. case TFTP_OACK:
  163. #ifdef ET_DEBUG
  164. printf("Got OACK: %s %s\n", pkt, pkt+strlen(pkt)+1);
  165. #endif
  166. TftpState = STATE_OACK;
  167. TftpServerPort = src;
  168. TftpSend (); /* Send ACK */
  169. break;
  170. case TFTP_DATA:
  171. if (len < 2)
  172. return;
  173. len -= 2;
  174. TftpBlock = ntohs(*(ushort *)pkt);
  175. /*
  176. * RFC1350 specifies that the first data packet will
  177. * have sequence number 1. If we receive a sequence
  178. * number of 0 this means that there was a wrap
  179. * around of the (16 bit) counter.
  180. */
  181. if (TftpBlock == 0) {
  182. TftpBlockWrap++;
  183. TftpBlockWrapOffset += TFTP_BLOCK_SIZE * TFTP_SEQUENCE_SIZE;
  184. printf ("\n\t %lu MB reveived\n\t ", TftpBlockWrapOffset>>20);
  185. } else {
  186. if (((TftpBlock - 1) % 10) == 0) {
  187. putc ('#');
  188. } else if ((TftpBlock % (10 * HASHES_PER_LINE)) == 0) {
  189. puts ("\n\t ");
  190. }
  191. }
  192. #ifdef ET_DEBUG
  193. if (TftpState == STATE_RRQ) {
  194. puts ("Server did not acknowledge timeout option!\n");
  195. }
  196. #endif
  197. if (TftpState == STATE_RRQ || TftpState == STATE_OACK) {
  198. /* first block received */
  199. TftpState = STATE_DATA;
  200. TftpServerPort = src;
  201. TftpLastBlock = 0;
  202. TftpBlockWrap = 0;
  203. TftpBlockWrapOffset = 0;
  204. if (TftpBlock != 1) { /* Assertion */
  205. printf ("\nTFTP error: "
  206. "First block is not block 1 (%ld)\n"
  207. "Starting again\n\n",
  208. TftpBlock);
  209. NetStartAgain ();
  210. break;
  211. }
  212. }
  213. if (TftpBlock == TftpLastBlock) {
  214. /*
  215. * Same block again; ignore it.
  216. */
  217. break;
  218. }
  219. TftpLastBlock = TftpBlock;
  220. NetSetTimeout (TIMEOUT * CFG_HZ, TftpTimeout);
  221. store_block (TftpBlock - 1, pkt + 2, len);
  222. /*
  223. * Acknoledge the block just received, which will prompt
  224. * the server for the next one.
  225. */
  226. TftpSend ();
  227. if (len < TFTP_BLOCK_SIZE) {
  228. /*
  229. * We received the whole thing. Try to
  230. * run it.
  231. */
  232. puts ("\ndone\n");
  233. NetState = NETLOOP_SUCCESS;
  234. }
  235. break;
  236. case TFTP_ERROR:
  237. printf ("\nTFTP error: '%s' (%d)\n",
  238. pkt + 2, ntohs(*(ushort *)pkt));
  239. puts ("Starting again\n\n");
  240. NetStartAgain ();
  241. break;
  242. }
  243. }
  244. static void
  245. TftpTimeout (void)
  246. {
  247. if (++TftpTimeoutCount > TIMEOUT_COUNT) {
  248. puts ("\nRetry count exceeded; starting again\n");
  249. NetStartAgain ();
  250. } else {
  251. puts ("T ");
  252. NetSetTimeout (TIMEOUT * CFG_HZ, TftpTimeout);
  253. TftpSend ();
  254. }
  255. }
  256. void
  257. TftpStart (void)
  258. {
  259. if (BootFile[0] == '\0') {
  260. IPaddr_t OurIP = ntohl(NetOurIP);
  261. sprintf(default_filename, "%02lX%02lX%02lX%02lX.img",
  262. OurIP & 0xFF,
  263. (OurIP >> 8) & 0xFF,
  264. (OurIP >> 16) & 0xFF,
  265. (OurIP >> 24) & 0xFF );
  266. tftp_filename = default_filename;
  267. printf ("*** Warning: no boot file name; using '%s'\n",
  268. tftp_filename);
  269. } else {
  270. tftp_filename = BootFile;
  271. }
  272. #if defined(CONFIG_NET_MULTI)
  273. printf ("Using %s device\n", eth_get_name());
  274. #endif
  275. puts ("TFTP from server "); print_IPaddr (NetServerIP);
  276. puts ("; our IP address is "); print_IPaddr (NetOurIP);
  277. /* Check if we need to send across this subnet */
  278. if (NetOurGatewayIP && NetOurSubnetMask) {
  279. IPaddr_t OurNet = NetOurIP & NetOurSubnetMask;
  280. IPaddr_t ServerNet = NetServerIP & NetOurSubnetMask;
  281. if (OurNet != ServerNet) {
  282. puts ("; sending through gateway ");
  283. print_IPaddr (NetOurGatewayIP) ;
  284. }
  285. }
  286. putc ('\n');
  287. printf ("Filename '%s'.", tftp_filename);
  288. if (NetBootFileSize) {
  289. printf (" Size is 0x%x Bytes = ", NetBootFileSize<<9);
  290. print_size (NetBootFileSize<<9, "");
  291. }
  292. putc ('\n');
  293. printf ("Load address: 0x%lx\n", load_addr);
  294. puts ("Loading: *\b");
  295. NetSetTimeout (TIMEOUT * CFG_HZ, TftpTimeout);
  296. NetSetHandler (TftpHandler);
  297. TftpServerPort = WELL_KNOWN_PORT;
  298. TftpTimeoutCount = 0;
  299. TftpState = STATE_RRQ;
  300. TftpOurPort = 1024 + (get_timer(0) % 3072);
  301. TftpBlock = 0;
  302. /* zero out server ether in case the server ip has changed */
  303. memset(NetServerEther, 0, 6);
  304. TftpSend ();
  305. }
  306. #endif /* CFG_CMD_NET */