2 Implementation of the boot file download function.
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"
19 Update the IP and URL device path node to include the boot resource information.
21 @param[in] Private The pointer to the driver's private data.
23 @retval EFI_SUCCESS Device patch successfully updated.
24 @retval EFI_OUT_OF_RESOURCES Could not allocate needed resources.
25 @retval Others Unexpected error happened.
29 HttpBootUpdateDevicePath (
30 IN HTTP_BOOT_PRIVATE_DATA
*Private
34 EFI_DEVICE_PATH_PROTOCOL
*TmpDevicePath
;
35 EFI_DEVICE_PATH_PROTOCOL
*NewDevicePath
;
42 // Update the IP node with DHCP assigned information.
44 if (!Private
->UsingIpv6
) {
45 Node
= AllocateZeroPool (sizeof (IPv4_DEVICE_PATH
));
47 return EFI_OUT_OF_RESOURCES
;
49 Node
->Ipv4
.Header
.Type
= MESSAGING_DEVICE_PATH
;
50 Node
->Ipv4
.Header
.SubType
= MSG_IPv4_DP
;
51 SetDevicePathNodeLength (Node
, sizeof (IPv4_DEVICE_PATH
));
52 CopyMem (&Node
->Ipv4
.LocalIpAddress
, &Private
->StationIp
, sizeof (EFI_IPv4_ADDRESS
));
53 Node
->Ipv4
.RemotePort
= Private
->Port
;
54 Node
->Ipv4
.Protocol
= EFI_IP_PROTO_TCP
;
55 Node
->Ipv4
.StaticIpAddress
= FALSE
;
56 CopyMem (&Node
->Ipv4
.GatewayIpAddress
, &Private
->GatewayIp
, sizeof (EFI_IPv4_ADDRESS
));
57 CopyMem (&Node
->Ipv4
.SubnetMask
, &Private
->SubnetMask
, sizeof (EFI_IPv4_ADDRESS
));
59 Node
= AllocateZeroPool (sizeof (IPv6_DEVICE_PATH
));
61 return EFI_OUT_OF_RESOURCES
;
63 Node
->Ipv6
.Header
.Type
= MESSAGING_DEVICE_PATH
;
64 Node
->Ipv6
.Header
.SubType
= MSG_IPv6_DP
;
65 SetDevicePathNodeLength (Node
, sizeof (IPv6_DEVICE_PATH
));
66 Node
->Ipv6
.PrefixLength
= IP6_PREFIX_LENGTH
;
67 Node
->Ipv6
.RemotePort
= Private
->Port
;
68 Node
->Ipv6
.Protocol
= EFI_IP_PROTO_TCP
;
69 Node
->Ipv6
.IpAddressOrigin
= 0;
70 CopyMem (&Node
->Ipv6
.LocalIpAddress
, &Private
->StationIp
.v6
, sizeof (EFI_IPv6_ADDRESS
));
71 CopyMem (&Node
->Ipv6
.RemoteIpAddress
, &Private
->ServerIp
.v6
, sizeof (EFI_IPv6_ADDRESS
));
72 CopyMem (&Node
->Ipv6
.GatewayIpAddress
, &Private
->GatewayIp
.v6
, sizeof (EFI_IPv6_ADDRESS
));
75 TmpDevicePath
= AppendDevicePathNode (Private
->ParentDevicePath
, (EFI_DEVICE_PATH_PROTOCOL
*) Node
);
77 if (TmpDevicePath
== NULL
) {
78 return EFI_OUT_OF_RESOURCES
;
82 // Update the URI node with the boot file URI.
84 Length
= sizeof (EFI_DEVICE_PATH_PROTOCOL
) + AsciiStrSize (Private
->BootFileUri
);
85 Node
= AllocatePool (Length
);
87 FreePool (TmpDevicePath
);
88 return EFI_OUT_OF_RESOURCES
;
90 Node
->DevPath
.Type
= MESSAGING_DEVICE_PATH
;
91 Node
->DevPath
.SubType
= MSG_URI_DP
;
92 SetDevicePathNodeLength (Node
, Length
);
93 CopyMem ((UINT8
*) Node
+ sizeof (EFI_DEVICE_PATH_PROTOCOL
), Private
->BootFileUri
, AsciiStrSize (Private
->BootFileUri
));
95 NewDevicePath
= AppendDevicePathNode (TmpDevicePath
, (EFI_DEVICE_PATH_PROTOCOL
*) Node
);
97 FreePool (TmpDevicePath
);
98 if (NewDevicePath
== NULL
) {
99 return EFI_OUT_OF_RESOURCES
;
102 if (!Private
->UsingIpv6
) {
104 // Reinstall the device path protocol of the child handle.
106 Status
= gBS
->ReinstallProtocolInterface (
107 Private
->Ip4Nic
->Controller
,
108 &gEfiDevicePathProtocolGuid
,
109 Private
->Ip4Nic
->DevicePath
,
112 if (EFI_ERROR (Status
)) {
116 FreePool (Private
->Ip4Nic
->DevicePath
);
117 Private
->Ip4Nic
->DevicePath
= NewDevicePath
;
120 // Reinstall the device path protocol of the child handle.
122 Status
= gBS
->ReinstallProtocolInterface (
123 Private
->Ip6Nic
->Controller
,
124 &gEfiDevicePathProtocolGuid
,
125 Private
->Ip6Nic
->DevicePath
,
128 if (EFI_ERROR (Status
)) {
131 FreePool (Private
->Ip6Nic
->DevicePath
);
132 Private
->Ip6Nic
->DevicePath
= NewDevicePath
;
139 Parse the boot file URI information from the selected Dhcp4 offer packet.
141 @param[in] Private The pointer to the driver's private data.
143 @retval EFI_SUCCESS Successfully parsed out all the boot information.
144 @retval Others Failed to parse out the boot information.
148 HttpBootDhcp4ExtractUriInfo (
149 IN HTTP_BOOT_PRIVATE_DATA
*Private
152 HTTP_BOOT_DHCP4_PACKET_CACHE
*SelectOffer
;
153 HTTP_BOOT_DHCP4_PACKET_CACHE
*HttpOffer
;
156 EFI_DHCP4_PACKET_OPTION
*Option
;
159 ASSERT (Private
!= NULL
);
160 ASSERT (Private
->SelectIndex
!= 0);
161 SelectIndex
= Private
->SelectIndex
- 1;
162 ASSERT (SelectIndex
< HTTP_BOOT_OFFER_MAX_NUM
);
164 Status
= EFI_SUCCESS
;
167 // SelectOffer contains the IP address configuration and name server configuration.
168 // HttpOffer contains the boot file URL.
170 SelectOffer
= &Private
->OfferBuffer
[SelectIndex
].Dhcp4
;
171 if (Private
->FilePathUri
== NULL
) {
173 // In Corporate environment, we need a HttpOffer.
175 if ((SelectOffer
->OfferType
== HttpOfferTypeDhcpIpUri
) ||
176 (SelectOffer
->OfferType
== HttpOfferTypeDhcpIpUriDns
) ||
177 (SelectOffer
->OfferType
== HttpOfferTypeDhcpNameUriDns
)) {
178 HttpOffer
= SelectOffer
;
180 ASSERT (Private
->SelectProxyType
!= HttpOfferTypeMax
);
181 ProxyIndex
= Private
->OfferIndex
[Private
->SelectProxyType
][0];
182 HttpOffer
= &Private
->OfferBuffer
[ProxyIndex
].Dhcp4
;
184 Private
->BootFileUriParser
= HttpOffer
->UriParser
;
185 Private
->BootFileUri
= (CHAR8
*) HttpOffer
->OptList
[HTTP_BOOT_DHCP4_TAG_INDEX_BOOTFILE
]->Data
;
188 // In Home environment the BootFileUri comes from the FilePath.
190 Private
->BootFileUriParser
= Private
->FilePathUriParser
;
191 Private
->BootFileUri
= Private
->FilePathUri
;
195 // Check the URI scheme.
197 Status
= HttpBootCheckUriScheme (Private
->BootFileUri
);
198 if (EFI_ERROR (Status
)) {
199 DEBUG ((EFI_D_ERROR
, "HttpBootDhcp4ExtractUriInfo: %r.\n", Status
));
204 // Configure the default DNS server if server assigned.
206 if ((SelectOffer
->OfferType
== HttpOfferTypeDhcpNameUriDns
) ||
207 (SelectOffer
->OfferType
== HttpOfferTypeDhcpDns
) ||
208 (SelectOffer
->OfferType
== HttpOfferTypeDhcpIpUriDns
)) {
209 Option
= SelectOffer
->OptList
[HTTP_BOOT_DHCP4_TAG_INDEX_DNS_SERVER
];
210 ASSERT (Option
!= NULL
);
211 Status
= HttpBootRegisterIp4Dns (
216 if (EFI_ERROR (Status
)) {
222 // Extract the port from URL, and use default HTTP port 80 if not provided.
224 Status
= HttpUrlGetPort (
225 Private
->BootFileUri
,
226 Private
->BootFileUriParser
,
229 if (EFI_ERROR (Status
) || Private
->Port
== 0) {
234 // All boot informations are valid here.
238 // Update the device path to include the IP and boot URI information.
240 Status
= HttpBootUpdateDevicePath (Private
);
246 Parse the boot file URI information from the selected Dhcp6 offer packet.
248 @param[in] Private The pointer to the driver's private data.
250 @retval EFI_SUCCESS Successfully parsed out all the boot information.
251 @retval Others Failed to parse out the boot information.
255 HttpBootDhcp6ExtractUriInfo (
256 IN HTTP_BOOT_PRIVATE_DATA
*Private
259 HTTP_BOOT_DHCP6_PACKET_CACHE
*SelectOffer
;
260 HTTP_BOOT_DHCP6_PACKET_CACHE
*HttpOffer
;
263 EFI_DHCP6_PACKET_OPTION
*Option
;
264 EFI_IPv6_ADDRESS IpAddr
;
270 ASSERT (Private
!= NULL
);
271 ASSERT (Private
->SelectIndex
!= 0);
272 SelectIndex
= Private
->SelectIndex
- 1;
273 ASSERT (SelectIndex
< HTTP_BOOT_OFFER_MAX_NUM
);
275 Status
= EFI_SUCCESS
;
278 // SelectOffer contains the IP address configuration and name server configuration.
279 // HttpOffer contains the boot file URL.
281 SelectOffer
= &Private
->OfferBuffer
[SelectIndex
].Dhcp6
;
282 if (Private
->FilePathUri
== NULL
) {
284 // In Corporate environment, we need a HttpOffer.
286 if ((SelectOffer
->OfferType
== HttpOfferTypeDhcpIpUri
) ||
287 (SelectOffer
->OfferType
== HttpOfferTypeDhcpIpUriDns
) ||
288 (SelectOffer
->OfferType
== HttpOfferTypeDhcpNameUriDns
)) {
289 HttpOffer
= SelectOffer
;
291 ASSERT (Private
->SelectProxyType
!= HttpOfferTypeMax
);
292 ProxyIndex
= Private
->OfferIndex
[Private
->SelectProxyType
][0];
293 HttpOffer
= &Private
->OfferBuffer
[ProxyIndex
].Dhcp6
;
295 Private
->BootFileUriParser
= HttpOffer
->UriParser
;
296 Private
->BootFileUri
= (CHAR8
*) HttpOffer
->OptList
[HTTP_BOOT_DHCP6_IDX_BOOT_FILE_URL
]->Data
;
299 // In Home environment the BootFileUri comes from the FilePath.
301 Private
->BootFileUriParser
= Private
->FilePathUriParser
;
302 Private
->BootFileUri
= Private
->FilePathUri
;
306 // Check the URI scheme.
308 Status
= HttpBootCheckUriScheme (Private
->BootFileUri
);
309 if (EFI_ERROR (Status
)) {
310 DEBUG ((EFI_D_ERROR
, "HttpBootDhcp6ExtractUriInfo: %r.\n", Status
));
315 // Set the Local station address to IP layer.
317 Status
= HttpBootSetIp6Address (Private
);
318 if (EFI_ERROR (Status
)) {
323 // Register the IPv6 gateway address to the network device.
325 Status
= HttpBootSetIp6Gateway (Private
);
326 if (EFI_ERROR (Status
)) {
331 // Configure the default DNS server if server assigned.
333 if ((SelectOffer
->OfferType
== HttpOfferTypeDhcpNameUriDns
) ||
334 (SelectOffer
->OfferType
== HttpOfferTypeDhcpDns
) ||
335 (SelectOffer
->OfferType
== HttpOfferTypeDhcpIpUriDns
)) {
336 Option
= SelectOffer
->OptList
[HTTP_BOOT_DHCP6_IDX_DNS_SERVER
];
337 ASSERT (Option
!= NULL
);
338 Status
= HttpBootSetIp6Dns (
340 HTONS (Option
->OpLen
),
343 if (EFI_ERROR (Status
)) {
349 // Extract the HTTP server Ip frome URL. This is used to Check route table
350 // whether can send message to HTTP Server Ip through the GateWay.
352 Status
= HttpUrlGetIp6 (
353 Private
->BootFileUri
,
354 Private
->BootFileUriParser
,
358 if (EFI_ERROR (Status
)) {
360 // The Http server address is expressed by Name Ip, so perform DNS resolution
362 Status
= HttpUrlGetHostName (
363 Private
->BootFileUri
,
364 Private
->BootFileUriParser
,
367 if (EFI_ERROR (Status
)) {
371 HostNameSize
= AsciiStrSize (HostName
);
372 HostNameStr
= AllocateZeroPool (HostNameSize
* sizeof (CHAR16
));
373 if (HostNameStr
== NULL
) {
374 Status
= EFI_OUT_OF_RESOURCES
;
378 AsciiStrToUnicodeStrS (HostName
, HostNameStr
, HostNameSize
);
379 Status
= HttpBootDns (Private
, HostNameStr
, &IpAddr
);
380 FreePool (HostNameStr
);
381 if (EFI_ERROR (Status
)) {
386 CopyMem (&Private
->ServerIp
.v6
, &IpAddr
, sizeof (EFI_IPv6_ADDRESS
));
389 // Extract the port from URL, and use default HTTP port 80 if not provided.
391 Status
= HttpUrlGetPort (
392 Private
->BootFileUri
,
393 Private
->BootFileUriParser
,
396 if (EFI_ERROR (Status
) || Private
->Port
== 0) {
401 // All boot informations are valid here.
405 // Update the device path to include the IP and boot URI information.
407 Status
= HttpBootUpdateDevicePath (Private
);
411 if (HostName
!= NULL
) {
420 Discover all the boot information for boot file.
422 @param[in, out] Private The pointer to the driver's private data.
424 @retval EFI_SUCCESS Successfully obtained all the boot information .
425 @retval Others Failed to retrieve the boot information.
429 HttpBootDiscoverBootInfo (
430 IN OUT HTTP_BOOT_PRIVATE_DATA
*Private
436 // Start D.O.R.A/S.A.R.R exchange to acquire station ip address and
437 // other Http boot information.
439 Status
= HttpBootDhcp (Private
);
440 if (EFI_ERROR (Status
)) {
444 if (!Private
->UsingIpv6
) {
445 Status
= HttpBootDhcp4ExtractUriInfo (Private
);
447 Status
= HttpBootDhcp6ExtractUriInfo (Private
);
454 HttpIo Callback function which will be invoked when specified HTTP_IO_CALLBACK_EVENT happened.
456 @param[in] EventType Indicate the Event type that occurs in the current callback.
457 @param[in] Message HTTP message which will be send to, or just received from HTTP server.
458 @param[in] Context The Callback Context pointer.
460 @retval EFI_SUCCESS Tells the HttpIo to continue the HTTP process.
461 @retval Others Tells the HttpIo to abort the current HTTP process.
465 HttpBootHttpIoCallback (
466 IN HTTP_IO_CALLBACK_EVENT EventType
,
467 IN EFI_HTTP_MESSAGE
*Message
,
471 HTTP_BOOT_PRIVATE_DATA
*Private
;
473 Private
= (HTTP_BOOT_PRIVATE_DATA
*) Context
;
474 if (Private
->HttpBootCallback
!= NULL
) {
475 Status
= Private
->HttpBootCallback
->Callback (
476 Private
->HttpBootCallback
,
477 EventType
== HttpIoRequest
? HttpBootHttpRequest
: HttpBootHttpResponse
,
478 EventType
== HttpIoRequest
? FALSE
: TRUE
,
479 sizeof (EFI_HTTP_MESSAGE
),
488 Create a HttpIo instance for the file download.
490 @param[in] Private The pointer to the driver's private data.
492 @retval EFI_SUCCESS Successfully created.
493 @retval Others Failed to create HttpIo.
497 HttpBootCreateHttpIo (
498 IN HTTP_BOOT_PRIVATE_DATA
*Private
501 HTTP_IO_CONFIG_DATA ConfigData
;
503 EFI_HANDLE ImageHandle
;
505 ASSERT (Private
!= NULL
);
507 ZeroMem (&ConfigData
, sizeof (HTTP_IO_CONFIG_DATA
));
508 if (!Private
->UsingIpv6
) {
509 ConfigData
.Config4
.HttpVersion
= HttpVersion11
;
510 ConfigData
.Config4
.RequestTimeOut
= HTTP_BOOT_REQUEST_TIMEOUT
;
511 IP4_COPY_ADDRESS (&ConfigData
.Config4
.LocalIp
, &Private
->StationIp
.v4
);
512 IP4_COPY_ADDRESS (&ConfigData
.Config4
.SubnetMask
, &Private
->SubnetMask
.v4
);
513 ImageHandle
= Private
->Ip4Nic
->ImageHandle
;
515 ConfigData
.Config6
.HttpVersion
= HttpVersion11
;
516 ConfigData
.Config6
.RequestTimeOut
= HTTP_BOOT_REQUEST_TIMEOUT
;
517 IP6_COPY_ADDRESS (&ConfigData
.Config6
.LocalIp
, &Private
->StationIp
.v6
);
518 ImageHandle
= Private
->Ip6Nic
->ImageHandle
;
521 Status
= HttpIoCreateIo (
524 Private
->UsingIpv6
? IP_VERSION_6
: IP_VERSION_4
,
526 HttpBootHttpIoCallback
,
530 if (EFI_ERROR (Status
)) {
534 Private
->HttpCreated
= TRUE
;
539 Release all the resource of a cache item.
541 @param[in] Cache The pointer to the cache item.
546 IN HTTP_BOOT_CACHE_CONTENT
*Cache
551 LIST_ENTRY
*NextEntry
;
552 HTTP_BOOT_ENTITY_DATA
*EntityData
;
556 // Free the request data
558 if (Cache
->RequestData
!= NULL
) {
559 if (Cache
->RequestData
->Url
!= NULL
) {
560 FreePool (Cache
->RequestData
->Url
);
562 FreePool (Cache
->RequestData
);
566 // Free the response header
568 if (Cache
->ResponseData
!= NULL
) {
569 if (Cache
->ResponseData
->Headers
!= NULL
) {
570 for (Index
= 0; Index
< Cache
->ResponseData
->HeaderCount
; Index
++) {
571 FreePool (Cache
->ResponseData
->Headers
[Index
].FieldName
);
572 FreePool (Cache
->ResponseData
->Headers
[Index
].FieldValue
);
574 FreePool (Cache
->ResponseData
->Headers
);
579 // Free the response body
581 NET_LIST_FOR_EACH_SAFE (Entry
, NextEntry
, &Cache
->EntityDataList
) {
582 EntityData
= NET_LIST_USER_STRUCT (Entry
, HTTP_BOOT_ENTITY_DATA
, Link
);
583 if (EntityData
->Block
!= NULL
) {
584 FreePool (EntityData
->Block
);
586 RemoveEntryList (&EntityData
->Link
);
587 FreePool (EntityData
);
595 Clean up all cached data.
597 @param[in] Private The pointer to the driver's private data.
601 HttpBootFreeCacheList (
602 IN HTTP_BOOT_PRIVATE_DATA
*Private
606 LIST_ENTRY
*NextEntry
;
607 HTTP_BOOT_CACHE_CONTENT
*Cache
;
609 NET_LIST_FOR_EACH_SAFE (Entry
, NextEntry
, &Private
->CacheList
) {
610 Cache
= NET_LIST_USER_STRUCT (Entry
, HTTP_BOOT_CACHE_CONTENT
, Link
);
611 RemoveEntryList (&Cache
->Link
);
612 HttpBootFreeCache (Cache
);
617 Get the file content from cached data.
619 @param[in] Private The pointer to the driver's private data.
620 @param[in] Uri Uri of the file to be retrieved from cache.
621 @param[in, out] BufferSize On input the size of Buffer in bytes. On output with a return
622 code of EFI_SUCCESS, the amount of data transferred to
623 Buffer. On output with a return code of EFI_BUFFER_TOO_SMALL,
624 the size of Buffer required to retrieve the requested file.
625 @param[out] Buffer The memory buffer to transfer the file to. IF Buffer is NULL,
626 then the size of the requested file is returned in
628 @param[out] ImageType The image type of the downloaded file.
630 @retval EFI_SUCCESS Successfully created.
631 @retval Others Failed to create HttpIo.
635 HttpBootGetFileFromCache (
636 IN HTTP_BOOT_PRIVATE_DATA
*Private
,
638 IN OUT UINTN
*BufferSize
,
640 OUT HTTP_BOOT_IMAGE_TYPE
*ImageType
645 HTTP_BOOT_CACHE_CONTENT
*Cache
;
646 HTTP_BOOT_ENTITY_DATA
*EntityData
;
649 if (Uri
== NULL
|| BufferSize
== 0 || Buffer
== NULL
|| ImageType
== NULL
) {
650 return EFI_INVALID_PARAMETER
;
653 NET_LIST_FOR_EACH (Entry
, &Private
->CacheList
) {
654 Cache
= NET_LIST_USER_STRUCT (Entry
, HTTP_BOOT_CACHE_CONTENT
, Link
);
656 // Compare the URI to see whether we already have a cache for this file.
658 if ((Cache
->RequestData
!= NULL
) &&
659 (Cache
->RequestData
->Url
!= NULL
) &&
660 (StrCmp (Uri
, Cache
->RequestData
->Url
) == 0))
663 // Hit in cache, record image type.
665 *ImageType
= Cache
->ImageType
;
668 // Check buffer size.
670 if (*BufferSize
< Cache
->EntityLength
) {
671 *BufferSize
= Cache
->EntityLength
;
672 return EFI_BUFFER_TOO_SMALL
;
676 // Fill data to buffer.
679 NET_LIST_FOR_EACH (Entry2
, &Cache
->EntityDataList
) {
680 EntityData
= NET_LIST_USER_STRUCT (Entry2
, HTTP_BOOT_ENTITY_DATA
, Link
);
681 if (*BufferSize
> CopyedSize
) {
684 EntityData
->DataStart
,
685 MIN (EntityData
->DataLength
, *BufferSize
- CopyedSize
)
687 CopyedSize
+= MIN (EntityData
->DataLength
, *BufferSize
- CopyedSize
);
690 *BufferSize
= CopyedSize
;
695 return EFI_NOT_FOUND
;
699 A callback function to intercept events during message parser.
701 This function will be invoked during HttpParseMessageBody() with various events type. An error
702 return status of the callback function will cause the HttpParseMessageBody() aborted.
704 @param[in] EventType Event type of this callback call.
705 @param[in] Data A pointer to data buffer.
706 @param[in] Length Length in bytes of the Data.
707 @param[in] Context Callback context set by HttpInitMsgParser().
709 @retval EFI_SUCCESS Continue to parser the message body.
710 @retval Others Abort the parse.
715 HttpBootGetBootFileCallback (
716 IN HTTP_BODY_PARSE_EVENT EventType
,
722 HTTP_BOOT_CALLBACK_DATA
*CallbackData
;
723 HTTP_BOOT_ENTITY_DATA
*NewEntityData
;
725 EFI_HTTP_BOOT_CALLBACK_PROTOCOL
*HttpBootCallback
;
728 // We only care about the entity data.
730 if (EventType
!= BodyParseEventOnData
) {
734 CallbackData
= (HTTP_BOOT_CALLBACK_DATA
*) Context
;
735 HttpBootCallback
= CallbackData
->Private
->HttpBootCallback
;
736 if (HttpBootCallback
!= NULL
) {
737 Status
= HttpBootCallback
->Callback (
739 HttpBootHttpEntityBody
,
744 if (EFI_ERROR (Status
)) {
749 // Copy data if caller has provided a buffer.
751 if (CallbackData
->BufferSize
> CallbackData
->CopyedSize
) {
753 CallbackData
->Buffer
+ CallbackData
->CopyedSize
,
755 MIN (Length
, CallbackData
->BufferSize
- CallbackData
->CopyedSize
)
757 CallbackData
->CopyedSize
+= MIN (Length
, CallbackData
->BufferSize
- CallbackData
->CopyedSize
);
761 // The caller doesn't provide a buffer, save the block into cache list.
763 if (CallbackData
->Cache
!= NULL
) {
764 NewEntityData
= AllocatePool (sizeof (HTTP_BOOT_ENTITY_DATA
));
765 if (NewEntityData
== NULL
) {
766 return EFI_OUT_OF_RESOURCES
;
768 if (CallbackData
->NewBlock
) {
769 NewEntityData
->Block
= CallbackData
->Block
;
770 CallbackData
->Block
= NULL
;
772 NewEntityData
->DataLength
= Length
;
773 NewEntityData
->DataStart
= (UINT8
*) Data
;
774 InsertTailList (&CallbackData
->Cache
->EntityDataList
, &NewEntityData
->Link
);
780 This function download the boot file by using UEFI HTTP protocol.
782 @param[in] Private The pointer to the driver's private data.
783 @param[in] HeaderOnly Only request the response header, it could save a lot of time if
784 the caller only want to know the size of the requested file.
785 @param[in, out] BufferSize On input the size of Buffer in bytes. On output with a return
786 code of EFI_SUCCESS, the amount of data transferred to
787 Buffer. On output with a return code of EFI_BUFFER_TOO_SMALL,
788 the size of Buffer required to retrieve the requested file.
789 @param[out] Buffer The memory buffer to transfer the file to. IF Buffer is NULL,
790 then the size of the requested file is returned in
792 @param[out] ImageType The image type of the downloaded file.
794 @retval EFI_SUCCESS The file was loaded.
795 @retval EFI_INVALID_PARAMETER BufferSize is NULL or Buffer Size is not NULL but Buffer is NULL.
796 @retval EFI_OUT_OF_RESOURCES Could not allocate needed resources
797 @retval EFI_BUFFER_TOO_SMALL The BufferSize is too small to read the current directory entry.
798 BufferSize has been updated with the size needed to complete
800 @retval Others Unexpected error happened.
804 HttpBootGetBootFile (
805 IN HTTP_BOOT_PRIVATE_DATA
*Private
,
806 IN BOOLEAN HeaderOnly
,
807 IN OUT UINTN
*BufferSize
,
809 OUT HTTP_BOOT_IMAGE_TYPE
*ImageType
813 EFI_HTTP_STATUS_CODE StatusCode
;
815 EFI_HTTP_REQUEST_DATA
*RequestData
;
816 HTTP_IO_RESPONSE_DATA
*ResponseData
;
817 HTTP_IO_RESPONSE_DATA ResponseBody
;
819 HTTP_IO_HEADER
*HttpIoHeader
;
821 HTTP_BOOT_CALLBACK_DATA Context
;
823 HTTP_BOOT_CACHE_CONTENT
*Cache
;
827 BOOLEAN IdentityMode
;
830 ASSERT (Private
!= NULL
);
831 ASSERT (Private
->HttpCreated
);
833 if (BufferSize
== NULL
|| ImageType
== NULL
) {
834 return EFI_INVALID_PARAMETER
;
837 if (*BufferSize
!= 0 && Buffer
== NULL
) {
838 return EFI_INVALID_PARAMETER
;
842 // First, check whether we already cached the requested Uri.
844 UrlSize
= AsciiStrSize (Private
->BootFileUri
);
845 Url
= AllocatePool (UrlSize
* sizeof (CHAR16
));
847 return EFI_OUT_OF_RESOURCES
;
849 AsciiStrToUnicodeStrS (Private
->BootFileUri
, Url
, UrlSize
);
851 Status
= HttpBootGetFileFromCache (Private
, Url
, BufferSize
, Buffer
, ImageType
);
852 if (Status
!= EFI_NOT_FOUND
) {
859 // Not found in cache, try to download it through HTTP.
863 // 1. Create a temp cache item for the requested URI if caller doesn't provide buffer.
866 if ((!HeaderOnly
) && (*BufferSize
== 0)) {
867 Cache
= AllocateZeroPool (sizeof (HTTP_BOOT_CACHE_CONTENT
));
869 Status
= EFI_OUT_OF_RESOURCES
;
872 Cache
->ImageType
= ImageTypeMax
;
873 InitializeListHead (&Cache
->EntityDataList
);
877 // 2. Send HTTP request message.
881 // 2.1 Build HTTP header for the request, 3 header is needed to download a boot file:
886 HttpIoHeader
= HttpBootCreateHeader (3);
887 if (HttpIoHeader
== NULL
) {
888 Status
= EFI_OUT_OF_RESOURCES
;
893 // Add HTTP header field 1: Host
896 Status
= HttpUrlGetHostName (
897 Private
->BootFileUri
,
898 Private
->BootFileUriParser
,
901 if (EFI_ERROR (Status
)) {
904 Status
= HttpBootSetHeader (
910 if (EFI_ERROR (Status
)) {
915 // Add HTTP header field 2: Accept
917 Status
= HttpBootSetHeader (
922 if (EFI_ERROR (Status
)) {
927 // Add HTTP header field 3: User-Agent
929 Status
= HttpBootSetHeader (
931 HTTP_HEADER_USER_AGENT
,
932 HTTP_USER_AGENT_EFI_HTTP_BOOT
934 if (EFI_ERROR (Status
)) {
939 // 2.2 Build the rest of HTTP request info.
941 RequestData
= AllocatePool (sizeof (EFI_HTTP_REQUEST_DATA
));
942 if (RequestData
== NULL
) {
943 Status
= EFI_OUT_OF_RESOURCES
;
946 RequestData
->Method
= HeaderOnly
? HttpMethodHead
: HttpMethodGet
;
947 RequestData
->Url
= Url
;
950 // 2.3 Record the request info in a temp cache item.
953 Cache
->RequestData
= RequestData
;
957 // 2.4 Send out the request to HTTP server.
959 HttpIo
= &Private
->HttpIo
;
960 Status
= HttpIoSendRequest (
963 HttpIoHeader
->HeaderCount
,
964 HttpIoHeader
->Headers
,
968 if (EFI_ERROR (Status
)) {
973 // 3. Receive HTTP response message.
977 // 3.1 First step, use zero BodyLength to only receive the response headers.
979 ResponseData
= AllocateZeroPool (sizeof(HTTP_IO_RESPONSE_DATA
));
980 if (ResponseData
== NULL
) {
981 Status
= EFI_OUT_OF_RESOURCES
;
984 Status
= HttpIoRecvResponse (
989 if (EFI_ERROR (Status
) || EFI_ERROR (ResponseData
->Status
)) {
990 if (EFI_ERROR (ResponseData
->Status
)) {
991 StatusCode
= HttpIo
->RspToken
.Message
->Data
.Response
->StatusCode
;
992 HttpBootPrintErrorMessage (StatusCode
);
993 Status
= ResponseData
->Status
;
999 // Check the image type according to server's response.
1001 Status
= HttpBootCheckImageType (
1002 Private
->BootFileUri
,
1003 Private
->BootFileUriParser
,
1004 ResponseData
->HeaderCount
,
1005 ResponseData
->Headers
,
1008 if (EFI_ERROR (Status
)) {
1013 // 3.2 Cache the response header.
1015 if (Cache
!= NULL
) {
1016 Cache
->ResponseData
= ResponseData
;
1017 Cache
->ImageType
= *ImageType
;
1021 // 3.3 Init a message-body parser from the header information.
1024 Context
.NewBlock
= FALSE
;
1025 Context
.Block
= NULL
;
1026 Context
.CopyedSize
= 0;
1027 Context
.Buffer
= Buffer
;
1028 Context
.BufferSize
= *BufferSize
;
1029 Context
.Cache
= Cache
;
1030 Context
.Private
= Private
;
1031 Status
= HttpInitMsgParser (
1032 HeaderOnly
? HttpMethodHead
: HttpMethodGet
,
1033 ResponseData
->Response
.StatusCode
,
1034 ResponseData
->HeaderCount
,
1035 ResponseData
->Headers
,
1036 HttpBootGetBootFileCallback
,
1040 if (EFI_ERROR (Status
)) {
1045 // 3.4 Continue to receive and parse message-body if needed.
1050 // 3.4.1, check whether we are in identity transfer-coding.
1053 Status
= HttpGetEntityLength (Parser
, &ContentLength
);
1054 if (!EFI_ERROR (Status
)) {
1055 IdentityMode
= TRUE
;
1057 IdentityMode
= FALSE
;
1061 // 3.4.2, start the message-body download, the identity and chunked transfer-coding
1062 // is handled in different path here.
1064 ZeroMem (&ResponseBody
, sizeof (HTTP_IO_RESPONSE_DATA
));
1067 // In identity transfer-coding there is no need to parse the message body,
1068 // just download the message body to the user provided buffer directly.
1071 while (ReceivedSize
< ContentLength
) {
1072 ResponseBody
.Body
= (CHAR8
*) Buffer
+ ReceivedSize
;
1073 ResponseBody
.BodyLength
= *BufferSize
- ReceivedSize
;
1074 Status
= HttpIoRecvResponse (
1079 if (EFI_ERROR (Status
) || EFI_ERROR (ResponseBody
.Status
)) {
1080 if (EFI_ERROR (ResponseBody
.Status
)) {
1081 Status
= ResponseBody
.Status
;
1085 ReceivedSize
+= ResponseBody
.BodyLength
;
1086 if (Private
->HttpBootCallback
!= NULL
) {
1087 Status
= Private
->HttpBootCallback
->Callback (
1088 Private
->HttpBootCallback
,
1089 HttpBootHttpEntityBody
,
1091 (UINT32
)ResponseBody
.BodyLength
,
1094 if (EFI_ERROR (Status
)) {
1101 // In "chunked" transfer-coding mode, so we need to parse the received
1102 // data to get the real entity content.
1105 while (!HttpIsMessageComplete (Parser
)) {
1107 // Allocate a buffer in Block to hold the message-body.
1108 // If caller provides a buffer, this Block will be reused in every HttpIoRecvResponse().
1109 // Otherwise a buffer, the buffer in Block will be cached and we should allocate a new before
1110 // every HttpIoRecvResponse().
1112 if (Block
== NULL
|| Context
.BufferSize
== 0) {
1113 Block
= AllocatePool (HTTP_BOOT_BLOCK_SIZE
);
1114 if (Block
== NULL
) {
1115 Status
= EFI_OUT_OF_RESOURCES
;
1118 Context
.NewBlock
= TRUE
;
1119 Context
.Block
= Block
;
1121 Context
.NewBlock
= FALSE
;
1124 ResponseBody
.Body
= (CHAR8
*) Block
;
1125 ResponseBody
.BodyLength
= HTTP_BOOT_BLOCK_SIZE
;
1126 Status
= HttpIoRecvResponse (
1131 if (EFI_ERROR (Status
) || EFI_ERROR (ResponseBody
.Status
)) {
1132 if (EFI_ERROR (ResponseBody
.Status
)) {
1133 Status
= ResponseBody
.Status
;
1139 // Parse the new received block of the message-body, the block will be saved in cache.
1141 Status
= HttpParseMessageBody (
1143 ResponseBody
.BodyLength
,
1146 if (EFI_ERROR (Status
)) {
1154 // 3.5 Message-body receive & parse is completed, we should be able to get the file size now.
1156 Status
= HttpGetEntityLength (Parser
, &ContentLength
);
1157 if (EFI_ERROR (Status
)) {
1161 if (*BufferSize
< ContentLength
) {
1162 Status
= EFI_BUFFER_TOO_SMALL
;
1164 Status
= EFI_SUCCESS
;
1166 *BufferSize
= ContentLength
;
1169 // 4. Save the cache item to driver's cache list and return.
1171 if (Cache
!= NULL
) {
1172 Cache
->EntityLength
= ContentLength
;
1173 InsertTailList (&Private
->CacheList
, &Cache
->Link
);
1176 if (Parser
!= NULL
) {
1177 HttpFreeMsgParser (Parser
);
1183 if (Parser
!= NULL
) {
1184 HttpFreeMsgParser (Parser
);
1186 if (Context
.Block
!= NULL
) {
1187 FreePool (Context
.Block
);
1189 HttpBootFreeCache (Cache
);
1192 if (ResponseData
!= NULL
) {
1193 FreePool (ResponseData
);
1196 if (RequestData
!= NULL
) {
1197 FreePool (RequestData
);
1200 HttpBootFreeHeader (HttpIoHeader
);
1202 if (Cache
!= NULL
) {