2 Support functions implementation for UEFI HTTP boot driver.
4 Copyright (c) 2015 - 2018, Intel Corporation. All rights reserved.<BR>
5 (C) Copyright 2016 - 2020 Hewlett Packard Enterprise Development LP<BR>
6 SPDX-License-Identifier: BSD-2-Clause-Patent
10 #include "HttpBootDxe.h"
13 Get the Nic handle using any child handle in the IPv4 stack.
15 @param[in] ControllerHandle Pointer to child handle over IPv4.
17 @return NicHandle The pointer to the Nic handle.
18 @return NULL Can't find the Nic handle.
22 HttpBootGetNicByIp4Children (
23 IN EFI_HANDLE ControllerHandle
28 NicHandle
= NetLibGetNicHandle (ControllerHandle
, &gEfiHttpProtocolGuid
);
29 if (NicHandle
== NULL
) {
30 NicHandle
= NetLibGetNicHandle (ControllerHandle
, &gEfiDhcp4ProtocolGuid
);
31 if (NicHandle
== NULL
) {
40 Get the Nic handle using any child handle in the IPv6 stack.
42 @param[in] ControllerHandle Pointer to child handle over IPv6.
44 @return NicHandle The pointer to the Nic handle.
45 @return NULL Can't find the Nic handle.
49 HttpBootGetNicByIp6Children (
50 IN EFI_HANDLE ControllerHandle
55 NicHandle
= NetLibGetNicHandle (ControllerHandle
, &gEfiHttpProtocolGuid
);
56 if (NicHandle
== NULL
) {
57 NicHandle
= NetLibGetNicHandle (ControllerHandle
, &gEfiDhcp6ProtocolGuid
);
58 if (NicHandle
== NULL
) {
67 This function is to convert UINTN to ASCII string with the required formatting.
69 @param[in] Number Numeric value to be converted.
70 @param[in] Buffer The pointer to the buffer for ASCII string.
71 @param[in] Length The length of the required format.
75 HttpBootUintnToAscDecWithFormat (
83 for ( ; Length
> 0; Length
--) {
84 Remainder
= Number
% 10;
86 Buffer
[Length
- 1] = (UINT8
)('0' + Remainder
);
91 This function is to display the IPv4 address.
93 @param[in] Ip The pointer to the IPv4 address.
98 IN EFI_IPv4_ADDRESS
*Ip
103 for (Index
= 0; Index
< 4; Index
++) {
104 AsciiPrint ("%d", Ip
->Addr
[Index
]);
112 This function is to display the IPv6 address.
114 @param[in] Ip The pointer to the IPv6 address.
118 HttpBootShowIp6Addr (
119 IN EFI_IPv6_ADDRESS
*Ip
124 for (Index
= 0; Index
< 16; Index
++) {
125 if (Ip
->Addr
[Index
] != 0) {
126 AsciiPrint ("%x", Ip
->Addr
[Index
]);
134 if (((Ip
->Addr
[Index
] & 0xf0) == 0) && (Ip
->Addr
[Index
- 1] != 0)) {
138 AsciiPrint ("%x", Ip
->Addr
[Index
]);
146 This function is to display the HTTP error status.
148 @param[in] StatusCode The status code value in HTTP message.
152 HttpBootPrintErrorMessage (
153 EFI_HTTP_STATUS_CODE StatusCode
158 switch (StatusCode
) {
159 case HTTP_STATUS_300_MULTIPLE_CHOICES
:
160 AsciiPrint ("\n Redirection: 300 Multiple Choices");
163 case HTTP_STATUS_301_MOVED_PERMANENTLY
:
164 AsciiPrint ("\n Redirection: 301 Moved Permanently");
167 case HTTP_STATUS_302_FOUND
:
168 AsciiPrint ("\n Redirection: 302 Found");
171 case HTTP_STATUS_303_SEE_OTHER
:
172 AsciiPrint ("\n Redirection: 303 See Other");
175 case HTTP_STATUS_304_NOT_MODIFIED
:
176 AsciiPrint ("\n Redirection: 304 Not Modified");
179 case HTTP_STATUS_305_USE_PROXY
:
180 AsciiPrint ("\n Redirection: 305 Use Proxy");
183 case HTTP_STATUS_307_TEMPORARY_REDIRECT
:
184 AsciiPrint ("\n Redirection: 307 Temporary Redirect");
187 case HTTP_STATUS_308_PERMANENT_REDIRECT
:
188 AsciiPrint ("\n Redirection: 308 Permanent Redirect");
191 case HTTP_STATUS_400_BAD_REQUEST
:
192 AsciiPrint ("\n Client Error: 400 Bad Request");
195 case HTTP_STATUS_401_UNAUTHORIZED
:
196 AsciiPrint ("\n Client Error: 401 Unauthorized");
199 case HTTP_STATUS_402_PAYMENT_REQUIRED
:
200 AsciiPrint ("\n Client Error: 402 Payment Required");
203 case HTTP_STATUS_403_FORBIDDEN
:
204 AsciiPrint ("\n Client Error: 403 Forbidden");
207 case HTTP_STATUS_404_NOT_FOUND
:
208 AsciiPrint ("\n Client Error: 404 Not Found");
211 case HTTP_STATUS_405_METHOD_NOT_ALLOWED
:
212 AsciiPrint ("\n Client Error: 405 Method Not Allowed");
215 case HTTP_STATUS_406_NOT_ACCEPTABLE
:
216 AsciiPrint ("\n Client Error: 406 Not Acceptable");
219 case HTTP_STATUS_407_PROXY_AUTHENTICATION_REQUIRED
:
220 AsciiPrint ("\n Client Error: 407 Proxy Authentication Required");
223 case HTTP_STATUS_408_REQUEST_TIME_OUT
:
224 AsciiPrint ("\n Client Error: 408 Request Timeout");
227 case HTTP_STATUS_409_CONFLICT
:
228 AsciiPrint ("\n Client Error: 409 Conflict");
231 case HTTP_STATUS_410_GONE
:
232 AsciiPrint ("\n Client Error: 410 Gone");
235 case HTTP_STATUS_411_LENGTH_REQUIRED
:
236 AsciiPrint ("\n Client Error: 411 Length Required");
239 case HTTP_STATUS_412_PRECONDITION_FAILED
:
240 AsciiPrint ("\n Client Error: 412 Precondition Failed");
243 case HTTP_STATUS_413_REQUEST_ENTITY_TOO_LARGE
:
244 AsciiPrint ("\n Client Error: 413 Request Entity Too Large");
247 case HTTP_STATUS_414_REQUEST_URI_TOO_LARGE
:
248 AsciiPrint ("\n Client Error: 414 Request URI Too Long");
251 case HTTP_STATUS_415_UNSUPPORTED_MEDIA_TYPE
:
252 AsciiPrint ("\n Client Error: 415 Unsupported Media Type");
255 case HTTP_STATUS_416_REQUESTED_RANGE_NOT_SATISFIED
:
256 AsciiPrint ("\n Client Error: 416 Requested Range Not Satisfiable");
259 case HTTP_STATUS_417_EXPECTATION_FAILED
:
260 AsciiPrint ("\n Client Error: 417 Expectation Failed");
263 case HTTP_STATUS_500_INTERNAL_SERVER_ERROR
:
264 AsciiPrint ("\n Server Error: 500 Internal Server Error");
267 case HTTP_STATUS_501_NOT_IMPLEMENTED
:
268 AsciiPrint ("\n Server Error: 501 Not Implemented");
271 case HTTP_STATUS_502_BAD_GATEWAY
:
272 AsciiPrint ("\n Server Error: 502 Bad Gateway");
275 case HTTP_STATUS_503_SERVICE_UNAVAILABLE
:
276 AsciiPrint ("\n Server Error: 503 Service Unavailable");
279 case HTTP_STATUS_504_GATEWAY_TIME_OUT
:
280 AsciiPrint ("\n Server Error: 504 Gateway Timeout");
283 case HTTP_STATUS_505_HTTP_VERSION_NOT_SUPPORTED
:
284 AsciiPrint ("\n Server Error: 505 HTTP Version Not Supported");
292 Notify the callback function when an event is triggered.
294 @param[in] Event The triggered event.
295 @param[in] Context The opaque parameter to the function.
300 HttpBootCommonNotify (
305 *((BOOLEAN
*)Context
) = TRUE
;
309 Retrieve the host address using the EFI_DNS6_PROTOCOL.
311 @param[in] Private The pointer to the driver's private data.
312 @param[in] HostName Pointer to buffer containing hostname.
313 @param[out] IpAddress On output, pointer to buffer containing IPv6 address.
315 @retval EFI_SUCCESS Operation succeeded.
316 @retval EFI_DEVICE_ERROR An unexpected network error occurred.
317 @retval Others Other errors as indicated.
321 IN HTTP_BOOT_PRIVATE_DATA
*Private
,
323 OUT EFI_IPv6_ADDRESS
*IpAddress
327 EFI_DNS6_PROTOCOL
*Dns6
;
328 EFI_DNS6_CONFIG_DATA Dns6ConfigData
;
329 EFI_DNS6_COMPLETION_TOKEN Token
;
330 EFI_HANDLE Dns6Handle
;
331 EFI_IP6_CONFIG_PROTOCOL
*Ip6Config
;
332 EFI_IPv6_ADDRESS
*DnsServerList
;
333 UINTN DnsServerListCount
;
337 DnsServerList
= NULL
;
338 DnsServerListCount
= 0;
341 ZeroMem (&Token
, sizeof (EFI_DNS6_COMPLETION_TOKEN
));
344 // Get DNS server list from EFI IPv6 Configuration protocol.
346 Status
= gBS
->HandleProtocol (Private
->Controller
, &gEfiIp6ConfigProtocolGuid
, (VOID
**)&Ip6Config
);
347 if (!EFI_ERROR (Status
)) {
349 // Get the required size.
352 Status
= Ip6Config
->GetData (Ip6Config
, Ip6ConfigDataTypeDnsServer
, &DataSize
, NULL
);
353 if (Status
== EFI_BUFFER_TOO_SMALL
) {
354 DnsServerList
= AllocatePool (DataSize
);
355 if (DnsServerList
== NULL
) {
356 return EFI_OUT_OF_RESOURCES
;
359 Status
= Ip6Config
->GetData (Ip6Config
, Ip6ConfigDataTypeDnsServer
, &DataSize
, DnsServerList
);
360 if (EFI_ERROR (Status
)) {
361 FreePool (DnsServerList
);
362 DnsServerList
= NULL
;
364 DnsServerListCount
= DataSize
/ sizeof (EFI_IPv6_ADDRESS
);
370 // Create a DNSv6 child instance and get the protocol.
372 Status
= NetLibCreateServiceChild (
374 Private
->Ip6Nic
->ImageHandle
,
375 &gEfiDns6ServiceBindingProtocolGuid
,
378 if (EFI_ERROR (Status
)) {
382 Status
= gBS
->OpenProtocol (
384 &gEfiDns6ProtocolGuid
,
386 Private
->Ip6Nic
->ImageHandle
,
388 EFI_OPEN_PROTOCOL_BY_DRIVER
390 if (EFI_ERROR (Status
)) {
395 // Configure DNS6 instance for the DNS server address and protocol.
397 ZeroMem (&Dns6ConfigData
, sizeof (EFI_DNS6_CONFIG_DATA
));
398 Dns6ConfigData
.DnsServerCount
= (UINT32
)DnsServerListCount
;
399 Dns6ConfigData
.DnsServerList
= DnsServerList
;
400 Dns6ConfigData
.EnableDnsCache
= TRUE
;
401 Dns6ConfigData
.Protocol
= EFI_IP_PROTO_UDP
;
402 IP6_COPY_ADDRESS (&Dns6ConfigData
.StationIp
, &Private
->StationIp
.v6
);
403 Status
= Dns6
->Configure (
407 if (EFI_ERROR (Status
)) {
411 Token
.Status
= EFI_NOT_READY
;
414 // Create event to set the IsDone flag when name resolution is finished.
416 Status
= gBS
->CreateEvent (
419 HttpBootCommonNotify
,
423 if (EFI_ERROR (Status
)) {
428 // Start asynchronous name resolution.
430 Status
= Dns6
->HostNameToIp (Dns6
, HostName
, &Token
);
431 if (EFI_ERROR (Status
)) {
440 // Name resolution is done, check result.
442 Status
= Token
.Status
;
443 if (!EFI_ERROR (Status
)) {
444 if (Token
.RspData
.H2AData
== NULL
) {
445 Status
= EFI_DEVICE_ERROR
;
449 if ((Token
.RspData
.H2AData
->IpCount
== 0) || (Token
.RspData
.H2AData
->IpList
== NULL
)) {
450 Status
= EFI_DEVICE_ERROR
;
455 // We just return the first IPv6 address from DNS protocol.
457 IP6_COPY_ADDRESS (IpAddress
, Token
.RspData
.H2AData
->IpList
);
458 Status
= EFI_SUCCESS
;
463 if (Token
.Event
!= NULL
) {
464 gBS
->CloseEvent (Token
.Event
);
467 if (Token
.RspData
.H2AData
!= NULL
) {
468 if (Token
.RspData
.H2AData
->IpList
!= NULL
) {
469 FreePool (Token
.RspData
.H2AData
->IpList
);
472 FreePool (Token
.RspData
.H2AData
);
476 Dns6
->Configure (Dns6
, NULL
);
480 &gEfiDns6ProtocolGuid
,
481 Private
->Ip6Nic
->ImageHandle
,
486 if (Dns6Handle
!= NULL
) {
487 NetLibDestroyServiceChild (
489 Private
->Ip6Nic
->ImageHandle
,
490 &gEfiDns6ServiceBindingProtocolGuid
,
495 if (DnsServerList
!= NULL
) {
496 FreePool (DnsServerList
);
503 This function checks the HTTP(S) URI scheme.
505 @param[in] Uri The pointer to the URI string.
507 @retval EFI_SUCCESS The URI scheme is valid.
508 @retval EFI_INVALID_PARAMETER The URI scheme is not HTTP or HTTPS.
509 @retval EFI_ACCESS_DENIED HTTP is disabled and the URI is HTTP.
513 HttpBootCheckUriScheme (
520 Status
= EFI_SUCCESS
;
523 // Convert the scheme to all lower case.
525 for (Index
= 0; Index
< AsciiStrLen (Uri
); Index
++) {
526 if (Uri
[Index
] == ':') {
530 if ((Uri
[Index
] >= 'A') && (Uri
[Index
] <= 'Z')) {
531 Uri
[Index
] -= (CHAR8
)('A' - 'a');
536 // Return EFI_INVALID_PARAMETER if the URI is not HTTP or HTTPS.
538 if ((AsciiStrnCmp (Uri
, "http://", 7) != 0) && (AsciiStrnCmp (Uri
, "https://", 8) != 0)) {
539 DEBUG ((DEBUG_ERROR
, "HttpBootCheckUriScheme: Invalid Uri.\n"));
540 return EFI_INVALID_PARAMETER
;
544 // HTTP is disabled, return EFI_ACCESS_DENIED if the URI is HTTP.
546 if (!PcdGetBool (PcdAllowHttpConnections
) && (AsciiStrnCmp (Uri
, "http://", 7) == 0)) {
547 DEBUG ((DEBUG_ERROR
, "HttpBootCheckUriScheme: HTTP is disabled.\n"));
548 return EFI_ACCESS_DENIED
;
555 Get the URI address string from the input device path.
557 Caller need to free the buffer in the UriAddress pointer.
559 @param[in] FilePath Pointer to the device path which contains a URI device path node.
560 @param[out] UriAddress The URI address string extract from the device path.
562 @retval EFI_SUCCESS The URI string is returned.
563 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory.
567 HttpBootParseFilePath (
568 IN EFI_DEVICE_PATH_PROTOCOL
*FilePath
,
569 OUT CHAR8
**UriAddress
572 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePath
;
573 URI_DEVICE_PATH
*UriDevicePath
;
577 if (FilePath
== NULL
) {
578 return EFI_INVALID_PARAMETER
;
584 // Extract the URI address from the FilePath
586 TempDevicePath
= FilePath
;
587 while (!IsDevicePathEnd (TempDevicePath
)) {
588 if ((DevicePathType (TempDevicePath
) == MESSAGING_DEVICE_PATH
) &&
589 (DevicePathSubType (TempDevicePath
) == MSG_URI_DP
))
591 UriDevicePath
= (URI_DEVICE_PATH
*)TempDevicePath
;
593 // UEFI Spec doesn't require the URI to be a NULL-terminated string
594 // So we allocate a new buffer and always append a '\0' to it.
596 UriStrLength
= DevicePathNodeLength (UriDevicePath
) - sizeof (EFI_DEVICE_PATH_PROTOCOL
);
597 if (UriStrLength
== 0) {
599 // return a NULL UriAddress if it's a empty URI device path node.
604 Uri
= AllocatePool (UriStrLength
+ 1);
606 return EFI_OUT_OF_RESOURCES
;
609 CopyMem (Uri
, UriDevicePath
->Uri
, DevicePathNodeLength (UriDevicePath
) - sizeof (EFI_DEVICE_PATH_PROTOCOL
));
610 Uri
[DevicePathNodeLength (UriDevicePath
) - sizeof (EFI_DEVICE_PATH_PROTOCOL
)] = '\0';
615 TempDevicePath
= NextDevicePathNode (TempDevicePath
);
622 This function returns the image type according to server replied HTTP message
623 and also the image's URI info.
625 @param[in] Uri The pointer to the image's URI string.
626 @param[in] UriParser URI Parse result returned by NetHttpParseUrl().
627 @param[in] HeaderCount Number of HTTP header structures in Headers list.
628 @param[in] Headers Array containing list of HTTP headers.
629 @param[out] ImageType The image type of the downloaded file.
631 @retval EFI_SUCCESS The image type is returned in ImageType.
632 @retval EFI_INVALID_PARAMETER ImageType, Uri or UriParser is NULL.
633 @retval EFI_INVALID_PARAMETER HeaderCount is not zero, and Headers is NULL.
634 @retval EFI_NOT_FOUND Failed to identify the image type.
635 @retval Others Unexpected error happened.
639 HttpBootCheckImageType (
642 IN UINTN HeaderCount
,
643 IN EFI_HTTP_HEADER
*Headers
,
644 OUT HTTP_BOOT_IMAGE_TYPE
*ImageType
648 EFI_HTTP_HEADER
*Header
;
652 if ((Uri
== NULL
) || (UriParser
== NULL
) || (ImageType
== NULL
)) {
653 return EFI_INVALID_PARAMETER
;
656 if ((HeaderCount
!= 0) && (Headers
== NULL
)) {
657 return EFI_INVALID_PARAMETER
;
661 // Determine the image type by the HTTP Content-Type header field first.
662 // "application/efi" -> EFI Image
663 // "application/vnd.efi-iso" -> CD/DVD Image
664 // "application/vnd.efi-img" -> Virtual Disk Image
666 Header
= HttpFindHeader (HeaderCount
, Headers
, HTTP_HEADER_CONTENT_TYPE
);
667 if (Header
!= NULL
) {
668 if (AsciiStriCmp (Header
->FieldValue
, HTTP_CONTENT_TYPE_APP_EFI
) == 0) {
669 *ImageType
= ImageTypeEfi
;
671 } else if (AsciiStriCmp (Header
->FieldValue
, HTTP_CONTENT_TYPE_APP_ISO
) == 0) {
672 *ImageType
= ImageTypeVirtualCd
;
674 } else if (AsciiStriCmp (Header
->FieldValue
, HTTP_CONTENT_TYPE_APP_IMG
) == 0) {
675 *ImageType
= ImageTypeVirtualDisk
;
681 // Determine the image type by file extension:
682 // *.efi -> EFI Image
683 // *.iso -> CD/DVD Image
684 // *.img -> Virtual Disk Image
686 Status
= HttpUrlGetPath (
691 if (EFI_ERROR (Status
)) {
695 FilePost
= FilePath
+ AsciiStrLen (FilePath
) - 4;
696 if (AsciiStriCmp (FilePost
, ".efi") == 0) {
697 *ImageType
= ImageTypeEfi
;
698 } else if (AsciiStriCmp (FilePost
, ".iso") == 0) {
699 *ImageType
= ImageTypeVirtualCd
;
700 } else if (AsciiStriCmp (FilePost
, ".img") == 0) {
701 *ImageType
= ImageTypeVirtualDisk
;
703 *ImageType
= ImageTypeMax
;
708 return (*ImageType
< ImageTypeMax
) ? EFI_SUCCESS
: EFI_NOT_FOUND
;
712 This function register the RAM disk info to the system.
714 @param[in] Private The pointer to the driver's private data.
715 @param[in] BufferSize The size of Buffer in bytes.
716 @param[in] Buffer The base address of the RAM disk.
717 @param[in] ImageType The image type of the file in Buffer.
719 @retval EFI_SUCCESS The RAM disk has been registered.
720 @retval EFI_NOT_FOUND No RAM disk protocol instances were found.
721 @retval EFI_UNSUPPORTED The ImageType is not supported.
722 @retval Others Unexpected error happened.
726 HttpBootRegisterRamDisk (
727 IN HTTP_BOOT_PRIVATE_DATA
*Private
,
730 IN HTTP_BOOT_IMAGE_TYPE ImageType
733 EFI_RAM_DISK_PROTOCOL
*RamDisk
;
735 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
736 EFI_GUID
*RamDiskType
;
738 ASSERT (Private
!= NULL
);
739 ASSERT (Buffer
!= NULL
);
740 ASSERT (BufferSize
!= 0);
742 Status
= gBS
->LocateProtocol (&gEfiRamDiskProtocolGuid
, NULL
, (VOID
**)&RamDisk
);
743 if (EFI_ERROR (Status
)) {
744 DEBUG ((DEBUG_ERROR
, "HTTP Boot: Couldn't find the RAM Disk protocol - %r\n", Status
));
748 if (ImageType
== ImageTypeVirtualCd
) {
749 RamDiskType
= &gEfiVirtualCdGuid
;
750 } else if (ImageType
== ImageTypeVirtualDisk
) {
751 RamDiskType
= &gEfiVirtualDiskGuid
;
753 return EFI_UNSUPPORTED
;
756 Status
= RamDisk
->Register (
760 Private
->UsingIpv6
? Private
->Ip6Nic
->DevicePath
: Private
->Ip4Nic
->DevicePath
,
763 if (EFI_ERROR (Status
)) {
764 DEBUG ((DEBUG_ERROR
, "HTTP Boot: Failed to register RAM Disk - %r\n", Status
));
771 Indicate if the HTTP status code indicates a redirection.
773 @param[in] StatusCode HTTP status code from server.
775 @return TRUE if it's redirection.
779 HttpBootIsHttpRedirectStatusCode (
780 IN EFI_HTTP_STATUS_CODE StatusCode
783 if ((StatusCode
== HTTP_STATUS_301_MOVED_PERMANENTLY
) ||
784 (StatusCode
== HTTP_STATUS_302_FOUND
) ||
785 (StatusCode
== HTTP_STATUS_307_TEMPORARY_REDIRECT
) ||
786 (StatusCode
== HTTP_STATUS_308_PERMANENT_REDIRECT
))