blob: 4c052661c7ad500d1a9035b7e6db50ef9ddf369b [file] [log] [blame]
wdenkfe8c2802002-11-03 00:38:21 +00001/*
Luca Ceresolic42fc272011-05-14 05:49:56 +00002 * Copyright 1994, 1995, 2000 Neil Russell.
3 * (See License)
4 * Copyright 2000, 2001 DENX Software Engineering, Wolfgang Denk, wd@denx.de
wdenkfe8c2802002-11-03 00:38:21 +00005 */
6
7#include <common.h>
8#include <command.h>
9#include <net.h>
10#include "tftp.h"
11#include "bootp.h"
12
Luca Ceresolic42fc272011-05-14 05:49:56 +000013/* Well known TFTP port # */
14#define WELL_KNOWN_PORT 69
15/* Millisecs to timeout for lost pkt */
16#define TIMEOUT 5000UL
wdenkfe8c2802002-11-03 00:38:21 +000017#ifndef CONFIG_NET_RETRY_COUNT
Luca Ceresolic42fc272011-05-14 05:49:56 +000018/* # of timeouts before giving up */
19# define TIMEOUT_COUNT 10
wdenkfe8c2802002-11-03 00:38:21 +000020#else
21# define TIMEOUT_COUNT (CONFIG_NET_RETRY_COUNT * 2)
22#endif
Luca Ceresolic42fc272011-05-14 05:49:56 +000023/* Number of "loading" hashes per line (for checking the image size) */
24#define HASHES_PER_LINE 65
wdenkfe8c2802002-11-03 00:38:21 +000025
26/*
27 * TFTP operations.
28 */
29#define TFTP_RRQ 1
30#define TFTP_WRQ 2
31#define TFTP_DATA 3
32#define TFTP_ACK 4
33#define TFTP_ERROR 5
wdenk7182b0f2003-10-06 21:55:32 +000034#define TFTP_OACK 6
wdenkfe8c2802002-11-03 00:38:21 +000035
Bartlomiej Sieka371cde72008-10-01 15:26:29 +020036static ulong TftpTimeoutMSecs = TIMEOUT;
37static int TftpTimeoutCountMax = TIMEOUT_COUNT;
38
39/*
40 * These globals govern the timeout behavior when attempting a connection to a
41 * TFTP server. TftpRRQTimeoutMSecs specifies the number of milliseconds to
42 * wait for the server to respond to initial connection. Second global,
43 * TftpRRQTimeoutCountMax, gives the number of such connection retries.
44 * TftpRRQTimeoutCountMax must be non-negative and TftpRRQTimeoutMSecs must be
45 * positive. The globals are meant to be set (and restored) by code needing
46 * non-standard timeout behavior when initiating a TFTP transfer.
47 */
48ulong TftpRRQTimeoutMSecs = TIMEOUT;
49int TftpRRQTimeoutCountMax = TIMEOUT_COUNT;
50
Remy Bohmerb46293f2009-10-28 22:13:40 +010051enum {
52 TFTP_ERR_UNDEFINED = 0,
53 TFTP_ERR_FILE_NOT_FOUND = 1,
54 TFTP_ERR_ACCESS_DENIED = 2,
55 TFTP_ERR_DISK_FULL = 3,
56 TFTP_ERR_UNEXPECTED_OPCODE = 4,
57 TFTP_ERR_UNKNOWN_TRANSFER_ID = 5,
58 TFTP_ERR_FILE_ALREADY_EXISTS = 6,
59};
60
Jean-Christophe PLAGNIOL-VILLARD58555412008-01-18 01:14:03 +010061static IPaddr_t TftpServerIP;
Luca Ceresolic42fc272011-05-14 05:49:56 +000062/* The UDP port at their end */
63static int TftpServerPort;
64/* The UDP port at our end */
65static int TftpOurPort;
wdenkfe8c2802002-11-03 00:38:21 +000066static int TftpTimeoutCount;
Luca Ceresolic42fc272011-05-14 05:49:56 +000067/* packet sequence number */
68static ulong TftpBlock;
69/* last packet sequence number received */
70static ulong TftpLastBlock;
71/* count of sequence number wraparounds */
72static ulong TftpBlockWrap;
73/* memory offset due to wrapping */
74static ulong TftpBlockWrapOffset;
wdenkfe8c2802002-11-03 00:38:21 +000075static int TftpState;
Robin Getzb3b00ed2009-08-20 10:50:20 -040076#ifdef CONFIG_TFTP_TSIZE
Luca Ceresolic42fc272011-05-14 05:49:56 +000077/* The file size reported by the server */
78static int TftpTsize;
79/* The number of hashes we printed */
80static short TftpNumchars;
Robin Getzb3b00ed2009-08-20 10:50:20 -040081#endif
wdenkef893942004-02-23 16:11:30 +000082
wdenkfe8c2802002-11-03 00:38:21 +000083#define STATE_RRQ 1
84#define STATE_DATA 2
85#define STATE_TOO_LARGE 3
86#define STATE_BAD_MAGIC 4
wdenk7182b0f2003-10-06 21:55:32 +000087#define STATE_OACK 5
wdenkfe8c2802002-11-03 00:38:21 +000088
Luca Ceresolic42fc272011-05-14 05:49:56 +000089/* default TFTP block size */
90#define TFTP_BLOCK_SIZE 512
91/* sequence number is 16 bit */
92#define TFTP_SEQUENCE_SIZE ((ulong)(1<<16))
wdenkef893942004-02-23 16:11:30 +000093
wdenkfe8c2802002-11-03 00:38:21 +000094#define DEFAULT_NAME_LEN (8 + 4 + 1)
95static char default_filename[DEFAULT_NAME_LEN];
Jean-Christophe PLAGNIOL-VILLARD58555412008-01-18 01:14:03 +010096
97#ifndef CONFIG_TFTP_FILE_NAME_MAX_LEN
98#define MAX_LEN 128
99#else
100#define MAX_LEN CONFIG_TFTP_FILE_NAME_MAX_LEN
101#endif
102
103static char tftp_filename[MAX_LEN];
wdenkfe8c2802002-11-03 00:38:21 +0000104
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200105#ifdef CONFIG_SYS_DIRECT_FLASH_TFTP
Marian Balakowicz513b4a12005-10-11 19:09:42 +0200106extern flash_info_t flash_info[];
wdenkfe8c2802002-11-03 00:38:21 +0000107#endif
108
Wolfgang Denk627f5c32007-08-14 09:47:27 +0200109/* 512 is poor choice for ethernet, MTU is typically 1500.
110 * Minus eth.hdrs thats 1468. Can get 2x better throughput with
David Updegraff7280da72007-06-11 10:41:07 -0500111 * almost-MTU block sizes. At least try... fall back to 512 if need be.
Alessandro Rubini8832f6e2009-08-07 13:59:06 +0200112 * (but those using CONFIG_IP_DEFRAG may want to set a larger block in cfg file)
David Updegraff7280da72007-06-11 10:41:07 -0500113 */
Alessandro Rubini8832f6e2009-08-07 13:59:06 +0200114#ifdef CONFIG_TFTP_BLOCKSIZE
115#define TFTP_MTU_BLOCKSIZE CONFIG_TFTP_BLOCKSIZE
116#else
David Updegraff7280da72007-06-11 10:41:07 -0500117#define TFTP_MTU_BLOCKSIZE 1468
Alessandro Rubini8832f6e2009-08-07 13:59:06 +0200118#endif
119
Luca Ceresolif40538a2011-05-14 05:49:57 +0000120static unsigned short TftpBlkSize = TFTP_BLOCK_SIZE;
121static unsigned short TftpBlkSizeOption = TFTP_MTU_BLOCKSIZE;
David Updegraff7280da72007-06-11 10:41:07 -0500122
123#ifdef CONFIG_MCAST_TFTP
124#include <malloc.h>
125#define MTFTP_BITMAPSIZE 0x1000
126static unsigned *Bitmap;
Luca Ceresolif40538a2011-05-14 05:49:57 +0000127static int PrevBitmapHole, Mapsize = MTFTP_BITMAPSIZE;
128static uchar ProhibitMcast = 0, MasterClient = 0;
129static uchar Multicast = 0;
David Updegraff7280da72007-06-11 10:41:07 -0500130extern IPaddr_t Mcast_addr;
131static int Mcast_port;
132static ulong TftpEndingBlock; /* can get 'last' block before done..*/
133
Luca Ceresolif40538a2011-05-14 05:49:57 +0000134static void parse_multicast_oack(char *pkt, int len);
David Updegraff7280da72007-06-11 10:41:07 -0500135
136static void
137mcast_cleanup(void)
138{
139 if (Mcast_addr) eth_mcast_join(Mcast_addr, 0);
140 if (Bitmap) free(Bitmap);
Luca Ceresolif40538a2011-05-14 05:49:57 +0000141 Bitmap = NULL;
David Updegraff7280da72007-06-11 10:41:07 -0500142 Mcast_addr = Multicast = Mcast_port = 0;
143 TftpEndingBlock = -1;
144}
145
146#endif /* CONFIG_MCAST_TFTP */
147
wdenkfe8c2802002-11-03 00:38:21 +0000148static __inline__ void
Luca Ceresolibf6ce412011-05-14 05:49:58 +0000149store_block(unsigned block, uchar *src, unsigned len)
wdenkfe8c2802002-11-03 00:38:21 +0000150{
David Updegraff7280da72007-06-11 10:41:07 -0500151 ulong offset = block * TftpBlkSize + TftpBlockWrapOffset;
wdenkef893942004-02-23 16:11:30 +0000152 ulong newsize = offset + len;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200153#ifdef CONFIG_SYS_DIRECT_FLASH_TFTP
wdenkfe8c2802002-11-03 00:38:21 +0000154 int i, rc = 0;
155
Luca Ceresolif40538a2011-05-14 05:49:57 +0000156 for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; i++) {
wdenkfe8c2802002-11-03 00:38:21 +0000157 /* start address in flash? */
Jochen Friedrichd31a59b2008-09-02 11:24:59 +0200158 if (flash_info[i].flash_id == FLASH_UNKNOWN)
159 continue;
wdenkfe8c2802002-11-03 00:38:21 +0000160 if (load_addr + offset >= flash_info[i].start[0]) {
161 rc = 1;
162 break;
163 }
164 }
165
166 if (rc) { /* Flash is destination for this packet */
Luca Ceresolif40538a2011-05-14 05:49:57 +0000167 rc = flash_write((char *)src, (ulong)(load_addr+offset), len);
wdenkfe8c2802002-11-03 00:38:21 +0000168 if (rc) {
Luca Ceresolif40538a2011-05-14 05:49:57 +0000169 flash_perror(rc);
wdenkfe8c2802002-11-03 00:38:21 +0000170 NetState = NETLOOP_FAIL;
171 return;
172 }
173 }
174 else
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200175#endif /* CONFIG_SYS_DIRECT_FLASH_TFTP */
wdenkfe8c2802002-11-03 00:38:21 +0000176 {
177 (void)memcpy((void *)(load_addr + offset), src, len);
178 }
David Updegraff7280da72007-06-11 10:41:07 -0500179#ifdef CONFIG_MCAST_TFTP
180 if (Multicast)
181 ext2_set_bit(block, Bitmap);
182#endif
wdenkfe8c2802002-11-03 00:38:21 +0000183
184 if (NetBootFileXferSize < newsize)
185 NetBootFileXferSize = newsize;
186}
187
Luca Ceresolif40538a2011-05-14 05:49:57 +0000188static void TftpSend(void);
189static void TftpTimeout(void);
wdenkfe8c2802002-11-03 00:38:21 +0000190
191/**********************************************************************/
192
193static void
Luca Ceresolif40538a2011-05-14 05:49:57 +0000194TftpSend(void)
wdenkfe8c2802002-11-03 00:38:21 +0000195{
Luca Ceresolibf6ce412011-05-14 05:49:58 +0000196 volatile uchar *pkt;
197 volatile uchar *xp;
198 int len = 0;
Wolfgang Denk3135c542005-08-26 01:36:03 +0200199 volatile ushort *s;
wdenkfe8c2802002-11-03 00:38:21 +0000200
Wolfgang Denk627f5c32007-08-14 09:47:27 +0200201#ifdef CONFIG_MCAST_TFTP
David Updegraff7280da72007-06-11 10:41:07 -0500202 /* Multicast TFTP.. non-MasterClients do not ACK data. */
Wolfgang Denk627f5c32007-08-14 09:47:27 +0200203 if (Multicast
204 && (TftpState == STATE_DATA)
205 && (MasterClient == 0))
David Updegraff7280da72007-06-11 10:41:07 -0500206 return;
207#endif
wdenkfe8c2802002-11-03 00:38:21 +0000208 /*
209 * We will always be sending some sort of packet, so
210 * cobble together the packet headers now.
211 */
wdenk145d2c12004-04-15 21:48:45 +0000212 pkt = NetTxPacket + NetEthHdrSize() + IP_HDR_SIZE;
wdenkfe8c2802002-11-03 00:38:21 +0000213
214 switch (TftpState) {
215
216 case STATE_RRQ:
217 xp = pkt;
Wolfgang Denk3135c542005-08-26 01:36:03 +0200218 s = (ushort *)pkt;
219 *s++ = htons(TFTP_RRQ);
220 pkt = (uchar *)s;
Luca Ceresolif40538a2011-05-14 05:49:57 +0000221 strcpy((char *)pkt, tftp_filename);
wdenkfe8c2802002-11-03 00:38:21 +0000222 pkt += strlen(tftp_filename) + 1;
Luca Ceresolif40538a2011-05-14 05:49:57 +0000223 strcpy((char *)pkt, "octet");
wdenkfe8c2802002-11-03 00:38:21 +0000224 pkt += 5 /*strlen("octet")*/ + 1;
Luca Ceresolif40538a2011-05-14 05:49:57 +0000225 strcpy((char *)pkt, "timeout");
wdenk7182b0f2003-10-06 21:55:32 +0000226 pkt += 7 /*strlen("timeout")*/ + 1;
Wolfgang Denkb233bd72010-01-17 23:55:53 +0100227 sprintf((char *)pkt, "%lu", TftpTimeoutMSecs / 1000);
Robin Getz9e0a4d62009-07-23 03:01:03 -0400228 debug("send option \"timeout %s\"\n", (char *)pkt);
wdenk7182b0f2003-10-06 21:55:32 +0000229 pkt += strlen((char *)pkt) + 1;
Robin Getzb3b00ed2009-08-20 10:50:20 -0400230#ifdef CONFIG_TFTP_TSIZE
231 memcpy((char *)pkt, "tsize\0000\0", 8);
232 pkt += 8;
233#endif
David Updegraff7280da72007-06-11 10:41:07 -0500234 /* try for more effic. blk size */
Luca Ceresolif40538a2011-05-14 05:49:57 +0000235 pkt += sprintf((char *)pkt, "blksize%c%d%c",
236 0, TftpBlkSizeOption, 0);
Wolfgang Denk627f5c32007-08-14 09:47:27 +0200237#ifdef CONFIG_MCAST_TFTP
David Updegraff7280da72007-06-11 10:41:07 -0500238 /* Check all preconditions before even trying the option */
Wolfgang Denk627f5c32007-08-14 09:47:27 +0200239 if (!ProhibitMcast
Luca Ceresolif40538a2011-05-14 05:49:57 +0000240 && (Bitmap = malloc(Mapsize))
David Updegraff7280da72007-06-11 10:41:07 -0500241 && eth_get_dev()->mcast) {
242 free(Bitmap);
Luca Ceresolif40538a2011-05-14 05:49:57 +0000243 Bitmap = NULL;
244 pkt += sprintf((char *)pkt, "multicast%c%c", 0, 0);
David Updegraff7280da72007-06-11 10:41:07 -0500245 }
246#endif /* CONFIG_MCAST_TFTP */
wdenkfe8c2802002-11-03 00:38:21 +0000247 len = pkt - xp;
248 break;
249
wdenk7182b0f2003-10-06 21:55:32 +0000250 case STATE_OACK:
David Updegraff7280da72007-06-11 10:41:07 -0500251#ifdef CONFIG_MCAST_TFTP
252 /* My turn! Start at where I need blocks I missed.*/
253 if (Multicast)
Luca Ceresolif40538a2011-05-14 05:49:57 +0000254 TftpBlock = ext2_find_next_zero_bit(Bitmap,
255 (Mapsize*8), 0);
David Updegraff7280da72007-06-11 10:41:07 -0500256 /*..falling..*/
257#endif
258 case STATE_DATA:
wdenkfe8c2802002-11-03 00:38:21 +0000259 xp = pkt;
Wolfgang Denk3135c542005-08-26 01:36:03 +0200260 s = (ushort *)pkt;
261 *s++ = htons(TFTP_ACK);
262 *s++ = htons(TftpBlock);
263 pkt = (uchar *)s;
wdenkfe8c2802002-11-03 00:38:21 +0000264 len = pkt - xp;
265 break;
266
267 case STATE_TOO_LARGE:
268 xp = pkt;
Wolfgang Denk3135c542005-08-26 01:36:03 +0200269 s = (ushort *)pkt;
270 *s++ = htons(TFTP_ERROR);
271 *s++ = htons(3);
272 pkt = (uchar *)s;
Luca Ceresolif40538a2011-05-14 05:49:57 +0000273 strcpy((char *)pkt, "File too large");
wdenkfe8c2802002-11-03 00:38:21 +0000274 pkt += 14 /*strlen("File too large")*/ + 1;
275 len = pkt - xp;
276 break;
277
278 case STATE_BAD_MAGIC:
279 xp = pkt;
Wolfgang Denk3135c542005-08-26 01:36:03 +0200280 s = (ushort *)pkt;
281 *s++ = htons(TFTP_ERROR);
282 *s++ = htons(2);
283 pkt = (uchar *)s;
Luca Ceresolif40538a2011-05-14 05:49:57 +0000284 strcpy((char *)pkt, "File has bad magic");
wdenkfe8c2802002-11-03 00:38:21 +0000285 pkt += 18 /*strlen("File has bad magic")*/ + 1;
286 len = pkt - xp;
287 break;
288 }
289
Luca Ceresolic42fc272011-05-14 05:49:56 +0000290 NetSendUDPPacket(NetServerEther, TftpServerIP, TftpServerPort,
291 TftpOurPort, len);
wdenkfe8c2802002-11-03 00:38:21 +0000292}
293
294
295static void
Luca Ceresoli428ab362011-04-18 06:19:50 +0000296TftpHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src,
297 unsigned len)
wdenkfe8c2802002-11-03 00:38:21 +0000298{
299 ushort proto;
Wolfgang Denk3135c542005-08-26 01:36:03 +0200300 ushort *s;
Wolfgang Denk83e8e472007-08-30 14:42:15 +0200301 int i;
wdenkfe8c2802002-11-03 00:38:21 +0000302
303 if (dest != TftpOurPort) {
David Updegraff7280da72007-06-11 10:41:07 -0500304#ifdef CONFIG_MCAST_TFTP
Wolfgang Denk627f5c32007-08-14 09:47:27 +0200305 if (Multicast
David Updegraff7280da72007-06-11 10:41:07 -0500306 && (!Mcast_port || (dest != Mcast_port)))
307#endif
wdenkfe8c2802002-11-03 00:38:21 +0000308 return;
309 }
310 if (TftpState != STATE_RRQ && src != TftpServerPort) {
311 return;
312 }
313
314 if (len < 2) {
315 return;
316 }
317 len -= 2;
318 /* warning: don't use increment (++) in ntohs() macros!! */
Wolfgang Denk3135c542005-08-26 01:36:03 +0200319 s = (ushort *)pkt;
320 proto = *s++;
321 pkt = (uchar *)s;
wdenkfe8c2802002-11-03 00:38:21 +0000322 switch (ntohs(proto)) {
323
324 case TFTP_RRQ:
325 case TFTP_WRQ:
326 case TFTP_ACK:
327 break;
328 default:
329 break;
330
wdenk7182b0f2003-10-06 21:55:32 +0000331 case TFTP_OACK:
Wolfgang Denk12730c72009-08-10 09:59:10 +0200332 debug("Got OACK: %s %s\n",
333 pkt,
334 pkt + strlen((char *)pkt) + 1);
wdenk7182b0f2003-10-06 21:55:32 +0000335 TftpState = STATE_OACK;
336 TftpServerPort = src;
Wolfgang Denk8b70f342007-08-31 10:01:51 +0200337 /*
338 * Check for 'blksize' option.
339 * Careful: "i" is signed, "len" is unsigned, thus
340 * something like "len-8" may give a *huge* number
341 */
Luca Ceresolif40538a2011-05-14 05:49:57 +0000342 for (i = 0; i+8 < len; i++) {
Luca Ceresolibf6ce412011-05-14 05:49:58 +0000343 if (strcmp((char *)pkt+i, "blksize") == 0) {
Wolfgang Denk83e8e472007-08-30 14:42:15 +0200344 TftpBlkSize = (unsigned short)
Luca Ceresolibf6ce412011-05-14 05:49:58 +0000345 simple_strtoul((char *)pkt+i+8, NULL,
Luca Ceresolif40538a2011-05-14 05:49:57 +0000346 10);
Robin Getz9e0a4d62009-07-23 03:01:03 -0400347 debug("Blocksize ack: %s, %d\n",
Luca Ceresolibf6ce412011-05-14 05:49:58 +0000348 (char *)pkt+i+8, TftpBlkSize);
Wolfgang Denk83e8e472007-08-30 14:42:15 +0200349 }
Robin Getzb3b00ed2009-08-20 10:50:20 -0400350#ifdef CONFIG_TFTP_TSIZE
Luca Ceresolibf6ce412011-05-14 05:49:58 +0000351 if (strcmp((char *)pkt+i, "tsize") == 0) {
352 TftpTsize = simple_strtoul((char *)pkt+i+6,
Luca Ceresolic42fc272011-05-14 05:49:56 +0000353 NULL, 10);
Robin Getzb3b00ed2009-08-20 10:50:20 -0400354 debug("size = %s, %d\n",
Luca Ceresolibf6ce412011-05-14 05:49:58 +0000355 (char *)pkt+i+6, TftpTsize);
Robin Getzb3b00ed2009-08-20 10:50:20 -0400356 }
357#endif
David Updegraff7280da72007-06-11 10:41:07 -0500358 }
359#ifdef CONFIG_MCAST_TFTP
Luca Ceresolif40538a2011-05-14 05:49:57 +0000360 parse_multicast_oack((char *)pkt, len-1);
Wolfgang Denk627f5c32007-08-14 09:47:27 +0200361 if ((Multicast) && (!MasterClient))
David Updegraff7280da72007-06-11 10:41:07 -0500362 TftpState = STATE_DATA; /* passive.. */
363 else
364#endif
Luca Ceresolif40538a2011-05-14 05:49:57 +0000365 TftpSend(); /* Send ACK */
wdenk7182b0f2003-10-06 21:55:32 +0000366 break;
wdenkfe8c2802002-11-03 00:38:21 +0000367 case TFTP_DATA:
368 if (len < 2)
369 return;
370 len -= 2;
371 TftpBlock = ntohs(*(ushort *)pkt);
wdenkef893942004-02-23 16:11:30 +0000372
373 /*
wdenkf70cbb22004-02-23 20:48:38 +0000374 * RFC1350 specifies that the first data packet will
375 * have sequence number 1. If we receive a sequence
376 * number of 0 this means that there was a wrap
377 * around of the (16 bit) counter.
wdenkef893942004-02-23 16:11:30 +0000378 */
379 if (TftpBlock == 0) {
380 TftpBlockWrap++;
Luca Ceresolic42fc272011-05-14 05:49:56 +0000381 TftpBlockWrapOffset +=
382 TftpBlkSize * TFTP_SEQUENCE_SIZE;
Luca Ceresolif40538a2011-05-14 05:49:57 +0000383 printf("\n\t %lu MB received\n\t ",
Luca Ceresolic42fc272011-05-14 05:49:56 +0000384 TftpBlockWrapOffset>>20);
Robin Getzb3b00ed2009-08-20 10:50:20 -0400385 }
386#ifdef CONFIG_TFTP_TSIZE
387 else if (TftpTsize) {
Luca Ceresolic42fc272011-05-14 05:49:56 +0000388 while (TftpNumchars <
389 NetBootFileXferSize * 50 / TftpTsize) {
Robin Getzb3b00ed2009-08-20 10:50:20 -0400390 putc('#');
391 TftpNumchars++;
392 }
393 }
394#endif
395 else {
wdenkef893942004-02-23 16:11:30 +0000396 if (((TftpBlock - 1) % 10) == 0) {
Luca Ceresolif40538a2011-05-14 05:49:57 +0000397 putc('#');
wdenkef893942004-02-23 16:11:30 +0000398 } else if ((TftpBlock % (10 * HASHES_PER_LINE)) == 0) {
Luca Ceresolif40538a2011-05-14 05:49:57 +0000399 puts("\n\t ");
wdenkef893942004-02-23 16:11:30 +0000400 }
wdenkfe8c2802002-11-03 00:38:21 +0000401 }
402
Robin Getz9e0a4d62009-07-23 03:01:03 -0400403 if (TftpState == STATE_RRQ)
404 debug("Server did not acknowledge timeout option!\n");
wdenk7182b0f2003-10-06 21:55:32 +0000405
406 if (TftpState == STATE_RRQ || TftpState == STATE_OACK) {
wdenkef893942004-02-23 16:11:30 +0000407 /* first block received */
wdenkfe8c2802002-11-03 00:38:21 +0000408 TftpState = STATE_DATA;
409 TftpServerPort = src;
410 TftpLastBlock = 0;
wdenkef893942004-02-23 16:11:30 +0000411 TftpBlockWrap = 0;
412 TftpBlockWrapOffset = 0;
wdenkfe8c2802002-11-03 00:38:21 +0000413
David Updegraff7280da72007-06-11 10:41:07 -0500414#ifdef CONFIG_MCAST_TFTP
415 if (Multicast) { /* start!=1 common if mcast */
416 TftpLastBlock = TftpBlock - 1;
417 } else
418#endif
wdenkfe8c2802002-11-03 00:38:21 +0000419 if (TftpBlock != 1) { /* Assertion */
Luca Ceresolif40538a2011-05-14 05:49:57 +0000420 printf("\nTFTP error: "
421 "First block is not block 1 (%ld)\n"
422 "Starting again\n\n",
wdenkfe8c2802002-11-03 00:38:21 +0000423 TftpBlock);
Luca Ceresolif40538a2011-05-14 05:49:57 +0000424 NetStartAgain();
wdenkfe8c2802002-11-03 00:38:21 +0000425 break;
426 }
427 }
428
429 if (TftpBlock == TftpLastBlock) {
430 /*
431 * Same block again; ignore it.
432 */
433 break;
434 }
435
436 TftpLastBlock = TftpBlock;
Bartlomiej Sieka371cde72008-10-01 15:26:29 +0200437 TftpTimeoutCountMax = TIMEOUT_COUNT;
Luca Ceresolif40538a2011-05-14 05:49:57 +0000438 NetSetTimeout(TftpTimeoutMSecs, TftpTimeout);
wdenkfe8c2802002-11-03 00:38:21 +0000439
Luca Ceresolif40538a2011-05-14 05:49:57 +0000440 store_block(TftpBlock - 1, pkt + 2, len);
wdenkfe8c2802002-11-03 00:38:21 +0000441
442 /*
443 * Acknoledge the block just received, which will prompt
444 * the server for the next one.
445 */
David Updegraff7280da72007-06-11 10:41:07 -0500446#ifdef CONFIG_MCAST_TFTP
Wolfgang Denk627f5c32007-08-14 09:47:27 +0200447 /* if I am the MasterClient, actively calculate what my next
448 * needed block is; else I'm passive; not ACKING
Jean-Christophe PLAGNIOL-VILLARD58555412008-01-18 01:14:03 +0100449 */
David Updegraff7280da72007-06-11 10:41:07 -0500450 if (Multicast) {
451 if (len < TftpBlkSize) {
452 TftpEndingBlock = TftpBlock;
453 } else if (MasterClient) {
Wolfgang Denk627f5c32007-08-14 09:47:27 +0200454 TftpBlock = PrevBitmapHole =
David Updegraff7280da72007-06-11 10:41:07 -0500455 ext2_find_next_zero_bit(
456 Bitmap,
457 (Mapsize*8),
458 PrevBitmapHole);
459 if (TftpBlock > ((Mapsize*8) - 1)) {
Luca Ceresolif40538a2011-05-14 05:49:57 +0000460 printf("tftpfile too big\n");
David Updegraff7280da72007-06-11 10:41:07 -0500461 /* try to double it and retry */
Luca Ceresolif40538a2011-05-14 05:49:57 +0000462 Mapsize <<= 1;
David Updegraff7280da72007-06-11 10:41:07 -0500463 mcast_cleanup();
Luca Ceresolif40538a2011-05-14 05:49:57 +0000464 NetStartAgain();
David Updegraff7280da72007-06-11 10:41:07 -0500465 return;
466 }
467 TftpLastBlock = TftpBlock;
468 }
469 }
470#endif
Luca Ceresolif40538a2011-05-14 05:49:57 +0000471 TftpSend();
wdenkfe8c2802002-11-03 00:38:21 +0000472
David Updegraff7280da72007-06-11 10:41:07 -0500473#ifdef CONFIG_MCAST_TFTP
474 if (Multicast) {
475 if (MasterClient && (TftpBlock >= TftpEndingBlock)) {
Luca Ceresolif40538a2011-05-14 05:49:57 +0000476 puts("\nMulticast tftp done\n");
David Updegraff7280da72007-06-11 10:41:07 -0500477 mcast_cleanup();
478 NetState = NETLOOP_SUCCESS;
Wolfgang Denk627f5c32007-08-14 09:47:27 +0200479 }
David Updegraff7280da72007-06-11 10:41:07 -0500480 }
481 else
482#endif
483 if (len < TftpBlkSize) {
wdenkfe8c2802002-11-03 00:38:21 +0000484 /*
485 * We received the whole thing. Try to
486 * run it.
487 */
Robin Getzb3b00ed2009-08-20 10:50:20 -0400488#ifdef CONFIG_TFTP_TSIZE
Luca Ceresolic42fc272011-05-14 05:49:56 +0000489 /* Print hash marks for the last packet received */
Robin Getzb3b00ed2009-08-20 10:50:20 -0400490 while (TftpTsize && TftpNumchars < 49) {
491 putc('#');
492 TftpNumchars++;
493 }
494#endif
Luca Ceresolif40538a2011-05-14 05:49:57 +0000495 puts("\ndone\n");
wdenkfe8c2802002-11-03 00:38:21 +0000496 NetState = NETLOOP_SUCCESS;
497 }
498 break;
499
500 case TFTP_ERROR:
Luca Ceresolif40538a2011-05-14 05:49:57 +0000501 printf("\nTFTP error: '%s' (%d)\n",
502 pkt + 2, ntohs(*(ushort *)pkt));
Remy Bohmerb46293f2009-10-28 22:13:40 +0100503
504 switch (ntohs(*(ushort *)pkt)) {
505 case TFTP_ERR_FILE_NOT_FOUND:
506 case TFTP_ERR_ACCESS_DENIED:
507 puts("Not retrying...\n");
508 eth_halt();
509 NetState = NETLOOP_FAIL;
510 break;
511 case TFTP_ERR_UNDEFINED:
512 case TFTP_ERR_DISK_FULL:
513 case TFTP_ERR_UNEXPECTED_OPCODE:
514 case TFTP_ERR_UNKNOWN_TRANSFER_ID:
515 case TFTP_ERR_FILE_ALREADY_EXISTS:
516 default:
517 puts("Starting again\n\n");
David Updegraff7280da72007-06-11 10:41:07 -0500518#ifdef CONFIG_MCAST_TFTP
Remy Bohmerb46293f2009-10-28 22:13:40 +0100519 mcast_cleanup();
David Updegraff7280da72007-06-11 10:41:07 -0500520#endif
Remy Bohmerb46293f2009-10-28 22:13:40 +0100521 NetStartAgain();
522 break;
523 }
wdenkfe8c2802002-11-03 00:38:21 +0000524 break;
525 }
526}
527
528
529static void
Luca Ceresolif40538a2011-05-14 05:49:57 +0000530TftpTimeout(void)
wdenkfe8c2802002-11-03 00:38:21 +0000531{
Bartlomiej Sieka371cde72008-10-01 15:26:29 +0200532 if (++TftpTimeoutCount > TftpTimeoutCountMax) {
Luca Ceresolif40538a2011-05-14 05:49:57 +0000533 puts("\nRetry count exceeded; starting again\n");
David Updegraff7280da72007-06-11 10:41:07 -0500534#ifdef CONFIG_MCAST_TFTP
535 mcast_cleanup();
536#endif
Luca Ceresolif40538a2011-05-14 05:49:57 +0000537 NetStartAgain();
wdenkfe8c2802002-11-03 00:38:21 +0000538 } else {
Luca Ceresolif40538a2011-05-14 05:49:57 +0000539 puts("T ");
540 NetSetTimeout(TftpTimeoutMSecs, TftpTimeout);
541 TftpSend();
wdenkfe8c2802002-11-03 00:38:21 +0000542 }
543}
544
545
546void
Luca Ceresolif40538a2011-05-14 05:49:57 +0000547TftpStart(void)
wdenkfe8c2802002-11-03 00:38:21 +0000548{
Wolfgang Denke3cfce52005-09-24 22:37:32 +0200549 char *ep; /* Environment pointer */
Alessandro Rubini8832f6e2009-08-07 13:59:06 +0200550
Wolfgang Denkb233bd72010-01-17 23:55:53 +0100551 /*
552 * Allow the user to choose TFTP blocksize and timeout.
553 * TFTP protocol has a minimal timeout of 1 second.
554 */
Alessandro Rubini8832f6e2009-08-07 13:59:06 +0200555 if ((ep = getenv("tftpblocksize")) != NULL)
556 TftpBlkSizeOption = simple_strtol(ep, NULL, 10);
Wolfgang Denkb233bd72010-01-17 23:55:53 +0100557
558 if ((ep = getenv("tftptimeout")) != NULL)
559 TftpTimeoutMSecs = simple_strtol(ep, NULL, 10);
560
561 if (TftpTimeoutMSecs < 1000) {
562 printf("TFTP timeout (%ld ms) too low, "
563 "set minimum = 1000 ms\n",
564 TftpTimeoutMSecs);
565 TftpTimeoutMSecs = 1000;
566 }
567
568 debug("TFTP blocksize = %i, timeout = %ld ms\n",
569 TftpBlkSizeOption, TftpTimeoutMSecs);
Wolfgang Denke3cfce52005-09-24 22:37:32 +0200570
Jean-Christophe PLAGNIOL-VILLARD58555412008-01-18 01:14:03 +0100571 TftpServerIP = NetServerIP;
wdenkfe8c2802002-11-03 00:38:21 +0000572 if (BootFile[0] == '\0') {
wdenkfe8c2802002-11-03 00:38:21 +0000573 sprintf(default_filename, "%02lX%02lX%02lX%02lX.img",
Wolfgang Denkdec97322005-08-04 01:09:44 +0200574 NetOurIP & 0xFF,
575 (NetOurIP >> 8) & 0xFF,
576 (NetOurIP >> 16) & 0xFF,
Luca Ceresolif40538a2011-05-14 05:49:57 +0000577 (NetOurIP >> 24) & 0xFF);
Jean-Christophe PLAGNIOL-VILLARD58555412008-01-18 01:14:03 +0100578
579 strncpy(tftp_filename, default_filename, MAX_LEN);
580 tftp_filename[MAX_LEN-1] = 0;
wdenkfe8c2802002-11-03 00:38:21 +0000581
Luca Ceresolif40538a2011-05-14 05:49:57 +0000582 printf("*** Warning: no boot file name; using '%s'\n",
wdenkfe8c2802002-11-03 00:38:21 +0000583 tftp_filename);
584 } else {
Luca Ceresolif40538a2011-05-14 05:49:57 +0000585 char *p = strchr(BootFile, ':');
Jean-Christophe PLAGNIOL-VILLARD58555412008-01-18 01:14:03 +0100586
587 if (p == NULL) {
588 strncpy(tftp_filename, BootFile, MAX_LEN);
589 tftp_filename[MAX_LEN-1] = 0;
590 } else {
Luca Ceresolif40538a2011-05-14 05:49:57 +0000591 TftpServerIP = string_to_ip(BootFile);
Peter Tyser5e58efa2008-12-01 16:29:38 -0600592 strncpy(tftp_filename, p + 1, MAX_LEN);
Jean-Christophe PLAGNIOL-VILLARD58555412008-01-18 01:14:03 +0100593 tftp_filename[MAX_LEN-1] = 0;
594 }
wdenkfe8c2802002-11-03 00:38:21 +0000595 }
596
wdenkc6abb7e2003-11-17 21:14:37 +0000597#if defined(CONFIG_NET_MULTI)
Luca Ceresolif40538a2011-05-14 05:49:57 +0000598 printf("Using %s device\n", eth_get_name());
wdenkc6abb7e2003-11-17 21:14:37 +0000599#endif
Mike Frysingerd5695f42009-02-17 00:00:53 -0500600 printf("TFTP from server %pI4"
601 "; our IP address is %pI4", &TftpServerIP, &NetOurIP);
wdenkfe8c2802002-11-03 00:38:21 +0000602
603 /* Check if we need to send across this subnet */
604 if (NetOurGatewayIP && NetOurSubnetMask) {
Jean-Christophe PLAGNIOL-VILLARD58555412008-01-18 01:14:03 +0100605 IPaddr_t OurNet = NetOurIP & NetOurSubnetMask;
606 IPaddr_t ServerNet = TftpServerIP & NetOurSubnetMask;
wdenkfe8c2802002-11-03 00:38:21 +0000607
Mike Frysingerd5695f42009-02-17 00:00:53 -0500608 if (OurNet != ServerNet)
609 printf("; sending through gateway %pI4", &NetOurGatewayIP);
wdenkfe8c2802002-11-03 00:38:21 +0000610 }
Luca Ceresolif40538a2011-05-14 05:49:57 +0000611 putc('\n');
wdenkfe8c2802002-11-03 00:38:21 +0000612
Luca Ceresolif40538a2011-05-14 05:49:57 +0000613 printf("Filename '%s'.", tftp_filename);
wdenkfe8c2802002-11-03 00:38:21 +0000614
615 if (NetBootFileSize) {
Luca Ceresolif40538a2011-05-14 05:49:57 +0000616 printf(" Size is 0x%x Bytes = ", NetBootFileSize<<9);
617 print_size(NetBootFileSize<<9, "");
wdenkfe8c2802002-11-03 00:38:21 +0000618 }
619
Luca Ceresolif40538a2011-05-14 05:49:57 +0000620 putc('\n');
wdenkfe8c2802002-11-03 00:38:21 +0000621
Luca Ceresolif40538a2011-05-14 05:49:57 +0000622 printf("Load address: 0x%lx\n", load_addr);
wdenkfe8c2802002-11-03 00:38:21 +0000623
Luca Ceresolif40538a2011-05-14 05:49:57 +0000624 puts("Loading: *\b");
wdenkfe8c2802002-11-03 00:38:21 +0000625
Bartlomiej Sieka371cde72008-10-01 15:26:29 +0200626 TftpTimeoutCountMax = TftpRRQTimeoutCountMax;
627
Luca Ceresolif40538a2011-05-14 05:49:57 +0000628 NetSetTimeout(TftpTimeoutMSecs, TftpTimeout);
629 NetSetHandler(TftpHandler);
wdenkfe8c2802002-11-03 00:38:21 +0000630
631 TftpServerPort = WELL_KNOWN_PORT;
632 TftpTimeoutCount = 0;
633 TftpState = STATE_RRQ;
Wolfgang Denke3cfce52005-09-24 22:37:32 +0200634 /* Use a pseudo-random port unless a specific port is set */
wdenkfe8c2802002-11-03 00:38:21 +0000635 TftpOurPort = 1024 + (get_timer(0) % 3072);
David Updegraff7280da72007-06-11 10:41:07 -0500636
Wolfgang Denke3cfce52005-09-24 22:37:32 +0200637#ifdef CONFIG_TFTP_PORT
Wolfgang Denk227b5192005-09-24 23:25:46 +0200638 if ((ep = getenv("tftpdstp")) != NULL) {
639 TftpServerPort = simple_strtol(ep, NULL, 10);
640 }
641 if ((ep = getenv("tftpsrcp")) != NULL) {
Luca Ceresolif40538a2011-05-14 05:49:57 +0000642 TftpOurPort = simple_strtol(ep, NULL, 10);
Wolfgang Denke3cfce52005-09-24 22:37:32 +0200643 }
644#endif
wdenk7182b0f2003-10-06 21:55:32 +0000645 TftpBlock = 0;
wdenkfe8c2802002-11-03 00:38:21 +0000646
wdenke6466f62003-06-05 19:27:42 +0000647 /* zero out server ether in case the server ip has changed */
648 memset(NetServerEther, 0, 6);
David Updegraff7280da72007-06-11 10:41:07 -0500649 /* Revert TftpBlkSize to dflt */
650 TftpBlkSize = TFTP_BLOCK_SIZE;
651#ifdef CONFIG_MCAST_TFTP
Jean-Christophe PLAGNIOL-VILLARD58555412008-01-18 01:14:03 +0100652 mcast_cleanup();
David Updegraff7280da72007-06-11 10:41:07 -0500653#endif
Robin Getzb3b00ed2009-08-20 10:50:20 -0400654#ifdef CONFIG_TFTP_TSIZE
655 TftpTsize = 0;
656 TftpNumchars = 0;
657#endif
wdenke6466f62003-06-05 19:27:42 +0000658
Luca Ceresolif40538a2011-05-14 05:49:57 +0000659 TftpSend();
wdenkfe8c2802002-11-03 00:38:21 +0000660}
661
David Updegraff7280da72007-06-11 10:41:07 -0500662#ifdef CONFIG_MCAST_TFTP
663/* Credits: atftp project.
664 */
665
666/* pick up BcastAddr, Port, and whether I am [now] the master-client. *
667 * Frame:
668 * +-------+-----------+---+-------~~-------+---+
669 * | opc | multicast | 0 | addr, port, mc | 0 |
670 * +-------+-----------+---+-------~~-------+---+
671 * The multicast addr/port becomes what I listen to, and if 'mc' is '1' then
672 * I am the new master-client so must send ACKs to DataBlocks. If I am not
673 * master-client, I'm a passive client, gathering what DataBlocks I may and
674 * making note of which ones I got in my bitmask.
675 * In theory, I never go from master->passive..
676 * .. this comes in with pkt already pointing just past opc
677 */
678static void parse_multicast_oack(char *pkt, int len)
679{
Luca Ceresolif40538a2011-05-14 05:49:57 +0000680 int i;
681 IPaddr_t addr;
682 char *mc_adr, *port, *mc;
David Updegraff7280da72007-06-11 10:41:07 -0500683
Luca Ceresolif40538a2011-05-14 05:49:57 +0000684 mc_adr = port = mc = NULL;
David Updegraff7280da72007-06-11 10:41:07 -0500685 /* march along looking for 'multicast\0', which has to start at least
686 * 14 bytes back from the end.
687 */
Luca Ceresolif40538a2011-05-14 05:49:57 +0000688 for (i = 0; i < len-14; i++)
689 if (strcmp(pkt+i, "multicast") == 0)
David Updegraff7280da72007-06-11 10:41:07 -0500690 break;
691 if (i >= (len-14)) /* non-Multicast OACK, ign. */
692 return;
Wolfgang Denk627f5c32007-08-14 09:47:27 +0200693
Luca Ceresolif40538a2011-05-14 05:49:57 +0000694 i += 10; /* strlen multicast */
David Updegraff7280da72007-06-11 10:41:07 -0500695 mc_adr = pkt+i;
Luca Ceresolif40538a2011-05-14 05:49:57 +0000696 for (; i < len; i++) {
David Updegraff7280da72007-06-11 10:41:07 -0500697 if (*(pkt+i) == ',') {
698 *(pkt+i) = '\0';
699 if (port) {
700 mc = pkt+i+1;
701 break;
702 } else {
703 port = pkt+i+1;
704 }
705 }
706 }
Luca Ceresolif40538a2011-05-14 05:49:57 +0000707 if (!port || !mc_adr || !mc) return;
David Updegraff7280da72007-06-11 10:41:07 -0500708 if (Multicast && MasterClient) {
Luca Ceresolif40538a2011-05-14 05:49:57 +0000709 printf("I got a OACK as master Client, WRONG!\n");
David Updegraff7280da72007-06-11 10:41:07 -0500710 return;
711 }
712 /* ..I now accept packets destined for this MCAST addr, port */
713 if (!Multicast) {
714 if (Bitmap) {
Luca Ceresolif40538a2011-05-14 05:49:57 +0000715 printf("Internal failure! no mcast.\n");
David Updegraff7280da72007-06-11 10:41:07 -0500716 free(Bitmap);
Luca Ceresolif40538a2011-05-14 05:49:57 +0000717 Bitmap = NULL;
718 ProhibitMcast = 1;
David Updegraff7280da72007-06-11 10:41:07 -0500719 return ;
720 }
721 /* I malloc instead of pre-declare; so that if the file ends
Wolfgang Denk627f5c32007-08-14 09:47:27 +0200722 * up being too big for this bitmap I can retry
723 */
Luca Ceresolif40538a2011-05-14 05:49:57 +0000724 if (!(Bitmap = malloc(Mapsize))) {
725 printf("No Bitmap, no multicast. Sorry.\n");
726 ProhibitMcast = 1;
David Updegraff7280da72007-06-11 10:41:07 -0500727 return;
728 }
Luca Ceresolif40538a2011-05-14 05:49:57 +0000729 memset(Bitmap, 0, Mapsize);
David Updegraff7280da72007-06-11 10:41:07 -0500730 PrevBitmapHole = 0;
731 Multicast = 1;
732 }
733 addr = string_to_ip(mc_adr);
734 if (Mcast_addr != addr) {
735 if (Mcast_addr)
736 eth_mcast_join(Mcast_addr, 0);
Luca Ceresolif40538a2011-05-14 05:49:57 +0000737 if (eth_mcast_join(Mcast_addr = addr, 1)) {
738 printf("Fail to set mcast, revert to TFTP\n");
739 ProhibitMcast = 1;
David Updegraff7280da72007-06-11 10:41:07 -0500740 mcast_cleanup();
741 NetStartAgain();
742 }
743 }
Luca Ceresolif40538a2011-05-14 05:49:57 +0000744 MasterClient = (unsigned char)simple_strtoul((char *)mc, NULL, 10);
745 Mcast_port = (unsigned short)simple_strtoul(port, NULL, 10);
746 printf("Multicast: %s:%d [%d]\n", mc_adr, Mcast_port, MasterClient);
David Updegraff7280da72007-06-11 10:41:07 -0500747 return;
748}
749
750#endif /* Multicast TFTP */