2 The implementation for the 'tftp' Shell command.
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>
8 This program and the accompanying materials
9 are licensed and made available under the terms and conditions of the BSD License
10 which accompanies this distribution. The full text of the license may be found at
11 http://opensource.org/licenses/bsd-license.php.
13 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
14 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
19 #define IP4_CONFIG2_INTERFACE_INFO_NAME_LENGTH 32
20 EFI_HANDLE mTftpHiiHandle
;
23 Constant strings and definitions related to the message indicating the amount of
24 progress in the dowloading of a TFTP file.
27 // Frame for the progression slider
28 STATIC CONST CHAR16 mTftpProgressFrame
[] = L
"[ ]";
30 // Number of steps in the progression slider
31 #define TFTP_PROGRESS_SLIDER_STEPS ((sizeof (mTftpProgressFrame) / sizeof (CHAR16)) - 3)
33 // Size in number of characters plus one (final zero) of the message to
34 // indicate the progress of a TFTP download. The format is "[(progress slider:
35 // 40 characters)] (nb of KBytes downloaded so far: 7 characters) Kb". There
36 // are thus the number of characters in mTftpProgressFrame[] plus 11 characters
37 // (2 // spaces, "Kb" and seven characters for the number of KBytes).
38 #define TFTP_PROGRESS_MESSAGE_SIZE ((sizeof (mTftpProgressFrame) / sizeof (CHAR16)) + 12)
40 // String to delete the TFTP progress message to be able to update it :
41 // (TFTP_PROGRESS_MESSAGE_SIZE-1) '\b'
42 STATIC 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";
45 SHELL_FILE_HANDLE mFileHandle
;
47 // Path of the local file, Unicode encoded
48 CONST CHAR16
*mLocalFilePath
;
51 Check and convert the UINT16 option values of the 'tftp' command
53 @param[in] ValueStr Value as an Unicode encoded string
54 @param[out] Value UINT16 value
56 @return TRUE The value was returned.
57 @return FALSE A parsing error occured.
62 IN CONST CHAR16
*ValueStr
,
67 Get the name of the NIC.
69 @param[in] ControllerHandle The network physical device handle.
70 @param[in] NicNumber The network physical device number.
71 @param[out] NicName Address where to store the NIC name.
72 The memory area has to be at least
73 IP4_CONFIG2_INTERFACE_INFO_NAME_LENGTH
76 @return EFI_SUCCESS The name of the NIC was returned.
77 @return Others The creation of the child for the Managed
78 Network Service failed or the opening of
79 the Managed Network Protocol failed or
80 the operational parameters for the
81 Managed Network Protocol could not be
87 IN EFI_HANDLE ControllerHandle
,
93 Create a child for the service identified by its service binding protocol GUID
94 and get from the child the interface of the protocol identified by its GUID.
96 @param[in] ControllerHandle Controller handle.
97 @param[in] ServiceBindingProtocolGuid Service binding protocol GUID of the
98 service to be created.
99 @param[in] ProtocolGuid GUID of the protocol to be open.
100 @param[out] ChildHandle Address where the handler of the
101 created child is returned. NULL is
102 returned in case of error.
103 @param[out] Interface Address where a pointer to the
104 protocol interface is returned in
107 @return EFI_SUCCESS The child was created and the protocol opened.
108 @return Others Either the creation of the child or the opening
109 of the protocol failed.
113 CreateServiceChildAndOpenProtocol (
114 IN EFI_HANDLE ControllerHandle
,
115 IN EFI_GUID
*ServiceBindingProtocolGuid
,
116 IN EFI_GUID
*ProtocolGuid
,
117 OUT EFI_HANDLE
*ChildHandle
,
122 Close the protocol identified by its GUID on the child handle of the service
123 identified by its service binding protocol GUID, then destroy the child
126 @param[in] ControllerHandle Controller handle.
127 @param[in] ServiceBindingProtocolGuid Service binding protocol GUID of the
128 service to be destroyed.
129 @param[in] ProtocolGuid GUID of the protocol to be closed.
130 @param[in] ChildHandle Handle of the child to be destroyed.
135 CloseProtocolAndDestroyServiceChild (
136 IN EFI_HANDLE ControllerHandle
,
137 IN EFI_GUID
*ServiceBindingProtocolGuid
,
138 IN EFI_GUID
*ProtocolGuid
,
139 IN EFI_HANDLE ChildHandle
143 Worker function that gets the size in numbers of bytes of a file from a TFTP
144 server before to download the file.
146 @param[in] Mtftp4 MTFTP4 protocol interface
147 @param[in] FilePath Path of the file, ASCII encoded
148 @param[out] FileSize Address where to store the file size in number of
151 @retval EFI_SUCCESS The size of the file was returned.
152 @retval EFI_UNSUPPORTED The server does not support the "tsize" option.
153 @retval Others Error when retrieving the information from the server
154 (see EFI_MTFTP4_PROTOCOL.GetInfo() status codes)
155 or error when parsing the response of the server.
160 IN EFI_MTFTP4_PROTOCOL
*Mtftp4
,
161 IN CONST CHAR8
*FilePath
,
166 Worker function that download the data of a file from a TFTP server given
167 the path of the file and its size.
169 @param[in] Mtftp4 MTFTP4 protocol interface
170 @param[in] FilePath Path of the file, Unicode encoded
171 @param[in] AsciiFilePath Path of the file, ASCII encoded
172 @param[in] FileSize Size of the file in number of bytes
173 @param[in] BlockSize Value of the TFTP blksize option
174 @param[in] WindowSize Value of the TFTP window size option
176 @retval EFI_SUCCESS The file was downloaded.
177 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
178 @retval Others The downloading of the file from the server failed
179 (see EFI_MTFTP4_PROTOCOL.ReadFile() status codes).
185 IN EFI_MTFTP4_PROTOCOL
*Mtftp4
,
186 IN CONST CHAR16
*FilePath
,
187 IN CONST CHAR8
*AsciiFilePath
,
194 Update the progress of a file download
195 This procedure is called each time a new TFTP packet is received.
197 @param[in] This MTFTP4 protocol interface
198 @param[in] Token Parameters for the download of the file
199 @param[in] PacketLen Length of the packet
200 @param[in] Packet Address of the packet
202 @retval EFI_SUCCESS All packets are accepted.
209 IN EFI_MTFTP4_PROTOCOL
*This
,
210 IN EFI_MTFTP4_TOKEN
*Token
,
212 IN EFI_MTFTP4_PACKET
*Packet
215 EFI_MTFTP4_CONFIG_DATA DefaultMtftp4ConfigData
= {
216 TRUE
, // Use default setting
217 { { 0, 0, 0, 0 } }, // StationIp - Not relevant as UseDefaultSetting=TRUE
218 { { 0, 0, 0, 0 } }, // SubnetMask - Not relevant as UseDefaultSetting=TRUE
219 0, // LocalPort - Automatically assigned port number.
220 { { 0, 0, 0, 0 } }, // GatewayIp - Not relevant as UseDefaultSetting=TRUE
221 { { 0, 0, 0, 0 } }, // ServerIp - Not known yet
222 69, // InitialServerPort - Standard TFTP server port
223 6, // TryCount - The number of times to transmit request packets and wait for a response.
224 4 // TimeoutValue - Retransmission timeout in seconds.
227 STATIC CONST SHELL_PARAM_ITEM ParamList
[] = {
239 /// The default block size (512) of tftp is defined in the RFC1350.
241 #define MTFTP_DEFAULT_BLKSIZE 512
243 /// The valid range of block size option is defined in the RFC2348.
245 #define MTFTP_MIN_BLKSIZE 8
246 #define MTFTP_MAX_BLKSIZE 65464
248 /// The default windowsize (1) of tftp.
250 #define MTFTP_DEFAULT_WINDOWSIZE 1
252 /// The valid range of window size option.
253 /// Note that: RFC 7440 does not mention max window size value, but for the
254 /// stability reason, the value is limited to 64.
256 #define MTFTP_MIN_WINDOWSIZE 1
257 #define MTFTP_MAX_WINDOWSIZE 64
260 Function for 'tftp' command.
262 @param[in] ImageHandle Handle to the Image (NULL if Internal).
263 @param[in] SystemTable Pointer to the System Table (NULL if Internal).
265 @return SHELL_SUCCESS The 'tftp' command completed successfully.
266 @return SHELL_ABORTED The Shell Library initialization failed.
267 @return SHELL_INVALID_PARAMETER At least one of the command's arguments is
269 @return SHELL_OUT_OF_RESOURCES A memory allocation failed.
270 @return SHELL_NOT_FOUND Network Interface Card not found or server
276 IN EFI_HANDLE ImageHandle
,
277 IN EFI_SYSTEM_TABLE
*SystemTable
280 SHELL_STATUS ShellStatus
;
282 LIST_ENTRY
*CheckPackage
;
283 CHAR16
*ProblemParam
;
285 CONST CHAR16
*UserNicName
;
287 CONST CHAR16
*ValueStr
;
288 CONST CHAR16
*RemoteFilePath
;
289 CHAR8
*AsciiRemoteFilePath
;
291 CONST CHAR16
*Walker
;
292 EFI_MTFTP4_CONFIG_DATA Mtftp4ConfigData
;
296 CHAR16 NicName
[IP4_CONFIG2_INTERFACE_INFO_NAME_LENGTH
];
297 EFI_HANDLE ControllerHandle
;
298 EFI_HANDLE Mtftp4ChildHandle
;
299 EFI_MTFTP4_PROTOCOL
*Mtftp4
;
304 ShellStatus
= SHELL_INVALID_PARAMETER
;
307 AsciiRemoteFilePath
= NULL
;
310 BlockSize
= MTFTP_DEFAULT_BLKSIZE
;
311 WindowSize
= MTFTP_DEFAULT_WINDOWSIZE
;
314 // Initialize the Shell library (we must be in non-auto-init...)
316 Status
= ShellInitialize ();
317 if (EFI_ERROR (Status
)) {
318 ASSERT_EFI_ERROR (Status
);
319 return SHELL_ABORTED
;
323 // Parse the command line.
325 Status
= ShellCommandLineParse (ParamList
, &CheckPackage
, &ProblemParam
, TRUE
);
326 if (EFI_ERROR (Status
)) {
327 if ((Status
== EFI_VOLUME_CORRUPTED
) &&
328 (ProblemParam
!= NULL
) ) {
330 -1, -1, NULL
, STRING_TOKEN (STR_GEN_PROBLEM
), mTftpHiiHandle
,
331 L
"tftp", ProblemParam
333 FreePool (ProblemParam
);
341 // Check the number of parameters
343 ParamCount
= ShellCommandLineGetCount (CheckPackage
);
344 if (ParamCount
> 4) {
346 -1, -1, NULL
, STRING_TOKEN (STR_GEN_TOO_MANY
),
347 mTftpHiiHandle
, L
"tftp"
351 if (ParamCount
< 3) {
353 -1, -1, NULL
, STRING_TOKEN (STR_GEN_TOO_FEW
),
354 mTftpHiiHandle
, L
"tftp"
359 CopyMem (&Mtftp4ConfigData
, &DefaultMtftp4ConfigData
, sizeof (EFI_MTFTP4_CONFIG_DATA
));
362 // Check the host IPv4 address
364 ValueStr
= ShellCommandLineGetRawValue (CheckPackage
, 1);
365 Status
= NetLibStrToIp4 (ValueStr
, &Mtftp4ConfigData
.ServerIp
);
366 if (EFI_ERROR (Status
)) {
368 -1, -1, NULL
, STRING_TOKEN (STR_GEN_PARAM_INV
),
369 mTftpHiiHandle
, L
"tftp", ValueStr
374 RemoteFilePath
= ShellCommandLineGetRawValue (CheckPackage
, 2);
375 ASSERT(RemoteFilePath
!= NULL
);
376 FilePathSize
= StrLen (RemoteFilePath
) + 1;
377 AsciiRemoteFilePath
= AllocatePool (FilePathSize
);
378 if (AsciiRemoteFilePath
== NULL
) {
379 ShellStatus
= SHELL_OUT_OF_RESOURCES
;
382 UnicodeStrToAsciiStrS (RemoteFilePath
, AsciiRemoteFilePath
, FilePathSize
);
384 if (ParamCount
== 4) {
385 mLocalFilePath
= ShellCommandLineGetRawValue (CheckPackage
, 3);
387 Walker
= RemoteFilePath
+ StrLen (RemoteFilePath
);
388 while ((--Walker
) >= RemoteFilePath
) {
389 if ((*Walker
== L
'\\') ||
390 (*Walker
== L
'/' ) ) {
394 mLocalFilePath
= Walker
+ 1;
398 // Get the name of the Network Interface Card to be used if any.
400 UserNicName
= ShellCommandLineGetValue (CheckPackage
, L
"-i");
402 ValueStr
= ShellCommandLineGetValue (CheckPackage
, L
"-l");
403 if (ValueStr
!= NULL
) {
404 if (!StringToUint16 (ValueStr
, &Mtftp4ConfigData
.LocalPort
)) {
409 ValueStr
= ShellCommandLineGetValue (CheckPackage
, L
"-r");
410 if (ValueStr
!= NULL
) {
411 if (!StringToUint16 (ValueStr
, &Mtftp4ConfigData
.InitialServerPort
)) {
416 ValueStr
= ShellCommandLineGetValue (CheckPackage
, L
"-c");
417 if (ValueStr
!= NULL
) {
418 if (!StringToUint16 (ValueStr
, &Mtftp4ConfigData
.TryCount
)) {
422 if (Mtftp4ConfigData
.TryCount
== 0) {
423 Mtftp4ConfigData
.TryCount
= 6;
427 ValueStr
= ShellCommandLineGetValue (CheckPackage
, L
"-t");
428 if (ValueStr
!= NULL
) {
429 if (!StringToUint16 (ValueStr
, &Mtftp4ConfigData
.TimeoutValue
)) {
432 if (Mtftp4ConfigData
.TimeoutValue
== 0) {
434 -1, -1, NULL
, STRING_TOKEN (STR_GEN_PARAM_INV
),
435 mTftpHiiHandle
, L
"tftp", ValueStr
441 ValueStr
= ShellCommandLineGetValue (CheckPackage
, L
"-s");
442 if (ValueStr
!= NULL
) {
443 if (!StringToUint16 (ValueStr
, &BlockSize
)) {
446 if (BlockSize
< MTFTP_MIN_BLKSIZE
|| BlockSize
> MTFTP_MAX_BLKSIZE
) {
448 -1, -1, NULL
, STRING_TOKEN (STR_GEN_PARAM_INV
),
449 mTftpHiiHandle
, L
"tftp", ValueStr
455 ValueStr
= ShellCommandLineGetValue (CheckPackage
, L
"-w");
456 if (ValueStr
!= NULL
) {
457 if (!StringToUint16 (ValueStr
, &WindowSize
)) {
460 if (WindowSize
< MTFTP_MIN_WINDOWSIZE
|| WindowSize
> MTFTP_MAX_WINDOWSIZE
) {
462 -1, -1, NULL
, STRING_TOKEN (STR_GEN_PARAM_INV
),
463 mTftpHiiHandle
, L
"tftp", ValueStr
470 // Locate all MTFTP4 Service Binding protocols
472 ShellStatus
= SHELL_NOT_FOUND
;
473 Status
= gBS
->LocateHandleBuffer (
475 &gEfiManagedNetworkServiceBindingProtocolGuid
,
480 if (EFI_ERROR (Status
) || (HandleCount
== 0)) {
482 -1, -1, NULL
, STRING_TOKEN (STR_TFTP_ERR_NO_NIC
),
489 (NicNumber
< HandleCount
) && (ShellStatus
!= SHELL_SUCCESS
);
491 ControllerHandle
= Handles
[NicNumber
];
493 Status
= GetNicName (ControllerHandle
, NicNumber
, NicName
);
494 if (EFI_ERROR (Status
)) {
496 -1, -1, NULL
, STRING_TOKEN (STR_TFTP_ERR_NIC_NAME
),
497 mTftpHiiHandle
, NicNumber
, Status
502 if (UserNicName
!= NULL
) {
503 if (StrCmp (NicName
, UserNicName
) != 0) {
509 Status
= CreateServiceChildAndOpenProtocol (
511 &gEfiMtftp4ServiceBindingProtocolGuid
,
512 &gEfiMtftp4ProtocolGuid
,
516 if (EFI_ERROR (Status
)) {
518 -1, -1, NULL
, STRING_TOKEN (STR_TFTP_ERR_OPEN_PROTOCOL
),
519 mTftpHiiHandle
, NicName
, Status
524 Status
= Mtftp4
->Configure (Mtftp4
, &Mtftp4ConfigData
);
525 if (EFI_ERROR (Status
)) {
527 -1, -1, NULL
, STRING_TOKEN (STR_TFTP_ERR_CONFIGURE
),
528 mTftpHiiHandle
, NicName
, Status
533 Status
= GetFileSize (Mtftp4
, AsciiRemoteFilePath
, &FileSize
);
534 if (EFI_ERROR (Status
)) {
536 -1, -1, NULL
, STRING_TOKEN (STR_TFTP_ERR_FILE_SIZE
),
537 mTftpHiiHandle
, RemoteFilePath
, NicName
, Status
542 Status
= DownloadFile (Mtftp4
, RemoteFilePath
, AsciiRemoteFilePath
, FileSize
, BlockSize
, WindowSize
);
543 if (EFI_ERROR (Status
)) {
545 -1, -1, NULL
, STRING_TOKEN (STR_TFTP_ERR_DOWNLOAD
),
546 mTftpHiiHandle
, RemoteFilePath
, NicName
, Status
553 CloseProtocolAndDestroyServiceChild (
555 &gEfiMtftp4ServiceBindingProtocolGuid
,
556 &gEfiMtftp4ProtocolGuid
,
561 if ((UserNicName
!= NULL
) && (!NicFound
)) {
563 -1, -1, NULL
, STRING_TOKEN (STR_TFTP_ERR_NIC_NOT_FOUND
),
564 mTftpHiiHandle
, UserNicName
570 ShellCommandLineFreeVarList (CheckPackage
);
571 if (AsciiRemoteFilePath
!= NULL
) {
572 FreePool (AsciiRemoteFilePath
);
574 if (Handles
!= NULL
) {
582 Check and convert the UINT16 option values of the 'tftp' command
584 @param[in] ValueStr Value as an Unicode encoded string
585 @param[out] Value UINT16 value
587 @return TRUE The value was returned.
588 @return FALSE A parsing error occured.
593 IN CONST CHAR16
*ValueStr
,
599 Val
= ShellStrToUintn (ValueStr
);
600 if (Val
> MAX_UINT16
) {
602 -1, -1, NULL
, STRING_TOKEN (STR_GEN_PARAM_INV
),
603 mTftpHiiHandle
, L
"tftp", ValueStr
608 *Value
= (UINT16
)Val
;
613 Get the name of the NIC.
615 @param[in] ControllerHandle The network physical device handle.
616 @param[in] NicNumber The network physical device number.
617 @param[out] NicName Address where to store the NIC name.
618 The memory area has to be at least
619 IP4_CONFIG2_INTERFACE_INFO_NAME_LENGTH
622 @return EFI_SUCCESS The name of the NIC was returned.
623 @return Others The creation of the child for the Managed
624 Network Service failed or the opening of
625 the Managed Network Protocol failed or
626 the operational parameters for the
627 Managed Network Protocol could not be
633 IN EFI_HANDLE ControllerHandle
,
639 EFI_HANDLE MnpHandle
;
640 EFI_MANAGED_NETWORK_PROTOCOL
*Mnp
;
641 EFI_SIMPLE_NETWORK_MODE SnpMode
;
643 Status
= CreateServiceChildAndOpenProtocol (
645 &gEfiManagedNetworkServiceBindingProtocolGuid
,
646 &gEfiManagedNetworkProtocolGuid
,
650 if (EFI_ERROR (Status
)) {
654 Status
= Mnp
->GetModeData (Mnp
, NULL
, &SnpMode
);
655 if (EFI_ERROR (Status
) && (Status
!= EFI_NOT_STARTED
)) {
661 IP4_CONFIG2_INTERFACE_INFO_NAME_LENGTH
,
662 SnpMode
.IfType
== NET_IFTYPE_ETHERNET
?
668 Status
= EFI_SUCCESS
;
672 if (MnpHandle
!= NULL
) {
673 CloseProtocolAndDestroyServiceChild (
675 &gEfiManagedNetworkServiceBindingProtocolGuid
,
676 &gEfiManagedNetworkProtocolGuid
,
685 Create a child for the service identified by its service binding protocol GUID
686 and get from the child the interface of the protocol identified by its GUID.
688 @param[in] ControllerHandle Controller handle.
689 @param[in] ServiceBindingProtocolGuid Service binding protocol GUID of the
690 service to be created.
691 @param[in] ProtocolGuid GUID of the protocol to be open.
692 @param[out] ChildHandle Address where the handler of the
693 created child is returned. NULL is
694 returned in case of error.
695 @param[out] Interface Address where a pointer to the
696 protocol interface is returned in
699 @return EFI_SUCCESS The child was created and the protocol opened.
700 @return Others Either the creation of the child or the opening
701 of the protocol failed.
705 CreateServiceChildAndOpenProtocol (
706 IN EFI_HANDLE ControllerHandle
,
707 IN EFI_GUID
*ServiceBindingProtocolGuid
,
708 IN EFI_GUID
*ProtocolGuid
,
709 OUT EFI_HANDLE
*ChildHandle
,
716 Status
= NetLibCreateServiceChild (
719 ServiceBindingProtocolGuid
,
722 if (!EFI_ERROR (Status
)) {
723 Status
= gBS
->OpenProtocol (
729 EFI_OPEN_PROTOCOL_GET_PROTOCOL
731 if (EFI_ERROR (Status
)) {
732 NetLibDestroyServiceChild (
735 ServiceBindingProtocolGuid
,
746 Close the protocol identified by its GUID on the child handle of the service
747 identified by its service binding protocol GUID, then destroy the child
750 @param[in] ControllerHandle Controller handle.
751 @param[in] ServiceBindingProtocolGuid Service binding protocol GUID of the
752 service to be destroyed.
753 @param[in] ProtocolGuid GUID of the protocol to be closed.
754 @param[in] ChildHandle Handle of the child to be destroyed.
759 CloseProtocolAndDestroyServiceChild (
760 IN EFI_HANDLE ControllerHandle
,
761 IN EFI_GUID
*ServiceBindingProtocolGuid
,
762 IN EFI_GUID
*ProtocolGuid
,
763 IN EFI_HANDLE ChildHandle
773 NetLibDestroyServiceChild (
776 ServiceBindingProtocolGuid
,
782 Worker function that gets the size in numbers of bytes of a file from a TFTP
783 server before to download the file.
785 @param[in] Mtftp4 MTFTP4 protocol interface
786 @param[in] FilePath Path of the file, ASCII encoded
787 @param[out] FileSize Address where to store the file size in number of
790 @retval EFI_SUCCESS The size of the file was returned.
791 @retval EFI_UNSUPPORTED The server does not support the "tsize" option.
792 @retval Others Error when retrieving the information from the server
793 (see EFI_MTFTP4_PROTOCOL.GetInfo() status codes)
794 or error when parsing the response of the server.
799 IN EFI_MTFTP4_PROTOCOL
*Mtftp4
,
800 IN CONST CHAR8
*FilePath
,
805 EFI_MTFTP4_OPTION ReqOpt
[1];
806 EFI_MTFTP4_PACKET
*Packet
;
808 EFI_MTFTP4_OPTION
*TableOfOptions
;
809 EFI_MTFTP4_OPTION
*Option
;
813 ReqOpt
[0].OptionStr
= (UINT8
*)"tsize";
816 ReqOpt
[0].ValueStr
= OptBuf
;
818 Status
= Mtftp4
->GetInfo (
829 if (EFI_ERROR (Status
)) {
833 Status
= Mtftp4
->ParseOptions (
840 if (EFI_ERROR (Status
)) {
844 Option
= TableOfOptions
;
845 while (OptCnt
!= 0) {
846 if (AsciiStrnCmp ((CHAR8
*)Option
->OptionStr
, "tsize", 5) == 0) {
847 *FileSize
= AsciiStrDecimalToUintn ((CHAR8
*)Option
->ValueStr
);
853 FreePool (TableOfOptions
);
856 Status
= EFI_UNSUPPORTED
;
865 Worker function that download the data of a file from a TFTP server given
866 the path of the file and its size.
868 @param[in] Mtftp4 MTFTP4 protocol interface
869 @param[in] FilePath Path of the file, Unicode encoded
870 @param[in] AsciiFilePath Path of the file, ASCII encoded
871 @param[in] FileSize Size of the file in number of bytes
872 @param[in] BlockSize Value of the TFTP blksize option
873 @param[in] WindowSize Value of the TFTP window size option
875 @retval EFI_SUCCESS The file was downloaded.
876 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
877 @retval Others The downloading of the file from the server failed
878 (see EFI_MTFTP4_PROTOCOL.ReadFile() status codes).
884 IN EFI_MTFTP4_PROTOCOL
*Mtftp4
,
885 IN CONST CHAR16
*FilePath
,
886 IN CONST CHAR8
*AsciiFilePath
,
893 DOWNLOAD_CONTEXT
*TftpContext
;
894 EFI_MTFTP4_TOKEN Mtftp4Token
;
895 UINT8 BlksizeBuf
[10];
896 UINT8 WindowsizeBuf
[10];
898 ZeroMem (&Mtftp4Token
, sizeof (EFI_MTFTP4_TOKEN
));
900 TftpContext
= AllocatePool (sizeof (DOWNLOAD_CONTEXT
));
901 if (TftpContext
== NULL
) {
902 Status
= EFI_OUT_OF_RESOURCES
;
905 TftpContext
->FileSize
= FileSize
;
906 TftpContext
->DownloadedNbOfBytes
= 0;
907 TftpContext
->LastReportedNbOfBytes
= 0;
909 Mtftp4Token
.Filename
= (UINT8
*)AsciiFilePath
;
910 Mtftp4Token
.CheckPacket
= CheckPacket
;
911 Mtftp4Token
.Context
= (VOID
*)TftpContext
;
912 Mtftp4Token
.OptionCount
= 0;
913 Mtftp4Token
.OptionList
= AllocatePool (sizeof (EFI_MTFTP4_OPTION
) * 2);
914 if (Mtftp4Token
.OptionList
== NULL
) {
915 Status
= EFI_OUT_OF_RESOURCES
;
919 if (BlockSize
!= MTFTP_DEFAULT_BLKSIZE
) {
920 Mtftp4Token
.OptionList
[Mtftp4Token
.OptionCount
].OptionStr
= (UINT8
*) "blksize";
921 AsciiSPrint ((CHAR8
*) BlksizeBuf
, sizeof (BlksizeBuf
), "%d", BlockSize
);
922 Mtftp4Token
.OptionList
[Mtftp4Token
.OptionCount
].ValueStr
= BlksizeBuf
;
923 Mtftp4Token
.OptionCount
++;
926 if (WindowSize
!= MTFTP_DEFAULT_WINDOWSIZE
) {
927 Mtftp4Token
.OptionList
[Mtftp4Token
.OptionCount
].OptionStr
= (UINT8
*) "windowsize";
928 AsciiSPrint ((CHAR8
*) WindowsizeBuf
, sizeof (WindowsizeBuf
), "%d", WindowSize
);
929 Mtftp4Token
.OptionList
[Mtftp4Token
.OptionCount
].ValueStr
= WindowsizeBuf
;
930 Mtftp4Token
.OptionCount
++;
934 -1, -1, NULL
, STRING_TOKEN (STR_TFTP_DOWNLOADING
),
935 mTftpHiiHandle
, FilePath
941 if (!EFI_ERROR (ShellFileExists (mLocalFilePath
))) {
942 ShellDeleteFileByName (mLocalFilePath
);
945 Status
= ShellOpenFileByName (
948 EFI_FILE_MODE_CREATE
|
949 EFI_FILE_MODE_WRITE
|
953 if (EFI_ERROR (Status
)) {
955 -1, -1, NULL
, STRING_TOKEN (STR_GEN_FILE_OPEN_FAIL
),
956 mTftpHiiHandle
, L
"tftp", mLocalFilePath
961 Status
= Mtftp4
->ReadFile (Mtftp4
, &Mtftp4Token
);
963 -1, -1, NULL
, STRING_TOKEN (STR_GEN_CRLF
),
970 ShellCloseFile (&mFileHandle
);
973 if (TftpContext
!= NULL
) {
974 FreePool (TftpContext
);
977 if (Mtftp4Token
.OptionList
!= NULL
) {
978 FreePool (Mtftp4Token
.OptionList
);
985 Update the progress of a file download
986 This procedure is called each time a new TFTP packet is received.
988 @param[in] This MTFTP4 protocol interface
989 @param[in] Token Parameters for the download of the file
990 @param[in] PacketLen Length of the packet
991 @param[in] Packet Address of the packet
993 @retval EFI_SUCCESS All packets are accepted.
1000 IN EFI_MTFTP4_PROTOCOL
*This
,
1001 IN EFI_MTFTP4_TOKEN
*Token
,
1002 IN UINT16 PacketLen
,
1003 IN EFI_MTFTP4_PACKET
*Packet
1006 DOWNLOAD_CONTEXT
*Context
;
1007 CHAR16 Progress
[TFTP_PROGRESS_MESSAGE_SIZE
];
1015 if ((NTOHS (Packet
->OpCode
)) != EFI_MTFTP4_OPCODE_DATA
) {
1019 Context
= (DOWNLOAD_CONTEXT
*)Token
->Context
;
1022 // The data in the packet are prepended with two UINT16 :
1023 // . OpCode = EFI_MTFTP4_OPCODE_DATA
1024 // . Block = the number of this block of data
1026 DownloadLen
= (UINTN
)PacketLen
- sizeof (Packet
->OpCode
) - sizeof (Packet
->Data
.Block
);
1028 ShellSetFilePosition(mFileHandle
, Context
->DownloadedNbOfBytes
);
1029 Status
= ShellWriteFile (mFileHandle
, &DownloadLen
, Packet
->Data
.Data
);
1030 if (EFI_ERROR (Status
)) {
1031 if (Context
->DownloadedNbOfBytes
> 0) {
1033 -1, -1, NULL
, STRING_TOKEN (STR_GEN_CRLF
),
1038 -1, -1, NULL
, STRING_TOKEN (STR_TFTP_ERR_WRITE
),
1039 mTftpHiiHandle
, mLocalFilePath
, Status
1044 if (Context
->DownloadedNbOfBytes
== 0) {
1045 ShellPrintEx (-1, -1, L
"%s 0 Kb", mTftpProgressFrame
);
1048 Context
->DownloadedNbOfBytes
+= DownloadLen
;
1049 NbOfKb
= Context
->DownloadedNbOfBytes
/ 1024;
1051 Progress
[0] = L
'\0';
1052 LastStep
= (Context
->LastReportedNbOfBytes
* TFTP_PROGRESS_SLIDER_STEPS
) / Context
->FileSize
;
1053 Step
= (Context
->DownloadedNbOfBytes
* TFTP_PROGRESS_SLIDER_STEPS
) / Context
->FileSize
;
1055 if (Step
<= LastStep
) {
1059 ShellPrintEx (-1, -1, L
"%s", mTftpProgressDelete
);
1061 Status
= StrCpyS (Progress
, TFTP_PROGRESS_MESSAGE_SIZE
, mTftpProgressFrame
);
1062 if (EFI_ERROR(Status
)) {
1065 for (Index
= 1; Index
< Step
; Index
++) {
1066 Progress
[Index
] = L
'=';
1068 Progress
[Step
] = L
'>';
1071 Progress
+ (sizeof (mTftpProgressFrame
) / sizeof (CHAR16
)) - 1,
1072 sizeof (Progress
) - sizeof (mTftpProgressFrame
),
1076 Context
->LastReportedNbOfBytes
= Context
->DownloadedNbOfBytes
;
1078 ShellPrintEx (-1, -1, L
"%s", Progress
);
1084 Retrive HII package list from ImageHandle and publish to HII database.
1086 @param ImageHandle The image handle of the process.
1091 InitializeHiiPackage (
1092 EFI_HANDLE ImageHandle
1096 EFI_HII_PACKAGE_LIST_HEADER
*PackageList
;
1097 EFI_HANDLE HiiHandle
;
1100 // Retrieve HII package list from ImageHandle
1102 Status
= gBS
->OpenProtocol (
1104 &gEfiHiiPackageListProtocolGuid
,
1105 (VOID
**)&PackageList
,
1108 EFI_OPEN_PROTOCOL_GET_PROTOCOL
1110 ASSERT_EFI_ERROR (Status
);
1111 if (EFI_ERROR (Status
)) {
1116 // Publish HII package list to HII Database.
1118 Status
= gHiiDatabase
->NewPackageList (
1124 ASSERT_EFI_ERROR (Status
);
1125 if (EFI_ERROR (Status
)) {