2 The implementation for the 'tftp' Shell command.
4 Copyright (c) 2015, ARM Ltd. All rights reserved.<BR>
6 This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php.
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
15 #include "UefiShellTftpCommandLib.h"
18 Constant strings and definitions related to the message indicating the amount of
19 progress in the dowloading of a TFTP file.
22 // Frame for the progression slider
23 STATIC CONST CHAR16 mTftpProgressFrame
[] = L
"[ ]";
25 // Number of steps in the progression slider
26 #define TFTP_PROGRESS_SLIDER_STEPS ((sizeof (mTftpProgressFrame) / sizeof (CHAR16)) - 3)
28 // Size in number of characters plus one (final zero) of the message to
29 // indicate the progress of a TFTP download. The format is "[(progress slider:
30 // 40 characters)] (nb of KBytes downloaded so far: 7 characters) Kb". There
31 // are thus the number of characters in mTftpProgressFrame[] plus 11 characters
32 // (2 // spaces, "Kb" and seven characters for the number of KBytes).
33 #define TFTP_PROGRESS_MESSAGE_SIZE ((sizeof (mTftpProgressFrame) / sizeof (CHAR16)) + 12)
35 // String to delete the TFTP progress message to be able to update it :
36 // (TFTP_PROGRESS_MESSAGE_SIZE-1) '\b'
37 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";
40 Check and convert the UINT16 option values of the 'tftp' command
42 @param[in] ValueStr Value as an Unicode encoded string
43 @param[out] Value UINT16 value
45 @return TRUE The value was returned.
46 @return FALSE A parsing error occured.
51 IN CONST CHAR16
*ValueStr
,
56 Get the name of the NIC.
58 @param[in] ControllerHandle The network physical device handle.
59 @param[in] NicNumber The network physical device number.
60 @param[out] NicName Address where to store the NIC name.
61 The memory area has to be at least
62 IP4_NIC_NAME_LENGTH bytes wide.
64 @return EFI_SUCCESS The name of the NIC was returned.
65 @return Others The creation of the child for the Managed
66 Network Service failed or the opening of
67 the Managed Network Protocol failed or
68 the operational parameters for the
69 Managed Network Protocol could not be
75 IN EFI_HANDLE ControllerHandle
,
81 Create a child for the service identified by its service binding protocol GUID
82 and get from the child the interface of the protocol identified by its GUID.
84 @param[in] ControllerHandle Controller handle.
85 @param[in] ServiceBindingProtocolGuid Service binding protocol GUID of the
86 service to be created.
87 @param[in] ProtocolGuid GUID of the protocol to be open.
88 @param[out] ChildHandle Address where the handler of the
89 created child is returned. NULL is
90 returned in case of error.
91 @param[out] Interface Address where a pointer to the
92 protocol interface is returned in
95 @return EFI_SUCCESS The child was created and the protocol opened.
96 @return Others Either the creation of the child or the opening
97 of the protocol failed.
101 CreateServiceChildAndOpenProtocol (
102 IN EFI_HANDLE ControllerHandle
,
103 IN EFI_GUID
*ServiceBindingProtocolGuid
,
104 IN EFI_GUID
*ProtocolGuid
,
105 OUT EFI_HANDLE
*ChildHandle
,
110 Close the protocol identified by its GUID on the child handle of the service
111 identified by its service binding protocol GUID, then destroy the child
114 @param[in] ControllerHandle Controller handle.
115 @param[in] ServiceBindingProtocolGuid Service binding protocol GUID of the
116 service to be destroyed.
117 @param[in] ProtocolGuid GUID of the protocol to be closed.
118 @param[in] ChildHandle Handle of the child to be destroyed.
123 CloseProtocolAndDestroyServiceChild (
124 IN EFI_HANDLE ControllerHandle
,
125 IN EFI_GUID
*ServiceBindingProtocolGuid
,
126 IN EFI_GUID
*ProtocolGuid
,
127 IN EFI_HANDLE ChildHandle
131 Worker function that gets the size in numbers of bytes of a file from a TFTP
132 server before to download the file.
134 @param[in] Mtftp4 MTFTP4 protocol interface
135 @param[in] FilePath Path of the file, ASCII encoded
136 @param[out] FileSize Address where to store the file size in number of
139 @retval EFI_SUCCESS The size of the file was returned.
140 @retval EFI_UNSUPPORTED The server does not support the "tsize" option.
141 @retval Others Error when retrieving the information from the server
142 (see EFI_MTFTP4_PROTOCOL.GetInfo() status codes)
143 or error when parsing the response of the server.
148 IN EFI_MTFTP4_PROTOCOL
*Mtftp4
,
149 IN CONST CHAR8
*FilePath
,
154 Worker function that download the data of a file from a TFTP server given
155 the path of the file and its size.
157 @param[in] Mtftp4 MTFTP4 protocol interface
158 @param[in] FilePath Path of the file, Unicode encoded
159 @param[in] AsciiFilePath Path of the file, ASCII encoded
160 @param[in] FileSize Size of the file in number of bytes
161 @param[out] Data Address where to store the address of the buffer
162 where the data of the file were downloaded in
165 @retval EFI_SUCCESS The file was downloaded.
166 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
167 @retval Others The downloading of the file from the server failed
168 (see EFI_MTFTP4_PROTOCOL.ReadFile() status codes).
174 IN EFI_MTFTP4_PROTOCOL
*Mtftp4
,
175 IN CONST CHAR16
*FilePath
,
176 IN CONST CHAR8
*AsciiFilePath
,
182 Update the progress of a file download
183 This procedure is called each time a new TFTP packet is received.
185 @param[in] This MTFTP4 protocol interface
186 @param[in] Token Parameters for the download of the file
187 @param[in] PacketLen Length of the packet
188 @param[in] Packet Address of the packet
190 @retval EFI_SUCCESS All packets are accepted.
196 IN EFI_MTFTP4_PROTOCOL
*This
,
197 IN EFI_MTFTP4_TOKEN
*Token
,
199 IN EFI_MTFTP4_PACKET
*Packet
202 EFI_MTFTP4_CONFIG_DATA DefaultMtftp4ConfigData
= {
203 TRUE
, // Use default setting
204 { { 0, 0, 0, 0 } }, // StationIp - Not relevant as UseDefaultSetting=TRUE
205 { { 0, 0, 0, 0 } }, // SubnetMask - Not relevant as UseDefaultSetting=TRUE
206 0, // LocalPort - Automatically assigned port number.
207 { { 0, 0, 0, 0 } }, // GatewayIp - Not relevant as UseDefaultSetting=TRUE
208 { { 0, 0, 0, 0 } }, // ServerIp - Not known yet
209 69, // InitialServerPort - Standard TFTP server port
210 6, // TryCount - Max number of retransmissions.
211 4 // TimeoutValue - Retransmission timeout in seconds.
214 STATIC CONST SHELL_PARAM_ITEM ParamList
[] = {
224 Function for 'tftp' command.
226 @param[in] ImageHandle Handle to the Image (NULL if Internal).
227 @param[in] SystemTable Pointer to the System Table (NULL if Internal).
229 @return SHELL_SUCCESS The 'tftp' command completed successfully.
230 @return SHELL_ABORTED The Shell Library initialization failed.
231 @return SHELL_INVALID_PARAMETER At least one of the command's arguments is
233 @return SHELL_OUT_OF_RESOURCES A memory allocation failed.
234 @return SHELL_NOT_FOUND Network Interface Card not found or server
240 ShellCommandRunTftp (
241 IN EFI_HANDLE ImageHandle
,
242 IN EFI_SYSTEM_TABLE
*SystemTable
245 SHELL_STATUS ShellStatus
;
247 LIST_ENTRY
*CheckPackage
;
248 CHAR16
*ProblemParam
;
250 CONST CHAR16
*UserNicName
;
252 CONST CHAR16
*ValueStr
;
253 CONST CHAR16
*RemoteFilePath
;
254 CHAR8
*AsciiRemoteFilePath
;
255 CONST CHAR16
*Walker
;
256 CONST CHAR16
*LocalFilePath
;
257 EFI_MTFTP4_CONFIG_DATA Mtftp4ConfigData
;
261 CHAR16 NicName
[IP4_NIC_NAME_LENGTH
];
262 EFI_HANDLE ControllerHandle
;
263 EFI_HANDLE Mtftp4ChildHandle
;
264 EFI_MTFTP4_PROTOCOL
*Mtftp4
;
267 SHELL_FILE_HANDLE FileHandle
;
269 ShellStatus
= SHELL_INVALID_PARAMETER
;
272 AsciiRemoteFilePath
= NULL
;
276 // Initialize the Shell library (we must be in non-auto-init...)
278 Status
= ShellInitialize ();
279 if (EFI_ERROR (Status
)) {
280 ASSERT_EFI_ERROR (Status
);
281 return SHELL_ABORTED
;
285 // Parse the command line.
287 Status
= ShellCommandLineParse (ParamList
, &CheckPackage
, &ProblemParam
, TRUE
);
288 if (EFI_ERROR (Status
)) {
289 if ((Status
== EFI_VOLUME_CORRUPTED
) &&
290 (ProblemParam
!= NULL
) ) {
292 -1, -1, NULL
, STRING_TOKEN (STR_GEN_PROBLEM
), gShellTftpHiiHandle
,
293 L
"tftp", ProblemParam
295 FreePool (ProblemParam
);
303 // Check the number of parameters
305 ParamCount
= ShellCommandLineGetCount (CheckPackage
);
306 if (ParamCount
> 4) {
308 -1, -1, NULL
, STRING_TOKEN (STR_GEN_TOO_MANY
),
309 gShellTftpHiiHandle
, L
"tftp"
313 if (ParamCount
< 3) {
315 -1, -1, NULL
, STRING_TOKEN (STR_GEN_TOO_FEW
),
316 gShellTftpHiiHandle
, L
"tftp"
321 Mtftp4ConfigData
= DefaultMtftp4ConfigData
;
324 // Check the host IPv4 address
326 ValueStr
= ShellCommandLineGetRawValue (CheckPackage
, 1);
327 Status
= NetLibStrToIp4 (ValueStr
, &Mtftp4ConfigData
.ServerIp
);
328 if (EFI_ERROR (Status
)) {
330 -1, -1, NULL
, STRING_TOKEN (STR_GEN_PARAM_INV
),
331 gShellTftpHiiHandle
, L
"tftp", ValueStr
336 RemoteFilePath
= ShellCommandLineGetRawValue (CheckPackage
, 2);
337 AsciiRemoteFilePath
= AllocatePool (
338 (StrLen (RemoteFilePath
) + 1) * sizeof (CHAR8
)
340 if (AsciiRemoteFilePath
== NULL
) {
341 ShellStatus
= SHELL_OUT_OF_RESOURCES
;
344 UnicodeStrToAsciiStr (RemoteFilePath
, AsciiRemoteFilePath
);
346 if (ParamCount
== 4) {
347 LocalFilePath
= ShellCommandLineGetRawValue (CheckPackage
, 3);
349 Walker
= RemoteFilePath
+ StrLen (RemoteFilePath
);
350 while ((--Walker
) >= RemoteFilePath
) {
351 if ((*Walker
== L
'\\') ||
352 (*Walker
== L
'/' ) ) {
356 LocalFilePath
= Walker
+ 1;
360 // Get the name of the Network Interface Card to be used if any.
362 UserNicName
= ShellCommandLineGetValue (CheckPackage
, L
"-i");
364 ValueStr
= ShellCommandLineGetValue (CheckPackage
, L
"-l");
365 if (ValueStr
!= NULL
) {
366 if (!StringToUint16 (ValueStr
, &Mtftp4ConfigData
.LocalPort
)) {
371 ValueStr
= ShellCommandLineGetValue (CheckPackage
, L
"-r");
372 if (ValueStr
!= NULL
) {
373 if (!StringToUint16 (ValueStr
, &Mtftp4ConfigData
.InitialServerPort
)) {
378 ValueStr
= ShellCommandLineGetValue (CheckPackage
, L
"-c");
379 if (ValueStr
!= NULL
) {
380 if (!StringToUint16 (ValueStr
, &Mtftp4ConfigData
.TryCount
)) {
385 ValueStr
= ShellCommandLineGetValue (CheckPackage
, L
"-t");
386 if (ValueStr
!= NULL
) {
387 if (!StringToUint16 (ValueStr
, &Mtftp4ConfigData
.TimeoutValue
)) {
390 if (Mtftp4ConfigData
.TimeoutValue
== 0) {
392 -1, -1, NULL
, STRING_TOKEN (STR_GEN_PARAM_INV
),
393 gShellTftpHiiHandle
, L
"tftp", ValueStr
400 // Locate all MTFTP4 Service Binding protocols
402 ShellStatus
= SHELL_NOT_FOUND
;
403 Status
= gBS
->LocateHandleBuffer (
405 &gEfiManagedNetworkServiceBindingProtocolGuid
,
410 if (EFI_ERROR (Status
) || (HandleCount
== 0)) {
412 -1, -1, NULL
, STRING_TOKEN (STR_TFTP_ERR_NO_NIC
),
419 (NicNumber
< HandleCount
) && (ShellStatus
!= SHELL_SUCCESS
);
421 ControllerHandle
= Handles
[NicNumber
];
424 Status
= GetNicName (ControllerHandle
, NicNumber
, NicName
);
425 if (EFI_ERROR (Status
)) {
427 -1, -1, NULL
, STRING_TOKEN (STR_TFTP_ERR_NIC_NAME
),
428 gShellTftpHiiHandle
, NicNumber
, Status
433 if (UserNicName
!= NULL
) {
434 if (StrCmp (NicName
, UserNicName
) != 0) {
440 Status
= CreateServiceChildAndOpenProtocol (
442 &gEfiMtftp4ServiceBindingProtocolGuid
,
443 &gEfiMtftp4ProtocolGuid
,
447 if (EFI_ERROR (Status
)) {
449 -1, -1, NULL
, STRING_TOKEN (STR_TFTP_ERR_OPEN_PROTOCOL
),
450 gShellTftpHiiHandle
, NicName
, Status
455 Status
= Mtftp4
->Configure (Mtftp4
, &Mtftp4ConfigData
);
456 if (EFI_ERROR (Status
)) {
458 -1, -1, NULL
, STRING_TOKEN (STR_TFTP_ERR_CONFIGURE
),
459 gShellTftpHiiHandle
, NicName
, Status
464 Status
= GetFileSize (Mtftp4
, AsciiRemoteFilePath
, &FileSize
);
465 if (EFI_ERROR (Status
)) {
467 -1, -1, NULL
, STRING_TOKEN (STR_TFTP_ERR_FILE_SIZE
),
468 gShellTftpHiiHandle
, RemoteFilePath
, NicName
, Status
473 Status
= DownloadFile (Mtftp4
, RemoteFilePath
, AsciiRemoteFilePath
, FileSize
, &Data
);
474 if (EFI_ERROR (Status
)) {
476 -1, -1, NULL
, STRING_TOKEN (STR_TFTP_ERR_DOWNLOAD
),
477 gShellTftpHiiHandle
, RemoteFilePath
, NicName
, Status
482 if (!EFI_ERROR (ShellFileExists (LocalFilePath
))) {
483 ShellDeleteFileByName (LocalFilePath
);
486 Status
= ShellOpenFileByName (
489 EFI_FILE_MODE_CREATE
|
490 EFI_FILE_MODE_WRITE
|
494 if (EFI_ERROR (Status
)) {
496 -1, -1, NULL
, STRING_TOKEN (STR_GEN_FILE_OPEN_FAIL
),
497 gShellTftpHiiHandle
, L
"tftp", LocalFilePath
502 Status
= ShellWriteFile (FileHandle
, &FileSize
, Data
);
503 if (!EFI_ERROR (Status
)) {
504 ShellStatus
= SHELL_SUCCESS
;
507 -1, -1, NULL
, STRING_TOKEN (STR_TFTP_ERR_WRITE
),
508 gShellTftpHiiHandle
, LocalFilePath
, Status
511 ShellCloseFile (&FileHandle
);
516 gBS
->FreePages ((EFI_PHYSICAL_ADDRESS
)(UINTN
)Data
, EFI_SIZE_TO_PAGES (FileSize
));
519 CloseProtocolAndDestroyServiceChild (
521 &gEfiMtftp4ServiceBindingProtocolGuid
,
522 &gEfiMtftp4ProtocolGuid
,
527 if ((UserNicName
!= NULL
) && (!NicFound
)) {
529 -1, -1, NULL
, STRING_TOKEN (STR_TFTP_ERR_NIC_NOT_FOUND
),
530 gShellTftpHiiHandle
, UserNicName
536 ShellCommandLineFreeVarList (CheckPackage
);
537 if (AsciiRemoteFilePath
!= NULL
) {
538 FreePool (AsciiRemoteFilePath
);
540 if (Handles
!= NULL
) {
548 Check and convert the UINT16 option values of the 'tftp' command
550 @param[in] ValueStr Value as an Unicode encoded string
551 @param[out] Value UINT16 value
553 @return TRUE The value was returned.
554 @return FALSE A parsing error occured.
559 IN CONST CHAR16
*ValueStr
,
565 Val
= ShellStrToUintn (ValueStr
);
566 if (Val
> MAX_UINT16
) {
568 -1, -1, NULL
, STRING_TOKEN (STR_GEN_PARAM_INV
),
569 gShellTftpHiiHandle
, L
"tftp", ValueStr
579 Get the name of the NIC.
581 @param[in] ControllerHandle The network physical device handle.
582 @param[in] NicNumber The network physical device number.
583 @param[out] NicName Address where to store the NIC name.
584 The memory area has to be at least
585 IP4_NIC_NAME_LENGTH bytes wide.
587 @return EFI_SUCCESS The name of the NIC was returned.
588 @return Others The creation of the child for the Managed
589 Network Service failed or the opening of
590 the Managed Network Protocol failed or
591 the operational parameters for the
592 Managed Network Protocol could not be
598 IN EFI_HANDLE ControllerHandle
,
604 EFI_HANDLE MnpHandle
;
605 EFI_MANAGED_NETWORK_PROTOCOL
*Mnp
;
606 EFI_SIMPLE_NETWORK_MODE SnpMode
;
608 Status
= CreateServiceChildAndOpenProtocol (
610 &gEfiManagedNetworkServiceBindingProtocolGuid
,
611 &gEfiManagedNetworkProtocolGuid
,
615 if (EFI_ERROR (Status
)) {
619 Status
= Mnp
->GetModeData (Mnp
, NULL
, &SnpMode
);
620 if (EFI_ERROR (Status
) && (Status
!= EFI_NOT_STARTED
)) {
627 SnpMode
.IfType
== NET_IFTYPE_ETHERNET
?
633 Status
= EFI_SUCCESS
;
637 if (MnpHandle
!= NULL
) {
638 CloseProtocolAndDestroyServiceChild (
640 &gEfiManagedNetworkServiceBindingProtocolGuid
,
641 &gEfiManagedNetworkProtocolGuid
,
650 Create a child for the service identified by its service binding protocol GUID
651 and get from the child the interface of the protocol identified by its GUID.
653 @param[in] ControllerHandle Controller handle.
654 @param[in] ServiceBindingProtocolGuid Service binding protocol GUID of the
655 service to be created.
656 @param[in] ProtocolGuid GUID of the protocol to be open.
657 @param[out] ChildHandle Address where the handler of the
658 created child is returned. NULL is
659 returned in case of error.
660 @param[out] Interface Address where a pointer to the
661 protocol interface is returned in
664 @return EFI_SUCCESS The child was created and the protocol opened.
665 @return Others Either the creation of the child or the opening
666 of the protocol failed.
670 CreateServiceChildAndOpenProtocol (
671 IN EFI_HANDLE ControllerHandle
,
672 IN EFI_GUID
*ServiceBindingProtocolGuid
,
673 IN EFI_GUID
*ProtocolGuid
,
674 OUT EFI_HANDLE
*ChildHandle
,
681 Status
= NetLibCreateServiceChild (
684 ServiceBindingProtocolGuid
,
687 if (!EFI_ERROR (Status
)) {
688 Status
= gBS
->OpenProtocol (
694 EFI_OPEN_PROTOCOL_GET_PROTOCOL
696 if (EFI_ERROR (Status
)) {
697 NetLibDestroyServiceChild (
700 ServiceBindingProtocolGuid
,
711 Close the protocol identified by its GUID on the child handle of the service
712 identified by its service binding protocol GUID, then destroy the child
715 @param[in] ControllerHandle Controller handle.
716 @param[in] ServiceBindingProtocolGuid Service binding protocol GUID of the
717 service to be destroyed.
718 @param[in] ProtocolGuid GUID of the protocol to be closed.
719 @param[in] ChildHandle Handle of the child to be destroyed.
724 CloseProtocolAndDestroyServiceChild (
725 IN EFI_HANDLE ControllerHandle
,
726 IN EFI_GUID
*ServiceBindingProtocolGuid
,
727 IN EFI_GUID
*ProtocolGuid
,
728 IN EFI_HANDLE ChildHandle
738 NetLibDestroyServiceChild (
741 ServiceBindingProtocolGuid
,
747 Worker function that gets the size in numbers of bytes of a file from a TFTP
748 server before to download the file.
750 @param[in] Mtftp4 MTFTP4 protocol interface
751 @param[in] FilePath Path of the file, ASCII encoded
752 @param[out] FileSize Address where to store the file size in number of
755 @retval EFI_SUCCESS The size of the file was returned.
756 @retval EFI_UNSUPPORTED The server does not support the "tsize" option.
757 @retval Others Error when retrieving the information from the server
758 (see EFI_MTFTP4_PROTOCOL.GetInfo() status codes)
759 or error when parsing the response of the server.
764 IN EFI_MTFTP4_PROTOCOL
*Mtftp4
,
765 IN CONST CHAR8
*FilePath
,
770 EFI_MTFTP4_OPTION ReqOpt
[1];
771 EFI_MTFTP4_PACKET
*Packet
;
773 EFI_MTFTP4_OPTION
*TableOfOptions
;
774 EFI_MTFTP4_OPTION
*Option
;
778 ReqOpt
[0].OptionStr
= (UINT8
*)"tsize";
781 ReqOpt
[0].ValueStr
= OptBuf
;
783 Status
= Mtftp4
->GetInfo (
794 if (EFI_ERROR (Status
)) {
798 Status
= Mtftp4
->ParseOptions (
805 if (EFI_ERROR (Status
)) {
809 Option
= TableOfOptions
;
810 while (OptCnt
!= 0) {
811 if (AsciiStrnCmp ((CHAR8
*)Option
->OptionStr
, "tsize", 5) == 0) {
812 *FileSize
= AsciiStrDecimalToUintn ((CHAR8
*)Option
->ValueStr
);
818 FreePool (TableOfOptions
);
821 Status
= EFI_UNSUPPORTED
;
830 Worker function that download the data of a file from a TFTP server given
831 the path of the file and its size.
833 @param[in] Mtftp4 MTFTP4 protocol interface
834 @param[in] FilePath Path of the file, Unicode encoded
835 @param[in] AsciiFilePath Path of the file, ASCII encoded
836 @param[in] FileSize Size of the file in number of bytes
837 @param[out] Data Address where to store the address of the buffer
838 where the data of the file were downloaded in
841 @retval EFI_SUCCESS The file was downloaded.
842 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
843 @retval Others The downloading of the file from the server failed
844 (see EFI_MTFTP4_PROTOCOL.ReadFile() status codes).
850 IN EFI_MTFTP4_PROTOCOL
*Mtftp4
,
851 IN CONST CHAR16
*FilePath
,
852 IN CONST CHAR8
*AsciiFilePath
,
858 EFI_PHYSICAL_ADDRESS PagesAddress
;
860 DOWNLOAD_CONTEXT
*TftpContext
;
861 EFI_MTFTP4_TOKEN Mtftp4Token
;
863 // Downloaded file can be large. BS.AllocatePages() is more faster
864 // than AllocatePool() and avoid fragmentation.
865 // The downloaded file could be an EFI application. Marking the
866 // allocated page as EfiBootServicesCode would allow to execute a
867 // potential downloaded EFI application.
868 Status
= gBS
->AllocatePages (
871 EFI_SIZE_TO_PAGES (FileSize
),
874 if (EFI_ERROR (Status
)) {
878 Buffer
= (VOID
*)(UINTN
)PagesAddress
;
879 TftpContext
= AllocatePool (sizeof (DOWNLOAD_CONTEXT
));
880 if (TftpContext
== NULL
) {
881 Status
= EFI_OUT_OF_RESOURCES
;
884 TftpContext
->FileSize
= FileSize
;
885 TftpContext
->DownloadedNbOfBytes
= 0;
886 TftpContext
->LastReportedNbOfBytes
= 0;
888 ZeroMem (&Mtftp4Token
, sizeof (EFI_MTFTP4_TOKEN
));
889 Mtftp4Token
.Filename
= (UINT8
*)AsciiFilePath
;
890 Mtftp4Token
.BufferSize
= FileSize
;
891 Mtftp4Token
.Buffer
= Buffer
;
892 Mtftp4Token
.CheckPacket
= CheckPacket
;
893 Mtftp4Token
.Context
= (VOID
*)TftpContext
;
896 -1, -1, NULL
, STRING_TOKEN (STR_TFTP_DOWNLOADING
),
897 gShellTftpHiiHandle
, FilePath
900 Status
= Mtftp4
->ReadFile (Mtftp4
, &Mtftp4Token
);
902 -1, -1, NULL
, STRING_TOKEN (STR_GEN_CRLF
),
908 if (TftpContext
== NULL
) {
909 FreePool (TftpContext
);
912 if (EFI_ERROR (Status
)) {
913 gBS
->FreePages (PagesAddress
, EFI_SIZE_TO_PAGES (FileSize
));
923 Update the progress of a file download
924 This procedure is called each time a new TFTP packet is received.
926 @param[in] This MTFTP4 protocol interface
927 @param[in] Token Parameters for the download of the file
928 @param[in] PacketLen Length of the packet
929 @param[in] Packet Address of the packet
931 @retval EFI_SUCCESS All packets are accepted.
937 IN EFI_MTFTP4_PROTOCOL
*This
,
938 IN EFI_MTFTP4_TOKEN
*Token
,
940 IN EFI_MTFTP4_PACKET
*Packet
943 DOWNLOAD_CONTEXT
*Context
;
944 CHAR16 Progress
[TFTP_PROGRESS_MESSAGE_SIZE
];
950 if ((NTOHS (Packet
->OpCode
)) != EFI_MTFTP4_OPCODE_DATA
) {
954 Context
= (DOWNLOAD_CONTEXT
*)Token
->Context
;
955 if (Context
->DownloadedNbOfBytes
== 0) {
956 ShellPrintEx (-1, -1, L
"%s 0 Kb", mTftpProgressFrame
);
960 // The data in the packet are prepended with two UINT16 :
961 // . OpCode = EFI_MTFTP4_OPCODE_DATA
962 // . Block = the number of this block of data
964 Context
->DownloadedNbOfBytes
+= PacketLen
- sizeof (Packet
->OpCode
)
965 - sizeof (Packet
->Data
.Block
);
966 NbOfKb
= Context
->DownloadedNbOfBytes
/ 1024;
969 LastStep
= (Context
->LastReportedNbOfBytes
* TFTP_PROGRESS_SLIDER_STEPS
) /
971 Step
= (Context
->DownloadedNbOfBytes
* TFTP_PROGRESS_SLIDER_STEPS
) /
973 if (Step
<= LastStep
) {
977 ShellPrintEx (-1, -1, L
"%s", mTftpProgressDelete
);
979 StrCpy (Progress
, mTftpProgressFrame
);
980 for (Index
= 1; Index
< Step
; Index
++) {
981 Progress
[Index
] = L
'=';
983 Progress
[Step
] = L
'>';
986 Progress
+ (sizeof (mTftpProgressFrame
) / sizeof (CHAR16
)) - 1,
987 sizeof (Progress
) - sizeof (mTftpProgressFrame
),
991 Context
->LastReportedNbOfBytes
= Context
->DownloadedNbOfBytes
;
993 ShellPrintEx (-1, -1, L
"%s", Progress
);