]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Universal/Network/UefiPxeBcDxe/PxeBcImpl.c
MdeModulePkg: Clean up source files
[mirror_edk2.git] / MdeModulePkg / Universal / Network / UefiPxeBcDxe / PxeBcImpl.c
index 663d4a0202826dcd290d66708faeb881e663f304..3fa3be99c1786e34d96dc5ce289d622ca82c4e29 100644 (file)
@@ -1,8 +1,8 @@
 /** @file\r
   Interface routines for PxeBc.\r
-  \r
-Copyright (c) 2007 - 2009, Intel Corporation.<BR>\r
-All rights reserved. This program and the accompanying materials\r
+\r
+Copyright (c) 2007 - 2018, 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
@@ -61,8 +61,8 @@ UpdateArpCache (
                            );\r
   for (Index = 0; Index < Mode->ArpCacheEntries; Index ++) {\r
     CopyMem (\r
-      &Mode->ArpCache[Index].IpAddr, \r
-      Entries + 1, \r
+      &Mode->ArpCache[Index].IpAddr,\r
+      Entries + 1,\r
       Entries->SwAddressLength\r
       );\r
     CopyMem (\r
@@ -84,8 +84,6 @@ UpdateArpCache (
 \r
   @param  Event              Pointer to EFI_PXE_BC_PROTOCOL\r
   @param  Context            Context of the timer event\r
-  \r
-  @return None.\r
 \r
 **/\r
 VOID\r
