2 Implementation of the boot file download function.
4 Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials are licensed and made available under
6 the terms and conditions of the BSD License that accompanies this distribution.
7 The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php.
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
15 #include "HttpBootDxe.h"
18 Update the IP and URL device path node to include the boot resource information.
20 @param[in] Private The pointer to the driver's private data.
22 @retval EFI_SUCCESS Device patch successfully updated.
23 @retval EFI_OUT_OF_RESOURCES Could not allocate needed resources.
24 @retval Others Unexpected error happened.
28 HttpBootUpdateDevicePath (
29 IN HTTP_BOOT_PRIVATE_DATA
*Private
33 EFI_DEVICE_PATH_PROTOCOL
*TmpDevicePath
;
34 EFI_DEVICE_PATH_PROTOCOL
*NewDevicePath
;
41 // Update the IP node with DHCP assigned information.
43 if (!Private
->UsingIpv6
) {
44 Node
= AllocateZeroPool (sizeof (IPv4_DEVICE_PATH
));
46 return EFI_OUT_OF_RESOURCES
;
48 Node
->Ipv4
.Header
.Type
= MESSAGING_DEVICE_PATH
;
49 Node
->Ipv4
.Header
.SubType
= MSG_IPv4_DP
;
50 SetDevicePathNodeLength (Node
, sizeof (IPv4_DEVICE_PATH
));
51 CopyMem (&Node
->Ipv4
.LocalIpAddress
, &Private
->StationIp
, sizeof (EFI_IPv4_ADDRESS
));
52 Node
->Ipv4
.RemotePort
= Private
->Port
;
53 Node
->Ipv4
.Protocol
= EFI_IP_PROTO_TCP
;
54 Node
->Ipv4
.StaticIpAddress
= FALSE
;
55 CopyMem (&Node
->Ipv4
.GatewayIpAddress
, &Private
->GatewayIp
, sizeof (EFI_IPv4_ADDRESS
));
56 CopyMem (&Node
->Ipv4
.SubnetMask
, &Private
->SubnetMask
, sizeof (EFI_IPv4_ADDRESS
));
58 Node
= AllocateZeroPool (sizeof (IPv6_DEVICE_PATH
));
60 return EFI_OUT_OF_RESOURCES
;
62 Node
->Ipv6
.Header
.Type
= MESSAGING_DEVICE_PATH
;
63 Node
->Ipv6
.Header
.SubType
= MSG_IPv6_DP
;
64 SetDevicePathNodeLength (Node
, sizeof (IPv6_DEVICE_PATH
));
65 Node
->Ipv6
.PrefixLength
= IP6_PREFIX_LENGTH
;
66 Node
->Ipv6
.RemotePort
= Private
->Port
;
67 Node
->Ipv6
.Protocol
= EFI_IP_PROTO_TCP
;
68 Node
->Ipv6
.IpAddressOrigin
= 0;
69 CopyMem (&Node
->Ipv6
.LocalIpAddress
, &Private
->StationIp
.v6
, sizeof (EFI_IPv6_ADDRESS
));
70 CopyMem (&Node
->Ipv6
.RemoteIpAddress
, &Private
->ServerIp
.v6
, sizeof (EFI_IPv6_ADDRESS
));
71 CopyMem (&Node
->Ipv6
.GatewayIpAddress
, &Private
->GatewayIp
.v6
, sizeof (EFI_IPv6_ADDRESS
));
74 TmpDevicePath
= AppendDevicePathNode (Private
->ParentDevicePath
, (EFI_DEVICE_PATH_PROTOCOL
*) Node
);
76 if (TmpDevicePath
== NULL
) {
77 return EFI_OUT_OF_RESOURCES
;
81 // Update the URI node with the boot file URI.
83 Length
= sizeof (EFI_DEVICE_PATH_PROTOCOL
) + AsciiStrSize (Private
->BootFileUri
);
84 Node
= AllocatePool (Length
);
86 FreePool (TmpDevicePath
);
87 return EFI_OUT_OF_RESOURCES
;
89 Node
->DevPath
.Type
= MESSAGING_DEVICE_PATH
;
90 Node
->DevPath
.SubType
= MSG_URI_DP
;
91 SetDevicePathNodeLength (Node
, Length
);
92 CopyMem ((UINT8
*) Node
+ sizeof (EFI_DEVICE_PATH_PROTOCOL
), Private
->BootFileUri
, AsciiStrSize (Private
->BootFileUri
));
94 NewDevicePath
= AppendDevicePathNode (TmpDevicePath
, (EFI_DEVICE_PATH_PROTOCOL
*) Node
);
96 FreePool (TmpDevicePath
);
97 if (NewDevicePath
== NULL
) {
98 return EFI_OUT_OF_RESOURCES
;
101 if (!Private
->UsingIpv6
) {
103 // Reinstall the device path protocol of the child handle.
105 Status
= gBS
->ReinstallProtocolInterface (
106 Private
->Ip4Nic
->Controller
,
107 &gEfiDevicePathProtocolGuid
,
108 Private
->Ip4Nic
->DevicePath
,
111 if (EFI_ERROR (Status
)) {
115 FreePool (Private
->Ip4Nic
->DevicePath
);
116 Private
->Ip4Nic
->DevicePath
= NewDevicePath
;
119 // Reinstall the device path protocol of the child handle.
121 Status
= gBS
->ReinstallProtocolInterface (
122 Private
->Ip6Nic
->Controller
,
123 &gEfiDevicePathProtocolGuid
,
124 Private
->Ip6Nic
->DevicePath
,
127 if (EFI_ERROR (Status
)) {
130 FreePool (Private
->Ip6Nic
->DevicePath
);
131 Private
->Ip6Nic
->DevicePath
= NewDevicePath
;
138 Parse the boot file URI information from the selected Dhcp4 offer packet.
140 @param[in] Private The pointer to the driver's private data.
142 @retval EFI_SUCCESS Successfully parsed out all the boot information.
143 @retval Others Failed to parse out the boot information.
147 HttpBootDhcp4ExtractUriInfo (
148 IN HTTP_BOOT_PRIVATE_DATA
*Private
151 HTTP_BOOT_DHCP4_PACKET_CACHE
*SelectOffer
;
152 HTTP_BOOT_DHCP4_PACKET_CACHE
*HttpOffer
;
155 EFI_DHCP4_PACKET_OPTION
*Option
;
158 ASSERT (Private
!= NULL
);
159 ASSERT (Private
->SelectIndex
!= 0);
160 SelectIndex
= Private
->SelectIndex
- 1;
161 ASSERT (SelectIndex
< HTTP_BOOT_OFFER_MAX_NUM
);
163 Status
= EFI_SUCCESS
;
166 // SelectOffer contains the IP address configuration and name server configuration.
167 // HttpOffer contains the boot file URL.
169 SelectOffer
= &Private
->OfferBuffer
[SelectIndex
].Dhcp4
;
170 if ((SelectOffer
->OfferType
== HttpOfferTypeDhcpIpUri
) || (SelectOffer
->OfferType
== HttpOfferTypeDhcpNameUriDns
)) {
171 HttpOffer
= SelectOffer
;
173 ASSERT (Private
->SelectProxyType
!= HttpOfferTypeMax
);
174 ProxyIndex
= Private
->OfferIndex
[Private
->SelectProxyType
][0];
175 HttpOffer
= &Private
->OfferBuffer
[ProxyIndex
].Dhcp4
;
179 // Configure the default DNS server if server assigned.
181 if ((SelectOffer
->OfferType
== HttpOfferTypeDhcpNameUriDns
) || (SelectOffer
->OfferType
== HttpOfferTypeDhcpDns
)) {
182 Option
= SelectOffer
->OptList
[HTTP_BOOT_DHCP4_TAG_INDEX_DNS_SERVER
];
183 ASSERT (Option
!= NULL
);
184 Status
= HttpBootRegisterIp4Dns (
189 if (EFI_ERROR (Status
)) {
195 // Extract the port from URL, and use default HTTP port 80 if not provided.
197 Status
= HttpUrlGetPort (
198 (CHAR8
*) HttpOffer
->OptList
[HTTP_BOOT_DHCP4_TAG_INDEX_BOOTFILE
]->Data
,
199 HttpOffer
->UriParser
,
202 if (EFI_ERROR (Status
) || Private
->Port
== 0) {
207 // Record the URI of boot file from the selected HTTP offer.
209 Private
->BootFileUriParser
= HttpOffer
->UriParser
;
210 Private
->BootFileUri
= (CHAR8
*) HttpOffer
->OptList
[HTTP_BOOT_DHCP4_TAG_INDEX_BOOTFILE
]->Data
;
214 // All boot informations are valid here.
216 AsciiPrint ("\n URI: %a", Private
->BootFileUri
);
219 // Update the device path to include the IP and boot URI information.
221 Status
= HttpBootUpdateDevicePath (Private
);
227 Parse the boot file URI information from the selected Dhcp6 offer packet.
229 @param[in] Private The pointer to the driver's private data.
231 @retval EFI_SUCCESS Successfully parsed out all the boot information.
232 @retval Others Failed to parse out the boot information.
236 HttpBootDhcp6ExtractUriInfo (
237 IN HTTP_BOOT_PRIVATE_DATA
*Private
240 HTTP_BOOT_DHCP6_PACKET_CACHE
*SelectOffer
;
241 HTTP_BOOT_DHCP6_PACKET_CACHE
*HttpOffer
;
244 EFI_DHCP6_PACKET_OPTION
*Option
;
245 EFI_IPv6_ADDRESS IpAddr
;
250 ASSERT (Private
!= NULL
);
251 ASSERT (Private
->SelectIndex
!= 0);
252 SelectIndex
= Private
->SelectIndex
- 1;
253 ASSERT (SelectIndex
< HTTP_BOOT_OFFER_MAX_NUM
);
255 Status
= EFI_SUCCESS
;
258 // SelectOffer contains the IP address configuration and name server configuration.
259 // HttpOffer contains the boot file URL.
261 SelectOffer
= &Private
->OfferBuffer
[SelectIndex
].Dhcp6
;
262 if ((SelectOffer
->OfferType
== HttpOfferTypeDhcpIpUri
) || (SelectOffer
->OfferType
== HttpOfferTypeDhcpNameUriDns
)) {
263 HttpOffer
= SelectOffer
;
265 ASSERT (Private
->SelectProxyType
!= HttpOfferTypeMax
);
266 ProxyIndex
= Private
->OfferIndex
[Private
->SelectProxyType
][0];
267 HttpOffer
= &Private
->OfferBuffer
[ProxyIndex
].Dhcp6
;
271 // Set the Local station address to IP layer.
273 Status
= HttpBootSetIp6Address (Private
);
274 if (EFI_ERROR (Status
)) {
279 // Configure the default DNS server if server assigned.
281 if ((SelectOffer
->OfferType
== HttpOfferTypeDhcpNameUriDns
) || (SelectOffer
->OfferType
== HttpOfferTypeDhcpDns
)) {
282 Option
= SelectOffer
->OptList
[HTTP_BOOT_DHCP6_IDX_DNS_SERVER
];
283 ASSERT (Option
!= NULL
);
284 Status
= HttpBootSetIp6Dns (
286 HTONS (Option
->OpLen
),
289 if (EFI_ERROR (Status
)) {
295 // Extract the HTTP server Ip frome URL. This is used to Check route table
296 // whether can send message to HTTP Server Ip through the GateWay.
298 Status
= HttpUrlGetIp6 (
299 (CHAR8
*) HttpOffer
->OptList
[HTTP_BOOT_DHCP6_IDX_BOOT_FILE_URL
]->Data
,
300 HttpOffer
->UriParser
,
304 if (EFI_ERROR (Status
)) {
306 // The Http server address is expressed by Name Ip, so perform DNS resolution
308 Status
= HttpUrlGetHostName (
309 (CHAR8
*) HttpOffer
->OptList
[HTTP_BOOT_DHCP6_IDX_BOOT_FILE_URL
]->Data
,
310 HttpOffer
->UriParser
,
313 if (EFI_ERROR (Status
)) {
317 HostNameStr
= AllocateZeroPool ((AsciiStrLen (HostName
) + 1) * sizeof (CHAR16
));
318 if (HostNameStr
== NULL
) {
319 Status
= EFI_OUT_OF_RESOURCES
;
323 AsciiStrToUnicodeStr (HostName
, HostNameStr
);
324 Status
= HttpBootDns (Private
, HostNameStr
, &IpAddr
);
325 FreePool (HostNameStr
);
326 if (EFI_ERROR (Status
)) {
331 CopyMem (&Private
->ServerIp
.v6
, &IpAddr
, sizeof (EFI_IPv6_ADDRESS
));
334 // register the IPv6 gateway address to the network device.
336 Status
= HttpBootSetIp6Gateway (Private
);
337 if (EFI_ERROR (Status
)) {
342 // Extract the port from URL, and use default HTTP port 80 if not provided.
344 Status
= HttpUrlGetPort (
345 (CHAR8
*) HttpOffer
->OptList
[HTTP_BOOT_DHCP6_IDX_BOOT_FILE_URL
]->Data
,
346 HttpOffer
->UriParser
,
349 if (EFI_ERROR (Status
) || Private
->Port
== 0) {
354 // Record the URI of boot file from the selected HTTP offer.
356 Private
->BootFileUriParser
= HttpOffer
->UriParser
;
357 Private
->BootFileUri
= (CHAR8
*) HttpOffer
->OptList
[HTTP_BOOT_DHCP6_IDX_BOOT_FILE_URL
]->Data
;
361 // All boot informations are valid here.
363 AsciiPrint ("\n URI: %a", Private
->BootFileUri
);
365 // Update the device path to include the IP and boot URI information.
367 Status
= HttpBootUpdateDevicePath (Private
);
371 if (HostName
!= NULL
) {
380 Discover all the boot information for boot file.
382 @param[in, out] Private The pointer to the driver's private data.
384 @retval EFI_SUCCESS Successfully obtained all the boot information .
385 @retval Others Failed to retrieve the boot information.
389 HttpBootDiscoverBootInfo (
390 IN OUT HTTP_BOOT_PRIVATE_DATA
*Private
396 // Start D.O.R.A/S.A.R.R exchange to acquire station ip address and
397 // other Http boot information.
399 Status
= HttpBootDhcp (Private
);
400 if (EFI_ERROR (Status
)) {
404 if (!Private
->UsingIpv6
) {
405 Status
= HttpBootDhcp4ExtractUriInfo (Private
);
407 Status
= HttpBootDhcp6ExtractUriInfo (Private
);
414 Create a HttpIo instance for the file download.
416 @param[in] Private The pointer to the driver's private data.
418 @retval EFI_SUCCESS Successfully created.
419 @retval Others Failed to create HttpIo.
423 HttpBootCreateHttpIo (
424 IN HTTP_BOOT_PRIVATE_DATA
*Private
427 HTTP_IO_CONFIG_DATA ConfigData
;
430 ASSERT (Private
!= NULL
);
432 ZeroMem (&ConfigData
, sizeof (HTTP_IO_CONFIG_DATA
));
433 if (!Private
->UsingIpv6
) {
434 ConfigData
.Config4
.HttpVersion
= HttpVersion11
;
435 ConfigData
.Config4
.RequestTimeOut
= HTTP_BOOT_REQUEST_TIMEOUT
;
436 IP4_COPY_ADDRESS (&ConfigData
.Config4
.LocalIp
, &Private
->StationIp
.v4
);
437 IP4_COPY_ADDRESS (&ConfigData
.Config4
.SubnetMask
, &Private
->SubnetMask
.v4
);
439 ConfigData
.Config6
.HttpVersion
= HttpVersion11
;
440 ConfigData
.Config6
.RequestTimeOut
= HTTP_BOOT_REQUEST_TIMEOUT
;
441 IP6_COPY_ADDRESS (&ConfigData
.Config6
.LocalIp
, &Private
->StationIp
.v6
);
444 Status
= HttpIoCreateIo (
447 Private
->UsingIpv6
? IP_VERSION_6
: IP_VERSION_4
,
451 if (EFI_ERROR (Status
)) {
455 Private
->HttpCreated
= TRUE
;
460 Get the file content from cached data.
462 @param[in] Private The pointer to the driver's private data.
463 @param[in] Uri Uri of the file to be retrieved from cache.
464 @param[in, out] BufferSize On input the size of Buffer in bytes. On output with a return
465 code of EFI_SUCCESS, the amount of data transferred to
466 Buffer. On output with a return code of EFI_BUFFER_TOO_SMALL,
467 the size of Buffer required to retrieve the requested file.
468 @param[out] Buffer The memory buffer to transfer the file to. IF Buffer is NULL,
469 then the size of the requested file is returned in
472 @retval EFI_SUCCESS Successfully created.
473 @retval Others Failed to create HttpIo.
477 HttpBootGetFileFromCache (
478 IN HTTP_BOOT_PRIVATE_DATA
*Private
,
480 IN OUT UINTN
*BufferSize
,
486 HTTP_BOOT_CACHE_CONTENT
*Cache
;
487 HTTP_BOOT_ENTITY_DATA
*EntityData
;
490 if (Uri
== NULL
|| BufferSize
== 0 || Buffer
== NULL
) {
491 return EFI_INVALID_PARAMETER
;
494 NET_LIST_FOR_EACH (Entry
, &Private
->CacheList
) {
495 Cache
= NET_LIST_USER_STRUCT (Entry
, HTTP_BOOT_CACHE_CONTENT
, Link
);
497 // Compare the URI to see whether we already have a cache for this file.
499 if ((Cache
->RequestData
!= NULL
) &&
500 (Cache
->RequestData
->Url
!= NULL
) &&
501 (StrCmp (Uri
, Cache
->RequestData
->Url
) == 0))
504 // Hit cache, check buffer size.
506 if (*BufferSize
< Cache
->EntityLength
) {
507 *BufferSize
= Cache
->EntityLength
;
508 return EFI_BUFFER_TOO_SMALL
;
512 // Fill data to buffer.
515 NET_LIST_FOR_EACH (Entry2
, &Cache
->EntityDataList
) {
516 EntityData
= NET_LIST_USER_STRUCT (Entry2
, HTTP_BOOT_ENTITY_DATA
, Link
);
517 if (*BufferSize
> CopyedSize
) {
520 EntityData
->DataStart
,
521 MIN (EntityData
->DataLength
, *BufferSize
- CopyedSize
)
523 CopyedSize
+= MIN (EntityData
->DataLength
, *BufferSize
- CopyedSize
);
526 *BufferSize
= CopyedSize
;
531 return EFI_NOT_FOUND
;
535 Release all the resource of a cache item.
537 @param[in] Cache The pointer to the cache item.
542 IN HTTP_BOOT_CACHE_CONTENT
*Cache
547 LIST_ENTRY
*NextEntry
;
548 HTTP_BOOT_ENTITY_DATA
*EntityData
;
552 // Free the request data
554 if (Cache
->RequestData
!= NULL
) {
555 if (Cache
->RequestData
->Url
!= NULL
) {
556 FreePool (Cache
->RequestData
->Url
);
558 FreePool (Cache
->RequestData
);
562 // Free the response header
564 if (Cache
->ResponseData
!= NULL
) {
565 if (Cache
->ResponseData
->Headers
!= NULL
) {
566 for (Index
= 0; Index
< Cache
->ResponseData
->HeaderCount
; Index
++) {
567 FreePool (Cache
->ResponseData
->Headers
[Index
].FieldName
);
568 FreePool (Cache
->ResponseData
->Headers
[Index
].FieldValue
);
570 FreePool (Cache
->ResponseData
->Headers
);
575 // Free the response body
577 NET_LIST_FOR_EACH_SAFE (Entry
, NextEntry
, &Cache
->EntityDataList
) {
578 EntityData
= NET_LIST_USER_STRUCT (Entry
, HTTP_BOOT_ENTITY_DATA
, Link
);
579 if (EntityData
->Block
!= NULL
) {
580 FreePool (EntityData
->Block
);
582 RemoveEntryList (&EntityData
->Link
);
583 FreePool (EntityData
);
591 Clean up all cached data.
593 @param[in] Private The pointer to the driver's private data.
597 HttpBootFreeCacheList (
598 IN HTTP_BOOT_PRIVATE_DATA
*Private
602 LIST_ENTRY
*NextEntry
;
603 HTTP_BOOT_CACHE_CONTENT
*Cache
;
605 NET_LIST_FOR_EACH_SAFE (Entry
, NextEntry
, &Private
->CacheList
) {
606 Cache
= NET_LIST_USER_STRUCT (Entry
, HTTP_BOOT_CACHE_CONTENT
, Link
);
607 RemoveEntryList (&Cache
->Link
);
608 HttpBootFreeCache (Cache
);
613 A callback function to intercept events during message parser.
615 This function will be invoked during HttpParseMessageBody() with various events type. An error
616 return status of the callback function will cause the HttpParseMessageBody() aborted.
618 @param[in] EventType Event type of this callback call.
619 @param[in] Data A pointer to data buffer.
620 @param[in] Length Length in bytes of the Data.
621 @param[in] Context Callback context set by HttpInitMsgParser().
623 @retval EFI_SUCCESS Continue to parser the message body.
624 @retval Others Abort the parse.
629 HttpBootGetBootFileCallback (
630 IN HTTP_BODY_PARSE_EVENT EventType
,
636 HTTP_BOOT_CALLBACK_DATA
*CallbackData
;
637 HTTP_BOOT_ENTITY_DATA
*NewEntityData
;
640 // We only care about the entity data.
642 if (EventType
!= BodyParseEventOnData
) {
646 CallbackData
= (HTTP_BOOT_CALLBACK_DATA
*) Context
;
648 // Copy data if caller has provided a buffer.
650 if (CallbackData
->BufferSize
> CallbackData
->CopyedSize
) {
652 CallbackData
->Buffer
+ CallbackData
->CopyedSize
,
654 MIN (Length
, CallbackData
->BufferSize
- CallbackData
->CopyedSize
)
656 CallbackData
->CopyedSize
+= MIN (Length
, CallbackData
->BufferSize
- CallbackData
->CopyedSize
);
660 // The caller doesn't provide a buffer, save the block into cache list.
662 if (CallbackData
->Cache
!= NULL
) {
663 NewEntityData
= AllocatePool (sizeof (HTTP_BOOT_ENTITY_DATA
));
664 if (NewEntityData
== NULL
) {
665 return EFI_OUT_OF_RESOURCES
;
667 if (CallbackData
->NewBlock
) {
668 NewEntityData
->Block
= CallbackData
->Block
;
669 CallbackData
->Block
= NULL
;
671 NewEntityData
->DataLength
= Length
;
672 NewEntityData
->DataStart
= (UINT8
*) Data
;
673 InsertTailList (&CallbackData
->Cache
->EntityDataList
, &NewEntityData
->Link
);
679 This function download the boot file by using UEFI HTTP protocol.
681 @param[in] Private The pointer to the driver's private data.
682 @param[in] HeaderOnly Only request the response header, it could save a lot of time if
683 the caller only want to know the size of the requested file.
684 @param[in, out] BufferSize On input the size of Buffer in bytes. On output with a return
685 code of EFI_SUCCESS, the amount of data transferred to
686 Buffer. On output with a return code of EFI_BUFFER_TOO_SMALL,
687 the size of Buffer required to retrieve the requested file.
688 @param[out] Buffer The memory buffer to transfer the file to. IF Buffer is NULL,
689 then the size of the requested file is returned in
692 @retval EFI_SUCCESS The file was loaded.
693 @retval EFI_INVALID_PARAMETER BufferSize is NULL or Buffer Size is not NULL but Buffer is NULL.
694 @retval EFI_OUT_OF_RESOURCES Could not allocate needed resources
695 @retval EFI_BUFFER_TOO_SMALL The BufferSize is too small to read the current directory entry.
696 BufferSize has been updated with the size needed to complete
698 @retval Others Unexpected error happened.
702 HttpBootGetBootFile (
703 IN HTTP_BOOT_PRIVATE_DATA
*Private
,
704 IN BOOLEAN HeaderOnly
,
705 IN OUT UINTN
*BufferSize
,
711 EFI_HTTP_REQUEST_DATA
*RequestData
;
712 HTTP_IO_RESOPNSE_DATA
*ResponseData
;
713 HTTP_IO_RESOPNSE_DATA ResponseBody
;
715 HTTP_IO_HEADER
*HttpIoHeader
;
717 HTTP_BOOT_CALLBACK_DATA Context
;
719 HTTP_BOOT_CACHE_CONTENT
*Cache
;
723 ASSERT (Private
!= NULL
);
724 ASSERT (Private
->HttpCreated
);
726 if (BufferSize
== NULL
) {
727 return EFI_INVALID_PARAMETER
;
730 if (*BufferSize
!= 0 && Buffer
== NULL
) {
731 return EFI_INVALID_PARAMETER
;
735 // First, check whether we already cached the requested Uri.
737 Url
= AllocatePool ((AsciiStrLen (Private
->BootFileUri
) + 1) * sizeof (CHAR16
));
739 return EFI_OUT_OF_RESOURCES
;
741 AsciiStrToUnicodeStr (Private
->BootFileUri
, Url
);
743 Status
= HttpBootGetFileFromCache (Private
, Url
, BufferSize
, Buffer
);
744 if (Status
!= EFI_NOT_FOUND
) {
751 // Not found in cache, try to download it through HTTP.
755 // 1. Create a temp cache item for the requested URI if caller doesn't provide buffer.
758 if ((!HeaderOnly
) && (*BufferSize
== 0)) {
759 Cache
= AllocateZeroPool (sizeof (HTTP_BOOT_CACHE_CONTENT
));
761 Status
= EFI_OUT_OF_RESOURCES
;
764 InitializeListHead (&Cache
->EntityDataList
);
768 // 2. Send HTTP request message.
772 // 2.1 Build HTTP header for the request, 3 header is needed to download a boot file:
777 HttpIoHeader
= HttpBootCreateHeader (3);
778 if (HttpIoHeader
== NULL
) {
779 Status
= EFI_OUT_OF_RESOURCES
;
784 // Add HTTP header field 1: Host
787 Status
= HttpUrlGetHostName (
788 Private
->BootFileUri
,
789 Private
->BootFileUriParser
,
792 if (EFI_ERROR (Status
)) {
795 Status
= HttpBootSetHeader (
797 HTTP_FIELD_NAME_HOST
,
801 if (EFI_ERROR (Status
)) {
806 // Add HTTP header field 2: Accept
808 Status
= HttpBootSetHeader (
810 HTTP_FIELD_NAME_ACCEPT
,
813 if (EFI_ERROR (Status
)) {
818 // Add HTTP header field 3: User-Agent
820 Status
= HttpBootSetHeader (
822 HTTP_FIELD_NAME_USER_AGENT
,
823 HTTP_USER_AGENT_EFI_HTTP_BOOT
825 if (EFI_ERROR (Status
)) {
830 // 2.2 Build the rest of HTTP request info.
832 RequestData
= AllocatePool (sizeof (EFI_HTTP_REQUEST_DATA
));
833 if (RequestData
== NULL
) {
834 Status
= EFI_OUT_OF_RESOURCES
;
837 RequestData
->Method
= HeaderOnly
? HttpMethodHead
: HttpMethodGet
;
838 RequestData
->Url
= Url
;
839 if (RequestData
->Url
== NULL
) {
840 Status
= EFI_OUT_OF_RESOURCES
;
843 AsciiStrToUnicodeStr (Private
->BootFileUri
, RequestData
->Url
);
846 // 2.3 Record the request info in a temp cache item.
849 Cache
->RequestData
= RequestData
;
853 // 2.4 Send out the request to HTTP server.
855 HttpIo
= &Private
->HttpIo
;
856 Status
= HttpIoSendRequest (
859 HttpIoHeader
->HeaderCount
,
860 HttpIoHeader
->Headers
,
864 if (EFI_ERROR (Status
)) {
869 // 3. Receive HTTP response message.
873 // 3.1 First step, use zero BodyLength to only receive the response headers.
875 ResponseData
= AllocateZeroPool (sizeof(HTTP_IO_RESOPNSE_DATA
));
876 if (ResponseData
== NULL
) {
877 Status
= EFI_OUT_OF_RESOURCES
;
880 Status
= HttpIoRecvResponse (
885 if (EFI_ERROR (Status
)) {
890 // 3.2 Cache the response header.
893 Cache
->ResponseData
= ResponseData
;
897 // 3.3 Init a message-body parser from the header information.
900 Context
.NewBlock
= FALSE
;
901 Context
.Block
= NULL
;
902 Context
.CopyedSize
= 0;
903 Context
.Buffer
= Buffer
;
904 Context
.BufferSize
= *BufferSize
;
905 Context
.Cache
= Cache
;
906 Status
= HttpInitMsgParser (
907 HeaderOnly
? HttpMethodHead
: HttpMethodGet
,
908 ResponseData
->Response
.StatusCode
,
909 ResponseData
->HeaderCount
,
910 ResponseData
->Headers
,
911 HttpBootGetBootFileCallback
,
915 if (EFI_ERROR (Status
)) {
920 // 3.4 Continue to receive and parse message-body if needed.
924 ZeroMem (&ResponseBody
, sizeof (HTTP_IO_RESOPNSE_DATA
));
925 while (!HttpIsMessageComplete (Parser
)) {
927 // Allocate a block to hold the message-body, if caller doesn't provide
928 // a buffer, the block will be cached and we will allocate a new one here.
930 if (Block
== NULL
|| Context
.BufferSize
== 0) {
931 Block
= AllocatePool (HTTP_BOOT_BLOCK_SIZE
);
933 Status
= EFI_OUT_OF_RESOURCES
;
936 Context
.NewBlock
= TRUE
;
937 Context
.Block
= Block
;
939 Context
.NewBlock
= FALSE
;
942 ResponseBody
.Body
= (CHAR8
*) Block
;
943 ResponseBody
.BodyLength
= HTTP_BOOT_BLOCK_SIZE
;
944 Status
= HttpIoRecvResponse (
949 if (EFI_ERROR (Status
)) {
954 // Parse the new received block of the message-body, the block will be saved in cache.
956 Status
= HttpParseMessageBody (
958 ResponseBody
.BodyLength
,
961 if (EFI_ERROR (Status
)) {
968 // 3.5 Message-body receive & parse is completed, get the file size.
970 Status
= HttpGetEntityLength (Parser
, &ContentLength
);
971 if (EFI_ERROR (Status
)) {
975 if (*BufferSize
< ContentLength
) {
976 Status
= EFI_BUFFER_TOO_SMALL
;
978 *BufferSize
= ContentLength
;
981 // 4. Save the cache item to driver's cache list and return.
984 Cache
->EntityLength
= ContentLength
;
985 InsertTailList (&Private
->CacheList
, &Cache
->Link
);
988 if (Parser
!= NULL
) {
989 HttpFreeMsgParser (Parser
);
995 if (Parser
!= NULL
) {
996 HttpFreeMsgParser (Parser
);
998 if (Context
.Block
!= NULL
) {
999 FreePool (Context
.Block
);
1001 HttpBootFreeCache (Cache
);
1004 if (ResponseData
!= NULL
) {
1005 FreePool (ResponseData
);
1008 if (RequestData
!= NULL
) {
1009 FreePool (RequestData
);
1012 HttpBootFreeHeader (HttpIoHeader
);
1014 if (Cache
!= NULL
) {