2 Support functions implementation for UEFI HTTP boot driver.
4 Copyright (c) 2015 - 2017, Intel Corporation. All rights reserved.<BR>
5 (C) Copyright 2016 Hewlett Packard Enterprise Development LP<BR>
6 This program and the accompanying materials are licensed and made available under
7 the terms and conditions of the BSD License that accompanies this distribution.
8 The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php.
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
16 #include "HttpBootDxe.h"
20 Get the Nic handle using any child handle in the IPv4 stack.
22 @param[in] ControllerHandle Pointer to child handle over IPv4.
24 @return NicHandle The pointer to the Nic handle.
25 @return NULL Can't find the Nic handle.
29 HttpBootGetNicByIp4Children (
30 IN EFI_HANDLE ControllerHandle
35 NicHandle
= NetLibGetNicHandle (ControllerHandle
, &gEfiHttpProtocolGuid
);
36 if (NicHandle
== NULL
) {
37 NicHandle
= NetLibGetNicHandle (ControllerHandle
, &gEfiDhcp4ProtocolGuid
);
38 if (NicHandle
== NULL
) {
47 Get the Nic handle using any child handle in the IPv6 stack.
49 @param[in] ControllerHandle Pointer to child handle over IPv6.
51 @return NicHandle The pointer to the Nic handle.
52 @return NULL Can't find the Nic handle.
56 HttpBootGetNicByIp6Children (
57 IN EFI_HANDLE ControllerHandle
61 NicHandle
= NetLibGetNicHandle (ControllerHandle
, &gEfiHttpProtocolGuid
);
62 if (NicHandle
== NULL
) {
63 NicHandle
= NetLibGetNicHandle (ControllerHandle
, &gEfiDhcp6ProtocolGuid
);
64 if (NicHandle
== NULL
) {
73 This function is to convert UINTN to ASCII string with the required formatting.
75 @param[in] Number Numeric value to be converted.
76 @param[in] Buffer The pointer to the buffer for ASCII string.
77 @param[in] Length The length of the required format.
81 HttpBootUintnToAscDecWithFormat (
89 for (; Length
> 0; Length
--) {
90 Remainder
= Number
% 10;
92 Buffer
[Length
- 1] = (UINT8
) ('0' + Remainder
);
97 This function is to display the IPv4 address.
99 @param[in] Ip The pointer to the IPv4 address.
103 HttpBootShowIp4Addr (
104 IN EFI_IPv4_ADDRESS
*Ip
109 for (Index
= 0; Index
< 4; Index
++) {
110 AsciiPrint ("%d", Ip
->Addr
[Index
]);
118 This function is to display the IPv6 address.
120 @param[in] Ip The pointer to the IPv6 address.
124 HttpBootShowIp6Addr (
125 IN EFI_IPv6_ADDRESS
*Ip
130 for (Index
= 0; Index
< 16; Index
++) {
132 if (Ip
->Addr
[Index
] != 0) {
133 AsciiPrint ("%x", Ip
->Addr
[Index
]);
139 if (((Ip
->Addr
[Index
] & 0xf0) == 0) && (Ip
->Addr
[Index
- 1] != 0)) {
142 AsciiPrint ("%x", Ip
->Addr
[Index
]);
150 This function is to display the HTTP error status.
152 @param[in] StatusCode The status code value in HTTP message.
156 HttpBootPrintErrorMessage (
157 EFI_HTTP_STATUS_CODE StatusCode
162 switch (StatusCode
) {
163 case HTTP_STATUS_300_MULTIPLE_CHOICES
:
164 AsciiPrint ("\n Redirection: 300 Multiple Choices");
167 case HTTP_STATUS_301_MOVED_PERMANENTLY
:
168 AsciiPrint ("\n Redirection: 301 Moved Permanently");
171 case HTTP_STATUS_302_FOUND
:
172 AsciiPrint ("\n Redirection: 302 Found");
175 case HTTP_STATUS_303_SEE_OTHER
:
176 AsciiPrint ("\n Redirection: 303 See Other");
179 case HTTP_STATUS_304_NOT_MODIFIED
:
180 AsciiPrint ("\n Redirection: 304 Not Modified");
183 case HTTP_STATUS_305_USE_PROXY
:
184 AsciiPrint ("\n Redirection: 305 Use Proxy");
187 case HTTP_STATUS_307_TEMPORARY_REDIRECT
:
188 AsciiPrint ("\n Redirection: 307 Temporary Redirect");
191 case HTTP_STATUS_308_PERMANENT_REDIRECT
:
192 AsciiPrint ("\n Redirection: 308 Permanent Redirect");
195 case HTTP_STATUS_400_BAD_REQUEST
:
196 AsciiPrint ("\n Client Error: 400 Bad Request");
199 case HTTP_STATUS_401_UNAUTHORIZED
:
200 AsciiPrint ("\n Client Error: 401 Unauthorized");
203 case HTTP_STATUS_402_PAYMENT_REQUIRED
:
204 AsciiPrint ("\n Client Error: 402 Payment Required");
207 case HTTP_STATUS_403_FORBIDDEN
:
208 AsciiPrint ("\n Client Error: 403 Forbidden");
211 case HTTP_STATUS_404_NOT_FOUND
:
212 AsciiPrint ("\n Client Error: 404 Not Found");
215 case HTTP_STATUS_405_METHOD_NOT_ALLOWED
:
216 AsciiPrint ("\n Client Error: 405 Method Not Allowed");
219 case HTTP_STATUS_406_NOT_ACCEPTABLE
:
220 AsciiPrint ("\n Client Error: 406 Not Acceptable");
223 case HTTP_STATUS_407_PROXY_AUTHENTICATION_REQUIRED
:
224 AsciiPrint ("\n Client Error: 407 Proxy Authentication Required");
227 case HTTP_STATUS_408_REQUEST_TIME_OUT
:
228 AsciiPrint ("\n Client Error: 408 Request Timeout");
231 case HTTP_STATUS_409_CONFLICT
:
232 AsciiPrint ("\n Client Error: 409 Conflict");
235 case HTTP_STATUS_410_GONE
:
236 AsciiPrint ("\n Client Error: 410 Gone");
239 case HTTP_STATUS_411_LENGTH_REQUIRED
:
240 AsciiPrint ("\n Client Error: 411 Length Required");
243 case HTTP_STATUS_412_PRECONDITION_FAILED
:
244 AsciiPrint ("\n Client Error: 412 Precondition Failed");
247 case HTTP_STATUS_413_REQUEST_ENTITY_TOO_LARGE
:
248 AsciiPrint ("\n Client Error: 413 Request Entity Too Large");
251 case HTTP_STATUS_414_REQUEST_URI_TOO_LARGE
:
252 AsciiPrint ("\n Client Error: 414 Request URI Too Long");
255 case HTTP_STATUS_415_UNSUPPORTED_MEDIA_TYPE
:
256 AsciiPrint ("\n Client Error: 415 Unsupported Media Type");
259 case HTTP_STATUS_416_REQUESTED_RANGE_NOT_SATISFIED
:
260 AsciiPrint ("\n Client Error: 416 Requested Range Not Satisfiable");
263 case HTTP_STATUS_417_EXPECTATION_FAILED
:
264 AsciiPrint ("\n Client Error: 417 Expectation Failed");
267 case HTTP_STATUS_500_INTERNAL_SERVER_ERROR
:
268 AsciiPrint ("\n Server Error: 500 Internal Server Error");
271 case HTTP_STATUS_501_NOT_IMPLEMENTED
:
272 AsciiPrint ("\n Server Error: 501 Not Implemented");
275 case HTTP_STATUS_502_BAD_GATEWAY
:
276 AsciiPrint ("\n Server Error: 502 Bad Gateway");
279 case HTTP_STATUS_503_SERVICE_UNAVAILABLE
:
280 AsciiPrint ("\n Server Error: 503 Service Unavailable");
283 case HTTP_STATUS_504_GATEWAY_TIME_OUT
:
284 AsciiPrint ("\n Server Error: 504 Gateway Timeout");
287 case HTTP_STATUS_505_HTTP_VERSION_NOT_SUPPORTED
:
288 AsciiPrint ("\n Server Error: 505 HTTP Version Not Supported");
297 Notify the callback function when an event is triggered.
299 @param[in] Event The triggered event.
300 @param[in] Context The opaque parameter to the function.
305 HttpBootCommonNotify (
310 *((BOOLEAN
*) Context
) = TRUE
;
314 Retrieve the host address using the EFI_DNS6_PROTOCOL.
316 @param[in] Private The pointer to the driver's private data.
317 @param[in] HostName Pointer to buffer containing hostname.
318 @param[out] IpAddress On output, pointer to buffer containing IPv6 address.
320 @retval EFI_SUCCESS Operation succeeded.
321 @retval EFI_DEVICE_ERROR An unexpected network error occurred.
322 @retval Others Other errors as indicated.
326 IN HTTP_BOOT_PRIVATE_DATA
*Private
,
328 OUT EFI_IPv6_ADDRESS
*IpAddress
332 EFI_DNS6_PROTOCOL
*Dns6
;
333 EFI_DNS6_CONFIG_DATA Dns6ConfigData
;
334 EFI_DNS6_COMPLETION_TOKEN Token
;
335 EFI_HANDLE Dns6Handle
;
336 EFI_IP6_CONFIG_PROTOCOL
*Ip6Config
;
337 EFI_IPv6_ADDRESS
*DnsServerList
;
338 UINTN DnsServerListCount
;
342 DnsServerList
= NULL
;
343 DnsServerListCount
= 0;
346 ZeroMem (&Token
, sizeof (EFI_DNS6_COMPLETION_TOKEN
));
349 // Get DNS server list from EFI IPv6 Configuration protocol.
351 Status
= gBS
->HandleProtocol (Private
->Controller
, &gEfiIp6ConfigProtocolGuid
, (VOID
**) &Ip6Config
);
352 if (!EFI_ERROR (Status
)) {
354 // Get the required size.
357 Status
= Ip6Config
->GetData (Ip6Config
, Ip6ConfigDataTypeDnsServer
, &DataSize
, NULL
);
358 if (Status
== EFI_BUFFER_TOO_SMALL
) {
359 DnsServerList
= AllocatePool (DataSize
);
360 if (DnsServerList
== NULL
) {
361 return EFI_OUT_OF_RESOURCES
;
364 Status
= Ip6Config
->GetData (Ip6Config
, Ip6ConfigDataTypeDnsServer
, &DataSize
, DnsServerList
);
365 if (EFI_ERROR (Status
)) {
366 FreePool (DnsServerList
);
367 DnsServerList
= NULL
;
369 DnsServerListCount
= DataSize
/ sizeof (EFI_IPv6_ADDRESS
);
374 // Create a DNSv6 child instance and get the protocol.
376 Status
= NetLibCreateServiceChild (
378 Private
->Ip6Nic
->ImageHandle
,
379 &gEfiDns6ServiceBindingProtocolGuid
,
382 if (EFI_ERROR (Status
)) {
386 Status
= gBS
->OpenProtocol (
388 &gEfiDns6ProtocolGuid
,
390 Private
->Ip6Nic
->ImageHandle
,
392 EFI_OPEN_PROTOCOL_BY_DRIVER
394 if (EFI_ERROR (Status
)) {
399 // Configure DNS6 instance for the DNS server address and protocol.
401 ZeroMem (&Dns6ConfigData
, sizeof (EFI_DNS6_CONFIG_DATA
));
402 Dns6ConfigData
.DnsServerCount
= (UINT32
)DnsServerListCount
;
403 Dns6ConfigData
.DnsServerList
= DnsServerList
;
404 Dns6ConfigData
.EnableDnsCache
= TRUE
;
405 Dns6ConfigData
.Protocol
= EFI_IP_PROTO_UDP
;
406 IP6_COPY_ADDRESS (&Dns6ConfigData
.StationIp
,&Private
->StationIp
.v6
);
407 Status
= Dns6
->Configure (
411 if (EFI_ERROR (Status
)) {
415 Token
.Status
= EFI_NOT_READY
;
418 // Create event to set the IsDone flag when name resolution is finished.
420 Status
= gBS
->CreateEvent (
423 HttpBootCommonNotify
,
427 if (EFI_ERROR (Status
)) {
432 // Start asynchronous name resolution.
434 Status
= Dns6
->HostNameToIp (Dns6
, HostName
, &Token
);
435 if (EFI_ERROR (Status
)) {
444 // Name resolution is done, check result.
446 Status
= Token
.Status
;
447 if (!EFI_ERROR (Status
)) {
448 if (Token
.RspData
.H2AData
== NULL
) {
449 Status
= EFI_DEVICE_ERROR
;
452 if (Token
.RspData
.H2AData
->IpCount
== 0 || Token
.RspData
.H2AData
->IpList
== NULL
) {
453 Status
= EFI_DEVICE_ERROR
;
457 // We just return the first IPv6 address from DNS protocol.
459 IP6_COPY_ADDRESS (IpAddress
, Token
.RspData
.H2AData
->IpList
);
460 Status
= EFI_SUCCESS
;
464 if (Token
.Event
!= NULL
) {
465 gBS
->CloseEvent (Token
.Event
);
467 if (Token
.RspData
.H2AData
!= NULL
) {
468 if (Token
.RspData
.H2AData
->IpList
!= NULL
) {
469 FreePool (Token
.RspData
.H2AData
->IpList
);
471 FreePool (Token
.RspData
.H2AData
);
475 Dns6
->Configure (Dns6
, NULL
);
479 &gEfiDns6ProtocolGuid
,
480 Private
->Ip6Nic
->ImageHandle
,
485 if (Dns6Handle
!= NULL
) {
486 NetLibDestroyServiceChild (
488 Private
->Ip6Nic
->ImageHandle
,
489 &gEfiDns6ServiceBindingProtocolGuid
,
494 if (DnsServerList
!= NULL
) {
495 FreePool (DnsServerList
);
501 Create a HTTP_IO_HEADER to hold the HTTP header items.
503 @param[in] MaxHeaderCount The maximun number of HTTP header in this holder.
505 @return A pointer of the HTTP header holder or NULL if failed.
509 HttpBootCreateHeader (
513 HTTP_IO_HEADER
*HttpIoHeader
;
515 if (MaxHeaderCount
== 0) {
519 HttpIoHeader
= AllocateZeroPool (sizeof (HTTP_IO_HEADER
) + MaxHeaderCount
* sizeof (EFI_HTTP_HEADER
));
520 if (HttpIoHeader
== NULL
) {
524 HttpIoHeader
->MaxHeaderCount
= MaxHeaderCount
;
525 HttpIoHeader
->Headers
= (EFI_HTTP_HEADER
*) (HttpIoHeader
+ 1);
531 Destroy the HTTP_IO_HEADER and release the resouces.
533 @param[in] HttpIoHeader Point to the HTTP header holder to be destroyed.
538 IN HTTP_IO_HEADER
*HttpIoHeader
543 if (HttpIoHeader
!= NULL
) {
544 if (HttpIoHeader
->HeaderCount
!= 0) {
545 for (Index
= 0; Index
< HttpIoHeader
->HeaderCount
; Index
++) {
546 FreePool (HttpIoHeader
->Headers
[Index
].FieldName
);
547 FreePool (HttpIoHeader
->Headers
[Index
].FieldValue
);
550 FreePool (HttpIoHeader
);
555 Set or update a HTTP header with the field name and corresponding value.
557 @param[in] HttpIoHeader Point to the HTTP header holder.
558 @param[in] FieldName Null terminated string which describes a field name.
559 @param[in] FieldValue Null terminated string which describes the corresponding field value.
561 @retval EFI_SUCCESS The HTTP header has been set or updated.
562 @retval EFI_INVALID_PARAMETER Any input parameter is invalid.
563 @retval EFI_OUT_OF_RESOURCES Insufficient resource to complete the operation.
564 @retval Other Unexpected error happened.
569 IN HTTP_IO_HEADER
*HttpIoHeader
,
574 EFI_HTTP_HEADER
*Header
;
576 CHAR8
*NewFieldValue
;
578 if (HttpIoHeader
== NULL
|| FieldName
== NULL
|| FieldValue
== NULL
) {
579 return EFI_INVALID_PARAMETER
;
582 Header
= HttpFindHeader (HttpIoHeader
->HeaderCount
, HttpIoHeader
->Headers
, FieldName
);
583 if (Header
== NULL
) {
587 if (HttpIoHeader
->HeaderCount
>= HttpIoHeader
->MaxHeaderCount
) {
588 return EFI_OUT_OF_RESOURCES
;
590 Header
= &HttpIoHeader
->Headers
[HttpIoHeader
->HeaderCount
];
592 StrSize
= AsciiStrSize (FieldName
);
593 Header
->FieldName
= AllocatePool (StrSize
);
594 if (Header
->FieldName
== NULL
) {
595 return EFI_OUT_OF_RESOURCES
;
597 CopyMem (Header
->FieldName
, FieldName
, StrSize
);
598 Header
->FieldName
[StrSize
-1] = '\0';
600 StrSize
= AsciiStrSize (FieldValue
);
601 Header
->FieldValue
= AllocatePool (StrSize
);
602 if (Header
->FieldValue
== NULL
) {
603 FreePool (Header
->FieldName
);
604 return EFI_OUT_OF_RESOURCES
;
606 CopyMem (Header
->FieldValue
, FieldValue
, StrSize
);
607 Header
->FieldValue
[StrSize
-1] = '\0';
609 HttpIoHeader
->HeaderCount
++;
612 // Update an existing one.
614 StrSize
= AsciiStrSize (FieldValue
);
615 NewFieldValue
= AllocatePool (StrSize
);
616 if (NewFieldValue
== NULL
) {
617 return EFI_OUT_OF_RESOURCES
;
619 CopyMem (NewFieldValue
, FieldValue
, StrSize
);
620 NewFieldValue
[StrSize
-1] = '\0';
622 if (Header
->FieldValue
!= NULL
) {
623 FreePool (Header
->FieldValue
);
625 Header
->FieldValue
= NewFieldValue
;
632 Notify the callback function when an event is triggered.
634 @param[in] Context The opaque parameter to the function.
643 *((BOOLEAN
*) Context
) = TRUE
;
647 Request HttpIoNotifyDpc as a DPC at TPL_CALLBACK.
649 @param[in] Event The event signaled.
650 @param[in] Context The opaque parameter to the function.
661 // Request HttpIoNotifyDpc as a DPC at TPL_CALLBACK
663 QueueDpc (TPL_CALLBACK
, HttpIoNotifyDpc
, Context
);
667 Create a HTTP_IO to access the HTTP service. It will create and configure
670 @param[in] Image The handle of the driver image.
671 @param[in] Controller The handle of the controller.
672 @param[in] IpVersion IP_VERSION_4 or IP_VERSION_6.
673 @param[in] ConfigData The HTTP_IO configuration data.
674 @param[in] Callback Callback function which will be invoked when specified
675 HTTP_IO_CALLBACK_EVENT happened.
676 @param[in] Context The Context data which will be passed to the Callback function.
677 @param[out] HttpIo The HTTP_IO.
679 @retval EFI_SUCCESS The HTTP_IO is created and configured.
680 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
681 @retval EFI_UNSUPPORTED One or more of the control options are not
682 supported in the implementation.
683 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory.
684 @retval Others Failed to create the HTTP_IO or configure it.
690 IN EFI_HANDLE Controller
,
692 IN HTTP_IO_CONFIG_DATA
*ConfigData
,
693 IN HTTP_IO_CALLBACK Callback
,
699 EFI_HTTP_CONFIG_DATA HttpConfigData
;
700 EFI_HTTPv4_ACCESS_POINT Http4AccessPoint
;
701 EFI_HTTPv6_ACCESS_POINT Http6AccessPoint
;
702 EFI_HTTP_PROTOCOL
*Http
;
705 if ((Image
== NULL
) || (Controller
== NULL
) || (ConfigData
== NULL
) || (HttpIo
== NULL
)) {
706 return EFI_INVALID_PARAMETER
;
709 if (IpVersion
!= IP_VERSION_4
&& IpVersion
!= IP_VERSION_6
) {
710 return EFI_UNSUPPORTED
;
713 ZeroMem (HttpIo
, sizeof (HTTP_IO
));
716 // Create the HTTP child instance and get the HTTP protocol.
718 Status
= NetLibCreateServiceChild (
721 &gEfiHttpServiceBindingProtocolGuid
,
724 if (EFI_ERROR (Status
)) {
728 Status
= gBS
->OpenProtocol (
730 &gEfiHttpProtocolGuid
,
734 EFI_OPEN_PROTOCOL_BY_DRIVER
736 if (EFI_ERROR (Status
) || (Http
== NULL
)) {
741 // Init the configuration data and configure the HTTP child.
743 HttpIo
->Image
= Image
;
744 HttpIo
->Controller
= Controller
;
745 HttpIo
->IpVersion
= IpVersion
;
747 HttpIo
->Callback
= Callback
;
748 HttpIo
->Context
= Context
;
750 ZeroMem (&HttpConfigData
, sizeof (EFI_HTTP_CONFIG_DATA
));
751 HttpConfigData
.HttpVersion
= HttpVersion11
;
752 HttpConfigData
.TimeOutMillisec
= ConfigData
->Config4
.RequestTimeOut
;
753 if (HttpIo
->IpVersion
== IP_VERSION_4
) {
754 HttpConfigData
.LocalAddressIsIPv6
= FALSE
;
756 Http4AccessPoint
.UseDefaultAddress
= ConfigData
->Config4
.UseDefaultAddress
;
757 Http4AccessPoint
.LocalPort
= ConfigData
->Config4
.LocalPort
;
758 IP4_COPY_ADDRESS (&Http4AccessPoint
.LocalAddress
, &ConfigData
->Config4
.LocalIp
);
759 IP4_COPY_ADDRESS (&Http4AccessPoint
.LocalSubnet
, &ConfigData
->Config4
.SubnetMask
);
760 HttpConfigData
.AccessPoint
.IPv4Node
= &Http4AccessPoint
;
762 HttpConfigData
.LocalAddressIsIPv6
= TRUE
;
763 Http6AccessPoint
.LocalPort
= ConfigData
->Config6
.LocalPort
;
764 IP6_COPY_ADDRESS (&Http6AccessPoint
.LocalAddress
, &ConfigData
->Config6
.LocalIp
);
765 HttpConfigData
.AccessPoint
.IPv6Node
= &Http6AccessPoint
;
768 Status
= Http
->Configure (Http
, &HttpConfigData
);
769 if (EFI_ERROR (Status
)) {
774 // Create events for variuos asynchronous operations.
776 Status
= gBS
->CreateEvent (
783 if (EFI_ERROR (Status
)) {
786 HttpIo
->ReqToken
.Event
= Event
;
787 HttpIo
->ReqToken
.Message
= &HttpIo
->ReqMessage
;
789 Status
= gBS
->CreateEvent (
796 if (EFI_ERROR (Status
)) {
799 HttpIo
->RspToken
.Event
= Event
;
800 HttpIo
->RspToken
.Message
= &HttpIo
->RspMessage
;
803 // Create TimeoutEvent for response
805 Status
= gBS
->CreateEvent (
812 if (EFI_ERROR (Status
)) {
815 HttpIo
->TimeoutEvent
= Event
;
820 HttpIoDestroyIo (HttpIo
);
826 Destroy the HTTP_IO and release the resouces.
828 @param[in] HttpIo The HTTP_IO which wraps the HTTP service to be destroyed.
836 EFI_HTTP_PROTOCOL
*Http
;
839 if (HttpIo
== NULL
) {
843 Event
= HttpIo
->ReqToken
.Event
;
845 gBS
->CloseEvent (Event
);
848 Event
= HttpIo
->RspToken
.Event
;
850 gBS
->CloseEvent (Event
);
853 Event
= HttpIo
->TimeoutEvent
;
855 gBS
->CloseEvent (Event
);
860 Http
->Configure (Http
, NULL
);
863 &gEfiHttpProtocolGuid
,
869 NetLibDestroyServiceChild (
872 &gEfiHttpServiceBindingProtocolGuid
,
878 Synchronously send a HTTP REQUEST message to the server.
880 @param[in] HttpIo The HttpIo wrapping the HTTP service.
881 @param[in] Request A pointer to storage such data as URL and HTTP method.
882 @param[in] HeaderCount Number of HTTP header structures in Headers list.
883 @param[in] Headers Array containing list of HTTP headers.
884 @param[in] BodyLength Length in bytes of the HTTP body.
885 @param[in] Body Body associated with the HTTP request.
887 @retval EFI_SUCCESS The HTTP request is trasmitted.
888 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
889 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory.
890 @retval EFI_DEVICE_ERROR An unexpected network or system error occurred.
891 @retval Others Other errors as indicated.
897 IN EFI_HTTP_REQUEST_DATA
*Request
,
898 IN UINTN HeaderCount
,
899 IN EFI_HTTP_HEADER
*Headers
,
905 EFI_HTTP_PROTOCOL
*Http
;
907 if (HttpIo
== NULL
|| HttpIo
->Http
== NULL
) {
908 return EFI_INVALID_PARAMETER
;
911 HttpIo
->ReqToken
.Status
= EFI_NOT_READY
;
912 HttpIo
->ReqToken
.Message
->Data
.Request
= Request
;
913 HttpIo
->ReqToken
.Message
->HeaderCount
= HeaderCount
;
914 HttpIo
->ReqToken
.Message
->Headers
= Headers
;
915 HttpIo
->ReqToken
.Message
->BodyLength
= BodyLength
;
916 HttpIo
->ReqToken
.Message
->Body
= Body
;
918 if (HttpIo
->Callback
!= NULL
) {
919 Status
= HttpIo
->Callback (
921 HttpIo
->ReqToken
.Message
,
924 if (EFI_ERROR (Status
)) {
930 // Queue the request token to HTTP instances.
933 HttpIo
->IsTxDone
= FALSE
;
934 Status
= Http
->Request (
938 if (EFI_ERROR (Status
)) {
943 // Poll the network until transmit finish.
945 while (!HttpIo
->IsTxDone
) {
949 return HttpIo
->ReqToken
.Status
;
953 Synchronously receive a HTTP RESPONSE message from the server.
955 @param[in] HttpIo The HttpIo wrapping the HTTP service.
956 @param[in] RecvMsgHeader TRUE to receive a new HTTP response (from message header).
957 FALSE to continue receive the previous response message.
958 @param[out] ResponseData Point to a wrapper of the received response data.
960 @retval EFI_SUCCESS The HTTP response is received.
961 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
962 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory.
963 @retval EFI_DEVICE_ERROR An unexpected network or system error occurred.
964 @retval Others Other errors as indicated.
970 IN BOOLEAN RecvMsgHeader
,
971 OUT HTTP_IO_RESPONSE_DATA
*ResponseData
975 EFI_HTTP_PROTOCOL
*Http
;
977 if (HttpIo
== NULL
|| HttpIo
->Http
== NULL
|| ResponseData
== NULL
) {
978 return EFI_INVALID_PARAMETER
;
982 // Start the timer, and wait Timeout seconds to receive the header packet.
984 Status
= gBS
->SetTimer (HttpIo
->TimeoutEvent
, TimerRelative
, HTTP_BOOT_RESPONSE_TIMEOUT
* TICKS_PER_MS
);
985 if (EFI_ERROR (Status
)) {
990 // Queue the response token to HTTP instances.
992 HttpIo
->RspToken
.Status
= EFI_NOT_READY
;
994 HttpIo
->RspToken
.Message
->Data
.Response
= &ResponseData
->Response
;
996 HttpIo
->RspToken
.Message
->Data
.Response
= NULL
;
998 HttpIo
->RspToken
.Message
->HeaderCount
= 0;
999 HttpIo
->RspToken
.Message
->Headers
= NULL
;
1000 HttpIo
->RspToken
.Message
->BodyLength
= ResponseData
->BodyLength
;
1001 HttpIo
->RspToken
.Message
->Body
= ResponseData
->Body
;
1003 Http
= HttpIo
->Http
;
1004 HttpIo
->IsRxDone
= FALSE
;
1005 Status
= Http
->Response (
1010 if (EFI_ERROR (Status
)) {
1011 gBS
->SetTimer (HttpIo
->TimeoutEvent
, TimerCancel
, 0);
1016 // Poll the network until receive finish.
1018 while (!HttpIo
->IsRxDone
&& ((HttpIo
->TimeoutEvent
== NULL
) || EFI_ERROR (gBS
->CheckEvent (HttpIo
->TimeoutEvent
)))) {
1022 gBS
->SetTimer (HttpIo
->TimeoutEvent
, TimerCancel
, 0);
1024 if (!HttpIo
->IsRxDone
) {
1026 // Timeout occurs, cancel the response token.
1028 Http
->Cancel (Http
, &HttpIo
->RspToken
);
1030 Status
= EFI_TIMEOUT
;
1034 HttpIo
->IsRxDone
= FALSE
;
1037 if ((HttpIo
->Callback
!= NULL
) &&
1038 (HttpIo
->RspToken
.Status
== EFI_SUCCESS
|| HttpIo
->RspToken
.Status
== EFI_HTTP_ERROR
)) {
1039 Status
= HttpIo
->Callback (
1041 HttpIo
->RspToken
.Message
,
1044 if (EFI_ERROR (Status
)) {
1050 // Store the received data into the wrapper.
1052 ResponseData
->Status
= HttpIo
->RspToken
.Status
;
1053 ResponseData
->HeaderCount
= HttpIo
->RspToken
.Message
->HeaderCount
;
1054 ResponseData
->Headers
= HttpIo
->RspToken
.Message
->Headers
;
1055 ResponseData
->BodyLength
= HttpIo
->RspToken
.Message
->BodyLength
;
1061 This function checks the HTTP(S) URI scheme.
1063 @param[in] Uri The pointer to the URI string.
1065 @retval EFI_SUCCESS The URI scheme is valid.
1066 @retval EFI_INVALID_PARAMETER The URI scheme is not HTTP or HTTPS.
1067 @retval EFI_ACCESS_DENIED HTTP is disabled and the URI is HTTP.
1071 HttpBootCheckUriScheme (
1078 Status
= EFI_SUCCESS
;
1081 // Convert the scheme to all lower case.
1083 for (Index
= 0; Index
< AsciiStrLen (Uri
); Index
++) {
1084 if (Uri
[Index
] == ':') {
1087 if (Uri
[Index
] >= 'A' && Uri
[Index
] <= 'Z') {
1088 Uri
[Index
] -= (CHAR8
)('A' - 'a');
1093 // Return EFI_INVALID_PARAMETER if the URI is not HTTP or HTTPS.
1095 if ((AsciiStrnCmp (Uri
, "http://", 7) != 0) && (AsciiStrnCmp (Uri
, "https://", 8) != 0)) {
1096 DEBUG ((EFI_D_ERROR
, "HttpBootCheckUriScheme: Invalid Uri.\n"));
1097 return EFI_INVALID_PARAMETER
;
1101 // HTTP is disabled, return EFI_ACCESS_DENIED if the URI is HTTP.
1103 if (!PcdGetBool (PcdAllowHttpConnections
) && (AsciiStrnCmp (Uri
, "http://", 7) == 0)) {
1104 DEBUG ((EFI_D_ERROR
, "HttpBootCheckUriScheme: HTTP is disabled.\n"));
1105 return EFI_ACCESS_DENIED
;
1112 Get the URI address string from the input device path.
1114 Caller need to free the buffer in the UriAddress pointer.
1116 @param[in] FilePath Pointer to the device path which contains a URI device path node.
1117 @param[out] UriAddress The URI address string extract from the device path.
1119 @retval EFI_SUCCESS The URI string is returned.
1120 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory.
1124 HttpBootParseFilePath (
1125 IN EFI_DEVICE_PATH_PROTOCOL
*FilePath
,
1126 OUT CHAR8
**UriAddress
1129 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePath
;
1130 URI_DEVICE_PATH
*UriDevicePath
;
1134 if (FilePath
== NULL
) {
1135 return EFI_INVALID_PARAMETER
;
1141 // Extract the URI address from the FilePath
1143 TempDevicePath
= FilePath
;
1144 while (!IsDevicePathEnd (TempDevicePath
)) {
1145 if ((DevicePathType (TempDevicePath
) == MESSAGING_DEVICE_PATH
) &&
1146 (DevicePathSubType (TempDevicePath
) == MSG_URI_DP
)) {
1147 UriDevicePath
= (URI_DEVICE_PATH
*) TempDevicePath
;
1149 // UEFI Spec doesn't require the URI to be a NULL-terminated string
1150 // So we allocate a new buffer and always append a '\0' to it.
1152 UriStrLength
= DevicePathNodeLength (UriDevicePath
) - sizeof(EFI_DEVICE_PATH_PROTOCOL
);
1153 if (UriStrLength
== 0) {
1155 // return a NULL UriAddress if it's a empty URI device path node.
1159 Uri
= AllocatePool (UriStrLength
+ 1);
1161 return EFI_OUT_OF_RESOURCES
;
1163 CopyMem (Uri
, UriDevicePath
->Uri
, DevicePathNodeLength (UriDevicePath
) - sizeof(EFI_DEVICE_PATH_PROTOCOL
));
1164 Uri
[DevicePathNodeLength (UriDevicePath
) - sizeof(EFI_DEVICE_PATH_PROTOCOL
)] = '\0';
1168 TempDevicePath
= NextDevicePathNode (TempDevicePath
);
1175 This function returns the image type according to server replied HTTP message
1176 and also the image's URI info.
1178 @param[in] Uri The pointer to the image's URI string.
1179 @param[in] UriParser URI Parse result returned by NetHttpParseUrl().
1180 @param[in] HeaderCount Number of HTTP header structures in Headers list.
1181 @param[in] Headers Array containing list of HTTP headers.
1182 @param[out] ImageType The image type of the downloaded file.
1184 @retval EFI_SUCCESS The image type is returned in ImageType.
1185 @retval EFI_INVALID_PARAMETER ImageType, Uri or UriParser is NULL.
1186 @retval EFI_INVALID_PARAMETER HeaderCount is not zero, and Headers is NULL.
1187 @retval EFI_NOT_FOUND Failed to identify the image type.
1188 @retval Others Unexpect error happened.
1192 HttpBootCheckImageType (
1195 IN UINTN HeaderCount
,
1196 IN EFI_HTTP_HEADER
*Headers
,
1197 OUT HTTP_BOOT_IMAGE_TYPE
*ImageType
1201 EFI_HTTP_HEADER
*Header
;
1205 if (Uri
== NULL
|| UriParser
== NULL
|| ImageType
== NULL
) {
1206 return EFI_INVALID_PARAMETER
;
1209 if (HeaderCount
!= 0 && Headers
== NULL
) {
1210 return EFI_INVALID_PARAMETER
;
1214 // Determine the image type by the HTTP Content-Type header field first.
1215 // "application/efi" -> EFI Image
1216 // "application/vnd.efi-iso" -> CD/DVD Image
1217 // "application/vnd.efi-img" -> Virtual Disk Image
1219 Header
= HttpFindHeader (HeaderCount
, Headers
, HTTP_HEADER_CONTENT_TYPE
);
1220 if (Header
!= NULL
) {
1221 if (AsciiStriCmp (Header
->FieldValue
, HTTP_CONTENT_TYPE_APP_EFI
) == 0) {
1222 *ImageType
= ImageTypeEfi
;
1224 } else if (AsciiStriCmp (Header
->FieldValue
, HTTP_CONTENT_TYPE_APP_ISO
) == 0) {
1225 *ImageType
= ImageTypeVirtualCd
;
1227 } else if (AsciiStriCmp (Header
->FieldValue
, HTTP_CONTENT_TYPE_APP_IMG
) == 0) {
1228 *ImageType
= ImageTypeVirtualDisk
;
1234 // Determine the image type by file extension:
1235 // *.efi -> EFI Image
1236 // *.iso -> CD/DVD Image
1237 // *.img -> Virtual Disk Image
1239 Status
= HttpUrlGetPath (
1244 if (EFI_ERROR (Status
)) {
1248 FilePost
= FilePath
+ AsciiStrLen (FilePath
) - 4;
1249 if (AsciiStrCmp (FilePost
, ".efi") == 0) {
1250 *ImageType
= ImageTypeEfi
;
1251 } else if (AsciiStrCmp (FilePost
, ".iso") == 0) {
1252 *ImageType
= ImageTypeVirtualCd
;
1253 } else if (AsciiStrCmp (FilePost
, ".img") == 0) {
1254 *ImageType
= ImageTypeVirtualDisk
;
1256 *ImageType
= ImageTypeMax
;
1259 FreePool (FilePath
);
1261 return (*ImageType
< ImageTypeMax
) ? EFI_SUCCESS
: EFI_NOT_FOUND
;
1265 This function register the RAM disk info to the system.
1267 @param[in] Private The pointer to the driver's private data.
1268 @param[in] BufferSize The size of Buffer in bytes.
1269 @param[in] Buffer The base address of the RAM disk.
1270 @param[in] ImageType The image type of the file in Buffer.
1272 @retval EFI_SUCCESS The RAM disk has been registered.
1273 @retval EFI_NOT_FOUND No RAM disk protocol instances were found.
1274 @retval EFI_UNSUPPORTED The ImageType is not supported.
1275 @retval Others Unexpected error happened.
1279 HttpBootRegisterRamDisk (
1280 IN HTTP_BOOT_PRIVATE_DATA
*Private
,
1281 IN UINTN BufferSize
,
1283 IN HTTP_BOOT_IMAGE_TYPE ImageType
1286 EFI_RAM_DISK_PROTOCOL
*RamDisk
;
1288 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
1289 EFI_GUID
*RamDiskType
;
1291 ASSERT (Private
!= NULL
);
1292 ASSERT (Buffer
!= NULL
);
1293 ASSERT (BufferSize
!= 0);
1295 Status
= gBS
->LocateProtocol (&gEfiRamDiskProtocolGuid
, NULL
, (VOID
**) &RamDisk
);
1296 if (EFI_ERROR (Status
)) {
1297 DEBUG ((EFI_D_ERROR
, "HTTP Boot: Couldn't find the RAM Disk protocol - %r\n", Status
));
1301 if (ImageType
== ImageTypeVirtualCd
) {
1302 RamDiskType
= &gEfiVirtualCdGuid
;
1303 } else if (ImageType
== ImageTypeVirtualDisk
) {
1304 RamDiskType
= &gEfiVirtualDiskGuid
;
1306 return EFI_UNSUPPORTED
;
1309 Status
= RamDisk
->Register (
1313 Private
->UsingIpv6
? Private
->Ip6Nic
->DevicePath
: Private
->Ip4Nic
->DevicePath
,
1316 if (EFI_ERROR (Status
)) {
1317 DEBUG ((EFI_D_ERROR
, "HTTP Boot: Failed to register RAM Disk - %r\n", Status
));
1324 Indicate if the HTTP status code indicates a redirection.
1326 @param[in] StatusCode HTTP status code from server.
1328 @return TRUE if it's redirection.
1332 HttpBootIsHttpRedirectStatusCode (
1333 IN EFI_HTTP_STATUS_CODE StatusCode
1336 if (StatusCode
== HTTP_STATUS_301_MOVED_PERMANENTLY
||
1337 StatusCode
== HTTP_STATUS_302_FOUND
||
1338 StatusCode
== HTTP_STATUS_307_TEMPORARY_REDIRECT
||
1339 StatusCode
== HTTP_STATUS_308_PERMANENT_REDIRECT
) {