@@ -100,12 +98,12 @@ ArpCacheUpdateTimeout (
 \r
 /**\r
   Do arp resolution from arp cache in PxeBcMode.\r
-  \r
+\r
   @param  PxeBcMode      The PXE BC mode to look into.\r
   @param  Ip4Addr        The Ip4 address for resolution.\r
   @param  MacAddress     The resoluted MAC address if the resolution is successful.\r
                          The value is undefined if resolution fails.\r
-                         \r
+\r
   @retval TRUE           The resolution is successful.\r
   @retval FALSE          Otherwise.\r
 \r
@@ -139,8 +137,6 @@ FindInArpCache (
 \r
   @param  Context               The PXEBC private data.\r
 \r
-  @return None.\r
-\r
 **/\r
 VOID\r
 EFIAPI\r
@@ -170,17 +166,21 @@ IcmpErrorListenHandlerDpc (
     return;\r
   }\r
 \r
-  if (EFI_ERROR (Status) || (RxData == NULL)) {\r
+  if (RxData == NULL) {\r
+    goto Resume;\r
+  }\r
+\r
+  if (Status != EFI_ICMP_ERROR) {\r
     //\r
-    // Only process the normal packets and the icmp error packets, if RxData is NULL\r
-    // with Status == EFI_SUCCESS or EFI_ICMP_ERROR, just resume the receive although\r
-    // this should be a bug of the low layer (IP).\r
+    // The return status should be recognized as EFI_ICMP_ERROR.\r
     //\r
-    goto Resume;\r
+    goto CleanUp;\r
   }\r
 \r
   if (EFI_IP4 (RxData->Header->SourceAddress) != 0 &&\r
-      !Ip4IsUnicast (EFI_NTOHL (RxData->Header->SourceAddress), 0)) {\r
+      (NTOHL (Mode->SubnetMask.Addr[0]) != 0) &&\r
+      IP4_NET_EQUAL (NTOHL(Mode->StationIp.Addr[0]), EFI_NTOHL (RxData->Header->SourceAddress), NTOHL (Mode->SubnetMask.Addr[0])) &&\r
+      !NetIp4IsUnicast (EFI_NTOHL (RxData->Header->SourceAddress), NTOHL (Mode->SubnetMask.Addr[0]))) {\r
     //\r
     // The source address is not zero and it's not a unicast IP address, discard it.\r
     //\r
@@ -218,8 +218,6 @@ IcmpErrorListenHandlerDpc (
     CopiedPointer += CopiedLen;\r
   }\r
 \r
-  goto Resume;\r
-\r
 CleanUp:\r
   gBS->SignalEvent (RxData->RecycleSignal);\r
 \r
@@ -233,8 +231,6 @@ Resume:
   @param  Event                 The event signaled.\r
   @param  Context               The context passed in by the event notifier.\r
 \r
-  @return None.\r
-\r
 **/\r
 VOID\r
 EFIAPI\r
@@ -246,10 +242,10 @@ IcmpErrorListenHandler (
   //\r
   // Request IpIoListenHandlerDpc as a DPC at TPL_CALLBACK\r
   //\r
-  NetLibQueueDpc (TPL_CALLBACK, IcmpErrorListenHandlerDpc, Context);\r
+  QueueDpc (TPL_CALLBACK, IcmpErrorListenHandlerDpc, Context);\r
 }\r
 \r
-/**                                                                 \r
+/**\r
   Enables the use of the PXE Base Code Protocol functions.\r
 \r
   This function enables the use of the PXE Base Code Protocol functions. If the\r
@@ -296,22 +292,22 @@ IcmpErrorListenHandler (
     TftpErrorZero-filled.\r
     MakeCallbacksSet to TRUE if the PXE Base Code Callback Protocol is available.\r
     Set to FALSE if the PXE Base Code Callback Protocol is not available.\r
-    \r
+\r
   @param  This                  Pointer to the EFI_PXE_BASE_CODE_PROTOCOL instance.\r
   @param  UseIpv6               Specifies the type of IP addresses that are to be used during the session\r
-                                that is being started. Set to TRUE for IPv6 addresses, and FALSE for     \r
-                                IPv4 addresses.                                                                                                   \r
-                                \r
+                                that is being started. Set to TRUE for IPv6 addresses, and FALSE for\r
+                                IPv4 addresses.\r
+\r
   @retval EFI_SUCCESS           The PXE Base Code Protocol was started.\r
-  @retval EFI_DEVICE_ERROR      The network device encountered an error during this oper  \r
+  @retval EFI_DEVICE_ERROR      The network device encountered an error during this oper\r
   @retval EFI_UNSUPPORTED       UseIpv6 is TRUE, but the Ipv6Supported field of the\r
-                                EFI_PXE_BASE_CODE_MODE structure is FALSE.  \r
-  @retval EFI_ALREADY_STARTED   The PXE Base Code Protocol is already in the started state.                                   \r
+                                EFI_PXE_BASE_CODE_MODE structure is FALSE.\r
+  @retval EFI_ALREADY_STARTED   The PXE Base Code Protocol is already in the started state.\r
   @retval EFI_INVALID_PARAMETER The This parameter is NULL or does not point to a valid\r
-                                EFI_PXE_BASE_CODE_PROTOCOL structure.      \r
-  @retval EFI_OUT_OF_RESOURCES  Could not allocate enough memory or other resources to start the                                          \r
-                                PXE Base Code Protocol.                                         \r
-                                     \r
+                                EFI_PXE_BASE_CODE_PROTOCOL structure.\r
+  @retval EFI_OUT_OF_RESOURCES  Could not allocate enough memory or other resources to start the\r
+                                PXE Base Code Protocol.\r
+\r
 **/\r
 EFI_STATUS\r
 EFIAPI\r
@@ -342,17 +338,32 @@ EfiPxeBcStart (
     return EFI_UNSUPPORTED;\r
   }\r
 \r
+  AsciiPrint ("\n>>Start PXE over IPv4");\r
+\r
   //\r
   // Configure the udp4 instance to let it receive data\r
   //\r
   Status = Private->Udp4Read->Configure (\r
-                               Private->Udp4Read, \r
+                               Private->Udp4Read,\r
                                &Private->Udp4CfgData\r
                                );\r
   if (EFI_ERROR (Status)) {\r
     return Status;\r
   }\r
 \r
+\r
+  //\r
+  // Configure block size for TFTP as a default value to handle all link layers.\r
+  //\r
+  Private->BlockSize   = MIN (Private->Ip4MaxPacketSize, PXEBC_DEFAULT_PACKET_SIZE) -\r
+                           PXEBC_DEFAULT_UDP_OVERHEAD_SIZE - PXEBC_DEFAULT_TFTP_OVERHEAD_SIZE;\r
+  //\r
+  // If PcdTftpBlockSize is set to non-zero, override the default value.\r
+  //\r
+  if (PcdGet64 (PcdTftpBlockSize) != 0) {\r
+    Private->BlockSize   = (UINTN) PcdGet64 (PcdTftpBlockSize);\r
+  }\r
+\r
   Private->AddressIsOk = FALSE;\r
 \r
   ZeroMem (Mode, sizeof (EFI_PXE_BASE_CODE_MODE));\r
@@ -403,6 +414,18 @@ EfiPxeBcStart (
     goto ON_EXIT;\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_EXIT;\r
+  }\r
+\r
   Status = Private->Ip4->Configure (Private->Ip4, &Private->Ip4ConfigData);\r
   if (EFI_ERROR (Status)) {\r
     goto ON_EXIT;\r
@@ -437,22 +460,22 @@ ON_EXIT:
 }\r
 \r
 \r
-/**                                                                 \r
+/**\r
   Disables the use of the PXE Base Code Protocol functions.\r
 \r
   This function stops all activity on the network device. All the resources allocated\r
   in Start() are released, the Started field of the EFI_PXE_BASE_CODE_MODE structure is\r
   set to FALSE and EFI_SUCCESS is returned. If the Started field of the EFI_PXE_BASE_CODE_MODE\r
   structure is already FALSE, then EFI_NOT_STARTED will be returned.\r
-  \r
+\r
   @param  This                  Pointer to the EFI_PXE_BASE_CODE_PROTOCOL instance.\r
\r
+\r
   @retval EFI_SUCCESS           The PXE Base Code Protocol was stopped.\r
-  @retval EFI_NOT_STARTED       The PXE Base Code Protocol is already in the stopped state.  \r
+  @retval EFI_NOT_STARTED       The PXE Base Code Protocol is already in the stopped state.\r
   @retval EFI_INVALID_PARAMETER The This parameter is NULL or does not point to a valid\r
-                                EFI_PXE_BASE_CODE_PROTOCOL structure.                  \r
-  @retval EFI_DEVICE_ERROR      The network device encountered an error during this operation.                                \r
-                                     \r
+                                EFI_PXE_BASE_CODE_PROTOCOL structure.\r
+  @retval EFI_DEVICE_ERROR      The network device encountered an error during this operation.\r
+\r
 **/\r
 EFI_STATUS\r
 EFIAPI\r
@@ -479,7 +502,7 @@ EfiPxeBcStop (
   // Dispatch the DPCs queued by the NotifyFunction of the canceled rx token's\r
   // events.\r
   //\r
-  NetLibDispatchDpc ();\r
+  DispatchDpc ();\r
 \r
   Private->Ip4->Configure (Private->Ip4, NULL);\r
 \r
@@ -514,7 +537,7 @@ EfiPxeBcStop (
 }\r
 \r
 \r
-/**                                                                 \r
+/**\r
   Attempts to complete a DHCPv4 D.O.R.A. (discover / offer / request / acknowledge) or DHCPv6\r
   S.A.R.R (solicit / advertise / request / reply) sequence.\r
 \r
@@ -530,22 +553,22 @@ EfiPxeBcStop (
   caller. If the DHCP sequence does not complete, then EFI_TIMEOUT will be returned.\r
   If the Callback Protocol does not return EFI_PXE_BASE_CODE_CALLBACK_STATUS_CONTINUE,\r
   then the DHCP sequence will be stopped and EFI_ABORTED will be returned.\r
-    \r
+\r
   @param  This                  Pointer to the EFI_PXE_BASE_CODE_PROTOCOL instance.\r
   @param  SortOffers            TRUE if the offers received should be sorted. Set to FALSE to try the\r
-                                offers in the order that they are received.                          \r
\r
+                                offers in the order that they are received.\r
+\r
   @retval EFI_SUCCESS           Valid DHCP has completed.\r
   @retval EFI_NOT_STARTED       The PXE Base Code Protocol is in the stopped state.\r
   @retval EFI_INVALID_PARAMETER The This parameter is NULL or does not point to a valid\r
-                                EFI_PXE_BASE_CODE_PROTOCOL structure.                  \r
-  @retval EFI_DEVICE_ERROR      The network device encountered an error during this operation.                                \r
+                                EFI_PXE_BASE_CODE_PROTOCOL structure.\r
+  @retval EFI_DEVICE_ERROR      The network device encountered an error during this operation.\r
   @retval EFI_OUT_OF_RESOURCES  Could not allocate enough memory to complete the DHCP Protocol.\r
   @retval EFI_ABORTED           The callback function aborted the DHCP Protocol.\r
   @retval EFI_TIMEOUT           The DHCP Protocol timed out.\r
   @retval EFI_ICMP_ERROR        An ICMP error packet was received during the DHCP session.\r
   @retval EFI_NO_RESPONSE       Valid PXE offer was not received.\r
-                                     \r
+\r
 **/\r
 EFI_STATUS\r
 EFIAPI\r
@@ -554,15 +577,16 @@ EfiPxeBcDhcp (
   IN BOOLEAN                          SortOffers\r
   )\r
 {\r
-  PXEBC_PRIVATE_DATA      *Private;\r
-  EFI_PXE_BASE_CODE_MODE  *Mode;\r
-  EFI_DHCP4_PROTOCOL      *Dhcp4;\r
-  EFI_DHCP4_CONFIG_DATA   Dhcp4CfgData;\r
-  EFI_DHCP4_MODE_DATA     Dhcp4Mode;\r
-  EFI_DHCP4_PACKET_OPTION *OptList[PXEBC_DHCP4_MAX_OPTION_NUM];\r
-  UINT32                  OptCount;\r
-  EFI_STATUS              Status;\r
-  EFI_ARP_CONFIG_DATA     ArpConfigData;\r
+  PXEBC_PRIVATE_DATA           *Private;\r
+  EFI_PXE_BASE_CODE_MODE       *Mode;\r
+  EFI_DHCP4_PROTOCOL           *Dhcp4;\r
+  EFI_DHCP4_CONFIG_DATA        Dhcp4CfgData;\r
+  EFI_DHCP4_MODE_DATA          Dhcp4Mode;\r
+  EFI_DHCP4_PACKET_OPTION      *OptList[PXEBC_DHCP4_MAX_OPTION_NUM];\r
+  UINT32                       OptCount;\r
+  EFI_STATUS                   Status;\r
+  EFI_ARP_CONFIG_DATA          ArpConfigData;\r
+  EFI_PXE_BASE_CODE_IP_FILTER  IpFilter;\r
 \r
   if (This == NULL) {\r
     return EFI_INVALID_PARAMETER;\r
@@ -581,6 +605,11 @@ EfiPxeBcDhcp (
 \r
   Mode->IcmpErrorReceived = FALSE;\r
 \r
+  //\r
+  // Stop Udp4Read instance\r
+  //\r
+  Private->Udp4Read->Configure (Private->Udp4Read, NULL);\r
+\r
   //\r
   // Initialize the DHCP options and build the option list\r
   //\r
@@ -602,7 +631,7 @@ EfiPxeBcDhcp (
   if (EFI_ERROR (Status)) {\r
     goto ON_EXIT;\r
   }\r
-  \r
+\r
   //\r
   // Zero those arrays to record the varies numbers of DHCP OFFERS.\r
   //\r
@@ -613,7 +642,7 @@ EfiPxeBcDhcp (
   ZeroMem (Private->ProxyIndex, sizeof (Private->ProxyIndex));\r
 \r
   Status = Dhcp4->Start (Dhcp4, NULL);\r
-  if (EFI_ERROR (Status)) {\r
+  if (EFI_ERROR (Status) && Status != EFI_ALREADY_STARTED) {\r
     if (Status == EFI_ICMP_ERROR) {\r
       Mode->IcmpErrorReceived = TRUE;\r
     }\r
@@ -639,9 +668,11 @@ EfiPxeBcDhcp (
   // finished, set the various Mode members.\r
   //\r
   Status = PxeBcCheckSelectedOffer (Private);\r
-  if (!EFI_ERROR (Status)) {\r
-    goto ON_EXIT;\r
-  }\r
+\r
+  AsciiPrint ("\n  Station IP address is ");\r
+\r
+  PxeBcShowIp4Addr (&Private->StationIp.v4);\r
+  AsciiPrint ("\n");\r
 \r
 ON_EXIT:\r
   if (EFI_ERROR (Status)) {\r
@@ -664,7 +695,7 @@ ON_EXIT:
       ZeroMem (&ArpConfigData, sizeof (EFI_ARP_CONFIG_DATA));\r
 \r
       ArpConfigData.SwAddressType   = 0x0800;\r
-      ArpConfigData.SwAddressLength = sizeof (EFI_IPv4_ADDRESS);\r
+      ArpConfigData.SwAddressLength = (UINT8) sizeof (EFI_IPv4_ADDRESS);\r
       ArpConfigData.StationAddress  = &Private->StationIp.v4;\r
 \r
       Private->Arp->Configure (Private->Arp, NULL);\r
@@ -687,14 +718,48 @@ ON_EXIT:
         Mode->RouteTable[1].SubnetMask.Addr[0] = 0;\r
         Mode->RouteTable[1].GwAddr.Addr[0]     = Private->GatewayIp.Addr[0];\r
       }\r
+\r
+      //\r
+      // Flush new station IP address into Udp4CfgData and Ip4ConfigData\r
+      //\r
+      CopyMem (&Private->Udp4CfgData.StationAddress, &Private->StationIp, sizeof (EFI_IPv4_ADDRESS));\r
+      CopyMem (&Private->Udp4CfgData.SubnetMask, &Private->SubnetMask, sizeof (EFI_IPv4_ADDRESS));\r
+      CopyMem (&Private->Ip4ConfigData.StationAddress, &Private->StationIp, sizeof (EFI_IPv4_ADDRESS));\r
+      CopyMem (&Private->Ip4ConfigData.SubnetMask, &Private->SubnetMask, sizeof (EFI_IPv4_ADDRESS));\r
+\r
+      //\r
+      // Reconfigure the Ip4 instance to capture background ICMP packets with new station Ip address.\r
+      //\r
+      Private->Ip4->Cancel (Private->Ip4, &Private->IcmpErrorRcvToken);\r
+      Private->Ip4->Configure (Private->Ip4, NULL);\r
+\r
+      Status = Private->Ip4->Configure (Private->Ip4, &Private->Ip4ConfigData);\r
+      if (EFI_ERROR (Status)) {\r
+        goto ON_EXIT;\r
+      }\r
+\r
+      Status = Private->Ip4->Receive (Private->Ip4, &Private->IcmpErrorRcvToken);\r
+      if (EFI_ERROR (Status)) {\r
+        goto ON_EXIT;\r
+      }\r
     }\r
   }\r
 \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
+  ZeroMem(&IpFilter, sizeof (EFI_PXE_BASE_CODE_IP_FILTER));\r
+  IpFilter.Filters = EFI_PXE_BASE_CODE_IP_FILTER_STATION_IP;\r
+  This->SetIpFilter (This, &IpFilter);\r
+\r
   return Status;\r
 }\r
 \r
 \r
-/**                                                                 \r
+/**\r
   Attempts to complete the PXE Boot Server and/or boot image discovery sequence.\r
 \r
   This function attempts to complete the PXE Boot Server and/or boot image discovery\r
@@ -716,26 +781,26 @@ ON_EXIT:
   additional details on the implementation of the Discovery sequence.\r
   If the Callback Protocol does not return EFI_PXE_BASE_CODE_CALLBACK_STATUS_CONTINUE,\r
   then the Discovery sequence is stopped and EFI_ABORTED will be returned.\r
-    \r
+\r
   @param  This                  Pointer to the EFI_PXE_BASE_CODE_PROTOCOL instance.\r
   @param  Type                  The type of bootstrap to perform.\r
   @param  Layer                 Pointer to the boot server layer number to discover, which must be\r
-                                PXE_BOOT_LAYER_INITIAL when a new server type is being            \r
-                                discovered.                                                       \r
-  @param  UseBis                TRUE if Boot Integrity Services are to be used. FALSE otherwise.                                \r
+                                PXE_BOOT_LAYER_INITIAL when a new server type is being\r
+                                discovered.\r
+  @param  UseBis                TRUE if Boot Integrity Services are to be used. FALSE otherwise.\r
   @param  Info                  Pointer to a data structure that contains additional information on the\r
-                                type of discovery operation that is to be performed.                   \r
-                                  \r
+                                type of discovery operation that is to be performed.\r
+\r
   @retval EFI_SUCCESS           The Discovery sequence has been completed.\r
   @retval EFI_NOT_STARTED       The PXE Base Code Protocol is in the stopped state.\r
-  @retval EFI_INVALID_PARAMETER One or more parameters are invalid.                                \r
-  @retval EFI_DEVICE_ERROR      The network device encountered an error during this operation.                                \r
+  @retval EFI_INVALID_PARAMETER One or more parameters are invalid.\r
+  @retval EFI_DEVICE_ERROR      The network device encountered an error during this operation.\r
   @retval EFI_OUT_OF_RESOURCES  Could not allocate enough memory to complete Discovery.\r
   @retval EFI_ABORTED           The callback function aborted the Discovery sequence.\r
   @retval EFI_TIMEOUT           The Discovery sequence timed out.\r
   @retval EFI_ICMP_ERROR        An ICMP error packet was received during the PXE discovery\r
-                                session.                                                  \r
-                                       \r
+                                session.\r
+\r
 **/\r
 EFI_STATUS\r
 EFIAPI\r
@@ -750,6 +815,7 @@ EfiPxeBcDiscover (
   PXEBC_PRIVATE_DATA              *Private;\r
   EFI_PXE_BASE_CODE_MODE          *Mode;\r
   EFI_PXE_BASE_CODE_DISCOVER_INFO DefaultInfo;\r
+  EFI_PXE_BASE_CODE_DISCOVER_INFO *CreatedInfo;\r
   EFI_PXE_BASE_CODE_SRVLIST       *SrvList;\r
   EFI_PXE_BASE_CODE_SRVLIST       DefaultSrvList;\r
   PXEBC_CACHED_DHCP4_PACKET       *Packet;\r
@@ -757,6 +823,7 @@ EfiPxeBcDiscover (
   UINT16                          Index;\r
   EFI_STATUS                      Status;\r
   PXEBC_BOOT_SVR_ENTRY            *BootSvrEntry;\r
+  EFI_PXE_BASE_CODE_IP_FILTER     IpFilter;\r
 \r
   if (This == NULL) {\r
     return EFI_INVALID_PARAMETER;\r
@@ -766,6 +833,7 @@ EfiPxeBcDiscover (
   Mode              = Private->PxeBc.Mode;\r
   BootSvrEntry      = NULL;\r
   SrvList           = NULL;\r
+  CreatedInfo       = NULL;\r
   Status            = EFI_DEVICE_ERROR;\r
   Private->Function = EFI_PXE_BASE_CODE_FUNCTION_DISCOVER;\r
 \r
@@ -777,6 +845,11 @@ EfiPxeBcDiscover (
     return EFI_NOT_STARTED;\r
   }\r
 \r
+  //\r
+  // Stop Udp4Read instance\r
+  //\r
+  Private->Udp4Read->Configure (Private->Udp4Read, NULL);\r
+\r
   Mode->IcmpErrorReceived = FALSE;\r
 \r
   //\r
@@ -785,11 +858,13 @@ EfiPxeBcDiscover (
   // If info isn't offered,\r
   //   use the cached DhcpAck and ProxyOffer packets.\r
   //\r
+  ZeroMem (&DefaultInfo, sizeof (EFI_PXE_BASE_CODE_DISCOVER_INFO));\r
   if (*Layer != EFI_PXE_BASE_CODE_BOOT_LAYER_INITIAL) {\r
 \r
     if (!Mode->PxeDiscoverValid || !Mode->PxeReplyReceived || (!Mode->PxeBisReplyReceived && UseBis)) {\r
 \r
-      return EFI_INVALID_PARAMETER;\r
+      Status = EFI_INVALID_PARAMETER;\r
+      goto ON_EXIT;\r
     }\r
 \r
     DefaultInfo.IpCnt                 = 1;\r
@@ -812,7 +887,8 @@ EfiPxeBcDiscover (
       //\r
       // Address is not acquired or no discovery options.\r
       //\r
-      return EFI_INVALID_PARAMETER;\r
+      Status = EFI_INVALID_PARAMETER;\r
+      goto ON_EXIT;\r
     }\r
 \r
     DefaultInfo.UseMCast    = (BOOLEAN)!IS_DISABLE_MCAST_DISCOVER (VendorOpt->DiscoverCtrl);\r
@@ -825,13 +901,15 @@ EfiPxeBcDiscover (
       // Get the multicast discover ip address from vendor option.\r
       //\r
       CopyMem (\r
-        &DefaultInfo.ServerMCastIp.Addr, \r
-        &VendorOpt->DiscoverMcastIp, \r
+        &DefaultInfo.ServerMCastIp.Addr,\r
+        &VendorOpt->DiscoverMcastIp,\r
         sizeof (EFI_IPv4_ADDRESS)\r
         );\r
     }\r
 \r
     DefaultInfo.IpCnt = 0;\r
+    Info    = &DefaultInfo;\r
+    SrvList = Info->SrvList;\r
 \r
     if (DefaultInfo.MustUseList) {\r
       BootSvrEntry  = VendorOpt->BootSvr;\r
@@ -848,13 +926,31 @@ EfiPxeBcDiscover (
       }\r
 \r
       if (EFI_ERROR (Status)) {\r
-        return Status;\r
+        goto ON_EXIT;\r
       }\r
 \r
       DefaultInfo.IpCnt = BootSvrEntry->IpCnt;\r
+\r
+      if (DefaultInfo.IpCnt >= 1) {\r
+        CreatedInfo = AllocatePool (sizeof (DefaultInfo) + (DefaultInfo.IpCnt - 1) * sizeof (*SrvList));\r
+        if (CreatedInfo == NULL) {\r
+          Status = EFI_OUT_OF_RESOURCES;\r
+          goto ON_EXIT;\r
+\r
+        }\r
+\r
+        CopyMem (CreatedInfo, &DefaultInfo, sizeof (DefaultInfo));\r
+        Info    = CreatedInfo;\r
+        SrvList = Info->SrvList;\r
+      }\r
+\r
+      for (Index = 0; Index < DefaultInfo.IpCnt; Index++) {\r
+        CopyMem (&SrvList[Index].IpAddr, &BootSvrEntry->IpAddr[Index], sizeof (EFI_IPv4_ADDRESS));\r
+        SrvList[Index].AcceptAnyResponse   = FALSE;\r
+        SrvList[Index].Type                = BootSvrEntry->Type;\r
+      }\r
     }\r
 \r
-    Info = &DefaultInfo;\r
   } else {\r
 \r
     SrvList = Info->SrvList;\r
@@ -868,14 +964,16 @@ EfiPxeBcDiscover (
       }\r
 \r
       if (Index != Info->IpCnt) {\r
-        return EFI_INVALID_PARAMETER;\r
+        Status = EFI_INVALID_PARAMETER;\r
+        goto ON_EXIT;\r
       }\r
     }\r
   }\r
 \r
   if ((!Info->UseUCast && !Info->UseBCast && !Info->UseMCast) || (Info->MustUseList && Info->IpCnt == 0)) {\r
 \r
-    return EFI_INVALID_PARAMETER;\r
+    Status = EFI_INVALID_PARAMETER;\r
+    goto ON_EXIT;\r
   }\r
   //\r
   // Execute discover by UniCast/BroadCast/MultiCast\r
@@ -888,8 +986,8 @@ EfiPxeBcDiscover (
         Private->ServerIp.Addr[0] = SrvList[Index].IpAddr.Addr[0];\r
       } else {\r
         CopyMem (\r
-          &Private->ServerIp, \r
-          &BootSvrEntry->IpAddr[Index], \r
+          &Private->ServerIp,\r
+          &BootSvrEntry->IpAddr[Index],\r
           sizeof (EFI_IPv4_ADDRESS)\r
           );\r
       }\r
@@ -905,6 +1003,9 @@ EfiPxeBcDiscover (
                 TRUE,\r
                 &Private->PxeReply.Packet.Ack\r
                 );\r
+      if (!EFI_ERROR (Status)) {\r
+        break;\r
+      }\r
     }\r
 \r
   } else if (Info->UseMCast) {\r
@@ -942,23 +1043,40 @@ EfiPxeBcDiscover (
     } else {\r
       Status = EFI_DEVICE_ERROR;\r
     }\r
+    goto ON_EXIT;\r
   } else {\r
     PxeBcParseCachedDhcpPacket (&Private->PxeReply);\r
   }\r
 \r
   if (Mode->PxeBisReplyReceived) {\r
     CopyMem (\r
-      &Private->ServerIp, \r
-      &Mode->PxeReply.Dhcpv4.BootpSiAddr, \r
+      &Private->ServerIp,\r
+      &Mode->PxeReply.Dhcpv4.BootpSiAddr,\r
       sizeof (EFI_IPv4_ADDRESS)\r
       );\r
   }\r
 \r
+  if (CreatedInfo != NULL) {\r
+    FreePool (CreatedInfo);\r
+  }\r
+\r
+ON_EXIT:\r
+\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
+  ZeroMem(&IpFilter, sizeof (EFI_PXE_BASE_CODE_IP_FILTER));\r
+  IpFilter.Filters = EFI_PXE_BASE_CODE_IP_FILTER_STATION_IP;\r
+  This->SetIpFilter (This, &IpFilter);\r
+\r
   return Status;\r
 }\r
 \r
 \r
-/**                                                                 \r
+/**\r
   Used to perform TFTP and MTFTP services.\r
 \r
   This function is used to perform TFTP and MTFTP services. This includes the\r
@@ -1003,31 +1121,31 @@ EfiPxeBcDiscover (
   IP address preceding the filename of the form %d.%d.%d.%d for IP v4. The final\r
   entry is itself null-terminated, so that the final information string is terminated\r
   with two null octets.\r
-    \r
+\r
   @param  This                  Pointer to the EFI_PXE_BASE_CODE_PROTOCOL instance.\r
   @param  Operation             The type of operation to perform.\r
-  @param  BufferPtr             A pointer to the data buffer.                                                                     \r
+  @param  BufferPtr             A pointer to the data buffer.\r
   @param  Overwrite             Only used on write file operations. TRUE if a file on a remote server can\r
-                                be overwritten.                                                          \r
+                                be overwritten.\r
   @param  BufferSize            For get-file-size operations, *BufferSize returns the size of the\r
-                                requested file.                                                  \r
+                                requested file.\r
   @param  BlockSize             The requested block size to be used during a TFTP transfer.\r
   @param  ServerIp              The TFTP / MTFTP server IP address.\r
   @param  Filename              A Null-terminated ASCII string that specifies a directory name or a file\r
-                                name.                                                                   \r
+                                name.\r
   @param  Info                  Pointer to the MTFTP information.\r
-  @param  DontUseBuffer         Set to FALSE for normal TFTP and MTFTP read file operation.                       \r
-                                  \r
+  @param  DontUseBuffer         Set to FALSE for normal TFTP and MTFTP read file operation.\r
+\r
   @retval EFI_SUCCESS           The TFTP/MTFTP operation was completed.\r
   @retval EFI_NOT_STARTED       The PXE Base Code Protocol is in the stopped state.\r
-  @retval EFI_INVALID_PARAMETER One or more parameters are invalid.                                \r
-  @retval EFI_DEVICE_ERROR      The network device encountered an error during this operation.                                \r
-  @retval EFI_BUFFER_TOO_SMALL  The buffer is not large enough to complete the read operation.   \r
+  @retval EFI_INVALID_PARAMETER One or more parameters are invalid.\r
+  @retval EFI_DEVICE_ERROR      The network device encountered an error during this operation.\r
+  @retval EFI_BUFFER_TOO_SMALL  The buffer is not large enough to complete the read operation.\r
   @retval EFI_ABORTED           The callback function aborted the TFTP/MTFTP operation.\r
   @retval EFI_TIMEOUT           The TFTP/MTFTP operation timed out.\r
   @retval EFI_ICMP_ERROR        An ICMP error packet was received during the MTFTP session.\r
   @retval EFI_TFTP_ERROR        A TFTP error packet was received during the MTFTP session.\r
-                                                                      \r
+\r
 **/\r
 EFI_STATUS\r
 EFIAPI\r
@@ -1044,17 +1162,20 @@ EfiPxeBcMtftp (
   IN BOOLEAN                          DontUseBuffer\r
   )\r
 {\r
-  PXEBC_PRIVATE_DATA      *Private;\r
-  EFI_MTFTP4_CONFIG_DATA  Mtftp4Config;\r
-  EFI_STATUS              Status;\r
-  EFI_PXE_BASE_CODE_MODE  *Mode;\r
-  EFI_MAC_ADDRESS         TempMacAddr;\r
-\r
-  if ((This == NULL)                                                       ||\r
-      (Filename == NULL)                                                   ||\r
-      (BufferSize == NULL)                                                 ||\r
-      ((ServerIp == NULL) || !Ip4IsUnicast (NTOHL (ServerIp->Addr[0]), 0)) ||\r
-      ((BufferPtr == NULL) && DontUseBuffer)                               ||\r
+  PXEBC_PRIVATE_DATA           *Private;\r
+  EFI_MTFTP4_CONFIG_DATA       Mtftp4Config;\r
+  EFI_STATUS                   Status;\r
+  EFI_PXE_BASE_CODE_MODE       *Mode;\r
+  EFI_MAC_ADDRESS              TempMacAddr;\r
+  EFI_PXE_BASE_CODE_IP_FILTER  IpFilter;\r
+\r
+  if ((This == NULL)                                                          ||\r
+      (Filename == NULL)                                                      ||\r
+      (BufferSize == NULL)                                                    ||\r
+      ((ServerIp == NULL) ||\r
+       (IP4_IS_UNSPECIFIED (NTOHL (ServerIp->Addr[0])) ||\r
+        IP4_IS_LOCAL_BROADCAST (NTOHL (ServerIp->Addr[0]))))                  ||\r
+      ((BufferPtr == NULL) && DontUseBuffer)                                  ||\r
       ((BlockSize != NULL) && (*BlockSize < 512))) {\r
 \r
     return EFI_INVALID_PARAMETER;\r
@@ -1074,6 +1195,11 @@ EfiPxeBcMtftp (
     }\r
   }\r
 \r
+  //\r
+  // Stop Udp4Read instance\r
+  //\r
+  Private->Udp4Read->Configure (Private->Udp4Read, NULL);\r
+\r
   Mode->TftpErrorReceived = FALSE;\r
   Mode->IcmpErrorReceived = FALSE;\r
 \r
@@ -1082,23 +1208,23 @@ EfiPxeBcMtftp (
   Mtftp4Config.TryCount          = PXEBC_MTFTP_RETRIES;\r
 \r
   CopyMem (\r
-    &Mtftp4Config.StationIp, \r
-    &Private->StationIp, \r
+    &Mtftp4Config.StationIp,\r
+    &Private->StationIp,\r
     sizeof (EFI_IPv4_ADDRESS)\r
     );\r
   CopyMem (\r
-    &Mtftp4Config.SubnetMask, \r
-    &Private->SubnetMask, \r
+    &Mtftp4Config.SubnetMask,\r
+    &Private->SubnetMask,\r
     sizeof (EFI_IPv4_ADDRESS)\r
     );\r
   CopyMem (\r
-    &Mtftp4Config.GatewayIp, \r
-    &Private->GatewayIp, \r
+    &Mtftp4Config.GatewayIp,\r
+    &Private->GatewayIp,\r
     sizeof (EFI_IPv4_ADDRESS)\r
     );\r
   CopyMem (\r
-    &Mtftp4Config.ServerIp, \r
-    ServerIp, \r
+    &Mtftp4Config.ServerIp,\r
+    ServerIp,\r
     sizeof (EFI_IPv4_ADDRESS)\r
     );\r
 \r
@@ -1174,11 +1300,25 @@ EfiPxeBcMtftp (
     Mode->IcmpErrorReceived = TRUE;\r
   }\r
 \r
+  if (EFI_ERROR (Status)) {\r
+    goto ON_EXIT;\r
+  }\r
+\r
+ON_EXIT:\r
+  Private->Udp4Read->Configure (Private->Udp4Read, &Private->Udp4CfgData);\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
+  ZeroMem(&IpFilter, sizeof (EFI_PXE_BASE_CODE_IP_FILTER));\r
+  IpFilter.Filters = EFI_PXE_BASE_CODE_IP_FILTER_STATION_IP;\r
+  This->SetIpFilter (This, &IpFilter);\r
+\r
   return Status;\r
 }\r
 \r
 \r
-/**                                                                 \r
+/**\r
   Writes a UDP packet to the network interface.\r
 \r
   This function writes a UDP packet specified by the (optional HeaderPtr and)\r
@@ -1191,29 +1331,29 @@ EfiPxeBcMtftp (
   the IcmpErrorReceived field is set to TRUE, the IcmpError field is filled in and\r
   EFI_ICMP_ERROR will be returned. If the Callback Protocol does not return\r
   EFI_PXE_BASE_CODE_CALLBACK_STATUS_CONTINUE, then EFI_ABORTED will be returned.\r
-    \r
+\r
   @param  This                  Pointer to the EFI_PXE_BASE_CODE_PROTOCOL instance.\r
-  @param  OpFlags               The UDP operation flags. \r
+  @param  OpFlags               The UDP operation flags.\r
   @param  DestIp                The destination IP address.\r
-  @param  DestPort              The destination UDP port number.                                                                         \r
-  @param  GatewayIp             The gateway IP address.                                                 \r
+  @param  DestPort              The destination UDP port number.\r
+  @param  GatewayIp             The gateway IP address.\r
   @param  SrcIp                 The source IP address.\r
   @param  SrcPort               The source UDP port number.\r
   @param  HeaderSize            An optional field which may be set to the length of a header at\r
-                                HeaderPtr to be prefixed to the data at BufferPtr.             \r
+                                HeaderPtr to be prefixed to the data at BufferPtr.\r
   @param  HeaderPtr             If HeaderSize is not NULL, a pointer to a header to be prefixed to the\r
-                                data at BufferPtr.                                                    \r
+                                data at BufferPtr.\r
   @param  BufferSize            A pointer to the size of the data at BufferPtr.\r
   @param  BufferPtr             A pointer to the data to be written.\r
-                                  \r
+\r
   @retval EFI_SUCCESS           The UDP Write operation was completed.\r
   @retval EFI_NOT_STARTED       The PXE Base Code Protocol is in the stopped state.\r
-  @retval EFI_INVALID_PARAMETER One or more parameters are invalid.                                \r
-  @retval EFI_BAD_BUFFER_SIZE   The buffer is too long to be transmitted.  \r
+  @retval EFI_INVALID_PARAMETER One or more parameters are invalid.\r
+  @retval EFI_BAD_BUFFER_SIZE   The buffer is too long to be transmitted.\r
   @retval EFI_ABORTED           The callback function aborted the UDP Write operation.\r
   @retval EFI_TIMEOUT           The UDP Write operation timed out.\r
-  @retval EFI_ICMP_ERROR        An ICMP error packet was received during the UDP write session.  \r
-                                                                      \r
+  @retval EFI_ICMP_ERROR        An ICMP error packet was received during the UDP write session.\r
+\r
 **/\r
 EFI_STATUS\r
 EFIAPI\r
@@ -1249,7 +1389,7 @@ EfiPxeBcUdpWrite (
     return EFI_INVALID_PARAMETER;\r
   }\r
 \r
-  if ((GatewayIp != NULL) && !Ip4IsUnicast (NTOHL (GatewayIp->Addr[0]), 0)) {\r
+  if ((GatewayIp != NULL) && (IP4_IS_UNSPECIFIED (NTOHL (GatewayIp->Addr[0])) || IP4_IS_LOCAL_BROADCAST (NTOHL (GatewayIp->Addr[0])))) {\r
     //\r
     // Gateway is provided but it's not a unicast IP address.\r
     //\r
@@ -1299,18 +1439,20 @@ EfiPxeBcUdpWrite (
     if (SrcPort != NULL) {\r
       Private->CurrentUdpSrcPort = *SrcPort;\r
     }\r
+  }\r
 \r
-    Status = PxeBcConfigureUdpWriteInstance (\r
-               Udp4,\r
-               &Private->StationIp.v4,\r
-               &Private->SubnetMask.v4,\r
-               &Private->GatewayIp.v4,\r
-               &Private->CurrentUdpSrcPort\r
-               );\r
-    if (EFI_ERROR (Status)) {\r
-      Private->CurrentUdpSrcPort = 0;\r
-      return EFI_INVALID_PARAMETER;\r
-    }\r
+  Status = PxeBcConfigureUdpWriteInstance (\r
+             Udp4,\r
+             &Private->StationIp.v4,\r
+             &Private->SubnetMask.v4,\r
+             &Private->GatewayIp.v4,\r
+             &Private->CurrentUdpSrcPort,\r
+             Private->Mode.TTL,\r
+             Private->Mode.ToS\r
+             );\r
+  if (EFI_ERROR (Status)) {\r
+    Private->CurrentUdpSrcPort = 0;\r
+    return EFI_INVALID_PARAMETER;\r
   }\r
 \r
   ZeroMem (&Token, sizeof (EFI_UDP4_COMPLETION_TOKEN));\r
@@ -1382,8 +1524,12 @@ ON_EXIT:
     gBS->CloseEvent (Token.Event);\r
   }\r
 \r
-  gBS->FreePool (Udp4TxData);\r
+  FreePool (Udp4TxData);\r
 \r
+  //\r
+  // Reset the instance.\r
+  //\r
+  Udp4->Configure (Udp4, NULL);\r
   return Status;\r
 }\r
 \r
@@ -1413,20 +1559,20 @@ CheckIpByFilter (
   }\r
 \r
   CopyMem (&DestIp4Address, &Session->DestinationAddress, sizeof (DestIp4Address));\r
-  if ((PxeBcMode->IpFilter.Filters & EFI_PXE_BASE_CODE_IP_FILTER_PROMISCUOUS_MULTICAST) &&\r
+  if (((PxeBcMode->IpFilter.Filters & EFI_PXE_BASE_CODE_IP_FILTER_PROMISCUOUS_MULTICAST) != 0) &&\r
       IP4_IS_MULTICAST (EFI_NTOHL (DestIp4Address))\r
       ) {\r
     return TRUE;\r
   }\r
 \r
-  if ((PxeBcMode->IpFilter.Filters & EFI_PXE_BASE_CODE_IP_FILTER_BROADCAST) &&\r
+  if (((PxeBcMode->IpFilter.Filters & EFI_PXE_BASE_CODE_IP_FILTER_BROADCAST) != 0) &&\r
       IP4_IS_LOCAL_BROADCAST (EFI_NTOHL (DestIp4Address))\r
       ) {\r
     return TRUE;\r
   }\r
 \r
   CopyMem (&Ip4Address, &PxeBcMode->StationIp.v4, sizeof (Ip4Address));\r
-  if ((PxeBcMode->IpFilter.Filters & EFI_PXE_BASE_CODE_IP_FILTER_STATION_IP) &&\r
+  if (((PxeBcMode->IpFilter.Filters & EFI_PXE_BASE_CODE_IP_FILTER_STATION_IP) != 0) &&\r
       EFI_IP4_EQUAL (&Ip4Address, &DestIp4Address)\r
       ) {\r
     return TRUE;\r
@@ -1436,8 +1582,8 @@ CheckIpByFilter (
 \r
   for (Index = 0; Index < PxeBcMode->IpFilter.IpCnt; Index++) {\r
     CopyMem (\r
-      &Ip4Address, \r
-      &PxeBcMode->IpFilter.IpList[Index].v4, \r
+      &Ip4Address,\r
+      &PxeBcMode->IpFilter.IpList[Index].v4,\r
       sizeof (Ip4Address)\r
       );\r
     if (EFI_IP4_EQUAL (&Ip4Address, &DestIp4Address)) {\r
@@ -1448,7 +1594,7 @@ CheckIpByFilter (
   return FALSE;\r
 }\r
 \r
-/**                                                                 \r
+/**\r
   Reads a UDP packet from the network interface.\r
 \r
   This function reads a UDP packet from a network interface. The data contents\r
@@ -1459,31 +1605,31 @@ CheckIpByFilter (
   contents of BufferPtr are undefined, and the packet is lost. If a UDP packet is\r
   successfully received, then EFI_SUCCESS will be returned, and the information\r
   from the UDP header will be returned in DestIp, DestPort, SrcIp, and SrcPort if\r
-  they are not NULL. Depending on the values of OpFlags and the DestIp, DestPort, \r
-  SrcIp, and SrcPort input values, different types of UDP packet receive filtering \r
+  they are not NULL. Depending on the values of OpFlags and the DestIp, DestPort,\r
+  SrcIp, and SrcPort input values, different types of UDP packet receive filtering\r
   will be performed. The following tables summarize these receive filter operations.\r
-    \r
+\r
   @param  This                  Pointer to the EFI_PXE_BASE_CODE_PROTOCOL instance.\r
-  @param  OpFlags               The UDP operation flags. \r
+  @param  OpFlags               The UDP operation flags.\r
   @param  DestIp                The destination IP address.\r
   @param  DestPort              The destination UDP port number.\r
   @param  SrcIp                 The source IP address.\r
   @param  SrcPort               The source UDP port number.\r
   @param  HeaderSize            An optional field which may be set to the length of a header at\r
-                                HeaderPtr to be prefixed to the data at BufferPtr.             \r
+                                HeaderPtr to be prefixed to the data at BufferPtr.\r
   @param  HeaderPtr             If HeaderSize is not NULL, a pointer to a header to be prefixed to the\r
-                                data at BufferPtr.                                                    \r
+                                data at BufferPtr.\r
   @param  BufferSize            A pointer to the size of the data at BufferPtr.\r
   @param  BufferPtr             A pointer to the data to be read.\r
-                                  \r
+\r
   @retval EFI_SUCCESS           The UDP Read operation was completed.\r
   @retval EFI_NOT_STARTED       The PXE Base Code Protocol is in the stopped state.\r
-  @retval EFI_INVALID_PARAMETER One or more parameters are invalid.                                \r
+  @retval EFI_INVALID_PARAMETER One or more parameters are invalid.\r
   @retval EFI_DEVICE_ERROR      The network device encountered an error during this operation.\r
   @retval EFI_BUFFER_TOO_SMALL  The packet is larger than Buffer can hold.\r
   @retval EFI_ABORTED           The callback function aborted the UDP Read operation.\r
-  @retval EFI_TIMEOUT           The UDP Read operation timed out.  \r
-                                                                      \r
+  @retval EFI_TIMEOUT           The UDP Read operation timed out.\r
+\r
 **/\r
 EFI_STATUS\r
 EFIAPI\r
@@ -1509,7 +1655,13 @@ EfiPxeBcUdpRead (
   EFI_STATUS                Status;\r
   BOOLEAN                   IsDone;\r
   BOOLEAN                   Matched;\r
-  UINTN                     CopyLen;\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
@@ -1525,7 +1677,7 @@ EfiPxeBcUdpRead (
     return EFI_INVALID_PARAMETER;\r
   }\r
 \r
-  if ((BufferSize == NULL) || ((BufferPtr == NULL) && (*BufferSize != 0))) {\r
+  if ((BufferSize == NULL) || (BufferPtr == NULL)) {\r
     return EFI_INVALID_PARAMETER;\r
   }\r
 \r
@@ -1666,26 +1818,51 @@ TRY_AGAIN:
     }\r
 \r
     if (Matched) {\r
+      ASSERT (RxData != NULL);\r
 \r
-      CopyLen = 0;\r
-\r
+      HeaderLen = 0;\r
       if (HeaderSize != NULL) {\r
-        CopyLen = MIN (*HeaderSize, RxData->DataLength);\r
-        CopyMem (HeaderPtr, RxData->FragmentTable[0].FragmentBuffer, CopyLen);\r
-        *HeaderSize = CopyLen;\r
+        HeaderLen = MIN (*HeaderSize, RxData->DataLength);\r
       }\r
 \r
-      if (RxData->DataLength - CopyLen > *BufferSize) {\r
-\r
+      if (RxData->DataLength - HeaderLen > *BufferSize) {\r
         Status = EFI_BUFFER_TOO_SMALL;\r
       } else {\r
-\r
-        *BufferSize = RxData->DataLength - CopyLen;\r
-        CopyMem (\r
-          BufferPtr, \r
-          (UINT8 *) RxData->FragmentTable[0].FragmentBuffer + CopyLen, \r
-          *BufferSize\r
-          );\r
+        *HeaderSize = HeaderLen;\r
+        *BufferSize = RxData->DataLength - HeaderLen;\r
+\r
+        HeaderCopiedLen = 0;\r
+        BufferCopiedLen = 0;\r
+        for (FragmentIndex = 0; FragmentIndex < RxData->FragmentCount; FragmentIndex++) {\r
+          FragmentLength = RxData->FragmentTable[FragmentIndex].FragmentLength;\r
+          FragmentBuffer = RxData->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
     } else {\r
 \r
@@ -1711,9 +1888,9 @@ ON_EXIT:
   return Status;\r
 }\r
 \r
-/**                                                                 \r
+/**\r
   Updates the IP receive filters of a network device and enables software filtering.\r
-  \r
+\r
   The NewFilter field is used to modify the network device's current IP receive\r
   filter settings and to enable a software filter. This function updates the IpFilter\r
   field of the EFI_PXE_BASE_CODE_MODE structure with the contents of NewIpFilter.\r
@@ -1734,14 +1911,14 @@ ON_EXIT:
   The IPlist field is used to enable IPs other than the StationIP. They may be\r
   multicast or unicast. If IPcnt is set as well as EFI_PXE_BASE_CODE_IP_FILTER_STATION_IP,\r
   then both the StationIP and the IPs from the IPlist will be used.\r
-    \r
+\r
   @param  This                  Pointer to the EFI_PXE_BASE_CODE_PROTOCOL instance.\r
   @param  NewFilter             Pointer to the new set of IP receive filters.\r
-  \r
+\r
   @retval EFI_SUCCESS           The IP receive filter settings were updated.\r
   @retval EFI_NOT_STARTED       The PXE Base Code Protocol is in the stopped state.\r
-  @retval EFI_INVALID_PARAMETER One or more parameters are invalid.                                  \r
-                                                                      \r
+  @retval EFI_INVALID_PARAMETER One or more parameters are invalid.\r
+\r
 **/\r
 EFI_STATUS\r
 EFIAPI\r
@@ -1754,7 +1931,11 @@ EfiPxeBcSetIpFilter (
   PXEBC_PRIVATE_DATA        *Private;\r
   EFI_PXE_BASE_CODE_MODE    *Mode;\r
   UINTN                     Index;\r
+  EFI_UDP4_CONFIG_DATA      *Udp4Cfg;\r
   BOOLEAN                   PromiscuousNeed;\r
+  BOOLEAN                   AcceptPromiscuous;\r
+  BOOLEAN                   AcceptBroadcast;\r
+  BOOLEAN                   MultiCastUpdate;\r
 \r
   if (This == NULL) {\r
     DEBUG ((EFI_D_ERROR, "This == NULL.\n"));\r
@@ -1779,6 +1960,11 @@ EfiPxeBcSetIpFilter (
     return EFI_NOT_STARTED;\r
   }\r
 \r
+  if (Mode->UsingIpv6) {\r
+    DEBUG ((EFI_D_ERROR, "This driver is PXE for IPv4 Only.\n"));\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
   PromiscuousNeed = FALSE;\r
 \r
   for (Index = 0; Index < NewFilter->IpCnt; ++Index) {\r
@@ -1789,9 +1975,11 @@ EfiPxeBcSetIpFilter (
       DEBUG ((EFI_D_ERROR, "There is broadcast address in NewFilter.\n"));\r
       return EFI_INVALID_PARAMETER;\r
     }\r
-    if (Ip4IsUnicast (EFI_IP4 (NewFilter->IpList[Index].v4), 0) &&\r
-        (NewFilter->Filters & EFI_PXE_BASE_CODE_IP_FILTER_STATION_IP)\r
-       ) {\r
+    if ((EFI_NTOHL(Mode->StationIp) != 0) &&\r
+        (EFI_NTOHL(Mode->SubnetMask) != 0) &&\r
+        IP4_NET_EQUAL(EFI_NTOHL(Mode->StationIp), EFI_NTOHL(NewFilter->IpList[Index].v4), EFI_NTOHL(Mode->SubnetMask)) &&\r
+        NetIp4IsUnicast (EFI_IP4 (NewFilter->IpList[Index].v4), EFI_NTOHL(Mode->SubnetMask)) &&\r
+        ((NewFilter->Filters & EFI_PXE_BASE_CODE_IP_FILTER_STATION_IP) != 0)) {\r
       //\r
       // If EFI_PXE_BASE_CODE_IP_FILTER_STATION_IP is set and IP4 address is in IpList,\r
       // promiscuous mode is needed.\r
@@ -1800,54 +1988,71 @@ EfiPxeBcSetIpFilter (
     }\r
   }\r
 \r
-  //\r
-  // Clear the UDP instance configuration, all joined groups will be left\r
-  // during the operation.\r
-  //\r
-  Private->Udp4Read->Configure (Private->Udp4Read, NULL);\r
-  Private->Udp4CfgData.AcceptPromiscuous  = FALSE;\r
-  Private->Udp4CfgData.AcceptBroadcast    = FALSE;\r
+  AcceptPromiscuous = FALSE;\r
+  AcceptBroadcast   = FALSE;\r
+  MultiCastUpdate   = FALSE;\r
 \r
   if (PromiscuousNeed ||\r
-      NewFilter->Filters & EFI_PXE_BASE_CODE_IP_FILTER_PROMISCUOUS ||\r
-      NewFilter->Filters & EFI_PXE_BASE_CODE_IP_FILTER_PROMISCUOUS_MULTICAST\r
+      ((NewFilter->Filters & EFI_PXE_BASE_CODE_IP_FILTER_PROMISCUOUS) != 0) ||\r
+      ((NewFilter->Filters & EFI_PXE_BASE_CODE_IP_FILTER_PROMISCUOUS_MULTICAST) != 0)\r
      ) {\r
     //\r
-    // Configure the udp4 filter to receive all packages\r
+    // Configure the udp4 filter to receive all packages.\r
     //\r
-    Private->Udp4CfgData.AcceptPromiscuous  = TRUE;\r
-\r
+    AcceptPromiscuous  = TRUE;\r
+  } else if ((NewFilter->Filters & EFI_PXE_BASE_CODE_IP_FILTER_BROADCAST) != 0) {\r
     //\r
-    // Configure the UDP instance with the new configuration.\r
+    // Configure the udp4 filter to receive all broadcast packages.\r
     //\r
-    Status = Private->Udp4Read->Configure (Private->Udp4Read, &Private->Udp4CfgData);\r
-    if (EFI_ERROR (Status)) {\r
-      return Status;\r
-    }\r
-\r
-  } else {\r
+    AcceptBroadcast   = TRUE;\r
+  }\r
 \r
-    if (NewFilter->Filters & EFI_PXE_BASE_CODE_IP_FILTER_BROADCAST) {\r
-      //\r
-      // Configure the udp4 filter to receive all broadcast packages\r
-      //\r
-      Private->Udp4CfgData.AcceptBroadcast    = TRUE;\r
+  //\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 (!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
+  //\r
+  // Check whether we need reconfigure the UDP instance.\r
+  //\r
+  Udp4Cfg = &Private->Udp4CfgData;\r
+  if ((AcceptPromiscuous != Udp4Cfg->AcceptPromiscuous) ||\r
+      (AcceptBroadcast != Udp4Cfg->AcceptBroadcast)     || MultiCastUpdate) {\r
+    //\r
+    // Clear the UDP instance configuration, all joined groups will be left\r
+    // during the operation.\r
+    //\r
+    Private->Udp4Read->Configure (Private->Udp4Read, NULL);\r
 \r
     //\r
     // Configure the UDP instance with the new configuration.\r
     //\r
-    Status = Private->Udp4Read->Configure (Private->Udp4Read, &Private->Udp4CfgData);\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
-    if (NewFilter->Filters & EFI_PXE_BASE_CODE_IP_FILTER_STATION_IP) {\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
+          // Join the mutilcast group.\r
           //\r
           Status = Private->Udp4Read->Groups (Private->Udp4Read, TRUE, &NewFilter->IpList[Index].v4);\r
           if (EFI_ERROR (Status)) {\r
@@ -1868,32 +2073,32 @@ EfiPxeBcSetIpFilter (
 }\r
 \r
 \r
-/**                                                                 \r
+/**\r
   Uses the ARP protocol to resolve a MAC address.\r
-  \r
+\r
   This function uses the ARP protocol to resolve a MAC address. The UsingIpv6 field\r
   of the EFI_PXE_BASE_CODE_MODE structure is used to determine if IPv4 or IPv6\r
   addresses are being used. The IP address specified by IpAddr is used to resolve\r
   a MAC address. If the ARP protocol succeeds in resolving the specified address,\r
   then the ArpCacheEntries and ArpCache fields of the EFI_PXE_BASE_CODE_MODE structure\r
   are updated, and EFI_SUCCESS is returned. If MacAddr is not NULL, the resolved\r
-  MAC address is placed there as well.  If the PXE Base Code protocol is in the \r
-  stopped state, then EFI_NOT_STARTED is returned. If the ARP protocol encounters \r
-  a timeout condition while attempting to resolve an address, then EFI_TIMEOUT is \r
-  returned. If the Callback Protocol does not return EFI_PXE_BASE_CODE_CALLBACK_STATUS_CONTINUE, \r
+  MAC address is placed there as well.  If the PXE Base Code protocol is in the\r
+  stopped state, then EFI_NOT_STARTED is returned. If the ARP protocol encounters\r
+  a timeout condition while attempting to resolve an address, then EFI_TIMEOUT is\r
+  returned. If the Callback Protocol does not return EFI_PXE_BASE_CODE_CALLBACK_STATUS_CONTINUE,\r
   then EFI_ABORTED is returned.\r
-    \r
+\r
   @param  This                  Pointer to the EFI_PXE_BASE_CODE_PROTOCOL instance.\r
   @param  IpAddr                Pointer to the IP address that is used to resolve a MAC address.\r
   @param  MacAddr               If not NULL, a pointer to the MAC address that was resolved with the\r
-                                ARP protocol.                                                       \r
-                                  \r
+                                ARP protocol.\r
+\r
   @retval EFI_SUCCESS           The IP or MAC address was resolved.\r
   @retval EFI_NOT_STARTED       The PXE Base Code Protocol is in the stopped state.\r
-  @retval EFI_INVALID_PARAMETER One or more parameters are invalid.                                \r
-  @retval EFI_DEVICE_ERROR      The network device encountered an error during this operation.  \r
-  @retval EFI_ICMP_ERROR        Something error occur with the ICMP packet message. \r
-                                                                       \r
+  @retval EFI_INVALID_PARAMETER One or more parameters are invalid.\r
+  @retval EFI_DEVICE_ERROR      The network device encountered an error during this operation.\r
+  @retval EFI_ICMP_ERROR        Something error occur with the ICMP packet message.\r
+\r
 **/\r
 EFI_STATUS\r
 EFIAPI\r
@@ -1954,9 +2159,9 @@ EfiPxeBcArp (
   return EFI_SUCCESS;\r
 }\r
 \r
-/**                                                                 \r
+/**\r
   Updates the parameters that affect the operation of the PXE Base Code Protocol.\r
-  \r
+\r
   This function sets parameters that affect the operation of the PXE Base Code Protocol.\r
   The parameter specified by NewAutoArp is used to control the generation of ARP\r
   protocol packets. If NewAutoArp is TRUE, then ARP Protocol packets will be generated\r
@@ -1968,23 +2173,23 @@ EfiPxeBcArp (
   the EFI_PXE_BASE_CODE_MODE structure to NewAutoArp.\r
   The SetParameters() call must be invoked after a Callback Protocol is installed\r
   to enable the use of callbacks.\r
-    \r
+\r
   @param  This                  Pointer to the EFI_PXE_BASE_CODE_PROTOCOL instance.\r
   @param  NewAutoArp            If not NULL, a pointer to a value that specifies whether to replace the\r
-                                current value of AutoARP.                                              \r
+                                current value of AutoARP.\r
   @param  NewSendGUID           If not NULL, a pointer to a value that specifies whether to replace the\r
-                                current value of SendGUID.                                             \r
+                                current value of SendGUID.\r
   @param  NewTTL                If not NULL, a pointer to be used in place of the current value of TTL,\r
-                                the "time to live" field of the IP header.                           \r
+                                the "time to live" field of the IP header.\r
   @param  NewToS                If not NULL, a pointer to be used in place of the current value of ToS,\r
-                                the "type of service" field of the IP header.                        \r
+                                the "type of service" field of the IP header.\r
   @param  NewMakeCallback       If not NULL, a pointer to a value that specifies whether to replace the\r
-                                current value of the MakeCallback field of the Mode structure.                                                                \r
-                                  \r
+                                current value of the MakeCallback field of the Mode structure.\r
+\r
   @retval EFI_SUCCESS           The new parameters values were updated.\r
   @retval EFI_NOT_STARTED       The PXE Base Code Protocol is in the stopped state.\r
-  @retval EFI_INVALID_PARAMETER One or more parameters are invalid.                                  \r
-                                                                      \r
+  @retval EFI_INVALID_PARAMETER One or more parameters are invalid.\r
+\r
 **/\r
 EFI_STATUS\r
 EFIAPI\r
@@ -2079,9 +2284,9 @@ ON_EXIT:
   return Status;\r
 }\r
 \r
-/**                                                                 \r
+/**\r
   Updates the station IP address and/or subnet mask values of a network device.\r
-  \r
+\r
   This function updates the station IP address and/or subnet mask values of a network\r
   device. The NewStationIp field is used to modify the network device's current IP address.\r
   If NewStationIP is NULL, then the current IP address will not be modified. Otherwise,\r
@@ -2090,15 +2295,15 @@ ON_EXIT:
   mask. If NewSubnetMask is NULL, then the current subnet mask will not be modified.\r
   Otherwise, this function updates the SubnetMask field of the EFI_PXE_BASE_CODE_MODE\r
   structure with NewSubnetMask.\r
-    \r
+\r
   @param  This                  Pointer to the EFI_PXE_BASE_CODE_PROTOCOL instance.\r
-  @param  NewStationIp          Pointer to the new IP address to be used by the network device.  \r
-  @param  NewSubnetMask         Pointer to the new subnet mask to be used by the network device.                                 \r
-                                  \r
+  @param  NewStationIp          Pointer to the new IP address to be used by the network device.\r
+  @param  NewSubnetMask         Pointer to the new subnet mask to be used by the network device.\r
+\r
   @retval EFI_SUCCESS           The new station IP address and/or subnet mask were updated.\r
   @retval EFI_NOT_STARTED       The PXE Base Code Protocol is in the stopped state.\r
-  @retval EFI_INVALID_PARAMETER One or more parameters are invalid.                                  \r
-                                                                      \r
+  @retval EFI_INVALID_PARAMETER One or more parameters are invalid.\r
+\r
 **/\r
 EFI_STATUS\r
 EFIAPI\r
@@ -2116,12 +2321,16 @@ EfiPxeBcSetStationIP (
     return EFI_INVALID_PARAMETER;\r
   }\r
 \r
-  if (NewStationIp != NULL && !Ip4IsUnicast (NTOHL (NewStationIp->Addr[0]), 0)) {\r
+  if (NewSubnetMask != NULL && !IP4_IS_VALID_NETMASK (NTOHL (NewSubnetMask->Addr[0]))) {\r
     return EFI_INVALID_PARAMETER;\r
   }\r
 \r
-  if (NewSubnetMask != NULL && !IP4_IS_VALID_NETMASK (NTOHL (NewSubnetMask->Addr[0]))) {\r
-    return EFI_INVALID_PARAMETER;\r
+  if (NewStationIp != NULL) {\r
+    if (IP4_IS_UNSPECIFIED(NTOHL (NewStationIp->Addr[0])) ||\r
+        IP4_IS_LOCAL_BROADCAST(NTOHL (NewStationIp->Addr[0])) ||\r
+        (NewSubnetMask != NULL && NewSubnetMask->Addr[0] != 0 && !NetIp4IsUnicast (NTOHL (NewStationIp->Addr[0]), NTOHL (NewSubnetMask->Addr[0])))) {\r
+      return EFI_INVALID_PARAMETER;\r
+    }\r
   }\r
 \r
   Private = PXEBC_PRIVATE_DATA_FROM_PXEBC (This);\r
@@ -2151,7 +2360,7 @@ EfiPxeBcSetStationIP (
     ZeroMem (&ArpConfigData, sizeof (EFI_ARP_CONFIG_DATA));\r
 \r
     ArpConfigData.SwAddressType   = 0x0800;\r
-    ArpConfigData.SwAddressLength = sizeof (EFI_IPv4_ADDRESS);\r
+    ArpConfigData.SwAddressLength = (UINT8) sizeof (EFI_IPv4_ADDRESS);\r
     ArpConfigData.StationAddress  = &Private->StationIp.v4;\r
 \r
     Private->Arp->Configure (Private->Arp, NULL);\r
@@ -2169,36 +2378,36 @@ EfiPxeBcSetStationIP (
   return EFI_SUCCESS;\r
 }\r
 \r
-/**                                                                 \r
+/**\r
   Updates the contents of the cached DHCP and Discover packets.\r
-  \r
+\r
   The pointers to the new packets are used to update the contents of the cached\r
   packets in the EFI_PXE_BASE_CODE_MODE structure.\r
-    \r
+\r
   @param  This                   Pointer to the EFI_PXE_BASE_CODE_PROTOCOL instance.\r
   @param  NewDhcpDiscoverValid   Pointer to a value that will replace the current\r
-                                 DhcpDiscoverValid field.                        \r
+                                 DhcpDiscoverValid field.\r
   @param  NewDhcpAckReceived     Pointer to a value that will replace the current\r
-                                 DhcpAckReceived field.                          \r
+                                 DhcpAckReceived field.\r
   @param  NewProxyOfferReceived  Pointer to a value that will replace the current\r
-                                 ProxyOfferReceived field.                       \r
-  @param  NewPxeDiscoverValid    Pointer to a value that will replace the current     \r
-                                 ProxyOfferReceived field.                       \r
+                                 ProxyOfferReceived field.\r
+  @param  NewPxeDiscoverValid    Pointer to a value that will replace the current\r
+                                 ProxyOfferReceived field.\r
   @param  NewPxeReplyReceived    Pointer to a value that will replace the current\r
-                                 PxeReplyReceived field.                         \r
+                                 PxeReplyReceived field.\r
   @param  NewPxeBisReplyReceived Pointer to a value that will replace the current\r
-                                 PxeBisReplyReceived field.                      \r
-  @param  NewDhcpDiscover        Pointer to the new cached DHCP Discover packet contents.   \r
+                                 PxeBisReplyReceived field.\r
+  @param  NewDhcpDiscover        Pointer to the new cached DHCP Discover packet contents.\r
   @param  NewDhcpAck             Pointer to the new cached DHCP Ack packet contents.\r
   @param  NewProxyOffer          Pointer to the new cached Proxy Offer packet contents.\r
   @param  NewPxeDiscover         Pointer to the new cached PXE Discover packet contents.\r
   @param  NewPxeReply            Pointer to the new cached PXE Reply packet contents.\r
   @param  NewPxeBisReply         Pointer to the new cached PXE BIS Reply packet contents.\r
-                                   \r
+\r
   @retval EFI_SUCCESS            The cached packet contents were updated.\r
   @retval EFI_NOT_STARTED        The PXE Base Code Protocol is in the stopped state.\r
   @retval EFI_INVALID_PARAMETER  This is NULL or not point to a valid EFI_PXE_BASE_CODE_PROTOCOL structure.\r
-                                                                      \r
+\r
 **/\r
 EFI_STATUS\r
 EFIAPI\r
@@ -2232,8 +2441,6 @@ EfiPxeBcSetPackets (
     return EFI_NOT_STARTED;\r
   }\r
 \r
-  Private->FileSize = 0;\r
-\r
   if (NewDhcpDiscoverValid != NULL) {\r
     Mode->DhcpDiscoverValid = *NewDhcpDiscoverValid;\r
   }\r
@@ -2302,10 +2509,10 @@ EFI_PXE_BASE_CODE_PROTOCOL  mPxeBcProtocolTemplate = {
   NULL\r
 };\r
 \r
-/**                                                                 \r
+/**\r
   Callback function that is invoked when the PXE Base Code Protocol is about to transmit, has\r
-  received, or is waiting to receive a packet.                                                 \r
-  \r
+  received, or is waiting to receive a packet.\r
+\r
   This function is invoked when the PXE Base Code Protocol is about to transmit, has received,\r
   or is waiting to receive a packet. Parameters Function and Received specify the type of event.\r
   Parameters PacketLen and Packet specify the packet that generated the event. If these fields\r
@@ -2315,19 +2522,19 @@ EFI_PXE_BASE_CODE_PROTOCOL  mPxeBcProtocolTemplate = {
   the polling nature of UEFI device drivers, a callback function should not execute for more than 5 ms.\r
   The SetParameters() function must be called after a Callback Protocol is installed to enable the\r
   use of callbacks.\r
-  \r
+\r
   @param  This                  Pointer to the EFI_PXE_BASE_CODE_CALLBACK_PROTOCOL instance.\r
-  @param  Function              The PXE Base Code Protocol function that is waiting for an event.                                                              \r
+  @param  Function              The PXE Base Code Protocol function that is waiting for an event.\r
   @param  Received              TRUE if the callback is being invoked due to a receive event. FALSE if\r
-                                the callback is being invoked due to a transmit event.                \r
+                                the callback is being invoked due to a transmit event.\r
   @param  PacketLength          The length, in bytes, of Packet. This field will have a value of zero if\r
-                                this is a wait for receive event.                                       \r
+                                this is a wait for receive event.\r
   @param  PacketPtr             If Received is TRUE, a pointer to the packet that was just received;\r
-                                otherwise a pointer to the packet that is about to be transmitted.  \r
-                                  \r
-  @retval EFI_PXE_BASE_CODE_CALLBACK_STATUS_CONTINUE if Function specifies a continue operation    \r
+                                otherwise a pointer to the packet that is about to be transmitted.\r
+\r
+  @retval EFI_PXE_BASE_CODE_CALLBACK_STATUS_CONTINUE if Function specifies a continue operation\r
   @retval EFI_PXE_BASE_CODE_CALLBACK_STATUS_ABORT    if Function specifies an abort operation\r
-                                   \r
+\r
 **/\r
 EFI_PXE_BASE_CODE_CALLBACK_STATUS\r
 EFIAPI\r
@@ -2404,15 +2611,15 @@ EFI_PXE_BASE_CODE_CALLBACK_PROTOCOL mPxeBcCallBackTemplate = {
 /**\r
   Find the boot file.\r
 \r
-  @param  Private      Pointer to PxeBc private data.                                                \r
-  @param  BufferSize   Pointer to buffer size.                                                  \r
-  @param  Buffer       Pointer to buffer.                               \r
+  @param  Private      Pointer to PxeBc private data.\r
+  @param  BufferSize   Pointer to buffer size.\r
+  @param  Buffer       Pointer to buffer.\r
 \r
   @retval EFI_SUCCESS          Discover the boot file successfully.\r
   @retval EFI_TIMEOUT          The TFTP/MTFTP operation timed out.\r
   @retval EFI_ABORTED          PXE bootstrap server, so local boot need abort.\r
   @retval EFI_BUFFER_TOO_SMALL The buffer is too small to load the boot file.\r
-  \r
+\r
 **/\r
 EFI_STATUS\r
 DiscoverBootFile (\r
@@ -2427,7 +2634,6 @@ DiscoverBootFile (
   UINT16                      Type;\r
   UINT16                      Layer;\r
   BOOLEAN                     UseBis;\r
-  UINTN                       BlockSize;\r
   PXEBC_CACHED_DHCP4_PACKET   *Packet;\r
   UINT16                      Value;\r
 \r
@@ -2472,7 +2678,6 @@ DiscoverBootFile (
   }\r
 \r
   *BufferSize = 0;\r
-  BlockSize   = 0x8000;\r
 \r
   //\r
   // Get bootfile name and (m)tftp server ip addresss\r
@@ -2486,9 +2691,11 @@ DiscoverBootFile (
   }\r
 \r
   //\r
-  // use option 54, if zero, use siaddr in header\r
+  // Use siaddr(next server) in DHCPOFFER packet header, if zero, use option 54(server identifier)\r
+  // in DHCPOFFER packet.\r
+  // (It does not comply with PXE Spec, Ver2.1)\r
   //\r
-  if (Packet->Dhcp4Option[PXEBC_DHCP4_TAG_INDEX_SERVER_ID] != NULL) {\r
+  if (EFI_IP4_EQUAL (&Packet->Packet.Offer.Dhcp4.Header.ServerAddr, &mZeroIp4Addr)) {\r
     CopyMem (\r
       &Private->ServerIp,\r
       Packet->Dhcp4Option[PXEBC_DHCP4_TAG_INDEX_SERVER_ID]->Data,\r
@@ -2496,8 +2703,8 @@ DiscoverBootFile (
       );\r
   } else {\r
     CopyMem (\r
-      &Private->ServerIp, \r
-      &Packet->Packet.Offer.Dhcp4.Header.ServerAddr, \r
+      &Private->ServerIp,\r
+      &Packet->Packet.Offer.Dhcp4.Header.ServerAddr,\r
       sizeof (EFI_IPv4_ADDRESS)\r
       );\r
   }\r
@@ -2530,7 +2737,7 @@ DiscoverBootFile (
                       Buffer,\r
                       FALSE,\r
                       BufferSize,\r
-                      &BlockSize,\r
+                      &Private->BlockSize,\r
                       &Private->ServerIp,\r
                       (UINT8 *) Private->BootFileName,\r
                       NULL,\r
@@ -2540,6 +2747,14 @@ DiscoverBootFile (
 \r
   Private->FileSize = (UINTN) *BufferSize;\r
 \r
+  //\r
+  // Display all the information: boot server address, boot file name and boot file size.\r
+  //\r
+  AsciiPrint ("\n  Server IP address is ");\r
+  PxeBcShowIp4Addr (&Private->ServerIp.v4);\r
+  AsciiPrint ("\n  NBP filename is %a", Private->BootFileName);\r
+  AsciiPrint ("\n  NBP filesize is %d Bytes", Private->FileSize);\r
+\r
   return Status;\r
 }\r
 \r
@@ -2548,21 +2763,21 @@ DiscoverBootFile (
 \r
   @param  This                  Protocol instance pointer.\r
   @param  FilePath              The device specific path of the file to load.\r
-  @param  BootPolicy            If TRUE, indicates that the request originates from the \r
+  @param  BootPolicy            If TRUE, indicates that the request originates from the\r
                                 boot manager is attempting to load FilePath as a boot\r
                                 selection. If FALSE, then FilePath must match as exact file\r
                                 to be loaded.\r
   @param  BufferSize            On input the size of Buffer in bytes. On output with a return\r
-                                code of EFI_SUCCESS, the amount of data transferred to \r
+                                code of EFI_SUCCESS, the amount of data transferred to\r
                                 Buffer. On output with a return code of EFI_BUFFER_TOO_SMALL,\r
                                 the size of Buffer required to retrieve the requested file.\r
   @param  Buffer                The memory buffer to transfer the file to. IF Buffer is NULL,\r
-                                then no the size of the requested file is returned in \r
+                                then no the size of the requested file is returned in\r
                                 BufferSize.\r
 \r
   @retval EFI_SUCCESS                 The file was loaded.\r
   @retval EFI_UNSUPPORTED             The device does not support the provided BootPolicy\r
-  @retval EFI_INVALID_PARAMETER       FilePath is not a valid device path, or \r
+  @retval EFI_INVALID_PARAMETER       FilePath is not a valid device path, or\r
                                       BufferSize is NULL.\r
   @retval EFI_NO_MEDIA                No medium was present to load the file.\r
   @retval EFI_DEVICE_ERROR            The file was not loaded due to a device error.\r
@@ -2584,14 +2799,17 @@ EfiPxeLoadFile (
   PXEBC_PRIVATE_DATA          *Private;\r
   EFI_PXE_BASE_CODE_PROTOCOL  *PxeBc;\r
   BOOLEAN                     NewMakeCallback;\r
-  UINTN                       BlockSize;\r
   EFI_STATUS                  Status;\r
   UINT64                      TmpBufSize;\r
+  EFI_STATUS                  MediaStatus;\r
+\r
+  if (FilePath == NULL || !IsDevicePathEnd (FilePath)) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
 \r
   Private         = PXEBC_PRIVATE_DATA_FROM_LOADFILE (This);\r
   PxeBc           = &Private->PxeBc;\r
   NewMakeCallback = FALSE;\r
-  BlockSize       = 0x8000;\r
   Status          = EFI_DEVICE_ERROR;\r
 \r
   if (This == NULL || BufferSize == NULL) {\r
@@ -2606,6 +2824,15 @@ EfiPxeLoadFile (
     return EFI_UNSUPPORTED;\r
   }\r
 \r
+  //\r
+  // Check media status before PXE start\r
+  //\r
+  MediaStatus = EFI_SUCCESS;\r
+  NetLibDetectMediaWaitTimeout (Private->Controller, PXEBC_CHECK_MEDIA_WAITING_TIME, &MediaStatus);\r
+  if (MediaStatus != EFI_SUCCESS) {\r
+    return EFI_NO_MEDIA;\r
+  }\r
+\r
   Status = PxeBc->Start (PxeBc, FALSE);\r
   if (EFI_ERROR (Status) && (Status != EFI_ALREADY_STARTED)) {\r
     return Status;\r
@@ -2643,6 +2870,7 @@ EfiPxeLoadFile (
     if (sizeof (UINTN) < sizeof (UINT64) && (TmpBufSize > 0xFFFFFFFF)) {\r
       Status = EFI_DEVICE_ERROR;\r
     } else if (TmpBufSize > 0 && *BufferSize >= (UINTN) TmpBufSize && Buffer != NULL) {\r
+      AsciiPrint ("\n Downloading NBP file...\n");\r
       *BufferSize = (UINTN) TmpBufSize;\r
       Status = PxeBc->Mtftp (\r
                         PxeBc,\r
@@ -2650,7 +2878,7 @@ EfiPxeLoadFile (
                         Buffer,\r
                         FALSE,\r
                         &TmpBufSize,\r
-                        &BlockSize,\r
+                        &Private->BlockSize,\r
                         &Private->ServerIp,\r
                         (UINT8 *) Private->BootFileName,\r
                         NULL,\r
@@ -2667,6 +2895,7 @@ EfiPxeLoadFile (
     //\r
     // Download the file.\r
     //\r
+    AsciiPrint ("\n Downloading NBP file...\n");\r
     TmpBufSize = (UINT64) (*BufferSize);\r
     Status = PxeBc->Mtftp (\r
                       PxeBc,\r
@@ -2674,7 +2903,7 @@ EfiPxeLoadFile (
                       Buffer,\r
                       FALSE,\r
                       &TmpBufSize,\r
-                      &BlockSize,\r
+                      &Private->BlockSize,\r
                       &Private->ServerIp,\r
                       (UINT8 *) Private->BootFileName,\r
                       NULL,\r
@@ -2701,12 +2930,25 @@ EfiPxeLoadFile (
   // Check download status\r
   //\r
   if (Status == EFI_SUCCESS) {\r
+    AsciiPrint ("\n  NBP file downloaded successfully.\n");\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 the other programs which\r
+    // also need to use DHCP4 will be impacted.\r
+    // The functionality of PXE Base Code protocol will not be stopped,\r
+    // when downloading is successfully.\r
+    //\r
+    Private->Dhcp4->Stop (Private->Dhcp4);\r
+    Private->Dhcp4->Configure (Private->Dhcp4, NULL);\r
     return EFI_SUCCESS;\r
 \r
   } else if (Status == EFI_BUFFER_TOO_SMALL) {\r
     if (Buffer != NULL) {\r
       AsciiPrint ("PXE-E05: Download buffer is smaller than requested file.\n");\r
     } else {\r
+      //\r
+      // The functionality of PXE Base Code protocol will not be stopped.\r
+      //\r
       return Status;\r
     }\r
 \r