]> git.ipfire.org Git - people/ms/u-boot.git/blame - net/tftp.c
tftpput: add save_addr and save_size global variables
[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
e4cde2f7
SG
193/* Clear our state ready for a new transfer */
194void new_transfer(void)
195{
196 TftpLastBlock = 0;
197 TftpBlockWrap = 0;
198 TftpBlockWrapOffset = 0;
199#ifdef CONFIG_CMD_TFTPPUT
200 TftpFinalBlock = 0;
201#endif
202}
203
c718b143
LC
204static void TftpSend(void);
205static void TftpTimeout(void);
fe8c2806
WD
206
207/**********************************************************************/
208
f5329bbc
SG
209static void show_block_marker(void)
210{
211#ifdef CONFIG_TFTP_TSIZE
212 if (TftpTsize) {
213 ulong pos = TftpBlock * TftpBlkSize + TftpBlockWrapOffset;
214
215 while (TftpNumchars < pos * 50 / TftpTsize) {
216 putc('#');
217 TftpNumchars++;
218 }
219 }
220#endif
221 else {
222 if (((TftpBlock - 1) % 10) == 0)
223 putc('#');
224 else if ((TftpBlock % (10 * HASHES_PER_LINE)) == 0)
225 puts("\n\t ");
226 }
227}
228
e4cde2f7
SG
229/**
230 * restart the current transfer due to an error
231 *
232 * @param msg Message to print for user
233 */
234static void restart(const char *msg)
235{
236 printf("\n%s; starting again\n", msg);
237#ifdef CONFIG_MCAST_TFTP
238 mcast_cleanup();
239#endif
240 NetStartAgain();
241}
242
243/*
244 * Check if the block number has wrapped, and update progress
245 *
246 * TODO: The egregious use of global variables in this file should be tidied.
247 */
248static void update_block_number(void)
249{
250 /*
251 * RFC1350 specifies that the first data packet will
252 * have sequence number 1. If we receive a sequence
253 * number of 0 this means that there was a wrap
254 * around of the (16 bit) counter.
255 */
256 if (TftpBlock == 0) {
257 TftpBlockWrap++;
258 TftpBlockWrapOffset += TftpBlkSize * TFTP_SEQUENCE_SIZE;
259 TftpTimeoutCount = 0; /* we've done well, reset thhe timeout */
260 } else {
261 show_block_marker();
262 }
263}
264
f5329bbc
SG
265/* The TFTP get or put is complete */
266static void tftp_complete(void)
267{
268#ifdef CONFIG_TFTP_TSIZE
269 /* Print hash marks for the last packet received */
270 while (TftpTsize && TftpNumchars < 49) {
271 putc('#');
272 TftpNumchars++;
273 }
274#endif
275 puts("\ndone\n");
276 NetState = NETLOOP_SUCCESS;
277}
278
fe8c2806 279static void
c718b143 280TftpSend(void)
fe8c2806 281{
2e320257
LC
282 volatile uchar *pkt;
283 volatile uchar *xp;
284 int len = 0;
7bc5ee07 285 volatile ushort *s;
fe8c2806 286
85eb5caf 287#ifdef CONFIG_MCAST_TFTP
53a5c424 288 /* Multicast TFTP.. non-MasterClients do not ACK data. */
85eb5caf
WD
289 if (Multicast
290 && (TftpState == STATE_DATA)
291 && (MasterClient == 0))
53a5c424
DU
292 return;
293#endif
fe8c2806
WD
294 /*
295 * We will always be sending some sort of packet, so
296 * cobble together the packet headers now.
297 */
a3d991bd 298 pkt = NetTxPacket + NetEthHdrSize() + IP_HDR_SIZE;
fe8c2806
WD
299
300 switch (TftpState) {
301
e3fb0abe 302 case STATE_SEND_RRQ:
fe8c2806 303 xp = pkt;
7bc5ee07
WD
304 s = (ushort *)pkt;
305 *s++ = htons(TFTP_RRQ);
306 pkt = (uchar *)s;
c718b143 307 strcpy((char *)pkt, tftp_filename);
fe8c2806 308 pkt += strlen(tftp_filename) + 1;
c718b143 309 strcpy((char *)pkt, "octet");
fe8c2806 310 pkt += 5 /*strlen("octet")*/ + 1;
c718b143 311 strcpy((char *)pkt, "timeout");
fbe4b5cb 312 pkt += 7 /*strlen("timeout")*/ + 1;
c96f86ee 313 sprintf((char *)pkt, "%lu", TftpTimeoutMSecs / 1000);
0ebf04c6 314 debug("send option \"timeout %s\"\n", (char *)pkt);
fbe4b5cb 315 pkt += strlen((char *)pkt) + 1;
4fccb818
RG
316#ifdef CONFIG_TFTP_TSIZE
317 memcpy((char *)pkt, "tsize\0000\0", 8);
318 pkt += 8;
319#endif
53a5c424 320 /* try for more effic. blk size */
c718b143
LC
321 pkt += sprintf((char *)pkt, "blksize%c%d%c",
322 0, TftpBlkSizeOption, 0);
85eb5caf 323#ifdef CONFIG_MCAST_TFTP
53a5c424 324 /* Check all preconditions before even trying the option */
85eb5caf 325 if (!ProhibitMcast
c718b143 326 && (Bitmap = malloc(Mapsize))
53a5c424
DU
327 && eth_get_dev()->mcast) {
328 free(Bitmap);
c718b143
LC
329 Bitmap = NULL;
330 pkt += sprintf((char *)pkt, "multicast%c%c", 0, 0);
53a5c424
DU
331 }
332#endif /* CONFIG_MCAST_TFTP */
fe8c2806
WD
333 len = pkt - xp;
334 break;
335
fbe4b5cb 336 case STATE_OACK:
53a5c424
DU
337#ifdef CONFIG_MCAST_TFTP
338 /* My turn! Start at where I need blocks I missed.*/
339 if (Multicast)
c718b143
LC
340 TftpBlock = ext2_find_next_zero_bit(Bitmap,
341 (Mapsize*8), 0);
53a5c424
DU
342 /*..falling..*/
343#endif
e59e3562
LC
344
345 case STATE_RECV_WRQ:
53a5c424 346 case STATE_DATA:
fe8c2806 347 xp = pkt;
7bc5ee07
WD
348 s = (ushort *)pkt;
349 *s++ = htons(TFTP_ACK);
350 *s++ = htons(TftpBlock);
351 pkt = (uchar *)s;
fe8c2806
WD
352 len = pkt - xp;
353 break;
354
355 case STATE_TOO_LARGE:
356 xp = pkt;
7bc5ee07
WD
357 s = (ushort *)pkt;
358 *s++ = htons(TFTP_ERROR);
359 *s++ = htons(3);
360 pkt = (uchar *)s;
c718b143 361 strcpy((char *)pkt, "File too large");
fe8c2806
WD
362 pkt += 14 /*strlen("File too large")*/ + 1;
363 len = pkt - xp;
364 break;
365
366 case STATE_BAD_MAGIC:
367 xp = pkt;
7bc5ee07
WD
368 s = (ushort *)pkt;
369 *s++ = htons(TFTP_ERROR);
370 *s++ = htons(2);
371 pkt = (uchar *)s;
c718b143 372 strcpy((char *)pkt, "File has bad magic");
fe8c2806
WD
373 pkt += 18 /*strlen("File has bad magic")*/ + 1;
374 len = pkt - xp;
375 break;
376 }
377
20478cea 378 NetSendUDPPacket(NetServerEther, TftpRemoteIP, TftpRemotePort,
2f09413f 379 TftpOurPort, len);
fe8c2806
WD
380}
381
382
383static void
03eb129f
LC
384TftpHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src,
385 unsigned len)
fe8c2806
WD
386{
387 ushort proto;
7bc5ee07 388 ushort *s;
ff13ac8c 389 int i;
fe8c2806
WD
390
391 if (dest != TftpOurPort) {
53a5c424 392#ifdef CONFIG_MCAST_TFTP
85eb5caf 393 if (Multicast
53a5c424
DU
394 && (!Mcast_port || (dest != Mcast_port)))
395#endif
0bdd8acc 396 return;
fe8c2806 397 }
e59e3562
LC
398 if (TftpState != STATE_SEND_RRQ && src != TftpRemotePort &&
399 TftpState != STATE_RECV_WRQ)
fe8c2806 400 return;
fe8c2806 401
7bc325a1 402 if (len < 2)
fe8c2806 403 return;
fe8c2806
WD
404 len -= 2;
405 /* warning: don't use increment (++) in ntohs() macros!! */
7bc5ee07
WD
406 s = (ushort *)pkt;
407 proto = *s++;
408 pkt = (uchar *)s;
fe8c2806
WD
409 switch (ntohs(proto)) {
410
411 case TFTP_RRQ:
fe8c2806
WD
412 case TFTP_ACK:
413 break;
414 default:
415 break;
416
e59e3562
LC
417#ifdef CONFIG_CMD_TFTPSRV
418 case TFTP_WRQ:
419 debug("Got WRQ\n");
420 TftpRemoteIP = sip;
421 TftpRemotePort = src;
422 TftpOurPort = 1024 + (get_timer(0) % 3072);
e4cde2f7 423 new_transfer();
e59e3562
LC
424 TftpSend(); /* Send ACK(0) */
425 break;
426#endif
427
fbe4b5cb 428 case TFTP_OACK:
d371708a
WD
429 debug("Got OACK: %s %s\n",
430 pkt,
431 pkt + strlen((char *)pkt) + 1);
fbe4b5cb 432 TftpState = STATE_OACK;
20478cea 433 TftpRemotePort = src;
60174746
WD
434 /*
435 * Check for 'blksize' option.
436 * Careful: "i" is signed, "len" is unsigned, thus
437 * something like "len-8" may give a *huge* number
438 */
c718b143 439 for (i = 0; i+8 < len; i++) {
2e320257 440 if (strcmp((char *)pkt+i, "blksize") == 0) {
ff13ac8c 441 TftpBlkSize = (unsigned short)
2e320257 442 simple_strtoul((char *)pkt+i+8, NULL,
c718b143 443 10);
0ebf04c6 444 debug("Blocksize ack: %s, %d\n",
2e320257 445 (char *)pkt+i+8, TftpBlkSize);
ff13ac8c 446 }
4fccb818 447#ifdef CONFIG_TFTP_TSIZE
2e320257
LC
448 if (strcmp((char *)pkt+i, "tsize") == 0) {
449 TftpTsize = simple_strtoul((char *)pkt+i+6,
2f09413f 450 NULL, 10);
4fccb818 451 debug("size = %s, %d\n",
2e320257 452 (char *)pkt+i+6, TftpTsize);
4fccb818
RG
453 }
454#endif
53a5c424
DU
455 }
456#ifdef CONFIG_MCAST_TFTP
c718b143 457 parse_multicast_oack((char *)pkt, len-1);
85eb5caf 458 if ((Multicast) && (!MasterClient))
53a5c424
DU
459 TftpState = STATE_DATA; /* passive.. */
460 else
461#endif
c718b143 462 TftpSend(); /* Send ACK */
fbe4b5cb 463 break;
fe8c2806
WD
464 case TFTP_DATA:
465 if (len < 2)
466 return;
467 len -= 2;
468 TftpBlock = ntohs(*(ushort *)pkt);
3f85ce27 469
e4cde2f7 470 update_block_number();
fe8c2806 471
e3fb0abe 472 if (TftpState == STATE_SEND_RRQ)
0ebf04c6 473 debug("Server did not acknowledge timeout option!\n");
fbe4b5cb 474
e59e3562
LC
475 if (TftpState == STATE_SEND_RRQ || TftpState == STATE_OACK ||
476 TftpState == STATE_RECV_WRQ) {
3f85ce27 477 /* first block received */
fe8c2806 478 TftpState = STATE_DATA;
20478cea 479 TftpRemotePort = src;
e4cde2f7 480 new_transfer();
fe8c2806 481
53a5c424
DU
482#ifdef CONFIG_MCAST_TFTP
483 if (Multicast) { /* start!=1 common if mcast */
484 TftpLastBlock = TftpBlock - 1;
485 } else
486#endif
fe8c2806 487 if (TftpBlock != 1) { /* Assertion */
c718b143
LC
488 printf("\nTFTP error: "
489 "First block is not block 1 (%ld)\n"
490 "Starting again\n\n",
fe8c2806 491 TftpBlock);
c718b143 492 NetStartAgain();
fe8c2806
WD
493 break;
494 }
495 }
496
497 if (TftpBlock == TftpLastBlock) {
498 /*
499 * Same block again; ignore it.
500 */
501 break;
502 }
503
504 TftpLastBlock = TftpBlock;
e83cc063 505 TftpTimeoutCountMax = TIMEOUT_COUNT;
c718b143 506 NetSetTimeout(TftpTimeoutMSecs, TftpTimeout);
fe8c2806 507
c718b143 508 store_block(TftpBlock - 1, pkt + 2, len);
fe8c2806
WD
509
510 /*
4d69e98c 511 * Acknowledge the block just received, which will prompt
20478cea 512 * the remote for the next one.
fe8c2806 513 */
53a5c424 514#ifdef CONFIG_MCAST_TFTP
85eb5caf
WD
515 /* if I am the MasterClient, actively calculate what my next
516 * needed block is; else I'm passive; not ACKING
a93907c4 517 */
53a5c424
DU
518 if (Multicast) {
519 if (len < TftpBlkSize) {
520 TftpEndingBlock = TftpBlock;
521 } else if (MasterClient) {
85eb5caf 522 TftpBlock = PrevBitmapHole =
53a5c424
DU
523 ext2_find_next_zero_bit(
524 Bitmap,
525 (Mapsize*8),
526 PrevBitmapHole);
527 if (TftpBlock > ((Mapsize*8) - 1)) {
c718b143 528 printf("tftpfile too big\n");
53a5c424 529 /* try to double it and retry */
c718b143 530 Mapsize <<= 1;
53a5c424 531 mcast_cleanup();
c718b143 532 NetStartAgain();
53a5c424
DU
533 return;
534 }
535 TftpLastBlock = TftpBlock;
536 }
537 }
538#endif
c718b143 539 TftpSend();
fe8c2806 540
53a5c424
DU
541#ifdef CONFIG_MCAST_TFTP
542 if (Multicast) {
543 if (MasterClient && (TftpBlock >= TftpEndingBlock)) {
c718b143 544 puts("\nMulticast tftp done\n");
53a5c424
DU
545 mcast_cleanup();
546 NetState = NETLOOP_SUCCESS;
85eb5caf 547 }
53a5c424
DU
548 }
549 else
550#endif
f5329bbc
SG
551 if (len < TftpBlkSize)
552 tftp_complete();
fe8c2806
WD
553 break;
554
555 case TFTP_ERROR:
c718b143
LC
556 printf("\nTFTP error: '%s' (%d)\n",
557 pkt + 2, ntohs(*(ushort *)pkt));
aafda38f
RB
558
559 switch (ntohs(*(ushort *)pkt)) {
560 case TFTP_ERR_FILE_NOT_FOUND:
561 case TFTP_ERR_ACCESS_DENIED:
562 puts("Not retrying...\n");
563 eth_halt();
564 NetState = NETLOOP_FAIL;
565 break;
566 case TFTP_ERR_UNDEFINED:
567 case TFTP_ERR_DISK_FULL:
568 case TFTP_ERR_UNEXPECTED_OPCODE:
569 case TFTP_ERR_UNKNOWN_TRANSFER_ID:
570 case TFTP_ERR_FILE_ALREADY_EXISTS:
571 default:
572 puts("Starting again\n\n");
53a5c424 573#ifdef CONFIG_MCAST_TFTP
aafda38f 574 mcast_cleanup();
53a5c424 575#endif
aafda38f
RB
576 NetStartAgain();
577 break;
578 }
fe8c2806
WD
579 break;
580 }
581}
582
583
584static void
c718b143 585TftpTimeout(void)
fe8c2806 586{
e83cc063 587 if (++TftpTimeoutCount > TftpTimeoutCountMax) {
e4cde2f7 588 restart("Retry count exceeded");
fe8c2806 589 } else {
c718b143
LC
590 puts("T ");
591 NetSetTimeout(TftpTimeoutMSecs, TftpTimeout);
e59e3562
LC
592 if (TftpState != STATE_RECV_WRQ)
593 TftpSend();
fe8c2806
WD
594 }
595}
596
597
58f317d1 598void TftpStart(enum proto_t protocol)
fe8c2806 599{
ecb0ccd9 600 char *ep; /* Environment pointer */
89ba81d1 601
c96f86ee
WD
602 /*
603 * Allow the user to choose TFTP blocksize and timeout.
604 * TFTP protocol has a minimal timeout of 1 second.
605 */
2cb53608
LC
606 ep = getenv("tftpblocksize");
607 if (ep != NULL)
89ba81d1 608 TftpBlkSizeOption = simple_strtol(ep, NULL, 10);
c96f86ee 609
2cb53608
LC
610 ep = getenv("tftptimeout");
611 if (ep != NULL)
c96f86ee
WD
612 TftpTimeoutMSecs = simple_strtol(ep, NULL, 10);
613
614 if (TftpTimeoutMSecs < 1000) {
615 printf("TFTP timeout (%ld ms) too low, "
616 "set minimum = 1000 ms\n",
617 TftpTimeoutMSecs);
618 TftpTimeoutMSecs = 1000;
619 }
620
621 debug("TFTP blocksize = %i, timeout = %ld ms\n",
622 TftpBlkSizeOption, TftpTimeoutMSecs);
ecb0ccd9 623
20478cea 624 TftpRemoteIP = NetServerIP;
fe8c2806 625 if (BootFile[0] == '\0') {
fe8c2806 626 sprintf(default_filename, "%02lX%02lX%02lX%02lX.img",
c43352cc
WD
627 NetOurIP & 0xFF,
628 (NetOurIP >> 8) & 0xFF,
629 (NetOurIP >> 16) & 0xFF,
c718b143 630 (NetOurIP >> 24) & 0xFF);
a93907c4
JCPV
631
632 strncpy(tftp_filename, default_filename, MAX_LEN);
633 tftp_filename[MAX_LEN-1] = 0;
fe8c2806 634
c718b143 635 printf("*** Warning: no boot file name; using '%s'\n",
fe8c2806
WD
636 tftp_filename);
637 } else {
c718b143 638 char *p = strchr(BootFile, ':');
a93907c4
JCPV
639
640 if (p == NULL) {
641 strncpy(tftp_filename, BootFile, MAX_LEN);
642 tftp_filename[MAX_LEN-1] = 0;
643 } else {
20478cea 644 TftpRemoteIP = string_to_ip(BootFile);
6a86bb6c 645 strncpy(tftp_filename, p + 1, MAX_LEN);
a93907c4
JCPV
646 tftp_filename[MAX_LEN-1] = 0;
647 }
fe8c2806
WD
648 }
649
c718b143 650 printf("Using %s device\n", eth_get_name());
b6446b67 651 printf("TFTP from server %pI4"
20478cea 652 "; our IP address is %pI4", &TftpRemoteIP, &NetOurIP);
fe8c2806
WD
653
654 /* Check if we need to send across this subnet */
655 if (NetOurGatewayIP && NetOurSubnetMask) {
0bdd8acc 656 IPaddr_t OurNet = NetOurIP & NetOurSubnetMask;
20478cea 657 IPaddr_t RemoteNet = TftpRemoteIP & NetOurSubnetMask;
fe8c2806 658
20478cea 659 if (OurNet != RemoteNet)
0bdd8acc
LC
660 printf("; sending through gateway %pI4",
661 &NetOurGatewayIP);
fe8c2806 662 }
c718b143 663 putc('\n');
fe8c2806 664
c718b143 665 printf("Filename '%s'.", tftp_filename);
fe8c2806
WD
666
667 if (NetBootFileSize) {
c718b143
LC
668 printf(" Size is 0x%x Bytes = ", NetBootFileSize<<9);
669 print_size(NetBootFileSize<<9, "");
fe8c2806
WD
670 }
671
c718b143 672 putc('\n');
fe8c2806 673
c718b143 674 printf("Load address: 0x%lx\n", load_addr);
fe8c2806 675
c718b143 676 puts("Loading: *\b");
fe8c2806 677
e83cc063
BS
678 TftpTimeoutCountMax = TftpRRQTimeoutCountMax;
679
c718b143
LC
680 NetSetTimeout(TftpTimeoutMSecs, TftpTimeout);
681 NetSetHandler(TftpHandler);
fe8c2806 682
20478cea 683 TftpRemotePort = WELL_KNOWN_PORT;
fe8c2806 684 TftpTimeoutCount = 0;
e3fb0abe 685 TftpState = STATE_SEND_RRQ;
ecb0ccd9 686 /* Use a pseudo-random port unless a specific port is set */
fe8c2806 687 TftpOurPort = 1024 + (get_timer(0) % 3072);
53a5c424 688
ecb0ccd9 689#ifdef CONFIG_TFTP_PORT
2cb53608 690 ep = getenv("tftpdstp");
7bc325a1 691 if (ep != NULL)
20478cea 692 TftpRemotePort = simple_strtol(ep, NULL, 10);
2cb53608 693 ep = getenv("tftpsrcp");
7bc325a1 694 if (ep != NULL)
c718b143 695 TftpOurPort = simple_strtol(ep, NULL, 10);
ecb0ccd9 696#endif
fbe4b5cb 697 TftpBlock = 0;
fe8c2806 698
73a8b27c
WD
699 /* zero out server ether in case the server ip has changed */
700 memset(NetServerEther, 0, 6);
53a5c424
DU
701 /* Revert TftpBlkSize to dflt */
702 TftpBlkSize = TFTP_BLOCK_SIZE;
703#ifdef CONFIG_MCAST_TFTP
a93907c4 704 mcast_cleanup();
53a5c424 705#endif
4fccb818
RG
706#ifdef CONFIG_TFTP_TSIZE
707 TftpTsize = 0;
708 TftpNumchars = 0;
709#endif
73a8b27c 710
c718b143 711 TftpSend();
fe8c2806
WD
712}
713
e59e3562
LC
714#ifdef CONFIG_CMD_TFTPSRV
715void
716TftpStartServer(void)
717{
718 tftp_filename[0] = 0;
719
e59e3562 720 printf("Using %s device\n", eth_get_name());
e59e3562
LC
721 printf("Listening for TFTP transfer on %pI4\n", &NetOurIP);
722 printf("Load address: 0x%lx\n", load_addr);
723
724 puts("Loading: *\b");
725
726 TftpTimeoutCountMax = TIMEOUT_COUNT;
727 TftpTimeoutCount = 0;
728 TftpTimeoutMSecs = TIMEOUT;
729 NetSetTimeout(TftpTimeoutMSecs, TftpTimeout);
730
731 /* Revert TftpBlkSize to dflt */
732 TftpBlkSize = TFTP_BLOCK_SIZE;
733 TftpBlock = 0;
734 TftpOurPort = WELL_KNOWN_PORT;
735
736#ifdef CONFIG_TFTP_TSIZE
737 TftpTsize = 0;
738 TftpNumchars = 0;
739#endif
740
741 TftpState = STATE_RECV_WRQ;
742 NetSetHandler(TftpHandler);
743}
744#endif /* CONFIG_CMD_TFTPSRV */
745
53a5c424
DU
746#ifdef CONFIG_MCAST_TFTP
747/* Credits: atftp project.
748 */
749
750/* pick up BcastAddr, Port, and whether I am [now] the master-client. *
751 * Frame:
752 * +-------+-----------+---+-------~~-------+---+
753 * | opc | multicast | 0 | addr, port, mc | 0 |
754 * +-------+-----------+---+-------~~-------+---+
755 * The multicast addr/port becomes what I listen to, and if 'mc' is '1' then
756 * I am the new master-client so must send ACKs to DataBlocks. If I am not
757 * master-client, I'm a passive client, gathering what DataBlocks I may and
758 * making note of which ones I got in my bitmask.
759 * In theory, I never go from master->passive..
760 * .. this comes in with pkt already pointing just past opc
761 */
762static void parse_multicast_oack(char *pkt, int len)
763{
c718b143
LC
764 int i;
765 IPaddr_t addr;
766 char *mc_adr, *port, *mc;
53a5c424 767
c718b143 768 mc_adr = port = mc = NULL;
53a5c424
DU
769 /* march along looking for 'multicast\0', which has to start at least
770 * 14 bytes back from the end.
771 */
c718b143
LC
772 for (i = 0; i < len-14; i++)
773 if (strcmp(pkt+i, "multicast") == 0)
53a5c424
DU
774 break;
775 if (i >= (len-14)) /* non-Multicast OACK, ign. */
776 return;
85eb5caf 777
c718b143 778 i += 10; /* strlen multicast */
53a5c424 779 mc_adr = pkt+i;
c718b143 780 for (; i < len; i++) {
53a5c424
DU
781 if (*(pkt+i) == ',') {
782 *(pkt+i) = '\0';
783 if (port) {
784 mc = pkt+i+1;
785 break;
786 } else {
787 port = pkt+i+1;
788 }
789 }
790 }
6d2231e8
LC
791 if (!port || !mc_adr || !mc)
792 return;
53a5c424 793 if (Multicast && MasterClient) {
c718b143 794 printf("I got a OACK as master Client, WRONG!\n");
53a5c424
DU
795 return;
796 }
797 /* ..I now accept packets destined for this MCAST addr, port */
798 if (!Multicast) {
799 if (Bitmap) {
c718b143 800 printf("Internal failure! no mcast.\n");
53a5c424 801 free(Bitmap);
c718b143
LC
802 Bitmap = NULL;
803 ProhibitMcast = 1;
53a5c424
DU
804 return ;
805 }
806 /* I malloc instead of pre-declare; so that if the file ends
85eb5caf
WD
807 * up being too big for this bitmap I can retry
808 */
2cb53608
LC
809 Bitmap = malloc(Mapsize);
810 if (!Bitmap) {
c718b143
LC
811 printf("No Bitmap, no multicast. Sorry.\n");
812 ProhibitMcast = 1;
53a5c424
DU
813 return;
814 }
c718b143 815 memset(Bitmap, 0, Mapsize);
53a5c424
DU
816 PrevBitmapHole = 0;
817 Multicast = 1;
818 }
819 addr = string_to_ip(mc_adr);
820 if (Mcast_addr != addr) {
821 if (Mcast_addr)
822 eth_mcast_join(Mcast_addr, 0);
2cb53608
LC
823 Mcast_addr = addr;
824 if (eth_mcast_join(Mcast_addr, 1)) {
c718b143
LC
825 printf("Fail to set mcast, revert to TFTP\n");
826 ProhibitMcast = 1;
53a5c424
DU
827 mcast_cleanup();
828 NetStartAgain();
829 }
830 }
c718b143
LC
831 MasterClient = (unsigned char)simple_strtoul((char *)mc, NULL, 10);
832 Mcast_port = (unsigned short)simple_strtoul(port, NULL, 10);
833 printf("Multicast: %s:%d [%d]\n", mc_adr, Mcast_port, MasterClient);
53a5c424
DU
834 return;
835}
836
837#endif /* Multicast TFTP */