1
0
mirror of git://projects.qi-hardware.com/openwrt-xburst.git synced 2025-04-21 12:27:27 +03:00

add u-boot sources for danube

git-svn-id: svn://svn.openwrt.org/openwrt/trunk@11108 3c298f89-4303-0410-b956-a3cf2f4a3e73
This commit is contained in:
blogic
2008-05-11 14:13:15 +00:00
parent e43a5d11b5
commit 9a24ac5b8c
82 changed files with 23917 additions and 0 deletions

View File

@@ -0,0 +1,4 @@
#define IFX_ETH_INITIALIZE_EXTERN extern int danube_switch_initialize(bd_t *);
#define IFX_ETH_INITIALIZE(bd_t) danube_switch_initialize(bd_t);

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,778 @@
/*
* NFS support driver - based on etherboot and U-BOOT's tftp.c
*
* Masami Komiya <mkomiya@sonare.it> 2004
*
*/
/* NOTE: the NFS code is heavily inspired by the NetBSD netboot code (read:
* large portions are copied verbatim) as distributed in OSKit 0.97. A few
* changes were necessary to adapt the code to Etherboot and to fix several
* inconsistencies. Also the RPC message preparation is done "by hand" to
* avoid adding netsprintf() which I find hard to understand and use. */
/* NOTE 2: Etherboot does not care about things beyond the kernel image, so
* it loads the kernel image off the boot server (ARP_SERVER) and does not
* access the client root disk (root-path in dhcpd.conf), which would use
* ARP_ROOTSERVER. The root disk is something the operating system we are
* about to load needs to use. This is different from the OSKit 0.97 logic. */
/* NOTE 3: Symlink handling introduced by Anselm M Hoffmeister, 2003-July-14
* If a symlink is encountered, it is followed as far as possible (recursion
* possible, maximum 16 steps). There is no clearing of ".."'s inside the
* path, so please DON'T DO THAT. thx. */
#include <common.h>
#include <command.h>
#include <net.h>
#include <malloc.h>
#include "nfs.h"
#include "bootp.h"
/*#define NFS_DEBUG*/
#if ((CONFIG_COMMANDS & CFG_CMD_NET) && (CONFIG_COMMANDS & CFG_CMD_NFS))
#define HASHES_PER_LINE 65 /* Number of "loading" hashes per line */
#define NFS_TIMEOUT 60
static int fs_mounted = 0;
static unsigned long rpc_id = 0;
static int nfs_offset = -1;
static int nfs_len;
static char dirfh[NFS_FHSIZE]; /* file handle of directory */
static char filefh[NFS_FHSIZE]; /* file handle of kernel image */
static int NfsDownloadState;
static IPaddr_t NfsServerIP;
static int NfsSrvMountPort;
static int NfsSrvNfsPort;
static int NfsOurPort;
static int NfsTimeoutCount;
static int NfsState;
#define STATE_PRCLOOKUP_PROG_MOUNT_REQ 1
#define STATE_PRCLOOKUP_PROG_NFS_REQ 2
#define STATE_MOUNT_REQ 3
#define STATE_UMOUNT_REQ 4
#define STATE_LOOKUP_REQ 5
#define STATE_READ_REQ 6
#define STATE_READLINK_REQ 7
static char default_filename[64];
static char *nfs_filename;
static char *nfs_path;
static char nfs_path_buff[2048];
static __inline__ int
store_block (uchar * src, unsigned offset, unsigned len)
{
ulong newsize = offset + len;
#ifdef CFG_DIRECT_FLASH_NFS
int i, rc = 0;
for (i=0; i<CFG_MAX_FLASH_BANKS; i++) {
/* start address in flash? */
if (load_addr + offset >= flash_info[i].start[0]) {
rc = 1;
break;
}
}
if (rc) { /* Flash is destination for this packet */
rc = flash_write ((uchar *)src, (ulong)(load_addr+offset), len);
if (rc) {
flash_perror (rc);
return -1;
}
} else
#endif /* CFG_DIRECT_FLASH_NFS */
{
(void)memcpy ((void *)(load_addr + offset), src, len);
}
if (NetBootFileXferSize < (offset+len))
NetBootFileXferSize = newsize;
return 0;
}
static char*
basename (char *path)
{
char *fname;
fname = path + strlen(path) - 1;
while (fname >= path) {
if (*fname == '/') {
fname++;
break;
}
fname--;
}
return fname;
}
static char*
dirname (char *path)
{
char *fname;
fname = basename (path);
--fname;
*fname = '\0';
return path;
}
/**************************************************************************
RPC_ADD_CREDENTIALS - Add RPC authentication/verifier entries
**************************************************************************/
static long *rpc_add_credentials (long *p)
{
int hl;
int hostnamelen;
char hostname[256];
strcpy (hostname, "");
hostnamelen=strlen (hostname);
/* Here's the executive summary on authentication requirements of the
* various NFS server implementations: Linux accepts both AUTH_NONE
* and AUTH_UNIX authentication (also accepts an empty hostname field
* in the AUTH_UNIX scheme). *BSD refuses AUTH_NONE, but accepts
* AUTH_UNIX (also accepts an empty hostname field in the AUTH_UNIX
* scheme). To be safe, use AUTH_UNIX and pass the hostname if we have
* it (if the BOOTP/DHCP reply didn't give one, just use an empty
* hostname). */
hl = (hostnamelen + 3) & ~3;
/* Provide an AUTH_UNIX credential. */
*p++ = htonl(1); /* AUTH_UNIX */
*p++ = htonl(hl+20); /* auth length */
*p++ = htonl(0); /* stamp */
*p++ = htonl(hostnamelen); /* hostname string */
if (hostnamelen & 3) {
*(p + hostnamelen / 4) = 0; /* add zero padding */
}
memcpy (p, hostname, hostnamelen);
p += hl / 4;
*p++ = 0; /* uid */
*p++ = 0; /* gid */
*p++ = 0; /* auxiliary gid list */
/* Provide an AUTH_NONE verifier. */
*p++ = 0; /* AUTH_NONE */
*p++ = 0; /* auth length */
return p;
}
/**************************************************************************
RPC_LOOKUP - Lookup RPC Port numbers
**************************************************************************/
static void
rpc_req (int rpc_prog, int rpc_proc, uint32_t *data, int datalen)
{
struct rpc_t pkt;
unsigned long id;
uint32_t *p;
int pktlen;
int sport;
id = ++rpc_id;
pkt.u.call.id = htonl(id);
pkt.u.call.type = htonl(MSG_CALL);
pkt.u.call.rpcvers = htonl(2); /* use RPC version 2 */
pkt.u.call.prog = htonl(rpc_prog);
pkt.u.call.vers = htonl(2); /* portmapper is version 2 */
pkt.u.call.proc = htonl(rpc_proc);
p = (uint32_t *)&(pkt.u.call.data);
if (datalen)
memcpy ((char *)p, (char *)data, datalen*sizeof(uint32_t));
pktlen = (char *)p + datalen*sizeof(uint32_t) - (char *)&pkt;
memcpy ((char *)NetTxPacket + NetEthHdrSize() + IP_HDR_SIZE, (char *)&pkt, pktlen);
if (rpc_prog == PROG_PORTMAP)
sport = SUNRPC_PORT;
else if (rpc_prog == PROG_MOUNT)
sport = NfsSrvMountPort;
else
sport = NfsSrvNfsPort;
NetSendUDPPacket (NetServerEther, NfsServerIP, sport, NfsOurPort, pktlen);
}
/**************************************************************************
RPC_LOOKUP - Lookup RPC Port numbers
**************************************************************************/
static void
rpc_lookup_req (int prog, int ver)
{
uint32_t data[16];
data[0] = 0; data[1] = 0; /* auth credential */
data[2] = 0; data[3] = 0; /* auth verifier */
data[4] = htonl(prog);
data[5] = htonl(ver);
data[6] = htonl(17); /* IP_UDP */
data[7] = 0;
rpc_req (PROG_PORTMAP, PORTMAP_GETPORT, data, 8);
}
/**************************************************************************
NFS_MOUNT - Mount an NFS Filesystem
**************************************************************************/
static void
nfs_mount_req (char *path)
{
uint32_t data[1024];
uint32_t *p;
int len;
int pathlen;
pathlen = strlen (path);
p = &(data[0]);
p = (uint32_t *)rpc_add_credentials((long *)p);
*p++ = htonl(pathlen);
if (pathlen & 3) *(p + pathlen / 4) = 0;
memcpy (p, path, pathlen);
p += (pathlen + 3) / 4;
len = (uint32_t *)p - (uint32_t *)&(data[0]);
rpc_req (PROG_MOUNT, MOUNT_ADDENTRY, data, len);
}
/**************************************************************************
NFS_UMOUNTALL - Unmount all our NFS Filesystems on the Server
**************************************************************************/
static void
nfs_umountall_req (void)
{
uint32_t data[1024];
uint32_t *p;
int len;
if ((NfsSrvMountPort == -1) || (!fs_mounted)) {
/* Nothing mounted, nothing to umount */
return;
}
p = &(data[0]);
p = (uint32_t *)rpc_add_credentials ((long *)p);
len = (uint32_t *)p - (uint32_t *)&(data[0]);
rpc_req (PROG_MOUNT, MOUNT_UMOUNTALL, data, len);
}
/***************************************************************************
* NFS_READLINK (AH 2003-07-14)
* This procedure is called when read of the first block fails -
* this probably happens when it's a directory or a symlink
* In case of successful readlink(), the dirname is manipulated,
* so that inside the nfs() function a recursion can be done.
**************************************************************************/
static void
nfs_readlink_req (void)
{
uint32_t data[1024];
uint32_t *p;
int len;
p = &(data[0]);
p = (uint32_t *)rpc_add_credentials ((long *)p);
memcpy (p, filefh, NFS_FHSIZE);
p += (NFS_FHSIZE / 4);
len = (uint32_t *)p - (uint32_t *)&(data[0]);
rpc_req (PROG_NFS, NFS_READLINK, data, len);
}
/**************************************************************************
NFS_LOOKUP - Lookup Pathname
**************************************************************************/
static void
nfs_lookup_req (char *fname)
{
uint32_t data[1024];
uint32_t *p;
int len;
int fnamelen;
fnamelen = strlen (fname);
p = &(data[0]);
p = (uint32_t *)rpc_add_credentials ((long *)p);
memcpy (p, dirfh, NFS_FHSIZE);
p += (NFS_FHSIZE / 4);
*p++ = htonl(fnamelen);
if (fnamelen & 3) *(p + fnamelen / 4) = 0;
memcpy (p, fname, fnamelen);
p += (fnamelen + 3) / 4;
len = (uint32_t *)p - (uint32_t *)&(data[0]);
rpc_req (PROG_NFS, NFS_LOOKUP, data, len);
}
/**************************************************************************
NFS_READ - Read File on NFS Server
**************************************************************************/
static void
nfs_read_req (int offset, int readlen)
{
uint32_t data[1024];
uint32_t *p;
int len;
p = &(data[0]);
p = (uint32_t *)rpc_add_credentials ((long *)p);
memcpy (p, filefh, NFS_FHSIZE);
p += (NFS_FHSIZE / 4);
*p++ = htonl(offset);
*p++ = htonl(readlen);
*p++ = 0;
len = (uint32_t *)p - (uint32_t *)&(data[0]);
rpc_req (PROG_NFS, NFS_READ, data, len);
}
/**************************************************************************
RPC request dispatcher
**************************************************************************/
static void
NfsSend (void)
{
#ifdef NFS_DEBUG
printf ("%s\n", __FUNCTION__);
#endif
switch (NfsState) {
case STATE_PRCLOOKUP_PROG_MOUNT_REQ:
rpc_lookup_req (PROG_MOUNT, 1);
break;
case STATE_PRCLOOKUP_PROG_NFS_REQ:
rpc_lookup_req (PROG_NFS, 2);
break;
case STATE_MOUNT_REQ:
nfs_mount_req (nfs_path);
break;
case STATE_UMOUNT_REQ:
nfs_umountall_req ();
break;
case STATE_LOOKUP_REQ:
nfs_lookup_req (nfs_filename);
break;
case STATE_READ_REQ:
nfs_read_req (nfs_offset, nfs_len);
break;
case STATE_READLINK_REQ:
nfs_readlink_req ();
break;
}
}
/**************************************************************************
Handlers for the reply from server
**************************************************************************/
static int
rpc_lookup_reply (int prog, uchar *pkt, unsigned len)
{
struct rpc_t rpc_pkt;
memcpy ((unsigned char *)&rpc_pkt, pkt, len);
#ifdef NFS_DEBUG
printf ("%s\n", __FUNCTION__);
#endif
if (ntohl(rpc_pkt.u.reply.id) != rpc_id)
return -1;
if (rpc_pkt.u.reply.rstatus ||
rpc_pkt.u.reply.verifier ||
rpc_pkt.u.reply.astatus ||
rpc_pkt.u.reply.astatus) {
return -1;
}
switch (prog) {
case PROG_MOUNT:
NfsSrvMountPort = ntohl(rpc_pkt.u.reply.data[0]);
break;
case PROG_NFS:
NfsSrvNfsPort = ntohl(rpc_pkt.u.reply.data[0]);
break;
}
return 0;
}
static int
nfs_mount_reply (uchar *pkt, unsigned len)
{
struct rpc_t rpc_pkt;
#ifdef NFS_DEBUG
printf ("%s\n", __FUNCTION__);
#endif
memcpy ((unsigned char *)&rpc_pkt, pkt, len);
if (ntohl(rpc_pkt.u.reply.id) != rpc_id)
return -1;
if (rpc_pkt.u.reply.rstatus ||
rpc_pkt.u.reply.verifier ||
rpc_pkt.u.reply.astatus ||
rpc_pkt.u.reply.data[0]) {
return -1;
}
fs_mounted = 1;
memcpy (dirfh, rpc_pkt.u.reply.data + 1, NFS_FHSIZE);
return 0;
}
static int
nfs_umountall_reply (uchar *pkt, unsigned len)
{
struct rpc_t rpc_pkt;
#ifdef NFS_DEBUG
printf ("%s\n", __FUNCTION__);
#endif
memcpy ((unsigned char *)&rpc_pkt, pkt, len);
if (ntohl(rpc_pkt.u.reply.id) != rpc_id)
return -1;
if (rpc_pkt.u.reply.rstatus ||
rpc_pkt.u.reply.verifier ||
rpc_pkt.u.reply.astatus) {
return -1;
}
fs_mounted = 0;
memset (dirfh, 0, sizeof(dirfh));
return 0;
}
static int
nfs_lookup_reply (uchar *pkt, unsigned len)
{
struct rpc_t rpc_pkt;
#ifdef NFS_DEBUG
printf ("%s\n", __FUNCTION__);
#endif
memcpy ((unsigned char *)&rpc_pkt, pkt, len);
if (ntohl(rpc_pkt.u.reply.id) != rpc_id)
return -1;
if (rpc_pkt.u.reply.rstatus ||
rpc_pkt.u.reply.verifier ||
rpc_pkt.u.reply.astatus ||
rpc_pkt.u.reply.data[0]) {
return -1;
}
memcpy (filefh, rpc_pkt.u.reply.data + 1, NFS_FHSIZE);
return 0;
}
static int
nfs_readlink_reply (uchar *pkt, unsigned len)
{
struct rpc_t rpc_pkt;
int rlen;
#ifdef NFS_DEBUG
printf ("%s\n", __FUNCTION__);
#endif
memcpy ((unsigned char *)&rpc_pkt, pkt, len);
if (ntohl(rpc_pkt.u.reply.id) != rpc_id)
return -1;
if (rpc_pkt.u.reply.rstatus ||
rpc_pkt.u.reply.verifier ||
rpc_pkt.u.reply.astatus ||
rpc_pkt.u.reply.data[0]) {
return -1;
}
rlen = ntohl (rpc_pkt.u.reply.data[1]); /* new path length */
if (*((char *)&(rpc_pkt.u.reply.data[2])) != '/') {
int pathlen;
strcat (nfs_path, "/");
pathlen = strlen(nfs_path);
memcpy (nfs_path+pathlen, (uchar *)&(rpc_pkt.u.reply.data[2]), rlen);
nfs_path[pathlen+rlen+1] = 0;
} else {
memcpy (nfs_path, (uchar *)&(rpc_pkt.u.reply.data[2]), rlen);
nfs_path[rlen] = 0;
}
return 0;
}
static int
nfs_read_reply (uchar *pkt, unsigned len)
{
struct rpc_t rpc_pkt;
int rlen;
#ifdef NFS_DEBUG_nop
printf ("%s\n", __FUNCTION__);
#endif
memcpy ((uchar *)&rpc_pkt, pkt, sizeof(rpc_pkt.u.reply));
if (ntohl(rpc_pkt.u.reply.id) != rpc_id)
return -1;
if (rpc_pkt.u.reply.rstatus ||
rpc_pkt.u.reply.verifier ||
rpc_pkt.u.reply.astatus ||
rpc_pkt.u.reply.data[0]) {
if (rpc_pkt.u.reply.rstatus) {
return -9999;
}
if (rpc_pkt.u.reply.astatus) {
return -9999;
}
return -ntohl(rpc_pkt.u.reply.data[0]);;
}
if ((nfs_offset!=0) && !((nfs_offset) % (NFS_READ_SIZE/2*10*HASHES_PER_LINE))) {
puts ("\n\t ");
}
if (!(nfs_offset % ((NFS_READ_SIZE/2)*10))) {
putc ('#');
}
rlen = ntohl(rpc_pkt.u.reply.data[18]);
if ( store_block ((uchar *)pkt+sizeof(rpc_pkt.u.reply), nfs_offset, rlen) )
return -9999;
return rlen;
}
/**************************************************************************
Interfaces of U-BOOT
**************************************************************************/
static void
NfsTimeout (void)
{
puts ("Timeout\n");
NetState = NETLOOP_FAIL;
return;
}
static void
NfsHandler (uchar *pkt, unsigned dest, unsigned src, unsigned len)
{
int rlen;
#ifdef NFS_DEBUG
printf ("%s\n", __FUNCTION__);
#endif
if (dest != NfsOurPort) return;
switch (NfsState) {
case STATE_PRCLOOKUP_PROG_MOUNT_REQ:
rpc_lookup_reply (PROG_MOUNT, pkt, len);
NfsState = STATE_PRCLOOKUP_PROG_NFS_REQ;
NfsSend ();
break;
case STATE_PRCLOOKUP_PROG_NFS_REQ:
rpc_lookup_reply (PROG_NFS, pkt, len);
NfsState = STATE_MOUNT_REQ;
NfsSend ();
break;
case STATE_MOUNT_REQ:
if (nfs_mount_reply(pkt, len)) {
puts ("*** ERROR: Cannot mount\n");
/* just to be sure... */
NfsState = STATE_UMOUNT_REQ;
NfsSend ();
} else {
NfsState = STATE_LOOKUP_REQ;
NfsSend ();
}
break;
case STATE_UMOUNT_REQ:
if (nfs_umountall_reply(pkt, len)) {
puts ("*** ERROR: Cannot umount\n");
NetState = NETLOOP_FAIL;
} else {
puts ("\ndone\n");
NetState = NfsDownloadState;
}
break;
case STATE_LOOKUP_REQ:
if (nfs_lookup_reply(pkt, len)) {
puts ("*** ERROR: File lookup fail\n");
NfsState = STATE_UMOUNT_REQ;
NfsSend ();
} else {
NfsState = STATE_READ_REQ;
nfs_offset = 0;
nfs_len = NFS_READ_SIZE;
NfsSend ();
}
break;
case STATE_READLINK_REQ:
if (nfs_readlink_reply(pkt, len)) {
puts ("*** ERROR: Symlink fail\n");
NfsState = STATE_UMOUNT_REQ;
NfsSend ();
} else {
#ifdef NFS_DEBUG
printf ("Symlink --> %s\n", nfs_path);
#endif
nfs_filename = basename (nfs_path);
nfs_path = dirname (nfs_path);
NfsState = STATE_MOUNT_REQ;
NfsSend ();
}
break;
case STATE_READ_REQ:
rlen = nfs_read_reply (pkt, len);
NetSetTimeout (NFS_TIMEOUT * CFG_HZ, NfsTimeout);
if (rlen > 0) {
nfs_offset += rlen;
NfsSend ();
}
else if ((rlen == -NFSERR_ISDIR)||(rlen == -NFSERR_INVAL)) {
/* symbolic link */
NfsState = STATE_READLINK_REQ;
NfsSend ();
} else {
if ( ! rlen ) NfsDownloadState = NETLOOP_SUCCESS;
NfsState = STATE_UMOUNT_REQ;
NfsSend ();
}
break;
}
}
void
NfsStart (void)
{
#ifdef NFS_DEBUG
printf ("%s\n", __FUNCTION__);
#endif
NfsDownloadState = NETLOOP_FAIL;
NfsServerIP = NetServerIP;
nfs_path = (char *)nfs_path_buff;
if (nfs_path == NULL) {
NetState = NETLOOP_FAIL;
puts ("*** ERROR: Fail allocate memory\n");
return;
}
if (BootFile[0] == '\0') {
sprintf (default_filename, "/nfsroot/%02lX%02lX%02lX%02lX.img",
NetOurIP & 0xFF,
(NetOurIP >> 8) & 0xFF,
(NetOurIP >> 16) & 0xFF,
(NetOurIP >> 24) & 0xFF );
strcpy (nfs_path, default_filename);
printf ("*** Warning: no boot file name; using '%s'\n",
nfs_path);
} else {
char *p=BootFile;
p = strchr (p, ':');
if (p != NULL) {
NfsServerIP = string_to_ip (BootFile);
++p;
strcpy (nfs_path, p);
} else {
strcpy (nfs_path, BootFile);
}
}
nfs_filename = basename (nfs_path);
nfs_path = dirname (nfs_path);
#if defined(CONFIG_NET_MULTI)
printf ("Using %s device\n", eth_get_name());
#endif
puts ("File transfer via NFS from server "); print_IPaddr (NfsServerIP);
puts ("; our IP address is "); print_IPaddr (NetOurIP);
/* Check if we need to send across this subnet */
if (NetOurGatewayIP && NetOurSubnetMask) {
IPaddr_t OurNet = NetOurIP & NetOurSubnetMask;
IPaddr_t ServerNet = NetServerIP & NetOurSubnetMask;
if (OurNet != ServerNet) {
puts ("; sending through gateway ");
print_IPaddr (NetOurGatewayIP) ;
}
}
printf ("\nFilename '%s/%s'.", nfs_path, nfs_filename);
if (NetBootFileSize) {
printf (" Size is 0x%x Bytes = ", NetBootFileSize<<9);
print_size (NetBootFileSize<<9, "");
}
printf ("\nLoad address: 0x%lx\n"
"Loading: *\b", load_addr);
NetSetTimeout (NFS_TIMEOUT * CFG_HZ, NfsTimeout);
NetSetHandler (NfsHandler);
NfsTimeoutCount = 0;
NfsState = STATE_PRCLOOKUP_PROG_MOUNT_REQ;
/*NfsOurPort = 4096 + (get_ticks() % 3072);*/
/*FIX ME !!!*/
NfsOurPort = 1000;
/* zero out server ether in case the server ip has changed */
memset (NetServerEther, 0, 6);
NfsSend ();
}
#endif /* CONFIG_COMMANDS & CFG_CMD_NFS */

