+\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
+ Caller need to free the buffer in the UriAddress pointer.\r
+\r
+ @param[in] FilePath Pointer to the device path which contains a URI device path node.\r
+ @param[out] UriAddress The URI address string extract from the device path.\r
+\r
+ @retval EFI_SUCCESS The URI string is returned.\r
+ @retval EFI_OUT_OF_RESOURCES Failed to allocate memory.\r
+\r
+**/\r
+EFI_STATUS\r
+HttpBootParseFilePath (\r
+ IN EFI_DEVICE_PATH_PROTOCOL *FilePath,\r
+ OUT CHAR8 **UriAddress\r
+ )\r
+{\r
+ EFI_DEVICE_PATH_PROTOCOL *TempDevicePath;\r
+ URI_DEVICE_PATH *UriDevicePath;\r
+ CHAR8 *Uri;\r
+ UINTN UriStrLength;\r
+\r
+ if (FilePath == NULL) {\r
+ return EFI_INVALID_PARAMETER;\r
+ }\r
+\r
+ *UriAddress = NULL;\r
+\r
+ //\r
+ // Extract the URI address from the FilePath\r
+ //\r
+ TempDevicePath = FilePath;\r
+ while (!IsDevicePathEnd (TempDevicePath)) {\r
+ if ((DevicePathType (TempDevicePath) == MESSAGING_DEVICE_PATH) &&\r
+ (DevicePathSubType (TempDevicePath) == MSG_URI_DP)) {\r
+ UriDevicePath = (URI_DEVICE_PATH*) TempDevicePath;\r
+ //\r
+ // UEFI Spec doesn't require the URI to be a NULL-terminated string\r
+ // So we allocate a new buffer and always append a '\0' to it.\r
+ //\r
+ UriStrLength = DevicePathNodeLength (UriDevicePath) - sizeof(EFI_DEVICE_PATH_PROTOCOL);\r
+ if (UriStrLength == 0) {\r
+ //\r
+ // return a NULL UriAddress if it's a empty URI device path node.\r
+ //\r
+ break;\r
+ }\r
+ Uri = AllocatePool (UriStrLength + 1);\r
+ if (Uri == NULL) {\r
+ return EFI_OUT_OF_RESOURCES;\r
+ }\r
+ CopyMem (Uri, UriDevicePath->Uri, DevicePathNodeLength (UriDevicePath) - sizeof(EFI_DEVICE_PATH_PROTOCOL));\r
+ Uri[DevicePathNodeLength (UriDevicePath) - sizeof(EFI_DEVICE_PATH_PROTOCOL)] = '\0';\r
+\r
+ *UriAddress = Uri;\r
+ }\r
+ TempDevicePath = NextDevicePathNode (TempDevicePath);\r
+ }\r
+\r
+ return EFI_SUCCESS;\r
+}\r
+\r
+/**\r
+ This function returns the image type according to server replied HTTP message\r
+ and also the image's URI info.\r
+\r
+ @param[in] Uri The pointer to the image's URI string.\r
+ @param[in] UriParser URI Parse result returned by NetHttpParseUrl().\r
+ @param[in] HeaderCount Number of HTTP header structures in Headers list.\r
+ @param[in] Headers Array containing list of HTTP headers.\r
+ @param[out] ImageType The image type of the downloaded file.\r
+\r
+ @retval EFI_SUCCESS The image type is returned in ImageType.\r
+ @retval EFI_INVALID_PARAMETER ImageType, Uri or UriParser is NULL.\r
+ @retval EFI_INVALID_PARAMETER HeaderCount is not zero, and Headers is NULL.\r
+ @retval EFI_NOT_FOUND Failed to identify the image type.\r
+ @retval Others Unexpect error happened.\r
+\r
+**/\r
+EFI_STATUS\r
+HttpBootCheckImageType (\r
+ IN CHAR8 *Uri,\r
+ IN VOID *UriParser,\r
+ IN UINTN HeaderCount,\r
+ IN EFI_HTTP_HEADER *Headers,\r
+ OUT HTTP_BOOT_IMAGE_TYPE *ImageType\r
+ )\r
+{\r
+ EFI_STATUS Status;\r
+ EFI_HTTP_HEADER *Header;\r
+ CHAR8 *FilePath;\r
+ CHAR8 *FilePost;\r
+\r
+ if (Uri == NULL || UriParser == NULL || ImageType == NULL) {\r
+ return EFI_INVALID_PARAMETER;\r
+ }\r
+\r
+ if (HeaderCount != 0 && Headers == NULL) {\r
+ return EFI_INVALID_PARAMETER;\r
+ }\r
+\r
+ //\r
+ // Determine the image type by the HTTP Content-Type header field first.\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
+ //\r
+ // Determine the image type by file extension:\r
+ // *.efi -> EFI Image\r
+ // *.iso -> CD/DVD Image\r
+ // *.img -> Virtual Disk Image\r
+ //\r
+ Status = HttpUrlGetPath (\r
+ Uri,\r
+ UriParser,\r
+ &FilePath\r
+ );\r
+ if (EFI_ERROR (Status)) {\r
+ return Status;\r
+ }\r
+\r
+ FilePost = FilePath + AsciiStrLen (FilePath) - 4;\r
+ if (AsciiStrCmp (FilePost, ".efi") == 0) {\r
+ *ImageType = ImageTypeEfi;\r
+ } else if (AsciiStrCmp (FilePost, ".iso") == 0) {\r
+ *ImageType = ImageTypeVirtualCd;\r
+ } else if (AsciiStrCmp (FilePost, ".img") == 0) {\r
+ *ImageType = ImageTypeVirtualDisk;\r
+ } else {\r
+ *ImageType = ImageTypeMax;\r
+ }\r
+\r
+ FreePool (FilePath);\r
+\r
+ return (*ImageType < ImageTypeMax) ? EFI_SUCCESS : EFI_NOT_FOUND;\r
+}\r
+\r
+/**\r
+ This function register the RAM disk info to the system.\r
+\r
+ @param[in] Private The pointer to the driver's private data.\r
+ @param[in] BufferSize The size of Buffer in bytes.\r
+ @param[in] Buffer The base address of the RAM disk.\r
+ @param[in] ImageType The image type of the file in Buffer.\r
+\r
+ @retval EFI_SUCCESS The RAM disk has been registered.\r
+ @retval EFI_NOT_FOUND No RAM disk protocol instances were found.\r
+ @retval EFI_UNSUPPORTED The ImageType is not supported.\r
+ @retval Others Unexpected error happened.\r
+\r
+**/\r
+EFI_STATUS\r
+HttpBootRegisterRamDisk (\r
+ IN HTTP_BOOT_PRIVATE_DATA *Private,\r
+ IN UINTN BufferSize,\r
+ IN VOID *Buffer,\r
+ IN HTTP_BOOT_IMAGE_TYPE ImageType\r
+ )\r
+{\r
+ EFI_RAM_DISK_PROTOCOL *RamDisk;\r
+ EFI_STATUS Status;\r
+ EFI_DEVICE_PATH_PROTOCOL *DevicePath;\r
+ EFI_GUID *RamDiskType;\r
+\r
+ ASSERT (Private != NULL);\r
+ ASSERT (Buffer != NULL);\r
+ ASSERT (BufferSize != 0);\r
+\r
+ Status = gBS->LocateProtocol (&gEfiRamDiskProtocolGuid, NULL, (VOID**) &RamDisk);\r
+ if (EFI_ERROR (Status)) {\r
+ DEBUG ((EFI_D_ERROR, "HTTP Boot: Couldn't find the RAM Disk protocol - %r\n", Status));\r
+ return Status;\r
+ }\r
+\r
+ if (ImageType == ImageTypeVirtualCd) {\r
+ RamDiskType = &gEfiVirtualCdGuid;\r
+ } else if (ImageType == ImageTypeVirtualDisk) {\r
+ RamDiskType = &gEfiVirtualDiskGuid;\r
+ } else {\r
+ return EFI_UNSUPPORTED;\r
+ }\r
+\r
+ Status = RamDisk->Register (\r
+ (UINTN)Buffer,\r
+ (UINT64)BufferSize,\r
+ RamDiskType,\r
+ Private->UsingIpv6 ? Private->Ip6Nic->DevicePath : Private->Ip4Nic->DevicePath,\r
+ &DevicePath\r
+ );\r
+ if (EFI_ERROR (Status)) {\r
+ DEBUG ((EFI_D_ERROR, "HTTP Boot: Failed to register RAM Disk - %r\n", Status));\r
+ }\r
+\r
+ return Status;\r
+}\r
+\r
+/**\r
+ Indicate if the HTTP status code indicates a redirection.\r
+\r
+ @param[in] StatusCode HTTP status code from server.\r
+\r
+ @return TRUE if it's redirection.\r
+\r
+**/\r
+BOOLEAN\r
+HttpBootIsHttpRedirectStatusCode (\r
+ IN EFI_HTTP_STATUS_CODE StatusCode\r
+ )\r
+{\r
+ if (StatusCode == HTTP_STATUS_301_MOVED_PERMANENTLY ||\r
+ StatusCode == HTTP_STATUS_302_FOUND ||\r
+ StatusCode == HTTP_STATUS_307_TEMPORARY_REDIRECT ||\r
+ StatusCode == HTTP_STATUS_308_PERMANENT_REDIRECT) {\r
+ return TRUE;\r
+ }\r
+\r
+ return FALSE;\r
+}\r