]> git.proxmox.com Git - mirror_edk2.git/blobdiff - NetworkPkg/HttpBootDxe/HttpBootSupport.c
NetworkPkg/HttpBootDxe: Add HTTP Boot Callback protocol support.
[mirror_edk2.git] / NetworkPkg / HttpBootDxe / HttpBootSupport.c
index 66eca783af712e13457bc79ab9884b35e5a62b23..5024f2e67672e34f7a143585c94c101bd20de6ec 100644 (file)
@@ -1,7 +1,7 @@
 /** @file\r
   Support functions implementation for UEFI HTTP boot driver.\r
 \r
-Copyright (c) 2015 - 2016, Intel Corporation. All rights reserved.<BR>\r
+Copyright (c) 2015 - 2017, 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
@@ -86,11 +86,10 @@ HttpBootUintnToAscDecWithFormat (
 {\r
   UINTN                          Remainder;\r
 \r
-  while (Length > 0) {\r
-    Length--;\r
+  for (; Length > 0; Length--) {\r
     Remainder      = Number % 10;\r
     Number        /= 10;\r
-    Buffer[Length] = (UINT8) ('0' + Remainder);\r
+    Buffer[Length - 1] = (UINT8) ('0' + Remainder);\r
   }\r
 }\r
 \r
@@ -187,6 +186,10 @@ HttpBootPrintErrorMessage (
 \r
   case HTTP_STATUS_307_TEMPORARY_REDIRECT:\r
     AsciiPrint ("\n  Redirection: 307 Temporary Redirect");\r
+    break;\r
+\r
+  case HTTP_STATUS_308_PERMANENT_REDIRECT:\r
+    AsciiPrint ("\n  Redirection: 308 Permanent Redirect");\r
     break; \r
 \r
   case HTTP_STATUS_400_BAD_REQUEST:\r
@@ -625,6 +628,41 @@ HttpBootSetHeader (
   return EFI_SUCCESS;\r
 }\r
 \r
+/**\r
+  Notify the callback function when an event is triggered.\r
+\r
+  @param[in]  Context         The opaque parameter to the function.\r
+\r
+**/\r
+VOID\r
+EFIAPI\r
+HttpIoNotifyDpc (\r
+  IN VOID                *Context\r
+  )\r
+{\r
+  *((BOOLEAN *) Context) = TRUE;\r
+}\r
+\r
+/**\r
+  Request HttpIoNotifyDpc as a DPC at TPL_CALLBACK.\r
+\r
+  @param[in]  Event                 The event signaled.\r
+  @param[in]  Context               The opaque parameter to the function.\r
+\r
+**/\r
+VOID\r
+EFIAPI\r
+HttpIoNotify (\r
+  IN EFI_EVENT              Event,\r
+  IN VOID                   *Context\r
+  )\r
+{\r
+  //\r
+  // Request HttpIoNotifyDpc as a DPC at TPL_CALLBACK\r
+  //\r
+  QueueDpc (TPL_CALLBACK, HttpIoNotifyDpc, Context);\r
+}\r
+\r
 /**\r
   Create a HTTP_IO to access the HTTP service. It will create and configure\r
   a HTTP child handle.\r
@@ -633,6 +671,9 @@ HttpBootSetHeader (
   @param[in]  Controller     The handle of the controller.\r
   @param[in]  IpVersion      IP_VERSION_4 or IP_VERSION_6.\r
   @param[in]  ConfigData     The HTTP_IO configuration data.\r
+  @param[in]  Callback       Callback function which will be invoked when specified\r
+                             HTTP_IO_CALLBACK_EVENT happened.\r
+  @param[in]  Context        The Context data which will be passed to the Callback function.\r
   @param[out] HttpIo         The HTTP_IO.\r
   \r
   @retval EFI_SUCCESS            The HTTP_IO is created and configured.\r
@@ -649,6 +690,8 @@ HttpIoCreateIo (
   IN EFI_HANDLE             Controller,\r
   IN UINT8                  IpVersion,\r
   IN HTTP_IO_CONFIG_DATA    *ConfigData,\r
+  IN HTTP_IO_CALLBACK       Callback,\r
+  IN VOID                   *Context,\r
   OUT HTTP_IO               *HttpIo\r
   )\r
 {\r
@@ -701,6 +744,8 @@ HttpIoCreateIo (
   HttpIo->Controller  = Controller;\r
   HttpIo->IpVersion   = IpVersion;\r
   HttpIo->Http        = Http;\r
+  HttpIo->Callback    = Callback;\r
+  HttpIo->Context     = Context;\r
 \r
   ZeroMem (&HttpConfigData, sizeof (EFI_HTTP_CONFIG_DATA));\r
   HttpConfigData.HttpVersion        = HttpVersion11;\r
@@ -731,7 +776,7 @@ HttpIoCreateIo (
   Status = gBS->CreateEvent (\r
                   EVT_NOTIFY_SIGNAL,\r
                   TPL_NOTIFY,\r
-                  HttpBootCommonNotify,\r
+                  HttpIoNotify,\r
                   &HttpIo->IsTxDone,\r
                   &Event\r
                   );\r
@@ -744,7 +789,7 @@ HttpIoCreateIo (
   Status = gBS->CreateEvent (\r
                   EVT_NOTIFY_SIGNAL,\r
                   TPL_NOTIFY,\r
-                  HttpBootCommonNotify,\r
+                  HttpIoNotify,\r
                   &HttpIo->IsRxDone,\r
                   &Event\r
                   );\r
@@ -754,6 +799,21 @@ HttpIoCreateIo (
   HttpIo->RspToken.Event = Event;\r
   HttpIo->RspToken.Message = &HttpIo->RspMessage;\r
 \r
+  //\r
+  // Create TimeoutEvent for response\r
+  //\r
+  Status = gBS->CreateEvent (\r
+                  EVT_TIMER,\r
+                  TPL_CALLBACK,\r
+                  NULL,\r
+                  NULL,\r
+                  &Event\r
+                  );\r
+  if (EFI_ERROR (Status)) {\r
+    goto ON_ERROR;\r
+  }\r
+  HttpIo->TimeoutEvent = Event;\r
+\r
   return EFI_SUCCESS;\r
   \r
 ON_ERROR:\r
@@ -789,6 +849,11 @@ HttpIoDestroyIo (
   if (Event != NULL) {\r
     gBS->CloseEvent (Event);\r
   }\r
+\r
+  Event = HttpIo->TimeoutEvent;\r
+  if (Event != NULL) {\r
+    gBS->CloseEvent (Event);\r
+  }\r
   \r
   Http = HttpIo->Http;\r
   if (Http != NULL) {\r
@@ -850,6 +915,17 @@ HttpIoSendRequest (
   HttpIo->ReqToken.Message->BodyLength   = BodyLength;\r
   HttpIo->ReqToken.Message->Body         = Body;\r
 \r
+  if (HttpIo->Callback != NULL) {\r
+    Status = HttpIo->Callback (\r
+               HttpIoRequest,\r
+               HttpIo->ReqToken.Message,\r
+               HttpIo->Context\r
+               );\r
+    if (EFI_ERROR (Status)) {\r
+      return Status;\r
+    }\r
+  }\r
+\r
   //\r
   // Queue the request token to HTTP instances.\r
   //\r
@@ -902,6 +978,14 @@ HttpIoRecvResponse (
     return EFI_INVALID_PARAMETER;\r
   }\r
 \r
+  //\r
+  // Start the timer, and wait Timeout seconds to receive the header packet.\r
+  //\r
+  Status = gBS->SetTimer (HttpIo->TimeoutEvent, TimerRelative, HTTP_BOOT_RESPONSE_TIMEOUT * TICKS_PER_MS);\r
+  if (EFI_ERROR (Status)) {\r
+    return Status;\r
+  }\r
+\r
   //\r
   // Queue the response token to HTTP instances.\r
   //\r
@@ -924,16 +1008,43 @@ HttpIoRecvResponse (
                    );\r
   \r
   if (EFI_ERROR (Status)) {\r
+    gBS->SetTimer (HttpIo->TimeoutEvent, TimerCancel, 0);\r
     return Status;\r
   }\r
 \r
   //\r
   // Poll the network until receive finish.\r
   //\r
-  while (!HttpIo->IsRxDone) {\r
+  while (!HttpIo->IsRxDone && ((HttpIo->TimeoutEvent == NULL) || EFI_ERROR (gBS->CheckEvent (HttpIo->TimeoutEvent)))) {\r
     Http->Poll (Http);\r
   }\r
 \r
+  gBS->SetTimer (HttpIo->TimeoutEvent, TimerCancel, 0);\r
+\r
+  if (!HttpIo->IsRxDone) {\r
+    //\r
+    // Timeout occurs, cancel the response token.\r
+    //\r
+    Http->Cancel (Http, &HttpIo->RspToken);\r
+   \r
+    Status = EFI_TIMEOUT;\r
+    \r
+    return Status;\r
+  } else {\r
+    HttpIo->IsRxDone = FALSE;\r
+  }\r
+\r
+  if (!EFI_ERROR (HttpIo->RspToken.Status) && HttpIo->Callback != NULL) {\r
+    Status = HttpIo->Callback (\r
+               HttpIoResponse,\r
+               HttpIo->RspToken.Message,\r
+               HttpIo->Context\r
+               );\r
+    if (EFI_ERROR (Status)) {\r
+      return Status;\r
+    }\r
+  }\r
+\r
   //\r
   // Store the received data into the wrapper.\r
   //\r
@@ -945,6 +1056,57 @@ HttpIoRecvResponse (
   return Status;\r
 }\r
 \r
+/**\r
+  This function checks the HTTP(S) URI scheme.\r
+\r
+  @param[in]    Uri              The pointer to the URI string.\r
+  \r
+  @retval EFI_SUCCESS            The URI scheme is valid.\r
+  @retval EFI_INVALID_PARAMETER  The URI scheme is not HTTP or HTTPS.\r
+  @retval EFI_ACCESS_DENIED      HTTP is disabled and the URI is HTTP.\r
+\r
+**/\r
+EFI_STATUS\r
+HttpBootCheckUriScheme (\r
+  IN      CHAR8                  *Uri\r
+  )\r
+{\r
+  UINTN                Index;\r
+  EFI_STATUS           Status;\r
+\r
+  Status = EFI_SUCCESS;\r
+\r
+  //\r
+  // Convert the scheme to all lower case.\r
+  //\r
+  for (Index = 0; Index < AsciiStrLen (Uri); Index++) {\r
+    if (Uri[Index] == ':') {\r
+      break;\r
+    }\r
+    if (Uri[Index] >= 'A' && Uri[Index] <= 'Z') {\r
+      Uri[Index] -= (CHAR8)('A' - 'a');\r
+    }\r
+  }\r
+\r
+  //\r
+  // Return EFI_INVALID_PARAMETER if the URI is not HTTP or HTTPS.\r
+  //\r
+  if ((AsciiStrnCmp (Uri, "http://", 7) != 0) && (AsciiStrnCmp (Uri, "https://", 8) != 0)) {\r
+    DEBUG ((EFI_D_ERROR, "HttpBootCheckUriScheme: Invalid Uri.\n"));\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+  \r
+  //\r
+  // HTTP is disabled, return EFI_ACCESS_DENIED if the URI is HTTP.\r
+  //\r
+  if (!PcdGetBool (PcdAllowHttpConnections) && (AsciiStrnCmp (Uri, "http://", 7) == 0)) {\r
+    DEBUG ((EFI_D_ERROR, "HttpBootCheckUriScheme: HTTP is disabled.\n"));\r
+    return EFI_ACCESS_DENIED;\r
+  }\r
+\r
+  return Status;\r
+}\r
+\r
 /**\r
   Get the URI address string from the input device path.\r
 \r
@@ -1049,13 +1211,21 @@ HttpBootCheckImageType (
 \r
   //\r
   // Determine the image type by the HTTP Content-Type header field first.\r
-  //   "application/efi" -> EFI Image\r
+  //   "application/efi"         -> EFI Image\r
+  //   "application/vnd.efi-iso" -> CD/DVD Image\r
+  //   "application/vnd.efi-img" -> Virtual Disk Image\r
   //\r
   Header = HttpFindHeader (HeaderCount, Headers, HTTP_HEADER_CONTENT_TYPE);\r
   if (Header != NULL) {\r
     if (AsciiStriCmp (Header->FieldValue, HTTP_CONTENT_TYPE_APP_EFI) == 0) {\r
       *ImageType = ImageTypeEfi;\r
       return EFI_SUCCESS;\r
+    } else if (AsciiStriCmp (Header->FieldValue, HTTP_CONTENT_TYPE_APP_ISO) == 0) {\r
+      *ImageType = ImageTypeVirtualCd;\r
+      return EFI_SUCCESS;\r
+    } else if (AsciiStriCmp (Header->FieldValue, HTTP_CONTENT_TYPE_APP_IMG) == 0) {\r
+      *ImageType = ImageTypeVirtualDisk;\r
+      return EFI_SUCCESS;\r
     }\r
   }\r
 \r