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-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
|
|
|
# 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
|
|
|
|
|
|
|
|
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
|
|
|
|
#define STATE_DATA 2
|
2009-10-17 07:18:49 +00:00
|
|
|
#define STATE_OACK 3
|
2010-06-02 13:50:45 +00:00
|
|
|
#define STATE_DONE 4
|
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 */
|
2010-06-02 13:50:45 +00:00
|
|
|
#define TFTP_SEQUENCE_SIZE ((unsigned long)(1<<16)) /* sequence number is 16 bit */
|
2004-02-23 16:11:30 +00:00
|
|
|
|
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-01-04 09:08:52 +00:00
|
|
|
static int net_store_fd;
|
2007-07-05 16:01:33 +00:00
|
|
|
|
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 *pkt;
|
|
|
|
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;
|
|
|
|
unsigned char *packet = net_udp_get_payload(tftp_con);
|
|
|
|
int ret;
|
2002-11-03 00:38:21 +00:00
|
|
|
|
2010-06-02 13:50:45 +00:00
|
|
|
pkt = packet;
|
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:
|
|
|
|
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_RRQ);
|
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;
|
|
|
|
|
|
|
|
case STATE_DATA:
|
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-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
|
|
|
|
2010-06-02 13:50:45 +00:00
|
|
|
static void tftp_handler(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;
|
|
|
|
/* warning: don't use increment (++) in ntohs() macros!! */
|
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;
|
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:
|
2010-06-02 13:50:45 +00:00
|
|
|
debug("Got OACK: %s %s\n", pkt, pkt + strlen(pkt) + 1);
|
|
|
|
tftp_state = STATE_OACK;
|
|
|
|
tftp_server_port = ntohs(udp->uh_sport);
|
|
|
|
tftp_con->udp->uh_dport = udp->uh_sport;
|
|
|
|
tftp_send(); /* 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;
|
2010-06-02 13:50:45 +00:00
|
|
|
tftp_block = ntohs(*(uint16_t *)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
|
|
|
*/
|
2010-06-14 12:56:50 +00:00
|
|
|
if (tftp_block) {
|
2010-06-02 13:50:45 +00:00
|
|
|
if (((tftp_block - 1) % 10) == 0) {
|
2007-09-21 07:09:06 +00:00
|
|
|
putchar('#');
|
2010-06-02 13:50:45 +00:00
|
|
|
} else if ((tftp_block % (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
|
|
|
}
|
|
|
|
|
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-02 13:50:45 +00:00
|
|
|
tftp_state = STATE_DATA;
|
|
|
|
tftp_con->udp->uh_dport = udp->uh_sport;
|
|
|
|
tftp_server_port = ntohs(udp->uh_sport);
|
|
|
|
tftp_last_block = 0;
|
|
|
|
|
|
|
|
if (tftp_block != 1) { /* Assertion */
|
|
|
|
printf("error: First block is not block 1 (%ld)\n",
|
|
|
|
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;
|
|
|
|
tftp_timer_start = get_time_ns();
|
2002-11-03 00:38:21 +00:00
|
|
|
|
2010-06-02 13:50:45 +00:00
|
|
|
ret = write(net_store_fd, pkt + 2, len);
|
|
|
|
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-02 13:50:45 +00:00
|
|
|
char *localfile;
|
|
|
|
char *remotefile;
|
|
|
|
char ip1[16];
|
2010-01-04 09:08:52 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2010-06-02 13:50:45 +00:00
|
|
|
tftp_con = net_udp_new(net_get_serverip(), TFTP_PORT, tftp_handler);
|
|
|
|
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-02 13:50:45 +00:00
|
|
|
printf("TFTP from server %s; Filename: '%s'\nLoading: ",
|
|
|
|
ip_to_string(net_get_serverip(), ip1),
|
|
|
|
tftp_filename);
|
|
|
|
|
|
|
|
tftp_timer_start = get_time_ns();
|
|
|
|
tftp_state = STATE_RRQ;
|
|
|
|
tftp_block = 0;
|
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)) {
|
|
|
|
tftp_timer_start = get_time_ns();
|
|
|
|
printf("T ");
|
|
|
|
tftp_send();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
out_unreg:
|
|
|
|
net_unregister(tftp_con);
|
|
|
|
out_close:
|
2010-01-04 09:08:52 +00:00
|
|
|
close(net_store_fd);
|
2010-06-02 13:50:45 +00:00
|
|
|
|
|
|
|
if (tftp_err) {
|
|
|
|
printf("\ntftp failed: %s\n", strerror(-tftp_err));
|
|
|
|
unlink(localfile);
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("\n");
|
|
|
|
|
|
|
|
return tftp_err == 0 ? 0 : 1;
|
2010-01-04 09:08:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|