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[in] WindowSize Value of the TFTP window size option
169 @param[out] Data Address where to store the address of the buffer
170 where the data of the file were downloaded in
173 @retval EFI_SUCCESS The file was downloaded.
174 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
175 @retval Others The downloading of the file from the server failed
176 (see EFI_MTFTP4_PROTOCOL.ReadFile() status codes).
182 IN EFI_MTFTP4_PROTOCOL
*Mtftp4
,
183 IN CONST CHAR16
*FilePath
,
184 IN CONST CHAR8
*AsciiFilePath
,
187 IN UINT16 WindowSize
,
192 Update the progress of a file download
193 This procedure is called each time a new TFTP packet is received.
195 @param[in] This MTFTP4 protocol interface
196 @param[in] Token Parameters for the download of the file
197 @param[in] PacketLen Length of the packet
198 @param[in] Packet Address of the packet
200 @retval EFI_SUCCESS All packets are accepted.
207 IN EFI_MTFTP4_PROTOCOL
*This
,
208 IN EFI_MTFTP4_TOKEN
*Token
,
210 IN EFI_MTFTP4_PACKET
*Packet
213 EFI_MTFTP4_CONFIG_DATA DefaultMtftp4ConfigData
= {
214 TRUE
, // Use default setting
215 { { 0, 0, 0, 0 } }, // StationIp - Not relevant as UseDefaultSetting=TRUE
216 { { 0, 0, 0, 0 } }, // SubnetMask - Not relevant as UseDefaultSetting=TRUE
217 0, // LocalPort - Automatically assigned port number.
218 { { 0, 0, 0, 0 } }, // GatewayIp - Not relevant as UseDefaultSetting=TRUE
219 { { 0, 0, 0, 0 } }, // ServerIp - Not known yet
220 69, // InitialServerPort - Standard TFTP server port
221 6, // TryCount - The number of times to transmit request packets and wait for a response.
222 4 // TimeoutValue - Retransmission timeout in seconds.
225 STATIC CONST SHELL_PARAM_ITEM ParamList
[] = {
237 /// The default block size (512) of tftp is defined in the RFC1350.
239 #define MTFTP_DEFAULT_BLKSIZE 512
241 /// The valid range of block size option is defined in the RFC2348.
243 #define MTFTP_MIN_BLKSIZE 8
244 #define MTFTP_MAX_BLKSIZE 65464
246 /// The default windowsize (1) of tftp.
248 #define MTFTP_DEFAULT_WINDOWSIZE 1
250 /// The valid range of window size option.
251 /// Note that: RFC 7440 does not mention max window size value, but for the
252 /// stability reason, the value is limited to 64.
254 #define MTFTP_MIN_WINDOWSIZE 1
255 #define MTFTP_MAX_WINDOWSIZE 64
258 Function for 'tftp' command.
260 @param[in] ImageHandle Handle to the Image (NULL if Internal).
261 @param[in] SystemTable Pointer to the System Table (NULL if Internal).
263 @return SHELL_SUCCESS The 'tftp' command completed successfully.
264 @return SHELL_ABORTED The Shell Library initialization failed.
265 @return SHELL_INVALID_PARAMETER At least one of the command's arguments is
267 @return SHELL_OUT_OF_RESOURCES A memory allocation failed.
268 @return SHELL_NOT_FOUND Network Interface Card not found or server
274 IN EFI_HANDLE ImageHandle
,
275 IN EFI_SYSTEM_TABLE
*SystemTable
278 SHELL_STATUS ShellStatus
;
280 LIST_ENTRY
*CheckPackage
;
281 CHAR16
*ProblemParam
;
283 CONST CHAR16
*UserNicName
;
285 CONST CHAR16
*ValueStr
;
286 CONST CHAR16
*RemoteFilePath
;
287 CHAR8
*AsciiRemoteFilePath
;
289 CONST CHAR16
*Walker
;
290 CONST CHAR16
*LocalFilePath
;
291 EFI_MTFTP4_CONFIG_DATA Mtftp4ConfigData
;
295 CHAR16 NicName
[IP4_CONFIG2_INTERFACE_INFO_NAME_LENGTH
];
296 EFI_HANDLE ControllerHandle
;
297 EFI_HANDLE Mtftp4ChildHandle
;
298 EFI_MTFTP4_PROTOCOL
*Mtftp4
;
302 SHELL_FILE_HANDLE FileHandle
;
306 ShellStatus
= SHELL_INVALID_PARAMETER
;
309 AsciiRemoteFilePath
= NULL
;
313 BlockSize
= MTFTP_DEFAULT_BLKSIZE
;
314 WindowSize
= MTFTP_DEFAULT_WINDOWSIZE
;
317 // Initialize the Shell library (we must be in non-auto-init...)
319 Status
= ShellInitialize ();
320 if (EFI_ERROR (Status
)) {
321 ASSERT_EFI_ERROR (Status
);
322 return SHELL_ABORTED
;
326 // Parse the command line.
328 Status
= ShellCommandLineParse (ParamList
, &CheckPackage
, &ProblemParam
, TRUE
);
329 if (EFI_ERROR (Status
)) {
330 if ((Status
== EFI_VOLUME_CORRUPTED
) &&
331 (ProblemParam
!= NULL
) ) {
333 -1, -1, NULL
, STRING_TOKEN (STR_GEN_PROBLEM
), mTftpHiiHandle
,
334 L
"tftp", ProblemParam
336 FreePool (ProblemParam
);
344 // Check the number of parameters
346 ParamCount
= ShellCommandLineGetCount (CheckPackage
);
347 if (ParamCount
> 4) {
349 -1, -1, NULL
, STRING_TOKEN (STR_GEN_TOO_MANY
),
350 mTftpHiiHandle
, L
"tftp"
354 if (ParamCount
< 3) {
356 -1, -1, NULL
, STRING_TOKEN (STR_GEN_TOO_FEW
),
357 mTftpHiiHandle
, L
"tftp"
362 CopyMem (&Mtftp4ConfigData
, &DefaultMtftp4ConfigData
, sizeof (EFI_MTFTP4_CONFIG_DATA
));
365 // Check the host IPv4 address
367 ValueStr
= ShellCommandLineGetRawValue (CheckPackage
, 1);
368 Status
= NetLibStrToIp4 (ValueStr
, &Mtftp4ConfigData
.ServerIp
);
369 if (EFI_ERROR (Status
)) {
371 -1, -1, NULL
, STRING_TOKEN (STR_GEN_PARAM_INV
),
372 mTftpHiiHandle
, L
"tftp", ValueStr
377 RemoteFilePath
= ShellCommandLineGetRawValue (CheckPackage
, 2);
378 ASSERT(RemoteFilePath
!= NULL
);
379 FilePathSize
= StrLen (RemoteFilePath
) + 1;
380 AsciiRemoteFilePath
= AllocatePool (FilePathSize
);
381 if (AsciiRemoteFilePath
== NULL
) {
382 ShellStatus
= SHELL_OUT_OF_RESOURCES
;
385 UnicodeStrToAsciiStrS (RemoteFilePath
, AsciiRemoteFilePath
, FilePathSize
);
387 if (ParamCount
== 4) {
388 LocalFilePath
= ShellCommandLineGetRawValue (CheckPackage
, 3);
390 Walker
= RemoteFilePath
+ StrLen (RemoteFilePath
);
391 while ((--Walker
) >= RemoteFilePath
) {
392 if ((*Walker
== L
'\\') ||
393 (*Walker
== L
'/' ) ) {
397 LocalFilePath
= Walker
+ 1;
401 // Get the name of the Network Interface Card to be used if any.
403 UserNicName
= ShellCommandLineGetValue (CheckPackage
, L
"-i");
405 ValueStr
= ShellCommandLineGetValue (CheckPackage
, L
"-l");
406 if (ValueStr
!= NULL
) {
407 if (!StringToUint16 (ValueStr
, &Mtftp4ConfigData
.LocalPort
)) {
412 ValueStr
= ShellCommandLineGetValue (CheckPackage
, L
"-r");
413 if (ValueStr
!= NULL
) {
414 if (!StringToUint16 (ValueStr
, &Mtftp4ConfigData
.InitialServerPort
)) {
419 ValueStr
= ShellCommandLineGetValue (CheckPackage
, L
"-c");
420 if (ValueStr
!= NULL
) {
421 if (!StringToUint16 (ValueStr
, &Mtftp4ConfigData
.TryCount
)) {
425 if (Mtftp4ConfigData
.TryCount
== 0) {
426 Mtftp4ConfigData
.TryCount
= 6;
430 ValueStr
= ShellCommandLineGetValue (CheckPackage
, L
"-t");
431 if (ValueStr
!= NULL
) {
432 if (!StringToUint16 (ValueStr
, &Mtftp4ConfigData
.TimeoutValue
)) {
435 if (Mtftp4ConfigData
.TimeoutValue
== 0) {
437 -1, -1, NULL
, STRING_TOKEN (STR_GEN_PARAM_INV
),
438 mTftpHiiHandle
, L
"tftp", ValueStr
444 ValueStr
= ShellCommandLineGetValue (CheckPackage
, L
"-s");
445 if (ValueStr
!= NULL
) {
446 if (!StringToUint16 (ValueStr
, &BlockSize
)) {
449 if (BlockSize
< MTFTP_MIN_BLKSIZE
|| BlockSize
> MTFTP_MAX_BLKSIZE
) {
451 -1, -1, NULL
, STRING_TOKEN (STR_GEN_PARAM_INV
),
452 mTftpHiiHandle
, L
"tftp", ValueStr
458 ValueStr
= ShellCommandLineGetValue (CheckPackage
, L
"-w");
459 if (ValueStr
!= NULL
) {
460 if (!StringToUint16 (ValueStr
, &WindowSize
)) {
463 if (WindowSize
< MTFTP_MIN_WINDOWSIZE
|| WindowSize
> MTFTP_MAX_WINDOWSIZE
) {
465 -1, -1, NULL
, STRING_TOKEN (STR_GEN_PARAM_INV
),
466 mTftpHiiHandle
, L
"tftp", ValueStr
473 // Locate all MTFTP4 Service Binding protocols
475 ShellStatus
= SHELL_NOT_FOUND
;
476 Status
= gBS
->LocateHandleBuffer (
478 &gEfiManagedNetworkServiceBindingProtocolGuid
,
483 if (EFI_ERROR (Status
) || (HandleCount
== 0)) {
485 -1, -1, NULL
, STRING_TOKEN (STR_TFTP_ERR_NO_NIC
),
492 (NicNumber
< HandleCount
) && (ShellStatus
!= SHELL_SUCCESS
);
494 ControllerHandle
= Handles
[NicNumber
];
497 Status
= GetNicName (ControllerHandle
, NicNumber
, NicName
);
498 if (EFI_ERROR (Status
)) {
500 -1, -1, NULL
, STRING_TOKEN (STR_TFTP_ERR_NIC_NAME
),
501 mTftpHiiHandle
, NicNumber
, Status
506 if (UserNicName
!= NULL
) {
507 if (StrCmp (NicName
, UserNicName
) != 0) {
513 Status
= CreateServiceChildAndOpenProtocol (
515 &gEfiMtftp4ServiceBindingProtocolGuid
,
516 &gEfiMtftp4ProtocolGuid
,
520 if (EFI_ERROR (Status
)) {
522 -1, -1, NULL
, STRING_TOKEN (STR_TFTP_ERR_OPEN_PROTOCOL
),
523 mTftpHiiHandle
, NicName
, Status
528 Status
= Mtftp4
->Configure (Mtftp4
, &Mtftp4ConfigData
);
529 if (EFI_ERROR (Status
)) {
531 -1, -1, NULL
, STRING_TOKEN (STR_TFTP_ERR_CONFIGURE
),
532 mTftpHiiHandle
, NicName
, Status
537 Status
= GetFileSize (Mtftp4
, AsciiRemoteFilePath
, &FileSize
);
538 if (EFI_ERROR (Status
)) {
540 -1, -1, NULL
, STRING_TOKEN (STR_TFTP_ERR_FILE_SIZE
),
541 mTftpHiiHandle
, RemoteFilePath
, NicName
, Status
546 Status
= DownloadFile (Mtftp4
, RemoteFilePath
, AsciiRemoteFilePath
, FileSize
, BlockSize
, WindowSize
, &Data
);
547 if (EFI_ERROR (Status
)) {
549 -1, -1, NULL
, STRING_TOKEN (STR_TFTP_ERR_DOWNLOAD
),
550 mTftpHiiHandle
, RemoteFilePath
, NicName
, Status
557 if (!EFI_ERROR (ShellFileExists (LocalFilePath
))) {
558 ShellDeleteFileByName (LocalFilePath
);
561 Status
= ShellOpenFileByName (
564 EFI_FILE_MODE_CREATE
|
565 EFI_FILE_MODE_WRITE
|
569 if (EFI_ERROR (Status
)) {
571 -1, -1, NULL
, STRING_TOKEN (STR_GEN_FILE_OPEN_FAIL
),
572 mTftpHiiHandle
, L
"tftp", LocalFilePath
577 Status
= ShellWriteFile (FileHandle
, &FileSize
, Data
);
578 if (!EFI_ERROR (Status
)) {
579 ShellStatus
= SHELL_SUCCESS
;
582 -1, -1, NULL
, STRING_TOKEN (STR_TFTP_ERR_WRITE
),
583 mTftpHiiHandle
, LocalFilePath
, Status
586 ShellCloseFile (&FileHandle
);
591 gBS
->FreePages ((EFI_PHYSICAL_ADDRESS
)(UINTN
)Data
, EFI_SIZE_TO_PAGES (DataSize
));
594 CloseProtocolAndDestroyServiceChild (
596 &gEfiMtftp4ServiceBindingProtocolGuid
,
597 &gEfiMtftp4ProtocolGuid
,
602 if ((UserNicName
!= NULL
) && (!NicFound
)) {
604 -1, -1, NULL
, STRING_TOKEN (STR_TFTP_ERR_NIC_NOT_FOUND
),
605 mTftpHiiHandle
, UserNicName
611 ShellCommandLineFreeVarList (CheckPackage
);
612 if (AsciiRemoteFilePath
!= NULL
) {
613 FreePool (AsciiRemoteFilePath
);
615 if (Handles
!= NULL
) {
623 Check and convert the UINT16 option values of the 'tftp' command
625 @param[in] ValueStr Value as an Unicode encoded string
626 @param[out] Value UINT16 value
628 @return TRUE The value was returned.
629 @return FALSE A parsing error occured.
634 IN CONST CHAR16
*ValueStr
,
640 Val
= ShellStrToUintn (ValueStr
);
641 if (Val
> MAX_UINT16
) {
643 -1, -1, NULL
, STRING_TOKEN (STR_GEN_PARAM_INV
),
644 mTftpHiiHandle
, L
"tftp", ValueStr
649 *Value
= (UINT16
)Val
;
654 Get the name of the NIC.
656 @param[in] ControllerHandle The network physical device handle.
657 @param[in] NicNumber The network physical device number.
658 @param[out] NicName Address where to store the NIC name.
659 The memory area has to be at least
660 IP4_CONFIG2_INTERFACE_INFO_NAME_LENGTH
663 @return EFI_SUCCESS The name of the NIC was returned.
664 @return Others The creation of the child for the Managed
665 Network Service failed or the opening of
666 the Managed Network Protocol failed or
667 the operational parameters for the
668 Managed Network Protocol could not be
674 IN EFI_HANDLE ControllerHandle
,
680 EFI_HANDLE MnpHandle
;
681 EFI_MANAGED_NETWORK_PROTOCOL
*Mnp
;
682 EFI_SIMPLE_NETWORK_MODE SnpMode
;
684 Status
= CreateServiceChildAndOpenProtocol (
686 &gEfiManagedNetworkServiceBindingProtocolGuid
,
687 &gEfiManagedNetworkProtocolGuid
,
691 if (EFI_ERROR (Status
)) {
695 Status
= Mnp
->GetModeData (Mnp
, NULL
, &SnpMode
);
696 if (EFI_ERROR (Status
) && (Status
!= EFI_NOT_STARTED
)) {
702 IP4_CONFIG2_INTERFACE_INFO_NAME_LENGTH
,
703 SnpMode
.IfType
== NET_IFTYPE_ETHERNET
?
709 Status
= EFI_SUCCESS
;
713 if (MnpHandle
!= NULL
) {
714 CloseProtocolAndDestroyServiceChild (
716 &gEfiManagedNetworkServiceBindingProtocolGuid
,
717 &gEfiManagedNetworkProtocolGuid
,
726 Create a child for the service identified by its service binding protocol GUID
727 and get from the child the interface of the protocol identified by its GUID.
729 @param[in] ControllerHandle Controller handle.
730 @param[in] ServiceBindingProtocolGuid Service binding protocol GUID of the
731 service to be created.
732 @param[in] ProtocolGuid GUID of the protocol to be open.
733 @param[out] ChildHandle Address where the handler of the
734 created child is returned. NULL is
735 returned in case of error.
736 @param[out] Interface Address where a pointer to the
737 protocol interface is returned in
740 @return EFI_SUCCESS The child was created and the protocol opened.
741 @return Others Either the creation of the child or the opening
742 of the protocol failed.
746 CreateServiceChildAndOpenProtocol (
747 IN EFI_HANDLE ControllerHandle
,
748 IN EFI_GUID
*ServiceBindingProtocolGuid
,
749 IN EFI_GUID
*ProtocolGuid
,
750 OUT EFI_HANDLE
*ChildHandle
,
757 Status
= NetLibCreateServiceChild (
760 ServiceBindingProtocolGuid
,
763 if (!EFI_ERROR (Status
)) {
764 Status
= gBS
->OpenProtocol (
770 EFI_OPEN_PROTOCOL_GET_PROTOCOL
772 if (EFI_ERROR (Status
)) {
773 NetLibDestroyServiceChild (
776 ServiceBindingProtocolGuid
,
787 Close the protocol identified by its GUID on the child handle of the service
788 identified by its service binding protocol GUID, then destroy the child
791 @param[in] ControllerHandle Controller handle.
792 @param[in] ServiceBindingProtocolGuid Service binding protocol GUID of the
793 service to be destroyed.
794 @param[in] ProtocolGuid GUID of the protocol to be closed.
795 @param[in] ChildHandle Handle of the child to be destroyed.
800 CloseProtocolAndDestroyServiceChild (
801 IN EFI_HANDLE ControllerHandle
,
802 IN EFI_GUID
*ServiceBindingProtocolGuid
,
803 IN EFI_GUID
*ProtocolGuid
,
804 IN EFI_HANDLE ChildHandle
814 NetLibDestroyServiceChild (
817 ServiceBindingProtocolGuid
,
823 Worker function that gets the size in numbers of bytes of a file from a TFTP
824 server before to download the file.
826 @param[in] Mtftp4 MTFTP4 protocol interface
827 @param[in] FilePath Path of the file, ASCII encoded
828 @param[out] FileSize Address where to store the file size in number of
831 @retval EFI_SUCCESS The size of the file was returned.
832 @retval EFI_UNSUPPORTED The server does not support the "tsize" option.
833 @retval Others Error when retrieving the information from the server
834 (see EFI_MTFTP4_PROTOCOL.GetInfo() status codes)
835 or error when parsing the response of the server.
840 IN EFI_MTFTP4_PROTOCOL
*Mtftp4
,
841 IN CONST CHAR8
*FilePath
,
846 EFI_MTFTP4_OPTION ReqOpt
[1];
847 EFI_MTFTP4_PACKET
*Packet
;
849 EFI_MTFTP4_OPTION
*TableOfOptions
;
850 EFI_MTFTP4_OPTION
*Option
;
854 ReqOpt
[0].OptionStr
= (UINT8
*)"tsize";
857 ReqOpt
[0].ValueStr
= OptBuf
;
859 Status
= Mtftp4
->GetInfo (
870 if (EFI_ERROR (Status
)) {
874 Status
= Mtftp4
->ParseOptions (
881 if (EFI_ERROR (Status
)) {
885 Option
= TableOfOptions
;
886 while (OptCnt
!= 0) {
887 if (AsciiStrnCmp ((CHAR8
*)Option
->OptionStr
, "tsize", 5) == 0) {
888 *FileSize
= AsciiStrDecimalToUintn ((CHAR8
*)Option
->ValueStr
);
894 FreePool (TableOfOptions
);
897 Status
= EFI_UNSUPPORTED
;
906 Worker function that download the data of a file from a TFTP server given
907 the path of the file and its size.
909 @param[in] Mtftp4 MTFTP4 protocol interface
910 @param[in] FilePath Path of the file, Unicode encoded
911 @param[in] AsciiFilePath Path of the file, ASCII encoded
912 @param[in] FileSize Size of the file in number of bytes
913 @param[in] BlockSize Value of the TFTP blksize option
914 @param[in] WindowSize Value of the TFTP window size option
915 @param[out] Data Address where to store the address of the buffer
916 where the data of the file were downloaded in
919 @retval EFI_SUCCESS The file was downloaded.
920 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
921 @retval Others The downloading of the file from the server failed
922 (see EFI_MTFTP4_PROTOCOL.ReadFile() status codes).
928 IN EFI_MTFTP4_PROTOCOL
*Mtftp4
,
929 IN CONST CHAR16
*FilePath
,
930 IN CONST CHAR8
*AsciiFilePath
,
933 IN UINT16 WindowSize
,
938 EFI_PHYSICAL_ADDRESS PagesAddress
;
940 DOWNLOAD_CONTEXT
*TftpContext
;
941 EFI_MTFTP4_TOKEN Mtftp4Token
;
942 UINT8 BlksizeBuf
[10];
943 UINT8 WindowsizeBuf
[10];
945 ZeroMem (&Mtftp4Token
, sizeof (EFI_MTFTP4_TOKEN
));
947 // Downloaded file can be large. BS.AllocatePages() is more faster
948 // than AllocatePool() and avoid fragmentation.
949 // The downloaded file could be an EFI application. Marking the
950 // allocated page as EfiBootServicesCode would allow to execute a
951 // potential downloaded EFI application.
952 Status
= gBS
->AllocatePages (
955 EFI_SIZE_TO_PAGES (FileSize
),
958 if (EFI_ERROR (Status
)) {
962 Buffer
= (VOID
*)(UINTN
)PagesAddress
;
963 TftpContext
= AllocatePool (sizeof (DOWNLOAD_CONTEXT
));
964 if (TftpContext
== NULL
) {
965 Status
= EFI_OUT_OF_RESOURCES
;
968 TftpContext
->FileSize
= FileSize
;
969 TftpContext
->DownloadedNbOfBytes
= 0;
970 TftpContext
->LastReportedNbOfBytes
= 0;
972 Mtftp4Token
.Filename
= (UINT8
*)AsciiFilePath
;
973 Mtftp4Token
.BufferSize
= FileSize
;
974 Mtftp4Token
.Buffer
= Buffer
;
975 Mtftp4Token
.CheckPacket
= CheckPacket
;
976 Mtftp4Token
.Context
= (VOID
*)TftpContext
;
977 Mtftp4Token
.OptionCount
= 0;
978 Mtftp4Token
.OptionList
= AllocatePool (sizeof (EFI_MTFTP4_OPTION
) * 2);
979 if (Mtftp4Token
.OptionList
== NULL
) {
980 Status
= EFI_OUT_OF_RESOURCES
;
984 if (BlockSize
!= MTFTP_DEFAULT_BLKSIZE
) {
985 Mtftp4Token
.OptionList
[Mtftp4Token
.OptionCount
].OptionStr
= (UINT8
*) "blksize";
986 AsciiSPrint ((CHAR8
*) BlksizeBuf
, sizeof (BlksizeBuf
), "%d", BlockSize
);
987 Mtftp4Token
.OptionList
[Mtftp4Token
.OptionCount
].ValueStr
= BlksizeBuf
;
988 Mtftp4Token
.OptionCount
++;
991 if (WindowSize
!= MTFTP_DEFAULT_WINDOWSIZE
) {
992 Mtftp4Token
.OptionList
[Mtftp4Token
.OptionCount
].OptionStr
= (UINT8
*) "windowsize";
993 AsciiSPrint ((CHAR8
*) WindowsizeBuf
, sizeof (WindowsizeBuf
), "%d", WindowSize
);
994 Mtftp4Token
.OptionList
[Mtftp4Token
.OptionCount
].ValueStr
= WindowsizeBuf
;
995 Mtftp4Token
.OptionCount
++;
999 -1, -1, NULL
, STRING_TOKEN (STR_TFTP_DOWNLOADING
),
1000 mTftpHiiHandle
, FilePath
1003 Status
= Mtftp4
->ReadFile (Mtftp4
, &Mtftp4Token
);
1005 -1, -1, NULL
, STRING_TOKEN (STR_GEN_CRLF
),
1011 if (TftpContext
!= NULL
) {
1012 FreePool (TftpContext
);
1015 if (Mtftp4Token
.OptionList
!= NULL
) {
1016 FreePool (Mtftp4Token
.OptionList
);
1019 if (EFI_ERROR (Status
)) {
1020 gBS
->FreePages (PagesAddress
, EFI_SIZE_TO_PAGES (FileSize
));
1030 Update the progress of a file download
1031 This procedure is called each time a new TFTP packet is received.
1033 @param[in] This MTFTP4 protocol interface
1034 @param[in] Token Parameters for the download of the file
1035 @param[in] PacketLen Length of the packet
1036 @param[in] Packet Address of the packet
1038 @retval EFI_SUCCESS All packets are accepted.
1045 IN EFI_MTFTP4_PROTOCOL
*This
,
1046 IN EFI_MTFTP4_TOKEN
*Token
,
1047 IN UINT16 PacketLen
,
1048 IN EFI_MTFTP4_PACKET
*Packet
1051 DOWNLOAD_CONTEXT
*Context
;
1052 CHAR16 Progress
[TFTP_PROGRESS_MESSAGE_SIZE
];
1059 if ((NTOHS (Packet
->OpCode
)) != EFI_MTFTP4_OPCODE_DATA
) {
1063 Context
= (DOWNLOAD_CONTEXT
*)Token
->Context
;
1064 if (Context
->DownloadedNbOfBytes
== 0) {
1065 ShellPrintEx (-1, -1, L
"%s 0 Kb", mTftpProgressFrame
);
1069 // The data in the packet are prepended with two UINT16 :
1070 // . OpCode = EFI_MTFTP4_OPCODE_DATA
1071 // . Block = the number of this block of data
1073 Context
->DownloadedNbOfBytes
+= PacketLen
- sizeof (Packet
->OpCode
)
1074 - sizeof (Packet
->Data
.Block
);
1075 NbOfKb
= Context
->DownloadedNbOfBytes
/ 1024;
1077 Progress
[0] = L
'\0';
1078 LastStep
= (Context
->LastReportedNbOfBytes
* TFTP_PROGRESS_SLIDER_STEPS
) / Context
->FileSize
;
1079 Step
= (Context
->DownloadedNbOfBytes
* TFTP_PROGRESS_SLIDER_STEPS
) / Context
->FileSize
;
1081 if (Step
<= LastStep
) {
1085 ShellPrintEx (-1, -1, L
"%s", mTftpProgressDelete
);
1087 Status
= StrCpyS (Progress
, TFTP_PROGRESS_MESSAGE_SIZE
, mTftpProgressFrame
);
1088 if (EFI_ERROR(Status
)) {
1091 for (Index
= 1; Index
< Step
; Index
++) {
1092 Progress
[Index
] = L
'=';
1094 Progress
[Step
] = L
'>';
1097 Progress
+ (sizeof (mTftpProgressFrame
) / sizeof (CHAR16
)) - 1,
1098 sizeof (Progress
) - sizeof (mTftpProgressFrame
),
1102 Context
->LastReportedNbOfBytes
= Context
->DownloadedNbOfBytes
;
1104 ShellPrintEx (-1, -1, L
"%s", Progress
);
1110 Retrive HII package list from ImageHandle and publish to HII database.
1112 @param ImageHandle The image handle of the process.
1117 InitializeHiiPackage (
1118 EFI_HANDLE ImageHandle
1122 EFI_HII_PACKAGE_LIST_HEADER
*PackageList
;
1123 EFI_HANDLE HiiHandle
;
1126 // Retrieve HII package list from ImageHandle
1128 Status
= gBS
->OpenProtocol (
1130 &gEfiHiiPackageListProtocolGuid
,
1131 (VOID
**)&PackageList
,
1134 EFI_OPEN_PROTOCOL_GET_PROTOCOL
1136 ASSERT_EFI_ERROR (Status
);
1137 if (EFI_ERROR (Status
)) {
1142 // Publish HII package list to HII Database.
1144 Status
= gHiiDatabase
->NewPackageList (
1150 ASSERT_EFI_ERROR (Status
);
1151 if (EFI_ERROR (Status
)) {