]> git.proxmox.com Git - mirror_edk2.git/blob - ShellPkg/DynamicCommand/HttpDynamicCommand/Http.c
ShellPkg: Apply uncrustify changes
[mirror_edk2.git] / ShellPkg / DynamicCommand / HttpDynamicCommand / Http.c
1 /** @file
2 The implementation for the 'http' Shell command.
3
4 Copyright (c) 2015, ARM Ltd. All rights reserved.<BR>
5 Copyright (c) 2015 - 2018, Intel Corporation. All rights reserved. <BR>
6 (C) Copyright 2015 Hewlett Packard Enterprise Development LP<BR>
7 Copyright (c) 2020, Broadcom. All rights reserved. <BR>
8
9 SPDX-License-Identifier: BSD-2-Clause-Patent
10 **/
11
12 #include "Http.h"
13
14 #define IP4_CONFIG2_INTERFACE_INFO_NAME_LENGTH 32
15
16 //
17 // Constant strings and definitions related to the message
18 // indicating the amount of progress in the dowloading of a HTTP file.
19 //
20
21 //
22 // Number of steps in the progression slider.
23 //
24 #define HTTP_PROGRESS_SLIDER_STEPS \
25 ((sizeof (HTTP_PROGR_FRAME) / sizeof (CHAR16)) - 3)
26
27 //
28 // Size in number of characters plus one (final zero) of the message to
29 // indicate the progress of an HTTP download. The format is "[(progress slider:
30 // 40 characters)] (nb of KBytes downloaded so far: 7 characters) Kb". There
31 // are thus the number of characters in HTTP_PROGR_FRAME[] plus 11 characters
32 // (2 // spaces, "Kb" and seven characters for the number of KBytes).
33 //
34 #define HTTP_PROGRESS_MESSAGE_SIZE \
35 ((sizeof (HTTP_PROGR_FRAME) / sizeof (CHAR16)) + 12)
36
37 //
38 // Buffer size. Note that larger buffer does not mean better speed.
39 //
40 #define DEFAULT_BUF_SIZE SIZE_32KB
41 #define MAX_BUF_SIZE SIZE_4MB
42
43 #define MIN_PARAM_COUNT 2
44 #define MAX_PARAM_COUNT 4
45 #define NEED_REDIRECTION(Code) \
46 (((Code >= HTTP_STATUS_300_MULTIPLE_CHOICES) \
47 && (Code <= HTTP_STATUS_307_TEMPORARY_REDIRECT)) \
48 || (Code == HTTP_STATUS_308_PERMANENT_REDIRECT))
49
50 #define CLOSE_HTTP_HANDLE(ControllerHandle, HttpChildHandle) \
51 do { \
52 if (HttpChildHandle) { \
53 CloseProtocolAndDestroyServiceChild ( \
54 ControllerHandle, \
55 &gEfiHttpServiceBindingProtocolGuid, \
56 &gEfiHttpProtocolGuid, \
57 HttpChildHandle \
58 ); \
59 HttpChildHandle = NULL; \
60 } \
61 } while (0)
62
63 typedef enum {
64 HdrHost,
65 HdrConn,
66 HdrAgent,
67 HdrMax
68 } HDR_TYPE;
69
70 #define USER_AGENT_HDR "Mozilla/5.0 (EDK2; Linux) Gecko/20100101 Firefox/79.0"
71
72 #define TIMER_MAX_TIMEOUT_S 10
73
74 //
75 // File name to use when Uri ends with "/".
76 //
77 #define DEFAULT_HTML_FILE L"index.html"
78 #define DEFAULT_HTTP_PROTO L"http"
79
80 //
81 // String to delete the HTTP progress message to be able to update it :
82 // (HTTP_PROGRESS_MESSAGE_SIZE-1) '\b'.
83 //
84 #define HTTP_PROGRESS_DEL \
85 L"\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\
86 \b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b"
87
88 #define HTTP_KB L"\b\b\b\b\b\b\b\b\b\b"
89 //
90 // Frame for the progression slider.
91 //
92 #define HTTP_PROGR_FRAME L"[ ]"
93
94 //
95 // Improve readability by using these macros.
96 //
97 #define PRINT_HII(token, ...) \
98 ShellPrintHiiEx (\
99 -1, -1, NULL, token, mHttpHiiHandle, __VA_ARGS__)
100
101 #define PRINT_HII_APP(token, value) \
102 PRINT_HII (token, HTTP_APP_NAME, value)
103
104 //
105 // TimeBaseLib.h constants.
106 // These will be removed once the library gets fixed.
107 //
108
109 //
110 // Define EPOCH (1970-JANUARY-01) in the Julian Date representation.
111 //
112 #define EPOCH_JULIAN_DATE 2440588
113
114 //
115 // Seconds per unit.
116 //
117 #define SEC_PER_MIN ((UINTN) 60)
118 #define SEC_PER_HOUR ((UINTN) 3600)
119 #define SEC_PER_DAY ((UINTN) 86400)
120
121 //
122 // String descriptions for server errors.
123 //
124 STATIC CONST CHAR16 *ErrStatusDesc[] =
125 {
126 L"400 Bad Request",
127 L"401 Unauthorized",
128 L"402 Payment required",
129 L"403 Forbidden",
130 L"404 Not Found",
131 L"405 Method not allowed",
132 L"406 Not acceptable",
133 L"407 Proxy authentication required",
134 L"408 Request time out",
135 L"409 Conflict",
136 L"410 Gone",
137 L"411 Length required",
138 L"412 Precondition failed",
139 L"413 Request entity too large",
140 L"414 Request URI to large",
141 L"415 Unsupported media type",
142 L"416 Requested range not satisfied",
143 L"417 Expectation failed",
144 L"500 Internal server error",
145 L"501 Not implemented",
146 L"502 Bad gateway",
147 L"503 Service unavailable",
148 L"504 Gateway timeout",
149 L"505 HTTP version not supported"
150 };
151
152 STATIC CONST SHELL_PARAM_ITEM ParamList[] = {
153 { L"-i", TypeValue },
154 { L"-k", TypeFlag },
155 { L"-l", TypeValue },
156 { L"-m", TypeFlag },
157 { L"-s", TypeValue },
158 { L"-t", TypeValue },
159 { NULL, TypeMax }
160 };
161
162 //
163 // Local File Handle.
164 //
165 STATIC SHELL_FILE_HANDLE mFileHandle = NULL;
166
167 //
168 // Path of the local file, Unicode encoded.
169 //
170 STATIC CONST CHAR16 *mLocalFilePath;
171
172 STATIC BOOLEAN gRequestCallbackComplete = FALSE;
173 STATIC BOOLEAN gResponseCallbackComplete = FALSE;
174
175 STATIC BOOLEAN gHttpError;
176
177 EFI_HII_HANDLE mHttpHiiHandle;
178
179 //
180 // Functions declarations.
181 //
182
183 /**
184 Check and convert the UINT16 option values of the 'http' command.
185
186 @param[in] ValueStr Value as an Unicode encoded string.
187 @param[out] Value UINT16 value.
188
189 @retval TRUE The value was returned.
190 @retval FALSE A parsing error occured.
191 **/
192 STATIC
193 BOOLEAN
194 StringToUint16 (
195 IN CONST CHAR16 *ValueStr,
196 OUT UINT16 *Value
197 );
198
199 /**
200 Get the name of the NIC.
201
202 @param[in] ControllerHandle The network physical device handle.
203 @param[in] NicNumber The network physical device number.
204 @param[out] NicName Address where to store the NIC name.
205 The memory area has to be at least
206 IP4_CONFIG2_INTERFACE_INFO_NAME_LENGTH
207 double byte wide.
208
209 @retval EFI_SUCCESS The name of the NIC was returned.
210 @retval Others The creation of the child for the Managed
211 Network Service failed or the opening of
212 the Managed Network Protocol failed or
213 the operational parameters for the
214 Managed Network Protocol could not be
215 read.
216 **/
217 STATIC
218 EFI_STATUS
219 GetNicName (
220 IN EFI_HANDLE ControllerHandle,
221 IN UINTN NicNumber,
222 OUT CHAR16 *NicName
223 );
224
225 /**
226 Create a child for the service identified by its service binding protocol GUID
227 and get from the child the interface of the protocol identified by its GUID.
228
229 @param[in] ControllerHandle Controller handle.
230 @param[in] ServiceBindingProtocolGuid Service binding protocol GUID of the
231 service to be created.
232 @param[in] ProtocolGuid GUID of the protocol to be open.
233 @param[out] ChildHandle Address where the handler of the
234 created child is returned. NULL is
235 returned in case of error.
236 @param[out] Interface Address where a pointer to the
237 protocol interface is returned in
238 case of success.
239
240 @retval EFI_SUCCESS The child was created and the protocol opened.
241 @retval Others Either the creation of the child or the opening
242 of the protocol failed.
243 **/
244 STATIC
245 EFI_STATUS
246 CreateServiceChildAndOpenProtocol (
247 IN EFI_HANDLE ControllerHandle,
248 IN EFI_GUID *ServiceBindingProtocolGuid,
249 IN EFI_GUID *ProtocolGuid,
250 OUT EFI_HANDLE *ChildHandle,
251 OUT VOID **Interface
252 );
253
254 /**
255 Close the protocol identified by its GUID on the child handle of the service
256 identified by its service binding protocol GUID, then destroy the child
257 handle.
258
259 @param[in] ControllerHandle Controller handle.
260 @param[in] ServiceBindingProtocolGuid Service binding protocol GUID of the
261 service to be destroyed.
262 @param[in] ProtocolGuid GUID of the protocol to be closed.
263 @param[in] ChildHandle Handle of the child to be destroyed.
264
265 **/
266 STATIC
267 VOID
268 CloseProtocolAndDestroyServiceChild (
269 IN EFI_HANDLE ControllerHandle,
270 IN EFI_GUID *ServiceBindingProtocolGuid,
271 IN EFI_GUID *ProtocolGuid,
272 IN EFI_HANDLE ChildHandle
273 );
274
275 /**
276 Worker function that download the data of a file from an HTTP server given
277 the path of the file and its size.
278
279 @param[in] Context A pointer to the download context.
280
281 @retval EFI_SUCCESS The file was downloaded.
282 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
283 @retval Others The downloading of the file
284 from the server failed.
285 **/
286 STATIC
287 EFI_STATUS
288 DownloadFile (
289 IN HTTP_DOWNLOAD_CONTEXT *Context,
290 IN EFI_HANDLE ControllerHandle,
291 IN CHAR16 *NicName
292 );
293
294 /**
295 Cleans off leading and trailing spaces and tabs.
296
297 @param[in] String pointer to the string to trim them off.
298
299 @retval EFI_SUCCESS No errors.
300 @retval EFI_INVALID_PARAMETER String pointer is NULL.
301 **/
302 STATIC
303 EFI_STATUS
304 TrimSpaces (
305 IN CHAR16 *String
306 )
307 {
308 CHAR16 *Str;
309 UINTN Len;
310
311 ASSERT (String != NULL);
312
313 if (String == NULL) {
314 return EFI_INVALID_PARAMETER;
315 }
316
317 Str = String;
318
319 //
320 // Remove any whitespace at the beginning of the Str.
321 //
322 while (*Str == L' ' || *Str == L'\t') {
323 Str++;
324 }
325
326 //
327 // Remove any whitespace at the end of the Str.
328 //
329 do {
330 Len = StrLen (Str);
331 if (!Len || ((Str[Len - 1] != L' ') && (Str[Len - 1] != '\t'))) {
332 break;
333 }
334
335 Str[Len - 1] = CHAR_NULL;
336 } while (Len);
337
338 CopyMem (String, Str, StrSize (Str));
339
340 return EFI_SUCCESS;
341 }
342
343 //
344 // Callbacks for request and response.
345 // We just acknowledge that operation has completed here.
346 //
347
348 /**
349 Callback to set the request completion flag.
350
351 @param[in] Event: The event.
352 @param[in] Context: pointer to Notification Context.
353 **/
354 STATIC
355 VOID
356 EFIAPI
357 RequestCallback (
358 IN EFI_EVENT Event,
359 IN VOID *Context
360 )
361 {
362 gRequestCallbackComplete = TRUE;
363 }
364
365 /**
366 Callback to set the response completion flag.
367 @param[in] Event: The event.
368 @param[in] Context: pointer to Notification Context.
369 **/
370 STATIC
371 VOID
372 EFIAPI
373 ResponseCallback (
374 IN EFI_EVENT Event,
375 IN VOID *Context
376 )
377 {
378 gResponseCallbackComplete = TRUE;
379 }
380
381 //
382 // Set of functions from TimeBaseLib.
383 // This will be removed once TimeBaseLib is enabled for ShellPkg.
384 //
385
386 /**
387 Calculate Epoch days.
388
389 @param[in] Time - a pointer to the EFI_TIME abstraction.
390
391 @retval Number of days elapsed since EPOCH_JULIAN_DAY.
392 **/
393 STATIC
394 UINTN
395 EfiGetEpochDays (
396 IN EFI_TIME *Time
397 )
398 {
399 UINTN a;
400 UINTN y;
401 UINTN m;
402 //
403 // Absolute Julian Date representation of the supplied Time.
404 //
405 UINTN JulianDate;
406 //
407 // Number of days elapsed since EPOCH_JULIAN_DAY.
408 //
409 UINTN EpochDays;
410
411 a = (14 - Time->Month) / 12;
412 y = Time->Year + 4800 - a;
413 m = Time->Month + (12 * a) - 3;
414
415 JulianDate = Time->Day + ((153 * m + 2) / 5) + (365 * y) + (y / 4) -
416 (y / 100) + (y / 400) - 32045;
417
418 ASSERT (JulianDate >= EPOCH_JULIAN_DATE);
419 EpochDays = JulianDate - EPOCH_JULIAN_DATE;
420
421 return EpochDays;
422 }
423
424 /**
425 Converts EFI_TIME to Epoch seconds
426 (elapsed since 1970 JANUARY 01, 00:00:00 UTC).
427
428 @param[in] Time: a pointer to EFI_TIME abstraction.
429 **/
430 STATIC
431 UINTN
432 EFIAPI
433 EfiTimeToEpoch (
434 IN EFI_TIME *Time
435 )
436 {
437 //
438 // Number of days elapsed since EPOCH_JULIAN_DAY.
439 //
440 UINTN EpochDays;
441 UINTN EpochSeconds;
442
443 EpochDays = EfiGetEpochDays (Time);
444
445 EpochSeconds = (EpochDays * SEC_PER_DAY) +
446 ((UINTN)Time->Hour * SEC_PER_HOUR) +
447 (Time->Minute * SEC_PER_MIN) + Time->Second;
448
449 return EpochSeconds;
450 }
451
452 /**
453 Function for 'http' command.
454
455 @param[in] ImageHandle Handle to the Image (NULL if Internal).
456 @param[in] SystemTable Pointer to the System Table (NULL if Internal).
457
458 @retval SHELL_SUCCESS The 'http' command completed successfully.
459 @retval SHELL_ABORTED The Shell Library initialization failed.
460 @retval SHELL_INVALID_PARAMETER At least one of the command's arguments is
461 not valid.
462 @retval SHELL_OUT_OF_RESOURCES A memory allocation failed.
463 @retval SHELL_NOT_FOUND Network Interface Card not found.
464 @retval SHELL_UNSUPPORTED Command was valid, but the server returned
465 a status code indicating some error.
466 Examine the file requested for error body.
467 **/
468 SHELL_STATUS
469 RunHttp (
470 IN EFI_HANDLE ImageHandle,
471 IN EFI_SYSTEM_TABLE *SystemTable
472 )
473 {
474 EFI_STATUS Status;
475 LIST_ENTRY *CheckPackage;
476 UINTN ParamCount;
477 UINTN HandleCount;
478 UINTN NicNumber;
479 UINTN InitialSize;
480 UINTN ParamOffset;
481 UINTN StartSize;
482 CHAR16 *ProblemParam;
483 CHAR16 NicName[IP4_CONFIG2_INTERFACE_INFO_NAME_LENGTH];
484 CHAR16 *Walker1;
485 CHAR16 *VStr;
486 CONST CHAR16 *UserNicName;
487 CONST CHAR16 *ValueStr;
488 CONST CHAR16 *RemoteFilePath;
489 CONST CHAR16 *Walker;
490 EFI_HTTPv4_ACCESS_POINT IPv4Node;
491 EFI_HANDLE *Handles;
492 EFI_HANDLE ControllerHandle;
493 HTTP_DOWNLOAD_CONTEXT Context;
494 BOOLEAN NicFound;
495
496 ProblemParam = NULL;
497 RemoteFilePath = NULL;
498 NicFound = FALSE;
499 Handles = NULL;
500
501 //
502 // Initialize the Shell library (we must be in non-auto-init...).
503 //
504 ParamOffset = 0;
505 gHttpError = FALSE;
506
507 Status = ShellInitialize ();
508 if (EFI_ERROR (Status)) {
509 ASSERT_EFI_ERROR (Status);
510 return SHELL_ABORTED;
511 }
512
513 ZeroMem (&Context, sizeof (Context));
514
515 //
516 // Parse the command line.
517 //
518 Status = ShellCommandLineParse (
519 ParamList,
520 &CheckPackage,
521 &ProblemParam,
522 TRUE
523 );
524 if (EFI_ERROR (Status)) {
525 if ( (Status == EFI_VOLUME_CORRUPTED)
526 && (ProblemParam != NULL))
527 {
528 PRINT_HII_APP (STRING_TOKEN (STR_GEN_PROBLEM), ProblemParam);
529 SHELL_FREE_NON_NULL (ProblemParam);
530 } else {
531 ASSERT (FALSE);
532 }
533
534 goto Error;
535 }
536
537 //
538 // Check the number of parameters.
539 //
540 Status = EFI_INVALID_PARAMETER;
541
542 ParamCount = ShellCommandLineGetCount (CheckPackage);
543 if (ParamCount > MAX_PARAM_COUNT) {
544 PRINT_HII_APP (STRING_TOKEN (STR_GEN_TOO_MANY), NULL);
545 goto Error;
546 }
547
548 if (ParamCount < MIN_PARAM_COUNT) {
549 PRINT_HII_APP (STRING_TOKEN (STR_GEN_TOO_FEW), NULL);
550 goto Error;
551 }
552
553 ZeroMem (&Context.HttpConfigData, sizeof (Context.HttpConfigData));
554 ZeroMem (&IPv4Node, sizeof (IPv4Node));
555 IPv4Node.UseDefaultAddress = TRUE;
556
557 Context.HttpConfigData.HttpVersion = HttpVersion11;
558 Context.HttpConfigData.AccessPoint.IPv4Node = &IPv4Node;
559
560 //
561 // Get the host address (not necessarily IPv4 format).
562 //
563 ValueStr = ShellCommandLineGetRawValue (CheckPackage, 1);
564 if (!ValueStr) {
565 PRINT_HII_APP (STRING_TOKEN (STR_GEN_PARAM_INV), ValueStr);
566 goto Error;
567 } else {
568 StartSize = 0;
569 TrimSpaces ((CHAR16 *)ValueStr);
570 if (!StrStr (ValueStr, L"://")) {
571 Context.ServerAddrAndProto = StrnCatGrow (
572 &Context.ServerAddrAndProto,
573 &StartSize,
574 DEFAULT_HTTP_PROTO,
575 StrLen (DEFAULT_HTTP_PROTO)
576 );
577 Context.ServerAddrAndProto = StrnCatGrow (
578 &Context.ServerAddrAndProto,
579 &StartSize,
580 L"://",
581 StrLen (L"://")
582 );
583 VStr = (CHAR16 *)ValueStr;
584 } else {
585 VStr = StrStr (ValueStr, L"://") + StrLen (L"://");
586 }
587
588 for (Walker1 = VStr; *Walker1; Walker1++) {
589 if (*Walker1 == L'/') {
590 break;
591 }
592 }
593
594 if (*Walker1 == L'/') {
595 ParamOffset = 1;
596 RemoteFilePath = Walker1;
597 }
598
599 Context.ServerAddrAndProto = StrnCatGrow (
600 &Context.ServerAddrAndProto,
601 &StartSize,
602 ValueStr,
603 StrLen (ValueStr) - StrLen (Walker1)
604 );
605 if (!Context.ServerAddrAndProto) {
606 Status = EFI_OUT_OF_RESOURCES;
607 goto Error;
608 }
609 }
610
611 if (!RemoteFilePath) {
612 RemoteFilePath = ShellCommandLineGetRawValue (CheckPackage, 2);
613 if (!RemoteFilePath) {
614 //
615 // If no path given, assume just "/".
616 //
617 RemoteFilePath = L"/";
618 }
619 }
620
621 TrimSpaces ((CHAR16 *)RemoteFilePath);
622
623 if (ParamCount == MAX_PARAM_COUNT - ParamOffset) {
624 mLocalFilePath = ShellCommandLineGetRawValue (
625 CheckPackage,
626 MAX_PARAM_COUNT - 1 - ParamOffset
627 );
628 } else {
629 Walker = RemoteFilePath + StrLen (RemoteFilePath);
630 while ((--Walker) >= RemoteFilePath) {
631 if ((*Walker == L'\\') ||
632 (*Walker == L'/'))
633 {
634 break;
635 }
636 }
637
638 mLocalFilePath = Walker + 1;
639 }
640
641 if (!StrLen (mLocalFilePath)) {
642 mLocalFilePath = DEFAULT_HTML_FILE;
643 }
644
645 InitialSize = 0;
646 Context.Uri = StrnCatGrow (
647 &Context.Uri,
648 &InitialSize,
649 RemoteFilePath,
650 StrLen (RemoteFilePath)
651 );
652 if (!Context.Uri) {
653 Status = EFI_OUT_OF_RESOURCES;
654 goto Error;
655 }
656
657 //
658 // Get the name of the Network Interface Card to be used if any.
659 //
660 UserNicName = ShellCommandLineGetValue (CheckPackage, L"-i");
661
662 ValueStr = ShellCommandLineGetValue (CheckPackage, L"-l");
663 if ( (ValueStr != NULL)
664 && (!StringToUint16 (
665 ValueStr,
666 &Context.HttpConfigData.AccessPoint.IPv4Node->LocalPort
667 )
668 ))
669 {
670 goto Error;
671 }
672
673 Context.BufferSize = DEFAULT_BUF_SIZE;
674
675 ValueStr = ShellCommandLineGetValue (CheckPackage, L"-s");
676 if (ValueStr != NULL) {
677 Context.BufferSize = ShellStrToUintn (ValueStr);
678 if (!Context.BufferSize || (Context.BufferSize > MAX_BUF_SIZE)) {
679 PRINT_HII_APP (STRING_TOKEN (STR_GEN_PARAM_INV), ValueStr);
680 goto Error;
681 }
682 }
683
684 ValueStr = ShellCommandLineGetValue (CheckPackage, L"-t");
685 if (ValueStr != NULL) {
686 Context.HttpConfigData.TimeOutMillisec = (UINT32)ShellStrToUintn (ValueStr);
687 }
688
689 //
690 // Locate all HTTP Service Binding protocols.
691 //
692 Status = gBS->LocateHandleBuffer (
693 ByProtocol,
694 &gEfiManagedNetworkServiceBindingProtocolGuid,
695 NULL,
696 &HandleCount,
697 &Handles
698 );
699 if (EFI_ERROR (Status) || (HandleCount == 0)) {
700 PRINT_HII (STRING_TOKEN (STR_HTTP_ERR_NO_NIC), NULL);
701 if (!EFI_ERROR (Status)) {
702 Status = EFI_NOT_FOUND;
703 }
704
705 goto Error;
706 }
707
708 Status = EFI_NOT_FOUND;
709
710 Context.Flags = 0;
711 if (ShellCommandLineGetFlag (CheckPackage, L"-m")) {
712 Context.Flags |= DL_FLAG_TIME;
713 }
714
715 if (ShellCommandLineGetFlag (CheckPackage, L"-k")) {
716 Context.Flags |= DL_FLAG_KEEP_BAD;
717 }
718
719 for (NicNumber = 0;
720 (NicNumber < HandleCount) && (Status != EFI_SUCCESS);
721 NicNumber++)
722 {
723 ControllerHandle = Handles[NicNumber];
724
725 Status = GetNicName (ControllerHandle, NicNumber, NicName);
726 if (EFI_ERROR (Status)) {
727 PRINT_HII (STRING_TOKEN (STR_HTTP_ERR_NIC_NAME), NicNumber, Status);
728 continue;
729 }
730
731 if (UserNicName != NULL) {
732 if (StrCmp (NicName, UserNicName) != 0) {
733 Status = EFI_NOT_FOUND;
734 continue;
735 }
736
737 NicFound = TRUE;
738 }
739
740 Status = DownloadFile (&Context, ControllerHandle, NicName);
741 PRINT_HII (STRING_TOKEN (STR_GEN_CRLF), NULL);
742
743 if (EFI_ERROR (Status)) {
744 PRINT_HII (
745 STRING_TOKEN (STR_HTTP_ERR_DOWNLOAD),
746 RemoteFilePath,
747 NicName,
748 Status
749 );
750 //
751 // If a user aborted the operation,
752 // do not try another controller.
753 //
754 if (Status == EFI_ABORTED) {
755 goto Error;
756 }
757 }
758
759 if (gHttpError) {
760 //
761 // This is not related to connection, so no need to repeat with
762 // another interface.
763 //
764 break;
765 }
766 }
767
768 if ((UserNicName != NULL) && (!NicFound)) {
769 PRINT_HII (STRING_TOKEN (STR_HTTP_ERR_NIC_NOT_FOUND), UserNicName);
770 }
771
772 Error:
773 ShellCommandLineFreeVarList (CheckPackage);
774 SHELL_FREE_NON_NULL (Handles);
775 SHELL_FREE_NON_NULL (Context.ServerAddrAndProto);
776 SHELL_FREE_NON_NULL (Context.Uri);
777
778 return Status & ~MAX_BIT;
779 }
780
781 /**
782 Check and convert the UINT16 option values of the 'http' command
783
784 @param[in] ValueStr Value as an Unicode encoded string
785 @param[out] Value UINT16 value
786
787 @retval TRUE The value was returned.
788 @retval FALSE A parsing error occured.
789 **/
790 STATIC
791 BOOLEAN
792 StringToUint16 (
793 IN CONST CHAR16 *ValueStr,
794 OUT UINT16 *Value
795 )
796 {
797 UINTN Val;
798
799 Val = ShellStrToUintn (ValueStr);
800 if (Val > MAX_UINT16) {
801 PRINT_HII_APP (STRING_TOKEN (STR_GEN_PARAM_INV), ValueStr);
802 return FALSE;
803 }
804
805 *Value = (UINT16)Val;
806 return TRUE;
807 }
808
809 /**
810 Get the name of the NIC.
811
812 @param[in] ControllerHandle The network physical device handle.
813 @param[in] NicNumber The network physical device number.
814 @param[out] NicName Address where to store the NIC name.
815 The memory area has to be at least
816 IP4_CONFIG2_INTERFACE_INFO_NAME_LENGTH
817 double byte wide.
818
819 @retval EFI_SUCCESS The name of the NIC was returned.
820 @retval Others The creation of the child for the Managed
821 Network Service failed or the opening of
822 the Managed Network Protocol failed or
823 the operational parameters for the
824 Managed Network Protocol could not be
825 read.
826 **/
827 STATIC
828 EFI_STATUS
829 GetNicName (
830 IN EFI_HANDLE ControllerHandle,
831 IN UINTN NicNumber,
832 OUT CHAR16 *NicName
833 )
834 {
835 EFI_STATUS Status;
836 EFI_HANDLE MnpHandle;
837 EFI_MANAGED_NETWORK_PROTOCOL *Mnp;
838 EFI_SIMPLE_NETWORK_MODE SnpMode;
839
840 Status = CreateServiceChildAndOpenProtocol (
841 ControllerHandle,
842 &gEfiManagedNetworkServiceBindingProtocolGuid,
843 &gEfiManagedNetworkProtocolGuid,
844 &MnpHandle,
845 (VOID **)&Mnp
846 );
847 if (EFI_ERROR (Status)) {
848 goto Error;
849 }
850
851 Status = Mnp->GetModeData (Mnp, NULL, &SnpMode);
852 if (EFI_ERROR (Status) && (Status != EFI_NOT_STARTED)) {
853 goto Error;
854 }
855
856 UnicodeSPrint (
857 NicName,
858 IP4_CONFIG2_INTERFACE_INFO_NAME_LENGTH,
859 SnpMode.IfType == NET_IFTYPE_ETHERNET ? L"eth%d" : L"unk%d",
860 NicNumber
861 );
862
863 Status = EFI_SUCCESS;
864
865 Error:
866
867 if (MnpHandle != NULL) {
868 CloseProtocolAndDestroyServiceChild (
869 ControllerHandle,
870 &gEfiManagedNetworkServiceBindingProtocolGuid,
871 &gEfiManagedNetworkProtocolGuid,
872 MnpHandle
873 );
874 }
875
876 return Status;
877 }
878
879 /**
880 Create a child for the service identified by its service binding protocol GUID
881 and get from the child the interface of the protocol identified by its GUID.
882
883 @param[in] ControllerHandle Controller handle.
884 @param[in] ServiceBindingProtocolGuid Service binding protocol GUID of the
885 service to be created.
886 @param[in] ProtocolGuid GUID of the protocol to be open.
887 @param[out] ChildHandle Address where the handler of the
888 created child is returned. NULL is
889 returned in case of error.
890 @param[out] Interface Address where a pointer to the
891 protocol interface is returned in
892 case of success.
893
894 @retval EFI_SUCCESS The child was created and the protocol opened.
895 @retval Others Either the creation of the child or the opening
896 of the protocol failed.
897 **/
898 STATIC
899 EFI_STATUS
900 CreateServiceChildAndOpenProtocol (
901 IN EFI_HANDLE ControllerHandle,
902 IN EFI_GUID *ServiceBindingProtocolGuid,
903 IN EFI_GUID *ProtocolGuid,
904 OUT EFI_HANDLE *ChildHandle,
905 OUT VOID **Interface
906 )
907 {
908 EFI_STATUS Status;
909
910 *ChildHandle = NULL;
911 Status = NetLibCreateServiceChild (
912 ControllerHandle,
913 gImageHandle,
914 ServiceBindingProtocolGuid,
915 ChildHandle
916 );
917 if (!EFI_ERROR (Status)) {
918 Status = gBS->OpenProtocol (
919 *ChildHandle,
920 ProtocolGuid,
921 Interface,
922 gImageHandle,
923 ControllerHandle,
924 EFI_OPEN_PROTOCOL_GET_PROTOCOL
925 );
926 if (EFI_ERROR (Status)) {
927 NetLibDestroyServiceChild (
928 ControllerHandle,
929 gImageHandle,
930 ServiceBindingProtocolGuid,
931 *ChildHandle
932 );
933 *ChildHandle = NULL;
934 }
935 }
936
937 return Status;
938 }
939
940 /**
941 Close the protocol identified by its GUID on the child handle of the service
942 identified by its service binding protocol GUID, then destroy the child
943 handle.
944
945 @param[in] ControllerHandle Controller handle.
946 @param[in] ServiceBindingProtocolGuid Service binding protocol GUID of the
947 service to be destroyed.
948 @param[in] ProtocolGuid GUID of the protocol to be closed.
949 @param[in] ChildHandle Handle of the child to be destroyed.
950 **/
951 STATIC
952 VOID
953 CloseProtocolAndDestroyServiceChild (
954 IN EFI_HANDLE ControllerHandle,
955 IN EFI_GUID *ServiceBindingProtocolGuid,
956 IN EFI_GUID *ProtocolGuid,
957 IN EFI_HANDLE ChildHandle
958 )
959 {
960 gBS->CloseProtocol (
961 ChildHandle,
962 ProtocolGuid,
963 gImageHandle,
964 ControllerHandle
965 );
966
967 NetLibDestroyServiceChild (
968 ControllerHandle,
969 gImageHandle,
970 ServiceBindingProtocolGuid,
971 ChildHandle
972 );
973 }
974
975 /**
976 Wait until operation completes. Completion is indicated by
977 setting of an appropriate variable.
978
979 @param[in] Context A pointer to the HTTP download context.
980 @param[in, out] CallBackComplete A pointer to the callback completion
981 variable set by the callback.
982
983 @retval EFI_SUCCESS Callback signalled completion.
984 @retval EFI_TIMEOUT Timed out waiting for completion.
985 @retval Others Error waiting for completion.
986 **/
987 STATIC
988 EFI_STATUS
989 WaitForCompletion (
990 IN HTTP_DOWNLOAD_CONTEXT *Context,
991 IN OUT BOOLEAN *CallBackComplete
992 )
993 {
994 EFI_STATUS Status;
995 EFI_EVENT WaitEvt;
996
997 Status = EFI_SUCCESS;
998
999 //
1000 // Use a timer to measure timeout. Cannot use Stall here!
1001 //
1002 Status = gBS->CreateEvent (
1003 EVT_TIMER,
1004 TPL_CALLBACK,
1005 NULL,
1006 NULL,
1007 &WaitEvt
1008 );
1009 ASSERT_EFI_ERROR (Status);
1010
1011 if (!EFI_ERROR (Status)) {
1012 Status = gBS->SetTimer (
1013 WaitEvt,
1014 TimerRelative,
1015 EFI_TIMER_PERIOD_SECONDS (TIMER_MAX_TIMEOUT_S)
1016 );
1017
1018 ASSERT_EFI_ERROR (Status);
1019 }
1020
1021 while ( !*CallBackComplete
1022 && (!EFI_ERROR (Status))
1023 && EFI_ERROR (gBS->CheckEvent (WaitEvt)))
1024 {
1025 Status = Context->Http->Poll (Context->Http);
1026 if ( !Context->ContentDownloaded
1027 && (CallBackComplete == &gResponseCallbackComplete))
1028 {
1029 //
1030 // An HTTP server may just send a response redirection header.
1031 // In this case, don't wait for the event as
1032 // it might never happen and we waste 10s waiting.
1033 // Note that at this point Response may not has been populated,
1034 // so it needs to be checked first.
1035 //
1036 if ( Context->ResponseToken.Message
1037 && Context->ResponseToken.Message->Data.Response
1038 && (NEED_REDIRECTION (
1039 Context->ResponseToken.Message->Data.Response->StatusCode
1040 )
1041 ))
1042 {
1043 break;
1044 }
1045 }
1046 }
1047
1048 gBS->SetTimer (WaitEvt, TimerCancel, 0);
1049 gBS->CloseEvent (WaitEvt);
1050
1051 if (*CallBackComplete) {
1052 return EFI_SUCCESS;
1053 }
1054
1055 if (!EFI_ERROR (Status)) {
1056 Status = EFI_TIMEOUT;
1057 }
1058
1059 return Status;
1060 }
1061
1062 /**
1063 Generate and send a request to the http server.
1064
1065 @param[in] Context HTTP download context.
1066 @param[in] DownloadUrl Fully qualified URL to be downloaded.
1067
1068 @retval EFI_SUCCESS Request has been sent successfully.
1069 @retval EFI_INVALID_PARAMETER Invalid URL.
1070 @retval EFI_OUT_OF_RESOURCES Out of memory.
1071 @retval EFI_DEVICE_ERROR If HTTPS is used, this probably
1072 means that TLS support either was not
1073 installed or not configured.
1074 @retval Others Error sending the request.
1075 **/
1076 STATIC
1077 EFI_STATUS
1078 SendRequest (
1079 IN HTTP_DOWNLOAD_CONTEXT *Context,
1080 IN CHAR16 *DownloadUrl
1081 )
1082 {
1083 EFI_HTTP_REQUEST_DATA RequestData;
1084 EFI_HTTP_HEADER RequestHeader[HdrMax];
1085 EFI_HTTP_MESSAGE RequestMessage;
1086 EFI_STATUS Status;
1087 CHAR16 *Host;
1088 UINTN StringSize;
1089
1090 ZeroMem (&RequestData, sizeof (RequestData));
1091 ZeroMem (&RequestHeader, sizeof (RequestHeader));
1092 ZeroMem (&RequestMessage, sizeof (RequestMessage));
1093 ZeroMem (&Context->RequestToken, sizeof (Context->RequestToken));
1094
1095 RequestHeader[HdrHost].FieldName = "Host";
1096 RequestHeader[HdrConn].FieldName = "Connection";
1097 RequestHeader[HdrAgent].FieldName = "User-Agent";
1098
1099 Host = (CHAR16 *)Context->ServerAddrAndProto;
1100 while (*Host != CHAR_NULL && *Host != L'/') {
1101 Host++;
1102 }
1103
1104 if (*Host == CHAR_NULL) {
1105 return EFI_INVALID_PARAMETER;
1106 }
1107
1108 //
1109 // Get the next slash.
1110 //
1111 Host++;
1112 //
1113 // And now the host name.
1114 //
1115 Host++;
1116
1117 StringSize = StrLen (Host) + 1;
1118 RequestHeader[HdrHost].FieldValue = AllocatePool (StringSize);
1119 if (!RequestHeader[HdrHost].FieldValue) {
1120 return EFI_OUT_OF_RESOURCES;
1121 }
1122
1123 UnicodeStrToAsciiStrS (
1124 Host,
1125 RequestHeader[HdrHost].FieldValue,
1126 StringSize
1127 );
1128
1129 RequestHeader[HdrConn].FieldValue = "close";
1130 RequestHeader[HdrAgent].FieldValue = USER_AGENT_HDR;
1131 RequestMessage.HeaderCount = HdrMax;
1132
1133 RequestData.Method = HttpMethodGet;
1134 RequestData.Url = DownloadUrl;
1135
1136 RequestMessage.Data.Request = &RequestData;
1137 RequestMessage.Headers = RequestHeader;
1138 RequestMessage.BodyLength = 0;
1139 RequestMessage.Body = NULL;
1140 Context->RequestToken.Event = NULL;
1141
1142 //
1143 // Completion callback event to be set when Request completes.
1144 //
1145 Status = gBS->CreateEvent (
1146 EVT_NOTIFY_SIGNAL,
1147 TPL_CALLBACK,
1148 RequestCallback,
1149 Context,
1150 &Context->RequestToken.Event
1151 );
1152 ASSERT_EFI_ERROR (Status);
1153
1154 Context->RequestToken.Status = EFI_SUCCESS;
1155 Context->RequestToken.Message = &RequestMessage;
1156 gRequestCallbackComplete = FALSE;
1157 Status = Context->Http->Request (Context->Http, &Context->RequestToken);
1158 if (EFI_ERROR (Status)) {
1159 goto Error;
1160 }
1161
1162 Status = WaitForCompletion (Context, &gRequestCallbackComplete);
1163 if (EFI_ERROR (Status)) {
1164 Context->Http->Cancel (Context->Http, &Context->RequestToken);
1165 }
1166
1167 Error:
1168 SHELL_FREE_NON_NULL (RequestHeader[HdrHost].FieldValue);
1169 if (Context->RequestToken.Event) {
1170 gBS->CloseEvent (Context->RequestToken.Event);
1171 ZeroMem (&Context->RequestToken, sizeof (Context->RequestToken));
1172 }
1173
1174 return Status;
1175 }
1176
1177 /**
1178 Update the progress of a file download
1179 This procedure is called each time a new HTTP body portion is received.
1180
1181 @param[in] Context HTTP download context.
1182 @param[in] DownloadLen Portion size, in bytes.
1183 @param[in] Buffer The pointer to the parsed buffer.
1184
1185 @retval EFI_SUCCESS Portion saved.
1186 @retval Other Error saving the portion.
1187 **/
1188 STATIC
1189 EFI_STATUS
1190 EFIAPI
1191 SavePortion (
1192 IN HTTP_DOWNLOAD_CONTEXT *Context,
1193 IN UINTN DownloadLen,
1194 IN CHAR8 *Buffer
1195 )
1196 {
1197 CHAR16 Progress[HTTP_PROGRESS_MESSAGE_SIZE];
1198 UINTN NbOfKb;
1199 UINTN Index;
1200 UINTN LastStep;
1201 UINTN Step;
1202 EFI_STATUS Status;
1203
1204 LastStep = 0;
1205 Step = 0;
1206
1207 ShellSetFilePosition (mFileHandle, Context->LastReportedNbOfBytes);
1208 Status = ShellWriteFile (mFileHandle, &DownloadLen, Buffer);
1209 if (EFI_ERROR (Status)) {
1210 if (Context->ContentDownloaded > 0) {
1211 PRINT_HII (STRING_TOKEN (STR_GEN_CRLF), NULL);
1212 }
1213
1214 PRINT_HII (STRING_TOKEN (STR_HTTP_ERR_WRITE), mLocalFilePath, Status);
1215 return Status;
1216 }
1217
1218 if (Context->ContentDownloaded == 0) {
1219 ShellPrintEx (-1, -1, L"%s 0 Kb", HTTP_PROGR_FRAME);
1220 }
1221
1222 Context->ContentDownloaded += DownloadLen;
1223 NbOfKb = Context->ContentDownloaded >> 10;
1224
1225 Progress[0] = L'\0';
1226 if (Context->ContentLength) {
1227 LastStep = (Context->LastReportedNbOfBytes * HTTP_PROGRESS_SLIDER_STEPS) /
1228 Context->ContentLength;
1229 Step = (Context->ContentDownloaded * HTTP_PROGRESS_SLIDER_STEPS) /
1230 Context->ContentLength;
1231 }
1232
1233 Context->LastReportedNbOfBytes = Context->ContentDownloaded;
1234
1235 if (Step <= LastStep) {
1236 if (!Context->ContentLength) {
1237 //
1238 // Update downloaded size, there is no length info available.
1239 //
1240 ShellPrintEx (-1, -1, L"%s", HTTP_KB);
1241 ShellPrintEx (-1, -1, L"%7d Kb", NbOfKb);
1242 }
1243
1244 return EFI_SUCCESS;
1245 }
1246
1247 ShellPrintEx (-1, -1, L"%s", HTTP_PROGRESS_DEL);
1248
1249 Status = StrCpyS (Progress, HTTP_PROGRESS_MESSAGE_SIZE, HTTP_PROGR_FRAME);
1250 if (EFI_ERROR (Status)) {
1251 return Status;
1252 }
1253
1254 for (Index = 1; Index < Step; Index++) {
1255 Progress[Index] = L'=';
1256 }
1257
1258 if (Step) {
1259 Progress[Step] = L'>';
1260 }
1261
1262 UnicodeSPrint (
1263 Progress + (sizeof (HTTP_PROGR_FRAME) / sizeof (CHAR16)) - 1,
1264 sizeof (Progress) - sizeof (HTTP_PROGR_FRAME),
1265 L" %7d Kb",
1266 NbOfKb
1267 );
1268
1269 ShellPrintEx (-1, -1, L"%s", Progress);
1270
1271 return EFI_SUCCESS;
1272 }
1273
1274 /**
1275 Replace the original Host and Uri with Host and Uri returned by the
1276 HTTP server in 'Location' header (redirection).
1277
1278 @param[in] Location A pointer to the 'Location' string
1279 provided by HTTP server.
1280 @param[in] Context A pointer to HTTP download context.
1281 @param[in] DownloadUrl Fully qualified HTTP URL.
1282
1283 @retval EFI_SUCCESS Host and Uri were successfully set.
1284 @retval EFI_OUT_OF_RESOURCES Error setting Host or Uri.
1285 **/
1286 STATIC
1287 EFI_STATUS
1288 SetHostURI (
1289 IN CHAR8 *Location,
1290 IN HTTP_DOWNLOAD_CONTEXT *Context,
1291 IN CHAR16 *DownloadUrl
1292 )
1293 {
1294 EFI_STATUS Status;
1295 UINTN StringSize;
1296 UINTN FirstStep;
1297 UINTN Idx;
1298 UINTN Step;
1299 CHAR8 *Walker;
1300 CHAR16 *Temp;
1301 CHAR8 *Tmp;
1302 CHAR16 *Url;
1303 BOOLEAN IsAbEmptyUrl;
1304
1305 Tmp = NULL;
1306 Url = NULL;
1307 IsAbEmptyUrl = FALSE;
1308 FirstStep = 0;
1309
1310 StringSize = (AsciiStrSize (Location) * sizeof (CHAR16));
1311 Url = AllocateZeroPool (StringSize);
1312 if (!Url) {
1313 return EFI_OUT_OF_RESOURCES;
1314 }
1315
1316 Status = AsciiStrToUnicodeStrS (
1317 (CONST CHAR8 *)Location,
1318 Url,
1319 StringSize
1320 );
1321
1322 if (EFI_ERROR (Status)) {
1323 goto Error;
1324 }
1325
1326 //
1327 // If an HTTP server redirects to the same location more than once,
1328 // then stop attempts and tell it is not reachable.
1329 //
1330 if (!StrCmp (Url, DownloadUrl)) {
1331 Status = EFI_NO_MAPPING;
1332 goto Error;
1333 }
1334
1335 if (AsciiStrLen (Location) > 2) {
1336 //
1337 // Some servers return 'Location: //server/resource'
1338 //
1339 IsAbEmptyUrl = (Location[0] == '/') && (Location[1] == '/');
1340 if (IsAbEmptyUrl) {
1341 //
1342 // Skip first "//"
1343 //
1344 Location += 2;
1345 FirstStep = 1;
1346 }
1347 }
1348
1349 if (AsciiStrStr (Location, "://") || IsAbEmptyUrl) {
1350 Idx = 0;
1351 Walker = Location;
1352
1353 for (Step = FirstStep; Step < 2; Step++) {
1354 for ( ; *Walker != '/' && *Walker != '\0'; Walker++) {
1355 Idx++;
1356 }
1357
1358 if (!Step) {
1359 //
1360 // Skip "//"
1361 //
1362 Idx += 2;
1363 Walker += 2;
1364 }
1365 }
1366
1367 Tmp = AllocateZeroPool (Idx + 1);
1368 if (!Tmp) {
1369 Status = EFI_OUT_OF_RESOURCES;
1370 goto Error;
1371 }
1372
1373 CopyMem (Tmp, Location, Idx);
1374
1375 //
1376 // Location now points to Uri
1377 //
1378 Location += Idx;
1379 StringSize = (Idx + 1) * sizeof (CHAR16);
1380
1381 SHELL_FREE_NON_NULL (Context->ServerAddrAndProto);
1382
1383 Temp = AllocateZeroPool (StringSize);
1384 if (!Temp) {
1385 Status = EFI_OUT_OF_RESOURCES;
1386 goto Error;
1387 }
1388
1389 Status = AsciiStrToUnicodeStrS (
1390 (CONST CHAR8 *)Tmp,
1391 Temp,
1392 StringSize
1393 );
1394 if (EFI_ERROR (Status)) {
1395 SHELL_FREE_NON_NULL (Temp);
1396 goto Error;
1397 }
1398
1399 Idx = 0;
1400 if (IsAbEmptyUrl) {
1401 Context->ServerAddrAndProto = StrnCatGrow (
1402 &Context->ServerAddrAndProto,
1403 &Idx,
1404 L"http://",
1405 StrLen (L"http://")
1406 );
1407 }
1408
1409 Context->ServerAddrAndProto = StrnCatGrow (
1410 &Context->ServerAddrAndProto,
1411 &Idx,
1412 Temp,
1413 StrLen (Temp)
1414 );
1415 SHELL_FREE_NON_NULL (Temp);
1416 if (!Context->ServerAddrAndProto) {
1417 Status = EFI_OUT_OF_RESOURCES;
1418 goto Error;
1419 }
1420 }
1421
1422 SHELL_FREE_NON_NULL (Context->Uri);
1423
1424 StringSize = AsciiStrSize (Location) * sizeof (CHAR16);
1425 Context->Uri = AllocateZeroPool (StringSize);
1426 if (!Context->Uri) {
1427 Status = EFI_OUT_OF_RESOURCES;
1428 goto Error;
1429 }
1430
1431 //
1432 // Now make changes to the Uri part.
1433 //
1434 Status = AsciiStrToUnicodeStrS (
1435 (CONST CHAR8 *)Location,
1436 Context->Uri,
1437 StringSize
1438 );
1439 Error:
1440 SHELL_FREE_NON_NULL (Tmp);
1441 SHELL_FREE_NON_NULL (Url);
1442
1443 return Status;
1444 }
1445
1446 /**
1447 Message parser callback.
1448 Save a portion of HTTP body.
1449
1450 @param[in] EventType Type of event. Can be either
1451 OnComplete or OnData.
1452 @param[in] Data A pointer to the buffer with data.
1453 @param[in] Length Data length of this portion.
1454 @param[in] Context A pointer to the HTTP download context.
1455
1456 @retval EFI_SUCCESS The portion was processed successfully.
1457 @retval Other Error returned by SavePortion.
1458 **/
1459 STATIC
1460 EFI_STATUS
1461 EFIAPI
1462 ParseMsg (
1463 IN HTTP_BODY_PARSE_EVENT EventType,
1464 IN CHAR8 *Data,
1465 IN UINTN Length,
1466 IN VOID *Context
1467 )
1468 {
1469 if ( (Data == NULL)
1470 || (EventType == BodyParseEventOnComplete)
1471 || (Context == NULL))
1472 {
1473 return EFI_SUCCESS;
1474 }
1475
1476 return SavePortion (Context, Length, Data);
1477 }
1478
1479 /**
1480 Get HTTP server response and collect the whole body as a file.
1481 Set appropriate status in Context (REQ_OK, REQ_REPEAT, REQ_ERROR).
1482 Note that even if HTTP server returns an error code, it might send
1483 the body as well. This body will be collected in the resultant file.
1484
1485 @param[in] Context A pointer to the HTTP download context.
1486 @param[in] DownloadUrl A pointer to the fully qualified URL to download.
1487
1488 @retval EFI_SUCCESS Valid file. Body successfully collected.
1489 @retval EFI_HTTP_ERROR Response is a valid HTTP response, but the
1490 HTTP server
1491 indicated an error (HTTP code >= 400).
1492 Response body MAY contain full
1493 HTTP server response.
1494 @retval Others Error getting the reponse from the HTTP server.
1495 Response body is not collected.
1496 **/
1497 STATIC
1498 EFI_STATUS
1499 GetResponse (
1500 IN HTTP_DOWNLOAD_CONTEXT *Context,
1501 IN CHAR16 *DownloadUrl
1502 )
1503 {
1504 EFI_HTTP_RESPONSE_DATA ResponseData;
1505 EFI_HTTP_MESSAGE ResponseMessage;
1506 EFI_HTTP_HEADER *Header;
1507 EFI_STATUS Status;
1508 VOID *MsgParser;
1509 EFI_TIME StartTime;
1510 EFI_TIME EndTime;
1511 CONST CHAR16 *Desc;
1512 UINTN ElapsedSeconds;
1513 BOOLEAN IsTrunked;
1514 BOOLEAN CanMeasureTime;
1515
1516 ZeroMem (&ResponseData, sizeof (ResponseData));
1517 ZeroMem (&ResponseMessage, sizeof (ResponseMessage));
1518 ZeroMem (&Context->ResponseToken, sizeof (Context->ResponseToken));
1519 IsTrunked = FALSE;
1520
1521 ResponseMessage.Body = Context->Buffer;
1522 Context->ResponseToken.Status = EFI_SUCCESS;
1523 Context->ResponseToken.Message = &ResponseMessage;
1524 Context->ContentLength = 0;
1525 Context->Status = REQ_OK;
1526 Status = EFI_SUCCESS;
1527 MsgParser = NULL;
1528 ResponseData.StatusCode = HTTP_STATUS_UNSUPPORTED_STATUS;
1529 ResponseMessage.Data.Response = &ResponseData;
1530 Context->ResponseToken.Event = NULL;
1531 CanMeasureTime = FALSE;
1532 if (Context->Flags & DL_FLAG_TIME) {
1533 ZeroMem (&StartTime, sizeof (StartTime));
1534 CanMeasureTime = !EFI_ERROR (gRT->GetTime (&StartTime, NULL));
1535 }
1536
1537 do {
1538 SHELL_FREE_NON_NULL (ResponseMessage.Headers);
1539 ResponseMessage.HeaderCount = 0;
1540 gResponseCallbackComplete = FALSE;
1541 ResponseMessage.BodyLength = Context->BufferSize;
1542
1543 if (ShellGetExecutionBreakFlag ()) {
1544 Status = EFI_ABORTED;
1545 break;
1546 }
1547
1548 if (!Context->ContentDownloaded && !Context->ResponseToken.Event) {
1549 Status = gBS->CreateEvent (
1550 EVT_NOTIFY_SIGNAL,
1551 TPL_CALLBACK,
1552 ResponseCallback,
1553 Context,
1554 &Context->ResponseToken.Event
1555 );
1556 ASSERT_EFI_ERROR (Status);
1557 } else {
1558 ResponseMessage.Data.Response = NULL;
1559 }
1560
1561 if (EFI_ERROR (Status)) {
1562 break;
1563 }
1564
1565 Status = Context->Http->Response (Context->Http, &Context->ResponseToken);
1566 if (EFI_ERROR (Status)) {
1567 break;
1568 }
1569
1570 Status = WaitForCompletion (Context, &gResponseCallbackComplete);
1571 if (EFI_ERROR (Status) && ResponseMessage.HeaderCount) {
1572 Status = EFI_SUCCESS;
1573 }
1574
1575 if (EFI_ERROR (Status)) {
1576 Context->Http->Cancel (Context->Http, &Context->ResponseToken);
1577 break;
1578 }
1579
1580 if (!Context->ContentDownloaded) {
1581 if (NEED_REDIRECTION (ResponseData.StatusCode)) {
1582 //
1583 // Need to repeat the request with new Location (server redirected).
1584 //
1585 Context->Status = REQ_NEED_REPEAT;
1586
1587 Header = HttpFindHeader (
1588 ResponseMessage.HeaderCount,
1589 ResponseMessage.Headers,
1590 "Location"
1591 );
1592 if (Header) {
1593 Status = SetHostURI (Header->FieldValue, Context, DownloadUrl);
1594 if (Status == EFI_NO_MAPPING) {
1595 PRINT_HII (
1596 STRING_TOKEN (STR_HTTP_ERR_STATUSCODE),
1597 Context->ServerAddrAndProto,
1598 L"Recursive HTTP server relocation",
1599 Context->Uri
1600 );
1601 }
1602 } else {
1603 //
1604 // Bad reply from the server. Server must specify the location.
1605 // Indicate that resource was not found, and no body collected.
1606 //
1607 Status = EFI_NOT_FOUND;
1608 }
1609
1610 Context->Http->Cancel (Context->Http, &Context->ResponseToken);
1611 break;
1612 }
1613
1614 //
1615 // Init message-body parser by header information.
1616 //
1617 if (!MsgParser) {
1618 Status = HttpInitMsgParser (
1619 ResponseMessage.Data.Request->Method,
1620 ResponseData.StatusCode,
1621 ResponseMessage.HeaderCount,
1622 ResponseMessage.Headers,
1623 ParseMsg,
1624 Context,
1625 &MsgParser
1626 );
1627 if (EFI_ERROR (Status)) {
1628 break;
1629 }
1630 }
1631
1632 //
1633 // If it is a trunked message, rely on the parser.
1634 //
1635 Header = HttpFindHeader (
1636 ResponseMessage.HeaderCount,
1637 ResponseMessage.Headers,
1638 "Transfer-Encoding"
1639 );
1640 IsTrunked = (Header && !AsciiStrCmp (Header->FieldValue, "chunked"));
1641
1642 HttpGetEntityLength (MsgParser, &Context->ContentLength);
1643
1644 if ( (ResponseData.StatusCode >= HTTP_STATUS_400_BAD_REQUEST)
1645 && (ResponseData.StatusCode != HTTP_STATUS_308_PERMANENT_REDIRECT))
1646 {
1647 //
1648 // Server reported an error via Response code.
1649 // Collect the body if any.
1650 //
1651 if (!gHttpError) {
1652 gHttpError = TRUE;
1653
1654 Desc = ErrStatusDesc[ResponseData.StatusCode -
1655 HTTP_STATUS_400_BAD_REQUEST];
1656 PRINT_HII (
1657 STRING_TOKEN (STR_HTTP_ERR_STATUSCODE),
1658 Context->ServerAddrAndProto,
1659 Desc,
1660 Context->Uri
1661 );
1662
1663 //
1664 // This gives an RFC HTTP error.
1665 //
1666 Context->Status = ShellStrToUintn (Desc);
1667 Status = ENCODE_ERROR (Context->Status);
1668 }
1669 }
1670 }
1671
1672 //
1673 // Do NOT try to parse an empty body.
1674 //
1675 if (ResponseMessage.BodyLength || IsTrunked) {
1676 Status = HttpParseMessageBody (
1677 MsgParser,
1678 ResponseMessage.BodyLength,
1679 ResponseMessage.Body
1680 );
1681 }
1682 } while ( !HttpIsMessageComplete (MsgParser)
1683 && !EFI_ERROR (Status)
1684 && ResponseMessage.BodyLength);
1685
1686 if ( (Context->Status != REQ_NEED_REPEAT)
1687 && (Status == EFI_SUCCESS)
1688 && CanMeasureTime)
1689 {
1690 if (!EFI_ERROR (gRT->GetTime (&EndTime, NULL))) {
1691 ElapsedSeconds = EfiTimeToEpoch (&EndTime) - EfiTimeToEpoch (&StartTime);
1692 Print (
1693 L",%a%Lus\n",
1694 ElapsedSeconds ? " " : " < ",
1695 ElapsedSeconds > 1 ? (UINT64)ElapsedSeconds : 1
1696 );
1697 }
1698 }
1699
1700 SHELL_FREE_NON_NULL (MsgParser);
1701 if (Context->ResponseToken.Event) {
1702 gBS->CloseEvent (Context->ResponseToken.Event);
1703 ZeroMem (&Context->ResponseToken, sizeof (Context->ResponseToken));
1704 }
1705
1706 return Status;
1707 }
1708
1709 /**
1710 Worker function that downloads the data of a file from an HTTP server given
1711 the path of the file and its size.
1712
1713 @param[in] Context A pointer to the HTTP download context.
1714 @param[in] ControllerHandle The handle of the network interface controller
1715 @param[in] NicName NIC name
1716
1717 @retval EFI_SUCCESS The file was downloaded.
1718 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
1719 #return EFI_HTTP_ERROR The server returned a valid HTTP error.
1720 Examine the mLocalFilePath file
1721 to get error body.
1722 @retval Others The downloading of the file from the server
1723 failed.
1724 **/
1725 STATIC
1726 EFI_STATUS
1727 DownloadFile (
1728 IN HTTP_DOWNLOAD_CONTEXT *Context,
1729 IN EFI_HANDLE ControllerHandle,
1730 IN CHAR16 *NicName
1731 )
1732 {
1733 EFI_STATUS Status;
1734 CHAR16 *DownloadUrl;
1735 UINTN UrlSize;
1736 EFI_HANDLE HttpChildHandle;
1737
1738 ASSERT (Context);
1739 if (Context == NULL) {
1740 return EFI_INVALID_PARAMETER;
1741 }
1742
1743 DownloadUrl = NULL;
1744 HttpChildHandle = NULL;
1745
1746 Context->Buffer = AllocatePool (Context->BufferSize);
1747 if (Context->Buffer == NULL) {
1748 Status = EFI_OUT_OF_RESOURCES;
1749 goto ON_EXIT;
1750 }
1751
1752 //
1753 // Open the file.
1754 //
1755 if (!EFI_ERROR (ShellFileExists (mLocalFilePath))) {
1756 ShellDeleteFileByName (mLocalFilePath);
1757 }
1758
1759 Status = ShellOpenFileByName (
1760 mLocalFilePath,
1761 &mFileHandle,
1762 EFI_FILE_MODE_CREATE |
1763 EFI_FILE_MODE_WRITE |
1764 EFI_FILE_MODE_READ,
1765 0
1766 );
1767 if (EFI_ERROR (Status)) {
1768 PRINT_HII_APP (STRING_TOKEN (STR_GEN_FILE_OPEN_FAIL), mLocalFilePath);
1769 goto ON_EXIT;
1770 }
1771
1772 do {
1773 SHELL_FREE_NON_NULL (DownloadUrl);
1774
1775 CLOSE_HTTP_HANDLE (ControllerHandle, HttpChildHandle);
1776
1777 Status = CreateServiceChildAndOpenProtocol (
1778 ControllerHandle,
1779 &gEfiHttpServiceBindingProtocolGuid,
1780 &gEfiHttpProtocolGuid,
1781 &HttpChildHandle,
1782 (VOID **)&Context->Http
1783 );
1784
1785 if (EFI_ERROR (Status)) {
1786 PRINT_HII (STRING_TOKEN (STR_HTTP_ERR_OPEN_PROTOCOL), NicName, Status);
1787 goto ON_EXIT;
1788 }
1789
1790 Status = Context->Http->Configure (Context->Http, &Context->HttpConfigData);
1791 if (EFI_ERROR (Status)) {
1792 PRINT_HII (STRING_TOKEN (STR_HTTP_ERR_CONFIGURE), NicName, Status);
1793 goto ON_EXIT;
1794 }
1795
1796 UrlSize = 0;
1797 DownloadUrl = StrnCatGrow (
1798 &DownloadUrl,
1799 &UrlSize,
1800 Context->ServerAddrAndProto,
1801 StrLen (Context->ServerAddrAndProto)
1802 );
1803 if (Context->Uri[0] != L'/') {
1804 DownloadUrl = StrnCatGrow (
1805 &DownloadUrl,
1806 &UrlSize,
1807 L"/",
1808 StrLen (Context->ServerAddrAndProto)
1809 );
1810 }
1811
1812 DownloadUrl = StrnCatGrow (
1813 &DownloadUrl,
1814 &UrlSize,
1815 Context->Uri,
1816 StrLen (Context->Uri)
1817 );
1818
1819 PRINT_HII (STRING_TOKEN (STR_HTTP_DOWNLOADING), DownloadUrl);
1820
1821 Status = SendRequest (Context, DownloadUrl);
1822 if (Status) {
1823 goto ON_EXIT;
1824 }
1825
1826 Status = GetResponse (Context, DownloadUrl);
1827
1828 if (Status) {
1829 goto ON_EXIT;
1830 }
1831 } while (Context->Status == REQ_NEED_REPEAT);
1832
1833 if (Context->Status) {
1834 Status = ENCODE_ERROR (Context->Status);
1835 }
1836
1837 ON_EXIT:
1838 //
1839 // Close the file.
1840 //
1841 if (mFileHandle != NULL) {
1842 if (EFI_ERROR (Status) && !(Context->Flags & DL_FLAG_KEEP_BAD)) {
1843 ShellDeleteFile (&mFileHandle);
1844 } else {
1845 ShellCloseFile (&mFileHandle);
1846 }
1847 }
1848
1849 SHELL_FREE_NON_NULL (DownloadUrl);
1850 SHELL_FREE_NON_NULL (Context->Buffer);
1851
1852 CLOSE_HTTP_HANDLE (ControllerHandle, HttpChildHandle);
1853
1854 return Status;
1855 }
1856
1857 /**
1858 Retrive HII package list from ImageHandle and publish to HII database.
1859
1860 @param[in] ImageHandle The image handle of the process.
1861
1862 @retval HII handle.
1863 **/
1864 EFI_HII_HANDLE
1865 InitializeHiiPackage (
1866 IN EFI_HANDLE ImageHandle
1867 )
1868 {
1869 EFI_STATUS Status;
1870 EFI_HII_PACKAGE_LIST_HEADER *PackageList;
1871 EFI_HII_HANDLE HiiHandle;
1872
1873 //
1874 // Retrieve HII package list from ImageHandle.
1875 //
1876 Status = gBS->OpenProtocol (
1877 ImageHandle,
1878 &gEfiHiiPackageListProtocolGuid,
1879 (VOID **)&PackageList,
1880 ImageHandle,
1881 NULL,
1882 EFI_OPEN_PROTOCOL_GET_PROTOCOL
1883 );
1884 ASSERT_EFI_ERROR (Status);
1885 if (EFI_ERROR (Status)) {
1886 return NULL;
1887 }
1888
1889 //
1890 // Publish HII package list to HII Database.
1891 //
1892 Status = gHiiDatabase->NewPackageList (
1893 gHiiDatabase,
1894 PackageList,
1895 NULL,
1896 &HiiHandle
1897 );
1898 ASSERT_EFI_ERROR (Status);
1899 if (EFI_ERROR (Status)) {
1900 return NULL;
1901 }
1902
1903 return HiiHandle;
1904 }