ARM: dts: ast2600: Disable I2C nodes by default
[platform/kernel/u-boot.git] / net / tftp.c
index 84e970b..bfc4c9b 100644 (file)
@@ -14,6 +14,7 @@
 #include <log.h>
 #include <mapmem.h>
 #include <net.h>
+#include <asm/global_data.h>
 #include <net/tftp.h>
 #include "bootp.h"
 #ifdef CONFIG_SYS_DIRECT_FLASH_TFTP
@@ -26,12 +27,6 @@ DECLARE_GLOBAL_DATA_PTR;
 #define WELL_KNOWN_PORT        69
 /* Millisecs to timeout for lost pkt */
 #define TIMEOUT                5000UL
-#ifndef        CONFIG_NET_RETRY_COUNT
-/* # of timeouts before giving up */
-# define TIMEOUT_COUNT 10
-#else
-# define TIMEOUT_COUNT  (CONFIG_NET_RETRY_COUNT * 2)
-#endif
 /* Number of "loading" hashes per line (for checking the image size) */
 #define HASHES_PER_LINE        65
 
@@ -46,7 +41,7 @@ DECLARE_GLOBAL_DATA_PTR;
 #define TFTP_OACK      6
 
 static ulong timeout_ms = TIMEOUT;
-static int timeout_count_max = TIMEOUT_COUNT;
+static int timeout_count_max = (CONFIG_NET_RETRY_COUNT * 2);
 static ulong time_start;   /* Record time we started tftp */
 
 /*
@@ -59,7 +54,7 @@ static ulong time_start;   /* Record time we started tftp */
  * non-standard timeout behavior when initiating a TFTP transfer.
  */
 ulong tftp_timeout_ms = TIMEOUT;
-int tftp_timeout_count_max = TIMEOUT_COUNT;
+int tftp_timeout_count_max = (CONFIG_NET_RETRY_COUNT * 2);
 
 enum {
        TFTP_ERR_UNDEFINED           = 0,
@@ -159,7 +154,8 @@ static unsigned short tftp_window_size_option = TFTP_WINDOWSIZE;
 
 static inline int store_block(int block, uchar *src, unsigned int len)
 {
-       ulong offset = block * tftp_block_size + tftp_block_wrap_offset;
+       ulong offset = block * tftp_block_size + tftp_block_wrap_offset -
+                       tftp_block_size;
        ulong newsize = offset + len;
        ulong store_addr = tftp_load_addr + offset;
 #ifdef CONFIG_SYS_DIRECT_FLASH_TFTP
@@ -228,12 +224,13 @@ static void new_transfer(void)
  * @param block        Block number to send
  * @param dst  Destination buffer for data
  * @param len  Number of bytes in block (this one and every other)
- * @return number of bytes loaded
+ * Return: number of bytes loaded
  */
 static int load_block(unsigned block, uchar *dst, unsigned len)
 {
        /* We may want to get the final block from the previous set */
-       ulong offset = ((int)block - 1) * len + tftp_block_wrap_offset;
+       ulong offset = block * tftp_block_size + tftp_block_wrap_offset -
+                      tftp_block_size;
        ulong tosend = len;
 
        tosend = min(net_boot_file_size - offset, tosend);
@@ -327,6 +324,12 @@ static void tftp_complete(void)
                        time_start * 1000, "/s");
        }
        puts("\ndone\n");
+       if (IS_ENABLED(CONFIG_CMD_BOOTEFI)) {
+               if (!tftp_put_active)
+                       efi_set_bootdev("Net", "", tftp_filename,
+                                       map_sysmem(tftp_load_addr, 0),
+                                       net_boot_file_size);
+       }
        net_set_state(NETLOOP_SUCCESS);
 }
 
