]> git.proxmox.com Git - mirror_edk2.git/blobdiff - NetworkPkg/UefiPxeBcDxe/PxeBcImpl.c
Fix a bug about the iSCSI DHCP dependency issue.
[mirror_edk2.git] / NetworkPkg / UefiPxeBcDxe / PxeBcImpl.c
index 82fddb169ae0a6e14525cb4130637ffd5331e3ae..15cda8cf69b67e47b21c82923528b872c6390960 100644 (file)
@@ -1,7 +1,7 @@
 /** @file\r
   This implementation of EFI_PXE_BASE_CODE_PROTOCOL and EFI_LOAD_FILE_PROTOCOL.\r
 \r
 /** @file\r
   This implementation of EFI_PXE_BASE_CODE_PROTOCOL and EFI_LOAD_FILE_PROTOCOL.\r
 \r
-  Copyright (c) 2007 - 2011, Intel Corporation. All rights reserved.<BR>\r
+  Copyright (c) 2007 - 2013, 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
 \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
   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
     //\r
     // Configure block size for TFTP as a default value to handle all link layers.\r
     //\r
@@ -115,6 +126,17 @@ EfiPxeBcStart (
     }\r
   } else {\r
     AsciiPrint ("\n>>Start PXE over IPv4");\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
     //\r
     // Configure block size for TFTP as a default value to handle all link layers.\r
     //\r
@@ -406,15 +428,6 @@ EfiPxeBcDhcp (
     // Start S.A.R.R. process to get a IPv6 address and other boot information.\r
     //\r
     Status = PxeBcDhcp6Sarr (Private, Private->Dhcp6);\r
     // Start S.A.R.R. process to get a IPv6 address and other boot information.\r
     //\r
     Status = PxeBcDhcp6Sarr (Private, Private->Dhcp6);\r
-\r
-    if (EFI_ERROR (Status)) {\r
-      return Status;\r
-    }\r
-\r
-    //\r
-    // Configure Udp6Read instance\r
-    //\r
-    Status = Private->Udp6Read->Configure (Private->Udp6Read, &Private->Udp6CfgData);    \r
   } else {\r
 \r
     //\r
   } else {\r
 \r
     //\r
@@ -426,17 +439,16 @@ EfiPxeBcDhcp (
     // Start D.O.R.A. process to get a IPv4 address and other boot information.\r
     //\r
     Status = PxeBcDhcp4Dora (Private, Private->Dhcp4);\r
     // Start D.O.R.A. process to get a IPv4 address and other boot information.\r
     //\r
     Status = PxeBcDhcp4Dora (Private, Private->Dhcp4);\r
-\r
-    if (EFI_ERROR (Status)) {\r
-      return Status;\r
-    }\r
-\r
-    //\r
-    // Configure Udp4Read instance\r
-    //\r
-    Status = Private->Udp4Read->Configure (Private->Udp4Read, &Private->Udp4CfgData);\r
   }\r
 \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
   //\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
@@ -511,6 +523,7 @@ EfiPxeBcDiscover (
   UINT16                          Index;\r
   EFI_STATUS                      Status;\r
   EFI_PXE_BASE_CODE_IP_FILTER     IpFilter;\r
   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
 \r
   if (This == NULL) {\r
     return EFI_INVALID_PARAMETER;\r
@@ -523,6 +536,7 @@ EfiPxeBcDiscover (
   SrvList                 = NULL;\r
   Status                  = EFI_DEVICE_ERROR;\r
   Private->Function       = EFI_PXE_BASE_CODE_FUNCTION_DISCOVER;\r
   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
 \r
   if (!Mode->Started) {\r
     return EFI_NOT_STARTED;\r
@@ -552,6 +566,7 @@ EfiPxeBcDiscover (
   //\r
   // There are 3 methods to get the information for discover.\r
   //\r
   //\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
   if (*Layer != EFI_PXE_BASE_CODE_BOOT_LAYER_INITIAL) {\r
     //\r
     // 1. Take the previous setting as the discover info.\r
@@ -576,12 +591,12 @@ EfiPxeBcDiscover (
     //\r
     // 2. Extract the discover information from the cached packets if unspecified.\r
     //\r
     //\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
     if (EFI_ERROR (Status)) {\r
       goto ON_EXIT;\r
     }\r
-\r
+    Info = NewCreatedInfo;\r
   } else {\r
     //\r
     // 3. Take the pass-in information as the discover info, and validate the server list.\r
   } else {\r
     //\r
     // 3. Take the pass-in information as the discover info, and validate the server list.\r
@@ -597,7 +612,7 @@ EfiPxeBcDiscover (
       if (Index != Info->IpCnt) {\r
         //\r
         // It's invalid if the first server doesn't accecpt any response\r
       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
         //\r
         Status = EFI_INVALID_PARAMETER;\r
         goto ON_EXIT;\r
@@ -616,30 +631,7 @@ EfiPxeBcDiscover (
 \r
   Private->IsDoDiscover = TRUE;\r
 \r
 \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
     //\r
     // Do discover by multicast.\r
     //\r
@@ -649,8 +641,8 @@ EfiPxeBcDiscover (
                Layer,\r
                UseBis,\r
                &Info->ServerMCastIp,\r
                Layer,\r
                UseBis,\r
                &Info->ServerMCastIp,\r
-               0,\r
-               NULL\r
+               Info->IpCnt,\r
+               SrvList\r
                );\r
 \r
   } else if (Info->UseBCast) {\r
                );\r
 \r
   } else if (Info->UseBCast) {\r
@@ -667,6 +659,30 @@ EfiPxeBcDiscover (
                Info->IpCnt,\r
                SrvList\r
                );\r
                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
   }\r
 \r
   if (!EFI_ERROR (Status)) {\r
@@ -678,8 +694,8 @@ EfiPxeBcDiscover (
       if (!EFI_ERROR (Status)) {\r
         CopyMem (\r
           &Mode->PxeReply.Dhcpv6,\r
       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
           );\r
         Mode->PxeReplyReceived = TRUE;\r
         Mode->PxeDiscoverValid = TRUE;\r
@@ -689,8 +705,8 @@ EfiPxeBcDiscover (
       if (!EFI_ERROR (Status)) {\r
         CopyMem (\r
           &Mode->PxeReply.Dhcpv4,\r
       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
           );\r
         Mode->PxeReplyReceived = TRUE;\r
         Mode->PxeDiscoverValid = TRUE;\r
@@ -700,10 +716,14 @@ EfiPxeBcDiscover (
 \r
 ON_EXIT:\r
 \r
 \r
 ON_EXIT:\r
 \r
+  if (NewCreatedInfo != NULL && NewCreatedInfo != &DefaultInfo) {\r
+    FreePool (NewCreatedInfo);\r
+  }\r
+  \r
   if (Mode->UsingIpv6) {\r
   if (Mode->UsingIpv6) {\r
-    Status = Private->Udp6Read->Configure (Private->Udp6Read, &Private->Udp6CfgData);    \r
+    Private->Udp6Read->Configure (Private->Udp6Read, &Private->Udp6CfgData);\r
   } else {\r
   } else {\r
-    Status = Private->Udp4Read->Configure (Private->Udp4Read, &Private->Udp4CfgData);    \r
+    Private->Udp4Read->Configure (Private->Udp4Read, &Private->Udp4CfgData);\r
   }\r
   \r
   //\r
   }\r
   \r
   //\r
@@ -937,12 +957,14 @@ EfiPxeBcMtftp (
     Mode->IcmpErrorReceived = TRUE;\r
   }\r
 \r
     Mode->IcmpErrorReceived = TRUE;\r
   }\r
 \r
+  //\r
+  // Reconfigure the UDP instance with the default configuration.\r
+  //\r
   if (Mode->UsingIpv6) {\r
   if (Mode->UsingIpv6) {\r
-    Status = Private->Udp6Read->Configure (Private->Udp6Read, &Private->Udp6CfgData);    \r
+    Private->Udp6Read->Configure (Private->Udp6Read, &Private->Udp6CfgData);\r
   } else {\r
   } else {\r
-    Status = Private->Udp4Read->Configure (Private->Udp4Read, &Private->Udp4CfgData);    \r
+    Private->Udp4Read->Configure (Private->Udp4Read, &Private->Udp4CfgData);\r
   }\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
   //\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
@@ -1222,6 +1244,12 @@ EfiPxeBcUdpRead (
   BOOLEAN                     IsDone;\r
   BOOLEAN                     IsMatched;\r
   UINTN                       CopiedLen;\r
   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
     return EFI_INVALID_PARAMETER;\r
 \r
   if (This == NULL || DestIp == NULL || DestPort == NULL) {\r
     return EFI_INVALID_PARAMETER;\r
@@ -1336,26 +1364,55 @@ EfiPxeBcUdpRead (
     //\r
     // Copy the rececived packet to user if matched by filter.\r
     //\r
     //\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
     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
       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
-      //\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
         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
       }\r
       //\r
       // Recycle the receiving buffer after copy to user.\r
@@ -1364,22 +1421,52 @@ EfiPxeBcUdpRead (
     } else {\r
       Udp4Rx = Udp4Token.Packet.RxData;\r
       ASSERT (Udp4Rx != NULL);\r
     } 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
       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
-      //\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
         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
       }\r
       //\r
       // Recycle the receiving buffer after copy to user.\r
@@ -2252,6 +2339,15 @@ EfiPxeLoadFile (
   // Start Pxe Base Code to initialize PXE boot.\r
   //\r
   Status = PxeBc->Start (PxeBc, UsingIpv6);\r
   // 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
   if (Status == EFI_SUCCESS || Status == EFI_ALREADY_STARTED) {\r
     Status = PxeBcLoadBootFile (Private, BufferSize, Buffer);\r
   }\r