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
;
261 ASSERT (Private
!= NULL
);
262 ASSERT (Private
->SelectIndex
!= 0);
263 SelectIndex
= Private
->SelectIndex
- 1;
264 ASSERT (SelectIndex
< HTTP_BOOT_OFFER_MAX_NUM
);
266 Status
= EFI_SUCCESS
;
269 // SelectOffer contains the IP address configuration and name server configuration.
270 // HttpOffer contains the boot file URL.
272 SelectOffer
= &Private
->OfferBuffer
[SelectIndex
].Dhcp6
;
273 if (Private
->FilePathUri
== NULL
) {
275 // In Corporate environment, we need a HttpOffer.
277 if ((SelectOffer
->OfferType
== HttpOfferTypeDhcpIpUri
) ||
278 (SelectOffer
->OfferType
== HttpOfferTypeDhcpIpUriDns
) ||
279 (SelectOffer
->OfferType
== HttpOfferTypeDhcpNameUriDns
)) {
280 HttpOffer
= SelectOffer
;
282 ASSERT (Private
->SelectProxyType
!= HttpOfferTypeMax
);
283 ProxyIndex
= Private
->OfferIndex
[Private
->SelectProxyType
][0];
284 HttpOffer
= &Private
->OfferBuffer
[ProxyIndex
].Dhcp6
;
286 Private
->BootFileUriParser
= HttpOffer
->UriParser
;
287 Private
->BootFileUri
= (CHAR8
*) HttpOffer
->OptList
[HTTP_BOOT_DHCP6_IDX_BOOT_FILE_URL
]->Data
;
290 // In Home environment the BootFileUri comes from the FilePath.
292 Private
->BootFileUriParser
= Private
->FilePathUriParser
;
293 Private
->BootFileUri
= Private
->FilePathUri
;
297 // Set the Local station address to IP layer.
299 Status
= HttpBootSetIp6Address (Private
);
300 if (EFI_ERROR (Status
)) {
305 // Configure the default DNS server if server assigned.
307 if ((SelectOffer
->OfferType
== HttpOfferTypeDhcpNameUriDns
) ||
308 (SelectOffer
->OfferType
== HttpOfferTypeDhcpDns
) ||
309 (SelectOffer
->OfferType
== HttpOfferTypeDhcpIpUriDns
)) {
310 Option
= SelectOffer
->OptList
[HTTP_BOOT_DHCP6_IDX_DNS_SERVER
];
311 ASSERT (Option
!= NULL
);
312 Status
= HttpBootSetIp6Dns (
314 HTONS (Option
->OpLen
),
317 if (EFI_ERROR (Status
)) {
323 // Extract the HTTP server Ip frome URL. This is used to Check route table
324 // whether can send message to HTTP Server Ip through the GateWay.
326 Status
= HttpUrlGetIp6 (
327 Private
->BootFileUri
,
328 Private
->BootFileUriParser
,
332 if (EFI_ERROR (Status
)) {
334 // The Http server address is expressed by Name Ip, so perform DNS resolution
336 Status
= HttpUrlGetHostName (
337 Private
->BootFileUri
,
338 Private
->BootFileUriParser
,
341 if (EFI_ERROR (Status
)) {
345 HostNameStr
= AllocateZeroPool ((AsciiStrLen (HostName
) + 1) * sizeof (CHAR16
));
346 if (HostNameStr
== NULL
) {
347 Status
= EFI_OUT_OF_RESOURCES
;
351 AsciiStrToUnicodeStr (HostName
, HostNameStr
);
352 Status
= HttpBootDns (Private
, HostNameStr
, &IpAddr
);
353 FreePool (HostNameStr
);
354 if (EFI_ERROR (Status
)) {
359 CopyMem (&Private
->ServerIp
.v6
, &IpAddr
, sizeof (EFI_IPv6_ADDRESS
));
362 // register the IPv6 gateway address to the network device.
364 Status
= HttpBootSetIp6Gateway (Private
);
365 if (EFI_ERROR (Status
)) {
370 // Extract the port from URL, and use default HTTP port 80 if not provided.
372 Status
= HttpUrlGetPort (
373 Private
->BootFileUri
,
374 Private
->BootFileUriParser
,
377 if (EFI_ERROR (Status
) || Private
->Port
== 0) {
382 // All boot informations are valid here.
384 AsciiPrint ("\n URI: %a", Private
->BootFileUri
);
386 // Update the device path to include the IP and boot URI information.
388 Status
= HttpBootUpdateDevicePath (Private
);
392 if (HostName
!= NULL
) {
401 Discover all the boot information for boot file.
403 @param[in, out] Private The pointer to the driver's private data.
405 @retval EFI_SUCCESS Successfully obtained all the boot information .
406 @retval Others Failed to retrieve the boot information.
410 HttpBootDiscoverBootInfo (
411 IN OUT HTTP_BOOT_PRIVATE_DATA
*Private
417 // Start D.O.R.A/S.A.R.R exchange to acquire station ip address and
418 // other Http boot information.
420 Status
= HttpBootDhcp (Private
);
421 if (EFI_ERROR (Status
)) {
425 if (!Private
->UsingIpv6
) {
426 Status
= HttpBootDhcp4ExtractUriInfo (Private
);
428 Status
= HttpBootDhcp6ExtractUriInfo (Private
);
435 Create a HttpIo instance for the file download.
437 @param[in] Private The pointer to the driver's private data.
439 @retval EFI_SUCCESS Successfully created.
440 @retval Others Failed to create HttpIo.
444 HttpBootCreateHttpIo (
445 IN HTTP_BOOT_PRIVATE_DATA
*Private
448 HTTP_IO_CONFIG_DATA ConfigData
;
450 EFI_HANDLE ImageHandle
;
452 ASSERT (Private
!= NULL
);
454 ZeroMem (&ConfigData
, sizeof (HTTP_IO_CONFIG_DATA
));
455 if (!Private
->UsingIpv6
) {
456 ConfigData
.Config4
.HttpVersion
= HttpVersion11
;
457 ConfigData
.Config4
.RequestTimeOut
= HTTP_BOOT_REQUEST_TIMEOUT
;
458 IP4_COPY_ADDRESS (&ConfigData
.Config4
.LocalIp
, &Private
->StationIp
.v4
);
459 IP4_COPY_ADDRESS (&ConfigData
.Config4
.SubnetMask
, &Private
->SubnetMask
.v4
);
460 ImageHandle
= Private
->Ip4Nic
->ImageHandle
;
462 ConfigData
.Config6
.HttpVersion
= HttpVersion11
;
463 ConfigData
.Config6
.RequestTimeOut
= HTTP_BOOT_REQUEST_TIMEOUT
;
464 IP6_COPY_ADDRESS (&ConfigData
.Config6
.LocalIp
, &Private
->StationIp
.v6
);
465 ImageHandle
= Private
->Ip6Nic
->ImageHandle
;
468 Status
= HttpIoCreateIo (
471 Private
->UsingIpv6
? IP_VERSION_6
: IP_VERSION_4
,
475 if (EFI_ERROR (Status
)) {
479 Private
->HttpCreated
= TRUE
;
484 Release all the resource of a cache item.
486 @param[in] Cache The pointer to the cache item.
491 IN HTTP_BOOT_CACHE_CONTENT
*Cache
496 LIST_ENTRY
*NextEntry
;
497 HTTP_BOOT_ENTITY_DATA
*EntityData
;
501 // Free the request data
503 if (Cache
->RequestData
!= NULL
) {
504 if (Cache
->RequestData
->Url
!= NULL
) {
505 FreePool (Cache
->RequestData
->Url
);
507 FreePool (Cache
->RequestData
);
511 // Free the response header
513 if (Cache
->ResponseData
!= NULL
) {
514 if (Cache
->ResponseData
->Headers
!= NULL
) {
515 for (Index
= 0; Index
< Cache
->ResponseData
->HeaderCount
; Index
++) {
516 FreePool (Cache
->ResponseData
->Headers
[Index
].FieldName
);
517 FreePool (Cache
->ResponseData
->Headers
[Index
].FieldValue
);
519 FreePool (Cache
->ResponseData
->Headers
);
524 // Free the response body
526 NET_LIST_FOR_EACH_SAFE (Entry
, NextEntry
, &Cache
->EntityDataList
) {
527 EntityData
= NET_LIST_USER_STRUCT (Entry
, HTTP_BOOT_ENTITY_DATA
, Link
);
528 if (EntityData
->Block
!= NULL
) {
529 FreePool (EntityData
->Block
);
531 RemoveEntryList (&EntityData
->Link
);
532 FreePool (EntityData
);
540 Clean up all cached data.
542 @param[in] Private The pointer to the driver's private data.
546 HttpBootFreeCacheList (
547 IN HTTP_BOOT_PRIVATE_DATA
*Private
551 LIST_ENTRY
*NextEntry
;
552 HTTP_BOOT_CACHE_CONTENT
*Cache
;
554 NET_LIST_FOR_EACH_SAFE (Entry
, NextEntry
, &Private
->CacheList
) {
555 Cache
= NET_LIST_USER_STRUCT (Entry
, HTTP_BOOT_CACHE_CONTENT
, Link
);
556 RemoveEntryList (&Cache
->Link
);
557 HttpBootFreeCache (Cache
);
562 Get the file content from cached data.
564 @param[in] Private The pointer to the driver's private data.
565 @param[in] Uri Uri of the file to be retrieved from cache.
566 @param[in, out] BufferSize On input the size of Buffer in bytes. On output with a return
567 code of EFI_SUCCESS, the amount of data transferred to
568 Buffer. On output with a return code of EFI_BUFFER_TOO_SMALL,
569 the size of Buffer required to retrieve the requested file.
570 @param[out] Buffer The memory buffer to transfer the file to. IF Buffer is NULL,
571 then the size of the requested file is returned in
574 @retval EFI_SUCCESS Successfully created.
575 @retval Others Failed to create HttpIo.
579 HttpBootGetFileFromCache (
580 IN HTTP_BOOT_PRIVATE_DATA
*Private
,
582 IN OUT UINTN
*BufferSize
,
588 HTTP_BOOT_CACHE_CONTENT
*Cache
;
589 HTTP_BOOT_ENTITY_DATA
*EntityData
;
592 if (Uri
== NULL
|| BufferSize
== 0 || Buffer
== NULL
) {
593 return EFI_INVALID_PARAMETER
;
596 NET_LIST_FOR_EACH (Entry
, &Private
->CacheList
) {
597 Cache
= NET_LIST_USER_STRUCT (Entry
, HTTP_BOOT_CACHE_CONTENT
, Link
);
599 // Compare the URI to see whether we already have a cache for this file.
601 if ((Cache
->RequestData
!= NULL
) &&
602 (Cache
->RequestData
->Url
!= NULL
) &&
603 (StrCmp (Uri
, Cache
->RequestData
->Url
) == 0))
606 // Hit cache, check buffer size.
608 if (*BufferSize
< Cache
->EntityLength
) {
609 *BufferSize
= Cache
->EntityLength
;
610 return EFI_BUFFER_TOO_SMALL
;
614 // Fill data to buffer.
617 NET_LIST_FOR_EACH (Entry2
, &Cache
->EntityDataList
) {
618 EntityData
= NET_LIST_USER_STRUCT (Entry2
, HTTP_BOOT_ENTITY_DATA
, Link
);
619 if (*BufferSize
> CopyedSize
) {
622 EntityData
->DataStart
,
623 MIN (EntityData
->DataLength
, *BufferSize
- CopyedSize
)
625 CopyedSize
+= MIN (EntityData
->DataLength
, *BufferSize
- CopyedSize
);
628 *BufferSize
= CopyedSize
;
633 return EFI_NOT_FOUND
;
637 A callback function to intercept events during message parser.
639 This function will be invoked during HttpParseMessageBody() with various events type. An error
640 return status of the callback function will cause the HttpParseMessageBody() aborted.
642 @param[in] EventType Event type of this callback call.
643 @param[in] Data A pointer to data buffer.
644 @param[in] Length Length in bytes of the Data.
645 @param[in] Context Callback context set by HttpInitMsgParser().
647 @retval EFI_SUCCESS Continue to parser the message body.
648 @retval Others Abort the parse.
653 HttpBootGetBootFileCallback (
654 IN HTTP_BODY_PARSE_EVENT EventType
,
660 HTTP_BOOT_CALLBACK_DATA
*CallbackData
;
661 HTTP_BOOT_ENTITY_DATA
*NewEntityData
;
664 // We only care about the entity data.
666 if (EventType
!= BodyParseEventOnData
) {
670 CallbackData
= (HTTP_BOOT_CALLBACK_DATA
*) Context
;
672 // Copy data if caller has provided a buffer.
674 if (CallbackData
->BufferSize
> CallbackData
->CopyedSize
) {
676 CallbackData
->Buffer
+ CallbackData
->CopyedSize
,
678 MIN (Length
, CallbackData
->BufferSize
- CallbackData
->CopyedSize
)
680 CallbackData
->CopyedSize
+= MIN (Length
, CallbackData
->BufferSize
- CallbackData
->CopyedSize
);
684 // The caller doesn't provide a buffer, save the block into cache list.
686 if (CallbackData
->Cache
!= NULL
) {
687 NewEntityData
= AllocatePool (sizeof (HTTP_BOOT_ENTITY_DATA
));
688 if (NewEntityData
== NULL
) {
689 return EFI_OUT_OF_RESOURCES
;
691 if (CallbackData
->NewBlock
) {
692 NewEntityData
->Block
= CallbackData
->Block
;
693 CallbackData
->Block
= NULL
;
695 NewEntityData
->DataLength
= Length
;
696 NewEntityData
->DataStart
= (UINT8
*) Data
;
697 InsertTailList (&CallbackData
->Cache
->EntityDataList
, &NewEntityData
->Link
);
703 This function download the boot file by using UEFI HTTP protocol.
705 @param[in] Private The pointer to the driver's private data.
706 @param[in] HeaderOnly Only request the response header, it could save a lot of time if
707 the caller only want to know the size of the requested file.
708 @param[in, out] BufferSize On input the size of Buffer in bytes. On output with a return
709 code of EFI_SUCCESS, the amount of data transferred to
710 Buffer. On output with a return code of EFI_BUFFER_TOO_SMALL,
711 the size of Buffer required to retrieve the requested file.
712 @param[out] Buffer The memory buffer to transfer the file to. IF Buffer is NULL,
713 then the size of the requested file is returned in
716 @retval EFI_SUCCESS The file was loaded.
717 @retval EFI_INVALID_PARAMETER BufferSize is NULL or Buffer Size is not NULL but Buffer is NULL.
718 @retval EFI_OUT_OF_RESOURCES Could not allocate needed resources
719 @retval EFI_BUFFER_TOO_SMALL The BufferSize is too small to read the current directory entry.
720 BufferSize has been updated with the size needed to complete
722 @retval Others Unexpected error happened.
726 HttpBootGetBootFile (
727 IN HTTP_BOOT_PRIVATE_DATA
*Private
,
728 IN BOOLEAN HeaderOnly
,
729 IN OUT UINTN
*BufferSize
,
734 EFI_HTTP_STATUS_CODE StatusCode
;
736 EFI_HTTP_REQUEST_DATA
*RequestData
;
737 HTTP_IO_RESPONSE_DATA
*ResponseData
;
738 HTTP_IO_RESPONSE_DATA ResponseBody
;
740 HTTP_IO_HEADER
*HttpIoHeader
;
742 HTTP_BOOT_CALLBACK_DATA Context
;
744 HTTP_BOOT_CACHE_CONTENT
*Cache
;
747 BOOLEAN IdentityMode
;
750 ASSERT (Private
!= NULL
);
751 ASSERT (Private
->HttpCreated
);
753 if (BufferSize
== NULL
) {
754 return EFI_INVALID_PARAMETER
;
757 if (*BufferSize
!= 0 && Buffer
== NULL
) {
758 return EFI_INVALID_PARAMETER
;
762 // First, check whether we already cached the requested Uri.
764 Url
= AllocatePool ((AsciiStrLen (Private
->BootFileUri
) + 1) * sizeof (CHAR16
));
766 return EFI_OUT_OF_RESOURCES
;
768 AsciiStrToUnicodeStr (Private
->BootFileUri
, Url
);
770 Status
= HttpBootGetFileFromCache (Private
, Url
, BufferSize
, Buffer
);
771 if (Status
!= EFI_NOT_FOUND
) {
778 // Not found in cache, try to download it through HTTP.
782 // 1. Create a temp cache item for the requested URI if caller doesn't provide buffer.
785 if ((!HeaderOnly
) && (*BufferSize
== 0)) {
786 Cache
= AllocateZeroPool (sizeof (HTTP_BOOT_CACHE_CONTENT
));
788 Status
= EFI_OUT_OF_RESOURCES
;
791 InitializeListHead (&Cache
->EntityDataList
);
795 // 2. Send HTTP request message.
799 // 2.1 Build HTTP header for the request, 3 header is needed to download a boot file:
804 HttpIoHeader
= HttpBootCreateHeader (3);
805 if (HttpIoHeader
== NULL
) {
806 Status
= EFI_OUT_OF_RESOURCES
;
811 // Add HTTP header field 1: Host
814 Status
= HttpUrlGetHostName (
815 Private
->BootFileUri
,
816 Private
->BootFileUriParser
,
819 if (EFI_ERROR (Status
)) {
822 Status
= HttpBootSetHeader (
828 if (EFI_ERROR (Status
)) {
833 // Add HTTP header field 2: Accept
835 Status
= HttpBootSetHeader (
840 if (EFI_ERROR (Status
)) {
845 // Add HTTP header field 3: User-Agent
847 Status
= HttpBootSetHeader (
849 HTTP_HEADER_USER_AGENT
,
850 HTTP_USER_AGENT_EFI_HTTP_BOOT
852 if (EFI_ERROR (Status
)) {
857 // 2.2 Build the rest of HTTP request info.
859 RequestData
= AllocatePool (sizeof (EFI_HTTP_REQUEST_DATA
));
860 if (RequestData
== NULL
) {
861 Status
= EFI_OUT_OF_RESOURCES
;
864 RequestData
->Method
= HeaderOnly
? HttpMethodHead
: HttpMethodGet
;
865 RequestData
->Url
= Url
;
866 if (RequestData
->Url
== NULL
) {
867 Status
= EFI_OUT_OF_RESOURCES
;
870 AsciiStrToUnicodeStr (Private
->BootFileUri
, RequestData
->Url
);
873 // 2.3 Record the request info in a temp cache item.
876 Cache
->RequestData
= RequestData
;
880 // 2.4 Send out the request to HTTP server.
882 HttpIo
= &Private
->HttpIo
;
883 Status
= HttpIoSendRequest (
886 HttpIoHeader
->HeaderCount
,
887 HttpIoHeader
->Headers
,
891 if (EFI_ERROR (Status
)) {
896 // 3. Receive HTTP response message.
900 // 3.1 First step, use zero BodyLength to only receive the response headers.
902 ResponseData
= AllocateZeroPool (sizeof(HTTP_IO_RESPONSE_DATA
));
903 if (ResponseData
== NULL
) {
904 Status
= EFI_OUT_OF_RESOURCES
;
907 Status
= HttpIoRecvResponse (
912 if (EFI_ERROR (Status
) || EFI_ERROR (ResponseData
->Status
)) {
913 if (EFI_ERROR (ResponseData
->Status
)) {
914 StatusCode
= HttpIo
->RspToken
.Message
->Data
.Response
->StatusCode
;
915 HttpBootPrintErrorMessage (StatusCode
);
916 Status
= ResponseData
->Status
;
922 // 3.2 Cache the response header.
925 Cache
->ResponseData
= ResponseData
;
929 // 3.3 Init a message-body parser from the header information.
932 Context
.NewBlock
= FALSE
;
933 Context
.Block
= NULL
;
934 Context
.CopyedSize
= 0;
935 Context
.Buffer
= Buffer
;
936 Context
.BufferSize
= *BufferSize
;
937 Context
.Cache
= Cache
;
938 Status
= HttpInitMsgParser (
939 HeaderOnly
? HttpMethodHead
: HttpMethodGet
,
940 ResponseData
->Response
.StatusCode
,
941 ResponseData
->HeaderCount
,
942 ResponseData
->Headers
,
943 HttpBootGetBootFileCallback
,
947 if (EFI_ERROR (Status
)) {
952 // 3.4 Continue to receive and parse message-body if needed.
957 // 3.4.1, check whether we are in identity transfer-coding.
960 Status
= HttpGetEntityLength (Parser
, &ContentLength
);
961 if (!EFI_ERROR (Status
)) {
964 IdentityMode
= FALSE
;
968 // 3.4.2, start the message-body download, the identity and chunked transfer-coding
969 // is handled in different path here.
971 ZeroMem (&ResponseBody
, sizeof (HTTP_IO_RESPONSE_DATA
));
974 // In identity transfer-coding there is no need to parse the message body,
975 // just download the message body to the user provided buffer directly.
978 while (ReceivedSize
< ContentLength
) {
979 ResponseBody
.Body
= (CHAR8
*) Buffer
+ ReceivedSize
;
980 ResponseBody
.BodyLength
= *BufferSize
- ReceivedSize
;
981 Status
= HttpIoRecvResponse (
986 if (EFI_ERROR (Status
)) {
989 ReceivedSize
+= ResponseBody
.BodyLength
;
993 // In "chunked" transfer-coding mode, so we need to parse the received
994 // data to get the real entity content.
997 while (!HttpIsMessageComplete (Parser
)) {
999 // Allocate a buffer in Block to hold the message-body.
1000 // If caller provides a buffer, this Block will be reused in every HttpIoRecvResponse().
1001 // Otherwise a buffer, the buffer in Block will be cached and we should allocate a new before
1002 // every HttpIoRecvResponse().
1004 if (Block
== NULL
|| Context
.BufferSize
== 0) {
1005 Block
= AllocatePool (HTTP_BOOT_BLOCK_SIZE
);
1006 if (Block
== NULL
) {
1007 Status
= EFI_OUT_OF_RESOURCES
;
1010 Context
.NewBlock
= TRUE
;
1011 Context
.Block
= Block
;
1013 Context
.NewBlock
= FALSE
;
1016 ResponseBody
.Body
= (CHAR8
*) Block
;
1017 ResponseBody
.BodyLength
= HTTP_BOOT_BLOCK_SIZE
;
1018 Status
= HttpIoRecvResponse (
1023 if (EFI_ERROR (Status
)) {
1028 // Parse the new received block of the message-body, the block will be saved in cache.
1030 Status
= HttpParseMessageBody (
1032 ResponseBody
.BodyLength
,
1035 if (EFI_ERROR (Status
)) {
1043 // 3.5 Message-body receive & parse is completed, we should be able to get the file size now.
1045 Status
= HttpGetEntityLength (Parser
, &ContentLength
);
1046 if (EFI_ERROR (Status
)) {
1050 if (*BufferSize
< ContentLength
) {
1051 Status
= EFI_BUFFER_TOO_SMALL
;
1053 *BufferSize
= ContentLength
;
1056 // 4. Save the cache item to driver's cache list and return.
1058 if (Cache
!= NULL
) {
1059 Cache
->EntityLength
= ContentLength
;
1060 InsertTailList (&Private
->CacheList
, &Cache
->Link
);
1063 if (Parser
!= NULL
) {
1064 HttpFreeMsgParser (Parser
);
1070 if (Parser
!= NULL
) {
1071 HttpFreeMsgParser (Parser
);
1073 if (Context
.Block
!= NULL
) {
1074 FreePool (Context
.Block
);
1076 HttpBootFreeCache (Cache
);
1079 if (ResponseData
!= NULL
) {
1080 FreePool (ResponseData
);
1083 if (RequestData
!= NULL
) {
1084 FreePool (RequestData
);
1087 HttpBootFreeHeader (HttpIoHeader
);
1089 if (Cache
!= NULL
) {