2 The implementation of EFI_LOAD_FILE_PROTOCOL for UEFI HTTP boot.
4 Copyright (c) 2015 - 2017, Intel Corporation. All rights reserved.<BR>
5 (C) Copyright 2016 Hewlett Packard Enterprise Development LP<BR>
6 This program and the accompanying materials are licensed and made available under
7 the terms and conditions of the BSD License that accompanies this distribution.
8 The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php.
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
16 #include "HttpBootDxe.h"
19 Install HTTP Boot Callback Protocol if not installed before.
21 @param[in] Private Pointer to HTTP Boot private data.
23 @retval EFI_SUCCESS HTTP Boot Callback Protocol installed succesfully.
24 @retval Others Failed to install HTTP Boot Callback Protocol.
28 HttpBootInstallCallback (
29 IN HTTP_BOOT_PRIVATE_DATA
*Private
33 EFI_HANDLE ControllerHandle
;
35 if (!Private
->UsingIpv6
) {
36 ControllerHandle
= Private
->Ip4Nic
->Controller
;
38 ControllerHandle
= Private
->Ip6Nic
->Controller
;
42 // Check whether gEfiHttpBootCallbackProtocolGuid already installed.
44 Status
= gBS
->HandleProtocol (
46 &gEfiHttpBootCallbackProtocolGuid
,
47 (VOID
**) &Private
->HttpBootCallback
49 if (Status
== EFI_UNSUPPORTED
) {
52 &Private
->LoadFileCallback
,
53 &gHttpBootDxeHttpBootCallback
,
54 sizeof (EFI_HTTP_BOOT_CALLBACK_PROTOCOL
)
58 // Install a default callback if user didn't offer one.
60 Status
= gBS
->InstallProtocolInterface (
62 &gEfiHttpBootCallbackProtocolGuid
,
64 &Private
->LoadFileCallback
66 if (EFI_ERROR (Status
)) {
69 Private
->HttpBootCallback
= &Private
->LoadFileCallback
;
76 Uninstall HTTP Boot Callback Protocol if it's installed by this driver.
78 @param[in] Private Pointer to HTTP Boot private data.
82 HttpBootUninstallCallback (
83 IN HTTP_BOOT_PRIVATE_DATA
*Private
86 if (Private
->HttpBootCallback
== &Private
->LoadFileCallback
) {
87 gBS
->UninstallProtocolInterface (
89 &gEfiHttpBootCallbackProtocolGuid
,
90 &Private
->HttpBootCallback
92 Private
->HttpBootCallback
= NULL
;
97 Enable the use of UEFI HTTP boot function.
99 If the driver has already been started but not satisfy the requirement (IP stack and
100 specified boot file path), this function will stop the driver and start it again.
102 @param[in] Private The pointer to the driver's private data.
103 @param[in] UsingIpv6 Specifies the type of IP addresses that are to be
104 used during the session that is being started.
105 Set to TRUE for IPv6, and FALSE for IPv4.
106 @param[in] FilePath The device specific path of the file to load.
108 @retval EFI_SUCCESS HTTP boot was successfully enabled.
109 @retval EFI_INVALID_PARAMETER Private is NULL or FilePath is NULL.
110 @retval EFI_INVALID_PARAMETER The FilePath doesn't contain a valid URI device path node.
111 @retval EFI_ALREADY_STARTED The driver is already in started state.
112 @retval EFI_OUT_OF_RESOURCES There are not enough resources.
117 IN HTTP_BOOT_PRIVATE_DATA
*Private
,
118 IN BOOLEAN UsingIpv6
,
119 IN EFI_DEVICE_PATH_PROTOCOL
*FilePath
128 if (Private
== NULL
|| FilePath
== NULL
) {
129 return EFI_INVALID_PARAMETER
;
133 // Check the URI in the input FilePath, in order to see whether it is
134 // required to boot from a new specified boot file.
136 Status
= HttpBootParseFilePath (FilePath
, &Uri
);
137 if (EFI_ERROR (Status
)) {
138 return EFI_INVALID_PARAMETER
;
142 // Check whether we need to stop and restart the HTTP boot driver.
144 if (Private
->Started
) {
146 // Restart is needed in 2 cases:
147 // 1. Http boot driver has already been started but not on the required IP stack.
148 // 2. The specified boot file URI in FilePath is different with the one we have
151 if ((UsingIpv6
!= Private
->UsingIpv6
) ||
152 ((Uri
!= NULL
) && (AsciiStrCmp (Private
->BootFileUri
, Uri
) != 0))) {
154 // Restart is required, first stop then continue this start function.
156 Status
= HttpBootStop (Private
);
157 if (EFI_ERROR (Status
)) {
165 // Restart is not required.
170 return EFI_ALREADY_STARTED
;
175 // Detect whether using ipv6 or not, and set it to the private data.
177 if (UsingIpv6
&& Private
->Ip6Nic
!= NULL
) {
178 Private
->UsingIpv6
= TRUE
;
179 } else if (!UsingIpv6
&& Private
->Ip4Nic
!= NULL
) {
180 Private
->UsingIpv6
= FALSE
;
185 return EFI_UNSUPPORTED
;
189 // Record the specified URI and prepare the URI parser if needed.
191 Private
->FilePathUri
= Uri
;
192 if (Private
->FilePathUri
!= NULL
) {
193 Status
= HttpParseUrl (
194 Private
->FilePathUri
,
195 (UINT32
) AsciiStrLen (Private
->FilePathUri
),
197 &Private
->FilePathUriParser
199 if (EFI_ERROR (Status
)) {
200 FreePool (Private
->FilePathUri
);
206 // Init the content of cached DHCP offer list.
208 ZeroMem (Private
->OfferBuffer
, sizeof (Private
->OfferBuffer
));
209 if (!Private
->UsingIpv6
) {
210 for (Index
= 0; Index
< HTTP_BOOT_OFFER_MAX_NUM
; Index
++) {
211 Private
->OfferBuffer
[Index
].Dhcp4
.Packet
.Offer
.Size
= HTTP_CACHED_DHCP4_PACKET_MAX_SIZE
;
214 for (Index
= 0; Index
< HTTP_BOOT_OFFER_MAX_NUM
; Index
++) {
215 Private
->OfferBuffer
[Index
].Dhcp6
.Packet
.Offer
.Size
= HTTP_CACHED_DHCP6_PACKET_MAX_SIZE
;
219 if (Private
->UsingIpv6
) {
221 // Set Ip6 policy to Automatic to start the Ip6 router discovery.
223 Status
= HttpBootSetIp6Policy (Private
);
224 if (EFI_ERROR (Status
)) {
228 Private
->Started
= TRUE
;
229 Print (L
"\n>>Start HTTP Boot over IPv%d", Private
->UsingIpv6
? 6 : 4);
235 Attempt to complete a DHCPv4 D.O.R.A or DHCPv6 S.R.A.A sequence to retrieve the boot resource information.
237 @param[in] Private The pointer to the driver's private data.
239 @retval EFI_SUCCESS Boot info was successfully retrieved.
240 @retval EFI_INVALID_PARAMETER Private is NULL.
241 @retval EFI_NOT_STARTED The driver is in stopped state.
242 @retval EFI_DEVICE_ERROR An unexpected network error occurred.
243 @retval Others Other errors as indicated.
248 IN HTTP_BOOT_PRIVATE_DATA
*Private
253 if (Private
== NULL
) {
254 return EFI_INVALID_PARAMETER
;
257 if (!Private
->Started
) {
258 return EFI_NOT_STARTED
;
261 Status
= EFI_DEVICE_ERROR
;
263 if (!Private
->UsingIpv6
) {
265 // Start D.O.R.A process to get a IPv4 address and other boot information.
267 Status
= HttpBootDhcp4Dora (Private
);
270 // Start S.A.R.R process to get a IPv6 address and other boot information.
272 Status
= HttpBootDhcp6Sarr (Private
);
279 Attempt to download the boot file through HTTP message exchange.
281 @param[in] Private The pointer to the driver's private data.
282 @param[in, out] BufferSize On input the size of Buffer in bytes. On output with a return
283 code of EFI_SUCCESS, the amount of data transferred to
284 Buffer. On output with a return code of EFI_BUFFER_TOO_SMALL,
285 the size of Buffer required to retrieve the requested file.
286 @param[in] Buffer The memory buffer to transfer the file to. If Buffer is NULL,
287 then the size of the requested file is returned in
289 @param[out] ImageType The image type of the downloaded file.
291 @retval EFI_SUCCESS Boot file was loaded successfully.
292 @retval EFI_INVALID_PARAMETER Private is NULL, or ImageType is NULL, or BufferSize is NULL.
293 @retval EFI_INVALID_PARAMETER *BufferSize is not zero, and Buffer is NULL.
294 @retval EFI_NOT_STARTED The driver is in stopped state.
295 @retval EFI_BUFFER_TOO_SMALL The BufferSize is too small to read the boot file. BufferSize has
296 been updated with the size needed to complete the request.
297 @retval EFI_DEVICE_ERROR An unexpected network error occurred.
298 @retval Others Other errors as indicated.
303 IN HTTP_BOOT_PRIVATE_DATA
*Private
,
304 IN OUT UINTN
*BufferSize
,
305 IN VOID
*Buffer
, OPTIONAL
306 OUT HTTP_BOOT_IMAGE_TYPE
*ImageType
311 if (Private
== NULL
|| ImageType
== NULL
|| BufferSize
== NULL
) {
312 return EFI_INVALID_PARAMETER
;
315 if (*BufferSize
!= 0 && Buffer
== NULL
) {
316 return EFI_INVALID_PARAMETER
;
319 if (!Private
->Started
) {
320 return EFI_NOT_STARTED
;
323 Status
= HttpBootInstallCallback (Private
);
324 if (EFI_ERROR(Status
)) {
328 if (Private
->BootFileUri
== NULL
) {
330 // Parse the cached offer to get the boot file URL first.
332 Status
= HttpBootDiscoverBootInfo (Private
);
333 if (EFI_ERROR (Status
)) {
334 AsciiPrint ("\n Error: Could not discover the boot information for DHCP server.\n");
339 if (!Private
->HttpCreated
) {
341 // Create HTTP child.
343 Status
= HttpBootCreateHttpIo (Private
);
344 if (EFI_ERROR (Status
)) {
349 if (Private
->BootFileSize
== 0) {
351 // Discover the information about the bootfile if we haven't.
355 // Try to use HTTP HEAD method.
357 Status
= HttpBootGetBootFile (
360 &Private
->BootFileSize
,
364 if (EFI_ERROR (Status
) && Status
!= EFI_BUFFER_TOO_SMALL
) {
366 // Failed to get file size by HEAD method, may be trunked encoding, try HTTP GET method.
368 ASSERT (Private
->BootFileSize
== 0);
369 Status
= HttpBootGetBootFile (
372 &Private
->BootFileSize
,
376 if (EFI_ERROR (Status
) && Status
!= EFI_BUFFER_TOO_SMALL
) {
377 AsciiPrint ("\n Error: Could not retrieve NBP file size from HTTP server.\n");
383 if (*BufferSize
< Private
->BootFileSize
) {
384 *BufferSize
= Private
->BootFileSize
;
385 *ImageType
= Private
->ImageType
;
386 Status
= EFI_BUFFER_TOO_SMALL
;
391 // Load the boot file into Buffer
393 Status
= HttpBootGetBootFile (
402 HttpBootUninstallCallback (Private
);
404 if (Status
== EFI_ACCESS_DENIED
) {
405 AsciiPrint ("\n Error: Could not establish connection with HTTP server.\n");
406 } else if (Status
== EFI_BUFFER_TOO_SMALL
&& Buffer
!= NULL
) {
407 AsciiPrint ("\n Error: Buffer size is smaller than the requested file.\n");
408 } else if (Status
== EFI_OUT_OF_RESOURCES
) {
409 AsciiPrint ("\n Error: Could not allocate I/O buffers.\n");
410 } else if (Status
== EFI_DEVICE_ERROR
) {
411 AsciiPrint ("\n Error: Network device error.\n");
412 } else if (Status
== EFI_TIMEOUT
) {
413 AsciiPrint ("\n Error: Server response timeout.\n");
414 } else if (Status
== EFI_ABORTED
) {
415 AsciiPrint ("\n Error: Remote boot cancelled.\n");
416 } else if (Status
!= EFI_BUFFER_TOO_SMALL
) {
417 AsciiPrint ("\n Error: Unexpected network error.\n");
423 Disable the use of UEFI HTTP boot function.
425 @param[in] Private The pointer to the driver's private data.
427 @retval EFI_SUCCESS HTTP boot was successfully disabled.
428 @retval EFI_NOT_STARTED The driver is already in stopped state.
429 @retval EFI_INVALID_PARAMETER Private is NULL.
430 @retval Others Unexpected error when stop the function.
435 IN HTTP_BOOT_PRIVATE_DATA
*Private
440 if (Private
== NULL
) {
441 return EFI_INVALID_PARAMETER
;
444 if (!Private
->Started
) {
445 return EFI_NOT_STARTED
;
448 if (Private
->HttpCreated
) {
449 HttpIoDestroyIo (&Private
->HttpIo
);
450 Private
->HttpCreated
= FALSE
;
453 Private
->Started
= FALSE
;
454 ZeroMem (&Private
->StationIp
, sizeof (EFI_IP_ADDRESS
));
455 ZeroMem (&Private
->SubnetMask
, sizeof (EFI_IP_ADDRESS
));
456 ZeroMem (&Private
->GatewayIp
, sizeof (EFI_IP_ADDRESS
));
458 Private
->BootFileUri
= NULL
;
459 Private
->BootFileUriParser
= NULL
;
460 Private
->BootFileSize
= 0;
461 Private
->SelectIndex
= 0;
462 Private
->SelectProxyType
= HttpOfferTypeMax
;
464 if (!Private
->UsingIpv6
) {
466 // Stop and release the DHCP4 child.
468 Private
->Dhcp4
->Stop (Private
->Dhcp4
);
469 Private
->Dhcp4
->Configure (Private
->Dhcp4
, NULL
);
471 for (Index
= 0; Index
< HTTP_BOOT_OFFER_MAX_NUM
; Index
++) {
472 if (Private
->OfferBuffer
[Index
].Dhcp4
.UriParser
) {
473 HttpUrlFreeParser (Private
->OfferBuffer
[Index
].Dhcp4
.UriParser
);
478 // Stop and release the DHCP6 child.
480 Private
->Dhcp6
->Stop (Private
->Dhcp6
);
481 Private
->Dhcp6
->Configure (Private
->Dhcp6
, NULL
);
483 for (Index
= 0; Index
< HTTP_BOOT_OFFER_MAX_NUM
; Index
++) {
484 if (Private
->OfferBuffer
[Index
].Dhcp6
.UriParser
) {
485 HttpUrlFreeParser (Private
->OfferBuffer
[Index
].Dhcp6
.UriParser
);
490 if (Private
->DnsServerIp
!= NULL
) {
491 FreePool (Private
->DnsServerIp
);
492 Private
->DnsServerIp
= NULL
;
495 if (Private
->FilePathUri
!= NULL
) {
496 FreePool (Private
->FilePathUri
);
497 HttpUrlFreeParser (Private
->FilePathUriParser
);
498 Private
->FilePathUri
= NULL
;
499 Private
->FilePathUriParser
= NULL
;
502 ZeroMem (Private
->OfferBuffer
, sizeof (Private
->OfferBuffer
));
503 Private
->OfferNum
= 0;
504 ZeroMem (Private
->OfferCount
, sizeof (Private
->OfferCount
));
505 ZeroMem (Private
->OfferIndex
, sizeof (Private
->OfferIndex
));
507 HttpBootFreeCacheList (Private
);
513 Causes the driver to load a specified file.
515 @param This Protocol instance pointer.
516 @param FilePath The device specific path of the file to load.
517 @param BootPolicy If TRUE, indicates that the request originates from the
518 boot manager is attempting to load FilePath as a boot
519 selection. If FALSE, then FilePath must match as exact file
521 @param BufferSize On input the size of Buffer in bytes. On output with a return
522 code of EFI_SUCCESS, the amount of data transferred to
523 Buffer. On output with a return code of EFI_BUFFER_TOO_SMALL,
524 the size of Buffer required to retrieve the requested file.
525 @param Buffer The memory buffer to transfer the file to. IF Buffer is NULL,
526 then the size of the requested file is returned in
529 @retval EFI_SUCCESS The file was loaded.
530 @retval EFI_UNSUPPORTED The device does not support the provided BootPolicy
531 @retval EFI_INVALID_PARAMETER FilePath is not a valid device path, or
533 @retval EFI_NO_MEDIA No medium was present to load the file.
534 @retval EFI_DEVICE_ERROR The file was not loaded due to a device error.
535 @retval EFI_NO_RESPONSE The remote system did not respond.
536 @retval EFI_NOT_FOUND The file was not found.
537 @retval EFI_ABORTED The file load process was manually cancelled.
538 @retval EFI_BUFFER_TOO_SMALL The BufferSize is too small to read the current directory entry.
539 BufferSize has been updated with the size needed to complete
545 HttpBootDxeLoadFile (
546 IN EFI_LOAD_FILE_PROTOCOL
*This
,
547 IN EFI_DEVICE_PATH_PROTOCOL
*FilePath
,
548 IN BOOLEAN BootPolicy
,
549 IN OUT UINTN
*BufferSize
,
550 IN VOID
*Buffer OPTIONAL
553 HTTP_BOOT_PRIVATE_DATA
*Private
;
554 HTTP_BOOT_VIRTUAL_NIC
*VirtualNic
;
555 EFI_STATUS MediaStatus
;
558 HTTP_BOOT_IMAGE_TYPE ImageType
;
560 if (This
== NULL
|| BufferSize
== NULL
|| FilePath
== NULL
) {
561 return EFI_INVALID_PARAMETER
;
565 // Only support BootPolicy
568 return EFI_UNSUPPORTED
;
571 VirtualNic
= HTTP_BOOT_VIRTUAL_NIC_FROM_LOADFILE (This
);
572 Private
= VirtualNic
->Private
;
575 // Check media status before HTTP boot start
577 MediaStatus
= EFI_SUCCESS
;
578 NetLibDetectMediaWaitTimeout (Private
->Controller
, HTTP_BOOT_CHECK_MEDIA_WAITING_TIME
, &MediaStatus
);
579 if (MediaStatus
!= EFI_SUCCESS
) {
580 AsciiPrint ("\n Error: Could not detect network connection.\n");
585 // Check whether the virtual nic is using IPv6 or not.
588 if (VirtualNic
== Private
->Ip6Nic
) {
593 // Initialize HTTP boot.
595 Status
= HttpBootStart (Private
, UsingIpv6
, FilePath
);
596 if (Status
!= EFI_SUCCESS
&& Status
!= EFI_ALREADY_STARTED
) {
601 // Load the boot file.
603 ImageType
= ImageTypeMax
;
604 Status
= HttpBootLoadFile (Private
, BufferSize
, Buffer
, &ImageType
);
605 if (EFI_ERROR (Status
)) {
606 if (Status
== EFI_BUFFER_TOO_SMALL
&& (ImageType
== ImageTypeVirtualCd
|| ImageType
== ImageTypeVirtualDisk
)) {
607 Status
= EFI_WARN_FILE_SYSTEM
;
608 } else if (Status
!= EFI_BUFFER_TOO_SMALL
) {
609 HttpBootStop (Private
);
615 // Register the RAM Disk to the system if needed.
617 if (ImageType
== ImageTypeVirtualCd
|| ImageType
== ImageTypeVirtualDisk
) {
618 Status
= HttpBootRegisterRamDisk (Private
, *BufferSize
, Buffer
, ImageType
);
619 if (!EFI_ERROR (Status
)) {
620 Status
= EFI_WARN_FILE_SYSTEM
;
622 AsciiPrint ("\n Error: Could not register RAM disk to the system.\n");
627 // Stop the HTTP Boot service after the boot image is downloaded.
629 HttpBootStop (Private
);
634 /// Load File Protocol instance
636 GLOBAL_REMOVE_IF_UNREFERENCED
637 EFI_LOAD_FILE_PROTOCOL gHttpBootDxeLoadFile
= {
642 Callback function that is invoked when the HTTP Boot driver is about to transmit or has received a
645 This function is invoked when the HTTP Boot driver is about to transmit or has received packet.
646 Parameters DataType and Received specify the type of event and the format of the buffer pointed
647 to by Data. Due to the polling nature of UEFI device drivers, this callback function should not
648 execute for more than 5 ms.
649 The returned status code determines the behavior of the HTTP Boot driver.
651 @param[in] This Pointer to the EFI_HTTP_BOOT_CALLBACK_PROTOCOL instance.
652 @param[in] DataType The event that occurs in the current state.
653 @param[in] Received TRUE if the callback is being invoked due to a receive event.
654 FALSE if the callback is being invoked due to a transmit event.
655 @param[in] DataLength The length in bytes of the buffer pointed to by Data.
656 @param[in] Data A pointer to the buffer of data, the data type is specified by
659 @retval EFI_SUCCESS Tells the HTTP Boot driver to continue the HTTP Boot process.
660 @retval EFI_ABORTED Tells the HTTP Boot driver to abort the current HTTP Boot process.
665 IN EFI_HTTP_BOOT_CALLBACK_PROTOCOL
*This
,
666 IN EFI_HTTP_BOOT_CALLBACK_DATA_TYPE DataType
,
668 IN UINT32 DataLength
,
669 IN VOID
*Data OPTIONAL
672 EFI_HTTP_MESSAGE
*HttpMessage
;
673 EFI_HTTP_HEADER
*HttpHeader
;
674 HTTP_BOOT_PRIVATE_DATA
*Private
;
677 Private
= HTTP_BOOT_PRIVATE_DATA_FROM_CALLBACK_PROTOCOL(This
);
685 case HttpBootHttpRequest
:
687 HttpMessage
= (EFI_HTTP_MESSAGE
*) Data
;
688 if (HttpMessage
->Data
.Request
->Method
== HttpMethodGet
&&
689 HttpMessage
->Data
.Request
->Url
!= NULL
) {
690 Print (L
"\n URI: %s\n", HttpMessage
->Data
.Request
->Url
);
695 case HttpBootHttpResponse
:
697 HttpMessage
= (EFI_HTTP_MESSAGE
*) Data
;
699 if (HttpMessage
->Data
.Response
!= NULL
) {
700 if (HttpBootIsHttpRedirectStatusCode (HttpMessage
->Data
.Response
->StatusCode
)) {
702 // Server indicates the resource has been redirected to a different URL
703 // according to the section 6.4 of RFC7231 and the RFC 7538.
704 // Display the redirect information on the screen.
706 HttpHeader
= HttpFindHeader (
707 HttpMessage
->HeaderCount
,
708 HttpMessage
->Headers
,
711 if (HttpHeader
!= NULL
) {
712 Print (L
"\n HTTP ERROR: Resource Redirected.\n New Location: %a\n", HttpHeader
->FieldValue
);
718 HttpHeader
= HttpFindHeader (
719 HttpMessage
->HeaderCount
,
720 HttpMessage
->Headers
,
721 HTTP_HEADER_CONTENT_LENGTH
723 if (HttpHeader
!= NULL
) {
724 Private
->FileSize
= AsciiStrDecimalToUintn (HttpHeader
->FieldValue
);
725 Private
->ReceivedSize
= 0;
726 Private
->Percentage
= 0;
731 case HttpBootHttpEntityBody
:
732 if (DataLength
!= 0) {
733 if (Private
->FileSize
!= 0) {
735 // We already know the file size, print in percentage format.
737 if (Private
->ReceivedSize
== 0) {
738 Print (L
" File Size: %lu Bytes\n", Private
->FileSize
);
740 Private
->ReceivedSize
+= DataLength
;
741 Percentage
= (UINT32
) DivU64x64Remainder (MultU64x32 (Private
->ReceivedSize
, 100), Private
->FileSize
, NULL
);
742 if (Private
->Percentage
!= Percentage
) {
743 Private
->Percentage
= Percentage
;
744 Print (L
"\r Downloading...%d%%", Percentage
);
748 // In some case we couldn't get the file size from the HTTP header, so we
749 // just print the downloaded file size.
751 Private
->ReceivedSize
+= DataLength
;
752 Print (L
"\r Downloading...%lu Bytes", Private
->ReceivedSize
);
765 /// HTTP Boot Callback Protocol instance
767 GLOBAL_REMOVE_IF_UNREFERENCED
768 EFI_HTTP_BOOT_CALLBACK_PROTOCOL gHttpBootDxeHttpBootCallback
= {