2 The implementation for the 'tftp' Shell command.
4 Copyright (c) 2015, ARM Ltd. All rights reserved.<BR>
5 Copyright (c) 2015, 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[out] Data Address where to store the address of the buffer
167 where the data of the file were downloaded in
170 @retval EFI_SUCCESS The file was downloaded.
171 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
172 @retval Others The downloading of the file from the server failed
173 (see EFI_MTFTP4_PROTOCOL.ReadFile() status codes).
179 IN EFI_MTFTP4_PROTOCOL
*Mtftp4
,
180 IN CONST CHAR16
*FilePath
,
181 IN CONST CHAR8
*AsciiFilePath
,
187 Update the progress of a file download
188 This procedure is called each time a new TFTP packet is received.
190 @param[in] This MTFTP4 protocol interface
191 @param[in] Token Parameters for the download of the file
192 @param[in] PacketLen Length of the packet
193 @param[in] Packet Address of the packet
195 @retval EFI_SUCCESS All packets are accepted.
202 IN EFI_MTFTP4_PROTOCOL
*This
,
203 IN EFI_MTFTP4_TOKEN
*Token
,
205 IN EFI_MTFTP4_PACKET
*Packet
208 EFI_MTFTP4_CONFIG_DATA DefaultMtftp4ConfigData
= {
209 TRUE
, // Use default setting
210 { { 0, 0, 0, 0 } }, // StationIp - Not relevant as UseDefaultSetting=TRUE
211 { { 0, 0, 0, 0 } }, // SubnetMask - Not relevant as UseDefaultSetting=TRUE
212 0, // LocalPort - Automatically assigned port number.
213 { { 0, 0, 0, 0 } }, // GatewayIp - Not relevant as UseDefaultSetting=TRUE
214 { { 0, 0, 0, 0 } }, // ServerIp - Not known yet
215 69, // InitialServerPort - Standard TFTP server port
216 6, // TryCount - Max number of retransmissions.
217 4 // TimeoutValue - Retransmission timeout in seconds.
220 STATIC CONST SHELL_PARAM_ITEM ParamList
[] = {
230 Function for 'tftp' command.
232 @param[in] ImageHandle Handle to the Image (NULL if Internal).
233 @param[in] SystemTable Pointer to the System Table (NULL if Internal).
235 @return SHELL_SUCCESS The 'tftp' command completed successfully.
236 @return SHELL_ABORTED The Shell Library initialization failed.
237 @return SHELL_INVALID_PARAMETER At least one of the command's arguments is
239 @return SHELL_OUT_OF_RESOURCES A memory allocation failed.
240 @return SHELL_NOT_FOUND Network Interface Card not found or server
246 ShellCommandRunTftp (
247 IN EFI_HANDLE ImageHandle
,
248 IN EFI_SYSTEM_TABLE
*SystemTable
251 SHELL_STATUS ShellStatus
;
253 LIST_ENTRY
*CheckPackage
;
254 CHAR16
*ProblemParam
;
256 CONST CHAR16
*UserNicName
;
258 CONST CHAR16
*ValueStr
;
259 CONST CHAR16
*RemoteFilePath
;
260 CHAR8
*AsciiRemoteFilePath
;
261 CONST CHAR16
*Walker
;
262 CONST CHAR16
*LocalFilePath
;
263 EFI_MTFTP4_CONFIG_DATA Mtftp4ConfigData
;
267 CHAR16 NicName
[IP4_CONFIG2_INTERFACE_INFO_NAME_LENGTH
];
268 EFI_HANDLE ControllerHandle
;
269 EFI_HANDLE Mtftp4ChildHandle
;
270 EFI_MTFTP4_PROTOCOL
*Mtftp4
;
273 SHELL_FILE_HANDLE FileHandle
;
275 ShellStatus
= SHELL_INVALID_PARAMETER
;
278 AsciiRemoteFilePath
= NULL
;
283 // Initialize the Shell library (we must be in non-auto-init...)
285 Status
= ShellInitialize ();
286 if (EFI_ERROR (Status
)) {
287 ASSERT_EFI_ERROR (Status
);
288 return SHELL_ABORTED
;
292 // Parse the command line.
294 Status
= ShellCommandLineParse (ParamList
, &CheckPackage
, &ProblemParam
, TRUE
);
295 if (EFI_ERROR (Status
)) {
296 if ((Status
== EFI_VOLUME_CORRUPTED
) &&
297 (ProblemParam
!= NULL
) ) {
299 -1, -1, NULL
, STRING_TOKEN (STR_GEN_PROBLEM
), gShellTftpHiiHandle
,
300 L
"tftp", ProblemParam
302 FreePool (ProblemParam
);
310 // Check the number of parameters
312 ParamCount
= ShellCommandLineGetCount (CheckPackage
);
313 if (ParamCount
> 4) {
315 -1, -1, NULL
, STRING_TOKEN (STR_GEN_TOO_MANY
),
316 gShellTftpHiiHandle
, L
"tftp"
320 if (ParamCount
< 3) {
322 -1, -1, NULL
, STRING_TOKEN (STR_GEN_TOO_FEW
),
323 gShellTftpHiiHandle
, L
"tftp"
328 Mtftp4ConfigData
= DefaultMtftp4ConfigData
;
331 // Check the host IPv4 address
333 ValueStr
= ShellCommandLineGetRawValue (CheckPackage
, 1);
334 Status
= NetLibStrToIp4 (ValueStr
, &Mtftp4ConfigData
.ServerIp
);
335 if (EFI_ERROR (Status
)) {
337 -1, -1, NULL
, STRING_TOKEN (STR_GEN_PARAM_INV
),
338 gShellTftpHiiHandle
, L
"tftp", ValueStr
343 RemoteFilePath
= ShellCommandLineGetRawValue (CheckPackage
, 2);
344 AsciiRemoteFilePath
= AllocatePool (
345 (StrLen (RemoteFilePath
) + 1) * sizeof (CHAR8
)
347 if (AsciiRemoteFilePath
== NULL
) {
348 ShellStatus
= SHELL_OUT_OF_RESOURCES
;
351 UnicodeStrToAsciiStr (RemoteFilePath
, AsciiRemoteFilePath
);
353 if (ParamCount
== 4) {
354 LocalFilePath
= ShellCommandLineGetRawValue (CheckPackage
, 3);
356 Walker
= RemoteFilePath
+ StrLen (RemoteFilePath
);
357 while ((--Walker
) >= RemoteFilePath
) {
358 if ((*Walker
== L
'\\') ||
359 (*Walker
== L
'/' ) ) {
363 LocalFilePath
= Walker
+ 1;
367 // Get the name of the Network Interface Card to be used if any.
369 UserNicName
= ShellCommandLineGetValue (CheckPackage
, L
"-i");
371 ValueStr
= ShellCommandLineGetValue (CheckPackage
, L
"-l");
372 if (ValueStr
!= NULL
) {
373 if (!StringToUint16 (ValueStr
, &Mtftp4ConfigData
.LocalPort
)) {
378 ValueStr
= ShellCommandLineGetValue (CheckPackage
, L
"-r");
379 if (ValueStr
!= NULL
) {
380 if (!StringToUint16 (ValueStr
, &Mtftp4ConfigData
.InitialServerPort
)) {
385 ValueStr
= ShellCommandLineGetValue (CheckPackage
, L
"-c");
386 if (ValueStr
!= NULL
) {
387 if (!StringToUint16 (ValueStr
, &Mtftp4ConfigData
.TryCount
)) {
392 ValueStr
= ShellCommandLineGetValue (CheckPackage
, L
"-t");
393 if (ValueStr
!= NULL
) {
394 if (!StringToUint16 (ValueStr
, &Mtftp4ConfigData
.TimeoutValue
)) {
397 if (Mtftp4ConfigData
.TimeoutValue
== 0) {
399 -1, -1, NULL
, STRING_TOKEN (STR_GEN_PARAM_INV
),
400 gShellTftpHiiHandle
, L
"tftp", ValueStr
407 // Locate all MTFTP4 Service Binding protocols
409 ShellStatus
= SHELL_NOT_FOUND
;
410 Status
= gBS
->LocateHandleBuffer (
412 &gEfiManagedNetworkServiceBindingProtocolGuid
,
417 if (EFI_ERROR (Status
) || (HandleCount
== 0)) {
419 -1, -1, NULL
, STRING_TOKEN (STR_TFTP_ERR_NO_NIC
),
426 (NicNumber
< HandleCount
) && (ShellStatus
!= SHELL_SUCCESS
);
428 ControllerHandle
= Handles
[NicNumber
];
431 Status
= GetNicName (ControllerHandle
, NicNumber
, NicName
);
432 if (EFI_ERROR (Status
)) {
434 -1, -1, NULL
, STRING_TOKEN (STR_TFTP_ERR_NIC_NAME
),
435 gShellTftpHiiHandle
, NicNumber
, Status
440 if (UserNicName
!= NULL
) {
441 if (StrCmp (NicName
, UserNicName
) != 0) {
447 Status
= CreateServiceChildAndOpenProtocol (
449 &gEfiMtftp4ServiceBindingProtocolGuid
,
450 &gEfiMtftp4ProtocolGuid
,
454 if (EFI_ERROR (Status
)) {
456 -1, -1, NULL
, STRING_TOKEN (STR_TFTP_ERR_OPEN_PROTOCOL
),
457 gShellTftpHiiHandle
, NicName
, Status
462 Status
= Mtftp4
->Configure (Mtftp4
, &Mtftp4ConfigData
);
463 if (EFI_ERROR (Status
)) {
465 -1, -1, NULL
, STRING_TOKEN (STR_TFTP_ERR_CONFIGURE
),
466 gShellTftpHiiHandle
, NicName
, Status
471 Status
= GetFileSize (Mtftp4
, AsciiRemoteFilePath
, &FileSize
);
472 if (EFI_ERROR (Status
)) {
474 -1, -1, NULL
, STRING_TOKEN (STR_TFTP_ERR_FILE_SIZE
),
475 gShellTftpHiiHandle
, RemoteFilePath
, NicName
, Status
480 Status
= DownloadFile (Mtftp4
, RemoteFilePath
, AsciiRemoteFilePath
, FileSize
, &Data
);
481 if (EFI_ERROR (Status
)) {
483 -1, -1, NULL
, STRING_TOKEN (STR_TFTP_ERR_DOWNLOAD
),
484 gShellTftpHiiHandle
, RemoteFilePath
, NicName
, Status
489 if (!EFI_ERROR (ShellFileExists (LocalFilePath
))) {
490 ShellDeleteFileByName (LocalFilePath
);
493 Status
= ShellOpenFileByName (
496 EFI_FILE_MODE_CREATE
|
497 EFI_FILE_MODE_WRITE
|
501 if (EFI_ERROR (Status
)) {
503 -1, -1, NULL
, STRING_TOKEN (STR_GEN_FILE_OPEN_FAIL
),
504 gShellTftpHiiHandle
, L
"tftp", LocalFilePath
509 Status
= ShellWriteFile (FileHandle
, &FileSize
, Data
);
510 if (!EFI_ERROR (Status
)) {
511 ShellStatus
= SHELL_SUCCESS
;
514 -1, -1, NULL
, STRING_TOKEN (STR_TFTP_ERR_WRITE
),
515 gShellTftpHiiHandle
, LocalFilePath
, Status
518 ShellCloseFile (&FileHandle
);
523 gBS
->FreePages ((EFI_PHYSICAL_ADDRESS
)(UINTN
)Data
, EFI_SIZE_TO_PAGES (FileSize
));
526 CloseProtocolAndDestroyServiceChild (
528 &gEfiMtftp4ServiceBindingProtocolGuid
,
529 &gEfiMtftp4ProtocolGuid
,
534 if ((UserNicName
!= NULL
) && (!NicFound
)) {
536 -1, -1, NULL
, STRING_TOKEN (STR_TFTP_ERR_NIC_NOT_FOUND
),
537 gShellTftpHiiHandle
, UserNicName
543 ShellCommandLineFreeVarList (CheckPackage
);
544 if (AsciiRemoteFilePath
!= NULL
) {
545 FreePool (AsciiRemoteFilePath
);
547 if (Handles
!= NULL
) {
555 Check and convert the UINT16 option values of the 'tftp' command
557 @param[in] ValueStr Value as an Unicode encoded string
558 @param[out] Value UINT16 value
560 @return TRUE The value was returned.
561 @return FALSE A parsing error occured.
566 IN CONST CHAR16
*ValueStr
,
572 Val
= ShellStrToUintn (ValueStr
);
573 if (Val
> MAX_UINT16
) {
575 -1, -1, NULL
, STRING_TOKEN (STR_GEN_PARAM_INV
),
576 gShellTftpHiiHandle
, L
"tftp", ValueStr
581 *Value
= (UINT16
)Val
;
586 Get the name of the NIC.
588 @param[in] ControllerHandle The network physical device handle.
589 @param[in] NicNumber The network physical device number.
590 @param[out] NicName Address where to store the NIC name.
591 The memory area has to be at least
592 IP4_CONFIG2_INTERFACE_INFO_NAME_LENGTH
595 @return EFI_SUCCESS The name of the NIC was returned.
596 @return Others The creation of the child for the Managed
597 Network Service failed or the opening of
598 the Managed Network Protocol failed or
599 the operational parameters for the
600 Managed Network Protocol could not be
606 IN EFI_HANDLE ControllerHandle
,
612 EFI_HANDLE MnpHandle
;
613 EFI_MANAGED_NETWORK_PROTOCOL
*Mnp
;
614 EFI_SIMPLE_NETWORK_MODE SnpMode
;
616 Status
= CreateServiceChildAndOpenProtocol (
618 &gEfiManagedNetworkServiceBindingProtocolGuid
,
619 &gEfiManagedNetworkProtocolGuid
,
623 if (EFI_ERROR (Status
)) {
627 Status
= Mnp
->GetModeData (Mnp
, NULL
, &SnpMode
);
628 if (EFI_ERROR (Status
) && (Status
!= EFI_NOT_STARTED
)) {
634 IP4_CONFIG2_INTERFACE_INFO_NAME_LENGTH
,
635 SnpMode
.IfType
== NET_IFTYPE_ETHERNET
?
641 Status
= EFI_SUCCESS
;
645 if (MnpHandle
!= NULL
) {
646 CloseProtocolAndDestroyServiceChild (
648 &gEfiManagedNetworkServiceBindingProtocolGuid
,
649 &gEfiManagedNetworkProtocolGuid
,
658 Create a child for the service identified by its service binding protocol GUID
659 and get from the child the interface of the protocol identified by its GUID.
661 @param[in] ControllerHandle Controller handle.
662 @param[in] ServiceBindingProtocolGuid Service binding protocol GUID of the
663 service to be created.
664 @param[in] ProtocolGuid GUID of the protocol to be open.
665 @param[out] ChildHandle Address where the handler of the
666 created child is returned. NULL is
667 returned in case of error.
668 @param[out] Interface Address where a pointer to the
669 protocol interface is returned in
672 @return EFI_SUCCESS The child was created and the protocol opened.
673 @return Others Either the creation of the child or the opening
674 of the protocol failed.
678 CreateServiceChildAndOpenProtocol (
679 IN EFI_HANDLE ControllerHandle
,
680 IN EFI_GUID
*ServiceBindingProtocolGuid
,
681 IN EFI_GUID
*ProtocolGuid
,
682 OUT EFI_HANDLE
*ChildHandle
,
689 Status
= NetLibCreateServiceChild (
692 ServiceBindingProtocolGuid
,
695 if (!EFI_ERROR (Status
)) {
696 Status
= gBS
->OpenProtocol (
702 EFI_OPEN_PROTOCOL_GET_PROTOCOL
704 if (EFI_ERROR (Status
)) {
705 NetLibDestroyServiceChild (
708 ServiceBindingProtocolGuid
,
719 Close the protocol identified by its GUID on the child handle of the service
720 identified by its service binding protocol GUID, then destroy the child
723 @param[in] ControllerHandle Controller handle.
724 @param[in] ServiceBindingProtocolGuid Service binding protocol GUID of the
725 service to be destroyed.
726 @param[in] ProtocolGuid GUID of the protocol to be closed.
727 @param[in] ChildHandle Handle of the child to be destroyed.
732 CloseProtocolAndDestroyServiceChild (
733 IN EFI_HANDLE ControllerHandle
,
734 IN EFI_GUID
*ServiceBindingProtocolGuid
,
735 IN EFI_GUID
*ProtocolGuid
,
736 IN EFI_HANDLE ChildHandle
746 NetLibDestroyServiceChild (
749 ServiceBindingProtocolGuid
,
755 Worker function that gets the size in numbers of bytes of a file from a TFTP
756 server before to download the file.
758 @param[in] Mtftp4 MTFTP4 protocol interface
759 @param[in] FilePath Path of the file, ASCII encoded
760 @param[out] FileSize Address where to store the file size in number of
763 @retval EFI_SUCCESS The size of the file was returned.
764 @retval EFI_UNSUPPORTED The server does not support the "tsize" option.
765 @retval Others Error when retrieving the information from the server
766 (see EFI_MTFTP4_PROTOCOL.GetInfo() status codes)
767 or error when parsing the response of the server.
772 IN EFI_MTFTP4_PROTOCOL
*Mtftp4
,
773 IN CONST CHAR8
*FilePath
,
778 EFI_MTFTP4_OPTION ReqOpt
[1];
779 EFI_MTFTP4_PACKET
*Packet
;
781 EFI_MTFTP4_OPTION
*TableOfOptions
;
782 EFI_MTFTP4_OPTION
*Option
;
786 ReqOpt
[0].OptionStr
= (UINT8
*)"tsize";
789 ReqOpt
[0].ValueStr
= OptBuf
;
791 Status
= Mtftp4
->GetInfo (
802 if (EFI_ERROR (Status
)) {
806 Status
= Mtftp4
->ParseOptions (
813 if (EFI_ERROR (Status
)) {
817 Option
= TableOfOptions
;
818 while (OptCnt
!= 0) {
819 if (AsciiStrnCmp ((CHAR8
*)Option
->OptionStr
, "tsize", 5) == 0) {
820 *FileSize
= AsciiStrDecimalToUintn ((CHAR8
*)Option
->ValueStr
);
826 FreePool (TableOfOptions
);
829 Status
= EFI_UNSUPPORTED
;
838 Worker function that download the data of a file from a TFTP server given
839 the path of the file and its size.
841 @param[in] Mtftp4 MTFTP4 protocol interface
842 @param[in] FilePath Path of the file, Unicode encoded
843 @param[in] AsciiFilePath Path of the file, ASCII encoded
844 @param[in] FileSize Size of the file in number of bytes
845 @param[out] Data Address where to store the address of the buffer
846 where the data of the file were downloaded in
849 @retval EFI_SUCCESS The file was downloaded.
850 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
851 @retval Others The downloading of the file from the server failed
852 (see EFI_MTFTP4_PROTOCOL.ReadFile() status codes).
858 IN EFI_MTFTP4_PROTOCOL
*Mtftp4
,
859 IN CONST CHAR16
*FilePath
,
860 IN CONST CHAR8
*AsciiFilePath
,
866 EFI_PHYSICAL_ADDRESS PagesAddress
;
868 DOWNLOAD_CONTEXT
*TftpContext
;
869 EFI_MTFTP4_TOKEN Mtftp4Token
;
871 // Downloaded file can be large. BS.AllocatePages() is more faster
872 // than AllocatePool() and avoid fragmentation.
873 // The downloaded file could be an EFI application. Marking the
874 // allocated page as EfiBootServicesCode would allow to execute a
875 // potential downloaded EFI application.
876 Status
= gBS
->AllocatePages (
879 EFI_SIZE_TO_PAGES (FileSize
),
882 if (EFI_ERROR (Status
)) {
886 Buffer
= (VOID
*)(UINTN
)PagesAddress
;
887 TftpContext
= AllocatePool (sizeof (DOWNLOAD_CONTEXT
));
888 if (TftpContext
== NULL
) {
889 Status
= EFI_OUT_OF_RESOURCES
;
892 TftpContext
->FileSize
= FileSize
;
893 TftpContext
->DownloadedNbOfBytes
= 0;
894 TftpContext
->LastReportedNbOfBytes
= 0;
896 ZeroMem (&Mtftp4Token
, sizeof (EFI_MTFTP4_TOKEN
));
897 Mtftp4Token
.Filename
= (UINT8
*)AsciiFilePath
;
898 Mtftp4Token
.BufferSize
= FileSize
;
899 Mtftp4Token
.Buffer
= Buffer
;
900 Mtftp4Token
.CheckPacket
= CheckPacket
;
901 Mtftp4Token
.Context
= (VOID
*)TftpContext
;
904 -1, -1, NULL
, STRING_TOKEN (STR_TFTP_DOWNLOADING
),
905 gShellTftpHiiHandle
, FilePath
908 Status
= Mtftp4
->ReadFile (Mtftp4
, &Mtftp4Token
);
910 -1, -1, NULL
, STRING_TOKEN (STR_GEN_CRLF
),
916 if (TftpContext
== NULL
) {
917 FreePool (TftpContext
);
920 if (EFI_ERROR (Status
)) {
921 gBS
->FreePages (PagesAddress
, EFI_SIZE_TO_PAGES (FileSize
));
931 Update the progress of a file download
932 This procedure is called each time a new TFTP packet is received.
934 @param[in] This MTFTP4 protocol interface
935 @param[in] Token Parameters for the download of the file
936 @param[in] PacketLen Length of the packet
937 @param[in] Packet Address of the packet
939 @retval EFI_SUCCESS All packets are accepted.
946 IN EFI_MTFTP4_PROTOCOL
*This
,
947 IN EFI_MTFTP4_TOKEN
*Token
,
949 IN EFI_MTFTP4_PACKET
*Packet
952 DOWNLOAD_CONTEXT
*Context
;
953 CHAR16 Progress
[TFTP_PROGRESS_MESSAGE_SIZE
];
959 if ((NTOHS (Packet
->OpCode
)) != EFI_MTFTP4_OPCODE_DATA
) {
963 Context
= (DOWNLOAD_CONTEXT
*)Token
->Context
;
964 if (Context
->DownloadedNbOfBytes
== 0) {
965 ShellPrintEx (-1, -1, L
"%s 0 Kb", mTftpProgressFrame
);
969 // The data in the packet are prepended with two UINT16 :
970 // . OpCode = EFI_MTFTP4_OPCODE_DATA
971 // . Block = the number of this block of data
973 Context
->DownloadedNbOfBytes
+= PacketLen
- sizeof (Packet
->OpCode
)
974 - sizeof (Packet
->Data
.Block
);
975 NbOfKb
= Context
->DownloadedNbOfBytes
/ 1024;
978 LastStep
= (Context
->LastReportedNbOfBytes
* TFTP_PROGRESS_SLIDER_STEPS
) / Context
->FileSize
;
979 Step
= (Context
->DownloadedNbOfBytes
* TFTP_PROGRESS_SLIDER_STEPS
) / Context
->FileSize
;
981 if (Step
<= LastStep
) {
985 ShellPrintEx (-1, -1, L
"%s", mTftpProgressDelete
);
987 StrCpy (Progress
, mTftpProgressFrame
);
988 for (Index
= 1; Index
< Step
; Index
++) {
989 Progress
[Index
] = L
'=';
991 Progress
[Step
] = L
'>';
994 Progress
+ (sizeof (mTftpProgressFrame
) / sizeof (CHAR16
)) - 1,
995 sizeof (Progress
) - sizeof (mTftpProgressFrame
),
999 Context
->LastReportedNbOfBytes
= Context
->DownloadedNbOfBytes
;
1001 ShellPrintEx (-1, -1, L
"%s", Progress
);