]> git.kernelconcepts.de Git - karo-tx-uboot.git/blobdiff - net/bootp.c
pxe: Allow use of environment variables in append string
[karo-tx-uboot.git] / net / bootp.c
index 0f0867b1c492dd65bbae9575c55636179f85aae5..a4f6db570c6c956db2e4e18d94b6923f5458ff4e 100644 (file)
 #include <command.h>
 #include <net.h>
 #include "bootp.h"
-#include "net_rand.h"
 #include "tftp.h"
 #include "nfs.h"
 #ifdef CONFIG_STATUS_LED
 #include <status_led.h>
 #endif
+#ifdef CONFIG_BOOTP_RANDOM_DELAY
+#include "net_rand.h"
+#endif
 
 #define BOOTP_VENDOR_MAGIC     0x63825363      /* RFC1048 Magic Cookie */
 
-#define TIMEOUT                5000UL  /* Milliseconds before trying BOOTP again */
+/*
+ * The timeout for the initial BOOTP/DHCP request used to be described by a
+ * counter of fixed-length timeout periods. TIMEOUT_COUNT represents
+ * that counter
+ *
+ * Now that the timeout periods are variable (exponential backoff and retry)
+ * we convert the timeout count to the absolute time it would have take to
+ * execute that many retries, and keep sending retry packets until that time
+ * is reached.
+ */
 #ifndef CONFIG_NET_RETRY_COUNT
 # define TIMEOUT_COUNT 5               /* # of timeouts before giving up */
 #else
 # define TIMEOUT_COUNT (CONFIG_NET_RETRY_COUNT)
 #endif
+#define TIMEOUT_MS     ((3 + (TIMEOUT_COUNT * 5)) * 1000)
 
 #define PORT_BOOTPS    67              /* BOOTP server UDP port */
 #define PORT_BOOTPC    68              /* BOOTP client UDP port */
 
 ulong          BootpID;
 int            BootpTry;
+ulong          bootp_start;
+ulong          bootp_timeout;
 
 #if defined(CONFIG_CMD_DHCP)
-dhcp_state_t dhcp_state = INIT;
-unsigned long dhcp_leasetime;
-IPaddr_t NetDHCPServerIP;
+static dhcp_state_t dhcp_state = INIT;
+static unsigned long dhcp_leasetime;
+static IPaddr_t NetDHCPServerIP;
 static void DhcpHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src,
                        unsigned len);
 
@@ -325,20 +339,34 @@ BootpHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src,
 static void
 BootpTimeout(void)
 {
-       if (BootpTry >= TIMEOUT_COUNT) {
+       ulong time_taken = get_timer(bootp_start);
+
+       if (time_taken >= TIMEOUT_MS) {
 #ifdef CONFIG_BOOTP_MAY_FAIL
-               puts("\nRetry count exceeded\n");
-               NetSetState(NETLOOP_FAIL);
+               puts("\nRetry time exceeded\n");
+               net_set_state(NETLOOP_FAIL);
 #else
-               puts("\nRetry count exceeded; starting again\n");
+               puts("\nRetry time exceeded; starting again\n");
                NetStartAgain();
 #endif
        } else {
-               NetSetTimeout(TIMEOUT, BootpTimeout);
+               bootp_timeout *= 2;
+               if (bootp_timeout > 1000)
+                       bootp_timeout = 1000;
+               NetSetTimeout(bootp_timeout, BootpTimeout);
                BootpRequest();
        }
 }
 
+#define put_vci(e, str)                                                \
+       do {                                                    \
+               size_t vci_strlen = strlen(str);                \
+               *e++ = 60;      /* Vendor Class Identifier */   \
+               *e++ = vci_strlen;                              \
+               memcpy(e, str, vci_strlen);                     \
+               e += vci_strlen;                                \
+       } while (0)
+
 /*
  *     Initialize BOOTP extension fields in the request.
  */
@@ -350,7 +378,6 @@ static int DhcpExtended(u8 *e, int message_type, IPaddr_t ServerID,
        u8 *cnt;
 #if defined(CONFIG_BOOTP_PXE)
        char *uuid;
-       size_t vci_strlen;
        u16 clientarch;
 #endif
 
@@ -429,18 +456,16 @@ static int DhcpExtended(u8 *e, int message_type, IPaddr_t ServerID,
                        *e++ = 17;
                        *e++ = 0;       /* type 0 - UUID */
 
-                       uuid_str_to_bin(uuid, e);
+                       uuid_str_to_bin(uuid, e, UUID_STR_FORMAT_STD);
                        e += 16;
                } else {
                        printf("Invalid pxeuuid: %s\n", uuid);
                }
        }
+#endif
 
-       *e++ = 60;      /* Vendor Class Identifier */
-       vci_strlen = strlen(CONFIG_BOOTP_VCI_STRING);
-       *e++ = vci_strlen;
-       memcpy(e, CONFIG_BOOTP_VCI_STRING, vci_strlen);
-       e += vci_strlen;
+#ifdef CONFIG_BOOTP_VCI_STRING
+       put_vci(e, CONFIG_BOOTP_VCI_STRING);
 #endif
 
 #if defined(CONFIG_BOOTP_VENDOREX)
@@ -527,6 +552,15 @@ static int BootpExtended(u8 *e)
        *e++ = (576 - 312 + OPT_FIELD_SIZE) & 0xff;
 #endif
 
+#if defined(CONFIG_BOOTP_VCI_STRING) || \
+       (defined(CONFIG_SPL_BUILD) && defined(CONFIG_SPL_NET_VCI_STRING))
+#ifdef CONFIG_SPL_BUILD
+       put_vci(e, CONFIG_SPL_NET_VCI_STRING);
+#else
+       put_vci(e, CONFIG_BOOTP_VCI_STRING);
+#endif
+#endif
+
 #if defined(CONFIG_BOOTP_SUBNETMASK)
        *e++ = 1;               /* Subnet mask request */
        *e++ = 4;
@@ -580,6 +614,13 @@ static int BootpExtended(u8 *e)
 }
 #endif
 
