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