[PATCH 1_4] Merge common get_dev() routines for block devices
[oweals/u-boot.git] / net / net.c
index 80d047519f3ed817ef7fc41bd6dac48f3cd53827..1d1c98f3c20204c74ef3173b50537e21af8c60cd 100644 (file)
--- a/net/net.c
+++ b/net/net.c
  *
  * DHCP:
  *
- *     Prerequisites:   - own ethernet address
- *     We want:         - IP, Netmask, ServerIP, Gateway IP
- *                      - bootfilename, lease time
- *     Next step:       - TFTP
+ *     Prerequisites:  - own ethernet address
+ *     We want:                - IP, Netmask, ServerIP, Gateway IP
+ *                     - bootfilename, lease time
+ *     Next step:      - TFTP
  *
  * TFTP:
  *
  *                       derived from our own IP address)
  *     We want:        - load the boot file
  *     Next step:      none
+ *
+ * SNTP:
+ *
+ *     Prerequisites:  - own ethernet address
+ *                     - own IP address
+ *     We want:        - network time
+ *     Next step:      none
  */
 
 
 #include <status_led.h>
 #include <miiphy.h>
 #endif
+#if (CONFIG_COMMANDS & CFG_CMD_SNTP)
+#include "sntp.h"
+#endif
 
 #if (CONFIG_COMMANDS & CFG_CMD_NET)
 
+DECLARE_GLOBAL_DATA_PTR;
+
 #define ARP_TIMEOUT            5               /* Seconds before trying ARP again */
 #ifndef        CONFIG_NET_RETRY_COUNT
 # define ARP_TIMEOUT_COUNT     5               /* # of timeouts before giving up  */
@@ -122,7 +134,7 @@ uchar               NetBcastAddr[6] =       /* Ethernet bcast address               */
 uchar          NetEtherNullAddr[6] =
                        { 0, 0, 0, 0, 0, 0 };
 #if (CONFIG_COMMANDS & CFG_CMD_CDP)
-uchar          NetCDPAddr[6] = /* Ethernet bcast address               */
+uchar          NetCDPAddr[6] =         /* Ethernet bcast address               */
                        { 0x01, 0x00, 0x0c, 0xcc, 0xcc, 0xcc };
 #endif
 int            NetState;               /* Network loop state                   */
@@ -132,8 +144,9 @@ static int  NetRestarted = 0;       /* Network loop restarted               */
 static int     NetDevExists = 0;       /* At least one device configured       */
 #endif
 
-ushort         NetOurVLAN = ntohs(-1); /* default is without VLAN              */
-ushort         NetOurNativeVLAN = htons(-1);   /* dido                         */
+/* XXX in both little & big endian machines 0xFFFF == ntohs(-1) */
+ushort         NetOurVLAN = 0xFFFF;            /* default is without VLAN      */
+ushort         NetOurNativeVLAN = 0xFFFF;      /* ditto                        */
 
 char           BootFile[128];          /* Boot File name                       */
 
@@ -147,6 +160,16 @@ static void PingStart(void);
 static void CDPStart(void);
 #endif
 
+#if (CONFIG_COMMANDS & CFG_CMD_SNTP)
+IPaddr_t       NetNtpServerIP;         /* NTP server IP address                */
+int            NetTimeOffset=0;        /* offset time from UTC                 */
+#endif
+
+#ifdef CONFIG_NETCONSOLE
+void NcStart(void);
+int nc_input_packet(uchar *pkt, unsigned dest, unsigned src, unsigned len);
+#endif
+
 volatile uchar PktBuf[(PKTBUFSRX+1) * PKTSIZE_ALIGN + PKTALIGN];
 
 volatile uchar *NetRxPackets[PKTBUFSRX]; /* Receive packets                    */
@@ -164,49 +187,53 @@ static int net_check_prereq (proto_t protocol);
 IPaddr_t       NetArpWaitPacketIP;
 IPaddr_t       NetArpWaitReplyIP;
 uchar         *NetArpWaitPacketMAC;    /* MAC address of waiting packet's destination  */
-uchar          *NetArpWaitTxPacket;    /* THE transmit packet                  */
+uchar         *NetArpWaitTxPacket;     /* THE transmit packet                  */
 int            NetArpWaitTxPacketSize;
 uchar          NetArpWaitPacketBuf[PKTSIZE_ALIGN + PKTALIGN];
 ulong          NetArpWaitTimerStart;
 int            NetArpWaitTry;
 
