VirtualBox

source: vbox/trunk/src/VBox/Devices/EFI/FirmwareNew/NetworkPkg/HttpBootDxe/HttpBootImpl.c@ 105670

Last change on this file since 105670 was 99404, checked in by vboxsync, 20 months ago

Devices/EFI/FirmwareNew: Update to edk2-stable202302 and make it build, bugref:4643

  • Property svn:eol-style set to native
File size: 24.2 KB
Line 
1/** @file
2 The implementation of EFI_LOAD_FILE_PROTOCOL for UEFI HTTP boot.
3
4Copyright (c) 2015 - 2018, Intel Corporation. All rights reserved.<BR>
5(C) Copyright 2016 Hewlett Packard Enterprise Development LP<BR>
6SPDX-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**/
21EFI_STATUS
22HttpBootInstallCallback (
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**/
75VOID
76HttpBootUninstallCallback (
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**/
109EFI_STATUS
110HttpBootStart (
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**/
245EFI_STATUS
246HttpBootDhcp (
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**/
300EFI_STATUS
301HttpBootLoadFile (
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 ((Private->AuthData != NULL) && (Status == EFI_ACCESS_DENIED)) {
364 //
365 // Try to use HTTP HEAD method again since the Authentication information is provided.
366 //
367 Status = HttpBootGetBootFile (
368 Private,
369 TRUE,
370 &Private->BootFileSize,
371 NULL,
372 &Private->ImageType
373 );
374 } else if ((EFI_ERROR (Status)) && (Status != EFI_BUFFER_TOO_SMALL)) {
375 //
376 // Failed to get file size by HEAD method, may be trunked encoding, try HTTP GET method.
377 //
378 ASSERT (Private->BootFileSize == 0);
379 Status = HttpBootGetBootFile (
380 Private,
381 FALSE,
382 &Private->BootFileSize,
383 NULL,
384 &Private->ImageType
385 );
386 if (EFI_ERROR (Status) && (Status != EFI_BUFFER_TOO_SMALL)) {
387 AsciiPrint ("\n Error: Could not retrieve NBP file size from HTTP server.\n");
388 goto ON_EXIT;
389 }
390 }
391 }
392
393 if (*BufferSize < Private->BootFileSize) {
394 *BufferSize = Private->BootFileSize;
395 *ImageType = Private->ImageType;
396 Status = EFI_BUFFER_TOO_SMALL;
397 goto ON_EXIT;
398 }
399
400 //
401 // Load the boot file into Buffer
402 //
403 Status = HttpBootGetBootFile (
404 Private,
405 FALSE,
406 BufferSize,
407 Buffer,
408 ImageType
409 );
410
411ON_EXIT:
412 HttpBootUninstallCallback (Private);
413
414 if (EFI_ERROR (Status)) {
415 if (Status == EFI_ACCESS_DENIED) {
416 AsciiPrint ("\n Error: Could not establish connection with HTTP server.\n");
417 } else if ((Status == EFI_BUFFER_TOO_SMALL) && (Buffer != NULL)) {
418 AsciiPrint ("\n Error: Buffer size is smaller than the requested file.\n");
419 } else if (Status == EFI_OUT_OF_RESOURCES) {
420 AsciiPrint ("\n Error: Could not allocate I/O buffers.\n");
421 } else if (Status == EFI_DEVICE_ERROR) {
422 AsciiPrint ("\n Error: Network device error.\n");
423 } else if (Status == EFI_TIMEOUT) {
424 AsciiPrint ("\n Error: Server response timeout.\n");
425 } else if (Status == EFI_ABORTED) {
426 AsciiPrint ("\n Error: Remote boot cancelled.\n");
427 } else if (Status != EFI_BUFFER_TOO_SMALL) {
428 AsciiPrint ("\n Error: Unexpected network error.\n");
429 }
430 }
431
432 return Status;
433}
434
435/**
436 Disable the use of UEFI HTTP boot function.
437
438 @param[in] Private The pointer to the driver's private data.
439
440 @retval EFI_SUCCESS HTTP boot was successfully disabled.
441 @retval EFI_NOT_STARTED The driver is already in stopped state.
442 @retval EFI_INVALID_PARAMETER Private is NULL.
443 @retval Others Unexpected error when stop the function.
444
445**/
446EFI_STATUS
447HttpBootStop (
448 IN HTTP_BOOT_PRIVATE_DATA *Private
449 )
450{
451 UINTN Index;
452
453 if (Private == NULL) {
454 return EFI_INVALID_PARAMETER;
455 }
456
457 if (!Private->Started) {
458 return EFI_NOT_STARTED;
459 }
460
461 if (Private->HttpCreated) {
462 HttpIoDestroyIo (&Private->HttpIo);
463 Private->HttpCreated = FALSE;
464 }
465
466 Private->Started = FALSE;
467 ZeroMem (&Private->StationIp, sizeof (EFI_IP_ADDRESS));
468 ZeroMem (&Private->SubnetMask, sizeof (EFI_IP_ADDRESS));
469 ZeroMem (&Private->GatewayIp, sizeof (EFI_IP_ADDRESS));
470 Private->Port = 0;
471 Private->BootFileUri = NULL;
472 Private->BootFileUriParser = NULL;
473 Private->BootFileSize = 0;
474 Private->SelectIndex = 0;
475 Private->SelectProxyType = HttpOfferTypeMax;
476
477 if (!Private->UsingIpv6) {
478 //
479 // Stop and release the DHCP4 child.
480 //
481 Private->Dhcp4->Stop (Private->Dhcp4);
482 Private->Dhcp4->Configure (Private->Dhcp4, NULL);
483
484 for (Index = 0; Index < HTTP_BOOT_OFFER_MAX_NUM; Index++) {
485 if (Private->OfferBuffer[Index].Dhcp4.UriParser) {
486 HttpUrlFreeParser (Private->OfferBuffer[Index].Dhcp4.UriParser);
487 }
488 }
489 } else {
490 //
491 // Stop and release the DHCP6 child.
492 //
493 Private->Dhcp6->Stop (Private->Dhcp6);
494 Private->Dhcp6->Configure (Private->Dhcp6, NULL);
495
496 for (Index = 0; Index < HTTP_BOOT_OFFER_MAX_NUM; Index++) {
497 if (Private->OfferBuffer[Index].Dhcp6.UriParser) {
498 HttpUrlFreeParser (Private->OfferBuffer[Index].Dhcp6.UriParser);
499 }
500 }
501 }
502
503 if (Private->AuthData != NULL) {
504 FreePool (Private->AuthData);
505 Private->AuthData = NULL;
506 }
507
508 if (Private->AuthScheme != NULL) {
509 FreePool (Private->AuthScheme);
510 Private->AuthScheme = NULL;
511 }
512
513 if (Private->DnsServerIp != NULL) {
514 FreePool (Private->DnsServerIp);
515 Private->DnsServerIp = NULL;
516 }
517
518 if (Private->FilePathUri != NULL) {
519 FreePool (Private->FilePathUri);
520 HttpUrlFreeParser (Private->FilePathUriParser);
521 Private->FilePathUri = NULL;
522 Private->FilePathUriParser = NULL;
523 }
524
525 ZeroMem (Private->OfferBuffer, sizeof (Private->OfferBuffer));
526 Private->OfferNum = 0;
527 ZeroMem (Private->OfferCount, sizeof (Private->OfferCount));
528 ZeroMem (Private->OfferIndex, sizeof (Private->OfferIndex));
529
530 HttpBootFreeCacheList (Private);
531
532 return EFI_SUCCESS;
533}
534
535/**
536 Causes the driver to load a specified file.
537
538 @param This Protocol instance pointer.
539 @param FilePath The device specific path of the file to load.
540 @param BootPolicy If TRUE, indicates that the request originates from the
541 boot manager is attempting to load FilePath as a boot
542 selection. If FALSE, then FilePath must match as exact file
543 to be loaded.
544 @param BufferSize On input the size of Buffer in bytes. On output with a return
545 code of EFI_SUCCESS, the amount of data transferred to
546 Buffer. On output with a return code of EFI_BUFFER_TOO_SMALL,
547 the size of Buffer required to retrieve the requested file.
548 @param Buffer The memory buffer to transfer the file to. IF Buffer is NULL,
549 then the size of the requested file is returned in
550 BufferSize.
551
552 @retval EFI_SUCCESS The file was loaded.
553 @retval EFI_UNSUPPORTED The device does not support the provided BootPolicy
554 @retval EFI_INVALID_PARAMETER FilePath is not a valid device path, or
555 BufferSize is NULL.
556 @retval EFI_NO_MEDIA No medium was present to load the file.
557 @retval EFI_DEVICE_ERROR The file was not loaded due to a device error.
558 @retval EFI_NO_RESPONSE The remote system did not respond.
559 @retval EFI_NOT_FOUND The file was not found.
560 @retval EFI_ABORTED The file load process was manually cancelled.
561 @retval EFI_BUFFER_TOO_SMALL The BufferSize is too small to read the current directory entry.
562 BufferSize has been updated with the size needed to complete
563 the request.
564
565**/
566EFI_STATUS
567EFIAPI
568HttpBootDxeLoadFile (
569 IN EFI_LOAD_FILE_PROTOCOL *This,
570 IN EFI_DEVICE_PATH_PROTOCOL *FilePath,
571 IN BOOLEAN BootPolicy,
572 IN OUT UINTN *BufferSize,
573 IN VOID *Buffer OPTIONAL
574 )
575{
576 HTTP_BOOT_PRIVATE_DATA *Private;
577 HTTP_BOOT_VIRTUAL_NIC *VirtualNic;
578 EFI_STATUS MediaStatus;
579 BOOLEAN UsingIpv6;
580 EFI_STATUS Status;
581 HTTP_BOOT_IMAGE_TYPE ImageType;
582
583 if ((This == NULL) || (BufferSize == NULL) || (FilePath == NULL)) {
584 return EFI_INVALID_PARAMETER;
585 }
586
587 //
588 // Only support BootPolicy
589 //
590 if (!BootPolicy) {
591 return EFI_UNSUPPORTED;
592 }
593
594 VirtualNic = HTTP_BOOT_VIRTUAL_NIC_FROM_LOADFILE (This);
595 Private = VirtualNic->Private;
596
597 //
598 // Check media status before HTTP boot start
599 //
600 MediaStatus = EFI_SUCCESS;
601 NetLibDetectMediaWaitTimeout (Private->Controller, HTTP_BOOT_CHECK_MEDIA_WAITING_TIME, &MediaStatus);
602 if (MediaStatus != EFI_SUCCESS) {
603 AsciiPrint ("\n Error: Could not detect network connection.\n");
604 return EFI_NO_MEDIA;
605 }
606
607 //
608 // Check whether the virtual nic is using IPv6 or not.
609 //
610 UsingIpv6 = FALSE;
611 if (VirtualNic == Private->Ip6Nic) {
612 UsingIpv6 = TRUE;
613 }
614
615 //
616 // Initialize HTTP boot.
617 //
618 Status = HttpBootStart (Private, UsingIpv6, FilePath);
619 if ((Status != EFI_SUCCESS) && (Status != EFI_ALREADY_STARTED)) {
620 return Status;
621 }
622
623 //
624 // Load the boot file.
625 //
626 ImageType = ImageTypeMax;
627 Status = HttpBootLoadFile (Private, BufferSize, Buffer, &ImageType);
628 if (EFI_ERROR (Status)) {
629 if ((Status == EFI_BUFFER_TOO_SMALL) && ((ImageType == ImageTypeVirtualCd) || (ImageType == ImageTypeVirtualDisk))) {
630 Status = EFI_WARN_FILE_SYSTEM;
631 } else if (Status != EFI_BUFFER_TOO_SMALL) {
632 HttpBootStop (Private);
633 }
634
635 return Status;
636 }
637
638 //
639 // Register the RAM Disk to the system if needed.
640 //
641 if ((ImageType == ImageTypeVirtualCd) || (ImageType == ImageTypeVirtualDisk)) {
642 Status = HttpBootRegisterRamDisk (Private, *BufferSize, Buffer, ImageType);
643 if (!EFI_ERROR (Status)) {
644 Status = EFI_WARN_FILE_SYSTEM;
645 } else {
646 AsciiPrint ("\n Error: Could not register RAM disk to the system.\n");
647 }
648 }
649
650 //
651 // Stop the HTTP Boot service after the boot image is downloaded.
652 //
653 HttpBootStop (Private);
654 return Status;
655}
656
657///
658/// Load File Protocol instance
659///
660GLOBAL_REMOVE_IF_UNREFERENCED
661EFI_LOAD_FILE_PROTOCOL gHttpBootDxeLoadFile = {
662 HttpBootDxeLoadFile
663};
664
665/**
666 Callback function that is invoked when the HTTP Boot driver is about to transmit or has received a
667 packet.
668
669 This function is invoked when the HTTP Boot driver is about to transmit or has received packet.
670 Parameters DataType and Received specify the type of event and the format of the buffer pointed
671 to by Data. Due to the polling nature of UEFI device drivers, this callback function should not
672 execute for more than 5 ms.
673 The returned status code determines the behavior of the HTTP Boot driver.
674
675 @param[in] This Pointer to the EFI_HTTP_BOOT_CALLBACK_PROTOCOL instance.
676 @param[in] DataType The event that occurs in the current state.
677 @param[in] Received TRUE if the callback is being invoked due to a receive event.
678 FALSE if the callback is being invoked due to a transmit event.
679 @param[in] DataLength The length in bytes of the buffer pointed to by Data.
680 @param[in] Data A pointer to the buffer of data, the data type is specified by
681 DataType.
682
683 @retval EFI_SUCCESS Tells the HTTP Boot driver to continue the HTTP Boot process.
684 @retval EFI_ABORTED Tells the HTTP Boot driver to abort the current HTTP Boot process.
685**/
686EFI_STATUS
687EFIAPI
688HttpBootCallback (
689 IN EFI_HTTP_BOOT_CALLBACK_PROTOCOL *This,
690 IN EFI_HTTP_BOOT_CALLBACK_DATA_TYPE DataType,
691 IN BOOLEAN Received,
692 IN UINT32 DataLength,
693 IN VOID *Data OPTIONAL
694 )
695{
696 EFI_HTTP_MESSAGE *HttpMessage;
697 EFI_HTTP_HEADER *HttpHeader;
698 HTTP_BOOT_PRIVATE_DATA *Private;
699 UINT32 Percentage;
700
701 Private = HTTP_BOOT_PRIVATE_DATA_FROM_CALLBACK_PROTOCOL (This);
702
703 switch (DataType) {
704 case HttpBootDhcp4:
705 case HttpBootDhcp6:
706 Print (L".");
707 break;
708
709 case HttpBootHttpRequest:
710 if (Data != NULL) {
711 HttpMessage = (EFI_HTTP_MESSAGE *)Data;
712 if ((HttpMessage->Data.Request->Method == HttpMethodGet) &&
713 (HttpMessage->Data.Request->Url != NULL))
714 {
715 Print (L"\n URI: %s\n", HttpMessage->Data.Request->Url);
716 }
717 }
718
719 break;
720
721 case HttpBootHttpResponse:
722 if (Data != NULL) {
723 HttpMessage = (EFI_HTTP_MESSAGE *)Data;
724
725 if (HttpMessage->Data.Response != NULL) {
726 if (HttpBootIsHttpRedirectStatusCode (HttpMessage->Data.Response->StatusCode)) {
727 //
728 // Server indicates the resource has been redirected to a different URL
729 // according to the section 6.4 of RFC7231 and the RFC 7538.
730 // Display the redirect information on the screen.
731 //
732 HttpHeader = HttpFindHeader (
733 HttpMessage->HeaderCount,
734 HttpMessage->Headers,
735 HTTP_HEADER_LOCATION
736 );
737 if (HttpHeader != NULL) {
738 Print (L"\n HTTP ERROR: Resource Redirected.\n New Location: %a\n", HttpHeader->FieldValue);
739 }
740
741 break;
742 }
743 }
744
745 HttpHeader = HttpFindHeader (
746 HttpMessage->HeaderCount,
747 HttpMessage->Headers,
748 HTTP_HEADER_CONTENT_LENGTH
749 );
750 if (HttpHeader != NULL) {
751 Private->FileSize = AsciiStrDecimalToUintn (HttpHeader->FieldValue);
752 Private->ReceivedSize = 0;
753 Private->Percentage = 0;
754 }
755 }
756
757 break;
758
759 case HttpBootHttpEntityBody:
760 if (DataLength != 0) {
761 if (Private->FileSize != 0) {
762 //
763 // We already know the file size, print in percentage format.
764 //
765 if (Private->ReceivedSize == 0) {
766 Print (L" File Size: %lu Bytes\n", Private->FileSize);
767 }
768
769 Private->ReceivedSize += DataLength;
770 Percentage = (UINT32)DivU64x64Remainder (MultU64x32 (Private->ReceivedSize, 100), Private->FileSize, NULL);
771 if (Private->Percentage != Percentage) {
772 Private->Percentage = Percentage;
773 Print (L"\r Downloading...%d%%", Percentage);
774 }
775 } else {
776 //
777 // In some case we couldn't get the file size from the HTTP header, so we
778 // just print the downloaded file size.
779 //
780 Private->ReceivedSize += DataLength;
781 Print (L"\r Downloading...%lu Bytes", Private->ReceivedSize);
782 }
783 }
784
785 break;
786
787 default:
788 break;
789 }
790
791 return EFI_SUCCESS;
792}
793
794///
795/// HTTP Boot Callback Protocol instance
796///
797GLOBAL_REMOVE_IF_UNREFERENCED
798EFI_HTTP_BOOT_CALLBACK_PROTOCOL gHttpBootDxeHttpBootCallback = {
799 HttpBootCallback
800};
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette