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 Check and convert the UINT16 option values of the 'tftp' command
47 @param[in] ValueStr Value as an Unicode encoded string
48 @param[out] Value UINT16 value
50 @return TRUE The value was returned.
51 @return FALSE A parsing error occured.
56 IN CONST CHAR16
*ValueStr
,
61 Get the name of the NIC.
63 @param[in] ControllerHandle The network physical device handle.
64 @param[in] NicNumber The network physical device number.
65 @param[out] NicName Address where to store the NIC name.
66 The memory area has to be at least
67 IP4_CONFIG2_INTERFACE_INFO_NAME_LENGTH
70 @return EFI_SUCCESS The name of the NIC was returned.
71 @return Others The creation of the child for the Managed
72 Network Service failed or the opening of
73 the Managed Network Protocol failed or
74 the operational parameters for the
75 Managed Network Protocol could not be
81 IN EFI_HANDLE ControllerHandle
,
87 Create a child for the service identified by its service binding protocol GUID
88 and get from the child the interface of the protocol identified by its GUID.
90 @param[in] ControllerHandle Controller handle.
91 @param[in] ServiceBindingProtocolGuid Service binding protocol GUID of the
92 service to be created.
93 @param[in] ProtocolGuid GUID of the protocol to be open.
94 @param[out] ChildHandle Address where the handler of the
95 created child is returned. NULL is
96 returned in case of error.
97 @param[out] Interface Address where a pointer to the
98 protocol interface is returned in
101 @return EFI_SUCCESS The child was created and the protocol opened.
102 @return Others Either the creation of the child or the opening
103 of the protocol failed.
107 CreateServiceChildAndOpenProtocol (
108 IN EFI_HANDLE ControllerHandle
,
109 IN EFI_GUID
*ServiceBindingProtocolGuid
,
110 IN EFI_GUID
*ProtocolGuid
,
111 OUT EFI_HANDLE
*ChildHandle
,
116 Close the protocol identified by its GUID on the child handle of the service
117 identified by its service binding protocol GUID, then destroy the child
120 @param[in] ControllerHandle Controller handle.
121 @param[in] ServiceBindingProtocolGuid Service binding protocol GUID of the
122 service to be destroyed.
123 @param[in] ProtocolGuid GUID of the protocol to be closed.
124 @param[in] ChildHandle Handle of the child to be destroyed.
129 CloseProtocolAndDestroyServiceChild (
130 IN EFI_HANDLE ControllerHandle
,
131 IN EFI_GUID
*ServiceBindingProtocolGuid
,
132 IN EFI_GUID
*ProtocolGuid
,
133 IN EFI_HANDLE ChildHandle
137 Worker function that gets the size in numbers of bytes of a file from a TFTP
138 server before to download the file.
140 @param[in] Mtftp4 MTFTP4 protocol interface
141 @param[in] FilePath Path of the file, ASCII encoded
142 @param[out] FileSize Address where to store the file size in number of
145 @retval EFI_SUCCESS The size of the file was returned.
146 @retval EFI_UNSUPPORTED The server does not support the "tsize" option.
147 @retval Others Error when retrieving the information from the server
148 (see EFI_MTFTP4_PROTOCOL.GetInfo() status codes)
149 or error when parsing the response of the server.
154 IN EFI_MTFTP4_PROTOCOL
*Mtftp4
,
155 IN CONST CHAR8
*FilePath
,
160 Worker function that download the data of a file from a TFTP server given
161 the path of the file and its size.
163 @param[in] Mtftp4 MTFTP4 protocol interface
164 @param[in] FilePath Path of the file, Unicode encoded
165 @param[in] AsciiFilePath Path of the file, ASCII encoded
166 @param[in] FileSize Size of the file in number of bytes
167 @param[in] BlockSize Value of the TFTP blksize option
168 @param[out] Data Address where to store the address of the buffer
169 where the data of the file were downloaded in
172 @retval EFI_SUCCESS The file was downloaded.
173 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
174 @retval Others The downloading of the file from the server failed
175 (see EFI_MTFTP4_PROTOCOL.ReadFile() status codes).
181 IN EFI_MTFTP4_PROTOCOL
*Mtftp4
,
182 IN CONST CHAR16
*FilePath
,
183 IN CONST CHAR8
*AsciiFilePath
,
186 IN UINT16 WindowSize
,
191 Update the progress of a file download
192 This procedure is called each time a new TFTP packet is received.
194 @param[in] This MTFTP4 protocol interface
195 @param[in] Token Parameters for the download of the file
196 @param[in] PacketLen Length of the packet
197 @param[in] Packet Address of the packet
199 @retval EFI_SUCCESS All packets are accepted.
206 IN EFI_MTFTP4_PROTOCOL
*This
,
207 IN EFI_MTFTP4_TOKEN
*Token
,
209 IN EFI_MTFTP4_PACKET
*Packet
212 EFI_MTFTP4_CONFIG_DATA DefaultMtftp4ConfigData
= {
213 TRUE
, // Use default setting
214 { { 0, 0, 0, 0 } }, // StationIp - Not relevant as UseDefaultSetting=TRUE
215 { { 0, 0, 0, 0 } }, // SubnetMask - Not relevant as UseDefaultSetting=TRUE
216 0, // LocalPort - Automatically assigned port number.
217 { { 0, 0, 0, 0 } }, // GatewayIp - Not relevant as UseDefaultSetting=TRUE
218 { { 0, 0, 0, 0 } }, // ServerIp - Not known yet
219 69, // InitialServerPort - Standard TFTP server port
220 6, // TryCount - Max number of retransmissions.
221 4 // TimeoutValue - Retransmission timeout in seconds.
224 STATIC CONST SHELL_PARAM_ITEM ParamList
[] = {
236 /// The default block size (512) of tftp is defined in the RFC1350.
238 #define MTFTP_DEFAULT_BLKSIZE 512
240 /// The valid range of block size option is defined in the RFC2348.
242 #define MTFTP_MIN_BLKSIZE 8
243 #define MTFTP_MAX_BLKSIZE 65464
245 /// The default windowsize (1) of tftp.
247 #define MTFTP_DEFAULT_WINDOWSIZE 1
249 /// The valid range of window size option.
250 /// Note that: RFC 7440 does not mention max window size value, but for the
251 /// stability reason, the value is limited to 64.
253 #define MTFTP_MIN_WINDOWSIZE 1
254 #define MTFTP_MAX_WINDOWSIZE 64
257 Function for 'tftp' command.
259 @param[in] ImageHandle Handle to the Image (NULL if Internal).
260 @param[in] SystemTable Pointer to the System Table (NULL if Internal).
262 @return SHELL_SUCCESS The 'tftp' command completed successfully.
263 @return SHELL_ABORTED The Shell Library initialization failed.
264 @return SHELL_INVALID_PARAMETER At least one of the command's arguments is
266 @return SHELL_OUT_OF_RESOURCES A memory allocation failed.
267 @return SHELL_NOT_FOUND Network Interface Card not found or server
273 IN EFI_HANDLE ImageHandle
,
274 IN EFI_SYSTEM_TABLE
*SystemTable
277 SHELL_STATUS ShellStatus
;
279 LIST_ENTRY
*CheckPackage
;
280 CHAR16
*ProblemParam
;
282 CONST CHAR16
*UserNicName
;
284 CONST CHAR16
*ValueStr
;
285 CONST CHAR16
*RemoteFilePath
;
286 CHAR8
*AsciiRemoteFilePath
;
288 CONST CHAR16
*Walker
;
289 CONST CHAR16
*LocalFilePath
;
290 EFI_MTFTP4_CONFIG_DATA Mtftp4ConfigData
;
294 CHAR16 NicName
[IP4_CONFIG2_INTERFACE_INFO_NAME_LENGTH
];
295 EFI_HANDLE ControllerHandle
;
296 EFI_HANDLE Mtftp4ChildHandle
;
297 EFI_MTFTP4_PROTOCOL
*Mtftp4
;
301 SHELL_FILE_HANDLE FileHandle
;
305 ShellStatus
= SHELL_INVALID_PARAMETER
;
308 AsciiRemoteFilePath
= NULL
;
312 BlockSize
= MTFTP_DEFAULT_BLKSIZE
;
313 WindowSize
= MTFTP_DEFAULT_WINDOWSIZE
;
316 // Initialize the Shell library (we must be in non-auto-init...)
318 Status
= ShellInitialize ();
319 if (EFI_ERROR (Status
)) {
320 ASSERT_EFI_ERROR (Status
);
321 return SHELL_ABORTED
;
325 // Parse the command line.
327 Status
= ShellCommandLineParse (ParamList
, &CheckPackage
, &ProblemParam
, TRUE
);
328 if (EFI_ERROR (Status
)) {
329 if ((Status
== EFI_VOLUME_CORRUPTED
) &&
330 (ProblemParam
!= NULL
) ) {
332 -1, -1, NULL
, STRING_TOKEN (STR_GEN_PROBLEM
), mTftpHiiHandle
,
333 L
"tftp", ProblemParam
335 FreePool (ProblemParam
);
343 // Check the number of parameters
345 ParamCount
= ShellCommandLineGetCount (CheckPackage
);
346 if (ParamCount
> 4) {
348 -1, -1, NULL
, STRING_TOKEN (STR_GEN_TOO_MANY
),
349 mTftpHiiHandle
, L
"tftp"
353 if (ParamCount
< 3) {
355 -1, -1, NULL
, STRING_TOKEN (STR_GEN_TOO_FEW
),
356 mTftpHiiHandle
, L
"tftp"
361 CopyMem (&Mtftp4ConfigData
, &DefaultMtftp4ConfigData
, sizeof (EFI_MTFTP4_CONFIG_DATA
));
364 // Check the host IPv4 address
366 ValueStr
= ShellCommandLineGetRawValue (CheckPackage
, 1);
367 Status
= NetLibStrToIp4 (ValueStr
, &Mtftp4ConfigData
.ServerIp
);
368 if (EFI_ERROR (Status
)) {
370 -1, -1, NULL
, STRING_TOKEN (STR_GEN_PARAM_INV
),
371 mTftpHiiHandle
, L
"tftp", ValueStr
376 RemoteFilePath
= ShellCommandLineGetRawValue (CheckPackage
, 2);
377 ASSERT(RemoteFilePath
!= NULL
);
378 FilePathSize
= StrLen (RemoteFilePath
) + 1;
379 AsciiRemoteFilePath
= AllocatePool (FilePathSize
);
380 if (AsciiRemoteFilePath
== NULL
) {
381 ShellStatus
= SHELL_OUT_OF_RESOURCES
;
384 UnicodeStrToAsciiStrS (RemoteFilePath
, AsciiRemoteFilePath
, FilePathSize
);
386 if (ParamCount
== 4) {
387 LocalFilePath
= ShellCommandLineGetRawValue (CheckPackage
, 3);
389 Walker
= RemoteFilePath
+ StrLen (RemoteFilePath
);
390 while ((--Walker
) >= RemoteFilePath
) {
391 if ((*Walker
== L
'\\') ||
392 (*Walker
== L
'/' ) ) {
396 LocalFilePath
= Walker
+ 1;
400 // Get the name of the Network Interface Card to be used if any.
402 UserNicName
= ShellCommandLineGetValue (CheckPackage
, L
"-i");
404 ValueStr
= ShellCommandLineGetValue (CheckPackage
, L
"-l");
405 if (ValueStr
!= NULL
) {
406 if (!StringToUint16 (ValueStr
, &Mtftp4ConfigData
.LocalPort
)) {
411 ValueStr
= ShellCommandLineGetValue (CheckPackage
, L
"-r");
412 if (ValueStr
!= NULL
) {
413 if (!StringToUint16 (ValueStr
, &Mtftp4ConfigData
.InitialServerPort
)) {
418 ValueStr
= ShellCommandLineGetValue (CheckPackage
, L
"-c");
419 if (ValueStr
!= NULL
) {
420 if (!StringToUint16 (ValueStr
, &Mtftp4ConfigData
.TryCount
)) {
425 ValueStr
= ShellCommandLineGetValue (CheckPackage
, L
"-t");
426 if (ValueStr
!= NULL
) {
427 if (!StringToUint16 (ValueStr
, &Mtftp4ConfigData
.TimeoutValue
)) {
430 if (Mtftp4ConfigData
.TimeoutValue
== 0) {
432 -1, -1, NULL
, STRING_TOKEN (STR_GEN_PARAM_INV
),
433 mTftpHiiHandle
, L
"tftp", ValueStr
439 ValueStr
= ShellCommandLineGetValue (CheckPackage
, L
"-s");
440 if (ValueStr
!= NULL
) {
441 if (!StringToUint16 (ValueStr
, &BlockSize
)) {
444 if (BlockSize
< MTFTP_MIN_BLKSIZE
|| BlockSize
> MTFTP_MAX_BLKSIZE
) {
446 -1, -1, NULL
, STRING_TOKEN (STR_GEN_PARAM_INV
),
447 mTftpHiiHandle
, L
"tftp", ValueStr
453 ValueStr
= ShellCommandLineGetValue (CheckPackage
, L
"-w");
454 if (ValueStr
!= NULL
) {
455 if (!StringToUint16 (ValueStr
, &WindowSize
)) {
458 if (WindowSize
< MTFTP_MIN_WINDOWSIZE
|| WindowSize
> MTFTP_MAX_WINDOWSIZE
) {
460 -1, -1, NULL
, STRING_TOKEN (STR_GEN_PARAM_INV
),
461 mTftpHiiHandle
, L
"tftp", ValueStr
468 // Locate all MTFTP4 Service Binding protocols
470 ShellStatus
= SHELL_NOT_FOUND
;
471 Status
= gBS
->LocateHandleBuffer (
473 &gEfiManagedNetworkServiceBindingProtocolGuid
,
478 if (EFI_ERROR (Status
) || (HandleCount
== 0)) {
480 -1, -1, NULL
, STRING_TOKEN (STR_TFTP_ERR_NO_NIC
),
487 (NicNumber
< HandleCount
) && (ShellStatus
!= SHELL_SUCCESS
);
489 ControllerHandle
= Handles
[NicNumber
];
492 Status
= GetNicName (ControllerHandle
, NicNumber
, NicName
);
493 if (EFI_ERROR (Status
)) {
495 -1, -1, NULL
, STRING_TOKEN (STR_TFTP_ERR_NIC_NAME
),
496 mTftpHiiHandle
, NicNumber
, Status
501 if (UserNicName
!= NULL
) {
502 if (StrCmp (NicName
, UserNicName
) != 0) {
508 Status
= CreateServiceChildAndOpenProtocol (
510 &gEfiMtftp4ServiceBindingProtocolGuid
,
511 &gEfiMtftp4ProtocolGuid
,
515 if (EFI_ERROR (Status
)) {
517 -1, -1, NULL
, STRING_TOKEN (STR_TFTP_ERR_OPEN_PROTOCOL
),
518 mTftpHiiHandle
, NicName
, Status
523 Status
= Mtftp4
->Configure (Mtftp4
, &Mtftp4ConfigData
);
524 if (EFI_ERROR (Status
)) {
526 -1, -1, NULL
, STRING_TOKEN (STR_TFTP_ERR_CONFIGURE
),
527 mTftpHiiHandle
, NicName
, Status
532 Status
= GetFileSize (Mtftp4
, AsciiRemoteFilePath
, &FileSize
);
533 if (EFI_ERROR (Status
)) {
535 -1, -1, NULL
, STRING_TOKEN (STR_TFTP_ERR_FILE_SIZE
),
536 mTftpHiiHandle
, RemoteFilePath
, NicName
, Status
541 Status
= DownloadFile (Mtftp4
, RemoteFilePath
, AsciiRemoteFilePath
, FileSize
, BlockSize
, WindowSize
, &Data
);
542 if (EFI_ERROR (Status
)) {
544 -1, -1, NULL
, STRING_TOKEN (STR_TFTP_ERR_DOWNLOAD
),
545 mTftpHiiHandle
, RemoteFilePath
, NicName
, Status
552 if (!EFI_ERROR (ShellFileExists (LocalFilePath
))) {
553 ShellDeleteFileByName (LocalFilePath
);
556 Status
= ShellOpenFileByName (
559 EFI_FILE_MODE_CREATE
|
560 EFI_FILE_MODE_WRITE
|
564 if (EFI_ERROR (Status
)) {
566 -1, -1, NULL
, STRING_TOKEN (STR_GEN_FILE_OPEN_FAIL
),
567 mTftpHiiHandle
, L
"tftp", LocalFilePath
572 Status
= ShellWriteFile (FileHandle
, &FileSize
, Data
);
573 if (!EFI_ERROR (Status
)) {
574 ShellStatus
= SHELL_SUCCESS
;
577 -1, -1, NULL
, STRING_TOKEN (STR_TFTP_ERR_WRITE
),
578 mTftpHiiHandle
, LocalFilePath
, Status
581 ShellCloseFile (&FileHandle
);
586 gBS
->FreePages ((EFI_PHYSICAL_ADDRESS
)(UINTN
)Data
, EFI_SIZE_TO_PAGES (DataSize
));
589 CloseProtocolAndDestroyServiceChild (
591 &gEfiMtftp4ServiceBindingProtocolGuid
,
592 &gEfiMtftp4ProtocolGuid
,
597 if ((UserNicName
!= NULL
) && (!NicFound
)) {
599 -1, -1, NULL
, STRING_TOKEN (STR_TFTP_ERR_NIC_NOT_FOUND
),
600 mTftpHiiHandle
, UserNicName
606 ShellCommandLineFreeVarList (CheckPackage
);
607 if (AsciiRemoteFilePath
!= NULL
) {
608 FreePool (AsciiRemoteFilePath
);
610 if (Handles
!= NULL
) {
618 Check and convert the UINT16 option values of the 'tftp' command
620 @param[in] ValueStr Value as an Unicode encoded string
621 @param[out] Value UINT16 value
623 @return TRUE The value was returned.
624 @return FALSE A parsing error occured.
629 IN CONST CHAR16
*ValueStr
,
635 Val
= ShellStrToUintn (ValueStr
);
636 if (Val
> MAX_UINT16
) {
638 -1, -1, NULL
, STRING_TOKEN (STR_GEN_PARAM_INV
),
639 mTftpHiiHandle
, L
"tftp", ValueStr
644 *Value
= (UINT16
)Val
;
649 Get the name of the NIC.
651 @param[in] ControllerHandle The network physical device handle.
652 @param[in] NicNumber The network physical device number.
653 @param[out] NicName Address where to store the NIC name.
654 The memory area has to be at least
655 IP4_CONFIG2_INTERFACE_INFO_NAME_LENGTH
658 @return EFI_SUCCESS The name of the NIC was returned.
659 @return Others The creation of the child for the Managed
660 Network Service failed or the opening of
661 the Managed Network Protocol failed or
662 the operational parameters for the
663 Managed Network Protocol could not be
669 IN EFI_HANDLE ControllerHandle
,
675 EFI_HANDLE MnpHandle
;
676 EFI_MANAGED_NETWORK_PROTOCOL
*Mnp
;
677 EFI_SIMPLE_NETWORK_MODE SnpMode
;
679 Status
= CreateServiceChildAndOpenProtocol (
681 &gEfiManagedNetworkServiceBindingProtocolGuid
,
682 &gEfiManagedNetworkProtocolGuid
,
686 if (EFI_ERROR (Status
)) {
690 Status
= Mnp
->GetModeData (Mnp
, NULL
, &SnpMode
);
691 if (EFI_ERROR (Status
) && (Status
!= EFI_NOT_STARTED
)) {
697 IP4_CONFIG2_INTERFACE_INFO_NAME_LENGTH
,
698 SnpMode
.IfType
== NET_IFTYPE_ETHERNET
?
704 Status
= EFI_SUCCESS
;
708 if (MnpHandle
!= NULL
) {
709 CloseProtocolAndDestroyServiceChild (
711 &gEfiManagedNetworkServiceBindingProtocolGuid
,
712 &gEfiManagedNetworkProtocolGuid
,
721 Create a child for the service identified by its service binding protocol GUID
722 and get from the child the interface of the protocol identified by its GUID.
724 @param[in] ControllerHandle Controller handle.
725 @param[in] ServiceBindingProtocolGuid Service binding protocol GUID of the
726 service to be created.
727 @param[in] ProtocolGuid GUID of the protocol to be open.
728 @param[out] ChildHandle Address where the handler of the
729 created child is returned. NULL is
730 returned in case of error.
731 @param[out] Interface Address where a pointer to the
732 protocol interface is returned in
735 @return EFI_SUCCESS The child was created and the protocol opened.
736 @return Others Either the creation of the child or the opening
737 of the protocol failed.
741 CreateServiceChildAndOpenProtocol (
742 IN EFI_HANDLE ControllerHandle
,
743 IN EFI_GUID
*ServiceBindingProtocolGuid
,
744 IN EFI_GUID
*ProtocolGuid
,
745 OUT EFI_HANDLE
*ChildHandle
,
752 Status
= NetLibCreateServiceChild (
755 ServiceBindingProtocolGuid
,
758 if (!EFI_ERROR (Status
)) {
759 Status
= gBS
->OpenProtocol (
765 EFI_OPEN_PROTOCOL_GET_PROTOCOL
767 if (EFI_ERROR (Status
)) {
768 NetLibDestroyServiceChild (
771 ServiceBindingProtocolGuid
,
782 Close the protocol identified by its GUID on the child handle of the service
783 identified by its service binding protocol GUID, then destroy the child
786 @param[in] ControllerHandle Controller handle.
787 @param[in] ServiceBindingProtocolGuid Service binding protocol GUID of the
788 service to be destroyed.
789 @param[in] ProtocolGuid GUID of the protocol to be closed.
790 @param[in] ChildHandle Handle of the child to be destroyed.
795 CloseProtocolAndDestroyServiceChild (
796 IN EFI_HANDLE ControllerHandle
,
797 IN EFI_GUID
*ServiceBindingProtocolGuid
,
798 IN EFI_GUID
*ProtocolGuid
,
799 IN EFI_HANDLE ChildHandle
809 NetLibDestroyServiceChild (
812 ServiceBindingProtocolGuid
,
818 Worker function that gets the size in numbers of bytes of a file from a TFTP
819 server before to download the file.
821 @param[in] Mtftp4 MTFTP4 protocol interface
822 @param[in] FilePath Path of the file, ASCII encoded
823 @param[out] FileSize Address where to store the file size in number of
826 @retval EFI_SUCCESS The size of the file was returned.
827 @retval EFI_UNSUPPORTED The server does not support the "tsize" option.
828 @retval Others Error when retrieving the information from the server
829 (see EFI_MTFTP4_PROTOCOL.GetInfo() status codes)
830 or error when parsing the response of the server.
835 IN EFI_MTFTP4_PROTOCOL
*Mtftp4
,
836 IN CONST CHAR8
*FilePath
,
841 EFI_MTFTP4_OPTION ReqOpt
[1];
842 EFI_MTFTP4_PACKET
*Packet
;
844 EFI_MTFTP4_OPTION
*TableOfOptions
;
845 EFI_MTFTP4_OPTION
*Option
;
849 ReqOpt
[0].OptionStr
= (UINT8
*)"tsize";
852 ReqOpt
[0].ValueStr
= OptBuf
;
854 Status
= Mtftp4
->GetInfo (
865 if (EFI_ERROR (Status
)) {
869 Status
= Mtftp4
->ParseOptions (
876 if (EFI_ERROR (Status
)) {
880 Option
= TableOfOptions
;
881 while (OptCnt
!= 0) {
882 if (AsciiStrnCmp ((CHAR8
*)Option
->OptionStr
, "tsize", 5) == 0) {
883 *FileSize
= AsciiStrDecimalToUintn ((CHAR8
*)Option
->ValueStr
);
889 FreePool (TableOfOptions
);
892 Status
= EFI_UNSUPPORTED
;
901 Worker function that download the data of a file from a TFTP server given
902 the path of the file and its size.
904 @param[in] Mtftp4 MTFTP4 protocol interface
905 @param[in] FilePath Path of the file, Unicode encoded
906 @param[in] AsciiFilePath Path of the file, ASCII encoded
907 @param[in] FileSize Size of the file in number of bytes
908 @param[in] BlockSize Value of the TFTP blksize option
909 @param[out] Data Address where to store the address of the buffer
910 where the data of the file were downloaded in
913 @retval EFI_SUCCESS The file was downloaded.
914 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
915 @retval Others The downloading of the file from the server failed
916 (see EFI_MTFTP4_PROTOCOL.ReadFile() status codes).
922 IN EFI_MTFTP4_PROTOCOL
*Mtftp4
,
923 IN CONST CHAR16
*FilePath
,
924 IN CONST CHAR8
*AsciiFilePath
,
927 IN UINT16 WindowSize
,
932 EFI_PHYSICAL_ADDRESS PagesAddress
;
934 DOWNLOAD_CONTEXT
*TftpContext
;
935 EFI_MTFTP4_TOKEN Mtftp4Token
;
936 UINT8 BlksizeBuf
[10];
937 UINT8 WindowsizeBuf
[10];
939 ZeroMem (&Mtftp4Token
, sizeof (EFI_MTFTP4_TOKEN
));
941 // Downloaded file can be large. BS.AllocatePages() is more faster
942 // than AllocatePool() and avoid fragmentation.
943 // The downloaded file could be an EFI application. Marking the
944 // allocated page as EfiBootServicesCode would allow to execute a
945 // potential downloaded EFI application.
946 Status
= gBS
->AllocatePages (
949 EFI_SIZE_TO_PAGES (FileSize
),
952 if (EFI_ERROR (Status
)) {
956 Buffer
= (VOID
*)(UINTN
)PagesAddress
;
957 TftpContext
= AllocatePool (sizeof (DOWNLOAD_CONTEXT
));
958 if (TftpContext
== NULL
) {
959 Status
= EFI_OUT_OF_RESOURCES
;
962 TftpContext
->FileSize
= FileSize
;
963 TftpContext
->DownloadedNbOfBytes
= 0;
964 TftpContext
->LastReportedNbOfBytes
= 0;
966 Mtftp4Token
.Filename
= (UINT8
*)AsciiFilePath
;
967 Mtftp4Token
.BufferSize
= FileSize
;
968 Mtftp4Token
.Buffer
= Buffer
;
969 Mtftp4Token
.CheckPacket
= CheckPacket
;
970 Mtftp4Token
.Context
= (VOID
*)TftpContext
;
971 Mtftp4Token
.OptionCount
= 0;
972 Mtftp4Token
.OptionList
= AllocatePool (sizeof (EFI_MTFTP4_OPTION
) * 2);
973 if (Mtftp4Token
.OptionList
== NULL
) {
974 Status
= EFI_OUT_OF_RESOURCES
;
978 if (BlockSize
!= MTFTP_DEFAULT_BLKSIZE
) {
979 Mtftp4Token
.OptionList
[Mtftp4Token
.OptionCount
].OptionStr
= (UINT8
*) "blksize";
980 AsciiSPrint ((CHAR8
*) BlksizeBuf
, sizeof (BlksizeBuf
), "%d", BlockSize
);
981 Mtftp4Token
.OptionList
[Mtftp4Token
.OptionCount
].ValueStr
= BlksizeBuf
;
982 Mtftp4Token
.OptionCount
++;
985 if (WindowSize
!= MTFTP_DEFAULT_WINDOWSIZE
) {
986 Mtftp4Token
.OptionList
[Mtftp4Token
.OptionCount
].OptionStr
= (UINT8
*) "windowsize";
987 AsciiSPrint ((CHAR8
*) WindowsizeBuf
, sizeof (WindowsizeBuf
), "%d", WindowSize
);
988 Mtftp4Token
.OptionList
[Mtftp4Token
.OptionCount
].ValueStr
= WindowsizeBuf
;
989 Mtftp4Token
.OptionCount
++;
993 -1, -1, NULL
, STRING_TOKEN (STR_TFTP_DOWNLOADING
),
994 mTftpHiiHandle
, FilePath
997 Status
= Mtftp4
->ReadFile (Mtftp4
, &Mtftp4Token
);
999 -1, -1, NULL
, STRING_TOKEN (STR_GEN_CRLF
),
1005 if (TftpContext
!= NULL
) {
1006 FreePool (TftpContext
);
1009 if (Mtftp4Token
.OptionList
!= NULL
) {
1010 FreePool (Mtftp4Token
.OptionList
);
1013 if (EFI_ERROR (Status
)) {
1014 gBS
->FreePages (PagesAddress
, EFI_SIZE_TO_PAGES (FileSize
));
1024 Update the progress of a file download
1025 This procedure is called each time a new TFTP packet is received.
1027 @param[in] This MTFTP4 protocol interface
1028 @param[in] Token Parameters for the download of the file
1029 @param[in] PacketLen Length of the packet
1030 @param[in] Packet Address of the packet
1032 @retval EFI_SUCCESS All packets are accepted.
1039 IN EFI_MTFTP4_PROTOCOL
*This
,
1040 IN EFI_MTFTP4_TOKEN
*Token
,
1041 IN UINT16 PacketLen
,
1042 IN EFI_MTFTP4_PACKET
*Packet
1045 DOWNLOAD_CONTEXT
*Context
;
1046 CHAR16 Progress
[TFTP_PROGRESS_MESSAGE_SIZE
];
1053 if ((NTOHS (Packet
->OpCode
)) != EFI_MTFTP4_OPCODE_DATA
) {
1057 Context
= (DOWNLOAD_CONTEXT
*)Token
->Context
;
1058 if (Context
->DownloadedNbOfBytes
== 0) {
1059 ShellPrintEx (-1, -1, L
"%s 0 Kb", mTftpProgressFrame
);
1063 // The data in the packet are prepended with two UINT16 :
1064 // . OpCode = EFI_MTFTP4_OPCODE_DATA
1065 // . Block = the number of this block of data
1067 Context
->DownloadedNbOfBytes
+= PacketLen
- sizeof (Packet
->OpCode
)
1068 - sizeof (Packet
->Data
.Block
);
1069 NbOfKb
= Context
->DownloadedNbOfBytes
/ 1024;
1071 Progress
[0] = L
'\0';
1072 LastStep
= (Context
->LastReportedNbOfBytes
* TFTP_PROGRESS_SLIDER_STEPS
) / Context
->FileSize
;
1073 Step
= (Context
->DownloadedNbOfBytes
* TFTP_PROGRESS_SLIDER_STEPS
) / Context
->FileSize
;
1075 if (Step
<= LastStep
) {
1079 ShellPrintEx (-1, -1, L
"%s", mTftpProgressDelete
);
1081 Status
= StrCpyS (Progress
, TFTP_PROGRESS_MESSAGE_SIZE
, mTftpProgressFrame
);
1082 if (EFI_ERROR(Status
)) {
1085 for (Index
= 1; Index
< Step
; Index
++) {
1086 Progress
[Index
] = L
'=';
1088 Progress
[Step
] = L
'>';
1091 Progress
+ (sizeof (mTftpProgressFrame
) / sizeof (CHAR16
)) - 1,
1092 sizeof (Progress
) - sizeof (mTftpProgressFrame
),
1096 Context
->LastReportedNbOfBytes
= Context
->DownloadedNbOfBytes
;
1098 ShellPrintEx (-1, -1, L
"%s", Progress
);
1104 Retrive HII package list from ImageHandle and publish to HII database.
1106 @param ImageHandle The image handle of the process.
1111 InitializeHiiPackage (
1112 EFI_HANDLE ImageHandle
1116 EFI_HII_PACKAGE_LIST_HEADER
*PackageList
;
1117 EFI_HANDLE HiiHandle
;
1120 // Retrieve HII package list from ImageHandle
1122 Status
= gBS
->OpenProtocol (
1124 &gEfiHiiPackageListProtocolGuid
,
1125 (VOID
**)&PackageList
,
1128 EFI_OPEN_PROTOCOL_GET_PROTOCOL
1130 ASSERT_EFI_ERROR (Status
);
1131 if (EFI_ERROR (Status
)) {
1136 // Publish HII package list to HII Database.
1138 Status
= gHiiDatabase
->NewPackageList (
1144 ASSERT_EFI_ERROR (Status
);
1145 if (EFI_ERROR (Status
)) {