-void ArpRequest(void)
+void ArpRequest (void)
 {
        int i;
        volatile uchar *pkt;
-       ARP_t * arp;
+       ARP_t *arp;
 
 #ifdef ET_DEBUG
-       printf("ARP broadcast %d\n", NetArpWaitTry);
+       printf ("ARP broadcast %d\n", NetArpWaitTry);
 #endif
        pkt = NetTxPacket;
 
-       pkt += NetSetEther(pkt, NetBcastAddr, PROT_ARP);
+       pkt += NetSetEther (pkt, NetBcastAddr, PROT_ARP);
 
-       arp = (ARP_t *)pkt;
+       arp = (ARP_t *) pkt;
 
-       arp->ar_hrd = htons(ARP_ETHER);
-       arp->ar_pro = htons(PROT_IP);
+       arp->ar_hrd = htons (ARP_ETHER);
+       arp->ar_pro = htons (PROT_IP);
        arp->ar_hln = 6;
        arp->ar_pln = 4;
-       arp->ar_op  = htons(ARPOP_REQUEST);
+       arp->ar_op = htons (ARPOP_REQUEST);
 
-       memcpy (&arp->ar_data[0], NetOurEther, 6);      /* source ET addr       */
-       NetWriteIP((uchar*)&arp->ar_data[6], NetOurIP); /* source IP addr       */
-       for (i=10; i<16; ++i) {
-               arp->ar_data[i] = 0;                    /* dest ET addr = 0     */
+       memcpy (&arp->ar_data[0], NetOurEther, 6);              /* source ET addr       */
+       NetWriteIP ((uchar *) & arp->ar_data[6], NetOurIP);     /* source IP addr       */
+       for (i = 10; i < 16; ++i) {
+               arp->ar_data[i] = 0;                            /* dest ET addr = 0     */
        }
 
-       if((NetArpWaitPacketIP & NetOurSubnetMask) != (NetOurIP & NetOurSubnetMask)) {
-           if (NetOurGatewayIP == 0) {
-               puts ("## Warning: gatewayip needed but not set\n");
-           }
-           NetArpWaitReplyIP = NetOurGatewayIP;
-       } else
-           NetArpWaitReplyIP = NetArpWaitPacketIP;
+       if ((NetArpWaitPacketIP & NetOurSubnetMask) !=
+           (NetOurIP & NetOurSubnetMask)) {
+               if (NetOurGatewayIP == 0) {
+                       puts ("## Warning: gatewayip needed but not set\n");
+                       NetArpWaitReplyIP = NetArpWaitPacketIP;
+               } else {
+                       NetArpWaitReplyIP = NetOurGatewayIP;
+               }
+       } else {
+               NetArpWaitReplyIP = NetArpWaitPacketIP;
+       }
 
-       NetWriteIP((uchar*)&arp->ar_data[16], NetArpWaitReplyIP);
-       (void) eth_send(NetTxPacket, (pkt - NetTxPacket) + ARP_HDR_SIZE);
+       NetWriteIP ((uchar *) & arp->ar_data[16], NetArpWaitReplyIP);
+       (void) eth_send (NetTxPacket, (pkt - NetTxPacket) + ARP_HDR_SIZE);
 }
 
 void ArpTimeoutCheck(void)
@@ -241,8 +268,6 @@ void ArpTimeoutCheck(void)
 int
 NetLoop(proto_t protocol)
 {
-       DECLARE_GLOBAL_DATA_PTR;
-
        bd_t *bd = gd->bd;
 
 #ifdef CONFIG_NET_MULTI
@@ -260,7 +285,6 @@ NetLoop(proto_t protocol)
 
        if (!NetTxPacket) {
                int     i;
-
                /*
                 *      Setup packet buffers, aligned correctly.
                 */
@@ -269,7 +293,6 @@ NetLoop(proto_t protocol)
                for (i = 0; i < PKTBUFSRX; i++) {
                        NetRxPackets[i] = NetTxPacket + (i+1)*PKTSIZE_ALIGN;
                }
-
        }
 
        if (!NetArpWaitTxPacket) {
@@ -279,9 +302,13 @@ NetLoop(proto_t protocol)
        }
 
        eth_halt();
+#ifdef CONFIG_NET_MULTI
        eth_set_current();
-       if(eth_init(bd) < 0)
-           return(-1);
+#endif
+       if (eth_init(bd) < 0) {
+               eth_halt();
+               return(-1);
+       }
 
 restart:
 #ifdef CONFIG_NET_MULTI
@@ -305,6 +332,10 @@ restart:
 #if (CONFIG_COMMANDS & CFG_CMD_PING)
        case PING:
 #endif
+#if (CONFIG_COMMANDS & CFG_CMD_SNTP)
+       case SNTP:
+#endif
+       case NETCONS:
        case TFTP:
                NetCopyIP(&NetOurIP, &bd->bi_ip_addr);
                NetOurGatewayIP = getenv_IPaddr ("gatewayip");
@@ -316,6 +347,7 @@ restart:
 #if (CONFIG_COMMANDS & CFG_CMD_NFS)
                case NFS:
 #endif
+               case NETCONS:
                case TFTP:
                        NetServerIP = getenv_IPaddr ("serverip");
                        break;
@@ -323,6 +355,11 @@ restart:
                case PING:
                        /* nothing */
                        break;
+#endif
+#if (CONFIG_COMMANDS & CFG_CMD_SNTP)
+               case SNTP:
+                       /* nothing */
+                       break;
 #endif
                default:
                        break;
@@ -337,11 +374,11 @@ restart:
                 */
                NetOurIP = 0;
                NetServerIP = getenv_IPaddr ("serverip");
-               NetOurVLAN = getenv_VLAN("vlan");       /* VLANs must be read */
-               NetOurNativeVLAN = getenv_VLAN("nvlan");
-       case CDP:
-               NetOurVLAN = getenv_VLAN("vlan");       /* VLANs must be read */
-               NetOurNativeVLAN = getenv_VLAN("nvlan");
+               NetOurVLAN = getenv_VLAN("vlan");       /* VLANs must be read */
+               NetOurNativeVLAN = getenv_VLAN("nvlan");
+       case CDP:
+               NetOurVLAN = getenv_VLAN("vlan");       /* VLANs must be read */
+               NetOurNativeVLAN = getenv_VLAN("nvlan");
                break;
        default:
                break;
@@ -350,6 +387,7 @@ restart:
        switch (net_check_prereq (protocol)) {
        case 1:
                /* network not configured */
+               eth_halt();
                return (-1);
 
 #ifdef CONFIG_NET_MULTI
@@ -371,6 +409,7 @@ restart:
 #if (CONFIG_COMMANDS & CFG_CMD_DHCP)
                case DHCP:
                        /* Start with a clean slate... */
+                       BootpTry = 0;
                        NetOurIP = 0;
                        NetServerIP = getenv_IPaddr ("serverip");
                        DhcpRequest();          /* Basically same as BOOTP */
@@ -400,6 +439,16 @@ restart:
                case CDP:
                        CDPStart();
                        break;
+#endif
+#ifdef CONFIG_NETCONSOLE
+               case NETCONS:
+                       NcStart();
+                       break;
+#endif
+#if (CONFIG_COMMANDS & CFG_CMD_SNTP)
+               case SNTP:
+                       SntpStart();
+                       break;
 #endif
                default:
                        break;
@@ -414,7 +463,7 @@ restart:
        /*
         * 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);
@@ -424,7 +473,7 @@ restart:
 
        /*
         *      Main packet reception loop.  Loop receiving packets until
-        *      someone sets `NetQuit'.
+        *      someone sets `NetState' to a state that terminates.
         */
        for (;;) {
                WATCHDOG_RESET();
@@ -459,16 +508,18 @@ restart:
                        thand_f *x;
 
 #if defined(CONFIG_MII) || (CONFIG_COMMANDS & CFG_CMD_MII)
-#if defined(CFG_FAULT_ECHO_LINK_DOWN) && defined(CONFIG_STATUS_LED) && defined(STATUS_LED_RED)
+#  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);
                        }
-#endif /* CFG_FAULT_ECHO_LINK_DOWN, ... */
+#  endif /* CFG_FAULT_ECHO_LINK_DOWN, ... */
 #endif /* CONFIG_MII, ... */
                        x = timeHandler;
                        timeHandler = (thand_f *)0;
@@ -519,43 +570,39 @@ startAgainHandler(uchar * pkt, unsigned dest, unsigned src, unsigned len)
        /* Totally ignore the packet */
 }
 
-void
-NetStartAgain(void)
+void NetStartAgain (void)
 {
-       DECLARE_GLOBAL_DATA_PTR;
-       char *s;
+       char *nretry;
+       int noretry = 0, once = 0;
 
-       if ((s = getenv("netretry")) != NULL && *s == 'n') {
-               eth_halt();
+       if ((nretry = getenv ("netretry")) != NULL) {
+               noretry = (strcmp (nretry, "no") == 0);
+               once = (strcmp (nretry, "once") == 0);
+       }
+       if (noretry) {
+               eth_halt ();
                NetState = NETLOOP_FAIL;
                return;
        }
-
 #ifndef CONFIG_NET_MULTI
-       NetSetTimeout(10 * CFG_HZ, startAgainTimeout);
-       NetSetHandler(startAgainHandler);
-#else
-       eth_halt();
-       eth_try_another(!NetRestarted);
-       eth_init(gd->bd);
-       if (NetRestartWrap)
-       {
+       NetSetTimeout (10 * CFG_HZ, startAgainTimeout);
+       NetSetHandler (startAgainHandler);
+#else  /* !CONFIG_NET_MULTI*/
+       eth_halt ();
+       eth_try_another (!NetRestarted);
+       eth_init (gd->bd);
+       if (NetRestartWrap) {
                NetRestartWrap = 0;
-               if (NetDevExists)
-               {
-                       NetSetTimeout(10 * CFG_HZ, startAgainTimeout);
-                       NetSetHandler(startAgainHandler);
-               }
-               else
-               {
+               if (NetDevExists && !once) {
+                       NetSetTimeout (10 * CFG_HZ, startAgainTimeout);
+                       NetSetHandler (startAgainHandler);
+               } else {
                        NetState = NETLOOP_FAIL;
                }
-       }
-       else
-       {
+       } else {
                NetState = NETLOOP_RESTART;
        }
-#endif
+#endif /* CONFIG_NET_MULTI */
 }
 
 /**********************************************************************/
@@ -571,7 +618,7 @@ NetSetHandler(rxhand_f * f)
 
 
 void
-NetSetTimeout(int iv, thand_f * f)
+NetSetTimeout(ulong iv, thand_f * f)
 {
        if (iv == 0) {
                timeHandler = (thand_f *)0;
@@ -629,7 +676,7 @@ NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, int sport, int len)
 
 #ifdef ET_DEBUG
        printf("sending UDP to %08lx/%02x:%02x:%02x:%02x:%02x:%02x\n",
-                       dest, ether[0], ether[1], ether[2], ether[3], ether[4], ether[5]);
+               dest, ether[0], ether[1], ether[2], ether[3], ether[4], ether[5]);
 #endif
 
        pkt = (uchar *)NetTxPacket;
@@ -637,7 +684,7 @@ NetSendUDPPacket(uchar *ether, IPaddr_t dest, int dport, int sport, int len)
        NetSetIP (pkt, dest, dport, sport, len);
        (void) eth_send(NetTxPacket, (pkt - NetTxPacket) + IP_HDR_SIZE + len);
 
-       return 0;       /* transmited */
+       return 0;       /* transmitted */
 }
 
 #if (CONFIG_COMMANDS & CFG_CMD_PING)
@@ -722,14 +769,13 @@ static void PingStart(void)
 {
 #if defined(CONFIG_NET_MULTI)
        printf ("Using %s device\n", eth_get_name());
-#endif
+#endif /* CONFIG_NET_MULTI */
        NetSetTimeout (10 * CFG_HZ, PingTimeout);
        NetSetHandler (PingHandler);
 
        PingSend();
 }
-
-#endif
+#endif /* CFG_CMD_PING */
 
 #if (CONFIG_COMMANDS & CFG_CMD_CDP)
 
@@ -763,6 +809,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;
@@ -772,14 +819,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)
@@ -812,9 +864,12 @@ int CDPSendTrigger(void)
        volatile ushort *s;
        volatile ushort *cp;
        Ethernet_t *et;
-       char buf[32];
        int len;
        ushort chksum;
+#if defined(CONFIG_CDP_DEVICE_ID) || defined(CONFIG_CDP_PORT_ID)   || \
+    defined(CONFIG_CDP_VERSION)   || defined(CONFIG_CDP_PLATFORM)
+       char buf[32];
+#endif
 
        pkt = NetTxPacket;
        et = (Ethernet_t *)pkt;
@@ -1073,8 +1128,7 @@ static void CDPStart(void)
 
        CDPSendTrigger();
 }
-
-#endif
+#endif /* CFG_CMD_CDP */
 
 
 void
@@ -1256,6 +1310,9 @@ NetReceive(volatile uchar * inpkt, int len)
                                /* save address for later use */
                                memcpy(NetArpWaitPacketMAC, &arp->ar_data[0], 6);
 
+#ifdef CONFIG_NETCONSOLE
+                               (*packetHandler)(0,0,0,0);
+#endif
                                /* modify header, and transmit it */
                                memcpy(((Ethernet_t *)NetArpWaitTxPacket)->et_dest, NetArpWaitPacketMAC, 6);
                                (void) eth_send(NetArpWaitTxPacket, NetArpWaitTxPacketSize);
@@ -1273,6 +1330,7 @@ NetReceive(volatile uchar * inpkt, int len)
 #endif
                        return;
                }
+               break;
 
        case PROT_RARP:
 #ifdef ET_DEBUG
@@ -1352,12 +1410,12 @@ NetReceive(volatile uchar * inpkt, int len)
 
                        switch (icmph->type) {
                        case ICMP_REDIRECT:
-                       if (icmph->code != ICMP_REDIR_HOST)
+                               if (icmph->code != ICMP_REDIR_HOST)
+                                       return;
+                               puts (" ICMP Host Redirect to ");
+                               print_IPaddr(icmph->un.gateway);
+                               putc(' ');
                                return;
-                       puts (" ICMP Host Redirect to ");
-                       print_IPaddr(icmph->un.gateway);
-                       putc(' ');
-                               break;
 #if (CONFIG_COMMANDS & CFG_CMD_PING)
                        case ICMP_ECHO_REPLY:
                                /*
@@ -1365,7 +1423,7 @@ NetReceive(volatile uchar * inpkt, int len)
                                 */
                                /* XXX point to ip packet */
                                (*packetHandler)((uchar *)ip, 0, 0, 0);
-                               break;
+                               return;
 #endif
                        default:
                                return;
@@ -1374,6 +1432,52 @@ NetReceive(volatile uchar * inpkt, int len)
                        return;
                }
 
+#ifdef CONFIG_UDP_CHECKSUM
+               if (ip->udp_xsum != 0) {
+                       ulong   xsum;
+                       ushort *sumptr;
+                       ushort  sumlen;
+
+                       xsum  = ip->ip_p;
+                       xsum += (ntohs(ip->udp_len));
+                       xsum += (ntohl(ip->ip_src) >> 16) & 0x0000ffff;
+                       xsum += (ntohl(ip->ip_src) >>  0) & 0x0000ffff;
+                       xsum += (ntohl(ip->ip_dst) >> 16) & 0x0000ffff;
+                       xsum += (ntohl(ip->ip_dst) >>  0) & 0x0000ffff;
+
+                       sumlen = ntohs(ip->udp_len);
+                       sumptr = (ushort *) &(ip->udp_src);
+
+                       while (sumlen > 1) {
+                               ushort sumdata;
+
+                               sumdata = *sumptr++;
+                               xsum += ntohs(sumdata);
+                               sumlen -= 2;
+                       }
+                       if (sumlen > 0) {
+                               ushort sumdata;
+
+                               sumdata = *(unsigned char *) sumptr;
+                               sumdata = (sumdata << 8) & 0xff00;
+                               xsum += sumdata;
+                       }
+                       while ((xsum >> 16) != 0) {
+                               xsum = (xsum & 0x0000ffff) + ((xsum >> 16) & 0x0000ffff);
+                       }
+                       if ((xsum != 0x00000000) && (xsum != 0x0000ffff)) {
+                               printf(" UDP wrong checksum %08x %08x\n", xsum, ntohs(ip->udp_xsum));
+                               return;
+                       }
+               }
+#endif
+
+#ifdef CONFIG_NETCONSOLE
+               nc_input_packet((uchar *)ip +IP_HDR_SIZE,
+                                               ntohs(ip->udp_dst),
+                                               ntohs(ip->udp_src),
+                                               ntohs(ip->udp_len) - 8);
+#endif
                /*
                 *      IP header OK.  Pass the packet to the current handler.
                 */
@@ -1381,7 +1485,6 @@ NetReceive(volatile uchar * inpkt, int len)
                                                ntohs(ip->udp_dst),
                                                ntohs(ip->udp_src),
                                                ntohs(ip->udp_len) - 8);
