]> git.proxmox.com Git - mirror_edk2.git/blobdiff - NetworkPkg/UefiPxeBcDxe/PxeBcImpl.c
NetworkPkg: reset DHCP child when leaving PXE LoadFile.
[mirror_edk2.git] / NetworkPkg / UefiPxeBcDxe / PxeBcImpl.c
index 7e5b4d63fdf47e461ed08b57cff7cc1a8430c372..12e5566a7913b1804551f69b23c970935758c0c0 100644 (file)
@@ -1,7 +1,7 @@
 /** @file\r
   This implementation of EFI_PXE_BASE_CODE_PROTOCOL and EFI_LOAD_FILE_PROTOCOL.\r
 \r
-  Copyright (c) 2007 - 2010, Intel Corporation. All rights reserved.<BR>\r
+  Copyright (c) 2007 - 2015, Intel Corporation. All rights reserved.<BR>\r
 \r
   This program and the accompanying materials\r
   are licensed and made available under the terms and conditions of the BSD License\r
@@ -81,6 +81,17 @@ EfiPxeBcStart (
 \r
   if (Mode->UsingIpv6) {\r
     AsciiPrint ("\n>>Start PXE over IPv6");\r
+    //\r
+    // Configure udp6 instance to receive data.\r
+    //\r
+    Status = Private->Udp6Read->Configure (\r
+                                  Private->Udp6Read,\r
+                                  &Private->Udp6CfgData\r
+                                  );\r
+    if (EFI_ERROR (Status)) {\r
+      goto ON_ERROR;\r
+    }\r
+    \r
     //\r
     // Configure block size for TFTP as a default value to handle all link layers.\r
     //\r
@@ -113,8 +124,27 @@ EfiPxeBcStart (
     if (EFI_ERROR (Status)) {\r
       goto ON_ERROR;\r
     }\r
+\r
+    //\r
+    // Set Ip6 policy to Automatic to start the IP6 router discovery.\r
+    //\r
+    Status = PxeBcSetIp6Policy (Private);\r
+    if (EFI_ERROR (Status)) {\r
+      goto ON_ERROR;\r
+    }\r
   } else {\r
     AsciiPrint ("\n>>Start PXE over IPv4");\r
+    //\r
+    // Configure udp4 instance to receive data.\r
+    //\r
+    Status = Private->Udp4Read->Configure (\r
+                                  Private->Udp4Read,\r
+                                  &Private->Udp4CfgData\r
+                                  );\r
+    if (EFI_ERROR (Status)) {\r
+      goto ON_ERROR;\r
+    }\r
+    \r
     //\r
     // Configure block size for TFTP as a default value to handle all link layers.\r
     //\r
@@ -174,6 +204,18 @@ EfiPxeBcStart (
     if (EFI_ERROR (Status)) {\r
       goto ON_ERROR;\r
     }\r
+\r
+    //\r
+    //DHCP4 service allows only one of its children to be configured in  \r
+    //the active state, If the DHCP4 D.O.R.A started by IP4 auto  \r
+    //configuration and has not been completed, the Dhcp4 state machine \r
+    //will not be in the right state for the PXE to start a new round D.O.R.A. \r
+    //so we need to switch it's policy to static.\r
+    //\r
+    Status = PxeBcSetIp4Policy (Private);\r
+    if (EFI_ERROR (Status)) {\r
+      goto ON_ERROR;\r
+    }\r
   }\r
 \r
   //\r
@@ -316,11 +358,18 @@ EfiPxeBcStop (
       gBS->CloseEvent (Private->IcmpToken.Event);\r
       Private->IcmpToken.Event = NULL;\r
     }\r
+    Private->BootFileName = NULL;\r
   }\r
 \r
   gBS->CloseEvent (Private->UdpTimeOutEvent);\r
   Private->CurSrcPort   = 0;\r
   Private->BootFileSize = 0;\r
+  Private->SolicitTimes = 0;\r
+  Private->ElapsedTime  = 0;\r
+  ZeroMem (&Private->StationIp, sizeof (EFI_IP_ADDRESS));\r
+  ZeroMem (&Private->SubnetMask, sizeof (EFI_IP_ADDRESS));\r
+  ZeroMem (&Private->GatewayIp, sizeof (EFI_IP_ADDRESS));\r
+  ZeroMem (&Private->ServerIp, sizeof (EFI_IP_ADDRESS));\r
 \r
   //\r
   // Reset the mode data.\r
@@ -386,6 +435,8 @@ EfiPxeBcDhcp (
   Mode->IcmpErrorReceived = FALSE;\r
   Private->Function       = EFI_PXE_BASE_CODE_FUNCTION_DHCP;\r
   Private->IsOfferSorted  = SortOffers;\r
+  Private->SolicitTimes   = 0;\r
+  Private->ElapsedTime    = 0;\r
 \r
   if (!Mode->Started) {\r
     return EFI_NOT_STARTED;\r
@@ -415,6 +466,14 @@ EfiPxeBcDhcp (
     Status = PxeBcDhcp4Dora (Private, Private->Dhcp4);\r
   }\r
 \r
+  //\r
+  // Reconfigure the UDP instance with the default configuration.\r
+  //\r
+  if (Mode->UsingIpv6) {\r
+    Private->Udp6Read->Configure (Private->Udp6Read, &Private->Udp6CfgData);\r
+  } else {\r
+    Private->Udp4Read->Configure (Private->Udp4Read, &Private->Udp4CfgData);\r
+  }\r
   //\r
   // Dhcp(), Discover(), and Mtftp() set the IP filter, and return with the IP\r
   // receive filter list emptied and the filter set to EFI_PXE_BASE_CODE_IP_FILTER_STATION_IP.\r
@@ -489,6 +548,7 @@ EfiPxeBcDiscover (
   UINT16                          Index;\r
   EFI_STATUS                      Status;\r
   EFI_PXE_BASE_CODE_IP_FILTER     IpFilter;\r
+  EFI_PXE_BASE_CODE_DISCOVER_INFO *NewCreatedInfo;\r
 \r
   if (This == NULL) {\r
     return EFI_INVALID_PARAMETER;\r
@@ -501,6 +561,7 @@ EfiPxeBcDiscover (
   SrvList                 = NULL;\r
   Status                  = EFI_DEVICE_ERROR;\r
   Private->Function       = EFI_PXE_BASE_CODE_FUNCTION_DISCOVER;\r
+  NewCreatedInfo          = NULL;\r
 \r
   if (!Mode->Started) {\r
     return EFI_NOT_STARTED;\r
@@ -530,6 +591,7 @@ EfiPxeBcDiscover (
   //\r
   // There are 3 methods to get the information for discover.\r
   //\r
+  ZeroMem (&DefaultInfo, sizeof (EFI_PXE_BASE_CODE_DISCOVER_INFO));\r
   if (*Layer != EFI_PXE_BASE_CODE_BOOT_LAYER_INITIAL) {\r
     //\r
     // 1. Take the previous setting as the discover info.\r
@@ -554,12 +616,13 @@ EfiPxeBcDiscover (
     //\r
     // 2. Extract the discover information from the cached packets if unspecified.\r
     //\r
-    Info   = &DefaultInfo;\r
-    Status = PxeBcExtractDiscoverInfo (Private, Type, Info, &BootSvrEntry, &SrvList);\r
+    NewCreatedInfo = &DefaultInfo;\r
+    Status = PxeBcExtractDiscoverInfo (Private, Type, &NewCreatedInfo, &BootSvrEntry, &SrvList);\r
     if (EFI_ERROR (Status)) {\r
       goto ON_EXIT;\r
     }\r
-\r
+    ASSERT (NewCreatedInfo != NULL);\r
+    Info = NewCreatedInfo;\r
   } else {\r
     //\r
     // 3. Take the pass-in information as the discover info, and validate the server list.\r
@@ -575,7 +638,7 @@ EfiPxeBcDiscover (
       if (Index != Info->IpCnt) {\r
         //\r
         // It's invalid if the first server doesn't accecpt any response\r
-        // and meanwhile any of the rest servers accept any reponse.\r
+        // but any of the other servers does accept any response.\r
         //\r
         Status = EFI_INVALID_PARAMETER;\r
         goto ON_EXIT;\r
@@ -594,30 +657,7 @@ EfiPxeBcDiscover (
 \r
   Private->IsDoDiscover = TRUE;\r
 \r
-  if (Info->UseUCast) {\r
-    //\r
-    // Do discover by unicast.\r
-    //\r
-    for (Index = 0; Index < Info->IpCnt; Index++) {\r
-      if (BootSvrEntry == NULL) {\r
-        CopyMem (&Private->ServerIp, &SrvList[Index].IpAddr, sizeof (EFI_IP_ADDRESS));\r
-      } else {\r
-        ASSERT (!Mode->UsingIpv6);\r
-        ZeroMem (&Private->ServerIp, sizeof (EFI_IP_ADDRESS));\r
-        CopyMem (&Private->ServerIp, &BootSvrEntry->IpAddr[Index], sizeof (EFI_IPv4_ADDRESS));\r
-      }\r
-\r
-      Status = PxeBcDiscoverBootServer (\r
-                 Private,\r
-                 Type,\r
-                 Layer,\r
-                 UseBis,\r
-                 &SrvList[Index].IpAddr,\r
-                 0,\r
-                 NULL\r
-                 );\r
-    }\r
-  } else if (Info->UseMCast) {\r
+  if (Info->UseMCast) {\r
     //\r
     // Do discover by multicast.\r
     //\r
@@ -627,8 +667,8 @@ EfiPxeBcDiscover (
                Layer,\r
                UseBis,\r
                &Info->ServerMCastIp,\r
-               0,\r
-               NULL\r
+               Info->IpCnt,\r
+               SrvList\r
                );\r
 \r
   } else if (Info->UseBCast) {\r
@@ -645,6 +685,30 @@ EfiPxeBcDiscover (
                Info->IpCnt,\r
                SrvList\r
                );\r
+\r
+  } else if (Info->UseUCast) {\r
+    //\r
+    // Do discover by unicast.\r
+    //\r
+    for (Index = 0; Index < Info->IpCnt; Index++) {\r
+      if (BootSvrEntry == NULL) {\r
+        CopyMem (&Private->ServerIp, &SrvList[Index].IpAddr, sizeof (EFI_IP_ADDRESS));\r
+      } else {\r
+        ASSERT (!Mode->UsingIpv6);\r
+        ZeroMem (&Private->ServerIp, sizeof (EFI_IP_ADDRESS));\r
+        CopyMem (&Private->ServerIp, &BootSvrEntry->IpAddr[Index], sizeof (EFI_IPv4_ADDRESS));\r
+      }\r
+\r
+      Status = PxeBcDiscoverBootServer (\r
+                 Private,\r
+                 Type,\r
+                 Layer,\r
+                 UseBis,\r
+                 &Private->ServerIp,\r
+                 Info->IpCnt,\r
+                 SrvList\r
+                 );\r
+      }\r
   }\r
 \r
   if (!EFI_ERROR (Status)) {\r
@@ -656,8 +720,8 @@ EfiPxeBcDiscover (
       if (!EFI_ERROR (Status)) {\r
         CopyMem (\r
           &Mode->PxeReply.Dhcpv6,\r
-          &Private->PxeReply.Dhcp6.Packet.Offer,\r
-          Private->PxeReply.Dhcp6.Packet.Offer.Length\r
+          &Private->PxeReply.Dhcp6.Packet.Ack.Dhcp6,\r
+          Private->PxeReply.Dhcp6.Packet.Ack.Length\r
           );\r
         Mode->PxeReplyReceived = TRUE;\r
         Mode->PxeDiscoverValid = TRUE;\r
@@ -667,8 +731,8 @@ EfiPxeBcDiscover (
       if (!EFI_ERROR (Status)) {\r
         CopyMem (\r
           &Mode->PxeReply.Dhcpv4,\r
-          &Private->PxeReply.Dhcp4.Packet.Offer,\r
-          Private->PxeReply.Dhcp4.Packet.Offer.Length\r
+          &Private->PxeReply.Dhcp4.Packet.Ack.Dhcp4,\r
+          Private->PxeReply.Dhcp4.Packet.Ack.Length\r
           );\r
         Mode->PxeReplyReceived = TRUE;\r
         Mode->PxeDiscoverValid = TRUE;\r
@@ -678,6 +742,16 @@ EfiPxeBcDiscover (
 \r
 ON_EXIT:\r
 \r
+  if (NewCreatedInfo != NULL && NewCreatedInfo != &DefaultInfo) {\r
+    FreePool (NewCreatedInfo);\r
+  }\r
+  \r
+  if (Mode->UsingIpv6) {\r
+    Private->Udp6Read->Configure (Private->Udp6Read, &Private->Udp6CfgData);\r
+  } else {\r
+    Private->Udp4Read->Configure (Private->Udp4Read, &Private->Udp4CfgData);\r
+  }\r
+  \r
   //\r
   // Dhcp(), Discover(), and Mtftp() set the IP filter, and return with the IP\r
   // receive filter list emptied and the filter set to EFI_PXE_BASE_CODE_IP_FILTER_STATION_IP.\r
@@ -909,6 +983,14 @@ EfiPxeBcMtftp (
     Mode->IcmpErrorReceived = TRUE;\r
   }\r
 \r
+  //\r
+  // Reconfigure the UDP instance with the default configuration.\r
+  //\r
+  if (Mode->UsingIpv6) {\r
+    Private->Udp6Read->Configure (Private->Udp6Read, &Private->Udp6CfgData);\r
+  } else {\r
+    Private->Udp4Read->Configure (Private->Udp4Read, &Private->Udp4CfgData);\r
+  }\r
   //\r
   // Dhcp(), Discover(), and Mtftp() set the IP filter, and return with the IP\r
   // receive filter list emptied and the filter set to EFI_PXE_BASE_CODE_IP_FILTER_STATION_IP.\r
@@ -1188,8 +1270,14 @@ EfiPxeBcUdpRead (
   BOOLEAN                     IsDone;\r
   BOOLEAN                     IsMatched;\r
   UINTN                       CopiedLen;\r
+  UINTN                       HeaderLen;\r
+  UINTN                       HeaderCopiedLen;\r
+  UINTN                       BufferCopiedLen;\r
+  UINT32                      FragmentLength;\r
+  UINTN                       FragmentIndex;\r
+  UINT8                       *FragmentBuffer;\r
 \r
-  if (This == NULL || DestIp == NULL || DestPort == NULL) {\r
+  if (This == NULL) {\r
     return EFI_INVALID_PARAMETER;\r
   }\r
 \r
@@ -1200,9 +1288,9 @@ EfiPxeBcUdpRead (
   Udp4Rx    = NULL;\r
   Udp6Rx    = NULL;\r
 \r
-  if (((OpFlags & EFI_PXE_BASE_CODE_UDP_OPFLAGS_ANY_DEST_PORT) != 0 && DestPort == NULL) ||\r
-      ((OpFlags & EFI_PXE_BASE_CODE_UDP_OPFLAGS_ANY_SRC_IP) != 0 && SrcIp == NULL) ||\r
-      ((OpFlags & EFI_PXE_BASE_CODE_UDP_OPFLAGS_ANY_SRC_PORT) != 0 && SrcPort == NULL)) {\r
+  if (((OpFlags & EFI_PXE_BASE_CODE_UDP_OPFLAGS_ANY_DEST_PORT) == 0 && DestPort == NULL) ||\r
+      ((OpFlags & EFI_PXE_BASE_CODE_UDP_OPFLAGS_ANY_SRC_IP) == 0 && SrcIp == NULL) ||\r
+      ((OpFlags & EFI_PXE_BASE_CODE_UDP_OPFLAGS_ANY_SRC_PORT) == 0 && SrcPort == NULL)) {\r
     return EFI_INVALID_PARAMETER;\r
   }\r
 \r
@@ -1302,26 +1390,55 @@ EfiPxeBcUdpRead (
     //\r
     // Copy the rececived packet to user if matched by filter.\r
     //\r
-    CopiedLen = 0;\r
     if (Mode->UsingIpv6) {\r
       Udp6Rx = Udp6Token.Packet.RxData;\r
       ASSERT (Udp6Rx != NULL);\r
-      //\r
-      // Copy the header part of received data.\r
-      //\r
+\r
+      HeaderLen = 0;\r
       if (HeaderSize != NULL) {\r
-        CopiedLen   = MIN (*HeaderSize, Udp6Rx->DataLength);\r
-        *HeaderSize = CopiedLen;\r
-        CopyMem (HeaderPtr, Udp6Rx->FragmentTable[0].FragmentBuffer, *HeaderSize);\r
+        HeaderLen = MIN (*HeaderSize, Udp6Rx->DataLength);\r
       }\r
-      //\r
-      // Copy the other part of received data.\r
-      //\r
-      if (Udp6Rx->DataLength - CopiedLen > *BufferSize) {\r
+\r
+      if (Udp6Rx->DataLength - HeaderLen > *BufferSize) {\r
         Status = EFI_BUFFER_TOO_SMALL;\r
       } else {\r
-        *BufferSize = Udp6Rx->DataLength - CopiedLen;\r
-        CopyMem (BufferPtr, (UINT8 *) Udp6Rx->FragmentTable[0].FragmentBuffer + CopiedLen, *BufferSize);\r
+        if (HeaderSize != NULL) {\r
+          *HeaderSize = HeaderLen;\r
+        }\r
+        *BufferSize = Udp6Rx->DataLength - HeaderLen;\r
+\r
+        HeaderCopiedLen = 0;\r
+        BufferCopiedLen = 0;\r
+        for (FragmentIndex = 0; FragmentIndex < Udp6Rx->FragmentCount; FragmentIndex++) {\r
+          FragmentLength = Udp6Rx->FragmentTable[FragmentIndex].FragmentLength;\r
+          FragmentBuffer = Udp6Rx->FragmentTable[FragmentIndex].FragmentBuffer;\r
+          if (HeaderCopiedLen + FragmentLength < HeaderLen) {\r
+            //\r
+            // Copy the header part of received data.\r
+            //\r
+            CopyMem ((UINT8 *) HeaderPtr + HeaderCopiedLen, FragmentBuffer, FragmentLength);\r
+            HeaderCopiedLen += FragmentLength;\r
+          } else if (HeaderCopiedLen < HeaderLen) {\r
+            //\r
+            // Copy the header part of received data.\r
+            //\r
+            CopiedLen = HeaderLen - HeaderCopiedLen;\r
+            CopyMem ((UINT8 *) HeaderPtr + HeaderCopiedLen, FragmentBuffer, CopiedLen);\r
+            HeaderCopiedLen += CopiedLen;\r
+\r
+            //\r
+            // Copy the other part of received data.\r
+            //\r
+            CopyMem ((UINT8 *) BufferPtr + BufferCopiedLen, FragmentBuffer + CopiedLen, FragmentLength - CopiedLen);\r
+            BufferCopiedLen += (FragmentLength - CopiedLen);\r
+          } else {\r
+            //\r
+            // Copy the other part of received data.\r
+            //\r
+            CopyMem ((UINT8 *) BufferPtr + BufferCopiedLen, FragmentBuffer, FragmentLength);\r
+            BufferCopiedLen += FragmentLength;\r
+          }\r
+        }\r
       }\r
       //\r
       // Recycle the receiving buffer after copy to user.\r
@@ -1330,22 +1447,52 @@ EfiPxeBcUdpRead (
     } else {\r
       Udp4Rx = Udp4Token.Packet.RxData;\r
       ASSERT (Udp4Rx != NULL);\r
-      //\r
-      // Copy the header part of received data.\r
-      //\r
+\r
+      HeaderLen = 0;\r
       if (HeaderSize != NULL) {\r
-        CopiedLen   = MIN (*HeaderSize, Udp4Rx->DataLength);\r
-        *HeaderSize = CopiedLen;\r
-        CopyMem (HeaderPtr, Udp4Rx->FragmentTable[0].FragmentBuffer, *HeaderSize);\r
+        HeaderLen = MIN (*HeaderSize, Udp4Rx->DataLength);\r
       }\r
-      //\r
-      // Copy the other part of received data.\r
-      //\r
-      if (Udp4Rx->DataLength - CopiedLen > *BufferSize) {\r
+\r
+      if (Udp4Rx->DataLength - HeaderLen > *BufferSize) {\r
         Status = EFI_BUFFER_TOO_SMALL;\r
       } else {\r
-        *BufferSize = Udp4Rx->DataLength - CopiedLen;\r
-        CopyMem (BufferPtr, (UINT8 *) Udp4Rx->FragmentTable[0].FragmentBuffer + CopiedLen, *BufferSize);\r
+        if (HeaderSize != NULL) {\r
+          *HeaderSize = HeaderLen;\r
+        }\r
+        *BufferSize = Udp4Rx->DataLength - HeaderLen;\r
+\r
+        HeaderCopiedLen = 0;\r
+        BufferCopiedLen = 0;\r
+        for (FragmentIndex = 0; FragmentIndex < Udp4Rx->FragmentCount; FragmentIndex++) {\r
+          FragmentLength = Udp4Rx->FragmentTable[FragmentIndex].FragmentLength;\r
+          FragmentBuffer = Udp4Rx->FragmentTable[FragmentIndex].FragmentBuffer;\r
+          if (HeaderCopiedLen + FragmentLength < HeaderLen) {\r
+            //\r
+            // Copy the header part of received data.\r
+            //\r
+            CopyMem ((UINT8 *) HeaderPtr + HeaderCopiedLen, FragmentBuffer, FragmentLength);\r
+            HeaderCopiedLen += FragmentLength;\r
+          } else if (HeaderCopiedLen < HeaderLen) {\r
+            //\r
+            // Copy the header part of received data.\r
+            //\r
+            CopiedLen = HeaderLen - HeaderCopiedLen;\r
+            CopyMem ((UINT8 *) HeaderPtr + HeaderCopiedLen, FragmentBuffer, CopiedLen);\r
+            HeaderCopiedLen += CopiedLen;\r
+\r
+            //\r
+            // Copy the other part of received data.\r
+            //\r
+            CopyMem ((UINT8 *) BufferPtr + BufferCopiedLen, FragmentBuffer + CopiedLen, FragmentLength - CopiedLen);\r
+            BufferCopiedLen += (FragmentLength - CopiedLen);\r
+          } else {\r
+            //\r
+            // Copy the other part of received data.\r
+            //\r
+            CopyMem ((UINT8 *) BufferPtr + BufferCopiedLen, FragmentBuffer, FragmentLength);\r
+            BufferCopiedLen += FragmentLength;\r
+          }\r
+        }\r
       }\r
       //\r
       // Recycle the receiving buffer after copy to user.\r
@@ -1408,10 +1555,13 @@ EfiPxeBcSetIpFilter (
   EFI_STATUS                Status;\r
   PXEBC_PRIVATE_DATA        *Private;\r
   EFI_PXE_BASE_CODE_MODE    *Mode;\r
-  EFI_UDP4_CONFIG_DATA      Udp4Cfg;\r
-  EFI_UDP6_CONFIG_DATA      Udp6Cfg;\r
+  EFI_UDP4_CONFIG_DATA      *Udp4Cfg;\r
+  EFI_UDP6_CONFIG_DATA      *Udp6Cfg;\r
   UINTN                     Index;\r
   BOOLEAN                   NeedPromiscuous;\r
+  BOOLEAN                   AcceptPromiscuous;\r
+  BOOLEAN                   AcceptBroadcast;\r
+  BOOLEAN                   MultiCastUpdate;\r
 \r
   if (This == NULL || NewFilter == NULL) {\r
     return EFI_INVALID_PARAMETER;\r
@@ -1446,19 +1596,9 @@ EfiPxeBcSetIpFilter (
     }\r
   }\r
 \r
-  //\r
-  // Clear configuration for UdpRead and leave the original group joined before.\r
-  //\r
-  if (Mode->UsingIpv6) {\r
-    CopyMem(&Udp6Cfg, &Private->Udp6CfgData, sizeof (EFI_UDP6_CONFIG_DATA));\r
-    Private->Udp6Read->Configure (Private->Udp6Read, NULL);\r
-    Udp6Cfg.AcceptPromiscuous  = FALSE;\r
-  } else {\r
-    CopyMem(&Udp4Cfg, &Private->Udp4CfgData, sizeof (EFI_UDP4_CONFIG_DATA));\r
-    Private->Udp4Read->Configure (Private->Udp4Read, NULL);\r
-    Udp4Cfg.AcceptPromiscuous = FALSE;\r
-    Udp4Cfg.AcceptBroadcast   = FALSE;\r
-  }\r
+  AcceptPromiscuous = FALSE;\r
+  AcceptBroadcast   = FALSE;\r
+  MultiCastUpdate   = FALSE;\r
 \r
   if (NeedPromiscuous ||\r
       (NewFilter->Filters & EFI_PXE_BASE_CODE_IP_FILTER_PROMISCUOUS) != 0 ||\r
@@ -1466,55 +1606,104 @@ EfiPxeBcSetIpFilter (
     //\r
     // Configure UDPv4/UDPv6 as promiscuous mode to receive all packets.\r
     //\r
-    Udp4Cfg.AcceptPromiscuous = TRUE;\r
-    Udp6Cfg.AcceptPromiscuous = TRUE;\r
-\r
+    AcceptPromiscuous = TRUE;\r
   } else if ((NewFilter->Filters & EFI_PXE_BASE_CODE_IP_FILTER_BROADCAST) != 0) {\r
     //\r
     // Configure UDPv4 to receive all broadcast packets.\r
     //\r
-    Udp4Cfg.AcceptBroadcast  = TRUE;\r
+    AcceptBroadcast  = TRUE;\r
   }\r
 \r
   //\r
-  // Configure UDPv4/UDPv6 instance with the new configuration.\r
+  // In multicast condition when Promiscuous FALSE and IpCnt no-zero.\r
+  // Here check if there is any update of the multicast ip address. If yes,\r
+  // we need leave the old multicast group (by Config UDP instance to NULL),\r
+  // and join the new multicast group.\r
   //\r
-  if (Mode->UsingIpv6) {\r
-    Status = Private->Udp6Read->Configure (Private->Udp6Read, &Udp6Cfg);\r
-  } else {\r
-    Status = Private->Udp4Read->Configure (Private->Udp4Read, &Udp4Cfg);\r
-  }\r
-\r
-  if (EFI_ERROR (Status)) {\r
-    goto ON_EXIT;\r
+  if (!AcceptPromiscuous) {\r
+    if ((NewFilter->Filters & EFI_PXE_BASE_CODE_IP_FILTER_STATION_IP) != 0) {\r
+      if (Mode->IpFilter.IpCnt != NewFilter->IpCnt) {\r
+        MultiCastUpdate = TRUE;\r
+      } else if (CompareMem (Mode->IpFilter.IpList, NewFilter->IpList, NewFilter->IpCnt * sizeof (EFI_IP_ADDRESS)) != 0 ) {\r
+        MultiCastUpdate = TRUE;\r
+      }\r
+    }\r
   }\r
 \r
-  if ((NewFilter->Filters & EFI_PXE_BASE_CODE_IP_FILTER_STATION_IP) != 0) {\r
-\r
-    for (Index = 0; Index < NewFilter->IpCnt; Index++) {\r
+  if (!Mode->UsingIpv6) {\r
+    //\r
+    // Check whether we need reconfigure the UDP4 instance.\r
+    //\r
+    Udp4Cfg = &Private->Udp4CfgData;\r
+    if ((AcceptPromiscuous != Udp4Cfg->AcceptPromiscuous)   ||\r
+         (AcceptBroadcast != Udp4Cfg->AcceptBroadcast)     || MultiCastUpdate) {\r
+      //\r
+      // Clear the UDP4 instance configuration, all joined groups will be left\r
+      // during the operation.\r
+      //\r
+      Private->Udp4Read->Configure (Private->Udp4Read, NULL);\r
+  \r
       //\r
-      // Join the multicast group if needed.\r
+      // Configure the UDP instance with the new configuration.\r
       //\r
-      if (Mode->UsingIpv6) {\r
-        if (IP6_IS_MULTICAST (&NewFilter->IpList[Index].v6)) {\r
-          Status = Private->Udp6Read->Groups (\r
-                                        Private->Udp6Read,\r
-                                        TRUE,\r
-                                        &NewFilter->IpList[Index].v6\r
-                                        );\r
-          if (EFI_ERROR (Status)) {\r
-            goto ON_EXIT;\r
+      Udp4Cfg->AcceptPromiscuous = AcceptPromiscuous;\r
+      Udp4Cfg->AcceptBroadcast   = AcceptBroadcast;\r
+      Status = Private->Udp4Read->Configure (Private->Udp4Read, Udp4Cfg);\r
+      if (EFI_ERROR (Status)) {\r
+        return Status;\r
+      }\r
+  \r
+      //\r
+      // In not Promiscuous mode, need to join the new multicast group.\r
+      //\r
+      if (!AcceptPromiscuous) {\r
+        for (Index = 0; Index < NewFilter->IpCnt; ++Index) {\r
+          if (IP4_IS_MULTICAST (EFI_NTOHL (NewFilter->IpList[Index].v4))) {\r
+            //\r
+            // Join the mutilcast group.\r
+            //\r
+            Status = Private->Udp4Read->Groups (Private->Udp4Read, TRUE, &NewFilter->IpList[Index].v4);\r
+            if (EFI_ERROR (Status)) {\r
+              return Status;\r
+            }\r
           }\r
         }\r
-      } else {\r
-        if (IP4_IS_MULTICAST (EFI_NTOHL (NewFilter->IpList[Index].v4))) {\r
-          Status = Private->Udp4Read->Groups (\r
-                                        Private->Udp4Read,\r
-                                        TRUE,\r
-                                        &NewFilter->IpList[Index].v4\r
-                                        );\r
-          if (EFI_ERROR (Status)) {\r
-            goto ON_EXIT;\r
+      }\r
+    }\r
+  } else {\r
+    //\r
+    // Check whether we need reconfigure the UDP6 instance.\r
+    //\r
+    Udp6Cfg = &Private->Udp6CfgData;\r
+    if ((AcceptPromiscuous != Udp6Cfg->AcceptPromiscuous) || MultiCastUpdate) {\r
+      //\r
+      // Clear the UDP6 instance configuration, all joined groups will be left\r
+      // during the operation.\r
+      //\r
+      Private->Udp6Read->Configure (Private->Udp6Read, NULL);\r
+  \r
+      //\r
+      // Configure the UDP instance with the new configuration.\r
+      //\r
+      Udp6Cfg->AcceptPromiscuous = AcceptPromiscuous;\r
+      Status = Private->Udp6Read->Configure (Private->Udp6Read, Udp6Cfg);\r
+      if (EFI_ERROR (Status)) {\r
+        return Status;\r
+      }\r
+  \r
+      //\r
+      // In not Promiscuous mode, need to join the new multicast group.\r
+      //\r
+      if (!AcceptPromiscuous) {\r
+        for (Index = 0; Index < NewFilter->IpCnt; ++Index) {\r
+          if (IP6_IS_MULTICAST (&NewFilter->IpList[Index].v6)) {\r
+            //\r
+            // Join the mutilcast group.\r
+            //\r
+            Status = Private->Udp6Read->Groups (Private->Udp6Read, TRUE, &NewFilter->IpList[Index].v6);\r
+            if (EFI_ERROR (Status)) {\r
+              return Status;\r
+            }\r
           }\r
         }\r
       }\r
@@ -1526,7 +1715,6 @@ EfiPxeBcSetIpFilter (
   //\r
   CopyMem (&Mode->IpFilter, NewFilter, sizeof (Mode->IpFilter));\r
 \r
-ON_EXIT:\r
   return Status;\r
 }\r
 \r
@@ -1737,7 +1925,7 @@ EfiPxeBcSetParameters (
   }\r
 \r
   if (NewSendGUID != NULL) {\r
-    if (*NewSendGUID && EFI_ERROR (PxeBcGetSystemGuid (&SystemGuid))) {\r
+    if (*NewSendGUID && EFI_ERROR (NetLibGetSystemGuid (&SystemGuid))) {\r
       return EFI_INVALID_PARAMETER;\r
     }\r
     Mode->SendGUID = *NewSendGUID;\r
@@ -1858,7 +2046,7 @@ EfiPxeBcSetStationIP (
     CopyMem (&Private->SubnetMask ,NewSubnetMask, sizeof (EFI_IP_ADDRESS));\r
   }\r
 \r
-  Status = PxeBcFlushStaionIp (Private, NewStationIp, NewSubnetMask);\r
+  Status = PxeBcFlushStationIp (Private, NewStationIp, NewSubnetMask);\r
 ON_EXIT:\r
   return Status;\r
 }\r
@@ -2140,6 +2328,10 @@ EfiPxeLoadFile (
   EFI_STATUS                  Status;\r
   BOOLEAN                     MediaPresent;\r
 \r
+  if (FilePath == NULL || !IsDevicePathEnd (FilePath)) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+  \r
   VirtualNic = PXEBC_VIRTUAL_NIC_FROM_LOADFILE (This);\r
   Private    = VirtualNic->Private;\r
   PxeBc      = &Private->PxeBc;\r
@@ -2177,6 +2369,15 @@ EfiPxeLoadFile (
   // Start Pxe Base Code to initialize PXE boot.\r
   //\r
   Status = PxeBc->Start (PxeBc, UsingIpv6);\r
+  if (Status == EFI_ALREADY_STARTED && UsingIpv6 != PxeBc->Mode->UsingIpv6) {\r
+    //\r
+    // PxeBc protocol has already been started but not on the required IP version, restart it.\r
+    //\r
+    Status = PxeBc->Stop (PxeBc);\r
+    if (!EFI_ERROR (Status)) {\r
+      Status = PxeBc->Start (PxeBc, UsingIpv6);\r
+    }\r
+  }\r
   if (Status == EFI_SUCCESS || Status == EFI_ALREADY_STARTED) {\r
     Status = PxeBcLoadBootFile (Private, BufferSize, Buffer);\r
   }\r
@@ -2191,6 +2392,16 @@ EfiPxeLoadFile (
     //   3. unsupported.\r
     //\r
     PxeBc->Stop (PxeBc);\r
+  } else {\r
+    //\r
+    // The DHCP4 can have only one configured child instance so we need to stop\r
+    // reset the DHCP4 child before we return. Otherwise these programs which \r
+    // also need to use DHCP4 will be impacted.\r
+    //\r
+    if (!PxeBc->Mode->UsingIpv6) {\r
+      Private->Dhcp4->Stop (Private->Dhcp4);\r
+      Private->Dhcp4->Configure (Private->Dhcp4, NULL);\r
+    }\r
   }\r
 \r
   return Status;\r