dma: ti: k3-udma: Fix build warnings when building for 32 bit platforms
[oweals/u-boot.git] / net / nfs.c
index bdbdc26c9eb047a896468c1a282e3c4004d37665..97e62f1dcebae1bbde78ab9f0291ce719ce1ec89 100644 (file)
--- a/net/nfs.c
+++ b/net/nfs.c
@@ -33,6 +33,7 @@
 #include <mapmem.h>
 #include "nfs.h"
 #include "bootp.h"
+#include <time.h>
 
 #define HASHES_PER_LINE 65     /* Number of "loading" hashes per line  */
 #define NFS_RETRY_COUNT 30
@@ -142,13 +143,6 @@ RPC_ADD_CREDENTIALS - Add RPC authentication/verifier entries
 **************************************************************************/
 static uint32_t *rpc_add_credentials(uint32_t *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
@@ -158,17 +152,11 @@ static uint32_t *rpc_add_credentials(uint32_t *p)
         * 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++ = htonl(20);               /* auth length */
+       *p++ = 0;                       /* stamp */
+       *p++ = 0;                       /* hostname string */
        *p++ = 0;                       /* uid */
        *p++ = 0;                       /* gid */
        *p++ = 0;                       /* auxiliary gid list */
@@ -209,10 +197,10 @@ static void rpc_req(int rpc_prog, int rpc_proc, uint32_t *data, int datalen)
                rpc_pkt.u.call.vers = htonl(2); /* portmapper is version 2 */
        }
        rpc_pkt.u.call.proc = htonl(rpc_proc);
-       p = (uint32_t *)&(rpc_pkt.u.call.data);
+       p = rpc_pkt.u.call.data;
 
        if (datalen)
-               memcpy((char *)p, (char *)data, datalen*sizeof(uint32_t));
+               memcpy(p, data, datalen * sizeof(uint32_t));
 
        pktlen = (char *)p + datalen * sizeof(uint32_t) - (char *)&rpc_pkt;
 
@@ -546,7 +534,7 @@ static int nfs_lookup_reply(uchar *pkt, unsigned len)
                        switch (ntohl(rpc_pkt.u.reply.data[0])) {
                        /* Minimal supported NFS version */
                        case 3:
-                               debug("*** Waring: NFS version not supported: Requested: V%d, accepted: min V%d - max V%d\n",
+                               debug("*** Warning: NFS version not supported: Requested: V%d, accepted: min V%d - max V%d\n",
                                      (supported_nfs_versions & NFSV2_FLAG) ?
                                                2 : 3,
                                      ntohl(rpc_pkt.u.reply.data[0]),
@@ -557,11 +545,13 @@ static int nfs_lookup_reply(uchar *pkt, unsigned len)
                                return -NFS_RPC_PROG_MISMATCH;
                        case 4:
                        default:
-                               printf("*** ERROR: NFS version not supported: Requested: V%d, accepted: min V%d - max V%d\n",
-                                      (supported_nfs_versions & NFSV2_FLAG) ?
+                               puts("*** ERROR: NFS version not supported");
+                               debug(": Requested: V%d, accepted: min V%d - max V%d\n",
+                                     (supported_nfs_versions & NFSV2_FLAG) ?
                                                2 : 3,
-                                      ntohl(rpc_pkt.u.reply.data[0]),
-                                      ntohl(rpc_pkt.u.reply.data[1]));
+                                     ntohl(rpc_pkt.u.reply.data[0]),
+                                     ntohl(rpc_pkt.u.reply.data[1]));
+                               puts("\n");
                        }
                        break;
                case NFS_RPC_PROG_UNAVAIL:
@@ -569,19 +559,23 @@ static int nfs_lookup_reply(uchar *pkt, unsigned len)
                case NFS_RPC_GARBAGE_ARGS:
                case NFS_RPC_SYSTEM_ERR:
                default: /* Unknown error on 'accept state' flag */
-                       printf("*** ERROR: accept state error (%d)\n",
-                              ntohl(rpc_pkt.u.reply.astatus));
+                       debug("*** ERROR: accept state error (%d)\n",
+                             ntohl(rpc_pkt.u.reply.astatus));
                        break;
                }
                return -1;
        }
 
        if (supported_nfs_versions & NFSV2_FLAG) {
+               if (((uchar *)&(rpc_pkt.u.reply.data[0]) - (uchar *)(&rpc_pkt) + NFS_FHSIZE) > len)
+                       return -NFS_RPC_DROP;
                memcpy(filefh, rpc_pkt.u.reply.data + 1, NFS_FHSIZE);
        } else {  /* NFSV3_FLAG */
                filefh3_length = ntohl(rpc_pkt.u.reply.data[1]);
                if (filefh3_length > NFS3_FHSIZE)
                        filefh3_length  = NFS3_FHSIZE;
+               if (((uchar *)&(rpc_pkt.u.reply.data[0]) - (uchar *)(&rpc_pkt) + filefh3_length) > len)
+                       return -NFS_RPC_DROP;
                memcpy(filefh, rpc_pkt.u.reply.data + 2, filefh3_length);
        }
 
