]> git.proxmox.com Git - mirror_edk2.git/blobdiff - NetworkPkg/HttpBootDxe/HttpBootDhcp4.c
NetworkPkg/HttpBootDxe: fix typo in DHCPv4 packet parsing
[mirror_edk2.git] / NetworkPkg / HttpBootDxe / HttpBootDhcp4.c
index 7486d24ead26849c7c06ffe7236c2fbdf5d7baeb..229e6cb0ec6a73791a9c25a55ff30cb90caf31fc 100644 (file)
-/** @file
-  Functions implementation related with DHCPv4 for HTTP boot driver.
-
-Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>
-This program and the accompanying materials are licensed and made available under 
-the terms and conditions of the BSD License that accompanies this distribution.  
-The full text of the license may be found at
-http://opensource.org/licenses/bsd-license.php.                                          
-    
-THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,                     
-WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
-
-**/
-
-#include "HttpBootDxe.h"
-
-//
-// This is a map from the interested DHCP4 option tags' index to the tag value.
-//
-UINT8 mInterestedDhcp4Tags[HTTP_BOOT_DHCP4_TAG_INDEX_MAX] = {
-  HTTP_BOOT_DHCP4_TAG_BOOTFILE_LEN,
-  HTTP_BOOT_DHCP4_TAG_OVERLOAD,
-  HTTP_BOOT_DHCP4_TAG_MSG_TYPE,
-  HTTP_BOOT_DHCP4_TAG_SERVER_ID,
-  HTTP_BOOT_DHCP4_TAG_CLASS_ID,
-  HTTP_BOOT_DHCP4_TAG_BOOTFILE,
-  HTTP_BOOT_DHCP4_TAG_DNS_SERVER
-};
-
-//
-// There are 4 times retries with the value of 4, 8, 16 and 32, refers to UEFI 2.5 spec.
-//
-UINT32 mHttpDhcpTimeout[4] = {4, 8, 16, 32};
-
-/**
-  Build the options buffer for the DHCPv4 request packet.
-
-  @param[in]  Private             Pointer to HTTP boot driver private data.
-  @param[out] OptList             Pointer to the option pointer array.
-  @param[in]  Buffer              Pointer to the buffer to contain the option list.
-
-  @return     Index               The count of the built-in options.
-
-**/
-UINT32
-HttpBootBuildDhcp4Options (
-  IN  HTTP_BOOT_PRIVATE_DATA        *Private,
-  OUT EFI_DHCP4_PACKET_OPTION       **OptList,
-  IN  UINT8                         *Buffer
-  )
-{
-  HTTP_BOOT_DHCP4_OPTION_ENTRY  OptEnt;
-  UINT16                        Value;
-  UINT32                        Index;
-
-  Index      = 0;
-  OptList[0] = (EFI_DHCP4_PACKET_OPTION *) Buffer;
-
-  //
-  // Append parameter request list option.
-  //
-  OptList[Index]->OpCode    = HTTP_BOOT_DHCP4_TAG_PARA_LIST;
-  OptList[Index]->Length    = 27;
-  OptEnt.Para               = (HTTP_BOOT_DHCP4_OPTION_PARA *) OptList[Index]->Data;
-  OptEnt.Para->ParaList[0]  = HTTP_BOOT_DHCP4_TAG_NETMASK;
-  OptEnt.Para->ParaList[1]  = HTTP_BOOT_DHCP4_TAG_TIME_OFFSET;
-  OptEnt.Para->ParaList[2]  = HTTP_BOOT_DHCP4_TAG_ROUTER;
-  OptEnt.Para->ParaList[3]  = HTTP_BOOT_DHCP4_TAG_TIME_SERVER;
-  OptEnt.Para->ParaList[4]  = HTTP_BOOT_DHCP4_TAG_NAME_SERVER;
-  OptEnt.Para->ParaList[5]  = HTTP_BOOT_DHCP4_TAG_DNS_SERVER;
-  OptEnt.Para->ParaList[6]  = HTTP_BOOT_DHCP4_TAG_HOSTNAME;
-  OptEnt.Para->ParaList[7]  = HTTP_BOOT_DHCP4_TAG_BOOTFILE_LEN;
-  OptEnt.Para->ParaList[8]  = HTTP_BOOT_DHCP4_TAG_DOMAINNAME;
-  OptEnt.Para->ParaList[9]  = HTTP_BOOT_DHCP4_TAG_ROOTPATH;
-  OptEnt.Para->ParaList[10] = HTTP_BOOT_DHCP4_TAG_EXTEND_PATH;
-  OptEnt.Para->ParaList[11] = HTTP_BOOT_DHCP4_TAG_EMTU;
-  OptEnt.Para->ParaList[12] = HTTP_BOOT_DHCP4_TAG_TTL;
-  OptEnt.Para->ParaList[13] = HTTP_BOOT_DHCP4_TAG_BROADCAST;
-  OptEnt.Para->ParaList[14] = HTTP_BOOT_DHCP4_TAG_NIS_DOMAIN;
-  OptEnt.Para->ParaList[15] = HTTP_BOOT_DHCP4_TAG_NIS_SERVER;
-  OptEnt.Para->ParaList[16] = HTTP_BOOT_DHCP4_TAG_NTP_SERVER;
-  OptEnt.Para->ParaList[17] = HTTP_BOOT_DHCP4_TAG_VENDOR;
-  OptEnt.Para->ParaList[18] = HTTP_BOOT_DHCP4_TAG_REQUEST_IP;
-  OptEnt.Para->ParaList[19] = HTTP_BOOT_DHCP4_TAG_LEASE;
-  OptEnt.Para->ParaList[20] = HTTP_BOOT_DHCP4_TAG_SERVER_ID;
-  OptEnt.Para->ParaList[21] = HTTP_BOOT_DHCP4_TAG_T1;
-  OptEnt.Para->ParaList[22] = HTTP_BOOT_DHCP4_TAG_T2;
-  OptEnt.Para->ParaList[23] = HTTP_BOOT_DHCP4_TAG_CLASS_ID;
-  OptEnt.Para->ParaList[25] = HTTP_BOOT_DHCP4_TAG_BOOTFILE;
-  OptEnt.Para->ParaList[26] = HTTP_BOOT_DHCP4_TAG_UUID;
-  Index++;
-  OptList[Index]            = GET_NEXT_DHCP_OPTION (OptList[Index - 1]);
-
-  //
-  // Append UUID/Guid-based client identifier option
-  //
-  OptList[Index]->OpCode  = HTTP_BOOT_DHCP4_TAG_UUID;
-  OptList[Index]->Length  = (UINT8) sizeof (HTTP_BOOT_DHCP4_OPTION_UUID);
-  OptEnt.Uuid             = (HTTP_BOOT_DHCP4_OPTION_UUID *) OptList[Index]->Data;
-  OptEnt.Uuid->Type       = 0;
-  if (EFI_ERROR (NetLibGetSystemGuid ((EFI_GUID *) OptEnt.Uuid->Guid))) {
-    //
-    // Zero the Guid to indicate NOT programable if failed to get system Guid.
-    //
-    ZeroMem (OptEnt.Uuid->Guid, sizeof (EFI_GUID));
-  }
-  Index++;
-  OptList[Index]          = GET_NEXT_DHCP_OPTION (OptList[Index - 1]);
-
-  //
-  // Append client network device interface option
-  //
-  OptList[Index]->OpCode  = HTTP_BOOT_DHCP4_TAG_UNDI;
-  OptList[Index]->Length  = (UINT8) sizeof (HTTP_BOOT_DHCP4_OPTION_UNDI);
-  OptEnt.Undi             = (HTTP_BOOT_DHCP4_OPTION_UNDI *) OptList[Index]->Data;
-
-  if (Private->Nii != NULL) {
-    OptEnt.Undi->Type     = Private->Nii->Type;
-    OptEnt.Undi->MajorVer = Private->Nii->MajorVer;
-    OptEnt.Undi->MinorVer = Private->Nii->MinorVer;
-  } else {
-    OptEnt.Undi->Type     = DEFAULT_UNDI_TYPE;
-    OptEnt.Undi->MajorVer = DEFAULT_UNDI_MAJOR;
-    OptEnt.Undi->MinorVer = DEFAULT_UNDI_MINOR;
-  }
-
-  Index++;
-  OptList[Index] = GET_NEXT_DHCP_OPTION (OptList[Index - 1]);
-
-  //
-  // Append client system architecture option
-  //
-  OptList[Index]->OpCode  = HTTP_BOOT_DHCP4_TAG_ARCH;
-  OptList[Index]->Length  = (UINT8) sizeof (HTTP_BOOT_DHCP4_OPTION_ARCH);
-  OptEnt.Arch             = (HTTP_BOOT_DHCP4_OPTION_ARCH *) OptList[Index]->Data;
-  Value                   = HTONS (EFI_HTTP_BOOT_CLIENT_SYSTEM_ARCHITECTURE);
-  CopyMem (&OptEnt.Arch->Type, &Value, sizeof (UINT16));
-  Index++;
-  OptList[Index]          = GET_NEXT_DHCP_OPTION (OptList[Index - 1]);
-
-  //
-  // Append vendor class identify option
-  //
-  OptList[Index]->OpCode  = HTTP_BOOT_DHCP4_TAG_CLASS_ID;
-  OptList[Index]->Length  = (UINT8) sizeof (HTTP_BOOT_DHCP4_OPTION_CLID);
-  OptEnt.Clid             = (HTTP_BOOT_DHCP4_OPTION_CLID *) OptList[Index]->Data;
-  CopyMem (
-    OptEnt.Clid,
-    DEFAULT_CLASS_ID_DATA,
-    sizeof (HTTP_BOOT_DHCP4_OPTION_CLID)
-    );
-  HttpBootUintnToAscDecWithFormat (
-    EFI_HTTP_BOOT_CLIENT_SYSTEM_ARCHITECTURE,
-    OptEnt.Clid->ArchitectureType,
-    sizeof (OptEnt.Clid->ArchitectureType)
-    );
-
-  if (Private->Nii != NULL) {
-    CopyMem (OptEnt.Clid->InterfaceName, Private->Nii->StringId, sizeof (OptEnt.Clid->InterfaceName));
-    HttpBootUintnToAscDecWithFormat (Private->Nii->MajorVer, OptEnt.Clid->UndiMajor, sizeof (OptEnt.Clid->UndiMajor));
-    HttpBootUintnToAscDecWithFormat (Private->Nii->MinorVer, OptEnt.Clid->UndiMinor, sizeof (OptEnt.Clid->UndiMinor));
-  }
-
-  Index++;
-
-  return Index;
-}
-
-/**
-  Parse a certain dhcp4 option by OptTag in Buffer, and return with start pointer.
-
-  @param[in]  Buffer              Pointer to the option buffer.
-  @param[in]  Length              Length of the option buffer.
-  @param[in]  OptTag              Tag of the required option.
-
-  @retval     NULL                Failed to find the required option.
-  @retval     Others              The position of the required option.
-
-**/
-EFI_DHCP4_PACKET_OPTION *
-HttpBootParseDhcp4Options (
-  IN UINT8                      *Buffer,
-  IN UINT32                     Length,
-  IN UINT8                      OptTag
-  )
-{
-  EFI_DHCP4_PACKET_OPTION       *Option;
-  UINT32                        Offset;
-
-  Option  = (EFI_DHCP4_PACKET_OPTION *) Buffer;
-  Offset  = 0;
-
-  while (Offset < Length && Option->OpCode != HTTP_BOOT_DHCP4_TAG_EOP) {
-
-    if (Option->OpCode == OptTag) {
-      //
-      // Found the required option.
-      //
-      return Option;
-    }
-
-    //
-    // Skip the current option to the next.
-    //
-    if (Option->OpCode == HTTP_BOOT_DHCP4_TAG_PAD) {
-      Offset++;
-    } else {
-      Offset += Option->Length + 2;
-    }
-
-    Option = (EFI_DHCP4_PACKET_OPTION *) (Buffer + Offset);
-  }
-
-  return NULL;
-}
-
-/**
-  Cache the DHCPv4 packet.
-
-  @param[in]  Dst          Pointer to the cache buffer for DHCPv4 packet.
-  @param[in]  Src          Pointer to the DHCPv4 packet to be cached.
-
-**/
-VOID
-HttpBootCacheDhcp4Packet (
-  IN EFI_DHCP4_PACKET     *Dst,
-  IN EFI_DHCP4_PACKET     *Src
-  )
-{
-  ASSERT (Dst->Size >= Src->Length);
-
-  CopyMem (&Dst->Dhcp4, &Src->Dhcp4, Src->Length);
-  Dst->Length = Src->Length;
-}
-
-/**
-  Parse the cached DHCPv4 packet, including all the options.
-
-  @param[in]  Cache4           Pointer to cached DHCPv4 packet.
-
-  @retval     EFI_SUCCESS      Parsed the DHCPv4 packet successfully.
-  @retval     EFI_DEVICE_ERROR Failed to parse an invalid packet.
-
-**/
-EFI_STATUS
-HttpBootParseDhcp4Packet (
-  IN HTTP_BOOT_DHCP4_PACKET_CACHE    *Cache4
-  )
-{
-  EFI_DHCP4_PACKET               *Offer;
-  EFI_DHCP4_PACKET_OPTION        **Options;
-  UINTN                          Index;
-  EFI_DHCP4_PACKET_OPTION        *Option;
-  BOOLEAN                        IsProxyOffer;
-  BOOLEAN                        IsHttpOffer;
-  BOOLEAN                        IsDnsOffer;
-  BOOLEAN                        IpExpressedUri;
-  UINT8                          *Ptr8;
-  EFI_STATUS                     Status;
-  HTTP_BOOT_OFFER_TYPE           OfferType;
-  EFI_IPv4_ADDRESS               IpAddr;
-  
-  IsDnsOffer     = FALSE;
-  IpExpressedUri = FALSE;
-  IsProxyOffer   = FALSE;
-  IsHttpOffer    = FALSE;
-
-  ZeroMem (Cache4->OptList, sizeof (Cache4->OptList));
-
-  Offer   = &Cache4->Packet.Offer;
-  Options = Cache4->OptList;
-
-  //
-  // Parse DHCPv4 options in this offer, and store the pointers.
-  // First, try to parse DHCPv4 options from the DHCP optional parameters field.
-  //
-  for (Index = 0; Index < HTTP_BOOT_DHCP4_TAG_INDEX_MAX; Index++) {
-    Options[Index] = HttpBootParseDhcp4Options (
-                       Offer->Dhcp4.Option,
-                       GET_OPTION_BUFFER_LEN (Offer),
-                       mInterestedDhcp4Tags[Index]
-                       );
-  }
-  //
-  // Second, Check if bootfilename and serverhostname is overloaded to carry DHCP options refers to rfc-2132. 
-  // If yes, try to parse options from the BootFileName field, then ServerName field.
-  //
-  Option = Options[HTTP_BOOT_DHCP4_TAG_INDEX_OVERLOAD];
-  if (Option != NULL) {
-    if ((Option->Data[0] & HTTP_BOOT_DHCP4_OVERLOAD_FILE) != 0) {
-      for (Index = 0; Index < HTTP_BOOT_DHCP4_TAG_INDEX_MAX; Index++) {
-        if (Options[Index] == NULL) {
-          Options[Index] = HttpBootParseDhcp4Options (
-                             (UINT8 *) Offer->Dhcp4.Header.BootFileName,
-                             sizeof (Offer->Dhcp4.Header.BootFileName),
-                             mInterestedDhcp4Tags[Index]
-                             );
-        }
-      }
-    }
-    if ((Option->Data[0] & HTTP_BOOT_DHCP4_OVERLOAD_SERVER_NAME) != 0) {
-      for (Index = 0; Index < HTTP_BOOT_DHCP4_TAG_INDEX_MAX; Index++) {
-        if (Options[Index] == NULL) {
-          Options[Index] = HttpBootParseDhcp4Options (
-                             (UINT8 *) Offer->Dhcp4.Header.ServerName,
-                             sizeof (Offer->Dhcp4.Header.ServerName),
-                             mInterestedDhcp4Tags[Index]
-                             );
-        }
-      }
-    }
-  }
-
-  //
-  // The offer with "yiaddr" is a proxy offer.
-  //
-  if (Offer->Dhcp4.Header.YourAddr.Addr[0] == 0) {
-    IsProxyOffer = TRUE;
-  }
-
-  //
-  // The offer with "HttpClient" is a Http offer.
-  //
-  Option = Options[HTTP_BOOT_DHCP4_TAG_INDEX_CLASS_ID];
-  if ((Option != NULL) && (Option->Length >= 9) &&
-      (CompareMem (Option->Data, DEFAULT_CLASS_ID_DATA, 9) == 0)) {
-    IsHttpOffer = TRUE;
-  }
-
-  //
-  // The offer with Domain Server is a DNS offer.
-  //
-  Option = Options[HTTP_BOOT_DHCP4_TAG_INDEX_DNS_SERVER];
-  if (Option != NULL) {
-    IsDnsOffer = TRUE;
-  }
-
-  //
-  // Parse boot file name:
-  // Boot URI information is provided thru 'file' field in DHCP Header or option 67.
-  // According to RFC 2132, boot file name should be read from DHCP option 67 (bootfile name) if present.
-  // Otherwise, read from boot file field in DHCP header.
-  //
-  if (Options[HTTP_BOOT_DHCP4_TAG_INDEX_BOOTFILE] != NULL) {
-    //
-    // RFC 2132, Section 9.5 does not strictly state Bootfile name (option 67) is null
-    // terminated string. So force to append null terminated character at the end of string.
-    //
-    Ptr8 =  (UINT8*)&Options[HTTP_BOOT_DHCP4_TAG_INDEX_BOOTFILE]->Data[0];
-    Ptr8 += Options[HTTP_BOOT_DHCP4_TAG_INDEX_BOOTFILE]->Length;
-    if (*(Ptr8 - 1) != '\0') {
-      *Ptr8 = '\0';
-    }
-  } else if (Offer->Dhcp4.Header.BootFileName[0] != 0) {
-    //
-    // If the bootfile is not present and bootfilename is present in DHCPv4 packet, just parse it.
-    // Do not count dhcp option header here, or else will destroy the serverhostname.
-    //
-    Options[HTTP_BOOT_DHCP4_TAG_INDEX_BOOTFILE] = (EFI_DHCP4_PACKET_OPTION *)
-                                                    (&Offer->Dhcp4.Header.BootFileName[0] -
-                                                    OFFSET_OF (EFI_DHCP4_PACKET_OPTION, Data[0]));
-  }
-
-  //
-  // Http offer must have a boot URI.
-  //
-  if (IsHttpOffer && Options[HTTP_BOOT_DHCP4_TAG_INDEX_BOOTFILE] == NULL) {
-    return EFI_DEVICE_ERROR;
-  }
-
-  //
-  // Try to retrieve the IP of HTTP server from URI. 
-  //
-  if (IsHttpOffer) {
-    Status = HttpParseUrl (
-               (CHAR8*) Options[HTTP_BOOT_DHCP4_TAG_INDEX_BOOTFILE]->Data,
-               (UINT32) AsciiStrLen ((CHAR8*) Options[HTTP_BOOT_DHCP4_TAG_INDEX_BOOTFILE]->Data),
-               FALSE,
-               &Cache4->UriParser
-               );
-    if (EFI_ERROR (Status)) {
-      return EFI_DEVICE_ERROR;
-    }
-
-    Status = HttpUrlGetIp4 (
-               (CHAR8*) Options[HTTP_BOOT_DHCP4_TAG_INDEX_BOOTFILE]->Data,
-               Cache4->UriParser,
-               &IpAddr
-               );
-    IpExpressedUri = !EFI_ERROR (Status);
-  }
-
-  //
-  // Determine offer type of the DHCPv4 packet.
-  //
-  if (IsHttpOffer) {
-    if (IpExpressedUri) {
-      OfferType = IsProxyOffer ? HttpOfferTypeProxyIpUri : HttpOfferTypeDhcpIpUri;
-    } else {
-      if (!IsProxyOffer) {
-        OfferType = IsDnsOffer ? HttpOfferTypeDhcpNameUriDns : HttpOfferTypeDhcpNameUri;
-      } else {
-        OfferType = HttpOfferTypeProxyNameUri;
-      }
-    }
-
-  } else {
-    if (!IsProxyOffer) {
-      OfferType = IsDnsOffer ? HttpOfferTypeDhcpDns : HttpOfferTypeDhcpOnly;
-    } else {
-      return EFI_DEVICE_ERROR;
-    }
-  }
-  
-  Cache4->OfferType = OfferType;
-  return EFI_SUCCESS;
-}
-
-/**
-  Cache all the received DHCPv4 offers, and set OfferIndex and OfferCount.
-
-  @param[in]  Private               Pointer to HTTP boot driver private data.
-  @param[in]  RcvdOffer             Pointer to the received offer packet.
-
-**/
-VOID
-HttpBootCacheDhcp4Offer (
-  IN HTTP_BOOT_PRIVATE_DATA  *Private,
-  IN EFI_DHCP4_PACKET        *RcvdOffer
-  )
-{
-  HTTP_BOOT_DHCP4_PACKET_CACHE  *Cache4;
-  EFI_DHCP4_PACKET              *Offer;
-  HTTP_BOOT_OFFER_TYPE          OfferType;
-
-  ASSERT (Private->OfferNum < HTTP_BOOT_OFFER_MAX_NUM);
-  Cache4 = &Private->OfferBuffer[Private->OfferNum].Dhcp4;
-  Offer  = &Cache4->Packet.Offer;
-
-  //
-  // Cache the content of DHCPv4 packet firstly.
-  //
-  HttpBootCacheDhcp4Packet (Offer, RcvdOffer);
-
-  //
-  // Validate the DHCPv4 packet, and parse the options and offer type.
-  //
-  if (EFI_ERROR (HttpBootParseDhcp4Packet (Cache4))) {
-    return;
-  }
-
-  //
-  // Determine whether cache the current offer by type, and record OfferIndex and OfferCount.
-  //
-  OfferType = Cache4->OfferType;
-  ASSERT (OfferType < HttpOfferTypeMax);
-  ASSERT (Private->OfferCount[OfferType] < HTTP_BOOT_OFFER_MAX_NUM);
-  Private->OfferIndex[OfferType][Private->OfferCount[OfferType]] = Private->OfferNum;
-  Private->OfferCount[OfferType]++;
-  Private->OfferNum++;
-}
-
-/**
-  Select an DHCPv4 offer, and record SelectIndex and SelectProxyType.
-
-  @param[in]  Private             Pointer to HTTP boot driver private data.
-
-**/
-VOID
-HttpBootSelectDhcp4Offer (
-  IN HTTP_BOOT_PRIVATE_DATA  *Private
-  )
-{
-  Private->SelectIndex = 0;
-  Private->SelectProxyType = HttpOfferTypeMax;
-  
-  //
-  // Priority1: HttpOfferTypeDhcpIpUri                           
-  // Priority2: HttpOfferTypeDhcpNameUriDns                      
-  // Priority3: HttpOfferTypeDhcpOnly + HttpOfferTypeProxyIpUri  
-  // Priority4: HttpOfferTypeDhcpDns  + HttpOfferTypeProxyIpUri  
-  // Priority5: HttpOfferTypeDhcpDns  + HttpOfferTypeProxyNameUri
-  // Priority6: HttpOfferTypeDhcpDns  + HttpOfferTypeDhcpNameUri 
-  //    
-  if (Private->OfferCount[HttpOfferTypeDhcpIpUri] > 0) {
-    
-    Private->SelectIndex = Private->OfferIndex[HttpOfferTypeDhcpIpUri][0] + 1;
-    
-  } else if (Private->OfferCount[HttpOfferTypeDhcpNameUriDns] > 0) {
-  
-    Private->SelectIndex = Private->OfferIndex[HttpOfferTypeDhcpNameUriDns][0] + 1;
-    
-  } else if (Private->OfferCount[HttpOfferTypeDhcpOnly] > 0 &&
-             Private->OfferCount[HttpOfferTypeProxyIpUri] > 0) {
-             
-    Private->SelectIndex     = Private->OfferIndex[HttpOfferTypeDhcpOnly][0] + 1;
-    Private->SelectProxyType = HttpOfferTypeProxyIpUri;
-    
-  } else if (Private->OfferCount[HttpOfferTypeDhcpDns] > 0 &&
-             Private->OfferCount[HttpOfferTypeProxyIpUri] > 0) {
-             
-    Private->SelectIndex     = Private->OfferIndex[HttpOfferTypeDhcpDns][0] + 1;
-    Private->SelectProxyType = HttpOfferTypeProxyIpUri;
-    
-  } else if (Private->OfferCount[HttpOfferTypeDhcpDns] > 0 &&
-             Private->OfferCount[HttpOfferTypeProxyNameUri] > 0) {
-             
-    Private->SelectIndex     = Private->OfferIndex[HttpOfferTypeDhcpDns][0] + 1;
-    Private->SelectProxyType = HttpOfferTypeProxyNameUri;
-    
-  } else if (Private->OfferCount[HttpOfferTypeDhcpDns] > 0 &&
-             Private->OfferCount[HttpOfferTypeDhcpNameUri] > 0) {
-             
-    Private->SelectIndex     = Private->OfferIndex[HttpOfferTypeDhcpDns][0] + 1;
-    Private->SelectProxyType = HttpOfferTypeDhcpNameUri;
-  }
-}
-
-
-/**
-  EFI_DHCP4_CALLBACK is provided by the consumer of the EFI DHCPv4 Protocol driver
-  to intercept events that occurred in the configuration process.
-
-  @param[in]  This              Pointer to the EFI DHCPv4 Protocol.
-  @param[in]  Context           Pointer to the context set by EFI_DHCP4_PROTOCOL.Configure().
-  @param[in]  CurrentState      The current operational state of the EFI DHCPv4 Protocol driver.
-  @param[in]  Dhcp4Event        The event that occurs in the current state, which usually means a
-                                state transition.
-  @param[in]  Packet            The DHCPv4 packet that is going to be sent or already received.
-  @param[out] NewPacket         The packet that is used to replace the above Packet.
-
-  @retval EFI_SUCCESS           Tells the EFI DHCPv4 Protocol driver to continue the DHCP process.
-  @retval EFI_NOT_READY         Only used in the Dhcp4Selecting state. The EFI DHCPv4 Protocol
-                                driver will continue to wait for more DHCPOFFER packets until the
-                                retry timeout expires.
-  @retval EFI_ABORTED           Tells the EFI DHCPv4 Protocol driver to abort the current process
-                                and return to the Dhcp4Init or Dhcp4InitReboot state.
-
-**/
-EFI_STATUS
-EFIAPI
-HttpBootDhcp4CallBack (
-  IN  EFI_DHCP4_PROTOCOL               *This,
-  IN  VOID                             *Context,
-  IN  EFI_DHCP4_STATE                  CurrentState,
-  IN  EFI_DHCP4_EVENT                  Dhcp4Event,
-  IN  EFI_DHCP4_PACKET                 *Packet            OPTIONAL,
-  OUT EFI_DHCP4_PACKET                 **NewPacket        OPTIONAL
-  )
-{
-  HTTP_BOOT_PRIVATE_DATA               *Private;
-  EFI_DHCP4_PACKET_OPTION              *MaxMsgSize;
-  UINT16                               Value;
-  EFI_STATUS                           Status;
-
-  if ((Dhcp4Event != Dhcp4RcvdOffer) && (Dhcp4Event != Dhcp4SelectOffer)) {
-    return EFI_SUCCESS;
-  }
-  
-  Private = (HTTP_BOOT_PRIVATE_DATA *) Context;
-
-  //
-  // Override the Maximum DHCP Message Size.
-  //
-  MaxMsgSize = HttpBootParseDhcp4Options (
-                 Packet->Dhcp4.Option,
-                 GET_OPTION_BUFFER_LEN (Packet),
-                 HTTP_BOOT_DHCP4_TAG_MAXMSG
-                 );
-  if (MaxMsgSize != NULL) {
-    Value = HTONS (HTTP_BOOT_DHCP4_PACKET_MAX_SIZE);
-    CopyMem (MaxMsgSize->Data, &Value, sizeof (Value));
-  }
-
-  Status = EFI_SUCCESS;
-  switch (Dhcp4Event) {
-  case Dhcp4RcvdOffer:
-    Status = EFI_NOT_READY;
-    if (Private->OfferNum < HTTP_BOOT_OFFER_MAX_NUM) {
-      //
-      // Cache the DHCPv4 offers to OfferBuffer[] for select later, and record
-      // the OfferIndex and OfferCount.
-      //
-      HttpBootCacheDhcp4Offer (Private, Packet);
-    }
-    break;
-
-  case Dhcp4SelectOffer:
-    //
-    // Select offer according to the priority in UEFI spec, and record the SelectIndex 
-    // and SelectProxyType.
-    //
-    HttpBootSelectDhcp4Offer (Private);
-
-    if (Private->SelectIndex == 0) {
-      Status = EFI_ABORTED;
-    } else {
-      *NewPacket = &Private->OfferBuffer[Private->SelectIndex - 1].Dhcp4.Packet.Offer;
-    }
-    break;
-    
-  default:
-    break;
-  }
-
-  return Status;
-}
-
-/**
-  This function will register the IPv4 gateway address to the network device.
-  
-  @param[in]  Private             The pointer to HTTP_BOOT_PRIVATE_DATA.
-
-  @retval     EFI_SUCCESS         The new IP configuration has been configured successfully.
-  @retval     Others              Failed to configure the address.
-
-**/
-EFI_STATUS
-HttpBootRegisterIp4Gateway (
-  IN HTTP_BOOT_PRIVATE_DATA         *Private
-  )
-{
-  EFI_STATUS                      Status;
-  EFI_IP4_CONFIG2_PROTOCOL        *Ip4Config2;
-
-  ASSERT (!Private->UsingIpv6);
-
-  Ip4Config2 = Private->Ip4Config2;
-
-  //
-  // Configure the gateway if valid.
-  //
-  if (!EFI_IP4_EQUAL (&Private->GatewayIp, &mZeroIp4Addr)) {
-    Status = Ip4Config2->SetData (
-                           Ip4Config2,
-                           Ip4Config2DataTypeGateway,
-                           sizeof (EFI_IPv4_ADDRESS),
-                           &Private->GatewayIp
-                           );
-    if (EFI_ERROR (Status)) {
-      return Status;
-    }
-  }
-
-  return EFI_SUCCESS;
-}
-
-/**
-  This function will register the default DNS addresses to the network device.
-  
-  @param[in]  Private             The pointer to HTTP_BOOT_PRIVATE_DATA.
-  @param[in]  DataLength          Size of the buffer pointed to by DnsServerData in bytes.
-  @param[in]  DnsServerData       Point a list of DNS server address in an array
-                                  of EFI_IPv4_ADDRESS instances.
-
-  @retval     EFI_SUCCESS         The DNS configuration has been configured successfully.
-  @retval     Others              Failed to configure the address.
-
-**/
-EFI_STATUS
-HttpBootRegisterIp4Dns (
-  IN HTTP_BOOT_PRIVATE_DATA         *Private,
-  IN UINTN                          DataLength,
-  IN VOID                           *DnsServerData
-  )
-{
-  EFI_IP4_CONFIG2_PROTOCOL        *Ip4Config2;
-  
-  ASSERT (!Private->UsingIpv6);
-
-  Ip4Config2 = Private->Ip4Config2;
-  
-  return Ip4Config2->SetData (
-                       Ip4Config2,
-                       Ip4Config2DataTypeDnsServer,
-                       DataLength,
-                       DnsServerData
-                       );
-}
-
-
-/**
-  This function will switch the IP4 configuration policy to Static.
-
-  @param[in]  Private             Pointer to HTTP boot driver private data.
-
-  @retval     EFI_SUCCESS         The policy is already configured to static.
-  @retval     Others              Other error as indicated..
-
-**/
-EFI_STATUS
-HttpBootSetIpPolicy (
-  IN HTTP_BOOT_PRIVATE_DATA         *Private
-  )
-{
-  EFI_IP4_CONFIG2_POLICY          Policy;
-  EFI_STATUS                      Status;
-  EFI_IP4_CONFIG2_PROTOCOL        *Ip4Config2;
-  UINTN                           DataSize;
-
-  Ip4Config2 = Private->Ip4Config2;
-
-  DataSize = sizeof (EFI_IP4_CONFIG2_POLICY);
-  Status = Ip4Config2->GetData (
-                         Ip4Config2,
-                         Ip4Config2DataTypePolicy,
-                         &DataSize,
-                         &Policy
-                         );
-  if (EFI_ERROR (Status)) {
-    return Status;
-  }
-
-  if (Policy != Ip4Config2PolicyStatic) {
-    Policy = Ip4Config2PolicyStatic;
-    Status= Ip4Config2->SetData (
-                          Ip4Config2,
-                          Ip4Config2DataTypePolicy,
-                          sizeof (EFI_IP4_CONFIG2_POLICY),
-                          &Policy
-                          );
-    if (EFI_ERROR (Status)) {
-      return Status;
-    } 
-  }
-
-  return EFI_SUCCESS;
-}
-
-/**
-  Start the D.O.R.A DHCPv4 process to acquire the IPv4 address and other Http boot information.
-
-  @param[in]  Private           Pointer to HTTP boot driver private data.
-
-  @retval EFI_SUCCESS           The D.O.R.A process successfully finished.
-  @retval Others                Failed to finish the D.O.R.A process.
-
-**/
-EFI_STATUS
-HttpBootDhcp4Dora (
-  IN HTTP_BOOT_PRIVATE_DATA         *Private
-  )
-{
-  EFI_DHCP4_PROTOCOL           *Dhcp4;
-  UINT32                       OptCount;
-  EFI_DHCP4_PACKET_OPTION      *OptList[HTTP_BOOT_DHCP4_OPTION_MAX_NUM];
-  UINT8                        Buffer[HTTP_BOOT_DHCP4_OPTION_MAX_SIZE];
-  EFI_DHCP4_CONFIG_DATA        Config;
-  EFI_STATUS                   Status;
-  EFI_DHCP4_MODE_DATA          Mode;
-  
-  Dhcp4 = Private->Dhcp4;
-  ASSERT (Dhcp4 != NULL);
-
-  Status = HttpBootSetIpPolicy (Private);
-  if (EFI_ERROR (Status)) {
-    return Status;
-  }
-
-  //
-  // Build option list for the request packet.
-  //
-  OptCount = HttpBootBuildDhcp4Options (Private, OptList, Buffer);
-  ASSERT (OptCount > 0);
-
-  ZeroMem (&Config, sizeof(Config));
-  Config.OptionCount      = OptCount;
-  Config.OptionList       = OptList;
-  Config.Dhcp4Callback    = HttpBootDhcp4CallBack;
-  Config.CallbackContext  = Private;
-  Config.DiscoverTryCount = HTTP_BOOT_DHCP_RETRIES;
-  Config.DiscoverTimeout  = mHttpDhcpTimeout;
-
-  //
-  // Configure the DHCPv4 instance for HTTP boot.
-  //
-  Status = Dhcp4->Configure (Dhcp4, &Config);
-  if (EFI_ERROR (Status)) {
-    goto ON_EXIT;
-  }
-
-  //
-  // Initialize the record fields for DHCPv4 offer in private data.
-  //
-  Private->OfferNum = 0;
-  ZeroMem (Private->OfferCount, sizeof (Private->OfferCount));
-  ZeroMem (Private->OfferIndex, sizeof (Private->OfferIndex));
-
-  //
-  // Start DHCPv4 D.O.R.A. process to acquire IPv4 address.
-  //
-  Status = Dhcp4->Start (Dhcp4, NULL);
-  if (EFI_ERROR (Status)) {
-    goto ON_EXIT;
-  }
-
-  //
-  // Get the acquired IPv4 address and store them.
-  //
-  Status = Dhcp4->GetModeData (Dhcp4, &Mode);
-  if (EFI_ERROR (Status)) {
-    goto ON_EXIT;
-  }
-
-  ASSERT (Mode.State == Dhcp4Bound);
-  CopyMem (&Private->StationIp, &Mode.ClientAddress, sizeof (EFI_IPv4_ADDRESS));
-  CopyMem (&Private->SubnetMask, &Mode.SubnetMask, sizeof (EFI_IPv4_ADDRESS));
-  CopyMem (&Private->GatewayIp, &Mode.RouterAddress, sizeof (EFI_IPv4_ADDRESS));
-
-  Status = HttpBootRegisterIp4Gateway (Private);
-  if (EFI_ERROR (Status)) {
-    goto ON_EXIT;
-  }
-
-  AsciiPrint ("\n  Station IP address is ");
-  HttpBootShowIp4Addr (&Private->StationIp.v4);
-  AsciiPrint ("\n");
-
-ON_EXIT:
-  if (EFI_ERROR (Status)) {
-    Dhcp4->Stop (Dhcp4);
-    Dhcp4->Configure (Dhcp4, NULL);
-  } else {
-    ZeroMem (&Config, sizeof (EFI_DHCP4_CONFIG_DATA));
-    Dhcp4->Configure (Dhcp4, &Config);
-  }
-
-  return Status;
-}
+/** @file\r
+  Functions implementation related with DHCPv4 for HTTP boot driver.\r
+\r
+Copyright (c) 2015 - 2017, Intel Corporation. All rights reserved.<BR>\r
+This program and the accompanying materials are licensed and made available under \r
+the terms and conditions of the BSD License that accompanies this distribution.  \r
+The full text of the license may be found at\r
+http://opensource.org/licenses/bsd-license.php.                                          \r
+    \r
+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
+**/\r
+\r
+#include "HttpBootDxe.h"\r
+\r
+//\r
+// This is a map from the interested DHCP4 option tags' index to the tag value.\r
+//\r
+UINT8 mInterestedDhcp4Tags[HTTP_BOOT_DHCP4_TAG_INDEX_MAX] = {\r
+  DHCP4_TAG_BOOTFILE_LEN,\r
+  DHCP4_TAG_OVERLOAD,\r
+  DHCP4_TAG_MSG_TYPE,\r
+  DHCP4_TAG_SERVER_ID,\r
+  DHCP4_TAG_VENDOR_CLASS_ID,\r
+  DHCP4_TAG_BOOTFILE,\r
+  DHCP4_TAG_DNS_SERVER\r
+};\r
+\r
+//\r
+// There are 4 times retries with the value of 4, 8, 16 and 32, refers to UEFI 2.5 spec.\r
+//\r
+UINT32 mHttpDhcpTimeout[4] = {4, 8, 16, 32};\r
+\r
+/**\r
+  Build the options buffer for the DHCPv4 request packet.\r
+\r
+  @param[in]  Private             Pointer to HTTP boot driver private data.\r
+  @param[out] OptList             Pointer to the option pointer array.\r
+  @param[in]  Buffer              Pointer to the buffer to contain the option list.\r
+\r
+  @return     Index               The count of the built-in options.\r
+\r
+**/\r
+UINT32\r
+HttpBootBuildDhcp4Options (\r
+  IN  HTTP_BOOT_PRIVATE_DATA        *Private,\r
+  OUT EFI_DHCP4_PACKET_OPTION       **OptList,\r
+  IN  UINT8                         *Buffer\r
+  )\r
+{\r
+  HTTP_BOOT_DHCP4_OPTION_ENTRY  OptEnt;\r
+  UINT16                        Value;\r
+  UINT32                        Index;\r
+\r
+  Index      = 0;\r
+  OptList[0] = (EFI_DHCP4_PACKET_OPTION *) Buffer;\r
+\r
+  //\r
+  // Append parameter request list option.\r
+  //\r
+  OptList[Index]->OpCode    = DHCP4_TAG_PARA_LIST;\r
+  OptList[Index]->Length    = 27;\r
+  OptEnt.Para               = (HTTP_BOOT_DHCP4_OPTION_PARA *) OptList[Index]->Data;\r
+  OptEnt.Para->ParaList[0]  = DHCP4_TAG_NETMASK;\r
+  OptEnt.Para->ParaList[1]  = DHCP4_TAG_TIME_OFFSET;\r
+  OptEnt.Para->ParaList[2]  = DHCP4_TAG_ROUTER;\r
+  OptEnt.Para->ParaList[3]  = DHCP4_TAG_TIME_SERVER;\r
+  OptEnt.Para->ParaList[4]  = DHCP4_TAG_NAME_SERVER;\r
+  OptEnt.Para->ParaList[5]  = DHCP4_TAG_DNS_SERVER;\r
+  OptEnt.Para->ParaList[6]  = DHCP4_TAG_HOSTNAME;\r
+  OptEnt.Para->ParaList[7]  = DHCP4_TAG_BOOTFILE_LEN;\r
+  OptEnt.Para->ParaList[8]  = DHCP4_TAG_DOMAINNAME;\r
+  OptEnt.Para->ParaList[9]  = DHCP4_TAG_ROOTPATH;\r
+  OptEnt.Para->ParaList[10] = DHCP4_TAG_EXTEND_PATH;\r
+  OptEnt.Para->ParaList[11] = DHCP4_TAG_EMTU;\r
+  OptEnt.Para->ParaList[12] = DHCP4_TAG_TTL;\r
+  OptEnt.Para->ParaList[13] = DHCP4_TAG_BROADCAST;\r
+  OptEnt.Para->ParaList[14] = DHCP4_TAG_NIS_DOMAIN;\r
+  OptEnt.Para->ParaList[15] = DHCP4_TAG_NIS_SERVER;\r
+  OptEnt.Para->ParaList[16] = DHCP4_TAG_NTP_SERVER;\r
+  OptEnt.Para->ParaList[17] = DHCP4_TAG_VENDOR;\r
+  OptEnt.Para->ParaList[18] = DHCP4_TAG_REQUEST_IP;\r
+  OptEnt.Para->ParaList[19] = DHCP4_TAG_LEASE;\r
+  OptEnt.Para->ParaList[20] = DHCP4_TAG_SERVER_ID;\r
+  OptEnt.Para->ParaList[21] = DHCP4_TAG_T1;\r
+  OptEnt.Para->ParaList[22] = DHCP4_TAG_T2;\r
+  OptEnt.Para->ParaList[23] = DHCP4_TAG_VENDOR_CLASS_ID;\r
+  OptEnt.Para->ParaList[25] = DHCP4_TAG_BOOTFILE;\r
+  OptEnt.Para->ParaList[26] = DHCP4_TAG_UUID;\r
+  Index++;\r
+  OptList[Index]            = GET_NEXT_DHCP_OPTION (OptList[Index - 1]);\r
+\r
+  //\r
+  // Append UUID/Guid-based client identifier option\r
+  //\r
+  OptList[Index]->OpCode  = DHCP4_TAG_UUID;\r
+  OptList[Index]->Length  = (UINT8) sizeof (HTTP_BOOT_DHCP4_OPTION_UUID);\r
+  OptEnt.Uuid             = (HTTP_BOOT_DHCP4_OPTION_UUID *) OptList[Index]->Data;\r
+  OptEnt.Uuid->Type       = 0;\r
+  if (EFI_ERROR (NetLibGetSystemGuid ((EFI_GUID *) OptEnt.Uuid->Guid))) {\r
+    //\r
+    // Zero the Guid to indicate NOT programable if failed to get system Guid.\r
+    //\r
+    ZeroMem (OptEnt.Uuid->Guid, sizeof (EFI_GUID));\r
+  }\r
+  Index++;\r
+  OptList[Index]          = GET_NEXT_DHCP_OPTION (OptList[Index - 1]);\r
+\r
+  //\r
+  // Append client network device interface option\r
+  //\r
+  OptList[Index]->OpCode  = DHCP4_TAG_UNDI;\r
+  OptList[Index]->Length  = (UINT8) sizeof (HTTP_BOOT_DHCP4_OPTION_UNDI);\r
+  OptEnt.Undi             = (HTTP_BOOT_DHCP4_OPTION_UNDI *) OptList[Index]->Data;\r
+\r
+  if (Private->Nii != NULL) {\r
+    OptEnt.Undi->Type     = Private->Nii->Type;\r
+    OptEnt.Undi->MajorVer = Private->Nii->MajorVer;\r
+    OptEnt.Undi->MinorVer = Private->Nii->MinorVer;\r
+  } else {\r
+    OptEnt.Undi->Type     = DEFAULT_UNDI_TYPE;\r
+    OptEnt.Undi->MajorVer = DEFAULT_UNDI_MAJOR;\r
+    OptEnt.Undi->MinorVer = DEFAULT_UNDI_MINOR;\r
+  }\r
+\r
+  Index++;\r
+  OptList[Index] = GET_NEXT_DHCP_OPTION (OptList[Index - 1]);\r
+\r
+  //\r
+  // Append client system architecture option\r
+  //\r
+  OptList[Index]->OpCode  = DHCP4_TAG_ARCH;\r
+  OptList[Index]->Length  = (UINT8) sizeof (HTTP_BOOT_DHCP4_OPTION_ARCH);\r
+  OptEnt.Arch             = (HTTP_BOOT_DHCP4_OPTION_ARCH *) OptList[Index]->Data;\r
+  Value                   = HTONS (EFI_HTTP_BOOT_CLIENT_SYSTEM_ARCHITECTURE);\r
+  CopyMem (&OptEnt.Arch->Type, &Value, sizeof (UINT16));\r
+  Index++;\r
+  OptList[Index]          = GET_NEXT_DHCP_OPTION (OptList[Index - 1]);\r
+\r
+  //\r
+  // Append vendor class identify option\r
+  //\r
+  OptList[Index]->OpCode  = DHCP4_TAG_VENDOR_CLASS_ID;\r
+  OptList[Index]->Length  = (UINT8) sizeof (HTTP_BOOT_DHCP4_OPTION_CLID);\r
+  OptEnt.Clid             = (HTTP_BOOT_DHCP4_OPTION_CLID *) OptList[Index]->Data;\r
+  CopyMem (\r
+    OptEnt.Clid,\r
+    DEFAULT_CLASS_ID_DATA,\r
+    sizeof (HTTP_BOOT_DHCP4_OPTION_CLID)\r
+    );\r
+  HttpBootUintnToAscDecWithFormat (\r
+    EFI_HTTP_BOOT_CLIENT_SYSTEM_ARCHITECTURE,\r
+    OptEnt.Clid->ArchitectureType,\r
+    sizeof (OptEnt.Clid->ArchitectureType)\r
+    );\r
+\r
+  if (Private->Nii != NULL) {\r
+    CopyMem (OptEnt.Clid->InterfaceName, Private->Nii->StringId, sizeof (OptEnt.Clid->InterfaceName));\r
+    HttpBootUintnToAscDecWithFormat (Private->Nii->MajorVer, OptEnt.Clid->UndiMajor, sizeof (OptEnt.Clid->UndiMajor));\r
+    HttpBootUintnToAscDecWithFormat (Private->Nii->MinorVer, OptEnt.Clid->UndiMinor, sizeof (OptEnt.Clid->UndiMinor));\r
+  }\r
+\r
+  Index++;\r
+\r
+  return Index;\r
+}\r
+\r
+/**\r
+  Parse a certain dhcp4 option by OptTag in Buffer, and return with start pointer.\r
+\r
+  @param[in]  Buffer              Pointer to the option buffer.\r
+  @param[in]  Length              Length of the option buffer.\r
+  @param[in]  OptTag              Tag of the required option.\r
+\r
+  @retval     NULL                Failed to find the required option.\r
+  @retval     Others              The position of the required option.\r
+\r
+**/\r
+EFI_DHCP4_PACKET_OPTION *\r
+HttpBootParseDhcp4Options (\r
+  IN UINT8                      *Buffer,\r
+  IN UINT32                     Length,\r
+  IN UINT8                      OptTag\r
+  )\r
+{\r
+  EFI_DHCP4_PACKET_OPTION       *Option;\r
+  UINT32                        Offset;\r
+\r
+  Option  = (EFI_DHCP4_PACKET_OPTION *) Buffer;\r
+  Offset  = 0;\r
+\r
+  while (Offset < Length && Option->OpCode != DHCP4_TAG_EOP) {\r
+\r
+    if (Option->OpCode == OptTag) {\r
+      //\r
+      // Found the required option.\r
+      //\r
+      return Option;\r
+    }\r
+\r
+    //\r
+    // Skip the current option to the next.\r
+    //\r
+    if (Option->OpCode == DHCP4_TAG_PAD) {\r
+      Offset++;\r
+    } else {\r
+      Offset += Option->Length + 2;\r
+    }\r
+\r
+    Option = (EFI_DHCP4_PACKET_OPTION *) (Buffer + Offset);\r
+  }\r
+\r
+  return NULL;\r
+}\r
+\r
+/**\r
+  Cache the DHCPv4 packet.\r
+\r
+  @param[in]  Dst          Pointer to the cache buffer for DHCPv4 packet.\r
+  @param[in]  Src          Pointer to the DHCPv4 packet to be cached.\r
+\r
+  @retval     EFI_SUCCESS                Packet is copied.\r
+  @retval     EFI_BUFFER_TOO_SMALL       Cache buffer is not big enough to hold the packet.\r
+\r
+**/\r
+EFI_STATUS\r
+HttpBootCacheDhcp4Packet (\r
+  IN EFI_DHCP4_PACKET     *Dst,\r
+  IN EFI_DHCP4_PACKET     *Src\r
+  )\r
+{\r
+  if (Dst->Size < Src->Length) {\r
+    return EFI_BUFFER_TOO_SMALL;\r
+  }\r
+\r
+  CopyMem (&Dst->Dhcp4, &Src->Dhcp4, Src->Length);\r
+  Dst->Length = Src->Length;\r
+\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+/**\r
+  Parse the cached DHCPv4 packet, including all the options.\r
+\r
+  @param[in]  Cache4           Pointer to cached DHCPv4 packet.\r
+\r
+  @retval     EFI_SUCCESS      Parsed the DHCPv4 packet successfully.\r
+  @retval     EFI_DEVICE_ERROR Failed to parse an invalid packet.\r
+\r
+**/\r
+EFI_STATUS\r
+HttpBootParseDhcp4Packet (\r
+  IN HTTP_BOOT_DHCP4_PACKET_CACHE    *Cache4\r
+  )\r
+{\r
+  EFI_DHCP4_PACKET               *Offer;\r
+  EFI_DHCP4_PACKET_OPTION        **Options;\r
+  UINTN                          Index;\r
+  EFI_DHCP4_PACKET_OPTION        *Option;\r
+  BOOLEAN                        IsProxyOffer;\r
+  BOOLEAN                        IsHttpOffer;\r
+  BOOLEAN                        IsDnsOffer;\r
+  BOOLEAN                        IpExpressedUri;\r
+  UINT8                          *Ptr8;\r
+  EFI_STATUS                     Status;\r
+  HTTP_BOOT_OFFER_TYPE           OfferType;\r
+  EFI_IPv4_ADDRESS               IpAddr;\r
+  BOOLEAN                        FileFieldOverloaded;\r
+  \r
+  IsDnsOffer     = FALSE;\r
+  IpExpressedUri = FALSE;\r
+  IsProxyOffer   = FALSE;\r
+  IsHttpOffer    = FALSE;\r
+  FileFieldOverloaded = FALSE;\r
+\r
+  ZeroMem (Cache4->OptList, sizeof (Cache4->OptList));\r
+\r
+  Offer   = &Cache4->Packet.Offer;\r
+  Options = Cache4->OptList;\r
+\r
+  //\r
+  // Parse DHCPv4 options in this offer, and store the pointers.\r
+  // First, try to parse DHCPv4 options from the DHCP optional parameters field.\r
+  //\r
+  for (Index = 0; Index < HTTP_BOOT_DHCP4_TAG_INDEX_MAX; Index++) {\r
+    Options[Index] = HttpBootParseDhcp4Options (\r
+                       Offer->Dhcp4.Option,\r
+                       GET_OPTION_BUFFER_LEN (Offer),\r
+                       mInterestedDhcp4Tags[Index]\r
+                       );\r
+  }\r
+  //\r
+  // Second, Check if bootfilename and serverhostname is overloaded to carry DHCP options refers to rfc-2132. \r
+  // If yes, try to parse options from the BootFileName field, then ServerName field.\r
+  //\r
+  Option = Options[HTTP_BOOT_DHCP4_TAG_INDEX_OVERLOAD];\r
+  if (Option != NULL) {\r
+    if ((Option->Data[0] & HTTP_BOOT_DHCP4_OVERLOAD_FILE) != 0) {\r
+      FileFieldOverloaded = TRUE;\r
+      for (Index = 0; Index < HTTP_BOOT_DHCP4_TAG_INDEX_MAX; Index++) {\r
+        if (Options[Index] == NULL) {\r
+          Options[Index] = HttpBootParseDhcp4Options (\r
+                             (UINT8 *) Offer->Dhcp4.Header.BootFileName,\r
+                             sizeof (Offer->Dhcp4.Header.BootFileName),\r
+                             mInterestedDhcp4Tags[Index]\r
+                             );\r
+        }\r
+      }\r
+    }\r
+    if ((Option->Data[0] & HTTP_BOOT_DHCP4_OVERLOAD_SERVER_NAME) != 0) {\r
+      for (Index = 0; Index < HTTP_BOOT_DHCP4_TAG_INDEX_MAX; Index++) {\r
+        if (Options[Index] == NULL) {\r
+          Options[Index] = HttpBootParseDhcp4Options (\r
+                             (UINT8 *) Offer->Dhcp4.Header.ServerName,\r
+                             sizeof (Offer->Dhcp4.Header.ServerName),\r
+                             mInterestedDhcp4Tags[Index]\r
+                             );\r
+        }\r
+      }\r
+    }\r
+  }\r
+\r
+  //\r
+  // The offer with "yiaddr" is a proxy offer.\r
+  //\r
+  if (Offer->Dhcp4.Header.YourAddr.Addr[0] == 0) {\r
+    IsProxyOffer = TRUE;\r
+  }\r
+\r
+  //\r
+  // The offer with "HTTPClient" is a Http offer.\r
+  //\r
+  Option = Options[HTTP_BOOT_DHCP4_TAG_INDEX_CLASS_ID];\r
+  if ((Option != NULL) && (Option->Length >= 10) &&\r
+      (CompareMem (Option->Data, DEFAULT_CLASS_ID_DATA, 10) == 0)) {\r
+    IsHttpOffer = TRUE;\r
+  }\r
+\r
+  //\r
+  // The offer with Domain Server is a DNS offer.\r
+  //\r
+  Option = Options[HTTP_BOOT_DHCP4_TAG_INDEX_DNS_SERVER];\r
+  if (Option != NULL) {\r
+    IsDnsOffer = TRUE;\r
+  }\r
+\r
+  //\r
+  // Parse boot file name:\r
+  // Boot URI information is provided thru 'file' field in DHCP Header or option 67.\r
+  // According to RFC 2132, boot file name should be read from DHCP option 67 (bootfile name) if present.\r
+  // Otherwise, read from boot file field in DHCP header.\r
+  //\r
+  if (Options[HTTP_BOOT_DHCP4_TAG_INDEX_BOOTFILE] != NULL) {\r
+    //\r
+    // RFC 2132, Section 9.5 does not strictly state Bootfile name (option 67) is null\r
+    // terminated string. So force to append null terminated character at the end of string.\r
+    //\r
+    Ptr8 =  (UINT8*)&Options[HTTP_BOOT_DHCP4_TAG_INDEX_BOOTFILE]->Data[0];\r
+    Ptr8 += Options[HTTP_BOOT_DHCP4_TAG_INDEX_BOOTFILE]->Length;\r
+    if (*(Ptr8 - 1) != '\0') {\r
+      *Ptr8 = '\0';\r
+    }\r
+  } else if (!FileFieldOverloaded && Offer->Dhcp4.Header.BootFileName[0] != 0) {\r
+    //\r
+    // If the bootfile is not present and bootfilename is present in DHCPv4 packet, just parse it.\r
+    // Do not count dhcp option header here, or else will destroy the serverhostname.\r
+    //\r
+    Options[HTTP_BOOT_DHCP4_TAG_INDEX_BOOTFILE] = (EFI_DHCP4_PACKET_OPTION *)\r
+                                                    (&Offer->Dhcp4.Header.BootFileName[0] -\r
+                                                    OFFSET_OF (EFI_DHCP4_PACKET_OPTION, Data[0]));\r
+  }\r
+\r
+  //\r
+  // Http offer must have a boot URI.\r
+  //\r
+  if (IsHttpOffer && Options[HTTP_BOOT_DHCP4_TAG_INDEX_BOOTFILE] == NULL) {\r
+    return EFI_DEVICE_ERROR;\r
+  }\r
+\r
+  //\r
+  // Try to retrieve the IP of HTTP server from URI. \r
+  //\r
+  if (IsHttpOffer) {\r
+    Status = HttpParseUrl (\r
+               (CHAR8*) Options[HTTP_BOOT_DHCP4_TAG_INDEX_BOOTFILE]->Data,\r
+               (UINT32) AsciiStrLen ((CHAR8*) Options[HTTP_BOOT_DHCP4_TAG_INDEX_BOOTFILE]->Data),\r
+               FALSE,\r
+               &Cache4->UriParser\r
+               );\r
+    if (EFI_ERROR (Status)) {\r
+      return EFI_DEVICE_ERROR;\r
+    }\r
+\r
+    Status = HttpUrlGetIp4 (\r
+               (CHAR8*) Options[HTTP_BOOT_DHCP4_TAG_INDEX_BOOTFILE]->Data,\r
+               Cache4->UriParser,\r
+               &IpAddr\r
+               );\r
+    IpExpressedUri = !EFI_ERROR (Status);\r
+  }\r
+\r
+  //\r
+  // Determine offer type of the DHCPv4 packet.\r
+  //\r
+  if (IsHttpOffer) {\r
+    if (IpExpressedUri) {\r
+      if (IsProxyOffer) {\r
+        OfferType = HttpOfferTypeProxyIpUri;\r
+      } else {\r
+        OfferType = IsDnsOffer ? HttpOfferTypeDhcpIpUriDns : HttpOfferTypeDhcpIpUri;\r
+      }\r
+    } else {\r
+      if (!IsProxyOffer) {\r
+        OfferType = IsDnsOffer ? HttpOfferTypeDhcpNameUriDns : HttpOfferTypeDhcpNameUri;\r
+      } else {\r
+        OfferType = HttpOfferTypeProxyNameUri;\r
+      }\r
+    }\r
+\r
+  } else {\r
+    if (!IsProxyOffer) {\r
+      OfferType = IsDnsOffer ? HttpOfferTypeDhcpDns : HttpOfferTypeDhcpOnly;\r
+    } else {\r
+      if (Cache4->UriParser != NULL) {\r
+        FreePool (Cache4->UriParser);\r
+      }\r
+      return EFI_DEVICE_ERROR;\r
+    }\r
+  }\r
+  \r
+  Cache4->OfferType = OfferType;\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+/**\r
+  Cache all the received DHCPv4 offers, and set OfferIndex and OfferCount.\r
+\r
+  @param[in]  Private               Pointer to HTTP boot driver private data.\r
+  @param[in]  RcvdOffer             Pointer to the received offer packet.\r
+\r
+  @retval     EFI_SUCCESS      Cache and parse the packet successfully.\r
+  @retval     Others           Operation failed.\r
+**/\r
+EFI_STATUS\r
+HttpBootCacheDhcp4Offer (\r
+  IN HTTP_BOOT_PRIVATE_DATA  *Private,\r
+  IN EFI_DHCP4_PACKET        *RcvdOffer\r
+  )\r
+{\r
+  HTTP_BOOT_DHCP4_PACKET_CACHE  *Cache4;\r
+  EFI_DHCP4_PACKET              *Offer;\r
+  HTTP_BOOT_OFFER_TYPE          OfferType;\r
+  EFI_STATUS                    Status;\r
+\r
+  ASSERT (Private->OfferNum < HTTP_BOOT_OFFER_MAX_NUM);\r
+  Cache4 = &Private->OfferBuffer[Private->OfferNum].Dhcp4;\r
+  Offer  = &Cache4->Packet.Offer;\r
+\r
+  //\r
+  // Cache the content of DHCPv4 packet firstly.\r
+  //\r
+  Status = HttpBootCacheDhcp4Packet (Offer, RcvdOffer);\r
+  if (EFI_ERROR (Status)) {\r
+    return Status;\r
+  }\r
+\r
+  //\r
+  // Validate the DHCPv4 packet, and parse the options and offer type.\r
+  //\r
+  if (EFI_ERROR (HttpBootParseDhcp4Packet (Cache4))) {\r
+    return EFI_ABORTED;\r
+  }\r
+\r
+  //\r
+  // Determine whether cache the current offer by type, and record OfferIndex and OfferCount.\r
+  //\r
+  OfferType = Cache4->OfferType;\r
+  ASSERT (OfferType < HttpOfferTypeMax);\r
+  ASSERT (Private->OfferCount[OfferType] < HTTP_BOOT_OFFER_MAX_NUM);\r
+  Private->OfferIndex[OfferType][Private->OfferCount[OfferType]] = Private->OfferNum;\r
+  Private->OfferCount[OfferType]++;\r
+  Private->OfferNum++;\r
+\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+/**\r
+  Select an DHCPv4 or DHCP6 offer, and record SelectIndex and SelectProxyType.\r
+\r
+  @param[in]  Private             Pointer to HTTP boot driver private data.\r
+\r
+**/\r
+VOID\r
+HttpBootSelectDhcpOffer (\r
+  IN HTTP_BOOT_PRIVATE_DATA  *Private\r
+  )\r
+{\r
+  Private->SelectIndex = 0;\r
+  Private->SelectProxyType = HttpOfferTypeMax;\r
+\r
+  if (Private->FilePathUri != NULL) {\r
+    //\r
+    // We are in home environment, the URI is already specified.\r
+    // Just need to choose a DHCP offer.\r
+    // The offer with DNS server address takes priority here.\r
+    //\r
+    if (Private->OfferCount[HttpOfferTypeDhcpDns] > 0) {\r
+      \r
+      Private->SelectIndex = Private->OfferIndex[HttpOfferTypeDhcpDns][0] + 1;\r
+      \r
+    } else if (Private->OfferCount[HttpOfferTypeDhcpIpUriDns] > 0) {\r
+    \r
+      Private->SelectIndex = Private->OfferIndex[HttpOfferTypeDhcpIpUriDns][0] + 1;\r
+      \r
+    } else if (Private->OfferCount[HttpOfferTypeDhcpNameUriDns] > 0) {\r
+    \r
+      Private->SelectIndex = Private->OfferIndex[HttpOfferTypeDhcpNameUriDns][0] + 1;\r
+      \r
+    }  else if (Private->OfferCount[HttpOfferTypeDhcpOnly] > 0) {\r
+    \r
+      Private->SelectIndex = Private->OfferIndex[HttpOfferTypeDhcpOnly][0] + 1;\r
+      \r
+    }  else if (Private->OfferCount[HttpOfferTypeDhcpIpUri] > 0) {\r
+    \r
+      Private->SelectIndex = Private->OfferIndex[HttpOfferTypeDhcpIpUri][0] + 1;\r
+    }\r
+    \r
+  } else {\r
+    //\r
+    // We are in corporate environment.\r
+    //\r
+    // Priority1: HttpOfferTypeDhcpIpUri or HttpOfferTypeDhcpIpUriDns\r
+    // Priority2: HttpOfferTypeDhcpNameUriDns                      \r
+    // Priority3: HttpOfferTypeDhcpOnly + HttpOfferTypeProxyIpUri  \r
+    // Priority4: HttpOfferTypeDhcpDns  + HttpOfferTypeProxyIpUri  \r
+    // Priority5: HttpOfferTypeDhcpDns  + HttpOfferTypeProxyNameUri\r
+    // Priority6: HttpOfferTypeDhcpDns  + HttpOfferTypeDhcpNameUri \r
+    //    \r
+    if (Private->OfferCount[HttpOfferTypeDhcpIpUri] > 0) {\r
+      \r
+      Private->SelectIndex = Private->OfferIndex[HttpOfferTypeDhcpIpUri][0] + 1;\r
+      \r
+    } else if (Private->OfferCount[HttpOfferTypeDhcpIpUriDns] > 0) {\r
+      \r
+      Private->SelectIndex = Private->OfferIndex[HttpOfferTypeDhcpIpUriDns][0] + 1;\r
+      \r
+    }else if (Private->OfferCount[HttpOfferTypeDhcpNameUriDns] > 0) {\r
+    \r
+      Private->SelectIndex = Private->OfferIndex[HttpOfferTypeDhcpNameUriDns][0] + 1;\r
+      \r
+    } else if (Private->OfferCount[HttpOfferTypeDhcpOnly] > 0 &&\r
+               Private->OfferCount[HttpOfferTypeProxyIpUri] > 0) {\r
+               \r
+      Private->SelectIndex     = Private->OfferIndex[HttpOfferTypeDhcpOnly][0] + 1;\r
+      Private->SelectProxyType = HttpOfferTypeProxyIpUri;\r
+      \r
+    } else if (Private->OfferCount[HttpOfferTypeDhcpDns] > 0 &&\r
+               Private->OfferCount[HttpOfferTypeProxyIpUri] > 0) {\r
+               \r
+      Private->SelectIndex     = Private->OfferIndex[HttpOfferTypeDhcpDns][0] + 1;\r
+      Private->SelectProxyType = HttpOfferTypeProxyIpUri;\r
+      \r
+    } else if (Private->OfferCount[HttpOfferTypeDhcpDns] > 0 &&\r
+               Private->OfferCount[HttpOfferTypeProxyNameUri] > 0) {\r
+               \r
+      Private->SelectIndex     = Private->OfferIndex[HttpOfferTypeDhcpDns][0] + 1;\r
+      Private->SelectProxyType = HttpOfferTypeProxyNameUri;\r
+      \r
+    } else if (Private->OfferCount[HttpOfferTypeDhcpDns] > 0 &&\r
+               Private->OfferCount[HttpOfferTypeDhcpNameUri] > 0) {\r
+               \r
+      Private->SelectIndex     = Private->OfferIndex[HttpOfferTypeDhcpDns][0] + 1;\r
+      Private->SelectProxyType = HttpOfferTypeDhcpNameUri;\r
+    }\r
+  }\r
+}\r
+\r
+\r
+/**\r
+  EFI_DHCP4_CALLBACK is provided by the consumer of the EFI DHCPv4 Protocol driver\r
+  to intercept events that occurred in the configuration process.\r
+\r
+  @param[in]  This              Pointer to the EFI DHCPv4 Protocol.\r
+  @param[in]  Context           Pointer to the context set by EFI_DHCP4_PROTOCOL.Configure().\r
+  @param[in]  CurrentState      The current operational state of the EFI DHCPv4 Protocol driver.\r
+  @param[in]  Dhcp4Event        The event that occurs in the current state, which usually means a\r
+                                state transition.\r
+  @param[in]  Packet            The DHCPv4 packet that is going to be sent or already received.\r
+  @param[out] NewPacket         The packet that is used to replace the above Packet.\r
+\r
+  @retval EFI_SUCCESS           Tells the EFI DHCPv4 Protocol driver to continue the DHCP process.\r
+  @retval EFI_NOT_READY         Only used in the Dhcp4Selecting state. The EFI DHCPv4 Protocol\r
+                                driver will continue to wait for more DHCPOFFER packets until the\r
+                                retry timeout expires.\r
+  @retval EFI_ABORTED           Tells the EFI DHCPv4 Protocol driver to abort the current process\r
+                                and return to the Dhcp4Init or Dhcp4InitReboot state.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+HttpBootDhcp4CallBack (\r
+  IN  EFI_DHCP4_PROTOCOL               *This,\r
+  IN  VOID                             *Context,\r
+  IN  EFI_DHCP4_STATE                  CurrentState,\r
+  IN  EFI_DHCP4_EVENT                  Dhcp4Event,\r
+  IN  EFI_DHCP4_PACKET                 *Packet            OPTIONAL,\r
+  OUT EFI_DHCP4_PACKET                 **NewPacket        OPTIONAL\r
+  )\r
+{\r
+  HTTP_BOOT_PRIVATE_DATA               *Private;\r
+  EFI_DHCP4_PACKET_OPTION              *MaxMsgSize;\r
+  UINT16                               Value;\r
+  EFI_STATUS                           Status;\r
+  BOOLEAN                              Received;\r
+\r
+  if ((Dhcp4Event != Dhcp4SendDiscover) && \r
+      (Dhcp4Event != Dhcp4RcvdOffer) && \r
+      (Dhcp4Event != Dhcp4SendRequest) && \r
+      (Dhcp4Event != Dhcp4RcvdAck) && \r
+      (Dhcp4Event != Dhcp4SelectOffer)) {\r
+    return EFI_SUCCESS;\r
+  }\r
+  \r
+  Private = (HTTP_BOOT_PRIVATE_DATA *) Context;\r
+\r
+  //\r
+  // Override the Maximum DHCP Message Size.\r
+  //\r
+  MaxMsgSize = HttpBootParseDhcp4Options (\r
+                 Packet->Dhcp4.Option,\r
+                 GET_OPTION_BUFFER_LEN (Packet),\r
+                 DHCP4_TAG_MAXMSG\r
+                 );\r
+  if (MaxMsgSize != NULL) {\r
+    Value = HTONS (HTTP_BOOT_DHCP4_PACKET_MAX_SIZE);\r
+    CopyMem (MaxMsgSize->Data, &Value, sizeof (Value));\r
+  }\r
+  \r
+  //\r
+  // Callback to user if any packets sent or received.\r
+  //\r
+  if (Private->HttpBootCallback != NULL && Dhcp4Event != Dhcp4SelectOffer) {\r
+    Received = (BOOLEAN) (Dhcp4Event == Dhcp4RcvdOffer || Dhcp4Event == Dhcp4RcvdAck);\r
+    Status = Private->HttpBootCallback->Callback (\r
+               Private->HttpBootCallback, \r
+               HttpBootDhcp4,\r
+               Received,\r
+               Packet->Length,\r
+               &Packet->Dhcp4\r
+               );\r
+    if (EFI_ERROR (Status)) {\r
+      return EFI_ABORTED;\r
+    }\r
+  }\r
+\r
+  Status = EFI_SUCCESS;\r
+  switch (Dhcp4Event) {\r
+  case Dhcp4RcvdOffer:\r
+    Status = EFI_NOT_READY;\r
+    if (Packet->Length > HTTP_BOOT_DHCP4_PACKET_MAX_SIZE) {\r
+      //\r
+      // Ignore the incoming packets which exceed the maximum length.\r
+      //\r
+      break;\r
+    }\r
+    if (Private->OfferNum < HTTP_BOOT_OFFER_MAX_NUM) {\r
+      //\r
+      // Cache the DHCPv4 offers to OfferBuffer[] for select later, and record\r
+      // the OfferIndex and OfferCount.\r
+      // If error happens, just ignore this packet and continue to wait more offer.\r
+      //\r
+      HttpBootCacheDhcp4Offer (Private, Packet);\r
+    }\r
+    break;\r
+\r
+  case Dhcp4SelectOffer:\r
+    //\r
+    // Select offer according to the priority in UEFI spec, and record the SelectIndex \r
+    // and SelectProxyType.\r
+    //\r
+    HttpBootSelectDhcpOffer (Private);\r
+\r
+    if (Private->SelectIndex == 0) {\r
+      Status = EFI_ABORTED;\r
+    } else {\r
+      *NewPacket = &Private->OfferBuffer[Private->SelectIndex - 1].Dhcp4.Packet.Offer;\r
+    }\r
+    break;\r
+    \r
+  default:\r
+    break;\r
+  }\r
+\r
+  return Status;\r
+}\r
+\r
+/**\r
+  This function will register the IPv4 gateway address to the network device.\r
+  \r
+  @param[in]  Private             The pointer to HTTP_BOOT_PRIVATE_DATA.\r
+\r
+  @retval     EFI_SUCCESS         The new IP configuration has been configured successfully.\r
+  @retval     Others              Failed to configure the address.\r
+\r
+**/\r
+EFI_STATUS\r
+HttpBootRegisterIp4Gateway (\r
+  IN HTTP_BOOT_PRIVATE_DATA         *Private\r
+  )\r
+{\r
+  EFI_STATUS                      Status;\r
+  EFI_IP4_CONFIG2_PROTOCOL        *Ip4Config2;\r
+\r
+  ASSERT (!Private->UsingIpv6);\r
+\r
+  Ip4Config2 = Private->Ip4Config2;\r
+\r
+  //\r
+  // Configure the gateway if valid.\r
+  //\r
+  if (!EFI_IP4_EQUAL (&Private->GatewayIp, &mZeroIp4Addr)) {\r
+    Status = Ip4Config2->SetData (\r
+                           Ip4Config2,\r
+                           Ip4Config2DataTypeGateway,\r
+                           sizeof (EFI_IPv4_ADDRESS),\r
+                           &Private->GatewayIp\r
+                           );\r
+    if (EFI_ERROR (Status)) {\r
+      return Status;\r
+    }\r
+  }\r
+\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+/**\r
+  This function will register the default DNS addresses to the network device.\r
+  \r
+  @param[in]  Private             The pointer to HTTP_BOOT_PRIVATE_DATA.\r
+  @param[in]  DataLength          Size of the buffer pointed to by DnsServerData in bytes.\r
+  @param[in]  DnsServerData       Point a list of DNS server address in an array\r
+                                  of EFI_IPv4_ADDRESS instances.\r
+\r
+  @retval     EFI_SUCCESS         The DNS configuration has been configured successfully.\r
+  @retval     Others              Failed to configure the address.\r
+\r
+**/\r
+EFI_STATUS\r
+HttpBootRegisterIp4Dns (\r
+  IN HTTP_BOOT_PRIVATE_DATA         *Private,\r
+  IN UINTN                          DataLength,\r
+  IN VOID                           *DnsServerData\r
+  )\r
+{\r
+  EFI_IP4_CONFIG2_PROTOCOL        *Ip4Config2;\r
+  \r
+  ASSERT (!Private->UsingIpv6);\r
+\r
+  Ip4Config2 = Private->Ip4Config2;\r
+  \r
+  return Ip4Config2->SetData (\r
+                       Ip4Config2,\r
+                       Ip4Config2DataTypeDnsServer,\r
+                       DataLength,\r
+                       DnsServerData\r
+                       );\r
+}\r
+\r
+\r
+/**\r
+  This function will switch the IP4 configuration policy to Static.\r
+\r
+  @param[in]  Private             Pointer to HTTP boot driver private data.\r
+\r
+  @retval     EFI_SUCCESS         The policy is already configured to static.\r
+  @retval     Others              Other error as indicated..\r
+\r
+**/\r
+EFI_STATUS\r
+HttpBootSetIp4Policy (\r
+  IN HTTP_BOOT_PRIVATE_DATA         *Private\r
+  )\r
+{\r
+  EFI_IP4_CONFIG2_POLICY          Policy;\r
+  EFI_STATUS                      Status;\r
+  EFI_IP4_CONFIG2_PROTOCOL        *Ip4Config2;\r
+  UINTN                           DataSize;\r
+\r
+  Ip4Config2 = Private->Ip4Config2;\r
+\r
+  DataSize = sizeof (EFI_IP4_CONFIG2_POLICY);\r
+  Status = Ip4Config2->GetData (\r
+                         Ip4Config2,\r
+                         Ip4Config2DataTypePolicy,\r
+                         &DataSize,\r
+                         &Policy\r
+                         );\r
+  if (EFI_ERROR (Status)) {\r
+    return Status;\r
+  }\r
+\r
+  if (Policy != Ip4Config2PolicyStatic) {\r
+    Policy = Ip4Config2PolicyStatic;\r
+    Status= Ip4Config2->SetData (\r
+                          Ip4Config2,\r
+                          Ip4Config2DataTypePolicy,\r
+                          sizeof (EFI_IP4_CONFIG2_POLICY),\r
+                          &Policy\r
+                          );\r
+    if (EFI_ERROR (Status)) {\r
+      return Status;\r
+    } \r
+  }\r
+\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+/**\r
+  Start the D.O.R.A DHCPv4 process to acquire the IPv4 address and other Http boot information.\r
+\r
+  @param[in]  Private           Pointer to HTTP boot driver private data.\r
+\r
+  @retval EFI_SUCCESS           The D.O.R.A process successfully finished.\r
+  @retval Others                Failed to finish the D.O.R.A process.\r
+\r
+**/\r
+EFI_STATUS\r
+HttpBootDhcp4Dora (\r
+  IN HTTP_BOOT_PRIVATE_DATA         *Private\r
+  )\r
+{\r
+  EFI_DHCP4_PROTOCOL           *Dhcp4;\r
+  UINT32                       OptCount;\r
+  EFI_DHCP4_PACKET_OPTION      *OptList[HTTP_BOOT_DHCP4_OPTION_MAX_NUM];\r
+  UINT8                        Buffer[HTTP_BOOT_DHCP4_OPTION_MAX_SIZE];\r
+  EFI_DHCP4_CONFIG_DATA        Config;\r
+  EFI_STATUS                   Status;\r
+  EFI_DHCP4_MODE_DATA          Mode;\r
+  \r
+  Dhcp4 = Private->Dhcp4;\r
+  ASSERT (Dhcp4 != NULL);\r
+\r
+  Status = HttpBootSetIp4Policy (Private);\r
+  if (EFI_ERROR (Status)) {\r
+    return Status;\r
+  }\r
+\r
+  //\r
+  // Build option list for the request packet.\r
+  //\r
+  OptCount = HttpBootBuildDhcp4Options (Private, OptList, Buffer);\r
+  ASSERT (OptCount > 0);\r
+\r
+  ZeroMem (&Config, sizeof(Config));\r
+  Config.OptionCount      = OptCount;\r
+  Config.OptionList       = OptList;\r
+  Config.Dhcp4Callback    = HttpBootDhcp4CallBack;\r
+  Config.CallbackContext  = Private;\r
+  Config.DiscoverTryCount = HTTP_BOOT_DHCP_RETRIES;\r
+  Config.DiscoverTimeout  = mHttpDhcpTimeout;\r
+\r
+  //\r
+  // Configure the DHCPv4 instance for HTTP boot.\r
+  //\r
+  Status = Dhcp4->Configure (Dhcp4, &Config);\r
+  if (EFI_ERROR (Status)) {\r
+    goto ON_EXIT;\r
+  }\r
+\r
+  //\r
+  // Initialize the record fields for DHCPv4 offer in private data.\r
+  //\r
+  Private->OfferNum = 0;\r
+  ZeroMem (Private->OfferCount, sizeof (Private->OfferCount));\r
+  ZeroMem (Private->OfferIndex, sizeof (Private->OfferIndex));\r
+\r
+  //\r
+  // Start DHCPv4 D.O.R.A. process to acquire IPv4 address.\r
+  //\r
+  Status = Dhcp4->Start (Dhcp4, NULL);\r
+  if (EFI_ERROR (Status)) {\r
+    goto ON_EXIT;\r
+  }\r
+\r
+  //\r
+  // Get the acquired IPv4 address and store them.\r
+  //\r
+  Status = Dhcp4->GetModeData (Dhcp4, &Mode);\r
+  if (EFI_ERROR (Status)) {\r
+    goto ON_EXIT;\r
+  }\r
+\r
+  ASSERT (Mode.State == Dhcp4Bound);\r
+  CopyMem (&Private->StationIp, &Mode.ClientAddress, sizeof (EFI_IPv4_ADDRESS));\r
+  CopyMem (&Private->SubnetMask, &Mode.SubnetMask, sizeof (EFI_IPv4_ADDRESS));\r
+  CopyMem (&Private->GatewayIp, &Mode.RouterAddress, sizeof (EFI_IPv4_ADDRESS));\r
+\r
+  Status = HttpBootRegisterIp4Gateway (Private);\r
+  if (EFI_ERROR (Status)) {\r
+    goto ON_EXIT;\r
+  }\r
+\r
+  AsciiPrint ("\n  Station IP address is ");\r
+  HttpBootShowIp4Addr (&Private->StationIp.v4);\r
+  AsciiPrint ("\n");\r
+\r
+ON_EXIT:\r
+  if (EFI_ERROR (Status)) {\r
+    Dhcp4->Stop (Dhcp4);\r
+    Dhcp4->Configure (Dhcp4, NULL);\r
+  } else {\r
+    ZeroMem (&Config, sizeof (EFI_DHCP4_CONFIG_DATA));\r
+    Dhcp4->Configure (Dhcp4, &Config);\r
+  }\r
+\r
+  return Status;\r
+}\r