]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Universal/Network/UefiPxeBcDxe/PxeBcImpl.c
Use siaddr in DHCP packet, if zero, use option 54 instead.
[mirror_edk2.git] / MdeModulePkg / Universal / Network / UefiPxeBcDxe / PxeBcImpl.c
index 85542f2b2e02ef1752e1af3c63fa47d92e29bb57..659f638930eaac63a506075ff835476ea62fb5cb 100644 (file)
@@ -1,6 +1,7 @@
 /** @file\r
-\r
-Copyright (c) 2007, Intel Corporation\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
 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
@@ -9,38 +10,302 @@ http://opensource.org/licenses/bsd-license.php
 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
 \r
-Module Name:\r
+**/\r
+\r
+\r
+#include "PxeBcImpl.h"\r
 \r
-  PxeBcImpl.c\r
+UINT32  mPxeDhcpTimeout[4] = { 4, 8, 16, 32 };\r
 \r
-Abstract:\r
+/**\r
+  Get and record the arp cache.\r
 \r
-  Interface routines for PxeBc\r
+  @param  This                    Pointer to EFI_PXE_BC_PROTOCOL\r
 \r
+  @retval EFI_SUCCESS             Arp cache updated successfully\r
+  @retval others                  If error occurs when getting arp cache\r
 \r
 **/\r
