2002-11-03 00:38:21 +00:00
|
|
|
/*
|
|
|
|
* Copyright 1994, 1995, 2000 Neil Russell.
|
|
|
|
* (See License)
|
|
|
|
* Copyright 2000, 2001 DENX Software Engineering, Wolfgang Denk, wd@denx.de
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <common.h>
|
|
|
|
#include <command.h>
|
|
|
|
#include <net.h>
|
2007-07-05 16:01:34 +00:00
|
|
|
#include <driver.h>
|
2007-07-05 16:01:15 +00:00
|
|
|
#include <clock.h>
|
2007-07-05 16:01:38 +00:00
|
|
|
#include <fs.h>
|
2007-07-05 16:01:51 +00:00
|
|
|
#include <errno.h>
|
2010-01-04 09:08:52 +00:00
|
|
|
#include <libgen.h>
|
|
|
|
#include <fcntl.h>
|
2002-11-03 00:38:21 +00:00
|
|
|
#include "tftp.h"
|
|
|
|
|
|
|
|
#define WELL_KNOWN_PORT 69 /* Well known TFTP port # */
|
2003-10-06 21:55:32 +00:00
|
|
|
#define TIMEOUT 5 /* Seconds to timeout for a lost pkt */
|
2002-11-03 00:38:21 +00:00
|
|
|
# define TIMEOUT_COUNT 10 /* # of timeouts before giving up */
|
|
|
|
/* (for checking the image size) */
|
|
|
|
#define HASHES_PER_LINE 65 /* Number of "loading" hashes per line */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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
|
|
|
|
|
|
|
|
|
|
|
static int TftpServerPort; /* The UDP port at their end */
|
|
|
|
static int TftpOurPort; /* The UDP port at our end */
|
2004-02-23 16:11:30 +00:00
|
|
|
static ulong TftpBlock; /* packet sequence number */
|
|
|
|
static ulong TftpLastBlock; /* last packet sequence number received */
|
|
|
|
static ulong TftpBlockWrap; /* count of sequence number wraparounds */
|
|
|
|
static ulong TftpBlockWrapOffset; /* memory offset due to wrapping */
|
2002-11-03 00:38:21 +00:00
|
|
|
static int TftpState;
|
2004-02-23 16:11:30 +00:00
|
|
|
|
2002-11-03 00:38:21 +00:00
|
|
|
#define STATE_RRQ 1
|
|
|
|
#define STATE_DATA 2
|
2009-10-17 07:18:49 +00:00
|
|
|
#define STATE_OACK 3
|
2002-11-03 00:38:21 +00:00
|
|
|
|
2004-02-23 16:11:30 +00:00
|
|
|
#define TFTP_BLOCK_SIZE 512 /* default TFTP block size */
|
|
|
|
#define TFTP_SEQUENCE_SIZE ((ulong)(1<<16)) /* sequence number is 16 bit */
|
|
|
|
|
2002-11-03 00:38:21 +00:00
|
|
|
static char *tftp_filename;
|
|
|
|
|
2010-01-04 09:08:52 +00:00
|
|
|
static int net_store_fd;
|
2007-07-05 16:01:33 +00:00
|
|
|
|
2009-10-17 07:24:33 +00:00
|
|
|
static int store_block(unsigned block, uchar * src, unsigned len)
|
2002-11-03 00:38:21 +00:00
|
|
|
{
|
2004-02-23 16:11:30 +00:00
|
|
|
ulong offset = block * TFTP_BLOCK_SIZE + TftpBlockWrapOffset;
|
|
|
|
ulong newsize = offset + len;
|
2007-07-05 16:01:51 +00:00
|
|
|
int ret;
|
2007-07-05 16:01:33 +00:00
|
|
|
|
2007-07-05 16:01:51 +00:00
|
|
|
ret = write(net_store_fd, src, len);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2002-11-03 00:38:21 +00:00
|
|
|
|
|
|
|
if (NetBootFileXferSize < newsize)
|
|
|
|
NetBootFileXferSize = newsize;
|
2007-07-05 16:01:51 +00:00
|
|
|
return 0;
|
2002-11-03 00:38:21 +00:00
|
|
|
}
|
|
|
|
|
2009-10-17 07:24:33 +00:00
|
|
|
static void TftpSend(void)
|
2002-11-03 00:38:21 +00:00
|
|
|
{
|
2009-10-17 07:15:35 +00:00
|
|
|
uchar *pkt;
|
|
|
|
uchar *xp;
|
|
|
|
int len = 0;
|
|
|
|
ushort *s;
|
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) {
|
|
|
|
case STATE_RRQ:
|
|
|
|
xp = pkt;
|
2005-08-25 23:36:03 +00:00
|
|
|
s = (ushort *)pkt;
|
|
|
|
*s++ = htons(TFTP_RRQ);
|
|
|
|
pkt = (uchar *)s;
|
2008-03-31 19:54:37 +00:00
|
|
|
pkt += sprintf((uchar *)pkt, "%s%coctet%ctimeout%c%d",
|
|
|
|
tftp_filename, 0, 0, 0, TIMEOUT) + 1;
|
2002-11-03 00:38:21 +00:00
|
|
|
len = pkt - xp;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case STATE_DATA:
|
2003-10-06 21:55:32 +00:00
|
|
|
case STATE_OACK:
|
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;
|
|
|
|
}
|
|
|
|
|
2009-10-17 07:24:33 +00:00
|
|
|
NetSendUDPPacket(NetServerEther, NetServerIP, TftpServerPort,
|
|
|
|
TftpOurPort, len);
|
2002-11-03 00:38:21 +00:00
|
|
|
}
|
|
|
|
|
2009-10-17 07:24:33 +00:00
|
|
|
static void TftpTimeout(void)
|
2009-10-17 07:21:19 +00:00
|
|
|
{
|
2009-10-17 07:24:33 +00:00
|
|
|
puts("T ");
|
|
|
|
NetSetTimeout(TIMEOUT * SECOND, TftpTimeout);
|
|
|
|
TftpSend();
|
2009-10-17 07:21:19 +00:00
|
|
|
}
|
2002-11-03 00:38:21 +00:00
|
|
|
|
2009-10-17 07:24:33 +00:00
|
|
|
static void TftpHandler(uchar * pkt, unsigned dest, unsigned src, unsigned len)
|
2002-11-03 00:38:21 +00:00
|
|
|
{
|
|
|
|
ushort proto;
|
2005-08-25 23:36:03 +00:00
|
|
|
ushort *s;
|
2002-11-03 00:38:21 +00:00
|
|
|
|
2009-10-17 07:24:33 +00:00
|
|
|
if (dest != TftpOurPort)
|
2002-11-03 00:38:21 +00:00
|
|
|
return;
|
2009-10-17 07:24:33 +00:00
|
|
|
|
|
|
|
if (TftpState != STATE_RRQ && src != TftpServerPort)
|
2002-11-03 00:38:21 +00:00
|
|
|
return;
|
|
|
|
|
2009-10-17 07:24:33 +00:00
|
|
|
if (len < 2)
|
2002-11-03 00:38:21 +00:00
|
|
|
return;
|
2009-10-17 07:24:33 +00:00
|
|
|
|
2002-11-03 00:38:21 +00:00
|
|
|
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_WRQ:
|
|
|
|
case TFTP_ACK:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
|
2003-10-06 21:55:32 +00:00
|
|
|
case TFTP_OACK:
|
2009-10-17 07:35:26 +00:00
|
|
|
debug("Got OACK: %s %s\n", pkt, pkt+strlen(pkt)+1);
|
2003-10-06 21:55:32 +00:00
|
|
|
TftpState = STATE_OACK;
|
|
|
|
TftpServerPort = src;
|
2009-10-17 07:24:33 +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++;
|
|
|
|
TftpBlockWrapOffset += TFTP_BLOCK_SIZE * TFTP_SEQUENCE_SIZE;
|
2006-08-14 20:43:13 +00:00
|
|
|
printf ("\n\t %lu MB received\n\t ", TftpBlockWrapOffset>>20);
|
2004-02-23 16:11:30 +00:00
|
|
|
} else {
|
|
|
|
if (((TftpBlock - 1) % 10) == 0) {
|
2007-09-21 07:09:06 +00:00
|
|
|
putchar('#');
|
2004-02-23 16:11:30 +00:00
|
|
|
} else if ((TftpBlock % (10 * HASHES_PER_LINE)) == 0) {
|
2009-10-17 07:24:33 +00:00
|
|
|
puts("\n\t ");
|
2004-02-23 16:11:30 +00:00
|
|
|
}
|
2002-11-03 00:38:21 +00:00
|
|
|
}
|
|
|
|
|
2009-10-17 07:24:33 +00:00
|
|
|
if (TftpState == STATE_RRQ)
|
2009-10-17 07:35:26 +00:00
|
|
|
debug("Server did not acknowledge timeout option!\n");
|
2003-10-06 21:55:32 +00:00
|
|
|
|
|
|
|
if (TftpState == STATE_RRQ || TftpState == STATE_OACK) {
|
2004-02-23 16:11:30 +00:00
|
|
|
/* first block received */
|
2002-11-03 00:38:21 +00:00
|
|
|
TftpState = STATE_DATA;
|
|
|
|
TftpServerPort = src;
|
|
|
|
TftpLastBlock = 0;
|
2004-02-23 16:11:30 +00:00
|
|
|
TftpBlockWrap = 0;
|
|
|
|
TftpBlockWrapOffset = 0;
|
2002-11-03 00:38:21 +00:00
|
|
|
|
|
|
|
if (TftpBlock != 1) { /* Assertion */
|
2009-10-17 07:24:33 +00:00
|
|
|
printf("\nTFTP error: "
|
2004-02-23 16:11:30 +00:00
|
|
|
"First block is not block 1 (%ld)\n"
|
2002-11-03 00:38:21 +00:00
|
|
|
"Starting again\n\n",
|
|
|
|
TftpBlock);
|
2009-08-19 11:29:47 +00:00
|
|
|
NetState = NETLOOP_FAIL;
|
2002-11-03 00:38:21 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-10-17 07:24:33 +00:00
|
|
|
if (TftpBlock == TftpLastBlock)
|
|
|
|
/* Same block again; ignore it. */
|
2002-11-03 00:38:21 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
TftpLastBlock = TftpBlock;
|
2009-10-17 07:24:33 +00:00
|
|
|
NetSetTimeout(TIMEOUT * SECOND, TftpTimeout);
|
2002-11-03 00:38:21 +00:00
|
|
|
|
2009-10-17 07:24:33 +00:00
|
|
|
if (store_block(TftpBlock - 1, pkt + 2, len) < 0) {
|
2007-07-05 16:01:51 +00:00
|
|
|
perror("write");
|
|
|
|
NetState = NETLOOP_FAIL;
|
|
|
|
return;
|
|
|
|
}
|
2002-11-03 00:38:21 +00:00
|
|
|
|
|
|
|
/*
|
2009-10-17 07:24:33 +00:00
|
|
|
* Acknowledge the block just received, which will prompt
|
2002-11-03 00:38:21 +00:00
|
|
|
* the server for the next one.
|
|
|
|
*/
|
2009-10-17 07:24:33 +00:00
|
|
|
TftpSend();
|
2002-11-03 00:38:21 +00:00
|
|
|
|
2004-02-23 16:11:30 +00:00
|
|
|
if (len < TFTP_BLOCK_SIZE) {
|
2002-11-03 00:38:21 +00:00
|
|
|
/*
|
|
|
|
* We received the whole thing. Try to
|
|
|
|
* run it.
|
|
|
|
*/
|
2009-10-17 07:24:33 +00:00
|
|
|
puts("\ndone\n");
|
2002-11-03 00:38:21 +00:00
|
|
|
NetState = NETLOOP_SUCCESS;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TFTP_ERROR:
|
2009-10-17 07:24:33 +00:00
|
|
|
printf("\nTFTP error: '%s' (%d)\n",
|
2002-11-03 00:38:21 +00:00
|
|
|
pkt + 2, ntohs(*(ushort *)pkt));
|
2009-08-19 11:29:47 +00:00
|
|
|
NetState = NETLOOP_FAIL;
|
2002-11-03 00:38:21 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-10-17 09:37:23 +00:00
|
|
|
void TftpStart(char *filename)
|
2002-11-03 00:38:21 +00:00
|
|
|
{
|
2009-10-17 07:10:09 +00:00
|
|
|
char ip1[16], ip2[16];
|
2005-09-24 20:37:32 +00:00
|
|
|
|
2009-10-17 09:37:23 +00:00
|
|
|
tftp_filename = filename;
|
2002-11-03 00:38:21 +00:00
|
|
|
|
2009-10-17 07:10:09 +00:00
|
|
|
printf("TFTP from server %s; our IP address is %s\n"
|
|
|
|
"\nFilename '%s'.\nLoading: *\b",
|
|
|
|
ip_to_string(NetServerIP, ip1),
|
|
|
|
ip_to_string(NetOurIP, ip2),
|
|
|
|
tftp_filename);
|
2002-11-03 00:38:21 +00:00
|
|
|
|
2009-10-17 07:24:33 +00:00
|
|
|
NetSetTimeout(TIMEOUT * SECOND, TftpTimeout);
|
|
|
|
NetSetHandler(TftpHandler);
|
2002-11-03 00:38:21 +00:00
|
|
|
|
|
|
|
TftpServerPort = WELL_KNOWN_PORT;
|
|
|
|
TftpState = STATE_RRQ;
|
2009-10-17 07:24:33 +00:00
|
|
|
/* Use a pseudo-random port */
|
2007-07-05 16:01:54 +00:00
|
|
|
TftpOurPort = 1024 + ((unsigned int)get_time_ns() % 3072);
|
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);
|
|
|
|
|
2009-10-17 07:24:33 +00:00
|
|
|
TftpSend();
|
2002-11-03 00:38:21 +00:00
|
|
|
}
|
2010-01-04 09:08:52 +00:00
|
|
|
|
2010-01-04 09:21:11 +00:00
|
|
|
static int do_tftpb(struct command *cmdtp, int argc, char *argv[])
|
2010-01-04 09:08:52 +00:00
|
|
|
{
|
|
|
|
int rcode = 0;
|
|
|
|
char *localfile;
|
|
|
|
char *remotefile;
|
|
|
|
|
|
|
|
if (argc < 2)
|
|
|
|
return COMMAND_ERROR_USAGE;
|
|
|
|
|
|
|
|
remotefile = argv[1];
|
|
|
|
|
|
|
|
if (argc == 2)
|
|
|
|
localfile = basename(remotefile);
|
|
|
|
else
|
|
|
|
localfile = argv[2];
|
|
|
|
|
|
|
|
net_store_fd = open(localfile, O_WRONLY | O_CREAT);
|
|
|
|
if (net_store_fd < 0) {
|
|
|
|
perror("open");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NetLoopInit(TFTP) < 0)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
TftpStart(remotefile);
|
|
|
|
|
2010-04-14 08:06:06 +00:00
|
|
|
if (NetLoop() < 0) {
|
2010-01-04 09:08:52 +00:00
|
|
|
rcode = 1;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* NetLoop ok, update environment */
|
|
|
|
netboot_update_env();
|
|
|
|
|
|
|
|
out:
|
|
|
|
close(net_store_fd);
|
|
|
|
return rcode;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const __maybe_unused char cmd_tftp_help[] =
|
|
|
|
"Usage: tftp <file> [localfile]\n"
|
|
|
|
"Load a file via network using BootP/TFTP protocol.\n";
|
|
|
|
|
|
|
|
BAREBOX_CMD_START(tftp)
|
|
|
|
.cmd = do_tftpb,
|
|
|
|
.usage = "Load file using tftp protocol",
|
|
|
|
BAREBOX_CMD_HELP(cmd_tftp_help)
|
|
|
|
BAREBOX_CMD_END
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @page tftp_command tftp
|
|
|
|
*
|
|
|
|
* Usage is: tftp \<filename\> [\<localfilename\>]
|
|
|
|
*
|
|
|
|
* Load a file via network using BootP/TFTP protocol. The loaded file you
|
|
|
|
* can find after download in you current ramdisk. Refer \b ls command.
|
|
|
|
*
|
|
|
|
* \<localfile> can be the local filename only, or also a device name. In the
|
|
|
|
* case of a device name, the will gets stored there. This works also for
|
|
|
|
* partitions of flash memory. Refer \b erase, \b unprotect for flash
|
|
|
|
* preparation.
|
|
|
|
*
|
|
|
|
* Note: This command is available only, if enabled in the menuconfig.
|
|
|
|
*/
|
|
|
|
|