]> git.sur5r.net Git - u-boot/blobdiff - net/net.c
POST: replace the LOGBUFF_INITIALIZED flag in gd->post_log_word (1 << 31) with the...
[u-boot] / net / net.c
index 00217be08103f85aef1b7b584a2f65da2aee41c6..b6dad89326128b936b09e393d058b6caa6b0cd21 100644 (file)
--- a/net/net.c
+++ b/net/net.c
 #include <status_led.h>
 #include <miiphy.h>
 #endif
-#if (CONFIG_COMMANDS & CFG_CMD_SNTP)
+#if defined(CONFIG_CMD_SNTP)
 #include "sntp.h"
 #endif
 
-#if (CONFIG_COMMANDS & CFG_CMD_NET)
+#if defined(CONFIG_CMD_NET)
+
+DECLARE_GLOBAL_DATA_PTR;
+
+#ifndef        CONFIG_ARP_TIMEOUT
+# define ARP_TIMEOUT           50UL    /* Deciseconds before trying ARP again */
+#elif (CONFIG_ARP_TIMEOUT < 100)
+# error "Due to possible overflow CONFIG_ARP_TIMEOUT must be greater than 100ms"
+#else
+# if (CONFIG_ARP_TIMEOUT % 100)
+#  warning "Supported ARP_TIMEOUT precision is 100ms"
+# endif
+# define ARP_TIMEOUT           (CONFIG_ARP_TIMEOUT / 100)
+#endif
+
 
-#define ARP_TIMEOUT            5               /* Seconds before trying ARP again */
 #ifndef        CONFIG_NET_RETRY_COUNT
-# define ARP_TIMEOUT_COUNT     5               /* # of timeouts before giving up  */
+# define ARP_TIMEOUT_COUNT     5       /* # of timeouts before giving up  */
 #else
-# define ARP_TIMEOUT_COUNT  (CONFIG_NET_RETRY_COUNT)
+# define ARP_TIMEOUT_COUNT     CONFIG_NET_RETRY_COUNT
 #endif
 
 #if 0
 IPaddr_t       NetOurSubnetMask=0;             /* Our subnet mask (0=unknown)  */
 IPaddr_t       NetOurGatewayIP=0;              /* Our gateways IP address      */
 IPaddr_t       NetOurDNSIP=0;                  /* Our DNS IP address           */
-#if (CONFIG_BOOTP_MASK & CONFIG_BOOTP_DNS2)
+#if defined(CONFIG_BOOTP_DNS2)
 IPaddr_t       NetOurDNS2IP=0;                 /* Our 2nd DNS IP address       */
 #endif
 char           NetOurNISDomain[32]={0,};       /* Our NIS domain               */
@@ -116,6 +129,10 @@ char               NetOurHostName[32]={0,};        /* Our hostname                 */
 char           NetOurRootPath[64]={0,};        /* Our bootpath                 */
 ushort         NetBootFileSize=0;              /* Our bootfile size in blocks  */
 
+#ifdef CONFIG_MCAST_TFTP       /* Multicast TFTP */
+IPaddr_t Mcast_addr;
+#endif
+
 /** END OF BOOTP EXTENTIONS **/
 
 ulong          NetBootFileXferSize;    /* The actual transferred size of the bootfile (in bytes) */
@@ -123,7 +140,7 @@ uchar               NetOurEther[6];         /* Our ethernet address                 */
 uchar          NetServerEther[6] =     /* Boot server enet address             */
                        { 0, 0, 0, 0, 0, 0 };
 IPaddr_t       NetOurIP;               /* Our IP addr (0 = unknown)            */
-IPaddr_t       NetServerIP;            /* Our IP addr (0 = unknown)            */
+IPaddr_t       NetServerIP;            /* Server IP addr (0 = unknown)         */
 volatile uchar *NetRxPkt;              /* Current receive packet               */
 int            NetRxPktLen;            /* Current rx packet length             */
 unsigned       NetIPID;                /* IP packet ID                         */
