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.
236 AsciiPrint ("\n URI: %a", Private
->BootFileUri
);
239 // Update the device path to include the IP and boot URI information.
241 Status
= HttpBootUpdateDevicePath (Private
);
247 Parse the boot file URI information from the selected Dhcp6 offer packet.
249 @param[in] Private The pointer to the driver's private data.
251 @retval EFI_SUCCESS Successfully parsed out all the boot information.
252 @retval Others Failed to parse out the boot information.
256 HttpBootDhcp6ExtractUriInfo (
257 IN HTTP_BOOT_PRIVATE_DATA
*Private
260 HTTP_BOOT_DHCP6_PACKET_CACHE
*SelectOffer
;
261 HTTP_BOOT_DHCP6_PACKET_CACHE
*HttpOffer
;
264 EFI_DHCP6_PACKET_OPTION
*Option
;
265 EFI_IPv6_ADDRESS IpAddr
;
271 ASSERT (Private
!= NULL
);
272 ASSERT (Private
->SelectIndex
!= 0);
273 SelectIndex
= Private
->SelectIndex
- 1;
274 ASSERT (SelectIndex
< HTTP_BOOT_OFFER_MAX_NUM
);
276 Status
= EFI_SUCCESS
;
279 // SelectOffer contains the IP address configuration and name server configuration.
280 // HttpOffer contains the boot file URL.
282 SelectOffer
= &Private
->OfferBuffer
[SelectIndex
].Dhcp6
;
283 if (Private
->FilePathUri
== NULL
) {
285 // In Corporate environment, we need a HttpOffer.
287 if ((SelectOffer
->OfferType
== HttpOfferTypeDhcpIpUri
) ||
288 (SelectOffer
->OfferType
== HttpOfferTypeDhcpIpUriDns
) ||
289 (SelectOffer
->OfferType
== HttpOfferTypeDhcpNameUriDns
)) {
290 HttpOffer
= SelectOffer
;
292 ASSERT (Private
->SelectProxyType
!= HttpOfferTypeMax
);
293 ProxyIndex
= Private
->OfferIndex
[Private
->SelectProxyType
][0];
294 HttpOffer
= &Private
->OfferBuffer
[ProxyIndex
].Dhcp6
;
296 Private
->BootFileUriParser
= HttpOffer
->UriParser
;
297 Private
->BootFileUri
= (CHAR8
*) HttpOffer
->OptList
[HTTP_BOOT_DHCP6_IDX_BOOT_FILE_URL
]->Data
;
300 // In Home environment the BootFileUri comes from the FilePath.
302 Private
->BootFileUriParser
= Private
->FilePathUriParser
;
303 Private
->BootFileUri
= Private
->FilePathUri
;
307 // Check the URI scheme.
309 Status
= HttpBootCheckUriScheme (Private
->BootFileUri
);
310 if (EFI_ERROR (Status
)) {
311 DEBUG ((EFI_D_ERROR
, "HttpBootDhcp6ExtractUriInfo: %r.\n", Status
));
316 // Set the Local station address to IP layer.
318 Status
= HttpBootSetIp6Address (Private
);
319 if (EFI_ERROR (Status
)) {
324 // Register the IPv6 gateway address to the network device.
326 Status
= HttpBootSetIp6Gateway (Private
);
327 if (EFI_ERROR (Status
)) {
332 // Configure the default DNS server if server assigned.
334 if ((SelectOffer
->OfferType
== HttpOfferTypeDhcpNameUriDns
) ||
335 (SelectOffer
->OfferType
== HttpOfferTypeDhcpDns
) ||
336 (SelectOffer
->OfferType
== HttpOfferTypeDhcpIpUriDns
)) {
337 Option
= SelectOffer
->OptList
[HTTP_BOOT_DHCP6_IDX_DNS_SERVER
];
338 ASSERT (Option
!= NULL
);
339 Status
= HttpBootSetIp6Dns (
341 HTONS (Option
->OpLen
),
344 if (EFI_ERROR (Status
)) {
350 // Extract the HTTP server Ip frome URL. This is used to Check route table
351 // whether can send message to HTTP Server Ip through the GateWay.
353 Status
= HttpUrlGetIp6 (
354 Private
->BootFileUri
,
355 Private
->BootFileUriParser
,
359 if (EFI_ERROR (Status
)) {
361 // The Http server address is expressed by Name Ip, so perform DNS resolution
363 Status
= HttpUrlGetHostName (
364 Private
->BootFileUri
,
365 Private
->BootFileUriParser
,
368 if (EFI_ERROR (Status
)) {
372 HostNameSize
= AsciiStrSize (HostName
);
373 HostNameStr
= AllocateZeroPool (HostNameSize
* sizeof (CHAR16
));
374 if (HostNameStr
== NULL
) {
375 Status
= EFI_OUT_OF_RESOURCES
;
379 AsciiStrToUnicodeStrS (HostName
, HostNameStr
, HostNameSize
);
380 Status
= HttpBootDns (Private
, HostNameStr
, &IpAddr
);
381 FreePool (HostNameStr
);
382 if (EFI_ERROR (Status
)) {
387 CopyMem (&Private
->ServerIp
.v6
, &IpAddr
, sizeof (EFI_IPv6_ADDRESS
));
390 // Extract the port from URL, and use default HTTP port 80 if not provided.
392 Status
= HttpUrlGetPort (
393 Private
->BootFileUri
,
394 Private
->BootFileUriParser
,
397 if (EFI_ERROR (Status
) || Private
->Port
== 0) {
402 // All boot informations are valid here.
404 AsciiPrint ("\n URI: %a", Private
->BootFileUri
);
406 // Update the device path to include the IP and boot URI information.
408 Status
= HttpBootUpdateDevicePath (Private
);
412 if (HostName
!= NULL
) {
421 Discover all the boot information for boot file.
423 @param[in, out] Private The pointer to the driver's private data.
425 @retval EFI_SUCCESS Successfully obtained all the boot information .
426 @retval Others Failed to retrieve the boot information.
430 HttpBootDiscoverBootInfo (
431 IN OUT HTTP_BOOT_PRIVATE_DATA
*Private
437 // Start D.O.R.A/S.A.R.R exchange to acquire station ip address and
438 // other Http boot information.
440 Status
= HttpBootDhcp (Private
);
441 if (EFI_ERROR (Status
)) {
445 if (!Private
->UsingIpv6
) {
446 Status
= HttpBootDhcp4ExtractUriInfo (Private
);
448 Status
= HttpBootDhcp6ExtractUriInfo (Private
);
455 Create a HttpIo instance for the file download.
457 @param[in] Private The pointer to the driver's private data.
459 @retval EFI_SUCCESS Successfully created.
460 @retval Others Failed to create HttpIo.
464 HttpBootCreateHttpIo (
465 IN HTTP_BOOT_PRIVATE_DATA
*Private
468 HTTP_IO_CONFIG_DATA ConfigData
;
470 EFI_HANDLE ImageHandle
;
472 ASSERT (Private
!= NULL
);
474 ZeroMem (&ConfigData
, sizeof (HTTP_IO_CONFIG_DATA
));
475 if (!Private
->UsingIpv6
) {
476 ConfigData
.Config4
.HttpVersion
= HttpVersion11
;
477 ConfigData
.Config4
.RequestTimeOut
= HTTP_BOOT_REQUEST_TIMEOUT
;
478 IP4_COPY_ADDRESS (&ConfigData
.Config4
.LocalIp
, &Private
->StationIp
.v4
);
479 IP4_COPY_ADDRESS (&ConfigData
.Config4
.SubnetMask
, &Private
->SubnetMask
.v4
);
480 ImageHandle
= Private
->Ip4Nic
->ImageHandle
;
482 ConfigData
.Config6
.HttpVersion
= HttpVersion11
;
483 ConfigData
.Config6
.RequestTimeOut
= HTTP_BOOT_REQUEST_TIMEOUT
;
484 IP6_COPY_ADDRESS (&ConfigData
.Config6
.LocalIp
, &Private
->StationIp
.v6
);
485 ImageHandle
= Private
->Ip6Nic
->ImageHandle
;
488 Status
= HttpIoCreateIo (
491 Private
->UsingIpv6
? IP_VERSION_6
: IP_VERSION_4
,
495 if (EFI_ERROR (Status
)) {
499 Private
->HttpCreated
= TRUE
;
504 Release all the resource of a cache item.
506 @param[in] Cache The pointer to the cache item.
511 IN HTTP_BOOT_CACHE_CONTENT
*Cache
516 LIST_ENTRY
*NextEntry
;
517 HTTP_BOOT_ENTITY_DATA
*EntityData
;
521 // Free the request data
523 if (Cache
->RequestData
!= NULL
) {
524 if (Cache
->RequestData
->Url
!= NULL
) {
525 FreePool (Cache
->RequestData
->Url
);
527 FreePool (Cache
->RequestData
);
531 // Free the response header
533 if (Cache
->ResponseData
!= NULL
) {
534 if (Cache
->ResponseData
->Headers
!= NULL
) {
535 for (Index
= 0; Index
< Cache
->ResponseData
->HeaderCount
; Index
++) {
536 FreePool (Cache
->ResponseData
->Headers
[Index
].FieldName
);
537 FreePool (Cache
->ResponseData
->Headers
[Index
].FieldValue
);
539 FreePool (Cache
->ResponseData
->Headers
);
544 // Free the response body
546 NET_LIST_FOR_EACH_SAFE (Entry
, NextEntry
, &Cache
->EntityDataList
) {
547 EntityData
= NET_LIST_USER_STRUCT (Entry
, HTTP_BOOT_ENTITY_DATA
, Link
);
548 if (EntityData
->Block
!= NULL
) {
549 FreePool (EntityData
->Block
);
551 RemoveEntryList (&EntityData
->Link
);
552 FreePool (EntityData
);
560 Clean up all cached data.
562 @param[in] Private The pointer to the driver's private data.
566 HttpBootFreeCacheList (
567 IN HTTP_BOOT_PRIVATE_DATA
*Private
571 LIST_ENTRY
*NextEntry
;
572 HTTP_BOOT_CACHE_CONTENT
*Cache
;
574 NET_LIST_FOR_EACH_SAFE (Entry
, NextEntry
, &Private
->CacheList
) {
575 Cache
= NET_LIST_USER_STRUCT (Entry
, HTTP_BOOT_CACHE_CONTENT
, Link
);
576 RemoveEntryList (&Cache
->Link
);
577 HttpBootFreeCache (Cache
);
582 Get the file content from cached data.
584 @param[in] Private The pointer to the driver's private data.
585 @param[in] Uri Uri of the file to be retrieved from cache.
586 @param[in, out] BufferSize On input the size of Buffer in bytes. On output with a return
587 code of EFI_SUCCESS, the amount of data transferred to
588 Buffer. On output with a return code of EFI_BUFFER_TOO_SMALL,
589 the size of Buffer required to retrieve the requested file.
590 @param[out] Buffer The memory buffer to transfer the file to. IF Buffer is NULL,
591 then the size of the requested file is returned in
593 @param[out] ImageType The image type of the downloaded file.
595 @retval EFI_SUCCESS Successfully created.
596 @retval Others Failed to create HttpIo.
600 HttpBootGetFileFromCache (
601 IN HTTP_BOOT_PRIVATE_DATA
*Private
,
603 IN OUT UINTN
*BufferSize
,
605 OUT HTTP_BOOT_IMAGE_TYPE
*ImageType
610 HTTP_BOOT_CACHE_CONTENT
*Cache
;
611 HTTP_BOOT_ENTITY_DATA
*EntityData
;
614 if (Uri
== NULL
|| BufferSize
== 0 || Buffer
== NULL
|| ImageType
== NULL
) {
615 return EFI_INVALID_PARAMETER
;
618 NET_LIST_FOR_EACH (Entry
, &Private
->CacheList
) {
619 Cache
= NET_LIST_USER_STRUCT (Entry
, HTTP_BOOT_CACHE_CONTENT
, Link
);
621 // Compare the URI to see whether we already have a cache for this file.
623 if ((Cache
->RequestData
!= NULL
) &&
624 (Cache
->RequestData
->Url
!= NULL
) &&
625 (StrCmp (Uri
, Cache
->RequestData
->Url
) == 0))
628 // Hit in cache, record image type.
630 *ImageType
= Cache
->ImageType
;
633 // Check buffer size.
635 if (*BufferSize
< Cache
->EntityLength
) {
636 *BufferSize
= Cache
->EntityLength
;
637 return EFI_BUFFER_TOO_SMALL
;
641 // Fill data to buffer.
644 NET_LIST_FOR_EACH (Entry2
, &Cache
->EntityDataList
) {
645 EntityData
= NET_LIST_USER_STRUCT (Entry2
, HTTP_BOOT_ENTITY_DATA
, Link
);
646 if (*BufferSize
> CopyedSize
) {
649 EntityData
->DataStart
,
650 MIN (EntityData
->DataLength
, *BufferSize
- CopyedSize
)
652 CopyedSize
+= MIN (EntityData
->DataLength
, *BufferSize
- CopyedSize
);
655 *BufferSize
= CopyedSize
;
660 return EFI_NOT_FOUND
;
664 A callback function to intercept events during message parser.
666 This function will be invoked during HttpParseMessageBody() with various events type. An error
667 return status of the callback function will cause the HttpParseMessageBody() aborted.
669 @param[in] EventType Event type of this callback call.
670 @param[in] Data A pointer to data buffer.
671 @param[in] Length Length in bytes of the Data.
672 @param[in] Context Callback context set by HttpInitMsgParser().
674 @retval EFI_SUCCESS Continue to parser the message body.
675 @retval Others Abort the parse.
680 HttpBootGetBootFileCallback (
681 IN HTTP_BODY_PARSE_EVENT EventType
,
687 HTTP_BOOT_CALLBACK_DATA
*CallbackData
;
688 HTTP_BOOT_ENTITY_DATA
*NewEntityData
;
691 // We only care about the entity data.
693 if (EventType
!= BodyParseEventOnData
) {
697 CallbackData
= (HTTP_BOOT_CALLBACK_DATA
*) Context
;
699 // Copy data if caller has provided a buffer.
701 if (CallbackData
->BufferSize
> CallbackData
->CopyedSize
) {
703 CallbackData
->Buffer
+ CallbackData
->CopyedSize
,
705 MIN (Length
, CallbackData
->BufferSize
- CallbackData
->CopyedSize
)
707 CallbackData
->CopyedSize
+= MIN (Length
, CallbackData
->BufferSize
- CallbackData
->CopyedSize
);
711 // The caller doesn't provide a buffer, save the block into cache list.
713 if (CallbackData
->Cache
!= NULL
) {
714 NewEntityData
= AllocatePool (sizeof (HTTP_BOOT_ENTITY_DATA
));
715 if (NewEntityData
== NULL
) {
716 return EFI_OUT_OF_RESOURCES
;
718 if (CallbackData
->NewBlock
) {
719 NewEntityData
->Block
= CallbackData
->Block
;
720 CallbackData
->Block
= NULL
;
722 NewEntityData
->DataLength
= Length
;
723 NewEntityData
->DataStart
= (UINT8
*) Data
;
724 InsertTailList (&CallbackData
->Cache
->EntityDataList
, &NewEntityData
->Link
);
730 This function download the boot file by using UEFI HTTP protocol.
732 @param[in] Private The pointer to the driver's private data.
733 @param[in] HeaderOnly Only request the response header, it could save a lot of time if
734 the caller only want to know the size of the requested file.
735 @param[in, out] BufferSize On input the size of Buffer in bytes. On output with a return
736 code of EFI_SUCCESS, the amount of data transferred to
737 Buffer. On output with a return code of EFI_BUFFER_TOO_SMALL,
738 the size of Buffer required to retrieve the requested file.
739 @param[out] Buffer The memory buffer to transfer the file to. IF Buffer is NULL,
740 then the size of the requested file is returned in
742 @param[out] ImageType The image type of the downloaded file.
744 @retval EFI_SUCCESS The file was loaded.
745 @retval EFI_INVALID_PARAMETER BufferSize is NULL or Buffer Size is not NULL but Buffer is NULL.
746 @retval EFI_OUT_OF_RESOURCES Could not allocate needed resources
747 @retval EFI_BUFFER_TOO_SMALL The BufferSize is too small to read the current directory entry.
748 BufferSize has been updated with the size needed to complete
750 @retval Others Unexpected error happened.
754 HttpBootGetBootFile (
755 IN HTTP_BOOT_PRIVATE_DATA
*Private
,
756 IN BOOLEAN HeaderOnly
,
757 IN OUT UINTN
*BufferSize
,
759 OUT HTTP_BOOT_IMAGE_TYPE
*ImageType
763 EFI_HTTP_STATUS_CODE StatusCode
;
765 EFI_HTTP_REQUEST_DATA
*RequestData
;
766 HTTP_IO_RESPONSE_DATA
*ResponseData
;
767 HTTP_IO_RESPONSE_DATA ResponseBody
;
769 HTTP_IO_HEADER
*HttpIoHeader
;
771 HTTP_BOOT_CALLBACK_DATA Context
;
773 HTTP_BOOT_CACHE_CONTENT
*Cache
;
777 BOOLEAN IdentityMode
;
780 ASSERT (Private
!= NULL
);
781 ASSERT (Private
->HttpCreated
);
783 if (BufferSize
== NULL
|| ImageType
== NULL
) {
784 return EFI_INVALID_PARAMETER
;
787 if (*BufferSize
!= 0 && Buffer
== NULL
) {
788 return EFI_INVALID_PARAMETER
;
792 // First, check whether we already cached the requested Uri.
794 UrlSize
= AsciiStrSize (Private
->BootFileUri
);
795 Url
= AllocatePool (UrlSize
* sizeof (CHAR16
));
797 return EFI_OUT_OF_RESOURCES
;
799 AsciiStrToUnicodeStrS (Private
->BootFileUri
, Url
, UrlSize
);
801 Status
= HttpBootGetFileFromCache (Private
, Url
, BufferSize
, Buffer
, ImageType
);
802 if (Status
!= EFI_NOT_FOUND
) {
809 // Not found in cache, try to download it through HTTP.
813 // 1. Create a temp cache item for the requested URI if caller doesn't provide buffer.
816 if ((!HeaderOnly
) && (*BufferSize
== 0)) {
817 Cache
= AllocateZeroPool (sizeof (HTTP_BOOT_CACHE_CONTENT
));
819 Status
= EFI_OUT_OF_RESOURCES
;
822 Cache
->ImageType
= ImageTypeMax
;
823 InitializeListHead (&Cache
->EntityDataList
);
827 // 2. Send HTTP request message.
831 // 2.1 Build HTTP header for the request, 3 header is needed to download a boot file:
836 HttpIoHeader
= HttpBootCreateHeader (3);
837 if (HttpIoHeader
== NULL
) {
838 Status
= EFI_OUT_OF_RESOURCES
;
843 // Add HTTP header field 1: Host
846 Status
= HttpUrlGetHostName (
847 Private
->BootFileUri
,
848 Private
->BootFileUriParser
,
851 if (EFI_ERROR (Status
)) {
854 Status
= HttpBootSetHeader (
860 if (EFI_ERROR (Status
)) {
865 // Add HTTP header field 2: Accept
867 Status
= HttpBootSetHeader (
872 if (EFI_ERROR (Status
)) {
877 // Add HTTP header field 3: User-Agent
879 Status
= HttpBootSetHeader (
881 HTTP_HEADER_USER_AGENT
,
882 HTTP_USER_AGENT_EFI_HTTP_BOOT
884 if (EFI_ERROR (Status
)) {
889 // 2.2 Build the rest of HTTP request info.
891 RequestData
= AllocatePool (sizeof (EFI_HTTP_REQUEST_DATA
));
892 if (RequestData
== NULL
) {
893 Status
= EFI_OUT_OF_RESOURCES
;
896 RequestData
->Method
= HeaderOnly
? HttpMethodHead
: HttpMethodGet
;
897 RequestData
->Url
= Url
;
900 // 2.3 Record the request info in a temp cache item.
903 Cache
->RequestData
= RequestData
;
907 // 2.4 Send out the request to HTTP server.
909 HttpIo
= &Private
->HttpIo
;
910 Status
= HttpIoSendRequest (
913 HttpIoHeader
->HeaderCount
,
914 HttpIoHeader
->Headers
,
918 if (EFI_ERROR (Status
)) {
923 // 3. Receive HTTP response message.
927 // 3.1 First step, use zero BodyLength to only receive the response headers.
929 ResponseData
= AllocateZeroPool (sizeof(HTTP_IO_RESPONSE_DATA
));
930 if (ResponseData
== NULL
) {
931 Status
= EFI_OUT_OF_RESOURCES
;
934 Status
= HttpIoRecvResponse (
939 if (EFI_ERROR (Status
) || EFI_ERROR (ResponseData
->Status
)) {
940 if (EFI_ERROR (ResponseData
->Status
)) {
941 StatusCode
= HttpIo
->RspToken
.Message
->Data
.Response
->StatusCode
;
942 HttpBootPrintErrorMessage (StatusCode
);
943 Status
= ResponseData
->Status
;
949 // Check the image type according to server's response.
951 Status
= HttpBootCheckImageType (
952 Private
->BootFileUri
,
953 Private
->BootFileUriParser
,
954 ResponseData
->HeaderCount
,
955 ResponseData
->Headers
,
958 if (EFI_ERROR (Status
)) {
963 // 3.2 Cache the response header.
966 Cache
->ResponseData
= ResponseData
;
967 Cache
->ImageType
= *ImageType
;
971 // 3.3 Init a message-body parser from the header information.
974 Context
.NewBlock
= FALSE
;
975 Context
.Block
= NULL
;
976 Context
.CopyedSize
= 0;
977 Context
.Buffer
= Buffer
;
978 Context
.BufferSize
= *BufferSize
;
979 Context
.Cache
= Cache
;
980 Status
= HttpInitMsgParser (
981 HeaderOnly
? HttpMethodHead
: HttpMethodGet
,
982 ResponseData
->Response
.StatusCode
,
983 ResponseData
->HeaderCount
,
984 ResponseData
->Headers
,
985 HttpBootGetBootFileCallback
,
989 if (EFI_ERROR (Status
)) {
994 // 3.4 Continue to receive and parse message-body if needed.
999 // 3.4.1, check whether we are in identity transfer-coding.
1002 Status
= HttpGetEntityLength (Parser
, &ContentLength
);
1003 if (!EFI_ERROR (Status
)) {
1004 IdentityMode
= TRUE
;
1006 IdentityMode
= FALSE
;
1010 // 3.4.2, start the message-body download, the identity and chunked transfer-coding
1011 // is handled in different path here.
1013 ZeroMem (&ResponseBody
, sizeof (HTTP_IO_RESPONSE_DATA
));
1016 // In identity transfer-coding there is no need to parse the message body,
1017 // just download the message body to the user provided buffer directly.
1020 while (ReceivedSize
< ContentLength
) {
1021 ResponseBody
.Body
= (CHAR8
*) Buffer
+ ReceivedSize
;
1022 ResponseBody
.BodyLength
= *BufferSize
- ReceivedSize
;
1023 Status
= HttpIoRecvResponse (
1028 if (EFI_ERROR (Status
) || EFI_ERROR (ResponseBody
.Status
)) {
1029 if (EFI_ERROR (ResponseBody
.Status
)) {
1030 Status
= ResponseBody
.Status
;
1034 ReceivedSize
+= ResponseBody
.BodyLength
;
1038 // In "chunked" transfer-coding mode, so we need to parse the received
1039 // data to get the real entity content.
1042 while (!HttpIsMessageComplete (Parser
)) {
1044 // Allocate a buffer in Block to hold the message-body.
1045 // If caller provides a buffer, this Block will be reused in every HttpIoRecvResponse().
1046 // Otherwise a buffer, the buffer in Block will be cached and we should allocate a new before
1047 // every HttpIoRecvResponse().
1049 if (Block
== NULL
|| Context
.BufferSize
== 0) {
1050 Block
= AllocatePool (HTTP_BOOT_BLOCK_SIZE
);
1051 if (Block
== NULL
) {
1052 Status
= EFI_OUT_OF_RESOURCES
;
1055 Context
.NewBlock
= TRUE
;
1056 Context
.Block
= Block
;
1058 Context
.NewBlock
= FALSE
;
1061 ResponseBody
.Body
= (CHAR8
*) Block
;
1062 ResponseBody
.BodyLength
= HTTP_BOOT_BLOCK_SIZE
;
1063 Status
= HttpIoRecvResponse (
1068 if (EFI_ERROR (Status
) || EFI_ERROR (ResponseBody
.Status
)) {
1069 if (EFI_ERROR (ResponseBody
.Status
)) {
1070 Status
= ResponseBody
.Status
;
1076 // Parse the new received block of the message-body, the block will be saved in cache.
1078 Status
= HttpParseMessageBody (
1080 ResponseBody
.BodyLength
,
1083 if (EFI_ERROR (Status
)) {
1091 // 3.5 Message-body receive & parse is completed, we should be able to get the file size now.
1093 Status
= HttpGetEntityLength (Parser
, &ContentLength
);
1094 if (EFI_ERROR (Status
)) {
1098 if (*BufferSize
< ContentLength
) {
1099 Status
= EFI_BUFFER_TOO_SMALL
;
1101 Status
= EFI_SUCCESS
;
1103 *BufferSize
= ContentLength
;
1106 // 4. Save the cache item to driver's cache list and return.
1108 if (Cache
!= NULL
) {
1109 Cache
->EntityLength
= ContentLength
;
1110 InsertTailList (&Private
->CacheList
, &Cache
->Link
);
1113 if (Parser
!= NULL
) {
1114 HttpFreeMsgParser (Parser
);
1120 if (Parser
!= NULL
) {
1121 HttpFreeMsgParser (Parser
);
1123 if (Context
.Block
!= NULL
) {
1124 FreePool (Context
.Block
);
1126 HttpBootFreeCache (Cache
);
1129 if (ResponseData
!= NULL
) {
1130 FreePool (ResponseData
);
1133 if (RequestData
!= NULL
) {
1134 FreePool (RequestData
);
1137 HttpBootFreeHeader (HttpIoHeader
);
1139 if (Cache
!= NULL
) {