2 Support functions implementation for UEFI HTTP boot driver.
4 Copyright (c) 2015 - 2018, Intel Corporation. All rights reserved.<BR>
5 (C) Copyright 2016 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
);
495 Create a HTTP_IO_HEADER to hold the HTTP header items.
497 @param[in] MaxHeaderCount The maximun number of HTTP header in this holder.
499 @return A pointer of the HTTP header holder or NULL if failed.
503 HttpBootCreateHeader (
507 HTTP_IO_HEADER
*HttpIoHeader
;
509 if (MaxHeaderCount
== 0) {
513 HttpIoHeader
= AllocateZeroPool (sizeof (HTTP_IO_HEADER
) + MaxHeaderCount
* sizeof (EFI_HTTP_HEADER
));
514 if (HttpIoHeader
== NULL
) {
518 HttpIoHeader
->MaxHeaderCount
= MaxHeaderCount
;
519 HttpIoHeader
->Headers
= (EFI_HTTP_HEADER
*) (HttpIoHeader
+ 1);
525 Destroy the HTTP_IO_HEADER and release the resouces.
527 @param[in] HttpIoHeader Point to the HTTP header holder to be destroyed.
532 IN HTTP_IO_HEADER
*HttpIoHeader
537 if (HttpIoHeader
!= NULL
) {
538 if (HttpIoHeader
->HeaderCount
!= 0) {
539 for (Index
= 0; Index
< HttpIoHeader
->HeaderCount
; Index
++) {
540 FreePool (HttpIoHeader
->Headers
[Index
].FieldName
);
541 FreePool (HttpIoHeader
->Headers
[Index
].FieldValue
);
544 FreePool (HttpIoHeader
);
549 Set or update a HTTP header with the field name and corresponding value.
551 @param[in] HttpIoHeader Point to the HTTP header holder.
552 @param[in] FieldName Null terminated string which describes a field name.
553 @param[in] FieldValue Null terminated string which describes the corresponding field value.
555 @retval EFI_SUCCESS The HTTP header has been set or updated.
556 @retval EFI_INVALID_PARAMETER Any input parameter is invalid.
557 @retval EFI_OUT_OF_RESOURCES Insufficient resource to complete the operation.
558 @retval Other Unexpected error happened.
563 IN HTTP_IO_HEADER
*HttpIoHeader
,
568 EFI_HTTP_HEADER
*Header
;
570 CHAR8
*NewFieldValue
;
572 if (HttpIoHeader
== NULL
|| FieldName
== NULL
|| FieldValue
== NULL
) {
573 return EFI_INVALID_PARAMETER
;
576 Header
= HttpFindHeader (HttpIoHeader
->HeaderCount
, HttpIoHeader
->Headers
, FieldName
);
577 if (Header
== NULL
) {
581 if (HttpIoHeader
->HeaderCount
>= HttpIoHeader
->MaxHeaderCount
) {
582 return EFI_OUT_OF_RESOURCES
;
584 Header
= &HttpIoHeader
->Headers
[HttpIoHeader
->HeaderCount
];
586 StrSize
= AsciiStrSize (FieldName
);
587 Header
->FieldName
= AllocatePool (StrSize
);
588 if (Header
->FieldName
== NULL
) {
589 return EFI_OUT_OF_RESOURCES
;
591 CopyMem (Header
->FieldName
, FieldName
, StrSize
);
592 Header
->FieldName
[StrSize
-1] = '\0';
594 StrSize
= AsciiStrSize (FieldValue
);
595 Header
->FieldValue
= AllocatePool (StrSize
);
596 if (Header
->FieldValue
== NULL
) {
597 FreePool (Header
->FieldName
);
598 return EFI_OUT_OF_RESOURCES
;
600 CopyMem (Header
->FieldValue
, FieldValue
, StrSize
);
601 Header
->FieldValue
[StrSize
-1] = '\0';
603 HttpIoHeader
->HeaderCount
++;
606 // Update an existing one.
608 StrSize
= AsciiStrSize (FieldValue
);
609 NewFieldValue
= AllocatePool (StrSize
);
610 if (NewFieldValue
== NULL
) {
611 return EFI_OUT_OF_RESOURCES
;
613 CopyMem (NewFieldValue
, FieldValue
, StrSize
);
614 NewFieldValue
[StrSize
-1] = '\0';
616 if (Header
->FieldValue
!= NULL
) {
617 FreePool (Header
->FieldValue
);
619 Header
->FieldValue
= NewFieldValue
;
626 Notify the callback function when an event is triggered.
628 @param[in] Context The opaque parameter to the function.
637 *((BOOLEAN
*) Context
) = TRUE
;
641 Request HttpIoNotifyDpc as a DPC at TPL_CALLBACK.
643 @param[in] Event The event signaled.
644 @param[in] Context The opaque parameter to the function.
655 // Request HttpIoNotifyDpc as a DPC at TPL_CALLBACK
657 QueueDpc (TPL_CALLBACK
, HttpIoNotifyDpc
, Context
);
661 Create a HTTP_IO to access the HTTP service. It will create and configure
664 @param[in] Image The handle of the driver image.
665 @param[in] Controller The handle of the controller.
666 @param[in] IpVersion IP_VERSION_4 or IP_VERSION_6.
667 @param[in] ConfigData The HTTP_IO configuration data.
668 @param[in] Callback Callback function which will be invoked when specified
669 HTTP_IO_CALLBACK_EVENT happened.
670 @param[in] Context The Context data which will be passed to the Callback function.
671 @param[out] HttpIo The HTTP_IO.
673 @retval EFI_SUCCESS The HTTP_IO is created and configured.
674 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
675 @retval EFI_UNSUPPORTED One or more of the control options are not
676 supported in the implementation.
677 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory.
678 @retval Others Failed to create the HTTP_IO or configure it.
684 IN EFI_HANDLE Controller
,
686 IN HTTP_IO_CONFIG_DATA
*ConfigData
,
687 IN HTTP_IO_CALLBACK Callback
,
693 EFI_HTTP_CONFIG_DATA HttpConfigData
;
694 EFI_HTTPv4_ACCESS_POINT Http4AccessPoint
;
695 EFI_HTTPv6_ACCESS_POINT Http6AccessPoint
;
696 EFI_HTTP_PROTOCOL
*Http
;
699 if ((Image
== NULL
) || (Controller
== NULL
) || (ConfigData
== NULL
) || (HttpIo
== NULL
)) {
700 return EFI_INVALID_PARAMETER
;
703 if (IpVersion
!= IP_VERSION_4
&& IpVersion
!= IP_VERSION_6
) {
704 return EFI_UNSUPPORTED
;
707 ZeroMem (HttpIo
, sizeof (HTTP_IO
));
710 // Create the HTTP child instance and get the HTTP protocol.
712 Status
= NetLibCreateServiceChild (
715 &gEfiHttpServiceBindingProtocolGuid
,
718 if (EFI_ERROR (Status
)) {
722 Status
= gBS
->OpenProtocol (
724 &gEfiHttpProtocolGuid
,
728 EFI_OPEN_PROTOCOL_BY_DRIVER
730 if (EFI_ERROR (Status
) || (Http
== NULL
)) {
735 // Init the configuration data and configure the HTTP child.
737 HttpIo
->Image
= Image
;
738 HttpIo
->Controller
= Controller
;
739 HttpIo
->IpVersion
= IpVersion
;
741 HttpIo
->Callback
= Callback
;
742 HttpIo
->Context
= Context
;
744 ZeroMem (&HttpConfigData
, sizeof (EFI_HTTP_CONFIG_DATA
));
745 HttpConfigData
.HttpVersion
= HttpVersion11
;
746 HttpConfigData
.TimeOutMillisec
= ConfigData
->Config4
.RequestTimeOut
;
747 if (HttpIo
->IpVersion
== IP_VERSION_4
) {
748 HttpConfigData
.LocalAddressIsIPv6
= FALSE
;
750 Http4AccessPoint
.UseDefaultAddress
= ConfigData
->Config4
.UseDefaultAddress
;
751 Http4AccessPoint
.LocalPort
= ConfigData
->Config4
.LocalPort
;
752 IP4_COPY_ADDRESS (&Http4AccessPoint
.LocalAddress
, &ConfigData
->Config4
.LocalIp
);
753 IP4_COPY_ADDRESS (&Http4AccessPoint
.LocalSubnet
, &ConfigData
->Config4
.SubnetMask
);
754 HttpConfigData
.AccessPoint
.IPv4Node
= &Http4AccessPoint
;
756 HttpConfigData
.LocalAddressIsIPv6
= TRUE
;
757 Http6AccessPoint
.LocalPort
= ConfigData
->Config6
.LocalPort
;
758 IP6_COPY_ADDRESS (&Http6AccessPoint
.LocalAddress
, &ConfigData
->Config6
.LocalIp
);
759 HttpConfigData
.AccessPoint
.IPv6Node
= &Http6AccessPoint
;
762 Status
= Http
->Configure (Http
, &HttpConfigData
);
763 if (EFI_ERROR (Status
)) {
768 // Create events for variuos asynchronous operations.
770 Status
= gBS
->CreateEvent (
777 if (EFI_ERROR (Status
)) {
780 HttpIo
->ReqToken
.Event
= Event
;
781 HttpIo
->ReqToken
.Message
= &HttpIo
->ReqMessage
;
783 Status
= gBS
->CreateEvent (
790 if (EFI_ERROR (Status
)) {
793 HttpIo
->RspToken
.Event
= Event
;
794 HttpIo
->RspToken
.Message
= &HttpIo
->RspMessage
;
797 // Create TimeoutEvent for response
799 Status
= gBS
->CreateEvent (
806 if (EFI_ERROR (Status
)) {
809 HttpIo
->TimeoutEvent
= Event
;
814 HttpIoDestroyIo (HttpIo
);
820 Destroy the HTTP_IO and release the resouces.
822 @param[in] HttpIo The HTTP_IO which wraps the HTTP service to be destroyed.
830 EFI_HTTP_PROTOCOL
*Http
;
833 if (HttpIo
== NULL
) {
837 Event
= HttpIo
->ReqToken
.Event
;
839 gBS
->CloseEvent (Event
);
842 Event
= HttpIo
->RspToken
.Event
;
844 gBS
->CloseEvent (Event
);
847 Event
= HttpIo
->TimeoutEvent
;
849 gBS
->CloseEvent (Event
);
854 Http
->Configure (Http
, NULL
);
857 &gEfiHttpProtocolGuid
,
863 NetLibDestroyServiceChild (
866 &gEfiHttpServiceBindingProtocolGuid
,
872 Synchronously send a HTTP REQUEST message to the server.
874 @param[in] HttpIo The HttpIo wrapping the HTTP service.
875 @param[in] Request A pointer to storage such data as URL and HTTP method.
876 @param[in] HeaderCount Number of HTTP header structures in Headers list.
877 @param[in] Headers Array containing list of HTTP headers.
878 @param[in] BodyLength Length in bytes of the HTTP body.
879 @param[in] Body Body associated with the HTTP request.
881 @retval EFI_SUCCESS The HTTP request is trasmitted.
882 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
883 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory.
884 @retval EFI_DEVICE_ERROR An unexpected network or system error occurred.
885 @retval Others Other errors as indicated.
891 IN EFI_HTTP_REQUEST_DATA
*Request
,
892 IN UINTN HeaderCount
,
893 IN EFI_HTTP_HEADER
*Headers
,
899 EFI_HTTP_PROTOCOL
*Http
;
901 if (HttpIo
== NULL
|| HttpIo
->Http
== NULL
) {
902 return EFI_INVALID_PARAMETER
;
905 HttpIo
->ReqToken
.Status
= EFI_NOT_READY
;
906 HttpIo
->ReqToken
.Message
->Data
.Request
= Request
;
907 HttpIo
->ReqToken
.Message
->HeaderCount
= HeaderCount
;
908 HttpIo
->ReqToken
.Message
->Headers
= Headers
;
909 HttpIo
->ReqToken
.Message
->BodyLength
= BodyLength
;
910 HttpIo
->ReqToken
.Message
->Body
= Body
;
912 if (HttpIo
->Callback
!= NULL
) {
913 Status
= HttpIo
->Callback (
915 HttpIo
->ReqToken
.Message
,
918 if (EFI_ERROR (Status
)) {
924 // Queue the request token to HTTP instances.
927 HttpIo
->IsTxDone
= FALSE
;
928 Status
= Http
->Request (
932 if (EFI_ERROR (Status
)) {
937 // Poll the network until transmit finish.
939 while (!HttpIo
->IsTxDone
) {
943 return HttpIo
->ReqToken
.Status
;
947 Synchronously receive a HTTP RESPONSE message from the server.
949 @param[in] HttpIo The HttpIo wrapping the HTTP service.
950 @param[in] RecvMsgHeader TRUE to receive a new HTTP response (from message header).
951 FALSE to continue receive the previous response message.
952 @param[out] ResponseData Point to a wrapper of the received response data.
954 @retval EFI_SUCCESS The HTTP response is received.
955 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
956 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory.
957 @retval EFI_DEVICE_ERROR An unexpected network or system error occurred.
958 @retval Others Other errors as indicated.
964 IN BOOLEAN RecvMsgHeader
,
965 OUT HTTP_IO_RESPONSE_DATA
*ResponseData
969 EFI_HTTP_PROTOCOL
*Http
;
971 if (HttpIo
== NULL
|| HttpIo
->Http
== NULL
|| ResponseData
== NULL
) {
972 return EFI_INVALID_PARAMETER
;
976 // Start the timer, and wait Timeout seconds to receive the header packet.
978 Status
= gBS
->SetTimer (HttpIo
->TimeoutEvent
, TimerRelative
, HTTP_BOOT_RESPONSE_TIMEOUT
* TICKS_PER_MS
);
979 if (EFI_ERROR (Status
)) {
984 // Queue the response token to HTTP instances.
986 HttpIo
->RspToken
.Status
= EFI_NOT_READY
;
988 HttpIo
->RspToken
.Message
->Data
.Response
= &ResponseData
->Response
;
990 HttpIo
->RspToken
.Message
->Data
.Response
= NULL
;
992 HttpIo
->RspToken
.Message
->HeaderCount
= 0;
993 HttpIo
->RspToken
.Message
->Headers
= NULL
;
994 HttpIo
->RspToken
.Message
->BodyLength
= ResponseData
->BodyLength
;
995 HttpIo
->RspToken
.Message
->Body
= ResponseData
->Body
;
998 HttpIo
->IsRxDone
= FALSE
;
999 Status
= Http
->Response (
1004 if (EFI_ERROR (Status
)) {
1005 gBS
->SetTimer (HttpIo
->TimeoutEvent
, TimerCancel
, 0);
1010 // Poll the network until receive finish.
1012 while (!HttpIo
->IsRxDone
&& ((HttpIo
->TimeoutEvent
== NULL
) || EFI_ERROR (gBS
->CheckEvent (HttpIo
->TimeoutEvent
)))) {
1016 gBS
->SetTimer (HttpIo
->TimeoutEvent
, TimerCancel
, 0);
1018 if (!HttpIo
->IsRxDone
) {
1020 // Timeout occurs, cancel the response token.
1022 Http
->Cancel (Http
, &HttpIo
->RspToken
);
1024 Status
= EFI_TIMEOUT
;
1028 HttpIo
->IsRxDone
= FALSE
;
1031 if ((HttpIo
->Callback
!= NULL
) &&
1032 (HttpIo
->RspToken
.Status
== EFI_SUCCESS
|| HttpIo
->RspToken
.Status
== EFI_HTTP_ERROR
)) {
1033 Status
= HttpIo
->Callback (
1035 HttpIo
->RspToken
.Message
,
1038 if (EFI_ERROR (Status
)) {
1044 // Store the received data into the wrapper.
1046 ResponseData
->Status
= HttpIo
->RspToken
.Status
;
1047 ResponseData
->HeaderCount
= HttpIo
->RspToken
.Message
->HeaderCount
;
1048 ResponseData
->Headers
= HttpIo
->RspToken
.Message
->Headers
;
1049 ResponseData
->BodyLength
= HttpIo
->RspToken
.Message
->BodyLength
;
1055 This function checks the HTTP(S) URI scheme.
1057 @param[in] Uri The pointer to the URI string.
1059 @retval EFI_SUCCESS The URI scheme is valid.
1060 @retval EFI_INVALID_PARAMETER The URI scheme is not HTTP or HTTPS.
1061 @retval EFI_ACCESS_DENIED HTTP is disabled and the URI is HTTP.
1065 HttpBootCheckUriScheme (
1072 Status
= EFI_SUCCESS
;
1075 // Convert the scheme to all lower case.
1077 for (Index
= 0; Index
< AsciiStrLen (Uri
); Index
++) {
1078 if (Uri
[Index
] == ':') {
1081 if (Uri
[Index
] >= 'A' && Uri
[Index
] <= 'Z') {
1082 Uri
[Index
] -= (CHAR8
)('A' - 'a');
1087 // Return EFI_INVALID_PARAMETER if the URI is not HTTP or HTTPS.
1089 if ((AsciiStrnCmp (Uri
, "http://", 7) != 0) && (AsciiStrnCmp (Uri
, "https://", 8) != 0)) {
1090 DEBUG ((EFI_D_ERROR
, "HttpBootCheckUriScheme: Invalid Uri.\n"));
1091 return EFI_INVALID_PARAMETER
;
1095 // HTTP is disabled, return EFI_ACCESS_DENIED if the URI is HTTP.
1097 if (!PcdGetBool (PcdAllowHttpConnections
) && (AsciiStrnCmp (Uri
, "http://", 7) == 0)) {
1098 DEBUG ((EFI_D_ERROR
, "HttpBootCheckUriScheme: HTTP is disabled.\n"));
1099 return EFI_ACCESS_DENIED
;
1106 Get the URI address string from the input device path.
1108 Caller need to free the buffer in the UriAddress pointer.
1110 @param[in] FilePath Pointer to the device path which contains a URI device path node.
1111 @param[out] UriAddress The URI address string extract from the device path.
1113 @retval EFI_SUCCESS The URI string is returned.
1114 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory.
1118 HttpBootParseFilePath (
1119 IN EFI_DEVICE_PATH_PROTOCOL
*FilePath
,
1120 OUT CHAR8
**UriAddress
1123 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePath
;
1124 URI_DEVICE_PATH
*UriDevicePath
;
1128 if (FilePath
== NULL
) {
1129 return EFI_INVALID_PARAMETER
;
1135 // Extract the URI address from the FilePath
1137 TempDevicePath
= FilePath
;
1138 while (!IsDevicePathEnd (TempDevicePath
)) {
1139 if ((DevicePathType (TempDevicePath
) == MESSAGING_DEVICE_PATH
) &&
1140 (DevicePathSubType (TempDevicePath
) == MSG_URI_DP
)) {
1141 UriDevicePath
= (URI_DEVICE_PATH
*) TempDevicePath
;
1143 // UEFI Spec doesn't require the URI to be a NULL-terminated string
1144 // So we allocate a new buffer and always append a '\0' to it.
1146 UriStrLength
= DevicePathNodeLength (UriDevicePath
) - sizeof(EFI_DEVICE_PATH_PROTOCOL
);
1147 if (UriStrLength
== 0) {
1149 // return a NULL UriAddress if it's a empty URI device path node.
1153 Uri
= AllocatePool (UriStrLength
+ 1);
1155 return EFI_OUT_OF_RESOURCES
;
1157 CopyMem (Uri
, UriDevicePath
->Uri
, DevicePathNodeLength (UriDevicePath
) - sizeof(EFI_DEVICE_PATH_PROTOCOL
));
1158 Uri
[DevicePathNodeLength (UriDevicePath
) - sizeof(EFI_DEVICE_PATH_PROTOCOL
)] = '\0';
1162 TempDevicePath
= NextDevicePathNode (TempDevicePath
);
1169 This function returns the image type according to server replied HTTP message
1170 and also the image's URI info.
1172 @param[in] Uri The pointer to the image's URI string.
1173 @param[in] UriParser URI Parse result returned by NetHttpParseUrl().
1174 @param[in] HeaderCount Number of HTTP header structures in Headers list.
1175 @param[in] Headers Array containing list of HTTP headers.
1176 @param[out] ImageType The image type of the downloaded file.
1178 @retval EFI_SUCCESS The image type is returned in ImageType.
1179 @retval EFI_INVALID_PARAMETER ImageType, Uri or UriParser is NULL.
1180 @retval EFI_INVALID_PARAMETER HeaderCount is not zero, and Headers is NULL.
1181 @retval EFI_NOT_FOUND Failed to identify the image type.
1182 @retval Others Unexpect error happened.
1186 HttpBootCheckImageType (
1189 IN UINTN HeaderCount
,
1190 IN EFI_HTTP_HEADER
*Headers
,
1191 OUT HTTP_BOOT_IMAGE_TYPE
*ImageType
1195 EFI_HTTP_HEADER
*Header
;
1199 if (Uri
== NULL
|| UriParser
== NULL
|| ImageType
== NULL
) {
1200 return EFI_INVALID_PARAMETER
;
1203 if (HeaderCount
!= 0 && Headers
== NULL
) {
1204 return EFI_INVALID_PARAMETER
;
1208 // Determine the image type by the HTTP Content-Type header field first.
1209 // "application/efi" -> EFI Image
1210 // "application/vnd.efi-iso" -> CD/DVD Image
1211 // "application/vnd.efi-img" -> Virtual Disk Image
1213 Header
= HttpFindHeader (HeaderCount
, Headers
, HTTP_HEADER_CONTENT_TYPE
);
1214 if (Header
!= NULL
) {
1215 if (AsciiStriCmp (Header
->FieldValue
, HTTP_CONTENT_TYPE_APP_EFI
) == 0) {
1216 *ImageType
= ImageTypeEfi
;
1218 } else if (AsciiStriCmp (Header
->FieldValue
, HTTP_CONTENT_TYPE_APP_ISO
) == 0) {
1219 *ImageType
= ImageTypeVirtualCd
;
1221 } else if (AsciiStriCmp (Header
->FieldValue
, HTTP_CONTENT_TYPE_APP_IMG
) == 0) {
1222 *ImageType
= ImageTypeVirtualDisk
;
1228 // Determine the image type by file extension:
1229 // *.efi -> EFI Image
1230 // *.iso -> CD/DVD Image
1231 // *.img -> Virtual Disk Image
1233 Status
= HttpUrlGetPath (
1238 if (EFI_ERROR (Status
)) {
1242 FilePost
= FilePath
+ AsciiStrLen (FilePath
) - 4;
1243 if (AsciiStrCmp (FilePost
, ".efi") == 0) {
1244 *ImageType
= ImageTypeEfi
;
1245 } else if (AsciiStrCmp (FilePost
, ".iso") == 0) {
1246 *ImageType
= ImageTypeVirtualCd
;
1247 } else if (AsciiStrCmp (FilePost
, ".img") == 0) {
1248 *ImageType
= ImageTypeVirtualDisk
;
1250 *ImageType
= ImageTypeMax
;
1253 FreePool (FilePath
);
1255 return (*ImageType
< ImageTypeMax
) ? EFI_SUCCESS
: EFI_NOT_FOUND
;
1259 This function register the RAM disk info to the system.
1261 @param[in] Private The pointer to the driver's private data.
1262 @param[in] BufferSize The size of Buffer in bytes.
1263 @param[in] Buffer The base address of the RAM disk.
1264 @param[in] ImageType The image type of the file in Buffer.
1266 @retval EFI_SUCCESS The RAM disk has been registered.
1267 @retval EFI_NOT_FOUND No RAM disk protocol instances were found.
1268 @retval EFI_UNSUPPORTED The ImageType is not supported.
1269 @retval Others Unexpected error happened.
1273 HttpBootRegisterRamDisk (
1274 IN HTTP_BOOT_PRIVATE_DATA
*Private
,
1275 IN UINTN BufferSize
,
1277 IN HTTP_BOOT_IMAGE_TYPE ImageType
1280 EFI_RAM_DISK_PROTOCOL
*RamDisk
;
1282 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
1283 EFI_GUID
*RamDiskType
;
1285 ASSERT (Private
!= NULL
);
1286 ASSERT (Buffer
!= NULL
);
1287 ASSERT (BufferSize
!= 0);
1289 Status
= gBS
->LocateProtocol (&gEfiRamDiskProtocolGuid
, NULL
, (VOID
**) &RamDisk
);
1290 if (EFI_ERROR (Status
)) {
1291 DEBUG ((EFI_D_ERROR
, "HTTP Boot: Couldn't find the RAM Disk protocol - %r\n", Status
));
1295 if (ImageType
== ImageTypeVirtualCd
) {
1296 RamDiskType
= &gEfiVirtualCdGuid
;
1297 } else if (ImageType
== ImageTypeVirtualDisk
) {
1298 RamDiskType
= &gEfiVirtualDiskGuid
;
1300 return EFI_UNSUPPORTED
;
1303 Status
= RamDisk
->Register (
1307 Private
->UsingIpv6
? Private
->Ip6Nic
->DevicePath
: Private
->Ip4Nic
->DevicePath
,
1310 if (EFI_ERROR (Status
)) {
1311 DEBUG ((EFI_D_ERROR
, "HTTP Boot: Failed to register RAM Disk - %r\n", Status
));
1318 Indicate if the HTTP status code indicates a redirection.
1320 @param[in] StatusCode HTTP status code from server.
1322 @return TRUE if it's redirection.
1326 HttpBootIsHttpRedirectStatusCode (
1327 IN EFI_HTTP_STATUS_CODE StatusCode
1330 if (StatusCode
== HTTP_STATUS_301_MOVED_PERMANENTLY
||
1331 StatusCode
== HTTP_STATUS_302_FOUND
||
1332 StatusCode
== HTTP_STATUS_307_TEMPORARY_REDIRECT
||
1333 StatusCode
== HTTP_STATUS_308_PERMANENT_REDIRECT
) {