]> git.sur5r.net Git - u-boot/blobdiff - net/nfs.c
POST: replace the LOGBUFF_INITIALIZED flag in gd->post_log_word (1 << 31) with the...
[u-boot] / net / nfs.c
index 6f4a06ad049dc19ac60b1f557d2550962c928461..6573c1716bb07fd731f5a03426cf1fd4cedb84ba 100644 (file)
--- a/net/nfs.c
+++ b/net/nfs.c
 
 /*#define NFS_DEBUG*/
 
-#if ((CONFIG_COMMANDS & CFG_CMD_NET) && (CONFIG_COMMANDS & CFG_CMD_NFS))
+#if defined(CONFIG_CMD_NET) && defined(CONFIG_CMD_NFS)
 
 #define HASHES_PER_LINE 65     /* Number of "loading" hashes per line  */
-#define NFS_TIMEOUT 10
+#define NFS_RETRY_COUNT 30
+#define NFS_TIMEOUT 2UL
 
 static int fs_mounted = 0;
-static unsigned long rpc_id;
+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;
@@ -63,9 +65,10 @@ static char *nfs_filename;
 static char *nfs_path;
 static char nfs_path_buff[2048];
 
-static __inline__ void
+static __inline__ int
 store_block (uchar * src, unsigned offset, unsigned len)
 {
+       ulong newsize = offset + len;
 #ifdef CFG_DIRECT_FLASH_NFS
        int i, rc = 0;
 
@@ -81,15 +84,17 @@ store_block (uchar * src, unsigned offset, unsigned len)
                rc = flash_write ((uchar *)src, (ulong)(load_addr+offset), len);
                if (rc) {
                        flash_perror (rc);
-                       NetState = NETLOOP_FAIL;
-                       return;
+                       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*
@@ -119,17 +124,6 @@ dirname (char *path)
        return path;
 }
 
-/**************************************************************************
-RPC_INIT - set up the ID counter to something fairly random
-**************************************************************************/
-static void
-rpc_init (void)
-{
-       unsigned long t;
-       t=get_ticks();
-       rpc_id = t ^ (t << 8) ^ (t << 16);
-}
-
 /**************************************************************************
 RPC_ADD_CREDENTIALS - Add RPC authentication/verifier entries
 **************************************************************************/
@@ -186,7 +180,7 @@ rpc_req (int rpc_prog, int rpc_proc, uint32_t *data, int datalen)
        int pktlen;
        int sport;
 
-       id = rpc_id++;
+       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 */
@@ -200,7 +194,7 @@ rpc_req (int rpc_prog, int rpc_proc, uint32_t *data, int datalen)
 
        pktlen = (char *)p + datalen*sizeof(uint32_t) - (char *)&pkt;
 
-       memcpy ((char *)NetTxPacket+ETHER_HDR_SIZE+IP_HDR_SIZE, (char *)&pkt, pktlen);
+       memcpy ((char *)NetTxPacket + NetEthHdrSize() + IP_HDR_SIZE, (char *)&pkt, pktlen);
 
        if (rpc_prog == PROG_PORTMAP)
                sport = SUNRPC_PORT;
@@ -407,10 +401,13 @@ rpc_lookup_reply (int prog, uchar *pkt, unsigned len)
        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) {
@@ -436,6 +433,9 @@ nfs_mount_reply (uchar *pkt, unsigned len)
 
        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  ||
@@ -460,6 +460,9 @@ nfs_umountall_reply (uchar *pkt, unsigned len)
 
        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) {
@@ -483,6 +486,9 @@ nfs_lookup_reply (uchar *pkt, unsigned len)
 
        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  ||
@@ -507,6 +513,9 @@ nfs_readlink_reply (uchar *pkt, unsigned len)
 
        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  ||
@@ -539,8 +548,10 @@ nfs_read_reply (uchar *pkt, unsigned len)
        printf ("%s\n", __FUNCTION__);
 #endif
 
-       rlen = (uchar *)&(rpc_pkt.u.reply.data[19]) - (uchar *)&(rpc_pkt.u.reply.id);
-       memcpy ((uchar *)&rpc_pkt, pkt, rlen);
+       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 ||
@@ -563,7 +574,8 @@ nfs_read_reply (uchar *pkt, unsigned len)
        }
 
        rlen = ntohl(rpc_pkt.u.reply.data[18]);
