2 Implementation of the boot file download function.
4 Copyright (c) 2015 - 2021, Intel Corporation. All rights reserved.<BR>
5 (C) Copyright 2016 Hewlett Packard Enterprise Development LP<BR>
6 SPDX-License-Identifier: BSD-2-Clause-Patent
10 #include "HttpBootDxe.h"
13 Update the device path node to include the boot resource information.
15 @param[in] Private The pointer to the driver's private data.
17 @retval EFI_SUCCESS Device patch successfully updated.
18 @retval EFI_OUT_OF_RESOURCES Could not allocate needed resources.
19 @retval Others Unexpected error happened.
23 HttpBootUpdateDevicePath (
24 IN HTTP_BOOT_PRIVATE_DATA
*Private
28 EFI_DEVICE_PATH_PROTOCOL
*TmpIpDevicePath
;
29 EFI_DEVICE_PATH_PROTOCOL
*TmpDnsDevicePath
;
30 EFI_DEVICE_PATH_PROTOCOL
*NewDevicePath
;
34 TmpIpDevicePath
= NULL
;
35 TmpDnsDevicePath
= NULL
;
38 // Update the IP node with DHCP assigned information.
40 if (!Private
->UsingIpv6
) {
41 Node
= AllocateZeroPool (sizeof (IPv4_DEVICE_PATH
));
43 return EFI_OUT_OF_RESOURCES
;
46 Node
->Ipv4
.Header
.Type
= MESSAGING_DEVICE_PATH
;
47 Node
->Ipv4
.Header
.SubType
= MSG_IPv4_DP
;
48 SetDevicePathNodeLength (Node
, sizeof (IPv4_DEVICE_PATH
));
49 CopyMem (&Node
->Ipv4
.LocalIpAddress
, &Private
->StationIp
, sizeof (EFI_IPv4_ADDRESS
));
50 Node
->Ipv4
.RemotePort
= Private
->Port
;
51 Node
->Ipv4
.Protocol
= EFI_IP_PROTO_TCP
;
52 Node
->Ipv4
.StaticIpAddress
= FALSE
;
53 CopyMem (&Node
->Ipv4
.GatewayIpAddress
, &Private
->GatewayIp
, sizeof (EFI_IPv4_ADDRESS
));
54 CopyMem (&Node
->Ipv4
.SubnetMask
, &Private
->SubnetMask
, sizeof (EFI_IPv4_ADDRESS
));
56 Node
= AllocateZeroPool (sizeof (IPv6_DEVICE_PATH
));
58 return EFI_OUT_OF_RESOURCES
;
61 Node
->Ipv6
.Header
.Type
= MESSAGING_DEVICE_PATH
;
62 Node
->Ipv6
.Header
.SubType
= MSG_IPv6_DP
;
63 SetDevicePathNodeLength (Node
, sizeof (IPv6_DEVICE_PATH
));
64 Node
->Ipv6
.PrefixLength
= IP6_PREFIX_LENGTH
;
65 Node
->Ipv6
.RemotePort
= Private
->Port
;
66 Node
->Ipv6
.Protocol
= EFI_IP_PROTO_TCP
;
67 Node
->Ipv6
.IpAddressOrigin
= 0;
68 CopyMem (&Node
->Ipv6
.LocalIpAddress
, &Private
->StationIp
.v6
, sizeof (EFI_IPv6_ADDRESS
));
69 CopyMem (&Node
->Ipv6
.RemoteIpAddress
, &Private
->ServerIp
.v6
, sizeof (EFI_IPv6_ADDRESS
));
70 CopyMem (&Node
->Ipv6
.GatewayIpAddress
, &Private
->GatewayIp
.v6
, sizeof (EFI_IPv6_ADDRESS
));
73 TmpIpDevicePath
= AppendDevicePathNode (Private
->ParentDevicePath
, (EFI_DEVICE_PATH_PROTOCOL
*)Node
);
75 if (TmpIpDevicePath
== NULL
) {
76 return EFI_OUT_OF_RESOURCES
;
80 // Update the DNS node with DNS server IP list if existed.
82 if (Private
->DnsServerIp
!= NULL
) {
83 Length
= sizeof (EFI_DEVICE_PATH_PROTOCOL
) + sizeof (Node
->Dns
.IsIPv6
) + Private
->DnsServerCount
* sizeof (EFI_IP_ADDRESS
);
84 Node
= AllocatePool (Length
);
86 FreePool (TmpIpDevicePath
);
87 return EFI_OUT_OF_RESOURCES
;
90 Node
->DevPath
.Type
= MESSAGING_DEVICE_PATH
;
91 Node
->DevPath
.SubType
= MSG_DNS_DP
;
92 SetDevicePathNodeLength (Node
, Length
);
93 Node
->Dns
.IsIPv6
= Private
->UsingIpv6
? 0x01 : 0x00;
94 CopyMem ((UINT8
*)Node
+ sizeof (EFI_DEVICE_PATH_PROTOCOL
) + sizeof (Node
->Dns
.IsIPv6
), Private
->DnsServerIp
, Private
->DnsServerCount
* sizeof (EFI_IP_ADDRESS
));
96 TmpDnsDevicePath
= AppendDevicePathNode (TmpIpDevicePath
, (EFI_DEVICE_PATH_PROTOCOL
*)Node
);
98 FreePool (TmpIpDevicePath
);
99 TmpIpDevicePath
= NULL
;
100 if (TmpDnsDevicePath
== NULL
) {
101 return EFI_OUT_OF_RESOURCES
;
106 // Update the URI node with the boot file URI.
108 Length
= sizeof (EFI_DEVICE_PATH_PROTOCOL
) + AsciiStrSize (Private
->BootFileUri
);
109 Node
= AllocatePool (Length
);
111 if (TmpIpDevicePath
!= NULL
) {
112 FreePool (TmpIpDevicePath
);
115 if (TmpDnsDevicePath
!= NULL
) {
116 FreePool (TmpDnsDevicePath
);
119 return EFI_OUT_OF_RESOURCES
;
122 Node
->DevPath
.Type
= MESSAGING_DEVICE_PATH
;
123 Node
->DevPath
.SubType
= MSG_URI_DP
;
124 SetDevicePathNodeLength (Node
, Length
);
125 CopyMem ((UINT8
*)Node
+ sizeof (EFI_DEVICE_PATH_PROTOCOL
), Private
->BootFileUri
, AsciiStrSize (Private
->BootFileUri
));
127 if (TmpDnsDevicePath
!= NULL
) {
128 NewDevicePath
= AppendDevicePathNode (TmpDnsDevicePath
, (EFI_DEVICE_PATH_PROTOCOL
*)Node
);
129 FreePool (TmpDnsDevicePath
);
131 ASSERT (TmpIpDevicePath
!= NULL
);
132 NewDevicePath
= AppendDevicePathNode (TmpIpDevicePath
, (EFI_DEVICE_PATH_PROTOCOL
*)Node
);
133 FreePool (TmpIpDevicePath
);
137 if (NewDevicePath
== NULL
) {
138 return EFI_OUT_OF_RESOURCES
;
141 if (!Private
->UsingIpv6
) {
143 // Reinstall the device path protocol of the child handle.
145 Status
= gBS
->ReinstallProtocolInterface (
146 Private
->Ip4Nic
->Controller
,
147 &gEfiDevicePathProtocolGuid
,
148 Private
->Ip4Nic
->DevicePath
,
151 if (EFI_ERROR (Status
)) {
155 FreePool (Private
->Ip4Nic
->DevicePath
);
156 Private
->Ip4Nic
->DevicePath
= NewDevicePath
;
159 // Reinstall the device path protocol of the child handle.
161 Status
= gBS
->ReinstallProtocolInterface (
162 Private
->Ip6Nic
->Controller
,
163 &gEfiDevicePathProtocolGuid
,
164 Private
->Ip6Nic
->DevicePath
,
167 if (EFI_ERROR (Status
)) {
171 FreePool (Private
->Ip6Nic
->DevicePath
);
172 Private
->Ip6Nic
->DevicePath
= NewDevicePath
;
179 Parse the boot file URI information from the selected Dhcp4 offer packet.
181 @param[in] Private The pointer to the driver's private data.
183 @retval EFI_SUCCESS Successfully parsed out all the boot information.
184 @retval Others Failed to parse out the boot information.
188 HttpBootDhcp4ExtractUriInfo (
189 IN HTTP_BOOT_PRIVATE_DATA
*Private
192 HTTP_BOOT_DHCP4_PACKET_CACHE
*SelectOffer
;
193 HTTP_BOOT_DHCP4_PACKET_CACHE
*HttpOffer
;
196 UINT32 DnsServerIndex
;
197 EFI_DHCP4_PACKET_OPTION
*Option
;
200 ASSERT (Private
!= NULL
);
201 ASSERT (Private
->SelectIndex
!= 0);
202 SelectIndex
= Private
->SelectIndex
- 1;
203 ASSERT (SelectIndex
< HTTP_BOOT_OFFER_MAX_NUM
);
207 Status
= EFI_SUCCESS
;
210 // SelectOffer contains the IP address configuration and name server configuration.
211 // HttpOffer contains the boot file URL.
213 SelectOffer
= &Private
->OfferBuffer
[SelectIndex
].Dhcp4
;
214 if (Private
->FilePathUri
== NULL
) {
216 // In Corporate environment, we need a HttpOffer.
218 if ((SelectOffer
->OfferType
== HttpOfferTypeDhcpIpUri
) ||
219 (SelectOffer
->OfferType
== HttpOfferTypeDhcpIpUriDns
) ||
220 (SelectOffer
->OfferType
== HttpOfferTypeDhcpNameUriDns
))
222 HttpOffer
= SelectOffer
;
224 ASSERT (Private
->SelectProxyType
!= HttpOfferTypeMax
);
225 ProxyIndex
= Private
->OfferIndex
[Private
->SelectProxyType
][0];
226 HttpOffer
= &Private
->OfferBuffer
[ProxyIndex
].Dhcp4
;
229 Private
->BootFileUriParser
= HttpOffer
->UriParser
;
230 Private
->BootFileUri
= (CHAR8
*)HttpOffer
->OptList
[HTTP_BOOT_DHCP4_TAG_INDEX_BOOTFILE
]->Data
;
233 // In Home environment the BootFileUri comes from the FilePath.
235 Private
->BootFileUriParser
= Private
->FilePathUriParser
;
236 Private
->BootFileUri
= Private
->FilePathUri
;
240 // Check the URI scheme.
242 Status
= HttpBootCheckUriScheme (Private
->BootFileUri
);
243 if (EFI_ERROR (Status
)) {
244 DEBUG ((DEBUG_ERROR
, "HttpBootDhcp4ExtractUriInfo: %r.\n", Status
));
245 if (Status
== EFI_INVALID_PARAMETER
) {
246 AsciiPrint ("\n Error: Invalid URI address.\n");
247 } else if (Status
== EFI_ACCESS_DENIED
) {
248 AsciiPrint ("\n Error: Access forbidden, only HTTPS connection is allowed.\n");
254 if ((SelectOffer
->OfferType
== HttpOfferTypeDhcpNameUriDns
) ||
255 (SelectOffer
->OfferType
== HttpOfferTypeDhcpDns
) ||
256 (SelectOffer
->OfferType
== HttpOfferTypeDhcpIpUriDns
))
258 Option
= SelectOffer
->OptList
[HTTP_BOOT_DHCP4_TAG_INDEX_DNS_SERVER
];
259 ASSERT (Option
!= NULL
);
262 // Record the Dns Server address list.
264 Private
->DnsServerCount
= (Option
->Length
) / sizeof (EFI_IPv4_ADDRESS
);
266 Private
->DnsServerIp
= AllocateZeroPool (Private
->DnsServerCount
* sizeof (EFI_IP_ADDRESS
));
267 if (Private
->DnsServerIp
== NULL
) {
268 return EFI_OUT_OF_RESOURCES
;
271 for (DnsServerIndex
= 0; DnsServerIndex
< Private
->DnsServerCount
; DnsServerIndex
++) {
272 CopyMem (&(Private
->DnsServerIp
[DnsServerIndex
].v4
), &(((EFI_IPv4_ADDRESS
*)Option
->Data
)[DnsServerIndex
]), sizeof (EFI_IPv4_ADDRESS
));
276 // Configure the default DNS server if server assigned.
278 Status
= HttpBootRegisterIp4Dns (
283 if (EFI_ERROR (Status
)) {
284 FreePool (Private
->DnsServerIp
);
285 Private
->DnsServerIp
= NULL
;
291 // Extract the port from URL, and use default HTTP port 80 if not provided.
293 Status
= HttpUrlGetPort (
294 Private
->BootFileUri
,
295 Private
->BootFileUriParser
,
298 if (EFI_ERROR (Status
) || (Private
->Port
== 0)) {
303 // All boot informations are valid here.
307 // Update the device path to include the boot resource information.
309 Status
= HttpBootUpdateDevicePath (Private
);
310 if (EFI_ERROR (Status
) && (Private
->DnsServerIp
!= NULL
)) {
311 FreePool (Private
->DnsServerIp
);
312 Private
->DnsServerIp
= NULL
;
319 Parse the boot file URI information from the selected Dhcp6 offer packet.
321 @param[in] Private The pointer to the driver's private data.
323 @retval EFI_SUCCESS Successfully parsed out all the boot information.
324 @retval Others Failed to parse out the boot information.
328 HttpBootDhcp6ExtractUriInfo (
329 IN HTTP_BOOT_PRIVATE_DATA
*Private
332 HTTP_BOOT_DHCP6_PACKET_CACHE
*SelectOffer
;
333 HTTP_BOOT_DHCP6_PACKET_CACHE
*HttpOffer
;
336 UINT32 DnsServerIndex
;
337 EFI_DHCP6_PACKET_OPTION
*Option
;
338 EFI_IPv6_ADDRESS IpAddr
;
344 ASSERT (Private
!= NULL
);
345 ASSERT (Private
->SelectIndex
!= 0);
346 SelectIndex
= Private
->SelectIndex
- 1;
347 ASSERT (SelectIndex
< HTTP_BOOT_OFFER_MAX_NUM
);
351 Status
= EFI_SUCCESS
;
354 // SelectOffer contains the IP address configuration and name server configuration.
355 // HttpOffer contains the boot file URL.
357 SelectOffer
= &Private
->OfferBuffer
[SelectIndex
].Dhcp6
;
358 if (Private
->FilePathUri
== NULL
) {
360 // In Corporate environment, we need a HttpOffer.
362 if ((SelectOffer
->OfferType
== HttpOfferTypeDhcpIpUri
) ||
363 (SelectOffer
->OfferType
== HttpOfferTypeDhcpIpUriDns
) ||
364 (SelectOffer
->OfferType
== HttpOfferTypeDhcpNameUriDns
))
366 HttpOffer
= SelectOffer
;
368 ASSERT (Private
->SelectProxyType
!= HttpOfferTypeMax
);
369 ProxyIndex
= Private
->OfferIndex
[Private
->SelectProxyType
][0];
370 HttpOffer
= &Private
->OfferBuffer
[ProxyIndex
].Dhcp6
;
373 Private
->BootFileUriParser
= HttpOffer
->UriParser
;
374 Private
->BootFileUri
= (CHAR8
*)HttpOffer
->OptList
[HTTP_BOOT_DHCP6_IDX_BOOT_FILE_URL
]->Data
;
377 // In Home environment the BootFileUri comes from the FilePath.
379 Private
->BootFileUriParser
= Private
->FilePathUriParser
;
380 Private
->BootFileUri
= Private
->FilePathUri
;
384 // Check the URI scheme.
386 Status
= HttpBootCheckUriScheme (Private
->BootFileUri
);
387 if (EFI_ERROR (Status
)) {
388 DEBUG ((DEBUG_ERROR
, "HttpBootDhcp6ExtractUriInfo: %r.\n", Status
));
389 if (Status
== EFI_INVALID_PARAMETER
) {
390 AsciiPrint ("\n Error: Invalid URI address.\n");
391 } else if (Status
== EFI_ACCESS_DENIED
) {
392 AsciiPrint ("\n Error: Access forbidden, only HTTPS connection is allowed.\n");
399 // Set the Local station address to IP layer.
401 Status
= HttpBootSetIp6Address (Private
);
402 if (EFI_ERROR (Status
)) {
407 // Register the IPv6 gateway address to the network device.
409 Status
= HttpBootSetIp6Gateway (Private
);
410 if (EFI_ERROR (Status
)) {
414 if ((SelectOffer
->OfferType
== HttpOfferTypeDhcpNameUriDns
) ||
415 (SelectOffer
->OfferType
== HttpOfferTypeDhcpDns
) ||
416 (SelectOffer
->OfferType
== HttpOfferTypeDhcpIpUriDns
))
418 Option
= SelectOffer
->OptList
[HTTP_BOOT_DHCP6_IDX_DNS_SERVER
];
419 ASSERT (Option
!= NULL
);
422 // Record the Dns Server address list.
424 Private
->DnsServerCount
= HTONS (Option
->OpLen
) / sizeof (EFI_IPv6_ADDRESS
);
426 Private
->DnsServerIp
= AllocateZeroPool (Private
->DnsServerCount
* sizeof (EFI_IP_ADDRESS
));
427 if (Private
->DnsServerIp
== NULL
) {
428 return EFI_OUT_OF_RESOURCES
;
431 for (DnsServerIndex
= 0; DnsServerIndex
< Private
->DnsServerCount
; DnsServerIndex
++) {
432 CopyMem (&(Private
->DnsServerIp
[DnsServerIndex
].v6
), &(((EFI_IPv6_ADDRESS
*)Option
->Data
)[DnsServerIndex
]), sizeof (EFI_IPv6_ADDRESS
));
436 // Configure the default DNS server if server assigned.
438 Status
= HttpBootSetIp6Dns (
440 HTONS (Option
->OpLen
),
443 if (EFI_ERROR (Status
)) {
449 // Extract the HTTP server Ip from URL. This is used to Check route table
450 // whether can send message to HTTP Server Ip through the GateWay.
452 Status
= HttpUrlGetIp6 (
453 Private
->BootFileUri
,
454 Private
->BootFileUriParser
,
458 if (EFI_ERROR (Status
)) {
460 // The Http server address is expressed by Name Ip, so perform DNS resolution
462 Status
= HttpUrlGetHostName (
463 Private
->BootFileUri
,
464 Private
->BootFileUriParser
,
467 if (EFI_ERROR (Status
)) {
471 HostNameSize
= AsciiStrSize (HostName
);
472 HostNameStr
= AllocateZeroPool (HostNameSize
* sizeof (CHAR16
));
473 if (HostNameStr
== NULL
) {
474 Status
= EFI_OUT_OF_RESOURCES
;
478 AsciiStrToUnicodeStrS (HostName
, HostNameStr
, HostNameSize
);
480 if (HostName
!= NULL
) {
484 Status
= HttpBootDns (Private
, HostNameStr
, &IpAddr
);
485 FreePool (HostNameStr
);
486 if (EFI_ERROR (Status
)) {
487 AsciiPrint ("\n Error: Could not retrieve the host address from DNS server.\n");
492 CopyMem (&Private
->ServerIp
.v6
, &IpAddr
, sizeof (EFI_IPv6_ADDRESS
));
495 // Extract the port from URL, and use default HTTP port 80 if not provided.
497 Status
= HttpUrlGetPort (
498 Private
->BootFileUri
,
499 Private
->BootFileUriParser
,
502 if (EFI_ERROR (Status
) || (Private
->Port
== 0)) {
507 // All boot informations are valid here.
511 // Update the device path to include the boot resource information.
513 Status
= HttpBootUpdateDevicePath (Private
);
514 if (EFI_ERROR (Status
)) {
521 if (Private
->DnsServerIp
!= NULL
) {
522 FreePool (Private
->DnsServerIp
);
523 Private
->DnsServerIp
= NULL
;
530 Discover all the boot information for boot file.
532 @param[in, out] Private The pointer to the driver's private data.
534 @retval EFI_SUCCESS Successfully obtained all the boot information .
535 @retval Others Failed to retrieve the boot information.
539 HttpBootDiscoverBootInfo (
540 IN OUT HTTP_BOOT_PRIVATE_DATA
*Private
546 // Start D.O.R.A/S.A.R.R exchange to acquire station ip address and
547 // other Http boot information.
549 Status
= HttpBootDhcp (Private
);
550 if (EFI_ERROR (Status
)) {
554 if (!Private
->UsingIpv6
) {
555 Status
= HttpBootDhcp4ExtractUriInfo (Private
);
557 Status
= HttpBootDhcp6ExtractUriInfo (Private
);
564 HttpIo Callback function which will be invoked when specified HTTP_IO_CALLBACK_EVENT happened.
566 @param[in] EventType Indicate the Event type that occurs in the current callback.
567 @param[in] Message HTTP message which will be send to, or just received from HTTP server.
568 @param[in] Context The Callback Context pointer.
570 @retval EFI_SUCCESS Tells the HttpIo to continue the HTTP process.
571 @retval Others Tells the HttpIo to abort the current HTTP process.
575 HttpBootHttpIoCallback (
576 IN HTTP_IO_CALLBACK_EVENT EventType
,
577 IN EFI_HTTP_MESSAGE
*Message
,
581 HTTP_BOOT_PRIVATE_DATA
*Private
;
584 Private
= (HTTP_BOOT_PRIVATE_DATA
*)Context
;
585 if (Private
->HttpBootCallback
!= NULL
) {
586 Status
= Private
->HttpBootCallback
->Callback (
587 Private
->HttpBootCallback
,
588 EventType
== HttpIoRequest
? HttpBootHttpRequest
: HttpBootHttpResponse
,
589 EventType
== HttpIoRequest
? FALSE
: TRUE
,
590 sizeof (EFI_HTTP_MESSAGE
),
600 Create a HttpIo instance for the file download.
602 @param[in] Private The pointer to the driver's private data.
604 @retval EFI_SUCCESS Successfully created.
605 @retval Others Failed to create HttpIo.
609 HttpBootCreateHttpIo (
610 IN HTTP_BOOT_PRIVATE_DATA
*Private
613 HTTP_IO_CONFIG_DATA ConfigData
;
615 EFI_HANDLE ImageHandle
;
618 ASSERT (Private
!= NULL
);
621 // Get HTTP timeout value
623 TimeoutValue
= PcdGet32 (PcdHttpIoTimeout
);
625 ZeroMem (&ConfigData
, sizeof (HTTP_IO_CONFIG_DATA
));
626 if (!Private
->UsingIpv6
) {
627 ConfigData
.Config4
.HttpVersion
= HttpVersion11
;
628 ConfigData
.Config4
.RequestTimeOut
= TimeoutValue
;
629 IP4_COPY_ADDRESS (&ConfigData
.Config4
.LocalIp
, &Private
->StationIp
.v4
);
630 IP4_COPY_ADDRESS (&ConfigData
.Config4
.SubnetMask
, &Private
->SubnetMask
.v4
);
631 ImageHandle
= Private
->Ip4Nic
->ImageHandle
;
633 ConfigData
.Config6
.HttpVersion
= HttpVersion11
;
634 ConfigData
.Config6
.RequestTimeOut
= TimeoutValue
;
635 IP6_COPY_ADDRESS (&ConfigData
.Config6
.LocalIp
, &Private
->StationIp
.v6
);
636 ImageHandle
= Private
->Ip6Nic
->ImageHandle
;
639 Status
= HttpIoCreateIo (
642 Private
->UsingIpv6
? IP_VERSION_6
: IP_VERSION_4
,
644 HttpBootHttpIoCallback
,
648 if (EFI_ERROR (Status
)) {
652 Private
->HttpCreated
= TRUE
;
657 Release all the resource of a cache item.
659 @param[in] Cache The pointer to the cache item.
664 IN HTTP_BOOT_CACHE_CONTENT
*Cache
669 LIST_ENTRY
*NextEntry
;
670 HTTP_BOOT_ENTITY_DATA
*EntityData
;
674 // Free the request data
676 if (Cache
->RequestData
!= NULL
) {
677 if (Cache
->RequestData
->Url
!= NULL
) {
678 FreePool (Cache
->RequestData
->Url
);
681 FreePool (Cache
->RequestData
);
685 // Free the response header
687 if (Cache
->ResponseData
!= NULL
) {
688 if (Cache
->ResponseData
->Headers
!= NULL
) {
689 for (Index
= 0; Index
< Cache
->ResponseData
->HeaderCount
; Index
++) {
690 FreePool (Cache
->ResponseData
->Headers
[Index
].FieldName
);
691 FreePool (Cache
->ResponseData
->Headers
[Index
].FieldValue
);
694 FreePool (Cache
->ResponseData
->Headers
);
699 // Free the response body
701 NET_LIST_FOR_EACH_SAFE (Entry
, NextEntry
, &Cache
->EntityDataList
) {
702 EntityData
= NET_LIST_USER_STRUCT (Entry
, HTTP_BOOT_ENTITY_DATA
, Link
);
703 if (EntityData
->Block
!= NULL
) {
704 FreePool (EntityData
->Block
);
707 RemoveEntryList (&EntityData
->Link
);
708 FreePool (EntityData
);
716 Clean up all cached data.
718 @param[in] Private The pointer to the driver's private data.
722 HttpBootFreeCacheList (
723 IN HTTP_BOOT_PRIVATE_DATA
*Private
727 LIST_ENTRY
*NextEntry
;
728 HTTP_BOOT_CACHE_CONTENT
*Cache
;
730 NET_LIST_FOR_EACH_SAFE (Entry
, NextEntry
, &Private
->CacheList
) {
731 Cache
= NET_LIST_USER_STRUCT (Entry
, HTTP_BOOT_CACHE_CONTENT
, Link
);
732 RemoveEntryList (&Cache
->Link
);
733 HttpBootFreeCache (Cache
);
738 Get the file content from cached data.
740 @param[in] Private The pointer to the driver's private data.
741 @param[in] Uri Uri of the file to be retrieved from cache.
742 @param[in, out] BufferSize On input the size of Buffer in bytes. On output with a return
743 code of EFI_SUCCESS, the amount of data transferred to
744 Buffer. On output with a return code of EFI_BUFFER_TOO_SMALL,
745 the size of Buffer required to retrieve the requested file.
746 @param[out] Buffer The memory buffer to transfer the file to. IF Buffer is NULL,
747 then the size of the requested file is returned in
749 @param[out] ImageType The image type of the downloaded file.
751 @retval EFI_SUCCESS Successfully created.
752 @retval Others Failed to create HttpIo.
756 HttpBootGetFileFromCache (
757 IN HTTP_BOOT_PRIVATE_DATA
*Private
,
759 IN OUT UINTN
*BufferSize
,
761 OUT HTTP_BOOT_IMAGE_TYPE
*ImageType
766 HTTP_BOOT_CACHE_CONTENT
*Cache
;
767 HTTP_BOOT_ENTITY_DATA
*EntityData
;
770 if ((Uri
== NULL
) || (BufferSize
== NULL
) || (Buffer
== NULL
) || (ImageType
== NULL
)) {
771 return EFI_INVALID_PARAMETER
;
774 NET_LIST_FOR_EACH (Entry
, &Private
->CacheList
) {
775 Cache
= NET_LIST_USER_STRUCT (Entry
, HTTP_BOOT_CACHE_CONTENT
, Link
);
777 // Compare the URI to see whether we already have a cache for this file.
779 if ((Cache
->RequestData
!= NULL
) &&
780 (Cache
->RequestData
->Url
!= NULL
) &&
781 (StrCmp (Uri
, Cache
->RequestData
->Url
) == 0))
784 // Hit in cache, record image type.
786 *ImageType
= Cache
->ImageType
;
789 // Check buffer size.
791 if (*BufferSize
< Cache
->EntityLength
) {
792 *BufferSize
= Cache
->EntityLength
;
793 return EFI_BUFFER_TOO_SMALL
;
797 // Fill data to buffer.
800 NET_LIST_FOR_EACH (Entry2
, &Cache
->EntityDataList
) {
801 EntityData
= NET_LIST_USER_STRUCT (Entry2
, HTTP_BOOT_ENTITY_DATA
, Link
);
802 if (*BufferSize
> CopyedSize
) {
805 EntityData
->DataStart
,
806 MIN (EntityData
->DataLength
, *BufferSize
- CopyedSize
)
808 CopyedSize
+= MIN (EntityData
->DataLength
, *BufferSize
- CopyedSize
);
811 *BufferSize
= CopyedSize
;
816 return EFI_NOT_FOUND
;
820 A callback function to intercept events during message parser.
822 This function will be invoked during HttpParseMessageBody() with various events type. An error
823 return status of the callback function will cause the HttpParseMessageBody() aborted.
825 @param[in] EventType Event type of this callback call.
826 @param[in] Data A pointer to data buffer.
827 @param[in] Length Length in bytes of the Data.
828 @param[in] Context Callback context set by HttpInitMsgParser().
830 @retval EFI_SUCCESS Continue to parser the message body.
831 @retval Others Abort the parse.
836 HttpBootGetBootFileCallback (
837 IN HTTP_BODY_PARSE_EVENT EventType
,
843 HTTP_BOOT_CALLBACK_DATA
*CallbackData
;
844 HTTP_BOOT_ENTITY_DATA
*NewEntityData
;
846 EFI_HTTP_BOOT_CALLBACK_PROTOCOL
*HttpBootCallback
;
849 // We only care about the entity data.
851 if (EventType
!= BodyParseEventOnData
) {
855 CallbackData
= (HTTP_BOOT_CALLBACK_DATA
*)Context
;
856 HttpBootCallback
= CallbackData
->Private
->HttpBootCallback
;
857 if (HttpBootCallback
!= NULL
) {
858 Status
= HttpBootCallback
->Callback (
860 HttpBootHttpEntityBody
,
865 if (EFI_ERROR (Status
)) {
871 // Copy data if caller has provided a buffer.
873 if (CallbackData
->BufferSize
> CallbackData
->CopyedSize
) {
875 CallbackData
->Buffer
+ CallbackData
->CopyedSize
,
877 MIN (Length
, CallbackData
->BufferSize
- CallbackData
->CopyedSize
)
879 CallbackData
->CopyedSize
+= MIN (Length
, CallbackData
->BufferSize
- CallbackData
->CopyedSize
);
883 // The caller doesn't provide a buffer, save the block into cache list.
885 if (CallbackData
->Cache
!= NULL
) {
886 NewEntityData
= AllocatePool (sizeof (HTTP_BOOT_ENTITY_DATA
));
887 if (NewEntityData
== NULL
) {
888 return EFI_OUT_OF_RESOURCES
;
891 if (CallbackData
->NewBlock
) {
892 NewEntityData
->Block
= CallbackData
->Block
;
893 CallbackData
->Block
= NULL
;
896 NewEntityData
->DataLength
= Length
;
897 NewEntityData
->DataStart
= (UINT8
*)Data
;
898 InsertTailList (&CallbackData
->Cache
->EntityDataList
, &NewEntityData
->Link
);
905 This function download the boot file by using UEFI HTTP protocol.
907 @param[in] Private The pointer to the driver's private data.
908 @param[in] HeaderOnly Only request the response header, it could save a lot of time if
909 the caller only want to know the size of the requested file.
910 @param[in, out] BufferSize On input the size of Buffer in bytes. On output with a return
911 code of EFI_SUCCESS, the amount of data transferred to
912 Buffer. On output with a return code of EFI_BUFFER_TOO_SMALL,
913 the size of Buffer required to retrieve the requested file.
914 @param[out] Buffer The memory buffer to transfer the file to. IF Buffer is NULL,
915 then the size of the requested file is returned in
917 @param[out] ImageType The image type of the downloaded file.
919 @retval EFI_SUCCESS The file was loaded.
920 @retval EFI_INVALID_PARAMETER BufferSize is NULL or Buffer Size is not NULL but Buffer is NULL.
921 @retval EFI_OUT_OF_RESOURCES Could not allocate needed resources
922 @retval EFI_BUFFER_TOO_SMALL The BufferSize is too small to read the current directory entry.
923 BufferSize has been updated with the size needed to complete
925 @retval EFI_ACCESS_DENIED The server needs to authenticate the client.
926 @retval Others Unexpected error happened.
930 HttpBootGetBootFile (
931 IN HTTP_BOOT_PRIVATE_DATA
*Private
,
932 IN BOOLEAN HeaderOnly
,
933 IN OUT UINTN
*BufferSize
,
935 OUT HTTP_BOOT_IMAGE_TYPE
*ImageType
939 EFI_HTTP_STATUS_CODE StatusCode
;
941 EFI_HTTP_REQUEST_DATA
*RequestData
;
942 HTTP_IO_RESPONSE_DATA
*ResponseData
;
943 HTTP_IO_RESPONSE_DATA ResponseBody
;
945 HTTP_IO_HEADER
*HttpIoHeader
;
947 HTTP_BOOT_CALLBACK_DATA Context
;
949 HTTP_BOOT_CACHE_CONTENT
*Cache
;
953 BOOLEAN IdentityMode
;
955 CHAR8 BaseAuthValue
[80];
956 EFI_HTTP_HEADER
*HttpHeader
;
959 ASSERT (Private
!= NULL
);
960 ASSERT (Private
->HttpCreated
);
962 if ((BufferSize
== NULL
) || (ImageType
== NULL
)) {
963 return EFI_INVALID_PARAMETER
;
966 if ((*BufferSize
!= 0) && (Buffer
== NULL
)) {
967 return EFI_INVALID_PARAMETER
;
971 // First, check whether we already cached the requested Uri.
973 UrlSize
= AsciiStrSize (Private
->BootFileUri
);
974 Url
= AllocatePool (UrlSize
* sizeof (CHAR16
));
976 return EFI_OUT_OF_RESOURCES
;
979 AsciiStrToUnicodeStrS (Private
->BootFileUri
, Url
, UrlSize
);
980 if (!HeaderOnly
&& (Buffer
!= NULL
)) {
981 Status
= HttpBootGetFileFromCache (Private
, Url
, BufferSize
, Buffer
, ImageType
);
982 if (Status
!= EFI_NOT_FOUND
) {
989 // Not found in cache, try to download it through HTTP.
993 // 1. Create a temp cache item for the requested URI if caller doesn't provide buffer.
996 if ((!HeaderOnly
) && (*BufferSize
== 0)) {
997 Cache
= AllocateZeroPool (sizeof (HTTP_BOOT_CACHE_CONTENT
));
999 Status
= EFI_OUT_OF_RESOURCES
;
1003 Cache
->ImageType
= ImageTypeMax
;
1004 InitializeListHead (&Cache
->EntityDataList
);
1008 // 2. Send HTTP request message.
1012 // 2.1 Build HTTP header for the request, 3 header is needed to download a boot file:
1018 HttpIoHeader
= HttpIoCreateHeader ((Private
->AuthData
!= NULL
) ? 4 : 3);
1019 if (HttpIoHeader
== NULL
) {
1020 Status
= EFI_OUT_OF_RESOURCES
;
1025 // Add HTTP header field 1: Host
1028 Status
= HttpUrlGetHostName (
1029 Private
->BootFileUri
,
1030 Private
->BootFileUriParser
,
1033 if (EFI_ERROR (Status
)) {
1037 Status
= HttpIoSetHeader (
1042 FreePool (HostName
);
1043 if (EFI_ERROR (Status
)) {
1048 // Add HTTP header field 2: Accept
1050 Status
= HttpIoSetHeader (
1055 if (EFI_ERROR (Status
)) {
1060 // Add HTTP header field 3: User-Agent
1062 Status
= HttpIoSetHeader (
1064 HTTP_HEADER_USER_AGENT
,
1065 HTTP_USER_AGENT_EFI_HTTP_BOOT
1067 if (EFI_ERROR (Status
)) {
1072 // Add HTTP header field 4: Authorization
1074 if (Private
->AuthData
!= NULL
) {
1075 ASSERT (HttpIoHeader
->MaxHeaderCount
== 4);
1077 if ((Private
->AuthScheme
!= NULL
) && (CompareMem (Private
->AuthScheme
, "Basic", 5) != 0)) {
1078 Status
= EFI_UNSUPPORTED
;
1084 sizeof (BaseAuthValue
),
1090 Status
= HttpIoSetHeader (
1092 HTTP_HEADER_AUTHORIZATION
,
1095 if (EFI_ERROR (Status
)) {
1101 // 2.2 Build the rest of HTTP request info.
1103 RequestData
= AllocatePool (sizeof (EFI_HTTP_REQUEST_DATA
));
1104 if (RequestData
== NULL
) {
1105 Status
= EFI_OUT_OF_RESOURCES
;
1109 RequestData
->Method
= HeaderOnly
? HttpMethodHead
: HttpMethodGet
;
1110 RequestData
->Url
= Url
;
1113 // 2.3 Record the request info in a temp cache item.
1115 if (Cache
!= NULL
) {
1116 Cache
->RequestData
= RequestData
;
1120 // 2.4 Send out the request to HTTP server.
1122 HttpIo
= &Private
->HttpIo
;
1123 Status
= HttpIoSendRequest (
1126 HttpIoHeader
->HeaderCount
,
1127 HttpIoHeader
->Headers
,
1131 if (EFI_ERROR (Status
)) {
1136 // 3. Receive HTTP response message.
1140 // 3.1 First step, use zero BodyLength to only receive the response headers.
1142 ResponseData
= AllocateZeroPool (sizeof (HTTP_IO_RESPONSE_DATA
));
1143 if (ResponseData
== NULL
) {
1144 Status
= EFI_OUT_OF_RESOURCES
;
1149 Status
= HttpIoRecvResponse (
1154 if (EFI_ERROR (Status
) || EFI_ERROR (ResponseData
->Status
)) {
1155 if (EFI_ERROR (ResponseData
->Status
)) {
1156 StatusCode
= HttpIo
->RspToken
.Message
->Data
.Response
->StatusCode
;
1157 HttpBootPrintErrorMessage (StatusCode
);
1158 Status
= ResponseData
->Status
;
1159 if ((StatusCode
== HTTP_STATUS_401_UNAUTHORIZED
) || \
1160 (StatusCode
== HTTP_STATUS_407_PROXY_AUTHENTICATION_REQUIRED
))
1162 if ((Private
->AuthData
!= NULL
) || (Private
->AuthScheme
!= NULL
)) {
1163 if (Private
->AuthData
!= NULL
) {
1164 FreePool (Private
->AuthData
);
1165 Private
->AuthData
= NULL
;
1168 if (Private
->AuthScheme
!= NULL
) {
1169 FreePool (Private
->AuthScheme
);
1170 Private
->AuthScheme
= NULL
;
1173 Status
= EFI_ACCESS_DENIED
;
1178 // Server indicates the user has to provide a user-id and password as a means of identification.
1180 if (Private
->HttpBootCallback
!= NULL
) {
1181 Data
= AllocateZeroPool (sizeof (CHAR8
) * HTTP_BOOT_AUTHENTICATION_INFO_MAX_LEN
);
1183 Status
= EFI_OUT_OF_RESOURCES
;
1187 Status
= Private
->HttpBootCallback
->Callback (
1188 Private
->HttpBootCallback
,
1189 HttpBootHttpAuthInfo
,
1191 HTTP_BOOT_AUTHENTICATION_INFO_MAX_LEN
,
1194 if (EFI_ERROR (Status
)) {
1202 Private
->AuthData
= (CHAR8
*)Data
;
1205 HttpHeader
= HttpFindHeader (
1206 ResponseData
->HeaderCount
,
1207 ResponseData
->Headers
,
1208 HTTP_HEADER_WWW_AUTHENTICATE
1210 if (HttpHeader
!= NULL
) {
1211 Private
->AuthScheme
= AllocateZeroPool (AsciiStrLen (HttpHeader
->FieldValue
) + 1);
1212 if (Private
->AuthScheme
== NULL
) {
1213 return EFI_OUT_OF_RESOURCES
;
1216 CopyMem (Private
->AuthScheme
, HttpHeader
->FieldValue
, AsciiStrLen (HttpHeader
->FieldValue
));
1219 Status
= EFI_ACCESS_DENIED
;
1227 // Check the image type according to server's response.
1229 Status
= HttpBootCheckImageType (
1230 Private
->BootFileUri
,
1231 Private
->BootFileUriParser
,
1232 ResponseData
->HeaderCount
,
1233 ResponseData
->Headers
,
1236 if (EFI_ERROR (Status
)) {
1241 // 3.2 Cache the response header.
1243 if (Cache
!= NULL
) {
1244 Cache
->ResponseData
= ResponseData
;
1245 Cache
->ImageType
= *ImageType
;
1249 // 3.3 Init a message-body parser from the header information.
1252 Context
.NewBlock
= FALSE
;
1253 Context
.Block
= NULL
;
1254 Context
.CopyedSize
= 0;
1255 Context
.Buffer
= Buffer
;
1256 Context
.BufferSize
= *BufferSize
;
1257 Context
.Cache
= Cache
;
1258 Context
.Private
= Private
;
1259 Status
= HttpInitMsgParser (
1260 HeaderOnly
? HttpMethodHead
: HttpMethodGet
,
1261 ResponseData
->Response
.StatusCode
,
1262 ResponseData
->HeaderCount
,
1263 ResponseData
->Headers
,
1264 HttpBootGetBootFileCallback
,
1268 if (EFI_ERROR (Status
)) {
1273 // 3.4 Continue to receive and parse message-body if needed.
1278 // 3.4.1, check whether we are in identity transfer-coding.
1281 Status
= HttpGetEntityLength (Parser
, &ContentLength
);
1282 if (!EFI_ERROR (Status
)) {
1283 IdentityMode
= TRUE
;
1285 IdentityMode
= FALSE
;
1289 // 3.4.2, start the message-body download, the identity and chunked transfer-coding
1290 // is handled in different path here.
1292 ZeroMem (&ResponseBody
, sizeof (HTTP_IO_RESPONSE_DATA
));
1295 // In identity transfer-coding there is no need to parse the message body,
1296 // just download the message body to the user provided buffer directly.
1299 while (ReceivedSize
< ContentLength
) {
1300 ResponseBody
.Body
= (CHAR8
*)Buffer
+ ReceivedSize
;
1301 ResponseBody
.BodyLength
= *BufferSize
- ReceivedSize
;
1302 Status
= HttpIoRecvResponse (
1307 if (EFI_ERROR (Status
) || EFI_ERROR (ResponseBody
.Status
)) {
1308 if (EFI_ERROR (ResponseBody
.Status
)) {
1309 Status
= ResponseBody
.Status
;
1315 ReceivedSize
+= ResponseBody
.BodyLength
;
1316 if (Private
->HttpBootCallback
!= NULL
) {
1317 Status
= Private
->HttpBootCallback
->Callback (
1318 Private
->HttpBootCallback
,
1319 HttpBootHttpEntityBody
,
1321 (UINT32
)ResponseBody
.BodyLength
,
1324 if (EFI_ERROR (Status
)) {
1331 // In "chunked" transfer-coding mode, so we need to parse the received
1332 // data to get the real entity content.
1335 while (!HttpIsMessageComplete (Parser
)) {
1337 // Allocate a buffer in Block to hold the message-body.
1338 // If caller provides a buffer, this Block will be reused in every HttpIoRecvResponse().
1339 // Otherwise a buffer, the buffer in Block will be cached and we should allocate a new before
1340 // every HttpIoRecvResponse().
1342 if ((Block
== NULL
) || (Context
.BufferSize
== 0)) {
1343 Block
= AllocatePool (HTTP_BOOT_BLOCK_SIZE
);
1344 if (Block
== NULL
) {
1345 Status
= EFI_OUT_OF_RESOURCES
;
1349 Context
.NewBlock
= TRUE
;
1350 Context
.Block
= Block
;
1352 Context
.NewBlock
= FALSE
;
1355 ResponseBody
.Body
= (CHAR8
*)Block
;
1356 ResponseBody
.BodyLength
= HTTP_BOOT_BLOCK_SIZE
;
1357 Status
= HttpIoRecvResponse (
1362 if (EFI_ERROR (Status
) || EFI_ERROR (ResponseBody
.Status
)) {
1363 if (EFI_ERROR (ResponseBody
.Status
)) {
1364 Status
= ResponseBody
.Status
;
1371 // Parse the new received block of the message-body, the block will be saved in cache.
1373 Status
= HttpParseMessageBody (
1375 ResponseBody
.BodyLength
,
1378 if (EFI_ERROR (Status
)) {
1386 // 3.5 Message-body receive & parse is completed, we should be able to get the file size now.
1388 Status
= HttpGetEntityLength (Parser
, &ContentLength
);
1389 if (EFI_ERROR (Status
)) {
1393 if (*BufferSize
< ContentLength
) {
1394 Status
= EFI_BUFFER_TOO_SMALL
;
1396 Status
= EFI_SUCCESS
;
1399 *BufferSize
= ContentLength
;
1402 // 4. Save the cache item to driver's cache list and return.
1404 if (Cache
!= NULL
) {
1405 Cache
->EntityLength
= ContentLength
;
1406 InsertTailList (&Private
->CacheList
, &Cache
->Link
);
1409 if (Parser
!= NULL
) {
1410 HttpFreeMsgParser (Parser
);
1416 if (Parser
!= NULL
) {
1417 HttpFreeMsgParser (Parser
);
1420 if (Context
.Block
!= NULL
) {
1421 FreePool (Context
.Block
);
1424 HttpBootFreeCache (Cache
);
1427 if (ResponseData
!= NULL
) {
1428 FreePool (ResponseData
);
1432 if (RequestData
!= NULL
) {
1433 FreePool (RequestData
);
1437 HttpIoFreeHeader (HttpIoHeader
);
1439 if (Cache
!= NULL
) {