]> git.ipfire.org Git - people/ms/u-boot.git/blame - net/tftp.c
net: cosmetic: Name ethaddr variables consistently
[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>
55d5fd9a 11#include <mapmem.h>
fe8c2806
WD
12#include <net.h>
13#include "tftp.h"
14#include "bootp.h"
13dfe943
JH
15#ifdef CONFIG_SYS_DIRECT_FLASH_TFTP
16#include <flash.h>
17#endif
fe8c2806 18
2f09413f
LC
19/* Well known TFTP port # */
20#define WELL_KNOWN_PORT 69
21/* Millisecs to timeout for lost pkt */
22#define TIMEOUT 5000UL
fe8c2806 23#ifndef CONFIG_NET_RETRY_COUNT
2f09413f
LC
24/* # of timeouts before giving up */
25# define TIMEOUT_COUNT 10
fe8c2806
WD
26#else
27# define TIMEOUT_COUNT (CONFIG_NET_RETRY_COUNT * 2)
28#endif
2f09413f
LC
29/* Number of "loading" hashes per line (for checking the image size) */
30#define HASHES_PER_LINE 65
fe8c2806
WD
31
32/*
33 * TFTP operations.
34 */
35#define TFTP_RRQ 1
36#define TFTP_WRQ 2
37#define TFTP_DATA 3
38#define TFTP_ACK 4
39#define TFTP_ERROR 5
fbe4b5cb 40#define TFTP_OACK 6
fe8c2806 41
e83cc063
BS
42static ulong TftpTimeoutMSecs = TIMEOUT;
43static int TftpTimeoutCountMax = TIMEOUT_COUNT;
85b19802 44static ulong time_start; /* Record time we started tftp */
e83cc063
BS
45
46/*
47 * These globals govern the timeout behavior when attempting a connection to a
48 * TFTP server. TftpRRQTimeoutMSecs specifies the number of milliseconds to
49 * wait for the server to respond to initial connection. Second global,
50 * TftpRRQTimeoutCountMax, gives the number of such connection retries.
51 * TftpRRQTimeoutCountMax must be non-negative and TftpRRQTimeoutMSecs must be
52 * positive. The globals are meant to be set (and restored) by code needing
53 * non-standard timeout behavior when initiating a TFTP transfer.
54 */
55ulong TftpRRQTimeoutMSecs = TIMEOUT;
56int TftpRRQTimeoutCountMax = TIMEOUT_COUNT;
57
aafda38f
RB
58enum {
59 TFTP_ERR_UNDEFINED = 0,
60 TFTP_ERR_FILE_NOT_FOUND = 1,
61 TFTP_ERR_ACCESS_DENIED = 2,
62 TFTP_ERR_DISK_FULL = 3,
63 TFTP_ERR_UNEXPECTED_OPCODE = 4,
64 TFTP_ERR_UNKNOWN_TRANSFER_ID = 5,
65 TFTP_ERR_FILE_ALREADY_EXISTS = 6,
66};
67
049a95a7 68static struct in_addr tftp_remote_ip;
2f09413f 69/* The UDP port at their end */
20478cea 70static int TftpRemotePort;
2f09413f
LC
71/* The UDP port at our end */
72static int TftpOurPort;
fe8c2806 73static int TftpTimeoutCount;
2f09413f
LC
74/* packet sequence number */
75static ulong TftpBlock;
76/* last packet sequence number received */
77static ulong TftpLastBlock;
78/* count of sequence number wraparounds */
79static ulong TftpBlockWrap;
80/* memory offset due to wrapping */
81static ulong TftpBlockWrapOffset;
fe8c2806 82static int TftpState;
4fccb818 83#ifdef CONFIG_TFTP_TSIZE
2f09413f
LC
84/* The file size reported by the server */
85static int TftpTsize;
86/* The number of hashes we printed */
87static short TftpNumchars;
4fccb818 88#endif
1fb7cd49
SG
89#ifdef CONFIG_CMD_TFTPPUT
90static int TftpWriting; /* 1 if writing, else 0 */
91static int TftpFinalBlock; /* 1 if we have sent the last block */
92#else
93#define TftpWriting 0
94#endif
3f85ce27 95
e3fb0abe 96#define STATE_SEND_RRQ 1
fe8c2806
WD
97#define STATE_DATA 2
98#define STATE_TOO_LARGE 3
99#define STATE_BAD_MAGIC 4
fbe4b5cb 100#define STATE_OACK 5
e59e3562 101#define STATE_RECV_WRQ 6
1fb7cd49 102#define STATE_SEND_WRQ 7
fe8c2806 103
2f09413f
LC
104/* default TFTP block size */
105#define TFTP_BLOCK_SIZE 512
106/* sequence number is 16 bit */
107#define TFTP_SEQUENCE_SIZE ((ulong)(1<<16))
3f85ce27 108
fe8c2806
WD
109#define DEFAULT_NAME_LEN (8 + 4 + 1)
110static char default_filename[DEFAULT_NAME_LEN];
a93907c4
JCPV
111
112#ifndef CONFIG_TFTP_FILE_NAME_MAX_LEN
113#define MAX_LEN 128
114#else
115#define MAX_LEN CONFIG_TFTP_FILE_NAME_MAX_LEN
116#endif
117
118static char tftp_filename[MAX_LEN];
fe8c2806 119
85eb5caf
WD
120/* 512 is poor choice for ethernet, MTU is typically 1500.
121 * Minus eth.hdrs thats 1468. Can get 2x better throughput with
53a5c424 122 * almost-MTU block sizes. At least try... fall back to 512 if need be.
89ba81d1 123 * (but those using CONFIG_IP_DEFRAG may want to set a larger block in cfg file)
53a5c424 124 */
89ba81d1
AR
125#ifdef CONFIG_TFTP_BLOCKSIZE
126#define TFTP_MTU_BLOCKSIZE CONFIG_TFTP_BLOCKSIZE
127#else
53a5c424 128#define TFTP_MTU_BLOCKSIZE 1468
89ba81d1
AR
129#endif
130
c718b143
LC
131static unsigned short TftpBlkSize = TFTP_BLOCK_SIZE;
132static unsigned short TftpBlkSizeOption = TFTP_MTU_BLOCKSIZE;
53a5c424
DU
133
134#ifdef CONFIG_MCAST_TFTP
135#include <malloc.h>
136#define MTFTP_BITMAPSIZE 0x1000
137static unsigned *Bitmap;
c718b143 138static int PrevBitmapHole, Mapsize = MTFTP_BITMAPSIZE;
9bb0a1bf
LC
139static uchar ProhibitMcast, MasterClient;
140static uchar Multicast;
53a5c424
DU
141static int Mcast_port;
142static ulong TftpEndingBlock; /* can get 'last' block before done..*/
143
c718b143 144static void parse_multicast_oack(char *pkt, int len);
53a5c424
DU
145
146static void
147mcast_cleanup(void)
148{
049a95a7
JH
149 if (net_mcast_addr)
150 eth_mcast_join(net_mcast_addr, 0);
6d2231e8
LC
151 if (Bitmap)
152 free(Bitmap);
c718b143 153 Bitmap = NULL;
049a95a7
JH
154 net_mcast_addr.s_addr = 0;
155 Multicast = 0;
156 Mcast_port = 0;
53a5c424
DU
157 TftpEndingBlock = -1;
158}
159
160#endif /* CONFIG_MCAST_TFTP */
161
13dfe943 162static inline void
bc46dfac 163store_block(int block, uchar *src, unsigned len)
fe8c2806 164{
53a5c424 165 ulong offset = block * TftpBlkSize + TftpBlockWrapOffset;
3f85ce27 166 ulong newsize = offset + len;
6d0f6bcf 167#ifdef CONFIG_SYS_DIRECT_FLASH_TFTP
fe8c2806
WD
168 int i, rc = 0;
169
c718b143 170 for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; i++) {
fe8c2806 171 /* start address in flash? */
be1b0d27
JF
172 if (flash_info[i].flash_id == FLASH_UNKNOWN)
173 continue;
fe8c2806
WD
174 if (load_addr + offset >= flash_info[i].start[0]) {
175 rc = 1;
176 break;
177 }
178 }
179
180 if (rc) { /* Flash is destination for this packet */
c718b143 181 rc = flash_write((char *)src, (ulong)(load_addr+offset), len);
fe8c2806 182 if (rc) {
c718b143 183 flash_perror(rc);
22f6e99d 184 net_set_state(NETLOOP_FAIL);
fe8c2806
WD
185 return;
186 }
13dfe943 187 } else
6d0f6bcf 188#endif /* CONFIG_SYS_DIRECT_FLASH_TFTP */
fe8c2806 189 {
55d5fd9a
JH
190 void *ptr = map_sysmem(load_addr + offset, len);
191
192 memcpy(ptr, src, len);
193 unmap_sysmem(ptr);
fe8c2806 194 }
53a5c424
DU
195#ifdef CONFIG_MCAST_TFTP
196 if (Multicast)
197 ext2_set_bit(block, Bitmap);
198#endif
fe8c2806 199
1411157d
JH
200 if (net_boot_file_size < newsize)
201 net_boot_file_size = newsize;
fe8c2806
WD
202}
203
e4cde2f7 204/* Clear our state ready for a new transfer */
165099e7 205static void new_transfer(void)
e4cde2f7
SG
206{
207 TftpLastBlock = 0;
208 TftpBlockWrap = 0;
209 TftpBlockWrapOffset = 0;
210#ifdef CONFIG_CMD_TFTPPUT
211 TftpFinalBlock = 0;
212#endif
213}
214
1fb7cd49
SG
215#ifdef CONFIG_CMD_TFTPPUT
216/**
217 * Load the next block from memory to be sent over tftp.
218 *
219 * @param block Block number to send
220 * @param dst Destination buffer for data
221 * @param len Number of bytes in block (this one and every other)
222 * @return number of bytes loaded
223 */
224static int load_block(unsigned block, uchar *dst, unsigned len)
225{
226 /* We may want to get the final block from the previous set */
227 ulong offset = ((int)block - 1) * len + TftpBlockWrapOffset;
228 ulong tosend = len;
229
1411157d 230 tosend = min(net_boot_file_size - offset, tosend);
1fb7cd49
SG
231 (void)memcpy(dst, (void *)(save_addr + offset), tosend);
232 debug("%s: block=%d, offset=%ld, len=%d, tosend=%ld\n", __func__,
233 block, offset, len, tosend);
234 return tosend;
235}
236#endif
237
c718b143
LC
238static void TftpSend(void);
239static void TftpTimeout(void);
fe8c2806
WD
240
241/**********************************************************************/
242
f5329bbc
SG
243static void show_block_marker(void)
244{
245#ifdef CONFIG_TFTP_TSIZE
246 if (TftpTsize) {
247 ulong pos = TftpBlock * TftpBlkSize + TftpBlockWrapOffset;
248
249 while (TftpNumchars < pos * 50 / TftpTsize) {
250 putc('#');
251 TftpNumchars++;
252 }
1fb7cd49 253 } else
f5329bbc 254#endif
1fb7cd49 255 {
f5329bbc
SG
256 if (((TftpBlock - 1) % 10) == 0)
257 putc('#');
258 else if ((TftpBlock % (10 * HASHES_PER_LINE)) == 0)
259 puts("\n\t ");
260 }
261}
262
e4cde2f7
SG
263/**
264 * restart the current transfer due to an error
265 *
266 * @param msg Message to print for user
267 */
268static void restart(const char *msg)
269{
270 printf("\n%s; starting again\n", msg);
271#ifdef CONFIG_MCAST_TFTP
272 mcast_cleanup();
273#endif
274 NetStartAgain();
275}
276
277/*
278 * Check if the block number has wrapped, and update progress
279 *
280 * TODO: The egregious use of global variables in this file should be tidied.
281 */
282static void update_block_number(void)
283{
284 /*
285 * RFC1350 specifies that the first data packet will
286 * have sequence number 1. If we receive a sequence
287 * number of 0 this means that there was a wrap
288 * around of the (16 bit) counter.
289 */
f754f5dc 290 if (TftpBlock == 0 && TftpLastBlock != 0) {
e4cde2f7
SG
291 TftpBlockWrap++;
292 TftpBlockWrapOffset += TftpBlkSize * TFTP_SEQUENCE_SIZE;
293 TftpTimeoutCount = 0; /* we've done well, reset thhe timeout */
294 } else {
295 show_block_marker();
296 }
297}
298
f5329bbc
SG
299/* The TFTP get or put is complete */
300static void tftp_complete(void)
301{
302#ifdef CONFIG_TFTP_TSIZE
303 /* Print hash marks for the last packet received */
304 while (TftpTsize && TftpNumchars < 49) {
305 putc('#');
306 TftpNumchars++;
307 }
8104f546
SG
308 puts(" ");
309 print_size(TftpTsize, "");
f5329bbc 310#endif
85b19802
SG
311 time_start = get_timer(time_start);
312 if (time_start > 0) {
313 puts("\n\t "); /* Line up with "Loading: " */
1411157d 314 print_size(net_boot_file_size /
85b19802
SG
315 time_start * 1000, "/s");
316 }
f5329bbc 317 puts("\ndone\n");
22f6e99d 318 net_set_state(NETLOOP_SUCCESS);
f5329bbc
SG
319}
320
fe8c2806 321static void
c718b143 322TftpSend(void)
fe8c2806 323{
1fb7cd49 324 uchar *pkt;
db288a96
JH
325 uchar *xp;
326 int len = 0;
327 ushort *s;
fe8c2806 328
85eb5caf 329#ifdef CONFIG_MCAST_TFTP
53a5c424 330 /* Multicast TFTP.. non-MasterClients do not ACK data. */
85eb5caf
WD
331 if (Multicast
332 && (TftpState == STATE_DATA)
333 && (MasterClient == 0))
53a5c424
DU
334 return;
335#endif
fe8c2806
WD
336 /*
337 * We will always be sending some sort of packet, so
338 * cobble together the packet headers now.
339 */
594c26f8 340 pkt = NetTxPacket + NetEthHdrSize() + IP_UDP_HDR_SIZE;
fe8c2806
WD
341
342 switch (TftpState) {
e3fb0abe 343 case STATE_SEND_RRQ:
1fb7cd49 344 case STATE_SEND_WRQ:
fe8c2806 345 xp = pkt;
7bc5ee07 346 s = (ushort *)pkt;
8c6914f1 347#ifdef CONFIG_CMD_TFTPPUT
1fb7cd49
SG
348 *s++ = htons(TftpState == STATE_SEND_RRQ ? TFTP_RRQ :
349 TFTP_WRQ);
8c6914f1
SG
350#else
351 *s++ = htons(TFTP_RRQ);
352#endif
7bc5ee07 353 pkt = (uchar *)s;
c718b143 354 strcpy((char *)pkt, tftp_filename);
fe8c2806 355 pkt += strlen(tftp_filename) + 1;
c718b143 356 strcpy((char *)pkt, "octet");
fe8c2806 357 pkt += 5 /*strlen("octet")*/ + 1;
c718b143 358 strcpy((char *)pkt, "timeout");
fbe4b5cb 359 pkt += 7 /*strlen("timeout")*/ + 1;
c96f86ee 360 sprintf((char *)pkt, "%lu", TftpTimeoutMSecs / 1000);
0ebf04c6 361 debug("send option \"timeout %s\"\n", (char *)pkt);
fbe4b5cb 362 pkt += strlen((char *)pkt) + 1;
4fccb818 363#ifdef CONFIG_TFTP_TSIZE
1411157d
JH
364 pkt += sprintf((char *)pkt, "tsize%c%u%c",
365 0, net_boot_file_size, 0);
4fccb818 366#endif
53a5c424 367 /* try for more effic. blk size */
c718b143
LC
368 pkt += sprintf((char *)pkt, "blksize%c%d%c",
369 0, TftpBlkSizeOption, 0);
85eb5caf 370#ifdef CONFIG_MCAST_TFTP
53a5c424 371 /* Check all preconditions before even trying the option */
13dfe943
JH
372 if (!ProhibitMcast) {
373 Bitmap = malloc(Mapsize);
374 if (Bitmap && eth_get_dev()->mcast) {
375 free(Bitmap);
376 Bitmap = NULL;
377 pkt += sprintf((char *)pkt, "multicast%c%c",
378 0, 0);
379 }
53a5c424
DU
380 }
381#endif /* CONFIG_MCAST_TFTP */
fe8c2806
WD
382 len = pkt - xp;
383 break;
384
fbe4b5cb 385 case STATE_OACK:
53a5c424
DU
386#ifdef CONFIG_MCAST_TFTP
387 /* My turn! Start at where I need blocks I missed.*/
388 if (Multicast)
c718b143
LC
389 TftpBlock = ext2_find_next_zero_bit(Bitmap,
390 (Mapsize*8), 0);
53a5c424
DU
391 /*..falling..*/
392#endif
e59e3562
LC
393
394 case STATE_RECV_WRQ:
53a5c424 395 case STATE_DATA:
fe8c2806 396 xp = pkt;
7bc5ee07 397 s = (ushort *)pkt;
1fb7cd49
SG
398 s[0] = htons(TFTP_ACK);
399 s[1] = htons(TftpBlock);
400 pkt = (uchar *)(s + 2);
401#ifdef CONFIG_CMD_TFTPPUT
402 if (TftpWriting) {
403 int toload = TftpBlkSize;
404 int loaded = load_block(TftpBlock, pkt, toload);
405
406 s[0] = htons(TFTP_DATA);
407 pkt += loaded;
408 TftpFinalBlock = (loaded < toload);
409 }
410#endif
fe8c2806
WD
411 len = pkt - xp;
412 break;
413
414 case STATE_TOO_LARGE:
415 xp = pkt;
7bc5ee07
WD
416 s = (ushort *)pkt;
417 *s++ = htons(TFTP_ERROR);
1fb7cd49
SG
418 *s++ = htons(3);
419
7bc5ee07 420 pkt = (uchar *)s;
c718b143 421 strcpy((char *)pkt, "File too large");
fe8c2806
WD
422 pkt += 14 /*strlen("File too large")*/ + 1;
423 len = pkt - xp;
424 break;
425
426 case STATE_BAD_MAGIC:
427 xp = pkt;
7bc5ee07
WD
428 s = (ushort *)pkt;
429 *s++ = htons(TFTP_ERROR);
430 *s++ = htons(2);
431 pkt = (uchar *)s;
c718b143 432 strcpy((char *)pkt, "File has bad magic");
fe8c2806
WD
433 pkt += 18 /*strlen("File has bad magic")*/ + 1;
434 len = pkt - xp;
435 break;
436 }
437
0adb5b76 438 NetSendUDPPacket(net_server_ethaddr, tftp_remote_ip, TftpRemotePort,
2f09413f 439 TftpOurPort, len);
fe8c2806
WD
440}
441
39bccd21 442#ifdef CONFIG_CMD_TFTPPUT
1fb7cd49 443static void icmp_handler(unsigned type, unsigned code, unsigned dest,
049a95a7
JH
444 struct in_addr sip, unsigned src, uchar *pkt,
445 unsigned len)
1fb7cd49
SG
446{
447 if (type == ICMP_NOT_REACH && code == ICMP_NOT_REACH_PORT) {
448 /* Oh dear the other end has gone away */
449 restart("TFTP server died");
450 }
451}
39bccd21 452#endif
1fb7cd49 453
049a95a7
JH
454static void tftp_handler(uchar *pkt, unsigned dest, struct in_addr sip,
455 unsigned src, unsigned len)
fe8c2806 456{
61fdd4f7
KP
457 __be16 proto;
458 __be16 *s;
ff13ac8c 459 int i;
fe8c2806
WD
460
461 if (dest != TftpOurPort) {
53a5c424 462#ifdef CONFIG_MCAST_TFTP
85eb5caf 463 if (Multicast
53a5c424
DU
464 && (!Mcast_port || (dest != Mcast_port)))
465#endif
0bdd8acc 466 return;
fe8c2806 467 }
e59e3562 468 if (TftpState != STATE_SEND_RRQ && src != TftpRemotePort &&
1fb7cd49 469 TftpState != STATE_RECV_WRQ && TftpState != STATE_SEND_WRQ)
fe8c2806 470 return;
fe8c2806 471
7bc325a1 472 if (len < 2)
fe8c2806 473 return;
fe8c2806
WD
474 len -= 2;
475 /* warning: don't use increment (++) in ntohs() macros!! */
61fdd4f7 476 s = (__be16 *)pkt;
7bc5ee07
WD
477 proto = *s++;
478 pkt = (uchar *)s;
fe8c2806
WD
479 switch (ntohs(proto)) {
480
481 case TFTP_RRQ:
1fb7cd49
SG
482 break;
483
fe8c2806 484 case TFTP_ACK:
1fb7cd49
SG
485#ifdef CONFIG_CMD_TFTPPUT
486 if (TftpWriting) {
487 if (TftpFinalBlock) {
488 tftp_complete();
489 } else {
490 /*
491 * Move to the next block. We want our block
492 * count to wrap just like the other end!
493 */
494 int block = ntohs(*s);
495 int ack_ok = (TftpBlock == block);
496
497 TftpBlock = (unsigned short)(block + 1);
498 update_block_number();
499 if (ack_ok)
500 TftpSend(); /* Send next data block */
501 }
502 }
503#endif
fe8c2806 504 break;
1fb7cd49 505
fe8c2806
WD
506 default:
507 break;
508
e59e3562
LC
509#ifdef CONFIG_CMD_TFTPSRV
510 case TFTP_WRQ:
511 debug("Got WRQ\n");
049a95a7 512 tftp_remote_ip = sip;
e59e3562
LC
513 TftpRemotePort = src;
514 TftpOurPort = 1024 + (get_timer(0) % 3072);
e4cde2f7 515 new_transfer();
e59e3562
LC
516 TftpSend(); /* Send ACK(0) */
517 break;
518#endif
519
fbe4b5cb 520 case TFTP_OACK:
d371708a
WD
521 debug("Got OACK: %s %s\n",
522 pkt,
523 pkt + strlen((char *)pkt) + 1);
fbe4b5cb 524 TftpState = STATE_OACK;
20478cea 525 TftpRemotePort = src;
60174746
WD
526 /*
527 * Check for 'blksize' option.
528 * Careful: "i" is signed, "len" is unsigned, thus
529 * something like "len-8" may give a *huge* number
530 */
c718b143 531 for (i = 0; i+8 < len; i++) {
2e320257 532 if (strcmp((char *)pkt+i, "blksize") == 0) {
ff13ac8c 533 TftpBlkSize = (unsigned short)
2e320257 534 simple_strtoul((char *)pkt+i+8, NULL,
c718b143 535 10);
0ebf04c6 536 debug("Blocksize ack: %s, %d\n",
2e320257 537 (char *)pkt+i+8, TftpBlkSize);
ff13ac8c 538 }
4fccb818 539#ifdef CONFIG_TFTP_TSIZE
2e320257
LC
540 if (strcmp((char *)pkt+i, "tsize") == 0) {
541 TftpTsize = simple_strtoul((char *)pkt+i+6,
2f09413f 542 NULL, 10);
4fccb818 543 debug("size = %s, %d\n",
2e320257 544 (char *)pkt+i+6, TftpTsize);
4fccb818
RG
545 }
546#endif
53a5c424
DU
547 }
548#ifdef CONFIG_MCAST_TFTP
c718b143 549 parse_multicast_oack((char *)pkt, len-1);
85eb5caf 550 if ((Multicast) && (!MasterClient))
53a5c424
DU
551 TftpState = STATE_DATA; /* passive.. */
552 else
553#endif
1fb7cd49
SG
554#ifdef CONFIG_CMD_TFTPPUT
555 if (TftpWriting) {
556 /* Get ready to send the first block */
557 TftpState = STATE_DATA;
558 TftpBlock++;
559 }
560#endif
561 TftpSend(); /* Send ACK or first data block */
fbe4b5cb 562 break;
fe8c2806
WD
563 case TFTP_DATA:
564 if (len < 2)
565 return;
566 len -= 2;
61fdd4f7 567 TftpBlock = ntohs(*(__be16 *)pkt);
3f85ce27 568
e4cde2f7 569 update_block_number();
fe8c2806 570
e3fb0abe 571 if (TftpState == STATE_SEND_RRQ)
0ebf04c6 572 debug("Server did not acknowledge timeout option!\n");
fbe4b5cb 573
e59e3562
LC
574 if (TftpState == STATE_SEND_RRQ || TftpState == STATE_OACK ||
575 TftpState == STATE_RECV_WRQ) {
3f85ce27 576 /* first block received */
fe8c2806 577 TftpState = STATE_DATA;
20478cea 578 TftpRemotePort = src;
e4cde2f7 579 new_transfer();
fe8c2806 580
53a5c424
DU
581#ifdef CONFIG_MCAST_TFTP
582 if (Multicast) { /* start!=1 common if mcast */
583 TftpLastBlock = TftpBlock - 1;
584 } else
585#endif
fe8c2806 586 if (TftpBlock != 1) { /* Assertion */
c718b143
LC
587 printf("\nTFTP error: "
588 "First block is not block 1 (%ld)\n"
589 "Starting again\n\n",
fe8c2806 590 TftpBlock);
c718b143 591 NetStartAgain();
fe8c2806
WD
592 break;
593 }
594 }
595
596 if (TftpBlock == TftpLastBlock) {
597 /*
598 * Same block again; ignore it.
599 */
600 break;
601 }
602
603 TftpLastBlock = TftpBlock;
e83cc063 604 TftpTimeoutCountMax = TIMEOUT_COUNT;
c718b143 605 NetSetTimeout(TftpTimeoutMSecs, TftpTimeout);
fe8c2806 606
c718b143 607 store_block(TftpBlock - 1, pkt + 2, len);
fe8c2806
WD
608
609 /*
4d69e98c 610 * Acknowledge the block just received, which will prompt
20478cea 611 * the remote for the next one.
fe8c2806 612 */
53a5c424 613#ifdef CONFIG_MCAST_TFTP
85eb5caf
WD
614 /* if I am the MasterClient, actively calculate what my next
615 * needed block is; else I'm passive; not ACKING
a93907c4 616 */
53a5c424
DU
617 if (Multicast) {
618 if (len < TftpBlkSize) {
619 TftpEndingBlock = TftpBlock;
620 } else if (MasterClient) {
85eb5caf 621 TftpBlock = PrevBitmapHole =
53a5c424
DU
622 ext2_find_next_zero_bit(
623 Bitmap,
624 (Mapsize*8),
625 PrevBitmapHole);
626 if (TftpBlock > ((Mapsize*8) - 1)) {
c718b143 627 printf("tftpfile too big\n");
53a5c424 628 /* try to double it and retry */
c718b143 629 Mapsize <<= 1;
53a5c424 630 mcast_cleanup();
c718b143 631 NetStartAgain();
53a5c424
DU
632 return;
633 }
634 TftpLastBlock = TftpBlock;
635 }
636 }
637#endif
c718b143 638 TftpSend();
fe8c2806 639
53a5c424
DU
640#ifdef CONFIG_MCAST_TFTP
641 if (Multicast) {
642 if (MasterClient && (TftpBlock >= TftpEndingBlock)) {
c718b143 643 puts("\nMulticast tftp done\n");
53a5c424 644 mcast_cleanup();
22f6e99d 645 net_set_state(NETLOOP_SUCCESS);
85eb5caf 646 }
13dfe943 647 } else
53a5c424 648#endif
f5329bbc
SG
649 if (len < TftpBlkSize)
650 tftp_complete();
fe8c2806
WD
651 break;
652
653 case TFTP_ERROR:
c718b143 654 printf("\nTFTP error: '%s' (%d)\n",
61fdd4f7 655 pkt + 2, ntohs(*(__be16 *)pkt));
aafda38f 656
61fdd4f7 657 switch (ntohs(*(__be16 *)pkt)) {
aafda38f
RB
658 case TFTP_ERR_FILE_NOT_FOUND:
659 case TFTP_ERR_ACCESS_DENIED:
660 puts("Not retrying...\n");
661 eth_halt();
22f6e99d 662 net_set_state(NETLOOP_FAIL);
aafda38f
RB
663 break;
664 case TFTP_ERR_UNDEFINED:
665 case TFTP_ERR_DISK_FULL:
666 case TFTP_ERR_UNEXPECTED_OPCODE:
667 case TFTP_ERR_UNKNOWN_TRANSFER_ID:
668 case TFTP_ERR_FILE_ALREADY_EXISTS:
669 default:
670 puts("Starting again\n\n");
53a5c424 671#ifdef CONFIG_MCAST_TFTP
aafda38f 672 mcast_cleanup();
53a5c424 673#endif
aafda38f
RB
674 NetStartAgain();
675 break;
676 }
fe8c2806
WD
677 break;
678 }
679}
680
681
682static void
c718b143 683TftpTimeout(void)
fe8c2806 684{
e83cc063 685 if (++TftpTimeoutCount > TftpTimeoutCountMax) {
e4cde2f7 686 restart("Retry count exceeded");
fe8c2806 687 } else {
c718b143
LC
688 puts("T ");
689 NetSetTimeout(TftpTimeoutMSecs, TftpTimeout);
e59e3562
LC
690 if (TftpState != STATE_RECV_WRQ)
691 TftpSend();
fe8c2806
WD
692 }
693}
694
695
58f317d1 696void TftpStart(enum proto_t protocol)
fe8c2806 697{
ecb0ccd9 698 char *ep; /* Environment pointer */
89ba81d1 699
c96f86ee
WD
700 /*
701 * Allow the user to choose TFTP blocksize and timeout.
702 * TFTP protocol has a minimal timeout of 1 second.
703 */
2cb53608
LC
704 ep = getenv("tftpblocksize");
705 if (ep != NULL)
89ba81d1 706 TftpBlkSizeOption = simple_strtol(ep, NULL, 10);
c96f86ee 707
2cb53608
LC
708 ep = getenv("tftptimeout");
709 if (ep != NULL)
c96f86ee
WD
710 TftpTimeoutMSecs = simple_strtol(ep, NULL, 10);
711
712 if (TftpTimeoutMSecs < 1000) {
713 printf("TFTP timeout (%ld ms) too low, "
714 "set minimum = 1000 ms\n",
715 TftpTimeoutMSecs);
716 TftpTimeoutMSecs = 1000;
717 }
718
719 debug("TFTP blocksize = %i, timeout = %ld ms\n",
720 TftpBlkSizeOption, TftpTimeoutMSecs);
ecb0ccd9 721
049a95a7 722 tftp_remote_ip = net_server_ip;
1411157d 723 if (net_boot_file_name[0] == '\0') {
ea45cb0a 724 sprintf(default_filename, "%02X%02X%02X%02X.img",
049a95a7
JH
725 net_ip.s_addr & 0xFF,
726 (net_ip.s_addr >> 8) & 0xFF,
727 (net_ip.s_addr >> 16) & 0xFF,
728 (net_ip.s_addr >> 24) & 0xFF);
a93907c4
JCPV
729
730 strncpy(tftp_filename, default_filename, MAX_LEN);
731 tftp_filename[MAX_LEN-1] = 0;
fe8c2806 732
c718b143 733 printf("*** Warning: no boot file name; using '%s'\n",
fe8c2806
WD
734 tftp_filename);
735 } else {
1411157d 736 char *p = strchr(net_boot_file_name, ':');
a93907c4
JCPV
737
738 if (p == NULL) {
1411157d 739 strncpy(tftp_filename, net_boot_file_name, MAX_LEN);
a93907c4
JCPV
740 tftp_filename[MAX_LEN-1] = 0;
741 } else {
1411157d 742 tftp_remote_ip = string_to_ip(net_boot_file_name);
6a86bb6c 743 strncpy(tftp_filename, p + 1, MAX_LEN);
a93907c4
JCPV
744 tftp_filename[MAX_LEN-1] = 0;
745 }
fe8c2806
WD
746 }
747
c718b143 748 printf("Using %s device\n", eth_get_name());
1fb7cd49 749 printf("TFTP %s server %pI4; our IP address is %pI4",
8c6914f1
SG
750#ifdef CONFIG_CMD_TFTPPUT
751 protocol == TFTPPUT ? "to" : "from",
752#else
753 "from",
754#endif
049a95a7 755 &tftp_remote_ip, &net_ip);
fe8c2806
WD
756
757 /* Check if we need to send across this subnet */
049a95a7
JH
758 if (net_gateway.s_addr && net_netmask.s_addr) {
759 struct in_addr our_net;
760 struct in_addr remote_net;
761
762 our_net.s_addr = net_ip.s_addr & net_netmask.s_addr;
763 remote_net.s_addr = tftp_remote_ip.s_addr & net_netmask.s_addr;
764 if (our_net.s_addr != remote_net.s_addr)
765 printf("; sending through gateway %pI4", &net_gateway);
fe8c2806 766 }
c718b143 767 putc('\n');
fe8c2806 768
c718b143 769 printf("Filename '%s'.", tftp_filename);
fe8c2806 770
1411157d
JH
771 if (net_boot_file_expected_size_in_blocks) {
772 printf(" Size is 0x%x Bytes = ",
773 net_boot_file_expected_size_in_blocks << 9);
774 print_size(net_boot_file_expected_size_in_blocks << 9, "");
fe8c2806
WD
775 }
776
c718b143 777 putc('\n');
1fb7cd49
SG
778#ifdef CONFIG_CMD_TFTPPUT
779 TftpWriting = (protocol == TFTPPUT);
780 if (TftpWriting) {
781 printf("Save address: 0x%lx\n", save_addr);
782 printf("Save size: 0x%lx\n", save_size);
1411157d 783 net_boot_file_size = save_size;
1fb7cd49
SG
784 puts("Saving: *\b");
785 TftpState = STATE_SEND_WRQ;
786 new_transfer();
787 } else
788#endif
789 {
790 printf("Load address: 0x%lx\n", load_addr);
791 puts("Loading: *\b");
792 TftpState = STATE_SEND_RRQ;
793 }
fe8c2806 794
85b19802 795 time_start = get_timer(0);
e83cc063
BS
796 TftpTimeoutCountMax = TftpRRQTimeoutCountMax;
797
c718b143 798 NetSetTimeout(TftpTimeoutMSecs, TftpTimeout);
049a95a7 799 net_set_udp_handler(tftp_handler);
39bccd21 800#ifdef CONFIG_CMD_TFTPPUT
1fb7cd49 801 net_set_icmp_handler(icmp_handler);
39bccd21 802#endif
20478cea 803 TftpRemotePort = WELL_KNOWN_PORT;
fe8c2806 804 TftpTimeoutCount = 0;
ecb0ccd9 805 /* Use a pseudo-random port unless a specific port is set */
fe8c2806 806 TftpOurPort = 1024 + (get_timer(0) % 3072);
53a5c424 807
ecb0ccd9 808#ifdef CONFIG_TFTP_PORT
2cb53608 809 ep = getenv("tftpdstp");
7bc325a1 810 if (ep != NULL)
20478cea 811 TftpRemotePort = simple_strtol(ep, NULL, 10);
2cb53608 812 ep = getenv("tftpsrcp");
7bc325a1 813 if (ep != NULL)
c718b143 814 TftpOurPort = simple_strtol(ep, NULL, 10);
ecb0ccd9 815#endif
fbe4b5cb 816 TftpBlock = 0;
fe8c2806 817
73a8b27c 818 /* zero out server ether in case the server ip has changed */
0adb5b76 819 memset(net_server_ethaddr, 0, 6);
53a5c424
DU
820 /* Revert TftpBlkSize to dflt */
821 TftpBlkSize = TFTP_BLOCK_SIZE;
822#ifdef CONFIG_MCAST_TFTP
a93907c4 823 mcast_cleanup();
53a5c424 824#endif
4fccb818
RG
825#ifdef CONFIG_TFTP_TSIZE
826 TftpTsize = 0;
827 TftpNumchars = 0;
828#endif
73a8b27c 829
c718b143 830 TftpSend();
fe8c2806
WD
831}
832
e59e3562
LC
833#ifdef CONFIG_CMD_TFTPSRV
834void
835TftpStartServer(void)
836{
837 tftp_filename[0] = 0;
838
e59e3562 839 printf("Using %s device\n", eth_get_name());
049a95a7 840 printf("Listening for TFTP transfer on %pI4\n", &net_ip);
e59e3562
LC
841 printf("Load address: 0x%lx\n", load_addr);
842
843 puts("Loading: *\b");
844
845 TftpTimeoutCountMax = TIMEOUT_COUNT;
846 TftpTimeoutCount = 0;
847 TftpTimeoutMSecs = TIMEOUT;
848 NetSetTimeout(TftpTimeoutMSecs, TftpTimeout);
849
850 /* Revert TftpBlkSize to dflt */
851 TftpBlkSize = TFTP_BLOCK_SIZE;
852 TftpBlock = 0;
853 TftpOurPort = WELL_KNOWN_PORT;
854
855#ifdef CONFIG_TFTP_TSIZE
856 TftpTsize = 0;
857 TftpNumchars = 0;
858#endif
859
860 TftpState = STATE_RECV_WRQ;
049a95a7 861 net_set_udp_handler(tftp_handler);
8e52533d
AR
862
863 /* zero out server ether in case the server ip has changed */
0adb5b76 864 memset(net_server_ethaddr, 0, 6);
e59e3562
LC
865}
866#endif /* CONFIG_CMD_TFTPSRV */
867
53a5c424
DU
868#ifdef CONFIG_MCAST_TFTP
869/* Credits: atftp project.
870 */
871
872/* pick up BcastAddr, Port, and whether I am [now] the master-client. *
873 * Frame:
874 * +-------+-----------+---+-------~~-------+---+
875 * | opc | multicast | 0 | addr, port, mc | 0 |
876 * +-------+-----------+---+-------~~-------+---+
877 * The multicast addr/port becomes what I listen to, and if 'mc' is '1' then
878 * I am the new master-client so must send ACKs to DataBlocks. If I am not
879 * master-client, I'm a passive client, gathering what DataBlocks I may and
880 * making note of which ones I got in my bitmask.
881 * In theory, I never go from master->passive..
882 * .. this comes in with pkt already pointing just past opc
883 */
884static void parse_multicast_oack(char *pkt, int len)
885{
c718b143 886 int i;
049a95a7 887 struct in_addr addr;
c718b143 888 char *mc_adr, *port, *mc;
53a5c424 889
c718b143 890 mc_adr = port = mc = NULL;
53a5c424
DU
891 /* march along looking for 'multicast\0', which has to start at least
892 * 14 bytes back from the end.
893 */
c718b143
LC
894 for (i = 0; i < len-14; i++)
895 if (strcmp(pkt+i, "multicast") == 0)
53a5c424
DU
896 break;
897 if (i >= (len-14)) /* non-Multicast OACK, ign. */
898 return;
85eb5caf 899
c718b143 900 i += 10; /* strlen multicast */
53a5c424 901 mc_adr = pkt+i;
c718b143 902 for (; i < len; i++) {
53a5c424
DU
903 if (*(pkt+i) == ',') {
904 *(pkt+i) = '\0';
905 if (port) {
906 mc = pkt+i+1;
907 break;
908 } else {
909 port = pkt+i+1;
910 }
911 }
912 }
6d2231e8
LC
913 if (!port || !mc_adr || !mc)
914 return;
53a5c424 915 if (Multicast && MasterClient) {
c718b143 916 printf("I got a OACK as master Client, WRONG!\n");
53a5c424
DU
917 return;
918 }
919 /* ..I now accept packets destined for this MCAST addr, port */
920 if (!Multicast) {
921 if (Bitmap) {
c718b143 922 printf("Internal failure! no mcast.\n");
53a5c424 923 free(Bitmap);
c718b143
LC
924 Bitmap = NULL;
925 ProhibitMcast = 1;
53a5c424
DU
926 return ;
927 }
928 /* I malloc instead of pre-declare; so that if the file ends
85eb5caf
WD
929 * up being too big for this bitmap I can retry
930 */
2cb53608
LC
931 Bitmap = malloc(Mapsize);
932 if (!Bitmap) {
c718b143
LC
933 printf("No Bitmap, no multicast. Sorry.\n");
934 ProhibitMcast = 1;
53a5c424
DU
935 return;
936 }
c718b143 937 memset(Bitmap, 0, Mapsize);
53a5c424
DU
938 PrevBitmapHole = 0;
939 Multicast = 1;
940 }
941 addr = string_to_ip(mc_adr);
049a95a7
JH
942 if (net_mcast_addr.s_addr != addr.s_addr) {
943 if (net_mcast_addr.s_addr)
944 eth_mcast_join(net_mcast_addr, 0);
945 net_mcast_addr = addr;
946 if (eth_mcast_join(net_mcast_addr, 1)) {
c718b143
LC
947 printf("Fail to set mcast, revert to TFTP\n");
948 ProhibitMcast = 1;
53a5c424
DU
949 mcast_cleanup();
950 NetStartAgain();
951 }
952 }
c718b143
LC
953 MasterClient = (unsigned char)simple_strtoul((char *)mc, NULL, 10);
954 Mcast_port = (unsigned short)simple_strtoul(port, NULL, 10);
955 printf("Multicast: %s:%d [%d]\n", mc_adr, Mcast_port, MasterClient);
53a5c424
DU
956 return;
957}
958
959#endif /* Multicast TFTP */