]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Universal/Network/Dhcp4Dxe/Dhcp4Io.c
1. Fix a bug in PXE driver that the PXE boot do not restart if a new boot option...
[mirror_edk2.git] / MdeModulePkg / Universal / Network / Dhcp4Dxe / Dhcp4Io.c
index 0a43b606e15e0d12afb337e501fc7ad304cb0ccf..19ad101d5404d812998568727fdbb7175f43e72c 100644 (file)
@@ -1,8 +1,8 @@
 /** @file\r
   EFI DHCP protocol implementation.\r
   \r
-Copyright (c) 2006 - 2009, Intel Corporation.<BR>\r
-All rights reserved. This program and the accompanying materials\r
+Copyright (c) 2006 - 2012, Intel Corporation. All rights reserved.<BR>\r
+This program and the accompanying materials\r
 are licensed and made available under the terms and conditions of the BSD License\r
 which accompanies this distribution.  The full text of the license may be found at\r
 http://opensource.org/licenses/bsd-license.php\r
@@ -37,6 +37,11 @@ DhcpInitRequest (
 \r
   ASSERT ((DhcpSb->DhcpState == Dhcp4Init) || (DhcpSb->DhcpState == Dhcp4InitReboot));\r
 \r
+  //\r
+  // Clear initial time to make sure that elapsed-time is set to 0 for first Discover or REQUEST message.\r
+  //\r
+  DhcpSb->ActiveChild->ElaspedTime= 0;\r
+  \r
   if (DhcpSb->DhcpState == Dhcp4Init) {\r
     DhcpSetState (DhcpSb, Dhcp4Selecting, FALSE);\r
     Status = DhcpSendMessage (DhcpSb, NULL, NULL, DHCP_MSG_DISCOVER, NULL);\r
@@ -272,7 +277,7 @@ DhcpSetTransmitTimer (
 \r
   @param  DhcpSb                The DHCP service instance\r
   @param  Para                  The DHCP parameter extracted from the server's\r
-                                response.                             \r
+                                response.\r
 **/\r
 VOID\r
 DhcpComputeLease (\r
@@ -313,6 +318,7 @@ DhcpComputeLease (
 \r
 **/\r
 EFI_STATUS\r
+EFIAPI\r
 DhcpConfigLeaseIoPort (\r
   IN UDP_IO                 *UdpIo,\r
   IN VOID                   *Context\r
@@ -482,6 +488,11 @@ DhcpCleanLease (
   DhcpSb->CurRetry      = 0;\r
   DhcpSb->MaxRetries    = 0;\r
   DhcpSb->LeaseLife     = 0;\r
+\r
+  //\r
+  // Clean active config data.\r
+  //\r
+  DhcpCleanConfigure (&DhcpSb->ActiveConfig);\r
 }\r
 \r
 \r
@@ -942,6 +953,7 @@ ON_EXIT:
 \r
 **/\r
 VOID\r
+EFIAPI\r
 DhcpInput (\r
   NET_BUF                   *UdpPacket,\r
   UDP_END_POINT             *EndPoint,\r
@@ -960,11 +972,11 @@ DhcpInput (
   DhcpSb  = (DHCP_SERVICE *) Context;\r
 \r
   //\r
-  // Don't restart receive if error occurs or DHCP is destoried.\r
+  // Don't restart receive if error occurs or DHCP is destroyed.\r
   //\r
   if (EFI_ERROR (IoStatus)) {\r
     return ;\r
-  } else if (DhcpSb->ServiceState == DHCP_DESTORY) {\r
+  } else if (DhcpSb->ServiceState == DHCP_DESTROY) {\r
     NetbufFree (UdpPacket);\r
     return ;\r
   }\r
@@ -1095,6 +1107,7 @@ RESTART:
 \r
 **/\r
 VOID\r
+EFIAPI\r
 DhcpReleasePacket (\r
   IN VOID                   *Arg\r
   )\r
@@ -1113,6 +1126,7 @@ DhcpReleasePacket (
 \r
 **/\r
 VOID\r
+EFIAPI\r
 DhcpOnPacketSent (\r
   NET_BUF                   *Packet,\r
   UDP_END_POINT             *EndPoint,\r
@@ -1209,6 +1223,17 @@ DhcpSendMessage (
   EFI_IP4 (Head->ClientAddr) = HTONL (DhcpSb->ClientAddr);\r
   CopyMem (Head->ClientHwAddr, DhcpSb->Mac.Addr, DhcpSb->HwLen);\r
 \r
+  if ((Type == DHCP_MSG_DECLINE) || (Type == DHCP_MSG_RELEASE)) {\r
+    Head->Seconds = 0;\r
+  } else if ((Type == DHCP_MSG_REQUEST) && (DhcpSb->DhcpState == Dhcp4Requesting)) {\r
+    //\r
+    // Use the same value as the original DHCPDISCOVER message.\r
+    //\r
+    Head->Seconds = DhcpSb->LastPacket->Dhcp4.Header.Seconds;\r
+  } else {\r
+    SetElapsedTime(&Head->Seconds, DhcpSb->ActiveChild);\r
+  }\r
+\r
   //\r
   // Append the DHCP message type\r
   //\r
@@ -1420,7 +1445,12 @@ DhcpRetransmit (
 \r
   ASSERT (DhcpSb->LastPacket != NULL);\r
 \r
-  DhcpSb->LastPacket->Dhcp4.Header.Seconds = HTONS (*(UINT16 *)(&DhcpSb->LastTimeout));\r
+  //\r
+  // For REQUEST message in Dhcp4Requesting state, do not change the secs fields.\r
+  //\r
+  if (DhcpSb->DhcpState != Dhcp4Requesting) {\r
+    SetElapsedTime(&DhcpSb->LastPacket->Dhcp4.Header.Seconds, DhcpSb->ActiveChild);\r
+  }\r
 \r
   //\r
   // Wrap it into a netbuf then send it.\r
@@ -1493,6 +1523,13 @@ DhcpOnTimerTick (
 \r
   DhcpSb   = (DHCP_SERVICE *) Context;\r
   Instance = DhcpSb->ActiveChild;\r
+\r
+  //\r
+  // 0xffff is the maximum supported value for elapsed time according to RFC.\r
+  //\r
+  if (Instance != NULL && Instance->ElaspedTime < 0xffff) {\r
+    Instance->ElaspedTime++;\r
+  }\r
   \r
   //\r
   // Check the retransmit timer\r
@@ -1507,8 +1544,10 @@ DhcpOnTimerTick (
       Status = DhcpChooseOffer (DhcpSb);\r
 \r
       if (EFI_ERROR(Status)) {\r
-        FreePool (DhcpSb->LastOffer);\r
-        DhcpSb->LastOffer = NULL;\r
+        if (DhcpSb->LastOffer != NULL) {\r
+          FreePool (DhcpSb->LastOffer);\r
+          DhcpSb->LastOffer = NULL;\r
+        }\r
       } else {\r
         goto ON_EXIT;\r
       }\r
@@ -1582,6 +1621,10 @@ DhcpOnTimerTick (
         goto END_SESSION;\r
       }\r
 \r
+      if (Instance != NULL) {\r
+        Instance->ElaspedTime= 0;\r
+      }      \r
+      \r
       Status = DhcpSendMessage (\r
                  DhcpSb,\r
                  DhcpSb->Selected,\r
@@ -1602,6 +1645,10 @@ DhcpOnTimerTick (
         goto END_SESSION;\r
       }\r
 \r
+      if (Instance != NULL) {\r
+        Instance->ElaspedTime= 0;\r
+      }    \r
+\r
       Status = DhcpSendMessage (\r
                  DhcpSb,\r
                  DhcpSb->Selected,\r