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 Release all the resource of a cache item.
462 @param[in] Cache The pointer to the cache item.
467 IN HTTP_BOOT_CACHE_CONTENT
*Cache
472 LIST_ENTRY
*NextEntry
;
473 HTTP_BOOT_ENTITY_DATA
*EntityData
;
477 // Free the request data
479 if (Cache
->RequestData
!= NULL
) {
480 if (Cache
->RequestData
->Url
!= NULL
) {
481 FreePool (Cache
->RequestData
->Url
);
483 FreePool (Cache
->RequestData
);
487 // Free the response header
489 if (Cache
->ResponseData
!= NULL
) {
490 if (Cache
->ResponseData
->Headers
!= NULL
) {
491 for (Index
= 0; Index
< Cache
->ResponseData
->HeaderCount
; Index
++) {
492 FreePool (Cache
->ResponseData
->Headers
[Index
].FieldName
);
493 FreePool (Cache
->ResponseData
->Headers
[Index
].FieldValue
);
495 FreePool (Cache
->ResponseData
->Headers
);
500 // Free the response body
502 NET_LIST_FOR_EACH_SAFE (Entry
, NextEntry
, &Cache
->EntityDataList
) {
503 EntityData
= NET_LIST_USER_STRUCT (Entry
, HTTP_BOOT_ENTITY_DATA
, Link
);
504 if (EntityData
->Block
!= NULL
) {
505 FreePool (EntityData
->Block
);
507 RemoveEntryList (&EntityData
->Link
);
508 FreePool (EntityData
);
516 Clean up all cached data.
518 @param[in] Private The pointer to the driver's private data.
522 HttpBootFreeCacheList (
523 IN HTTP_BOOT_PRIVATE_DATA
*Private
527 LIST_ENTRY
*NextEntry
;
528 HTTP_BOOT_CACHE_CONTENT
*Cache
;
530 NET_LIST_FOR_EACH_SAFE (Entry
, NextEntry
, &Private
->CacheList
) {
531 Cache
= NET_LIST_USER_STRUCT (Entry
, HTTP_BOOT_CACHE_CONTENT
, Link
);
532 RemoveEntryList (&Cache
->Link
);
533 HttpBootFreeCache (Cache
);
538 Get the file content from cached data.
540 @param[in] Private The pointer to the driver's private data.
541 @param[in] Uri Uri of the file to be retrieved from cache.
542 @param[in, out] BufferSize On input the size of Buffer in bytes. On output with a return
543 code of EFI_SUCCESS, the amount of data transferred to
544 Buffer. On output with a return code of EFI_BUFFER_TOO_SMALL,
545 the size of Buffer required to retrieve the requested file.
546 @param[out] Buffer The memory buffer to transfer the file to. IF Buffer is NULL,
547 then the size of the requested file is returned in
550 @retval EFI_SUCCESS Successfully created.
551 @retval Others Failed to create HttpIo.
555 HttpBootGetFileFromCache (
556 IN HTTP_BOOT_PRIVATE_DATA
*Private
,
558 IN OUT UINTN
*BufferSize
,
564 HTTP_BOOT_CACHE_CONTENT
*Cache
;
565 HTTP_BOOT_ENTITY_DATA
*EntityData
;
568 if (Uri
== NULL
|| BufferSize
== 0 || Buffer
== NULL
) {
569 return EFI_INVALID_PARAMETER
;
573 // Search file in the cache list, the cache entry will be released upon a successful
576 NET_LIST_FOR_EACH (Entry
, &Private
->CacheList
) {
577 Cache
= NET_LIST_USER_STRUCT (Entry
, HTTP_BOOT_CACHE_CONTENT
, Link
);
579 // Compare the URI to see whether we already have a cache for this file.
581 if ((Cache
->RequestData
!= NULL
) &&
582 (Cache
->RequestData
->Url
!= NULL
) &&
583 (StrCmp (Uri
, Cache
->RequestData
->Url
) == 0))
586 // Hit cache, check buffer size.
588 if (*BufferSize
< Cache
->EntityLength
) {
589 *BufferSize
= Cache
->EntityLength
;
590 return EFI_BUFFER_TOO_SMALL
;
594 // Fill data to buffer.
597 NET_LIST_FOR_EACH (Entry2
, &Cache
->EntityDataList
) {
598 EntityData
= NET_LIST_USER_STRUCT (Entry2
, HTTP_BOOT_ENTITY_DATA
, Link
);
599 if (*BufferSize
> CopyedSize
) {
602 EntityData
->DataStart
,
603 MIN (EntityData
->DataLength
, *BufferSize
- CopyedSize
)
605 CopyedSize
+= MIN (EntityData
->DataLength
, *BufferSize
- CopyedSize
);
608 *BufferSize
= CopyedSize
;
611 // On success, free the cached data to release the memory resource.
613 RemoveEntryList (&Cache
->Link
);
614 HttpBootFreeCache (Cache
);
619 return EFI_NOT_FOUND
;
623 A callback function to intercept events during message parser.
625 This function will be invoked during HttpParseMessageBody() with various events type. An error
626 return status of the callback function will cause the HttpParseMessageBody() aborted.
628 @param[in] EventType Event type of this callback call.
629 @param[in] Data A pointer to data buffer.
630 @param[in] Length Length in bytes of the Data.
631 @param[in] Context Callback context set by HttpInitMsgParser().
633 @retval EFI_SUCCESS Continue to parser the message body.
634 @retval Others Abort the parse.
639 HttpBootGetBootFileCallback (
640 IN HTTP_BODY_PARSE_EVENT EventType
,
646 HTTP_BOOT_CALLBACK_DATA
*CallbackData
;
647 HTTP_BOOT_ENTITY_DATA
*NewEntityData
;
650 // We only care about the entity data.
652 if (EventType
!= BodyParseEventOnData
) {
656 CallbackData
= (HTTP_BOOT_CALLBACK_DATA
*) Context
;
658 // Copy data if caller has provided a buffer.
660 if (CallbackData
->BufferSize
> CallbackData
->CopyedSize
) {
662 CallbackData
->Buffer
+ CallbackData
->CopyedSize
,
664 MIN (Length
, CallbackData
->BufferSize
- CallbackData
->CopyedSize
)
666 CallbackData
->CopyedSize
+= MIN (Length
, CallbackData
->BufferSize
- CallbackData
->CopyedSize
);
670 // The caller doesn't provide a buffer, save the block into cache list.
672 if (CallbackData
->Cache
!= NULL
) {
673 NewEntityData
= AllocatePool (sizeof (HTTP_BOOT_ENTITY_DATA
));
674 if (NewEntityData
== NULL
) {
675 return EFI_OUT_OF_RESOURCES
;
677 if (CallbackData
->NewBlock
) {
678 NewEntityData
->Block
= CallbackData
->Block
;
679 CallbackData
->Block
= NULL
;
681 NewEntityData
->DataLength
= Length
;
682 NewEntityData
->DataStart
= (UINT8
*) Data
;
683 InsertTailList (&CallbackData
->Cache
->EntityDataList
, &NewEntityData
->Link
);
689 This function download the boot file by using UEFI HTTP protocol.
691 @param[in] Private The pointer to the driver's private data.
692 @param[in] HeaderOnly Only request the response header, it could save a lot of time if
693 the caller only want to know the size of the requested file.
694 @param[in, out] BufferSize On input the size of Buffer in bytes. On output with a return
695 code of EFI_SUCCESS, the amount of data transferred to
696 Buffer. On output with a return code of EFI_BUFFER_TOO_SMALL,
697 the size of Buffer required to retrieve the requested file.
698 @param[out] Buffer The memory buffer to transfer the file to. IF Buffer is NULL,
699 then the size of the requested file is returned in
702 @retval EFI_SUCCESS The file was loaded.
703 @retval EFI_INVALID_PARAMETER BufferSize is NULL or Buffer Size is not NULL but Buffer is NULL.
704 @retval EFI_OUT_OF_RESOURCES Could not allocate needed resources
705 @retval EFI_BUFFER_TOO_SMALL The BufferSize is too small to read the current directory entry.
706 BufferSize has been updated with the size needed to complete
708 @retval Others Unexpected error happened.
712 HttpBootGetBootFile (
713 IN HTTP_BOOT_PRIVATE_DATA
*Private
,
714 IN BOOLEAN HeaderOnly
,
715 IN OUT UINTN
*BufferSize
,
721 EFI_HTTP_REQUEST_DATA
*RequestData
;
722 HTTP_IO_RESOPNSE_DATA
*ResponseData
;
723 HTTP_IO_RESOPNSE_DATA ResponseBody
;
725 HTTP_IO_HEADER
*HttpIoHeader
;
727 HTTP_BOOT_CALLBACK_DATA Context
;
729 HTTP_BOOT_CACHE_CONTENT
*Cache
;
732 BOOLEAN IdentityMode
;
735 ASSERT (Private
!= NULL
);
736 ASSERT (Private
->HttpCreated
);
738 if (BufferSize
== NULL
) {
739 return EFI_INVALID_PARAMETER
;
742 if (*BufferSize
!= 0 && Buffer
== NULL
) {
743 return EFI_INVALID_PARAMETER
;
747 // First, check whether we already cached the requested Uri.
749 Url
= AllocatePool ((AsciiStrLen (Private
->BootFileUri
) + 1) * sizeof (CHAR16
));
751 return EFI_OUT_OF_RESOURCES
;
753 AsciiStrToUnicodeStr (Private
->BootFileUri
, Url
);
755 Status
= HttpBootGetFileFromCache (Private
, Url
, BufferSize
, Buffer
);
756 if (Status
!= EFI_NOT_FOUND
) {
763 // Not found in cache, try to download it through HTTP.
767 // 1. Create a temp cache item for the requested URI if caller doesn't provide buffer.
770 if ((!HeaderOnly
) && (*BufferSize
== 0)) {
771 Cache
= AllocateZeroPool (sizeof (HTTP_BOOT_CACHE_CONTENT
));
773 Status
= EFI_OUT_OF_RESOURCES
;
776 InitializeListHead (&Cache
->EntityDataList
);
780 // 2. Send HTTP request message.
784 // 2.1 Build HTTP header for the request, 3 header is needed to download a boot file:
789 HttpIoHeader
= HttpBootCreateHeader (3);
790 if (HttpIoHeader
== NULL
) {
791 Status
= EFI_OUT_OF_RESOURCES
;
796 // Add HTTP header field 1: Host
799 Status
= HttpUrlGetHostName (
800 Private
->BootFileUri
,
801 Private
->BootFileUriParser
,
804 if (EFI_ERROR (Status
)) {
807 Status
= HttpBootSetHeader (
809 HTTP_FIELD_NAME_HOST
,
813 if (EFI_ERROR (Status
)) {
818 // Add HTTP header field 2: Accept
820 Status
= HttpBootSetHeader (
822 HTTP_FIELD_NAME_ACCEPT
,
825 if (EFI_ERROR (Status
)) {
830 // Add HTTP header field 3: User-Agent
832 Status
= HttpBootSetHeader (
834 HTTP_FIELD_NAME_USER_AGENT
,
835 HTTP_USER_AGENT_EFI_HTTP_BOOT
837 if (EFI_ERROR (Status
)) {
842 // 2.2 Build the rest of HTTP request info.
844 RequestData
= AllocatePool (sizeof (EFI_HTTP_REQUEST_DATA
));
845 if (RequestData
== NULL
) {
846 Status
= EFI_OUT_OF_RESOURCES
;
849 RequestData
->Method
= HeaderOnly
? HttpMethodHead
: HttpMethodGet
;
850 RequestData
->Url
= Url
;
851 if (RequestData
->Url
== NULL
) {
852 Status
= EFI_OUT_OF_RESOURCES
;
855 AsciiStrToUnicodeStr (Private
->BootFileUri
, RequestData
->Url
);
858 // 2.3 Record the request info in a temp cache item.
861 Cache
->RequestData
= RequestData
;
865 // 2.4 Send out the request to HTTP server.
867 HttpIo
= &Private
->HttpIo
;
868 Status
= HttpIoSendRequest (
871 HttpIoHeader
->HeaderCount
,
872 HttpIoHeader
->Headers
,
876 if (EFI_ERROR (Status
)) {
881 // 3. Receive HTTP response message.
885 // 3.1 First step, use zero BodyLength to only receive the response headers.
887 ResponseData
= AllocateZeroPool (sizeof(HTTP_IO_RESOPNSE_DATA
));
888 if (ResponseData
== NULL
) {
889 Status
= EFI_OUT_OF_RESOURCES
;
892 Status
= HttpIoRecvResponse (
897 if (EFI_ERROR (Status
)) {
902 // 3.2 Cache the response header.
905 Cache
->ResponseData
= ResponseData
;
909 // 3.3 Init a message-body parser from the header information.
912 Context
.NewBlock
= FALSE
;
913 Context
.Block
= NULL
;
914 Context
.CopyedSize
= 0;
915 Context
.Buffer
= Buffer
;
916 Context
.BufferSize
= *BufferSize
;
917 Context
.Cache
= Cache
;
918 Status
= HttpInitMsgParser (
919 HeaderOnly
? HttpMethodHead
: HttpMethodGet
,
920 ResponseData
->Response
.StatusCode
,
921 ResponseData
->HeaderCount
,
922 ResponseData
->Headers
,
923 HttpBootGetBootFileCallback
,
927 if (EFI_ERROR (Status
)) {
932 // 3.4 Continue to receive and parse message-body if needed.
937 // 3.4.1, check whether we are in identity transfer-coding.
940 Status
= HttpGetEntityLength (Parser
, &ContentLength
);
941 if (!EFI_ERROR (Status
)) {
944 IdentityMode
= FALSE
;
948 // 3.4.2, start the message-body download, the identity and chunked transfer-coding
949 // is handled in different path here.
951 ZeroMem (&ResponseBody
, sizeof (HTTP_IO_RESOPNSE_DATA
));
954 // In identity transfer-coding there is no need to parse the message body,
955 // just download the message body to the user provided buffer directly.
958 while (ReceivedSize
< ContentLength
) {
959 ResponseBody
.Body
= (CHAR8
*) Buffer
+ ReceivedSize
;
960 ResponseBody
.BodyLength
= *BufferSize
- ReceivedSize
;
961 Status
= HttpIoRecvResponse (
966 if (EFI_ERROR (Status
)) {
969 ReceivedSize
+= ResponseBody
.BodyLength
;
973 // In "chunked" transfer-coding mode, so we need to parse the received
974 // data to get the real entity content.
977 while (!HttpIsMessageComplete (Parser
)) {
979 // Allocate a buffer in Block to hold the message-body.
980 // If caller provides a buffer, this Block will be reused in every HttpIoRecvResponse().
981 // Otherwise a buffer, the buffer in Block will be cached and we should allocate a new before
982 // every HttpIoRecvResponse().
984 if (Block
== NULL
|| Context
.BufferSize
== 0) {
985 Block
= AllocatePool (HTTP_BOOT_BLOCK_SIZE
);
987 Status
= EFI_OUT_OF_RESOURCES
;
990 Context
.NewBlock
= TRUE
;
991 Context
.Block
= Block
;
993 Context
.NewBlock
= FALSE
;
996 ResponseBody
.Body
= (CHAR8
*) Block
;
997 ResponseBody
.BodyLength
= HTTP_BOOT_BLOCK_SIZE
;
998 Status
= HttpIoRecvResponse (
1003 if (EFI_ERROR (Status
)) {
1008 // Parse the new received block of the message-body, the block will be saved in cache.
1010 Status
= HttpParseMessageBody (
1012 ResponseBody
.BodyLength
,
1015 if (EFI_ERROR (Status
)) {
1023 // 3.5 Message-body receive & parse is completed, we should be able to get the file size now.
1025 Status
= HttpGetEntityLength (Parser
, &ContentLength
);
1026 if (EFI_ERROR (Status
)) {
1030 if (*BufferSize
< ContentLength
) {
1031 Status
= EFI_BUFFER_TOO_SMALL
;
1033 *BufferSize
= ContentLength
;
1036 // 4. Save the cache item to driver's cache list and return.
1038 if (Cache
!= NULL
) {
1039 Cache
->EntityLength
= ContentLength
;
1040 InsertTailList (&Private
->CacheList
, &Cache
->Link
);
1043 if (Parser
!= NULL
) {
1044 HttpFreeMsgParser (Parser
);
1050 if (Parser
!= NULL
) {
1051 HttpFreeMsgParser (Parser
);
1053 if (Context
.Block
!= NULL
) {
1054 FreePool (Context
.Block
);
1056 HttpBootFreeCache (Cache
);
1059 if (ResponseData
!= NULL
) {
1060 FreePool (ResponseData
);
1063 if (RequestData
!= NULL
) {
1064 FreePool (RequestData
);
1067 HttpBootFreeHeader (HttpIoHeader
);
1069 if (Cache
!= NULL
) {