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"
14 Get the Nic handle using any child handle in the IPv4 stack.
16 @param[in] ControllerHandle Pointer to child handle over IPv4.
18 @return NicHandle The pointer to the Nic handle.
19 @return NULL Can't find the Nic handle.
23 HttpBootGetNicByIp4Children (
24 IN EFI_HANDLE ControllerHandle
29 NicHandle
= NetLibGetNicHandle (ControllerHandle
, &gEfiHttpProtocolGuid
);
30 if (NicHandle
== NULL
) {
31 NicHandle
= NetLibGetNicHandle (ControllerHandle
, &gEfiDhcp4ProtocolGuid
);
32 if (NicHandle
== NULL
) {
41 Get the Nic handle using any child handle in the IPv6 stack.
43 @param[in] ControllerHandle Pointer to child handle over IPv6.
45 @return NicHandle The pointer to the Nic handle.
46 @return NULL Can't find the Nic handle.
50 HttpBootGetNicByIp6Children (
51 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
++) {
126 if (Ip
->Addr
[Index
] != 0) {
127 AsciiPrint ("%x", Ip
->Addr
[Index
]);
133 if (((Ip
->Addr
[Index
] & 0xf0) == 0) && (Ip
->Addr
[Index
- 1] != 0)) {
136 AsciiPrint ("%x", Ip
->Addr
[Index
]);
144 This function is to display the HTTP error status.
146 @param[in] StatusCode The status code value in HTTP message.
150 HttpBootPrintErrorMessage (
151 EFI_HTTP_STATUS_CODE StatusCode
156 switch (StatusCode
) {
157 case HTTP_STATUS_300_MULTIPLE_CHOICES
:
158 AsciiPrint ("\n Redirection: 300 Multiple Choices");
161 case HTTP_STATUS_301_MOVED_PERMANENTLY
:
162 AsciiPrint ("\n Redirection: 301 Moved Permanently");
165 case HTTP_STATUS_302_FOUND
:
166 AsciiPrint ("\n Redirection: 302 Found");
169 case HTTP_STATUS_303_SEE_OTHER
:
170 AsciiPrint ("\n Redirection: 303 See Other");
173 case HTTP_STATUS_304_NOT_MODIFIED
:
174 AsciiPrint ("\n Redirection: 304 Not Modified");
177 case HTTP_STATUS_305_USE_PROXY
:
178 AsciiPrint ("\n Redirection: 305 Use Proxy");
181 case HTTP_STATUS_307_TEMPORARY_REDIRECT
:
182 AsciiPrint ("\n Redirection: 307 Temporary Redirect");
185 case HTTP_STATUS_308_PERMANENT_REDIRECT
:
186 AsciiPrint ("\n Redirection: 308 Permanent Redirect");
189 case HTTP_STATUS_400_BAD_REQUEST
:
190 AsciiPrint ("\n Client Error: 400 Bad Request");
193 case HTTP_STATUS_401_UNAUTHORIZED
:
194 AsciiPrint ("\n Client Error: 401 Unauthorized");
197 case HTTP_STATUS_402_PAYMENT_REQUIRED
:
198 AsciiPrint ("\n Client Error: 402 Payment Required");
201 case HTTP_STATUS_403_FORBIDDEN
:
202 AsciiPrint ("\n Client Error: 403 Forbidden");
205 case HTTP_STATUS_404_NOT_FOUND
:
206 AsciiPrint ("\n Client Error: 404 Not Found");
209 case HTTP_STATUS_405_METHOD_NOT_ALLOWED
:
210 AsciiPrint ("\n Client Error: 405 Method Not Allowed");
213 case HTTP_STATUS_406_NOT_ACCEPTABLE
:
214 AsciiPrint ("\n Client Error: 406 Not Acceptable");
217 case HTTP_STATUS_407_PROXY_AUTHENTICATION_REQUIRED
:
218 AsciiPrint ("\n Client Error: 407 Proxy Authentication Required");
221 case HTTP_STATUS_408_REQUEST_TIME_OUT
:
222 AsciiPrint ("\n Client Error: 408 Request Timeout");
225 case HTTP_STATUS_409_CONFLICT
:
226 AsciiPrint ("\n Client Error: 409 Conflict");
229 case HTTP_STATUS_410_GONE
:
230 AsciiPrint ("\n Client Error: 410 Gone");
233 case HTTP_STATUS_411_LENGTH_REQUIRED
:
234 AsciiPrint ("\n Client Error: 411 Length Required");
237 case HTTP_STATUS_412_PRECONDITION_FAILED
:
238 AsciiPrint ("\n Client Error: 412 Precondition Failed");
241 case HTTP_STATUS_413_REQUEST_ENTITY_TOO_LARGE
:
242 AsciiPrint ("\n Client Error: 413 Request Entity Too Large");
245 case HTTP_STATUS_414_REQUEST_URI_TOO_LARGE
:
246 AsciiPrint ("\n Client Error: 414 Request URI Too Long");
249 case HTTP_STATUS_415_UNSUPPORTED_MEDIA_TYPE
:
250 AsciiPrint ("\n Client Error: 415 Unsupported Media Type");
253 case HTTP_STATUS_416_REQUESTED_RANGE_NOT_SATISFIED
:
254 AsciiPrint ("\n Client Error: 416 Requested Range Not Satisfiable");
257 case HTTP_STATUS_417_EXPECTATION_FAILED
:
258 AsciiPrint ("\n Client Error: 417 Expectation Failed");
261 case HTTP_STATUS_500_INTERNAL_SERVER_ERROR
:
262 AsciiPrint ("\n Server Error: 500 Internal Server Error");
265 case HTTP_STATUS_501_NOT_IMPLEMENTED
:
266 AsciiPrint ("\n Server Error: 501 Not Implemented");
269 case HTTP_STATUS_502_BAD_GATEWAY
:
270 AsciiPrint ("\n Server Error: 502 Bad Gateway");
273 case HTTP_STATUS_503_SERVICE_UNAVAILABLE
:
274 AsciiPrint ("\n Server Error: 503 Service Unavailable");
277 case HTTP_STATUS_504_GATEWAY_TIME_OUT
:
278 AsciiPrint ("\n Server Error: 504 Gateway Timeout");
281 case HTTP_STATUS_505_HTTP_VERSION_NOT_SUPPORTED
:
282 AsciiPrint ("\n Server Error: 505 HTTP Version Not Supported");
291 Notify the callback function when an event is triggered.
293 @param[in] Event The triggered event.
294 @param[in] Context The opaque parameter to the function.
299 HttpBootCommonNotify (
304 *((BOOLEAN
*) Context
) = TRUE
;
308 Retrieve the host address using the EFI_DNS6_PROTOCOL.
310 @param[in] Private The pointer to the driver's private data.
311 @param[in] HostName Pointer to buffer containing hostname.
312 @param[out] IpAddress On output, pointer to buffer containing IPv6 address.
314 @retval EFI_SUCCESS Operation succeeded.
315 @retval EFI_DEVICE_ERROR An unexpected network error occurred.
316 @retval Others Other errors as indicated.
320 IN HTTP_BOOT_PRIVATE_DATA
*Private
,
322 OUT EFI_IPv6_ADDRESS
*IpAddress
326 EFI_DNS6_PROTOCOL
*Dns6
;
327 EFI_DNS6_CONFIG_DATA Dns6ConfigData
;
328 EFI_DNS6_COMPLETION_TOKEN Token
;
329 EFI_HANDLE Dns6Handle
;
330 EFI_IP6_CONFIG_PROTOCOL
*Ip6Config
;
331 EFI_IPv6_ADDRESS
*DnsServerList
;
332 UINTN DnsServerListCount
;
336 DnsServerList
= NULL
;
337 DnsServerListCount
= 0;
340 ZeroMem (&Token
, sizeof (EFI_DNS6_COMPLETION_TOKEN
));
343 // Get DNS server list from EFI IPv6 Configuration protocol.
345 Status
= gBS
->HandleProtocol (Private
->Controller
, &gEfiIp6ConfigProtocolGuid
, (VOID
**) &Ip6Config
);
346 if (!EFI_ERROR (Status
)) {
348 // Get the required size.
351 Status
= Ip6Config
->GetData (Ip6Config
, Ip6ConfigDataTypeDnsServer
, &DataSize
, NULL
);
352 if (Status
== EFI_BUFFER_TOO_SMALL
) {
353 DnsServerList
= AllocatePool (DataSize
);
354 if (DnsServerList
== NULL
) {
355 return EFI_OUT_OF_RESOURCES
;
358 Status
= Ip6Config
->GetData (Ip6Config
, Ip6ConfigDataTypeDnsServer
, &DataSize
, DnsServerList
);
359 if (EFI_ERROR (Status
)) {
360 FreePool (DnsServerList
);
361 DnsServerList
= NULL
;
363 DnsServerListCount
= DataSize
/ sizeof (EFI_IPv6_ADDRESS
);
368 // Create a DNSv6 child instance and get the protocol.
370 Status
= NetLibCreateServiceChild (
372 Private
->Ip6Nic
->ImageHandle
,
373 &gEfiDns6ServiceBindingProtocolGuid
,
376 if (EFI_ERROR (Status
)) {
380 Status
= gBS
->OpenProtocol (
382 &gEfiDns6ProtocolGuid
,
384 Private
->Ip6Nic
->ImageHandle
,
386 EFI_OPEN_PROTOCOL_BY_DRIVER
388 if (EFI_ERROR (Status
)) {
393 // Configure DNS6 instance for the DNS server address and protocol.
395 ZeroMem (&Dns6ConfigData
, sizeof (EFI_DNS6_CONFIG_DATA
));
396 Dns6ConfigData
.DnsServerCount
= (UINT32
)DnsServerListCount
;
397 Dns6ConfigData
.DnsServerList
= DnsServerList
;
398 Dns6ConfigData
.EnableDnsCache
= TRUE
;
399 Dns6ConfigData
.Protocol
= EFI_IP_PROTO_UDP
;
400 IP6_COPY_ADDRESS (&Dns6ConfigData
.StationIp
,&Private
->StationIp
.v6
);
401 Status
= Dns6
->Configure (
405 if (EFI_ERROR (Status
)) {
409 Token
.Status
= EFI_NOT_READY
;
412 // Create event to set the IsDone flag when name resolution is finished.
414 Status
= gBS
->CreateEvent (
417 HttpBootCommonNotify
,
421 if (EFI_ERROR (Status
)) {
426 // Start asynchronous name resolution.
428 Status
= Dns6
->HostNameToIp (Dns6
, HostName
, &Token
);
429 if (EFI_ERROR (Status
)) {
438 // Name resolution is done, check result.
440 Status
= Token
.Status
;
441 if (!EFI_ERROR (Status
)) {
442 if (Token
.RspData
.H2AData
== NULL
) {
443 Status
= EFI_DEVICE_ERROR
;
446 if (Token
.RspData
.H2AData
->IpCount
== 0 || Token
.RspData
.H2AData
->IpList
== NULL
) {
447 Status
= EFI_DEVICE_ERROR
;
451 // We just return the first IPv6 address from DNS protocol.
453 IP6_COPY_ADDRESS (IpAddress
, Token
.RspData
.H2AData
->IpList
);
454 Status
= EFI_SUCCESS
;
458 if (Token
.Event
!= NULL
) {
459 gBS
->CloseEvent (Token
.Event
);
461 if (Token
.RspData
.H2AData
!= NULL
) {
462 if (Token
.RspData
.H2AData
->IpList
!= NULL
) {
463 FreePool (Token
.RspData
.H2AData
->IpList
);
465 FreePool (Token
.RspData
.H2AData
);
469 Dns6
->Configure (Dns6
, NULL
);
473 &gEfiDns6ProtocolGuid
,
474 Private
->Ip6Nic
->ImageHandle
,
479 if (Dns6Handle
!= NULL
) {
480 NetLibDestroyServiceChild (
482 Private
->Ip6Nic
->ImageHandle
,
483 &gEfiDns6ServiceBindingProtocolGuid
,
488 if (DnsServerList
!= NULL
) {
489 FreePool (DnsServerList
);
496 This function checks the HTTP(S) URI scheme.
498 @param[in] Uri The pointer to the URI string.
500 @retval EFI_SUCCESS The URI scheme is valid.
501 @retval EFI_INVALID_PARAMETER The URI scheme is not HTTP or HTTPS.
502 @retval EFI_ACCESS_DENIED HTTP is disabled and the URI is HTTP.
506 HttpBootCheckUriScheme (
513 Status
= EFI_SUCCESS
;
516 // Convert the scheme to all lower case.
518 for (Index
= 0; Index
< AsciiStrLen (Uri
); Index
++) {
519 if (Uri
[Index
] == ':') {
522 if (Uri
[Index
] >= 'A' && Uri
[Index
] <= 'Z') {
523 Uri
[Index
] -= (CHAR8
)('A' - 'a');
528 // Return EFI_INVALID_PARAMETER if the URI is not HTTP or HTTPS.
530 if ((AsciiStrnCmp (Uri
, "http://", 7) != 0) && (AsciiStrnCmp (Uri
, "https://", 8) != 0)) {
531 DEBUG ((EFI_D_ERROR
, "HttpBootCheckUriScheme: Invalid Uri.\n"));
532 return EFI_INVALID_PARAMETER
;
536 // HTTP is disabled, return EFI_ACCESS_DENIED if the URI is HTTP.
538 if (!PcdGetBool (PcdAllowHttpConnections
) && (AsciiStrnCmp (Uri
, "http://", 7) == 0)) {
539 DEBUG ((EFI_D_ERROR
, "HttpBootCheckUriScheme: HTTP is disabled.\n"));
540 return EFI_ACCESS_DENIED
;
547 Get the URI address string from the input device path.
549 Caller need to free the buffer in the UriAddress pointer.
551 @param[in] FilePath Pointer to the device path which contains a URI device path node.
552 @param[out] UriAddress The URI address string extract from the device path.
554 @retval EFI_SUCCESS The URI string is returned.
555 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory.
559 HttpBootParseFilePath (
560 IN EFI_DEVICE_PATH_PROTOCOL
*FilePath
,
561 OUT CHAR8
**UriAddress
564 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePath
;
565 URI_DEVICE_PATH
*UriDevicePath
;
569 if (FilePath
== NULL
) {
570 return EFI_INVALID_PARAMETER
;
576 // Extract the URI address from the FilePath
578 TempDevicePath
= FilePath
;
579 while (!IsDevicePathEnd (TempDevicePath
)) {
580 if ((DevicePathType (TempDevicePath
) == MESSAGING_DEVICE_PATH
) &&
581 (DevicePathSubType (TempDevicePath
) == MSG_URI_DP
)) {
582 UriDevicePath
= (URI_DEVICE_PATH
*) TempDevicePath
;
584 // UEFI Spec doesn't require the URI to be a NULL-terminated string
585 // So we allocate a new buffer and always append a '\0' to it.
587 UriStrLength
= DevicePathNodeLength (UriDevicePath
) - sizeof(EFI_DEVICE_PATH_PROTOCOL
);
588 if (UriStrLength
== 0) {
590 // return a NULL UriAddress if it's a empty URI device path node.
594 Uri
= AllocatePool (UriStrLength
+ 1);
596 return EFI_OUT_OF_RESOURCES
;
598 CopyMem (Uri
, UriDevicePath
->Uri
, DevicePathNodeLength (UriDevicePath
) - sizeof(EFI_DEVICE_PATH_PROTOCOL
));
599 Uri
[DevicePathNodeLength (UriDevicePath
) - sizeof(EFI_DEVICE_PATH_PROTOCOL
)] = '\0';
603 TempDevicePath
= NextDevicePathNode (TempDevicePath
);
610 This function returns the image type according to server replied HTTP message
611 and also the image's URI info.
613 @param[in] Uri The pointer to the image's URI string.
614 @param[in] UriParser URI Parse result returned by NetHttpParseUrl().
615 @param[in] HeaderCount Number of HTTP header structures in Headers list.
616 @param[in] Headers Array containing list of HTTP headers.
617 @param[out] ImageType The image type of the downloaded file.
619 @retval EFI_SUCCESS The image type is returned in ImageType.
620 @retval EFI_INVALID_PARAMETER ImageType, Uri or UriParser is NULL.
621 @retval EFI_INVALID_PARAMETER HeaderCount is not zero, and Headers is NULL.
622 @retval EFI_NOT_FOUND Failed to identify the image type.
623 @retval Others Unexpected error happened.
627 HttpBootCheckImageType (
630 IN UINTN HeaderCount
,
631 IN EFI_HTTP_HEADER
*Headers
,
632 OUT HTTP_BOOT_IMAGE_TYPE
*ImageType
636 EFI_HTTP_HEADER
*Header
;
640 if (Uri
== NULL
|| UriParser
== NULL
|| ImageType
== NULL
) {
641 return EFI_INVALID_PARAMETER
;
644 if (HeaderCount
!= 0 && Headers
== NULL
) {
645 return EFI_INVALID_PARAMETER
;
649 // Determine the image type by the HTTP Content-Type header field first.
650 // "application/efi" -> EFI Image
651 // "application/vnd.efi-iso" -> CD/DVD Image
652 // "application/vnd.efi-img" -> Virtual Disk Image
654 Header
= HttpFindHeader (HeaderCount
, Headers
, HTTP_HEADER_CONTENT_TYPE
);
655 if (Header
!= NULL
) {
656 if (AsciiStriCmp (Header
->FieldValue
, HTTP_CONTENT_TYPE_APP_EFI
) == 0) {
657 *ImageType
= ImageTypeEfi
;
659 } else if (AsciiStriCmp (Header
->FieldValue
, HTTP_CONTENT_TYPE_APP_ISO
) == 0) {
660 *ImageType
= ImageTypeVirtualCd
;
662 } else if (AsciiStriCmp (Header
->FieldValue
, HTTP_CONTENT_TYPE_APP_IMG
) == 0) {
663 *ImageType
= ImageTypeVirtualDisk
;
669 // Determine the image type by file extension:
670 // *.efi -> EFI Image
671 // *.iso -> CD/DVD Image
672 // *.img -> Virtual Disk Image
674 Status
= HttpUrlGetPath (
679 if (EFI_ERROR (Status
)) {
683 FilePost
= FilePath
+ AsciiStrLen (FilePath
) - 4;
684 if (AsciiStrCmp (FilePost
, ".efi") == 0) {
685 *ImageType
= ImageTypeEfi
;
686 } else if (AsciiStrCmp (FilePost
, ".iso") == 0) {
687 *ImageType
= ImageTypeVirtualCd
;
688 } else if (AsciiStrCmp (FilePost
, ".img") == 0) {
689 *ImageType
= ImageTypeVirtualDisk
;
691 *ImageType
= ImageTypeMax
;
696 return (*ImageType
< ImageTypeMax
) ? EFI_SUCCESS
: EFI_NOT_FOUND
;
700 This function register the RAM disk info to the system.
702 @param[in] Private The pointer to the driver's private data.
703 @param[in] BufferSize The size of Buffer in bytes.
704 @param[in] Buffer The base address of the RAM disk.
705 @param[in] ImageType The image type of the file in Buffer.
707 @retval EFI_SUCCESS The RAM disk has been registered.
708 @retval EFI_NOT_FOUND No RAM disk protocol instances were found.
709 @retval EFI_UNSUPPORTED The ImageType is not supported.
710 @retval Others Unexpected error happened.
714 HttpBootRegisterRamDisk (
715 IN HTTP_BOOT_PRIVATE_DATA
*Private
,
718 IN HTTP_BOOT_IMAGE_TYPE ImageType
721 EFI_RAM_DISK_PROTOCOL
*RamDisk
;
723 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
724 EFI_GUID
*RamDiskType
;
726 ASSERT (Private
!= NULL
);
727 ASSERT (Buffer
!= NULL
);
728 ASSERT (BufferSize
!= 0);
730 Status
= gBS
->LocateProtocol (&gEfiRamDiskProtocolGuid
, NULL
, (VOID
**) &RamDisk
);
731 if (EFI_ERROR (Status
)) {
732 DEBUG ((EFI_D_ERROR
, "HTTP Boot: Couldn't find the RAM Disk protocol - %r\n", Status
));
736 if (ImageType
== ImageTypeVirtualCd
) {
737 RamDiskType
= &gEfiVirtualCdGuid
;
738 } else if (ImageType
== ImageTypeVirtualDisk
) {
739 RamDiskType
= &gEfiVirtualDiskGuid
;
741 return EFI_UNSUPPORTED
;
744 Status
= RamDisk
->Register (
748 Private
->UsingIpv6
? Private
->Ip6Nic
->DevicePath
: Private
->Ip4Nic
->DevicePath
,
751 if (EFI_ERROR (Status
)) {
752 DEBUG ((EFI_D_ERROR
, "HTTP Boot: Failed to register RAM Disk - %r\n", Status
));
759 Indicate if the HTTP status code indicates a redirection.
761 @param[in] StatusCode HTTP status code from server.
763 @return TRUE if it's redirection.
767 HttpBootIsHttpRedirectStatusCode (
768 IN EFI_HTTP_STATUS_CODE StatusCode
771 if (StatusCode
== HTTP_STATUS_301_MOVED_PERMANENTLY
||
772 StatusCode
== HTTP_STATUS_302_FOUND
||
773 StatusCode
== HTTP_STATUS_307_TEMPORARY_REDIRECT
||
774 StatusCode
== HTTP_STATUS_308_PERMANENT_REDIRECT
) {