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>
|
2010-06-14 20:42:11 +00:00
|
|
|
#include <progress.h>
|
2010-06-15 07:32:51 +00:00
|
|
|
#include <getopt.h>
|
|
|
|
#include <fs.h>
|
|
|
|
#include <linux/stat.h>
|
2010-06-02 13:50:45 +00:00
|
|
|
#include <linux/err.h>
|
2002-11-03 00:38:21 +00:00
|
|
|
|
2010-06-02 13:50:45 +00:00
|
|
|
#define TFTP_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
|
|
|
|
|
|
|
/*
|
|
|
|
* 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
|
|
|
|
|
|
|
|
2010-06-02 13:50:45 +00:00
|
|
|
static int tftp_server_port; /* The UDP port at their end */
|
|
|
|
static unsigned int tftp_block; /* packet sequence number */
|
|
|
|
static unsigned int tftp_last_block; /* last packet sequence number received */
|
|
|
|
static int tftp_state;
|
|
|
|
static uint64_t tftp_timer_start;
|
|
|
|
static int tftp_err;
|
2004-02-23 16:11:30 +00:00
|
|
|
|
2002-11-03 00:38:21 +00:00
|
|
|
#define STATE_RRQ 1
|
2010-06-15 07:32:51 +00:00
|
|
|
#define STATE_WRQ 2
|
|
|
|
#define STATE_RDATA 3
|
|
|
|
#define STATE_WDATA 4
|
|
|
|
#define STATE_OACK 5
|
|
|
|
#define STATE_LAST 6
|
|
|
|
#define STATE_DONE 7
|
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 */
|
|
|
|
|
2002-11-03 00:38:21 +00:00
|
|
|
static char *tftp_filename;
|
2010-06-02 13:50:45 +00:00
|
|
|
static struct net_connection *tftp_con;
|
2010-06-15 07:32:51 +00:00
|
|
|
static int tftp_fd;
|
2010-06-14 20:42:11 +00:00
|
|
|
static int tftp_size;
|
2007-07-05 16:01:33 +00:00
|
|
|
|
2010-06-15 07:32:51 +00:00
|
|
|
#ifdef CONFIG_NET_TFTP_PUSH
|
|
|
|
static int tftp_push;
|
|
|
|
|
|
|
|
static inline void do_tftp_push(int push)
|
|
|
|
{
|
|
|
|
tftp_push = push;
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
#define tftp_push 0
|
|
|
|
|
|
|
|
static inline void do_tftp_push(int push)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2010-06-02 13:50:45 +00:00
|
|
|
static int tftp_send(void)
|
2002-11-03 00:38:21 +00:00
|
|
|
{
|
2010-06-02 13:50:45 +00:00
|
|
|
unsigned char *xp;
|
2009-10-17 07:15:35 +00:00
|
|
|
int len = 0;
|
2010-06-02 13:50:45 +00:00
|
|
|
uint16_t *s;
|
2010-06-15 07:32:51 +00:00
|
|
|
unsigned char *pkt = net_udp_get_payload(tftp_con);
|
2010-06-02 13:50:45 +00:00
|
|
|
int ret;
|
2010-06-15 07:32:51 +00:00
|
|
|
static int last_len;
|
2002-11-03 00:38:21 +00:00
|
|
|
|
2010-06-02 13:50:45 +00:00
|
|
|
switch (tftp_state) {
|
2002-11-03 00:38:21 +00:00
|
|
|
case STATE_RRQ:
|
2010-06-15 07:32:51 +00:00
|
|
|
case STATE_WRQ:
|
2002-11-03 00:38:21 +00:00
|
|
|
xp = pkt;
|
2010-06-02 13:50:45 +00:00
|
|
|
s = (uint16_t *)pkt;
|
2010-06-15 07:32:51 +00:00
|
|
|
if (tftp_state == STATE_RRQ)
|
|
|
|
*s++ = htons(TFTP_RRQ);
|
|
|
|
else
|
|
|
|
*s++ = htons(TFTP_WRQ);
|
2010-06-02 13:50:45 +00:00
|
|
|
pkt = (unsigned char *)s;
|
|
|
|
pkt += sprintf((unsigned char *)pkt, "%s%coctet%ctimeout%c%d",
|
2008-03-31 19:54:37 +00:00
|
|
|
tftp_filename, 0, 0, 0, TIMEOUT) + 1;
|
2002-11-03 00:38:21 +00:00
|
|
|
len = pkt - xp;
|
|
|
|
break;
|
|
|
|
|
2010-06-15 07:32:51 +00:00
|
|
|
case STATE_WDATA:
|
|
|
|
if (!tftp_push)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (tftp_last_block == tftp_block) {
|
|
|
|
len = last_len;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
tftp_last_block = tftp_block;
|
|
|
|
s = (uint16_t *)pkt;
|
|
|
|
*s++ = htons(TFTP_DATA);
|
|
|
|
*s++ = htons(tftp_block);
|
|
|
|
len = read(tftp_fd, s, 512);
|
|
|
|
if (len < 0) {
|
|
|
|
perror("read");
|
|
|
|
tftp_err = -errno;
|
|
|
|
tftp_state = STATE_DONE;
|
|
|
|
return tftp_err;
|
|
|
|
}
|
|
|
|
tftp_size += len;
|
|
|
|
if (len < 512)
|
|
|
|
tftp_state = STATE_LAST;
|
|
|
|
len += 4;
|
|
|
|
last_len = len;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case STATE_RDATA:
|
2003-10-06 21:55:32 +00:00
|
|
|
case STATE_OACK:
|
2002-11-03 00:38:21 +00:00
|
|
|
xp = pkt;
|
2010-06-02 13:50:45 +00:00
|
|
|
s = (uint16_t *)pkt;
|
2005-08-25 23:36:03 +00:00
|
|
|
*s++ = htons(TFTP_ACK);
|
2010-06-02 13:50:45 +00:00
|
|
|
*s++ = htons(tftp_block);
|
|
|
|
pkt = (unsigned char *)s;
|
2002-11-03 00:38:21 +00:00
|
|
|
len = pkt - xp;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2010-06-14 20:42:11 +00:00
|
|
|
tftp_timer_start = get_time_ns();
|
|
|
|
show_progress(tftp_size);
|
2010-06-02 13:50:45 +00:00
|
|
|
ret = net_udp_send(tftp_con, len);
|
2002-11-03 00:38:21 +00:00
|
|
|
|
2010-06-02 13:50:45 +00:00
|
|
|
return ret;
|
2009-10-17 07:21:19 +00:00
|
|
|
}
|
2002-11-03 00:38:21 +00:00
|
|
|
|
2011-04-10 17:09:21 +00:00
|
|
|
static void tftp_handler(void *ctx, char *packet, unsigned len)
|
2002-11-03 00:38:21 +00:00
|
|
|
{
|
2010-06-02 13:50:45 +00:00
|
|
|
uint16_t proto;
|
|
|
|
uint16_t *s;
|
|
|
|
char *pkt = net_eth_to_udp_payload(packet);
|
|
|
|
struct udphdr *udp = net_eth_to_udphdr(packet);
|
|
|
|
int ret;
|
2002-11-03 00:38:21 +00:00
|
|
|
|
2010-06-02 13:50:45 +00:00
|
|
|
len = net_eth_to_udplen(packet);
|
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;
|
2010-06-15 07:32:51 +00:00
|
|
|
|
2010-06-02 13:50:45 +00:00
|
|
|
s = (uint16_t *)pkt;
|
2005-08-25 23:36:03 +00:00
|
|
|
proto = *s++;
|
2010-06-02 13:50:45 +00:00
|
|
|
pkt = (unsigned char *)s;
|
2010-06-15 07:32:51 +00:00
|
|
|
|
2002-11-03 00:38:21 +00:00
|
|
|
switch (ntohs(proto)) {
|
|
|
|
case TFTP_RRQ:
|
|
|
|
case TFTP_WRQ:
|
|
|
|
default:
|
|
|
|
break;
|
2010-06-15 07:32:51 +00:00
|
|
|
case TFTP_ACK:
|
|
|
|
if (!tftp_push)
|
|
|
|
break;
|
|
|
|
|
|
|
|
tftp_block = ntohs(*(uint16_t *)pkt);
|
|
|
|
if (tftp_block != tftp_last_block) {
|
|
|
|
debug("ack %d != %d\n", tftp_block, tftp_last_block);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
tftp_block++;
|
|
|
|
if (tftp_state == STATE_LAST) {
|
|
|
|
tftp_state = STATE_DONE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
tftp_con->udp->uh_dport = udp->uh_sport;
|
|
|
|
tftp_state = STATE_WDATA;
|
|
|
|
tftp_send();
|
|
|
|
break;
|
2002-11-03 00:38:21 +00:00
|
|
|
|
2003-10-06 21:55:32 +00:00
|
|
|
case TFTP_OACK:
|
2010-06-02 13:50:45 +00:00
|
|
|
debug("Got OACK: %s %s\n", pkt, pkt + strlen(pkt) + 1);
|
|
|
|
tftp_server_port = ntohs(udp->uh_sport);
|
|
|
|
tftp_con->udp->uh_dport = udp->uh_sport;
|
2010-06-15 07:32:51 +00:00
|
|
|
|
|
|
|
if (tftp_push) {
|
|
|
|
/* send first block */
|
|
|
|
tftp_state = STATE_WDATA;
|
|
|
|
tftp_block = 1;
|
|
|
|
} else {
|
|
|
|
/* send ACK */
|
|
|
|
tftp_state = STATE_OACK;
|
|
|
|
tftp_block = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
tftp_send();
|
|
|
|
|
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;
|
2010-06-02 13:50:45 +00:00
|
|
|
tftp_block = ntohs(*(uint16_t *)pkt);
|
2004-02-23 16:11:30 +00:00
|
|
|
|
2010-06-02 13:50:45 +00:00
|
|
|
if (tftp_state == 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
|
|
|
|
2010-06-02 13:50:45 +00:00
|
|
|
if (tftp_state == STATE_RRQ || tftp_state == STATE_OACK) {
|
2004-02-23 16:11:30 +00:00
|
|
|
/* first block received */
|
2010-06-15 07:32:51 +00:00
|
|
|
tftp_state = STATE_RDATA;
|
2010-06-02 13:50:45 +00:00
|
|
|
tftp_con->udp->uh_dport = udp->uh_sport;
|
|
|
|
tftp_server_port = ntohs(udp->uh_sport);
|
|
|
|
tftp_last_block = 0;
|
|
|
|
|
|
|
|
if (tftp_block != 1) { /* Assertion */
|
2010-12-20 23:02:02 +00:00
|
|
|
printf("error: First block is not block 1 (%d)\n",
|
2010-06-02 13:50:45 +00:00
|
|
|
tftp_block);
|
|
|
|
tftp_err = -EINVAL;
|
|
|
|
tftp_state = STATE_DONE;
|
2002-11-03 00:38:21 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-06-02 13:50:45 +00:00
|
|
|
if (tftp_block == tftp_last_block)
|
2009-10-17 07:24:33 +00:00
|
|
|
/* Same block again; ignore it. */
|
2002-11-03 00:38:21 +00:00
|
|
|
break;
|
|
|
|
|
2010-06-02 13:50:45 +00:00
|
|
|
tftp_last_block = tftp_block;
|
2010-06-14 20:42:11 +00:00
|
|
|
|
|
|
|
if (!(tftp_block % 10))
|
|
|
|
tftp_size++;
|
2002-11-03 00:38:21 +00:00
|
|
|
|
2010-06-15 07:32:51 +00:00
|
|
|
ret = write(tftp_fd, pkt + 2, len);
|
2010-06-02 13:50:45 +00:00
|
|
|
if (ret < 0) {
|
2007-07-05 16:01:51 +00:00
|
|
|
perror("write");
|
2010-06-02 13:50:45 +00:00
|
|
|
tftp_err = -errno;
|
|
|
|
tftp_state = STATE_DONE;
|
2007-07-05 16:01:51 +00:00
|
|
|
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.
|
|
|
|
*/
|
2010-06-02 13:50:45 +00:00
|
|
|
tftp_send();
|
|
|
|
|
|
|
|
if (len < TFTP_BLOCK_SIZE)
|
|
|
|
tftp_state = STATE_DONE;
|
|
|
|
|
2002-11-03 00:38:21 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case TFTP_ERROR:
|
2010-06-02 13:50:45 +00:00
|
|
|
debug("\nTFTP error: '%s' (%d)\n",
|
|
|
|
pkt + 2, ntohs(*(uint16_t *)pkt));
|
|
|
|
switch (ntohs(*(uint16_t *)pkt)) {
|
|
|
|
case 1: tftp_err = -ENOENT; break;
|
|
|
|
case 2: tftp_err = -EACCES; break;
|
|
|
|
default: tftp_err = -EINVAL; break;
|
|
|
|
}
|
|
|
|
tftp_state = STATE_DONE;
|
2002-11-03 00:38:21 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2010-06-15 07:32:51 +00:00
|
|
|
char *localfile, *remotefile, *file1, *file2;
|
2010-06-02 13:50:45 +00:00
|
|
|
char ip1[16];
|
2010-06-15 07:32:51 +00:00
|
|
|
int opt;
|
|
|
|
struct stat s;
|
|
|
|
unsigned long flags;
|
2010-01-04 09:08:52 +00:00
|
|
|
|
2010-06-15 07:32:51 +00:00
|
|
|
do_tftp_push(0);
|
|
|
|
tftp_last_block = 0;
|
2010-06-14 20:42:11 +00:00
|
|
|
tftp_size = 0;
|
|
|
|
|
2010-06-15 07:32:51 +00:00
|
|
|
while((opt = getopt(argc, argv, "p")) > 0) {
|
|
|
|
switch(opt) {
|
|
|
|
case 'p':
|
|
|
|
do_tftp_push(1);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (argc <= optind)
|
2010-01-04 09:08:52 +00:00
|
|
|
return COMMAND_ERROR_USAGE;
|
|
|
|
|
2010-06-15 07:32:51 +00:00
|
|
|
file1 = argv[optind++];
|
2010-01-04 09:08:52 +00:00
|
|
|
|
2010-06-15 07:32:51 +00:00
|
|
|
if (argc == optind)
|
|
|
|
file2 = basename(file1);
|
2010-01-04 09:08:52 +00:00
|
|
|
else
|
2010-06-15 07:32:51 +00:00
|
|
|
file2 = argv[optind];
|
|
|
|
|
|
|
|
if (tftp_push) {
|
|
|
|
localfile = file1;
|
|
|
|
remotefile = file2;
|
|
|
|
stat(localfile, &s);
|
|
|
|
flags = O_RDONLY;
|
|
|
|
} else {
|
|
|
|
localfile = file2;
|
|
|
|
remotefile = file1;
|
|
|
|
flags = O_WRONLY | O_CREAT;
|
|
|
|
}
|
2010-01-04 09:08:52 +00:00
|
|
|
|
2010-06-15 07:32:51 +00:00
|
|
|
tftp_fd = open(localfile, flags);
|
|
|
|
if (tftp_fd < 0) {
|
2010-01-04 09:08:52 +00:00
|
|
|
perror("open");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2011-04-10 17:09:21 +00:00
|
|
|
tftp_con = net_udp_new(net_get_serverip(), TFTP_PORT, tftp_handler, NULL);
|
2010-06-02 13:50:45 +00:00
|
|
|
if (IS_ERR(tftp_con)) {
|
|
|
|
tftp_err = PTR_ERR(tftp_con);
|
|
|
|
goto out_close;
|
|
|
|
}
|
2010-01-04 09:08:52 +00:00
|
|
|
|
2010-06-02 13:50:45 +00:00
|
|
|
tftp_filename = remotefile;
|
2010-01-04 09:08:52 +00:00
|
|
|
|
2010-06-15 07:32:51 +00:00
|
|
|
printf("TFTP %s server %s ('%s' -> '%s')\n",
|
|
|
|
tftp_push ? "to" : "from",
|
2010-06-02 13:50:45 +00:00
|
|
|
ip_to_string(net_get_serverip(), ip1),
|
2010-06-15 07:32:51 +00:00
|
|
|
file1, file2);
|
2010-06-02 13:50:45 +00:00
|
|
|
|
2010-06-15 07:32:51 +00:00
|
|
|
init_progression_bar(tftp_push ? s.st_size : 0);
|
2010-06-14 20:42:11 +00:00
|
|
|
|
2010-06-02 13:50:45 +00:00
|
|
|
tftp_timer_start = get_time_ns();
|
2010-06-15 07:32:51 +00:00
|
|
|
tftp_state = tftp_push ? STATE_WRQ : STATE_RRQ;
|
|
|
|
tftp_block = 1;
|
2010-01-04 09:08:52 +00:00
|
|
|
|
2010-06-02 13:50:45 +00:00
|
|
|
tftp_err = tftp_send();
|
|
|
|
if (tftp_err)
|
|
|
|
goto out_unreg;
|
2010-01-04 09:08:52 +00:00
|
|
|
|
2010-06-02 13:50:45 +00:00
|
|
|
while (tftp_state != STATE_DONE) {
|
|
|
|
if (ctrlc()) {
|
|
|
|
tftp_err = -EINTR;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
net_poll();
|
|
|
|
if (is_timeout(tftp_timer_start, SECOND)) {
|
2010-06-14 20:42:11 +00:00
|
|
|
show_progress(-1);
|
2010-06-02 13:50:45 +00:00
|
|
|
tftp_send();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
out_unreg:
|
|
|
|
net_unregister(tftp_con);
|
|
|
|
out_close:
|
2010-06-15 07:32:51 +00:00
|
|
|
close(tftp_fd);
|
2010-06-02 13:50:45 +00:00
|
|
|
|
|
|
|
if (tftp_err) {
|
|
|
|
printf("\ntftp failed: %s\n", strerror(-tftp_err));
|
2010-06-15 07:32:51 +00:00
|
|
|
if (!tftp_push)
|
|
|
|
unlink(localfile);
|
2010-06-02 13:50:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
printf("\n");
|
|
|
|
|
|
|
|
return tftp_err == 0 ? 0 : 1;
|
2010-01-04 09:08:52 +00:00
|
|
|
}
|
|
|
|
|
2010-10-31 21:05:47 +00:00
|
|
|
BAREBOX_CMD_HELP_START(tftp)
|
2010-06-15 07:32:51 +00:00
|
|
|
#ifdef CONFIG_NET_TFTP_PUSH
|
2010-10-31 21:05:47 +00:00
|
|
|
BAREBOX_CMD_HELP_USAGE("tftp <remotefile> [localfile], tftp -p <localfile> [remotefile]\n")
|
|
|
|
BAREBOX_CMD_HELP_SHORT("Load a file from or upload to TFTP server.\n")
|
|
|
|
BAREBOX_CMD_HELP_END
|
|
|
|
#else
|
|
|
|
BAREBOX_CMD_HELP_USAGE("tftp <remotefile> [localfile]\n")
|
|
|
|
BAREBOX_CMD_HELP_SHORT("Load a file from a TFTP server.\n")
|
|
|
|
BAREBOX_CMD_HELP_END
|
2010-06-15 07:32:51 +00:00
|
|
|
#endif
|
2010-10-31 21:05:47 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @page tftp_command
|
|
|
|
|
|
|
|
The second file argument can be skipped in which case the first filename
|
|
|
|
is used (without the directory part).
|
|
|
|
|
|
|
|
\<localfile> can be the local filename or a device file under /dev.
|
|
|
|
This also works for flash memory. Refer to \ref erase_command and \ref
|
|
|
|
unprotect_command for flash preparation.
|
|
|
|
|
|
|
|
\note This command is available only if enabled in menuconfig.
|
|
|
|
*/
|
2010-01-04 09:08:52 +00:00
|
|
|
|
|
|
|
BAREBOX_CMD_START(tftp)
|
|
|
|
.cmd = do_tftpb,
|
2010-06-15 07:32:51 +00:00
|
|
|
.usage =
|
|
|
|
#ifdef CONFIG_NET_TFTP_PUSH
|
|
|
|
"(up-)"
|
|
|
|
#endif
|
|
|
|
"Load file using tftp protocol",
|
2010-01-04 09:08:52 +00:00
|
|
|
BAREBOX_CMD_HELP(cmd_tftp_help)
|
|
|
|
BAREBOX_CMD_END
|
|
|
|
|