+EFI_STATUS\r
+UpdateArpCache (\r
+  IN EFI_PXE_BASE_CODE_PROTOCOL     * This\r
+  )\r
+{\r
+  PXEBC_PRIVATE_DATA      *Private;\r
+  EFI_PXE_BASE_CODE_MODE  *Mode;\r
+  EFI_STATUS              Status;\r
+  UINT32                  EntryLength;\r
+  UINT32                  EntryCount;\r
+  EFI_ARP_FIND_DATA       *Entries;\r
+  UINT32                  Index;\r
 \r
+  Private = PXEBC_PRIVATE_DATA_FROM_PXEBC (This);\r
+  Mode    = Private->PxeBc.Mode;\r
 \r
-#include "PxeBcImpl.h"\r
+  Status = Private->Arp->Find (\r
+                     Private->Arp,\r
+                     TRUE,\r
+                     NULL,\r
+                     &EntryLength,\r
+                     &EntryCount,\r
+                     &Entries,\r
+                     TRUE\r
+                     );\r
+  if (EFI_ERROR (Status)) {\r
+    return Status;\r
+  }\r
+\r
+  Mode->ArpCacheEntries = MIN (\r
+                           EntryCount,\r
+                           EFI_PXE_BASE_CODE_MAX_ARP_ENTRIES\r
+                           );\r
+  for (Index = 0; Index < Mode->ArpCacheEntries; Index ++) {\r
+    CopyMem (\r
+      &Mode->ArpCache[Index].IpAddr, \r
+      Entries + 1, \r
+      Entries->SwAddressLength\r
+      );\r
+    CopyMem (\r
+      &Mode->ArpCache[Index].MacAddr,\r
+      (UINT8 *) (Entries + 1) + Entries->SwAddressLength,\r
+      Entries->HwAddressLength\r
+      );\r
+    //\r
+    // Slip to the next FindData.\r
+    //\r
+    Entries = (EFI_ARP_FIND_DATA *) ((UINT8 *) Entries + EntryLength);\r
+  }\r
 \r
+  return EFI_SUCCESS;\r
+}\r
 \r
 /**\r
-  GC_NOTO: Add function description\r
-\r
-  @param  This                                        GC_NOTO: add argument\r
-                                                      description\r
-  @param  UseIpv6                                     GC_NOTO: add argument\r
-                                                      description\r
-\r
-  @retval EFI_INVALID_PARAMETER                       GC_NOTO: Add description for\r
-                                                      return value\r
-  @retval EFI_ALREADY_STARTED                         GC_NOTO: Add description for\r
-                                                      return value\r
-  @retval EFI_UNSUPPORTED                             GC_NOTO: Add description for\r
-                                                      return value\r
-  @retval EFI_SUCCESS                                 GC_NOTO: Add description for\r
-                                                      return value\r
+  Timeout routine to update arp cache.\r
+\r
+  @param  Event              Pointer to EFI_PXE_BC_PROTOCOL\r
+  @param  Context            Context of the timer event\r
 \r
+**/\r
+VOID\r
+EFIAPI\r
+ArpCacheUpdateTimeout (\r
+  IN EFI_EVENT    Event,\r
+  IN VOID         *Context\r
+  )\r
+{\r
+  UpdateArpCache ((EFI_PXE_BASE_CODE_PROTOCOL *) Context);\r
+}\r
+\r
+/**\r
+  Do arp resolution from arp cache in PxeBcMode.\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
+  @retval TRUE           The resolution is successful.\r
+  @retval FALSE          Otherwise.\r
+\r
+**/\r
+BOOLEAN\r
+FindInArpCache (\r
+  IN  EFI_PXE_BASE_CODE_MODE    *PxeBcMode,\r
+  IN  EFI_IPv4_ADDRESS          *Ip4Addr,\r
+  OUT EFI_MAC_ADDRESS           *MacAddress\r
+  )\r
+{\r
+  UINT32                  Index;\r
+\r
+  for (Index = 0; Index < PxeBcMode->ArpCacheEntries; Index ++) {\r
+    if (EFI_IP4_EQUAL (&PxeBcMode->ArpCache[Index].IpAddr.v4, Ip4Addr)) {\r
+      CopyMem (\r
+        MacAddress,\r
+        &PxeBcMode->ArpCache[Index].MacAddr,\r
+        sizeof (EFI_MAC_ADDRESS)\r
+        );\r
+      return TRUE;\r
+    }\r
+  }\r
+\r
+  return FALSE;\r
+}\r
+\r
+/**\r
+  Notify function for the ICMP receive token, used to process\r
+  the received ICMP packets.\r
+\r
+  @param  Context               The PXEBC private data.\r
+\r
+**/\r
+VOID\r
+EFIAPI\r
+IcmpErrorListenHandlerDpc (\r
+  IN VOID      *Context\r
+  )\r
+{\r
+  EFI_STATUS              Status;\r
+  EFI_IP4_RECEIVE_DATA    *RxData;\r
+  EFI_IP4_PROTOCOL        *Ip4;\r
+  PXEBC_PRIVATE_DATA      *Private;\r
+  EFI_PXE_BASE_CODE_MODE  *Mode;\r
+  UINTN                   Index;\r
+  UINT32                  CopiedLen;\r
+  UINT8                   *CopiedPointer;\r
+\r
+  Private = (PXEBC_PRIVATE_DATA *) Context;\r
+  Mode    = &Private->Mode;\r
+  Status  = Private->IcmpErrorRcvToken.Status;\r
+  RxData  = Private->IcmpErrorRcvToken.Packet.RxData;\r
+  Ip4     = Private->Ip4;\r
+\r
+  if (Status == EFI_ABORTED) {\r
+    //\r
+    // The reception is actively aborted by the consumer, directly return.\r
+    //\r
+    return;\r
+  }\r
+\r
+  if (EFI_ERROR (Status) || (RxData == NULL)) {\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
+    //\r
+    goto Resume;\r
+  }\r
+\r
+  if (EFI_IP4 (RxData->Header->SourceAddress) != 0 &&\r
+      !Ip4IsUnicast (EFI_NTOHL (RxData->Header->SourceAddress), 0)) {\r
+    //\r
+    // The source address is not zero and it's not a unicast IP address, discard it.\r
+    //\r
+    goto CleanUp;\r
+  }\r
+\r
+  if (!EFI_IP4_EQUAL (&RxData->Header->DestinationAddress, &Mode->StationIp.v4)) {\r
+    //\r
+    // The dest address is not equal to Station Ip address, discard it.\r
+    //\r
+    goto CleanUp;\r
+  }\r
+\r
+  //\r
+  // Constructor ICMP error packet\r
+  //\r
+  CopiedLen = 0;\r
+  CopiedPointer = (UINT8 *) &Mode->IcmpError;\r
+\r
+  for (Index = 0; Index < RxData->FragmentCount; Index ++) {\r
+    CopiedLen += RxData->FragmentTable[Index].FragmentLength;\r
+    if (CopiedLen <= sizeof (EFI_PXE_BASE_CODE_ICMP_ERROR)) {\r
+      CopyMem (\r
+        CopiedPointer,\r
+        RxData->FragmentTable[Index].FragmentBuffer,\r
+        RxData->FragmentTable[Index].FragmentLength\r
+        );\r
+    } else {\r
+      CopyMem (\r
+        CopiedPointer,\r
+        RxData->FragmentTable[Index].FragmentBuffer,\r
+        CopiedLen - sizeof (EFI_PXE_BASE_CODE_ICMP_ERROR)\r
+        );\r
+    }\r
+    CopiedPointer += CopiedLen;\r
+  }\r
+\r
+  goto Resume;\r
+\r
+CleanUp:\r
+  gBS->SignalEvent (RxData->RecycleSignal);\r
+\r
+Resume:\r
+  Ip4->Receive (Ip4, &(Private->IcmpErrorRcvToken));\r
+}\r
+\r
+/**\r
+  Request IcmpErrorListenHandlerDpc as a DPC at TPL_CALLBACK\r
+\r
+  @param  Event                 The event signaled.\r
+  @param  Context               The context passed in by the event notifier.\r
+\r
+**/\r
+VOID\r
+EFIAPI\r
+IcmpErrorListenHandler (\r
+  IN EFI_EVENT Event,\r
+  IN VOID      *Context\r
+  )\r
+{\r
+  //\r
+  // Request IpIoListenHandlerDpc as a DPC at TPL_CALLBACK\r
+  //\r
+  QueueDpc (TPL_CALLBACK, IcmpErrorListenHandlerDpc, Context);\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
+  Started field of the EFI_PXE_BASE_CODE_MODE structure is already TRUE, then\r
+  EFI_ALREADY_STARTED will be returned. If UseIpv6 is TRUE, then IPv6 formatted\r
+  addresses will be used in this session. If UseIpv6 is FALSE, then IPv4 formatted\r
+  addresses will be used in this session. If UseIpv6 is TRUE, and the Ipv6Supported\r
+  field of the EFI_PXE_BASE_CODE_MODE structure is FALSE, then EFI_UNSUPPORTED will\r
+  be returned. If there is not enough memory or other resources to start the PXE\r
+  Base Code Protocol, then EFI_OUT_OF_RESOURCES will be returned. Otherwise, the\r
+  PXE Base Code Protocol will be started, and all of the fields of the EFI_PXE_BASE_CODE_MODE\r
+  structure will be initialized as follows:\r
+    StartedSet to TRUE.\r
+    Ipv6SupportedUnchanged.\r
+    Ipv6AvailableUnchanged.\r
+    UsingIpv6Set to UseIpv6.\r
+    BisSupportedUnchanged.\r
+    BisDetectedUnchanged.\r
+    AutoArpSet to TRUE.\r
+    SendGUIDSet to FALSE.\r
+    TTLSet to DEFAULT_TTL.\r
+    ToSSet to DEFAULT_ToS.\r
+    DhcpCompletedSet to FALSE.\r
+    ProxyOfferReceivedSet to FALSE.\r
+    StationIpSet to an address of all zeros.\r
+    SubnetMaskSet to a subnet mask of all zeros.\r
+    DhcpDiscoverZero-filled.\r
+    DhcpAckZero-filled.\r
+    ProxyOfferZero-filled.\r
+    PxeDiscoverValidSet to FALSE.\r
+    PxeDiscoverZero-filled.\r
+    PxeReplyValidSet to FALSE.\r
+    PxeReplyZero-filled.\r
+    PxeBisReplyValidSet to FALSE.\r
+    PxeBisReplyZero-filled.\r
+    IpFilterSet the Filters field to 0 and the IpCnt field to 0.\r
+    ArpCacheEntriesSet to 0.\r
+    ArpCacheZero-filled.\r
+    RouteTableEntriesSet to 0.\r
+    RouteTableZero-filled.\r
+    IcmpErrorReceivedSet to FALSE.\r
+    IcmpErrorZero-filled.\r
+    TftpErroReceivedSet to FALSE.\r
+    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
+  @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
+  @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_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
+  @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
 **/\r
 EFI_STATUS\r
 EFIAPI\r
@@ -74,7 +339,10 @@ EfiPxeBcStart (
   //\r
   // Configure the udp4 instance to let it receive data\r
   //\r
-  Status = Private->Udp4->Configure (Private->Udp4, &Private->Udp4CfgData);\r
+  Status = Private->Udp4Read->Configure (\r
+                               Private->Udp4Read, \r
+                               &Private->Udp4CfgData\r
+                               );\r
   if (EFI_ERROR (Status)) {\r
     return Status;\r
   }\r
@@ -88,23 +356,97 @@ EfiPxeBcStart (
   Mode->ToS     = DEFAULT_ToS;\r
   Mode->AutoArp = TRUE;\r
 \r
-  return EFI_SUCCESS;\r
-}\r
+  //\r
+  // Create the event for Arp Cache checking.\r
+  //\r
+  Status = gBS->CreateEvent (\r
+                  EVT_TIMER | EVT_NOTIFY_SIGNAL,\r
+                  TPL_CALLBACK,\r
+                  ArpCacheUpdateTimeout,\r
+                  This,\r
+                  &Private->GetArpCacheEvent\r
+                  );\r
+  if (EFI_ERROR (Status)) {\r
+    goto ON_EXIT;\r
+  }\r
+\r
+  //\r
+  // Start the timeout timer event.\r
+  //\r
+  Status = gBS->SetTimer (\r
+                  Private->GetArpCacheEvent,\r
+                  TimerPeriodic,\r
+                  TICKS_PER_SECOND\r
+                  );\r
 \r
+  if (EFI_ERROR (Status)) {\r
+    goto ON_EXIT;\r
+  }\r
 \r
-/**\r
-  GC_NOTO: Add function description\r
+  //\r
+  // Create ICMP error receiving event\r
+  //\r
+  Status = gBS->CreateEvent (\r
+                  EVT_NOTIFY_SIGNAL,\r
+                  TPL_NOTIFY,\r
+                  IcmpErrorListenHandler,\r
+                  Private,\r
+                  &(Private->IcmpErrorRcvToken.Event)\r
+                  );\r
+  if (EFI_ERROR (Status)) {\r
+    goto ON_EXIT;\r
+  }\r
 \r
-  @param  This                                        GC_NOTO: add argument\r
-                                                      description\r
+  Status = Private->Ip4->Configure (Private->Ip4, &Private->Ip4ConfigData);\r
+  if (EFI_ERROR (Status)) {\r
+    goto ON_EXIT;\r
+  }\r
 \r
-  @retval EFI_INVALID_PARAMETER                       GC_NOTO: Add description for\r
-                                                      return value\r
-  @retval EFI_NOT_STARTED                             GC_NOTO: Add description for\r
-                                                      return value\r
-  @retval EFI_SUCCESS                                 GC_NOTO: Add description for\r
-                                                      return value\r
+  //\r
+  // start to listen incoming packet\r
+  //\r
+  Status = Private->Ip4->Receive (Private->Ip4, &Private->IcmpErrorRcvToken);\r
+  if (!EFI_ERROR (Status)) {\r
+    return Status;\r
+  }\r
+\r
+ON_EXIT:\r
+  Private->Ip4->Configure (Private->Ip4, NULL);\r
+\r
+  if (Private->IcmpErrorRcvToken.Event != NULL) {\r
+    gBS->CloseEvent (Private->IcmpErrorRcvToken.Event);\r
+  }\r
+\r
+  if (Private->GetArpCacheEvent != NULL) {\r
+    gBS->SetTimer (Private->GetArpCacheEvent, TimerCancel, 0);\r
+    gBS->CloseEvent (Private->GetArpCacheEvent);\r
+  }\r
 \r
+  Mode->Started = FALSE;\r
+  Mode->TTL     = 0;\r
+  Mode->ToS     = 0;\r
+  Mode->AutoArp = FALSE;\r
+\r
+  return Status;\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
+  @param  This                  Pointer to the EFI_PXE_BASE_CODE_PROTOCOL instance.\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_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
 **/\r
 EFI_STATUS\r
 EFIAPI\r
@@ -126,9 +468,36 @@ EfiPxeBcStop (
     return EFI_NOT_STARTED;\r
   }\r
 \r
+  Private->Ip4->Cancel (Private->Ip4, NULL);\r
+  //\r
+  // Dispatch the DPCs queued by the NotifyFunction of the canceled rx token's\r
+  // events.\r
+  //\r
+  DispatchDpc ();\r
+\r
+  Private->Ip4->Configure (Private->Ip4, NULL);\r
+\r
+  //\r
+  // Close the ICMP error receiving event.\r
+  //\r
+  gBS->CloseEvent (Private->IcmpErrorRcvToken.Event);\r
+\r
+  //\r
+  // Cancel the TimeoutEvent timer.\r
+  //\r
+  gBS->SetTimer (Private->GetArpCacheEvent, TimerCancel, 0);\r
+\r
+  //\r
+  // Close the TimeoutEvent event.\r
+  //\r
+  gBS->CloseEvent (Private->GetArpCacheEvent);\r
+\r
   Mode->Started = FALSE;\r
 \r
-  Private->Udp4->Configure (Private->Udp4, NULL);\r
+  Private->CurrentUdpSrcPort = 0;\r
+  Private->Udp4Write->Configure (Private->Udp4Write, NULL);\r
+  Private->Udp4Read->Groups (Private->Udp4Read, FALSE, NULL);\r
+  Private->Udp4Read->Configure (Private->Udp4Read, NULL);\r
 \r
   Private->Dhcp4->Stop (Private->Dhcp4);\r
   Private->Dhcp4->Configure (Private->Dhcp4, NULL);\r
@@ -139,19 +508,38 @@ EfiPxeBcStop (
 }\r
 \r
 \r
-/**\r
-  GC_NOTO: Add function description\r
-\r
-  @param  This                                        GC_NOTO: add argument\r
-                                                      description\r
-  @param  SortOffers                                  GC_NOTO: add argument\r
-                                                      description\r
-\r
-  @retval EFI_INVALID_PARAMETER                       GC_NOTO: Add description for\r
-                                                      return value\r
-  @retval EFI_NOT_STARTED                             GC_NOTO: Add description for\r
-                                                      return value\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
+  This function attempts to complete the DHCP sequence. If this sequence is completed,\r
+  then EFI_SUCCESS is returned, and the DhcpCompleted, ProxyOfferReceived, StationIp,\r
+  SubnetMask, DhcpDiscover, DhcpAck, and ProxyOffer fields of the EFI_PXE_BASE_CODE_MODE\r
+  structure are filled in.\r
+  If SortOffers is TRUE, then the cached DHCP offer packets will be sorted before\r
+  they are tried. If SortOffers is FALSE, then the cached DHCP offer packets will\r
+  be tried in the order in which they are received. Please see the Preboot Execution\r
+  Environment (PXE) Specification for additional details on the implementation of DHCP.\r
+  This function can take at least 31 seconds to timeout and return control to the\r
+  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
+  @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
+  @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
+  @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
 EFI_STATUS\r
 EFIAPI\r
@@ -167,9 +555,8 @@ EfiPxeBcDhcp (
   EFI_DHCP4_MODE_DATA     Dhcp4Mode;\r
   EFI_DHCP4_PACKET_OPTION *OptList[PXEBC_DHCP4_MAX_OPTION_NUM];\r
   UINT32                  OptCount;\r
-  UINT32                  DiscoverTimeout;\r
-  UINTN                   Index;\r
   EFI_STATUS              Status;\r
+  EFI_ARP_CONFIG_DATA     ArpConfigData;\r
 \r
   if (This == NULL) {\r
     return EFI_INVALID_PARAMETER;\r
@@ -185,6 +572,9 @@ EfiPxeBcDhcp (
   if (!Mode->Started) {\r
     return EFI_NOT_STARTED;\r
   }\r
+\r
+  Mode->IcmpErrorReceived = FALSE;\r
+\r
   //\r
   // Initialize the DHCP options and build the option list\r
   //\r
@@ -192,69 +582,62 @@ EfiPxeBcDhcp (
 \r
   //\r
   // Set the DHCP4 config data.\r
+  // The four discovery timeouts are 4, 8, 16, 32 seconds respectively.\r
   //\r
   ZeroMem (&Dhcp4CfgData, sizeof (EFI_DHCP4_CONFIG_DATA));\r
   Dhcp4CfgData.OptionCount      = OptCount;\r
   Dhcp4CfgData.OptionList       = OptList;\r
   Dhcp4CfgData.Dhcp4Callback    = PxeBcDhcpCallBack;\r
   Dhcp4CfgData.CallbackContext  = Private;\r
-  Dhcp4CfgData.DiscoverTryCount = 1;\r
-  Dhcp4CfgData.DiscoverTimeout  = &DiscoverTimeout;\r
+  Dhcp4CfgData.DiscoverTryCount = 4;\r
+  Dhcp4CfgData.DiscoverTimeout  = mPxeDhcpTimeout;\r
 \r
-  for (Index = 0; Index < PXEBC_DHCP4_DISCOVER_RETRIES; Index++) {\r
-    //\r
-    // The four discovery timeouts are 4, 8, 16, 32 seconds respectively.\r
-    //\r
-    DiscoverTimeout = (PXEBC_DHCP4_DISCOVER_INIT_TIMEOUT << Index);\r
+  Status          = Dhcp4->Configure (Dhcp4, &Dhcp4CfgData);\r
+  if (EFI_ERROR (Status)) {\r
+    goto ON_EXIT;\r
+  }\r
+  \r
+  //\r
+  // Zero those arrays to record the varies numbers of DHCP OFFERS.\r
+  //\r
+  Private->GotProxyOffer = FALSE;\r
+  Private->NumOffers     = 0;\r
+  Private->BootpIndex    = 0;\r
+  ZeroMem (Private->ServerCount, sizeof (Private->ServerCount));\r
+  ZeroMem (Private->ProxyIndex, sizeof (Private->ProxyIndex));\r
 \r
-    Status          = Dhcp4->Configure (Dhcp4, &Dhcp4CfgData);\r
-    if (EFI_ERROR (Status)) {\r
-      break;\r
+  Status = Dhcp4->Start (Dhcp4, NULL);\r
+  if (EFI_ERROR (Status)) {\r
+    if (Status == EFI_ICMP_ERROR) {\r
+      Mode->IcmpErrorReceived = TRUE;\r
     }\r
-    //\r
-    // Zero those arrays to record the varies numbers of DHCP OFFERS.\r
-    //\r
-    Private->NumOffers   = 0;\r
-    Private->BootpIndex  = 0;\r
-    ZeroMem (Private->ServerCount, sizeof (Private->ServerCount));\r
-    ZeroMem (Private->ProxyIndex, sizeof (Private->ProxyIndex));\r
+    goto ON_EXIT;\r
+  }\r
 \r
-    Status = Dhcp4->Start (Dhcp4, NULL);\r
-    if (EFI_ERROR (Status)) {\r
-      if (Status == EFI_TIMEOUT) {\r
-        //\r
-        // If no response is received or all received offers don't match\r
-        // the PXE boot requirements, EFI_TIMEOUT will be returned.\r
-        //\r
-        continue;\r
-      }\r
-      //\r
-      // Other error status means the DHCP really fails.\r
-      //\r
-      break;\r
-    }\r
+  Status = Dhcp4->GetModeData (Dhcp4, &Dhcp4Mode);\r
+  if (EFI_ERROR (Status)) {\r
+    goto ON_EXIT;\r
+  }\r
 \r
-    Status = Dhcp4->GetModeData (Dhcp4, &Dhcp4Mode);\r
-    if (EFI_ERROR (Status)) {\r
-      break;\r
-    }\r
+  ASSERT (Dhcp4Mode.State == Dhcp4Bound);\r
 \r
-    ASSERT (Dhcp4Mode.State == Dhcp4Bound);\r
+  CopyMem (&Private->StationIp, &Dhcp4Mode.ClientAddress, sizeof (EFI_IPv4_ADDRESS));\r
+  CopyMem (&Private->SubnetMask, &Dhcp4Mode.SubnetMask, sizeof (EFI_IPv4_ADDRESS));\r
+  CopyMem (&Private->GatewayIp, &Dhcp4Mode.RouterAddress, sizeof (EFI_IPv4_ADDRESS));\r
 \r
-    CopyMem (&Private->StationIp, &Dhcp4Mode.ClientAddress, sizeof (EFI_IPv4_ADDRESS));\r
-    CopyMem (&Private->SubnetMask, &Dhcp4Mode.SubnetMask, sizeof (EFI_IPv4_ADDRESS));\r
-    CopyMem (&Private->GatewayIp, &Dhcp4Mode.RouterAddress, sizeof (EFI_IPv4_ADDRESS));\r
+  CopyMem (&Mode->StationIp, &Private->StationIp, sizeof (EFI_IPv4_ADDRESS));\r
+  CopyMem (&Mode->SubnetMask, &Private->SubnetMask, sizeof (EFI_IPv4_ADDRESS));\r
 \r
-    //\r
-    // Check the selected offer to see whether BINL is required, if no or BINL is\r
-    // finished, set the various Mode members.\r
-    //\r
-    Status = PxeBcCheckSelectedOffer (Private);\r
-    if (!EFI_ERROR (Status)) {\r
-      break;\r
-    }\r
+  //\r
+  // Check the selected offer to see whether BINL is required, if no or BINL is\r
+  // finished, set the various Mode members.\r
+  //\r
+  Status = PxeBcCheckSelectedOffer (Private);\r
+  if (!EFI_ERROR (Status)) {\r
+    goto ON_EXIT;\r
   }\r
 \r
+ON_EXIT:\r
   if (EFI_ERROR (Status)) {\r
     Dhcp4->Stop (Dhcp4);\r
     Dhcp4->Configure (Dhcp4, NULL);\r
@@ -266,39 +649,87 @@ EfiPxeBcDhcp (
     Dhcp4->Configure (Dhcp4, &Dhcp4CfgData);\r
 \r
     Private->AddressIsOk = TRUE;\r
+\r
+    if (!Mode->UsingIpv6) {\r
+      //\r
+      // If in IPv4 mode, configure the corresponding ARP with this new\r
+      // station IP address.\r
+      //\r
+      ZeroMem (&ArpConfigData, sizeof (EFI_ARP_CONFIG_DATA));\r
+\r
+      ArpConfigData.SwAddressType   = 0x0800;\r
+      ArpConfigData.SwAddressLength = sizeof (EFI_IPv4_ADDRESS);\r
+      ArpConfigData.StationAddress  = &Private->StationIp.v4;\r
+\r
+      Private->Arp->Configure (Private->Arp, NULL);\r
+      Private->Arp->Configure (Private->Arp, &ArpConfigData);\r
+\r
+      //\r
+      // Updated the route table. Fill the first entry.\r
+      //\r
+      Mode->RouteTableEntries                = 1;\r
+      Mode->RouteTable[0].IpAddr.Addr[0]     = Private->StationIp.Addr[0] & Private->SubnetMask.Addr[0];\r
+      Mode->RouteTable[0].SubnetMask.Addr[0] = Private->SubnetMask.Addr[0];\r
+      Mode->RouteTable[0].GwAddr.Addr[0]     = 0;\r
+\r
+      //\r
+      // Create the default route entry if there is a default router.\r
+      //\r
+      if (Private->GatewayIp.Addr[0] != 0) {\r
+        Mode->RouteTableEntries                = 2;\r
+        Mode->RouteTable[1].IpAddr.Addr[0]     = 0;\r
+        Mode->RouteTable[1].SubnetMask.Addr[0] = 0;\r
+        Mode->RouteTable[1].GwAddr.Addr[0]     = Private->GatewayIp.Addr[0];\r
+      }\r
+    }\r
   }\r
 \r
   return Status;\r
 }\r
 \r
 \r
-/**\r
-  GC_NOTO: Add function description\r
-\r
-  @param  This                                        GC_NOTO: add argument\r
-                                                      description\r
-  @param  Type                                        GC_NOTO: add argument\r
-                                                      description\r
-  @param  Layer                                       GC_NOTO: add argument\r
-                                                      description\r
-  @param  UseBis                                      GC_NOTO: add argument\r
-                                                      description\r
-  @param  Info                                        GC_NOTO: add argument\r
-                                                      description\r
-\r
-  @retval EFI_INVALID_PARAMETER                       GC_NOTO: Add description for\r
-                                                      return value\r
-  @retval EFI_NOT_STARTED                             GC_NOTO: Add description for\r
-                                                      return value\r
-  @retval EFI_INVALID_PARAMETER                       GC_NOTO: Add description for\r
-                                                      return value\r
-  @retval EFI_INVALID_PARAMETER                       GC_NOTO: Add description for\r
-                                                      return value\r
-  @retval EFI_INVALID_PARAMETER                       GC_NOTO: Add description for\r
-                                                      return value\r
-  @retval EFI_INVALID_PARAMETER                       GC_NOTO: Add description for\r
-                                                      return value\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
+  sequence. If this sequence is completed, then EFI_SUCCESS is returned, and the\r
+  PxeDiscoverValid, PxeDiscover, PxeReplyReceived, and PxeReply fields of the\r
+  EFI_PXE_BASE_CODE_MODE structure are filled in. If UseBis is TRUE, then the\r
+  PxeBisReplyReceived and PxeBisReply fields of the EFI_PXE_BASE_CODE_MODE structure\r
+  will also be filled in. If UseBis is FALSE, then PxeBisReplyValid will be set to FALSE.\r
+  In the structure referenced by parameter Info, the PXE Boot Server list, SrvList[],\r
+  has two uses: It is the Boot Server IP address list used for unicast discovery\r
+  (if the UseUCast field is TRUE), and it is the list used for Boot Server verification\r
+  (if the MustUseList field is TRUE). Also, if the MustUseList field in that structure\r
+  is TRUE and the AcceptAnyResponse field in the SrvList[] array is TRUE, any Boot\r
+  Server reply of that type will be accepted. If the AcceptAnyResponse field is\r
+  FALSE, only responses from Boot Servers with matching IP addresses will be accepted.\r
+  This function can take at least 10 seconds to timeout and return control to the\r
+  caller. If the Discovery sequence does not complete, then EFI_TIMEOUT will be\r
+  returned. Please see the Preboot Execution Environment (PXE) Specification for\r
+  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
+  @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
+  @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
+  @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_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
 **/\r
 EFI_STATUS\r
 EFIAPI\r
@@ -316,7 +747,7 @@ EfiPxeBcDiscover (
   EFI_PXE_BASE_CODE_SRVLIST       *SrvList;\r
   EFI_PXE_BASE_CODE_SRVLIST       DefaultSrvList;\r
   PXEBC_CACHED_DHCP4_PACKET       *Packet;\r
-  PXEBC_VENDOR_OPTION            *VendorOpt;\r
+  PXEBC_VENDOR_OPTION             *VendorOpt;\r
   UINT16                          Index;\r
   EFI_STATUS                      Status;\r
   PXEBC_BOOT_SVR_ENTRY            *BootSvrEntry;\r
@@ -340,6 +771,8 @@ EfiPxeBcDiscover (
     return EFI_NOT_STARTED;\r
   }\r
 \r
+  Mode->IcmpErrorReceived = FALSE;\r
+\r
   //\r
   // If layer isn't EFI_PXE_BASE_CODE_BOOT_LAYER_INITIAL,\r
   //   use the previous setting;\r
@@ -385,7 +818,11 @@ EfiPxeBcDiscover (
       //\r
       // Get the multicast discover ip address from vendor option.\r
       //\r
-      CopyMem (&DefaultInfo.ServerMCastIp.Addr, &VendorOpt->DiscoverMcastIp, sizeof (EFI_IPv4_ADDRESS));\r
+      CopyMem (\r
+        &DefaultInfo.ServerMCastIp.Addr, \r
+        &VendorOpt->DiscoverMcastIp, \r
+        sizeof (EFI_IPv4_ADDRESS)\r
+        );\r
     }\r
 \r
     DefaultInfo.IpCnt = 0;\r
@@ -444,7 +881,11 @@ EfiPxeBcDiscover (
       if (BootSvrEntry == NULL) {\r
         Private->ServerIp.Addr[0] = SrvList[Index].IpAddr.Addr[0];\r
       } else {\r
-        CopyMem (&Private->ServerIp, &BootSvrEntry->IpAddr[Index], sizeof (EFI_IPv4_ADDRESS));\r
+        CopyMem (\r
+          &Private->ServerIp, \r
+          &BootSvrEntry->IpAddr[Index], \r
+          sizeof (EFI_IPv4_ADDRESS)\r
+          );\r
       }\r
 \r
       Status = PxeBcDiscvBootService (\r
@@ -490,47 +931,97 @@ EfiPxeBcDiscover (
   }\r
 \r
   if (EFI_ERROR (Status) || !Mode->PxeReplyReceived || (!Mode->PxeBisReplyReceived && UseBis)) {\r
-\r
-    Status = EFI_DEVICE_ERROR;\r
+    if (Status == EFI_ICMP_ERROR) {\r
+      Mode->IcmpErrorReceived = TRUE;\r
+    } else {\r
+      Status = EFI_DEVICE_ERROR;\r
+    }\r
   } else {\r
     PxeBcParseCachedDhcpPacket (&Private->PxeReply);\r
   }\r
 \r
   if (Mode->PxeBisReplyReceived) {\r
-    CopyMem (&Private->ServerIp, &Mode->PxeReply.Dhcpv4.BootpSiAddr, sizeof (EFI_IPv4_ADDRESS));\r
+    CopyMem (\r
+      &Private->ServerIp, \r
+      &Mode->PxeReply.Dhcpv4.BootpSiAddr, \r
+      sizeof (EFI_IPv4_ADDRESS)\r
+      );\r
   }\r
 \r
   return Status;\r
 }\r
 \r
 \r
-/**\r
-  GC_NOTO: Add function description\r
-\r
-  @param  This                                        GC_NOTO: add argument\r
-                                                      description\r
-  @param  Operation                                   GC_NOTO: add argument\r
-                                                      description\r
-  @param  BufferPtr                                   GC_NOTO: add argument\r
-                                                      description\r
-  @param  Overwrite                                   GC_NOTO: add argument\r
-                                                      description\r
-  @param  BufferSize                                  GC_NOTO: add argument\r
-                                                      description\r
-  @param  BlockSize                                   GC_NOTO: add argument\r
-                                                      description\r
-  @param  ServerIp                                    GC_NOTO: add argument\r
-                                                      description\r
-  @param  Filename                                    GC_NOTO: add argument\r
-                                                      description\r
-  @param  Info                                        GC_NOTO: add argument\r
-                                                      description\r
-  @param  DontUseBuffer                               GC_NOTO: add argument\r
-                                                      description\r
-\r
-  @retval EFI_INVALID_PARAMETER                       GC_NOTO: Add description for\r
-                                                      return value\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
+  TFTP operations to get the size of a file, read a directory, read a file, and\r
+  write a file. It also includes the MTFTP operations to get the size of a file,\r
+  read a directory, and read a file. The type of operation is specified by Operation.\r
+  If the callback function that is invoked during the TFTP/MTFTP operation does\r
+  not return EFI_PXE_BASE_CODE_CALLBACK_STATUS_CONTINUE, then EFI_ABORTED will\r
+  be returned.\r
+  For read operations, the return data will be placed in the buffer specified by\r
+  BufferPtr. If BufferSize is too small to contain the entire downloaded file,\r
+  then EFI_BUFFER_TOO_SMALL will be returned and BufferSize will be set to zero\r
+  or the size of the requested file (the size of the requested file is only returned\r
+  if the TFTP server supports TFTP options). If BufferSize is large enough for the\r
+  read operation, then BufferSize will be set to the size of the downloaded file,\r
+  and EFI_SUCCESS will be returned. Applications using the PxeBc.Mtftp() services\r
+  should use the get-file-size operations to determine the size of the downloaded\r
+  file prior to using the read-file operations-especially when downloading large\r
+  (greater than 64 MB) files-instead of making two calls to the read-file operation.\r
+  Following this recommendation will save time if the file is larger than expected\r
+  and the TFTP server does not support TFTP option extensions. Without TFTP option\r
+  extension support, the client has to download the entire file, counting and discarding\r
+  the received packets, to determine the file size.\r
+  For write operations, the data to be sent is in the buffer specified by BufferPtr.\r
+  BufferSize specifies the number of bytes to send. If the write operation completes\r
+  successfully, then EFI_SUCCESS will be returned.\r
+  For TFTP "get file size" operations, the size of the requested file or directory\r
+  is returned in BufferSize, and EFI_SUCCESS will be returned. If the TFTP server\r
+  does not support options, the file will be downloaded into a bit bucket and the\r
+  length of the downloaded file will be returned. For MTFTP "get file size" operations,\r
+  if the MTFTP server does not support the "get file size" option, EFI_UNSUPPORTED\r
+  will be returned.\r
+  This function can take up to 10 seconds to timeout and return control to the caller.\r
+  If the TFTP sequence does not complete, EFI_TIMEOUT will be returned.\r
+  If the Callback Protocol does not return EFI_PXE_BASE_CODE_CALLBACK_STATUS_CONTINUE,\r
+  then the TFTP sequence is stopped and EFI_ABORTED will be returned.\r
+  The format of the data returned from a TFTP read directory operation is a null-terminated\r
+  filename followed by a null-terminated information string, of the form\r
+  "size year-month-day hour:minute:second" (i.e. %d %d-%d-%d %d:%d:%f - note that\r
+  the seconds field can be a decimal number), where the date and time are UTC. For\r
+  an MTFTP read directory command, there is additionally a null-terminated multicast\r
+  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
+  @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  Overwrite             Only used on write file operations. TRUE if a file on a remote server can\r
+                                be overwritten.                                                          \r
+  @param  BufferSize            For get-file-size operations, *BufferSize returns the size of the\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
+  @param  Info                  Pointer to the MTFTP information.\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_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
 EFI_STATUS\r
 EFIAPI\r
@@ -550,12 +1041,14 @@ EfiPxeBcMtftp (
   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
+  if ((This == NULL)                                                       ||\r
+      (Filename == NULL)                                                   ||\r
+      (BufferSize == NULL)                                                 ||\r
       ((ServerIp == NULL) || !Ip4IsUnicast (NTOHL (ServerIp->Addr[0]), 0)) ||\r
-      ((BufferPtr == NULL) && DontUseBuffer) ||\r
+      ((BufferPtr == NULL) && DontUseBuffer)                               ||\r
       ((BlockSize != NULL) && (*BlockSize < 512))) {\r
 \r
     return EFI_INVALID_PARAMETER;\r
@@ -563,15 +1056,45 @@ EfiPxeBcMtftp (
 \r
   Status  = EFI_DEVICE_ERROR;\r
   Private = PXEBC_PRIVATE_DATA_FROM_PXEBC (This);\r
+  Mode    = &Private->Mode;\r
+\r
+  if (!Mode->AutoArp) {\r
+    //\r
+    // If AutoArp is set false, check arp cache\r
+    //\r
+    UpdateArpCache (This);\r
+    if (!FindInArpCache (Mode, &ServerIp->v4, &TempMacAddr)) {\r
+      return EFI_DEVICE_ERROR;\r
+    }\r
+  }\r
+\r
+  Mode->TftpErrorReceived = FALSE;\r
+  Mode->IcmpErrorReceived = FALSE;\r
 \r
   Mtftp4Config.UseDefaultSetting = FALSE;\r
   Mtftp4Config.TimeoutValue      = PXEBC_MTFTP_TIMEOUT;\r
   Mtftp4Config.TryCount          = PXEBC_MTFTP_RETRIES;\r
 \r
-  CopyMem (&Mtftp4Config.StationIp, &Private->StationIp, sizeof (EFI_IPv4_ADDRESS));\r
-  CopyMem (&Mtftp4Config.SubnetMask, &Private->SubnetMask, sizeof (EFI_IPv4_ADDRESS));\r
-  CopyMem (&Mtftp4Config.GatewayIp, &Private->GatewayIp, sizeof (EFI_IPv4_ADDRESS));\r
-  CopyMem (&Mtftp4Config.ServerIp, ServerIp, sizeof (EFI_IPv4_ADDRESS));\r
+  CopyMem (\r
+    &Mtftp4Config.StationIp, \r
+    &Private->StationIp, \r
+    sizeof (EFI_IPv4_ADDRESS)\r
+    );\r
+  CopyMem (\r
+    &Mtftp4Config.SubnetMask, \r
+    &Private->SubnetMask, \r
+    sizeof (EFI_IPv4_ADDRESS)\r
+    );\r
+  CopyMem (\r
+    &Mtftp4Config.GatewayIp, \r
+    &Private->GatewayIp, \r
+    sizeof (EFI_IPv4_ADDRESS)\r
+    );\r
+  CopyMem (\r
+    &Mtftp4Config.ServerIp, \r
+    ServerIp, \r
+    sizeof (EFI_IPv4_ADDRESS)\r
+    );\r
 \r
   switch (Operation) {\r
 \r
@@ -585,10 +1108,6 @@ EfiPxeBcMtftp (
               BufferSize\r
               );\r
 \r
-    if (!EFI_ERROR (Status)) {\r
-      Status = EFI_BUFFER_TOO_SMALL;\r
-    }\r
-\r
     break;\r
 \r
   case EFI_PXE_BASE_CODE_TFTP_READ_FILE:\r
@@ -645,47 +1164,50 @@ EfiPxeBcMtftp (
     break;\r
   }\r
 \r
+  if (Status == EFI_ICMP_ERROR) {\r
+    Mode->IcmpErrorReceived = TRUE;\r
+  }\r
+\r
   return Status;\r
 }\r
 \r
 \r
-/**\r
-  GC_NOTO: Add function description\r
-\r
-  @param  This                                        GC_NOTO: add argument\r
-                                                      description\r
-  @param  OpFlags                                     GC_NOTO: add argument\r
-                                                      description\r
-  @param  DestIp                                      GC_NOTO: add argument\r
-                                                      description\r
-  @param  DestPort                                    GC_NOTO: add argument\r
-                                                      description\r
-  @param  GatewayIp                                   GC_NOTO: add argument\r
-                                                      description\r
-  @param  SrcIp                                       GC_NOTO: add argument\r
-                                                      description\r
-  @param  SrcPort                                     GC_NOTO: add argument\r
-                                                      description\r
-  @param  HeaderSize                                  GC_NOTO: add argument\r
-                                                      description\r
-  @param  HeaderPtr                                   GC_NOTO: add argument\r
-                                                      description\r
-  @param  BufferSize                                  GC_NOTO: add argument\r
-                                                      description\r
-  @param  BufferPtr                                   GC_NOTO: add argument\r
-                                                      description\r
-\r
-  @retval EFI_INVALID_PARAMETER                       GC_NOTO: Add description for\r
-                                                      return value\r
-  @retval EFI_INVALID_PARAMETER                       GC_NOTO: Add description for\r
-                                                      return value\r
-  @retval EFI_INVALID_PARAMETER                       GC_NOTO: Add description for\r
-                                                      return value\r
-  @retval EFI_INVALID_PARAMETER                       GC_NOTO: Add description for\r
-                                                      return value\r
-  @retval EFI_OUT_OF_RESOURCES                        GC_NOTO: Add description for\r
-                                                      return value\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
+  BufferPtr parameters to the network interface. The UDP header is automatically\r
+  built by this routine. It uses the parameters OpFlags, DestIp, DestPort, GatewayIp,\r
+  SrcIp, and SrcPort to build this header. If the packet is successfully built and\r
+  transmitted through the network interface, then EFI_SUCCESS will be returned.\r
+  If a timeout occurs during the transmission of the packet, then EFI_TIMEOUT will\r
+  be returned. If an ICMP error occurs during the transmission of the packet, then\r
+  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
+  @param  This                  Pointer to the EFI_PXE_BASE_CODE_PROTOCOL instance.\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  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
+  @param  HeaderPtr             If HeaderSize is not NULL, a pointer to a header to be prefixed to the\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
+  @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_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
 **/\r
 EFI_STATUS\r
 EFIAPI\r
@@ -712,7 +1234,8 @@ EfiPxeBcUdpWrite (
   EFI_UDP4_SESSION_DATA     Udp4Session;\r
   EFI_STATUS                Status;\r
   BOOLEAN                   IsDone;\r
-  UINT16                    RandomSrcPort;\r
+  EFI_PXE_BASE_CODE_MODE    *Mode;\r
+  EFI_MAC_ADDRESS           TempMacAddr;\r
 \r
   IsDone = FALSE;\r
 \r
@@ -740,29 +1263,47 @@ EfiPxeBcUdpWrite (
   }\r
 \r
   Private = PXEBC_PRIVATE_DATA_FROM_PXEBC (This);\r
-  Udp4    = Private->Udp4;\r
+  Udp4    = Private->Udp4Write;\r
+  Mode    = &Private->Mode;\r
+  if (!Mode->Started) {\r
+    return EFI_NOT_STARTED;\r
+  }\r
 \r
   if (!Private->AddressIsOk && (SrcIp == NULL)) {\r
     return EFI_INVALID_PARAMETER;\r
   }\r
 \r
-  if (SrcIp == NULL) {\r
-    SrcIp = &Private->StationIp;\r
-\r
-    if (GatewayIp == NULL) {\r
-      GatewayIp = &Private->GatewayIp;\r
+  if (!Mode->AutoArp) {\r
+    //\r
+    // If AutoArp is set false, check arp cache\r
+    //\r
+    UpdateArpCache (This);\r
+    if (!FindInArpCache (Mode, &DestIp->v4, &TempMacAddr)) {\r
+      return EFI_DEVICE_ERROR;\r
     }\r
   }\r
 \r
-  if ((SrcPort == NULL) || (OpFlags & EFI_PXE_BASE_CODE_UDP_OPFLAGS_ANY_SRC_PORT)) {\r
-    RandomSrcPort = (UINT16) (NET_RANDOM (NetRandomInitSeed ()) % 10000 + 1024);\r
+  Mode->IcmpErrorReceived = FALSE;\r
 \r
-    if (SrcPort == NULL) {\r
-\r
-      SrcPort  = &RandomSrcPort;\r
-    } else {\r
+  if ((Private->CurrentUdpSrcPort == 0) ||\r
+      ((SrcPort != NULL) && (*SrcPort != Private->CurrentUdpSrcPort))) {\r
+    //\r
+    // Port is changed, (re)configure the Udp4Write instance\r
+    //\r
+    if (SrcPort != NULL) {\r
+      Private->CurrentUdpSrcPort = *SrcPort;\r
+    }\r
 \r
-      *SrcPort = RandomSrcPort;\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
   }\r
 \r
@@ -771,11 +1312,15 @@ EfiPxeBcUdpWrite (
 \r
   CopyMem (&Udp4Session.DestinationAddress, DestIp, sizeof (EFI_IPv4_ADDRESS));\r
   Udp4Session.DestinationPort = *DestPort;\r
-  CopyMem (&Udp4Session.SourceAddress, SrcIp, sizeof (EFI_IPv4_ADDRESS));\r
-  Udp4Session.SourcePort = *SrcPort;\r
+  if (SrcIp != NULL) {\r
+    CopyMem (&Udp4Session.SourceAddress, SrcIp, sizeof (EFI_IPv4_ADDRESS));\r
+  }\r
+  if (SrcPort != NULL) {\r
+    Udp4Session.SourcePort = *SrcPort;\r
+  }\r
 \r
   FragCount = (HeaderSize != NULL) ? 2 : 1;\r
-  Udp4TxData = (EFI_UDP4_TRANSMIT_DATA *) AllocatePool (sizeof (EFI_UDP4_TRANSMIT_DATA) + (FragCount - 1) * sizeof (EFI_UDP4_FRAGMENT_DATA));\r
+  Udp4TxData = (EFI_UDP4_TRANSMIT_DATA *) AllocateZeroPool (sizeof (EFI_UDP4_TRANSMIT_DATA) + (FragCount - 1) * sizeof (EFI_UDP4_FRAGMENT_DATA));\r
   if (Udp4TxData == NULL) {\r
     return EFI_OUT_OF_RESOURCES;\r
   }\r
@@ -792,7 +1337,9 @@ EfiPxeBcUdpWrite (
     DataLength += (UINT32) *HeaderSize;\r
   }\r
 \r
-  Udp4TxData->GatewayAddress  = (EFI_IPv4_ADDRESS *) GatewayIp;\r
+  if (GatewayIp != NULL) {\r
+    Udp4TxData->GatewayAddress  = (EFI_IPv4_ADDRESS *) GatewayIp;\r
+  }\r
   Udp4TxData->UdpSessionData  = &Udp4Session;\r
   Udp4TxData->DataLength      = DataLength;\r
   Token.Packet.TxData         = Udp4TxData;\r
@@ -810,6 +1357,9 @@ EfiPxeBcUdpWrite (
 \r
   Status = Udp4->Transmit (Udp4, &Token);\r
   if (EFI_ERROR (Status)) {\r
+    if (Status == EFI_ICMP_ERROR) {\r
+      Mode->IcmpErrorReceived = TRUE;\r
+    }\r
     goto ON_EXIT;\r
   }\r
 \r
@@ -831,58 +1381,117 @@ ON_EXIT:
   return Status;\r
 }\r
 \r
-\r
 /**\r
-  GC_NOTO: Add function description\r
-\r
-  @param  This                                        GC_NOTO: add argument\r
-                                                      description\r
-  @param  OpFlags                                     GC_NOTO: add argument\r
-                                                      description\r
-  @param  DestIp                                      GC_NOTO: add argument\r
-                                                      description\r
-  @param  DestPort                                    GC_NOTO: add argument\r
-                                                      description\r
-  @param  SrcIp                                       GC_NOTO: add argument\r
-                                                      description\r
-  @param  SrcPort                                     GC_NOTO: add argument\r
-                                                      description\r
-  @param  HeaderSize                                  GC_NOTO: add argument\r
-                                                      description\r
-  @param  HeaderPtr                                   GC_NOTO: add argument\r
-                                                      description\r
-  @param  BufferSize                                  GC_NOTO: add argument\r
-                                                      description\r
-  @param  BufferPtr                                   GC_NOTO: add argument\r
-                                                      description\r
-\r
-  @retval EFI_INVALID_PARAMETER                       GC_NOTO: Add description for\r
-                                                      return value\r
-  @retval EFI_INVALID_PARAMETER                       GC_NOTO: Add description for\r
-                                                      return value\r
-  @retval EFI_INVALID_PARAMETER                       GC_NOTO: Add description for\r
-                                                      return value\r
-  @retval EFI_INVALID_PARAMETER                       GC_NOTO: Add description for\r
-                                                      return value\r
-  @retval EFI_NOT_STARTED                             GC_NOTO: Add description for\r
-                                                      return value\r
-  @retval EFI_OUT_OF_RESOURCES                        GC_NOTO: Add description for\r
-                                                      return value\r
+  Decide whether the incoming UDP packet is acceptable per IP filter settings\r
+  in provided PxeBcMode.\r
+\r
+  @param  PxeBcMode          Pointer to EFI_PXE_BASE_CODE_MODE.\r
+  @param  Session            Received UDP session.\r
+\r
+  @retval TRUE               The UDP package matches IP filters.\r
+  @retval FALSE              The UDP package doesn't matches IP filters.\r
 \r
+**/\r
+BOOLEAN\r
+CheckIpByFilter (\r
+  IN EFI_PXE_BASE_CODE_MODE    *PxeBcMode,\r
+  IN EFI_UDP4_SESSION_DATA     *Session\r
+  )\r
+{\r
+  UINTN                   Index;\r
+  EFI_IPv4_ADDRESS        Ip4Address;\r
+  EFI_IPv4_ADDRESS        DestIp4Address;\r
+\r
+  if ((PxeBcMode->IpFilter.Filters & EFI_PXE_BASE_CODE_IP_FILTER_PROMISCUOUS) != 0) {\r
+    return TRUE;\r
+  }\r
+\r
+  CopyMem (&DestIp4Address, &Session->DestinationAddress, sizeof (DestIp4Address));\r
+  if ((PxeBcMode->IpFilter.Filters & EFI_PXE_BASE_CODE_IP_FILTER_PROMISCUOUS_MULTICAST) &&\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
+      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
+      EFI_IP4_EQUAL (&Ip4Address, &DestIp4Address)\r
+      ) {\r
+    return TRUE;\r
+  }\r
+\r
+  ASSERT (PxeBcMode->IpFilter.IpCnt < EFI_PXE_BASE_CODE_MAX_IPCNT);\r
+\r
+  for (Index = 0; Index < PxeBcMode->IpFilter.IpCnt; Index++) {\r
+    CopyMem (\r
+      &Ip4Address, \r
+      &PxeBcMode->IpFilter.IpList[Index].v4, \r
+      sizeof (Ip4Address)\r
+      );\r
+    if (EFI_IP4_EQUAL (&Ip4Address, &DestIp4Address)) {\r
+      return TRUE;\r
+    }\r
+  }\r
+\r
+  return FALSE;\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
+  are returned in (the optional HeaderPtr and) BufferPtr, and the size of the\r
+  buffer received is returned in BufferSize . If the input BufferSize is smaller\r
+  than the UDP packet received (less optional HeaderSize), it will be set to the\r
+  required size, and EFI_BUFFER_TOO_SMALL will be returned. In this case, the\r
+  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
+  will be performed. The following tables summarize these receive filter operations.\r
+    \r
+  @param  This                  Pointer to the EFI_PXE_BASE_CODE_PROTOCOL instance.\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
+  @param  HeaderPtr             If HeaderSize is not NULL, a pointer to a header to be prefixed to the\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
+  @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_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
 **/\r
 EFI_STATUS\r
 EFIAPI\r
 EfiPxeBcUdpRead (\r
-  IN EFI_PXE_BASE_CODE_PROTOCOL       *This,\r
-  IN UINT16                           OpFlags,\r
-  IN OUT EFI_IP_ADDRESS               *DestIp, OPTIONAL\r
-  IN OUT EFI_PXE_BASE_CODE_UDP_PORT   *DestPort, OPTIONAL\r
-  IN OUT EFI_IP_ADDRESS               *SrcIp, OPTIONAL\r
-  IN OUT EFI_PXE_BASE_CODE_UDP_PORT   *SrcPort, OPTIONAL\r
-  IN UINTN                            *HeaderSize, OPTIONAL\r
-  IN VOID                             *HeaderPtr, OPTIONAL\r
-  IN OUT UINTN                        *BufferSize,\r
-  IN VOID                             *BufferPtr\r
+  IN EFI_PXE_BASE_CODE_PROTOCOL                *This,\r
+  IN UINT16                                    OpFlags,\r
+  IN OUT EFI_IP_ADDRESS                        *DestIp     OPTIONAL,\r
+  IN OUT EFI_PXE_BASE_CODE_UDP_PORT            *DestPort   OPTIONAL,\r
+  IN OUT EFI_IP_ADDRESS                        *SrcIp      OPTIONAL,\r
+  IN OUT EFI_PXE_BASE_CODE_UDP_PORT            *SrcPort    OPTIONAL,\r
+  IN UINTN                                     *HeaderSize OPTIONAL,\r
+  IN VOID                                      *HeaderPtr  OPTIONAL,\r
+  IN OUT UINTN                                 *BufferSize,\r
+  IN VOID                                      *BufferPtr\r
   )\r
 {\r
   PXEBC_PRIVATE_DATA        *Private;\r
@@ -900,17 +1509,13 @@ EfiPxeBcUdpRead (
     return EFI_INVALID_PARAMETER;\r
   }\r
 \r
-  if (OpFlags & EFI_PXE_BASE_CODE_UDP_OPFLAGS_USE_FILTER) {\r
-    return EFI_UNSUPPORTED;\r
-  }\r
-\r
-  if ((!(OpFlags & EFI_PXE_BASE_CODE_UDP_OPFLAGS_ANY_DEST_PORT) && (DestPort == NULL)) ||\r
-      (!(OpFlags & EFI_PXE_BASE_CODE_UDP_OPFLAGS_ANY_DEST_PORT) && (SrcIp == NULL)) ||\r
-      (!(OpFlags & EFI_PXE_BASE_CODE_UDP_OPFLAGS_ANY_SRC_PORT) && (SrcPort == NULL))) {\r
+  if (((OpFlags & EFI_PXE_BASE_CODE_UDP_OPFLAGS_ANY_DEST_PORT) == 0 && (DestPort == NULL)) ||\r
+      ((OpFlags & EFI_PXE_BASE_CODE_UDP_OPFLAGS_ANY_DEST_PORT) == 0 && (SrcIp == NULL)) ||\r
+      ((OpFlags & EFI_PXE_BASE_CODE_UDP_OPFLAGS_ANY_SRC_PORT) == 0 && (SrcPort == NULL))) {\r
     return EFI_INVALID_PARAMETER;\r
   }\r
 \r
-  if (((HeaderSize != NULL) && (*HeaderSize == 0)) || ((HeaderPtr == NULL) && (*HeaderSize != 0))) {\r
+  if (((HeaderSize != NULL) && (*HeaderSize == 0)) || ((HeaderSize != NULL) && (HeaderPtr == NULL))) {\r
     return EFI_INVALID_PARAMETER;\r
   }\r
 \r
@@ -920,12 +1525,14 @@ EfiPxeBcUdpRead (
 \r
   Private = PXEBC_PRIVATE_DATA_FROM_PXEBC (This);\r
   Mode    = Private->PxeBc.Mode;\r
-  Udp4    = Private->Udp4;\r
+  Udp4    = Private->Udp4Read;\r
 \r
   if (!Mode->Started) {\r
     return EFI_NOT_STARTED;\r
   }\r
 \r
+  Mode->IcmpErrorReceived = FALSE;\r
+\r
   Status = gBS->CreateEvent (\r
                   EVT_NOTIFY_SIGNAL,\r
                   TPL_NOTIFY,\r
@@ -937,9 +1544,14 @@ EfiPxeBcUdpRead (
     return EFI_OUT_OF_RESOURCES;\r
   }\r
 \r
+TRY_AGAIN:\r
+\r
   IsDone = FALSE;\r
   Status = Udp4->Receive (Udp4, &Token);\r
   if (EFI_ERROR (Status)) {\r
+    if (Status == EFI_ICMP_ERROR) {\r
+      Mode->IcmpErrorReceived = TRUE;\r
+    }\r
     goto ON_EXIT;\r
   }\r
 \r
@@ -959,25 +1571,39 @@ EfiPxeBcUdpRead (
     RxData  = Token.Packet.RxData;\r
     Session = &RxData->UdpSession;\r
 \r
-    Matched = FALSE;\r
+    Matched = TRUE;\r
 \r
-    //\r
-    // Match the destination ip of the received udp dgram\r
-    //\r
-    if (OpFlags & EFI_PXE_BASE_CODE_UDP_OPFLAGS_ANY_DEST_IP) {\r
-      Matched = TRUE;\r
-\r
-      if (DestIp != NULL) {\r
-        CopyMem (DestIp, &Session->DestinationAddress, sizeof (EFI_IPv4_ADDRESS));\r
+    if ((OpFlags & EFI_PXE_BASE_CODE_UDP_OPFLAGS_USE_FILTER) != 0) {\r
+      Matched = FALSE;\r
+      //\r
+      // Check UDP package by IP filter settings\r
+      //\r
+      if (CheckIpByFilter (Mode, Session)) {\r
+        Matched = TRUE;\r
       }\r
-    } else {\r
-      if (DestIp != NULL) {\r
-        if (EFI_IP4_EQUAL (DestIp, &Session->DestinationAddress)) {\r
-          Matched = TRUE;\r
+    }\r
+\r
+    if (Matched) {\r
+      Matched = FALSE;\r
+\r
+      //\r
+      // Match the destination ip of the received udp dgram\r
+      //\r
+      if ((OpFlags & EFI_PXE_BASE_CODE_UDP_OPFLAGS_ANY_DEST_IP) != 0) {\r
+        Matched = TRUE;\r
+\r
+        if (DestIp != NULL) {\r
+          CopyMem (DestIp, &Session->DestinationAddress, sizeof (EFI_IPv4_ADDRESS));\r
         }\r
       } else {\r
-        if (EFI_IP4_EQUAL (&Private->StationIp, &Session->DestinationAddress)) {\r
-          Matched = TRUE;\r
+        if (DestIp != NULL) {\r
+          if (EFI_IP4_EQUAL (DestIp, &Session->DestinationAddress)) {\r
+            Matched = TRUE;\r
+          }\r
+        } else {\r
+          if (EFI_IP4_EQUAL (&Private->StationIp, &Session->DestinationAddress)) {\r
+            Matched = TRUE;\r
+          }\r
         }\r
       }\r
     }\r
@@ -986,7 +1612,7 @@ EfiPxeBcUdpRead (
       //\r
       // Match the destination port of the received udp dgram\r
       //\r
-      if (OpFlags & EFI_PXE_BASE_CODE_UDP_OPFLAGS_ANY_DEST_PORT) {\r
+      if ((OpFlags & EFI_PXE_BASE_CODE_UDP_OPFLAGS_ANY_DEST_PORT) != 0) {\r
 \r
         if (DestPort != NULL) {\r
           *DestPort = Session->DestinationPort;\r
@@ -1003,7 +1629,7 @@ EfiPxeBcUdpRead (
       //\r
       // Match the source ip of the received udp dgram\r
       //\r
-      if (OpFlags & EFI_PXE_BASE_CODE_UDP_OPFLAGS_ANY_SRC_IP) {\r
+      if ((OpFlags & EFI_PXE_BASE_CODE_UDP_OPFLAGS_ANY_SRC_IP) != 0) {\r
 \r
         if (SrcIp != NULL) {\r
           CopyMem (SrcIp, &Session->SourceAddress, sizeof (EFI_IPv4_ADDRESS));\r
@@ -1020,7 +1646,7 @@ EfiPxeBcUdpRead (
       //\r
       // Match the source port of the received udp dgram\r
       //\r
-      if (OpFlags & EFI_PXE_BASE_CODE_UDP_OPFLAGS_ANY_SRC_PORT) {\r
+      if ((OpFlags & EFI_PXE_BASE_CODE_UDP_OPFLAGS_ANY_SRC_PORT) != 0) {\r
 \r
         if (SrcPort != NULL) {\r
           *SrcPort = Session->SourcePort;\r
@@ -1049,7 +1675,11 @@ EfiPxeBcUdpRead (
       } else {\r
 \r
         *BufferSize = RxData->DataLength - CopyLen;\r
-        CopyMem (BufferPtr, (UINT8 *) RxData->FragmentTable[0].FragmentBuffer + CopyLen, *BufferSize);\r
+        CopyMem (\r
+          BufferPtr, \r
+          (UINT8 *) RxData->FragmentTable[0].FragmentBuffer + CopyLen, \r
+          *BufferSize\r
+          );\r
       }\r
     } else {\r
 \r
@@ -1060,6 +1690,10 @@ EfiPxeBcUdpRead (
     // Recycle the RxData\r
     //\r
     gBS->SignalEvent (RxData->RecycleSignal);\r
+\r
+    if (!Matched) {\r
+      goto TRY_AGAIN;\r
+    }\r
   }\r
 \r
 ON_EXIT:\r
@@ -1071,18 +1705,37 @@ ON_EXIT:
   return Status;\r
 }\r
 \r
-\r
-/**\r
-  GC_NOTO: Add function description\r
-\r
-  @param  This                                        GC_NOTO: add argument\r
-                                                      description\r
-  @param  NewFilter                                   GC_NOTO: add argument\r
-                                                      description\r
-\r
-  @retval EFI_UNSUPPORTED                             GC_NOTO: Add description for\r
-                                                      return value\r
-\r
+/**                                                                 \r
+  Updates the IP receive filters of a network device and enables software filtering.\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
+  The software filter is used when the USE_FILTER in OpFlags is set to UdpRead().\r
+  The current hardware filter remains in effect no matter what the settings of OpFlags\r
+  are, so that the meaning of ANY_DEST_IP set in OpFlags to UdpRead() is from those\r
+  packets whose reception is enabled in hardware-physical NIC address (unicast),\r
+  broadcast address, logical address or addresses (multicast), or all (promiscuous).\r
+  UdpRead() does not modify the IP filter settings.\r
+  Dhcp(), Discover(), and Mtftp() set the IP filter, and return with the IP receive\r
+  filter list emptied and the filter set to EFI_PXE_BASE_CODE_IP_FILTER_STATION_IP.\r
+  If an application or driver wishes to preserve the IP receive filter settings,\r
+  it will have to preserve the IP receive filter settings before these calls, and\r
+  use SetIpFilter() to restore them after the calls. If incompatible filtering is\r
+  requested (for example, PROMISCUOUS with anything else) or if the device does not\r
+  support a requested filter setting and it cannot be accommodated in software\r
+  (for example, PROMISCUOUS not supported), EFI_INVALID_PARAMETER will be returned.\r
+  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
+  @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
+  @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
 **/\r
 EFI_STATUS\r
 EFIAPI\r
@@ -1091,23 +1744,150 @@ EfiPxeBcSetIpFilter (
   IN EFI_PXE_BASE_CODE_IP_FILTER      *NewFilter\r
   )\r
 {\r
-  return EFI_UNSUPPORTED;\r
-}\r
+  EFI_STATUS                Status;\r
+  PXEBC_PRIVATE_DATA        *Private;\r
+  EFI_PXE_BASE_CODE_MODE    *Mode;\r
+  UINTN                     Index;\r
+  BOOLEAN                   PromiscuousNeed;\r
+\r
+  if (This == NULL) {\r
+    DEBUG ((EFI_D_ERROR, "This == NULL.\n"));\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
 \r
+  Private = PXEBC_PRIVATE_DATA_FROM_PXEBC (This);\r
+  Mode = Private->PxeBc.Mode;\r
 \r
-/**\r
-  GC_NOTO: Add function description\r
+  if (NewFilter == NULL) {\r
+    DEBUG ((EFI_D_ERROR, "NewFilter == NULL.\n"));\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
+  if (NewFilter->IpCnt > EFI_PXE_BASE_CODE_MAX_IPCNT) {\r
+    DEBUG ((EFI_D_ERROR, "NewFilter->IpCnt > %d.\n", EFI_PXE_BASE_CODE_MAX_IPCNT));\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
 \r
-  @param  This                                        GC_NOTO: add argument\r
-                                                      description\r
-  @param  IpAddr                                      GC_NOTO: add argument\r
-                                                      description\r
-  @param  MacAddr                                     GC_NOTO: add argument\r
-                                                      description\r
+  if (!Mode->Started) {\r
+    DEBUG ((EFI_D_ERROR, "BC was not started.\n"));\r
+    return EFI_NOT_STARTED;\r
+  }\r
+\r
+  PromiscuousNeed = FALSE;\r
+\r
+  for (Index = 0; Index < NewFilter->IpCnt; ++Index) {\r
+    if (IP4_IS_LOCAL_BROADCAST (EFI_IP4 (NewFilter->IpList[Index].v4))) {\r
+      //\r
+      // The IP is a broadcast address.\r
+      //\r
+      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
+      //\r
+      // If EFI_PXE_BASE_CODE_IP_FILTER_STATION_IP is set and IP4 address is in IpList,\r
+      // promiscuous mode is needed.\r
+      //\r
+      PromiscuousNeed = TRUE;\r
+    }\r
+  }\r
 \r
-  @retval EFI_UNSUPPORTED                             GC_NOTO: Add description for\r
-                                                      return value\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
+\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
+     ) {\r
+    //\r
+    // Configure the udp4 filter to receive all packages\r
+    //\r
+    Private->Udp4CfgData.AcceptPromiscuous  = TRUE;\r
 \r
+    //\r
+    // Configure the UDP instance with the new configuration.\r
+    //\r
+    Status = Private->Udp4Read->Configure (Private->Udp4Read, &Private->Udp4CfgData);\r
+    if (EFI_ERROR (Status)) {\r
+      return Status;\r
+    }\r
+\r
+  } else {\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
+\r
+    //\r
+    // Configure the UDP instance with the new configuration.\r
+    //\r
+    Status = Private->Udp4Read->Configure (Private->Udp4Read, &Private->Udp4CfgData);\r
+    if (EFI_ERROR (Status)) {\r
+      return Status;\r
+    }\r
+\r
+    if (NewFilter->Filters & EFI_PXE_BASE_CODE_IP_FILTER_STATION_IP) {\r
+\r
+      for (Index = 0; Index < NewFilter->IpCnt; ++Index) {\r
+        if (IP4_IS_MULTICAST (EFI_NTOHL (NewFilter->IpList[Index].v4))) {\r
+          //\r
+          // Join the mutilcast group\r
+          //\r
+          Status = Private->Udp4Read->Groups (Private->Udp4Read, TRUE, &NewFilter->IpList[Index].v4);\r
+          if (EFI_ERROR (Status)) {\r
+            return Status;\r
+          }\r
+        }\r
+      }\r
+    }\r
+  }\r
+\r
+\r
+  //\r
+  // Save the new filter.\r
+  //\r
+  CopyMem (&Mode->IpFilter, NewFilter, sizeof (Mode->IpFilter));\r
+\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+\r
+/**                                                                 \r
+  Uses the ARP protocol to resolve a MAC address.\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
+  then EFI_ABORTED is returned.\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
+  @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
 **/\r
 EFI_STATUS\r
 EFIAPI\r
@@ -1117,37 +1897,97 @@ EfiPxeBcArp (
   IN EFI_MAC_ADDRESS                  * MacAddr OPTIONAL\r
   )\r
 {\r
-  return EFI_UNSUPPORTED;\r
-}\r
+  PXEBC_PRIVATE_DATA      *Private;\r
+  EFI_PXE_BASE_CODE_MODE  *Mode;\r
+  EFI_STATUS              Status;\r
+  EFI_MAC_ADDRESS         TempMacAddr;\r
 \r
+  if (This == NULL || IpAddr == NULL) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
 \r
-/**\r
-  GC_NOTO: Add function description\r
-\r
-  @param  This                                        GC_NOTO: add argument\r
-                                                      description\r
-  @param  NewAutoArp                                  GC_NOTO: add argument\r
-                                                      description\r
-  @param  NewSendGUID                                 GC_NOTO: add argument\r
-                                                      description\r
-  @param  NewTTL                                      GC_NOTO: add argument\r
-                                                      description\r
-  @param  NewToS                                      GC_NOTO: add argument\r
-                                                      description\r
-  @param  NewMakeCallback                             GC_NOTO: add argument\r
-                                                      description\r
-\r
-  @return GC_NOTO: add return values\r
+  Private = PXEBC_PRIVATE_DATA_FROM_PXEBC (This);\r
+  Mode    = Private->PxeBc.Mode;\r
+\r
+  if (!Mode->Started) {\r
+    return EFI_NOT_STARTED;\r
+  }\r
+\r
+  if (!Private->AddressIsOk || Mode->UsingIpv6) {\r
+    //\r
+    // We can't resolve the IP address if we don't have a local address now.\r
+    // Don't have ARP for IPv6.\r
+    //\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
+  Mode->IcmpErrorReceived = FALSE;\r
+\r
+  if (!Mode->AutoArp) {\r
+    //\r
+    // If AutoArp is set false, check arp cache\r
+    //\r
+    UpdateArpCache (This);\r
+    if (!FindInArpCache (Mode, &IpAddr->v4, &TempMacAddr)) {\r
+      return EFI_DEVICE_ERROR;\r
+    }\r
+  } else {\r
+    Status = Private->Arp->Request (Private->Arp, &IpAddr->v4, NULL, &TempMacAddr);\r
+    if (EFI_ERROR (Status)) {\r
+      if (Status == EFI_ICMP_ERROR) {\r
+        Mode->IcmpErrorReceived = TRUE;\r
+      }\r
+      return Status;\r
+    }\r
+  }\r
+\r
+  if (MacAddr != NULL) {\r
+    CopyMem (MacAddr, &TempMacAddr, sizeof (EFI_MAC_ADDRESS));\r
+  }\r
 \r
+  return EFI_SUCCESS;\r
+}\r
+\r
+/**                                                                 \r
+  Updates the parameters that affect the operation of the PXE Base Code Protocol.\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
+  as required by the PXE Base Code Protocol. If NewAutoArp is FALSE, then no ARP\r
+  Protocol packets will be generated. In this case, the only mappings that are\r
+  available are those stored in the ArpCache of the EFI_PXE_BASE_CODE_MODE structure.\r
+  If there are not enough mappings in the ArpCache to perform a PXE Base Code Protocol\r
+  service, then the service will fail. This function updates the AutoArp field of\r
+  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
+  @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
+  @param  NewSendGUID           If not NULL, a pointer to a value that specifies whether to replace the\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
+  @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
+  @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
+  @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
 **/\r
 EFI_STATUS\r
 EFIAPI\r
 EfiPxeBcSetParameters (\r
   IN EFI_PXE_BASE_CODE_PROTOCOL       *This,\r
-  IN BOOLEAN                          *NewAutoArp, OPTIONAL\r
-  IN BOOLEAN                          *NewSendGUID, OPTIONAL\r
-  IN UINT8                            *NewTTL, OPTIONAL\r
-  IN UINT8                            *NewToS, OPTIONAL\r
+  IN BOOLEAN                          *NewAutoArp OPTIONAL,\r
+  IN BOOLEAN                          *NewSendGUID OPTIONAL,\r
+  IN UINT8                            *NewTTL OPTIONAL,\r
+  IN UINT8                            *NewToS OPTIONAL,\r
   IN BOOLEAN                          *NewMakeCallback  // OPTIONAL\r
   )\r
 {\r
@@ -1165,13 +2005,13 @@ EfiPxeBcSetParameters (
   Private = PXEBC_PRIVATE_DATA_FROM_PXEBC (This);\r
   Mode    = Private->PxeBc.Mode;\r
 \r
-  if (NewSendGUID != NULL && *NewSendGUID == TRUE) {\r
+  if (NewSendGUID != NULL && *NewSendGUID) {\r
     //\r
     // FixMe, cann't locate SendGuid\r
     //\r
   }\r
 \r
-  if (NewMakeCallback != NULL && *NewMakeCallback == TRUE) {\r
+  if (NewMakeCallback != NULL && *NewMakeCallback) {\r
 \r
     Status = gBS->HandleProtocol (\r
                     Private->Controller,\r
@@ -1233,39 +2073,38 @@ ON_EXIT:
   return Status;\r
 }\r
 \r
-\r
-/**\r
-  GC_NOTO: Add function description\r
-\r
-  @param  This                                        GC_NOTO: add argument\r
-                                                      description\r
-  @param  NewStationIp                                GC_NOTO: add argument\r
-                                                      description\r
-  @param  NewSubnetMask                               GC_NOTO: add argument\r
-                                                      description\r
-\r
-  @retval EFI_INVALID_PARAMETER                       GC_NOTO: Add description for\r
-                                                      return value\r
-  @retval EFI_INVALID_PARAMETER                       GC_NOTO: Add description for\r
-                                                      return value\r
-  @retval EFI_INVALID_PARAMETER                       GC_NOTO: Add description for\r
-                                                      return value\r
-  @retval EFI_NOT_STARTED                             GC_NOTO: Add description for\r
-                                                      return value\r
-  @retval EFI_SUCCESS                                 GC_NOTO: Add description for\r
-                                                      return value\r
-\r
+/**                                                                 \r
+  Updates the station IP address and/or subnet mask values of a network device.\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
+  this function updates the StationIp field of the EFI_PXE_BASE_CODE_MODE structure\r
+  with NewStationIp. The NewSubnetMask field is used to modify the network device's current subnet\r
+  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
+  @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
+  @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
 **/\r
 EFI_STATUS\r
 EFIAPI\r
 EfiPxeBcSetStationIP (\r
   IN EFI_PXE_BASE_CODE_PROTOCOL       * This,\r
-  IN EFI_IP_ADDRESS                   * NewStationIp, OPTIONAL\r
+  IN EFI_IP_ADDRESS                   * NewStationIp  OPTIONAL,\r
   IN EFI_IP_ADDRESS                   * NewSubnetMask OPTIONAL\r
   )\r
 {\r
   PXEBC_PRIVATE_DATA      *Private;\r
   EFI_PXE_BASE_CODE_MODE  *Mode;\r
+  EFI_ARP_CONFIG_DATA     ArpConfigData;\r
 \r
   if (This == NULL) {\r
     return EFI_INVALID_PARAMETER;\r
@@ -1287,72 +2126,89 @@ EfiPxeBcSetStationIP (
   }\r
 \r
   if (NewStationIp != NULL) {\r
-    Mode->StationIp = *NewStationIp;\r
+    CopyMem (&Mode->StationIp, NewStationIp, sizeof (EFI_IP_ADDRESS));\r
+    CopyMem (&Private->StationIp, NewStationIp, sizeof (EFI_IP_ADDRESS));\r
   }\r
 \r
   if (NewSubnetMask != NULL) {\r
-    Mode->SubnetMask = *NewSubnetMask;\r
+    CopyMem (&Mode->SubnetMask, NewSubnetMask, sizeof (EFI_IP_ADDRESS));\r
+    CopyMem (&Private->SubnetMask ,NewSubnetMask, sizeof (EFI_IP_ADDRESS));\r
   }\r
 \r
   Private->AddressIsOk = TRUE;\r
 \r
-  return EFI_SUCCESS;\r
-}\r
+  if (!Mode->UsingIpv6) {\r
+    //\r
+    // If in IPv4 mode, configure the corresponding ARP with this new\r
+    // station IP address.\r
+    //\r
+    ZeroMem (&ArpConfigData, sizeof (EFI_ARP_CONFIG_DATA));\r
 \r
+    ArpConfigData.SwAddressType   = 0x0800;\r
+    ArpConfigData.SwAddressLength = sizeof (EFI_IPv4_ADDRESS);\r
+    ArpConfigData.StationAddress  = &Private->StationIp.v4;\r
 \r
-/**\r
-  GC_NOTO: Add function description\r
-\r
-  @param  This                                        GC_NOTO: add argument\r
-                                                      description\r
-  @param  NewDhcpDiscoverValid                        GC_NOTO: add argument\r
-                                                      description\r
-  @param  NewDhcpAckReceived                          GC_NOTO: add argument\r
-                                                      description\r
-  @param  NewProxyOfferReceived                       GC_NOTO: add argument\r
-                                                      description\r
-  @param  NewPxeDiscoverValid                         GC_NOTO: add argument\r
-                                                      description\r
-  @param  NewPxeReplyReceived                         GC_NOTO: add argument\r
-                                                      description\r
-  @param  NewPxeBisReplyReceived                      GC_NOTO: add argument\r
-                                                      description\r
-  @param  NewDhcpDiscover                             GC_NOTO: add argument\r
-                                                      description\r
-  @param  NewDhcpAck                                  GC_NOTO: add argument\r
-                                                      description\r
-  @param  NewProxyOffer                               GC_NOTO: add argument\r
-                                                      description\r
-  @param  NewPxeDiscover                              GC_NOTO: add argument\r
-                                                      description\r
-  @param  NewPxeReply                                 GC_NOTO: add argument\r
-                                                      description\r
-  @param  NewPxeBisReply                              GC_NOTO: add argument\r
-                                                      description\r
-\r
-  @retval EFI_INVALID_PARAMETER                       GC_NOTO: Add description for\r
-                                                      return value\r
-  @retval EFI_NOT_STARTED                             GC_NOTO: Add description for\r
-                                                      return value\r
-  @retval EFI_SUCCESS                                 GC_NOTO: Add description for\r
-                                                      return value\r
+    Private->Arp->Configure (Private->Arp, NULL);\r
+    Private->Arp->Configure (Private->Arp, &ArpConfigData);\r
 \r
+    //\r
+    // Update the route table.\r
+    //\r
+    Mode->RouteTableEntries                = 1;\r
+    Mode->RouteTable[0].IpAddr.Addr[0]     = Private->StationIp.Addr[0] & Private->SubnetMask.Addr[0];\r
+    Mode->RouteTable[0].SubnetMask.Addr[0] = Private->SubnetMask.Addr[0];\r
+    Mode->RouteTable[0].GwAddr.Addr[0]     = 0;\r
+  }\r
+\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+/**                                                                 \r
+  Updates the contents of the cached DHCP and Discover packets.\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
+  @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
+  @param  NewDhcpAckReceived     Pointer to a value that will replace the current\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
+  @param  NewPxeReplyReceived    Pointer to a value that will replace the current\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
+  @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
+  @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
 EFI_STATUS\r
 EFIAPI\r
 EfiPxeBcSetPackets (\r
   IN EFI_PXE_BASE_CODE_PROTOCOL       * This,\r
-  IN BOOLEAN                          * NewDhcpDiscoverValid, OPTIONAL\r
-  IN BOOLEAN                          * NewDhcpAckReceived, OPTIONAL\r
-  IN BOOLEAN                          * NewProxyOfferReceived, OPTIONAL\r
-  IN BOOLEAN                          * NewPxeDiscoverValid, OPTIONAL\r
-  IN BOOLEAN                          * NewPxeReplyReceived, OPTIONAL\r
-  IN BOOLEAN                          * NewPxeBisReplyReceived, OPTIONAL\r
-  IN EFI_PXE_BASE_CODE_PACKET         * NewDhcpDiscover, OPTIONAL\r
-  IN EFI_PXE_BASE_CODE_PACKET         * NewDhcpAck, OPTIONAL\r
-  IN EFI_PXE_BASE_CODE_PACKET         * NewProxyOffer, OPTIONAL\r
-  IN EFI_PXE_BASE_CODE_PACKET         * NewPxeDiscover, OPTIONAL\r
-  IN EFI_PXE_BASE_CODE_PACKET         * NewPxeReply, OPTIONAL\r
+  IN BOOLEAN                          * NewDhcpDiscoverValid OPTIONAL,\r
+  IN BOOLEAN                          * NewDhcpAckReceived OPTIONAL,\r
+  IN BOOLEAN                          * NewProxyOfferReceived OPTIONAL,\r
+  IN BOOLEAN                          * NewPxeDiscoverValid OPTIONAL,\r
+  IN BOOLEAN                          * NewPxeReplyReceived OPTIONAL,\r
+  IN BOOLEAN                          * NewPxeBisReplyReceived OPTIONAL,\r
+  IN EFI_PXE_BASE_CODE_PACKET         * NewDhcpDiscover OPTIONAL,\r
+  IN EFI_PXE_BASE_CODE_PACKET         * NewDhcpAck OPTIONAL,\r
+  IN EFI_PXE_BASE_CODE_PACKET         * NewProxyOffer OPTIONAL,\r
+  IN EFI_PXE_BASE_CODE_PACKET         * NewPxeDiscover OPTIONAL,\r
+  IN EFI_PXE_BASE_CODE_PACKET         * NewPxeReply OPTIONAL,\r
   IN EFI_PXE_BASE_CODE_PACKET         * NewPxeBisReply OPTIONAL\r
   )\r
 {\r
@@ -1440,32 +2296,32 @@ EFI_PXE_BASE_CODE_PROTOCOL  mPxeBcProtocolTemplate = {
   NULL\r
 };\r
 \r
-\r
-/**\r
-  GC_NOTO: Add function description\r
-\r
-  @param  This                                        GC_NOTO: add argument\r
-                                                      description\r
-  @param  Function                                    GC_NOTO: add argument\r
-                                                      description\r
-  @param  Received                                    GC_NOTO: add argument\r
-                                                      description\r
-  @param  PacketLength                                GC_NOTO: add argument\r
-                                                      description\r
-  @param  PacketPtr                                   GC_NOTO: add argument\r
-                                                      description\r
-\r
-  @retval EFI_PXE_BASE_CODE_CALLBACK_STATUS_ABORT     GC_NOTO: Add description for\r
-                                                      return value\r
-  @retval EFI_PXE_BASE_CODE_CALLBACK_STATUS_CONTINUE  GC_NOTO: Add description for\r
-                                                      return value\r
-  @retval EFI_PXE_BASE_CODE_CALLBACK_STATUS_CONTINUE  GC_NOTO: Add description for\r
-                                                      return value\r
-  @retval EFI_PXE_BASE_CODE_CALLBACK_STATUS_CONTINUE  GC_NOTO: Add description for\r
-                                                      return value\r
-  @retval EFI_PXE_BASE_CODE_CALLBACK_STATUS_CONTINUE  GC_NOTO: Add description for\r
-                                                      return value\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
+  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
+  are zero and NULL respectively, then this is a status update callback. If the operation specified\r
+  by Function is to continue, then CALLBACK_STATUS_CONTINUE should be returned. If the operation\r
+  specified by Function should be aborted, then CALLBACK_STATUS_ABORT should be returned. Due to\r
+  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
+  @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  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
+  @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
+  @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
+  @retval EFI_PXE_BASE_CODE_CALLBACK_STATUS_ABORT    if Function specifies an abort operation\r
+                                   \r
 **/\r
 EFI_PXE_BASE_CODE_CALLBACK_STATUS\r
 EFIAPI\r
@@ -1540,17 +2396,17 @@ EFI_PXE_BASE_CODE_CALLBACK_PROTOCOL mPxeBcCallBackTemplate = {
 \r
 \r
 /**\r
-  GC_NOTO: Add function description\r
-\r
-  @param  Private                                     GC_NOTO: add argument\r
-                                                      description\r
-  @param  BufferSize                                  GC_NOTO: add argument\r
-                                                      description\r
-  @param  Buffer                                      GC_NOTO: add argument\r
-                                                      description\r
+  Find the boot file.\r
 \r
-  @return GC_NOTO: add return values\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
 EFI_STATUS\r
 DiscoverBootFile (\r
@@ -1623,16 +2479,26 @@ DiscoverBootFile (
     Packet = &Private->Dhcp4Ack;\r
   }\r
 \r
-  CopyMem (&Private->ServerIp, &Packet->Packet.Offer.Dhcp4.Header.ServerAddr, sizeof (EFI_IPv4_ADDRESS));\r
-  if (Private->ServerIp.Addr[0] == 0) {\r
-    //\r
-    // next server ip address is zero, use option 54 instead\r
-    //\r
+  //\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 (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
       sizeof (EFI_IPv4_ADDRESS)\r
       );\r
+  } else {\r
+    CopyMem (\r
+      &Private->ServerIp, \r
+      &Packet->Packet.Offer.Dhcp4.Header.ServerAddr, \r
+      sizeof (EFI_IPv4_ADDRESS)\r
+      );\r
+  }\r
+  if (Private->ServerIp.Addr[0] == 0) {\r
+    return EFI_DEVICE_ERROR;\r
   }\r
 \r
   ASSERT (Packet->Dhcp4Option[PXEBC_DHCP4_TAG_INDEX_BOOTFILE] != NULL);\r
@@ -1673,25 +2539,32 @@ DiscoverBootFile (
   return Status;\r
 }\r
 \r
-\r
 /**\r
-  GC_NOTO: Add function description\r
-\r
-  @param  This                                        GC_NOTO: add argument\r
-                                                      description\r
-  @param  FilePath                                    GC_NOTO: add argument\r
-                                                      description\r
-  @param  BootPolicy                                  GC_NOTO: add argument\r
-                                                      description\r
-  @param  BufferSize                                  GC_NOTO: add argument\r
-                                                      description\r
-  @param  Buffer                                      GC_NOTO: add argument\r
-                                                      description\r
-\r
-  @retval EFI_INVALID_PARAMETER                       GC_NOTO: Add description for\r
-                                                      return value\r
-  @retval EFI_UNSUPPORTED                             GC_NOTO: Add description for\r
-                                                      return value\r
+  Causes the driver to load a specified file.\r
+\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
+                                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
+                                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
+                                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
+                                      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
+  @retval EFI_NO_RESPONSE             The remote system did not respond.\r
+  @retval EFI_NOT_FOUND               The file was not found.\r
+  @retval EFI_ABORTED                 The file load process was manually cancelled.\r
 \r
 **/\r
 EFI_STATUS\r
@@ -1765,10 +2638,25 @@ EfiPxeLoadFile (
 \r
     if (sizeof (UINTN) < sizeof (UINT64) && (TmpBufSize > 0xFFFFFFFF)) {\r
       Status = EFI_DEVICE_ERROR;\r
-    } else {\r
+    } else if (TmpBufSize > 0 && *BufferSize >= (UINTN) TmpBufSize && Buffer != NULL) {\r
+      *BufferSize = (UINTN) TmpBufSize;\r
+      Status = PxeBc->Mtftp (\r
+                        PxeBc,\r
+                        EFI_PXE_BASE_CODE_TFTP_READ_FILE,\r
+                        Buffer,\r
+                        FALSE,\r
+                        &TmpBufSize,\r
+                        &BlockSize,\r
+                        &Private->ServerIp,\r
+                        (UINT8 *) Private->BootFileName,\r
+                        NULL,\r
+                        FALSE\r
+                        );\r
+    } else if (TmpBufSize > 0) {\r
       *BufferSize = (UINTN) TmpBufSize;\r
+      Status      = EFI_BUFFER_TOO_SMALL;\r
     }\r
-  } else if (Buffer == NULL) {\r
+  } else if (Buffer == NULL || Private->FileSize > *BufferSize) {\r
     *BufferSize = Private->FileSize;\r
     Status      = EFI_BUFFER_TOO_SMALL;\r
   } else {\r
@@ -1804,57 +2692,46 @@ EfiPxeLoadFile (
           &Private->LoadFileCallback\r
           );\r
   }\r
+\r
   //\r
   // Check download status\r
   //\r
-  switch (Status) {\r
-\r
-  case EFI_SUCCESS:\r
-    break;\r
+  if (Status == EFI_SUCCESS) {\r
+    return EFI_SUCCESS;\r
 \r
-  case EFI_BUFFER_TOO_SMALL:\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
       return Status;\r
     }\r
-    break;\r
 \r
-  case EFI_DEVICE_ERROR:\r
+  } else if (Status == EFI_DEVICE_ERROR) {\r
     AsciiPrint ("PXE-E07: Network device error.\n");\r
-    break;\r
 \r
-  case EFI_OUT_OF_RESOURCES:\r
+  } else if (Status == EFI_OUT_OF_RESOURCES) {\r
     AsciiPrint ("PXE-E09: Could not allocate I/O buffers.\n");\r
-    break;\r
 \r
-  case EFI_NO_MEDIA:\r
+  } else if (Status == EFI_NO_MEDIA) {\r
     AsciiPrint ("PXE-E12: Could not detect network connection.\n");\r
-    break;\r
 \r
-  case EFI_NO_RESPONSE:\r
+  } else if (Status == EFI_NO_RESPONSE) {\r
     AsciiPrint ("PXE-E16: No offer received.\n");\r
-    break;\r
 \r
-  case EFI_TIMEOUT:\r
+  } else if (Status == EFI_TIMEOUT) {\r
     AsciiPrint ("PXE-E18: Server response timeout.\n");\r
-    break;\r
 \r
-  case EFI_ABORTED:\r
+  } else if (Status == EFI_ABORTED) {\r
     AsciiPrint ("PXE-E21: Remote boot cancelled.\n");\r
-    break;\r
 \r
-  case EFI_ICMP_ERROR:\r
+  } else if (Status == EFI_ICMP_ERROR) {\r
     AsciiPrint ("PXE-E22: Client received ICMP error from server.\n");\r
-    break;\r
 \r
-  case EFI_TFTP_ERROR:\r
+  } else if (Status == EFI_TFTP_ERROR) {\r
     AsciiPrint ("PXE-E23: Client received TFTP error from server.\n");\r
-    break;\r
 \r
-  default:\r
+  } else {\r
     AsciiPrint ("PXE-E99: Unexpected network error.\n");\r
-    break;\r
   }\r
 \r
   PxeBc->Stop (PxeBc);\r