2 Support functions implementation for UEFI HTTP boot driver.
4 Copyright (c) 2015 - 2016, 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 (
91 Remainder
= Number
% 10;
93 Buffer
[Length
] = (UINT8
) ('0' + Remainder
);
98 This function is to display the IPv4 address.
100 @param[in] Ip The pointer to the IPv4 address.
104 HttpBootShowIp4Addr (
105 IN EFI_IPv4_ADDRESS
*Ip
110 for (Index
= 0; Index
< 4; Index
++) {
111 AsciiPrint ("%d", Ip
->Addr
[Index
]);
119 This function is to display the IPv6 address.
121 @param[in] Ip The pointer to the IPv6 address.
125 HttpBootShowIp6Addr (
126 IN EFI_IPv6_ADDRESS
*Ip
131 for (Index
= 0; Index
< 16; Index
++) {
133 if (Ip
->Addr
[Index
] != 0) {
134 AsciiPrint ("%x", Ip
->Addr
[Index
]);
140 if (((Ip
->Addr
[Index
] & 0xf0) == 0) && (Ip
->Addr
[Index
- 1] != 0)) {
143 AsciiPrint ("%x", Ip
->Addr
[Index
]);
151 This function is to display the HTTP error status.
153 @param[in] StatusCode The status code value in HTTP message.
157 HttpBootPrintErrorMessage (
158 EFI_HTTP_STATUS_CODE StatusCode
163 switch (StatusCode
) {
164 case HTTP_STATUS_300_MULTIPLE_CHIOCES
:
165 AsciiPrint ("\n Redirection: 300 Multiple Choices");
168 case HTTP_STATUS_301_MOVED_PERMANENTLY
:
169 AsciiPrint ("\n Redirection: 301 Moved Permanently");
172 case HTTP_STATUS_302_FOUND
:
173 AsciiPrint ("\n Redirection: 302 Found");
176 case HTTP_STATUS_303_SEE_OTHER
:
177 AsciiPrint ("\n Redirection: 303 See Other");
180 case HTTP_STATUS_304_NOT_MODIFIED
:
181 AsciiPrint ("\n Redirection: 304 Not Modified");
184 case HTTP_STATUS_305_USE_PROXY
:
185 AsciiPrint ("\n Redirection: 305 Use Proxy");
188 case HTTP_STATUS_307_TEMPORARY_REDIRECT
:
189 AsciiPrint ("\n Redirection: 307 Temporary Redirect");
192 case HTTP_STATUS_400_BAD_REQUEST
:
193 AsciiPrint ("\n Client Error: 400 Bad Request");
196 case HTTP_STATUS_401_UNAUTHORIZED
:
197 AsciiPrint ("\n Client Error: 401 Unauthorized");
200 case HTTP_STATUS_402_PAYMENT_REQUIRED
:
201 AsciiPrint ("\n Client Error: 402 Payment Required");
204 case HTTP_STATUS_403_FORBIDDEN
:
205 AsciiPrint ("\n Client Error: 403 Forbidden");
208 case HTTP_STATUS_404_NOT_FOUND
:
209 AsciiPrint ("\n Client Error: 404 Not Found");
212 case HTTP_STATUS_405_METHOD_NOT_ALLOWED
:
213 AsciiPrint ("\n Client Error: 405 Method Not Allowed");
216 case HTTP_STATUS_406_NOT_ACCEPTABLE
:
217 AsciiPrint ("\n Client Error: 406 Not Acceptable");
220 case HTTP_STATUS_407_PROXY_AUTHENTICATION_REQUIRED
:
221 AsciiPrint ("\n Client Error: 407 Proxy Authentication Required");
224 case HTTP_STATUS_408_REQUEST_TIME_OUT
:
225 AsciiPrint ("\n Client Error: 408 Request Timeout");
228 case HTTP_STATUS_409_CONFLICT
:
229 AsciiPrint ("\n Client Error: 409 Conflict");
232 case HTTP_STATUS_410_GONE
:
233 AsciiPrint ("\n Client Error: 410 Gone");
236 case HTTP_STATUS_411_LENGTH_REQUIRED
:
237 AsciiPrint ("\n Client Error: 411 Length Required");
240 case HTTP_STATUS_412_PRECONDITION_FAILED
:
241 AsciiPrint ("\n Client Error: 412 Precondition Failed");
244 case HTTP_STATUS_413_REQUEST_ENTITY_TOO_LARGE
:
245 AsciiPrint ("\n Client Error: 413 Request Entity Too Large");
248 case HTTP_STATUS_414_REQUEST_URI_TOO_LARGE
:
249 AsciiPrint ("\n Client Error: 414 Request URI Too Long");
252 case HTTP_STATUS_415_UNSUPPORTED_MEDIA_TYPE
:
253 AsciiPrint ("\n Client Error: 415 Unsupported Media Type");
256 case HTTP_STATUS_416_REQUESTED_RANGE_NOT_SATISFIED
:
257 AsciiPrint ("\n Client Error: 416 Requested Range Not Satisfiable");
260 case HTTP_STATUS_417_EXPECTATION_FAILED
:
261 AsciiPrint ("\n Client Error: 417 Expectation Failed");
264 case HTTP_STATUS_500_INTERNAL_SERVER_ERROR
:
265 AsciiPrint ("\n Server Error: 500 Internal Server Error");
268 case HTTP_STATUS_501_NOT_IMPLEMENTED
:
269 AsciiPrint ("\n Server Error: 501 Not Implemented");
272 case HTTP_STATUS_502_BAD_GATEWAY
:
273 AsciiPrint ("\n Server Error: 502 Bad Gateway");
276 case HTTP_STATUS_503_SERVICE_UNAVAILABLE
:
277 AsciiPrint ("\n Server Error: 503 Service Unavailable");
280 case HTTP_STATUS_504_GATEWAY_TIME_OUT
:
281 AsciiPrint ("\n Server Error: 504 Gateway Timeout");
284 case HTTP_STATUS_505_HTTP_VERSION_NOT_SUPPORTED
:
285 AsciiPrint ("\n Server Error: 505 HTTP Version Not Supported");
294 Notify the callback function when an event is triggered.
296 @param[in] Event The triggered event.
297 @param[in] Context The opaque parameter to the function.
302 HttpBootCommonNotify (
307 *((BOOLEAN
*) Context
) = TRUE
;
311 Retrieve the host address using the EFI_DNS6_PROTOCOL.
313 @param[in] Private The pointer to the driver's private data.
314 @param[in] HostName Pointer to buffer containing hostname.
315 @param[out] IpAddress On output, pointer to buffer containing IPv6 address.
317 @retval EFI_SUCCESS Operation succeeded.
318 @retval EFI_DEVICE_ERROR An unexpected network error occurred.
319 @retval Others Other errors as indicated.
323 IN HTTP_BOOT_PRIVATE_DATA
*Private
,
325 OUT EFI_IPv6_ADDRESS
*IpAddress
329 EFI_DNS6_PROTOCOL
*Dns6
;
330 EFI_DNS6_CONFIG_DATA Dns6ConfigData
;
331 EFI_DNS6_COMPLETION_TOKEN Token
;
332 EFI_HANDLE Dns6Handle
;
333 EFI_IP6_CONFIG_PROTOCOL
*Ip6Config
;
334 EFI_IPv6_ADDRESS
*DnsServerList
;
335 UINTN DnsServerListCount
;
339 DnsServerList
= NULL
;
340 DnsServerListCount
= 0;
343 ZeroMem (&Token
, sizeof (EFI_DNS6_COMPLETION_TOKEN
));
346 // Get DNS server list from EFI IPv6 Configuration protocol.
348 Status
= gBS
->HandleProtocol (Private
->Controller
, &gEfiIp6ConfigProtocolGuid
, (VOID
**) &Ip6Config
);
349 if (!EFI_ERROR (Status
)) {
351 // Get the required size.
354 Status
= Ip6Config
->GetData (Ip6Config
, Ip6ConfigDataTypeDnsServer
, &DataSize
, NULL
);
355 if (Status
== EFI_BUFFER_TOO_SMALL
) {
356 DnsServerList
= AllocatePool (DataSize
);
357 if (DnsServerList
== NULL
) {
358 return EFI_OUT_OF_RESOURCES
;
361 Status
= Ip6Config
->GetData (Ip6Config
, Ip6ConfigDataTypeDnsServer
, &DataSize
, DnsServerList
);
362 if (EFI_ERROR (Status
)) {
363 FreePool (DnsServerList
);
364 DnsServerList
= NULL
;
366 DnsServerListCount
= DataSize
/ sizeof (EFI_IPv6_ADDRESS
);
371 // Create a DNSv6 child instance and get the protocol.
373 Status
= NetLibCreateServiceChild (
375 Private
->Ip6Nic
->ImageHandle
,
376 &gEfiDns6ServiceBindingProtocolGuid
,
379 if (EFI_ERROR (Status
)) {
383 Status
= gBS
->OpenProtocol (
385 &gEfiDns6ProtocolGuid
,
387 Private
->Ip6Nic
->ImageHandle
,
389 EFI_OPEN_PROTOCOL_BY_DRIVER
391 if (EFI_ERROR (Status
)) {
396 // Configure DNS6 instance for the DNS server address and protocol.
398 ZeroMem (&Dns6ConfigData
, sizeof (EFI_DNS6_CONFIG_DATA
));
399 Dns6ConfigData
.DnsServerCount
= (UINT32
)DnsServerListCount
;
400 Dns6ConfigData
.DnsServerList
= DnsServerList
;
401 Dns6ConfigData
.EnableDnsCache
= TRUE
;
402 Dns6ConfigData
.Protocol
= EFI_IP_PROTO_UDP
;
403 IP6_COPY_ADDRESS (&Dns6ConfigData
.StationIp
,&Private
->StationIp
.v6
);
404 Status
= Dns6
->Configure (
408 if (EFI_ERROR (Status
)) {
412 Token
.Status
= EFI_NOT_READY
;
415 // Create event to set the IsDone flag when name resolution is finished.
417 Status
= gBS
->CreateEvent (
420 HttpBootCommonNotify
,
424 if (EFI_ERROR (Status
)) {
429 // Start asynchronous name resolution.
431 Status
= Dns6
->HostNameToIp (Dns6
, HostName
, &Token
);
432 if (EFI_ERROR (Status
)) {
441 // Name resolution is done, check result.
443 Status
= Token
.Status
;
444 if (!EFI_ERROR (Status
)) {
445 if (Token
.RspData
.H2AData
== NULL
) {
446 Status
= EFI_DEVICE_ERROR
;
449 if (Token
.RspData
.H2AData
->IpCount
== 0 || Token
.RspData
.H2AData
->IpList
== NULL
) {
450 Status
= EFI_DEVICE_ERROR
;
454 // We just return the first IPv6 address from DNS protocol.
456 IP6_COPY_ADDRESS (IpAddress
, Token
.RspData
.H2AData
->IpList
);
457 Status
= EFI_SUCCESS
;
461 if (Token
.Event
!= NULL
) {
462 gBS
->CloseEvent (Token
.Event
);
464 if (Token
.RspData
.H2AData
!= NULL
) {
465 if (Token
.RspData
.H2AData
->IpList
!= NULL
) {
466 FreePool (Token
.RspData
.H2AData
->IpList
);
468 FreePool (Token
.RspData
.H2AData
);
472 Dns6
->Configure (Dns6
, NULL
);
476 &gEfiDns6ProtocolGuid
,
477 Private
->Ip6Nic
->ImageHandle
,
482 if (Dns6Handle
!= NULL
) {
483 NetLibDestroyServiceChild (
485 Private
->Ip6Nic
->ImageHandle
,
486 &gEfiDns6ServiceBindingProtocolGuid
,
491 if (DnsServerList
!= NULL
) {
492 FreePool (DnsServerList
);
498 Create a HTTP_IO_HEADER to hold the HTTP header items.
500 @param[in] MaxHeaderCount The maximun number of HTTP header in this holder.
502 @return A pointer of the HTTP header holder or NULL if failed.
506 HttpBootCreateHeader (
510 HTTP_IO_HEADER
*HttpIoHeader
;
512 if (MaxHeaderCount
== 0) {
516 HttpIoHeader
= AllocateZeroPool (sizeof (HTTP_IO_HEADER
) + MaxHeaderCount
* sizeof (EFI_HTTP_HEADER
));
517 if (HttpIoHeader
== NULL
) {
521 HttpIoHeader
->MaxHeaderCount
= MaxHeaderCount
;
522 HttpIoHeader
->Headers
= (EFI_HTTP_HEADER
*) (HttpIoHeader
+ 1);
528 Destroy the HTTP_IO_HEADER and release the resouces.
530 @param[in] HttpIoHeader Point to the HTTP header holder to be destroyed.
535 IN HTTP_IO_HEADER
*HttpIoHeader
540 if (HttpIoHeader
!= NULL
) {
541 if (HttpIoHeader
->HeaderCount
!= 0) {
542 for (Index
= 0; Index
< HttpIoHeader
->HeaderCount
; Index
++) {
543 FreePool (HttpIoHeader
->Headers
[Index
].FieldName
);
544 FreePool (HttpIoHeader
->Headers
[Index
].FieldValue
);
547 FreePool (HttpIoHeader
);
552 Set or update a HTTP header with the field name and corresponding value.
554 @param[in] HttpIoHeader Point to the HTTP header holder.
555 @param[in] FieldName Null terminated string which describes a field name.
556 @param[in] FieldValue Null terminated string which describes the corresponding field value.
558 @retval EFI_SUCCESS The HTTP header has been set or updated.
559 @retval EFI_INVALID_PARAMETER Any input parameter is invalid.
560 @retval EFI_OUT_OF_RESOURCES Insufficient resource to complete the operation.
561 @retval Other Unexpected error happened.
566 IN HTTP_IO_HEADER
*HttpIoHeader
,
571 EFI_HTTP_HEADER
*Header
;
573 CHAR8
*NewFieldValue
;
575 if (HttpIoHeader
== NULL
|| FieldName
== NULL
|| FieldValue
== NULL
) {
576 return EFI_INVALID_PARAMETER
;
579 Header
= HttpFindHeader (HttpIoHeader
->HeaderCount
, HttpIoHeader
->Headers
, FieldName
);
580 if (Header
== NULL
) {
584 if (HttpIoHeader
->HeaderCount
>= HttpIoHeader
->MaxHeaderCount
) {
585 return EFI_OUT_OF_RESOURCES
;
587 Header
= &HttpIoHeader
->Headers
[HttpIoHeader
->HeaderCount
];
589 StrSize
= AsciiStrSize (FieldName
);
590 Header
->FieldName
= AllocatePool (StrSize
);
591 if (Header
->FieldName
== NULL
) {
592 return EFI_OUT_OF_RESOURCES
;
594 CopyMem (Header
->FieldName
, FieldName
, StrSize
);
595 Header
->FieldName
[StrSize
-1] = '\0';
597 StrSize
= AsciiStrSize (FieldValue
);
598 Header
->FieldValue
= AllocatePool (StrSize
);
599 if (Header
->FieldValue
== NULL
) {
600 FreePool (Header
->FieldName
);
601 return EFI_OUT_OF_RESOURCES
;
603 CopyMem (Header
->FieldValue
, FieldValue
, StrSize
);
604 Header
->FieldValue
[StrSize
-1] = '\0';
606 HttpIoHeader
->HeaderCount
++;
609 // Update an existing one.
611 StrSize
= AsciiStrSize (FieldValue
);
612 NewFieldValue
= AllocatePool (StrSize
);
613 if (NewFieldValue
== NULL
) {
614 return EFI_OUT_OF_RESOURCES
;
616 CopyMem (NewFieldValue
, FieldValue
, StrSize
);
617 NewFieldValue
[StrSize
-1] = '\0';
619 if (Header
->FieldValue
!= NULL
) {
620 FreePool (Header
->FieldValue
);
622 Header
->FieldValue
= NewFieldValue
;
629 Create a HTTP_IO to access the HTTP service. It will create and configure
632 @param[in] Image The handle of the driver image.
633 @param[in] Controller The handle of the controller.
634 @param[in] IpVersion IP_VERSION_4 or IP_VERSION_6.
635 @param[in] ConfigData The HTTP_IO configuration data.
636 @param[out] HttpIo The HTTP_IO.
638 @retval EFI_SUCCESS The HTTP_IO is created and configured.
639 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
640 @retval EFI_UNSUPPORTED One or more of the control options are not
641 supported in the implementation.
642 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory.
643 @retval Others Failed to create the HTTP_IO or configure it.
649 IN EFI_HANDLE Controller
,
651 IN HTTP_IO_CONFIG_DATA
*ConfigData
,
656 EFI_HTTP_CONFIG_DATA HttpConfigData
;
657 EFI_HTTPv4_ACCESS_POINT Http4AccessPoint
;
658 EFI_HTTPv6_ACCESS_POINT Http6AccessPoint
;
659 EFI_HTTP_PROTOCOL
*Http
;
662 if ((Image
== NULL
) || (Controller
== NULL
) || (ConfigData
== NULL
) || (HttpIo
== NULL
)) {
663 return EFI_INVALID_PARAMETER
;
666 if (IpVersion
!= IP_VERSION_4
&& IpVersion
!= IP_VERSION_6
) {
667 return EFI_UNSUPPORTED
;
670 ZeroMem (HttpIo
, sizeof (HTTP_IO
));
673 // Create the HTTP child instance and get the HTTP protocol.
675 Status
= NetLibCreateServiceChild (
678 &gEfiHttpServiceBindingProtocolGuid
,
681 if (EFI_ERROR (Status
)) {
685 Status
= gBS
->OpenProtocol (
687 &gEfiHttpProtocolGuid
,
691 EFI_OPEN_PROTOCOL_BY_DRIVER
693 if (EFI_ERROR (Status
) || (Http
== NULL
)) {
698 // Init the configuration data and configure the HTTP child.
700 HttpIo
->Image
= Image
;
701 HttpIo
->Controller
= Controller
;
702 HttpIo
->IpVersion
= IpVersion
;
705 ZeroMem (&HttpConfigData
, sizeof (EFI_HTTP_CONFIG_DATA
));
706 HttpConfigData
.HttpVersion
= HttpVersion11
;
707 HttpConfigData
.TimeOutMillisec
= ConfigData
->Config4
.RequestTimeOut
;
708 if (HttpIo
->IpVersion
== IP_VERSION_4
) {
709 HttpConfigData
.LocalAddressIsIPv6
= FALSE
;
711 Http4AccessPoint
.UseDefaultAddress
= ConfigData
->Config4
.UseDefaultAddress
;
712 Http4AccessPoint
.LocalPort
= ConfigData
->Config4
.LocalPort
;
713 IP4_COPY_ADDRESS (&Http4AccessPoint
.LocalAddress
, &ConfigData
->Config4
.LocalIp
);
714 IP4_COPY_ADDRESS (&Http4AccessPoint
.LocalSubnet
, &ConfigData
->Config4
.SubnetMask
);
715 HttpConfigData
.AccessPoint
.IPv4Node
= &Http4AccessPoint
;
717 HttpConfigData
.LocalAddressIsIPv6
= TRUE
;
718 Http6AccessPoint
.LocalPort
= ConfigData
->Config6
.LocalPort
;
719 IP6_COPY_ADDRESS (&Http6AccessPoint
.LocalAddress
, &ConfigData
->Config6
.LocalIp
);
720 HttpConfigData
.AccessPoint
.IPv6Node
= &Http6AccessPoint
;
723 Status
= Http
->Configure (Http
, &HttpConfigData
);
724 if (EFI_ERROR (Status
)) {
729 // Create events for variuos asynchronous operations.
731 Status
= gBS
->CreateEvent (
734 HttpBootCommonNotify
,
738 if (EFI_ERROR (Status
)) {
741 HttpIo
->ReqToken
.Event
= Event
;
742 HttpIo
->ReqToken
.Message
= &HttpIo
->ReqMessage
;
744 Status
= gBS
->CreateEvent (
747 HttpBootCommonNotify
,
751 if (EFI_ERROR (Status
)) {
754 HttpIo
->RspToken
.Event
= Event
;
755 HttpIo
->RspToken
.Message
= &HttpIo
->RspMessage
;
760 HttpIoDestroyIo (HttpIo
);
766 Destroy the HTTP_IO and release the resouces.
768 @param[in] HttpIo The HTTP_IO which wraps the HTTP service to be destroyed.
776 EFI_HTTP_PROTOCOL
*Http
;
779 if (HttpIo
== NULL
) {
783 Event
= HttpIo
->ReqToken
.Event
;
785 gBS
->CloseEvent (Event
);
788 Event
= HttpIo
->RspToken
.Event
;
790 gBS
->CloseEvent (Event
);
795 Http
->Configure (Http
, NULL
);
798 &gEfiHttpProtocolGuid
,
804 NetLibDestroyServiceChild (
807 &gEfiHttpServiceBindingProtocolGuid
,
813 Synchronously send a HTTP REQUEST message to the server.
815 @param[in] HttpIo The HttpIo wrapping the HTTP service.
816 @param[in] Request A pointer to storage such data as URL and HTTP method.
817 @param[in] HeaderCount Number of HTTP header structures in Headers list.
818 @param[in] Headers Array containing list of HTTP headers.
819 @param[in] BodyLength Length in bytes of the HTTP body.
820 @param[in] Body Body associated with the HTTP request.
822 @retval EFI_SUCCESS The HTTP request is trasmitted.
823 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
824 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory.
825 @retval EFI_DEVICE_ERROR An unexpected network or system error occurred.
826 @retval Others Other errors as indicated.
832 IN EFI_HTTP_REQUEST_DATA
*Request
,
833 IN UINTN HeaderCount
,
834 IN EFI_HTTP_HEADER
*Headers
,
840 EFI_HTTP_PROTOCOL
*Http
;
842 if (HttpIo
== NULL
|| HttpIo
->Http
== NULL
) {
843 return EFI_INVALID_PARAMETER
;
846 HttpIo
->ReqToken
.Status
= EFI_NOT_READY
;
847 HttpIo
->ReqToken
.Message
->Data
.Request
= Request
;
848 HttpIo
->ReqToken
.Message
->HeaderCount
= HeaderCount
;
849 HttpIo
->ReqToken
.Message
->Headers
= Headers
;
850 HttpIo
->ReqToken
.Message
->BodyLength
= BodyLength
;
851 HttpIo
->ReqToken
.Message
->Body
= Body
;
854 // Queue the request token to HTTP instances.
857 HttpIo
->IsTxDone
= FALSE
;
858 Status
= Http
->Request (
862 if (EFI_ERROR (Status
)) {
867 // Poll the network until transmit finish.
869 while (!HttpIo
->IsTxDone
) {
873 return HttpIo
->ReqToken
.Status
;
877 Synchronously receive a HTTP RESPONSE message from the server.
879 @param[in] HttpIo The HttpIo wrapping the HTTP service.
880 @param[in] RecvMsgHeader TRUE to receive a new HTTP response (from message header).
881 FALSE to continue receive the previous response message.
882 @param[out] ResponseData Point to a wrapper of the received response data.
884 @retval EFI_SUCCESS The HTTP response is received.
885 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
886 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory.
887 @retval EFI_DEVICE_ERROR An unexpected network or system error occurred.
888 @retval Others Other errors as indicated.
894 IN BOOLEAN RecvMsgHeader
,
895 OUT HTTP_IO_RESPONSE_DATA
*ResponseData
899 EFI_HTTP_PROTOCOL
*Http
;
901 if (HttpIo
== NULL
|| HttpIo
->Http
== NULL
|| ResponseData
== NULL
) {
902 return EFI_INVALID_PARAMETER
;
906 // Queue the response token to HTTP instances.
908 HttpIo
->RspToken
.Status
= EFI_NOT_READY
;
910 HttpIo
->RspToken
.Message
->Data
.Response
= &ResponseData
->Response
;
912 HttpIo
->RspToken
.Message
->Data
.Response
= NULL
;
914 HttpIo
->RspToken
.Message
->HeaderCount
= 0;
915 HttpIo
->RspToken
.Message
->Headers
= NULL
;
916 HttpIo
->RspToken
.Message
->BodyLength
= ResponseData
->BodyLength
;
917 HttpIo
->RspToken
.Message
->Body
= ResponseData
->Body
;
920 HttpIo
->IsRxDone
= FALSE
;
921 Status
= Http
->Response (
926 if (EFI_ERROR (Status
)) {
931 // Poll the network until receive finish.
933 while (!HttpIo
->IsRxDone
) {
938 // Store the received data into the wrapper.
940 ResponseData
->Status
= HttpIo
->RspToken
.Status
;
941 ResponseData
->HeaderCount
= HttpIo
->RspToken
.Message
->HeaderCount
;
942 ResponseData
->Headers
= HttpIo
->RspToken
.Message
->Headers
;
943 ResponseData
->BodyLength
= HttpIo
->RspToken
.Message
->BodyLength
;
949 Get the URI address string from the input device path.
951 Caller need to free the buffer in the UriAddress pointer.
953 @param[in] FilePath Pointer to the device path which contains a URI device path node.
954 @param[out] UriAddress The URI address string extract from the device path.
956 @retval EFI_SUCCESS The URI string is returned.
957 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory.
961 HttpBootParseFilePath (
962 IN EFI_DEVICE_PATH_PROTOCOL
*FilePath
,
963 OUT CHAR8
**UriAddress
966 EFI_DEVICE_PATH_PROTOCOL
*TempDevicePath
;
967 URI_DEVICE_PATH
*UriDevicePath
;
971 if (FilePath
== NULL
) {
972 return EFI_INVALID_PARAMETER
;
978 // Extract the URI address from the FilePath
980 TempDevicePath
= FilePath
;
981 while (!IsDevicePathEnd (TempDevicePath
)) {
982 if ((DevicePathType (TempDevicePath
) == MESSAGING_DEVICE_PATH
) &&
983 (DevicePathSubType (TempDevicePath
) == MSG_URI_DP
)) {
984 UriDevicePath
= (URI_DEVICE_PATH
*) TempDevicePath
;
986 // UEFI Spec doesn't require the URI to be a NULL-terminated string
987 // So we allocate a new buffer and always append a '\0' to it.
989 UriStrLength
= DevicePathNodeLength (UriDevicePath
) - sizeof(EFI_DEVICE_PATH_PROTOCOL
);
990 if (UriStrLength
== 0) {
992 // return a NULL UriAddress if it's a empty URI device path node.
996 Uri
= AllocatePool (UriStrLength
+ 1);
998 return EFI_OUT_OF_RESOURCES
;
1000 CopyMem (Uri
, UriDevicePath
->Uri
, DevicePathNodeLength (UriDevicePath
) - sizeof(EFI_DEVICE_PATH_PROTOCOL
));
1001 Uri
[DevicePathNodeLength (UriDevicePath
) - sizeof(EFI_DEVICE_PATH_PROTOCOL
)] = '\0';
1005 TempDevicePath
= NextDevicePathNode (TempDevicePath
);
1012 This function returns the image type according to server replied HTTP message
1013 and also the image's URI info.
1015 @param[in] Uri The pointer to the image's URI string.
1016 @param[in] UriParser URI Parse result returned by NetHttpParseUrl().
1017 @param[in] HeaderCount Number of HTTP header structures in Headers list.
1018 @param[in] Headers Array containing list of HTTP headers.
1019 @param[out] ImageType The image type of the downloaded file.
1021 @retval EFI_SUCCESS The image type is returned in ImageType.
1022 @retval EFI_INVALID_PARAMETER ImageType, Uri or UriParser is NULL.
1023 @retval EFI_INVALID_PARAMETER HeaderCount is not zero, and Headers is NULL.
1024 @retval EFI_NOT_FOUND Failed to identify the image type.
1025 @retval Others Unexpect error happened.
1029 HttpBootCheckImageType (
1032 IN UINTN HeaderCount
,
1033 IN EFI_HTTP_HEADER
*Headers
,
1034 OUT HTTP_BOOT_IMAGE_TYPE
*ImageType
1038 EFI_HTTP_HEADER
*Header
;
1042 if (Uri
== NULL
|| UriParser
== NULL
|| ImageType
== NULL
) {
1043 return EFI_INVALID_PARAMETER
;
1046 if (HeaderCount
!= 0 && Headers
== NULL
) {
1047 return EFI_INVALID_PARAMETER
;
1051 // Determine the image type by the HTTP Content-Type header field first.
1052 // "application/efi" -> EFI Image
1054 Header
= HttpFindHeader (HeaderCount
, Headers
, HTTP_HEADER_CONTENT_TYPE
);
1055 if (Header
!= NULL
) {
1056 if (AsciiStriCmp (Header
->FieldValue
, HTTP_CONTENT_TYPE_APP_EFI
) == 0) {
1057 *ImageType
= ImageTypeEfi
;
1063 // Determine the image type by file extension:
1064 // *.efi -> EFI Image
1065 // *.iso -> CD/DVD Image
1066 // *.img -> Virtual Disk Image
1068 Status
= HttpUrlGetPath (
1073 if (EFI_ERROR (Status
)) {
1077 FilePost
= FilePath
+ AsciiStrLen (FilePath
) - 4;
1078 if (AsciiStrCmp (FilePost
, ".efi") == 0) {
1079 *ImageType
= ImageTypeEfi
;
1080 } else if (AsciiStrCmp (FilePost
, ".iso") == 0) {
1081 *ImageType
= ImageTypeVirtualCd
;
1082 } else if (AsciiStrCmp (FilePost
, ".img") == 0) {
1083 *ImageType
= ImageTypeVirtualDisk
;
1085 *ImageType
= ImageTypeMax
;
1088 FreePool (FilePath
);
1090 return (*ImageType
< ImageTypeMax
) ? EFI_SUCCESS
: EFI_NOT_FOUND
;
1094 This function register the RAM disk info to the system.
1096 @param[in] Private The pointer to the driver's private data.
1097 @param[in] BufferSize The size of Buffer in bytes.
1098 @param[in] Buffer The base address of the RAM disk.
1099 @param[in] ImageType The image type of the file in Buffer.
1101 @retval EFI_SUCCESS The RAM disk has been registered.
1102 @retval EFI_NOT_FOUND No RAM disk protocol instances were found.
1103 @retval EFI_UNSUPPORTED The ImageType is not supported.
1104 @retval Others Unexpected error happened.
1108 HttpBootRegisterRamDisk (
1109 IN HTTP_BOOT_PRIVATE_DATA
*Private
,
1110 IN UINTN BufferSize
,
1112 IN HTTP_BOOT_IMAGE_TYPE ImageType
1115 EFI_RAM_DISK_PROTOCOL
*RamDisk
;
1117 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
1118 EFI_GUID
*RamDiskType
;
1120 ASSERT (Private
!= NULL
);
1121 ASSERT (Buffer
!= NULL
);
1122 ASSERT (BufferSize
!= 0);
1124 Status
= gBS
->LocateProtocol (&gEfiRamDiskProtocolGuid
, NULL
, (VOID
**) &RamDisk
);
1125 if (EFI_ERROR (Status
)) {
1126 DEBUG ((EFI_D_ERROR
, "HTTP Boot: Couldn't find the RAM Disk protocol - %r\n", Status
));
1130 if (ImageType
== ImageTypeVirtualCd
) {
1131 RamDiskType
= &gEfiVirtualCdGuid
;
1132 } else if (ImageType
== ImageTypeVirtualDisk
) {
1133 RamDiskType
= &gEfiVirtualDiskGuid
;
1135 return EFI_UNSUPPORTED
;
1138 Status
= RamDisk
->Register (
1142 Private
->UsingIpv6
? Private
->Ip6Nic
->DevicePath
: Private
->Ip4Nic
->DevicePath
,
1145 if (EFI_ERROR (Status
)) {
1146 DEBUG ((EFI_D_ERROR
, "HTTP Boot: Failed to register RAM Disk - %r\n", Status
));