2002-11-03 00:38:21 +00:00
|
|
|
/*
|
2011-05-14 05:49:56 +00:00
|
|
|
* Copyright 1994, 1995, 2000 Neil Russell.
|
|
|
|
* (See License)
|
|
|
|
* Copyright 2000, 2001 DENX Software Engineering, Wolfgang Denk, wd@denx.de
|
2011-05-17 00:03:39 +00:00
|
|
|
* Copyright 2011 Comelit Group SpA,
|
|
|
|
* Luca Ceresoli <luca.ceresoli@comelit.it>
|
2002-11-03 00:38:21 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <common.h>
|
|
|
|
#include <command.h>
|
|
|
|
#include <net.h>
|
|
|
|
#include "tftp.h"
|
|
|
|
#include "bootp.h"
|
|
|
|
|
2011-05-14 05:49:56 +00:00
|
|
|
/* Well known TFTP port # */
|
|
|
|
#define WELL_KNOWN_PORT 69
|
|
|
|
/* Millisecs to timeout for lost pkt */
|
|
|
|
#define TIMEOUT 5000UL
|
2002-11-03 00:38:21 +00:00
|
|
|
#ifndef CONFIG_NET_RETRY_COUNT
|
2011-05-14 05:49:56 +00:00
|
|
|
/* # of timeouts before giving up */
|
|
|
|
# define TIMEOUT_COUNT 10
|
2002-11-03 00:38:21 +00:00
|
|
|
#else
|
|
|
|
# define TIMEOUT_COUNT (CONFIG_NET_RETRY_COUNT * 2)
|
|
|
|
#endif
|
2011-05-14 05:49:56 +00:00
|
|
|
/* Number of "loading" hashes per line (for checking the image size) */
|
|
|
|
#define HASHES_PER_LINE 65
|
2002-11-03 00:38:21 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* TFTP operations.
|
|
|
|
*/
|
|
|
|
#define TFTP_RRQ 1
|
|
|
|
#define TFTP_WRQ 2
|
|
|
|
#define TFTP_DATA 3
|
|
|
|
#define TFTP_ACK 4
|
|
|
|
#define TFTP_ERROR 5
|
2003-10-06 21:55:32 +00:00
|
|
|
#define TFTP_OACK 6
|
2002-11-03 00:38:21 +00:00
|
|
|
|
2008-10-01 13:26:29 +00:00
|
|
|
static ulong TftpTimeoutMSecs = TIMEOUT;
|
|
|
|
static int TftpTimeoutCountMax = TIMEOUT_COUNT;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* These globals govern the timeout behavior when attempting a connection to a
|
|
|
|
* TFTP server. TftpRRQTimeoutMSecs specifies the number of milliseconds to
|
|
|
|
* wait for the server to respond to initial connection. Second global,
|
|
|
|
* TftpRRQTimeoutCountMax, gives the number of such connection retries.
|
|
|
|
* TftpRRQTimeoutCountMax must be non-negative and TftpRRQTimeoutMSecs must be
|
|
|
|
* positive. The globals are meant to be set (and restored) by code needing
|
|
|
|
* non-standard timeout behavior when initiating a TFTP transfer.
|
|
|
|
*/
|
|
|
|
ulong TftpRRQTimeoutMSecs = TIMEOUT;
|
|
|
|
int TftpRRQTimeoutCountMax = TIMEOUT_COUNT;
|
|
|
|
|
2009-10-28 21:13:40 +00:00
|
|
|
enum {
|
|
|
|
TFTP_ERR_UNDEFINED = 0,
|
|
|
|
TFTP_ERR_FILE_NOT_FOUND = 1,
|
|
|
|
TFTP_ERR_ACCESS_DENIED = 2,
|
|
|
|
TFTP_ERR_DISK_FULL = 3,
|
|
|
|
TFTP_ERR_UNEXPECTED_OPCODE = 4,
|
|
|
|
TFTP_ERR_UNKNOWN_TRANSFER_ID = 5,
|
|
|
|
TFTP_ERR_FILE_ALREADY_EXISTS = 6,
|
|
|
|
};
|
|
|
|
|
2011-05-17 00:03:37 +00:00
|
|
|
static IPaddr_t TftpRemoteIP;
|
2011-05-14 05:49:56 +00:00
|
|
|
/* The UDP port at their end */
|
2011-05-17 00:03:37 +00:00
|
|
|
static int TftpRemotePort;
|
2011-05-14 05:49:56 +00:00
|
|
|
/* The UDP port at our end */
|
|
|
|
static int TftpOurPort;
|
2002-11-03 00:38:21 +00:00
|
|
|
static int TftpTimeoutCount;
|
2011-05-14 05:49:56 +00:00
|
|
|
/* packet sequence number */
|
|
|
|
static ulong TftpBlock;
|
|
|
|
/* last packet sequence number received */
|
|
|
|
static ulong TftpLastBlock;
|
|
|
|
/* count of sequence number wraparounds */
|
|
|
|
static ulong TftpBlockWrap;
|
|
|
|
/* memory offset due to wrapping */
|
|
|
|
static ulong TftpBlockWrapOffset;
|
2002-11-03 00:38:21 +00:00
|
|
|
static int TftpState;
|
2009-08-20 14:50:20 +00:00
|
|
|
#ifdef CONFIG_TFTP_TSIZE
|
2011-05-14 05:49:56 +00:00
|
|
|
/* The file size reported by the server */
|
|
|
|
static int TftpTsize;
|
|
|
|
/* The number of hashes we printed */
|
|
|
|
static short TftpNumchars;
|
2009-08-20 14:50:20 +00:00
|
|
|
#endif
|
2004-02-23 16:11:30 +00:00
|
|
|
|
2011-05-17 00:03:38 +00:00
|
|
|
#define STATE_SEND_RRQ 1
|
2002-11-03 00:38:21 +00:00
|
|
|
#define STATE_DATA 2
|
|
|
|
#define STATE_TOO_LARGE 3
|
|
|
|
#define STATE_BAD_MAGIC 4
|
2003-10-06 21:55:32 +00:00
|
|
|
#define STATE_OACK 5
|
2011-05-17 00:03:39 +00:00
|
|
|
#define STATE_RECV_WRQ 6
|
2002-11-03 00:38:21 +00:00
|
|
|
|
2011-05-14 05:49:56 +00:00
|
|
|
/* default TFTP block size */
|
|
|
|
#define TFTP_BLOCK_SIZE 512
|
|
|
|
/* sequence number is 16 bit */
|
|
|
|
#define TFTP_SEQUENCE_SIZE ((ulong)(1<<16))
|
2004-02-23 16:11:30 +00:00
|
|
|
|
2002-11-03 00:38:21 +00:00
|
|
|
#define DEFAULT_NAME_LEN (8 + 4 + 1)
|
|
|
|
static char default_filename[DEFAULT_NAME_LEN];
|
2008-01-18 00:14:03 +00:00
|
|
|
|
|
|
|
#ifndef CONFIG_TFTP_FILE_NAME_MAX_LEN
|
|
|
|
#define MAX_LEN 128
|
|
|
|
#else
|
|
|
|
#define MAX_LEN CONFIG_TFTP_FILE_NAME_MAX_LEN
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static char tftp_filename[MAX_LEN];
|
2002-11-03 00:38:21 +00:00
|
|
|
|
2008-10-16 13:01:15 +00:00
|
|
|
#ifdef CONFIG_SYS_DIRECT_FLASH_TFTP
|
2005-10-11 17:09:42 +00:00
|
|
|
extern flash_info_t flash_info[];
|
2002-11-03 00:38:21 +00:00
|
|
|
#endif
|
|
|
|
|
2007-08-14 07:47:27 +00:00
|
|
|
/* 512 is poor choice for ethernet, MTU is typically 1500.
|
|
|
|
* Minus eth.hdrs thats 1468. Can get 2x better throughput with
|
2007-06-11 15:41:07 +00:00
|
|
|
* almost-MTU block sizes. At least try... fall back to 512 if need be.
|
2009-08-07 11:59:06 +00:00
|
|
|
* (but those using CONFIG_IP_DEFRAG may want to set a larger block in cfg file)
|
2007-06-11 15:41:07 +00:00
|
|
|
*/
|
2009-08-07 11:59:06 +00:00
|
|
|
#ifdef CONFIG_TFTP_BLOCKSIZE
|
|
|
|
#define TFTP_MTU_BLOCKSIZE CONFIG_TFTP_BLOCKSIZE
|
|
|
|
#else
|
2007-06-11 15:41:07 +00:00
|
|
|
#define TFTP_MTU_BLOCKSIZE 1468
|
2009-08-07 11:59:06 +00:00
|
|
|
#endif
|
|
|
|
|
2011-05-14 05:49:57 +00:00
|
|
|
static unsigned short TftpBlkSize = TFTP_BLOCK_SIZE;
|
|
|
|
static unsigned short TftpBlkSizeOption = TFTP_MTU_BLOCKSIZE;
|
2007-06-11 15:41:07 +00:00
|
|
|
|
|
|
|
#ifdef CONFIG_MCAST_TFTP
|
|
|
|
#include <malloc.h>
|
|
|
|
#define MTFTP_BITMAPSIZE 0x1000
|
|
|
|
static unsigned *Bitmap;
|
2011-05-14 05:49:57 +00:00
|
|
|
static int PrevBitmapHole, Mapsize = MTFTP_BITMAPSIZE;
|
2011-05-14 05:50:03 +00:00
|
|
|
static uchar ProhibitMcast, MasterClient;
|
|
|
|
static uchar Multicast;
|
2007-06-11 15:41:07 +00:00
|
|
|
extern IPaddr_t Mcast_addr;
|
|
|
|
static int Mcast_port;
|
|
|
|
static ulong TftpEndingBlock; /* can get 'last' block before done..*/
|
|
|
|
|
2011-05-14 05:49:57 +00:00
|
|
|
static void parse_multicast_oack(char *pkt, int len);
|
2007-06-11 15:41:07 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
mcast_cleanup(void)
|
|
|
|
{
|
2011-05-14 05:50:01 +00:00
|
|
|
if (Mcast_addr)
|
|
|
|
eth_mcast_join(Mcast_addr, 0);
|
|
|
|
if (Bitmap)
|
|
|
|
free(Bitmap);
|
2011-05-14 05:49:57 +00:00
|
|
|
Bitmap = NULL;
|
2007-06-11 15:41:07 +00:00
|
|
|
Mcast_addr = Multicast = Mcast_port = 0;
|
|
|
|
TftpEndingBlock = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* CONFIG_MCAST_TFTP */
|
|
|
|
|
2002-11-03 00:38:21 +00:00
|
|
|
static __inline__ void
|
2011-05-14 05:49:58 +00:00
|
|
|
store_block(unsigned block, uchar *src, unsigned len)
|
2002-11-03 00:38:21 +00:00
|
|
|
{
|
2007-06-11 15:41:07 +00:00
|
|
|
ulong offset = block * TftpBlkSize + TftpBlockWrapOffset;
|
2004-02-23 16:11:30 +00:00
|
|
|
ulong newsize = offset + len;
|
2008-10-16 13:01:15 +00:00
|
|
|
#ifdef CONFIG_SYS_DIRECT_FLASH_TFTP
|
2002-11-03 00:38:21 +00:00
|
|
|
int i, rc = 0;
|
|
|
|
|
2011-05-14 05:49:57 +00:00
|
|
|
for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; i++) {
|
2002-11-03 00:38:21 +00:00
|
|
|
/* start address in flash? */
|
2008-09-02 09:24:59 +00:00
|
|
|
if (flash_info[i].flash_id == FLASH_UNKNOWN)
|
|
|
|
continue;
|
2002-11-03 00:38:21 +00:00
|
|
|
if (load_addr + offset >= flash_info[i].start[0]) {
|
|
|
|
rc = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rc) { /* Flash is destination for this packet */
|
2011-05-14 05:49:57 +00:00
|
|
|
rc = flash_write((char *)src, (ulong)(load_addr+offset), len);
|
2002-11-03 00:38:21 +00:00
|
|
|
if (rc) {
|
2011-05-14 05:49:57 +00:00
|
|
|
flash_perror(rc);
|
2002-11-03 00:38:21 +00:00
|
|
|
NetState = NETLOOP_FAIL;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2008-10-16 13:01:15 +00:00
|
|
|
#endif /* CONFIG_SYS_DIRECT_FLASH_TFTP */
|
2002-11-03 00:38:21 +00:00
|
|
|
{
|
|
|
|
(void)memcpy((void *)(load_addr + offset), src, len);
|
|
|
|
}
|
2007-06-11 15:41:07 +00:00
|
|
|
#ifdef CONFIG_MCAST_TFTP
|
|
|
|
if (Multicast)
|
|
|
|
ext2_set_bit(block, Bitmap);
|
|
|
|
#endif
|
2002-11-03 00:38:21 +00:00
|
|
|
|
|
|
|
if (NetBootFileXferSize < newsize)
|
|
|
|
NetBootFileXferSize = newsize;
|
|
|
|
}
|
|
|
|
|
2011-05-14 05:49:57 +00:00
|
|
|
static void TftpSend(void);
|
|
|
|
static void TftpTimeout(void);
|
2002-11-03 00:38:21 +00:00
|
|
|
|
|
|
|
/**********************************************************************/
|
|
|
|
|
2011-10-24 18:00:03 +00:00
|
|
|
static void show_block_marker(void)
|
|
|
|
{
|
|
|
|
#ifdef CONFIG_TFTP_TSIZE
|
|
|
|
if (TftpTsize) {
|
|
|
|
ulong pos = TftpBlock * TftpBlkSize + TftpBlockWrapOffset;
|
|
|
|
|
|
|
|
while (TftpNumchars < pos * 50 / TftpTsize) {
|
|
|
|
putc('#');
|
|
|
|
TftpNumchars++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
else {
|
|
|
|
if (((TftpBlock - 1) % 10) == 0)
|
|
|
|
putc('#');
|
|
|
|
else if ((TftpBlock % (10 * HASHES_PER_LINE)) == 0)
|
|
|
|
puts("\n\t ");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The TFTP get or put is complete */
|
|
|
|
static void tftp_complete(void)
|
|
|
|
{
|
|
|
|
#ifdef CONFIG_TFTP_TSIZE
|
|
|
|
/* Print hash marks for the last packet received */
|
|
|
|
while (TftpTsize && TftpNumchars < 49) {
|
|
|
|
putc('#');
|
|
|
|
TftpNumchars++;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
puts("\ndone\n");
|
|
|
|
NetState = NETLOOP_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2002-11-03 00:38:21 +00:00
|
|
|
static void
|
2011-05-14 05:49:57 +00:00
|
|
|
TftpSend(void)
|
2002-11-03 00:38:21 +00:00
|
|
|
{
|
2011-05-14 05:49:58 +00:00
|
|
|
volatile uchar *pkt;
|
|
|
|
volatile uchar *xp;
|
|
|
|
int len = 0;
|
2005-08-25 23:36:03 +00:00
|
|
|
volatile ushort *s;
|
2002-11-03 00:38:21 +00:00
|
|
|
|
2007-08-14 07:47:27 +00:00
|
|
|
#ifdef CONFIG_MCAST_TFTP
|
2007-06-11 15:41:07 +00:00
|
|
|
/* Multicast TFTP.. non-MasterClients do not ACK data. */
|
2007-08-14 07:47:27 +00:00
|
|
|
if (Multicast
|
|
|
|
&& (TftpState == STATE_DATA)
|
|
|
|
&& (MasterClient == 0))
|
2007-06-11 15:41:07 +00:00
|
|
|
return;
|
|
|
|
#endif
|
2002-11-03 00:38:21 +00:00
|
|
|
/*
|
|
|
|
* We will always be sending some sort of packet, so
|
|
|
|
* cobble together the packet headers now.
|
|
|
|
*/
|
2004-04-15 21:48:45 +00:00
|
|
|
pkt = NetTxPacket + NetEthHdrSize() + IP_HDR_SIZE;
|
2002-11-03 00:38:21 +00:00
|
|
|
|
|
|
|
switch (TftpState) {
|
|
|
|
|
2011-05-17 00:03:38 +00:00
|
|
|
case STATE_SEND_RRQ:
|
2002-11-03 00:38:21 +00:00
|
|
|
xp = pkt;
|
2005-08-25 23:36:03 +00:00
|
|
|
s = (ushort *)pkt;
|
|
|
|
*s++ = htons(TFTP_RRQ);
|
|
|
|
pkt = (uchar *)s;
|
2011-05-14 05:49:57 +00:00
|
|
|
strcpy((char *)pkt, tftp_filename);
|
2002-11-03 00:38:21 +00:00
|
|
|
pkt += strlen(tftp_filename) + 1;
|
2011-05-14 05:49:57 +00:00
|
|
|
strcpy((char *)pkt, "octet");
|
2002-11-03 00:38:21 +00:00
|
|
|
pkt += 5 /*strlen("octet")*/ + 1;
|
2011-05-14 05:49:57 +00:00
|
|
|
strcpy((char *)pkt, "timeout");
|
2003-10-06 21:55:32 +00:00
|
|
|
pkt += 7 /*strlen("timeout")*/ + 1;
|
2010-01-17 22:55:53 +00:00
|
|
|
sprintf((char *)pkt, "%lu", TftpTimeoutMSecs / 1000);
|
2009-07-23 07:01:03 +00:00
|
|
|
debug("send option \"timeout %s\"\n", (char *)pkt);
|
2003-10-06 21:55:32 +00:00
|
|
|
pkt += strlen((char *)pkt) + 1;
|
2009-08-20 14:50:20 +00:00
|
|
|
#ifdef CONFIG_TFTP_TSIZE
|
|
|
|
memcpy((char *)pkt, "tsize\0000\0", 8);
|
|
|
|
pkt += 8;
|
|
|
|
#endif
|
2007-06-11 15:41:07 +00:00
|
|
|
/* try for more effic. blk size */
|
2011-05-14 05:49:57 +00:00
|
|
|
pkt += sprintf((char *)pkt, "blksize%c%d%c",
|
|
|
|
0, TftpBlkSizeOption, 0);
|
2007-08-14 07:47:27 +00:00
|
|
|
#ifdef CONFIG_MCAST_TFTP
|
2007-06-11 15:41:07 +00:00
|
|
|
/* Check all preconditions before even trying the option */
|
2007-08-14 07:47:27 +00:00
|
|
|
if (!ProhibitMcast
|
2011-05-14 05:49:57 +00:00
|
|
|
&& (Bitmap = malloc(Mapsize))
|
2007-06-11 15:41:07 +00:00
|
|
|
&& eth_get_dev()->mcast) {
|
|
|
|
free(Bitmap);
|
2011-05-14 05:49:57 +00:00
|
|
|
Bitmap = NULL;
|
|
|
|
pkt += sprintf((char *)pkt, "multicast%c%c", 0, 0);
|
2007-06-11 15:41:07 +00:00
|
|
|
}
|
|
|
|
#endif /* CONFIG_MCAST_TFTP */
|
2002-11-03 00:38:21 +00:00
|
|
|
len = pkt - xp;
|
|
|
|
break;
|
|
|
|
|
2003-10-06 21:55:32 +00:00
|
|
|
case STATE_OACK:
|
2007-06-11 15:41:07 +00:00
|
|
|
#ifdef CONFIG_MCAST_TFTP
|
|
|
|
/* My turn! Start at where I need blocks I missed.*/
|
|
|
|
if (Multicast)
|
2011-05-14 05:49:57 +00:00
|
|
|
TftpBlock = ext2_find_next_zero_bit(Bitmap,
|
|
|
|
(Mapsize*8), 0);
|
2007-06-11 15:41:07 +00:00
|
|
|
/*..falling..*/
|
|
|
|
#endif
|
2011-05-17 00:03:39 +00:00
|
|
|
|
|
|
|
case STATE_RECV_WRQ:
|
2007-06-11 15:41:07 +00:00
|
|
|
case STATE_DATA:
|
2002-11-03 00:38:21 +00:00
|
|
|
xp = pkt;
|
2005-08-25 23:36:03 +00:00
|
|
|
s = (ushort *)pkt;
|
|
|
|
*s++ = htons(TFTP_ACK);
|
|
|
|
*s++ = htons(TftpBlock);
|
|
|
|
pkt = (uchar *)s;
|
2002-11-03 00:38:21 +00:00
|
|
|
len = pkt - xp;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case STATE_TOO_LARGE:
|
|
|
|
xp = pkt;
|
2005-08-25 23:36:03 +00:00
|
|
|
s = (ushort *)pkt;
|
|
|
|
*s++ = htons(TFTP_ERROR);
|
|
|
|
*s++ = htons(3);
|
|
|
|
pkt = (uchar *)s;
|
2011-05-14 05:49:57 +00:00
|
|
|
strcpy((char *)pkt, "File too large");
|
2002-11-03 00:38:21 +00:00
|
|
|
pkt += 14 /*strlen("File too large")*/ + 1;
|
|
|
|
len = pkt - xp;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case STATE_BAD_MAGIC:
|
|
|
|
xp = pkt;
|
2005-08-25 23:36:03 +00:00
|
|
|
s = (ushort *)pkt;
|
|
|
|
*s++ = htons(TFTP_ERROR);
|
|
|
|
*s++ = htons(2);
|
|
|
|
pkt = (uchar *)s;
|
2011-05-14 05:49:57 +00:00
|
|
|
strcpy((char *)pkt, "File has bad magic");
|
2002-11-03 00:38:21 +00:00
|
|
|
pkt += 18 /*strlen("File has bad magic")*/ + 1;
|
|
|
|
len = pkt - xp;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2011-05-17 00:03:37 +00:00
|
|
|
NetSendUDPPacket(NetServerEther, TftpRemoteIP, TftpRemotePort,
|
2011-05-14 05:49:56 +00:00
|
|
|
TftpOurPort, len);
|
2002-11-03 00:38:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
2011-04-18 06:19:50 +00:00
|
|
|
TftpHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src,
|
|
|
|
unsigned len)
|
2002-11-03 00:38:21 +00:00
|
|
|
{
|
|
|
|
ushort proto;
|
2005-08-25 23:36:03 +00:00
|
|
|
ushort *s;
|
2007-08-30 12:42:15 +00:00
|
|
|
int i;
|
2002-11-03 00:38:21 +00:00
|
|
|
|
|
|
|
if (dest != TftpOurPort) {
|
2007-06-11 15:41:07 +00:00
|
|
|
#ifdef CONFIG_MCAST_TFTP
|
2007-08-14 07:47:27 +00:00
|
|
|
if (Multicast
|
2007-06-11 15:41:07 +00:00
|
|
|
&& (!Mcast_port || (dest != Mcast_port)))
|
|
|
|
#endif
|
2011-05-14 05:50:02 +00:00
|
|
|
return;
|
2002-11-03 00:38:21 +00:00
|
|
|
}
|
2011-05-17 00:03:39 +00:00
|
|
|
if (TftpState != STATE_SEND_RRQ && src != TftpRemotePort &&
|
|
|
|
TftpState != STATE_RECV_WRQ)
|
2002-11-03 00:38:21 +00:00
|
|
|
return;
|
|
|
|
|
2011-05-14 05:50:00 +00:00
|
|
|
if (len < 2)
|
2002-11-03 00:38:21 +00:00
|
|
|
return;
|
|
|
|
len -= 2;
|
|
|
|
/* warning: don't use increment (++) in ntohs() macros!! */
|
2005-08-25 23:36:03 +00:00
|
|
|
s = (ushort *)pkt;
|
|
|
|
proto = *s++;
|
|
|
|
pkt = (uchar *)s;
|
2002-11-03 00:38:21 +00:00
|
|
|
switch (ntohs(proto)) {
|
|
|
|
|
|
|
|
case TFTP_RRQ:
|
|
|
|
case TFTP_ACK:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
|
2011-05-17 00:03:39 +00:00
|
|
|
#ifdef CONFIG_CMD_TFTPSRV
|
|
|
|
case TFTP_WRQ:
|
|
|
|
debug("Got WRQ\n");
|
|
|
|
TftpRemoteIP = sip;
|
|
|
|
TftpRemotePort = src;
|
|
|
|
TftpOurPort = 1024 + (get_timer(0) % 3072);
|
|
|
|
TftpLastBlock = 0;
|
|
|
|
TftpBlockWrap = 0;
|
|
|
|
TftpBlockWrapOffset = 0;
|
|
|
|
TftpSend(); /* Send ACK(0) */
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
2003-10-06 21:55:32 +00:00
|
|
|
case TFTP_OACK:
|
2009-08-10 07:59:10 +00:00
|
|
|
debug("Got OACK: %s %s\n",
|
|
|
|
pkt,
|
|
|
|
pkt + strlen((char *)pkt) + 1);
|
2003-10-06 21:55:32 +00:00
|
|
|
TftpState = STATE_OACK;
|
2011-05-17 00:03:37 +00:00
|
|
|
TftpRemotePort = src;
|
2007-08-31 08:01:51 +00:00
|
|
|
/*
|
|
|
|
* Check for 'blksize' option.
|
|
|
|
* Careful: "i" is signed, "len" is unsigned, thus
|
|
|
|
* something like "len-8" may give a *huge* number
|
|
|
|
*/
|
2011-05-14 05:49:57 +00:00
|
|
|
for (i = 0; i+8 < len; i++) {
|
2011-05-14 05:49:58 +00:00
|
|
|
if (strcmp((char *)pkt+i, "blksize") == 0) {
|
2007-08-30 12:42:15 +00:00
|
|
|
TftpBlkSize = (unsigned short)
|
2011-05-14 05:49:58 +00:00
|
|
|
simple_strtoul((char *)pkt+i+8, NULL,
|
2011-05-14 05:49:57 +00:00
|
|
|
10);
|
2009-07-23 07:01:03 +00:00
|
|
|
debug("Blocksize ack: %s, %d\n",
|
2011-05-14 05:49:58 +00:00
|
|
|
(char *)pkt+i+8, TftpBlkSize);
|
2007-08-30 12:42:15 +00:00
|
|
|
}
|
2009-08-20 14:50:20 +00:00
|
|
|
#ifdef CONFIG_TFTP_TSIZE
|
2011-05-14 05:49:58 +00:00
|
|
|
if (strcmp((char *)pkt+i, "tsize") == 0) {
|
|
|
|
TftpTsize = simple_strtoul((char *)pkt+i+6,
|
2011-05-14 05:49:56 +00:00
|
|
|
NULL, 10);
|
2009-08-20 14:50:20 +00:00
|
|
|
debug("size = %s, %d\n",
|
2011-05-14 05:49:58 +00:00
|
|
|
(char *)pkt+i+6, TftpTsize);
|
2009-08-20 14:50:20 +00:00
|
|
|
}
|
|
|
|
#endif
|
2007-06-11 15:41:07 +00:00
|
|
|
}
|
|
|
|
#ifdef CONFIG_MCAST_TFTP
|
2011-05-14 05:49:57 +00:00
|
|
|
parse_multicast_oack((char *)pkt, len-1);
|
2007-08-14 07:47:27 +00:00
|
|
|
if ((Multicast) && (!MasterClient))
|
2007-06-11 15:41:07 +00:00
|
|
|
TftpState = STATE_DATA; /* passive.. */
|
|
|
|
else
|
|
|
|
#endif
|
2011-05-14 05:49:57 +00:00
|
|
|
TftpSend(); /* Send ACK */
|
2003-10-06 21:55:32 +00:00
|
|
|
break;
|
2002-11-03 00:38:21 +00:00
|
|
|
case TFTP_DATA:
|
|
|
|
if (len < 2)
|
|
|
|
return;
|
|
|
|
len -= 2;
|
|
|
|
TftpBlock = ntohs(*(ushort *)pkt);
|
2004-02-23 16:11:30 +00:00
|
|
|
|
|
|
|
/*
|
2004-02-23 20:48:38 +00:00
|
|
|
* RFC1350 specifies that the first data packet will
|
|
|
|
* have sequence number 1. If we receive a sequence
|
|
|
|
* number of 0 this means that there was a wrap
|
|
|
|
* around of the (16 bit) counter.
|
2004-02-23 16:11:30 +00:00
|
|
|
*/
|
|
|
|
if (TftpBlock == 0) {
|
|
|
|
TftpBlockWrap++;
|
2011-05-14 05:49:56 +00:00
|
|
|
TftpBlockWrapOffset +=
|
|
|
|
TftpBlkSize * TFTP_SEQUENCE_SIZE;
|
2011-05-14 05:49:57 +00:00
|
|
|
printf("\n\t %lu MB received\n\t ",
|
2011-05-14 05:49:56 +00:00
|
|
|
TftpBlockWrapOffset>>20);
|
2011-10-24 18:00:03 +00:00
|
|
|
} else {
|
|
|
|
show_block_marker();
|
2002-11-03 00:38:21 +00:00
|
|
|
}
|
|
|
|
|
2011-05-17 00:03:38 +00:00
|
|
|
if (TftpState == STATE_SEND_RRQ)
|
2009-07-23 07:01:03 +00:00
|
|
|
debug("Server did not acknowledge timeout option!\n");
|
2003-10-06 21:55:32 +00:00
|
|
|
|
2011-05-17 00:03:39 +00:00
|
|
|
if (TftpState == STATE_SEND_RRQ || TftpState == STATE_OACK ||
|
|
|
|
TftpState == STATE_RECV_WRQ) {
|
2004-02-23 16:11:30 +00:00
|
|
|
/* first block received */
|
2002-11-03 00:38:21 +00:00
|
|
|
TftpState = STATE_DATA;
|
2011-05-17 00:03:37 +00:00
|
|
|
TftpRemotePort = src;
|
2002-11-03 00:38:21 +00:00
|
|
|
TftpLastBlock = 0;
|
2004-02-23 16:11:30 +00:00
|
|
|
TftpBlockWrap = 0;
|
|
|
|
TftpBlockWrapOffset = 0;
|
2002-11-03 00:38:21 +00:00
|
|
|
|
2007-06-11 15:41:07 +00:00
|
|
|
#ifdef CONFIG_MCAST_TFTP
|
|
|
|
if (Multicast) { /* start!=1 common if mcast */
|
|
|
|
TftpLastBlock = TftpBlock - 1;
|
|
|
|
} else
|
|
|
|
#endif
|
2002-11-03 00:38:21 +00:00
|
|
|
if (TftpBlock != 1) { /* Assertion */
|
2011-05-14 05:49:57 +00:00
|
|
|
printf("\nTFTP error: "
|
|
|
|
"First block is not block 1 (%ld)\n"
|
|
|
|
"Starting again\n\n",
|
2002-11-03 00:38:21 +00:00
|
|
|
TftpBlock);
|
2011-05-14 05:49:57 +00:00
|
|
|
NetStartAgain();
|
2002-11-03 00:38:21 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (TftpBlock == TftpLastBlock) {
|
|
|
|
/*
|
|
|
|
* Same block again; ignore it.
|
|
|
|
*/
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
TftpLastBlock = TftpBlock;
|
2008-10-01 13:26:29 +00:00
|
|
|
TftpTimeoutCountMax = TIMEOUT_COUNT;
|
2011-05-14 05:49:57 +00:00
|
|
|
NetSetTimeout(TftpTimeoutMSecs, TftpTimeout);
|
2002-11-03 00:38:21 +00:00
|
|
|
|
2011-05-14 05:49:57 +00:00
|
|
|
store_block(TftpBlock - 1, pkt + 2, len);
|
2002-11-03 00:38:21 +00:00
|
|
|
|
|
|
|
/*
|
2011-05-17 00:03:41 +00:00
|
|
|
* Acknowledge the block just received, which will prompt
|
2011-05-17 00:03:37 +00:00
|
|
|
* the remote for the next one.
|
2002-11-03 00:38:21 +00:00
|
|
|
*/
|
2007-06-11 15:41:07 +00:00
|
|
|
#ifdef CONFIG_MCAST_TFTP
|
2007-08-14 07:47:27 +00:00
|
|
|
/* if I am the MasterClient, actively calculate what my next
|
|
|
|
* needed block is; else I'm passive; not ACKING
|
2008-01-18 00:14:03 +00:00
|
|
|
*/
|
2007-06-11 15:41:07 +00:00
|
|
|
if (Multicast) {
|
|
|
|
if (len < TftpBlkSize) {
|
|
|
|
TftpEndingBlock = TftpBlock;
|
|
|
|
} else if (MasterClient) {
|
2007-08-14 07:47:27 +00:00
|
|
|
TftpBlock = PrevBitmapHole =
|
2007-06-11 15:41:07 +00:00
|
|
|
ext2_find_next_zero_bit(
|
|
|
|
Bitmap,
|
|
|
|
(Mapsize*8),
|
|
|
|
PrevBitmapHole);
|
|
|
|
if (TftpBlock > ((Mapsize*8) - 1)) {
|
2011-05-14 05:49:57 +00:00
|
|
|
printf("tftpfile too big\n");
|
2007-06-11 15:41:07 +00:00
|
|
|
/* try to double it and retry */
|
2011-05-14 05:49:57 +00:00
|
|
|
Mapsize <<= 1;
|
2007-06-11 15:41:07 +00:00
|
|
|
mcast_cleanup();
|
2011-05-14 05:49:57 +00:00
|
|
|
NetStartAgain();
|
2007-06-11 15:41:07 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
TftpLastBlock = TftpBlock;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2011-05-14 05:49:57 +00:00
|
|
|
TftpSend();
|
2002-11-03 00:38:21 +00:00
|
|
|
|
2007-06-11 15:41:07 +00:00
|
|
|
#ifdef CONFIG_MCAST_TFTP
|
|
|
|
if (Multicast) {
|
|
|
|
if (MasterClient && (TftpBlock >= TftpEndingBlock)) {
|
2011-05-14 05:49:57 +00:00
|
|
|
puts("\nMulticast tftp done\n");
|
2007-06-11 15:41:07 +00:00
|
|
|
mcast_cleanup();
|
|
|
|
NetState = NETLOOP_SUCCESS;
|
2007-08-14 07:47:27 +00:00
|
|
|
}
|
2007-06-11 15:41:07 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
2011-10-24 18:00:03 +00:00
|
|
|
if (len < TftpBlkSize)
|
|
|
|
tftp_complete();
|
2002-11-03 00:38:21 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case TFTP_ERROR:
|
2011-05-14 05:49:57 +00:00
|
|
|
printf("\nTFTP error: '%s' (%d)\n",
|
|
|
|
pkt + 2, ntohs(*(ushort *)pkt));
|
2009-10-28 21:13:40 +00:00
|
|
|
|
|
|
|
switch (ntohs(*(ushort *)pkt)) {
|
|
|
|
case TFTP_ERR_FILE_NOT_FOUND:
|
|
|
|
case TFTP_ERR_ACCESS_DENIED:
|
|
|
|
puts("Not retrying...\n");
|
|
|
|
eth_halt();
|
|
|
|
NetState = NETLOOP_FAIL;
|
|
|
|
break;
|
|
|
|
case TFTP_ERR_UNDEFINED:
|
|
|
|
case TFTP_ERR_DISK_FULL:
|
|
|
|
case TFTP_ERR_UNEXPECTED_OPCODE:
|
|
|
|
case TFTP_ERR_UNKNOWN_TRANSFER_ID:
|
|
|
|
case TFTP_ERR_FILE_ALREADY_EXISTS:
|
|
|
|
default:
|
|
|
|
puts("Starting again\n\n");
|
2007-06-11 15:41:07 +00:00
|
|
|
#ifdef CONFIG_MCAST_TFTP
|
2009-10-28 21:13:40 +00:00
|
|
|
mcast_cleanup();
|
2007-06-11 15:41:07 +00:00
|
|
|
#endif
|
2009-10-28 21:13:40 +00:00
|
|
|
NetStartAgain();
|
|
|
|
break;
|
|
|
|
}
|
2002-11-03 00:38:21 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
2011-05-14 05:49:57 +00:00
|
|
|
TftpTimeout(void)
|
2002-11-03 00:38:21 +00:00
|
|
|
{
|
2008-10-01 13:26:29 +00:00
|
|
|
if (++TftpTimeoutCount > TftpTimeoutCountMax) {
|
2011-05-14 05:49:57 +00:00
|
|
|
puts("\nRetry count exceeded; starting again\n");
|
2007-06-11 15:41:07 +00:00
|
|
|
#ifdef CONFIG_MCAST_TFTP
|
|
|
|
mcast_cleanup();
|
|
|
|
#endif
|
2011-05-14 05:49:57 +00:00
|
|
|
NetStartAgain();
|
2002-11-03 00:38:21 +00:00
|
|
|
} else {
|
2011-05-14 05:49:57 +00:00
|
|
|
puts("T ");
|
|
|
|
NetSetTimeout(TftpTimeoutMSecs, TftpTimeout);
|
2011-05-17 00:03:39 +00:00
|
|
|
if (TftpState != STATE_RECV_WRQ)
|
|
|
|
TftpSend();
|
2002-11-03 00:38:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2011-05-14 05:49:57 +00:00
|
|
|
TftpStart(void)
|
2002-11-03 00:38:21 +00:00
|
|
|
{
|
2005-09-24 20:37:32 +00:00
|
|
|
char *ep; /* Environment pointer */
|
2009-08-07 11:59:06 +00:00
|
|
|
|
2010-01-17 22:55:53 +00:00
|
|
|
/*
|
|
|
|
* Allow the user to choose TFTP blocksize and timeout.
|
|
|
|
* TFTP protocol has a minimal timeout of 1 second.
|
|
|
|
*/
|
2011-05-14 05:49:59 +00:00
|
|
|
ep = getenv("tftpblocksize");
|
|
|
|
if (ep != NULL)
|
2009-08-07 11:59:06 +00:00
|
|
|
TftpBlkSizeOption = simple_strtol(ep, NULL, 10);
|
2010-01-17 22:55:53 +00:00
|
|
|
|
2011-05-14 05:49:59 +00:00
|
|
|
ep = getenv("tftptimeout");
|
|
|
|
if (ep != NULL)
|
2010-01-17 22:55:53 +00:00
|
|
|
TftpTimeoutMSecs = simple_strtol(ep, NULL, 10);
|
|
|
|
|
|
|
|
if (TftpTimeoutMSecs < 1000) {
|
|
|
|
printf("TFTP timeout (%ld ms) too low, "
|
|
|
|
"set minimum = 1000 ms\n",
|
|
|
|
TftpTimeoutMSecs);
|
|
|
|
TftpTimeoutMSecs = 1000;
|
|
|
|
}
|
|
|
|
|
|
|
|
debug("TFTP blocksize = %i, timeout = %ld ms\n",
|
|
|
|
TftpBlkSizeOption, TftpTimeoutMSecs);
|
2005-09-24 20:37:32 +00:00
|
|
|
|
2011-05-17 00:03:37 +00:00
|
|
|
TftpRemoteIP = NetServerIP;
|
2002-11-03 00:38:21 +00:00
|
|
|
if (BootFile[0] == '\0') {
|
|
|
|
sprintf(default_filename, "%02lX%02lX%02lX%02lX.img",
|
2005-08-03 23:09:44 +00:00
|
|
|
NetOurIP & 0xFF,
|
|
|
|
(NetOurIP >> 8) & 0xFF,
|
|
|
|
(NetOurIP >> 16) & 0xFF,
|
2011-05-14 05:49:57 +00:00
|
|
|
(NetOurIP >> 24) & 0xFF);
|
2008-01-18 00:14:03 +00:00
|
|
|
|
|
|
|
strncpy(tftp_filename, default_filename, MAX_LEN);
|
|
|
|
tftp_filename[MAX_LEN-1] = 0;
|
2002-11-03 00:38:21 +00:00
|
|
|
|
2011-05-14 05:49:57 +00:00
|
|
|
printf("*** Warning: no boot file name; using '%s'\n",
|
2002-11-03 00:38:21 +00:00
|
|
|
tftp_filename);
|
|
|
|
} else {
|
2011-05-14 05:49:57 +00:00
|
|
|
char *p = strchr(BootFile, ':');
|
2008-01-18 00:14:03 +00:00
|
|
|
|
|
|
|
if (p == NULL) {
|
|
|
|
strncpy(tftp_filename, BootFile, MAX_LEN);
|
|
|
|
tftp_filename[MAX_LEN-1] = 0;
|
|
|
|
} else {
|
2011-05-17 00:03:37 +00:00
|
|
|
TftpRemoteIP = string_to_ip(BootFile);
|
2008-12-01 22:29:38 +00:00
|
|
|
strncpy(tftp_filename, p + 1, MAX_LEN);
|
2008-01-18 00:14:03 +00:00
|
|
|
tftp_filename[MAX_LEN-1] = 0;
|
|
|
|
}
|
2002-11-03 00:38:21 +00:00
|
|
|
}
|
|
|
|
|
2011-05-14 05:49:57 +00:00
|
|
|
printf("Using %s device\n", eth_get_name());
|
2009-02-17 05:00:53 +00:00
|
|
|
printf("TFTP from server %pI4"
|
2011-05-17 00:03:37 +00:00
|
|
|
"; our IP address is %pI4", &TftpRemoteIP, &NetOurIP);
|
2002-11-03 00:38:21 +00:00
|
|
|
|
|
|
|
/* Check if we need to send across this subnet */
|
|
|
|
if (NetOurGatewayIP && NetOurSubnetMask) {
|
2011-05-14 05:50:02 +00:00
|
|
|
IPaddr_t OurNet = NetOurIP & NetOurSubnetMask;
|
2011-05-17 00:03:37 +00:00
|
|
|
IPaddr_t RemoteNet = TftpRemoteIP & NetOurSubnetMask;
|
2002-11-03 00:38:21 +00:00
|
|
|
|
2011-05-17 00:03:37 +00:00
|
|
|
if (OurNet != RemoteNet)
|
2011-05-14 05:50:02 +00:00
|
|
|
printf("; sending through gateway %pI4",
|
|
|
|
&NetOurGatewayIP);
|
2002-11-03 00:38:21 +00:00
|
|
|
}
|
2011-05-14 05:49:57 +00:00
|
|
|
putc('\n');
|
2002-11-03 00:38:21 +00:00
|
|
|
|
2011-05-14 05:49:57 +00:00
|
|
|
printf("Filename '%s'.", tftp_filename);
|
2002-11-03 00:38:21 +00:00
|
|
|
|
|
|
|
if (NetBootFileSize) {
|
2011-05-14 05:49:57 +00:00
|
|
|
printf(" Size is 0x%x Bytes = ", NetBootFileSize<<9);
|
|
|
|
print_size(NetBootFileSize<<9, "");
|
2002-11-03 00:38:21 +00:00
|
|
|
}
|
|
|
|
|
2011-05-14 05:49:57 +00:00
|
|
|
putc('\n');
|
2002-11-03 00:38:21 +00:00
|
|
|
|
2011-05-14 05:49:57 +00:00
|
|
|
printf("Load address: 0x%lx\n", load_addr);
|
2002-11-03 00:38:21 +00:00
|
|
|
|
2011-05-14 05:49:57 +00:00
|
|
|
puts("Loading: *\b");
|
2002-11-03 00:38:21 +00:00
|
|
|
|
2008-10-01 13:26:29 +00:00
|
|
|
TftpTimeoutCountMax = TftpRRQTimeoutCountMax;
|
|
|
|
|
2011-05-14 05:49:57 +00:00
|
|
|
NetSetTimeout(TftpTimeoutMSecs, TftpTimeout);
|
|
|
|
NetSetHandler(TftpHandler);
|
2002-11-03 00:38:21 +00:00
|
|
|
|
2011-05-17 00:03:37 +00:00
|
|
|
TftpRemotePort = WELL_KNOWN_PORT;
|
2002-11-03 00:38:21 +00:00
|
|
|
TftpTimeoutCount = 0;
|
2011-05-17 00:03:38 +00:00
|
|
|
TftpState = STATE_SEND_RRQ;
|
2005-09-24 20:37:32 +00:00
|
|
|
/* Use a pseudo-random port unless a specific port is set */
|
2002-11-03 00:38:21 +00:00
|
|
|
TftpOurPort = 1024 + (get_timer(0) % 3072);
|
2007-06-11 15:41:07 +00:00
|
|
|
|
2005-09-24 20:37:32 +00:00
|
|
|
#ifdef CONFIG_TFTP_PORT
|
2011-05-14 05:49:59 +00:00
|
|
|
ep = getenv("tftpdstp");
|
2011-05-14 05:50:00 +00:00
|
|
|
if (ep != NULL)
|
2011-05-17 00:03:37 +00:00
|
|
|
TftpRemotePort = simple_strtol(ep, NULL, 10);
|
2011-05-14 05:49:59 +00:00
|
|
|
ep = getenv("tftpsrcp");
|
2011-05-14 05:50:00 +00:00
|
|
|
if (ep != NULL)
|
2011-05-14 05:49:57 +00:00
|
|
|
TftpOurPort = simple_strtol(ep, NULL, 10);
|
2005-09-24 20:37:32 +00:00
|
|
|
#endif
|
2003-10-06 21:55:32 +00:00
|
|
|
TftpBlock = 0;
|
2002-11-03 00:38:21 +00:00
|
|
|
|
2003-06-05 19:27:42 +00:00
|
|
|
/* zero out server ether in case the server ip has changed */
|
|
|
|
memset(NetServerEther, 0, 6);
|
2007-06-11 15:41:07 +00:00
|
|
|
/* Revert TftpBlkSize to dflt */
|
|
|
|
TftpBlkSize = TFTP_BLOCK_SIZE;
|
|
|
|
#ifdef CONFIG_MCAST_TFTP
|
2008-01-18 00:14:03 +00:00
|
|
|
mcast_cleanup();
|
2007-06-11 15:41:07 +00:00
|
|
|
#endif
|
2009-08-20 14:50:20 +00:00
|
|
|
#ifdef CONFIG_TFTP_TSIZE
|
|
|
|
TftpTsize = 0;
|
|
|
|
TftpNumchars = 0;
|
|
|
|
#endif
|
2003-06-05 19:27:42 +00:00
|
|
|
|
2011-05-14 05:49:57 +00:00
|
|
|
TftpSend();
|
2002-11-03 00:38:21 +00:00
|
|
|
}
|
|
|
|
|
2011-05-17 00:03:39 +00:00
|
|
|
#ifdef CONFIG_CMD_TFTPSRV
|
|
|
|
void
|
|
|
|
TftpStartServer(void)
|
|
|
|
{
|
|
|
|
tftp_filename[0] = 0;
|
|
|
|
|
|
|
|
printf("Using %s device\n", eth_get_name());
|
|
|
|
printf("Listening for TFTP transfer on %pI4\n", &NetOurIP);
|
|
|
|
printf("Load address: 0x%lx\n", load_addr);
|
|
|
|
|
|
|
|
puts("Loading: *\b");
|
|
|
|
|
|
|
|
TftpTimeoutCountMax = TIMEOUT_COUNT;
|
|
|
|
TftpTimeoutCount = 0;
|
|
|
|
TftpTimeoutMSecs = TIMEOUT;
|
|
|
|
NetSetTimeout(TftpTimeoutMSecs, TftpTimeout);
|
|
|
|
|
|
|
|
/* Revert TftpBlkSize to dflt */
|
|
|
|
TftpBlkSize = TFTP_BLOCK_SIZE;
|
|
|
|
TftpBlock = 0;
|
|
|
|
TftpOurPort = WELL_KNOWN_PORT;
|
|
|
|
|
|
|
|
#ifdef CONFIG_TFTP_TSIZE
|
|
|
|
TftpTsize = 0;
|
|
|
|
TftpNumchars = 0;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
TftpState = STATE_RECV_WRQ;
|
|
|
|
NetSetHandler(TftpHandler);
|
|
|
|
}
|
|
|
|
#endif /* CONFIG_CMD_TFTPSRV */
|
|
|
|
|
2007-06-11 15:41:07 +00:00
|
|
|
#ifdef CONFIG_MCAST_TFTP
|
|
|
|
/* Credits: atftp project.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* pick up BcastAddr, Port, and whether I am [now] the master-client. *
|
|
|
|
* Frame:
|
|
|
|
* +-------+-----------+---+-------~~-------+---+
|
|
|
|
* | opc | multicast | 0 | addr, port, mc | 0 |
|
|
|
|
* +-------+-----------+---+-------~~-------+---+
|
|
|
|
* The multicast addr/port becomes what I listen to, and if 'mc' is '1' then
|
|
|
|
* I am the new master-client so must send ACKs to DataBlocks. If I am not
|
|
|
|
* master-client, I'm a passive client, gathering what DataBlocks I may and
|
|
|
|
* making note of which ones I got in my bitmask.
|
|
|
|
* In theory, I never go from master->passive..
|
|
|
|
* .. this comes in with pkt already pointing just past opc
|
|
|
|
*/
|
|
|
|
static void parse_multicast_oack(char *pkt, int len)
|
|
|
|
{
|
2011-05-14 05:49:57 +00:00
|
|
|
int i;
|
|
|
|
IPaddr_t addr;
|
|
|
|
char *mc_adr, *port, *mc;
|
2007-06-11 15:41:07 +00:00
|
|
|
|
2011-05-14 05:49:57 +00:00
|
|
|
mc_adr = port = mc = NULL;
|
2007-06-11 15:41:07 +00:00
|
|
|
/* march along looking for 'multicast\0', which has to start at least
|
|
|
|
* 14 bytes back from the end.
|
|
|
|
*/
|
2011-05-14 05:49:57 +00:00
|
|
|
for (i = 0; i < len-14; i++)
|
|
|
|
if (strcmp(pkt+i, "multicast") == 0)
|
2007-06-11 15:41:07 +00:00
|
|
|
break;
|
|
|
|
if (i >= (len-14)) /* non-Multicast OACK, ign. */
|
|
|
|
return;
|
2007-08-14 07:47:27 +00:00
|
|
|
|
2011-05-14 05:49:57 +00:00
|
|
|
i += 10; /* strlen multicast */
|
2007-06-11 15:41:07 +00:00
|
|
|
mc_adr = pkt+i;
|
2011-05-14 05:49:57 +00:00
|
|
|
for (; i < len; i++) {
|
2007-06-11 15:41:07 +00:00
|
|
|
if (*(pkt+i) == ',') {
|
|
|
|
*(pkt+i) = '\0';
|
|
|
|
if (port) {
|
|
|
|
mc = pkt+i+1;
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
port = pkt+i+1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-05-14 05:50:01 +00:00
|
|
|
if (!port || !mc_adr || !mc)
|
|
|
|
return;
|
2007-06-11 15:41:07 +00:00
|
|
|
if (Multicast && MasterClient) {
|
2011-05-14 05:49:57 +00:00
|
|
|
printf("I got a OACK as master Client, WRONG!\n");
|
2007-06-11 15:41:07 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
/* ..I now accept packets destined for this MCAST addr, port */
|
|
|
|
if (!Multicast) {
|
|
|
|
if (Bitmap) {
|
2011-05-14 05:49:57 +00:00
|
|
|
printf("Internal failure! no mcast.\n");
|
2007-06-11 15:41:07 +00:00
|
|
|
free(Bitmap);
|
2011-05-14 05:49:57 +00:00
|
|
|
Bitmap = NULL;
|
|
|
|
ProhibitMcast = 1;
|
2007-06-11 15:41:07 +00:00
|
|
|
return ;
|
|
|
|
}
|
|
|
|
/* I malloc instead of pre-declare; so that if the file ends
|
2007-08-14 07:47:27 +00:00
|
|
|
* up being too big for this bitmap I can retry
|
|
|
|
*/
|
2011-05-14 05:49:59 +00:00
|
|
|
Bitmap = malloc(Mapsize);
|
|
|
|
if (!Bitmap) {
|
2011-05-14 05:49:57 +00:00
|
|
|
printf("No Bitmap, no multicast. Sorry.\n");
|
|
|
|
ProhibitMcast = 1;
|
2007-06-11 15:41:07 +00:00
|
|
|
return;
|
|
|
|
}
|
2011-05-14 05:49:57 +00:00
|
|
|
memset(Bitmap, 0, Mapsize);
|
2007-06-11 15:41:07 +00:00
|
|
|
PrevBitmapHole = 0;
|
|
|
|
Multicast = 1;
|
|
|
|
}
|
|
|
|
addr = string_to_ip(mc_adr);
|
|
|
|
if (Mcast_addr != addr) {
|
|
|
|
if (Mcast_addr)
|
|
|
|
eth_mcast_join(Mcast_addr, 0);
|
2011-05-14 05:49:59 +00:00
|
|
|
Mcast_addr = addr;
|
|
|
|
if (eth_mcast_join(Mcast_addr, 1)) {
|
2011-05-14 05:49:57 +00:00
|
|
|
printf("Fail to set mcast, revert to TFTP\n");
|
|
|
|
ProhibitMcast = 1;
|
2007-06-11 15:41:07 +00:00
|
|
|
mcast_cleanup();
|
|
|
|
NetStartAgain();
|
|
|
|
}
|
|
|
|
}
|
2011-05-14 05:49:57 +00:00
|
|
|
MasterClient = (unsigned char)simple_strtoul((char *)mc, NULL, 10);
|
|
|
|
Mcast_port = (unsigned short)simple_strtoul(port, NULL, 10);
|
|
|
|
printf("Multicast: %s:%d [%d]\n", mc_adr, Mcast_port, MasterClient);
|
2007-06-11 15:41:07 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* Multicast TFTP */
|