@@ -131,7 +148,10 @@ uchar              NetBcastAddr[6] =       /* Ethernet bcast address               */
                        { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
 uchar          NetEtherNullAddr[6] =
                        { 0, 0, 0, 0, 0, 0 };
-#if (CONFIG_COMMANDS & CFG_CMD_CDP)
+#ifdef CONFIG_API
+void           (*push_packet)(volatile void *, int len) = 0;
+#endif
+#if defined(CONFIG_CMD_CDP)
 uchar          NetCDPAddr[6] =         /* Ethernet bcast address               */
                        { 0x01, 0x00, 0x0c, 0xcc, 0xcc, 0xcc };
 #endif
@@ -148,17 +168,17 @@ ushort            NetOurNativeVLAN = 0xFFFF;      /* ditto                        */
 
 char           BootFile[128];          /* Boot File name                       */
 
-#if (CONFIG_COMMANDS & CFG_CMD_PING)
+#if defined(CONFIG_CMD_PING)
 IPaddr_t       NetPingIP;              /* the ip address to ping               */
 
 static void PingStart(void);
 #endif
 
-#if (CONFIG_COMMANDS & CFG_CMD_CDP)
+#if defined(CONFIG_CMD_CDP)
 static void CDPStart(void);
 #endif
 
-#if (CONFIG_COMMANDS & CFG_CMD_SNTP)
+#if defined(CONFIG_CMD_SNTP)
 IPaddr_t       NetNtpServerIP;         /* NTP server IP address                */
 int            NetTimeOffset=0;        /* offset time from UTC                 */
 #endif
@@ -222,8 +242,10 @@ void ArpRequest (void)
            (NetOurIP & NetOurSubnetMask)) {
                if (NetOurGatewayIP == 0) {
                        puts ("## Warning: gatewayip needed but not set\n");
+                       NetArpWaitReplyIP = NetArpWaitPacketIP;
+               } else {
+                       NetArpWaitReplyIP = NetOurGatewayIP;
                }
-               NetArpWaitReplyIP = NetOurGatewayIP;
        } else {
                NetArpWaitReplyIP = NetArpWaitPacketIP;
        }
@@ -242,7 +264,7 @@ void ArpTimeoutCheck(void)
        t = get_timer(0);
 
        /* check for arp timeout */
