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_CHIOCES
:
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_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");
293 Notify the callback function when an event is triggered.
295 @param[in] Event The triggered event.
296 @param[in] Context The opaque parameter to the function.
301 HttpBootCommonNotify (
306 *((BOOLEAN
*) Context
) = TRUE
;
310 Retrieve the host address using the EFI_DNS6_PROTOCOL.
312 @param[in] Private The pointer to the driver's private data.
313 @param[in] HostName Pointer to buffer containing hostname.
314 @param[out] IpAddress On output, pointer to buffer containing IPv6 address.
316 @retval EFI_SUCCESS Operation succeeded.
317 @retval EFI_DEVICE_ERROR An unexpected network error occurred.
318 @retval Others Other errors as indicated.
322 IN HTTP_BOOT_PRIVATE_DATA
*Private
,
324 OUT EFI_IPv6_ADDRESS
*IpAddress
328 EFI_DNS6_PROTOCOL
*Dns6
;
329 EFI_DNS6_CONFIG_DATA Dns6ConfigData
;
330 EFI_DNS6_COMPLETION_TOKEN Token
;
331 EFI_HANDLE Dns6Handle
;
332 EFI_IP6_CONFIG_PROTOCOL
*Ip6Config
;
333 EFI_IPv6_ADDRESS
*DnsServerList
;
334 UINTN DnsServerListCount
;
338 DnsServerList
= NULL
;
339 DnsServerListCount
= 0;
342 ZeroMem (&Token
, sizeof (EFI_DNS6_COMPLETION_TOKEN
));
345 // Get DNS server list from EFI IPv6 Configuration protocol.
347 Status
= gBS
->HandleProtocol (Private
->Controller
, &gEfiIp6ConfigProtocolGuid
, (VOID
**) &Ip6Config
);
348 if (!EFI_ERROR (Status
)) {
350 // Get the required size.
353 Status
= Ip6Config
->GetData (Ip6Config
, Ip6ConfigDataTypeDnsServer
, &DataSize
, NULL
);
354 if (Status
== EFI_BUFFER_TOO_SMALL
) {
355 DnsServerList
= AllocatePool (DataSize
);
356 if (DnsServerList
== NULL
) {
357 return EFI_OUT_OF_RESOURCES
;
360 Status
= Ip6Config
->GetData (Ip6Config
, Ip6ConfigDataTypeDnsServer
, &DataSize
, DnsServerList
);
361 if (EFI_ERROR (Status
)) {
362 FreePool (DnsServerList
);
363 DnsServerList
= NULL
;
365 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
;
448 if (Token
.RspData
.H2AData
->IpCount
== 0 || Token
.RspData
.H2AData
->IpList
== NULL
) {
449 Status
= EFI_DEVICE_ERROR
;
453 // We just return the first IPv6 address from DNS protocol.
455 IP6_COPY_ADDRESS (IpAddress
, Token
.RspData
.H2AData
->IpList
);
456 Status
= EFI_SUCCESS
;
460 if (Token
.Event
!= NULL
) {
461 gBS
->CloseEvent (Token
.Event
);
463 if (Token
.RspData
.H2AData
!= NULL
) {
464 if (Token
.RspData
.H2AData
->IpList
!= NULL
) {
465 FreePool (Token
.RspData
.H2AData
->IpList
);
467 FreePool (Token
.RspData
.H2AData
);
471 Dns6
->Configure (Dns6
, NULL
);
475 &gEfiDns6ProtocolGuid
,
476 Private
->Ip6Nic
->ImageHandle
,
481 if (Dns6Handle
!= NULL
) {
482 NetLibDestroyServiceChild (
484 Private
->Ip6Nic
->ImageHandle
,
485 &gEfiDns6ServiceBindingProtocolGuid
,
490 if (DnsServerList
!= NULL
) {
491 FreePool (DnsServerList
);
497 Create a HTTP_IO_HEADER to hold the HTTP header items.
499 @param[in] MaxHeaderCount The maximun number of HTTP header in this holder.
501 @return A pointer of the HTTP header holder or NULL if failed.
505 HttpBootCreateHeader (
509 HTTP_IO_HEADER
*HttpIoHeader
;
511 if (MaxHeaderCount
== 0) {
515 HttpIoHeader
= AllocateZeroPool (sizeof (HTTP_IO_HEADER
) + MaxHeaderCount
* sizeof (EFI_HTTP_HEADER
));
516 if (HttpIoHeader
== NULL
) {
520 HttpIoHeader
->MaxHeaderCount
= MaxHeaderCount
;
521 HttpIoHeader
->Headers
= (EFI_HTTP_HEADER
*) (HttpIoHeader
+ 1);
527 Destroy the HTTP_IO_HEADER and release the resouces.
529 @param[in] HttpIoHeader Point to the HTTP header holder to be destroyed.
534 IN HTTP_IO_HEADER
*HttpIoHeader
539 if (HttpIoHeader
!= NULL
) {
540 if (HttpIoHeader
->HeaderCount
!= 0) {
541 for (Index
= 0; Index
< HttpIoHeader
->HeaderCount
; Index
++) {
542 FreePool (HttpIoHeader
->Headers
[Index
].FieldName
);
543 FreePool (HttpIoHeader
->Headers
[Index
].FieldValue
);
546 FreePool (HttpIoHeader
);
551 Set or update a HTTP header with the field name and corresponding value.
553 @param[in] HttpIoHeader Point to the HTTP header holder.
554 @param[in] FieldName Null terminated string which describes a field name.
555 @param[in] FieldValue Null terminated string which describes the corresponding field value.
557 @retval EFI_SUCCESS The HTTP header has been set or updated.
558 @retval EFI_INVALID_PARAMETER Any input parameter is invalid.
559 @retval EFI_OUT_OF_RESOURCES Insufficient resource to complete the operation.
560 @retval Other Unexpected error happened.
565 IN HTTP_IO_HEADER
*HttpIoHeader
,
570 EFI_HTTP_HEADER
*Header
;
572 CHAR8
*NewFieldValue
;
574 if (HttpIoHeader
== NULL
|| FieldName
== NULL
|| FieldValue
== NULL
) {
575 return EFI_INVALID_PARAMETER
;
578 Header
= HttpFindHeader (HttpIoHeader
->HeaderCount
, HttpIoHeader
->Headers
, FieldName
);
579 if (Header
== NULL
) {
583 if (HttpIoHeader
->HeaderCount
>= HttpIoHeader
->MaxHeaderCount
) {
584 return EFI_OUT_OF_RESOURCES
;
586 Header
= &HttpIoHeader
->Headers
[HttpIoHeader
->HeaderCount
];
588 StrSize
= AsciiStrSize (FieldName
);
589 Header
->FieldName
= AllocatePool (StrSize
);
590 if (Header
->FieldName
== NULL
) {
591 return EFI_OUT_OF_RESOURCES
;
593 CopyMem (Header
->FieldName
, FieldName
, StrSize
);
594 Header
->FieldName
[StrSize
-1] = '\0';
596 StrSize
= AsciiStrSize (FieldValue
);
597 Header
->FieldValue
= AllocatePool (StrSize
);
598 if (Header
->FieldValue
== NULL
) {
599 FreePool (Header
->FieldName
);
600 return EFI_OUT_OF_RESOURCES
;
602 CopyMem (Header
->FieldValue
, FieldValue
, StrSize
);
603 Header
->FieldValue
[StrSize
-1] = '\0';
605 HttpIoHeader
->HeaderCount
++;
608 // Update an existing one.
610 StrSize
= AsciiStrSize (FieldValue
);
611 NewFieldValue
= AllocatePool (StrSize
);
612 if (NewFieldValue
== NULL
) {
613 return EFI_OUT_OF_RESOURCES
;
615 CopyMem (NewFieldValue
, FieldValue
, StrSize
);
616 NewFieldValue
[StrSize
-1] = '\0';
618 if (Header
->FieldValue
!= NULL
) {
619 FreePool (Header
->FieldValue
);
621 Header
->FieldValue
= NewFieldValue
;
628 Notify the callback function when an event is triggered.
630 @param[in] Context The opaque parameter to the function.
639 *((BOOLEAN
*) Context
) = TRUE
;
643 Request HttpIoNotifyDpc as a DPC at TPL_CALLBACK.
645 @param[in] Event The event signaled.
646 @param[in] Context The opaque parameter to the function.
657 // Request HttpIoNotifyDpc as a DPC at TPL_CALLBACK
659 QueueDpc (TPL_CALLBACK
, HttpIoNotifyDpc
, Context
);
663 Create a HTTP_IO to access the HTTP service. It will create and configure
666 @param[in] Image The handle of the driver image.
667 @param[in] Controller The handle of the controller.
668 @param[in] IpVersion IP_VERSION_4 or IP_VERSION_6.
669 @param[in] ConfigData The HTTP_IO configuration data.
670 @param[out] HttpIo The HTTP_IO.
672 @retval EFI_SUCCESS The HTTP_IO is created and configured.
673 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
674 @retval EFI_UNSUPPORTED One or more of the control options are not
675 supported in the implementation.
676 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory.
677 @retval Others Failed to create the HTTP_IO or configure it.
683 IN EFI_HANDLE Controller
,
685 IN HTTP_IO_CONFIG_DATA
*ConfigData
,
690 EFI_HTTP_CONFIG_DATA HttpConfigData
;
691 EFI_HTTPv4_ACCESS_POINT Http4AccessPoint
;
692 EFI_HTTPv6_ACCESS_POINT Http6AccessPoint
;
693 EFI_HTTP_PROTOCOL
*Http
;
696 if ((Image
== NULL
) || (Controller
== NULL
) || (ConfigData
== NULL
) || (HttpIo
== NULL
)) {
697 return EFI_INVALID_PARAMETER
;
700 if (IpVersion
!= IP_VERSION_4
&& IpVersion
!= IP_VERSION_6
) {
701 return EFI_UNSUPPORTED
;
704 ZeroMem (HttpIo
, sizeof (HTTP_IO
));
707 // Create the HTTP child instance and get the HTTP protocol.
709 Status
= NetLibCreateServiceChild (
712 &gEfiHttpServiceBindingProtocolGuid
,
715 if (EFI_ERROR (Status
)) {
719 Status
= gBS
->OpenProtocol (
721 &gEfiHttpProtocolGuid
,
725 EFI_OPEN_PROTOCOL_BY_DRIVER
727 if (EFI_ERROR (Status
) || (Http
== NULL
)) {
732 // Init the configuration data and configure the HTTP child.
734 HttpIo
->Image
= Image
;
735 HttpIo
->Controller
= Controller
;
736 HttpIo
->IpVersion
= IpVersion
;
739 ZeroMem (&HttpConfigData
, sizeof (EFI_HTTP_CONFIG_DATA
));
740 HttpConfigData
.HttpVersion
= HttpVersion11
;
741 HttpConfigData
.TimeOutMillisec
= ConfigData
->Config4
.RequestTimeOut
;
742 if (HttpIo
->IpVersion
== IP_VERSION_4
) {
743 HttpConfigData
.LocalAddressIsIPv6
= FALSE
;
745 Http4AccessPoint
.UseDefaultAddress
= ConfigData
->Config4
.UseDefaultAddress
;
746 Http4AccessPoint
.LocalPort
= ConfigData
->Config4
.LocalPort
;
747 IP4_COPY_ADDRESS (&Http4AccessPoint
.LocalAddress
, &ConfigData
->Config4
.LocalIp
);
748 IP4_COPY_ADDRESS (&Http4AccessPoint
.LocalSubnet
, &ConfigData
->Config4
.SubnetMask
);
749 HttpConfigData
.AccessPoint
.IPv4Node
= &Http4AccessPoint
;
751 HttpConfigData
.LocalAddressIsIPv6
= TRUE
;
752 Http6AccessPoint
.LocalPort
= ConfigData
->Config6
.LocalPort
;
753 IP6_COPY_ADDRESS (&Http6AccessPoint
.LocalAddress
, &ConfigData
->Config6
.LocalIp
);
754 HttpConfigData
.AccessPoint
.IPv6Node
= &Http6AccessPoint
;
757 Status
= Http
->Configure (Http
, &HttpConfigData
);
758 if (EFI_ERROR (Status
)) {
763 // Create events for variuos asynchronous operations.
765 Status
= gBS
->CreateEvent (
772 if (EFI_ERROR (Status
)) {
775 HttpIo
->ReqToken
.Event
= Event
;
776 HttpIo
->ReqToken
.Message
= &HttpIo
->ReqMessage
;
778 Status
= gBS
->CreateEvent (
785 if (EFI_ERROR (Status
)) {
788 HttpIo
->RspToken
.Event
= Event
;
789 HttpIo
->RspToken
.Message
= &HttpIo
->RspMessage
;
792 // Create TimeoutEvent for response
794 Status
= gBS
->CreateEvent (
801 if (EFI_ERROR (Status
)) {
804 HttpIo
->TimeoutEvent
= Event
;
809 HttpIoDestroyIo (HttpIo
);
815 Destroy the HTTP_IO and release the resouces.
817 @param[in] HttpIo The HTTP_IO which wraps the HTTP service to be destroyed.
825 EFI_HTTP_PROTOCOL
*Http
;
828 if (HttpIo
== NULL
) {
832 Event
= HttpIo
->ReqToken
.Event
;
834 gBS
->CloseEvent (Event
);
837 Event
= HttpIo
->RspToken
.Event
;
839 gBS
->CloseEvent (Event
);
842 Event
= HttpIo
->TimeoutEvent
;
844 gBS
->CloseEvent (Event
);
849 Http
->Configure (Http
, NULL
);
852 &gEfiHttpProtocolGuid
,
858 NetLibDestroyServiceChild (
861 &gEfiHttpServiceBindingProtocolGuid
,
867 Synchronously send a HTTP REQUEST message to the server.
869 @param[in] HttpIo The HttpIo wrapping the HTTP service.
870 @param[in] Request A pointer to storage such data as URL and HTTP method.
871 @param[in] HeaderCount Number of HTTP header structures in Headers list.
872 @param[in] Headers Array containing list of HTTP headers.
873 @param[in] BodyLength Length in bytes of the HTTP body.
874 @param[in] Body Body associated with the HTTP request.
876 @retval EFI_SUCCESS The HTTP request is trasmitted.
877 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
878 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory.
879 @retval EFI_DEVICE_ERROR An unexpected network or system error occurred.
880 @retval Others Other errors as indicated.
886 IN EFI_HTTP_REQUEST_DATA
*Request
,
887 IN UINTN HeaderCount
,
888 IN EFI_HTTP_HEADER
*Headers
,
894 EFI_HTTP_PROTOCOL
*Http
;
896 if (HttpIo
== NULL
|| HttpIo
->Http
== NULL
) {
897 return EFI_INVALID_PARAMETER
;
900 HttpIo
->ReqToken
.Status
= EFI_NOT_READY
;
901 HttpIo
->ReqToken
.Message
->Data
.Request
= Request
;
902 HttpIo
->ReqToken
.Message
->HeaderCount
= HeaderCount
;
903 HttpIo
->ReqToken
.Message
->Headers
= Headers
;
904 HttpIo
->ReqToken
.Message
->BodyLength
= BodyLength
;
905 HttpIo
->ReqToken
.Message
->Body
= Body
;
908 // Queue the request token to HTTP instances.
911 HttpIo
->IsTxDone
= FALSE
;
912 Status
= Http
->Request (
916 if (EFI_ERROR (Status
)) {
921 // Poll the network until transmit finish.
923 while (!HttpIo
->IsTxDone
) {
927 return HttpIo
->ReqToken
.Status
;
931 Synchronously receive a HTTP RESPONSE message from the server.
933 @param[in] HttpIo The HttpIo wrapping the HTTP service.
934 @param[in] RecvMsgHeader TRUE to receive a new HTTP response (from message header).
935 FALSE to continue receive the previous response message.
936 @param[out] ResponseData Point to a wrapper of the received response data.
938 @retval EFI_SUCCESS The HTTP response is received.
939 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
940 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory.
941 @retval EFI_DEVICE_ERROR An unexpected network or system error occurred.
942 @retval Others Other errors as indicated.
948 IN BOOLEAN RecvMsgHeader
,
949 OUT HTTP_IO_RESPONSE_DATA
*ResponseData
953 EFI_HTTP_PROTOCOL
*Http
;
955 if (HttpIo
== NULL
|| HttpIo
->Http
== NULL
|| ResponseData
== NULL
) {
956 return EFI_INVALID_PARAMETER
;
960 // Start the timer, and wait Timeout seconds to receive the header packet.
962 Status
= gBS
->SetTimer (HttpIo
->TimeoutEvent
, TimerRelative
, HTTP_BOOT_RESPONSE_TIMEOUT
* TICKS_PER_MS
);
963 if (EFI_ERROR (Status
)) {
968 // Queue the response token to HTTP instances.
970 HttpIo
->RspToken
.Status
= EFI_NOT_READY
;
972 HttpIo
->RspToken
.Message
->Data
.Response
= &ResponseData
->Response
;
974 HttpIo
->RspToken
.Message
->Data
.Response
= NULL
;
976 HttpIo
->RspToken
.Message
->HeaderCount
= 0;
977 HttpIo
->RspToken
.Message
->Headers
= NULL
;
978 HttpIo
->RspToken
.Message
->BodyLength
= ResponseData
->BodyLength
;
979 HttpIo
->RspToken
.Message
->Body
= ResponseData
->Body
;
982 HttpIo
->IsRxDone
= FALSE
;
983 Status
= Http
->Response (
988 if (EFI_ERROR (Status
)) {
989 gBS
->SetTimer (HttpIo
->TimeoutEvent
, TimerCancel
, 0);
994 // Poll the network until receive finish.
996 while (!HttpIo
->IsRxDone
&& ((HttpIo
->TimeoutEvent
== NULL
) || EFI_ERROR (gBS
->CheckEvent (HttpIo
->TimeoutEvent
)))) {
1000 gBS
->SetTimer (HttpIo
->TimeoutEvent
, TimerCancel
, 0);
1002 if (!HttpIo
->IsRxDone
) {
1004 // Timeout occurs, cancel the response token.
1006 Http
->Cancel (Http
, &HttpIo
->RspToken
);
1008 Status
= EFI_TIMEOUT
;
1012 HttpIo
->IsRxDone
= FALSE
;
1016 // Store the received data into the wrapper.
1018 ResponseData
->Status
= HttpIo
->RspToken
.Status
;
1019 ResponseData
->HeaderCount
= HttpIo
->RspToken
.Message
->HeaderCount
;
1020 ResponseData
->Headers
= HttpIo
->RspToken
.Message
->Headers
;
1021 ResponseData
->BodyLength
= HttpIo
->RspToken
.Message
->BodyLength
;
1027 This function checks the HTTP(S) URI scheme.
1029 @param[in] Uri The pointer to the URI string.
1031 @retval EFI_SUCCESS The URI scheme is valid.
1032 @retval EFI_INVALID_PARAMETER The URI scheme is not HTTP or HTTPS.
1033 @retval EFI_ACCESS_DENIED HTTP is disabled and the URI is HTTP.
1037 HttpBootCheckUriScheme (
1044 Status
= EFI_SUCCESS
;
1047 // Convert the scheme to all lower case.
1049 for (Index
= 0; Index
< AsciiStrLen (Uri
); Index
++) {
1050 if (Uri
[Index
] == ':') {
1053 if (Uri
[Index
] >= 'A' && Uri
[Index
] <= 'Z') {
1054 Uri
[Index
] -= (CHAR8
)('A' - 'a');
1059 // Return EFI_INVALID_PARAMETER if the URI is not HTTP or HTTPS.
1061 if ((AsciiStrnCmp (Uri
, "http://", 7) != 0) && (AsciiStrnCmp (Uri
, "https://", 8) != 0)) {
1062 DEBUG ((EFI_D_ERROR
, "HttpBootCheckUriScheme: Invalid Uri.\n"));
1063 return EFI_INVALID_PARAMETER
;
1067 // HTTP is disabled, return EFI_ACCESS_DENIED if the URI is HTTP.
1069 if (!PcdGetBool (PcdAllowHttpConnections
) && (AsciiStrnCmp (Uri
, "http://", 7) == 0)) {
1070 DEBUG ((EFI_D_ERROR
, "HttpBootCheckUriScheme: HTTP is disabled.\n"));
1071 return EFI_ACCESS_DENIED
;
1078 Get the URI address string from the input device path.
1080 Caller need to free the buffer in the UriAddress pointer.
1082 @param[in] FilePath Pointer to the device path which contains a URI device path node.
1083 @param[out] UriAddress The URI address string extract from the device path.
1085 @retval EFI_SUCCESS The URI string is returned.
1086 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory.
1090 HttpBootParseFilePath (
1091 IN EFI_DEVICE_PATH_PROTOCOL
*FilePath
,
1092 OUT CHAR8
**UriAddress
1095 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePath
;
1096 URI_DEVICE_PATH
*UriDevicePath
;
1100 if (FilePath
== NULL
) {
1101 return EFI_INVALID_PARAMETER
;
1107 // Extract the URI address from the FilePath
1109 TempDevicePath
= FilePath
;
1110 while (!IsDevicePathEnd (TempDevicePath
)) {
1111 if ((DevicePathType (TempDevicePath
) == MESSAGING_DEVICE_PATH
) &&
1112 (DevicePathSubType (TempDevicePath
) == MSG_URI_DP
)) {
1113 UriDevicePath
= (URI_DEVICE_PATH
*) TempDevicePath
;
1115 // UEFI Spec doesn't require the URI to be a NULL-terminated string
1116 // So we allocate a new buffer and always append a '\0' to it.
1118 UriStrLength
= DevicePathNodeLength (UriDevicePath
) - sizeof(EFI_DEVICE_PATH_PROTOCOL
);
1119 if (UriStrLength
== 0) {
1121 // return a NULL UriAddress if it's a empty URI device path node.
1125 Uri
= AllocatePool (UriStrLength
+ 1);
1127 return EFI_OUT_OF_RESOURCES
;
1129 CopyMem (Uri
, UriDevicePath
->Uri
, DevicePathNodeLength (UriDevicePath
) - sizeof(EFI_DEVICE_PATH_PROTOCOL
));
1130 Uri
[DevicePathNodeLength (UriDevicePath
) - sizeof(EFI_DEVICE_PATH_PROTOCOL
)] = '\0';
1134 TempDevicePath
= NextDevicePathNode (TempDevicePath
);
1141 This function returns the image type according to server replied HTTP message
1142 and also the image's URI info.
1144 @param[in] Uri The pointer to the image's URI string.
1145 @param[in] UriParser URI Parse result returned by NetHttpParseUrl().
1146 @param[in] HeaderCount Number of HTTP header structures in Headers list.
1147 @param[in] Headers Array containing list of HTTP headers.
1148 @param[out] ImageType The image type of the downloaded file.
1150 @retval EFI_SUCCESS The image type is returned in ImageType.
1151 @retval EFI_INVALID_PARAMETER ImageType, Uri or UriParser is NULL.
1152 @retval EFI_INVALID_PARAMETER HeaderCount is not zero, and Headers is NULL.
1153 @retval EFI_NOT_FOUND Failed to identify the image type.
1154 @retval Others Unexpect error happened.
1158 HttpBootCheckImageType (
1161 IN UINTN HeaderCount
,
1162 IN EFI_HTTP_HEADER
*Headers
,
1163 OUT HTTP_BOOT_IMAGE_TYPE
*ImageType
1167 EFI_HTTP_HEADER
*Header
;
1171 if (Uri
== NULL
|| UriParser
== NULL
|| ImageType
== NULL
) {
1172 return EFI_INVALID_PARAMETER
;
1175 if (HeaderCount
!= 0 && Headers
== NULL
) {
1176 return EFI_INVALID_PARAMETER
;
1180 // Determine the image type by the HTTP Content-Type header field first.
1181 // "application/efi" -> EFI Image
1183 Header
= HttpFindHeader (HeaderCount
, Headers
, HTTP_HEADER_CONTENT_TYPE
);
1184 if (Header
!= NULL
) {
1185 if (AsciiStriCmp (Header
->FieldValue
, HTTP_CONTENT_TYPE_APP_EFI
) == 0) {
1186 *ImageType
= ImageTypeEfi
;
1192 // Determine the image type by file extension:
1193 // *.efi -> EFI Image
1194 // *.iso -> CD/DVD Image
1195 // *.img -> Virtual Disk Image
1197 Status
= HttpUrlGetPath (
1202 if (EFI_ERROR (Status
)) {
1206 FilePost
= FilePath
+ AsciiStrLen (FilePath
) - 4;
1207 if (AsciiStrCmp (FilePost
, ".efi") == 0) {
1208 *ImageType
= ImageTypeEfi
;
1209 } else if (AsciiStrCmp (FilePost
, ".iso") == 0) {
1210 *ImageType
= ImageTypeVirtualCd
;
1211 } else if (AsciiStrCmp (FilePost
, ".img") == 0) {
1212 *ImageType
= ImageTypeVirtualDisk
;
1214 *ImageType
= ImageTypeMax
;
1217 FreePool (FilePath
);
1219 return (*ImageType
< ImageTypeMax
) ? EFI_SUCCESS
: EFI_NOT_FOUND
;
1223 This function register the RAM disk info to the system.
1225 @param[in] Private The pointer to the driver's private data.
1226 @param[in] BufferSize The size of Buffer in bytes.
1227 @param[in] Buffer The base address of the RAM disk.
1228 @param[in] ImageType The image type of the file in Buffer.
1230 @retval EFI_SUCCESS The RAM disk has been registered.
1231 @retval EFI_NOT_FOUND No RAM disk protocol instances were found.
1232 @retval EFI_UNSUPPORTED The ImageType is not supported.
1233 @retval Others Unexpected error happened.
1237 HttpBootRegisterRamDisk (
1238 IN HTTP_BOOT_PRIVATE_DATA
*Private
,
1239 IN UINTN BufferSize
,
1241 IN HTTP_BOOT_IMAGE_TYPE ImageType
1244 EFI_RAM_DISK_PROTOCOL
*RamDisk
;
1246 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
1247 EFI_GUID
*RamDiskType
;
1249 ASSERT (Private
!= NULL
);
1250 ASSERT (Buffer
!= NULL
);
1251 ASSERT (BufferSize
!= 0);
1253 Status
= gBS
->LocateProtocol (&gEfiRamDiskProtocolGuid
, NULL
, (VOID
**) &RamDisk
);
1254 if (EFI_ERROR (Status
)) {
1255 DEBUG ((EFI_D_ERROR
, "HTTP Boot: Couldn't find the RAM Disk protocol - %r\n", Status
));
1259 if (ImageType
== ImageTypeVirtualCd
) {
1260 RamDiskType
= &gEfiVirtualCdGuid
;
1261 } else if (ImageType
== ImageTypeVirtualDisk
) {
1262 RamDiskType
= &gEfiVirtualDiskGuid
;
1264 return EFI_UNSUPPORTED
;
1267 Status
= RamDisk
->Register (
1271 Private
->UsingIpv6
? Private
->Ip6Nic
->DevicePath
: Private
->Ip4Nic
->DevicePath
,
1274 if (EFI_ERROR (Status
)) {
1275 DEBUG ((EFI_D_ERROR
, "HTTP Boot: Failed to register RAM Disk - %r\n", Status
));