blob: 00abec38049484b32063496bf8e9853e52b65b82 [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
wdenkfe8c2802002-11-03 00:38:21 +000013#define WELL_KNOWN_PORT 69 /* Well known TFTP port # */
Bartlomiej Sieka49f3bdb2008-10-01 15:26:28 +020014#define TIMEOUT 5000UL /* Millisecs to timeout for lost pkt */
wdenkfe8c2802002-11-03 00:38:21 +000015#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/*
24 * TFTP operations.
25 */
26#define TFTP_RRQ 1
27#define TFTP_WRQ 2
28#define TFTP_DATA 3
29#define TFTP_ACK 4
30#define TFTP_ERROR 5
wdenkfbe4b5c2003-10-06 21:55:32 +000031#define TFTP_OACK 6
wdenkfe8c2802002-11-03 00:38:21 +000032
Bartlomiej Siekae83cc062008-10-01 15:26:29 +020033static ulong TftpTimeoutMSecs = TIMEOUT;
34static int TftpTimeoutCountMax = TIMEOUT_COUNT;
35
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 */
45ulong TftpRRQTimeoutMSecs = TIMEOUT;
46int TftpRRQTimeoutCountMax = TIMEOUT_COUNT;
47
Remy Bohmeraafda382009-10-28 22:13:40 +010048enum {
49 TFTP_ERR_UNDEFINED = 0,
50 TFTP_ERR_FILE_NOT_FOUND = 1,
51 TFTP_ERR_ACCESS_DENIED = 2,
52 TFTP_ERR_DISK_FULL = 3,
53 TFTP_ERR_UNEXPECTED_OPCODE = 4,
54 TFTP_ERR_UNKNOWN_TRANSFER_ID = 5,
55 TFTP_ERR_FILE_ALREADY_EXISTS = 6,
56};
57
Jean-Christophe PLAGNIOL-VILLARDa93907c2008-01-18 01:14:03 +010058static IPaddr_t TftpServerIP;
wdenkfe8c2802002-11-03 00:38:21 +000059static int TftpServerPort; /* The UDP port at their end */
60static int TftpOurPort; /* The UDP port at our end */
61static int TftpTimeoutCount;
wdenk3f85ce22004-02-23 16:11:30 +000062static ulong TftpBlock; /* packet sequence number */
63static ulong TftpLastBlock; /* last packet sequence number received */
64static ulong TftpBlockWrap; /* count of sequence number wraparounds */
65static ulong TftpBlockWrapOffset; /* memory offset due to wrapping */
wdenkfe8c2802002-11-03 00:38:21 +000066static int TftpState;
Robin Getz4fccb812009-08-20 10:50:20 -040067#ifdef CONFIG_TFTP_TSIZE
68static int TftpTsize; /* The file size reported by the server */
69static short TftpNumchars; /* The number of hashes we printed */
70#endif
wdenk3f85ce22004-02-23 16:11:30 +000071
wdenkfe8c2802002-11-03 00:38:21 +000072#define STATE_RRQ 1
73#define STATE_DATA 2
74#define STATE_TOO_LARGE 3
75#define STATE_BAD_MAGIC 4
wdenkfbe4b5c2003-10-06 21:55:32 +000076#define STATE_OACK 5
wdenkfe8c2802002-11-03 00:38:21 +000077
wdenk3f85ce22004-02-23 16:11:30 +000078#define TFTP_BLOCK_SIZE 512 /* default TFTP block size */
79#define TFTP_SEQUENCE_SIZE ((ulong)(1<<16)) /* sequence number is 16 bit */
80
wdenkfe8c2802002-11-03 00:38:21 +000081#define DEFAULT_NAME_LEN (8 + 4 + 1)
82static char default_filename[DEFAULT_NAME_LEN];
Jean-Christophe PLAGNIOL-VILLARDa93907c2008-01-18 01:14:03 +010083
84#ifndef CONFIG_TFTP_FILE_NAME_MAX_LEN
85#define MAX_LEN 128
86#else
87#define MAX_LEN CONFIG_TFTP_FILE_NAME_MAX_LEN
88#endif
89
90static char tftp_filename[MAX_LEN];
wdenkfe8c2802002-11-03 00:38:21 +000091
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020092#ifdef CONFIG_SYS_DIRECT_FLASH_TFTP
Marian Balakowicze6f2e902005-10-11 19:09:42 +020093extern flash_info_t flash_info[];
wdenkfe8c2802002-11-03 00:38:21 +000094#endif
95
Wolfgang Denk85eb5ca2007-08-14 09:47:27 +020096/* 512 is poor choice for ethernet, MTU is typically 1500.
97 * Minus eth.hdrs thats 1468. Can get 2x better throughput with
David Updegraff53a5c422007-06-11 10:41:07 -050098 * almost-MTU block sizes. At least try... fall back to 512 if need be.
Alessandro Rubini89ba81d2009-08-07 13:59:06 +020099 * (but those using CONFIG_IP_DEFRAG may want to set a larger block in cfg file)
David Updegraff53a5c422007-06-11 10:41:07 -0500100 */
Alessandro Rubini89ba81d2009-08-07 13:59:06 +0200101#ifdef CONFIG_TFTP_BLOCKSIZE
102#define TFTP_MTU_BLOCKSIZE CONFIG_TFTP_BLOCKSIZE
103#else
David Updegraff53a5c422007-06-11 10:41:07 -0500104#define TFTP_MTU_BLOCKSIZE 1468
Alessandro Rubini89ba81d2009-08-07 13:59:06 +0200105#endif
106
David Updegraff53a5c422007-06-11 10:41:07 -0500107static unsigned short TftpBlkSize=TFTP_BLOCK_SIZE;
108static unsigned short TftpBlkSizeOption=TFTP_MTU_BLOCKSIZE;
109
110#ifdef CONFIG_MCAST_TFTP
111#include <malloc.h>
112#define MTFTP_BITMAPSIZE 0x1000
113static unsigned *Bitmap;
114static int PrevBitmapHole,Mapsize=MTFTP_BITMAPSIZE;
115static uchar ProhibitMcast=0, MasterClient=0;
116static uchar Multicast=0;
117extern IPaddr_t Mcast_addr;
118static int Mcast_port;
119static ulong TftpEndingBlock; /* can get 'last' block before done..*/
120
121static void parse_multicast_oack(char *pkt,int len);
122
123static void
124mcast_cleanup(void)
125{
126 if (Mcast_addr) eth_mcast_join(Mcast_addr, 0);
127 if (Bitmap) free(Bitmap);
128 Bitmap=NULL;
129 Mcast_addr = Multicast = Mcast_port = 0;
130 TftpEndingBlock = -1;
131}
132
133#endif /* CONFIG_MCAST_TFTP */
134
wdenkfe8c2802002-11-03 00:38:21 +0000135static __inline__ void
136store_block (unsigned block, uchar * src, unsigned len)
137{
David Updegraff53a5c422007-06-11 10:41:07 -0500138 ulong offset = block * TftpBlkSize + TftpBlockWrapOffset;
wdenk3f85ce22004-02-23 16:11:30 +0000139 ulong newsize = offset + len;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200140#ifdef CONFIG_SYS_DIRECT_FLASH_TFTP
wdenkfe8c2802002-11-03 00:38:21 +0000141 int i, rc = 0;
142
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200143 for (i=0; i<CONFIG_SYS_MAX_FLASH_BANKS; i++) {
wdenkfe8c2802002-11-03 00:38:21 +0000144 /* start address in flash? */
Jochen Friedrichbe1b0d22008-09-02 11:24:59 +0200145 if (flash_info[i].flash_id == FLASH_UNKNOWN)
146 continue;
wdenkfe8c2802002-11-03 00:38:21 +0000147 if (load_addr + offset >= flash_info[i].start[0]) {
148 rc = 1;
149 break;
150 }
151 }
152
153 if (rc) { /* Flash is destination for this packet */
Wolfgang Denk77ddac92005-10-13 16:45:02 +0200154 rc = flash_write ((char *)src, (ulong)(load_addr+offset), len);
wdenkfe8c2802002-11-03 00:38:21 +0000155 if (rc) {
156 flash_perror (rc);
157 NetState = NETLOOP_FAIL;
158 return;
159 }
160 }
161 else
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200162#endif /* CONFIG_SYS_DIRECT_FLASH_TFTP */
wdenkfe8c2802002-11-03 00:38:21 +0000163 {
164 (void)memcpy((void *)(load_addr + offset), src, len);
165 }
David Updegraff53a5c422007-06-11 10:41:07 -0500166#ifdef CONFIG_MCAST_TFTP
167 if (Multicast)
168 ext2_set_bit(block, Bitmap);
169#endif
wdenkfe8c2802002-11-03 00:38:21 +0000170
171 if (NetBootFileXferSize < newsize)
172 NetBootFileXferSize = newsize;
173}
174
175static void TftpSend (void);
176static void TftpTimeout (void);
177
178/**********************************************************************/
179
180static void
181TftpSend (void)
182{
183 volatile uchar * pkt;
184 volatile uchar * xp;
185 int len = 0;
Wolfgang Denk7bc5ee02005-08-26 01:36:03 +0200186 volatile ushort *s;
wdenkfe8c2802002-11-03 00:38:21 +0000187
Wolfgang Denk85eb5ca2007-08-14 09:47:27 +0200188#ifdef CONFIG_MCAST_TFTP
David Updegraff53a5c422007-06-11 10:41:07 -0500189 /* Multicast TFTP.. non-MasterClients do not ACK data. */
Wolfgang Denk85eb5ca2007-08-14 09:47:27 +0200190 if (Multicast
191 && (TftpState == STATE_DATA)
192 && (MasterClient == 0))
David Updegraff53a5c422007-06-11 10:41:07 -0500193 return;
194#endif
wdenkfe8c2802002-11-03 00:38:21 +0000195 /*
196 * We will always be sending some sort of packet, so
197 * cobble together the packet headers now.
198 */
wdenka3d991b2004-04-15 21:48:45 +0000199 pkt = NetTxPacket + NetEthHdrSize() + IP_HDR_SIZE;
wdenkfe8c2802002-11-03 00:38:21 +0000200
201 switch (TftpState) {
202
203 case STATE_RRQ:
204 xp = pkt;
Wolfgang Denk7bc5ee02005-08-26 01:36:03 +0200205 s = (ushort *)pkt;
206 *s++ = htons(TFTP_RRQ);
207 pkt = (uchar *)s;
wdenkfe8c2802002-11-03 00:38:21 +0000208 strcpy ((char *)pkt, tftp_filename);
209 pkt += strlen(tftp_filename) + 1;
210 strcpy ((char *)pkt, "octet");
211 pkt += 5 /*strlen("octet")*/ + 1;
wdenkfbe4b5c2003-10-06 21:55:32 +0000212 strcpy ((char *)pkt, "timeout");
213 pkt += 7 /*strlen("timeout")*/ + 1;
Wolfgang Denkc96f86e2010-01-17 23:55:53 +0100214 sprintf((char *)pkt, "%lu", TftpTimeoutMSecs / 1000);
Robin Getz0ebf04c2009-07-23 03:01:03 -0400215 debug("send option \"timeout %s\"\n", (char *)pkt);
wdenkfbe4b5c2003-10-06 21:55:32 +0000216 pkt += strlen((char *)pkt) + 1;
Robin Getz4fccb812009-08-20 10:50:20 -0400217#ifdef CONFIG_TFTP_TSIZE
218 memcpy((char *)pkt, "tsize\0000\0", 8);
219 pkt += 8;
220#endif
David Updegraff53a5c422007-06-11 10:41:07 -0500221 /* try for more effic. blk size */
222 pkt += sprintf((char *)pkt,"blksize%c%d%c",
stefano babicef8f2072007-08-21 15:52:33 +0200223 0,TftpBlkSizeOption,0);
Wolfgang Denk85eb5ca2007-08-14 09:47:27 +0200224#ifdef CONFIG_MCAST_TFTP
David Updegraff53a5c422007-06-11 10:41:07 -0500225 /* Check all preconditions before even trying the option */
Wolfgang Denk85eb5ca2007-08-14 09:47:27 +0200226 if (!ProhibitMcast
227 && (Bitmap=malloc(Mapsize))
David Updegraff53a5c422007-06-11 10:41:07 -0500228 && eth_get_dev()->mcast) {
229 free(Bitmap);
230 Bitmap=NULL;
231 pkt += sprintf((char *)pkt,"multicast%c%c",0,0);
232 }
233#endif /* CONFIG_MCAST_TFTP */
wdenkfe8c2802002-11-03 00:38:21 +0000234 len = pkt - xp;
235 break;
236
wdenkfbe4b5c2003-10-06 21:55:32 +0000237 case STATE_OACK:
David Updegraff53a5c422007-06-11 10:41:07 -0500238#ifdef CONFIG_MCAST_TFTP
239 /* My turn! Start at where I need blocks I missed.*/
240 if (Multicast)
241 TftpBlock=ext2_find_next_zero_bit(Bitmap,(Mapsize*8),0);
242 /*..falling..*/
243#endif
244 case STATE_DATA:
wdenkfe8c2802002-11-03 00:38:21 +0000245 xp = pkt;
Wolfgang Denk7bc5ee02005-08-26 01:36:03 +0200246 s = (ushort *)pkt;
247 *s++ = htons(TFTP_ACK);
248 *s++ = htons(TftpBlock);
249 pkt = (uchar *)s;
wdenkfe8c2802002-11-03 00:38:21 +0000250 len = pkt - xp;
251 break;
252
253 case STATE_TOO_LARGE:
254 xp = pkt;
Wolfgang Denk7bc5ee02005-08-26 01:36:03 +0200255 s = (ushort *)pkt;
256 *s++ = htons(TFTP_ERROR);
257 *s++ = htons(3);
258 pkt = (uchar *)s;
wdenkfe8c2802002-11-03 00:38:21 +0000259 strcpy ((char *)pkt, "File too large");
260 pkt += 14 /*strlen("File too large")*/ + 1;
261 len = pkt - xp;
262 break;
263
264 case STATE_BAD_MAGIC:
265 xp = pkt;
Wolfgang Denk7bc5ee02005-08-26 01:36:03 +0200266 s = (ushort *)pkt;
267 *s++ = htons(TFTP_ERROR);
268 *s++ = htons(2);
269 pkt = (uchar *)s;
wdenkfe8c2802002-11-03 00:38:21 +0000270 strcpy ((char *)pkt, "File has bad magic");
271 pkt += 18 /*strlen("File has bad magic")*/ + 1;
272 len = pkt - xp;
273 break;
274 }
275
Jean-Christophe PLAGNIOL-VILLARDa93907c2008-01-18 01:14:03 +0100276 NetSendUDPPacket(NetServerEther, TftpServerIP, TftpServerPort, TftpOurPort, len);
wdenkfe8c2802002-11-03 00:38:21 +0000277}
278
279
280static void
Luca Ceresoli03eb1292011-04-18 06:19:50 +0000281TftpHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src,
282 unsigned len)
wdenkfe8c2802002-11-03 00:38:21 +0000283{
284 ushort proto;
Wolfgang Denk7bc5ee02005-08-26 01:36:03 +0200285 ushort *s;
Wolfgang Denkff13ac82007-08-30 14:42:15 +0200286 int i;
wdenkfe8c2802002-11-03 00:38:21 +0000287
288 if (dest != TftpOurPort) {
David Updegraff53a5c422007-06-11 10:41:07 -0500289#ifdef CONFIG_MCAST_TFTP
Wolfgang Denk85eb5ca2007-08-14 09:47:27 +0200290 if (Multicast
David Updegraff53a5c422007-06-11 10:41:07 -0500291 && (!Mcast_port || (dest != Mcast_port)))
292#endif
wdenkfe8c2802002-11-03 00:38:21 +0000293 return;
294 }
295 if (TftpState != STATE_RRQ && src != TftpServerPort) {
296 return;
297 }
298
299 if (len < 2) {
300 return;
301 }
302 len -= 2;
303 /* warning: don't use increment (++) in ntohs() macros!! */
Wolfgang Denk7bc5ee02005-08-26 01:36:03 +0200304 s = (ushort *)pkt;
305 proto = *s++;
306 pkt = (uchar *)s;
wdenkfe8c2802002-11-03 00:38:21 +0000307 switch (ntohs(proto)) {
308
309 case TFTP_RRQ:
310 case TFTP_WRQ:
311 case TFTP_ACK:
312 break;
313 default:
314 break;
315
wdenkfbe4b5c2003-10-06 21:55:32 +0000316 case TFTP_OACK:
Wolfgang Denkd3717082009-08-10 09:59:10 +0200317 debug("Got OACK: %s %s\n",
318 pkt,
319 pkt + strlen((char *)pkt) + 1);
wdenkfbe4b5c2003-10-06 21:55:32 +0000320 TftpState = STATE_OACK;
321 TftpServerPort = src;
Wolfgang Denk60174742007-08-31 10:01:51 +0200322 /*
323 * Check for 'blksize' option.
324 * Careful: "i" is signed, "len" is unsigned, thus
325 * something like "len-8" may give a *huge* number
326 */
327 for (i=0; i+8<len; i++) {
Wolfgang Denkff13ac82007-08-30 14:42:15 +0200328 if (strcmp ((char*)pkt+i,"blksize") == 0) {
329 TftpBlkSize = (unsigned short)
330 simple_strtoul((char*)pkt+i+8,NULL,10);
Robin Getz0ebf04c2009-07-23 03:01:03 -0400331 debug("Blocksize ack: %s, %d\n",
Wolfgang Denkff13ac82007-08-30 14:42:15 +0200332 (char*)pkt+i+8,TftpBlkSize);
Wolfgang Denkff13ac82007-08-30 14:42:15 +0200333 }
Robin Getz4fccb812009-08-20 10:50:20 -0400334#ifdef CONFIG_TFTP_TSIZE
335 if (strcmp ((char*)pkt+i,"tsize") == 0) {
336 TftpTsize = simple_strtoul((char*)pkt+i+6,NULL,10);
337 debug("size = %s, %d\n",
338 (char*)pkt+i+6, TftpTsize);
339 }
340#endif
David Updegraff53a5c422007-06-11 10:41:07 -0500341 }
342#ifdef CONFIG_MCAST_TFTP
343 parse_multicast_oack((char *)pkt,len-1);
Wolfgang Denk85eb5ca2007-08-14 09:47:27 +0200344 if ((Multicast) && (!MasterClient))
David Updegraff53a5c422007-06-11 10:41:07 -0500345 TftpState = STATE_DATA; /* passive.. */
346 else
347#endif
wdenkfbe4b5c2003-10-06 21:55:32 +0000348 TftpSend (); /* Send ACK */
349 break;
wdenkfe8c2802002-11-03 00:38:21 +0000350 case TFTP_DATA:
351 if (len < 2)
352 return;
353 len -= 2;
354 TftpBlock = ntohs(*(ushort *)pkt);
wdenk3f85ce22004-02-23 16:11:30 +0000355
356 /*
wdenkcd0a9de2004-02-23 20:48:38 +0000357 * RFC1350 specifies that the first data packet will
358 * have sequence number 1. If we receive a sequence
359 * number of 0 this means that there was a wrap
360 * around of the (16 bit) counter.
wdenk3f85ce22004-02-23 16:11:30 +0000361 */
362 if (TftpBlock == 0) {
363 TftpBlockWrap++;
David Updegraff53a5c422007-06-11 10:41:07 -0500364 TftpBlockWrapOffset += TftpBlkSize * TFTP_SEQUENCE_SIZE;
Wolfgang Denkddd5d9d2006-08-14 22:43:13 +0200365 printf ("\n\t %lu MB received\n\t ", TftpBlockWrapOffset>>20);
Robin Getz4fccb812009-08-20 10:50:20 -0400366 }
367#ifdef CONFIG_TFTP_TSIZE
368 else if (TftpTsize) {
369 while (TftpNumchars < NetBootFileXferSize * 50 / TftpTsize) {
370 putc('#');
371 TftpNumchars++;
372 }
373 }
374#endif
375 else {
wdenk3f85ce22004-02-23 16:11:30 +0000376 if (((TftpBlock - 1) % 10) == 0) {
377 putc ('#');
378 } else if ((TftpBlock % (10 * HASHES_PER_LINE)) == 0) {
379 puts ("\n\t ");
380 }
wdenkfe8c2802002-11-03 00:38:21 +0000381 }
382
Robin Getz0ebf04c2009-07-23 03:01:03 -0400383 if (TftpState == STATE_RRQ)
384 debug("Server did not acknowledge timeout option!\n");
wdenkfbe4b5c2003-10-06 21:55:32 +0000385
386 if (TftpState == STATE_RRQ || TftpState == STATE_OACK) {
wdenk3f85ce22004-02-23 16:11:30 +0000387 /* first block received */
wdenkfe8c2802002-11-03 00:38:21 +0000388 TftpState = STATE_DATA;
389 TftpServerPort = src;
390 TftpLastBlock = 0;
wdenk3f85ce22004-02-23 16:11:30 +0000391 TftpBlockWrap = 0;
392 TftpBlockWrapOffset = 0;
wdenkfe8c2802002-11-03 00:38:21 +0000393
David Updegraff53a5c422007-06-11 10:41:07 -0500394#ifdef CONFIG_MCAST_TFTP
395 if (Multicast) { /* start!=1 common if mcast */
396 TftpLastBlock = TftpBlock - 1;
397 } else
398#endif
wdenkfe8c2802002-11-03 00:38:21 +0000399 if (TftpBlock != 1) { /* Assertion */
400 printf ("\nTFTP error: "
wdenk3f85ce22004-02-23 16:11:30 +0000401 "First block is not block 1 (%ld)\n"
wdenkfe8c2802002-11-03 00:38:21 +0000402 "Starting again\n\n",
403 TftpBlock);
404 NetStartAgain ();
405 break;
406 }
407 }
408
409 if (TftpBlock == TftpLastBlock) {
410 /*
411 * Same block again; ignore it.
412 */
413 break;
414 }
415
416 TftpLastBlock = TftpBlock;
Bartlomiej Siekae83cc062008-10-01 15:26:29 +0200417 TftpTimeoutCountMax = TIMEOUT_COUNT;
418 NetSetTimeout (TftpTimeoutMSecs, TftpTimeout);
wdenkfe8c2802002-11-03 00:38:21 +0000419
420 store_block (TftpBlock - 1, pkt + 2, len);
421
422 /*
423 * Acknoledge the block just received, which will prompt
424 * the server for the next one.
425 */
David Updegraff53a5c422007-06-11 10:41:07 -0500426#ifdef CONFIG_MCAST_TFTP
Wolfgang Denk85eb5ca2007-08-14 09:47:27 +0200427 /* if I am the MasterClient, actively calculate what my next
428 * needed block is; else I'm passive; not ACKING
Jean-Christophe PLAGNIOL-VILLARDa93907c2008-01-18 01:14:03 +0100429 */
David Updegraff53a5c422007-06-11 10:41:07 -0500430 if (Multicast) {
431 if (len < TftpBlkSize) {
432 TftpEndingBlock = TftpBlock;
433 } else if (MasterClient) {
Wolfgang Denk85eb5ca2007-08-14 09:47:27 +0200434 TftpBlock = PrevBitmapHole =
David Updegraff53a5c422007-06-11 10:41:07 -0500435 ext2_find_next_zero_bit(
436 Bitmap,
437 (Mapsize*8),
438 PrevBitmapHole);
439 if (TftpBlock > ((Mapsize*8) - 1)) {
440 printf ("tftpfile too big\n");
441 /* try to double it and retry */
442 Mapsize<<=1;
443 mcast_cleanup();
444 NetStartAgain ();
445 return;
446 }
447 TftpLastBlock = TftpBlock;
448 }
449 }
450#endif
wdenkfe8c2802002-11-03 00:38:21 +0000451 TftpSend ();
452
David Updegraff53a5c422007-06-11 10:41:07 -0500453#ifdef CONFIG_MCAST_TFTP
454 if (Multicast) {
455 if (MasterClient && (TftpBlock >= TftpEndingBlock)) {
456 puts ("\nMulticast tftp done\n");
457 mcast_cleanup();
458 NetState = NETLOOP_SUCCESS;
Wolfgang Denk85eb5ca2007-08-14 09:47:27 +0200459 }
David Updegraff53a5c422007-06-11 10:41:07 -0500460 }
461 else
462#endif
463 if (len < TftpBlkSize) {
wdenkfe8c2802002-11-03 00:38:21 +0000464 /*
465 * We received the whole thing. Try to
466 * run it.
467 */
Robin Getz4fccb812009-08-20 10:50:20 -0400468#ifdef CONFIG_TFTP_TSIZE
469 /* Print out the hash marks for the last packet received */
470 while (TftpTsize && TftpNumchars < 49) {
471 putc('#');
472 TftpNumchars++;
473 }
474#endif
wdenkfe8c2802002-11-03 00:38:21 +0000475 puts ("\ndone\n");
476 NetState = NETLOOP_SUCCESS;
477 }
478 break;
479
480 case TFTP_ERROR:
481 printf ("\nTFTP error: '%s' (%d)\n",
482 pkt + 2, ntohs(*(ushort *)pkt));
Remy Bohmeraafda382009-10-28 22:13:40 +0100483
484 switch (ntohs(*(ushort *)pkt)) {
485 case TFTP_ERR_FILE_NOT_FOUND:
486 case TFTP_ERR_ACCESS_DENIED:
487 puts("Not retrying...\n");
488 eth_halt();
489 NetState = NETLOOP_FAIL;
490 break;
491 case TFTP_ERR_UNDEFINED:
492 case TFTP_ERR_DISK_FULL:
493 case TFTP_ERR_UNEXPECTED_OPCODE:
494 case TFTP_ERR_UNKNOWN_TRANSFER_ID:
495 case TFTP_ERR_FILE_ALREADY_EXISTS:
496 default:
497 puts("Starting again\n\n");
David Updegraff53a5c422007-06-11 10:41:07 -0500498#ifdef CONFIG_MCAST_TFTP
Remy Bohmeraafda382009-10-28 22:13:40 +0100499 mcast_cleanup();
David Updegraff53a5c422007-06-11 10:41:07 -0500500#endif
Remy Bohmeraafda382009-10-28 22:13:40 +0100501 NetStartAgain();
502 break;
503 }
wdenkfe8c2802002-11-03 00:38:21 +0000504 break;
505 }
506}
507
508
509static void
510TftpTimeout (void)
511{
Bartlomiej Siekae83cc062008-10-01 15:26:29 +0200512 if (++TftpTimeoutCount > TftpTimeoutCountMax) {
wdenkfe8c2802002-11-03 00:38:21 +0000513 puts ("\nRetry count exceeded; starting again\n");
David Updegraff53a5c422007-06-11 10:41:07 -0500514#ifdef CONFIG_MCAST_TFTP
515 mcast_cleanup();
516#endif
wdenkfe8c2802002-11-03 00:38:21 +0000517 NetStartAgain ();
518 } else {
519 puts ("T ");
Bartlomiej Siekae83cc062008-10-01 15:26:29 +0200520 NetSetTimeout (TftpTimeoutMSecs, TftpTimeout);
wdenkfe8c2802002-11-03 00:38:21 +0000521 TftpSend ();
522 }
523}
524
525
526void
527TftpStart (void)
528{
Wolfgang Denkecb0ccd2005-09-24 22:37:32 +0200529 char *ep; /* Environment pointer */
Alessandro Rubini89ba81d2009-08-07 13:59:06 +0200530
Wolfgang Denkc96f86e2010-01-17 23:55:53 +0100531 /*
532 * Allow the user to choose TFTP blocksize and timeout.
533 * TFTP protocol has a minimal timeout of 1 second.
534 */
Alessandro Rubini89ba81d2009-08-07 13:59:06 +0200535 if ((ep = getenv("tftpblocksize")) != NULL)
536 TftpBlkSizeOption = simple_strtol(ep, NULL, 10);
Wolfgang Denkc96f86e2010-01-17 23:55:53 +0100537
538 if ((ep = getenv("tftptimeout")) != NULL)
539 TftpTimeoutMSecs = simple_strtol(ep, NULL, 10);
540
541 if (TftpTimeoutMSecs < 1000) {
542 printf("TFTP timeout (%ld ms) too low, "
543 "set minimum = 1000 ms\n",
544 TftpTimeoutMSecs);
545 TftpTimeoutMSecs = 1000;
546 }
547
548 debug("TFTP blocksize = %i, timeout = %ld ms\n",
549 TftpBlkSizeOption, TftpTimeoutMSecs);
Wolfgang Denkecb0ccd2005-09-24 22:37:32 +0200550
Jean-Christophe PLAGNIOL-VILLARDa93907c2008-01-18 01:14:03 +0100551 TftpServerIP = NetServerIP;
wdenkfe8c2802002-11-03 00:38:21 +0000552 if (BootFile[0] == '\0') {
wdenkfe8c2802002-11-03 00:38:21 +0000553 sprintf(default_filename, "%02lX%02lX%02lX%02lX.img",
Wolfgang Denkc43352c2005-08-04 01:09:44 +0200554 NetOurIP & 0xFF,
555 (NetOurIP >> 8) & 0xFF,
556 (NetOurIP >> 16) & 0xFF,
557 (NetOurIP >> 24) & 0xFF );
Jean-Christophe PLAGNIOL-VILLARDa93907c2008-01-18 01:14:03 +0100558
559 strncpy(tftp_filename, default_filename, MAX_LEN);
560 tftp_filename[MAX_LEN-1] = 0;
wdenkfe8c2802002-11-03 00:38:21 +0000561
562 printf ("*** Warning: no boot file name; using '%s'\n",
563 tftp_filename);
564 } else {
Jean-Christophe PLAGNIOL-VILLARD38cc09c2008-02-14 08:02:12 +0100565 char *p = strchr (BootFile, ':');
Jean-Christophe PLAGNIOL-VILLARDa93907c2008-01-18 01:14:03 +0100566
567 if (p == NULL) {
568 strncpy(tftp_filename, BootFile, MAX_LEN);
569 tftp_filename[MAX_LEN-1] = 0;
570 } else {
Jean-Christophe PLAGNIOL-VILLARDa93907c2008-01-18 01:14:03 +0100571 TftpServerIP = string_to_ip (BootFile);
Peter Tyser6a86bb62008-12-01 16:29:38 -0600572 strncpy(tftp_filename, p + 1, MAX_LEN);
Jean-Christophe PLAGNIOL-VILLARDa93907c2008-01-18 01:14:03 +0100573 tftp_filename[MAX_LEN-1] = 0;
574 }
wdenkfe8c2802002-11-03 00:38:21 +0000575 }
576
wdenk5bb226e2003-11-17 21:14:37 +0000577#if defined(CONFIG_NET_MULTI)
578 printf ("Using %s device\n", eth_get_name());
579#endif
Mike Frysingerb6446b62009-02-17 00:00:53 -0500580 printf("TFTP from server %pI4"
581 "; our IP address is %pI4", &TftpServerIP, &NetOurIP);
wdenkfe8c2802002-11-03 00:38:21 +0000582
583 /* Check if we need to send across this subnet */
584 if (NetOurGatewayIP && NetOurSubnetMask) {
Jean-Christophe PLAGNIOL-VILLARDa93907c2008-01-18 01:14:03 +0100585 IPaddr_t OurNet = NetOurIP & NetOurSubnetMask;
586 IPaddr_t ServerNet = TftpServerIP & NetOurSubnetMask;
wdenkfe8c2802002-11-03 00:38:21 +0000587
Mike Frysingerb6446b62009-02-17 00:00:53 -0500588 if (OurNet != ServerNet)
589 printf("; sending through gateway %pI4", &NetOurGatewayIP);
wdenkfe8c2802002-11-03 00:38:21 +0000590 }
591 putc ('\n');
592
593 printf ("Filename '%s'.", tftp_filename);
594
595 if (NetBootFileSize) {
596 printf (" Size is 0x%x Bytes = ", NetBootFileSize<<9);
597 print_size (NetBootFileSize<<9, "");
598 }
599
600 putc ('\n');
601
602 printf ("Load address: 0x%lx\n", load_addr);
603
604 puts ("Loading: *\b");
605
Bartlomiej Siekae83cc062008-10-01 15:26:29 +0200606 TftpTimeoutCountMax = TftpRRQTimeoutCountMax;
607
608 NetSetTimeout (TftpTimeoutMSecs, TftpTimeout);
wdenkfe8c2802002-11-03 00:38:21 +0000609 NetSetHandler (TftpHandler);
610
611 TftpServerPort = WELL_KNOWN_PORT;
612 TftpTimeoutCount = 0;
613 TftpState = STATE_RRQ;
Wolfgang Denkecb0ccd2005-09-24 22:37:32 +0200614 /* Use a pseudo-random port unless a specific port is set */
wdenkfe8c2802002-11-03 00:38:21 +0000615 TftpOurPort = 1024 + (get_timer(0) % 3072);
David Updegraff53a5c422007-06-11 10:41:07 -0500616
Wolfgang Denkecb0ccd2005-09-24 22:37:32 +0200617#ifdef CONFIG_TFTP_PORT
Wolfgang Denk28cb9372005-09-24 23:25:46 +0200618 if ((ep = getenv("tftpdstp")) != NULL) {
619 TftpServerPort = simple_strtol(ep, NULL, 10);
620 }
621 if ((ep = getenv("tftpsrcp")) != NULL) {
Wolfgang Denkecb0ccd2005-09-24 22:37:32 +0200622 TftpOurPort= simple_strtol(ep, NULL, 10);
623 }
624#endif
wdenkfbe4b5c2003-10-06 21:55:32 +0000625 TftpBlock = 0;
wdenkfe8c2802002-11-03 00:38:21 +0000626
wdenk73a8b272003-06-05 19:27:42 +0000627 /* zero out server ether in case the server ip has changed */
628 memset(NetServerEther, 0, 6);
David Updegraff53a5c422007-06-11 10:41:07 -0500629 /* Revert TftpBlkSize to dflt */
630 TftpBlkSize = TFTP_BLOCK_SIZE;
631#ifdef CONFIG_MCAST_TFTP
Jean-Christophe PLAGNIOL-VILLARDa93907c2008-01-18 01:14:03 +0100632 mcast_cleanup();
David Updegraff53a5c422007-06-11 10:41:07 -0500633#endif
Robin Getz4fccb812009-08-20 10:50:20 -0400634#ifdef CONFIG_TFTP_TSIZE
635 TftpTsize = 0;
636 TftpNumchars = 0;
637#endif
wdenk73a8b272003-06-05 19:27:42 +0000638
wdenkfe8c2802002-11-03 00:38:21 +0000639 TftpSend ();
640}
641
David Updegraff53a5c422007-06-11 10:41:07 -0500642#ifdef CONFIG_MCAST_TFTP
643/* Credits: atftp project.
644 */
645
646/* pick up BcastAddr, Port, and whether I am [now] the master-client. *
647 * Frame:
648 * +-------+-----------+---+-------~~-------+---+
649 * | opc | multicast | 0 | addr, port, mc | 0 |
650 * +-------+-----------+---+-------~~-------+---+
651 * The multicast addr/port becomes what I listen to, and if 'mc' is '1' then
652 * I am the new master-client so must send ACKs to DataBlocks. If I am not
653 * master-client, I'm a passive client, gathering what DataBlocks I may and
654 * making note of which ones I got in my bitmask.
655 * In theory, I never go from master->passive..
656 * .. this comes in with pkt already pointing just past opc
657 */
658static void parse_multicast_oack(char *pkt, int len)
659{
660 int i;
661 IPaddr_t addr;
662 char *mc_adr, *port, *mc;
663
664 mc_adr=port=mc=NULL;
665 /* march along looking for 'multicast\0', which has to start at least
666 * 14 bytes back from the end.
667 */
668 for (i=0;i<len-14;i++)
669 if (strcmp (pkt+i,"multicast") == 0)
670 break;
671 if (i >= (len-14)) /* non-Multicast OACK, ign. */
672 return;
Wolfgang Denk85eb5ca2007-08-14 09:47:27 +0200673
David Updegraff53a5c422007-06-11 10:41:07 -0500674 i+=10; /* strlen multicast */
675 mc_adr = pkt+i;
676 for (;i<len;i++) {
677 if (*(pkt+i) == ',') {
678 *(pkt+i) = '\0';
679 if (port) {
680 mc = pkt+i+1;
681 break;
682 } else {
683 port = pkt+i+1;
684 }
685 }
686 }
687 if (!port || !mc_adr || !mc ) return;
688 if (Multicast && MasterClient) {
689 printf ("I got a OACK as master Client, WRONG!\n");
690 return;
691 }
692 /* ..I now accept packets destined for this MCAST addr, port */
693 if (!Multicast) {
694 if (Bitmap) {
695 printf ("Internal failure! no mcast.\n");
696 free(Bitmap);
697 Bitmap=NULL;
698 ProhibitMcast=1;
699 return ;
700 }
701 /* I malloc instead of pre-declare; so that if the file ends
Wolfgang Denk85eb5ca2007-08-14 09:47:27 +0200702 * up being too big for this bitmap I can retry
703 */
David Updegraff53a5c422007-06-11 10:41:07 -0500704 if (!(Bitmap = malloc (Mapsize))) {
705 printf ("No Bitmap, no multicast. Sorry.\n");
706 ProhibitMcast=1;
707 return;
708 }
709 memset (Bitmap,0,Mapsize);
710 PrevBitmapHole = 0;
711 Multicast = 1;
712 }
713 addr = string_to_ip(mc_adr);
714 if (Mcast_addr != addr) {
715 if (Mcast_addr)
716 eth_mcast_join(Mcast_addr, 0);
717 if (eth_mcast_join(Mcast_addr=addr, 1)) {
718 printf ("Fail to set mcast, revert to TFTP\n");
719 ProhibitMcast=1;
720 mcast_cleanup();
721 NetStartAgain();
722 }
723 }
724 MasterClient = (unsigned char)simple_strtoul((char *)mc,NULL,10);
725 Mcast_port = (unsigned short)simple_strtoul(port,NULL,10);
726 printf ("Multicast: %s:%d [%d]\n", mc_adr, Mcast_port, MasterClient);
727 return;
728}
729
730#endif /* Multicast TFTP */