X-Git-Url: https://git.proxmox.com/?a=blobdiff_plain;f=NetworkPkg%2FHttpBootDxe%2FHttpBootClient.c;h=1d1e47008da23ee1fb8c9ba2037c1ed3b407c9f0;hb=373b1d0ee31ea2e2868307ea82277271b18a7be7;hp=46cf9ca6fb78e49cc48ed5cf1cb1f7c5d3b94133;hpb=a93786ae6c4f8a92e216899c05366ba7aacf72e7;p=mirror_edk2.git diff --git a/NetworkPkg/HttpBootDxe/HttpBootClient.c b/NetworkPkg/HttpBootDxe/HttpBootClient.c index 46cf9ca6fb..1d1e47008d 100644 --- a/NetworkPkg/HttpBootDxe/HttpBootClient.c +++ b/NetworkPkg/HttpBootDxe/HttpBootClient.c @@ -1,7 +1,7 @@ /** @file Implementation of the boot file download function. -Copyright (c) 2015 - 2016, Intel Corporation. All rights reserved.
+Copyright (c) 2015 - 2018, Intel Corporation. All rights reserved.
(C) Copyright 2016 Hewlett Packard Enterprise Development LP
This program and the accompanying materials are licensed and made available under the terms and conditions of the BSD License that accompanies this distribution. @@ -16,7 +16,7 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. #include "HttpBootDxe.h" /** - Update the IP and URL device path node to include the boot resource information. + Update the device path node to include the boot resource information. @param[in] Private The pointer to the driver's private data. @@ -31,12 +31,14 @@ HttpBootUpdateDevicePath ( ) { EFI_DEV_PATH *Node; - EFI_DEVICE_PATH_PROTOCOL *TmpDevicePath; + EFI_DEVICE_PATH_PROTOCOL *TmpIpDevicePath; + EFI_DEVICE_PATH_PROTOCOL *TmpDnsDevicePath; EFI_DEVICE_PATH_PROTOCOL *NewDevicePath; UINTN Length; EFI_STATUS Status; - TmpDevicePath = NULL; + TmpIpDevicePath = NULL; + TmpDnsDevicePath = NULL; // // Update the IP node with DHCP assigned information. @@ -72,29 +74,65 @@ HttpBootUpdateDevicePath ( CopyMem (&Node->Ipv6.GatewayIpAddress, &Private->GatewayIp.v6, sizeof (EFI_IPv6_ADDRESS)); } - TmpDevicePath = AppendDevicePathNode (Private->ParentDevicePath, (EFI_DEVICE_PATH_PROTOCOL*) Node); + TmpIpDevicePath = AppendDevicePathNode (Private->ParentDevicePath, (EFI_DEVICE_PATH_PROTOCOL*) Node); FreePool (Node); - if (TmpDevicePath == NULL) { + if (TmpIpDevicePath == NULL) { return EFI_OUT_OF_RESOURCES; } + // + // Update the DNS node with DNS server IP list if existed. + // + if (Private->DnsServerIp != NULL) { + Length = sizeof (EFI_DEVICE_PATH_PROTOCOL) + sizeof (Node->Dns.IsIPv6) + Private->DnsServerCount * sizeof (EFI_IP_ADDRESS); + Node = AllocatePool (Length); + if (Node == NULL) { + FreePool (TmpIpDevicePath); + return EFI_OUT_OF_RESOURCES; + } + Node->DevPath.Type = MESSAGING_DEVICE_PATH; + Node->DevPath.SubType = MSG_DNS_DP; + SetDevicePathNodeLength (Node, Length); + Node->Dns.IsIPv6 = Private->UsingIpv6 ? 0x01 : 0x00; + CopyMem ((UINT8*) Node + sizeof (EFI_DEVICE_PATH_PROTOCOL) + sizeof (Node->Dns.IsIPv6), Private->DnsServerIp, Private->DnsServerCount * sizeof (EFI_IP_ADDRESS)); + + TmpDnsDevicePath = AppendDevicePathNode (TmpIpDevicePath, (EFI_DEVICE_PATH_PROTOCOL*) Node); + FreePool (Node); + FreePool (TmpIpDevicePath); + TmpIpDevicePath = NULL; + if (TmpDnsDevicePath == NULL) { + return EFI_OUT_OF_RESOURCES; + } + } + // // Update the URI node with the boot file URI. // Length = sizeof (EFI_DEVICE_PATH_PROTOCOL) + AsciiStrSize (Private->BootFileUri); Node = AllocatePool (Length); if (Node == NULL) { - FreePool (TmpDevicePath); + if (TmpIpDevicePath != NULL) { + FreePool (TmpIpDevicePath); + } + if (TmpDnsDevicePath != NULL) { + FreePool (TmpDnsDevicePath); + } return EFI_OUT_OF_RESOURCES; } Node->DevPath.Type = MESSAGING_DEVICE_PATH; Node->DevPath.SubType = MSG_URI_DP; SetDevicePathNodeLength (Node, Length); CopyMem ((UINT8*) Node + sizeof (EFI_DEVICE_PATH_PROTOCOL), Private->BootFileUri, AsciiStrSize (Private->BootFileUri)); - - NewDevicePath = AppendDevicePathNode (TmpDevicePath, (EFI_DEVICE_PATH_PROTOCOL*) Node); + + if (TmpDnsDevicePath != NULL) { + NewDevicePath = AppendDevicePathNode (TmpDnsDevicePath, (EFI_DEVICE_PATH_PROTOCOL*) Node); + FreePool (TmpDnsDevicePath); + } else { + ASSERT (TmpIpDevicePath != NULL); + NewDevicePath = AppendDevicePathNode (TmpIpDevicePath, (EFI_DEVICE_PATH_PROTOCOL*) Node); + FreePool (TmpIpDevicePath); + } FreePool (Node); - FreePool (TmpDevicePath); if (NewDevicePath == NULL) { return EFI_OUT_OF_RESOURCES; } @@ -153,6 +191,7 @@ HttpBootDhcp4ExtractUriInfo ( HTTP_BOOT_DHCP4_PACKET_CACHE *HttpOffer; UINT32 SelectIndex; UINT32 ProxyIndex; + UINT32 DnsServerIndex; EFI_DHCP4_PACKET_OPTION *Option; EFI_STATUS Status; @@ -161,6 +200,8 @@ HttpBootDhcp4ExtractUriInfo ( SelectIndex = Private->SelectIndex - 1; ASSERT (SelectIndex < HTTP_BOOT_OFFER_MAX_NUM); + DnsServerIndex = 0; + Status = EFI_SUCCESS; // @@ -192,19 +233,50 @@ HttpBootDhcp4ExtractUriInfo ( } // - // Configure the default DNS server if server assigned. + // Check the URI scheme. // + Status = HttpBootCheckUriScheme (Private->BootFileUri); + if (EFI_ERROR (Status)) { + DEBUG ((EFI_D_ERROR, "HttpBootDhcp4ExtractUriInfo: %r.\n", Status)); + if (Status == EFI_INVALID_PARAMETER) { + AsciiPrint ("\n Error: Invalid URI address.\n"); + } else if (Status == EFI_ACCESS_DENIED) { + AsciiPrint ("\n Error: Access forbidden, only HTTPS connection is allowed.\n"); + } + return Status; + } + if ((SelectOffer->OfferType == HttpOfferTypeDhcpNameUriDns) || (SelectOffer->OfferType == HttpOfferTypeDhcpDns) || (SelectOffer->OfferType == HttpOfferTypeDhcpIpUriDns)) { Option = SelectOffer->OptList[HTTP_BOOT_DHCP4_TAG_INDEX_DNS_SERVER]; ASSERT (Option != NULL); + + // + // Record the Dns Server address list. + // + Private->DnsServerCount = (Option->Length) / sizeof (EFI_IPv4_ADDRESS); + + Private->DnsServerIp = AllocateZeroPool (Private->DnsServerCount * sizeof (EFI_IP_ADDRESS)); + if (Private->DnsServerIp == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + for (DnsServerIndex = 0; DnsServerIndex < Private->DnsServerCount; DnsServerIndex++) { + CopyMem (&(Private->DnsServerIp[DnsServerIndex].v4), &(((EFI_IPv4_ADDRESS *) Option->Data)[DnsServerIndex]), sizeof (EFI_IPv4_ADDRESS)); + } + + // + // Configure the default DNS server if server assigned. + // Status = HttpBootRegisterIp4Dns ( Private, Option->Length, Option->Data ); if (EFI_ERROR (Status)) { + FreePool (Private->DnsServerIp); + Private->DnsServerIp = NULL; return Status; } } @@ -224,12 +296,15 @@ HttpBootDhcp4ExtractUriInfo ( // // All boot informations are valid here. // - AsciiPrint ("\n URI: %a", Private->BootFileUri); // - // Update the device path to include the IP and boot URI information. + // Update the device path to include the boot resource information. // Status = HttpBootUpdateDevicePath (Private); + if (EFI_ERROR (Status) && Private->DnsServerIp != NULL) { + FreePool (Private->DnsServerIp); + Private->DnsServerIp = NULL; + } return Status; } @@ -252,9 +327,11 @@ HttpBootDhcp6ExtractUriInfo ( HTTP_BOOT_DHCP6_PACKET_CACHE *HttpOffer; UINT32 SelectIndex; UINT32 ProxyIndex; + UINT32 DnsServerIndex; EFI_DHCP6_PACKET_OPTION *Option; EFI_IPv6_ADDRESS IpAddr; CHAR8 *HostName; + UINTN HostNameSize; CHAR16 *HostNameStr; EFI_STATUS Status; @@ -263,6 +340,8 @@ HttpBootDhcp6ExtractUriInfo ( SelectIndex = Private->SelectIndex - 1; ASSERT (SelectIndex < HTTP_BOOT_OFFER_MAX_NUM); + DnsServerIndex = 0; + Status = EFI_SUCCESS; HostName = NULL; // @@ -293,6 +372,20 @@ HttpBootDhcp6ExtractUriInfo ( Private->BootFileUri = Private->FilePathUri; } + // + // Check the URI scheme. + // + Status = HttpBootCheckUriScheme (Private->BootFileUri); + if (EFI_ERROR (Status)) { + DEBUG ((EFI_D_ERROR, "HttpBootDhcp6ExtractUriInfo: %r.\n", Status)); + if (Status == EFI_INVALID_PARAMETER) { + AsciiPrint ("\n Error: Invalid URI address.\n"); + } else if (Status == EFI_ACCESS_DENIED) { + AsciiPrint ("\n Error: Access forbidden, only HTTPS connection is allowed.\n"); + } + return Status; + } + // // Set the Local station address to IP layer. // @@ -300,27 +393,50 @@ HttpBootDhcp6ExtractUriInfo ( if (EFI_ERROR (Status)) { return Status; } - + // - // Configure the default DNS server if server assigned. + // Register the IPv6 gateway address to the network device. // + Status = HttpBootSetIp6Gateway (Private); + if (EFI_ERROR (Status)) { + return Status; + } + if ((SelectOffer->OfferType == HttpOfferTypeDhcpNameUriDns) || (SelectOffer->OfferType == HttpOfferTypeDhcpDns) || (SelectOffer->OfferType == HttpOfferTypeDhcpIpUriDns)) { Option = SelectOffer->OptList[HTTP_BOOT_DHCP6_IDX_DNS_SERVER]; ASSERT (Option != NULL); + + // + // Record the Dns Server address list. + // + Private->DnsServerCount = HTONS (Option->OpLen) / sizeof (EFI_IPv6_ADDRESS); + + Private->DnsServerIp = AllocateZeroPool (Private->DnsServerCount * sizeof (EFI_IP_ADDRESS)); + if (Private->DnsServerIp == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + for (DnsServerIndex = 0; DnsServerIndex < Private->DnsServerCount; DnsServerIndex++) { + CopyMem (&(Private->DnsServerIp[DnsServerIndex].v6), &(((EFI_IPv6_ADDRESS *) Option->Data)[DnsServerIndex]), sizeof (EFI_IPv6_ADDRESS)); + } + + // + // Configure the default DNS server if server assigned. + // Status = HttpBootSetIp6Dns ( Private, HTONS (Option->OpLen), Option->Data ); if (EFI_ERROR (Status)) { - return Status; + goto Error; } } // - // Extract the HTTP server Ip frome URL. This is used to Check route table + // Extract the HTTP server Ip from URL. This is used to Check route table // whether can send message to HTTP Server Ip through the GateWay. // Status = HttpUrlGetIp6 ( @@ -339,32 +455,31 @@ HttpBootDhcp6ExtractUriInfo ( &HostName ); if (EFI_ERROR (Status)) { - return Status; + goto Error; } - - HostNameStr = AllocateZeroPool ((AsciiStrLen (HostName) + 1) * sizeof (CHAR16)); + + HostNameSize = AsciiStrSize (HostName); + HostNameStr = AllocateZeroPool (HostNameSize * sizeof (CHAR16)); if (HostNameStr == NULL) { Status = EFI_OUT_OF_RESOURCES; goto Error; } - AsciiStrToUnicodeStr (HostName, HostNameStr); + AsciiStrToUnicodeStrS (HostName, HostNameStr, HostNameSize); + + if (HostName != NULL) { + FreePool (HostName); + } + Status = HttpBootDns (Private, HostNameStr, &IpAddr); FreePool (HostNameStr); if (EFI_ERROR (Status)) { + AsciiPrint ("\n Error: Could not retrieve the host address from DNS server.\n"); goto Error; } } - CopyMem (&Private->ServerIp.v6, &IpAddr, sizeof (EFI_IPv6_ADDRESS)); - - // - // register the IPv6 gateway address to the network device. - // - Status = HttpBootSetIp6Gateway (Private); - if (EFI_ERROR (Status)) { - return Status; - } + CopyMem (&Private->ServerIp.v6, &IpAddr, sizeof (EFI_IPv6_ADDRESS)); // // Extract the port from URL, and use default HTTP port 80 if not provided. @@ -381,18 +496,23 @@ HttpBootDhcp6ExtractUriInfo ( // // All boot informations are valid here. // - AsciiPrint ("\n URI: %a", Private->BootFileUri); + // - // Update the device path to include the IP and boot URI information. + // Update the device path to include the boot resource information. // Status = HttpBootUpdateDevicePath (Private); + if (EFI_ERROR (Status)) { + goto Error; + } + + return Status; Error: - - if (HostName != NULL) { - FreePool (HostName); + if (Private->DnsServerIp != NULL) { + FreePool (Private->DnsServerIp); + Private->DnsServerIp = NULL; } - + return Status; } @@ -431,6 +551,40 @@ HttpBootDiscoverBootInfo ( return Status; } +/** + HttpIo Callback function which will be invoked when specified HTTP_IO_CALLBACK_EVENT happened. + + @param[in] EventType Indicate the Event type that occurs in the current callback. + @param[in] Message HTTP message which will be send to, or just received from HTTP server. + @param[in] Context The Callback Context pointer. + + @retval EFI_SUCCESS Tells the HttpIo to continue the HTTP process. + @retval Others Tells the HttpIo to abort the current HTTP process. +**/ +EFI_STATUS +EFIAPI +HttpBootHttpIoCallback ( + IN HTTP_IO_CALLBACK_EVENT EventType, + IN EFI_HTTP_MESSAGE *Message, + IN VOID *Context + ) +{ + HTTP_BOOT_PRIVATE_DATA *Private; + EFI_STATUS Status; + Private = (HTTP_BOOT_PRIVATE_DATA *) Context; + if (Private->HttpBootCallback != NULL) { + Status = Private->HttpBootCallback->Callback ( + Private->HttpBootCallback, + EventType == HttpIoRequest ? HttpBootHttpRequest : HttpBootHttpResponse, + EventType == HttpIoRequest ? FALSE : TRUE, + sizeof (EFI_HTTP_MESSAGE), + (VOID *) Message + ); + return Status; + } + return EFI_SUCCESS; +} + /** Create a HttpIo instance for the file download. @@ -470,6 +624,8 @@ HttpBootCreateHttpIo ( Private->Controller, Private->UsingIpv6 ? IP_VERSION_6 : IP_VERSION_4, &ConfigData, + HttpBootHttpIoCallback, + (VOID *) Private, &Private->HttpIo ); if (EFI_ERROR (Status)) { @@ -591,7 +747,7 @@ HttpBootGetFileFromCache ( HTTP_BOOT_ENTITY_DATA *EntityData; UINTN CopyedSize; - if (Uri == NULL || BufferSize == 0 || Buffer == NULL || ImageType == NULL) { + if (Uri == NULL || BufferSize == NULL || Buffer == NULL || ImageType == NULL) { return EFI_INVALID_PARAMETER; } @@ -602,8 +758,7 @@ HttpBootGetFileFromCache ( // if ((Cache->RequestData != NULL) && (Cache->RequestData->Url != NULL) && - (StrCmp (Uri, Cache->RequestData->Url) == 0)) - { + (StrCmp (Uri, Cache->RequestData->Url) == 0)) { // // Hit in cache, record image type. // @@ -666,6 +821,8 @@ HttpBootGetBootFileCallback ( { HTTP_BOOT_CALLBACK_DATA *CallbackData; HTTP_BOOT_ENTITY_DATA *NewEntityData; + EFI_STATUS Status; + EFI_HTTP_BOOT_CALLBACK_PROTOCOL *HttpBootCallback; // // We only care about the entity data. @@ -675,6 +832,19 @@ HttpBootGetBootFileCallback ( } CallbackData = (HTTP_BOOT_CALLBACK_DATA *) Context; + HttpBootCallback = CallbackData->Private->HttpBootCallback; + if (HttpBootCallback != NULL) { + Status = HttpBootCallback->Callback ( + HttpBootCallback, + HttpBootHttpEntityBody, + TRUE, + (UINT32)Length, + Data + ); + if (EFI_ERROR (Status)) { + return Status; + } + } // // Copy data if caller has provided a buffer. // @@ -752,6 +922,7 @@ HttpBootGetBootFile ( UINTN ContentLength; HTTP_BOOT_CACHE_CONTENT *Cache; UINT8 *Block; + UINTN UrlSize; CHAR16 *Url; BOOLEAN IdentityMode; UINTN ReceivedSize; @@ -770,12 +941,13 @@ HttpBootGetBootFile ( // // First, check whether we already cached the requested Uri. // - Url = AllocatePool ((AsciiStrLen (Private->BootFileUri) + 1) * sizeof (CHAR16)); + UrlSize = AsciiStrSize (Private->BootFileUri); + Url = AllocatePool (UrlSize * sizeof (CHAR16)); if (Url == NULL) { return EFI_OUT_OF_RESOURCES; } - AsciiStrToUnicodeStr (Private->BootFileUri, Url); - if (!HeaderOnly) { + AsciiStrToUnicodeStrS (Private->BootFileUri, Url, UrlSize); + if (!HeaderOnly && Buffer != NULL) { Status = HttpBootGetFileFromCache (Private, Url, BufferSize, Buffer, ImageType); if (Status != EFI_NOT_FOUND) { FreePool (Url); @@ -873,11 +1045,6 @@ HttpBootGetBootFile ( } RequestData->Method = HeaderOnly ? HttpMethodHead : HttpMethodGet; RequestData->Url = Url; - if (RequestData->Url == NULL) { - Status = EFI_OUT_OF_RESOURCES; - goto ERROR_4; - } - AsciiStrToUnicodeStr (Private->BootFileUri, RequestData->Url); // // 2.3 Record the request info in a temp cache item. @@ -960,8 +1127,9 @@ HttpBootGetBootFile ( Context.Buffer = Buffer; Context.BufferSize = *BufferSize; Context.Cache = Cache; + Context.Private = Private; Status = HttpInitMsgParser ( - HeaderOnly? HttpMethodHead : HttpMethodGet, + HeaderOnly ? HttpMethodHead : HttpMethodGet, ResponseData->Response.StatusCode, ResponseData->HeaderCount, ResponseData->Headers, @@ -1008,10 +1176,25 @@ HttpBootGetBootFile ( FALSE, &ResponseBody ); - if (EFI_ERROR (Status)) { + if (EFI_ERROR (Status) || EFI_ERROR (ResponseBody.Status)) { + if (EFI_ERROR (ResponseBody.Status)) { + Status = ResponseBody.Status; + } goto ERROR_6; } ReceivedSize += ResponseBody.BodyLength; + if (Private->HttpBootCallback != NULL) { + Status = Private->HttpBootCallback->Callback ( + Private->HttpBootCallback, + HttpBootHttpEntityBody, + TRUE, + (UINT32)ResponseBody.BodyLength, + ResponseBody.Body + ); + if (EFI_ERROR (Status)) { + goto ERROR_6; + } + } } } else { // @@ -1045,7 +1228,10 @@ HttpBootGetBootFile ( FALSE, &ResponseBody ); - if (EFI_ERROR (Status)) { + if (EFI_ERROR (Status) || EFI_ERROR (ResponseBody.Status)) { + if (EFI_ERROR (ResponseBody.Status)) { + Status = ResponseBody.Status; + } goto ERROR_6; }