]> git.proxmox.com Git - mirror_edk2.git/blob - NetworkPkg/HttpBootDxe/HttpBootImpl.c
NetworkPkg: Apply uncrustify changes
[mirror_edk2.git] / NetworkPkg / HttpBootDxe / HttpBootImpl.c
1 /** @file
2 The implementation of EFI_LOAD_FILE_PROTOCOL for UEFI HTTP boot.
3
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
7
8 **/
9
10 #include "HttpBootDxe.h"
11
12 /**
13 Install HTTP Boot Callback Protocol if not installed before.
14
15 @param[in] Private Pointer to HTTP Boot private data.
16
17 @retval EFI_SUCCESS HTTP Boot Callback Protocol installed successfully.
18 @retval Others Failed to install HTTP Boot Callback Protocol.
19
20 **/
21 EFI_STATUS
22 HttpBootInstallCallback (
23 IN HTTP_BOOT_PRIVATE_DATA *Private
24 )
25 {
26 EFI_STATUS Status;
27 EFI_HANDLE ControllerHandle;
28
29 if (!Private->UsingIpv6) {
30 ControllerHandle = Private->Ip4Nic->Controller;
31 } else {
32 ControllerHandle = Private->Ip6Nic->Controller;
33 }
34
35 //
36 // Check whether gEfiHttpBootCallbackProtocolGuid already installed.
37 //
38 Status = gBS->HandleProtocol (
39 ControllerHandle,
40 &gEfiHttpBootCallbackProtocolGuid,
41 (VOID **)&Private->HttpBootCallback
42 );
43 if (Status == EFI_UNSUPPORTED) {
44 CopyMem (
45 &Private->LoadFileCallback,
46 &gHttpBootDxeHttpBootCallback,
47 sizeof (EFI_HTTP_BOOT_CALLBACK_PROTOCOL)
48 );
49
50 //
51 // Install a default callback if user didn't offer one.
52 //
53 Status = gBS->InstallProtocolInterface (
54 &ControllerHandle,
55 &gEfiHttpBootCallbackProtocolGuid,
56 EFI_NATIVE_INTERFACE,
57 &Private->LoadFileCallback
58 );
59 if (EFI_ERROR (Status)) {
60 return Status;
61 }
62
63 Private->HttpBootCallback = &Private->LoadFileCallback;
64 }
65
66 return EFI_SUCCESS;
67 }
68
69 /**
70 Uninstall HTTP Boot Callback Protocol if it's installed by this driver.
71
72 @param[in] Private Pointer to HTTP Boot private data.
73
74 **/
75 VOID
76 HttpBootUninstallCallback (
77 IN HTTP_BOOT_PRIVATE_DATA *Private
78 )
79 {
80 if (Private->HttpBootCallback == &Private->LoadFileCallback) {
81 gBS->UninstallProtocolInterface (
82 Private->Controller,
83 &gEfiHttpBootCallbackProtocolGuid,
84 &Private->HttpBootCallback
85 );
86 Private->HttpBootCallback = NULL;
87 }
88 }
89
90 /**
91 Enable the use of UEFI HTTP boot function.
92
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.
95
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.
101
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.
107
108 **/
109 EFI_STATUS
110 HttpBootStart (
111 IN HTTP_BOOT_PRIVATE_DATA *Private,
112 IN BOOLEAN UsingIpv6,
113 IN EFI_DEVICE_PATH_PROTOCOL *FilePath
114 )
115 {
116 UINTN Index;
117 EFI_STATUS Status;
118 CHAR8 *Uri;
119
120 Uri = NULL;
121
122 if ((Private == NULL) || (FilePath == NULL)) {
123 return EFI_INVALID_PARAMETER;
124 }
125
126 //
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.
129 //
130 Status = HttpBootParseFilePath (FilePath, &Uri);
131 if (EFI_ERROR (Status)) {
132 return EFI_INVALID_PARAMETER;
133 }
134
135 //
136 // Check whether we need to stop and restart the HTTP boot driver.
137 //
138 if (Private->Started) {
139 //
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
143 // recorded before.
144 //
145 if ((UsingIpv6 != Private->UsingIpv6) ||
146 ((Uri != NULL) && (AsciiStrCmp (Private->BootFileUri, Uri) != 0)))
147 {
148 //
149 // Restart is required, first stop then continue this start function.
150 //
151 Status = HttpBootStop (Private);
152 if (EFI_ERROR (Status)) {
153 if (Uri != NULL) {
154 FreePool (Uri);
155 }
156
157 return Status;
158 }
159 } else {
160 //
161 // Restart is not required.
162 //
163 if (Uri != NULL) {
164 FreePool (Uri);
165 }
166
167 return EFI_ALREADY_STARTED;
168 }
169 }
170
171 //
172 // Detect whether using ipv6 or not, and set it to the private data.
173 //
174 if (UsingIpv6 && (Private->Ip6Nic != NULL)) {
175 Private->UsingIpv6 = TRUE;
176 } else if (!UsingIpv6 && (Private->Ip4Nic != NULL)) {
177 Private->UsingIpv6 = FALSE;
178 } else {
179 if (Uri != NULL) {
180 FreePool (Uri);
181 }
182
183 return EFI_UNSUPPORTED;
184 }
185
186 //
187 // Record the specified URI and prepare the URI parser if needed.
188 //
189 Private->FilePathUri = Uri;
190 if (Private->FilePathUri != NULL) {
191 Status = HttpParseUrl (
192 Private->FilePathUri,
193 (UINT32)AsciiStrLen (Private->FilePathUri),
194 FALSE,
195 &Private->FilePathUriParser
196 );
197 if (EFI_ERROR (Status)) {
198 FreePool (Private->FilePathUri);
199 return Status;
200 }
201 }
202
203 //
204 // Init the content of cached DHCP offer list.
205 //
206 ZeroMem (Private->OfferBuffer, sizeof (Private->OfferBuffer));
207 if (!Private->UsingIpv6) {
208 for (Index = 0; Index < HTTP_BOOT_OFFER_MAX_NUM; Index++) {
209 Private->OfferBuffer[Index].Dhcp4.Packet.Offer.Size = HTTP_CACHED_DHCP4_PACKET_MAX_SIZE;
210 }
211 } else {
212 for (Index = 0; Index < HTTP_BOOT_OFFER_MAX_NUM; Index++) {
213 Private->OfferBuffer[Index].Dhcp6.Packet.Offer.Size = HTTP_CACHED_DHCP6_PACKET_MAX_SIZE;
214 }
215 }
216
217 if (Private->UsingIpv6) {
218 //
219 // Set Ip6 policy to Automatic to start the Ip6 router discovery.
220 //
221 Status = HttpBootSetIp6Policy (Private);
222 if (EFI_ERROR (Status)) {
223 return Status;
224 }
225 }
226
227 Private->Started = TRUE;
228 Print (L"\n>>Start HTTP Boot over IPv%d", Private->UsingIpv6 ? 6 : 4);
229
230 return EFI_SUCCESS;
231 }
232
233 /**
234 Attempt to complete a DHCPv4 D.O.R.A or DHCPv6 S.R.A.A sequence to retrieve the boot resource information.
235
236 @param[in] Private The pointer to the driver's private data.
237
238 @retval EFI_SUCCESS Boot info was successfully retrieved.
239 @retval EFI_INVALID_PARAMETER Private is NULL.
240 @retval EFI_NOT_STARTED The driver is in stopped state.
241 @retval EFI_DEVICE_ERROR An unexpected network error occurred.
242 @retval Others Other errors as indicated.
243
244 **/
245 EFI_STATUS
246 HttpBootDhcp (
247 IN HTTP_BOOT_PRIVATE_DATA *Private
248 )
249 {
250 EFI_STATUS Status;
251
252 if (Private == NULL) {
253 return EFI_INVALID_PARAMETER;
254 }
255
256 if (!Private->Started) {
257 return EFI_NOT_STARTED;
258 }
259
260 Status = EFI_DEVICE_ERROR;
261
262 if (!Private->UsingIpv6) {
263 //
264 // Start D.O.R.A process to get a IPv4 address and other boot information.
265 //
266 Status = HttpBootDhcp4Dora (Private);
267 } else {
268 //
269 // Start S.A.R.R process to get a IPv6 address and other boot information.
270 //
271 Status = HttpBootDhcp6Sarr (Private);
272 }
273
274 return Status;
275 }
276
277 /**
278 Attempt to download the boot file through HTTP message exchange.
279
280 @param[in] Private The pointer to the driver's private data.
281 @param[in, out] BufferSize On input the size of Buffer in bytes. On output with a return
282 code of EFI_SUCCESS, the amount of data transferred to
283 Buffer. On output with a return code of EFI_BUFFER_TOO_SMALL,
284 the size of Buffer required to retrieve the requested file.
285 @param[in] Buffer The memory buffer to transfer the file to. If Buffer is NULL,
286 then the size of the requested file is returned in
287 BufferSize.
288 @param[out] ImageType The image type of the downloaded file.
289
290 @retval EFI_SUCCESS Boot file was loaded successfully.
291 @retval EFI_INVALID_PARAMETER Private is NULL, or ImageType is NULL, or BufferSize is NULL.
292 @retval EFI_INVALID_PARAMETER *BufferSize is not zero, and Buffer is NULL.
293 @retval EFI_NOT_STARTED The driver is in stopped state.
294 @retval EFI_BUFFER_TOO_SMALL The BufferSize is too small to read the boot file. BufferSize has
295 been updated with the size needed to complete the request.
296 @retval EFI_DEVICE_ERROR An unexpected network error occurred.
297 @retval Others Other errors as indicated.
298
299 **/
300 EFI_STATUS
301 HttpBootLoadFile (
302 IN HTTP_BOOT_PRIVATE_DATA *Private,
303 IN OUT UINTN *BufferSize,
304 IN VOID *Buffer OPTIONAL,
305 OUT HTTP_BOOT_IMAGE_TYPE *ImageType
306 )
307 {
308 EFI_STATUS Status;
309
310 if ((Private == NULL) || (ImageType == NULL) || (BufferSize == NULL)) {
311 return EFI_INVALID_PARAMETER;
312 }
313
314 if ((*BufferSize != 0) && (Buffer == NULL)) {
315 return EFI_INVALID_PARAMETER;
316 }
317
318 if (!Private->Started) {
319 return EFI_NOT_STARTED;
320 }
321
322 Status = HttpBootInstallCallback (Private);
323 if (EFI_ERROR (Status)) {
324 goto ON_EXIT;
325 }
326
327 if (Private->BootFileUri == NULL) {
328 //
329 // Parse the cached offer to get the boot file URL first.
330 //
331 Status = HttpBootDiscoverBootInfo (Private);
332 if (EFI_ERROR (Status)) {
333 AsciiPrint ("\n Error: Could not retrieve NBP file size from HTTP server.\n");
334 goto ON_EXIT;
335 }
336 }
337
338 if (!Private->HttpCreated) {
339 //
340 // Create HTTP child.
341 //
342 Status = HttpBootCreateHttpIo (Private);
343 if (EFI_ERROR (Status)) {
344 goto ON_EXIT;
345 }
346 }
347
348 if (Private->BootFileSize == 0) {
349 //
350 // Discover the information about the bootfile if we haven't.
351 //
352
353 //
354 // Try to use HTTP HEAD method.
355 //
356 Status = HttpBootGetBootFile (
357 Private,
358 TRUE,
359 &Private->BootFileSize,
360 NULL,
361 &Private->ImageType
362 );
363 if (EFI_ERROR (Status) && (Status != EFI_BUFFER_TOO_SMALL)) {
364 //
365 // Failed to get file size by HEAD method, may be trunked encoding, try HTTP GET method.
366 //
367 ASSERT (Private->BootFileSize == 0);
368 Status = HttpBootGetBootFile (
369 Private,
370 FALSE,
371 &Private->BootFileSize,
372 NULL,
373 &Private->ImageType
374 );
375 if (EFI_ERROR (Status) && (Status != EFI_BUFFER_TOO_SMALL)) {
376 AsciiPrint ("\n Error: Could not retrieve NBP file size from HTTP server.\n");
377 goto ON_EXIT;
378 }
379 }
380 }
381
382 if (*BufferSize < Private->BootFileSize) {
383 *BufferSize = Private->BootFileSize;
384 *ImageType = Private->ImageType;
385 Status = EFI_BUFFER_TOO_SMALL;
386 goto ON_EXIT;
387 }
388
389 //
390 // Load the boot file into Buffer
391 //
392 Status = HttpBootGetBootFile (
393 Private,
394 FALSE,
395 BufferSize,
396 Buffer,
397 ImageType
398 );
399
400 ON_EXIT:
401 HttpBootUninstallCallback (Private);
402
403 if (EFI_ERROR (Status)) {
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");
418 }
419 }
420
421 return Status;
422 }
423
424 /**
425 Disable the use of UEFI HTTP boot function.
426
427 @param[in] Private The pointer to the driver's private data.
428
429 @retval EFI_SUCCESS HTTP boot was successfully disabled.
430 @retval EFI_NOT_STARTED The driver is already in stopped state.
431 @retval EFI_INVALID_PARAMETER Private is NULL.
432 @retval Others Unexpected error when stop the function.
433
434 **/
435 EFI_STATUS
436 HttpBootStop (
437 IN HTTP_BOOT_PRIVATE_DATA *Private
438 )
439 {
440 UINTN Index;
441
442 if (Private == NULL) {
443 return EFI_INVALID_PARAMETER;
444 }
445
446 if (!Private->Started) {
447 return EFI_NOT_STARTED;
448 }
449
450 if (Private->HttpCreated) {
451 HttpIoDestroyIo (&Private->HttpIo);
452 Private->HttpCreated = FALSE;
453 }
454
455 Private->Started = FALSE;
456 ZeroMem (&Private->StationIp, sizeof (EFI_IP_ADDRESS));
457 ZeroMem (&Private->SubnetMask, sizeof (EFI_IP_ADDRESS));
458 ZeroMem (&Private->GatewayIp, sizeof (EFI_IP_ADDRESS));
459 Private->Port = 0;
460 Private->BootFileUri = NULL;
461 Private->BootFileUriParser = NULL;
462 Private->BootFileSize = 0;
463 Private->SelectIndex = 0;
464 Private->SelectProxyType = HttpOfferTypeMax;
465
466 if (!Private->UsingIpv6) {
467 //
468 // Stop and release the DHCP4 child.
469 //
470 Private->Dhcp4->Stop (Private->Dhcp4);
471 Private->Dhcp4->Configure (Private->Dhcp4, NULL);
472
473 for (Index = 0; Index < HTTP_BOOT_OFFER_MAX_NUM; Index++) {
474 if (Private->OfferBuffer[Index].Dhcp4.UriParser) {
475 HttpUrlFreeParser (Private->OfferBuffer[Index].Dhcp4.UriParser);
476 }
477 }
478 } else {
479 //
480 // Stop and release the DHCP6 child.
481 //
482 Private->Dhcp6->Stop (Private->Dhcp6);
483 Private->Dhcp6->Configure (Private->Dhcp6, NULL);
484
485 for (Index = 0; Index < HTTP_BOOT_OFFER_MAX_NUM; Index++) {
486 if (Private->OfferBuffer[Index].Dhcp6.UriParser) {
487 HttpUrlFreeParser (Private->OfferBuffer[Index].Dhcp6.UriParser);
488 }
489 }
490 }
491
492 if (Private->DnsServerIp != NULL) {
493 FreePool (Private->DnsServerIp);
494 Private->DnsServerIp = NULL;
495 }
496
497 if (Private->FilePathUri != NULL) {
498 FreePool (Private->FilePathUri);
499 HttpUrlFreeParser (Private->FilePathUriParser);
500 Private->FilePathUri = NULL;
501 Private->FilePathUriParser = NULL;
502 }
503
504 ZeroMem (Private->OfferBuffer, sizeof (Private->OfferBuffer));
505 Private->OfferNum = 0;
506 ZeroMem (Private->OfferCount, sizeof (Private->OfferCount));
507 ZeroMem (Private->OfferIndex, sizeof (Private->OfferIndex));
508
509 HttpBootFreeCacheList (Private);
510
511 return EFI_SUCCESS;
512 }
513
514 /**
515 Causes the driver to load a specified file.
516
517 @param This Protocol instance pointer.
518 @param FilePath The device specific path of the file to load.
519 @param BootPolicy If TRUE, indicates that the request originates from the
520 boot manager is attempting to load FilePath as a boot
521 selection. If FALSE, then FilePath must match as exact file
522 to be loaded.
523 @param BufferSize On input the size of Buffer in bytes. On output with a return
524 code of EFI_SUCCESS, the amount of data transferred to
525 Buffer. On output with a return code of EFI_BUFFER_TOO_SMALL,
526 the size of Buffer required to retrieve the requested file.
527 @param Buffer The memory buffer to transfer the file to. IF Buffer is NULL,
528 then the size of the requested file is returned in
529 BufferSize.
530
531 @retval EFI_SUCCESS The file was loaded.
532 @retval EFI_UNSUPPORTED The device does not support the provided BootPolicy
533 @retval EFI_INVALID_PARAMETER FilePath is not a valid device path, or
534 BufferSize is NULL.
535 @retval EFI_NO_MEDIA No medium was present to load the file.
536 @retval EFI_DEVICE_ERROR The file was not loaded due to a device error.
537 @retval EFI_NO_RESPONSE The remote system did not respond.
538 @retval EFI_NOT_FOUND The file was not found.
539 @retval EFI_ABORTED The file load process was manually cancelled.
540 @retval EFI_BUFFER_TOO_SMALL The BufferSize is too small to read the current directory entry.
541 BufferSize has been updated with the size needed to complete
542 the request.
543
544 **/
545 EFI_STATUS
546 EFIAPI
547 HttpBootDxeLoadFile (
548 IN EFI_LOAD_FILE_PROTOCOL *This,
549 IN EFI_DEVICE_PATH_PROTOCOL *FilePath,
550 IN BOOLEAN BootPolicy,
551 IN OUT UINTN *BufferSize,
552 IN VOID *Buffer OPTIONAL
553 )
554 {
555 HTTP_BOOT_PRIVATE_DATA *Private;
556 HTTP_BOOT_VIRTUAL_NIC *VirtualNic;
557 EFI_STATUS MediaStatus;
558 BOOLEAN UsingIpv6;
559 EFI_STATUS Status;
560 HTTP_BOOT_IMAGE_TYPE ImageType;
561
562 if ((This == NULL) || (BufferSize == NULL) || (FilePath == NULL)) {
563 return EFI_INVALID_PARAMETER;
564 }
565
566 //
567 // Only support BootPolicy
568 //
569 if (!BootPolicy) {
570 return EFI_UNSUPPORTED;
571 }
572
573 VirtualNic = HTTP_BOOT_VIRTUAL_NIC_FROM_LOADFILE (This);
574 Private = VirtualNic->Private;
575
576 //
577 // Check media status before HTTP boot start
578 //
579 MediaStatus = EFI_SUCCESS;
580 NetLibDetectMediaWaitTimeout (Private->Controller, HTTP_BOOT_CHECK_MEDIA_WAITING_TIME, &MediaStatus);
581 if (MediaStatus != EFI_SUCCESS) {
582 AsciiPrint ("\n Error: Could not detect network connection.\n");
583 return EFI_NO_MEDIA;
584 }
585
586 //
587 // Check whether the virtual nic is using IPv6 or not.
588 //
589 UsingIpv6 = FALSE;
590 if (VirtualNic == Private->Ip6Nic) {
591 UsingIpv6 = TRUE;
592 }
593
594 //
595 // Initialize HTTP boot.
596 //
597 Status = HttpBootStart (Private, UsingIpv6, FilePath);
598 if ((Status != EFI_SUCCESS) && (Status != EFI_ALREADY_STARTED)) {
599 return Status;
600 }
601
602 //
603 // Load the boot file.
604 //
605 ImageType = ImageTypeMax;
606 Status = HttpBootLoadFile (Private, BufferSize, Buffer, &ImageType);
607 if (EFI_ERROR (Status)) {
608 if ((Status == EFI_BUFFER_TOO_SMALL) && ((ImageType == ImageTypeVirtualCd) || (ImageType == ImageTypeVirtualDisk))) {
609 Status = EFI_WARN_FILE_SYSTEM;
610 } else if (Status != EFI_BUFFER_TOO_SMALL) {
611 HttpBootStop (Private);
612 }
613
614 return Status;
615 }
616
617 //
618 // Register the RAM Disk to the system if needed.
619 //
620 if ((ImageType == ImageTypeVirtualCd) || (ImageType == ImageTypeVirtualDisk)) {
621 Status = HttpBootRegisterRamDisk (Private, *BufferSize, Buffer, ImageType);
622 if (!EFI_ERROR (Status)) {
623 Status = EFI_WARN_FILE_SYSTEM;
624 } else {
625 AsciiPrint ("\n Error: Could not register RAM disk to the system.\n");
626 }
627 }
628
629 //
630 // Stop the HTTP Boot service after the boot image is downloaded.
631 //
632 HttpBootStop (Private);
633 return Status;
634 }
635
636 ///
637 /// Load File Protocol instance
638 ///
639 GLOBAL_REMOVE_IF_UNREFERENCED
640 EFI_LOAD_FILE_PROTOCOL gHttpBootDxeLoadFile = {
641 HttpBootDxeLoadFile
642 };
643
644 /**
645 Callback function that is invoked when the HTTP Boot driver is about to transmit or has received a
646 packet.
647
648 This function is invoked when the HTTP Boot driver is about to transmit or has received packet.
649 Parameters DataType and Received specify the type of event and the format of the buffer pointed
650 to by Data. Due to the polling nature of UEFI device drivers, this callback function should not
651 execute for more than 5 ms.
652 The returned status code determines the behavior of the HTTP Boot driver.
653
654 @param[in] This Pointer to the EFI_HTTP_BOOT_CALLBACK_PROTOCOL instance.
655 @param[in] DataType The event that occurs in the current state.
656 @param[in] Received TRUE if the callback is being invoked due to a receive event.
657 FALSE if the callback is being invoked due to a transmit event.
658 @param[in] DataLength The length in bytes of the buffer pointed to by Data.
659 @param[in] Data A pointer to the buffer of data, the data type is specified by
660 DataType.
661
662 @retval EFI_SUCCESS Tells the HTTP Boot driver to continue the HTTP Boot process.
663 @retval EFI_ABORTED Tells the HTTP Boot driver to abort the current HTTP Boot process.
664 **/
665 EFI_STATUS
666 EFIAPI
667 HttpBootCallback (
668 IN EFI_HTTP_BOOT_CALLBACK_PROTOCOL *This,
669 IN EFI_HTTP_BOOT_CALLBACK_DATA_TYPE DataType,
670 IN BOOLEAN Received,
671 IN UINT32 DataLength,
672 IN VOID *Data OPTIONAL
673 )
674 {
675 EFI_HTTP_MESSAGE *HttpMessage;
676 EFI_HTTP_HEADER *HttpHeader;
677 HTTP_BOOT_PRIVATE_DATA *Private;
678 UINT32 Percentage;
679
680 Private = HTTP_BOOT_PRIVATE_DATA_FROM_CALLBACK_PROTOCOL (This);
681
682 switch (DataType) {
683 case HttpBootDhcp4:
684 case HttpBootDhcp6:
685 Print (L".");
686 break;
687
688 case HttpBootHttpRequest:
689 if (Data != NULL) {
690 HttpMessage = (EFI_HTTP_MESSAGE *)Data;
691 if ((HttpMessage->Data.Request->Method == HttpMethodGet) &&
692 (HttpMessage->Data.Request->Url != NULL))
693 {
694 Print (L"\n URI: %s\n", HttpMessage->Data.Request->Url);
695 }
696 }
697
698 break;
699
700 case HttpBootHttpResponse:
701 if (Data != NULL) {
702 HttpMessage = (EFI_HTTP_MESSAGE *)Data;
703
704 if (HttpMessage->Data.Response != NULL) {
705 if (HttpBootIsHttpRedirectStatusCode (HttpMessage->Data.Response->StatusCode)) {
706 //
707 // Server indicates the resource has been redirected to a different URL
708 // according to the section 6.4 of RFC7231 and the RFC 7538.
709 // Display the redirect information on the screen.
710 //
711 HttpHeader = HttpFindHeader (
712 HttpMessage->HeaderCount,
713 HttpMessage->Headers,
714 HTTP_HEADER_LOCATION
715 );
716 if (HttpHeader != NULL) {
717 Print (L"\n HTTP ERROR: Resource Redirected.\n New Location: %a\n", HttpHeader->FieldValue);
718 }
719
720 break;
721 }
722 }
723
724 HttpHeader = HttpFindHeader (
725 HttpMessage->HeaderCount,
726 HttpMessage->Headers,
727 HTTP_HEADER_CONTENT_LENGTH
728 );
729 if (HttpHeader != NULL) {
730 Private->FileSize = AsciiStrDecimalToUintn (HttpHeader->FieldValue);
731 Private->ReceivedSize = 0;
732 Private->Percentage = 0;
733 }
734 }
735
736 break;
737
738 case HttpBootHttpEntityBody:
739 if (DataLength != 0) {
740 if (Private->FileSize != 0) {
741 //
742 // We already know the file size, print in percentage format.
743 //
744 if (Private->ReceivedSize == 0) {
745 Print (L" File Size: %lu Bytes\n", Private->FileSize);
746 }
747
748 Private->ReceivedSize += DataLength;
749 Percentage = (UINT32)DivU64x64Remainder (MultU64x32 (Private->ReceivedSize, 100), Private->FileSize, NULL);
750 if (Private->Percentage != Percentage) {
751 Private->Percentage = Percentage;
752 Print (L"\r Downloading...%d%%", Percentage);
753 }
754 } else {
755 //
756 // In some case we couldn't get the file size from the HTTP header, so we
757 // just print the downloaded file size.
758 //
759 Private->ReceivedSize += DataLength;
760 Print (L"\r Downloading...%lu Bytes", Private->ReceivedSize);
761 }
762 }
763
764 break;
765
766 default:
767 break;
768 }
769
770 return EFI_SUCCESS;
771 }
772
773 ///
774 /// HTTP Boot Callback Protocol instance
775 ///
776 GLOBAL_REMOVE_IF_UNREFERENCED
777 EFI_HTTP_BOOT_CALLBACK_PROTOCOL gHttpBootDxeHttpBootCallback = {
778 HttpBootCallback
779 };