2 * Copyright 1994, 1995, 2000 Neil Russell.
4 * Copyright 2000, 2001 DENX Software Engineering, Wolfgang Denk, wd@denx.de
15 #if (CONFIG_COMMANDS & CFG_CMD_NET)
17 #define WELL_KNOWN_PORT 69 /* Well known TFTP port # */
18 #define TIMEOUT 5 /* Seconds to timeout for a lost pkt */
19 #ifndef CONFIG_NET_RETRY_COUNT
20 # define TIMEOUT_COUNT 10 /* # of timeouts before giving up */
22 # define TIMEOUT_COUNT (CONFIG_NET_RETRY_COUNT * 2)
24 /* (for checking the image size) */
25 #define HASHES_PER_LINE 65 /* Number of "loading" hashes per line */
38 static int TftpServerPort; /* The UDP port at their end */
39 static int TftpOurPort; /* The UDP port at our end */
40 static int TftpTimeoutCount;
41 static unsigned TftpBlock;
42 static unsigned TftpLastBlock;
46 #define STATE_TOO_LARGE 3
47 #define STATE_BAD_MAGIC 4
50 #define DEFAULT_NAME_LEN (8 + 4 + 1)
51 static char default_filename[DEFAULT_NAME_LEN];
52 static char *tftp_filename;
54 #ifdef CFG_DIRECT_FLASH_TFTP
55 extern flash_info_t flash_info[CFG_MAX_FLASH_BANKS];
58 static __inline__ void
59 store_block (unsigned block, uchar * src, unsigned len)
61 ulong offset = block * 512, newsize = offset + len;
62 #ifdef CFG_DIRECT_FLASH_TFTP
65 for (i=0; i<CFG_MAX_FLASH_BANKS; i++) {
66 /* start address in flash? */
67 if (load_addr + offset >= flash_info[i].start[0]) {
73 if (rc) { /* Flash is destination for this packet */
74 rc = flash_write ((uchar *)src, (ulong)(load_addr+offset), len);
77 NetState = NETLOOP_FAIL;
82 #endif /* CFG_DIRECT_FLASH_TFTP */
84 (void)memcpy((void *)(load_addr + offset), src, len);
87 if (NetBootFileXferSize < newsize)
88 NetBootFileXferSize = newsize;
91 static void TftpSend (void);
92 static void TftpTimeout (void);
94 /**********************************************************************/
104 * We will always be sending some sort of packet, so
105 * cobble together the packet headers now.
107 pkt = NetTxPacket + ETHER_HDR_SIZE + IP_HDR_SIZE;
113 *((ushort *)pkt)++ = htons(TFTP_RRQ);
114 strcpy ((char *)pkt, tftp_filename);
115 pkt += strlen(tftp_filename) + 1;
116 strcpy ((char *)pkt, "octet");
117 pkt += 5 /*strlen("octet")*/ + 1;
118 strcpy ((char *)pkt, "timeout");
119 pkt += 7 /*strlen("timeout")*/ + 1;
120 sprintf((char *)pkt, "%d", TIMEOUT);
122 printf("send option \"timeout %s\"\n", (char *)pkt);
124 pkt += strlen((char *)pkt) + 1;
131 *((ushort *)pkt)++ = htons(TFTP_ACK);
132 *((ushort *)pkt)++ = htons(TftpBlock);
136 case STATE_TOO_LARGE:
138 *((ushort *)pkt)++ = htons(TFTP_ERROR);
139 *((ushort *)pkt)++ = htons(3);
140 strcpy ((char *)pkt, "File too large");
141 pkt += 14 /*strlen("File too large")*/ + 1;
145 case STATE_BAD_MAGIC:
147 *((ushort *)pkt)++ = htons(TFTP_ERROR);
148 *((ushort *)pkt)++ = htons(2);
149 strcpy ((char *)pkt, "File has bad magic");
150 pkt += 18 /*strlen("File has bad magic")*/ + 1;
155 NetSendUDPPacket(NetServerEther, NetServerIP, TftpServerPort, TftpOurPort, len);
160 TftpHandler (uchar * pkt, unsigned dest, unsigned src, unsigned len)
164 if (dest != TftpOurPort) {
167 if (TftpState != STATE_RRQ && src != TftpServerPort) {
175 /* warning: don't use increment (++) in ntohs() macros!! */
176 proto = *((ushort *)pkt)++;
177 switch (ntohs(proto)) {
188 printf("Got OACK: %s %s\n", pkt, pkt+strlen(pkt)+1);
190 TftpState = STATE_OACK;
191 TftpServerPort = src;
192 TftpSend (); /* Send ACK */
198 TftpBlock = ntohs(*(ushort *)pkt);
199 if (((TftpBlock - 1) % 10) == 0) {
201 } else if ((TftpBlock % (10 * HASHES_PER_LINE)) == 0) {
206 if (TftpState == STATE_RRQ) {
207 printf("Server did not acknowledge timeout option!\n");
211 if (TftpState == STATE_RRQ || TftpState == STATE_OACK) {
212 TftpState = STATE_DATA;
213 TftpServerPort = src;
216 if (TftpBlock != 1) { /* Assertion */
217 printf ("\nTFTP error: "
218 "First block is not block 1 (%d)\n"
219 "Starting again\n\n",
226 if (TftpBlock == TftpLastBlock) {
228 * Same block again; ignore it.
233 TftpLastBlock = TftpBlock;
234 NetSetTimeout (TIMEOUT * CFG_HZ, TftpTimeout);
236 store_block (TftpBlock - 1, pkt + 2, len);
239 * Acknoledge the block just received, which will prompt
240 * the server for the next one.
246 * We received the whole thing. Try to
250 NetState = NETLOOP_SUCCESS;
255 printf ("\nTFTP error: '%s' (%d)\n",
256 pkt + 2, ntohs(*(ushort *)pkt));
257 puts ("Starting again\n\n");
267 if (++TftpTimeoutCount > TIMEOUT_COUNT) {
268 puts ("\nRetry count exceeded; starting again\n");
272 NetSetTimeout (TIMEOUT * CFG_HZ, TftpTimeout);
281 if (BootFile[0] == '\0') {
282 IPaddr_t OurIP = ntohl(NetOurIP);
284 sprintf(default_filename, "%02lX%02lX%02lX%02lX.img",
287 (OurIP >> 16) & 0xFF,
288 (OurIP >> 24) & 0xFF );
289 tftp_filename = default_filename;
291 printf ("*** Warning: no boot file name; using '%s'\n",
294 tftp_filename = BootFile;
297 puts ("TFTP from server "); print_IPaddr (NetServerIP);
298 puts ("; our IP address is "); print_IPaddr (NetOurIP);
300 /* Check if we need to send across this subnet */
301 if (NetOurGatewayIP && NetOurSubnetMask) {
302 IPaddr_t OurNet = NetOurIP & NetOurSubnetMask;
303 IPaddr_t ServerNet = NetServerIP & NetOurSubnetMask;
305 if (OurNet != ServerNet) {
306 puts ("; sending through gateway ");
307 print_IPaddr (NetOurGatewayIP) ;
312 printf ("Filename '%s'.", tftp_filename);
314 if (NetBootFileSize) {
315 printf (" Size is 0x%x Bytes = ", NetBootFileSize<<9);
316 print_size (NetBootFileSize<<9, "");
321 printf ("Load address: 0x%lx\n", load_addr);
323 puts ("Loading: *\b");
325 NetSetTimeout (TIMEOUT * CFG_HZ, TftpTimeout);
326 NetSetHandler (TftpHandler);
328 TftpServerPort = WELL_KNOWN_PORT;
329 TftpTimeoutCount = 0;
330 TftpState = STATE_RRQ;
331 TftpOurPort = 1024 + (get_timer(0) % 3072);
334 /* zero out server ether in case the server ip has changed */
335 memset(NetServerEther, 0, 6);
340 #endif /* CFG_CMD_NET */