TianoCore EDK2 master
Loading...
Searching...
No Matches
Tftp.c
Go to the documentation of this file.
1
11#include "Tftp.h"
12
13#define IP4_CONFIG2_INTERFACE_INFO_NAME_LENGTH 32
14EFI_HII_HANDLE mTftpHiiHandle;
15
16/*
17 Constant strings and definitions related to the message indicating the amount of
18 progress in the downloading of a TFTP file.
19*/
20
21// Frame for the progression slider
22STATIC CONST CHAR16 mTftpProgressFrame[] = L"[ ]";
23
24// Number of steps in the progression slider
25#define TFTP_PROGRESS_SLIDER_STEPS ((sizeof (mTftpProgressFrame) / sizeof (CHAR16)) - 3)
26
27// Size in number of characters plus one (final zero) of the message to
28// indicate the progress of a TFTP download. The format is "[(progress slider:
29// 40 characters)] (nb of KBytes downloaded so far: 7 characters) Kb". There
30// are thus the number of characters in mTftpProgressFrame[] plus 11 characters
31// (2 // spaces, "Kb" and seven characters for the number of KBytes).
32#define TFTP_PROGRESS_MESSAGE_SIZE ((sizeof (mTftpProgressFrame) / sizeof (CHAR16)) + 12)
33
34// String to delete the TFTP progress message to be able to update it :
35// (TFTP_PROGRESS_MESSAGE_SIZE-1) '\b'
36STATIC CONST CHAR16 mTftpProgressDelete[] = 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\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b";
37
38// Local File Handle
39SHELL_FILE_HANDLE mFileHandle;
40
41// Path of the local file, Unicode encoded
42CONST CHAR16 *mLocalFilePath;
43
54BOOLEAN
56 IN CONST CHAR16 *ValueStr,
57 OUT UINT16 *Value
58 );
59
81 IN EFI_HANDLE ControllerHandle,
82 IN UINTN NicNumber,
83 OUT CHAR16 *NicName
84 );
85
105STATIC
108 IN EFI_HANDLE ControllerHandle,
109 IN EFI_GUID *ServiceBindingProtocolGuid,
110 IN EFI_GUID *ProtocolGuid,
111 OUT EFI_HANDLE *ChildHandle,
112 OUT VOID **Interface
113 );
114
127STATIC
128VOID
130 IN EFI_HANDLE ControllerHandle,
131 IN EFI_GUID *ServiceBindingProtocolGuid,
132 IN EFI_GUID *ProtocolGuid,
133 IN EFI_HANDLE ChildHandle
134 );
135
151STATIC
154 IN EFI_MTFTP4_PROTOCOL *Mtftp4,
155 IN CONST CHAR8 *FilePath,
156 OUT UINTN *FileSize
157 );
158
176STATIC
179 IN EFI_MTFTP4_PROTOCOL *Mtftp4,
180 IN CONST CHAR16 *FilePath,
181 IN CONST CHAR8 *AsciiFilePath,
182 IN UINTN FileSize,
183 IN UINT16 BlockSize,
184 IN UINT16 WindowSize
185 );
186
199STATIC
201EFIAPI
204 IN EFI_MTFTP4_TOKEN *Token,
205 IN UINT16 PacketLen,
206 IN EFI_MTFTP4_PACKET *Packet
207 );
208
209EFI_MTFTP4_CONFIG_DATA DefaultMtftp4ConfigData = {
210 TRUE, // Use default setting
211 {
212 { 0, 0, 0, 0 }
213 }, // StationIp - Not relevant as UseDefaultSetting=TRUE
214 {
215 { 0, 0, 0, 0 }
216 }, // SubnetMask - Not relevant as UseDefaultSetting=TRUE
217 0, // LocalPort - Automatically assigned port number.
218 {
219 { 0, 0, 0, 0 }
220 }, // GatewayIp - Not relevant as UseDefaultSetting=TRUE
221 {
222 { 0, 0, 0, 0 }
223 }, // ServerIp - Not known yet
224 69, // InitialServerPort - Standard TFTP server port
225 6, // TryCount - The number of times to transmit request packets and wait for a response.
226 4 // TimeoutValue - Retransmission timeout in seconds.
227};
228
229STATIC CONST SHELL_PARAM_ITEM ParamList[] = {
230 { L"-i", TypeValue },
231 { L"-l", TypeValue },
232 { L"-r", TypeValue },
233 { L"-c", TypeValue },
234 { L"-t", TypeValue },
235 { L"-s", TypeValue },
236 { L"-w", TypeValue },
237 { NULL, TypeMax }
238};
239
243#define MTFTP_DEFAULT_BLKSIZE 512
247#define MTFTP_MIN_BLKSIZE 8
248#define MTFTP_MAX_BLKSIZE 65464
252#define MTFTP_DEFAULT_WINDOWSIZE 1
258#define MTFTP_MIN_WINDOWSIZE 1
259#define MTFTP_MAX_WINDOWSIZE 64
260
278 IN EFI_HANDLE ImageHandle,
279 IN EFI_SYSTEM_TABLE *SystemTable
280 )
281{
282 SHELL_STATUS ShellStatus;
283 EFI_STATUS Status;
284 LIST_ENTRY *CheckPackage;
285 CHAR16 *ProblemParam;
286 UINTN ParamCount;
287 CONST CHAR16 *UserNicName;
288 BOOLEAN NicFound;
289 CONST CHAR16 *ValueStr;
290 CONST CHAR16 *RemoteFilePath;
291 CHAR8 *AsciiRemoteFilePath;
292 UINTN FilePathSize;
293 CONST CHAR16 *Walker;
294 EFI_MTFTP4_CONFIG_DATA Mtftp4ConfigData;
295 EFI_HANDLE *Handles;
296 UINTN HandleCount;
297 UINTN NicNumber;
298 CHAR16 NicName[IP4_CONFIG2_INTERFACE_INFO_NAME_LENGTH];
299 EFI_HANDLE ControllerHandle;
300 EFI_HANDLE Mtftp4ChildHandle;
301 EFI_MTFTP4_PROTOCOL *Mtftp4;
302 UINTN FileSize;
303 UINT16 BlockSize;
304 UINT16 WindowSize;
305
306 ShellStatus = SHELL_INVALID_PARAMETER;
307 ProblemParam = NULL;
308 NicFound = FALSE;
309 AsciiRemoteFilePath = NULL;
310 Handles = NULL;
311 FileSize = 0;
312 BlockSize = MTFTP_DEFAULT_BLKSIZE;
313 WindowSize = MTFTP_DEFAULT_WINDOWSIZE;
314
315 //
316 // Initialize the Shell library (we must be in non-auto-init...)
317 //
318 Status = ShellInitialize ();
319 if (EFI_ERROR (Status)) {
320 ASSERT_EFI_ERROR (Status);
321 return SHELL_ABORTED;
322 }
323
324 //
325 // Parse the command line.
326 //
327 Status = ShellCommandLineParse (ParamList, &CheckPackage, &ProblemParam, TRUE);
328 if (EFI_ERROR (Status)) {
329 if ((Status == EFI_VOLUME_CORRUPTED) &&
330 (ProblemParam != NULL))
331 {
333 -1,
334 -1,
335 NULL,
336 STRING_TOKEN (STR_GEN_PROBLEM),
337 mTftpHiiHandle,
338 L"tftp",
339 ProblemParam
340 );
341 FreePool (ProblemParam);
342 } else {
343 ASSERT (FALSE);
344 }
345
346 goto Error;
347 }
348
349 //
350 // Check the number of parameters
351 //
352 ParamCount = ShellCommandLineGetCount (CheckPackage);
353 if (ParamCount > 4) {
355 -1,
356 -1,
357 NULL,
358 STRING_TOKEN (STR_GEN_TOO_MANY),
359 mTftpHiiHandle,
360 L"tftp"
361 );
362 goto Error;
363 }
364
365 if (ParamCount < 3) {
367 -1,
368 -1,
369 NULL,
370 STRING_TOKEN (STR_GEN_TOO_FEW),
371 mTftpHiiHandle,
372 L"tftp"
373 );
374 goto Error;
375 }
376
377 CopyMem (&Mtftp4ConfigData, &DefaultMtftp4ConfigData, sizeof (EFI_MTFTP4_CONFIG_DATA));
378
379 //
380 // Check the host IPv4 address
381 //
382 ValueStr = ShellCommandLineGetRawValue (CheckPackage, 1);
383 Status = NetLibStrToIp4 (ValueStr, &Mtftp4ConfigData.ServerIp);
384 if (EFI_ERROR (Status)) {
386 -1,
387 -1,
388 NULL,
389 STRING_TOKEN (STR_GEN_PARAM_INV),
390 mTftpHiiHandle,
391 L"tftp",
392 ValueStr
393 );
394 goto Error;
395 }
396
397 RemoteFilePath = ShellCommandLineGetRawValue (CheckPackage, 2);
398 if (RemoteFilePath == NULL) {
399 ASSERT (RemoteFilePath != NULL);
400 goto Error;
401 }
402
403 FilePathSize = StrLen (RemoteFilePath) + 1;
404 AsciiRemoteFilePath = AllocatePool (FilePathSize);
405 if (AsciiRemoteFilePath == NULL) {
406 ShellStatus = SHELL_OUT_OF_RESOURCES;
407 goto Error;
408 }
409
410 UnicodeStrToAsciiStrS (RemoteFilePath, AsciiRemoteFilePath, FilePathSize);
411
412 if (ParamCount == 4) {
413 mLocalFilePath = ShellCommandLineGetRawValue (CheckPackage, 3);
414 } else {
415 Walker = RemoteFilePath + StrLen (RemoteFilePath);
416 while ((--Walker) >= RemoteFilePath) {
417 if ((*Walker == L'\\') ||
418 (*Walker == L'/'))
419 {
420 break;
421 }
422 }
423
424 mLocalFilePath = Walker + 1;
425 }
426
427 //
428 // Get the name of the Network Interface Card to be used if any.
429 //
430 UserNicName = ShellCommandLineGetValue (CheckPackage, L"-i");
431
432 ValueStr = ShellCommandLineGetValue (CheckPackage, L"-l");
433 if (ValueStr != NULL) {
434 if (!StringToUint16 (ValueStr, &Mtftp4ConfigData.LocalPort)) {
435 goto Error;
436 }
437 }
438
439 ValueStr = ShellCommandLineGetValue (CheckPackage, L"-r");
440 if (ValueStr != NULL) {
441 if (!StringToUint16 (ValueStr, &Mtftp4ConfigData.InitialServerPort)) {
442 goto Error;
443 }
444 }
445
446 ValueStr = ShellCommandLineGetValue (CheckPackage, L"-c");
447 if (ValueStr != NULL) {
448 if (!StringToUint16 (ValueStr, &Mtftp4ConfigData.TryCount)) {
449 goto Error;
450 }
451
452 if (Mtftp4ConfigData.TryCount == 0) {
453 Mtftp4ConfigData.TryCount = 6;
454 }
455 }
456
457 ValueStr = ShellCommandLineGetValue (CheckPackage, L"-t");
458 if (ValueStr != NULL) {
459 if (!StringToUint16 (ValueStr, &Mtftp4ConfigData.TimeoutValue)) {
460 goto Error;
461 }
462
463 if (Mtftp4ConfigData.TimeoutValue == 0) {
465 -1,
466 -1,
467 NULL,
468 STRING_TOKEN (STR_GEN_PARAM_INV),
469 mTftpHiiHandle,
470 L"tftp",
471 ValueStr
472 );
473 goto Error;
474 }
475 }
476
477 ValueStr = ShellCommandLineGetValue (CheckPackage, L"-s");
478 if (ValueStr != NULL) {
479 if (!StringToUint16 (ValueStr, &BlockSize)) {
480 goto Error;
481 }
482
483 if ((BlockSize < MTFTP_MIN_BLKSIZE) || (BlockSize > MTFTP_MAX_BLKSIZE)) {
485 -1,
486 -1,
487 NULL,
488 STRING_TOKEN (STR_GEN_PARAM_INV),
489 mTftpHiiHandle,
490 L"tftp",
491 ValueStr
492 );
493 goto Error;
494 }
495 }
496
497 ValueStr = ShellCommandLineGetValue (CheckPackage, L"-w");
498 if (ValueStr != NULL) {
499 if (!StringToUint16 (ValueStr, &WindowSize)) {
500 goto Error;
501 }
502
503 if ((WindowSize < MTFTP_MIN_WINDOWSIZE) || (WindowSize > MTFTP_MAX_WINDOWSIZE)) {
505 -1,
506 -1,
507 NULL,
508 STRING_TOKEN (STR_GEN_PARAM_INV),
509 mTftpHiiHandle,
510 L"tftp",
511 ValueStr
512 );
513 goto Error;
514 }
515 }
516
517 //
518 // Locate all MTFTP4 Service Binding protocols
519 //
520 ShellStatus = SHELL_NOT_FOUND;
521 Status = gBS->LocateHandleBuffer (
523 &gEfiManagedNetworkServiceBindingProtocolGuid,
524 NULL,
525 &HandleCount,
526 &Handles
527 );
528 if (EFI_ERROR (Status) || (HandleCount == 0)) {
530 -1,
531 -1,
532 NULL,
533 STRING_TOKEN (STR_TFTP_ERR_NO_NIC),
534 mTftpHiiHandle
535 );
536 goto Error;
537 }
538
539 for (NicNumber = 0;
540 (NicNumber < HandleCount) && (ShellStatus != SHELL_SUCCESS);
541 NicNumber++)
542 {
543 ControllerHandle = Handles[NicNumber];
544
545 Status = GetNicName (ControllerHandle, NicNumber, NicName);
546 if (EFI_ERROR (Status)) {
548 -1,
549 -1,
550 NULL,
551 STRING_TOKEN (STR_TFTP_ERR_NIC_NAME),
552 mTftpHiiHandle,
553 NicNumber,
554 Status
555 );
556 continue;
557 }
558
559 if (UserNicName != NULL) {
560 if (StrCmp (NicName, UserNicName) != 0) {
561 continue;
562 }
563
564 NicFound = TRUE;
565 }
566
568 ControllerHandle,
569 &gEfiMtftp4ServiceBindingProtocolGuid,
570 &gEfiMtftp4ProtocolGuid,
571 &Mtftp4ChildHandle,
572 (VOID **)&Mtftp4
573 );
574 if (EFI_ERROR (Status)) {
576 -1,
577 -1,
578 NULL,
579 STRING_TOKEN (STR_TFTP_ERR_OPEN_PROTOCOL),
580 mTftpHiiHandle,
581 NicName,
582 Status
583 );
584 continue;
585 }
586
587 Status = Mtftp4->Configure (Mtftp4, &Mtftp4ConfigData);
588 if (EFI_ERROR (Status)) {
590 -1,
591 -1,
592 NULL,
593 STRING_TOKEN (STR_TFTP_ERR_CONFIGURE),
594 mTftpHiiHandle,
595 NicName,
596 Status
597 );
598 goto NextHandle;
599 }
600
601 Status = GetFileSize (Mtftp4, AsciiRemoteFilePath, &FileSize);
602 if (EFI_ERROR (Status)) {
604 -1,
605 -1,
606 NULL,
607 STRING_TOKEN (STR_TFTP_ERR_FILE_SIZE),
608 mTftpHiiHandle,
609 RemoteFilePath,
610 NicName,
611 Status
612 );
613 goto NextHandle;
614 }
615
616 Status = DownloadFile (Mtftp4, RemoteFilePath, AsciiRemoteFilePath, FileSize, BlockSize, WindowSize);
617 if (EFI_ERROR (Status)) {
619 -1,
620 -1,
621 NULL,
622 STRING_TOKEN (STR_TFTP_ERR_DOWNLOAD),
623 mTftpHiiHandle,
624 RemoteFilePath,
625 NicName,
626 Status
627 );
628 goto NextHandle;
629 }
630
631 ShellStatus = SHELL_SUCCESS;
632
633NextHandle:
634
636 ControllerHandle,
637 &gEfiMtftp4ServiceBindingProtocolGuid,
638 &gEfiMtftp4ProtocolGuid,
639 Mtftp4ChildHandle
640 );
641 }
642
643 if ((UserNicName != NULL) && (!NicFound)) {
645 -1,
646 -1,
647 NULL,
648 STRING_TOKEN (STR_TFTP_ERR_NIC_NOT_FOUND),
649 mTftpHiiHandle,
650 UserNicName
651 );
652 }
653
654Error:
655
656 ShellCommandLineFreeVarList (CheckPackage);
657 if (AsciiRemoteFilePath != NULL) {
658 FreePool (AsciiRemoteFilePath);
659 }
660
661 if (Handles != NULL) {
662 FreePool (Handles);
663 }
664
665 if ((ShellStatus != SHELL_SUCCESS) && (EFI_ERROR (Status))) {
666 ShellStatus = Status & ~MAX_BIT;
667 }
668
669 return ShellStatus;
670}
671
681STATIC
682BOOLEAN
684 IN CONST CHAR16 *ValueStr,
685 OUT UINT16 *Value
686 )
687{
688 UINTN Val;
689
690 Val = ShellStrToUintn (ValueStr);
691 if (Val > MAX_UINT16) {
693 -1,
694 -1,
695 NULL,
696 STRING_TOKEN (STR_GEN_PARAM_INV),
697 mTftpHiiHandle,
698 L"tftp",
699 ValueStr
700 );
701 return FALSE;
702 }
703
704 *Value = (UINT16)Val;
705 return TRUE;
706}
707
726STATIC
729 IN EFI_HANDLE ControllerHandle,
730 IN UINTN NicNumber,
731 OUT CHAR16 *NicName
732 )
733{
734 EFI_STATUS Status;
735 EFI_HANDLE MnpHandle;
738
740 ControllerHandle,
741 &gEfiManagedNetworkServiceBindingProtocolGuid,
742 &gEfiManagedNetworkProtocolGuid,
743 &MnpHandle,
744 (VOID **)&Mnp
745 );
746 if (EFI_ERROR (Status)) {
747 goto Error;
748 }
749
750 Status = Mnp->GetModeData (Mnp, NULL, &SnpMode);
751 if (EFI_ERROR (Status) && (Status != EFI_NOT_STARTED)) {
752 goto Error;
753 }
754
756 NicName,
757 IP4_CONFIG2_INTERFACE_INFO_NAME_LENGTH,
758 SnpMode.IfType == NET_IFTYPE_ETHERNET ?
759 L"eth%d" :
760 L"unk%d",
761 NicNumber
762 );
763
764 Status = EFI_SUCCESS;
765
766Error:
767
768 if (MnpHandle != NULL) {
770 ControllerHandle,
771 &gEfiManagedNetworkServiceBindingProtocolGuid,
772 &gEfiManagedNetworkProtocolGuid,
773 MnpHandle
774 );
775 }
776
777 return Status;
778}
779
799STATIC
802 IN EFI_HANDLE ControllerHandle,
803 IN EFI_GUID *ServiceBindingProtocolGuid,
804 IN EFI_GUID *ProtocolGuid,
805 OUT EFI_HANDLE *ChildHandle,
806 OUT VOID **Interface
807 )
808{
809 EFI_STATUS Status;
810
811 *ChildHandle = NULL;
812 Status = NetLibCreateServiceChild (
813 ControllerHandle,
815 ServiceBindingProtocolGuid,
816 ChildHandle
817 );
818 if (!EFI_ERROR (Status)) {
819 Status = gBS->OpenProtocol (
820 *ChildHandle,
821 ProtocolGuid,
822 Interface,
824 ControllerHandle,
825 EFI_OPEN_PROTOCOL_GET_PROTOCOL
826 );
827 if (EFI_ERROR (Status)) {
829 ControllerHandle,
831 ServiceBindingProtocolGuid,
832 *ChildHandle
833 );
834 *ChildHandle = NULL;
835 }
836 }
837
838 return Status;
839}
840
853STATIC
854VOID
856 IN EFI_HANDLE ControllerHandle,
857 IN EFI_GUID *ServiceBindingProtocolGuid,
858 IN EFI_GUID *ProtocolGuid,
859 IN EFI_HANDLE ChildHandle
860 )
861{
862 gBS->CloseProtocol (
863 ChildHandle,
864 ProtocolGuid,
866 ControllerHandle
867 );
868
870 ControllerHandle,
872 ServiceBindingProtocolGuid,
873 ChildHandle
874 );
875}
876
892STATIC
895 IN EFI_MTFTP4_PROTOCOL *Mtftp4,
896 IN CONST CHAR8 *FilePath,
897 OUT UINTN *FileSize
898 )
899{
900 EFI_STATUS Status;
901 EFI_MTFTP4_OPTION ReqOpt[1];
902 EFI_MTFTP4_PACKET *Packet;
903 UINT32 PktLen;
904 EFI_MTFTP4_OPTION *TableOfOptions;
905 EFI_MTFTP4_OPTION *Option;
906 UINT32 OptCnt;
907 UINT8 OptBuf[128];
908
909 ReqOpt[0].OptionStr = (UINT8 *)"tsize";
910 OptBuf[0] = '0';
911 OptBuf[1] = 0;
912 ReqOpt[0].ValueStr = OptBuf;
913
914 Status = Mtftp4->GetInfo (
915 Mtftp4,
916 NULL,
917 (UINT8 *)FilePath,
918 NULL,
919 1,
920 ReqOpt,
921 &PktLen,
922 &Packet
923 );
924
925 if (EFI_ERROR (Status)) {
926 goto Error;
927 }
928
929 Status = Mtftp4->ParseOptions (
930 Mtftp4,
931 PktLen,
932 Packet,
933 (UINT32 *)&OptCnt,
934 &TableOfOptions
935 );
936 if (EFI_ERROR (Status)) {
937 goto Error;
938 }
939
940 Option = TableOfOptions;
941 while (OptCnt != 0) {
942 if (AsciiStrnCmp ((CHAR8 *)Option->OptionStr, "tsize", 5) == 0) {
943 *FileSize = AsciiStrDecimalToUintn ((CHAR8 *)Option->ValueStr);
944 break;
945 }
946
947 OptCnt--;
948 Option++;
949 }
950
951 FreePool (TableOfOptions);
952
953 if (OptCnt == 0) {
954 Status = EFI_UNSUPPORTED;
955 }
956
957Error:
958
959 return Status;
960}
961
979STATIC
982 IN EFI_MTFTP4_PROTOCOL *Mtftp4,
983 IN CONST CHAR16 *FilePath,
984 IN CONST CHAR8 *AsciiFilePath,
985 IN UINTN FileSize,
986 IN UINT16 BlockSize,
987 IN UINT16 WindowSize
988 )
989{
990 EFI_STATUS Status;
991 DOWNLOAD_CONTEXT *TftpContext;
992 EFI_MTFTP4_TOKEN Mtftp4Token;
993 UINT8 BlksizeBuf[10];
994 UINT8 WindowsizeBuf[10];
995
996 ZeroMem (&Mtftp4Token, sizeof (EFI_MTFTP4_TOKEN));
997
998 TftpContext = AllocatePool (sizeof (DOWNLOAD_CONTEXT));
999 if (TftpContext == NULL) {
1000 Status = EFI_OUT_OF_RESOURCES;
1001 goto Error;
1002 }
1003
1004 TftpContext->FileSize = FileSize;
1005 TftpContext->DownloadedNbOfBytes = 0;
1006 TftpContext->LastReportedNbOfBytes = 0;
1007
1008 Mtftp4Token.Filename = (UINT8 *)AsciiFilePath;
1009 Mtftp4Token.CheckPacket = CheckPacket;
1010 Mtftp4Token.Context = (VOID *)TftpContext;
1011 Mtftp4Token.OptionCount = 0;
1012 Mtftp4Token.OptionList = AllocatePool (sizeof (EFI_MTFTP4_OPTION) * 2);
1013 if (Mtftp4Token.OptionList == NULL) {
1014 Status = EFI_OUT_OF_RESOURCES;
1015 goto Error;
1016 }
1017
1018 if (BlockSize != MTFTP_DEFAULT_BLKSIZE) {
1019 Mtftp4Token.OptionList[Mtftp4Token.OptionCount].OptionStr = (UINT8 *)"blksize";
1020 AsciiSPrint ((CHAR8 *)BlksizeBuf, sizeof (BlksizeBuf), "%d", BlockSize);
1021 Mtftp4Token.OptionList[Mtftp4Token.OptionCount].ValueStr = BlksizeBuf;
1022 Mtftp4Token.OptionCount++;
1023 }
1024
1025 if (WindowSize != MTFTP_DEFAULT_WINDOWSIZE) {
1026 Mtftp4Token.OptionList[Mtftp4Token.OptionCount].OptionStr = (UINT8 *)"windowsize";
1027 AsciiSPrint ((CHAR8 *)WindowsizeBuf, sizeof (WindowsizeBuf), "%d", WindowSize);
1028 Mtftp4Token.OptionList[Mtftp4Token.OptionCount].ValueStr = WindowsizeBuf;
1029 Mtftp4Token.OptionCount++;
1030 }
1031
1033 -1,
1034 -1,
1035 NULL,
1036 STRING_TOKEN (STR_TFTP_DOWNLOADING),
1037 mTftpHiiHandle,
1038 FilePath
1039 );
1040
1041 //
1042 // OPEN FILE
1043 //
1044 if (!EFI_ERROR (ShellFileExists (mLocalFilePath))) {
1045 ShellDeleteFileByName (mLocalFilePath);
1046 }
1047
1048 Status = ShellOpenFileByName (
1049 mLocalFilePath,
1050 &mFileHandle,
1051 EFI_FILE_MODE_CREATE |
1052 EFI_FILE_MODE_WRITE |
1053 EFI_FILE_MODE_READ,
1054 0
1055 );
1056 if (EFI_ERROR (Status)) {
1058 -1,
1059 -1,
1060 NULL,
1061 STRING_TOKEN (STR_GEN_FILE_OPEN_FAIL),
1062 mTftpHiiHandle,
1063 L"tftp",
1064 mLocalFilePath
1065 );
1066 goto Error;
1067 }
1068
1069 Status = Mtftp4->ReadFile (Mtftp4, &Mtftp4Token);
1071 -1,
1072 -1,
1073 NULL,
1074 STRING_TOKEN (STR_GEN_CRLF),
1075 mTftpHiiHandle
1076 );
1077
1078 //
1079 // CLOSE FILE
1080 //
1081 ShellCloseFile (&mFileHandle);
1082
1083Error:
1084 if (TftpContext != NULL) {
1085 FreePool (TftpContext);
1086 }
1087
1088 if (Mtftp4Token.OptionList != NULL) {
1089 FreePool (Mtftp4Token.OptionList);
1090 }
1091
1092 return Status;
1093}
1094
1107STATIC
1109EFIAPI
1111 IN EFI_MTFTP4_PROTOCOL *This,
1112 IN EFI_MTFTP4_TOKEN *Token,
1113 IN UINT16 PacketLen,
1114 IN EFI_MTFTP4_PACKET *Packet
1115 )
1116{
1117 DOWNLOAD_CONTEXT *Context;
1118 CHAR16 Progress[TFTP_PROGRESS_MESSAGE_SIZE];
1119 UINTN NbOfKb;
1120 UINTN Index;
1121 UINTN LastStep;
1122 UINTN Step;
1123 UINTN DownloadLen;
1124 EFI_STATUS Status;
1125
1126 if ((NTOHS (Packet->OpCode)) != EFI_MTFTP4_OPCODE_DATA) {
1127 return EFI_SUCCESS;
1128 }
1129
1130 Context = (DOWNLOAD_CONTEXT *)Token->Context;
1131
1132 //
1133 // The data in the packet are prepended with two UINT16 :
1134 // . OpCode = EFI_MTFTP4_OPCODE_DATA
1135 // . Block = the number of this block of data
1136 //
1137 DownloadLen = (UINTN)PacketLen - sizeof (Packet->OpCode) - sizeof (Packet->Data.Block);
1138
1139 ShellSetFilePosition (mFileHandle, Context->DownloadedNbOfBytes);
1140 Status = ShellWriteFile (mFileHandle, &DownloadLen, Packet->Data.Data);
1141 if (EFI_ERROR (Status)) {
1142 if (Context->DownloadedNbOfBytes > 0) {
1144 -1,
1145 -1,
1146 NULL,
1147 STRING_TOKEN (STR_GEN_CRLF),
1148 mTftpHiiHandle
1149 );
1150 }
1151
1153 -1,
1154 -1,
1155 NULL,
1156 STRING_TOKEN (STR_TFTP_ERR_WRITE),
1157 mTftpHiiHandle,
1158 mLocalFilePath,
1159 Status
1160 );
1161 return Status;
1162 }
1163
1164 if (Context->DownloadedNbOfBytes == 0) {
1165 ShellPrintEx (-1, -1, L"%s 0 Kb", mTftpProgressFrame);
1166 }
1167
1168 Context->DownloadedNbOfBytes += DownloadLen;
1169 NbOfKb = Context->DownloadedNbOfBytes / 1024;
1170
1171 Progress[0] = L'\0';
1172 LastStep = (Context->LastReportedNbOfBytes * TFTP_PROGRESS_SLIDER_STEPS) / Context->FileSize;
1173 Step = (Context->DownloadedNbOfBytes * TFTP_PROGRESS_SLIDER_STEPS) / Context->FileSize;
1174
1175 if (Step <= LastStep) {
1176 return EFI_SUCCESS;
1177 }
1178
1179 ShellPrintEx (-1, -1, L"%s", mTftpProgressDelete);
1180
1181 Status = StrCpyS (Progress, TFTP_PROGRESS_MESSAGE_SIZE, mTftpProgressFrame);
1182 if (EFI_ERROR (Status)) {
1183 return Status;
1184 }
1185
1186 for (Index = 1; Index < Step; Index++) {
1187 Progress[Index] = L'=';
1188 }
1189
1190 Progress[Step] = L'>';
1191
1193 Progress + (sizeof (mTftpProgressFrame) / sizeof (CHAR16)) - 1,
1194 sizeof (Progress) - sizeof (mTftpProgressFrame),
1195 L" %7d Kb",
1196 NbOfKb
1197 );
1198 Context->LastReportedNbOfBytes = Context->DownloadedNbOfBytes;
1199
1200 ShellPrintEx (-1, -1, L"%s", Progress);
1201
1202 return EFI_SUCCESS;
1203}
1204
1214 EFI_HANDLE ImageHandle
1215 )
1216{
1217 EFI_STATUS Status;
1218 EFI_HII_PACKAGE_LIST_HEADER *PackageList;
1219 EFI_HII_HANDLE HiiHandle;
1220
1221 //
1222 // Retrieve HII package list from ImageHandle
1223 //
1224 Status = gBS->OpenProtocol (
1225 ImageHandle,
1226 &gEfiHiiPackageListProtocolGuid,
1227 (VOID **)&PackageList,
1228 ImageHandle,
1229 NULL,
1230 EFI_OPEN_PROTOCOL_GET_PROTOCOL
1231 );
1232 ASSERT_EFI_ERROR (Status);
1233 if (EFI_ERROR (Status)) {
1234 return NULL;
1235 }
1236
1237 //
1238 // Publish HII package list to HII Database.
1239 //
1240 Status = gHiiDatabase->NewPackageList (
1242 PackageList,
1243 NULL,
1244 &HiiHandle
1245 );
1246 ASSERT_EFI_ERROR (Status);
1247 if (EFI_ERROR (Status)) {
1248 return NULL;
1249 }
1250
1251 return HiiHandle;
1252}
UINT64 UINTN
RETURN_STATUS EFIAPI StrCpyS(OUT CHAR16 *Destination, IN UINTN DestMax, IN CONST CHAR16 *Source)
Definition: SafeString.c:226
INTN EFIAPI StrCmp(IN CONST CHAR16 *FirstString, IN CONST CHAR16 *SecondString)
Definition: String.c:109
INTN EFIAPI AsciiStrnCmp(IN CONST CHAR8 *FirstString, IN CONST CHAR8 *SecondString, IN UINTN Length)
Definition: String.c:872
RETURN_STATUS EFIAPI UnicodeStrToAsciiStrS(IN CONST CHAR16 *Source, OUT CHAR8 *Destination, IN UINTN DestMax)
Definition: SafeString.c:2650
UINTN EFIAPI AsciiStrDecimalToUintn(IN CONST CHAR8 *String)
Definition: String.c:1006
UINTN EFIAPI StrLen(IN CONST CHAR16 *String)
Definition: String.c:30
VOID *EFIAPI CopyMem(OUT VOID *DestinationBuffer, IN CONST VOID *SourceBuffer, IN UINTN Length)
VOID *EFIAPI ZeroMem(OUT VOID *Buffer, IN UINTN Length)
VOID EFIAPI FreePool(IN VOID *Buffer)
UINTN EFIAPI UnicodeSPrint(OUT CHAR16 *StartOfBuffer, IN UINTN BufferSize, IN CONST CHAR16 *FormatString,...)
Definition: PrintLib.c:408
UINTN EFIAPI AsciiSPrint(OUT CHAR8 *StartOfBuffer, IN UINTN BufferSize, IN CONST CHAR8 *FormatString,...)
Definition: PrintLib.c:813
#define NULL
Definition: Base.h:319
#define CONST
Definition: Base.h:259
#define STATIC
Definition: Base.h:264
#define TRUE
Definition: Base.h:301
#define FALSE
Definition: Base.h:307
#define IN
Definition: Base.h:279
#define OUT
Definition: Base.h:284
#define ASSERT_EFI_ERROR(StatusParameter)
Definition: DebugLib.h:462
SHELL_STATUS
Definition: Shell.h:21
@ SHELL_OUT_OF_RESOURCES
Definition: Shell.h:73
@ SHELL_ABORTED
Definition: Shell.h:128
@ SHELL_SUCCESS
Definition: Shell.h:25
@ SHELL_NOT_FOUND
Definition: Shell.h:101
@ SHELL_INVALID_PARAMETER
Definition: Shell.h:35
EFI_STATUS EFIAPI NetLibCreateServiceChild(IN EFI_HANDLE Controller, IN EFI_HANDLE Image, IN EFI_GUID *ServiceBindingGuid, IN OUT EFI_HANDLE *ChildHandle)
Definition: DxeNetLib.c:1967
EFI_STATUS EFIAPI NetLibDestroyServiceChild(IN EFI_HANDLE Controller, IN EFI_HANDLE Image, IN EFI_GUID *ServiceBindingGuid, IN EFI_HANDLE ChildHandle)
Definition: DxeNetLib.c:2020
EFI_STATUS EFIAPI NetLibStrToIp4(IN CONST CHAR16 *String, OUT EFI_IPv4_ADDRESS *Ip4Address)
Definition: DxeNetLib.c:3125
VOID *EFIAPI AllocatePool(IN UINTN AllocationSize)
CONST CHAR16 *EFIAPI ShellCommandLineGetValue(IN CONST LIST_ENTRY *CheckPackage, IN CHAR16 *KeyString)
UINTN EFIAPI ShellStrToUintn(IN CONST CHAR16 *String)
EFI_STATUS EFIAPI ShellDeleteFileByName(IN CONST CHAR16 *FileName)
#define ShellCommandLineParse(CheckList, CheckPackage, ProblemParam, AutoPageBreak)
Make it easy to upgrade from older versions of the shell library.
Definition: ShellLib.h:755
EFI_STATUS EFIAPI ShellPrintHiiEx(IN INT32 Col OPTIONAL, IN INT32 Row OPTIONAL, IN CONST CHAR8 *Language OPTIONAL, IN CONST EFI_STRING_ID HiiFormatStringId, IN CONST EFI_HII_HANDLE HiiFormatHandle,...)
@ TypeValue
A flag that has some data following it with a space (IE "-a 1").
Definition: ShellLib.h:700
EFI_STATUS EFIAPI ShellOpenFileByName(IN CONST CHAR16 *FileName, OUT SHELL_FILE_HANDLE *FileHandle, IN UINT64 OpenMode, IN UINT64 Attributes)
Definition: UefiShellLib.c:720
EFI_STATUS EFIAPI ShellFileExists(IN CONST CHAR16 *Name)
VOID EFIAPI ShellCommandLineFreeVarList(IN LIST_ENTRY *CheckPackage)
EFI_STATUS EFIAPI ShellInitialize(VOID)
Definition: UefiShellLib.c:532
EFI_STATUS EFIAPI ShellSetFilePosition(IN SHELL_FILE_HANDLE FileHandle, IN UINT64 Position)
EFI_STATUS EFIAPI ShellPrintEx(IN INT32 Col OPTIONAL, IN INT32 Row OPTIONAL, IN CONST CHAR16 *Format,...)
CONST CHAR16 *EFIAPI ShellCommandLineGetRawValue(IN CONST LIST_ENTRY *CONST CheckPackage, IN UINTN Position)
UINTN EFIAPI ShellCommandLineGetCount(IN CONST LIST_ENTRY *CheckPackage)
EFI_STATUS EFIAPI ShellWriteFile(IN SHELL_FILE_HANDLE FileHandle, IN OUT UINTN *BufferSize, IN VOID *Buffer)
Definition: UefiShellLib.c:947
EFI_STATUS EFIAPI ShellCloseFile(IN SHELL_FILE_HANDLE *FileHandle)
Definition: UefiShellLib.c:969
STATIC VOID CloseProtocolAndDestroyServiceChild(IN EFI_HANDLE ControllerHandle, IN EFI_GUID *ServiceBindingProtocolGuid, IN EFI_GUID *ProtocolGuid, IN EFI_HANDLE ChildHandle)
Definition: Tftp.c:855
STATIC BOOLEAN StringToUint16(IN CONST CHAR16 *ValueStr, OUT UINT16 *Value)
Definition: Tftp.c:683
#define MTFTP_DEFAULT_BLKSIZE
Definition: Tftp.c:243
EFI_HII_HANDLE InitializeHiiPackage(EFI_HANDLE ImageHandle)
Definition: Tftp.c:1213
STATIC EFI_STATUS DownloadFile(IN EFI_MTFTP4_PROTOCOL *Mtftp4, IN CONST CHAR16 *FilePath, IN CONST CHAR8 *AsciiFilePath, IN UINTN FileSize, IN UINT16 BlockSize, IN UINT16 WindowSize)
Definition: Tftp.c:981
STATIC EFI_STATUS CreateServiceChildAndOpenProtocol(IN EFI_HANDLE ControllerHandle, IN EFI_GUID *ServiceBindingProtocolGuid, IN EFI_GUID *ProtocolGuid, OUT EFI_HANDLE *ChildHandle, OUT VOID **Interface)
Definition: Tftp.c:801
#define MTFTP_MIN_BLKSIZE
Definition: Tftp.c:247
STATIC EFI_STATUS GetNicName(IN EFI_HANDLE ControllerHandle, IN UINTN NicNumber, OUT CHAR16 *NicName)
Definition: Tftp.c:728
#define MTFTP_DEFAULT_WINDOWSIZE
Definition: Tftp.c:252
STATIC EFI_STATUS GetFileSize(IN EFI_MTFTP4_PROTOCOL *Mtftp4, IN CONST CHAR8 *FilePath, OUT UINTN *FileSize)
Definition: Tftp.c:894
STATIC EFI_STATUS EFIAPI CheckPacket(IN EFI_MTFTP4_PROTOCOL *This, IN EFI_MTFTP4_TOKEN *Token, IN UINT16 PacketLen, IN EFI_MTFTP4_PACKET *Packet)
Definition: Tftp.c:1110
SHELL_STATUS RunTftp(IN EFI_HANDLE ImageHandle, IN EFI_SYSTEM_TABLE *SystemTable)
Definition: Tftp.c:277
#define MTFTP_MIN_WINDOWSIZE
Definition: Tftp.c:258
RETURN_STATUS EFI_STATUS
Definition: UefiBaseType.h:29
VOID * EFI_HANDLE
Definition: UefiBaseType.h:33
#define EFI_SUCCESS
Definition: UefiBaseType.h:112
EFI_HANDLE gImageHandle
EFI_BOOT_SERVICES * gBS
EFI_HII_DATABASE_PROTOCOL * gHiiDatabase
#define STRING_TOKEN(t)
VOID * EFI_HII_HANDLE
@ ByProtocol
Definition: UefiSpec.h:1518
EFI_MTFTP4_CHECK_PACKET CheckPacket
Definition: Mtftp4.h:562
UINT32 OptionCount
Definition: Mtftp4.h:539
VOID * Context
Definition: Mtftp4.h:558
EFI_MTFTP4_OPTION * OptionList
Definition: Mtftp4.h:543
UINT8 * Filename
Definition: Mtftp4.h:531
Definition: Base.h:213