-       store_block ((uchar *)&(rpc_pkt.u.reply.data[19]), nfs_offset, rlen);
+       if ( store_block ((uchar *)pkt+sizeof(rpc_pkt.u.reply), nfs_offset, rlen) )
+               return -9999;
 
        return rlen;
 }
@@ -572,6 +584,18 @@ nfs_read_reply (uchar *pkt, unsigned len)
 Interfaces of U-BOOT
 **************************************************************************/
 
+static void
+NfsTimeout (void)
+{
+       if ( NfsTimeoutCount++ < NFS_RETRY_COUNT ) {
+               NfsSend ();
+               return;
+       }
+       puts ("Timeout\n");
+       NetState = NETLOOP_FAIL;
+       return;
+}
+
 static void
 NfsHandler (uchar *pkt, unsigned dest, unsigned src, unsigned len)
 {
@@ -613,7 +637,8 @@ NfsHandler (uchar *pkt, unsigned dest, unsigned src, unsigned len)
                        puts ("*** ERROR: Cannot umount\n");
                        NetState = NETLOOP_FAIL;
                } else {
-                       NetState = NETLOOP_SUCCESS;
+                       puts ("\ndone\n");
+                       NetState = NfsDownloadState;
                }
                break;
 
@@ -649,6 +674,7 @@ NfsHandler (uchar *pkt, unsigned dest, unsigned src, unsigned len)
 
        case STATE_READ_REQ:
                rlen = nfs_read_reply (pkt, len);
+               NetSetTimeout (NFS_TIMEOUT * CFG_HZ, NfsTimeout);
                if (rlen > 0) {
                        nfs_offset += rlen;
                        NfsSend ();
@@ -658,9 +684,7 @@ NfsHandler (uchar *pkt, unsigned dest, unsigned src, unsigned len)
                        NfsState = STATE_READLINK_REQ;
                        NfsSend ();
                } else {
-                       puts ("\ndone\n");
-                       printf ("Bytes transferred = %d (%x hex)\n",
-                               nfs_offset, nfs_offset);
+                       if ( ! rlen ) NfsDownloadState = NETLOOP_SUCCESS;
                        NfsState = STATE_UMOUNT_REQ;
                        NfsSend ();
                }
@@ -668,13 +692,6 @@ NfsHandler (uchar *pkt, unsigned dest, unsigned src, unsigned len)
        }
 }
 
-static void
-NfsTimeout (void)
-{
-       puts ("Timeout\n");
-       NetState = NETLOOP_FAIL;
-       return;
-}
 
 void
 NfsStart (void)
@@ -682,6 +699,7 @@ NfsStart (void)
 #ifdef NFS_DEBUG
        printf ("%s\n", __FUNCTION__);
 #endif
+       NfsDownloadState = NETLOOP_FAIL;
 
        NfsServerIP = NetServerIP;
        nfs_path = (char *)nfs_path_buff;
@@ -693,13 +711,11 @@ NfsStart (void)
        }
 
        if (BootFile[0] == '\0') {
-               IPaddr_t OurIP = ntohl (NetOurIP);
-
                sprintf (default_filename, "/nfsroot/%02lX%02lX%02lX%02lX.img",
-                       OurIP & 0xFF,
-                       (OurIP >>  8) & 0xFF,
-                       (OurIP >> 16) & 0xFF,
-                       (OurIP >> 24) & 0xFF    );
+                       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",
@@ -738,24 +754,18 @@ NfsStart (void)
                        print_IPaddr (NetOurGatewayIP) ;
                }
        }
-       putc ('\n');
-
-       printf ("Filename '%s/%s'.", nfs_path, nfs_filename);
+       printf ("\nFilename '%s/%s'.", nfs_path, nfs_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);
-
-       printf ("Loading: *\b");
+       printf ("\nLoad address: 0x%lx\n"
+               "Loading: *\b", load_addr);
 
        NetSetTimeout (NFS_TIMEOUT * CFG_HZ, NfsTimeout);
        NetSetHandler (NfsHandler);
 
-       rpc_init ();
        NfsTimeoutCount = 0;
        NfsState = STATE_PRCLOOKUP_PROG_MOUNT_REQ;
 
@@ -769,4 +779,4 @@ NfsStart (void)
        NfsSend ();
 }
 
-#endif /* CONFIG_COMMANDS & CFG_CMD_NFS */
+#endif