-
                break;
        }
 }
@@ -1392,68 +1495,76 @@ NetReceive(volatile uchar * inpkt, int len)
 static int net_check_prereq (proto_t protocol)
 {
        switch (protocol) {
-                       /* Fall through */
+               /* Fall through */
 #if (CONFIG_COMMANDS & CFG_CMD_PING)
        case PING:
-                       if (NetPingIP == 0) {
-                               puts ("*** ERROR: ping address not given\n");
-                               return (1);
-                       }
-                       goto common;
+               if (NetPingIP == 0) {
+                       puts ("*** ERROR: ping address not given\n");
+                       return (1);
+               }
+               goto common;
+#endif
+#if (CONFIG_COMMANDS & CFG_CMD_SNTP)
+       case SNTP:
+               if (NetNtpServerIP == 0) {
+                       puts ("*** ERROR: NTP server address not given\n");
+                       return (1);
+               }
+               goto common;
 #endif
 #if (CONFIG_COMMANDS & CFG_CMD_NFS)
        case NFS:
 #endif
+       case NETCONS:
        case TFTP:
-                       if (NetServerIP == 0) {
-                               puts ("*** ERROR: `serverip' not set\n");
-                               return (1);
-                       }
-
-#if (CONFIG_COMMANDS & CFG_CMD_PING)
-               common:
+               if (NetServerIP == 0) {
+                       puts ("*** ERROR: `serverip' not set\n");
+                       return (1);
+               }
+#if (CONFIG_COMMANDS & (CFG_CMD_PING | CFG_CMD_SNTP))
+    common:
 #endif
 
-                       if (NetOurIP == 0) {
-                               puts ("*** ERROR: `ipaddr' not set\n");
-                               return (1);
-                       }
-                       /* Fall through */
+               if (NetOurIP == 0) {
+                       puts ("*** ERROR: `ipaddr' not set\n");
+                       return (1);
+               }
+               /* Fall through */
 
        case DHCP:
        case RARP:
        case BOOTP:
        case CDP:
-                       if (memcmp(NetOurEther, "\0\0\0\0\0\0", 6) == 0) {
+               if (memcmp (NetOurEther, "\0\0\0\0\0\0", 6) == 0) {
 #ifdef CONFIG_NET_MULTI
-                           extern int eth_get_dev_index (void);
-                           int num = eth_get_dev_index();
+                       extern int eth_get_dev_index (void);
+                       int num = eth_get_dev_index ();
 
-                           switch (num) {
-                           case -1:
+                       switch (num) {
+                       case -1:
                                puts ("*** ERROR: No ethernet found.\n");
                                return (1);
-                           case 0:
+                       case 0:
                                puts ("*** ERROR: `ethaddr' not set\n");
                                break;
-                           default:
+                       default:
                                printf ("*** ERROR: `eth%daddr' not set\n",
                                        num);
                                break;
-                           }
+                       }
 
-                           NetStartAgain ();
-                           return (2);
+                       NetStartAgain ();
+                       return (2);
 #else
-                           puts ("*** ERROR: `ethaddr' not set\n");
-                           return (1);
+                       puts ("*** ERROR: `ethaddr' not set\n");
+                       return (1);
 #endif
-                       }
-                       /* Fall through */
-               default:
-                       return(0);
+               }
+               /* Fall through */
+       default:
+               return (0);
        }
-       return (0);     /* OK */
+       return (0);             /* OK */
 }
 /**********************************************************************/
 