-       if ((t - NetArpWaitTimerStart) > ARP_TIMEOUT * CFG_HZ) {
+       if ((t - NetArpWaitTimerStart) > ARP_TIMEOUT * CFG_HZ / 10) {
                NetArpWaitTry++;
 
                if (NetArpWaitTry >= ARP_TIMEOUT_COUNT) {
@@ -264,8 +286,6 @@ void ArpTimeoutCheck(void)
 int
 NetLoop(proto_t protocol)
 {
-       DECLARE_GLOBAL_DATA_PTR;
-
        bd_t *bd = gd->bd;
 
 #ifdef CONFIG_NET_MULTI
@@ -324,13 +344,13 @@ restart:
         */
 
        switch (protocol) {
-#if (CONFIG_COMMANDS & CFG_CMD_NFS)
+#if defined(CONFIG_CMD_NFS)
        case NFS:
 #endif
-#if (CONFIG_COMMANDS & CFG_CMD_PING)
+#if defined(CONFIG_CMD_PING)
        case PING:
 #endif
-#if (CONFIG_COMMANDS & CFG_CMD_SNTP)
+#if defined(CONFIG_CMD_SNTP)
        case SNTP:
 #endif
        case NETCONS:
@@ -342,19 +362,19 @@ restart:
                NetOurNativeVLAN = getenv_VLAN("nvlan");
 
                switch (protocol) {
-#if (CONFIG_COMMANDS & CFG_CMD_NFS)
+#if defined(CONFIG_CMD_NFS)
                case NFS:
 #endif
                case NETCONS:
                case TFTP:
                        NetServerIP = getenv_IPaddr ("serverip");
                        break;
-#if (CONFIG_COMMANDS & CFG_CMD_PING)
+#if defined(CONFIG_CMD_PING)
                case PING:
                        /* nothing */
                        break;
 #endif
-#if (CONFIG_COMMANDS & CFG_CMD_SNTP)
+#if defined(CONFIG_CMD_SNTP)
                case SNTP:
                        /* nothing */
                        break;
@@ -404,7 +424,7 @@ restart:
                        TftpStart();
                        break;
 
-#if (CONFIG_COMMANDS & CFG_CMD_DHCP)
+#if defined(CONFIG_CMD_DHCP)
                case DHCP:
                        /* Start with a clean slate... */
                        BootpTry = 0;
@@ -412,7 +432,7 @@ restart:
                        NetServerIP = getenv_IPaddr ("serverip");
                        DhcpRequest();          /* Basically same as BOOTP */
                        break;
-#endif /* CFG_CMD_DHCP */
+#endif
 
                case BOOTP:
                        BootpTry = 0;
@@ -423,17 +443,17 @@ restart:
                        RarpTry = 0;
                        RarpRequest ();
                        break;
-#if (CONFIG_COMMANDS & CFG_CMD_PING)
+#if defined(CONFIG_CMD_PING)
                case PING:
                        PingStart();
                        break;
 #endif
-#if (CONFIG_COMMANDS & CFG_CMD_NFS)
+#if defined(CONFIG_CMD_NFS)
                case NFS:
                        NfsStart();
                        break;
 #endif
-#if (CONFIG_COMMANDS & CFG_CMD_CDP)
+#if defined(CONFIG_CMD_CDP)
                case CDP:
                        CDPStart();
                        break;
@@ -443,7 +463,7 @@ restart:
                        NcStart();
                        break;
 #endif
-#if (CONFIG_COMMANDS & CFG_CMD_SNTP)
+#if defined(CONFIG_CMD_SNTP)
                case SNTP:
                        SntpStart();
                        break;
@@ -456,12 +476,12 @@ restart:
                break;
        }
 
-#if defined(CONFIG_MII) || (CONFIG_COMMANDS & CFG_CMD_MII)
+#if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
 #if defined(CFG_FAULT_ECHO_LINK_DOWN) && defined(CONFIG_STATUS_LED) && defined(STATUS_LED_RED)
        /*
         * Echo the inverted link state to the fault LED.
         */
-       if(miiphy_link(CFG_FAULT_MII_ADDR)) {
+       if(miiphy_link(eth_get_dev()->name, CFG_FAULT_MII_ADDR)) {
                status_led_set (STATUS_LED_RED, STATUS_LED_OFF);
        } else {
                status_led_set (STATUS_LED_RED, STATUS_LED_ON);
@@ -485,7 +505,7 @@ restart:
                 *      Check the ethernet for a new packet.  The ethernet
                 *      receive routine will process it.
                 */
-                       eth_rx();
+               eth_rx();
 
                /*
                 *      Abort if ctrl-c was pressed.
@@ -505,14 +525,14 @@ restart:
                if (timeHandler && ((get_timer(0) - timeStart) > timeDelta)) {
                        thand_f *x;
 
-#if defined(CONFIG_MII) || (CONFIG_COMMANDS & CFG_CMD_MII)
+#if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
 #  if defined(CFG_FAULT_ECHO_LINK_DOWN) && \
       defined(CONFIG_STATUS_LED) &&       \
       defined(STATUS_LED_RED)
                        /*
                         * Echo the inverted link state to the fault LED.
                         */
-                       if(miiphy_link(CFG_FAULT_MII_ADDR)) {
+                       if(miiphy_link(eth_get_dev()->name, CFG_FAULT_MII_ADDR)) {
                                status_led_set (STATUS_LED_RED, STATUS_LED_OFF);
                        } else {
                                status_led_set (STATUS_LED_RED, STATUS_LED_ON);
@@ -535,11 +555,11 @@ restart:
 
                case NETLOOP_SUCCESS:
                        if (NetBootFileXferSize > 0) {
-                               char buf[10];
+                               char buf[20];
                                printf("Bytes transferred = %ld (%lx hex)\n",
                                        NetBootFileXferSize,
                                        NetBootFileXferSize);
-                               sprintf(buf, "%lx", NetBootFileXferSize);
+                               sprintf(buf, "%lX", NetBootFileXferSize);
                                setenv("filesize", buf);
 
                                sprintf(buf, "%lX", (unsigned long)load_addr);
@@ -570,9 +590,6 @@ startAgainHandler(uchar * pkt, unsigned dest, unsigned src, unsigned len)
 
 void NetStartAgain (void)
 {
-#ifdef CONFIG_NET_MULTI
-       DECLARE_GLOBAL_DATA_PTR;
-#endif
        char *nretry;
        int noretry = 0, once = 0;
 
@@ -586,16 +603,18 @@ void NetStartAgain (void)
                return;
        }
 #ifndef CONFIG_NET_MULTI
-       NetSetTimeout (10 * CFG_HZ, startAgainTimeout);
+       NetSetTimeout (10UL * CFG_HZ, startAgainTimeout);
        NetSetHandler (startAgainHandler);
 #else  /* !CONFIG_NET_MULTI*/
        eth_halt ();
+#if !defined(CONFIG_NET_DO_NOT_TRY_ANOTHER)
        eth_try_another (!NetRestarted);
+#endif
        eth_init (gd->bd);
        if (NetRestartWrap) {
                NetRestartWrap = 0;
                if (NetDevExists && !once) {
-                       NetSetTimeout (10 * CFG_HZ, startAgainTimeout);
+                       NetSetTimeout (10UL * CFG_HZ, startAgainTimeout);
                        NetSetHandler (startAgainHandler);
                } else {
                        NetState = NETLOOP_FAIL;
@@ -688,7 +707,7 @@ NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, int sport, int len)
        return 0;       /* transmitted */
 }
 
-#if (CONFIG_COMMANDS & CFG_CMD_PING)
+#if defined(CONFIG_CMD_PING)
 static ushort PingSeqNo;
 
 int PingSend(void)
@@ -771,14 +790,14 @@ static void PingStart(void)
 #if defined(CONFIG_NET_MULTI)
        printf ("Using %s device\n", eth_get_name());
 #endif /* CONFIG_NET_MULTI */
-       NetSetTimeout (10 * CFG_HZ, PingTimeout);
+       NetSetTimeout (10UL * CFG_HZ, PingTimeout);
        NetSetHandler (PingHandler);
 
        PingSend();
 }
-#endif /* CFG_CMD_PING */
+#endif
 
-#if (CONFIG_COMMANDS & CFG_CMD_CDP)
+#if defined(CONFIG_CMD_CDP)
 
 #define CDP_DEVICE_ID_TLV              0x0001
 #define CDP_ADDRESS_TLV                        0x0002
@@ -810,6 +829,7 @@ static ushort CDP_compute_csum(const uchar *buff, ushort len)
        int     odd;
        ulong   result = 0;
        ushort  leftover;
+       ushort *p;
 
        if (len > 0) {
                odd = 1 & (ulong)buff;
@@ -819,14 +839,19 @@ static ushort CDP_compute_csum(const uchar *buff, ushort len)
                        buff++;
                }
                while (len > 1) {
-                       result += *((const ushort *)buff)++;
+                       p = (ushort *)buff;
+                       result += *p++;
+                       buff = (uchar *)p;
                        if (result & 0x80000000)
                                result = (result & 0xFFFF) + (result >> 16);
                        len -= 2;
                }
                if (len) {
                        leftover = (signed short)(*(const signed char *)buff);
-                       /* * XXX CISCO SUCKS big time! (and blows too) */
+                       /* CISCO SUCKS big time! (and blows too):
+                        * CDP uses the IP checksum algorithm with a twist;
+                        * for the last byte it *sign* extends and sums.
+                        */
                        result = (result & 0xffff0000) | ((result + leftover) & 0x0000ffff);
                }
                while (result >> 16)
@@ -1123,7 +1148,7 @@ static void CDPStart(void)
 
        CDPSendTrigger();
 }
-#endif /* CFG_CMD_CDP */
+#endif
 
 
 void
@@ -1135,7 +1160,7 @@ NetReceive(volatile uchar * inpkt, int len)
        IPaddr_t tmp;
        int     x;
        uchar *pkt;
-#if (CONFIG_COMMANDS & CFG_CMD_CDP)
+#if defined(CONFIG_CMD_CDP)
        int iscdp;
 #endif
        ushort cti = 0, vlanid = VLAN_NONE, myvlanid, mynvlanid;
@@ -1152,7 +1177,14 @@ NetReceive(volatile uchar * inpkt, int len)
        if (len < ETHER_HDR_SIZE)
                return;
 
-#if (CONFIG_COMMANDS & CFG_CMD_CDP)
+#ifdef CONFIG_API
+       if (push_packet) {
+               (*push_packet)(inpkt, len);
+               return;
+       }
+#endif
+
+#if defined(CONFIG_CMD_CDP)
        /* keep track if packet is CDP */
        iscdp = memcmp(et->et_dest, NetCDPAddr, 6) == 0;
 #endif
@@ -1195,7 +1227,7 @@ NetReceive(volatile uchar * inpkt, int len)
 
                /* if no VLAN active */
                if ((ntohs(NetOurVLAN) & VLAN_IDMASK) == VLAN_NONE
-#if (CONFIG_COMMANDS & CFG_CMD_CDP)
+#if defined(CONFIG_CMD_CDP)
                                && iscdp == 0
 #endif
                                )
@@ -1213,7 +1245,7 @@ NetReceive(volatile uchar * inpkt, int len)
        printf("Receive from protocol 0x%x\n", x);
 #endif
 
-#if (CONFIG_COMMANDS & CFG_CMD_CDP)
+#if defined(CONFIG_CMD_CDP)
        if (iscdp) {
                CDPHandler((uchar *)ip, len);
                return;
@@ -1381,6 +1413,9 @@ NetReceive(volatile uchar * inpkt, int len)
                }
                tmp = NetReadIP(&ip->ip_dst);
                if (NetOurIP && tmp != NetOurIP && tmp != 0xFFFFFFFF) {
+#ifdef CONFIG_MCAST_TFTP
+                       if (Mcast_addr != tmp)
+#endif
                        return;
                }
                /*
@@ -1411,7 +1446,7 @@ NetReceive(volatile uchar * inpkt, int len)
                                print_IPaddr(icmph->un.gateway);
                                putc(' ');
                                return;
-#if (CONFIG_COMMANDS & CFG_CMD_PING)
+#if defined(CONFIG_CMD_PING)
                        case ICMP_ECHO_REPLY:
                                /*
                                 *      IP header OK.  Pass the packet to the current handler.
@@ -1419,6 +1454,26 @@ NetReceive(volatile uchar * inpkt, int len)
                                /* XXX point to ip packet */
                                (*packetHandler)((uchar *)ip, 0, 0, 0);
                                return;
+                       case ICMP_ECHO_REQUEST:
+#ifdef ET_DEBUG
+                               printf ("Got ICMP ECHO REQUEST, return %d bytes \n",
+                                       ETHER_HDR_SIZE + len);
+#endif
+                               memcpy (&et->et_dest[0], &et->et_src[0], 6);
+                               memcpy (&et->et_src[ 0], NetOurEther, 6);
+
+                               ip->ip_sum = 0;
+                               ip->ip_off = 0;
+                               NetCopyIP((void*)&ip->ip_dst, &ip->ip_src);
+                               NetCopyIP((void*)&ip->ip_src, &NetOurIP);
+                               ip->ip_sum = ~NetCksum((uchar *)ip, IP_HDR_SIZE_NO_UDP >> 1);
+
+                               icmph->type = ICMP_ECHO_REPLY;
+                               icmph->checksum = 0;
+                               icmph->checksum = ~NetCksum((uchar *)icmph,
+                                               (len - IP_HDR_SIZE_NO_UDP) >> 1);
+                               (void) eth_send((uchar *)et, ETHER_HDR_SIZE + len);
+                               return;
 #endif
                        default:
                                return;
@@ -1467,6 +1522,7 @@ NetReceive(volatile uchar * inpkt, int len)
                }
 #endif
 
+
 #ifdef CONFIG_NETCONSOLE
                nc_input_packet((uchar *)ip +IP_HDR_SIZE,
                                                ntohs(ip->udp_dst),
@@ -1491,7 +1547,7 @@ static int net_check_prereq (proto_t protocol)
 {
        switch (protocol) {
                /* Fall through */
-#if (CONFIG_COMMANDS & CFG_CMD_PING)
+#if defined(CONFIG_CMD_PING)
        case PING:
                if (NetPingIP == 0) {
                        puts ("*** ERROR: ping address not given\n");
@@ -1499,7 +1555,7 @@ static int net_check_prereq (proto_t protocol)
                }
                goto common;
 #endif
-#if (CONFIG_COMMANDS & CFG_CMD_SNTP)
+#if defined(CONFIG_CMD_SNTP)
        case SNTP:
                if (NetNtpServerIP == 0) {
                        puts ("*** ERROR: NTP server address not given\n");
@@ -1507,7 +1563,7 @@ static int net_check_prereq (proto_t protocol)
                }
                goto common;
 #endif
-#if (CONFIG_COMMANDS & CFG_CMD_NFS)
+#if defined(CONFIG_CMD_NFS)
        case NFS:
 #endif
        case NETCONS:
@@ -1516,7 +1572,7 @@ static int net_check_prereq (proto_t protocol)
                        puts ("*** ERROR: `serverip' not set\n");
                        return (1);
                }
-#if (CONFIG_COMMANDS & (CFG_CMD_PING | CFG_CMD_SNTP))
+#if defined(CONFIG_CMD_PING) || defined(CONFIG_CMD_SNTP)
     common:
 #endif
 
@@ -1574,10 +1630,11 @@ unsigned
 NetCksum(uchar * ptr, int len)
 {
        ulong   xsum;
+       ushort *p = (ushort *)ptr;
 
        xsum = 0;
        while (len-- > 0)
-               xsum += *((ushort *)ptr)++;
+               xsum += *p++;
        xsum = (xsum & 0xffff) + (xsum >> 16);
        xsum = (xsum & 0xffff) + (xsum >> 16);
        return (xsum & 0xffff);
@@ -1654,7 +1711,7 @@ NetSetIP(volatile uchar * xip, IPaddr_t dest, int dport, int sport, int len)
        ip->ip_sum   = ~NetCksum((uchar *)ip, IP_HDR_SIZE_NO_UDP / 2);
 }
 
-void copy_filename (uchar *dst, uchar *src, int size)
+void copy_filename (char *dst, char *src, int size)
 {
        if (*src && (*src == '"')) {
                ++src;
@@ -1667,7 +1724,7 @@ void copy_filename (uchar *dst, uchar *src, int size)
        *dst = '\0';
 }
 
-#endif /* CFG_CMD_NET */
+#endif
 
 void ip_to_string (IPaddr_t x, char *s)
 {