]> git.proxmox.com Git - mirror_edk2.git/blobdiff - NetworkPkg/HttpBootDxe/HttpBootClient.c
BaseTools/Capsule: Do not support -o with --dump-info
[mirror_edk2.git] / NetworkPkg / HttpBootDxe / HttpBootClient.c
index b81b03c96070400db493e148f53ebfcb1e4cb2a4..90a6acb0d4ed4203e154c04289e1e9635c0d0fe8 100644 (file)
@@ -1,13 +1,14 @@
 /** @file\r
   Implementation of the boot file download function.\r
 \r
-Copyright (c) 2015, 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
+Copyright (c) 2015 - 2018, Intel Corporation. All rights reserved.<BR>\r
+(C) Copyright 2016 Hewlett Packard Enterprise Development LP<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
+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
@@ -15,14 +16,14 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
 #include "HttpBootDxe.h"\r
 \r
 /**\r
-  Update the IP and URL device path node to include the boot resource information.\r
+  Update the device path node to include the boot resource information.\r
 \r
   @param[in]    Private            The pointer to the driver's private data.\r
 \r
   @retval EFI_SUCCESS              Device patch successfully updated.\r
   @retval EFI_OUT_OF_RESOURCES     Could not allocate needed resources.\r
   @retval Others                   Unexpected error happened.\r
-  \r
+\r
 **/\r
 EFI_STATUS\r
 HttpBootUpdateDevicePath (\r
@@ -30,13 +31,15 @@ HttpBootUpdateDevicePath (
   )\r
 {\r
   EFI_DEV_PATH               *Node;\r
-  EFI_DEVICE_PATH_PROTOCOL   *TmpDevicePath;\r
+  EFI_DEVICE_PATH_PROTOCOL   *TmpIpDevicePath;\r
+  EFI_DEVICE_PATH_PROTOCOL   *TmpDnsDevicePath;\r
   EFI_DEVICE_PATH_PROTOCOL   *NewDevicePath;\r
   UINTN                      Length;\r
   EFI_STATUS                 Status;\r
 \r
-  TmpDevicePath = NULL;\r
-  \r
+  TmpIpDevicePath  = NULL;\r
+  TmpDnsDevicePath = NULL;\r
+\r
   //\r
   // Update the IP node with DHCP assigned information.\r
   //\r
@@ -64,36 +67,72 @@ HttpBootUpdateDevicePath (
     SetDevicePathNodeLength (Node, sizeof (IPv6_DEVICE_PATH));\r
     Node->Ipv6.PrefixLength    = IP6_PREFIX_LENGTH;\r
     Node->Ipv6.RemotePort      = Private->Port;\r
-    Node->Ipv6.Protocol        = EFI_IP_PROTO_TCP; \r
+    Node->Ipv6.Protocol        = EFI_IP_PROTO_TCP;\r
     Node->Ipv6.IpAddressOrigin = 0;\r
     CopyMem (&Node->Ipv6.LocalIpAddress, &Private->StationIp.v6, sizeof (EFI_IPv6_ADDRESS));\r
     CopyMem (&Node->Ipv6.RemoteIpAddress, &Private->ServerIp.v6, sizeof (EFI_IPv6_ADDRESS));\r
     CopyMem (&Node->Ipv6.GatewayIpAddress, &Private->GatewayIp.v6, sizeof (EFI_IPv6_ADDRESS));\r
   }\r
-  \r
-  TmpDevicePath = AppendDevicePathNode (Private->ParentDevicePath, (EFI_DEVICE_PATH_PROTOCOL*) Node);\r
+\r
+  TmpIpDevicePath = AppendDevicePathNode (Private->ParentDevicePath, (EFI_DEVICE_PATH_PROTOCOL*) Node);\r
   FreePool (Node);\r
-  if (TmpDevicePath == NULL) {\r
+  if (TmpIpDevicePath == NULL) {\r
     return EFI_OUT_OF_RESOURCES;\r
   }\r
 \r
+  //\r
+  // Update the DNS node with DNS server IP list if existed.\r
+  //\r
+  if (Private->DnsServerIp != NULL) {\r
+    Length = sizeof (EFI_DEVICE_PATH_PROTOCOL) + sizeof (Node->Dns.IsIPv6) + Private->DnsServerCount * sizeof (EFI_IP_ADDRESS);\r
+    Node = AllocatePool (Length);\r
+    if (Node == NULL) {\r
+      FreePool (TmpIpDevicePath);\r
+      return EFI_OUT_OF_RESOURCES;\r
+    }\r
+    Node->DevPath.Type    = MESSAGING_DEVICE_PATH;\r
+    Node->DevPath.SubType = MSG_DNS_DP;\r
+    SetDevicePathNodeLength (Node, Length);\r
+    Node->Dns.IsIPv6 = Private->UsingIpv6 ? 0x01 : 0x00;\r
+    CopyMem ((UINT8*) Node + sizeof (EFI_DEVICE_PATH_PROTOCOL) + sizeof (Node->Dns.IsIPv6), Private->DnsServerIp, Private->DnsServerCount * sizeof (EFI_IP_ADDRESS));\r
+\r
+    TmpDnsDevicePath = AppendDevicePathNode (TmpIpDevicePath, (EFI_DEVICE_PATH_PROTOCOL*) Node);\r
+    FreePool (Node);\r
+    FreePool (TmpIpDevicePath);\r
+    TmpIpDevicePath = NULL;\r
+    if (TmpDnsDevicePath == NULL) {\r
+      return EFI_OUT_OF_RESOURCES;\r
+    }\r
+  }\r
+\r
   //\r
   // Update the URI node with the boot file URI.\r
   //\r
   Length = sizeof (EFI_DEVICE_PATH_PROTOCOL) + AsciiStrSize (Private->BootFileUri);\r
   Node = AllocatePool (Length);\r
   if (Node == NULL) {\r
-    FreePool (TmpDevicePath);\r
+    if (TmpIpDevicePath != NULL) {\r
+      FreePool (TmpIpDevicePath);\r
+    }\r
+    if (TmpDnsDevicePath != NULL) {\r
+      FreePool (TmpDnsDevicePath);\r
+    }\r
     return EFI_OUT_OF_RESOURCES;\r
   }\r
   Node->DevPath.Type    = MESSAGING_DEVICE_PATH;\r
   Node->DevPath.SubType = MSG_URI_DP;\r
   SetDevicePathNodeLength (Node, Length);\r
   CopyMem ((UINT8*) Node + sizeof (EFI_DEVICE_PATH_PROTOCOL), Private->BootFileUri, AsciiStrSize (Private->BootFileUri));\r
-  \r
-  NewDevicePath = AppendDevicePathNode (TmpDevicePath, (EFI_DEVICE_PATH_PROTOCOL*) Node);\r
+\r
+  if (TmpDnsDevicePath != NULL) {\r
+    NewDevicePath = AppendDevicePathNode (TmpDnsDevicePath, (EFI_DEVICE_PATH_PROTOCOL*) Node);\r
+    FreePool (TmpDnsDevicePath);\r
+  } else {\r
+    ASSERT (TmpIpDevicePath != NULL);\r
+    NewDevicePath = AppendDevicePathNode (TmpIpDevicePath, (EFI_DEVICE_PATH_PROTOCOL*) Node);\r
+    FreePool (TmpIpDevicePath);\r
+  }\r
   FreePool (Node);\r
-  FreePool (TmpDevicePath);\r
   if (NewDevicePath == NULL) {\r
     return EFI_OUT_OF_RESOURCES;\r
   }\r
@@ -111,7 +150,7 @@ HttpBootUpdateDevicePath (
     if (EFI_ERROR (Status)) {\r
       return Status;\r
     }\r
-    \r
+\r
     FreePool (Private->Ip4Nic->DevicePath);\r
     Private->Ip4Nic->DevicePath = NewDevicePath;\r
   } else {\r
@@ -130,7 +169,7 @@ HttpBootUpdateDevicePath (
     FreePool (Private->Ip6Nic->DevicePath);\r
     Private->Ip6Nic->DevicePath = NewDevicePath;\r
   }\r
-  \r
+\r
   return EFI_SUCCESS;\r
 }\r
 \r
@@ -152,6 +191,7 @@ HttpBootDhcp4ExtractUriInfo (
   HTTP_BOOT_DHCP4_PACKET_CACHE    *HttpOffer;\r
   UINT32                          SelectIndex;\r
   UINT32                          ProxyIndex;\r
+  UINT32                          DnsServerIndex;\r
   EFI_DHCP4_PACKET_OPTION         *Option;\r
   EFI_STATUS                      Status;\r
 \r
@@ -160,6 +200,8 @@ HttpBootDhcp4ExtractUriInfo (
   SelectIndex = Private->SelectIndex - 1;\r
   ASSERT (SelectIndex < HTTP_BOOT_OFFER_MAX_NUM);\r
 \r
+  DnsServerIndex = 0;\r
+\r
   Status = EFI_SUCCESS;\r
 \r
   //\r
@@ -167,26 +209,74 @@ HttpBootDhcp4ExtractUriInfo (
   // HttpOffer contains the boot file URL.\r
   //\r
   SelectOffer = &Private->OfferBuffer[SelectIndex].Dhcp4;\r
-  if ((SelectOffer->OfferType == HttpOfferTypeDhcpIpUri) || (SelectOffer->OfferType == HttpOfferTypeDhcpNameUriDns)) {\r
-    HttpOffer = SelectOffer;\r
+  if (Private->FilePathUri == NULL) {\r
+    //\r
+    // In Corporate environment, we need a HttpOffer.\r
+    //\r
+    if ((SelectOffer->OfferType == HttpOfferTypeDhcpIpUri) ||\r
+        (SelectOffer->OfferType == HttpOfferTypeDhcpIpUriDns) ||\r
+        (SelectOffer->OfferType == HttpOfferTypeDhcpNameUriDns)) {\r
+      HttpOffer = SelectOffer;\r
+    } else {\r
+      ASSERT (Private->SelectProxyType != HttpOfferTypeMax);\r
+      ProxyIndex = Private->OfferIndex[Private->SelectProxyType][0];\r
+      HttpOffer = &Private->OfferBuffer[ProxyIndex].Dhcp4;\r
+    }\r
+    Private->BootFileUriParser = HttpOffer->UriParser;\r
+    Private->BootFileUri = (CHAR8*) HttpOffer->OptList[HTTP_BOOT_DHCP4_TAG_INDEX_BOOTFILE]->Data;\r
   } else {\r
-    ASSERT (Private->SelectProxyType != HttpOfferTypeMax);\r
-    ProxyIndex = Private->OfferIndex[Private->SelectProxyType][0];\r
-    HttpOffer = &Private->OfferBuffer[ProxyIndex].Dhcp4;\r
+    //\r
+    // In Home environment the BootFileUri comes from the FilePath.\r
+    //\r
+    Private->BootFileUriParser = Private->FilePathUriParser;\r
+    Private->BootFileUri = Private->FilePathUri;\r
   }\r
 \r
   //\r
-  // Configure the default DNS server if server assigned.\r
+  // Check the URI scheme.\r
   //\r
-  if ((SelectOffer->OfferType == HttpOfferTypeDhcpNameUriDns) || (SelectOffer->OfferType == HttpOfferTypeDhcpDns)) {\r
+  Status = HttpBootCheckUriScheme (Private->BootFileUri);\r
+  if (EFI_ERROR (Status)) {\r
+    DEBUG ((EFI_D_ERROR, "HttpBootDhcp4ExtractUriInfo: %r.\n", Status));\r
+    if (Status == EFI_INVALID_PARAMETER) {\r
+      AsciiPrint ("\n  Error: Invalid URI address.\n");\r
+    } else if (Status == EFI_ACCESS_DENIED) {\r
+      AsciiPrint ("\n  Error: Access forbidden, only HTTPS connection is allowed.\n");\r
+    }\r
+    return Status;\r
+  }\r
+\r
+  if ((SelectOffer->OfferType == HttpOfferTypeDhcpNameUriDns) ||\r
+      (SelectOffer->OfferType == HttpOfferTypeDhcpDns) ||\r
+      (SelectOffer->OfferType == HttpOfferTypeDhcpIpUriDns)) {\r
     Option = SelectOffer->OptList[HTTP_BOOT_DHCP4_TAG_INDEX_DNS_SERVER];\r
     ASSERT (Option != NULL);\r
+\r
+    //\r
+    // Record the Dns Server address list.\r
+    //\r
+    Private->DnsServerCount = (Option->Length) / sizeof (EFI_IPv4_ADDRESS);\r
+\r
+    Private->DnsServerIp = AllocateZeroPool (Private->DnsServerCount * sizeof (EFI_IP_ADDRESS));\r
+    if (Private->DnsServerIp == NULL) {\r
+      return EFI_OUT_OF_RESOURCES;\r
+    }\r
+\r
+    for (DnsServerIndex = 0; DnsServerIndex < Private->DnsServerCount; DnsServerIndex++) {\r
+      CopyMem (&(Private->DnsServerIp[DnsServerIndex].v4), &(((EFI_IPv4_ADDRESS *) Option->Data)[DnsServerIndex]), sizeof (EFI_IPv4_ADDRESS));\r
+    }\r
+\r
+    //\r
+    // Configure the default DNS server if server assigned.\r
+    //\r
     Status = HttpBootRegisterIp4Dns (\r
                Private,\r
                Option->Length,\r
                Option->Data\r
                );\r
     if (EFI_ERROR (Status)) {\r
+      FreePool (Private->DnsServerIp);\r
+      Private->DnsServerIp = NULL;\r
       return Status;\r
     }\r
   }\r
@@ -195,30 +285,26 @@ HttpBootDhcp4ExtractUriInfo (
   // Extract the port from URL, and use default HTTP port 80 if not provided.\r
   //\r
   Status = HttpUrlGetPort (\r
-             (CHAR8*) HttpOffer->OptList[HTTP_BOOT_DHCP4_TAG_INDEX_BOOTFILE]->Data,\r
-             HttpOffer->UriParser,\r
+             Private->BootFileUri,\r
+             Private->BootFileUriParser,\r
              &Private->Port\r
              );\r
   if (EFI_ERROR (Status) || Private->Port == 0) {\r
     Private->Port = 80;\r
   }\r
-  \r
-  //\r
-  // Record the URI of boot file from the selected HTTP offer.\r
-  //\r
-  Private->BootFileUriParser = HttpOffer->UriParser;\r
-  Private->BootFileUri = (CHAR8*) HttpOffer->OptList[HTTP_BOOT_DHCP4_TAG_INDEX_BOOTFILE]->Data;\r
 \r
-  \r
   //\r
   // All boot informations are valid here.\r
   //\r
-  AsciiPrint ("\n  URI: %a", Private->BootFileUri);\r
 \r
   //\r
-  // Update the device path to include the IP and boot URI information.\r
+  // Update the device path to include the boot resource information.\r
   //\r
   Status = HttpBootUpdateDevicePath (Private);\r
+  if (EFI_ERROR (Status) && Private->DnsServerIp != NULL) {\r
+    FreePool (Private->DnsServerIp);\r
+    Private->DnsServerIp = NULL;\r
+  }\r
 \r
   return Status;\r
 }\r
@@ -241,9 +327,11 @@ HttpBootDhcp6ExtractUriInfo (
   HTTP_BOOT_DHCP6_PACKET_CACHE    *HttpOffer;\r
   UINT32                          SelectIndex;\r
   UINT32                          ProxyIndex;\r
+  UINT32                          DnsServerIndex;\r
   EFI_DHCP6_PACKET_OPTION         *Option;\r
   EFI_IPv6_ADDRESS                IpAddr;\r
   CHAR8                           *HostName;\r
+  UINTN                           HostNameSize;\r
   CHAR16                          *HostNameStr;\r
   EFI_STATUS                      Status;\r
 \r
@@ -252,6 +340,8 @@ HttpBootDhcp6ExtractUriInfo (
   SelectIndex = Private->SelectIndex - 1;\r
   ASSERT (SelectIndex < HTTP_BOOT_OFFER_MAX_NUM);\r
 \r
+  DnsServerIndex = 0;\r
+\r
   Status   = EFI_SUCCESS;\r
   HostName = NULL;\r
   //\r
@@ -259,12 +349,41 @@ HttpBootDhcp6ExtractUriInfo (
   // HttpOffer contains the boot file URL.\r
   //\r
   SelectOffer = &Private->OfferBuffer[SelectIndex].Dhcp6;\r
-  if ((SelectOffer->OfferType == HttpOfferTypeDhcpIpUri) || (SelectOffer->OfferType == HttpOfferTypeDhcpNameUriDns)) {\r
-    HttpOffer = SelectOffer;\r
+  if (Private->FilePathUri == NULL) {\r
+    //\r
+    // In Corporate environment, we need a HttpOffer.\r
+    //\r
+    if ((SelectOffer->OfferType == HttpOfferTypeDhcpIpUri) ||\r
+        (SelectOffer->OfferType == HttpOfferTypeDhcpIpUriDns) ||\r
+        (SelectOffer->OfferType == HttpOfferTypeDhcpNameUriDns)) {\r
+      HttpOffer = SelectOffer;\r
+    } else {\r
+      ASSERT (Private->SelectProxyType != HttpOfferTypeMax);\r
+      ProxyIndex = Private->OfferIndex[Private->SelectProxyType][0];\r
+      HttpOffer = &Private->OfferBuffer[ProxyIndex].Dhcp6;\r
+    }\r
+    Private->BootFileUriParser = HttpOffer->UriParser;\r
+    Private->BootFileUri = (CHAR8*) HttpOffer->OptList[HTTP_BOOT_DHCP6_IDX_BOOT_FILE_URL]->Data;\r
   } else {\r
-    ASSERT (Private->SelectProxyType != HttpOfferTypeMax);\r
-    ProxyIndex = Private->OfferIndex[Private->SelectProxyType][0];\r
-    HttpOffer = &Private->OfferBuffer[ProxyIndex].Dhcp6;\r
+    //\r
+    // In Home environment the BootFileUri comes from the FilePath.\r
+    //\r
+    Private->BootFileUriParser = Private->FilePathUriParser;\r
+    Private->BootFileUri = Private->FilePathUri;\r
+  }\r
+\r
+  //\r
+  // Check the URI scheme.\r
+  //\r
+  Status = HttpBootCheckUriScheme (Private->BootFileUri);\r
+  if (EFI_ERROR (Status)) {\r
+    DEBUG ((EFI_D_ERROR, "HttpBootDhcp6ExtractUriInfo: %r.\n", Status));\r
+    if (Status == EFI_INVALID_PARAMETER) {\r
+      AsciiPrint ("\n  Error: Invalid URI address.\n");\r
+    } else if (Status == EFI_ACCESS_DENIED) {\r
+      AsciiPrint ("\n  Error: Access forbidden, only HTTPS connection is allowed.\n");\r
+    }\r
+    return Status;\r
   }\r
 \r
   //\r
@@ -274,104 +393,126 @@ HttpBootDhcp6ExtractUriInfo (
   if (EFI_ERROR (Status)) {\r
     return Status;\r
   }\r
-  \r
+\r
   //\r
-  // Configure the default DNS server if server assigned.\r
+  // Register the IPv6 gateway address to the network device.\r
   //\r
-  if ((SelectOffer->OfferType == HttpOfferTypeDhcpNameUriDns) || (SelectOffer->OfferType == HttpOfferTypeDhcpDns)) {\r
+  Status = HttpBootSetIp6Gateway (Private);\r
+  if (EFI_ERROR (Status)) {\r
+    return Status;\r
+  }\r
+\r
+  if ((SelectOffer->OfferType == HttpOfferTypeDhcpNameUriDns) ||\r
+      (SelectOffer->OfferType == HttpOfferTypeDhcpDns) ||\r
+      (SelectOffer->OfferType == HttpOfferTypeDhcpIpUriDns)) {\r
     Option = SelectOffer->OptList[HTTP_BOOT_DHCP6_IDX_DNS_SERVER];\r
     ASSERT (Option != NULL);\r
+\r
+    //\r
+    // Record the Dns Server address list.\r
+    //\r
+    Private->DnsServerCount = HTONS (Option->OpLen) / sizeof (EFI_IPv6_ADDRESS);\r
+\r
+    Private->DnsServerIp = AllocateZeroPool (Private->DnsServerCount * sizeof (EFI_IP_ADDRESS));\r
+    if (Private->DnsServerIp == NULL) {\r
+      return EFI_OUT_OF_RESOURCES;\r
+    }\r
+\r
+    for (DnsServerIndex = 0; DnsServerIndex < Private->DnsServerCount; DnsServerIndex++) {\r
+      CopyMem (&(Private->DnsServerIp[DnsServerIndex].v6), &(((EFI_IPv6_ADDRESS *) Option->Data)[DnsServerIndex]), sizeof (EFI_IPv6_ADDRESS));\r
+    }\r
+\r
+    //\r
+    // Configure the default DNS server if server assigned.\r
+    //\r
     Status = HttpBootSetIp6Dns (\r
                Private,\r
                HTONS (Option->OpLen),\r
                Option->Data\r
                );\r
     if (EFI_ERROR (Status)) {\r
-      return Status;\r
+      goto Error;\r
     }\r
   }\r
-  \r
+\r
   //\r
-  // Extract the HTTP server Ip frome URL. This is used to Check route table \r
+  // Extract the HTTP server Ip from URL. This is used to Check route table\r
   // whether can send message to HTTP Server Ip through the GateWay.\r
   //\r
   Status = HttpUrlGetIp6 (\r
-             (CHAR8*) HttpOffer->OptList[HTTP_BOOT_DHCP6_IDX_BOOT_FILE_URL]->Data,\r
-             HttpOffer->UriParser,\r
+             Private->BootFileUri,\r
+             Private->BootFileUriParser,\r
              &IpAddr\r
              );\r
-  \r
+\r
   if (EFI_ERROR (Status)) {\r
     //\r
     // The Http server address is expressed by Name Ip, so perform DNS resolution\r
     //\r
     Status = HttpUrlGetHostName (\r
-               (CHAR8*) HttpOffer->OptList[HTTP_BOOT_DHCP6_IDX_BOOT_FILE_URL]->Data,\r
-               HttpOffer->UriParser,\r
+               Private->BootFileUri,\r
+               Private->BootFileUriParser,\r
                &HostName\r
                );\r
     if (EFI_ERROR (Status)) {\r
-      return Status;\r
+      goto Error;\r
     }\r
-    \r
-    HostNameStr = AllocateZeroPool ((AsciiStrLen (HostName) + 1) * sizeof (CHAR16));\r
+\r
+    HostNameSize = AsciiStrSize (HostName);\r
+    HostNameStr = AllocateZeroPool (HostNameSize * sizeof (CHAR16));\r
     if (HostNameStr == NULL) {\r
       Status = EFI_OUT_OF_RESOURCES;\r
       goto Error;\r
     }\r
-    \r
-    AsciiStrToUnicodeStr (HostName, HostNameStr);\r
+\r
+    AsciiStrToUnicodeStrS (HostName, HostNameStr, HostNameSize);\r
+\r
+    if (HostName != NULL) {\r
+      FreePool (HostName);\r
+    }\r
+\r
     Status = HttpBootDns (Private, HostNameStr, &IpAddr);\r
     FreePool (HostNameStr);\r
     if (EFI_ERROR (Status)) {\r
+      AsciiPrint ("\n  Error: Could not retrieve the host address from DNS server.\n");\r
       goto Error;\r
-    }  \r
-  } \r
-  \r
-  CopyMem (&Private->ServerIp.v6, &IpAddr, sizeof (EFI_IPv6_ADDRESS));  \r
-    \r
-  //\r
-  // register the IPv6 gateway address to the network device.\r
-  //\r
-  Status = HttpBootSetIp6Gateway (Private);\r
-  if (EFI_ERROR (Status)) {\r
-    return Status;\r
+    }\r
   }\r
-  \r
+\r
+  CopyMem (&Private->ServerIp.v6, &IpAddr, sizeof (EFI_IPv6_ADDRESS));\r
+\r
   //\r
   // Extract the port from URL, and use default HTTP port 80 if not provided.\r
   //\r
   Status = HttpUrlGetPort (\r
-             (CHAR8*) HttpOffer->OptList[HTTP_BOOT_DHCP6_IDX_BOOT_FILE_URL]->Data,\r
-             HttpOffer->UriParser,\r
+             Private->BootFileUri,\r
+             Private->BootFileUriParser,\r
              &Private->Port\r
              );\r
   if (EFI_ERROR (Status) || Private->Port == 0) {\r
     Private->Port = 80;\r
   }\r
-  \r
-  //\r
-  // Record the URI of boot file from the selected HTTP offer.\r
-  //\r
-  Private->BootFileUriParser = HttpOffer->UriParser;\r
-  Private->BootFileUri = (CHAR8*) HttpOffer->OptList[HTTP_BOOT_DHCP6_IDX_BOOT_FILE_URL]->Data;\r
 \r
-  \r
   //\r
   // All boot informations are valid here.\r
   //\r
-  AsciiPrint ("\n  URI: %a", Private->BootFileUri);\r
+\r
   //\r
-  // Update the device path to include the IP and boot URI information.\r
+  // Update the device path to include the boot resource information.\r
   //\r
   Status = HttpBootUpdateDevicePath (Private);\r
+  if (EFI_ERROR (Status)) {\r
+    goto Error;\r
+  }\r
+\r
+  return Status;\r
 \r
 Error:\r
-  \r
-  if (HostName != NULL) {\r
-    FreePool (HostName);\r
+  if (Private->DnsServerIp != NULL) {\r
+    FreePool (Private->DnsServerIp);\r
+    Private->DnsServerIp = NULL;\r
   }\r
-    \r
+\r
   return Status;\r
 }\r
 \r
@@ -391,7 +532,7 @@ HttpBootDiscoverBootInfo (
   )\r
 {\r
   EFI_STATUS              Status;\r
-  \r
+\r
   //\r
   // Start D.O.R.A/S.A.R.R exchange to acquire station ip address and\r
   // other Http boot information.\r
@@ -410,6 +551,40 @@ HttpBootDiscoverBootInfo (
   return Status;\r
 }\r
 \r
+/**\r
+  HttpIo Callback function which will be invoked when specified HTTP_IO_CALLBACK_EVENT happened.\r
+\r
+  @param[in]    EventType      Indicate the Event type that occurs in the current callback.\r
+  @param[in]    Message        HTTP message which will be send to, or just received from HTTP server.\r
+  @param[in]    Context        The Callback Context pointer.\r
+\r
+  @retval EFI_SUCCESS          Tells the HttpIo to continue the HTTP process.\r
+  @retval Others               Tells the HttpIo to abort the current HTTP process.\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+HttpBootHttpIoCallback (\r
+  IN  HTTP_IO_CALLBACK_EVENT    EventType,\r
+  IN  EFI_HTTP_MESSAGE          *Message,\r
+  IN  VOID                      *Context\r
+  )\r
+{\r
+  HTTP_BOOT_PRIVATE_DATA       *Private;\r
+  EFI_STATUS                   Status;\r
+  Private = (HTTP_BOOT_PRIVATE_DATA *) Context;\r
+  if (Private->HttpBootCallback != NULL) {\r
+    Status = Private->HttpBootCallback->Callback (\r
+               Private->HttpBootCallback,\r
+               EventType == HttpIoRequest ? HttpBootHttpRequest : HttpBootHttpResponse,\r
+               EventType == HttpIoRequest ? FALSE : TRUE,\r
+               sizeof (EFI_HTTP_MESSAGE),\r
+               (VOID *) Message\r
+               );\r
+    return Status;\r
+  }\r
+  return EFI_SUCCESS;\r
+}\r
+\r
 /**\r
   Create a HttpIo instance for the file download.\r
 \r
@@ -426,6 +601,7 @@ HttpBootCreateHttpIo (
 {\r
   HTTP_IO_CONFIG_DATA          ConfigData;\r
   EFI_STATUS                   Status;\r
+  EFI_HANDLE                   ImageHandle;\r
 \r
   ASSERT (Private != NULL);\r
 \r
@@ -435,17 +611,21 @@ HttpBootCreateHttpIo (
     ConfigData.Config4.RequestTimeOut = HTTP_BOOT_REQUEST_TIMEOUT;\r
     IP4_COPY_ADDRESS (&ConfigData.Config4.LocalIp, &Private->StationIp.v4);\r
     IP4_COPY_ADDRESS (&ConfigData.Config4.SubnetMask, &Private->SubnetMask.v4);\r
+    ImageHandle = Private->Ip4Nic->ImageHandle;\r
   } else {\r
     ConfigData.Config6.HttpVersion    = HttpVersion11;\r
     ConfigData.Config6.RequestTimeOut = HTTP_BOOT_REQUEST_TIMEOUT;\r
     IP6_COPY_ADDRESS (&ConfigData.Config6.LocalIp, &Private->StationIp.v6);\r
+    ImageHandle = Private->Ip6Nic->ImageHandle;\r
   }\r
 \r
   Status = HttpIoCreateIo (\r
-             Private->Image,\r
+             ImageHandle,\r
              Private->Controller,\r
              Private->UsingIpv6 ? IP_VERSION_6 : IP_VERSION_4,\r
              &ConfigData,\r
+             HttpBootHttpIoCallback,\r
+             (VOID *) Private,\r
              &Private->HttpIo\r
              );\r
   if (EFI_ERROR (Status)) {\r
@@ -456,81 +636,6 @@ HttpBootCreateHttpIo (
   return EFI_SUCCESS;\r
 }\r
 \r
-/**\r
-  Get the file content from cached data.\r
-\r
-  @param[in]          Private         The pointer to the driver's private data.\r
-  @param[in]          Uri             Uri of the file to be retrieved from cache.\r
-  @param[in, out]     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[out]         Buffer          The memory buffer to transfer the file to. IF Buffer is NULL,\r
-                                      then the size of the requested file is returned in\r
-                                      BufferSize.\r
-\r
-  @retval EFI_SUCCESS          Successfully created.\r
-  @retval Others               Failed to create HttpIo.\r
-\r
-**/\r
-EFI_STATUS\r
-HttpBootGetFileFromCache (\r
-  IN     HTTP_BOOT_PRIVATE_DATA   *Private,\r
-  IN     CHAR16                   *Uri,\r
-  IN OUT UINTN                    *BufferSize,\r
-     OUT UINT8                    *Buffer\r
-  )\r
-{\r
-  LIST_ENTRY                  *Entry;\r
-  LIST_ENTRY                  *Entry2;\r
-  HTTP_BOOT_CACHE_CONTENT     *Cache;\r
-  HTTP_BOOT_ENTITY_DATA       *EntityData;\r
-  UINTN                       CopyedSize;\r
-  \r
-  if (Uri == NULL || BufferSize == 0 || Buffer == NULL) {\r
-    return EFI_INVALID_PARAMETER;\r
-  }\r
-\r
-  NET_LIST_FOR_EACH (Entry, &Private->CacheList) {\r
-    Cache = NET_LIST_USER_STRUCT (Entry, HTTP_BOOT_CACHE_CONTENT, Link);\r
-    //\r
-    // Compare the URI to see whether we already have a cache for this file.\r
-    //\r
-    if ((Cache->RequestData != NULL) &&\r
-        (Cache->RequestData->Url != NULL) &&\r
-        (StrCmp (Uri, Cache->RequestData->Url) == 0)) \r
-    {\r
-      //\r
-      // Hit cache, check buffer size.\r
-      //\r
-      if (*BufferSize < Cache->EntityLength) {\r
-        *BufferSize = Cache->EntityLength;\r
-        return EFI_BUFFER_TOO_SMALL;\r
-      }\r
-\r
-      //\r
-      // Fill data to buffer.\r
-      //\r
-      CopyedSize = 0;\r
-      NET_LIST_FOR_EACH (Entry2, &Cache->EntityDataList) {\r
-        EntityData = NET_LIST_USER_STRUCT (Entry2, HTTP_BOOT_ENTITY_DATA, Link);\r
-        if (*BufferSize > CopyedSize) {\r
-          CopyMem (\r
-            Buffer + CopyedSize,\r
-            EntityData->DataStart,\r
-            MIN (EntityData->DataLength, *BufferSize - CopyedSize)\r
-            );\r
-          CopyedSize += MIN (EntityData->DataLength, *BufferSize - CopyedSize);\r
-        }\r
-      }\r
-      *BufferSize = CopyedSize;\r
-      return EFI_SUCCESS;\r
-    }\r
-  }\r
-\r
-  return EFI_NOT_FOUND;\r
-}\r
-\r
 /**\r
   Release all the resource of a cache item.\r
 \r
@@ -601,7 +706,7 @@ HttpBootFreeCacheList (
   LIST_ENTRY                  *Entry;\r
   LIST_ENTRY                  *NextEntry;\r
   HTTP_BOOT_CACHE_CONTENT     *Cache;\r
-  \r
+\r
   NET_LIST_FOR_EACH_SAFE (Entry, NextEntry, &Private->CacheList) {\r
     Cache = NET_LIST_USER_STRUCT (Entry, HTTP_BOOT_CACHE_CONTENT, Link);\r
     RemoveEntryList (&Cache->Link);\r
@@ -609,6 +714,87 @@ HttpBootFreeCacheList (
   }\r
 }\r
 \r
+/**\r
+  Get the file content from cached data.\r
+\r
+  @param[in]          Private         The pointer to the driver's private data.\r
+  @param[in]          Uri             Uri of the file to be retrieved from cache.\r
+  @param[in, out]     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[out]         Buffer          The memory buffer to transfer the file to. IF Buffer is NULL,\r
+                                      then the size of the requested file is returned in\r
+                                      BufferSize.\r
+  @param[out]         ImageType       The image type of the downloaded file.\r
+\r
+  @retval EFI_SUCCESS          Successfully created.\r
+  @retval Others               Failed to create HttpIo.\r
+\r
+**/\r
+EFI_STATUS\r
+HttpBootGetFileFromCache (\r
+  IN     HTTP_BOOT_PRIVATE_DATA   *Private,\r
+  IN     CHAR16                   *Uri,\r
+  IN OUT UINTN                    *BufferSize,\r
+     OUT UINT8                    *Buffer,\r
+     OUT HTTP_BOOT_IMAGE_TYPE     *ImageType\r
+  )\r
+{\r
+  LIST_ENTRY                  *Entry;\r
+  LIST_ENTRY                  *Entry2;\r
+  HTTP_BOOT_CACHE_CONTENT     *Cache;\r
+  HTTP_BOOT_ENTITY_DATA       *EntityData;\r
+  UINTN                       CopyedSize;\r
+\r
+  if (Uri == NULL || BufferSize == NULL || Buffer == NULL || ImageType == NULL) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
+  NET_LIST_FOR_EACH (Entry, &Private->CacheList) {\r
+    Cache = NET_LIST_USER_STRUCT (Entry, HTTP_BOOT_CACHE_CONTENT, Link);\r
+    //\r
+    // Compare the URI to see whether we already have a cache for this file.\r
+    //\r
+    if ((Cache->RequestData != NULL) &&\r
+        (Cache->RequestData->Url != NULL) &&\r
+        (StrCmp (Uri, Cache->RequestData->Url) == 0)) {\r
+      //\r
+      // Hit in cache, record image type.\r
+      //\r
+      *ImageType  = Cache->ImageType;\r
+\r
+      //\r
+      // Check buffer size.\r
+      //\r
+      if (*BufferSize < Cache->EntityLength) {\r
+        *BufferSize = Cache->EntityLength;\r
+        return EFI_BUFFER_TOO_SMALL;\r
+      }\r
+\r
+      //\r
+      // Fill data to buffer.\r
+      //\r
+      CopyedSize = 0;\r
+      NET_LIST_FOR_EACH (Entry2, &Cache->EntityDataList) {\r
+        EntityData = NET_LIST_USER_STRUCT (Entry2, HTTP_BOOT_ENTITY_DATA, Link);\r
+        if (*BufferSize > CopyedSize) {\r
+          CopyMem (\r
+            Buffer + CopyedSize,\r
+            EntityData->DataStart,\r
+            MIN (EntityData->DataLength, *BufferSize - CopyedSize)\r
+            );\r
+          CopyedSize += MIN (EntityData->DataLength, *BufferSize - CopyedSize);\r
+        }\r
+      }\r
+      *BufferSize = CopyedSize;\r
+      return EFI_SUCCESS;\r
+    }\r
+  }\r
+\r
+  return EFI_NOT_FOUND;\r
+}\r
+\r
 /**\r
   A callback function to intercept events during message parser.\r
 \r
@@ -622,7 +808,7 @@ HttpBootFreeCacheList (
 \r
   @retval EFI_SUCCESS              Continue to parser the message body.\r
   @retval Others                   Abort the parse.\r
\r
+\r
 **/\r
 EFI_STATUS\r
 EFIAPI\r
@@ -635,6 +821,8 @@ HttpBootGetBootFileCallback (
 {\r
   HTTP_BOOT_CALLBACK_DATA      *CallbackData;\r
   HTTP_BOOT_ENTITY_DATA        *NewEntityData;\r
+  EFI_STATUS                   Status;\r
+  EFI_HTTP_BOOT_CALLBACK_PROTOCOL   *HttpBootCallback;\r
 \r
   //\r
   // We only care about the entity data.\r
@@ -644,6 +832,19 @@ HttpBootGetBootFileCallback (
   }\r
 \r
   CallbackData = (HTTP_BOOT_CALLBACK_DATA *) Context;\r
+  HttpBootCallback = CallbackData->Private->HttpBootCallback;\r
+  if (HttpBootCallback != NULL) {\r
+    Status = HttpBootCallback->Callback (\r
+               HttpBootCallback,\r
+               HttpBootHttpEntityBody,\r
+               TRUE,\r
+               (UINT32)Length,\r
+               Data\r
+               );\r
+    if (EFI_ERROR (Status)) {\r
+      return Status;\r
+    }\r
+  }\r
   //\r
   // Copy data if caller has provided a buffer.\r
   //\r
@@ -677,7 +878,7 @@ HttpBootGetBootFileCallback (
 \r
 /**\r
   This function download the boot file by using UEFI HTTP protocol.\r
-  \r
+\r
   @param[in]       Private         The pointer to the driver's private data.\r
   @param[in]       HeaderOnly      Only request the response header, it could save a lot of time if\r
                                    the caller only want to know the size of the requested file.\r
@@ -688,6 +889,7 @@ HttpBootGetBootFileCallback (
   @param[out]      Buffer          The memory buffer to transfer the file to. IF Buffer is NULL,\r
                                    then the size of the requested file is returned in\r
                                    BufferSize.\r
+  @param[out]      ImageType       The image type of the downloaded file.\r
 \r
   @retval EFI_SUCCESS              The file was loaded.\r
   @retval EFI_INVALID_PARAMETER    BufferSize is NULL or Buffer Size is not NULL but Buffer is NULL.\r
@@ -703,14 +905,16 @@ HttpBootGetBootFile (
   IN     HTTP_BOOT_PRIVATE_DATA   *Private,\r
   IN     BOOLEAN                  HeaderOnly,\r
   IN OUT UINTN                    *BufferSize,\r
-     OUT UINT8                    *Buffer\r
+     OUT UINT8                    *Buffer,\r
+     OUT HTTP_BOOT_IMAGE_TYPE     *ImageType\r
   )\r
 {\r
   EFI_STATUS                 Status;\r
+  EFI_HTTP_STATUS_CODE       StatusCode;\r
   CHAR8                      *HostName;\r
   EFI_HTTP_REQUEST_DATA      *RequestData;\r
-  HTTP_IO_RESOPNSE_DATA      *ResponseData;\r
-  HTTP_IO_RESOPNSE_DATA      ResponseBody;\r
+  HTTP_IO_RESPONSE_DATA      *ResponseData;\r
+  HTTP_IO_RESPONSE_DATA      ResponseBody;\r
   HTTP_IO                    *HttpIo;\r
   HTTP_IO_HEADER             *HttpIoHeader;\r
   VOID                       *Parser;\r
@@ -718,12 +922,15 @@ HttpBootGetBootFile (
   UINTN                      ContentLength;\r
   HTTP_BOOT_CACHE_CONTENT    *Cache;\r
   UINT8                      *Block;\r
+  UINTN                      UrlSize;\r
   CHAR16                     *Url;\r
-  \r
+  BOOLEAN                    IdentityMode;\r
+  UINTN                      ReceivedSize;\r
+\r
   ASSERT (Private != NULL);\r
   ASSERT (Private->HttpCreated);\r
 \r
-  if (BufferSize == NULL) {\r
+  if (BufferSize == NULL || ImageType == NULL) {\r
     return EFI_INVALID_PARAMETER;\r
   }\r
 \r
@@ -734,13 +941,14 @@ HttpBootGetBootFile (
   //\r
   // First, check whether we already cached the requested Uri.\r
   //\r
-  Url = AllocatePool ((AsciiStrLen (Private->BootFileUri) + 1) * sizeof (CHAR16));\r
+  UrlSize = AsciiStrSize (Private->BootFileUri);\r
+  Url = AllocatePool (UrlSize * sizeof (CHAR16));\r
   if (Url == NULL) {\r
     return EFI_OUT_OF_RESOURCES;\r
   }\r
-  AsciiStrToUnicodeStr (Private->BootFileUri, Url);\r
-  if (!HeaderOnly) {\r
-    Status = HttpBootGetFileFromCache (Private, Url, BufferSize, Buffer);\r
+  AsciiStrToUnicodeStrS (Private->BootFileUri, Url, UrlSize);\r
+  if (!HeaderOnly && Buffer != NULL) {\r
+    Status = HttpBootGetFileFromCache (Private, Url, BufferSize, Buffer, ImageType);\r
     if (Status != EFI_NOT_FOUND) {\r
       FreePool (Url);\r
       return Status;\r
@@ -761,6 +969,7 @@ HttpBootGetBootFile (
       Status = EFI_OUT_OF_RESOURCES;\r
       goto ERROR_1;\r
     }\r
+    Cache->ImageType = ImageTypeMax;\r
     InitializeListHead (&Cache->EntityDataList);\r
   }\r
 \r
@@ -794,7 +1003,7 @@ HttpBootGetBootFile (
   }\r
   Status = HttpBootSetHeader (\r
              HttpIoHeader,\r
-             HTTP_FIELD_NAME_HOST,\r
+             HTTP_HEADER_HOST,\r
              HostName\r
              );\r
   FreePool (HostName);\r
@@ -807,7 +1016,7 @@ HttpBootGetBootFile (
   //\r
   Status = HttpBootSetHeader (\r
              HttpIoHeader,\r
-             HTTP_FIELD_NAME_ACCEPT,\r
+             HTTP_HEADER_ACCEPT,\r
              "*/*"\r
              );\r
   if (EFI_ERROR (Status)) {\r
@@ -819,7 +1028,7 @@ HttpBootGetBootFile (
   //\r
   Status = HttpBootSetHeader (\r
              HttpIoHeader,\r
-             HTTP_FIELD_NAME_USER_AGENT,\r
+             HTTP_HEADER_USER_AGENT,\r
              HTTP_USER_AGENT_EFI_HTTP_BOOT\r
              );\r
   if (EFI_ERROR (Status)) {\r
@@ -836,11 +1045,6 @@ HttpBootGetBootFile (
   }\r
   RequestData->Method = HeaderOnly ? HttpMethodHead : HttpMethodGet;\r
   RequestData->Url = Url;\r
-  if (RequestData->Url == NULL) {\r
-    Status = EFI_OUT_OF_RESOURCES;\r
-    goto ERROR_4;\r
-  }\r
-  AsciiStrToUnicodeStr (Private->BootFileUri, RequestData->Url);\r
 \r
   //\r
   // 2.3 Record the request info in a temp cache item.\r
@@ -872,7 +1076,7 @@ HttpBootGetBootFile (
   //\r
   // 3.1 First step, use zero BodyLength to only receive the response headers.\r
   //\r
-  ResponseData = AllocateZeroPool (sizeof(HTTP_IO_RESOPNSE_DATA));\r
+  ResponseData = AllocateZeroPool (sizeof(HTTP_IO_RESPONSE_DATA));\r
   if (ResponseData == NULL) {\r
     Status = EFI_OUT_OF_RESOURCES;\r
     goto ERROR_4;\r
@@ -882,6 +1086,25 @@ HttpBootGetBootFile (
              TRUE,\r
              ResponseData\r
              );\r
+  if (EFI_ERROR (Status) || EFI_ERROR (ResponseData->Status)) {\r
+    if (EFI_ERROR (ResponseData->Status)) {\r
+      StatusCode = HttpIo->RspToken.Message->Data.Response->StatusCode;\r
+      HttpBootPrintErrorMessage (StatusCode);\r
+      Status = ResponseData->Status;\r
+    }\r
+    goto ERROR_5;\r
+  }\r
+\r
+  //\r
+  // Check the image type according to server's response.\r
+  //\r
+  Status = HttpBootCheckImageType (\r
+             Private->BootFileUri,\r
+             Private->BootFileUriParser,\r
+             ResponseData->HeaderCount,\r
+             ResponseData->Headers,\r
+             ImageType\r
+             );\r
   if (EFI_ERROR (Status)) {\r
     goto ERROR_5;\r
   }\r
@@ -891,8 +1114,9 @@ HttpBootGetBootFile (
   //\r
   if (Cache != NULL) {\r
     Cache->ResponseData = ResponseData;\r
+    Cache->ImageType = *ImageType;\r
   }\r
-  \r
+\r
   //\r
   // 3.3 Init a message-body parser from the header information.\r
   //\r
@@ -903,8 +1127,9 @@ HttpBootGetBootFile (
   Context.Buffer     = Buffer;\r
   Context.BufferSize = *BufferSize;\r
   Context.Cache      = Cache;\r
+  Context.Private    = Private;\r
   Status = HttpInitMsgParser (\r
-             HeaderOnly? HttpMethodHead : HttpMethodGet,\r
+             HeaderOnly ? HttpMethodHead : HttpMethodGet,\r
              ResponseData->Response.StatusCode,\r
              ResponseData->HeaderCount,\r
              ResponseData->Headers,\r
@@ -921,51 +1146,112 @@ HttpBootGetBootFile (
   //\r
   Block = NULL;\r
   if (!HeaderOnly) {\r
-    ZeroMem (&ResponseBody, sizeof (HTTP_IO_RESOPNSE_DATA));\r
-    while (!HttpIsMessageComplete (Parser)) {\r
+    //\r
+    // 3.4.1, check whether we are in identity transfer-coding.\r
+    //\r
+    ContentLength = 0;\r
+    Status = HttpGetEntityLength (Parser, &ContentLength);\r
+    if (!EFI_ERROR (Status)) {\r
+      IdentityMode = TRUE;\r
+    } else {\r
+      IdentityMode = FALSE;\r
+    }\r
+\r
+    //\r
+    // 3.4.2, start the message-body download, the identity and chunked transfer-coding\r
+    // is handled in different path here.\r
+    //\r
+    ZeroMem (&ResponseBody, sizeof (HTTP_IO_RESPONSE_DATA));\r
+    if (IdentityMode) {\r
       //\r
-      // Allocate a block to hold the message-body, if caller doesn't provide\r
-      // a buffer, the block will be cached and we will allocate a new one here.\r
+      // In identity transfer-coding there is no need to parse the message body,\r
+      // just download the message body to the user provided buffer directly.\r
       //\r
-      if (Block == NULL || Context.BufferSize == 0) {\r
-        Block = AllocatePool (HTTP_BOOT_BLOCK_SIZE);\r
-        if (Block == NULL) {\r
-          Status = EFI_OUT_OF_RESOURCES;\r
+      ReceivedSize = 0;\r
+      while (ReceivedSize < ContentLength) {\r
+        ResponseBody.Body       = (CHAR8*) Buffer + ReceivedSize;\r
+        ResponseBody.BodyLength = *BufferSize - ReceivedSize;\r
+        Status = HttpIoRecvResponse (\r
+                   &Private->HttpIo,\r
+                   FALSE,\r
+                   &ResponseBody\r
+                   );\r
+        if (EFI_ERROR (Status) || EFI_ERROR (ResponseBody.Status)) {\r
+          if (EFI_ERROR (ResponseBody.Status)) {\r
+            Status = ResponseBody.Status;\r
+          }\r
           goto ERROR_6;\r
         }\r
-        Context.NewBlock = TRUE;\r
-        Context.Block = Block;\r
-      } else {\r
-        Context.NewBlock = FALSE;\r
-      }\r
-\r
-      ResponseBody.Body       = (CHAR8*) Block;\r
-      ResponseBody.BodyLength = HTTP_BOOT_BLOCK_SIZE;\r
-      Status = HttpIoRecvResponse (\r
-                 &Private->HttpIo,\r
-                 FALSE,\r
-                 &ResponseBody\r
-                 );\r
-      if (EFI_ERROR (Status)) {\r
-        goto ERROR_6;\r
+        ReceivedSize += ResponseBody.BodyLength;\r
+        if (Private->HttpBootCallback != NULL) {\r
+          Status = Private->HttpBootCallback->Callback (\r
+                     Private->HttpBootCallback,\r
+                     HttpBootHttpEntityBody,\r
+                     TRUE,\r
+                     (UINT32)ResponseBody.BodyLength,\r
+                     ResponseBody.Body\r
+                     );\r
+          if (EFI_ERROR (Status)) {\r
+            goto ERROR_6;\r
+          }\r
+        }\r
       }\r
-\r
+    } else {\r
       //\r
-      // Parse the new received block of the message-body, the block will be saved in cache.\r
+      // In "chunked" transfer-coding mode, so we need to parse the received\r
+      // data to get the real entity content.\r
       //\r
-      Status = HttpParseMessageBody (\r
-                 Parser,\r
-                 ResponseBody.BodyLength,\r
-                 ResponseBody.Body\r
-                 );\r
-      if (EFI_ERROR (Status)) {\r
-        goto ERROR_6;\r
+      Block = NULL;\r
+      while (!HttpIsMessageComplete (Parser)) {\r
+        //\r
+        // Allocate a buffer in Block to hold the message-body.\r
+        // If caller provides a buffer, this Block will be reused in every HttpIoRecvResponse().\r
+        // Otherwise a buffer, the buffer in Block will be cached and we should allocate a new before\r
+        // every HttpIoRecvResponse().\r
+        //\r
+        if (Block == NULL || Context.BufferSize == 0) {\r
+          Block = AllocatePool (HTTP_BOOT_BLOCK_SIZE);\r
+          if (Block == NULL) {\r
+            Status = EFI_OUT_OF_RESOURCES;\r
+            goto ERROR_6;\r
+          }\r
+          Context.NewBlock = TRUE;\r
+          Context.Block = Block;\r
+        } else {\r
+          Context.NewBlock = FALSE;\r
+        }\r
+\r
+        ResponseBody.Body       = (CHAR8*) Block;\r
+        ResponseBody.BodyLength = HTTP_BOOT_BLOCK_SIZE;\r
+        Status = HttpIoRecvResponse (\r
+                   &Private->HttpIo,\r
+                   FALSE,\r
+                   &ResponseBody\r
+                   );\r
+        if (EFI_ERROR (Status) || EFI_ERROR (ResponseBody.Status)) {\r
+          if (EFI_ERROR (ResponseBody.Status)) {\r
+            Status = ResponseBody.Status;\r
+          }\r
+          goto ERROR_6;\r
+        }\r
+\r
+        //\r
+        // Parse the new received block of the message-body, the block will be saved in cache.\r
+        //\r
+        Status = HttpParseMessageBody (\r
+                   Parser,\r
+                   ResponseBody.BodyLength,\r
+                   ResponseBody.Body\r
+                   );\r
+        if (EFI_ERROR (Status)) {\r
+          goto ERROR_6;\r
+        }\r
       }\r
     }\r
   }\r
-  \r
+\r
   //\r
-  // 3.5 Message-body receive & parse is completed, get the file size.\r
+  // 3.5 Message-body receive & parse is completed, we should be able to get the file size now.\r
   //\r
   Status = HttpGetEntityLength (Parser, &ContentLength);\r
   if (EFI_ERROR (Status)) {\r
@@ -974,6 +1260,8 @@ HttpBootGetBootFile (
 \r
   if (*BufferSize < ContentLength) {\r
     Status = EFI_BUFFER_TOO_SMALL;\r
+  } else {\r
+    Status = EFI_SUCCESS;\r
   }\r
   *BufferSize = ContentLength;\r
 \r
@@ -989,8 +1277,8 @@ HttpBootGetBootFile (
     HttpFreeMsgParser (Parser);\r
   }\r
 \r
-  return EFI_SUCCESS;\r
-  \r
+  return Status;\r
+\r
 ERROR_6:\r
   if (Parser != NULL) {\r
     HttpFreeMsgParser (Parser);\r
@@ -999,7 +1287,7 @@ ERROR_6:
     FreePool (Context.Block);\r
   }\r
   HttpBootFreeCache (Cache);\r
-  \r
+\r
 ERROR_5:\r
   if (ResponseData != NULL) {\r
     FreePool (ResponseData);\r
@@ -1021,3 +1309,4 @@ ERROR_1:
 \r
   return Status;\r
 }\r
+\r