tftp.c 7.3 KB

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