OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [bootloaders/] [orpmon/] [services/] [tftp.c] - Diff between revs 2 and 140

Go to most recent revision | Show entire file | Details | Blame | View Log

Rev 2 Rev 140
Line 8... Line 8...
#include "support.h"
#include "support.h"
#include "net.h"
#include "net.h"
#include "tftp.h"
#include "tftp.h"
#include "bootp.h"
#include "bootp.h"
 
 
#undef  ET_DEBUG
//#undef        ET_DEBUG
 
#define ET_DEBUG
 
 
#define WELL_KNOWN_PORT 69              /* Well known TFTP port #               */
#define WELL_KNOWN_PORT 69              /* Well known TFTP port #               */
#define TIMEOUT         2               /* Seconds to timeout for a lost pkt    */
#define TIMEOUT         2               /* Seconds to timeout for a lost pkt    */
#define TIMEOUT_COUNT   10              /* # of timeouts before giving up       */
#define TIMEOUT_COUNT   10              /* # of timeouts before giving up       */
                                        /* (for checking the image size)        */
                                        /* (for checking the image size)        */
Line 39... Line 40...
#define STATE_TOO_LARGE 3
#define STATE_TOO_LARGE 3
#define STATE_BAD_MAGIC 4
#define STATE_BAD_MAGIC 4
 
 
char *tftp_filename;
char *tftp_filename;
 
 
 
 
 
// running TFTP CRC value
 
unsigned long TFTP_CHKSUM;
 
 
#ifdef CFG_DIRECT_FLASH_TFTP
#ifdef CFG_DIRECT_FLASH_TFTP
extern flash_info_t flash_info[CFG_MAX_FLASH_BANKS];
extern flash_info_t flash_info[CFG_MAX_FLASH_BANKS];
#endif
#endif
 
 
static __inline__ void
static __inline__ void
Line 84... Line 89...
                        return;
                        return;
                }
                }
        }
        }
        else
        else
#endif /* CFG_DIRECT_FLASH_TFTP */
#endif /* CFG_DIRECT_FLASH_TFTP */
 
 
 
//#define QUICK_ETHPACKET_COPY    
 
#ifdef QUICK_ETHPACKET_COPY
 
          {
 
            unsigned char * dst = (unsigned char*)(global.src_addr + offset);
 
            //printf("quick ethpacket copy: src: 0x%x dst: 0x%x, len: %d\n",(unsigned long)src, (unsigned long)dst, len);
 
            // First, align the destination address so we can copy words
 
            // If either src or dst are not word aligned, then we will never
 
            // be able to do word copies
 
            while((len) && ((((unsigned long)dst) & 0x3) || (((unsigned long)src) & 0x3)))
 
              {
 
                //              printf("bc: src: 0x%x dst: 0x%x, len: %d\n",(unsigned long)src, (unsigned long)dst, len);
 
                dst[0] = src[0];
 
                len--;
 
                dst++;
 
                src++;
 
 
 
              }
 
            unsigned long *wdst, *wsrc;
 
            wdst = (unsigned long*) dst;
 
            wsrc = (unsigned long*) src;
 
            while(len >= 4)
 
              {
 
                //printf("wc: src: 0x%x dst: 0x%x, len: %d\n",(unsigned long)src, (unsigned long)dst, len);
 
                wdst[0] = wsrc[0];
 
                wdst++; wsrc++;
 
                len -= 4;
 
                dst+=4;
 
                src+=4;
 
 
 
              }
 
            while (len)
 
              {
 
                //printf("cu: src: 0x%x dst: 0x%x, len: %d\n",(unsigned long)src, (unsigned long)dst, len);
 
                dst[0] = src[0];
 
                len--;
 
                dst++;
 
                src++;
 
              }
 
          }
 
#else
 
 
 
#ifdef TFTP_CALC_CRC
 
        // Call special memcpy that calculates CRC for us:
 
        TFTP_CHKSUM += memcpy_crc((void *)(global.src_addr + offset), src, len);
 
#else
 
        // Standard memcpy:
        (void)memcpy((void *)(global.src_addr + offset), src, len);
        (void)memcpy((void *)(global.src_addr + offset), src, len);
 