@@ -590,7 +584,7 @@ static int nfs_lookup_reply(uchar *pkt, unsigned len)
 
 static int nfs3_get_attributes_offset(uint32_t *data)
 {
-       if (ntohl(data[1]) != 0) {
+       if (data[1]) {
                /* 'attributes_follow' flag is TRUE,
                 * so we have attributes on 21 dwords */
                /* Skip unused values :
@@ -645,6 +639,9 @@ static int nfs_readlink_reply(uchar *pkt, unsigned len)
        /* new path length */
        rlen = ntohl(rpc_pkt.u.reply.data[1 + nfsv3_data_offset]);
 
+       if (((uchar *)&(rpc_pkt.u.reply.data[0]) - (uchar *)(&rpc_pkt) + rlen) > len)
+               return -NFS_RPC_DROP;
+
        if (*((char *)&(rpc_pkt.u.reply.data[2 + nfsv3_data_offset])) != '/') {
                int pathlen;
 
@@ -712,6 +709,9 @@ static int nfs_read_reply(uchar *pkt, unsigned len)
                        &(rpc_pkt.u.reply.data[4 + nfsv3_data_offset]);
        }
 
+       if (((uchar *)&(rpc_pkt.u.reply.data[0]) - (uchar *)(&rpc_pkt) + rlen) > len)
+                       return -9999;
+
        if (store_block(data_ptr, nfs_offset, rlen))
                        return -9999;
 
@@ -743,6 +743,9 @@ static void nfs_handler(uchar *pkt, unsigned dest, struct in_addr sip,
 
        debug("%s\n", __func__);
 
+       if (len > sizeof(struct rpc_t))
+               return;
+
        if (dest != nfs_our_port)
                return;
 
@@ -781,7 +784,7 @@ static void nfs_handler(uchar *pkt, unsigned dest, struct in_addr sip,
                if (reply == -NFS_RPC_DROP) {
                        break;
                } else if (reply == -NFS_RPC_ERR) {
-                       puts("*** ERROR: Cannot umount\n");
+                       debug("*** ERROR: Cannot umount\n");
                        net_set_state(NETLOOP_FAIL);
                } else {
                        puts("\ndone\n");
@@ -833,6 +836,8 @@ static void nfs_handler(uchar *pkt, unsigned dest, struct in_addr sip,
 
        case STATE_READ_REQ:
                rlen = nfs_read_reply(pkt, len);
+               if (rlen == -NFS_RPC_DROP)
+                       break;
                net_set_timeout_handler(nfs_timeout, nfs_timeout_handler);
                if (rlen > 0) {
                        nfs_offset += rlen;
@@ -845,7 +850,7 @@ static void nfs_handler(uchar *pkt, unsigned dest, struct in_addr sip,
                        if (!rlen)
                                nfs_download_state = NETLOOP_SUCCESS;
                        if (rlen < 0)
-                               printf("NFS READ error (%d)\n", rlen);
+                               debug("NFS READ error (%d)\n", rlen);
                        nfs_state = STATE_UMOUNT_REQ;
                        nfs_send();
                }
@@ -864,11 +869,12 @@ void nfs_start(void)
 
        if (nfs_path == NULL) {
                net_set_state(NETLOOP_FAIL);
-               puts("*** ERROR: Fail allocate memory\n");
+               printf("*** ERROR: Fail allocate memory\n");
                return;
        }
 
-       if (net_boot_file_name[0] == '\0') {
+       if (!net_parse_bootfile(&nfs_server_ip, nfs_path,
+                               sizeof(nfs_path_buff))) {
                sprintf(nfs_path, "/nfsroot/%02X%02X%02X%02X.img",
                        net_ip.s_addr & 0xFF,
                        (net_ip.s_addr >>  8) & 0xFF,
@@ -877,18 +883,6 @@ void nfs_start(void)
 
                printf("*** Warning: no boot file name; using '%s'\n",
                       nfs_path);
-       } else {
-               char *p = net_boot_file_name;
-
-               p = strchr(p, ':');
-
-               if (p != NULL) {
-                       nfs_server_ip = string_to_ip(net_boot_file_name);
-                       ++p;
-                       strcpy(nfs_path, p);
-               } else {
-                       strcpy(nfs_path, net_boot_file_name);
-               }
        }
 
        nfs_filename = basename(nfs_path);
@@ -905,7 +899,7 @@ void nfs_start(void)
                struct in_addr server_net;
 
                our_net.s_addr = net_ip.s_addr & net_netmask.s_addr;
-               server_net.s_addr = net_server_ip.s_addr & net_netmask.s_addr;
+               server_net.s_addr = nfs_server_ip.s_addr & net_netmask.s_addr;
                if (our_net.s_addr != server_net.s_addr)
                        printf("; sending through gateway %pI4",
                               &net_gateway);
@@ -917,8 +911,7 @@ void nfs_start(void)
                       net_boot_file_expected_size_in_blocks << 9);
                print_size(net_boot_file_expected_size_in_blocks << 9, "");
        }
-       printf("\nLoad address: 0x%lx\n"
-               "Loading: *\b", load_addr);
+       printf("\nLoad address: 0x%lx\nLoading: *\b", load_addr);
 
        net_set_timeout_handler(nfs_timeout, nfs_timeout_handler);
        net_set_udp_handler(nfs_handler);