]> git.proxmox.com Git - mirror_edk2.git/blobdiff - NetworkPkg/HttpBootDxe/HttpBootImpl.c
UefiCpuPkg: Move AsmRelocateApLoopStart from Mpfuncs.nasm to AmdSev.nasm
[mirror_edk2.git] / NetworkPkg / HttpBootDxe / HttpBootImpl.c
index cf643d86bc1936df146739c9ef43bd028445aed9..b4c61925b94f83d6eb1e778caf13a07ff3ea76d5 100644 (file)
@@ -1,23 +1,96 @@
 /** @file\r
   The implementation of EFI_LOAD_FILE_PROTOCOL for UEFI HTTP boot.\r
 \r
-Copyright (c) 2015 - 2016, 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
+Copyright (c) 2015 - 2018, Intel Corporation. All rights reserved.<BR>\r
+(C) Copyright 2016 Hewlett Packard Enterprise Development LP<BR>\r
+SPDX-License-Identifier: BSD-2-Clause-Patent\r
 \r
 **/\r
 \r
 #include "HttpBootDxe.h"\r
 \r
+/**\r
+  Install HTTP Boot Callback Protocol if not installed before.\r
+\r
+  @param[in] Private           Pointer to HTTP Boot private data.\r
+\r
+  @retval EFI_SUCCESS          HTTP Boot Callback Protocol installed successfully.\r
+  @retval Others               Failed to install HTTP Boot Callback Protocol.\r
+\r
+**/\r
+EFI_STATUS\r
+HttpBootInstallCallback (\r
+  IN HTTP_BOOT_PRIVATE_DATA  *Private\r
+  )\r
+{\r
+  EFI_STATUS  Status;\r
+  EFI_HANDLE  ControllerHandle;\r
+\r
+  if (!Private->UsingIpv6) {\r
+    ControllerHandle = Private->Ip4Nic->Controller;\r
+  } else {\r
+    ControllerHandle = Private->Ip6Nic->Controller;\r
+  }\r
+\r
+  //\r
+  // Check whether gEfiHttpBootCallbackProtocolGuid already installed.\r
+  //\r
+  Status = gBS->HandleProtocol (\r
+                  ControllerHandle,\r
+                  &gEfiHttpBootCallbackProtocolGuid,\r
+                  (VOID **)&Private->HttpBootCallback\r
+                  );\r
+  if (Status == EFI_UNSUPPORTED) {\r
+    CopyMem (\r
+      &Private->LoadFileCallback,\r
+      &gHttpBootDxeHttpBootCallback,\r
+      sizeof (EFI_HTTP_BOOT_CALLBACK_PROTOCOL)\r
+      );\r
+\r
+    //\r
+    // Install a default callback if user didn't offer one.\r
+    //\r
+    Status = gBS->InstallProtocolInterface (\r
+                    &ControllerHandle,\r
+                    &gEfiHttpBootCallbackProtocolGuid,\r
+                    EFI_NATIVE_INTERFACE,\r
+                    &Private->LoadFileCallback\r
+                    );\r
+    if (EFI_ERROR (Status)) {\r
+      return Status;\r
+    }\r
+\r
+    Private->HttpBootCallback = &Private->LoadFileCallback;\r
+  }\r
+\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+/**\r
+  Uninstall HTTP Boot Callback Protocol if it's installed by this driver.\r
+\r
+  @param[in] Private           Pointer to HTTP Boot private data.\r
+\r
+**/\r
+VOID\r
+HttpBootUninstallCallback (\r
+  IN HTTP_BOOT_PRIVATE_DATA  *Private\r
+  )\r
+{\r
+  if (Private->HttpBootCallback == &Private->LoadFileCallback) {\r
+    gBS->UninstallProtocolInterface (\r
+           Private->Controller,\r
+           &gEfiHttpBootCallbackProtocolGuid,\r
+           &Private->HttpBootCallback\r
+           );\r
+    Private->HttpBootCallback = NULL;\r
+  }\r
+}\r
+\r
 /**\r
   Enable the use of UEFI HTTP boot function.\r
 \r
-  If the driver has already been started but not satisfy the requirement (IP stack and \r
+  If the driver has already been started but not satisfy the requirement (IP stack and\r
   specified boot file path), this function will stop the driver and start it again.\r
 \r
   @param[in]    Private            The pointer to the driver's private data.\r
@@ -31,33 +104,34 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
   @retval EFI_INVALID_PARAMETER    The FilePath doesn't contain a valid URI device path node.\r
   @retval EFI_ALREADY_STARTED      The driver is already in started state.\r
   @retval EFI_OUT_OF_RESOURCES     There are not enough resources.\r
-  \r
+\r
 **/\r
 EFI_STATUS\r
 HttpBootStart (\r
-  IN HTTP_BOOT_PRIVATE_DATA           *Private,\r
-  IN BOOLEAN                          UsingIpv6,\r
-  IN EFI_DEVICE_PATH_PROTOCOL         *FilePath\r
+  IN HTTP_BOOT_PRIVATE_DATA    *Private,\r
+  IN BOOLEAN                   UsingIpv6,\r
+  IN EFI_DEVICE_PATH_PROTOCOL  *FilePath\r
   )\r
 {\r
-  UINTN                Index;\r
-  EFI_STATUS           Status;\r
-  CHAR8                *Uri;\r
-  \r
+  UINTN       Index;\r
+  EFI_STATUS  Status;\r
+  CHAR8       *Uri;\r
 \r
-  if (Private == NULL || FilePath == NULL) {\r
+  Uri = NULL;\r
+\r
+  if ((Private == NULL) || (FilePath == NULL)) {\r
     return EFI_INVALID_PARAMETER;\r
   }\r
-  \r
+\r
   //\r
   // Check the URI in the input FilePath, in order to see whether it is\r
-  // required to boot from a new specified boot file. \r
+  // required to boot from a new specified boot file.\r
   //\r
   Status = HttpBootParseFilePath (FilePath, &Uri);\r
   if (EFI_ERROR (Status)) {\r
     return EFI_INVALID_PARAMETER;\r
   }\r
-  \r
+\r
   //\r
   // Check whether we need to stop and restart the HTTP boot driver.\r
   //\r
@@ -68,13 +142,18 @@ HttpBootStart (
     // 2. The specified boot file URI in FilePath is different with the one we have\r
     // recorded before.\r
     //\r
-    if ((UsingIpv6 != Private->UsingIpv6) || \r
-        ((Uri != NULL) && (AsciiStrCmp (Private->BootFileUri, Uri) != 0))) {\r
+    if ((UsingIpv6 != Private->UsingIpv6) ||\r
+        ((Uri != NULL) && (AsciiStrCmp (Private->BootFileUri, Uri) != 0)))\r
+    {\r
       //\r
       // Restart is required, first stop then continue this start function.\r
       //\r
       Status = HttpBootStop (Private);\r
       if (EFI_ERROR (Status)) {\r
+        if (Uri != NULL) {\r
+          FreePool (Uri);\r
+        }\r
+\r
         return Status;\r
       }\r
     } else {\r
@@ -84,6 +163,7 @@ HttpBootStart (
       if (Uri != NULL) {\r
         FreePool (Uri);\r
       }\r
+\r
       return EFI_ALREADY_STARTED;\r
     }\r
   }\r
@@ -91,14 +171,15 @@ HttpBootStart (
   //\r
   // Detect whether using ipv6 or not, and set it to the private data.\r
   //\r
-  if (UsingIpv6 && Private->Ip6Nic != NULL) {\r
+  if (UsingIpv6 && (Private->Ip6Nic != NULL)) {\r
     Private->UsingIpv6 = TRUE;\r
-  } else if (!UsingIpv6 && Private->Ip4Nic != NULL) {\r
+  } else if (!UsingIpv6 && (Private->Ip4Nic != NULL)) {\r
     Private->UsingIpv6 = FALSE;\r
   } else {\r
     if (Uri != NULL) {\r
       FreePool (Uri);\r
     }\r
+\r
     return EFI_UNSUPPORTED;\r
   }\r
 \r
@@ -109,7 +190,7 @@ HttpBootStart (
   if (Private->FilePathUri != NULL) {\r
     Status = HttpParseUrl (\r
                Private->FilePathUri,\r
-               (UINT32) AsciiStrLen (Private->FilePathUri),\r
+               (UINT32)AsciiStrLen (Private->FilePathUri),\r
                FALSE,\r
                &Private->FilePathUriParser\r
                );\r
@@ -118,18 +199,18 @@ HttpBootStart (
       return Status;\r
     }\r
   }\r
-  \r
+\r
   //\r
   // Init the content of cached DHCP offer list.\r
   //\r
   ZeroMem (Private->OfferBuffer, sizeof (Private->OfferBuffer));\r
   if (!Private->UsingIpv6) {\r
     for (Index = 0; Index < HTTP_BOOT_OFFER_MAX_NUM; Index++) {\r
-      Private->OfferBuffer[Index].Dhcp4.Packet.Offer.Size = HTTP_BOOT_DHCP4_PACKET_MAX_SIZE;\r
+      Private->OfferBuffer[Index].Dhcp4.Packet.Offer.Size = HTTP_CACHED_DHCP4_PACKET_MAX_SIZE;\r
     }\r
   } else {\r
     for (Index = 0; Index < HTTP_BOOT_OFFER_MAX_NUM; Index++) {\r
-      Private->OfferBuffer[Index].Dhcp6.Packet.Offer.Size = HTTP_BOOT_DHCP6_PACKET_MAX_SIZE;\r
+      Private->OfferBuffer[Index].Dhcp6.Packet.Offer.Size = HTTP_CACHED_DHCP6_PACKET_MAX_SIZE;\r
     }\r
   }\r
 \r
@@ -142,7 +223,9 @@ HttpBootStart (
       return Status;\r
     }\r
   }\r
-  Private->Started   = TRUE;\r
+\r
+  Private->Started = TRUE;\r
+  Print (L"\n>>Start HTTP Boot over IPv%d", Private->UsingIpv6 ? 6 : 4);\r
 \r
   return EFI_SUCCESS;\r
 }\r
@@ -157,19 +240,19 @@ HttpBootStart (
   @retval EFI_NOT_STARTED          The driver is in stopped state.\r
   @retval EFI_DEVICE_ERROR         An unexpected network error occurred.\r
   @retval Others                   Other errors as indicated.\r
-  \r
+\r
 **/\r
 EFI_STATUS\r
 HttpBootDhcp (\r
-  IN HTTP_BOOT_PRIVATE_DATA           *Private\r
+  IN HTTP_BOOT_PRIVATE_DATA  *Private\r
   )\r
 {\r
-  EFI_STATUS                Status;\r
+  EFI_STATUS  Status;\r
 \r
   if (Private == NULL) {\r
     return EFI_INVALID_PARAMETER;\r
   }\r
-  \r
+\r
   if (!Private->Started) {\r
     return EFI_NOT_STARTED;\r
   }\r
@@ -182,7 +265,7 @@ HttpBootDhcp (
     //\r
     Status = HttpBootDhcp4Dora (Private);\r
   } else {\r
-     //\r
+    //\r
     // Start S.A.R.R process to get a IPv6 address and other boot information.\r
     //\r
     Status = HttpBootDhcp6Sarr (Private);\r
@@ -208,35 +291,38 @@ HttpBootDhcp (
   @retval EFI_INVALID_PARAMETER       Private is NULL, or ImageType is NULL, or BufferSize is NULL.\r
   @retval EFI_INVALID_PARAMETER       *BufferSize is not zero, and Buffer is NULL.\r
   @retval EFI_NOT_STARTED             The driver is in stopped state.\r
-  @retval EFI_BUFFER_TOO_SMALL        The BufferSize is too small to read the boot file. BufferSize has \r
+  @retval EFI_BUFFER_TOO_SMALL        The BufferSize is too small to read the boot file. BufferSize has\r
                                       been updated with the size needed to complete the request.\r
   @retval EFI_DEVICE_ERROR            An unexpected network error occurred.\r
   @retval Others                      Other errors as indicated.\r
-  \r
+\r
 **/\r
 EFI_STATUS\r
 HttpBootLoadFile (\r
-  IN     HTTP_BOOT_PRIVATE_DATA       *Private,\r
-  IN OUT UINTN                        *BufferSize,\r
-  IN     VOID                         *Buffer,       OPTIONAL\r
-     OUT HTTP_BOOT_IMAGE_TYPE         *ImageType\r
+  IN     HTTP_BOOT_PRIVATE_DATA  *Private,\r
+  IN OUT UINTN                   *BufferSize,\r
+  IN     VOID                    *Buffer        OPTIONAL,\r
+  OUT HTTP_BOOT_IMAGE_TYPE       *ImageType\r
   )\r
 {\r
-  EFI_STATUS             Status;\r
+  EFI_STATUS  Status;\r
 \r
-  if (Private == NULL || ImageType == NULL || BufferSize == NULL ) {\r
+  if ((Private == NULL) || (ImageType == NULL) || (BufferSize == NULL)) {\r
     return EFI_INVALID_PARAMETER;\r
   }\r
 \r
-  if (*BufferSize != 0 && Buffer == NULL) {\r
+  if ((*BufferSize != 0) && (Buffer == NULL)) {\r
     return EFI_INVALID_PARAMETER;\r
   }\r
-  \r
+\r
   if (!Private->Started) {\r
     return EFI_NOT_STARTED;\r
   }\r
 \r
-  Status = EFI_DEVICE_ERROR;\r
+  Status = HttpBootInstallCallback (Private);\r
+  if (EFI_ERROR (Status)) {\r
+    goto ON_EXIT;\r
+  }\r
 \r
   if (Private->BootFileUri == NULL) {\r
     //\r
@@ -244,7 +330,8 @@ HttpBootLoadFile (
     //\r
     Status = HttpBootDiscoverBootInfo (Private);\r
     if (EFI_ERROR (Status)) {\r
-      return Status;\r
+      AsciiPrint ("\n  Error: Could not retrieve NBP file size from HTTP server.\n");\r
+      goto ON_EXIT;\r
     }\r
   }\r
 \r
@@ -254,7 +341,7 @@ HttpBootLoadFile (
     //\r
     Status = HttpBootCreateHttpIo (Private);\r
     if (EFI_ERROR (Status)) {\r
-      return Status;\r
+      goto ON_EXIT;\r
     }\r
   }\r
 \r
@@ -271,9 +358,20 @@ HttpBootLoadFile (
                TRUE,\r
                &Private->BootFileSize,\r
                NULL,\r
-               ImageType\r
+               &Private->ImageType\r
                );\r
-    if (EFI_ERROR (Status) && Status != EFI_BUFFER_TOO_SMALL) {\r
+    if ((Private->AuthData != NULL) && (Status == EFI_ACCESS_DENIED)) {\r
+      //\r
+      // Try to use HTTP HEAD method again since the Authentication information is provided.\r
+      //\r
+      Status = HttpBootGetBootFile (\r
+                 Private,\r
+                 TRUE,\r
+                 &Private->BootFileSize,\r
+                 NULL,\r
+                 &Private->ImageType\r
+                 );\r
+    } else if ((EFI_ERROR (Status)) && (Status != EFI_BUFFER_TOO_SMALL)) {\r
       //\r
       // Failed to get file size by HEAD method, may be trunked encoding, try HTTP GET method.\r
       //\r
@@ -283,29 +381,55 @@ HttpBootLoadFile (
                  FALSE,\r
                  &Private->BootFileSize,\r
                  NULL,\r
-                 ImageType\r
+                 &Private->ImageType\r
                  );\r
-      if (EFI_ERROR (Status) && Status != EFI_BUFFER_TOO_SMALL) {\r
-        return Status;\r
+      if (EFI_ERROR (Status) && (Status != EFI_BUFFER_TOO_SMALL)) {\r
+        AsciiPrint ("\n  Error: Could not retrieve NBP file size from HTTP server.\n");\r
+        goto ON_EXIT;\r
       }\r
     }\r
   }\r
 \r
   if (*BufferSize < Private->BootFileSize) {\r
     *BufferSize = Private->BootFileSize;\r
-    return EFI_BUFFER_TOO_SMALL;\r
+    *ImageType  = Private->ImageType;\r
+    Status      = EFI_BUFFER_TOO_SMALL;\r
+    goto ON_EXIT;\r
   }\r
 \r
   //\r
   // Load the boot file into Buffer\r
   //\r
-  return  HttpBootGetBootFile (\r
-            Private,\r
-            FALSE,\r
-            BufferSize,\r
-            Buffer,\r
-            ImageType\r
-            );\r
+  Status = HttpBootGetBootFile (\r
+             Private,\r
+             FALSE,\r
+             BufferSize,\r
+             Buffer,\r
+             ImageType\r
+             );\r
+\r
+ON_EXIT:\r
+  HttpBootUninstallCallback (Private);\r
+\r
+  if (EFI_ERROR (Status)) {\r
+    if (Status == EFI_ACCESS_DENIED) {\r
+      AsciiPrint ("\n  Error: Could not establish connection with HTTP server.\n");\r
+    } else if ((Status == EFI_BUFFER_TOO_SMALL) && (Buffer != NULL)) {\r
+      AsciiPrint ("\n  Error: Buffer size is smaller than the requested file.\n");\r
+    } else if (Status == EFI_OUT_OF_RESOURCES) {\r
+      AsciiPrint ("\n  Error: Could not allocate I/O buffers.\n");\r
+    } else if (Status == EFI_DEVICE_ERROR) {\r
+      AsciiPrint ("\n  Error: Network device error.\n");\r
+    } else if (Status == EFI_TIMEOUT) {\r
+      AsciiPrint ("\n  Error: Server response timeout.\n");\r
+    } else if (Status == EFI_ABORTED) {\r
+      AsciiPrint ("\n  Error: Remote boot cancelled.\n");\r
+    } else if (Status != EFI_BUFFER_TOO_SMALL) {\r
+      AsciiPrint ("\n  Error: Unexpected network error.\n");\r
+    }\r
+  }\r
+\r
+  return Status;\r
 }\r
 \r
 /**\r
@@ -317,38 +441,38 @@ HttpBootLoadFile (
   @retval EFI_NOT_STARTED          The driver is already in stopped state.\r
   @retval EFI_INVALID_PARAMETER    Private is NULL.\r
   @retval Others                   Unexpected error when stop the function.\r
-  \r
+\r
 **/\r
 EFI_STATUS\r
 HttpBootStop (\r
-  IN HTTP_BOOT_PRIVATE_DATA           *Private\r
+  IN HTTP_BOOT_PRIVATE_DATA  *Private\r
   )\r
 {\r
-  UINTN            Index;\r
+  UINTN  Index;\r
 \r
   if (Private == NULL) {\r
     return EFI_INVALID_PARAMETER;\r
   }\r
-  \r
+\r
   if (!Private->Started) {\r
     return EFI_NOT_STARTED;\r
   }\r
-  \r
+\r
   if (Private->HttpCreated) {\r
     HttpIoDestroyIo (&Private->HttpIo);\r
     Private->HttpCreated = FALSE;\r
   }\r
-  \r
+\r
   Private->Started = FALSE;\r
   ZeroMem (&Private->StationIp, sizeof (EFI_IP_ADDRESS));\r
   ZeroMem (&Private->SubnetMask, sizeof (EFI_IP_ADDRESS));\r
   ZeroMem (&Private->GatewayIp, sizeof (EFI_IP_ADDRESS));\r
-  Private->Port = 0;\r
-  Private->BootFileUri = NULL;\r
+  Private->Port              = 0;\r
+  Private->BootFileUri       = NULL;\r
   Private->BootFileUriParser = NULL;\r
-  Private->BootFileSize = 0;\r
-  Private->SelectIndex = 0;\r
-  Private->SelectProxyType = HttpOfferTypeMax; \r
+  Private->BootFileSize      = 0;\r
+  Private->SelectIndex       = 0;\r
+  Private->SelectProxyType   = HttpOfferTypeMax;\r
 \r
   if (!Private->UsingIpv6) {\r
     //\r
@@ -368,7 +492,7 @@ HttpBootStop (
     //\r
     Private->Dhcp6->Stop (Private->Dhcp6);\r
     Private->Dhcp6->Configure (Private->Dhcp6, NULL);\r
-    \r
+\r
     for (Index = 0; Index < HTTP_BOOT_OFFER_MAX_NUM; Index++) {\r
       if (Private->OfferBuffer[Index].Dhcp6.UriParser) {\r
         HttpUrlFreeParser (Private->OfferBuffer[Index].Dhcp6.UriParser);\r
@@ -376,20 +500,35 @@ HttpBootStop (
     }\r
   }\r
 \r
-  if (Private->FilePathUri!= NULL) {\r
+  if (Private->AuthData != NULL) {\r
+    FreePool (Private->AuthData);\r
+    Private->AuthData = NULL;\r
+  }\r
+\r
+  if (Private->AuthScheme != NULL) {\r
+    FreePool (Private->AuthScheme);\r
+    Private->AuthScheme = NULL;\r
+  }\r
+\r
+  if (Private->DnsServerIp != NULL) {\r
+    FreePool (Private->DnsServerIp);\r
+    Private->DnsServerIp = NULL;\r
+  }\r
+\r
+  if (Private->FilePathUri != NULL) {\r
     FreePool (Private->FilePathUri);\r
     HttpUrlFreeParser (Private->FilePathUriParser);\r
-    Private->FilePathUri = NULL;\r
+    Private->FilePathUri       = NULL;\r
     Private->FilePathUriParser = NULL;\r
   }\r
-  \r
+\r
   ZeroMem (Private->OfferBuffer, sizeof (Private->OfferBuffer));\r
   Private->OfferNum = 0;\r
   ZeroMem (Private->OfferCount, sizeof (Private->OfferCount));\r
   ZeroMem (Private->OfferIndex, sizeof (Private->OfferIndex));\r
-  \r
+\r
   HttpBootFreeCacheList (Private);\r
-  \r
+\r
   return EFI_SUCCESS;\r
 }\r
 \r
@@ -427,21 +566,21 @@ HttpBootStop (
 EFI_STATUS\r
 EFIAPI\r
 HttpBootDxeLoadFile (\r
-  IN EFI_LOAD_FILE_PROTOCOL           *This,\r
-  IN EFI_DEVICE_PATH_PROTOCOL         *FilePath,\r
-  IN BOOLEAN                          BootPolicy,\r
-  IN OUT UINTN                        *BufferSize,\r
-  IN VOID                             *Buffer OPTIONAL\r
+  IN EFI_LOAD_FILE_PROTOCOL    *This,\r
+  IN EFI_DEVICE_PATH_PROTOCOL  *FilePath,\r
+  IN BOOLEAN                   BootPolicy,\r
+  IN OUT UINTN                 *BufferSize,\r
+  IN VOID                      *Buffer OPTIONAL\r
   )\r
 {\r
-  HTTP_BOOT_PRIVATE_DATA        *Private;\r
-  HTTP_BOOT_VIRTUAL_NIC         *VirtualNic;\r
-  BOOLEAN                       MediaPresent;\r
-  BOOLEAN                       UsingIpv6;\r
-  EFI_STATUS                    Status;\r
-  HTTP_BOOT_IMAGE_TYPE          ImageType;\r
-\r
-  if (This == NULL || BufferSize == NULL || FilePath == NULL) {\r
+  HTTP_BOOT_PRIVATE_DATA  *Private;\r
+  HTTP_BOOT_VIRTUAL_NIC   *VirtualNic;\r
+  EFI_STATUS              MediaStatus;\r
+  BOOLEAN                 UsingIpv6;\r
+  EFI_STATUS              Status;\r
+  HTTP_BOOT_IMAGE_TYPE    ImageType;\r
+\r
+  if ((This == NULL) || (BufferSize == NULL) || (FilePath == NULL)) {\r
     return EFI_INVALID_PARAMETER;\r
   }\r
 \r
@@ -453,17 +592,18 @@ HttpBootDxeLoadFile (
   }\r
 \r
   VirtualNic = HTTP_BOOT_VIRTUAL_NIC_FROM_LOADFILE (This);\r
-  Private = VirtualNic->Private;\r
-  \r
+  Private    = VirtualNic->Private;\r
+\r
   //\r
   // Check media status before HTTP boot start\r
   //\r
-  MediaPresent = TRUE;\r
-  NetLibDetectMedia (Private->Controller, &MediaPresent);\r
-  if (!MediaPresent) {\r
+  MediaStatus = EFI_SUCCESS;\r
+  NetLibDetectMediaWaitTimeout (Private->Controller, HTTP_BOOT_CHECK_MEDIA_WAITING_TIME, &MediaStatus);\r
+  if (MediaStatus != EFI_SUCCESS) {\r
+    AsciiPrint ("\n  Error: Could not detect network connection.\n");\r
     return EFI_NO_MEDIA;\r
   }\r
-  \r
+\r
   //\r
   // Check whether the virtual nic is using IPv6 or not.\r
   //\r
@@ -476,41 +616,185 @@ HttpBootDxeLoadFile (
   // Initialize HTTP boot.\r
   //\r
   Status = HttpBootStart (Private, UsingIpv6, FilePath);\r
-  if (Status != EFI_SUCCESS && Status != EFI_ALREADY_STARTED) {\r
+  if ((Status != EFI_SUCCESS) && (Status != EFI_ALREADY_STARTED)) {\r
     return Status;\r
   }\r
-  \r
+\r
   //\r
   // Load the boot file.\r
   //\r
   ImageType = ImageTypeMax;\r
-  Status = HttpBootLoadFile (Private, BufferSize, Buffer, &ImageType);\r
+  Status    = HttpBootLoadFile (Private, BufferSize, Buffer, &ImageType);\r
   if (EFI_ERROR (Status)) {\r
-    if (Status == EFI_BUFFER_TOO_SMALL && (ImageType == ImageTypeVirtualCd || ImageType == ImageTypeVirtualDisk)) {\r
+    if ((Status == EFI_BUFFER_TOO_SMALL) && ((ImageType == ImageTypeVirtualCd) || (ImageType == ImageTypeVirtualDisk))) {\r
       Status = EFI_WARN_FILE_SYSTEM;\r
     } else if (Status != EFI_BUFFER_TOO_SMALL) {\r
       HttpBootStop (Private);\r
     }\r
+\r
     return Status;\r
   }\r
 \r
   //\r
   // Register the RAM Disk to the system if needed.\r
   //\r
-  if (ImageType == ImageTypeVirtualCd || ImageType == ImageTypeVirtualDisk) {\r
+  if ((ImageType == ImageTypeVirtualCd) || (ImageType == ImageTypeVirtualDisk)) {\r
     Status = HttpBootRegisterRamDisk (Private, *BufferSize, Buffer, ImageType);\r
     if (!EFI_ERROR (Status)) {\r
       Status = EFI_WARN_FILE_SYSTEM;\r
+    } else {\r
+      AsciiPrint ("\n  Error: Could not register RAM disk to the system.\n");\r
     }\r
   }\r
-  \r
+\r
+  //\r
+  // Stop the HTTP Boot service after the boot image is downloaded.\r
+  //\r
+  HttpBootStop (Private);\r
   return Status;\r
 }\r
 \r
 ///\r
 /// Load File Protocol instance\r
 ///\r
-GLOBAL_REMOVE_IF_UNREFERENCED \r
+GLOBAL_REMOVE_IF_UNREFERENCED\r
 EFI_LOAD_FILE_PROTOCOL  gHttpBootDxeLoadFile = {\r
   HttpBootDxeLoadFile\r
 };\r
+\r
+/**\r
+  Callback function that is invoked when the HTTP Boot driver is about to transmit or has received a\r
+  packet.\r
+\r
+  This function is invoked when the HTTP Boot driver is about to transmit or has received packet.\r
+  Parameters DataType and Received specify the type of event and the format of the buffer pointed\r
+  to by Data. Due to the polling nature of UEFI device drivers, this callback function should not\r
+  execute for more than 5 ms.\r
+  The returned status code determines the behavior of the HTTP Boot driver.\r
+\r
+  @param[in]  This                Pointer to the EFI_HTTP_BOOT_CALLBACK_PROTOCOL instance.\r
+  @param[in]  DataType            The event that occurs in the current state.\r
+  @param[in]  Received            TRUE if the callback is being invoked due to a receive event.\r
+                                  FALSE if the callback is being invoked due to a transmit event.\r
+  @param[in]  DataLength          The length in bytes of the buffer pointed to by Data.\r
+  @param[in]  Data                A pointer to the buffer of data, the data type is specified by\r
+                                  DataType.\r
+\r
+  @retval EFI_SUCCESS             Tells the HTTP Boot driver to continue the HTTP Boot process.\r
+  @retval EFI_ABORTED             Tells the HTTP Boot driver to abort the current HTTP Boot process.\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+HttpBootCallback (\r
+  IN EFI_HTTP_BOOT_CALLBACK_PROTOCOL   *This,\r
+  IN EFI_HTTP_BOOT_CALLBACK_DATA_TYPE  DataType,\r
+  IN BOOLEAN                           Received,\r
+  IN UINT32                            DataLength,\r
+  IN VOID                              *Data     OPTIONAL\r
+  )\r
+{\r
+  EFI_HTTP_MESSAGE        *HttpMessage;\r
+  EFI_HTTP_HEADER         *HttpHeader;\r
+  HTTP_BOOT_PRIVATE_DATA  *Private;\r
+  UINT32                  Percentage;\r
+\r
+  Private = HTTP_BOOT_PRIVATE_DATA_FROM_CALLBACK_PROTOCOL (This);\r
+\r
+  switch (DataType) {\r
+    case HttpBootDhcp4:\r
+    case HttpBootDhcp6:\r
+      Print (L".");\r
+      break;\r
+\r
+    case HttpBootHttpRequest:\r
+      if (Data != NULL) {\r
+        HttpMessage = (EFI_HTTP_MESSAGE *)Data;\r
+        if ((HttpMessage->Data.Request->Method == HttpMethodGet) &&\r
+            (HttpMessage->Data.Request->Url != NULL))\r
+        {\r
+          Print (L"\n  URI: %s\n", HttpMessage->Data.Request->Url);\r
+        }\r
+      }\r
+\r
+      break;\r
+\r
+    case HttpBootHttpResponse:\r
+      if (Data != NULL) {\r
+        HttpMessage = (EFI_HTTP_MESSAGE *)Data;\r
+\r
+        if (HttpMessage->Data.Response != NULL) {\r
+          if (HttpBootIsHttpRedirectStatusCode (HttpMessage->Data.Response->StatusCode)) {\r
+            //\r
+            // Server indicates the resource has been redirected to a different URL\r
+            // according to the section 6.4 of RFC7231 and the RFC 7538.\r
+            // Display the redirect information on the screen.\r
+            //\r
+            HttpHeader = HttpFindHeader (\r
+                           HttpMessage->HeaderCount,\r
+                           HttpMessage->Headers,\r
+                           HTTP_HEADER_LOCATION\r
+                           );\r
+            if (HttpHeader != NULL) {\r
+              Print (L"\n  HTTP ERROR: Resource Redirected.\n  New Location: %a\n", HttpHeader->FieldValue);\r
+            }\r
+\r
+            break;\r
+          }\r
+        }\r
+\r
+        HttpHeader = HttpFindHeader (\r
+                       HttpMessage->HeaderCount,\r
+                       HttpMessage->Headers,\r
+                       HTTP_HEADER_CONTENT_LENGTH\r
+                       );\r
+        if (HttpHeader != NULL) {\r
+          Private->FileSize     = AsciiStrDecimalToUintn (HttpHeader->FieldValue);\r
+          Private->ReceivedSize = 0;\r
+          Private->Percentage   = 0;\r
+        }\r
+      }\r
+\r
+      break;\r
+\r
+    case HttpBootHttpEntityBody:\r
+      if (DataLength != 0) {\r
+        if (Private->FileSize != 0) {\r
+          //\r
+          // We already know the file size, print in percentage format.\r
+          //\r
+          if (Private->ReceivedSize == 0) {\r
+            Print (L"  File Size: %lu Bytes\n", Private->FileSize);\r
+          }\r
+\r
+          Private->ReceivedSize += DataLength;\r
+          Percentage             = (UINT32)DivU64x64Remainder (MultU64x32 (Private->ReceivedSize, 100), Private->FileSize, NULL);\r
+          if (Private->Percentage != Percentage) {\r
+            Private->Percentage = Percentage;\r
+            Print (L"\r  Downloading...%d%%", Percentage);\r
+          }\r
+        } else {\r
+          //\r
+          // In some case we couldn't get the file size from the HTTP header, so we\r
+          // just print the downloaded file size.\r
+          //\r
+          Private->ReceivedSize += DataLength;\r
+          Print (L"\r  Downloading...%lu Bytes", Private->ReceivedSize);\r
+        }\r
+      }\r
+\r
+      break;\r
+\r
+    default:\r
+      break;\r
+  }\r
+\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+///\r
+/// HTTP Boot Callback Protocol instance\r
+///\r
+GLOBAL_REMOVE_IF_UNREFERENCED\r
+EFI_HTTP_BOOT_CALLBACK_PROTOCOL  gHttpBootDxeHttpBootCallback = {\r
+  HttpBootCallback\r
+};\r