@@ -1468,10 +1579,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);
@@ -1529,7 +1641,7 @@ NetSetIP(volatile uchar * xip, IPaddr_t dest, int dport, int sport, int len)
 
        /*
         *      Construct an IP and UDP header.
-                       (need to set no fragment bit - XXX)
+        *      (need to set no fragment bit - XXX)
         */
        ip->ip_hl_v  = 0x45;            /* IP_HDR_SIZE / 4 (not including UDP) */
        ip->ip_tos   = 0;
@@ -1548,7 +1660,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;
@@ -1570,7 +1682,7 @@ void ip_to_string (IPaddr_t x, char *s)
                 (int) ((x >> 24) & 0xff),
                 (int) ((x >> 16) & 0xff),
                 (int) ((x >> 8) & 0xff), (int) ((x >> 0) & 0xff)
-               );
+       );
 }
 
 IPaddr_t string_to_ip(char *s)
@@ -1612,14 +1724,14 @@ ushort string_to_VLAN(char *s)
        ushort id;
 
        if (s == NULL)
-               return VLAN_NONE;
+               return htons(VLAN_NONE);
 
        if (*s < '0' || *s > '9')
                id = VLAN_NONE;
        else
                id = (ushort)simple_strtoul(s, NULL, 10);
 
-       return id;
+       return htons(id);
 }
 
 void print_IPaddr (IPaddr_t x)