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
,
190 Update the progress of a file download
191 This procedure is called each time a new TFTP packet is received.
193 @param[in] This MTFTP4 protocol interface
194 @param[in] Token Parameters for the download of the file
195 @param[in] PacketLen Length of the packet
196 @param[in] Packet Address of the packet
198 @retval EFI_SUCCESS All packets are accepted.
205 IN EFI_MTFTP4_PROTOCOL
*This
,
206 IN EFI_MTFTP4_TOKEN
*Token
,
208 IN EFI_MTFTP4_PACKET
*Packet
211 EFI_MTFTP4_CONFIG_DATA DefaultMtftp4ConfigData
= {
212 TRUE
, // Use default setting
213 { { 0, 0, 0, 0 } }, // StationIp - Not relevant as UseDefaultSetting=TRUE
214 { { 0, 0, 0, 0 } }, // SubnetMask - Not relevant as UseDefaultSetting=TRUE
215 0, // LocalPort - Automatically assigned port number.
216 { { 0, 0, 0, 0 } }, // GatewayIp - Not relevant as UseDefaultSetting=TRUE
217 { { 0, 0, 0, 0 } }, // ServerIp - Not known yet
218 69, // InitialServerPort - Standard TFTP server port
219 6, // TryCount - Max number of retransmissions.
220 4 // TimeoutValue - Retransmission timeout in seconds.
223 STATIC CONST SHELL_PARAM_ITEM ParamList
[] = {
234 /// The default block size (512) of tftp is defined in the RFC1350.
236 #define MTFTP_DEFAULT_BLKSIZE 512
238 /// The valid range of block size option is defined in the RFC2348.
240 #define MTFTP_MIN_BLKSIZE 8
241 #define MTFTP_MAX_BLKSIZE 65464
245 Function for 'tftp' command.
247 @param[in] ImageHandle Handle to the Image (NULL if Internal).
248 @param[in] SystemTable Pointer to the System Table (NULL if Internal).
250 @return SHELL_SUCCESS The 'tftp' command completed successfully.
251 @return SHELL_ABORTED The Shell Library initialization failed.
252 @return SHELL_INVALID_PARAMETER At least one of the command's arguments is
254 @return SHELL_OUT_OF_RESOURCES A memory allocation failed.
255 @return SHELL_NOT_FOUND Network Interface Card not found or server
261 IN EFI_HANDLE ImageHandle
,
262 IN EFI_SYSTEM_TABLE
*SystemTable
265 SHELL_STATUS ShellStatus
;
267 LIST_ENTRY
*CheckPackage
;
268 CHAR16
*ProblemParam
;
270 CONST CHAR16
*UserNicName
;
272 CONST CHAR16
*ValueStr
;
273 CONST CHAR16
*RemoteFilePath
;
274 CHAR8
*AsciiRemoteFilePath
;
276 CONST CHAR16
*Walker
;
277 CONST CHAR16
*LocalFilePath
;
278 EFI_MTFTP4_CONFIG_DATA Mtftp4ConfigData
;
282 CHAR16 NicName
[IP4_CONFIG2_INTERFACE_INFO_NAME_LENGTH
];
283 EFI_HANDLE ControllerHandle
;
284 EFI_HANDLE Mtftp4ChildHandle
;
285 EFI_MTFTP4_PROTOCOL
*Mtftp4
;
289 SHELL_FILE_HANDLE FileHandle
;
292 ShellStatus
= SHELL_INVALID_PARAMETER
;
295 AsciiRemoteFilePath
= NULL
;
299 BlockSize
= MTFTP_DEFAULT_BLKSIZE
;
302 // Initialize the Shell library (we must be in non-auto-init...)
304 Status
= ShellInitialize ();
305 if (EFI_ERROR (Status
)) {
306 ASSERT_EFI_ERROR (Status
);
307 return SHELL_ABORTED
;
311 // Parse the command line.
313 Status
= ShellCommandLineParse (ParamList
, &CheckPackage
, &ProblemParam
, TRUE
);
314 if (EFI_ERROR (Status
)) {
315 if ((Status
== EFI_VOLUME_CORRUPTED
) &&
316 (ProblemParam
!= NULL
) ) {
318 -1, -1, NULL
, STRING_TOKEN (STR_GEN_PROBLEM
), mTftpHiiHandle
,
319 L
"tftp", ProblemParam
321 FreePool (ProblemParam
);
329 // Check the number of parameters
331 ParamCount
= ShellCommandLineGetCount (CheckPackage
);
332 if (ParamCount
> 4) {
334 -1, -1, NULL
, STRING_TOKEN (STR_GEN_TOO_MANY
),
335 mTftpHiiHandle
, L
"tftp"
339 if (ParamCount
< 3) {
341 -1, -1, NULL
, STRING_TOKEN (STR_GEN_TOO_FEW
),
342 mTftpHiiHandle
, L
"tftp"
347 CopyMem (&Mtftp4ConfigData
, &DefaultMtftp4ConfigData
, sizeof (EFI_MTFTP4_CONFIG_DATA
));
350 // Check the host IPv4 address
352 ValueStr
= ShellCommandLineGetRawValue (CheckPackage
, 1);
353 Status
= NetLibStrToIp4 (ValueStr
, &Mtftp4ConfigData
.ServerIp
);
354 if (EFI_ERROR (Status
)) {
356 -1, -1, NULL
, STRING_TOKEN (STR_GEN_PARAM_INV
),
357 mTftpHiiHandle
, L
"tftp", ValueStr
362 RemoteFilePath
= ShellCommandLineGetRawValue (CheckPackage
, 2);
363 ASSERT(RemoteFilePath
!= NULL
);
364 FilePathSize
= StrLen (RemoteFilePath
) + 1;
365 AsciiRemoteFilePath
= AllocatePool (FilePathSize
);
366 if (AsciiRemoteFilePath
== NULL
) {
367 ShellStatus
= SHELL_OUT_OF_RESOURCES
;
370 UnicodeStrToAsciiStrS (RemoteFilePath
, AsciiRemoteFilePath
, FilePathSize
);
372 if (ParamCount
== 4) {
373 LocalFilePath
= ShellCommandLineGetRawValue (CheckPackage
, 3);
375 Walker
= RemoteFilePath
+ StrLen (RemoteFilePath
);
376 while ((--Walker
) >= RemoteFilePath
) {
377 if ((*Walker
== L
'\\') ||
378 (*Walker
== L
'/' ) ) {
382 LocalFilePath
= Walker
+ 1;
386 // Get the name of the Network Interface Card to be used if any.
388 UserNicName
= ShellCommandLineGetValue (CheckPackage
, L
"-i");
390 ValueStr
= ShellCommandLineGetValue (CheckPackage
, L
"-l");
391 if (ValueStr
!= NULL
) {
392 if (!StringToUint16 (ValueStr
, &Mtftp4ConfigData
.LocalPort
)) {
397 ValueStr
= ShellCommandLineGetValue (CheckPackage
, L
"-r");
398 if (ValueStr
!= NULL
) {
399 if (!StringToUint16 (ValueStr
, &Mtftp4ConfigData
.InitialServerPort
)) {
404 ValueStr
= ShellCommandLineGetValue (CheckPackage
, L
"-c");
405 if (ValueStr
!= NULL
) {
406 if (!StringToUint16 (ValueStr
, &Mtftp4ConfigData
.TryCount
)) {
411 ValueStr
= ShellCommandLineGetValue (CheckPackage
, L
"-t");
412 if (ValueStr
!= NULL
) {
413 if (!StringToUint16 (ValueStr
, &Mtftp4ConfigData
.TimeoutValue
)) {
416 if (Mtftp4ConfigData
.TimeoutValue
== 0) {
418 -1, -1, NULL
, STRING_TOKEN (STR_GEN_PARAM_INV
),
419 mTftpHiiHandle
, L
"tftp", ValueStr
425 ValueStr
= ShellCommandLineGetValue (CheckPackage
, L
"-s");
426 if (ValueStr
!= NULL
) {
427 if (!StringToUint16 (ValueStr
, &BlockSize
)) {
430 if (BlockSize
< MTFTP_MIN_BLKSIZE
|| BlockSize
> MTFTP_MAX_BLKSIZE
) {
432 -1, -1, NULL
, STRING_TOKEN (STR_GEN_PARAM_INV
),
433 mTftpHiiHandle
, L
"tftp", ValueStr
440 // Locate all MTFTP4 Service Binding protocols
442 ShellStatus
= SHELL_NOT_FOUND
;
443 Status
= gBS
->LocateHandleBuffer (
445 &gEfiManagedNetworkServiceBindingProtocolGuid
,
450 if (EFI_ERROR (Status
) || (HandleCount
== 0)) {
452 -1, -1, NULL
, STRING_TOKEN (STR_TFTP_ERR_NO_NIC
),
459 (NicNumber
< HandleCount
) && (ShellStatus
!= SHELL_SUCCESS
);
461 ControllerHandle
= Handles
[NicNumber
];
464 Status
= GetNicName (ControllerHandle
, NicNumber
, NicName
);
465 if (EFI_ERROR (Status
)) {
467 -1, -1, NULL
, STRING_TOKEN (STR_TFTP_ERR_NIC_NAME
),
468 mTftpHiiHandle
, NicNumber
, Status
473 if (UserNicName
!= NULL
) {
474 if (StrCmp (NicName
, UserNicName
) != 0) {
480 Status
= CreateServiceChildAndOpenProtocol (
482 &gEfiMtftp4ServiceBindingProtocolGuid
,
483 &gEfiMtftp4ProtocolGuid
,
487 if (EFI_ERROR (Status
)) {
489 -1, -1, NULL
, STRING_TOKEN (STR_TFTP_ERR_OPEN_PROTOCOL
),
490 mTftpHiiHandle
, NicName
, Status
495 Status
= Mtftp4
->Configure (Mtftp4
, &Mtftp4ConfigData
);
496 if (EFI_ERROR (Status
)) {
498 -1, -1, NULL
, STRING_TOKEN (STR_TFTP_ERR_CONFIGURE
),
499 mTftpHiiHandle
, NicName
, Status
504 Status
= GetFileSize (Mtftp4
, AsciiRemoteFilePath
, &FileSize
);
505 if (EFI_ERROR (Status
)) {
507 -1, -1, NULL
, STRING_TOKEN (STR_TFTP_ERR_FILE_SIZE
),
508 mTftpHiiHandle
, RemoteFilePath
, NicName
, Status
513 Status
= DownloadFile (Mtftp4
, RemoteFilePath
, AsciiRemoteFilePath
, FileSize
, BlockSize
, &Data
);
514 if (EFI_ERROR (Status
)) {
516 -1, -1, NULL
, STRING_TOKEN (STR_TFTP_ERR_DOWNLOAD
),
517 mTftpHiiHandle
, RemoteFilePath
, NicName
, Status
522 if (!EFI_ERROR (ShellFileExists (LocalFilePath
))) {
523 ShellDeleteFileByName (LocalFilePath
);
526 Status
= ShellOpenFileByName (
529 EFI_FILE_MODE_CREATE
|
530 EFI_FILE_MODE_WRITE
|
534 if (EFI_ERROR (Status
)) {
536 -1, -1, NULL
, STRING_TOKEN (STR_GEN_FILE_OPEN_FAIL
),
537 mTftpHiiHandle
, L
"tftp", LocalFilePath
543 Status
= ShellWriteFile (FileHandle
, &FileSize
, Data
);
544 if (!EFI_ERROR (Status
)) {
545 ShellStatus
= SHELL_SUCCESS
;
548 -1, -1, NULL
, STRING_TOKEN (STR_TFTP_ERR_WRITE
),
549 mTftpHiiHandle
, LocalFilePath
, Status
552 ShellCloseFile (&FileHandle
);
557 gBS
->FreePages ((EFI_PHYSICAL_ADDRESS
)(UINTN
)Data
, EFI_SIZE_TO_PAGES (DataSize
));
560 CloseProtocolAndDestroyServiceChild (
562 &gEfiMtftp4ServiceBindingProtocolGuid
,
563 &gEfiMtftp4ProtocolGuid
,
568 if ((UserNicName
!= NULL
) && (!NicFound
)) {
570 -1, -1, NULL
, STRING_TOKEN (STR_TFTP_ERR_NIC_NOT_FOUND
),
571 mTftpHiiHandle
, UserNicName
577 ShellCommandLineFreeVarList (CheckPackage
);
578 if (AsciiRemoteFilePath
!= NULL
) {
579 FreePool (AsciiRemoteFilePath
);
581 if (Handles
!= NULL
) {
589 Check and convert the UINT16 option values of the 'tftp' command
591 @param[in] ValueStr Value as an Unicode encoded string
592 @param[out] Value UINT16 value
594 @return TRUE The value was returned.
595 @return FALSE A parsing error occured.
600 IN CONST CHAR16
*ValueStr
,
606 Val
= ShellStrToUintn (ValueStr
);
607 if (Val
> MAX_UINT16
) {
609 -1, -1, NULL
, STRING_TOKEN (STR_GEN_PARAM_INV
),
610 mTftpHiiHandle
, L
"tftp", ValueStr
615 *Value
= (UINT16
)Val
;
620 Get the name of the NIC.
622 @param[in] ControllerHandle The network physical device handle.
623 @param[in] NicNumber The network physical device number.
624 @param[out] NicName Address where to store the NIC name.
625 The memory area has to be at least
626 IP4_CONFIG2_INTERFACE_INFO_NAME_LENGTH
629 @return EFI_SUCCESS The name of the NIC was returned.
630 @return Others The creation of the child for the Managed
631 Network Service failed or the opening of
632 the Managed Network Protocol failed or
633 the operational parameters for the
634 Managed Network Protocol could not be
640 IN EFI_HANDLE ControllerHandle
,
646 EFI_HANDLE MnpHandle
;
647 EFI_MANAGED_NETWORK_PROTOCOL
*Mnp
;
648 EFI_SIMPLE_NETWORK_MODE SnpMode
;
650 Status
= CreateServiceChildAndOpenProtocol (
652 &gEfiManagedNetworkServiceBindingProtocolGuid
,
653 &gEfiManagedNetworkProtocolGuid
,
657 if (EFI_ERROR (Status
)) {
661 Status
= Mnp
->GetModeData (Mnp
, NULL
, &SnpMode
);
662 if (EFI_ERROR (Status
) && (Status
!= EFI_NOT_STARTED
)) {
668 IP4_CONFIG2_INTERFACE_INFO_NAME_LENGTH
,
669 SnpMode
.IfType
== NET_IFTYPE_ETHERNET
?
675 Status
= EFI_SUCCESS
;
679 if (MnpHandle
!= NULL
) {
680 CloseProtocolAndDestroyServiceChild (
682 &gEfiManagedNetworkServiceBindingProtocolGuid
,
683 &gEfiManagedNetworkProtocolGuid
,
692 Create a child for the service identified by its service binding protocol GUID
693 and get from the child the interface of the protocol identified by its GUID.
695 @param[in] ControllerHandle Controller handle.
696 @param[in] ServiceBindingProtocolGuid Service binding protocol GUID of the
697 service to be created.
698 @param[in] ProtocolGuid GUID of the protocol to be open.
699 @param[out] ChildHandle Address where the handler of the
700 created child is returned. NULL is
701 returned in case of error.
702 @param[out] Interface Address where a pointer to the
703 protocol interface is returned in
706 @return EFI_SUCCESS The child was created and the protocol opened.
707 @return Others Either the creation of the child or the opening
708 of the protocol failed.
712 CreateServiceChildAndOpenProtocol (
713 IN EFI_HANDLE ControllerHandle
,
714 IN EFI_GUID
*ServiceBindingProtocolGuid
,
715 IN EFI_GUID
*ProtocolGuid
,
716 OUT EFI_HANDLE
*ChildHandle
,
723 Status
= NetLibCreateServiceChild (
726 ServiceBindingProtocolGuid
,
729 if (!EFI_ERROR (Status
)) {
730 Status
= gBS
->OpenProtocol (
736 EFI_OPEN_PROTOCOL_GET_PROTOCOL
738 if (EFI_ERROR (Status
)) {
739 NetLibDestroyServiceChild (
742 ServiceBindingProtocolGuid
,
753 Close the protocol identified by its GUID on the child handle of the service
754 identified by its service binding protocol GUID, then destroy the child
757 @param[in] ControllerHandle Controller handle.
758 @param[in] ServiceBindingProtocolGuid Service binding protocol GUID of the
759 service to be destroyed.
760 @param[in] ProtocolGuid GUID of the protocol to be closed.
761 @param[in] ChildHandle Handle of the child to be destroyed.
766 CloseProtocolAndDestroyServiceChild (
767 IN EFI_HANDLE ControllerHandle
,
768 IN EFI_GUID
*ServiceBindingProtocolGuid
,
769 IN EFI_GUID
*ProtocolGuid
,
770 IN EFI_HANDLE ChildHandle
780 NetLibDestroyServiceChild (
783 ServiceBindingProtocolGuid
,
789 Worker function that gets the size in numbers of bytes of a file from a TFTP
790 server before to download the file.
792 @param[in] Mtftp4 MTFTP4 protocol interface
793 @param[in] FilePath Path of the file, ASCII encoded
794 @param[out] FileSize Address where to store the file size in number of
797 @retval EFI_SUCCESS The size of the file was returned.
798 @retval EFI_UNSUPPORTED The server does not support the "tsize" option.
799 @retval Others Error when retrieving the information from the server
800 (see EFI_MTFTP4_PROTOCOL.GetInfo() status codes)
801 or error when parsing the response of the server.
806 IN EFI_MTFTP4_PROTOCOL
*Mtftp4
,
807 IN CONST CHAR8
*FilePath
,
812 EFI_MTFTP4_OPTION ReqOpt
[1];
813 EFI_MTFTP4_PACKET
*Packet
;
815 EFI_MTFTP4_OPTION
*TableOfOptions
;
816 EFI_MTFTP4_OPTION
*Option
;
820 ReqOpt
[0].OptionStr
= (UINT8
*)"tsize";
823 ReqOpt
[0].ValueStr
= OptBuf
;
825 Status
= Mtftp4
->GetInfo (
836 if (EFI_ERROR (Status
)) {
840 Status
= Mtftp4
->ParseOptions (
847 if (EFI_ERROR (Status
)) {
851 Option
= TableOfOptions
;
852 while (OptCnt
!= 0) {
853 if (AsciiStrnCmp ((CHAR8
*)Option
->OptionStr
, "tsize", 5) == 0) {
854 *FileSize
= AsciiStrDecimalToUintn ((CHAR8
*)Option
->ValueStr
);
860 FreePool (TableOfOptions
);
863 Status
= EFI_UNSUPPORTED
;
872 Worker function that download the data of a file from a TFTP server given
873 the path of the file and its size.
875 @param[in] Mtftp4 MTFTP4 protocol interface
876 @param[in] FilePath Path of the file, Unicode encoded
877 @param[in] AsciiFilePath Path of the file, ASCII encoded
878 @param[in] FileSize Size of the file in number of bytes
879 @param[in] BlockSize Value of the TFTP blksize option
880 @param[out] Data Address where to store the address of the buffer
881 where the data of the file were downloaded in
884 @retval EFI_SUCCESS The file was downloaded.
885 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
886 @retval Others The downloading of the file from the server failed
887 (see EFI_MTFTP4_PROTOCOL.ReadFile() status codes).
893 IN EFI_MTFTP4_PROTOCOL
*Mtftp4
,
894 IN CONST CHAR16
*FilePath
,
895 IN CONST CHAR8
*AsciiFilePath
,
902 EFI_PHYSICAL_ADDRESS PagesAddress
;
904 DOWNLOAD_CONTEXT
*TftpContext
;
905 EFI_MTFTP4_TOKEN Mtftp4Token
;
906 EFI_MTFTP4_OPTION ReqOpt
;
909 // Downloaded file can be large. BS.AllocatePages() is more faster
910 // than AllocatePool() and avoid fragmentation.
911 // The downloaded file could be an EFI application. Marking the
912 // allocated page as EfiBootServicesCode would allow to execute a
913 // potential downloaded EFI application.
914 Status
= gBS
->AllocatePages (
917 EFI_SIZE_TO_PAGES (FileSize
),
920 if (EFI_ERROR (Status
)) {
924 Buffer
= (VOID
*)(UINTN
)PagesAddress
;
925 TftpContext
= AllocatePool (sizeof (DOWNLOAD_CONTEXT
));
926 if (TftpContext
== NULL
) {
927 Status
= EFI_OUT_OF_RESOURCES
;
930 TftpContext
->FileSize
= FileSize
;
931 TftpContext
->DownloadedNbOfBytes
= 0;
932 TftpContext
->LastReportedNbOfBytes
= 0;
934 ZeroMem (&Mtftp4Token
, sizeof (EFI_MTFTP4_TOKEN
));
935 Mtftp4Token
.Filename
= (UINT8
*)AsciiFilePath
;
936 Mtftp4Token
.BufferSize
= FileSize
;
937 Mtftp4Token
.Buffer
= Buffer
;
938 Mtftp4Token
.CheckPacket
= CheckPacket
;
939 Mtftp4Token
.Context
= (VOID
*)TftpContext
;
940 if (BlockSize
!= MTFTP_DEFAULT_BLKSIZE
) {
941 ReqOpt
.OptionStr
= (UINT8
*) "blksize";
942 AsciiSPrint ((CHAR8
*)OptBuf
, sizeof (OptBuf
), "%d", BlockSize
);
943 ReqOpt
.ValueStr
= OptBuf
;
945 Mtftp4Token
.OptionCount
= 1;
946 Mtftp4Token
.OptionList
= &ReqOpt
;
950 -1, -1, NULL
, STRING_TOKEN (STR_TFTP_DOWNLOADING
),
951 mTftpHiiHandle
, FilePath
954 Status
= Mtftp4
->ReadFile (Mtftp4
, &Mtftp4Token
);
956 -1, -1, NULL
, STRING_TOKEN (STR_GEN_CRLF
),
962 if (TftpContext
== NULL
) {
963 FreePool (TftpContext
);
966 if (EFI_ERROR (Status
)) {
967 gBS
->FreePages (PagesAddress
, EFI_SIZE_TO_PAGES (FileSize
));
977 Update the progress of a file download
978 This procedure is called each time a new TFTP packet is received.
980 @param[in] This MTFTP4 protocol interface
981 @param[in] Token Parameters for the download of the file
982 @param[in] PacketLen Length of the packet
983 @param[in] Packet Address of the packet
985 @retval EFI_SUCCESS All packets are accepted.
992 IN EFI_MTFTP4_PROTOCOL
*This
,
993 IN EFI_MTFTP4_TOKEN
*Token
,
995 IN EFI_MTFTP4_PACKET
*Packet
998 DOWNLOAD_CONTEXT
*Context
;
999 CHAR16 Progress
[TFTP_PROGRESS_MESSAGE_SIZE
];
1006 if ((NTOHS (Packet
->OpCode
)) != EFI_MTFTP4_OPCODE_DATA
) {
1010 Context
= (DOWNLOAD_CONTEXT
*)Token
->Context
;
1011 if (Context
->DownloadedNbOfBytes
== 0) {
1012 ShellPrintEx (-1, -1, L
"%s 0 Kb", mTftpProgressFrame
);
1016 // The data in the packet are prepended with two UINT16 :
1017 // . OpCode = EFI_MTFTP4_OPCODE_DATA
1018 // . Block = the number of this block of data
1020 Context
->DownloadedNbOfBytes
+= PacketLen
- sizeof (Packet
->OpCode
)
1021 - sizeof (Packet
->Data
.Block
);
1022 NbOfKb
= Context
->DownloadedNbOfBytes
/ 1024;
1024 Progress
[0] = L
'\0';
1025 LastStep
= (Context
->LastReportedNbOfBytes
* TFTP_PROGRESS_SLIDER_STEPS
) / Context
->FileSize
;
1026 Step
= (Context
->DownloadedNbOfBytes
* TFTP_PROGRESS_SLIDER_STEPS
) / Context
->FileSize
;
1028 if (Step
<= LastStep
) {
1032 ShellPrintEx (-1, -1, L
"%s", mTftpProgressDelete
);
1034 Status
= StrCpyS (Progress
, TFTP_PROGRESS_MESSAGE_SIZE
, mTftpProgressFrame
);
1035 if (EFI_ERROR(Status
)) {
1038 for (Index
= 1; Index
< Step
; Index
++) {
1039 Progress
[Index
] = L
'=';
1041 Progress
[Step
] = L
'>';
1044 Progress
+ (sizeof (mTftpProgressFrame
) / sizeof (CHAR16
)) - 1,
1045 sizeof (Progress
) - sizeof (mTftpProgressFrame
),
1049 Context
->LastReportedNbOfBytes
= Context
->DownloadedNbOfBytes
;
1051 ShellPrintEx (-1, -1, L
"%s", Progress
);
1057 Retrive HII package list from ImageHandle and publish to HII database.
1059 @param ImageHandle The image handle of the process.
1064 InitializeHiiPackage (
1065 EFI_HANDLE ImageHandle
1069 EFI_HII_PACKAGE_LIST_HEADER
*PackageList
;
1070 EFI_HANDLE HiiHandle
;
1073 // Retrieve HII package list from ImageHandle
1075 Status
= gBS
->OpenProtocol (
1077 &gEfiHiiPackageListProtocolGuid
,
1078 (VOID
**)&PackageList
,
1081 EFI_OPEN_PROTOCOL_GET_PROTOCOL
1083 ASSERT_EFI_ERROR (Status
);
1084 if (EFI_ERROR (Status
)) {
1089 // Publish HII package list to HII Database.
1091 Status
= gHiiDatabase
->NewPackageList (
1097 ASSERT_EFI_ERROR (Status
);
1098 if (EFI_ERROR (Status
)) {