blob: f83c0d6f8c1b36c88c5be17554c2e9ade3056328 [file] [log] [blame]
wdenkfe8c2802002-11-03 00:38:21 +00001/*
Luca Ceresoli2f094132011-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 Ceresoli2f094132011-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 Ceresoli2f094132011-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 Ceresoli2f094132011-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
wdenkfbe4b5c2003-10-06 21:55:32 +000034#define TFTP_OACK 6
wdenkfe8c2802002-11-03 00:38:21 +000035
Bartlomiej Siekae83cc062008-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 Bohmeraafda382009-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-VILLARDa93907c2008-01-18 01:14:03 +010061static IPaddr_t TftpServerIP;
Luca Ceresoli2f094132011-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 Ceresoli2f094132011-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 Getz4fccb812009-08-20 10:50:20 -040076#ifdef CONFIG_TFTP_TSIZE
Luca Ceresoli2f094132011-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 Getz4fccb812009-08-20 10:50:20 -040081#endif
wdenk3f85ce22004-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
wdenkfbe4b5c2003-10-06 21:55:32 +000087#define STATE_OACK 5
wdenkfe8c2802002-11-03 00:38:21 +000088
Luca Ceresoli2f094132011-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))
wdenk3f85ce22004-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-VILLARDa93907c2008-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-VILLARD6d0f6bc2008-10-16 15:01:15 +0200105#ifdef CONFIG_SYS_DIRECT_FLASH_TFTP
Marian Balakowicze6f2e902005-10-11 19:09:42 +0200106extern flash_info_t flash_info[];
wdenkfe8c2802002-11-03 00:38:21 +0000107#endif
108
Wolfgang Denk85eb5ca2007-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 Updegraff53a5c422007-06-11 10:41:07 -0500111 * almost-MTU block sizes. At least try... fall back to 512 if need be.
Alessandro Rubini89ba81d2009-08-07 13:59:06 +0200112 * (but those using CONFIG_IP_DEFRAG may want to set a larger block in cfg file)
David Updegraff53a5c422007-06-11 10:41:07 -0500113 */
Alessandro Rubini89ba81d2009-08-07 13:59:06 +0200114#ifdef CONFIG_TFTP_BLOCKSIZE
115#define TFTP_MTU_BLOCKSIZE CONFIG_TFTP_BLOCKSIZE
116#else
David Updegraff53a5c422007-06-11 10:41:07 -0500117#define TFTP_MTU_BLOCKSIZE 1468
Alessandro Rubini89ba81d2009-08-07 13:59:06 +0200118#endif
119
Luca Ceresolic718b142011-05-14 05:49:57 +0000120static unsigned short TftpBlkSize = TFTP_BLOCK_SIZE;
121static unsigned short TftpBlkSizeOption = TFTP_MTU_BLOCKSIZE;
David Updegraff53a5c422007-06-11 10:41:07 -0500122
123#ifdef CONFIG_MCAST_TFTP
124#include <malloc.h>
125#define MTFTP_BITMAPSIZE 0x1000
126static unsigned *Bitmap;
Luca Ceresolic718b142011-05-14 05:49:57 +0000127static int PrevBitmapHole, Mapsize = MTFTP_BITMAPSIZE;
128static uchar ProhibitMcast = 0, MasterClient = 0;
129static uchar Multicast = 0;
David Updegraff53a5c422007-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 Ceresolic718b142011-05-14 05:49:57 +0000134static void parse_multicast_oack(char *pkt, int len);
David Updegraff53a5c422007-06-11 10:41:07 -0500135
136static void
137mcast_cleanup(void)
138{
Luca Ceresoli6d2231e2011-05-14 05:50:01 +0000139 if (Mcast_addr)
140 eth_mcast_join(Mcast_addr, 0);
141 if (Bitmap)
142 free(Bitmap);
Luca Ceresolic718b142011-05-14 05:49:57 +0000143 Bitmap = NULL;
David Updegraff53a5c422007-06-11 10:41:07 -0500144 Mcast_addr = Multicast = Mcast_port = 0;
145 TftpEndingBlock = -1;
146}
147
148#endif /* CONFIG_MCAST_TFTP */
149
wdenkfe8c2802002-11-03 00:38:21 +0000150static __inline__ void
Luca Ceresoli2e320252011-05-14 05:49:58 +0000151store_block(unsigned block, uchar *src, unsigned len)
wdenkfe8c2802002-11-03 00:38:21 +0000152{
David Updegraff53a5c422007-06-11 10:41:07 -0500153 ulong offset = block * TftpBlkSize + TftpBlockWrapOffset;
wdenk3f85ce22004-02-23 16:11:30 +0000154 ulong newsize = offset + len;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200155#ifdef CONFIG_SYS_DIRECT_FLASH_TFTP
wdenkfe8c2802002-11-03 00:38:21 +0000156 int i, rc = 0;
157
Luca Ceresolic718b142011-05-14 05:49:57 +0000158 for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; i++) {
wdenkfe8c2802002-11-03 00:38:21 +0000159 /* start address in flash? */
Jochen Friedrichbe1b0d22008-09-02 11:24:59 +0200160 if (flash_info[i].flash_id == FLASH_UNKNOWN)
161 continue;
wdenkfe8c2802002-11-03 00:38:21 +0000162 if (load_addr + offset >= flash_info[i].start[0]) {
163 rc = 1;
164 break;
165 }
166 }
167
168 if (rc) { /* Flash is destination for this packet */
Luca Ceresolic718b142011-05-14 05:49:57 +0000169 rc = flash_write((char *)src, (ulong)(load_addr+offset), len);
wdenkfe8c2802002-11-03 00:38:21 +0000170 if (rc) {
Luca Ceresolic718b142011-05-14 05:49:57 +0000171 flash_perror(rc);
wdenkfe8c2802002-11-03 00:38:21 +0000172 NetState = NETLOOP_FAIL;
173 return;
174 }
175 }
176 else
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200177#endif /* CONFIG_SYS_DIRECT_FLASH_TFTP */
wdenkfe8c2802002-11-03 00:38:21 +0000178 {
179 (void)memcpy((void *)(load_addr + offset), src, len);
180 }
David Updegraff53a5c422007-06-11 10:41:07 -0500181#ifdef CONFIG_MCAST_TFTP
182 if (Multicast)
183 ext2_set_bit(block, Bitmap);
184#endif
wdenkfe8c2802002-11-03 00:38:21 +0000185
186 if (NetBootFileXferSize < newsize)
187 NetBootFileXferSize = newsize;
188}
189
Luca Ceresolic718b142011-05-14 05:49:57 +0000190static void TftpSend(void);
191static void TftpTimeout(void);
wdenkfe8c2802002-11-03 00:38:21 +0000192
193/**********************************************************************/
194
195static void
Luca Ceresolic718b142011-05-14 05:49:57 +0000196TftpSend(void)
wdenkfe8c2802002-11-03 00:38:21 +0000197{
Luca Ceresoli2e320252011-05-14 05:49:58 +0000198 volatile uchar *pkt;
199 volatile uchar *xp;
200 int len = 0;
Wolfgang Denk7bc5ee02005-08-26 01:36:03 +0200201 volatile ushort *s;
wdenkfe8c2802002-11-03 00:38:21 +0000202
Wolfgang Denk85eb5ca2007-08-14 09:47:27 +0200203#ifdef CONFIG_MCAST_TFTP
David Updegraff53a5c422007-06-11 10:41:07 -0500204 /* Multicast TFTP.. non-MasterClients do not ACK data. */
Wolfgang Denk85eb5ca2007-08-14 09:47:27 +0200205 if (Multicast
206 && (TftpState == STATE_DATA)
207 && (MasterClient == 0))
David Updegraff53a5c422007-06-11 10:41:07 -0500208 return;
209#endif
wdenkfe8c2802002-11-03 00:38:21 +0000210 /*
211 * We will always be sending some sort of packet, so
212 * cobble together the packet headers now.
213 */
wdenka3d991b2004-04-15 21:48:45 +0000214 pkt = NetTxPacket + NetEthHdrSize() + IP_HDR_SIZE;
wdenkfe8c2802002-11-03 00:38:21 +0000215
216 switch (TftpState) {
217
218 case STATE_RRQ:
219 xp = pkt;
Wolfgang Denk7bc5ee02005-08-26 01:36:03 +0200220 s = (ushort *)pkt;
221 *s++ = htons(TFTP_RRQ);
222 pkt = (uchar *)s;
Luca Ceresolic718b142011-05-14 05:49:57 +0000223 strcpy((char *)pkt, tftp_filename);
wdenkfe8c2802002-11-03 00:38:21 +0000224 pkt += strlen(tftp_filename) + 1;
Luca Ceresolic718b142011-05-14 05:49:57 +0000225 strcpy((char *)pkt, "octet");
wdenkfe8c2802002-11-03 00:38:21 +0000226 pkt += 5 /*strlen("octet")*/ + 1;
Luca Ceresolic718b142011-05-14 05:49:57 +0000227 strcpy((char *)pkt, "timeout");
wdenkfbe4b5c2003-10-06 21:55:32 +0000228 pkt += 7 /*strlen("timeout")*/ + 1;
Wolfgang Denkc96f86e2010-01-17 23:55:53 +0100229 sprintf((char *)pkt, "%lu", TftpTimeoutMSecs / 1000);
Robin Getz0ebf04c2009-07-23 03:01:03 -0400230 debug("send option \"timeout %s\"\n", (char *)pkt);
wdenkfbe4b5c2003-10-06 21:55:32 +0000231 pkt += strlen((char *)pkt) + 1;
Robin Getz4fccb812009-08-20 10:50:20 -0400232#ifdef CONFIG_TFTP_TSIZE
233 memcpy((char *)pkt, "tsize\0000\0", 8);
234 pkt += 8;
235#endif
David Updegraff53a5c422007-06-11 10:41:07 -0500236 /* try for more effic. blk size */
Luca Ceresolic718b142011-05-14 05:49:57 +0000237 pkt += sprintf((char *)pkt, "blksize%c%d%c",
238 0, TftpBlkSizeOption, 0);
Wolfgang Denk85eb5ca2007-08-14 09:47:27 +0200239#ifdef CONFIG_MCAST_TFTP
David Updegraff53a5c422007-06-11 10:41:07 -0500240 /* Check all preconditions before even trying the option */
Wolfgang Denk85eb5ca2007-08-14 09:47:27 +0200241 if (!ProhibitMcast
Luca Ceresolic718b142011-05-14 05:49:57 +0000242 && (Bitmap = malloc(Mapsize))
David Updegraff53a5c422007-06-11 10:41:07 -0500243 && eth_get_dev()->mcast) {
244 free(Bitmap);
Luca Ceresolic718b142011-05-14 05:49:57 +0000245 Bitmap = NULL;
246 pkt += sprintf((char *)pkt, "multicast%c%c", 0, 0);
David Updegraff53a5c422007-06-11 10:41:07 -0500247 }
248#endif /* CONFIG_MCAST_TFTP */
wdenkfe8c2802002-11-03 00:38:21 +0000249 len = pkt - xp;
250 break;
251
wdenkfbe4b5c2003-10-06 21:55:32 +0000252 case STATE_OACK:
David Updegraff53a5c422007-06-11 10:41:07 -0500253#ifdef CONFIG_MCAST_TFTP
254 /* My turn! Start at where I need blocks I missed.*/
255 if (Multicast)
Luca Ceresolic718b142011-05-14 05:49:57 +0000256 TftpBlock = ext2_find_next_zero_bit(Bitmap,
257 (Mapsize*8), 0);
David Updegraff53a5c422007-06-11 10:41:07 -0500258 /*..falling..*/
259#endif
260 case STATE_DATA:
wdenkfe8c2802002-11-03 00:38:21 +0000261 xp = pkt;
Wolfgang Denk7bc5ee02005-08-26 01:36:03 +0200262 s = (ushort *)pkt;
263 *s++ = htons(TFTP_ACK);
264 *s++ = htons(TftpBlock);
265 pkt = (uchar *)s;
wdenkfe8c2802002-11-03 00:38:21 +0000266 len = pkt - xp;
267 break;
268
269 case STATE_TOO_LARGE:
270 xp = pkt;
Wolfgang Denk7bc5ee02005-08-26 01:36:03 +0200271 s = (ushort *)pkt;
272 *s++ = htons(TFTP_ERROR);
273 *s++ = htons(3);
274 pkt = (uchar *)s;
Luca Ceresolic718b142011-05-14 05:49:57 +0000275 strcpy((char *)pkt, "File too large");
wdenkfe8c2802002-11-03 00:38:21 +0000276 pkt += 14 /*strlen("File too large")*/ + 1;
277 len = pkt - xp;
278 break;
279
280 case STATE_BAD_MAGIC:
281 xp = pkt;
Wolfgang Denk7bc5ee02005-08-26 01:36:03 +0200282 s = (ushort *)pkt;
283 *s++ = htons(TFTP_ERROR);
284 *s++ = htons(2);
285 pkt = (uchar *)s;
Luca Ceresolic718b142011-05-14 05:49:57 +0000286 strcpy((char *)pkt, "File has bad magic");
wdenkfe8c2802002-11-03 00:38:21 +0000287 pkt += 18 /*strlen("File has bad magic")*/ + 1;
288 len = pkt - xp;
289 break;
290 }
291
Luca Ceresoli2f094132011-05-14 05:49:56 +0000292 NetSendUDPPacket(NetServerEther, TftpServerIP, TftpServerPort,
293 TftpOurPort, len);
wdenkfe8c2802002-11-03 00:38:21 +0000294}
295
296
297static void
Luca Ceresoli03eb1292011-04-18 06:19:50 +0000298TftpHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src,
299 unsigned len)
wdenkfe8c2802002-11-03 00:38:21 +0000300{
301 ushort proto;
Wolfgang Denk7bc5ee02005-08-26 01:36:03 +0200302 ushort *s;
Wolfgang Denkff13ac82007-08-30 14:42:15 +0200303 int i;
wdenkfe8c2802002-11-03 00:38:21 +0000304
305 if (dest != TftpOurPort) {
David Updegraff53a5c422007-06-11 10:41:07 -0500306#ifdef CONFIG_MCAST_TFTP
Wolfgang Denk85eb5ca2007-08-14 09:47:27 +0200307 if (Multicast
David Updegraff53a5c422007-06-11 10:41:07 -0500308 && (!Mcast_port || (dest != Mcast_port)))
309#endif
wdenkfe8c2802002-11-03 00:38:21 +0000310 return;
311 }
Luca Ceresoli7bc325a2011-05-14 05:50:00 +0000312 if (TftpState != STATE_RRQ && src != TftpServerPort)
wdenkfe8c2802002-11-03 00:38:21 +0000313 return;
wdenkfe8c2802002-11-03 00:38:21 +0000314
Luca Ceresoli7bc325a2011-05-14 05:50:00 +0000315 if (len < 2)
wdenkfe8c2802002-11-03 00:38:21 +0000316 return;
wdenkfe8c2802002-11-03 00:38:21 +0000317 len -= 2;
318 /* warning: don't use increment (++) in ntohs() macros!! */
Wolfgang Denk7bc5ee02005-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
wdenkfbe4b5c2003-10-06 21:55:32 +0000331 case TFTP_OACK:
Wolfgang Denkd3717082009-08-10 09:59:10 +0200332 debug("Got OACK: %s %s\n",
333 pkt,
334 pkt + strlen((char *)pkt) + 1);
wdenkfbe4b5c2003-10-06 21:55:32 +0000335 TftpState = STATE_OACK;
336 TftpServerPort = src;
Wolfgang Denk60174742007-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 Ceresolic718b142011-05-14 05:49:57 +0000342 for (i = 0; i+8 < len; i++) {
Luca Ceresoli2e320252011-05-14 05:49:58 +0000343 if (strcmp((char *)pkt+i, "blksize") == 0) {
Wolfgang Denkff13ac82007-08-30 14:42:15 +0200344 TftpBlkSize = (unsigned short)
Luca Ceresoli2e320252011-05-14 05:49:58 +0000345 simple_strtoul((char *)pkt+i+8, NULL,
Luca Ceresolic718b142011-05-14 05:49:57 +0000346 10);
Robin Getz0ebf04c2009-07-23 03:01:03 -0400347 debug("Blocksize ack: %s, %d\n",
Luca Ceresoli2e320252011-05-14 05:49:58 +0000348 (char *)pkt+i+8, TftpBlkSize);
Wolfgang Denkff13ac82007-08-30 14:42:15 +0200349 }
Robin Getz4fccb812009-08-20 10:50:20 -0400350#ifdef CONFIG_TFTP_TSIZE
Luca Ceresoli2e320252011-05-14 05:49:58 +0000351 if (strcmp((char *)pkt+i, "tsize") == 0) {
352 TftpTsize = simple_strtoul((char *)pkt+i+6,
Luca Ceresoli2f094132011-05-14 05:49:56 +0000353 NULL, 10);
Robin Getz4fccb812009-08-20 10:50:20 -0400354 debug("size = %s, %d\n",
Luca Ceresoli2e320252011-05-14 05:49:58 +0000355 (char *)pkt+i+6, TftpTsize);
Robin Getz4fccb812009-08-20 10:50:20 -0400356 }
357#endif
David Updegraff53a5c422007-06-11 10:41:07 -0500358 }
359#ifdef CONFIG_MCAST_TFTP
Luca Ceresolic718b142011-05-14 05:49:57 +0000360 parse_multicast_oack((char *)pkt, len-1);
Wolfgang Denk85eb5ca2007-08-14 09:47:27 +0200361 if ((Multicast) && (!MasterClient))
David Updegraff53a5c422007-06-11 10:41:07 -0500362 TftpState = STATE_DATA; /* passive.. */
363 else
364#endif
Luca Ceresolic718b142011-05-14 05:49:57 +0000365 TftpSend(); /* Send ACK */
wdenkfbe4b5c2003-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);
wdenk3f85ce22004-02-23 16:11:30 +0000372
373 /*
wdenkcd0a9de2004-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.
wdenk3f85ce22004-02-23 16:11:30 +0000378 */
379 if (TftpBlock == 0) {
380 TftpBlockWrap++;
Luca Ceresoli2f094132011-05-14 05:49:56 +0000381 TftpBlockWrapOffset +=
382 TftpBlkSize * TFTP_SEQUENCE_SIZE;
Luca Ceresolic718b142011-05-14 05:49:57 +0000383 printf("\n\t %lu MB received\n\t ",
Luca Ceresoli2f094132011-05-14 05:49:56 +0000384 TftpBlockWrapOffset>>20);
Robin Getz4fccb812009-08-20 10:50:20 -0400385 }
386#ifdef CONFIG_TFTP_TSIZE
387 else if (TftpTsize) {
Luca Ceresoli2f094132011-05-14 05:49:56 +0000388 while (TftpNumchars <
389 NetBootFileXferSize * 50 / TftpTsize) {
Robin Getz4fccb812009-08-20 10:50:20 -0400390 putc('#');
391 TftpNumchars++;
392 }
393 }
394#endif
395 else {
Luca Ceresoli7bc325a2011-05-14 05:50:00 +0000396 if (((TftpBlock - 1) % 10) == 0)
Luca Ceresolic718b142011-05-14 05:49:57 +0000397 putc('#');
Luca Ceresoli7bc325a2011-05-14 05:50:00 +0000398 else if ((TftpBlock % (10 * HASHES_PER_LINE)) == 0)
Luca Ceresolic718b142011-05-14 05:49:57 +0000399 puts("\n\t ");
wdenkfe8c2802002-11-03 00:38:21 +0000400 }
401
Robin Getz0ebf04c2009-07-23 03:01:03 -0400402 if (TftpState == STATE_RRQ)
403 debug("Server did not acknowledge timeout option!\n");
wdenkfbe4b5c2003-10-06 21:55:32 +0000404
405 if (TftpState == STATE_RRQ || TftpState == STATE_OACK) {
wdenk3f85ce22004-02-23 16:11:30 +0000406 /* first block received */
wdenkfe8c2802002-11-03 00:38:21 +0000407 TftpState = STATE_DATA;
408 TftpServerPort = src;
409 TftpLastBlock = 0;
wdenk3f85ce22004-02-23 16:11:30 +0000410 TftpBlockWrap = 0;
411 TftpBlockWrapOffset = 0;
wdenkfe8c2802002-11-03 00:38:21 +0000412
David Updegraff53a5c422007-06-11 10:41:07 -0500413#ifdef CONFIG_MCAST_TFTP
414 if (Multicast) { /* start!=1 common if mcast */
415 TftpLastBlock = TftpBlock - 1;
416 } else
417#endif
wdenkfe8c2802002-11-03 00:38:21 +0000418 if (TftpBlock != 1) { /* Assertion */
Luca Ceresolic718b142011-05-14 05:49:57 +0000419 printf("\nTFTP error: "
420 "First block is not block 1 (%ld)\n"
421 "Starting again\n\n",
wdenkfe8c2802002-11-03 00:38:21 +0000422 TftpBlock);
Luca Ceresolic718b142011-05-14 05:49:57 +0000423 NetStartAgain();
wdenkfe8c2802002-11-03 00:38:21 +0000424 break;
425 }
426 }
427
428 if (TftpBlock == TftpLastBlock) {
429 /*
430 * Same block again; ignore it.
431 */
432 break;
433 }
434
435 TftpLastBlock = TftpBlock;
Bartlomiej Siekae83cc062008-10-01 15:26:29 +0200436 TftpTimeoutCountMax = TIMEOUT_COUNT;
Luca Ceresolic718b142011-05-14 05:49:57 +0000437 NetSetTimeout(TftpTimeoutMSecs, TftpTimeout);
wdenkfe8c2802002-11-03 00:38:21 +0000438
Luca Ceresolic718b142011-05-14 05:49:57 +0000439 store_block(TftpBlock - 1, pkt + 2, len);
wdenkfe8c2802002-11-03 00:38:21 +0000440
441 /*
442 * Acknoledge the block just received, which will prompt
443 * the server for the next one.
444 */
David Updegraff53a5c422007-06-11 10:41:07 -0500445#ifdef CONFIG_MCAST_TFTP
Wolfgang Denk85eb5ca2007-08-14 09:47:27 +0200446 /* if I am the MasterClient, actively calculate what my next
447 * needed block is; else I'm passive; not ACKING
Jean-Christophe PLAGNIOL-VILLARDa93907c2008-01-18 01:14:03 +0100448 */
David Updegraff53a5c422007-06-11 10:41:07 -0500449 if (Multicast) {
450 if (len < TftpBlkSize) {
451 TftpEndingBlock = TftpBlock;
452 } else if (MasterClient) {
Wolfgang Denk85eb5ca2007-08-14 09:47:27 +0200453 TftpBlock = PrevBitmapHole =
David Updegraff53a5c422007-06-11 10:41:07 -0500454 ext2_find_next_zero_bit(
455 Bitmap,
456 (Mapsize*8),
457 PrevBitmapHole);
458 if (TftpBlock > ((Mapsize*8) - 1)) {
Luca Ceresolic718b142011-05-14 05:49:57 +0000459 printf("tftpfile too big\n");
David Updegraff53a5c422007-06-11 10:41:07 -0500460 /* try to double it and retry */
Luca Ceresolic718b142011-05-14 05:49:57 +0000461 Mapsize <<= 1;
David Updegraff53a5c422007-06-11 10:41:07 -0500462 mcast_cleanup();
Luca Ceresolic718b142011-05-14 05:49:57 +0000463 NetStartAgain();
David Updegraff53a5c422007-06-11 10:41:07 -0500464 return;
465 }
466 TftpLastBlock = TftpBlock;
467 }
468 }
469#endif
Luca Ceresolic718b142011-05-14 05:49:57 +0000470 TftpSend();
wdenkfe8c2802002-11-03 00:38:21 +0000471
David Updegraff53a5c422007-06-11 10:41:07 -0500472#ifdef CONFIG_MCAST_TFTP
473 if (Multicast) {
474 if (MasterClient && (TftpBlock >= TftpEndingBlock)) {
Luca Ceresolic718b142011-05-14 05:49:57 +0000475 puts("\nMulticast tftp done\n");
David Updegraff53a5c422007-06-11 10:41:07 -0500476 mcast_cleanup();
477 NetState = NETLOOP_SUCCESS;
Wolfgang Denk85eb5ca2007-08-14 09:47:27 +0200478 }
David Updegraff53a5c422007-06-11 10:41:07 -0500479 }
480 else
481#endif
482 if (len < TftpBlkSize) {
wdenkfe8c2802002-11-03 00:38:21 +0000483 /*
484 * We received the whole thing. Try to
485 * run it.
486 */
Robin Getz4fccb812009-08-20 10:50:20 -0400487#ifdef CONFIG_TFTP_TSIZE
Luca Ceresoli2f094132011-05-14 05:49:56 +0000488 /* Print hash marks for the last packet received */
Robin Getz4fccb812009-08-20 10:50:20 -0400489 while (TftpTsize && TftpNumchars < 49) {
490 putc('#');
491 TftpNumchars++;
492 }
493#endif
Luca Ceresolic718b142011-05-14 05:49:57 +0000494 puts("\ndone\n");
wdenkfe8c2802002-11-03 00:38:21 +0000495 NetState = NETLOOP_SUCCESS;
496 }
497 break;
498
499 case TFTP_ERROR:
Luca Ceresolic718b142011-05-14 05:49:57 +0000500 printf("\nTFTP error: '%s' (%d)\n",
501 pkt + 2, ntohs(*(ushort *)pkt));
Remy Bohmeraafda382009-10-28 22:13:40 +0100502
503 switch (ntohs(*(ushort *)pkt)) {
504 case TFTP_ERR_FILE_NOT_FOUND:
505 case TFTP_ERR_ACCESS_DENIED:
506 puts("Not retrying...\n");
507 eth_halt();
508 NetState = NETLOOP_FAIL;
509 break;
510 case TFTP_ERR_UNDEFINED:
511 case TFTP_ERR_DISK_FULL:
512 case TFTP_ERR_UNEXPECTED_OPCODE:
513 case TFTP_ERR_UNKNOWN_TRANSFER_ID:
514 case TFTP_ERR_FILE_ALREADY_EXISTS:
515 default:
516 puts("Starting again\n\n");
David Updegraff53a5c422007-06-11 10:41:07 -0500517#ifdef CONFIG_MCAST_TFTP
Remy Bohmeraafda382009-10-28 22:13:40 +0100518 mcast_cleanup();
David Updegraff53a5c422007-06-11 10:41:07 -0500519#endif
Remy Bohmeraafda382009-10-28 22:13:40 +0100520 NetStartAgain();
521 break;
522 }
wdenkfe8c2802002-11-03 00:38:21 +0000523 break;
524 }
525}
526
527
528static void
Luca Ceresolic718b142011-05-14 05:49:57 +0000529TftpTimeout(void)
wdenkfe8c2802002-11-03 00:38:21 +0000530{
Bartlomiej Siekae83cc062008-10-01 15:26:29 +0200531 if (++TftpTimeoutCount > TftpTimeoutCountMax) {
Luca Ceresolic718b142011-05-14 05:49:57 +0000532 puts("\nRetry count exceeded; starting again\n");
David Updegraff53a5c422007-06-11 10:41:07 -0500533#ifdef CONFIG_MCAST_TFTP
534 mcast_cleanup();
535#endif
Luca Ceresolic718b142011-05-14 05:49:57 +0000536 NetStartAgain();
wdenkfe8c2802002-11-03 00:38:21 +0000537 } else {
Luca Ceresolic718b142011-05-14 05:49:57 +0000538 puts("T ");
539 NetSetTimeout(TftpTimeoutMSecs, TftpTimeout);
540 TftpSend();
wdenkfe8c2802002-11-03 00:38:21 +0000541 }
542}
543
544
545void
Luca Ceresolic718b142011-05-14 05:49:57 +0000546TftpStart(void)
wdenkfe8c2802002-11-03 00:38:21 +0000547{
Wolfgang Denkecb0ccd2005-09-24 22:37:32 +0200548 char *ep; /* Environment pointer */
Alessandro Rubini89ba81d2009-08-07 13:59:06 +0200549
Wolfgang Denkc96f86e2010-01-17 23:55:53 +0100550 /*
551 * Allow the user to choose TFTP blocksize and timeout.
552 * TFTP protocol has a minimal timeout of 1 second.
553 */
Luca Ceresoli2cb53602011-05-14 05:49:59 +0000554 ep = getenv("tftpblocksize");
555 if (ep != NULL)
Alessandro Rubini89ba81d2009-08-07 13:59:06 +0200556 TftpBlkSizeOption = simple_strtol(ep, NULL, 10);
Wolfgang Denkc96f86e2010-01-17 23:55:53 +0100557
Luca Ceresoli2cb53602011-05-14 05:49:59 +0000558 ep = getenv("tftptimeout");
559 if (ep != NULL)
Wolfgang Denkc96f86e2010-01-17 23:55:53 +0100560 TftpTimeoutMSecs = simple_strtol(ep, NULL, 10);
561
562 if (TftpTimeoutMSecs < 1000) {
563 printf("TFTP timeout (%ld ms) too low, "
564 "set minimum = 1000 ms\n",
565 TftpTimeoutMSecs);
566 TftpTimeoutMSecs = 1000;
567 }
568
569 debug("TFTP blocksize = %i, timeout = %ld ms\n",
570 TftpBlkSizeOption, TftpTimeoutMSecs);
Wolfgang Denkecb0ccd2005-09-24 22:37:32 +0200571
Jean-Christophe PLAGNIOL-VILLARDa93907c2008-01-18 01:14:03 +0100572 TftpServerIP = NetServerIP;
wdenkfe8c2802002-11-03 00:38:21 +0000573 if (BootFile[0] == '\0') {
wdenkfe8c2802002-11-03 00:38:21 +0000574 sprintf(default_filename, "%02lX%02lX%02lX%02lX.img",
Wolfgang Denkc43352c2005-08-04 01:09:44 +0200575 NetOurIP & 0xFF,
576 (NetOurIP >> 8) & 0xFF,
577 (NetOurIP >> 16) & 0xFF,
Luca Ceresolic718b142011-05-14 05:49:57 +0000578 (NetOurIP >> 24) & 0xFF);
Jean-Christophe PLAGNIOL-VILLARDa93907c2008-01-18 01:14:03 +0100579
580 strncpy(tftp_filename, default_filename, MAX_LEN);
581 tftp_filename[MAX_LEN-1] = 0;
wdenkfe8c2802002-11-03 00:38:21 +0000582
Luca Ceresolic718b142011-05-14 05:49:57 +0000583 printf("*** Warning: no boot file name; using '%s'\n",
wdenkfe8c2802002-11-03 00:38:21 +0000584 tftp_filename);
585 } else {
Luca Ceresolic718b142011-05-14 05:49:57 +0000586 char *p = strchr(BootFile, ':');
Jean-Christophe PLAGNIOL-VILLARDa93907c2008-01-18 01:14:03 +0100587
588 if (p == NULL) {
589 strncpy(tftp_filename, BootFile, MAX_LEN);
590 tftp_filename[MAX_LEN-1] = 0;
591 } else {
Luca Ceresolic718b142011-05-14 05:49:57 +0000592 TftpServerIP = string_to_ip(BootFile);
Peter Tyser6a86bb62008-12-01 16:29:38 -0600593 strncpy(tftp_filename, p + 1, MAX_LEN);
Jean-Christophe PLAGNIOL-VILLARDa93907c2008-01-18 01:14:03 +0100594 tftp_filename[MAX_LEN-1] = 0;
595 }
wdenkfe8c2802002-11-03 00:38:21 +0000596 }
597
wdenk5bb226e2003-11-17 21:14:37 +0000598#if defined(CONFIG_NET_MULTI)
Luca Ceresolic718b142011-05-14 05:49:57 +0000599 printf("Using %s device\n", eth_get_name());
wdenk5bb226e2003-11-17 21:14:37 +0000600#endif
Mike Frysingerb6446b62009-02-17 00:00:53 -0500601 printf("TFTP from server %pI4"
602 "; our IP address is %pI4", &TftpServerIP, &NetOurIP);
wdenkfe8c2802002-11-03 00:38:21 +0000603
604 /* Check if we need to send across this subnet */
605 if (NetOurGatewayIP && NetOurSubnetMask) {
Jean-Christophe PLAGNIOL-VILLARDa93907c2008-01-18 01:14:03 +0100606 IPaddr_t OurNet = NetOurIP & NetOurSubnetMask;
607 IPaddr_t ServerNet = TftpServerIP & NetOurSubnetMask;
wdenkfe8c2802002-11-03 00:38:21 +0000608
Mike Frysingerb6446b62009-02-17 00:00:53 -0500609 if (OurNet != ServerNet)
610 printf("; sending through gateway %pI4", &NetOurGatewayIP);
wdenkfe8c2802002-11-03 00:38:21 +0000611 }
Luca Ceresolic718b142011-05-14 05:49:57 +0000612 putc('\n');
wdenkfe8c2802002-11-03 00:38:21 +0000613
Luca Ceresolic718b142011-05-14 05:49:57 +0000614 printf("Filename '%s'.", tftp_filename);
wdenkfe8c2802002-11-03 00:38:21 +0000615
616 if (NetBootFileSize) {
Luca Ceresolic718b142011-05-14 05:49:57 +0000617 printf(" Size is 0x%x Bytes = ", NetBootFileSize<<9);
618 print_size(NetBootFileSize<<9, "");
wdenkfe8c2802002-11-03 00:38:21 +0000619 }
620
Luca Ceresolic718b142011-05-14 05:49:57 +0000621 putc('\n');
wdenkfe8c2802002-11-03 00:38:21 +0000622
Luca Ceresolic718b142011-05-14 05:49:57 +0000623 printf("Load address: 0x%lx\n", load_addr);
wdenkfe8c2802002-11-03 00:38:21 +0000624
Luca Ceresolic718b142011-05-14 05:49:57 +0000625 puts("Loading: *\b");
wdenkfe8c2802002-11-03 00:38:21 +0000626
Bartlomiej Siekae83cc062008-10-01 15:26:29 +0200627 TftpTimeoutCountMax = TftpRRQTimeoutCountMax;
628
Luca Ceresolic718b142011-05-14 05:49:57 +0000629 NetSetTimeout(TftpTimeoutMSecs, TftpTimeout);
630 NetSetHandler(TftpHandler);
wdenkfe8c2802002-11-03 00:38:21 +0000631
632 TftpServerPort = WELL_KNOWN_PORT;
633 TftpTimeoutCount = 0;
634 TftpState = STATE_RRQ;
Wolfgang Denkecb0ccd2005-09-24 22:37:32 +0200635 /* Use a pseudo-random port unless a specific port is set */
wdenkfe8c2802002-11-03 00:38:21 +0000636 TftpOurPort = 1024 + (get_timer(0) % 3072);
David Updegraff53a5c422007-06-11 10:41:07 -0500637
Wolfgang Denkecb0ccd2005-09-24 22:37:32 +0200638#ifdef CONFIG_TFTP_PORT
Luca Ceresoli2cb53602011-05-14 05:49:59 +0000639 ep = getenv("tftpdstp");
Luca Ceresoli7bc325a2011-05-14 05:50:00 +0000640 if (ep != NULL)
Wolfgang Denk28cb9372005-09-24 23:25:46 +0200641 TftpServerPort = simple_strtol(ep, NULL, 10);
Luca Ceresoli2cb53602011-05-14 05:49:59 +0000642 ep = getenv("tftpsrcp");
Luca Ceresoli7bc325a2011-05-14 05:50:00 +0000643 if (ep != NULL)
Luca Ceresolic718b142011-05-14 05:49:57 +0000644 TftpOurPort = simple_strtol(ep, NULL, 10);
Wolfgang Denkecb0ccd2005-09-24 22:37:32 +0200645#endif
wdenkfbe4b5c2003-10-06 21:55:32 +0000646 TftpBlock = 0;
wdenkfe8c2802002-11-03 00:38:21 +0000647
wdenk73a8b272003-06-05 19:27:42 +0000648 /* zero out server ether in case the server ip has changed */
649 memset(NetServerEther, 0, 6);
David Updegraff53a5c422007-06-11 10:41:07 -0500650 /* Revert TftpBlkSize to dflt */
651 TftpBlkSize = TFTP_BLOCK_SIZE;
652#ifdef CONFIG_MCAST_TFTP
Jean-Christophe PLAGNIOL-VILLARDa93907c2008-01-18 01:14:03 +0100653 mcast_cleanup();
David Updegraff53a5c422007-06-11 10:41:07 -0500654#endif
Robin Getz4fccb812009-08-20 10:50:20 -0400655#ifdef CONFIG_TFTP_TSIZE
656 TftpTsize = 0;
657 TftpNumchars = 0;
658#endif
wdenk73a8b272003-06-05 19:27:42 +0000659
Luca Ceresolic718b142011-05-14 05:49:57 +0000660 TftpSend();
wdenkfe8c2802002-11-03 00:38:21 +0000661}
662
David Updegraff53a5c422007-06-11 10:41:07 -0500663#ifdef CONFIG_MCAST_TFTP
664/* Credits: atftp project.
665 */
666
667/* pick up BcastAddr, Port, and whether I am [now] the master-client. *
668 * Frame:
669 * +-------+-----------+---+-------~~-------+---+
670 * | opc | multicast | 0 | addr, port, mc | 0 |
671 * +-------+-----------+---+-------~~-------+---+
672 * The multicast addr/port becomes what I listen to, and if 'mc' is '1' then
673 * I am the new master-client so must send ACKs to DataBlocks. If I am not
674 * master-client, I'm a passive client, gathering what DataBlocks I may and
675 * making note of which ones I got in my bitmask.
676 * In theory, I never go from master->passive..
677 * .. this comes in with pkt already pointing just past opc
678 */
679static void parse_multicast_oack(char *pkt, int len)
680{
Luca Ceresolic718b142011-05-14 05:49:57 +0000681 int i;
682 IPaddr_t addr;
683 char *mc_adr, *port, *mc;
David Updegraff53a5c422007-06-11 10:41:07 -0500684
Luca Ceresolic718b142011-05-14 05:49:57 +0000685 mc_adr = port = mc = NULL;
David Updegraff53a5c422007-06-11 10:41:07 -0500686 /* march along looking for 'multicast\0', which has to start at least
687 * 14 bytes back from the end.
688 */
Luca Ceresolic718b142011-05-14 05:49:57 +0000689 for (i = 0; i < len-14; i++)
690 if (strcmp(pkt+i, "multicast") == 0)
David Updegraff53a5c422007-06-11 10:41:07 -0500691 break;
692 if (i >= (len-14)) /* non-Multicast OACK, ign. */
693 return;
Wolfgang Denk85eb5ca2007-08-14 09:47:27 +0200694
Luca Ceresolic718b142011-05-14 05:49:57 +0000695 i += 10; /* strlen multicast */
David Updegraff53a5c422007-06-11 10:41:07 -0500696 mc_adr = pkt+i;
Luca Ceresolic718b142011-05-14 05:49:57 +0000697 for (; i < len; i++) {
David Updegraff53a5c422007-06-11 10:41:07 -0500698 if (*(pkt+i) == ',') {
699 *(pkt+i) = '\0';
700 if (port) {
701 mc = pkt+i+1;
702 break;
703 } else {
704 port = pkt+i+1;
705 }
706 }
707 }
Luca Ceresoli6d2231e2011-05-14 05:50:01 +0000708 if (!port || !mc_adr || !mc)
709 return;
David Updegraff53a5c422007-06-11 10:41:07 -0500710 if (Multicast && MasterClient) {
Luca Ceresolic718b142011-05-14 05:49:57 +0000711 printf("I got a OACK as master Client, WRONG!\n");
David Updegraff53a5c422007-06-11 10:41:07 -0500712 return;
713 }
714 /* ..I now accept packets destined for this MCAST addr, port */
715 if (!Multicast) {
716 if (Bitmap) {
Luca Ceresolic718b142011-05-14 05:49:57 +0000717 printf("Internal failure! no mcast.\n");
David Updegraff53a5c422007-06-11 10:41:07 -0500718 free(Bitmap);
Luca Ceresolic718b142011-05-14 05:49:57 +0000719 Bitmap = NULL;
720 ProhibitMcast = 1;
David Updegraff53a5c422007-06-11 10:41:07 -0500721 return ;
722 }
723 /* I malloc instead of pre-declare; so that if the file ends
Wolfgang Denk85eb5ca2007-08-14 09:47:27 +0200724 * up being too big for this bitmap I can retry
725 */
Luca Ceresoli2cb53602011-05-14 05:49:59 +0000726 Bitmap = malloc(Mapsize);
727 if (!Bitmap) {
Luca Ceresolic718b142011-05-14 05:49:57 +0000728 printf("No Bitmap, no multicast. Sorry.\n");
729 ProhibitMcast = 1;
David Updegraff53a5c422007-06-11 10:41:07 -0500730 return;
731 }
Luca Ceresolic718b142011-05-14 05:49:57 +0000732 memset(Bitmap, 0, Mapsize);
David Updegraff53a5c422007-06-11 10:41:07 -0500733 PrevBitmapHole = 0;
734 Multicast = 1;
735 }
736 addr = string_to_ip(mc_adr);
737 if (Mcast_addr != addr) {
738 if (Mcast_addr)
739 eth_mcast_join(Mcast_addr, 0);
Luca Ceresoli2cb53602011-05-14 05:49:59 +0000740 Mcast_addr = addr;
741 if (eth_mcast_join(Mcast_addr, 1)) {
Luca Ceresolic718b142011-05-14 05:49:57 +0000742 printf("Fail to set mcast, revert to TFTP\n");
743 ProhibitMcast = 1;
David Updegraff53a5c422007-06-11 10:41:07 -0500744 mcast_cleanup();
745 NetStartAgain();
746 }
747 }
Luca Ceresolic718b142011-05-14 05:49:57 +0000748 MasterClient = (unsigned char)simple_strtoul((char *)mc, NULL, 10);
749 Mcast_port = (unsigned short)simple_strtoul(port, NULL, 10);
750 printf("Multicast: %s:%d [%d]\n", mc_adr, Mcast_port, MasterClient);
David Updegraff53a5c422007-06-11 10:41:07 -0500751 return;
752}
753
754#endif /* Multicast TFTP */