2 The implementation for the 'tftp' Shell command.
4 Copyright (c) 2015, ARM Ltd. All rights reserved.<BR>
5 Copyright (c) 2015 - 2016, 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.
17 #include "UefiShellTftpCommandLib.h"
19 #define IP4_CONFIG2_INTERFACE_INFO_NAME_LENGTH 32
22 Constant strings and definitions related to the message indicating the amount of
23 progress in the dowloading of a TFTP file.
26 // Frame for the progression slider
27 STATIC CONST CHAR16 mTftpProgressFrame
[] = L
"[ ]";
29 // Number of steps in the progression slider
30 #define TFTP_PROGRESS_SLIDER_STEPS ((sizeof (mTftpProgressFrame) / sizeof (CHAR16)) - 3)
32 // Size in number of characters plus one (final zero) of the message to
33 // indicate the progress of a TFTP download. The format is "[(progress slider:
34 // 40 characters)] (nb of KBytes downloaded so far: 7 characters) Kb". There
35 // are thus the number of characters in mTftpProgressFrame[] plus 11 characters
36 // (2 // spaces, "Kb" and seven characters for the number of KBytes).
37 #define TFTP_PROGRESS_MESSAGE_SIZE ((sizeof (mTftpProgressFrame) / sizeof (CHAR16)) + 12)
39 // String to delete the TFTP progress message to be able to update it :
40 // (TFTP_PROGRESS_MESSAGE_SIZE-1) '\b'
41 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";
44 Check and convert the UINT16 option values of the 'tftp' command
46 @param[in] ValueStr Value as an Unicode encoded string
47 @param[out] Value UINT16 value
49 @return TRUE The value was returned.
50 @return FALSE A parsing error occured.
55 IN CONST CHAR16
*ValueStr
,
60 Get the name of the NIC.
62 @param[in] ControllerHandle The network physical device handle.
63 @param[in] NicNumber The network physical device number.
64 @param[out] NicName Address where to store the NIC name.
65 The memory area has to be at least
66 IP4_CONFIG2_INTERFACE_INFO_NAME_LENGTH
69 @return EFI_SUCCESS The name of the NIC was returned.
70 @return Others The creation of the child for the Managed
71 Network Service failed or the opening of
72 the Managed Network Protocol failed or
73 the operational parameters for the
74 Managed Network Protocol could not be
80 IN EFI_HANDLE ControllerHandle
,
86 Create a child for the service identified by its service binding protocol GUID
87 and get from the child the interface of the protocol identified by its GUID.
89 @param[in] ControllerHandle Controller handle.
90 @param[in] ServiceBindingProtocolGuid Service binding protocol GUID of the
91 service to be created.
92 @param[in] ProtocolGuid GUID of the protocol to be open.
93 @param[out] ChildHandle Address where the handler of the
94 created child is returned. NULL is
95 returned in case of error.
96 @param[out] Interface Address where a pointer to the
97 protocol interface is returned in
100 @return EFI_SUCCESS The child was created and the protocol opened.
101 @return Others Either the creation of the child or the opening
102 of the protocol failed.
106 CreateServiceChildAndOpenProtocol (
107 IN EFI_HANDLE ControllerHandle
,
108 IN EFI_GUID
*ServiceBindingProtocolGuid
,
109 IN EFI_GUID
*ProtocolGuid
,
110 OUT EFI_HANDLE
*ChildHandle
,
115 Close the protocol identified by its GUID on the child handle of the service
116 identified by its service binding protocol GUID, then destroy the child
119 @param[in] ControllerHandle Controller handle.
120 @param[in] ServiceBindingProtocolGuid Service binding protocol GUID of the
121 service to be destroyed.
122 @param[in] ProtocolGuid GUID of the protocol to be closed.
123 @param[in] ChildHandle Handle of the child to be destroyed.
128 CloseProtocolAndDestroyServiceChild (
129 IN EFI_HANDLE ControllerHandle
,
130 IN EFI_GUID
*ServiceBindingProtocolGuid
,
131 IN EFI_GUID
*ProtocolGuid
,
132 IN EFI_HANDLE ChildHandle
136 Worker function that gets the size in numbers of bytes of a file from a TFTP
137 server before to download the file.
139 @param[in] Mtftp4 MTFTP4 protocol interface
140 @param[in] FilePath Path of the file, ASCII encoded
141 @param[out] FileSize Address where to store the file size in number of
144 @retval EFI_SUCCESS The size of the file was returned.
145 @retval EFI_UNSUPPORTED The server does not support the "tsize" option.
146 @retval Others Error when retrieving the information from the server
147 (see EFI_MTFTP4_PROTOCOL.GetInfo() status codes)
148 or error when parsing the response of the server.
153 IN EFI_MTFTP4_PROTOCOL
*Mtftp4
,
154 IN CONST CHAR8
*FilePath
,
159 Worker function that download the data of a file from a TFTP server given
160 the path of the file and its size.
162 @param[in] Mtftp4 MTFTP4 protocol interface
163 @param[in] FilePath Path of the file, Unicode encoded
164 @param[in] AsciiFilePath Path of the file, ASCII encoded
165 @param[in] FileSize Size of the file in number of bytes
166 @param[in] BlockSize Value of the TFTP blksize option
167 @param[out] Data Address where to store the address of the buffer
168 where the data of the file were downloaded in
171 @retval EFI_SUCCESS The file was downloaded.
172 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
173 @retval Others The downloading of the file from the server failed
174 (see EFI_MTFTP4_PROTOCOL.ReadFile() status codes).
180 IN EFI_MTFTP4_PROTOCOL
*Mtftp4
,
181 IN CONST CHAR16
*FilePath
,
182 IN CONST CHAR8
*AsciiFilePath
,
189 Update the progress of a file download
190 This procedure is called each time a new TFTP packet is received.
192 @param[in] This MTFTP4 protocol interface
193 @param[in] Token Parameters for the download of the file
194 @param[in] PacketLen Length of the packet
195 @param[in] Packet Address of the packet
197 @retval EFI_SUCCESS All packets are accepted.
204 IN EFI_MTFTP4_PROTOCOL
*This
,
205 IN EFI_MTFTP4_TOKEN
*Token
,
207 IN EFI_MTFTP4_PACKET
*Packet
210 EFI_MTFTP4_CONFIG_DATA DefaultMtftp4ConfigData
= {
211 TRUE
, // Use default setting
212 { { 0, 0, 0, 0 } }, // StationIp - Not relevant as UseDefaultSetting=TRUE
213 { { 0, 0, 0, 0 } }, // SubnetMask - Not relevant as UseDefaultSetting=TRUE
214 0, // LocalPort - Automatically assigned port number.
215 { { 0, 0, 0, 0 } }, // GatewayIp - Not relevant as UseDefaultSetting=TRUE
216 { { 0, 0, 0, 0 } }, // ServerIp - Not known yet
217 69, // InitialServerPort - Standard TFTP server port
218 6, // TryCount - Max number of retransmissions.
219 4 // TimeoutValue - Retransmission timeout in seconds.
222 STATIC CONST SHELL_PARAM_ITEM ParamList
[] = {
233 /// The default block size (512) of tftp is defined in the RFC1350.
235 #define MTFTP_DEFAULT_BLKSIZE 512
237 /// The valid range of block size option is defined in the RFC2348.
239 #define MTFTP_MIN_BLKSIZE 8
240 #define MTFTP_MAX_BLKSIZE 65464
244 Function for 'tftp' command.
246 @param[in] ImageHandle Handle to the Image (NULL if Internal).
247 @param[in] SystemTable Pointer to the System Table (NULL if Internal).
249 @return SHELL_SUCCESS The 'tftp' command completed successfully.
250 @return SHELL_ABORTED The Shell Library initialization failed.
251 @return SHELL_INVALID_PARAMETER At least one of the command's arguments is
253 @return SHELL_OUT_OF_RESOURCES A memory allocation failed.
254 @return SHELL_NOT_FOUND Network Interface Card not found or server
260 ShellCommandRunTftp (
261 IN EFI_HANDLE ImageHandle
,
262 IN EFI_SYSTEM_TABLE
*SystemTable
265 SHELL_STATUS ShellStatus
;
267 LIST_ENTRY
*CheckPackage
;
268 CHAR16
*ProblemParam
;
270 CONST CHAR16
*UserNicName
;
272 CONST CHAR16
*ValueStr
;
273 CONST CHAR16
*RemoteFilePath
;
274 CHAR8
*AsciiRemoteFilePath
;
276 CONST CHAR16
*Walker
;
277 CONST CHAR16
*LocalFilePath
;
278 EFI_MTFTP4_CONFIG_DATA Mtftp4ConfigData
;
282 CHAR16 NicName
[IP4_CONFIG2_INTERFACE_INFO_NAME_LENGTH
];
283 EFI_HANDLE ControllerHandle
;
284 EFI_HANDLE Mtftp4ChildHandle
;
285 EFI_MTFTP4_PROTOCOL
*Mtftp4
;
288 SHELL_FILE_HANDLE FileHandle
;
291 ShellStatus
= SHELL_INVALID_PARAMETER
;
294 AsciiRemoteFilePath
= NULL
;
297 BlockSize
= MTFTP_DEFAULT_BLKSIZE
;
300 // Initialize the Shell library (we must be in non-auto-init...)
302 Status
= ShellInitialize ();
303 if (EFI_ERROR (Status
)) {
304 ASSERT_EFI_ERROR (Status
);
305 return SHELL_ABORTED
;
309 // Parse the command line.
311 Status
= ShellCommandLineParse (ParamList
, &CheckPackage
, &ProblemParam
, TRUE
);
312 if (EFI_ERROR (Status
)) {
313 if ((Status
== EFI_VOLUME_CORRUPTED
) &&
314 (ProblemParam
!= NULL
) ) {
316 -1, -1, NULL
, STRING_TOKEN (STR_GEN_PROBLEM
), gShellTftpHiiHandle
,
317 L
"tftp", ProblemParam
319 FreePool (ProblemParam
);
327 // Check the number of parameters
329 ParamCount
= ShellCommandLineGetCount (CheckPackage
);
330 if (ParamCount
> 4) {
332 -1, -1, NULL
, STRING_TOKEN (STR_GEN_TOO_MANY
),
333 gShellTftpHiiHandle
, L
"tftp"
337 if (ParamCount
< 3) {
339 -1, -1, NULL
, STRING_TOKEN (STR_GEN_TOO_FEW
),
340 gShellTftpHiiHandle
, L
"tftp"
345 CopyMem (&Mtftp4ConfigData
, &DefaultMtftp4ConfigData
, sizeof (EFI_MTFTP4_CONFIG_DATA
));
348 // Check the host IPv4 address
350 ValueStr
= ShellCommandLineGetRawValue (CheckPackage
, 1);
351 Status
= NetLibStrToIp4 (ValueStr
, &Mtftp4ConfigData
.ServerIp
);
352 if (EFI_ERROR (Status
)) {
354 -1, -1, NULL
, STRING_TOKEN (STR_GEN_PARAM_INV
),
355 gShellTftpHiiHandle
, L
"tftp", ValueStr
360 RemoteFilePath
= ShellCommandLineGetRawValue (CheckPackage
, 2);
361 ASSERT(RemoteFilePath
!= NULL
);
362 FilePathSize
= StrLen (RemoteFilePath
) + 1;
363 AsciiRemoteFilePath
= AllocatePool (FilePathSize
);
364 if (AsciiRemoteFilePath
== NULL
) {
365 ShellStatus
= SHELL_OUT_OF_RESOURCES
;
368 UnicodeStrToAsciiStrS (RemoteFilePath
, AsciiRemoteFilePath
, FilePathSize
);
370 if (ParamCount
== 4) {
371 LocalFilePath
= ShellCommandLineGetRawValue (CheckPackage
, 3);
373 Walker
= RemoteFilePath
+ StrLen (RemoteFilePath
);
374 while ((--Walker
) >= RemoteFilePath
) {
375 if ((*Walker
== L
'\\') ||
376 (*Walker
== L
'/' ) ) {
380 LocalFilePath
= Walker
+ 1;
384 // Get the name of the Network Interface Card to be used if any.
386 UserNicName
= ShellCommandLineGetValue (CheckPackage
, L
"-i");
388 ValueStr
= ShellCommandLineGetValue (CheckPackage
, L
"-l");
389 if (ValueStr
!= NULL
) {
390 if (!StringToUint16 (ValueStr
, &Mtftp4ConfigData
.LocalPort
)) {
395 ValueStr
= ShellCommandLineGetValue (CheckPackage
, L
"-r");
396 if (ValueStr
!= NULL
) {
397 if (!StringToUint16 (ValueStr
, &Mtftp4ConfigData
.InitialServerPort
)) {
402 ValueStr
= ShellCommandLineGetValue (CheckPackage
, L
"-c");
403 if (ValueStr
!= NULL
) {
404 if (!StringToUint16 (ValueStr
, &Mtftp4ConfigData
.TryCount
)) {
409 ValueStr
= ShellCommandLineGetValue (CheckPackage
, L
"-t");
410 if (ValueStr
!= NULL
) {
411 if (!StringToUint16 (ValueStr
, &Mtftp4ConfigData
.TimeoutValue
)) {
414 if (Mtftp4ConfigData
.TimeoutValue
== 0) {
416 -1, -1, NULL
, STRING_TOKEN (STR_GEN_PARAM_INV
),
417 gShellTftpHiiHandle
, L
"tftp", ValueStr
423 ValueStr
= ShellCommandLineGetValue (CheckPackage
, L
"-s");
424 if (ValueStr
!= NULL
) {
425 if (!StringToUint16 (ValueStr
, &BlockSize
)) {
428 if (BlockSize
< MTFTP_MIN_BLKSIZE
|| BlockSize
> MTFTP_MAX_BLKSIZE
) {
430 -1, -1, NULL
, STRING_TOKEN (STR_GEN_PARAM_INV
),
431 gShellTftpHiiHandle
, L
"tftp", ValueStr
438 // Locate all MTFTP4 Service Binding protocols
440 ShellStatus
= SHELL_NOT_FOUND
;
441 Status
= gBS
->LocateHandleBuffer (
443 &gEfiManagedNetworkServiceBindingProtocolGuid
,
448 if (EFI_ERROR (Status
) || (HandleCount
== 0)) {
450 -1, -1, NULL
, STRING_TOKEN (STR_TFTP_ERR_NO_NIC
),
457 (NicNumber
< HandleCount
) && (ShellStatus
!= SHELL_SUCCESS
);
459 ControllerHandle
= Handles
[NicNumber
];
462 Status
= GetNicName (ControllerHandle
, NicNumber
, NicName
);
463 if (EFI_ERROR (Status
)) {
465 -1, -1, NULL
, STRING_TOKEN (STR_TFTP_ERR_NIC_NAME
),
466 gShellTftpHiiHandle
, NicNumber
, Status
471 if (UserNicName
!= NULL
) {
472 if (StrCmp (NicName
, UserNicName
) != 0) {
478 Status
= CreateServiceChildAndOpenProtocol (
480 &gEfiMtftp4ServiceBindingProtocolGuid
,
481 &gEfiMtftp4ProtocolGuid
,
485 if (EFI_ERROR (Status
)) {
487 -1, -1, NULL
, STRING_TOKEN (STR_TFTP_ERR_OPEN_PROTOCOL
),
488 gShellTftpHiiHandle
, NicName
, Status
493 Status
= Mtftp4
->Configure (Mtftp4
, &Mtftp4ConfigData
);
494 if (EFI_ERROR (Status
)) {
496 -1, -1, NULL
, STRING_TOKEN (STR_TFTP_ERR_CONFIGURE
),
497 gShellTftpHiiHandle
, NicName
, Status
502 Status
= GetFileSize (Mtftp4
, AsciiRemoteFilePath
, &FileSize
);
503 if (EFI_ERROR (Status
)) {
505 -1, -1, NULL
, STRING_TOKEN (STR_TFTP_ERR_FILE_SIZE
),
506 gShellTftpHiiHandle
, RemoteFilePath
, NicName
, Status
511 Status
= DownloadFile (Mtftp4
, RemoteFilePath
, AsciiRemoteFilePath
, FileSize
, BlockSize
, &Data
);
512 if (EFI_ERROR (Status
)) {
514 -1, -1, NULL
, STRING_TOKEN (STR_TFTP_ERR_DOWNLOAD
),
515 gShellTftpHiiHandle
, RemoteFilePath
, NicName
, Status
520 if (!EFI_ERROR (ShellFileExists (LocalFilePath
))) {
521 ShellDeleteFileByName (LocalFilePath
);
524 Status
= ShellOpenFileByName (
527 EFI_FILE_MODE_CREATE
|
528 EFI_FILE_MODE_WRITE
|
532 if (EFI_ERROR (Status
)) {
534 -1, -1, NULL
, STRING_TOKEN (STR_GEN_FILE_OPEN_FAIL
),
535 gShellTftpHiiHandle
, L
"tftp", LocalFilePath
540 Status
= ShellWriteFile (FileHandle
, &FileSize
, Data
);
541 if (!EFI_ERROR (Status
)) {
542 ShellStatus
= SHELL_SUCCESS
;
545 -1, -1, NULL
, STRING_TOKEN (STR_TFTP_ERR_WRITE
),
546 gShellTftpHiiHandle
, LocalFilePath
, Status
549 ShellCloseFile (&FileHandle
);
554 gBS
->FreePages ((EFI_PHYSICAL_ADDRESS
)(UINTN
)Data
, EFI_SIZE_TO_PAGES (FileSize
));
557 CloseProtocolAndDestroyServiceChild (
559 &gEfiMtftp4ServiceBindingProtocolGuid
,
560 &gEfiMtftp4ProtocolGuid
,
565 if ((UserNicName
!= NULL
) && (!NicFound
)) {
567 -1, -1, NULL
, STRING_TOKEN (STR_TFTP_ERR_NIC_NOT_FOUND
),
568 gShellTftpHiiHandle
, UserNicName
574 ShellCommandLineFreeVarList (CheckPackage
);
575 if (AsciiRemoteFilePath
!= NULL
) {
576 FreePool (AsciiRemoteFilePath
);
578 if (Handles
!= NULL
) {
586 Check and convert the UINT16 option values of the 'tftp' command
588 @param[in] ValueStr Value as an Unicode encoded string
589 @param[out] Value UINT16 value
591 @return TRUE The value was returned.
592 @return FALSE A parsing error occured.
597 IN CONST CHAR16
*ValueStr
,
603 Val
= ShellStrToUintn (ValueStr
);
604 if (Val
> MAX_UINT16
) {
606 -1, -1, NULL
, STRING_TOKEN (STR_GEN_PARAM_INV
),
607 gShellTftpHiiHandle
, L
"tftp", ValueStr
612 *Value
= (UINT16
)Val
;
617 Get the name of the NIC.
619 @param[in] ControllerHandle The network physical device handle.
620 @param[in] NicNumber The network physical device number.
621 @param[out] NicName Address where to store the NIC name.
622 The memory area has to be at least
623 IP4_CONFIG2_INTERFACE_INFO_NAME_LENGTH
626 @return EFI_SUCCESS The name of the NIC was returned.
627 @return Others The creation of the child for the Managed
628 Network Service failed or the opening of
629 the Managed Network Protocol failed or
630 the operational parameters for the
631 Managed Network Protocol could not be
637 IN EFI_HANDLE ControllerHandle
,
643 EFI_HANDLE MnpHandle
;
644 EFI_MANAGED_NETWORK_PROTOCOL
*Mnp
;
645 EFI_SIMPLE_NETWORK_MODE SnpMode
;
647 Status
= CreateServiceChildAndOpenProtocol (
649 &gEfiManagedNetworkServiceBindingProtocolGuid
,
650 &gEfiManagedNetworkProtocolGuid
,
654 if (EFI_ERROR (Status
)) {
658 Status
= Mnp
->GetModeData (Mnp
, NULL
, &SnpMode
);
659 if (EFI_ERROR (Status
) && (Status
!= EFI_NOT_STARTED
)) {
665 IP4_CONFIG2_INTERFACE_INFO_NAME_LENGTH
,
666 SnpMode
.IfType
== NET_IFTYPE_ETHERNET
?
672 Status
= EFI_SUCCESS
;
676 if (MnpHandle
!= NULL
) {
677 CloseProtocolAndDestroyServiceChild (
679 &gEfiManagedNetworkServiceBindingProtocolGuid
,
680 &gEfiManagedNetworkProtocolGuid
,
689 Create a child for the service identified by its service binding protocol GUID
690 and get from the child the interface of the protocol identified by its GUID.
692 @param[in] ControllerHandle Controller handle.
693 @param[in] ServiceBindingProtocolGuid Service binding protocol GUID of the
694 service to be created.
695 @param[in] ProtocolGuid GUID of the protocol to be open.
696 @param[out] ChildHandle Address where the handler of the
697 created child is returned. NULL is
698 returned in case of error.
699 @param[out] Interface Address where a pointer to the
700 protocol interface is returned in
703 @return EFI_SUCCESS The child was created and the protocol opened.
704 @return Others Either the creation of the child or the opening
705 of the protocol failed.
709 CreateServiceChildAndOpenProtocol (
710 IN EFI_HANDLE ControllerHandle
,
711 IN EFI_GUID
*ServiceBindingProtocolGuid
,
712 IN EFI_GUID
*ProtocolGuid
,
713 OUT EFI_HANDLE
*ChildHandle
,
720 Status
= NetLibCreateServiceChild (
723 ServiceBindingProtocolGuid
,
726 if (!EFI_ERROR (Status
)) {
727 Status
= gBS
->OpenProtocol (
733 EFI_OPEN_PROTOCOL_GET_PROTOCOL
735 if (EFI_ERROR (Status
)) {
736 NetLibDestroyServiceChild (
739 ServiceBindingProtocolGuid
,
750 Close the protocol identified by its GUID on the child handle of the service
751 identified by its service binding protocol GUID, then destroy the child
754 @param[in] ControllerHandle Controller handle.
755 @param[in] ServiceBindingProtocolGuid Service binding protocol GUID of the
756 service to be destroyed.
757 @param[in] ProtocolGuid GUID of the protocol to be closed.
758 @param[in] ChildHandle Handle of the child to be destroyed.
763 CloseProtocolAndDestroyServiceChild (
764 IN EFI_HANDLE ControllerHandle
,
765 IN EFI_GUID
*ServiceBindingProtocolGuid
,
766 IN EFI_GUID
*ProtocolGuid
,
767 IN EFI_HANDLE ChildHandle
777 NetLibDestroyServiceChild (
780 ServiceBindingProtocolGuid
,
786 Worker function that gets the size in numbers of bytes of a file from a TFTP
787 server before to download the file.
789 @param[in] Mtftp4 MTFTP4 protocol interface
790 @param[in] FilePath Path of the file, ASCII encoded
791 @param[out] FileSize Address where to store the file size in number of
794 @retval EFI_SUCCESS The size of the file was returned.
795 @retval EFI_UNSUPPORTED The server does not support the "tsize" option.
796 @retval Others Error when retrieving the information from the server
797 (see EFI_MTFTP4_PROTOCOL.GetInfo() status codes)
798 or error when parsing the response of the server.
803 IN EFI_MTFTP4_PROTOCOL
*Mtftp4
,
804 IN CONST CHAR8
*FilePath
,
809 EFI_MTFTP4_OPTION ReqOpt
[1];
810 EFI_MTFTP4_PACKET
*Packet
;
812 EFI_MTFTP4_OPTION
*TableOfOptions
;
813 EFI_MTFTP4_OPTION
*Option
;
817 ReqOpt
[0].OptionStr
= (UINT8
*)"tsize";
820 ReqOpt
[0].ValueStr
= OptBuf
;
822 Status
= Mtftp4
->GetInfo (
833 if (EFI_ERROR (Status
)) {
837 Status
= Mtftp4
->ParseOptions (
844 if (EFI_ERROR (Status
)) {
848 Option
= TableOfOptions
;
849 while (OptCnt
!= 0) {
850 if (AsciiStrnCmp ((CHAR8
*)Option
->OptionStr
, "tsize", 5) == 0) {
851 *FileSize
= AsciiStrDecimalToUintn ((CHAR8
*)Option
->ValueStr
);
857 FreePool (TableOfOptions
);
860 Status
= EFI_UNSUPPORTED
;
869 Worker function that download the data of a file from a TFTP server given
870 the path of the file and its size.
872 @param[in] Mtftp4 MTFTP4 protocol interface
873 @param[in] FilePath Path of the file, Unicode encoded
874 @param[in] AsciiFilePath Path of the file, ASCII encoded
875 @param[in] FileSize Size of the file in number of bytes
876 @param[in] BlockSize Value of the TFTP blksize option
877 @param[out] Data Address where to store the address of the buffer
878 where the data of the file were downloaded in
881 @retval EFI_SUCCESS The file was downloaded.
882 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
883 @retval Others The downloading of the file from the server failed
884 (see EFI_MTFTP4_PROTOCOL.ReadFile() status codes).
890 IN EFI_MTFTP4_PROTOCOL
*Mtftp4
,
891 IN CONST CHAR16
*FilePath
,
892 IN CONST CHAR8
*AsciiFilePath
,
899 EFI_PHYSICAL_ADDRESS PagesAddress
;
901 DOWNLOAD_CONTEXT
*TftpContext
;
902 EFI_MTFTP4_TOKEN Mtftp4Token
;
903 EFI_MTFTP4_OPTION ReqOpt
;
906 // Downloaded file can be large. BS.AllocatePages() is more faster
907 // than AllocatePool() and avoid fragmentation.
908 // The downloaded file could be an EFI application. Marking the
909 // allocated page as EfiBootServicesCode would allow to execute a
910 // potential downloaded EFI application.
911 Status
= gBS
->AllocatePages (
914 EFI_SIZE_TO_PAGES (FileSize
),
917 if (EFI_ERROR (Status
)) {
921 Buffer
= (VOID
*)(UINTN
)PagesAddress
;
922 TftpContext
= AllocatePool (sizeof (DOWNLOAD_CONTEXT
));
923 if (TftpContext
== NULL
) {
924 Status
= EFI_OUT_OF_RESOURCES
;
927 TftpContext
->FileSize
= FileSize
;
928 TftpContext
->DownloadedNbOfBytes
= 0;
929 TftpContext
->LastReportedNbOfBytes
= 0;
931 ZeroMem (&Mtftp4Token
, sizeof (EFI_MTFTP4_TOKEN
));
932 Mtftp4Token
.Filename
= (UINT8
*)AsciiFilePath
;
933 Mtftp4Token
.BufferSize
= FileSize
;
934 Mtftp4Token
.Buffer
= Buffer
;
935 Mtftp4Token
.CheckPacket
= CheckPacket
;
936 Mtftp4Token
.Context
= (VOID
*)TftpContext
;
937 if (BlockSize
!= MTFTP_DEFAULT_BLKSIZE
) {
938 ReqOpt
.OptionStr
= (UINT8
*) "blksize";
939 AsciiSPrint ((CHAR8
*)OptBuf
, sizeof (OptBuf
), "%d", BlockSize
);
940 ReqOpt
.ValueStr
= OptBuf
;
942 Mtftp4Token
.OptionCount
= 1;
943 Mtftp4Token
.OptionList
= &ReqOpt
;
947 -1, -1, NULL
, STRING_TOKEN (STR_TFTP_DOWNLOADING
),
948 gShellTftpHiiHandle
, FilePath
951 Status
= Mtftp4
->ReadFile (Mtftp4
, &Mtftp4Token
);
953 -1, -1, NULL
, STRING_TOKEN (STR_GEN_CRLF
),
959 if (TftpContext
== NULL
) {
960 FreePool (TftpContext
);
963 if (EFI_ERROR (Status
)) {
964 gBS
->FreePages (PagesAddress
, EFI_SIZE_TO_PAGES (FileSize
));
974 Update the progress of a file download
975 This procedure is called each time a new TFTP packet is received.
977 @param[in] This MTFTP4 protocol interface
978 @param[in] Token Parameters for the download of the file
979 @param[in] PacketLen Length of the packet
980 @param[in] Packet Address of the packet
982 @retval EFI_SUCCESS All packets are accepted.
989 IN EFI_MTFTP4_PROTOCOL
*This
,
990 IN EFI_MTFTP4_TOKEN
*Token
,
992 IN EFI_MTFTP4_PACKET
*Packet
995 DOWNLOAD_CONTEXT
*Context
;
996 CHAR16 Progress
[TFTP_PROGRESS_MESSAGE_SIZE
];
1003 if ((NTOHS (Packet
->OpCode
)) != EFI_MTFTP4_OPCODE_DATA
) {
1007 Context
= (DOWNLOAD_CONTEXT
*)Token
->Context
;
1008 if (Context
->DownloadedNbOfBytes
== 0) {
1009 ShellPrintEx (-1, -1, L
"%s 0 Kb", mTftpProgressFrame
);
1013 // The data in the packet are prepended with two UINT16 :
1014 // . OpCode = EFI_MTFTP4_OPCODE_DATA
1015 // . Block = the number of this block of data
1017 Context
->DownloadedNbOfBytes
+= PacketLen
- sizeof (Packet
->OpCode
)
1018 - sizeof (Packet
->Data
.Block
);
1019 NbOfKb
= Context
->DownloadedNbOfBytes
/ 1024;
1021 Progress
[0] = L
'\0';
1022 LastStep
= (Context
->LastReportedNbOfBytes
* TFTP_PROGRESS_SLIDER_STEPS
) / Context
->FileSize
;
1023 Step
= (Context
->DownloadedNbOfBytes
* TFTP_PROGRESS_SLIDER_STEPS
) / Context
->FileSize
;
1025 if (Step
<= LastStep
) {
1029 ShellPrintEx (-1, -1, L
"%s", mTftpProgressDelete
);
1031 Status
= StrCpyS (Progress
, TFTP_PROGRESS_MESSAGE_SIZE
, mTftpProgressFrame
);
1032 if (EFI_ERROR(Status
)) {
1035 for (Index
= 1; Index
< Step
; Index
++) {
1036 Progress
[Index
] = L
'=';
1038 Progress
[Step
] = L
'>';
1041 Progress
+ (sizeof (mTftpProgressFrame
) / sizeof (CHAR16
)) - 1,
1042 sizeof (Progress
) - sizeof (mTftpProgressFrame
),
1046 Context
->LastReportedNbOfBytes
= Context
->DownloadedNbOfBytes
;
1048 ShellPrintEx (-1, -1, L
"%s", Progress
);