@@ -502,6 +505,7 @@ static void tftp_handler(uchar *pkt, unsigned dest, struct in_addr sip,
                                int block = ntohs(*s);
                                int ack_ok = (tftp_cur_block == block);
 
+                               tftp_prev_block = tftp_cur_block;
                                tftp_cur_block = (unsigned short)(block + 1);
                                update_block_number();
                                if (ack_ok)
@@ -544,8 +548,7 @@ static void tftp_handler(uchar *pkt, unsigned dest, struct in_addr sip,
                for (i = 0; i+8 < len; i++) {
                        if (strcasecmp((char *)pkt + i, "blksize") == 0) {
                                tftp_block_size = (unsigned short)
-                                       simple_strtoul((char *)pkt + i + 8,
-                                                      NULL, 10);
+                                       dectoul((char *)pkt + i + 8, NULL);
                                debug("Blocksize oack: %s, %d\n",
                                      (char *)pkt + i + 8, tftp_block_size);
                                if (tftp_block_size > tftp_block_size_option) {
@@ -556,8 +559,7 @@ static void tftp_handler(uchar *pkt, unsigned dest, struct in_addr sip,
                        }
                        if (strcasecmp((char *)pkt + i, "timeout") == 0) {
                                timeout_val_rcvd = (unsigned short)
-                                       simple_strtoul((char *)pkt + i + 8,
-                                                      NULL, 10);
+                                       dectoul((char *)pkt + i + 8, NULL);
                                debug("Timeout oack: %s, %d\n",
                                      (char *)pkt + i + 8, timeout_val_rcvd);
                                if (timeout_val_rcvd != (timeout_ms / 1000)) {
@@ -568,16 +570,15 @@ static void tftp_handler(uchar *pkt, unsigned dest, struct in_addr sip,
                        }
 #ifdef CONFIG_TFTP_TSIZE
                        if (strcasecmp((char *)pkt + i, "tsize") == 0) {
-                               tftp_tsize = simple_strtoul((char *)pkt + i + 6,
-                                                          NULL, 10);
+                               tftp_tsize = dectoul((char *)pkt + i + 6,
+                                                    NULL);
                                debug("size = %s, %d\n",
                                      (char *)pkt + i + 6, tftp_tsize);
                        }
 #endif
                        if (strcasecmp((char *)pkt + i,  "windowsize") == 0) {
                                tftp_windowsize =
-                                       simple_strtoul((char *)pkt + i + 11,
-                                                      NULL, 10);
+                                       dectoul((char *)pkt + i + 11, NULL);
                                debug("windowsize = %s, %d\n",
                                      (char *)pkt + i + 11, tftp_windowsize);
                        }
@@ -621,8 +622,10 @@ static void tftp_handler(uchar *pkt, unsigned dest, struct in_addr sip,
                tftp_cur_block++;
                tftp_cur_block %= TFTP_SEQUENCE_SIZE;
 
-               if (tftp_state == STATE_SEND_RRQ)
+               if (tftp_state == STATE_SEND_RRQ) {
                        debug("Server did not acknowledge any options!\n");
+                       tftp_next_ack = tftp_windowsize;
+               }
 
                if (tftp_state == STATE_SEND_RRQ || tftp_state == STATE_OACK ||
                    tftp_state == STATE_RECV_WRQ) {
@@ -651,12 +654,18 @@ static void tftp_handler(uchar *pkt, unsigned dest, struct in_addr sip,
                timeout_count_max = tftp_timeout_count_max;
                net_set_timeout_handler(timeout_ms, tftp_timeout_handler);
 
-               if (store_block(tftp_cur_block - 1, pkt + 2, len)) {
+               if (store_block(tftp_cur_block, pkt + 2, len)) {
                        eth_halt();
                        net_set_state(NETLOOP_FAIL);
                        break;
                }
 
+               if (len < tftp_block_size) {
+                       tftp_send();
+                       tftp_complete();
+                       break;
+               }
+
                /*
                 *      Acknowledge the block just received, which will prompt
                 *      the remote for the next one.
@@ -665,11 +674,6 @@ static void tftp_handler(uchar *pkt, unsigned dest, struct in_addr sip,
                        tftp_send();
                        tftp_next_ack += tftp_windowsize;
                }
-
-               if (len < tftp_block_size) {
-                       tftp_send();
-                       tftp_complete();
-               }
                break;
 
        case TFTP_ERROR:
@@ -838,9 +842,6 @@ void tftp_start(enum proto_t protocol)
                printf("Load address: 0x%lx\n", tftp_load_addr);
                puts("Loading: *\b");
                tftp_state = STATE_SEND_RRQ;
-#ifdef CONFIG_CMD_BOOTEFI
-               efi_set_bootdev("Net", "", tftp_filename);
-#endif
        }
 
        time_start = get_timer(0);
@@ -905,6 +906,8 @@ void tftp_start_server(void)
        tftp_block_size = TFTP_BLOCK_SIZE;
        tftp_cur_block = 0;
        tftp_our_port = WELL_KNOWN_PORT;
+       tftp_windowsize = 1;
+       tftp_next_ack = tftp_windowsize;
 
 #ifdef CONFIG_TFTP_TSIZE
        tftp_tsize = 0;
@@ -918,4 +921,3 @@ void tftp_start_server(void)
        memset(net_server_ethaddr, 0, 6);
 }
 #endif /* CONFIG_CMD_TFTPSRV */
-