blob: fdf1770f0177247d7b2b25e06547266d4d0e6f2c [file] [log] [blame]
wdenkfe8c2802002-11-03 00:38:21 +00001/*
2 * Copyright 1994, 1995, 2000 Neil Russell.
3 * (See License)
4 * Copyright 2000, 2001 DENX Software Engineering, Wolfgang Denk, wd@denx.de
5 */
6
7#include <common.h>
8#include <command.h>
9#include <net.h>
10#include "tftp.h"
11#include "bootp.h"
12
13#undef ET_DEBUG
14
15#if (CONFIG_COMMANDS & CFG_CMD_NET)
16
17#define WELL_KNOWN_PORT 69 /* Well known TFTP port # */
wdenkfbe4b5c2003-10-06 21:55:32 +000018#define TIMEOUT 5 /* Seconds to timeout for a lost pkt */
wdenkfe8c2802002-11-03 00:38:21 +000019#ifndef CONFIG_NET_RETRY_COUNT
20# define TIMEOUT_COUNT 10 /* # of timeouts before giving up */
21#else
22# define TIMEOUT_COUNT (CONFIG_NET_RETRY_COUNT * 2)
23#endif
24 /* (for checking the image size) */
25#define HASHES_PER_LINE 65 /* Number of "loading" hashes per line */
26
27/*
28 * TFTP operations.
29 */
30#define TFTP_RRQ 1
31#define TFTP_WRQ 2
32#define TFTP_DATA 3
33#define TFTP_ACK 4
34#define TFTP_ERROR 5
wdenkfbe4b5c2003-10-06 21:55:32 +000035#define TFTP_OACK 6
wdenkfe8c2802002-11-03 00:38:21 +000036
37
38static int TftpServerPort; /* The UDP port at their end */
39static int TftpOurPort; /* The UDP port at our end */
40static int TftpTimeoutCount;
wdenk3f85ce22004-02-23 16:11:30 +000041static ulong TftpBlock; /* packet sequence number */
42static ulong TftpLastBlock; /* last packet sequence number received */
43static ulong TftpBlockWrap; /* count of sequence number wraparounds */
44static ulong TftpBlockWrapOffset; /* memory offset due to wrapping */
wdenkfe8c2802002-11-03 00:38:21 +000045static int TftpState;
wdenk3f85ce22004-02-23 16:11:30 +000046
wdenkfe8c2802002-11-03 00:38:21 +000047#define STATE_RRQ 1
48#define STATE_DATA 2
49#define STATE_TOO_LARGE 3
50#define STATE_BAD_MAGIC 4
wdenkfbe4b5c2003-10-06 21:55:32 +000051#define STATE_OACK 5
wdenkfe8c2802002-11-03 00:38:21 +000052
wdenk3f85ce22004-02-23 16:11:30 +000053#define TFTP_BLOCK_SIZE 512 /* default TFTP block size */
54#define TFTP_SEQUENCE_SIZE ((ulong)(1<<16)) /* sequence number is 16 bit */
55
wdenkfe8c2802002-11-03 00:38:21 +000056#define DEFAULT_NAME_LEN (8 + 4 + 1)
57static char default_filename[DEFAULT_NAME_LEN];
58static char *tftp_filename;
59
60#ifdef CFG_DIRECT_FLASH_TFTP
61extern flash_info_t flash_info[CFG_MAX_FLASH_BANKS];
62#endif
63
64static __inline__ void
65store_block (unsigned block, uchar * src, unsigned len)
66{
wdenk3f85ce22004-02-23 16:11:30 +000067 ulong offset = block * TFTP_BLOCK_SIZE + TftpBlockWrapOffset;
68 ulong newsize = offset + len;
wdenkfe8c2802002-11-03 00:38:21 +000069#ifdef CFG_DIRECT_FLASH_TFTP
70 int i, rc = 0;
71
72 for (i=0; i<CFG_MAX_FLASH_BANKS; i++) {
73 /* start address in flash? */
74 if (load_addr + offset >= flash_info[i].start[0]) {
75 rc = 1;
76 break;
77 }
78 }
79
80 if (rc) { /* Flash is destination for this packet */
81 rc = flash_write ((uchar *)src, (ulong)(load_addr+offset), len);
82 if (rc) {
83 flash_perror (rc);
84 NetState = NETLOOP_FAIL;
85 return;
86 }
87 }
88 else
89#endif /* CFG_DIRECT_FLASH_TFTP */
90 {
91 (void)memcpy((void *)(load_addr + offset), src, len);
92 }
93
94 if (NetBootFileXferSize < newsize)
95 NetBootFileXferSize = newsize;
96}
97
98static void TftpSend (void);
99static void TftpTimeout (void);
100
101/**********************************************************************/
102
103static void
104TftpSend (void)
105{
106 volatile uchar * pkt;
107 volatile uchar * xp;
108 int len = 0;
109
110 /*
111 * We will always be sending some sort of packet, so
112 * cobble together the packet headers now.
113 */
114 pkt = NetTxPacket + ETHER_HDR_SIZE + IP_HDR_SIZE;
115
116 switch (TftpState) {
117
118 case STATE_RRQ:
119 xp = pkt;
120 *((ushort *)pkt)++ = htons(TFTP_RRQ);
121 strcpy ((char *)pkt, tftp_filename);
122 pkt += strlen(tftp_filename) + 1;
123 strcpy ((char *)pkt, "octet");
124 pkt += 5 /*strlen("octet")*/ + 1;
wdenkfbe4b5c2003-10-06 21:55:32 +0000125 strcpy ((char *)pkt, "timeout");
126 pkt += 7 /*strlen("timeout")*/ + 1;
127 sprintf((char *)pkt, "%d", TIMEOUT);
128#ifdef ET_DEBUG
129 printf("send option \"timeout %s\"\n", (char *)pkt);
130#endif
131 pkt += strlen((char *)pkt) + 1;
wdenkfe8c2802002-11-03 00:38:21 +0000132 len = pkt - xp;
133 break;
134
135 case STATE_DATA:
wdenkfbe4b5c2003-10-06 21:55:32 +0000136 case STATE_OACK:
wdenkfe8c2802002-11-03 00:38:21 +0000137 xp = pkt;
138 *((ushort *)pkt)++ = htons(TFTP_ACK);
139 *((ushort *)pkt)++ = htons(TftpBlock);
140 len = pkt - xp;
141 break;
142
143 case STATE_TOO_LARGE:
144 xp = pkt;
145 *((ushort *)pkt)++ = htons(TFTP_ERROR);
146 *((ushort *)pkt)++ = htons(3);
147 strcpy ((char *)pkt, "File too large");
148 pkt += 14 /*strlen("File too large")*/ + 1;
149 len = pkt - xp;
150 break;
151
152 case STATE_BAD_MAGIC:
153 xp = pkt;
154 *((ushort *)pkt)++ = htons(TFTP_ERROR);
155 *((ushort *)pkt)++ = htons(2);
156 strcpy ((char *)pkt, "File has bad magic");
157 pkt += 18 /*strlen("File has bad magic")*/ + 1;
158 len = pkt - xp;
159 break;
160 }
161
wdenk73a8b272003-06-05 19:27:42 +0000162 NetSendUDPPacket(NetServerEther, NetServerIP, TftpServerPort, TftpOurPort, len);
wdenkfe8c2802002-11-03 00:38:21 +0000163}
164
165
166static void
167TftpHandler (uchar * pkt, unsigned dest, unsigned src, unsigned len)
168{
169 ushort proto;
170
171 if (dest != TftpOurPort) {
172 return;
173 }
174 if (TftpState != STATE_RRQ && src != TftpServerPort) {
175 return;
176 }
177
178 if (len < 2) {
179 return;
180 }
181 len -= 2;
182 /* warning: don't use increment (++) in ntohs() macros!! */
183 proto = *((ushort *)pkt)++;
184 switch (ntohs(proto)) {
185
186 case TFTP_RRQ:
187 case TFTP_WRQ:
188 case TFTP_ACK:
189 break;
190 default:
191 break;
192
wdenkfbe4b5c2003-10-06 21:55:32 +0000193 case TFTP_OACK:
194#ifdef ET_DEBUG
195 printf("Got OACK: %s %s\n", pkt, pkt+strlen(pkt)+1);
196#endif
197 TftpState = STATE_OACK;
198 TftpServerPort = src;
199 TftpSend (); /* Send ACK */
200 break;
wdenkfe8c2802002-11-03 00:38:21 +0000201 case TFTP_DATA:
202 if (len < 2)
203 return;
204 len -= 2;
205 TftpBlock = ntohs(*(ushort *)pkt);
wdenk3f85ce22004-02-23 16:11:30 +0000206
207 /*
wdenkcd0a9de2004-02-23 20:48:38 +0000208 * RFC1350 specifies that the first data packet will
209 * have sequence number 1. If we receive a sequence
210 * number of 0 this means that there was a wrap
211 * around of the (16 bit) counter.
wdenk3f85ce22004-02-23 16:11:30 +0000212 */
213 if (TftpBlock == 0) {
214 TftpBlockWrap++;
215 TftpBlockWrapOffset += TFTP_BLOCK_SIZE * TFTP_SEQUENCE_SIZE;
216 printf ("\n\t %lu MB reveived\n\t ", TftpBlockWrapOffset>>20);
217 } else {
218 if (((TftpBlock - 1) % 10) == 0) {
219 putc ('#');
220 } else if ((TftpBlock % (10 * HASHES_PER_LINE)) == 0) {
221 puts ("\n\t ");
222 }
wdenkfe8c2802002-11-03 00:38:21 +0000223 }
224
wdenkfbe4b5c2003-10-06 21:55:32 +0000225#ifdef ET_DEBUG
wdenkfe8c2802002-11-03 00:38:21 +0000226 if (TftpState == STATE_RRQ) {
wdenkfbe4b5c2003-10-06 21:55:32 +0000227 printf("Server did not acknowledge timeout option!\n");
228 }
229#endif
230
231 if (TftpState == STATE_RRQ || TftpState == STATE_OACK) {
wdenk3f85ce22004-02-23 16:11:30 +0000232 /* first block received */
wdenkfe8c2802002-11-03 00:38:21 +0000233 TftpState = STATE_DATA;
234 TftpServerPort = src;
235 TftpLastBlock = 0;
wdenk3f85ce22004-02-23 16:11:30 +0000236 TftpBlockWrap = 0;
237 TftpBlockWrapOffset = 0;
wdenkfe8c2802002-11-03 00:38:21 +0000238
239 if (TftpBlock != 1) { /* Assertion */
240 printf ("\nTFTP error: "
wdenk3f85ce22004-02-23 16:11:30 +0000241 "First block is not block 1 (%ld)\n"
wdenkfe8c2802002-11-03 00:38:21 +0000242 "Starting again\n\n",
243 TftpBlock);
244 NetStartAgain ();
245 break;
246 }
247 }
248
249 if (TftpBlock == TftpLastBlock) {
250 /*
251 * Same block again; ignore it.
252 */
253 break;
254 }
255
256 TftpLastBlock = TftpBlock;
257 NetSetTimeout (TIMEOUT * CFG_HZ, TftpTimeout);
258
259 store_block (TftpBlock - 1, pkt + 2, len);
260
261 /*
262 * Acknoledge the block just received, which will prompt
263 * the server for the next one.
264 */
265 TftpSend ();
266
wdenk3f85ce22004-02-23 16:11:30 +0000267 if (len < TFTP_BLOCK_SIZE) {
wdenkfe8c2802002-11-03 00:38:21 +0000268 /*
269 * We received the whole thing. Try to
270 * run it.
271 */
272 puts ("\ndone\n");
273 NetState = NETLOOP_SUCCESS;
274 }
275 break;
276
277 case TFTP_ERROR:
278 printf ("\nTFTP error: '%s' (%d)\n",
279 pkt + 2, ntohs(*(ushort *)pkt));
280 puts ("Starting again\n\n");
281 NetStartAgain ();
282 break;
283 }
284}
285
286
287static void
288TftpTimeout (void)
289{
wdenke0ac62d2003-08-17 18:55:18 +0000290 if (++TftpTimeoutCount > TIMEOUT_COUNT) {
wdenkfe8c2802002-11-03 00:38:21 +0000291 puts ("\nRetry count exceeded; starting again\n");
292 NetStartAgain ();
293 } else {
294 puts ("T ");
295 NetSetTimeout (TIMEOUT * CFG_HZ, TftpTimeout);
296 TftpSend ();
297 }
298}
299
300
301void
302TftpStart (void)
303{
wdenkfe8c2802002-11-03 00:38:21 +0000304 if (BootFile[0] == '\0') {
305 IPaddr_t OurIP = ntohl(NetOurIP);
306
307 sprintf(default_filename, "%02lX%02lX%02lX%02lX.img",
308 OurIP & 0xFF,
309 (OurIP >> 8) & 0xFF,
310 (OurIP >> 16) & 0xFF,
311 (OurIP >> 24) & 0xFF );
312 tftp_filename = default_filename;
313
314 printf ("*** Warning: no boot file name; using '%s'\n",
315 tftp_filename);
316 } else {
317 tftp_filename = BootFile;
318 }
319
wdenk5bb226e2003-11-17 21:14:37 +0000320#if defined(CONFIG_NET_MULTI)
321 printf ("Using %s device\n", eth_get_name());
322#endif
wdenkfe8c2802002-11-03 00:38:21 +0000323 puts ("TFTP from server "); print_IPaddr (NetServerIP);
324 puts ("; our IP address is "); print_IPaddr (NetOurIP);
325
326 /* Check if we need to send across this subnet */
327 if (NetOurGatewayIP && NetOurSubnetMask) {
328 IPaddr_t OurNet = NetOurIP & NetOurSubnetMask;
329 IPaddr_t ServerNet = NetServerIP & NetOurSubnetMask;
330
331 if (OurNet != ServerNet) {
332 puts ("; sending through gateway ");
333 print_IPaddr (NetOurGatewayIP) ;
334 }
335 }
336 putc ('\n');
337
338 printf ("Filename '%s'.", tftp_filename);
339
340 if (NetBootFileSize) {
341 printf (" Size is 0x%x Bytes = ", NetBootFileSize<<9);
342 print_size (NetBootFileSize<<9, "");
343 }
344
345 putc ('\n');
346
347 printf ("Load address: 0x%lx\n", load_addr);
348
349 puts ("Loading: *\b");
350
351 NetSetTimeout (TIMEOUT * CFG_HZ, TftpTimeout);
352 NetSetHandler (TftpHandler);
353
354 TftpServerPort = WELL_KNOWN_PORT;
355 TftpTimeoutCount = 0;
356 TftpState = STATE_RRQ;
357 TftpOurPort = 1024 + (get_timer(0) % 3072);
wdenkfbe4b5c2003-10-06 21:55:32 +0000358 TftpBlock = 0;
wdenkfe8c2802002-11-03 00:38:21 +0000359
wdenk73a8b272003-06-05 19:27:42 +0000360 /* zero out server ether in case the server ip has changed */
361 memset(NetServerEther, 0, 6);
362
wdenkfe8c2802002-11-03 00:38:21 +0000363 TftpSend ();
364}
365
366#endif /* CFG_CMD_NET */