2 The implementation of EFI_LOAD_FILE_PROTOCOL for UEFI HTTP boot.
4 Copyright (c) 2015 - 2018, 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 Install HTTP Boot Callback Protocol if not installed before.
15 @param[in] Private Pointer to HTTP Boot private data.
17 @retval EFI_SUCCESS HTTP Boot Callback Protocol installed succesfully.
18 @retval Others Failed to install HTTP Boot Callback Protocol.
22 HttpBootInstallCallback (
23 IN HTTP_BOOT_PRIVATE_DATA
*Private
27 EFI_HANDLE ControllerHandle
;
29 if (!Private
->UsingIpv6
) {
30 ControllerHandle
= Private
->Ip4Nic
->Controller
;
32 ControllerHandle
= Private
->Ip6Nic
->Controller
;
36 // Check whether gEfiHttpBootCallbackProtocolGuid already installed.
38 Status
= gBS
->HandleProtocol (
40 &gEfiHttpBootCallbackProtocolGuid
,
41 (VOID
**) &Private
->HttpBootCallback
43 if (Status
== EFI_UNSUPPORTED
) {
46 &Private
->LoadFileCallback
,
47 &gHttpBootDxeHttpBootCallback
,
48 sizeof (EFI_HTTP_BOOT_CALLBACK_PROTOCOL
)
52 // Install a default callback if user didn't offer one.
54 Status
= gBS
->InstallProtocolInterface (
56 &gEfiHttpBootCallbackProtocolGuid
,
58 &Private
->LoadFileCallback
60 if (EFI_ERROR (Status
)) {
63 Private
->HttpBootCallback
= &Private
->LoadFileCallback
;
70 Uninstall HTTP Boot Callback Protocol if it's installed by this driver.
72 @param[in] Private Pointer to HTTP Boot private data.
76 HttpBootUninstallCallback (
77 IN HTTP_BOOT_PRIVATE_DATA
*Private
80 if (Private
->HttpBootCallback
== &Private
->LoadFileCallback
) {
81 gBS
->UninstallProtocolInterface (
83 &gEfiHttpBootCallbackProtocolGuid
,
84 &Private
->HttpBootCallback
86 Private
->HttpBootCallback
= NULL
;
91 Enable the use of UEFI HTTP boot function.
93 If the driver has already been started but not satisfy the requirement (IP stack and
94 specified boot file path), this function will stop the driver and start it again.
96 @param[in] Private The pointer to the driver's private data.
97 @param[in] UsingIpv6 Specifies the type of IP addresses that are to be
98 used during the session that is being started.
99 Set to TRUE for IPv6, and FALSE for IPv4.
100 @param[in] FilePath The device specific path of the file to load.
102 @retval EFI_SUCCESS HTTP boot was successfully enabled.
103 @retval EFI_INVALID_PARAMETER Private is NULL or FilePath is NULL.
104 @retval EFI_INVALID_PARAMETER The FilePath doesn't contain a valid URI device path node.
105 @retval EFI_ALREADY_STARTED The driver is already in started state.
106 @retval EFI_OUT_OF_RESOURCES There are not enough resources.
111 IN HTTP_BOOT_PRIVATE_DATA
*Private
,
112 IN BOOLEAN UsingIpv6
,
113 IN EFI_DEVICE_PATH_PROTOCOL
*FilePath
122 if (Private
== NULL
|| FilePath
== NULL
) {
123 return EFI_INVALID_PARAMETER
;
127 // Check the URI in the input FilePath, in order to see whether it is
128 // required to boot from a new specified boot file.
130 Status
= HttpBootParseFilePath (FilePath
, &Uri
);
131 if (EFI_ERROR (Status
)) {
132 return EFI_INVALID_PARAMETER
;
136 // Check whether we need to stop and restart the HTTP boot driver.
138 if (Private
->Started
) {
140 // Restart is needed in 2 cases:
141 // 1. Http boot driver has already been started but not on the required IP stack.
142 // 2. The specified boot file URI in FilePath is different with the one we have
145 if ((UsingIpv6
!= Private
->UsingIpv6
) ||
146 ((Uri
!= NULL
) && (AsciiStrCmp (Private
->BootFileUri
, Uri
) != 0))) {
148 // Restart is required, first stop then continue this start function.
150 Status
= HttpBootStop (Private
);
151 if (EFI_ERROR (Status
)) {
159 // Restart is not required.
164 return EFI_ALREADY_STARTED
;
169 // Detect whether using ipv6 or not, and set it to the private data.
171 if (UsingIpv6
&& Private
->Ip6Nic
!= NULL
) {
172 Private
->UsingIpv6
= TRUE
;
173 } else if (!UsingIpv6
&& Private
->Ip4Nic
!= NULL
) {
174 Private
->UsingIpv6
= FALSE
;
179 return EFI_UNSUPPORTED
;
183 // Record the specified URI and prepare the URI parser if needed.
185 Private
->FilePathUri
= Uri
;
186 if (Private
->FilePathUri
!= NULL
) {
187 Status
= HttpParseUrl (
188 Private
->FilePathUri
,
189 (UINT32
) AsciiStrLen (Private
->FilePathUri
),
191 &Private
->FilePathUriParser
193 if (EFI_ERROR (Status
)) {
194 FreePool (Private
->FilePathUri
);
200 // Init the content of cached DHCP offer list.
202 ZeroMem (Private
->OfferBuffer
, sizeof (Private
->OfferBuffer
));
203 if (!Private
->UsingIpv6
) {
204 for (Index
= 0; Index
< HTTP_BOOT_OFFER_MAX_NUM
; Index
++) {
205 Private
->OfferBuffer
[Index
].Dhcp4
.Packet
.Offer
.Size
= HTTP_CACHED_DHCP4_PACKET_MAX_SIZE
;
208 for (Index
= 0; Index
< HTTP_BOOT_OFFER_MAX_NUM
; Index
++) {
209 Private
->OfferBuffer
[Index
].Dhcp6
.Packet
.Offer
.Size
= HTTP_CACHED_DHCP6_PACKET_MAX_SIZE
;
213 if (Private
->UsingIpv6
) {
215 // Set Ip6 policy to Automatic to start the Ip6 router discovery.
217 Status
= HttpBootSetIp6Policy (Private
);
218 if (EFI_ERROR (Status
)) {
222 Private
->Started
= TRUE
;
223 Print (L
"\n>>Start HTTP Boot over IPv%d", Private
->UsingIpv6
? 6 : 4);
229 Attempt to complete a DHCPv4 D.O.R.A or DHCPv6 S.R.A.A sequence to retrieve the boot resource information.
231 @param[in] Private The pointer to the driver's private data.
233 @retval EFI_SUCCESS Boot info was successfully retrieved.
234 @retval EFI_INVALID_PARAMETER Private is NULL.
235 @retval EFI_NOT_STARTED The driver is in stopped state.
236 @retval EFI_DEVICE_ERROR An unexpected network error occurred.
237 @retval Others Other errors as indicated.
242 IN HTTP_BOOT_PRIVATE_DATA
*Private
247 if (Private
== NULL
) {
248 return EFI_INVALID_PARAMETER
;
251 if (!Private
->Started
) {
252 return EFI_NOT_STARTED
;
255 Status
= EFI_DEVICE_ERROR
;
257 if (!Private
->UsingIpv6
) {
259 // Start D.O.R.A process to get a IPv4 address and other boot information.
261 Status
= HttpBootDhcp4Dora (Private
);
264 // Start S.A.R.R process to get a IPv6 address and other boot information.
266 Status
= HttpBootDhcp6Sarr (Private
);
273 Attempt to download the boot file through HTTP message exchange.
275 @param[in] Private The pointer to the driver's private data.
276 @param[in, out] BufferSize On input the size of Buffer in bytes. On output with a return
277 code of EFI_SUCCESS, the amount of data transferred to
278 Buffer. On output with a return code of EFI_BUFFER_TOO_SMALL,
279 the size of Buffer required to retrieve the requested file.
280 @param[in] Buffer The memory buffer to transfer the file to. If Buffer is NULL,
281 then the size of the requested file is returned in
283 @param[out] ImageType The image type of the downloaded file.
285 @retval EFI_SUCCESS Boot file was loaded successfully.
286 @retval EFI_INVALID_PARAMETER Private is NULL, or ImageType is NULL, or BufferSize is NULL.
287 @retval EFI_INVALID_PARAMETER *BufferSize is not zero, and Buffer is NULL.
288 @retval EFI_NOT_STARTED The driver is in stopped state.
289 @retval EFI_BUFFER_TOO_SMALL The BufferSize is too small to read the boot file. BufferSize has
290 been updated with the size needed to complete the request.
291 @retval EFI_DEVICE_ERROR An unexpected network error occurred.
292 @retval Others Other errors as indicated.
297 IN HTTP_BOOT_PRIVATE_DATA
*Private
,
298 IN OUT UINTN
*BufferSize
,
299 IN VOID
*Buffer
, OPTIONAL
300 OUT HTTP_BOOT_IMAGE_TYPE
*ImageType
305 if (Private
== NULL
|| ImageType
== NULL
|| BufferSize
== NULL
) {
306 return EFI_INVALID_PARAMETER
;
309 if (*BufferSize
!= 0 && Buffer
== NULL
) {
310 return EFI_INVALID_PARAMETER
;
313 if (!Private
->Started
) {
314 return EFI_NOT_STARTED
;
317 Status
= HttpBootInstallCallback (Private
);
318 if (EFI_ERROR(Status
)) {
322 if (Private
->BootFileUri
== NULL
) {
324 // Parse the cached offer to get the boot file URL first.
326 Status
= HttpBootDiscoverBootInfo (Private
);
327 if (EFI_ERROR (Status
)) {
328 AsciiPrint ("\n Error: Could not retrieve NBP file size from HTTP server.\n");
333 if (!Private
->HttpCreated
) {
335 // Create HTTP child.
337 Status
= HttpBootCreateHttpIo (Private
);
338 if (EFI_ERROR (Status
)) {
343 if (Private
->BootFileSize
== 0) {
345 // Discover the information about the bootfile if we haven't.
349 // Try to use HTTP HEAD method.
351 Status
= HttpBootGetBootFile (
354 &Private
->BootFileSize
,
358 if (EFI_ERROR (Status
) && Status
!= EFI_BUFFER_TOO_SMALL
) {
360 // Failed to get file size by HEAD method, may be trunked encoding, try HTTP GET method.
362 ASSERT (Private
->BootFileSize
== 0);
363 Status
= HttpBootGetBootFile (
366 &Private
->BootFileSize
,
370 if (EFI_ERROR (Status
) && Status
!= EFI_BUFFER_TOO_SMALL
) {
371 AsciiPrint ("\n Error: Could not retrieve NBP file size from HTTP server.\n");
377 if (*BufferSize
< Private
->BootFileSize
) {
378 *BufferSize
= Private
->BootFileSize
;
379 *ImageType
= Private
->ImageType
;
380 Status
= EFI_BUFFER_TOO_SMALL
;
385 // Load the boot file into Buffer
387 Status
= HttpBootGetBootFile (
396 HttpBootUninstallCallback (Private
);
398 if (EFI_ERROR (Status
)) {
399 if (Status
== EFI_ACCESS_DENIED
) {
400 AsciiPrint ("\n Error: Could not establish connection with HTTP server.\n");
401 } else if (Status
== EFI_BUFFER_TOO_SMALL
&& Buffer
!= NULL
) {
402 AsciiPrint ("\n Error: Buffer size is smaller than the requested file.\n");
403 } else if (Status
== EFI_OUT_OF_RESOURCES
) {
404 AsciiPrint ("\n Error: Could not allocate I/O buffers.\n");
405 } else if (Status
== EFI_DEVICE_ERROR
) {
406 AsciiPrint ("\n Error: Network device error.\n");
407 } else if (Status
== EFI_TIMEOUT
) {
408 AsciiPrint ("\n Error: Server response timeout.\n");
409 } else if (Status
== EFI_ABORTED
) {
410 AsciiPrint ("\n Error: Remote boot cancelled.\n");
411 } else if (Status
!= EFI_BUFFER_TOO_SMALL
) {
412 AsciiPrint ("\n Error: Unexpected network error.\n");
420 Disable the use of UEFI HTTP boot function.
422 @param[in] Private The pointer to the driver's private data.
424 @retval EFI_SUCCESS HTTP boot was successfully disabled.
425 @retval EFI_NOT_STARTED The driver is already in stopped state.
426 @retval EFI_INVALID_PARAMETER Private is NULL.
427 @retval Others Unexpected error when stop the function.
432 IN HTTP_BOOT_PRIVATE_DATA
*Private
437 if (Private
== NULL
) {
438 return EFI_INVALID_PARAMETER
;
441 if (!Private
->Started
) {
442 return EFI_NOT_STARTED
;
445 if (Private
->HttpCreated
) {
446 HttpIoDestroyIo (&Private
->HttpIo
);
447 Private
->HttpCreated
= FALSE
;
450 Private
->Started
= FALSE
;
451 ZeroMem (&Private
->StationIp
, sizeof (EFI_IP_ADDRESS
));
452 ZeroMem (&Private
->SubnetMask
, sizeof (EFI_IP_ADDRESS
));
453 ZeroMem (&Private
->GatewayIp
, sizeof (EFI_IP_ADDRESS
));
455 Private
->BootFileUri
= NULL
;
456 Private
->BootFileUriParser
= NULL
;
457 Private
->BootFileSize
= 0;
458 Private
->SelectIndex
= 0;
459 Private
->SelectProxyType
= HttpOfferTypeMax
;
461 if (!Private
->UsingIpv6
) {
463 // Stop and release the DHCP4 child.
465 Private
->Dhcp4
->Stop (Private
->Dhcp4
);
466 Private
->Dhcp4
->Configure (Private
->Dhcp4
, NULL
);
468 for (Index
= 0; Index
< HTTP_BOOT_OFFER_MAX_NUM
; Index
++) {
469 if (Private
->OfferBuffer
[Index
].Dhcp4
.UriParser
) {
470 HttpUrlFreeParser (Private
->OfferBuffer
[Index
].Dhcp4
.UriParser
);
475 // Stop and release the DHCP6 child.
477 Private
->Dhcp6
->Stop (Private
->Dhcp6
);
478 Private
->Dhcp6
->Configure (Private
->Dhcp6
, NULL
);
480 for (Index
= 0; Index
< HTTP_BOOT_OFFER_MAX_NUM
; Index
++) {
481 if (Private
->OfferBuffer
[Index
].Dhcp6
.UriParser
) {
482 HttpUrlFreeParser (Private
->OfferBuffer
[Index
].Dhcp6
.UriParser
);
487 if (Private
->DnsServerIp
!= NULL
) {
488 FreePool (Private
->DnsServerIp
);
489 Private
->DnsServerIp
= NULL
;
492 if (Private
->FilePathUri
!= NULL
) {
493 FreePool (Private
->FilePathUri
);
494 HttpUrlFreeParser (Private
->FilePathUriParser
);
495 Private
->FilePathUri
= NULL
;
496 Private
->FilePathUriParser
= NULL
;
499 ZeroMem (Private
->OfferBuffer
, sizeof (Private
->OfferBuffer
));
500 Private
->OfferNum
= 0;
501 ZeroMem (Private
->OfferCount
, sizeof (Private
->OfferCount
));
502 ZeroMem (Private
->OfferIndex
, sizeof (Private
->OfferIndex
));
504 HttpBootFreeCacheList (Private
);
510 Causes the driver to load a specified file.
512 @param This Protocol instance pointer.
513 @param FilePath The device specific path of the file to load.
514 @param BootPolicy If TRUE, indicates that the request originates from the
515 boot manager is attempting to load FilePath as a boot
516 selection. If FALSE, then FilePath must match as exact file
518 @param BufferSize On input the size of Buffer in bytes. On output with a return
519 code of EFI_SUCCESS, the amount of data transferred to
520 Buffer. On output with a return code of EFI_BUFFER_TOO_SMALL,
521 the size of Buffer required to retrieve the requested file.
522 @param Buffer The memory buffer to transfer the file to. IF Buffer is NULL,
523 then the size of the requested file is returned in
526 @retval EFI_SUCCESS The file was loaded.
527 @retval EFI_UNSUPPORTED The device does not support the provided BootPolicy
528 @retval EFI_INVALID_PARAMETER FilePath is not a valid device path, or
530 @retval EFI_NO_MEDIA No medium was present to load the file.
531 @retval EFI_DEVICE_ERROR The file was not loaded due to a device error.
532 @retval EFI_NO_RESPONSE The remote system did not respond.
533 @retval EFI_NOT_FOUND The file was not found.
534 @retval EFI_ABORTED The file load process was manually cancelled.
535 @retval EFI_BUFFER_TOO_SMALL The BufferSize is too small to read the current directory entry.
536 BufferSize has been updated with the size needed to complete
542 HttpBootDxeLoadFile (
543 IN EFI_LOAD_FILE_PROTOCOL
*This
,
544 IN EFI_DEVICE_PATH_PROTOCOL
*FilePath
,
545 IN BOOLEAN BootPolicy
,
546 IN OUT UINTN
*BufferSize
,
547 IN VOID
*Buffer OPTIONAL
550 HTTP_BOOT_PRIVATE_DATA
*Private
;
551 HTTP_BOOT_VIRTUAL_NIC
*VirtualNic
;
552 EFI_STATUS MediaStatus
;
555 HTTP_BOOT_IMAGE_TYPE ImageType
;
557 if (This
== NULL
|| BufferSize
== NULL
|| FilePath
== NULL
) {
558 return EFI_INVALID_PARAMETER
;
562 // Only support BootPolicy
565 return EFI_UNSUPPORTED
;
568 VirtualNic
= HTTP_BOOT_VIRTUAL_NIC_FROM_LOADFILE (This
);
569 Private
= VirtualNic
->Private
;
572 // Check media status before HTTP boot start
574 MediaStatus
= EFI_SUCCESS
;
575 NetLibDetectMediaWaitTimeout (Private
->Controller
, HTTP_BOOT_CHECK_MEDIA_WAITING_TIME
, &MediaStatus
);
576 if (MediaStatus
!= EFI_SUCCESS
) {
577 AsciiPrint ("\n Error: Could not detect network connection.\n");
582 // Check whether the virtual nic is using IPv6 or not.
585 if (VirtualNic
== Private
->Ip6Nic
) {
590 // Initialize HTTP boot.
592 Status
= HttpBootStart (Private
, UsingIpv6
, FilePath
);
593 if (Status
!= EFI_SUCCESS
&& Status
!= EFI_ALREADY_STARTED
) {
598 // Load the boot file.
600 ImageType
= ImageTypeMax
;
601 Status
= HttpBootLoadFile (Private
, BufferSize
, Buffer
, &ImageType
);
602 if (EFI_ERROR (Status
)) {
603 if (Status
== EFI_BUFFER_TOO_SMALL
&& (ImageType
== ImageTypeVirtualCd
|| ImageType
== ImageTypeVirtualDisk
)) {
604 Status
= EFI_WARN_FILE_SYSTEM
;
605 } else if (Status
!= EFI_BUFFER_TOO_SMALL
) {
606 HttpBootStop (Private
);
612 // Register the RAM Disk to the system if needed.
614 if (ImageType
== ImageTypeVirtualCd
|| ImageType
== ImageTypeVirtualDisk
) {
615 Status
= HttpBootRegisterRamDisk (Private
, *BufferSize
, Buffer
, ImageType
);
616 if (!EFI_ERROR (Status
)) {
617 Status
= EFI_WARN_FILE_SYSTEM
;
619 AsciiPrint ("\n Error: Could not register RAM disk to the system.\n");
624 // Stop the HTTP Boot service after the boot image is downloaded.
626 HttpBootStop (Private
);
631 /// Load File Protocol instance
633 GLOBAL_REMOVE_IF_UNREFERENCED
634 EFI_LOAD_FILE_PROTOCOL gHttpBootDxeLoadFile
= {
639 Callback function that is invoked when the HTTP Boot driver is about to transmit or has received a
642 This function is invoked when the HTTP Boot driver is about to transmit or has received packet.
643 Parameters DataType and Received specify the type of event and the format of the buffer pointed
644 to by Data. Due to the polling nature of UEFI device drivers, this callback function should not
645 execute for more than 5 ms.
646 The returned status code determines the behavior of the HTTP Boot driver.
648 @param[in] This Pointer to the EFI_HTTP_BOOT_CALLBACK_PROTOCOL instance.
649 @param[in] DataType The event that occurs in the current state.
650 @param[in] Received TRUE if the callback is being invoked due to a receive event.
651 FALSE if the callback is being invoked due to a transmit event.
652 @param[in] DataLength The length in bytes of the buffer pointed to by Data.
653 @param[in] Data A pointer to the buffer of data, the data type is specified by
656 @retval EFI_SUCCESS Tells the HTTP Boot driver to continue the HTTP Boot process.
657 @retval EFI_ABORTED Tells the HTTP Boot driver to abort the current HTTP Boot process.
662 IN EFI_HTTP_BOOT_CALLBACK_PROTOCOL
*This
,
663 IN EFI_HTTP_BOOT_CALLBACK_DATA_TYPE DataType
,
665 IN UINT32 DataLength
,
666 IN VOID
*Data OPTIONAL
669 EFI_HTTP_MESSAGE
*HttpMessage
;
670 EFI_HTTP_HEADER
*HttpHeader
;
671 HTTP_BOOT_PRIVATE_DATA
*Private
;
674 Private
= HTTP_BOOT_PRIVATE_DATA_FROM_CALLBACK_PROTOCOL(This
);
682 case HttpBootHttpRequest
:
684 HttpMessage
= (EFI_HTTP_MESSAGE
*) Data
;
685 if (HttpMessage
->Data
.Request
->Method
== HttpMethodGet
&&
686 HttpMessage
->Data
.Request
->Url
!= NULL
) {
687 Print (L
"\n URI: %s\n", HttpMessage
->Data
.Request
->Url
);
692 case HttpBootHttpResponse
:
694 HttpMessage
= (EFI_HTTP_MESSAGE
*) Data
;
696 if (HttpMessage
->Data
.Response
!= NULL
) {
697 if (HttpBootIsHttpRedirectStatusCode (HttpMessage
->Data
.Response
->StatusCode
)) {
699 // Server indicates the resource has been redirected to a different URL
700 // according to the section 6.4 of RFC7231 and the RFC 7538.
701 // Display the redirect information on the screen.
703 HttpHeader
= HttpFindHeader (
704 HttpMessage
->HeaderCount
,
705 HttpMessage
->Headers
,
708 if (HttpHeader
!= NULL
) {
709 Print (L
"\n HTTP ERROR: Resource Redirected.\n New Location: %a\n", HttpHeader
->FieldValue
);
715 HttpHeader
= HttpFindHeader (
716 HttpMessage
->HeaderCount
,
717 HttpMessage
->Headers
,
718 HTTP_HEADER_CONTENT_LENGTH
720 if (HttpHeader
!= NULL
) {
721 Private
->FileSize
= AsciiStrDecimalToUintn (HttpHeader
->FieldValue
);
722 Private
->ReceivedSize
= 0;
723 Private
->Percentage
= 0;
728 case HttpBootHttpEntityBody
:
729 if (DataLength
!= 0) {
730 if (Private
->FileSize
!= 0) {
732 // We already know the file size, print in percentage format.
734 if (Private
->ReceivedSize
== 0) {
735 Print (L
" File Size: %lu Bytes\n", Private
->FileSize
);
737 Private
->ReceivedSize
+= DataLength
;
738 Percentage
= (UINT32
) DivU64x64Remainder (MultU64x32 (Private
->ReceivedSize
, 100), Private
->FileSize
, NULL
);
739 if (Private
->Percentage
!= Percentage
) {
740 Private
->Percentage
= Percentage
;
741 Print (L
"\r Downloading...%d%%", Percentage
);
745 // In some case we couldn't get the file size from the HTTP header, so we
746 // just print the downloaded file size.
748 Private
->ReceivedSize
+= DataLength
;
749 Print (L
"\r Downloading...%lu Bytes", Private
->ReceivedSize
);
762 /// HTTP Boot Callback Protocol instance
764 GLOBAL_REMOVE_IF_UNREFERENCED
765 EFI_HTTP_BOOT_CALLBACK_PROTOCOL gHttpBootDxeHttpBootCallback
= {