2 Implementation of the boot file download function.
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"
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 // Configure the default DNS server if server assigned.
197 if ((SelectOffer
->OfferType
== HttpOfferTypeDhcpNameUriDns
) ||
198 (SelectOffer
->OfferType
== HttpOfferTypeDhcpDns
) ||
199 (SelectOffer
->OfferType
== HttpOfferTypeDhcpIpUriDns
)) {
200 Option
= SelectOffer
->OptList
[HTTP_BOOT_DHCP4_TAG_INDEX_DNS_SERVER
];
201 ASSERT (Option
!= NULL
);
202 Status
= HttpBootRegisterIp4Dns (
207 if (EFI_ERROR (Status
)) {
213 // Extract the port from URL, and use default HTTP port 80 if not provided.
215 Status
= HttpUrlGetPort (
216 Private
->BootFileUri
,
217 Private
->BootFileUriParser
,
220 if (EFI_ERROR (Status
) || Private
->Port
== 0) {
225 // All boot informations are valid here.
227 AsciiPrint ("\n URI: %a", Private
->BootFileUri
);
230 // Update the device path to include the IP and boot URI information.
232 Status
= HttpBootUpdateDevicePath (Private
);
238 Parse the boot file URI information from the selected Dhcp6 offer packet.
240 @param[in] Private The pointer to the driver's private data.
242 @retval EFI_SUCCESS Successfully parsed out all the boot information.
243 @retval Others Failed to parse out the boot information.
247 HttpBootDhcp6ExtractUriInfo (
248 IN HTTP_BOOT_PRIVATE_DATA
*Private
251 HTTP_BOOT_DHCP6_PACKET_CACHE
*SelectOffer
;
252 HTTP_BOOT_DHCP6_PACKET_CACHE
*HttpOffer
;
255 EFI_DHCP6_PACKET_OPTION
*Option
;
256 EFI_IPv6_ADDRESS IpAddr
;
262 ASSERT (Private
!= NULL
);
263 ASSERT (Private
->SelectIndex
!= 0);
264 SelectIndex
= Private
->SelectIndex
- 1;
265 ASSERT (SelectIndex
< HTTP_BOOT_OFFER_MAX_NUM
);
267 Status
= EFI_SUCCESS
;
270 // SelectOffer contains the IP address configuration and name server configuration.
271 // HttpOffer contains the boot file URL.
273 SelectOffer
= &Private
->OfferBuffer
[SelectIndex
].Dhcp6
;
274 if (Private
->FilePathUri
== NULL
) {
276 // In Corporate environment, we need a HttpOffer.
278 if ((SelectOffer
->OfferType
== HttpOfferTypeDhcpIpUri
) ||
279 (SelectOffer
->OfferType
== HttpOfferTypeDhcpIpUriDns
) ||
280 (SelectOffer
->OfferType
== HttpOfferTypeDhcpNameUriDns
)) {
281 HttpOffer
= SelectOffer
;
283 ASSERT (Private
->SelectProxyType
!= HttpOfferTypeMax
);
284 ProxyIndex
= Private
->OfferIndex
[Private
->SelectProxyType
][0];
285 HttpOffer
= &Private
->OfferBuffer
[ProxyIndex
].Dhcp6
;
287 Private
->BootFileUriParser
= HttpOffer
->UriParser
;
288 Private
->BootFileUri
= (CHAR8
*) HttpOffer
->OptList
[HTTP_BOOT_DHCP6_IDX_BOOT_FILE_URL
]->Data
;
291 // In Home environment the BootFileUri comes from the FilePath.
293 Private
->BootFileUriParser
= Private
->FilePathUriParser
;
294 Private
->BootFileUri
= Private
->FilePathUri
;
298 // Set the Local station address to IP layer.
300 Status
= HttpBootSetIp6Address (Private
);
301 if (EFI_ERROR (Status
)) {
306 // Register the IPv6 gateway address to the network device.
308 Status
= HttpBootSetIp6Gateway (Private
);
309 if (EFI_ERROR (Status
)) {
314 // Configure the default DNS server if server assigned.
316 if ((SelectOffer
->OfferType
== HttpOfferTypeDhcpNameUriDns
) ||
317 (SelectOffer
->OfferType
== HttpOfferTypeDhcpDns
) ||
318 (SelectOffer
->OfferType
== HttpOfferTypeDhcpIpUriDns
)) {
319 Option
= SelectOffer
->OptList
[HTTP_BOOT_DHCP6_IDX_DNS_SERVER
];
320 ASSERT (Option
!= NULL
);
321 Status
= HttpBootSetIp6Dns (
323 HTONS (Option
->OpLen
),
326 if (EFI_ERROR (Status
)) {
332 // Extract the HTTP server Ip frome URL. This is used to Check route table
333 // whether can send message to HTTP Server Ip through the GateWay.
335 Status
= HttpUrlGetIp6 (
336 Private
->BootFileUri
,
337 Private
->BootFileUriParser
,
341 if (EFI_ERROR (Status
)) {
343 // The Http server address is expressed by Name Ip, so perform DNS resolution
345 Status
= HttpUrlGetHostName (
346 Private
->BootFileUri
,
347 Private
->BootFileUriParser
,
350 if (EFI_ERROR (Status
)) {
354 HostNameSize
= AsciiStrSize (HostName
);
355 HostNameStr
= AllocateZeroPool (HostNameSize
* sizeof (CHAR16
));
356 if (HostNameStr
== NULL
) {
357 Status
= EFI_OUT_OF_RESOURCES
;
361 AsciiStrToUnicodeStrS (HostName
, HostNameStr
, HostNameSize
);
362 Status
= HttpBootDns (Private
, HostNameStr
, &IpAddr
);
363 FreePool (HostNameStr
);
364 if (EFI_ERROR (Status
)) {
369 CopyMem (&Private
->ServerIp
.v6
, &IpAddr
, sizeof (EFI_IPv6_ADDRESS
));
372 // Extract the port from URL, and use default HTTP port 80 if not provided.
374 Status
= HttpUrlGetPort (
375 Private
->BootFileUri
,
376 Private
->BootFileUriParser
,
379 if (EFI_ERROR (Status
) || Private
->Port
== 0) {
384 // All boot informations are valid here.
386 AsciiPrint ("\n URI: %a", Private
->BootFileUri
);
388 // Update the device path to include the IP and boot URI information.
390 Status
= HttpBootUpdateDevicePath (Private
);
394 if (HostName
!= NULL
) {
403 Discover all the boot information for boot file.
405 @param[in, out] Private The pointer to the driver's private data.
407 @retval EFI_SUCCESS Successfully obtained all the boot information .
408 @retval Others Failed to retrieve the boot information.
412 HttpBootDiscoverBootInfo (
413 IN OUT HTTP_BOOT_PRIVATE_DATA
*Private
419 // Start D.O.R.A/S.A.R.R exchange to acquire station ip address and
420 // other Http boot information.
422 Status
= HttpBootDhcp (Private
);
423 if (EFI_ERROR (Status
)) {
427 if (!Private
->UsingIpv6
) {
428 Status
= HttpBootDhcp4ExtractUriInfo (Private
);
430 Status
= HttpBootDhcp6ExtractUriInfo (Private
);
437 Create a HttpIo instance for the file download.
439 @param[in] Private The pointer to the driver's private data.
441 @retval EFI_SUCCESS Successfully created.
442 @retval Others Failed to create HttpIo.
446 HttpBootCreateHttpIo (
447 IN HTTP_BOOT_PRIVATE_DATA
*Private
450 HTTP_IO_CONFIG_DATA ConfigData
;
452 EFI_HANDLE ImageHandle
;
454 ASSERT (Private
!= NULL
);
456 ZeroMem (&ConfigData
, sizeof (HTTP_IO_CONFIG_DATA
));
457 if (!Private
->UsingIpv6
) {
458 ConfigData
.Config4
.HttpVersion
= HttpVersion11
;
459 ConfigData
.Config4
.RequestTimeOut
= HTTP_BOOT_REQUEST_TIMEOUT
;
460 IP4_COPY_ADDRESS (&ConfigData
.Config4
.LocalIp
, &Private
->StationIp
.v4
);
461 IP4_COPY_ADDRESS (&ConfigData
.Config4
.SubnetMask
, &Private
->SubnetMask
.v4
);
462 ImageHandle
= Private
->Ip4Nic
->ImageHandle
;
464 ConfigData
.Config6
.HttpVersion
= HttpVersion11
;
465 ConfigData
.Config6
.RequestTimeOut
= HTTP_BOOT_REQUEST_TIMEOUT
;
466 IP6_COPY_ADDRESS (&ConfigData
.Config6
.LocalIp
, &Private
->StationIp
.v6
);
467 ImageHandle
= Private
->Ip6Nic
->ImageHandle
;
470 Status
= HttpIoCreateIo (
473 Private
->UsingIpv6
? IP_VERSION_6
: IP_VERSION_4
,
477 if (EFI_ERROR (Status
)) {
481 Private
->HttpCreated
= TRUE
;
486 Release all the resource of a cache item.
488 @param[in] Cache The pointer to the cache item.
493 IN HTTP_BOOT_CACHE_CONTENT
*Cache
498 LIST_ENTRY
*NextEntry
;
499 HTTP_BOOT_ENTITY_DATA
*EntityData
;
503 // Free the request data
505 if (Cache
->RequestData
!= NULL
) {
506 if (Cache
->RequestData
->Url
!= NULL
) {
507 FreePool (Cache
->RequestData
->Url
);
509 FreePool (Cache
->RequestData
);
513 // Free the response header
515 if (Cache
->ResponseData
!= NULL
) {
516 if (Cache
->ResponseData
->Headers
!= NULL
) {
517 for (Index
= 0; Index
< Cache
->ResponseData
->HeaderCount
; Index
++) {
518 FreePool (Cache
->ResponseData
->Headers
[Index
].FieldName
);
519 FreePool (Cache
->ResponseData
->Headers
[Index
].FieldValue
);
521 FreePool (Cache
->ResponseData
->Headers
);
526 // Free the response body
528 NET_LIST_FOR_EACH_SAFE (Entry
, NextEntry
, &Cache
->EntityDataList
) {
529 EntityData
= NET_LIST_USER_STRUCT (Entry
, HTTP_BOOT_ENTITY_DATA
, Link
);
530 if (EntityData
->Block
!= NULL
) {
531 FreePool (EntityData
->Block
);
533 RemoveEntryList (&EntityData
->Link
);
534 FreePool (EntityData
);
542 Clean up all cached data.
544 @param[in] Private The pointer to the driver's private data.
548 HttpBootFreeCacheList (
549 IN HTTP_BOOT_PRIVATE_DATA
*Private
553 LIST_ENTRY
*NextEntry
;
554 HTTP_BOOT_CACHE_CONTENT
*Cache
;
556 NET_LIST_FOR_EACH_SAFE (Entry
, NextEntry
, &Private
->CacheList
) {
557 Cache
= NET_LIST_USER_STRUCT (Entry
, HTTP_BOOT_CACHE_CONTENT
, Link
);
558 RemoveEntryList (&Cache
->Link
);
559 HttpBootFreeCache (Cache
);
564 Get the file content from cached data.
566 @param[in] Private The pointer to the driver's private data.
567 @param[in] Uri Uri of the file to be retrieved from cache.
568 @param[in, out] BufferSize On input the size of Buffer in bytes. On output with a return
569 code of EFI_SUCCESS, the amount of data transferred to
570 Buffer. On output with a return code of EFI_BUFFER_TOO_SMALL,
571 the size of Buffer required to retrieve the requested file.
572 @param[out] Buffer The memory buffer to transfer the file to. IF Buffer is NULL,
573 then the size of the requested file is returned in
575 @param[out] ImageType The image type of the downloaded file.
577 @retval EFI_SUCCESS Successfully created.
578 @retval Others Failed to create HttpIo.
582 HttpBootGetFileFromCache (
583 IN HTTP_BOOT_PRIVATE_DATA
*Private
,
585 IN OUT UINTN
*BufferSize
,
587 OUT HTTP_BOOT_IMAGE_TYPE
*ImageType
592 HTTP_BOOT_CACHE_CONTENT
*Cache
;
593 HTTP_BOOT_ENTITY_DATA
*EntityData
;
596 if (Uri
== NULL
|| BufferSize
== 0 || Buffer
== NULL
|| ImageType
== NULL
) {
597 return EFI_INVALID_PARAMETER
;
600 NET_LIST_FOR_EACH (Entry
, &Private
->CacheList
) {
601 Cache
= NET_LIST_USER_STRUCT (Entry
, HTTP_BOOT_CACHE_CONTENT
, Link
);
603 // Compare the URI to see whether we already have a cache for this file.
605 if ((Cache
->RequestData
!= NULL
) &&
606 (Cache
->RequestData
->Url
!= NULL
) &&
607 (StrCmp (Uri
, Cache
->RequestData
->Url
) == 0))
610 // Hit in cache, record image type.
612 *ImageType
= Cache
->ImageType
;
615 // Check buffer size.
617 if (*BufferSize
< Cache
->EntityLength
) {
618 *BufferSize
= Cache
->EntityLength
;
619 return EFI_BUFFER_TOO_SMALL
;
623 // Fill data to buffer.
626 NET_LIST_FOR_EACH (Entry2
, &Cache
->EntityDataList
) {
627 EntityData
= NET_LIST_USER_STRUCT (Entry2
, HTTP_BOOT_ENTITY_DATA
, Link
);
628 if (*BufferSize
> CopyedSize
) {
631 EntityData
->DataStart
,
632 MIN (EntityData
->DataLength
, *BufferSize
- CopyedSize
)
634 CopyedSize
+= MIN (EntityData
->DataLength
, *BufferSize
- CopyedSize
);
637 *BufferSize
= CopyedSize
;
642 return EFI_NOT_FOUND
;
646 A callback function to intercept events during message parser.
648 This function will be invoked during HttpParseMessageBody() with various events type. An error
649 return status of the callback function will cause the HttpParseMessageBody() aborted.
651 @param[in] EventType Event type of this callback call.
652 @param[in] Data A pointer to data buffer.
653 @param[in] Length Length in bytes of the Data.
654 @param[in] Context Callback context set by HttpInitMsgParser().
656 @retval EFI_SUCCESS Continue to parser the message body.
657 @retval Others Abort the parse.
662 HttpBootGetBootFileCallback (
663 IN HTTP_BODY_PARSE_EVENT EventType
,
669 HTTP_BOOT_CALLBACK_DATA
*CallbackData
;
670 HTTP_BOOT_ENTITY_DATA
*NewEntityData
;
673 // We only care about the entity data.
675 if (EventType
!= BodyParseEventOnData
) {
679 CallbackData
= (HTTP_BOOT_CALLBACK_DATA
*) Context
;
681 // Copy data if caller has provided a buffer.
683 if (CallbackData
->BufferSize
> CallbackData
->CopyedSize
) {
685 CallbackData
->Buffer
+ CallbackData
->CopyedSize
,
687 MIN (Length
, CallbackData
->BufferSize
- CallbackData
->CopyedSize
)
689 CallbackData
->CopyedSize
+= MIN (Length
, CallbackData
->BufferSize
- CallbackData
->CopyedSize
);
693 // The caller doesn't provide a buffer, save the block into cache list.
695 if (CallbackData
->Cache
!= NULL
) {
696 NewEntityData
= AllocatePool (sizeof (HTTP_BOOT_ENTITY_DATA
));
697 if (NewEntityData
== NULL
) {
698 return EFI_OUT_OF_RESOURCES
;
700 if (CallbackData
->NewBlock
) {
701 NewEntityData
->Block
= CallbackData
->Block
;
702 CallbackData
->Block
= NULL
;
704 NewEntityData
->DataLength
= Length
;
705 NewEntityData
->DataStart
= (UINT8
*) Data
;
706 InsertTailList (&CallbackData
->Cache
->EntityDataList
, &NewEntityData
->Link
);
712 This function download the boot file by using UEFI HTTP protocol.
714 @param[in] Private The pointer to the driver's private data.
715 @param[in] HeaderOnly Only request the response header, it could save a lot of time if
716 the caller only want to know the size of the requested file.
717 @param[in, out] BufferSize On input the size of Buffer in bytes. On output with a return
718 code of EFI_SUCCESS, the amount of data transferred to
719 Buffer. On output with a return code of EFI_BUFFER_TOO_SMALL,
720 the size of Buffer required to retrieve the requested file.
721 @param[out] Buffer The memory buffer to transfer the file to. IF Buffer is NULL,
722 then the size of the requested file is returned in
724 @param[out] ImageType The image type of the downloaded file.
726 @retval EFI_SUCCESS The file was loaded.
727 @retval EFI_INVALID_PARAMETER BufferSize is NULL or Buffer Size is not NULL but Buffer is NULL.
728 @retval EFI_OUT_OF_RESOURCES Could not allocate needed resources
729 @retval EFI_BUFFER_TOO_SMALL The BufferSize is too small to read the current directory entry.
730 BufferSize has been updated with the size needed to complete
732 @retval Others Unexpected error happened.
736 HttpBootGetBootFile (
737 IN HTTP_BOOT_PRIVATE_DATA
*Private
,
738 IN BOOLEAN HeaderOnly
,
739 IN OUT UINTN
*BufferSize
,
741 OUT HTTP_BOOT_IMAGE_TYPE
*ImageType
745 EFI_HTTP_STATUS_CODE StatusCode
;
747 EFI_HTTP_REQUEST_DATA
*RequestData
;
748 HTTP_IO_RESPONSE_DATA
*ResponseData
;
749 HTTP_IO_RESPONSE_DATA ResponseBody
;
751 HTTP_IO_HEADER
*HttpIoHeader
;
753 HTTP_BOOT_CALLBACK_DATA Context
;
755 HTTP_BOOT_CACHE_CONTENT
*Cache
;
759 BOOLEAN IdentityMode
;
762 ASSERT (Private
!= NULL
);
763 ASSERT (Private
->HttpCreated
);
765 if (BufferSize
== NULL
|| ImageType
== NULL
) {
766 return EFI_INVALID_PARAMETER
;
769 if (*BufferSize
!= 0 && Buffer
== NULL
) {
770 return EFI_INVALID_PARAMETER
;
774 // First, check whether we already cached the requested Uri.
776 UrlSize
= AsciiStrSize (Private
->BootFileUri
);
777 Url
= AllocatePool (UrlSize
* sizeof (CHAR16
));
779 return EFI_OUT_OF_RESOURCES
;
781 AsciiStrToUnicodeStrS (Private
->BootFileUri
, Url
, UrlSize
);
783 Status
= HttpBootGetFileFromCache (Private
, Url
, BufferSize
, Buffer
, ImageType
);
784 if (Status
!= EFI_NOT_FOUND
) {
791 // Not found in cache, try to download it through HTTP.
795 // 1. Create a temp cache item for the requested URI if caller doesn't provide buffer.
798 if ((!HeaderOnly
) && (*BufferSize
== 0)) {
799 Cache
= AllocateZeroPool (sizeof (HTTP_BOOT_CACHE_CONTENT
));
801 Status
= EFI_OUT_OF_RESOURCES
;
804 Cache
->ImageType
= ImageTypeMax
;
805 InitializeListHead (&Cache
->EntityDataList
);
809 // 2. Send HTTP request message.
813 // 2.1 Build HTTP header for the request, 3 header is needed to download a boot file:
818 HttpIoHeader
= HttpBootCreateHeader (3);
819 if (HttpIoHeader
== NULL
) {
820 Status
= EFI_OUT_OF_RESOURCES
;
825 // Add HTTP header field 1: Host
828 Status
= HttpUrlGetHostName (
829 Private
->BootFileUri
,
830 Private
->BootFileUriParser
,
833 if (EFI_ERROR (Status
)) {
836 Status
= HttpBootSetHeader (
842 if (EFI_ERROR (Status
)) {
847 // Add HTTP header field 2: Accept
849 Status
= HttpBootSetHeader (
854 if (EFI_ERROR (Status
)) {
859 // Add HTTP header field 3: User-Agent
861 Status
= HttpBootSetHeader (
863 HTTP_HEADER_USER_AGENT
,
864 HTTP_USER_AGENT_EFI_HTTP_BOOT
866 if (EFI_ERROR (Status
)) {
871 // 2.2 Build the rest of HTTP request info.
873 RequestData
= AllocatePool (sizeof (EFI_HTTP_REQUEST_DATA
));
874 if (RequestData
== NULL
) {
875 Status
= EFI_OUT_OF_RESOURCES
;
878 RequestData
->Method
= HeaderOnly
? HttpMethodHead
: HttpMethodGet
;
879 RequestData
->Url
= Url
;
882 // 2.3 Record the request info in a temp cache item.
885 Cache
->RequestData
= RequestData
;
889 // 2.4 Send out the request to HTTP server.
891 HttpIo
= &Private
->HttpIo
;
892 Status
= HttpIoSendRequest (
895 HttpIoHeader
->HeaderCount
,
896 HttpIoHeader
->Headers
,
900 if (EFI_ERROR (Status
)) {
905 // 3. Receive HTTP response message.
909 // 3.1 First step, use zero BodyLength to only receive the response headers.
911 ResponseData
= AllocateZeroPool (sizeof(HTTP_IO_RESPONSE_DATA
));
912 if (ResponseData
== NULL
) {
913 Status
= EFI_OUT_OF_RESOURCES
;
916 Status
= HttpIoRecvResponse (
921 if (EFI_ERROR (Status
) || EFI_ERROR (ResponseData
->Status
)) {
922 if (EFI_ERROR (ResponseData
->Status
)) {
923 StatusCode
= HttpIo
->RspToken
.Message
->Data
.Response
->StatusCode
;
924 HttpBootPrintErrorMessage (StatusCode
);
925 Status
= ResponseData
->Status
;
931 // Check the image type according to server's response.
933 Status
= HttpBootCheckImageType (
934 Private
->BootFileUri
,
935 Private
->BootFileUriParser
,
936 ResponseData
->HeaderCount
,
937 ResponseData
->Headers
,
940 if (EFI_ERROR (Status
)) {
945 // 3.2 Cache the response header.
948 Cache
->ResponseData
= ResponseData
;
949 Cache
->ImageType
= *ImageType
;
953 // 3.3 Init a message-body parser from the header information.
956 Context
.NewBlock
= FALSE
;
957 Context
.Block
= NULL
;
958 Context
.CopyedSize
= 0;
959 Context
.Buffer
= Buffer
;
960 Context
.BufferSize
= *BufferSize
;
961 Context
.Cache
= Cache
;
962 Status
= HttpInitMsgParser (
963 HeaderOnly
? HttpMethodHead
: HttpMethodGet
,
964 ResponseData
->Response
.StatusCode
,
965 ResponseData
->HeaderCount
,
966 ResponseData
->Headers
,
967 HttpBootGetBootFileCallback
,
971 if (EFI_ERROR (Status
)) {
976 // 3.4 Continue to receive and parse message-body if needed.
981 // 3.4.1, check whether we are in identity transfer-coding.
984 Status
= HttpGetEntityLength (Parser
, &ContentLength
);
985 if (!EFI_ERROR (Status
)) {
988 IdentityMode
= FALSE
;
992 // 3.4.2, start the message-body download, the identity and chunked transfer-coding
993 // is handled in different path here.
995 ZeroMem (&ResponseBody
, sizeof (HTTP_IO_RESPONSE_DATA
));
998 // In identity transfer-coding there is no need to parse the message body,
999 // just download the message body to the user provided buffer directly.
1002 while (ReceivedSize
< ContentLength
) {
1003 ResponseBody
.Body
= (CHAR8
*) Buffer
+ ReceivedSize
;
1004 ResponseBody
.BodyLength
= *BufferSize
- ReceivedSize
;
1005 Status
= HttpIoRecvResponse (
1010 if (EFI_ERROR (Status
) || EFI_ERROR (ResponseBody
.Status
)) {
1011 if (EFI_ERROR (ResponseBody
.Status
)) {
1012 Status
= ResponseBody
.Status
;
1016 ReceivedSize
+= ResponseBody
.BodyLength
;
1020 // In "chunked" transfer-coding mode, so we need to parse the received
1021 // data to get the real entity content.
1024 while (!HttpIsMessageComplete (Parser
)) {
1026 // Allocate a buffer in Block to hold the message-body.
1027 // If caller provides a buffer, this Block will be reused in every HttpIoRecvResponse().
1028 // Otherwise a buffer, the buffer in Block will be cached and we should allocate a new before
1029 // every HttpIoRecvResponse().
1031 if (Block
== NULL
|| Context
.BufferSize
== 0) {
1032 Block
= AllocatePool (HTTP_BOOT_BLOCK_SIZE
);
1033 if (Block
== NULL
) {
1034 Status
= EFI_OUT_OF_RESOURCES
;
1037 Context
.NewBlock
= TRUE
;
1038 Context
.Block
= Block
;
1040 Context
.NewBlock
= FALSE
;
1043 ResponseBody
.Body
= (CHAR8
*) Block
;
1044 ResponseBody
.BodyLength
= HTTP_BOOT_BLOCK_SIZE
;
1045 Status
= HttpIoRecvResponse (
1050 if (EFI_ERROR (Status
) || EFI_ERROR (ResponseBody
.Status
)) {
1051 if (EFI_ERROR (ResponseBody
.Status
)) {
1052 Status
= ResponseBody
.Status
;
1058 // Parse the new received block of the message-body, the block will be saved in cache.
1060 Status
= HttpParseMessageBody (
1062 ResponseBody
.BodyLength
,
1065 if (EFI_ERROR (Status
)) {
1073 // 3.5 Message-body receive & parse is completed, we should be able to get the file size now.
1075 Status
= HttpGetEntityLength (Parser
, &ContentLength
);
1076 if (EFI_ERROR (Status
)) {
1080 if (*BufferSize
< ContentLength
) {
1081 Status
= EFI_BUFFER_TOO_SMALL
;
1083 Status
= EFI_SUCCESS
;
1085 *BufferSize
= ContentLength
;
1088 // 4. Save the cache item to driver's cache list and return.
1090 if (Cache
!= NULL
) {
1091 Cache
->EntityLength
= ContentLength
;
1092 InsertTailList (&Private
->CacheList
, &Cache
->Link
);
1095 if (Parser
!= NULL
) {
1096 HttpFreeMsgParser (Parser
);
1102 if (Parser
!= NULL
) {
1103 HttpFreeMsgParser (Parser
);
1105 if (Context
.Block
!= NULL
) {
1106 FreePool (Context
.Block
);
1108 HttpBootFreeCache (Cache
);
1111 if (ResponseData
!= NULL
) {
1112 FreePool (ResponseData
);
1115 if (RequestData
!= NULL
) {
1116 FreePool (RequestData
);
1119 HttpBootFreeHeader (HttpIoHeader
);
1121 if (Cache
!= NULL
) {