//==========================================================================
|
//==========================================================================
|
//
|
//
|
// net/tftp_client.c
|
// net/tftp_client.c
|
//
|
//
|
// Stand-alone TFTP support for RedBoot
|
// Stand-alone TFTP support for RedBoot
|
//
|
//
|
//==========================================================================
|
//==========================================================================
|
//####ECOSGPLCOPYRIGHTBEGIN####
|
//####ECOSGPLCOPYRIGHTBEGIN####
|
// -------------------------------------------
|
// -------------------------------------------
|
// This file is part of eCos, the Embedded Configurable Operating System.
|
// This file is part of eCos, the Embedded Configurable Operating System.
|
// Copyright (C) 1998, 1999, 2000, 2001, 2002 Red Hat, Inc.
|
// Copyright (C) 1998, 1999, 2000, 2001, 2002 Red Hat, Inc.
|
// Copyright (C) 2002 Gary Thomas
|
// Copyright (C) 2002 Gary Thomas
|
//
|
//
|
// eCos is free software; you can redistribute it and/or modify it under
|
// eCos is free software; you can redistribute it and/or modify it under
|
// the terms of the GNU General Public License as published by the Free
|
// the terms of the GNU General Public License as published by the Free
|
// Software Foundation; either version 2 or (at your option) any later version.
|
// Software Foundation; either version 2 or (at your option) any later version.
|
//
|
//
|
// eCos is distributed in the hope that it will be useful, but WITHOUT ANY
|
// eCos is distributed in the hope that it will be useful, but WITHOUT ANY
|
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
// for more details.
|
// for more details.
|
//
|
//
|
// You should have received a copy of the GNU General Public License along
|
// You should have received a copy of the GNU General Public License along
|
// with eCos; if not, write to the Free Software Foundation, Inc.,
|
// with eCos; if not, write to the Free Software Foundation, Inc.,
|
// 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
|
// 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
|
//
|
//
|
// As a special exception, if other files instantiate templates or use macros
|
// As a special exception, if other files instantiate templates or use macros
|
// or inline functions from this file, or you compile this file and link it
|
// or inline functions from this file, or you compile this file and link it
|
// with other works to produce a work based on this file, this file does not
|
// with other works to produce a work based on this file, this file does not
|
// by itself cause the resulting work to be covered by the GNU General Public
|
// by itself cause the resulting work to be covered by the GNU General Public
|
// License. However the source code for this file must still be made available
|
// License. However the source code for this file must still be made available
|
// in accordance with section (3) of the GNU General Public License.
|
// in accordance with section (3) of the GNU General Public License.
|
//
|
//
|
// This exception does not invalidate any other reasons why a work based on
|
// This exception does not invalidate any other reasons why a work based on
|
// this file might be covered by the GNU General Public License.
|
// this file might be covered by the GNU General Public License.
|
//
|
//
|
// Alternative licenses for eCos may be arranged by contacting Red Hat, Inc.
|
// Alternative licenses for eCos may be arranged by contacting Red Hat, Inc.
|
// at http://sources.redhat.com/ecos/ecos-license/
|
// at http://sources.redhat.com/ecos/ecos-license/
|
// -------------------------------------------
|
// -------------------------------------------
|
//####ECOSGPLCOPYRIGHTEND####
|
//####ECOSGPLCOPYRIGHTEND####
|
//==========================================================================
|
//==========================================================================
|
//#####DESCRIPTIONBEGIN####
|
//#####DESCRIPTIONBEGIN####
|
//
|
//
|
// Author(s): gthomas
|
// Author(s): gthomas
|
// Contributors: gthomas
|
// Contributors: gthomas
|
// Date: 2000-07-14
|
// Date: 2000-07-14
|
// Purpose:
|
// Purpose:
|
// Description:
|
// Description:
|
//
|
//
|
// This code is part of RedBoot (tm).
|
// This code is part of RedBoot (tm).
|
//
|
//
|
//####DESCRIPTIONEND####
|
//####DESCRIPTIONEND####
|
//
|
//
|
//==========================================================================
|
//==========================================================================
|
|
|
// TFTP client support
|
// TFTP client support
|
|
|
#include <redboot.h> // have_net
|
#include <redboot.h> // have_net
|
#include <net/net.h>
|
#include <net/net.h>
|
#include <net/tftp.h>
|
#include <net/tftp.h>
|
#include <net/tftp_support.h>
|
#include <net/tftp_support.h>
|
|
|
// So we remember which ports have been used
|
// So we remember which ports have been used
|
static int get_port = 7700;
|
static int get_port = 7700;
|
|
|
static struct {
|
static struct {
|
bool open;
|
bool open;
|
int total_timeouts;
|
int total_timeouts;
|
unsigned short last_good_block;
|
unsigned short last_good_block;
|
int avail, actual_len;
|
int avail, actual_len;
|
struct sockaddr_in local_addr, from_addr;
|
struct sockaddr_in local_addr, from_addr;
|
char data[SEGSIZE+sizeof(struct tftphdr)];
|
char data[SEGSIZE+sizeof(struct tftphdr)];
|
char *bufp;
|
char *bufp;
|
} tftp_stream;
|
} tftp_stream;
|
|
|
int
|
int
|
tftp_stream_open(connection_info_t *info,
|
tftp_stream_open(connection_info_t *info,
|
int *err)
|
int *err)
|
{
|
{
|
struct tftphdr *hdr = (struct tftphdr *)tftp_stream.data;
|
struct tftphdr *hdr = (struct tftphdr *)tftp_stream.data;
|
char *cp, *fp;
|
char *cp, *fp;
|
char test_buf;
|
char test_buf;
|
|
|
if (!have_net || tftp_stream.open) {
|
if (!have_net || tftp_stream.open) {
|
*err = TFTP_INVALID; // Already open
|
*err = TFTP_INVALID; // Already open
|
return -1;
|
return -1;
|
}
|
}
|
|
|
// Create initial request
|
// Create initial request
|
hdr->th_opcode = htons(RRQ); // Read file
|
hdr->th_opcode = htons(RRQ); // Read file
|
cp = (char *)&hdr->th_stuff;
|
cp = (char *)&hdr->th_stuff;
|
fp = info->filename;
|
fp = info->filename;
|
while (*fp) *cp++ = *fp++;
|
while (*fp) *cp++ = *fp++;
|
*cp++ = '\0';
|
*cp++ = '\0';
|
// Since this is used for downloading data, OCTET (binary) is the
|
// Since this is used for downloading data, OCTET (binary) is the
|
// only mode that makes sense.
|
// only mode that makes sense.
|
fp = "OCTET";
|
fp = "OCTET";
|
while (*fp) *cp++ = *fp++;
|
while (*fp) *cp++ = *fp++;
|
*cp++ = '\0';
|
*cp++ = '\0';
|
|
|
memset((char *)&tftp_stream.local_addr, 0, sizeof(tftp_stream.local_addr));
|
memset((char *)&tftp_stream.local_addr, 0, sizeof(tftp_stream.local_addr));
|
tftp_stream.local_addr.sin_family = AF_INET;
|
tftp_stream.local_addr.sin_family = AF_INET;
|
tftp_stream.local_addr.sin_addr.s_addr = htonl(INADDR_ANY);
|
tftp_stream.local_addr.sin_addr.s_addr = htonl(INADDR_ANY);
|
tftp_stream.local_addr.sin_port = htons(get_port++);
|
tftp_stream.local_addr.sin_port = htons(get_port++);
|
|
|
if (info->server->sin_port == 0) {
|
if (info->server->sin_port == 0) {
|
info->server->sin_port = htons(TFTP_PORT);
|
info->server->sin_port = htons(TFTP_PORT);
|
}
|
}
|
|
|
// Send request - note: RFC 1350 (TFTP rev 2) indicates that this should be
|
// Send request - note: RFC 1350 (TFTP rev 2) indicates that this should be
|
// only as long as required to hold the request, with the nul terminator.
|
// only as long as required to hold the request, with the nul terminator.
|
// Some servers silently go to lunch if the request is not the correct size.
|
// Some servers silently go to lunch if the request is not the correct size.
|
if (__udp_sendto(tftp_stream.data, cp-(char *)hdr,
|
if (__udp_sendto(tftp_stream.data, cp-(char *)hdr,
|
info->server, &tftp_stream.local_addr) < 0) {
|
info->server, &tftp_stream.local_addr) < 0) {
|
// Problem sending request
|
// Problem sending request
|
*err = TFTP_NETERR;
|
*err = TFTP_NETERR;
|
return -1;
|
return -1;
|
}
|
}
|
|
|
tftp_stream.open = true;
|
tftp_stream.open = true;
|
tftp_stream.avail = 0;
|
tftp_stream.avail = 0;
|
tftp_stream.actual_len = -1;
|
tftp_stream.actual_len = -1;
|
tftp_stream.last_good_block = 0;
|
tftp_stream.last_good_block = 0;
|
tftp_stream.total_timeouts = 0;
|
tftp_stream.total_timeouts = 0;
|
tftp_stream.from_addr.sin_port = 0;
|
tftp_stream.from_addr.sin_port = 0;
|
|
|
// Try and read the first byte [block] since no errors are
|
// Try and read the first byte [block] since no errors are
|
// reported until then.
|
// reported until then.
|
if (tftp_stream_read(&test_buf, 1, err) == 1) {
|
if (tftp_stream_read(&test_buf, 1, err) == 1) {
|
// Back up [rewind] over this datum
|
// Back up [rewind] over this datum
|
tftp_stream.bufp--;
|
tftp_stream.bufp--;
|
tftp_stream.avail++;
|
tftp_stream.avail++;
|
return 0; // Open and first read successful
|
return 0; // Open and first read successful
|
} else {
|
} else {
|
tftp_stream.open = false;
|
tftp_stream.open = false;
|
return -1; // Couldn't read
|
return -1; // Couldn't read
|
}
|
}
|
}
|
}
|
|
|
void
|
void
|
tftp_stream_close(int *err)
|
tftp_stream_close(int *err)
|
{
|
{
|
tftp_stream.open = false;
|
tftp_stream.open = false;
|
}
|
}
|
|
|
int
|
int
|
tftp_stream_read(char *buf,
|
tftp_stream_read(char *buf,
|
int len,
|
int len,
|
int *err)
|
int *err)
|
{
|
{
|
int total_bytes = 0;
|
int total_bytes = 0;
|
int size, recv_len, data_len;
|
int size, recv_len, data_len;
|
struct timeval timeout;
|
struct timeval timeout;
|
struct tftphdr *hdr = (struct tftphdr *)tftp_stream.data;
|
struct tftphdr *hdr = (struct tftphdr *)tftp_stream.data;
|
|
|
while (total_bytes < len) {
|
while (total_bytes < len) {
|
// Move any bytes which we've already read/buffered
|
// Move any bytes which we've already read/buffered
|
if (tftp_stream.avail > 0) {
|
if (tftp_stream.avail > 0) {
|
size = tftp_stream.avail;
|
size = tftp_stream.avail;
|
if (size > (len - total_bytes)) size = len - total_bytes;
|
if (size > (len - total_bytes)) size = len - total_bytes;
|
memcpy(buf, tftp_stream.bufp, size);
|
memcpy(buf, tftp_stream.bufp, size);
|
buf += size;
|
buf += size;
|
tftp_stream.bufp += size;
|
tftp_stream.bufp += size;
|
tftp_stream.avail -= size;
|
tftp_stream.avail -= size;
|
total_bytes += size;
|
total_bytes += size;
|
} else {
|
} else {
|
if (tftp_stream.last_good_block != 0) {
|
if (tftp_stream.last_good_block != 0) {
|
// Send out the ACK
|
// Send out the ACK
|
hdr->th_opcode = htons(ACK);
|
hdr->th_opcode = htons(ACK);
|
hdr->th_block = htons(tftp_stream.last_good_block);
|
hdr->th_block = htons(tftp_stream.last_good_block);
|
if (__udp_sendto(tftp_stream.data, 4 /* FIXME */,
|
if (__udp_sendto(tftp_stream.data, 4 /* FIXME */,
|
&tftp_stream.from_addr, &tftp_stream.local_addr) < 0) {
|
&tftp_stream.from_addr, &tftp_stream.local_addr) < 0) {
|
// Problem sending ACK
|
// Problem sending ACK
|
*err = TFTP_NETERR;
|
*err = TFTP_NETERR;
|
return -1;
|
return -1;
|
}
|
}
|
}
|
}
|
if ((tftp_stream.actual_len >= 0) && (tftp_stream.actual_len < SEGSIZE)) {
|
if ((tftp_stream.actual_len >= 0) && (tftp_stream.actual_len < SEGSIZE)) {
|
// Out of data
|
// Out of data
|
break;
|
break;
|
}
|
}
|
timeout.tv_sec = TFTP_TIMEOUT_PERIOD;
|
timeout.tv_sec = TFTP_TIMEOUT_PERIOD;
|
timeout.tv_usec = 0;
|
timeout.tv_usec = 0;
|
recv_len = sizeof(tftp_stream.data);
|
recv_len = sizeof(tftp_stream.data);
|
if ((data_len = __udp_recvfrom(&tftp_stream.data[0], recv_len, &tftp_stream.from_addr,
|
if ((data_len = __udp_recvfrom(&tftp_stream.data[0], recv_len, &tftp_stream.from_addr,
|
&tftp_stream.local_addr, &timeout)) < 0) {
|
&tftp_stream.local_addr, &timeout)) < 0) {
|
// No data, try again
|
// No data, try again
|
if ((++tftp_stream.total_timeouts > TFTP_TIMEOUT_MAX) ||
|
if ((++tftp_stream.total_timeouts > TFTP_TIMEOUT_MAX) ||
|
(tftp_stream.last_good_block == 0)) {
|
(tftp_stream.last_good_block == 0)) {
|
// Timeout - no data received
|
// Timeout - no data received
|
*err = TFTP_TIMEOUT;
|
*err = TFTP_TIMEOUT;
|
return -1;
|
return -1;
|
}
|
}
|
} else {
|
} else {
|
if (ntohs(hdr->th_opcode) == DATA) {
|
if (ntohs(hdr->th_opcode) == DATA) {
|
if (ntohs(hdr->th_block) == (tftp_stream.last_good_block+1)) {
|
if (ntohs(hdr->th_block) == (tftp_stream.last_good_block+1)) {
|
// Consume this data
|
// Consume this data
|
data_len -= 4; /* Sizeof TFTP header */
|
data_len -= 4; /* Sizeof TFTP header */
|
tftp_stream.avail = tftp_stream.actual_len = data_len;
|
tftp_stream.avail = tftp_stream.actual_len = data_len;
|
tftp_stream.bufp = hdr->th_data;
|
tftp_stream.bufp = hdr->th_data;
|
tftp_stream.last_good_block++;
|
tftp_stream.last_good_block++;
|
}
|
}
|
} else {
|
} else {
|
if (ntohs(hdr->th_opcode) == ERROR) {
|
if (ntohs(hdr->th_opcode) == ERROR) {
|
*err = ntohs(hdr->th_code);
|
*err = ntohs(hdr->th_code);
|
return -1;
|
return -1;
|
} else {
|
} else {
|
// What kind of packet is this?
|
// What kind of packet is this?
|
*err = TFTP_PROTOCOL;
|
*err = TFTP_PROTOCOL;
|
return -1;
|
return -1;
|
}
|
}
|
}
|
}
|
}
|
}
|
}
|
}
|
}
|
}
|
return total_bytes;
|
return total_bytes;
|
}
|
}
|
|
|
char *
|
char *
|
tftp_error(int err)
|
tftp_error(int err)
|
{
|
{
|
char *errmsg = "Unknown error";
|
char *errmsg = "Unknown error";
|
|
|
switch (err) {
|
switch (err) {
|
case TFTP_ENOTFOUND:
|
case TFTP_ENOTFOUND:
|
return "file not found";
|
return "file not found";
|
case TFTP_EACCESS:
|
case TFTP_EACCESS:
|
return "access violation";
|
return "access violation";
|
case TFTP_ENOSPACE:
|
case TFTP_ENOSPACE:
|
return "disk full or allocation exceeded";
|
return "disk full or allocation exceeded";
|
case TFTP_EBADOP:
|
case TFTP_EBADOP:
|
return "illegal TFTP operation";
|
return "illegal TFTP operation";
|
case TFTP_EBADID:
|
case TFTP_EBADID:
|
return "unknown transfer ID";
|
return "unknown transfer ID";
|
case TFTP_EEXISTS:
|
case TFTP_EEXISTS:
|
return "file already exists";
|
return "file already exists";
|
case TFTP_ENOUSER:
|
case TFTP_ENOUSER:
|
return "no such user";
|
return "no such user";
|
case TFTP_TIMEOUT:
|
case TFTP_TIMEOUT:
|
return "operation timed out";
|
return "operation timed out";
|
case TFTP_NETERR:
|
case TFTP_NETERR:
|
return "some sort of network error";
|
return "some sort of network error";
|
case TFTP_INVALID:
|
case TFTP_INVALID:
|
return "invalid parameter";
|
return "invalid parameter";
|
case TFTP_PROTOCOL:
|
case TFTP_PROTOCOL:
|
return "protocol violation";
|
return "protocol violation";
|
case TFTP_TOOLARGE:
|
case TFTP_TOOLARGE:
|
return "file is larger than buffer";
|
return "file is larger than buffer";
|
}
|
}
|
return errmsg;
|
return errmsg;
|
}
|
}
|
|
|
//
|
//
|
// RedBoot interface
|
// RedBoot interface
|
//
|
//
|
GETC_IO_FUNCS(tftp_io, tftp_stream_open, tftp_stream_close,
|
GETC_IO_FUNCS(tftp_io, tftp_stream_open, tftp_stream_close,
|
0, tftp_stream_read, tftp_error);
|
0, tftp_stream_read, tftp_error);
|
RedBoot_load(tftp, tftp_io, true, true, 0);
|
RedBoot_load(tftp, tftp_io, true, true, 0);
|
|
|
|
|