]> git.proxmox.com Git - mirror_edk2.git/blob - NetworkPkg/HttpBootDxe/HttpBootImpl.c
EmulatorPkg/PeiEmuSerialPortLib: Update the INF file Guid
[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 succesfully.
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
45 CopyMem (
46 &Private->LoadFileCallback,
47 &gHttpBootDxeHttpBootCallback,
48 sizeof (EFI_HTTP_BOOT_CALLBACK_PROTOCOL)
49 );
50
51 //
52 // Install a default callback if user didn't offer one.
53 //
54 Status = gBS->InstallProtocolInterface (
55 &ControllerHandle,
56 &gEfiHttpBootCallbackProtocolGuid,
57 EFI_NATIVE_INTERFACE,
58 &Private->LoadFileCallback
59 );
60 if (EFI_ERROR (Status)) {
61 return Status;
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 // Restart is required, first stop then continue this start function.
149 //
150 Status = HttpBootStop (Private);
151 if (EFI_ERROR (Status)) {
152 if (Uri != NULL) {
153 FreePool (Uri);
154 }
155 return Status;
156 }
157 } else {
158 //
159 // Restart is not required.
160 //
161 if (Uri != NULL) {
162 FreePool (Uri);
163 }
164 return EFI_ALREADY_STARTED;
165 }
166 }
167
168 //
169 // Detect whether using ipv6 or not, and set it to the private data.
170 //
171 if (UsingIpv6 && Private->Ip6Nic != NULL) {
172 Private->UsingIpv6 = TRUE;
173 } else if (!UsingIpv6 && Private->Ip4Nic != NULL) {
174 Private->UsingIpv6 = FALSE;
175 } else {
176 if (Uri != NULL) {
177 FreePool (Uri);
178 }
179 return EFI_UNSUPPORTED;
180 }
181
182 //
183 // Record the specified URI and prepare the URI parser if needed.
184 //
185 Private->FilePathUri = Uri;
186 if (Private->FilePathUri != NULL) {
187 Status = HttpParseUrl (
188 Private->FilePathUri,
189 (UINT32) AsciiStrLen (Private->FilePathUri),
190 FALSE,
191 &Private->FilePathUriParser
192 );
193 if (EFI_ERROR (Status)) {
194 FreePool (Private->FilePathUri);
195 return Status;
196 }
197 }
198
199 //
200 // Init the content of cached DHCP offer list.
201 //
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;
206 }
207 } else {
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;
210 }
211 }
212
213 if (Private->UsingIpv6) {
214 //
215 // Set Ip6 policy to Automatic to start the Ip6 router discovery.
216 //
217 Status = HttpBootSetIp6Policy (Private);
218 if (EFI_ERROR (Status)) {
219 return Status;
220 }
221 }
222 Private->Started = TRUE;
223 Print (L"\n>>Start HTTP Boot over IPv%d", Private->UsingIpv6 ? 6 : 4);
224
225 return EFI_SUCCESS;
226 }
227
228 /**
229 Attempt to complete a DHCPv4 D.O.R.A or DHCPv6 S.R.A.A sequence to retrieve the boot resource information.
230
231 @param[in] Private The pointer to the driver's private data.
232
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.
238
239 **/
240 EFI_STATUS
241 HttpBootDhcp (
242 IN HTTP_BOOT_PRIVATE_DATA *Private
243 )
244 {
245 EFI_STATUS Status;
246
247 if (Private == NULL) {
248 return EFI_INVALID_PARAMETER;
249 }
250
251 if (!Private->Started) {
252 return EFI_NOT_STARTED;
253 }
254
255 Status = EFI_DEVICE_ERROR;
256
257 if (!Private->UsingIpv6) {
258 //
259 // Start D.O.R.A process to get a IPv4 address and other boot information.
260 //
261 Status = HttpBootDhcp4Dora (Private);
262 } else {
263 //
264 // Start S.A.R.R process to get a IPv6 address and other boot information.
265 //
266 Status = HttpBootDhcp6Sarr (Private);
267 }
268
269 return Status;
270 }
271
272 /**
273 Attempt to download the boot file through HTTP message exchange.
274
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
282 BufferSize.
283 @param[out] ImageType The image type of the downloaded file.
284
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.
293
294 **/
295 EFI_STATUS
296 HttpBootLoadFile (
297 IN HTTP_BOOT_PRIVATE_DATA *Private,
298 IN OUT UINTN *BufferSize,
299 IN VOID *Buffer, OPTIONAL
300 OUT HTTP_BOOT_IMAGE_TYPE *ImageType
301 )
302 {
303 EFI_STATUS Status;
304
305 if (Private == NULL || ImageType == NULL || BufferSize == NULL ) {
306 return EFI_INVALID_PARAMETER;
307 }
308
309 if (*BufferSize != 0 && Buffer == NULL) {
310 return EFI_INVALID_PARAMETER;
311 }
312
313 if (!Private->Started) {
314 return EFI_NOT_STARTED;
315 }
316
317 Status = HttpBootInstallCallback (Private);
318 if (EFI_ERROR(Status)) {
319 goto ON_EXIT;
320 }
321
322 if (Private->BootFileUri == NULL) {
323 //
324 // Parse the cached offer to get the boot file URL first.
325 //
326 Status = HttpBootDiscoverBootInfo (Private);
327 if (EFI_ERROR (Status)) {
328 AsciiPrint ("\n Error: Could not retrieve NBP file size from HTTP server.\n");
329 goto ON_EXIT;
330 }
331 }
332
333 if (!Private->HttpCreated) {
334 //
335 // Create HTTP child.
336 //
337 Status = HttpBootCreateHttpIo (Private);
338 if (EFI_ERROR (Status)) {
339 goto ON_EXIT;
340 }
341 }
342
343 if (Private->BootFileSize == 0) {
344 //
345 // Discover the information about the bootfile if we haven't.
346 //
347
348 //
349 // Try to use HTTP HEAD method.
350 //
351 Status = HttpBootGetBootFile (
352 Private,
353 TRUE,
354 &Private->BootFileSize,
355 NULL,
356 &Private->ImageType
357 );
358 if (EFI_ERROR (Status) && Status != EFI_BUFFER_TOO_SMALL) {
359 //
360 // Failed to get file size by HEAD method, may be trunked encoding, try HTTP GET method.
361 //
362 ASSERT (Private->BootFileSize == 0);
363 Status = HttpBootGetBootFile (
364 Private,
365 FALSE,
366 &Private->BootFileSize,
367 NULL,
368 &Private->ImageType
369 );
370 if (EFI_ERROR (Status) && Status != EFI_BUFFER_TOO_SMALL) {
371 AsciiPrint ("\n Error: Could not retrieve NBP file size from HTTP server.\n");
372 goto ON_EXIT;
373 }
374 }
375 }
376
377 if (*BufferSize < Private->BootFileSize) {
378 *BufferSize = Private->BootFileSize;
379 *ImageType = Private->ImageType;
380 Status = EFI_BUFFER_TOO_SMALL;
381 goto ON_EXIT;
382 }
383
384 //
385 // Load the boot file into Buffer
386 //
387 Status = HttpBootGetBootFile (
388 Private,
389 FALSE,
390 BufferSize,
391 Buffer,
392 ImageType
393 );
394
395 ON_EXIT:
396 HttpBootUninstallCallback (Private);
397
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");
413 }
414 }
415
416 return Status;
417 }
418
419 /**
420 Disable the use of UEFI HTTP boot function.
421
422 @param[in] Private The pointer to the driver's private data.
423
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.
428
429 **/
430 EFI_STATUS
431 HttpBootStop (
432 IN HTTP_BOOT_PRIVATE_DATA *Private
433 )
434 {
435 UINTN Index;
436
437 if (Private == NULL) {
438 return EFI_INVALID_PARAMETER;
439 }
440
441 if (!Private->Started) {
442 return EFI_NOT_STARTED;
443 }
444
445 if (Private->HttpCreated) {
446 HttpIoDestroyIo (&Private->HttpIo);
447 Private->HttpCreated = FALSE;
448 }
449
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));
454 Private->Port = 0;
455 Private->BootFileUri = NULL;
456 Private->BootFileUriParser = NULL;
457 Private->BootFileSize = 0;
458 Private->SelectIndex = 0;
459 Private->SelectProxyType = HttpOfferTypeMax;
460
461 if (!Private->UsingIpv6) {
462 //
463 // Stop and release the DHCP4 child.
464 //
465 Private->Dhcp4->Stop (Private->Dhcp4);
466 Private->Dhcp4->Configure (Private->Dhcp4, NULL);
467
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);
471 }
472 }
473 } else {
474 //
475 // Stop and release the DHCP6 child.
476 //
477 Private->Dhcp6->Stop (Private->Dhcp6);
478 Private->Dhcp6->Configure (Private->Dhcp6, NULL);
479
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);
483 }
484 }
485 }
486
487 if (Private->DnsServerIp != NULL) {
488 FreePool (Private->DnsServerIp);
489 Private->DnsServerIp = NULL;
490 }
491
492 if (Private->FilePathUri!= NULL) {
493 FreePool (Private->FilePathUri);
494 HttpUrlFreeParser (Private->FilePathUriParser);
495 Private->FilePathUri = NULL;
496 Private->FilePathUriParser = NULL;
497 }
498
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));
503
504 HttpBootFreeCacheList (Private);
505
506 return EFI_SUCCESS;
507 }
508
509 /**
510 Causes the driver to load a specified file.
511
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
517 to be loaded.
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
524 BufferSize.
525
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
529 BufferSize is NULL.
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
537 the request.
538
539 **/
540 EFI_STATUS
541 EFIAPI
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
548 )
549 {
550 HTTP_BOOT_PRIVATE_DATA *Private;
551 HTTP_BOOT_VIRTUAL_NIC *VirtualNic;
552 EFI_STATUS MediaStatus;
553 BOOLEAN UsingIpv6;
554 EFI_STATUS Status;
555 HTTP_BOOT_IMAGE_TYPE ImageType;
556
557 if (This == NULL || BufferSize == NULL || FilePath == NULL) {
558 return EFI_INVALID_PARAMETER;
559 }
560
561 //
562 // Only support BootPolicy
563 //
564 if (!BootPolicy) {
565 return EFI_UNSUPPORTED;
566 }
567
568 VirtualNic = HTTP_BOOT_VIRTUAL_NIC_FROM_LOADFILE (This);
569 Private = VirtualNic->Private;
570
571 //
572 // Check media status before HTTP boot start
573 //
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");
578 return EFI_NO_MEDIA;
579 }
580
581 //
582 // Check whether the virtual nic is using IPv6 or not.
583 //
584 UsingIpv6 = FALSE;
585 if (VirtualNic == Private->Ip6Nic) {
586 UsingIpv6 = TRUE;
587 }
588
589 //
590 // Initialize HTTP boot.
591 //
592 Status = HttpBootStart (Private, UsingIpv6, FilePath);
593 if (Status != EFI_SUCCESS && Status != EFI_ALREADY_STARTED) {
594 return Status;
595 }
596
597 //
598 // Load the boot file.
599 //
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);
607 }
608 return Status;
609 }
610
611 //
612 // Register the RAM Disk to the system if needed.
613 //
614 if (ImageType == ImageTypeVirtualCd || ImageType == ImageTypeVirtualDisk) {
615 Status = HttpBootRegisterRamDisk (Private, *BufferSize, Buffer, ImageType);
616 if (!EFI_ERROR (Status)) {
617 Status = EFI_WARN_FILE_SYSTEM;
618 } else {
619 AsciiPrint ("\n Error: Could not register RAM disk to the system.\n");
620 }
621 }
622
623 //
624 // Stop the HTTP Boot service after the boot image is downloaded.
625 //
626 HttpBootStop (Private);
627 return Status;
628 }
629
630 ///
631 /// Load File Protocol instance
632 ///
633 GLOBAL_REMOVE_IF_UNREFERENCED
634 EFI_LOAD_FILE_PROTOCOL gHttpBootDxeLoadFile = {
635 HttpBootDxeLoadFile
636 };
637
638 /**
639 Callback function that is invoked when the HTTP Boot driver is about to transmit or has received a
640 packet.
641
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.
647
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
654 DataType.
655
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.
658 **/
659 EFI_STATUS
660 EFIAPI
661 HttpBootCallback (
662 IN EFI_HTTP_BOOT_CALLBACK_PROTOCOL *This,
663 IN EFI_HTTP_BOOT_CALLBACK_DATA_TYPE DataType,
664 IN BOOLEAN Received,
665 IN UINT32 DataLength,
666 IN VOID *Data OPTIONAL
667 )
668 {
669 EFI_HTTP_MESSAGE *HttpMessage;
670 EFI_HTTP_HEADER *HttpHeader;
671 HTTP_BOOT_PRIVATE_DATA *Private;
672 UINT32 Percentage;
673
674 Private = HTTP_BOOT_PRIVATE_DATA_FROM_CALLBACK_PROTOCOL(This);
675
676 switch (DataType) {
677 case HttpBootDhcp4:
678 case HttpBootDhcp6:
679 Print (L".");
680 break;
681
682 case HttpBootHttpRequest:
683 if (Data != NULL) {
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);
688 }
689 }
690 break;
691
692 case HttpBootHttpResponse:
693 if (Data != NULL) {
694 HttpMessage = (EFI_HTTP_MESSAGE *) Data;
695
696 if (HttpMessage->Data.Response != NULL) {
697 if (HttpBootIsHttpRedirectStatusCode (HttpMessage->Data.Response->StatusCode)) {
698 //
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.
702 //
703 HttpHeader = HttpFindHeader (
704 HttpMessage->HeaderCount,
705 HttpMessage->Headers,
706 HTTP_HEADER_LOCATION
707 );
708 if (HttpHeader != NULL) {
709 Print (L"\n HTTP ERROR: Resource Redirected.\n New Location: %a\n", HttpHeader->FieldValue);
710 }
711 break;
712 }
713 }
714
715 HttpHeader = HttpFindHeader (
716 HttpMessage->HeaderCount,
717 HttpMessage->Headers,
718 HTTP_HEADER_CONTENT_LENGTH
719 );
720 if (HttpHeader != NULL) {
721 Private->FileSize = AsciiStrDecimalToUintn (HttpHeader->FieldValue);
722 Private->ReceivedSize = 0;
723 Private->Percentage = 0;
724 }
725 }
726 break;
727
728 case HttpBootHttpEntityBody:
729 if (DataLength != 0) {
730 if (Private->FileSize != 0) {
731 //
732 // We already know the file size, print in percentage format.
733 //
734 if (Private->ReceivedSize == 0) {
735 Print (L" File Size: %lu Bytes\n", Private->FileSize);
736 }
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);
742 }
743 } else {
744 //
745 // In some case we couldn't get the file size from the HTTP header, so we
746 // just print the downloaded file size.
747 //
748 Private->ReceivedSize += DataLength;
749 Print (L"\r Downloading...%lu Bytes", Private->ReceivedSize);
750 }
751 }
752 break;
753
754 default:
755 break;
756 };
757
758 return EFI_SUCCESS;
759 }
760
761 ///
762 /// HTTP Boot Callback Protocol instance
763 ///
764 GLOBAL_REMOVE_IF_UNREFERENCED
765 EFI_HTTP_BOOT_CALLBACK_PROTOCOL gHttpBootDxeHttpBootCallback = {
766 HttpBootCallback
767 };