]> git.ipfire.org Git - people/ms/u-boot.git/blame - net/tftp.c
TFTP: add tftpsrv command
[people/ms/u-boot.git] / net / tftp.c
CommitLineData
fe8c2806 1/*
2f09413f
LC
2 * Copyright 1994, 1995, 2000 Neil Russell.
3 * (See License)
4 * Copyright 2000, 2001 DENX Software Engineering, Wolfgang Denk, wd@denx.de
e59e3562
LC
5 * Copyright 2011 Comelit Group SpA,
6 * Luca Ceresoli <luca.ceresoli@comelit.it>
fe8c2806
WD
7 */
8
9#include <common.h>
10#include <command.h>
11#include <net.h>
12#include "tftp.h"
13#include "bootp.h"
14
2f09413f
LC
15/* Well known TFTP port # */
16#define WELL_KNOWN_PORT 69
17/* Millisecs to timeout for lost pkt */
18#define TIMEOUT 5000UL
fe8c2806 19#ifndef CONFIG_NET_RETRY_COUNT
2f09413f
LC
20/* # of timeouts before giving up */
21# define TIMEOUT_COUNT 10
fe8c2806
WD
22#else
23# define TIMEOUT_COUNT (CONFIG_NET_RETRY_COUNT * 2)
24#endif
2f09413f
LC
25/* Number of "loading" hashes per line (for checking the image size) */
26#define HASHES_PER_LINE 65
fe8c2806
WD
27
28/*
29 * TFTP operations.
30 */
31#define TFTP_RRQ 1
32#define TFTP_WRQ 2
33#define TFTP_DATA 3
34#define TFTP_ACK 4
35#define TFTP_ERROR 5
fbe4b5cb 36#define TFTP_OACK 6
fe8c2806 37
e83cc063
BS
38static ulong TftpTimeoutMSecs = TIMEOUT;
39static int TftpTimeoutCountMax = TIMEOUT_COUNT;
40
41/*
42 * These globals govern the timeout behavior when attempting a connection to a
43 * TFTP server. TftpRRQTimeoutMSecs specifies the number of milliseconds to
44 * wait for the server to respond to initial connection. Second global,
45 * TftpRRQTimeoutCountMax, gives the number of such connection retries.
46 * TftpRRQTimeoutCountMax must be non-negative and TftpRRQTimeoutMSecs must be
47 * positive. The globals are meant to be set (and restored) by code needing
48 * non-standard timeout behavior when initiating a TFTP transfer.
49 */
50ulong TftpRRQTimeoutMSecs = TIMEOUT;
51int TftpRRQTimeoutCountMax = TIMEOUT_COUNT;
52
aafda38f
RB
53enum {
54 TFTP_ERR_UNDEFINED = 0,
55 TFTP_ERR_FILE_NOT_FOUND = 1,
56 TFTP_ERR_ACCESS_DENIED = 2,
57 TFTP_ERR_DISK_FULL = 3,
58 TFTP_ERR_UNEXPECTED_OPCODE = 4,
59 TFTP_ERR_UNKNOWN_TRANSFER_ID = 5,
60 TFTP_ERR_FILE_ALREADY_EXISTS = 6,
61};
62
20478cea 63static IPaddr_t TftpRemoteIP;
2f09413f 64/* The UDP port at their end */
20478cea 65static int TftpRemotePort;
2f09413f
LC
66/* The UDP port at our end */
67static int TftpOurPort;
fe8c2806 68static int TftpTimeoutCount;
2f09413f
LC
69/* packet sequence number */
70static ulong TftpBlock;
71/* last packet sequence number received */
72static ulong TftpLastBlock;
73/* count of sequence number wraparounds */
74static ulong TftpBlockWrap;
75/* memory offset due to wrapping */
76static ulong TftpBlockWrapOffset;
fe8c2806 77static int TftpState;
4fccb818 78#ifdef CONFIG_TFTP_TSIZE
2f09413f
LC
79/* The file size reported by the server */
80static int TftpTsize;
81/* The number of hashes we printed */
82static short TftpNumchars;
4fccb818 83#endif
3f85ce27 84
e3fb0abe 85#define STATE_SEND_RRQ 1
fe8c2806
WD
86#define STATE_DATA 2
87#define STATE_TOO_LARGE 3
88#define STATE_BAD_MAGIC 4
fbe4b5cb 89#define STATE_OACK 5
e59e3562 90#define STATE_RECV_WRQ 6
fe8c2806 91
2f09413f
LC
92/* default TFTP block size */
93#define TFTP_BLOCK_SIZE 512
94/* sequence number is 16 bit */
95#define TFTP_SEQUENCE_SIZE ((ulong)(1<<16))
3f85ce27 96
fe8c2806
WD
97#define DEFAULT_NAME_LEN (8 + 4 + 1)
98static char default_filename[DEFAULT_NAME_LEN];
a93907c4
JCPV
99
100#ifndef CONFIG_TFTP_FILE_NAME_MAX_LEN
101#define MAX_LEN 128
102#else
103#define MAX_LEN CONFIG_TFTP_FILE_NAME_MAX_LEN
104#endif
105
106static char tftp_filename[MAX_LEN];
fe8c2806 107
6d0f6bcf 108#ifdef CONFIG_SYS_DIRECT_FLASH_TFTP
e6f2e902 109extern flash_info_t flash_info[];
fe8c2806
WD
110#endif
111
85eb5caf
WD
112/* 512 is poor choice for ethernet, MTU is typically 1500.
113 * Minus eth.hdrs thats 1468. Can get 2x better throughput with
53a5c424 114 * almost-MTU block sizes. At least try... fall back to 512 if need be.
89ba81d1 115 * (but those using CONFIG_IP_DEFRAG may want to set a larger block in cfg file)
53a5c424 116 */
89ba81d1
AR
117#ifdef CONFIG_TFTP_BLOCKSIZE
118#define TFTP_MTU_BLOCKSIZE CONFIG_TFTP_BLOCKSIZE
119#else
53a5c424 120#define TFTP_MTU_BLOCKSIZE 1468
89ba81d1
AR
121#endif
122
c718b143
LC
123static unsigned short TftpBlkSize = TFTP_BLOCK_SIZE;
124static unsigned short TftpBlkSizeOption = TFTP_MTU_BLOCKSIZE;
53a5c424
DU
125
126#ifdef CONFIG_MCAST_TFTP
127#include <malloc.h>
128#define MTFTP_BITMAPSIZE 0x1000
129static unsigned *Bitmap;
c718b143 130static int PrevBitmapHole, Mapsize = MTFTP_BITMAPSIZE;
9bb0a1bf
LC
131static uchar ProhibitMcast, MasterClient;
132static uchar Multicast;
53a5c424
DU
133extern IPaddr_t Mcast_addr;
134static int Mcast_port;
135static ulong TftpEndingBlock; /* can get 'last' block before done..*/
136
c718b143 137static void parse_multicast_oack(char *pkt, int len);
53a5c424
DU
138
139static void
140mcast_cleanup(void)
141{
6d2231e8
LC
142 if (Mcast_addr)
143 eth_mcast_join(Mcast_addr, 0);
144 if (Bitmap)
145 free(Bitmap);
c718b143 146 Bitmap = NULL;
53a5c424
DU
147 Mcast_addr = Multicast = Mcast_port = 0;
148 TftpEndingBlock = -1;
149}
150
151#endif /* CONFIG_MCAST_TFTP */
152
fe8c2806 153static __inline__ void
2e320257 154store_block(unsigned block, uchar *src, unsigned len)
fe8c2806 155{
53a5c424 156 ulong offset = block * TftpBlkSize + TftpBlockWrapOffset;
3f85ce27 157 ulong newsize = offset + len;
6d0f6bcf 158#ifdef CONFIG_SYS_DIRECT_FLASH_TFTP
fe8c2806
WD
159 int i, rc = 0;
160
c718b143 161 for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; i++) {
fe8c2806 162 /* start address in flash? */
be1b0d27
JF
163 if (flash_info[i].flash_id == FLASH_UNKNOWN)
164 continue;
fe8c2806
WD
165 if (load_addr + offset >= flash_info[i].start[0]) {
166 rc = 1;
167 break;
168 }
169 }
170
171 if (rc) { /* Flash is destination for this packet */
c718b143 172 rc = flash_write((char *)src, (ulong)(load_addr+offset), len);
fe8c2806 173 if (rc) {
c718b143 174 flash_perror(rc);
fe8c2806
WD
175 NetState = NETLOOP_FAIL;
176 return;
177 }
178 }
179 else
6d0f6bcf 180#endif /* CONFIG_SYS_DIRECT_FLASH_TFTP */
fe8c2806
WD
181 {
182 (void)memcpy((void *)(load_addr + offset), src, len);
183 }
53a5c424
DU
184#ifdef CONFIG_MCAST_TFTP
185 if (Multicast)
186 ext2_set_bit(block, Bitmap);
187#endif
fe8c2806
WD
188
189 if (NetBootFileXferSize < newsize)
190 NetBootFileXferSize = newsize;
191}
192
c718b143
LC
193static void TftpSend(void);
194static void TftpTimeout(void);
fe8c2806
WD
195
196/**********************************************************************/
197
198static void
c718b143 199TftpSend(void)
fe8c2806 200{
2e320257
LC
201 volatile uchar *pkt;
202 volatile uchar *xp;
203 int len = 0;
7bc5ee07 204 volatile ushort *s;
fe8c2806 205
85eb5caf 206#ifdef CONFIG_MCAST_TFTP
53a5c424 207 /* Multicast TFTP.. non-MasterClients do not ACK data. */
85eb5caf
WD
208 if (Multicast
209 && (TftpState == STATE_DATA)
210 && (MasterClient == 0))
53a5c424
DU
211 return;
212#endif
fe8c2806
WD
213 /*
214 * We will always be sending some sort of packet, so
215 * cobble together the packet headers now.
216 */
a3d991bd 217 pkt = NetTxPacket + NetEthHdrSize() + IP_HDR_SIZE;
fe8c2806
WD
218
219 switch (TftpState) {
220
e3fb0abe 221 case STATE_SEND_RRQ:
fe8c2806 222 xp = pkt;
7bc5ee07
WD
223 s = (ushort *)pkt;
224 *s++ = htons(TFTP_RRQ);
225 pkt = (uchar *)s;
c718b143 226 strcpy((char *)pkt, tftp_filename);
fe8c2806 227 pkt += strlen(tftp_filename) + 1;
c718b143 228 strcpy((char *)pkt, "octet");
fe8c2806 229 pkt += 5 /*strlen("octet")*/ + 1;
c718b143 230 strcpy((char *)pkt, "timeout");
fbe4b5cb 231 pkt += 7 /*strlen("timeout")*/ + 1;
c96f86ee 232 sprintf((char *)pkt, "%lu", TftpTimeoutMSecs / 1000);
0ebf04c6 233 debug("send option \"timeout %s\"\n", (char *)pkt);
fbe4b5cb 234 pkt += strlen((char *)pkt) + 1;
4fccb818
RG
235#ifdef CONFIG_TFTP_TSIZE
236 memcpy((char *)pkt, "tsize\0000\0", 8);
237 pkt += 8;
238#endif
53a5c424 239 /* try for more effic. blk size */
c718b143
LC
240 pkt += sprintf((char *)pkt, "blksize%c%d%c",
241 0, TftpBlkSizeOption, 0);
85eb5caf 242#ifdef CONFIG_MCAST_TFTP
53a5c424 243 /* Check all preconditions before even trying the option */
85eb5caf 244 if (!ProhibitMcast
c718b143 245 && (Bitmap = malloc(Mapsize))
53a5c424
DU
246 && eth_get_dev()->mcast) {
247 free(Bitmap);
c718b143
LC
248 Bitmap = NULL;
249 pkt += sprintf((char *)pkt, "multicast%c%c", 0, 0);
53a5c424
DU
250 }
251#endif /* CONFIG_MCAST_TFTP */
fe8c2806
WD
252 len = pkt - xp;
253 break;
254
fbe4b5cb 255 case STATE_OACK:
53a5c424
DU
256#ifdef CONFIG_MCAST_TFTP
257 /* My turn! Start at where I need blocks I missed.*/
258 if (Multicast)
c718b143
LC
259 TftpBlock = ext2_find_next_zero_bit(Bitmap,
260 (Mapsize*8), 0);
53a5c424
DU
261 /*..falling..*/
262#endif
e59e3562
LC
263
264 case STATE_RECV_WRQ:
53a5c424 265 case STATE_DATA:
fe8c2806 266 xp = pkt;
7bc5ee07
WD
267 s = (ushort *)pkt;
268 *s++ = htons(TFTP_ACK);
269 *s++ = htons(TftpBlock);
270 pkt = (uchar *)s;
fe8c2806
WD
271 len = pkt - xp;
272 break;
273
274 case STATE_TOO_LARGE:
275 xp = pkt;
7bc5ee07
WD
276 s = (ushort *)pkt;
277 *s++ = htons(TFTP_ERROR);
278 *s++ = htons(3);
279 pkt = (uchar *)s;
c718b143 280 strcpy((char *)pkt, "File too large");
fe8c2806
WD
281 pkt += 14 /*strlen("File too large")*/ + 1;
282 len = pkt - xp;
283 break;
284
285 case STATE_BAD_MAGIC:
286 xp = pkt;
7bc5ee07
WD
287 s = (ushort *)pkt;
288 *s++ = htons(TFTP_ERROR);
289 *s++ = htons(2);
290 pkt = (uchar *)s;
c718b143 291 strcpy((char *)pkt, "File has bad magic");
fe8c2806
WD
292 pkt += 18 /*strlen("File has bad magic")*/ + 1;
293 len = pkt - xp;
294 break;
295 }
296
20478cea 297 NetSendUDPPacket(NetServerEther, TftpRemoteIP, TftpRemotePort,
2f09413f 298 TftpOurPort, len);
fe8c2806
WD
299}
300
301
302static void
03eb129f
LC
303TftpHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src,
304 unsigned len)
fe8c2806
WD
305{
306 ushort proto;
7bc5ee07 307 ushort *s;
ff13ac8c 308 int i;
fe8c2806
WD
309
310 if (dest != TftpOurPort) {
53a5c424 311#ifdef CONFIG_MCAST_TFTP
85eb5caf 312 if (Multicast
53a5c424
DU
313 && (!Mcast_port || (dest != Mcast_port)))
314#endif
0bdd8acc 315 return;
fe8c2806 316 }
e59e3562
LC
317 if (TftpState != STATE_SEND_RRQ && src != TftpRemotePort &&
318 TftpState != STATE_RECV_WRQ)
fe8c2806 319 return;
fe8c2806 320
7bc325a1 321 if (len < 2)
fe8c2806 322 return;
fe8c2806
WD
323 len -= 2;
324 /* warning: don't use increment (++) in ntohs() macros!! */
7bc5ee07
WD
325 s = (ushort *)pkt;
326 proto = *s++;
327 pkt = (uchar *)s;
fe8c2806
WD
328 switch (ntohs(proto)) {
329
330 case TFTP_RRQ:
fe8c2806
WD
331 case TFTP_ACK:
332 break;
333 default:
334 break;
335
e59e3562
LC
336#ifdef CONFIG_CMD_TFTPSRV
337 case TFTP_WRQ:
338 debug("Got WRQ\n");
339 TftpRemoteIP = sip;
340 TftpRemotePort = src;
341 TftpOurPort = 1024 + (get_timer(0) % 3072);
342 TftpLastBlock = 0;
343 TftpBlockWrap = 0;
344 TftpBlockWrapOffset = 0;
345 TftpSend(); /* Send ACK(0) */
346 break;
347#endif
348
fbe4b5cb 349 case TFTP_OACK:
d371708a
WD
350 debug("Got OACK: %s %s\n",
351 pkt,
352 pkt + strlen((char *)pkt) + 1);
fbe4b5cb 353 TftpState = STATE_OACK;
20478cea 354 TftpRemotePort = src;
60174746
WD
355 /*
356 * Check for 'blksize' option.
357 * Careful: "i" is signed, "len" is unsigned, thus
358 * something like "len-8" may give a *huge* number
359 */
c718b143 360 for (i = 0; i+8 < len; i++) {
2e320257 361 if (strcmp((char *)pkt+i, "blksize") == 0) {
ff13ac8c 362 TftpBlkSize = (unsigned short)
2e320257 363 simple_strtoul((char *)pkt+i+8, NULL,
c718b143 364 10);
0ebf04c6 365 debug("Blocksize ack: %s, %d\n",
2e320257 366 (char *)pkt+i+8, TftpBlkSize);
ff13ac8c 367 }
4fccb818 368#ifdef CONFIG_TFTP_TSIZE
2e320257
LC
369 if (strcmp((char *)pkt+i, "tsize") == 0) {
370 TftpTsize = simple_strtoul((char *)pkt+i+6,
2f09413f 371 NULL, 10);
4fccb818 372 debug("size = %s, %d\n",
2e320257 373 (char *)pkt+i+6, TftpTsize);
4fccb818
RG
374 }
375#endif
53a5c424
DU
376 }
377#ifdef CONFIG_MCAST_TFTP
c718b143 378 parse_multicast_oack((char *)pkt, len-1);
85eb5caf 379 if ((Multicast) && (!MasterClient))
53a5c424
DU
380 TftpState = STATE_DATA; /* passive.. */
381 else
382#endif
c718b143 383 TftpSend(); /* Send ACK */
fbe4b5cb 384 break;
fe8c2806
WD
385 case TFTP_DATA:
386 if (len < 2)
387 return;
388 len -= 2;
389 TftpBlock = ntohs(*(ushort *)pkt);
3f85ce27
WD
390
391 /*
cd0a9de6
WD
392 * RFC1350 specifies that the first data packet will
393 * have sequence number 1. If we receive a sequence
394 * number of 0 this means that there was a wrap
395 * around of the (16 bit) counter.
3f85ce27
WD
396 */
397 if (TftpBlock == 0) {
398 TftpBlockWrap++;
2f09413f
LC
399 TftpBlockWrapOffset +=
400 TftpBlkSize * TFTP_SEQUENCE_SIZE;
c718b143 401 printf("\n\t %lu MB received\n\t ",
2f09413f 402 TftpBlockWrapOffset>>20);
4fccb818
RG
403 }
404#ifdef CONFIG_TFTP_TSIZE
405 else if (TftpTsize) {
2f09413f
LC
406 while (TftpNumchars <
407 NetBootFileXferSize * 50 / TftpTsize) {
4fccb818
RG
408 putc('#');
409 TftpNumchars++;
410 }
411 }
412#endif
413 else {
7bc325a1 414 if (((TftpBlock - 1) % 10) == 0)
c718b143 415 putc('#');
7bc325a1 416 else if ((TftpBlock % (10 * HASHES_PER_LINE)) == 0)
c718b143 417 puts("\n\t ");
fe8c2806
WD
418 }
419
e3fb0abe 420 if (TftpState == STATE_SEND_RRQ)
0ebf04c6 421 debug("Server did not acknowledge timeout option!\n");
fbe4b5cb 422
e59e3562
LC
423 if (TftpState == STATE_SEND_RRQ || TftpState == STATE_OACK ||
424 TftpState == STATE_RECV_WRQ) {
3f85ce27 425 /* first block received */
fe8c2806 426 TftpState = STATE_DATA;
20478cea 427 TftpRemotePort = src;
fe8c2806 428 TftpLastBlock = 0;
3f85ce27
WD
429 TftpBlockWrap = 0;
430 TftpBlockWrapOffset = 0;
fe8c2806 431
53a5c424
DU
432#ifdef CONFIG_MCAST_TFTP
433 if (Multicast) { /* start!=1 common if mcast */
434 TftpLastBlock = TftpBlock - 1;
435 } else
436#endif
fe8c2806 437 if (TftpBlock != 1) { /* Assertion */
c718b143
LC
438 printf("\nTFTP error: "
439 "First block is not block 1 (%ld)\n"
440 "Starting again\n\n",
fe8c2806 441 TftpBlock);
c718b143 442 NetStartAgain();
fe8c2806
WD
443 break;
444 }
445 }
446
447 if (TftpBlock == TftpLastBlock) {
448 /*
449 * Same block again; ignore it.
450 */
451 break;
452 }
453
454 TftpLastBlock = TftpBlock;
e83cc063 455 TftpTimeoutCountMax = TIMEOUT_COUNT;
c718b143 456 NetSetTimeout(TftpTimeoutMSecs, TftpTimeout);
fe8c2806 457
c718b143 458 store_block(TftpBlock - 1, pkt + 2, len);
fe8c2806
WD
459
460 /*
461 * Acknoledge the block just received, which will prompt
20478cea 462 * the remote for the next one.
fe8c2806 463 */
53a5c424 464#ifdef CONFIG_MCAST_TFTP
85eb5caf
WD
465 /* if I am the MasterClient, actively calculate what my next
466 * needed block is; else I'm passive; not ACKING
a93907c4 467 */
53a5c424
DU
468 if (Multicast) {
469 if (len < TftpBlkSize) {
470 TftpEndingBlock = TftpBlock;
471 } else if (MasterClient) {
85eb5caf 472 TftpBlock = PrevBitmapHole =
53a5c424
DU
473 ext2_find_next_zero_bit(
474 Bitmap,
475 (Mapsize*8),
476 PrevBitmapHole);
477 if (TftpBlock > ((Mapsize*8) - 1)) {
c718b143 478 printf("tftpfile too big\n");
53a5c424 479 /* try to double it and retry */
c718b143 480 Mapsize <<= 1;
53a5c424 481 mcast_cleanup();
c718b143 482 NetStartAgain();
53a5c424
DU
483 return;
484 }
485 TftpLastBlock = TftpBlock;
486 }
487 }
488#endif
c718b143 489 TftpSend();
fe8c2806 490
53a5c424
DU
491#ifdef CONFIG_MCAST_TFTP
492 if (Multicast) {
493 if (MasterClient && (TftpBlock >= TftpEndingBlock)) {
c718b143 494 puts("\nMulticast tftp done\n");
53a5c424
DU
495 mcast_cleanup();
496 NetState = NETLOOP_SUCCESS;
85eb5caf 497 }
53a5c424
DU
498 }
499 else
500#endif
501 if (len < TftpBlkSize) {
fe8c2806
WD
502 /*
503 * We received the whole thing. Try to
504 * run it.
505 */
4fccb818 506#ifdef CONFIG_TFTP_TSIZE
2f09413f 507 /* Print hash marks for the last packet received */
4fccb818
RG
508 while (TftpTsize && TftpNumchars < 49) {
509 putc('#');
510 TftpNumchars++;
511 }
512#endif
c718b143 513 puts("\ndone\n");
fe8c2806
WD
514 NetState = NETLOOP_SUCCESS;
515 }
516 break;
517
518 case TFTP_ERROR:
c718b143
LC
519 printf("\nTFTP error: '%s' (%d)\n",
520 pkt + 2, ntohs(*(ushort *)pkt));
aafda38f
RB
521
522 switch (ntohs(*(ushort *)pkt)) {
523 case TFTP_ERR_FILE_NOT_FOUND:
524 case TFTP_ERR_ACCESS_DENIED:
525 puts("Not retrying...\n");
526 eth_halt();
527 NetState = NETLOOP_FAIL;
528 break;
529 case TFTP_ERR_UNDEFINED:
530 case TFTP_ERR_DISK_FULL:
531 case TFTP_ERR_UNEXPECTED_OPCODE:
532 case TFTP_ERR_UNKNOWN_TRANSFER_ID:
533 case TFTP_ERR_FILE_ALREADY_EXISTS:
534 default:
535 puts("Starting again\n\n");
53a5c424 536#ifdef CONFIG_MCAST_TFTP
aafda38f 537 mcast_cleanup();
53a5c424 538#endif
aafda38f
RB
539 NetStartAgain();
540 break;
541 }
fe8c2806
WD
542 break;
543 }
544}
545
546
547static void
c718b143 548TftpTimeout(void)
fe8c2806 549{
e83cc063 550 if (++TftpTimeoutCount > TftpTimeoutCountMax) {
c718b143 551 puts("\nRetry count exceeded; starting again\n");
53a5c424
DU
552#ifdef CONFIG_MCAST_TFTP
553 mcast_cleanup();
554#endif
c718b143 555 NetStartAgain();
fe8c2806 556 } else {
c718b143
LC
557 puts("T ");
558 NetSetTimeout(TftpTimeoutMSecs, TftpTimeout);
e59e3562
LC
559 if (TftpState != STATE_RECV_WRQ)
560 TftpSend();
fe8c2806
WD
561 }
562}
563
564
565void
c718b143 566TftpStart(void)
fe8c2806 567{
ecb0ccd9 568 char *ep; /* Environment pointer */
89ba81d1 569
c96f86ee
WD
570 /*
571 * Allow the user to choose TFTP blocksize and timeout.
572 * TFTP protocol has a minimal timeout of 1 second.
573 */
2cb53608
LC
574 ep = getenv("tftpblocksize");
575 if (ep != NULL)
89ba81d1 576 TftpBlkSizeOption = simple_strtol(ep, NULL, 10);
c96f86ee 577
2cb53608
LC
578 ep = getenv("tftptimeout");
579 if (ep != NULL)
c96f86ee
WD
580 TftpTimeoutMSecs = simple_strtol(ep, NULL, 10);
581
582 if (TftpTimeoutMSecs < 1000) {
583 printf("TFTP timeout (%ld ms) too low, "
584 "set minimum = 1000 ms\n",
585 TftpTimeoutMSecs);
586 TftpTimeoutMSecs = 1000;
587 }
588
589 debug("TFTP blocksize = %i, timeout = %ld ms\n",
590 TftpBlkSizeOption, TftpTimeoutMSecs);
ecb0ccd9 591
20478cea 592 TftpRemoteIP = NetServerIP;
fe8c2806 593 if (BootFile[0] == '\0') {
fe8c2806 594 sprintf(default_filename, "%02lX%02lX%02lX%02lX.img",
c43352cc
WD
595 NetOurIP & 0xFF,
596 (NetOurIP >> 8) & 0xFF,
597 (NetOurIP >> 16) & 0xFF,
c718b143 598 (NetOurIP >> 24) & 0xFF);
a93907c4
JCPV
599
600 strncpy(tftp_filename, default_filename, MAX_LEN);
601 tftp_filename[MAX_LEN-1] = 0;
fe8c2806 602
c718b143 603 printf("*** Warning: no boot file name; using '%s'\n",
fe8c2806
WD
604 tftp_filename);
605 } else {
c718b143 606 char *p = strchr(BootFile, ':');
a93907c4
JCPV
607
608 if (p == NULL) {
609 strncpy(tftp_filename, BootFile, MAX_LEN);
610 tftp_filename[MAX_LEN-1] = 0;
611 } else {
20478cea 612 TftpRemoteIP = string_to_ip(BootFile);
6a86bb6c 613 strncpy(tftp_filename, p + 1, MAX_LEN);
a93907c4
JCPV
614 tftp_filename[MAX_LEN-1] = 0;
615 }
fe8c2806
WD
616 }
617
5bb226e8 618#if defined(CONFIG_NET_MULTI)
c718b143 619 printf("Using %s device\n", eth_get_name());
5bb226e8 620#endif
b6446b67 621 printf("TFTP from server %pI4"
20478cea 622 "; our IP address is %pI4", &TftpRemoteIP, &NetOurIP);
fe8c2806
WD
623
624 /* Check if we need to send across this subnet */
625 if (NetOurGatewayIP && NetOurSubnetMask) {
0bdd8acc 626 IPaddr_t OurNet = NetOurIP & NetOurSubnetMask;
20478cea 627 IPaddr_t RemoteNet = TftpRemoteIP & NetOurSubnetMask;
fe8c2806 628
20478cea 629 if (OurNet != RemoteNet)
0bdd8acc
LC
630 printf("; sending through gateway %pI4",
631 &NetOurGatewayIP);
fe8c2806 632 }
c718b143 633 putc('\n');
fe8c2806 634
c718b143 635 printf("Filename '%s'.", tftp_filename);
fe8c2806
WD
636
637 if (NetBootFileSize) {
c718b143
LC
638 printf(" Size is 0x%x Bytes = ", NetBootFileSize<<9);
639 print_size(NetBootFileSize<<9, "");
fe8c2806
WD
640 }
641
c718b143 642 putc('\n');
fe8c2806 643
c718b143 644 printf("Load address: 0x%lx\n", load_addr);
fe8c2806 645
c718b143 646 puts("Loading: *\b");
fe8c2806 647
e83cc063
BS
648 TftpTimeoutCountMax = TftpRRQTimeoutCountMax;
649
c718b143
LC
650 NetSetTimeout(TftpTimeoutMSecs, TftpTimeout);
651 NetSetHandler(TftpHandler);
fe8c2806 652
20478cea 653 TftpRemotePort = WELL_KNOWN_PORT;
fe8c2806 654 TftpTimeoutCount = 0;
e3fb0abe 655 TftpState = STATE_SEND_RRQ;
ecb0ccd9 656 /* Use a pseudo-random port unless a specific port is set */
fe8c2806 657 TftpOurPort = 1024 + (get_timer(0) % 3072);
53a5c424 658
ecb0ccd9 659#ifdef CONFIG_TFTP_PORT
2cb53608 660 ep = getenv("tftpdstp");
7bc325a1 661 if (ep != NULL)
20478cea 662 TftpRemotePort = simple_strtol(ep, NULL, 10);
2cb53608 663 ep = getenv("tftpsrcp");
7bc325a1 664 if (ep != NULL)
c718b143 665 TftpOurPort = simple_strtol(ep, NULL, 10);
ecb0ccd9 666#endif
fbe4b5cb 667 TftpBlock = 0;
fe8c2806 668
73a8b27c
WD
669 /* zero out server ether in case the server ip has changed */
670 memset(NetServerEther, 0, 6);
53a5c424
DU
671 /* Revert TftpBlkSize to dflt */
672 TftpBlkSize = TFTP_BLOCK_SIZE;
673#ifdef CONFIG_MCAST_TFTP
a93907c4 674 mcast_cleanup();
53a5c424 675#endif
4fccb818
RG
676#ifdef CONFIG_TFTP_TSIZE
677 TftpTsize = 0;
678 TftpNumchars = 0;
679#endif
73a8b27c 680
c718b143 681 TftpSend();
fe8c2806
WD
682}
683
e59e3562
LC
684#ifdef CONFIG_CMD_TFTPSRV
685void
686TftpStartServer(void)
687{
688 tftp_filename[0] = 0;
689
690#if defined(CONFIG_NET_MULTI)
691 printf("Using %s device\n", eth_get_name());
692#endif
693 printf("Listening for TFTP transfer on %pI4\n", &NetOurIP);
694 printf("Load address: 0x%lx\n", load_addr);
695
696 puts("Loading: *\b");
697
698 TftpTimeoutCountMax = TIMEOUT_COUNT;
699 TftpTimeoutCount = 0;
700 TftpTimeoutMSecs = TIMEOUT;
701 NetSetTimeout(TftpTimeoutMSecs, TftpTimeout);
702
703 /* Revert TftpBlkSize to dflt */
704 TftpBlkSize = TFTP_BLOCK_SIZE;
705 TftpBlock = 0;
706 TftpOurPort = WELL_KNOWN_PORT;
707
708#ifdef CONFIG_TFTP_TSIZE
709 TftpTsize = 0;
710 TftpNumchars = 0;
711#endif
712
713 TftpState = STATE_RECV_WRQ;
714 NetSetHandler(TftpHandler);
715}
716#endif /* CONFIG_CMD_TFTPSRV */
717
53a5c424
DU
718#ifdef CONFIG_MCAST_TFTP
719/* Credits: atftp project.
720 */
721
722/* pick up BcastAddr, Port, and whether I am [now] the master-client. *
723 * Frame:
724 * +-------+-----------+---+-------~~-------+---+
725 * | opc | multicast | 0 | addr, port, mc | 0 |
726 * +-------+-----------+---+-------~~-------+---+
727 * The multicast addr/port becomes what I listen to, and if 'mc' is '1' then
728 * I am the new master-client so must send ACKs to DataBlocks. If I am not
729 * master-client, I'm a passive client, gathering what DataBlocks I may and
730 * making note of which ones I got in my bitmask.
731 * In theory, I never go from master->passive..
732 * .. this comes in with pkt already pointing just past opc
733 */
734static void parse_multicast_oack(char *pkt, int len)
735{
c718b143
LC
736 int i;
737 IPaddr_t addr;
738 char *mc_adr, *port, *mc;
53a5c424 739
c718b143 740 mc_adr = port = mc = NULL;
53a5c424
DU
741 /* march along looking for 'multicast\0', which has to start at least
742 * 14 bytes back from the end.
743 */
c718b143
LC
744 for (i = 0; i < len-14; i++)
745 if (strcmp(pkt+i, "multicast") == 0)
53a5c424
DU
746 break;
747 if (i >= (len-14)) /* non-Multicast OACK, ign. */
748 return;
85eb5caf 749
c718b143 750 i += 10; /* strlen multicast */
53a5c424 751 mc_adr = pkt+i;
c718b143 752 for (; i < len; i++) {
53a5c424
DU
753 if (*(pkt+i) == ',') {
754 *(pkt+i) = '\0';
755 if (port) {
756 mc = pkt+i+1;
757 break;
758 } else {
759 port = pkt+i+1;
760 }
761 }
762 }
6d2231e8
LC
763 if (!port || !mc_adr || !mc)
764 return;
53a5c424 765 if (Multicast && MasterClient) {
c718b143 766 printf("I got a OACK as master Client, WRONG!\n");
53a5c424
DU
767 return;
768 }
769 /* ..I now accept packets destined for this MCAST addr, port */
770 if (!Multicast) {
771 if (Bitmap) {
c718b143 772 printf("Internal failure! no mcast.\n");
53a5c424 773 free(Bitmap);
c718b143
LC
774 Bitmap = NULL;
775 ProhibitMcast = 1;
53a5c424
DU
776 return ;
777 }
778 /* I malloc instead of pre-declare; so that if the file ends
85eb5caf
WD
779 * up being too big for this bitmap I can retry
780 */
2cb53608
LC
781 Bitmap = malloc(Mapsize);
782 if (!Bitmap) {
c718b143
LC
783 printf("No Bitmap, no multicast. Sorry.\n");
784 ProhibitMcast = 1;
53a5c424
DU
785 return;
786 }
c718b143 787 memset(Bitmap, 0, Mapsize);
53a5c424
DU
788 PrevBitmapHole = 0;
789 Multicast = 1;
790 }
791 addr = string_to_ip(mc_adr);
792 if (Mcast_addr != addr) {
793 if (Mcast_addr)
794 eth_mcast_join(Mcast_addr, 0);
2cb53608
LC
795 Mcast_addr = addr;
796 if (eth_mcast_join(Mcast_addr, 1)) {
c718b143
LC
797 printf("Fail to set mcast, revert to TFTP\n");
798 ProhibitMcast = 1;
53a5c424
DU
799 mcast_cleanup();
800 NetStartAgain();
801 }
802 }
c718b143
LC
803 MasterClient = (unsigned char)simple_strtoul((char *)mc, NULL, 10);
804 Mcast_port = (unsigned short)simple_strtoul(port, NULL, 10);
805 printf("Multicast: %s:%d [%d]\n", mc_adr, Mcast_port, MasterClient);
53a5c424
DU
806 return;
807}
808
809#endif /* Multicast TFTP */