View File

@@ -0,0 +1,389 @@
/*
* 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>
#include "tftp.h"
#include "bootp.h"
#undef ET_DEBUG
#if (CONFIG_COMMANDS & CFG_CMD_NET)
#define WELL_KNOWN_PORT 69 /* Well known TFTP port # */
#define TIMEOUT 5 /* Seconds to timeout for a lost pkt */
#ifndef CONFIG_NET_RETRY_COUNT
# define TIMEOUT_COUNT 10 /* # of timeouts before giving up */
#else
# define TIMEOUT_COUNT (CONFIG_NET_RETRY_COUNT * 2)
#endif
/* (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
#define TFTP_OACK 6
static int TftpServerPort; /* The UDP port at their end */
static int TftpOurPort; /* The UDP port at our end */
static int TftpTimeoutCount;
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 */
static int TftpState;
#define STATE_RRQ 1
#define STATE_DATA 2
#define STATE_TOO_LARGE 3
#define STATE_BAD_MAGIC 4
#define STATE_OACK 5
#define TFTP_BLOCK_SIZE 512 /* default TFTP block size */
#define TFTP_SEQUENCE_SIZE ((ulong)(1<<16)) /* sequence number is 16 bit */
#define DEFAULT_NAME_LEN (8 + 4 + 1)
static char default_filename[DEFAULT_NAME_LEN];
static char *tftp_filename;
#ifdef CFG_DIRECT_FLASH_TFTP
extern flash_info_t flash_info[];
#endif
static __inline__ void
store_block (unsigned block, uchar * src, unsigned len)
{
ulong offset = block * TFTP_BLOCK_SIZE + TftpBlockWrapOffset;
ulong newsize = offset + len;
#ifdef CFG_DIRECT_FLASH_TFTP
int i, rc = 0;
for (i=0; i<CFG_MAX_FLASH_BANKS; i++) {
/* start address in flash? */
if (load_addr + offset >= flash_info[i].start[0]) {
rc = 1;
break;
}
}
if (rc) { /* Flash is destination for this packet */
rc = flash_write ((char *)src, (ulong)(load_addr+offset), len);
if (rc) {
flash_perror (rc);
NetState = NETLOOP_FAIL;
return;
}
}
else
#endif /* CFG_DIRECT_FLASH_TFTP */
{
(void)memcpy((void *)(load_addr + offset), src, len);
}
if (NetBootFileXferSize < newsize)
NetBootFileXferSize = newsize;
}
static void TftpSend (void);
static void TftpTimeout (void);
/**********************************************************************/
static void
TftpSend (void)
{
volatile uchar * pkt;
volatile uchar * xp;
int len = 0;
volatile ushort *s;
/*
* We will always be sending some sort of packet, so
* cobble together the packet headers now.
*/
pkt = NetTxPacket + NetEthHdrSize() + IP_HDR_SIZE;
switch (TftpState) {
case STATE_RRQ:
xp = pkt;
s = (ushort *)pkt;
*s++ = htons(TFTP_RRQ);
pkt = (uchar *)s;
strcpy ((char *)pkt, tftp_filename);
pkt += strlen(tftp_filename) + 1;
strcpy ((char *)pkt, "octet");
pkt += 5 /*strlen("octet")*/ + 1;
strcpy ((char *)pkt, "timeout");
pkt += 7 /*strlen("timeout")*/ + 1;
sprintf((char *)pkt, "%d", TIMEOUT);
#ifdef ET_DEBUG
printf("send option \"timeout %s\"\n", (char *)pkt);
#endif
pkt += strlen((char *)pkt) + 1;
len = pkt - xp;
break;
case STATE_DATA:
case STATE_OACK:
xp = pkt;
s = (ushort *)pkt;
*s++ = htons(TFTP_ACK);
*s++ = htons(TftpBlock);
pkt = (uchar *)s;
len = pkt - xp;
break;
case STATE_TOO_LARGE:
xp = pkt;
s = (ushort *)pkt;
*s++ = htons(TFTP_ERROR);
*s++ = htons(3);
pkt = (uchar *)s;
strcpy ((char *)pkt, "File too large");
pkt += 14 /*strlen("File too large")*/ + 1;
len = pkt - xp;
break;
case STATE_BAD_MAGIC:
xp = pkt;
s = (ushort *)pkt;
*s++ = htons(TFTP_ERROR);
*s++ = htons(2);
pkt = (uchar *)s;
strcpy ((char *)pkt, "File has bad magic");
pkt += 18 /*strlen("File has bad magic")*/ + 1;
len = pkt - xp;
break;
}
NetSendUDPPacket(NetServerEther, NetServerIP, TftpServerPort, TftpOurPort, len);
}
static void
TftpHandler (uchar * pkt, unsigned dest, unsigned src, unsigned len)
{
ushort proto;
ushort *s;
if (dest != TftpOurPort) {
return;
}
if (TftpState != STATE_RRQ && src != TftpServerPort) {
return;
}
if (len < 2) {
return;
}
len -= 2;
/* warning: don't use increment (++) in ntohs() macros!! */
s = (ushort *)pkt;
proto = *s++;
pkt = (uchar *)s;
switch (ntohs(proto)) {
case TFTP_RRQ:
case TFTP_WRQ:
case TFTP_ACK:
break;
default:
break;
case TFTP_OACK:
#ifdef ET_DEBUG
printf("Got OACK: %s %s\n", pkt, pkt+strlen(pkt)+1);
#endif
TftpState = STATE_OACK;
TftpServerPort = src;
TftpSend (); /* Send ACK */
break;
case TFTP_DATA:
if (len < 2)
return;
len -= 2;
TftpBlock = ntohs(*(ushort *)pkt);
/*
* 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.
*/
if (TftpBlock == 0) {
TftpBlockWrap++;
TftpBlockWrapOffset += TFTP_BLOCK_SIZE * TFTP_SEQUENCE_SIZE;
printf ("\n\t %lu MB received\n\t ", TftpBlockWrapOffset>>20);
} else {
if (((TftpBlock - 1) % 10) == 0) {
putc ('#');
} else if ((TftpBlock % (10 * HASHES_PER_LINE)) == 0) {
puts ("\n\t ");
}
}
#ifdef ET_DEBUG
if (TftpState == STATE_RRQ) {
puts ("Server did not acknowledge timeout option!\n");
}
#endif
if (TftpState == STATE_RRQ || TftpState == STATE_OACK) {
/* first block received */
TftpState = STATE_DATA;
TftpServerPort = src;
TftpLastBlock = 0;
TftpBlockWrap = 0;
TftpBlockWrapOffset = 0;
if (TftpBlock != 1) { /* Assertion */
printf ("\nTFTP error: "
"First block is not block 1 (%ld)\n"
"Starting again\n\n",
TftpBlock);
NetStartAgain ();
break;
}
}
if (TftpBlock == TftpLastBlock) {
/*
* Same block again; ignore it.
*/
break;
}
TftpLastBlock = TftpBlock;
NetSetTimeout (TIMEOUT * CFG_HZ, TftpTimeout);
store_block (TftpBlock - 1, pkt + 2, len);
/*
* Acknoledge the block just received, which will prompt
* the server for the next one.
*/
TftpSend ();
if (len < TFTP_BLOCK_SIZE) {
/*
* We received the whole thing. Try to
* run it.
*/
puts ("\ndone\n");
NetState = NETLOOP_SUCCESS;
}
break;
case TFTP_ERROR:
printf ("\nTFTP error: '%s' (%d)\n",
pkt + 2, ntohs(*(ushort *)pkt));
puts ("Starting again\n\n");
NetStartAgain ();
break;
}
}
static void
TftpTimeout (void)
{
if (++TftpTimeoutCount > TIMEOUT_COUNT) {
puts ("\nRetry count exceeded; starting again\n");
NetStartAgain ();
} else {
puts ("T ");
NetSetTimeout (TIMEOUT * CFG_HZ, TftpTimeout);
TftpSend ();
}
}
void
TftpStart (void)
{
#ifdef CONFIG_TFTP_PORT
char *ep; /* Environment pointer */
#endif
if (BootFile[0] == '\0') {
sprintf(default_filename, "%02lX%02lX%02lX%02lX.img",
NetOurIP & 0xFF,
(NetOurIP >> 8) & 0xFF,
(NetOurIP >> 16) & 0xFF,
(NetOurIP >> 24) & 0xFF );
tftp_filename = default_filename;
printf ("*** Warning: no boot file name; using '%s'\n",
tftp_filename);
} else {
tftp_filename = BootFile;
}
#if defined(CONFIG_NET_MULTI)
printf ("Using %s device\n", eth_get_name());
#endif
puts ("TFTP from server "); print_IPaddr (NetServerIP);
puts ("; our IP address is "); print_IPaddr (NetOurIP);
/* Check if we need to send across this subnet */
if (NetOurGatewayIP && NetOurSubnetMask) {
IPaddr_t OurNet = NetOurIP & NetOurSubnetMask;
IPaddr_t ServerNet = NetServerIP & NetOurSubnetMask;
if (OurNet != ServerNet) {
puts ("; sending through gateway ");
print_IPaddr (NetOurGatewayIP) ;
}
}
putc ('\n');
printf ("Filename '%s'.", tftp_filename);
if (NetBootFileSize) {
printf (" Size is 0x%x Bytes = ", NetBootFileSize<<9);
print_size (NetBootFileSize<<9, "");
}
putc ('\n');
printf ("Load address: 0x%lx\n", load_addr);
puts ("Loading: *\b");
NetSetTimeout (TIMEOUT * CFG_HZ, TftpTimeout);
NetSetHandler (TftpHandler);
TftpServerPort = WELL_KNOWN_PORT;
TftpTimeoutCount = 0;
TftpState = STATE_RRQ;
/* Use a pseudo-random port unless a specific port is set */
TftpOurPort = 1024 + (get_timer(0) % 3072);
#ifdef CONFIG_TFTP_PORT
if ((ep = getenv("tftpdstp")) != NULL) {
TftpServerPort = simple_strtol(ep, NULL, 10);
}
if ((ep = getenv("tftpsrcp")) != NULL) {
TftpOurPort= simple_strtol(ep, NULL, 10);
}
#endif
TftpBlock = 0;
/* zero out server ether in case the server ip has changed */
memset(NetServerEther, 0, 6);
TftpSend ();
}
#endif /* CFG_CMD_NET */