+void BootpReset(void)
+{
+       BootpTry = 0;
+       bootp_start = get_timer(0);
+       bootp_timeout = 10;
+}
+
 void
 BootpRequest(void)
 {
@@ -588,7 +629,7 @@ BootpRequest(void)
        int extlen, pktlen, iplen;
        int eth_hdr_size;
 #ifdef CONFIG_BOOTP_RANDOM_DELAY
-       ulong i, rand_ms;
+       ulong rand_ms;
 #endif
 
        bootstage_mark_name(BOOTSTAGE_ID_BOOTP_START, "bootp_start");
@@ -606,8 +647,7 @@ BootpRequest(void)
                rand_ms = rand() >> 19;
 
        printf("Random delay: %ld ms...\n", rand_ms);
-       for (i = 0; i < rand_ms; i++)
-               udelay(1000); /*Wait 1ms*/
+       mdelay(rand_ms);
 
 #endif /* CONFIG_BOOTP_RANDOM_DELAY */
 
@@ -669,7 +709,7 @@ BootpRequest(void)
        iplen = BOOTP_HDR_SIZE - OPT_FIELD_SIZE + extlen;
        pktlen = eth_hdr_size + IP_UDP_HDR_SIZE + iplen;
        net_set_udp_header(iphdr, 0xFFFFFFFFL, PORT_BOOTPS, PORT_BOOTPC, iplen);
-       NetSetTimeout(SELECT_TIMEOUT, BootpTimeout);
+       NetSetTimeout(bootp_timeout, BootpTimeout);
 
 #if defined(CONFIG_CMD_DHCP)
        dhcp_state = SELECTING;
@@ -726,6 +766,8 @@ static void DhcpOptionsProcess(uchar *popt, struct Bootp_t *bp)
                        memcpy(&NetOurRootPath, popt + 2, size);
                        NetOurRootPath[size] = 0;
                        break;
+               case 28:        /* Ignore Broadcast Address Option */
+                       break;
 #if defined(CONFIG_CMD_SNTP) && defined(CONFIG_BOOTP_NTPSERVER)
                case 42:        /* NTP server IP */
                        NetCopyIP(&NetNtpServerIP, (popt + 2));
@@ -900,7 +942,7 @@ DhcpHandler(uchar *pkt, unsigned dest, IPaddr_t sip, unsigned src,
                                                htonl(BOOTP_VENDOR_MAGIC))
                                DhcpOptionsProcess((u8 *)&bp->bp_vend[4], bp);
 
-                       NetSetTimeout(TIMEOUT, BootpTimeout);
+                       NetSetTimeout(5000, BootpTimeout);
                        DhcpSendRequestPkt(bp);
 #ifdef CONFIG_SYS_BOOTFILE_PREFIX
                }