#endif
 
 
 
 
 
#endif    
 
 
        if (NetBootFileXferSize < newsize)
        if (NetBootFileXferSize < newsize)
                NetBootFileXferSize = newsize;
                NetBootFileXferSize = newsize;
}
}
 
 
Line 102... Line 158...
{
{
        volatile unsigned char *        pkt;
        volatile unsigned char *        pkt;
        volatile unsigned char *        xp;
        volatile unsigned char *        xp;
        int                     len = 0;
        int                     len = 0;
 
 
 
#ifdef ET_DEBUG
 
        //printf("TftpSend: %d\n", TftpState);
 
#endif
 
 
 
 
        /*
        /*
         *      We will always be sending some sort of packet, so
         *      We will always be sending some sort of packet, so
         *      cobble together the packet headers now.
         *      cobble together the packet headers now.
         */
         */
        pkt = NetTxPacket + ETHER_HDR_SIZE + IP_HDR_SIZE;
        pkt = NetTxPacket + ETHER_HDR_SIZE + IP_HDR_SIZE;
 
 
        switch (TftpState) {
        switch (TftpState) {
 
 
        case STATE_RRQ:
        case STATE_RRQ:
                xp = pkt;
                xp = pkt;
                *((unsigned short *)pkt)++ = SWAP16c(TFTP_RRQ);
                (*((unsigned short *)pkt)) = SWAP16c(TFTP_RRQ);
 
                pkt += sizeof(short);
                strcpy ((char *)pkt, tftp_filename);
                strcpy ((char *)pkt, tftp_filename);
                pkt += strlen(tftp_filename) + 1;
                pkt += strlen(tftp_filename) + 1;
                strcpy ((char *)pkt, "octet");
                strcpy ((char *)pkt, "octet");
                pkt += 5 /*strlen("octet")*/ + 1;
                pkt += 5 /*strlen("octet")*/ + 1;
                len = pkt - xp;
                len = pkt - xp;
 
 
                break;
                break;
 
 
        case STATE_DATA:
        case STATE_DATA:
                xp = pkt;
                xp = pkt;
                *((unsigned short *)pkt)++ = SWAP16c(TFTP_ACK);
                *((unsigned short *)pkt) = SWAP16c(TFTP_ACK);
                *((unsigned short *)pkt)++ = SWAP16(TftpBlock);
                pkt += sizeof(short);
 
                *((unsigned short *)pkt) = SWAP16(TftpBlock);
 
                pkt += sizeof(short);
                len = pkt - xp;
                len = pkt - xp;
                break;
                break;
 
 
        case STATE_TOO_LARGE:
        case STATE_TOO_LARGE:
                xp = pkt;
                xp = pkt;
                *((unsigned short *)pkt)++ = SWAP16c(TFTP_ERROR);
                *((unsigned short *)pkt) = SWAP16c(TFTP_ERROR);
                *((unsigned short *)pkt)++ = SWAP16(3);
                pkt += sizeof(short);
 
                *((unsigned short *)pkt) = SWAP16(3);
 
                pkt += sizeof(short);
                strcpy ((char *)pkt, "File too large");
                strcpy ((char *)pkt, "File too large");
                pkt += 14 /*strlen("File too large")*/ + 1;
                pkt += 14 /*strlen("File too large")*/ + 1;
                len = pkt - xp;
                len = pkt - xp;
                break;
                break;
 
 
        case STATE_BAD_MAGIC:
        case STATE_BAD_MAGIC:
                xp = pkt;
                xp = pkt;
                *((unsigned short *)pkt)++ = SWAP16c(TFTP_ERROR);
                *((unsigned short *)pkt) = SWAP16c(TFTP_ERROR);
                *((unsigned short *)pkt)++ = SWAP16(2);
                pkt += sizeof(short);
 
                *((unsigned short *)pkt) = SWAP16(2);
 
                pkt += sizeof(short);
                strcpy ((char *)pkt, "File has bad magic");
                strcpy ((char *)pkt, "File has bad magic");
                pkt += 18 /*strlen("File has bad magic")*/ + 1;
                pkt += 18 /*strlen("File has bad magic")*/ + 1;
                len = pkt - xp;
                len = pkt - xp;
                break;
                break;
        }
        }
 
 
        NetSetEther (NetTxPacket, NetServerEther, PROT_IP);
        NetSetEther (NetTxPacket, NetServerEther, PROT_IP);
        NetSetIP (NetTxPacket + ETHER_HDR_SIZE, NetServerIP,
        NetSetIP (NetTxPacket + ETHER_HDR_SIZE, NetServerIP,
                                        TftpServerPort, TftpOurPort, len);
                                        TftpServerPort, TftpOurPort, len);
        NetSendPacket (NetTxPacket, ETHER_HDR_SIZE + IP_HDR_SIZE + len);
        NetSendPacket (NetTxPacket, ETHER_HDR_SIZE + IP_HDR_SIZE + len);
 
 
}
}
 
 
 
 
static void
static void
TftpHandler (unsigned char * pkt, unsigned dest, unsigned src, unsigned len)
TftpHandler (unsigned char * pkt, unsigned dest, unsigned src, unsigned len)
{
{
 
#ifdef ET_DEBUG
 
  //  printf("TftpHandler\n");
 
#endif
        if (dest != TftpOurPort) {
        if (dest != TftpOurPort) {
                return;
                return;
        }
        }
        if (TftpState != STATE_RRQ && src != TftpServerPort) {
        if (TftpState != STATE_RRQ && src != TftpServerPort) {
                return;
                return;
Line 167... Line 240...
 
 
        if (len < 2) {
        if (len < 2) {
                return;
                return;
        }
        }
        len -= 2;
        len -= 2;
        switch (SWAP16(*((unsigned short *)pkt)++)) {
        switch (SWAP16(*((unsigned short *)pkt))) {
 
 
        case TFTP_RRQ:
        case TFTP_RRQ:
        case TFTP_WRQ:
        case TFTP_WRQ:
        case TFTP_ACK:
        case TFTP_ACK:
 
             pkt += sizeof(short);
                break;
                break;
        default:
        default:
                break;
                break;
 
 
        case TFTP_DATA:
        case TFTP_DATA:
 
                pkt += sizeof(short);
                if (len < 2)
                if (len < 2)
                        return;
                        return;
                len -= 2;
                len -= 2;
                TftpBlock = SWAP16(*(unsigned short *)pkt);
                TftpBlock = SWAP16(*(unsigned short *)pkt);
                if (((TftpBlock - 1) % 10) == 0) {
                if (((TftpBlock - 1) % 10) == 0) {
Line 232... Line 307...
                        NetState = NETLOOP_SUCCESS;
                        NetState = NETLOOP_SUCCESS;
                }
                }
                break;
                break;
 
 
        case TFTP_ERROR:
        case TFTP_ERROR:
 
                pkt += sizeof(short);
                printf ("\nTFTP error: '%s' (%d)\n",
                printf ("\nTFTP error: '%s' (%d)\n",
                                        pkt + 2, SWAP16(*(unsigned short *)pkt));
                                        pkt + 2, SWAP16(*(unsigned short *)pkt));
                printf ("Starting again\n\n");
                printf ("Starting again\n\n");
                NetStartAgain ();
                NetStartAgain ();
                break;
                break;
        }
        }
 
 
}
}
 
 
 
 
static void
static void
TftpTimeout (void)
TftpTimeout (void)
Line 269... Line 346...
                NetServerEther[4],
                NetServerEther[4],
                NetServerEther[5]
                NetServerEther[5]
        );
        );
#endif /* DEBUG */
#endif /* DEBUG */
 
 
 
        TFTP_CHKSUM = 0; // Reset checksum
 
 
        printf ("TFTP from server ");   print_IPaddr (NetServerIP);
        printf ("TFTP from server ");   print_IPaddr (NetServerIP);
        printf ("; our IP address is ");        print_IPaddr (NetOurIP);
        printf ("; our IP address is ");        print_IPaddr (NetOurIP);
 
 
        // Check if we need to send across this subnet
        // Check if we need to send across this subnet
        if (NetOurGatewayIP && NetOurSubnetMask) {
        if (NetOurGatewayIP && NetOurSubnetMask) {

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.