]> git.proxmox.com Git - mirror_edk2.git/blame - ShellPkg/DynamicCommand/TftpDynamicCommand/Tftp.c
NetworkPkg/Mtftp6Dxe: Support windowsize in read request operation.
[mirror_edk2.git] / ShellPkg / DynamicCommand / TftpDynamicCommand / Tftp.c
CommitLineData
68074260
RC
1/** @file\r
2 The implementation for the 'tftp' Shell command.\r
3\r
4 Copyright (c) 2015, ARM Ltd. All rights reserved.<BR>\r
ba0014b9 5 Copyright (c) 2015 - 2018, Intel Corporation. All rights reserved. <BR>\r
6add86ab 6 (C) Copyright 2015 Hewlett Packard Enterprise Development LP<BR>\r
68074260
RC
7\r
8 This program and the accompanying materials\r
9 are licensed and made available under the terms and conditions of the BSD License\r
10 which accompanies this distribution. The full text of the license may be found at\r
11 http://opensource.org/licenses/bsd-license.php.\r
12\r
13 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
14 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
15**/\r
16\r
09610023 17#include "Tftp.h"\r
68074260 18\r
6add86ab 19#define IP4_CONFIG2_INTERFACE_INFO_NAME_LENGTH 32\r
09610023 20EFI_HANDLE mTftpHiiHandle;\r
6add86ab 21\r
68074260
RC
22/*\r
23 Constant strings and definitions related to the message indicating the amount of\r
24 progress in the dowloading of a TFTP file.\r
25*/\r
26\r
27// Frame for the progression slider\r
28STATIC CONST CHAR16 mTftpProgressFrame[] = L"[ ]";\r
29\r
30// Number of steps in the progression slider\r
31#define TFTP_PROGRESS_SLIDER_STEPS ((sizeof (mTftpProgressFrame) / sizeof (CHAR16)) - 3)\r
32\r
33// Size in number of characters plus one (final zero) of the message to\r
34// indicate the progress of a TFTP download. The format is "[(progress slider:\r
35// 40 characters)] (nb of KBytes downloaded so far: 7 characters) Kb". There\r
36// are thus the number of characters in mTftpProgressFrame[] plus 11 characters\r
37// (2 // spaces, "Kb" and seven characters for the number of KBytes).\r
38#define TFTP_PROGRESS_MESSAGE_SIZE ((sizeof (mTftpProgressFrame) / sizeof (CHAR16)) + 12)\r
39\r
40// String to delete the TFTP progress message to be able to update it :\r
41// (TFTP_PROGRESS_MESSAGE_SIZE-1) '\b'\r
42STATIC 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";\r
43\r
0fcf66a4
QS
44/**\r
45 Check and convert the UINT16 option values of the 'tftp' command\r
46\r
47 @param[in] ValueStr Value as an Unicode encoded string\r
48 @param[out] Value UINT16 value\r
49\r
50 @return TRUE The value was returned.\r
51 @return FALSE A parsing error occured.\r
52**/\r
ba0014b9
LG
53STATIC\r
54BOOLEAN\r
0fcf66a4 55StringToUint16 (\r
68074260
RC
56 IN CONST CHAR16 *ValueStr,\r
57 OUT UINT16 *Value\r
58 );\r
59\r
0fcf66a4
QS
60/**\r
61 Get the name of the NIC.\r
62\r
63 @param[in] ControllerHandle The network physical device handle.\r
64 @param[in] NicNumber The network physical device number.\r
65 @param[out] NicName Address where to store the NIC name.\r
66 The memory area has to be at least\r
ba0014b9 67 IP4_CONFIG2_INTERFACE_INFO_NAME_LENGTH\r
6add86ab 68 double byte wide.\r
0fcf66a4
QS
69\r
70 @return EFI_SUCCESS The name of the NIC was returned.\r
71 @return Others The creation of the child for the Managed\r
72 Network Service failed or the opening of\r
73 the Managed Network Protocol failed or\r
74 the operational parameters for the\r
75 Managed Network Protocol could not be\r
76 read.\r
77**/\r
ba0014b9
LG
78STATIC\r
79EFI_STATUS\r
0fcf66a4 80GetNicName (\r
68074260 81 IN EFI_HANDLE ControllerHandle,\r
a0f2af3a 82 IN UINTN NicNumber,\r
68074260
RC
83 OUT CHAR16 *NicName\r
84 );\r
85\r
0fcf66a4
QS
86/**\r
87 Create a child for the service identified by its service binding protocol GUID\r
88 and get from the child the interface of the protocol identified by its GUID.\r
89\r
90 @param[in] ControllerHandle Controller handle.\r
91 @param[in] ServiceBindingProtocolGuid Service binding protocol GUID of the\r
92 service to be created.\r
93 @param[in] ProtocolGuid GUID of the protocol to be open.\r
94 @param[out] ChildHandle Address where the handler of the\r
95 created child is returned. NULL is\r
96 returned in case of error.\r
97 @param[out] Interface Address where a pointer to the\r
98 protocol interface is returned in\r
99 case of success.\r
100\r
101 @return EFI_SUCCESS The child was created and the protocol opened.\r
102 @return Others Either the creation of the child or the opening\r
103 of the protocol failed.\r
104**/\r
ba0014b9
LG
105STATIC\r
106EFI_STATUS\r
0fcf66a4 107CreateServiceChildAndOpenProtocol (\r
68074260
RC
108 IN EFI_HANDLE ControllerHandle,\r
109 IN EFI_GUID *ServiceBindingProtocolGuid,\r
110 IN EFI_GUID *ProtocolGuid,\r
111 OUT EFI_HANDLE *ChildHandle,\r
112 OUT VOID **Interface\r
113 );\r
114\r
0fcf66a4
QS
115/**\r
116 Close the protocol identified by its GUID on the child handle of the service\r
117 identified by its service binding protocol GUID, then destroy the child\r
118 handle.\r
119\r
120 @param[in] ControllerHandle Controller handle.\r
121 @param[in] ServiceBindingProtocolGuid Service binding protocol GUID of the\r
122 service to be destroyed.\r
123 @param[in] ProtocolGuid GUID of the protocol to be closed.\r
124 @param[in] ChildHandle Handle of the child to be destroyed.\r
125\r
126**/\r
ba0014b9
LG
127STATIC\r
128VOID\r
0fcf66a4 129CloseProtocolAndDestroyServiceChild (\r
68074260
RC
130 IN EFI_HANDLE ControllerHandle,\r
131 IN EFI_GUID *ServiceBindingProtocolGuid,\r
132 IN EFI_GUID *ProtocolGuid,\r
133 IN EFI_HANDLE ChildHandle\r
134 );\r
135\r
0fcf66a4
QS
136/**\r
137 Worker function that gets the size in numbers of bytes of a file from a TFTP\r
138 server before to download the file.\r
139\r
140 @param[in] Mtftp4 MTFTP4 protocol interface\r
141 @param[in] FilePath Path of the file, ASCII encoded\r
142 @param[out] FileSize Address where to store the file size in number of\r
143 bytes.\r
144\r
145 @retval EFI_SUCCESS The size of the file was returned.\r
146 @retval EFI_UNSUPPORTED The server does not support the "tsize" option.\r
147 @retval Others Error when retrieving the information from the server\r
148 (see EFI_MTFTP4_PROTOCOL.GetInfo() status codes)\r
149 or error when parsing the response of the server.\r
150**/\r
ba0014b9
LG
151STATIC\r
152EFI_STATUS\r
0fcf66a4 153GetFileSize (\r
68074260
RC
154 IN EFI_MTFTP4_PROTOCOL *Mtftp4,\r
155 IN CONST CHAR8 *FilePath,\r
156 OUT UINTN *FileSize\r
157 );\r
158\r
0fcf66a4
QS
159/**\r
160 Worker function that download the data of a file from a TFTP server given\r
161 the path of the file and its size.\r
162\r
163 @param[in] Mtftp4 MTFTP4 protocol interface\r
164 @param[in] FilePath Path of the file, Unicode encoded\r
165 @param[in] AsciiFilePath Path of the file, ASCII encoded\r
166 @param[in] FileSize Size of the file in number of bytes\r
2be45bfe 167 @param[in] BlockSize Value of the TFTP blksize option\r
0fcf66a4
QS
168 @param[out] Data Address where to store the address of the buffer\r
169 where the data of the file were downloaded in\r
170 case of success.\r
171\r
172 @retval EFI_SUCCESS The file was downloaded.\r
173 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.\r
174 @retval Others The downloading of the file from the server failed\r
175 (see EFI_MTFTP4_PROTOCOL.ReadFile() status codes).\r
176\r
177**/\r
ba0014b9
LG
178STATIC\r
179EFI_STATUS\r
0fcf66a4 180DownloadFile (\r
68074260
RC
181 IN EFI_MTFTP4_PROTOCOL *Mtftp4,\r
182 IN CONST CHAR16 *FilePath,\r
183 IN CONST CHAR8 *AsciiFilePath,\r
184 IN UINTN FileSize,\r
2be45bfe 185 IN UINT16 BlockSize,\r
68074260
RC
186 OUT VOID **Data\r
187 );\r
188\r
0fcf66a4
QS
189/**\r
190 Update the progress of a file download\r
191 This procedure is called each time a new TFTP packet is received.\r
192\r
193 @param[in] This MTFTP4 protocol interface\r
194 @param[in] Token Parameters for the download of the file\r
195 @param[in] PacketLen Length of the packet\r
196 @param[in] Packet Address of the packet\r
197\r
198 @retval EFI_SUCCESS All packets are accepted.\r
199\r
200**/\r
ba0014b9
LG
201STATIC\r
202EFI_STATUS\r
7367e66f 203EFIAPI\r
0fcf66a4 204CheckPacket (\r
68074260
RC
205 IN EFI_MTFTP4_PROTOCOL *This,\r
206 IN EFI_MTFTP4_TOKEN *Token,\r
207 IN UINT16 PacketLen,\r
208 IN EFI_MTFTP4_PACKET *Packet\r
209 );\r
210\r
211EFI_MTFTP4_CONFIG_DATA DefaultMtftp4ConfigData = {\r
212 TRUE, // Use default setting\r
213 { { 0, 0, 0, 0 } }, // StationIp - Not relevant as UseDefaultSetting=TRUE\r
214 { { 0, 0, 0, 0 } }, // SubnetMask - Not relevant as UseDefaultSetting=TRUE\r
215 0, // LocalPort - Automatically assigned port number.\r
216 { { 0, 0, 0, 0 } }, // GatewayIp - Not relevant as UseDefaultSetting=TRUE\r
217 { { 0, 0, 0, 0 } }, // ServerIp - Not known yet\r
218 69, // InitialServerPort - Standard TFTP server port\r
219 6, // TryCount - Max number of retransmissions.\r
220 4 // TimeoutValue - Retransmission timeout in seconds.\r
221};\r
222\r
223STATIC CONST SHELL_PARAM_ITEM ParamList[] = {\r
224 {L"-i", TypeValue},\r
225 {L"-l", TypeValue},\r
226 {L"-r", TypeValue},\r
227 {L"-c", TypeValue},\r
228 {L"-t", TypeValue},\r
2be45bfe 229 {L"-s", TypeValue},\r
68074260
RC
230 {NULL , TypeMax}\r
231 };\r
232\r
2be45bfe
FS
233///\r
234/// The default block size (512) of tftp is defined in the RFC1350.\r
235///\r
236#define MTFTP_DEFAULT_BLKSIZE 512\r
237///\r
238/// The valid range of block size option is defined in the RFC2348.\r
239///\r
240#define MTFTP_MIN_BLKSIZE 8\r
241#define MTFTP_MAX_BLKSIZE 65464\r
242\r
243\r
68074260
RC
244/**\r
245 Function for 'tftp' command.\r
246\r
247 @param[in] ImageHandle Handle to the Image (NULL if Internal).\r
248 @param[in] SystemTable Pointer to the System Table (NULL if Internal).\r
249\r
250 @return SHELL_SUCCESS The 'tftp' command completed successfully.\r
251 @return SHELL_ABORTED The Shell Library initialization failed.\r
252 @return SHELL_INVALID_PARAMETER At least one of the command's arguments is\r
253 not valid.\r
254 @return SHELL_OUT_OF_RESOURCES A memory allocation failed.\r
255 @return SHELL_NOT_FOUND Network Interface Card not found or server\r
256 error or file error.\r
257\r
258**/\r
259SHELL_STATUS\r
09610023 260RunTftp (\r
68074260
RC
261 IN EFI_HANDLE ImageHandle,\r
262 IN EFI_SYSTEM_TABLE *SystemTable\r
263 )\r
264{\r
265 SHELL_STATUS ShellStatus;\r
266 EFI_STATUS Status;\r
267 LIST_ENTRY *CheckPackage;\r
268 CHAR16 *ProblemParam;\r
269 UINTN ParamCount;\r
270 CONST CHAR16 *UserNicName;\r
271 BOOLEAN NicFound;\r
272 CONST CHAR16 *ValueStr;\r
273 CONST CHAR16 *RemoteFilePath;\r
274 CHAR8 *AsciiRemoteFilePath;\r
46213c8e 275 UINTN FilePathSize;\r
68074260
RC
276 CONST CHAR16 *Walker;\r
277 CONST CHAR16 *LocalFilePath;\r
278 EFI_MTFTP4_CONFIG_DATA Mtftp4ConfigData;\r
279 EFI_HANDLE *Handles;\r
280 UINTN HandleCount;\r
281 UINTN NicNumber;\r
6add86ab 282 CHAR16 NicName[IP4_CONFIG2_INTERFACE_INFO_NAME_LENGTH];\r
68074260
RC
283 EFI_HANDLE ControllerHandle;\r
284 EFI_HANDLE Mtftp4ChildHandle;\r
285 EFI_MTFTP4_PROTOCOL *Mtftp4;\r
286 UINTN FileSize;\r
34cd44d4 287 UINTN DataSize;\r
68074260
RC
288 VOID *Data;\r
289 SHELL_FILE_HANDLE FileHandle;\r
2be45bfe 290 UINT16 BlockSize;\r
68074260
RC
291\r
292 ShellStatus = SHELL_INVALID_PARAMETER;\r
293 ProblemParam = NULL;\r
294 NicFound = FALSE;\r
295 AsciiRemoteFilePath = NULL;\r
296 Handles = NULL;\r
6add86ab 297 FileSize = 0;\r
34cd44d4 298 DataSize = 0;\r
2be45bfe 299 BlockSize = MTFTP_DEFAULT_BLKSIZE;\r
68074260
RC
300\r
301 //\r
302 // Initialize the Shell library (we must be in non-auto-init...)\r
303 //\r
304 Status = ShellInitialize ();\r
305 if (EFI_ERROR (Status)) {\r
306 ASSERT_EFI_ERROR (Status);\r
307 return SHELL_ABORTED;\r
308 }\r
309\r
310 //\r
311 // Parse the command line.\r
312 //\r
313 Status = ShellCommandLineParse (ParamList, &CheckPackage, &ProblemParam, TRUE);\r
314 if (EFI_ERROR (Status)) {\r
315 if ((Status == EFI_VOLUME_CORRUPTED) &&\r
316 (ProblemParam != NULL) ) {\r
317 ShellPrintHiiEx (\r
09610023 318 -1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM), mTftpHiiHandle,\r
68074260
RC
319 L"tftp", ProblemParam\r
320 );\r
321 FreePool (ProblemParam);\r
322 } else {\r
323 ASSERT (FALSE);\r
324 }\r
325 goto Error;\r
326 }\r
327\r
328 //\r
329 // Check the number of parameters\r
330 //\r
331 ParamCount = ShellCommandLineGetCount (CheckPackage);\r
332 if (ParamCount > 4) {\r
333 ShellPrintHiiEx (\r
334 -1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_MANY),\r
09610023 335 mTftpHiiHandle, L"tftp"\r
68074260
RC
336 );\r
337 goto Error;\r
338 }\r
339 if (ParamCount < 3) {\r
340 ShellPrintHiiEx (\r
341 -1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_FEW),\r
09610023 342 mTftpHiiHandle, L"tftp"\r
68074260
RC
343 );\r
344 goto Error;\r
345 }\r
346\r
ba601bdd 347 CopyMem (&Mtftp4ConfigData, &DefaultMtftp4ConfigData, sizeof (EFI_MTFTP4_CONFIG_DATA));\r
68074260
RC
348\r
349 //\r
350 // Check the host IPv4 address\r
351 //\r
352 ValueStr = ShellCommandLineGetRawValue (CheckPackage, 1);\r
353 Status = NetLibStrToIp4 (ValueStr, &Mtftp4ConfigData.ServerIp);\r
354 if (EFI_ERROR (Status)) {\r
355 ShellPrintHiiEx (\r
356 -1, -1, NULL, STRING_TOKEN (STR_GEN_PARAM_INV),\r
09610023 357 mTftpHiiHandle, L"tftp", ValueStr\r
68074260
RC
358 );\r
359 goto Error;\r
360 }\r
361\r
362 RemoteFilePath = ShellCommandLineGetRawValue (CheckPackage, 2);\r
c6173804 363 ASSERT(RemoteFilePath != NULL);\r
46213c8e
SZ
364 FilePathSize = StrLen (RemoteFilePath) + 1;\r
365 AsciiRemoteFilePath = AllocatePool (FilePathSize);\r
68074260
RC
366 if (AsciiRemoteFilePath == NULL) {\r
367 ShellStatus = SHELL_OUT_OF_RESOURCES;\r
368 goto Error;\r
369 }\r
46213c8e 370 UnicodeStrToAsciiStrS (RemoteFilePath, AsciiRemoteFilePath, FilePathSize);\r
68074260
RC
371\r
372 if (ParamCount == 4) {\r
373 LocalFilePath = ShellCommandLineGetRawValue (CheckPackage, 3);\r
374 } else {\r
375 Walker = RemoteFilePath + StrLen (RemoteFilePath);\r
376 while ((--Walker) >= RemoteFilePath) {\r
377 if ((*Walker == L'\\') ||\r
378 (*Walker == L'/' ) ) {\r
379 break;\r
380 }\r
381 }\r
382 LocalFilePath = Walker + 1;\r
383 }\r
384\r
385 //\r
386 // Get the name of the Network Interface Card to be used if any.\r
387 //\r
388 UserNicName = ShellCommandLineGetValue (CheckPackage, L"-i");\r
389\r
390 ValueStr = ShellCommandLineGetValue (CheckPackage, L"-l");\r
391 if (ValueStr != NULL) {\r
392 if (!StringToUint16 (ValueStr, &Mtftp4ConfigData.LocalPort)) {\r
393 goto Error;\r
394 }\r
395 }\r
396\r
397 ValueStr = ShellCommandLineGetValue (CheckPackage, L"-r");\r
398 if (ValueStr != NULL) {\r
399 if (!StringToUint16 (ValueStr, &Mtftp4ConfigData.InitialServerPort)) {\r
400 goto Error;\r
401 }\r
402 }\r
403\r
404 ValueStr = ShellCommandLineGetValue (CheckPackage, L"-c");\r
405 if (ValueStr != NULL) {\r
406 if (!StringToUint16 (ValueStr, &Mtftp4ConfigData.TryCount)) {\r
407 goto Error;\r
408 }\r
409 }\r
410\r
411 ValueStr = ShellCommandLineGetValue (CheckPackage, L"-t");\r
412 if (ValueStr != NULL) {\r
413 if (!StringToUint16 (ValueStr, &Mtftp4ConfigData.TimeoutValue)) {\r
414 goto Error;\r
415 }\r
416 if (Mtftp4ConfigData.TimeoutValue == 0) {\r
417 ShellPrintHiiEx (\r
418 -1, -1, NULL, STRING_TOKEN (STR_GEN_PARAM_INV),\r
09610023 419 mTftpHiiHandle, L"tftp", ValueStr\r
68074260
RC
420 );\r
421 goto Error;\r
422 }\r
423 }\r
424\r
2be45bfe
FS
425 ValueStr = ShellCommandLineGetValue (CheckPackage, L"-s");\r
426 if (ValueStr != NULL) {\r
427 if (!StringToUint16 (ValueStr, &BlockSize)) {\r
428 goto Error;\r
429 }\r
430 if (BlockSize < MTFTP_MIN_BLKSIZE || BlockSize > MTFTP_MAX_BLKSIZE) {\r
431 ShellPrintHiiEx (\r
432 -1, -1, NULL, STRING_TOKEN (STR_GEN_PARAM_INV),\r
09610023 433 mTftpHiiHandle, L"tftp", ValueStr\r
2be45bfe
FS
434 );\r
435 goto Error;\r
436 }\r
437 }\r
438\r
68074260
RC
439 //\r
440 // Locate all MTFTP4 Service Binding protocols\r
441 //\r
442 ShellStatus = SHELL_NOT_FOUND;\r
443 Status = gBS->LocateHandleBuffer (\r
444 ByProtocol,\r
445 &gEfiManagedNetworkServiceBindingProtocolGuid,\r
446 NULL,\r
447 &HandleCount,\r
448 &Handles\r
449 );\r
450 if (EFI_ERROR (Status) || (HandleCount == 0)) {\r
451 ShellPrintHiiEx (\r
452 -1, -1, NULL, STRING_TOKEN (STR_TFTP_ERR_NO_NIC),\r
09610023 453 mTftpHiiHandle\r
68074260
RC
454 );\r
455 goto Error;\r
456 }\r
457\r
458 for (NicNumber = 0;\r
459 (NicNumber < HandleCount) && (ShellStatus != SHELL_SUCCESS);\r
460 NicNumber++) {\r
461 ControllerHandle = Handles[NicNumber];\r
462 Data = NULL;\r
463\r
464 Status = GetNicName (ControllerHandle, NicNumber, NicName);\r
465 if (EFI_ERROR (Status)) {\r
466 ShellPrintHiiEx (\r
467 -1, -1, NULL, STRING_TOKEN (STR_TFTP_ERR_NIC_NAME),\r
09610023 468 mTftpHiiHandle, NicNumber, Status\r
68074260
RC
469 );\r
470 continue;\r
471 }\r
472\r
473 if (UserNicName != NULL) {\r
474 if (StrCmp (NicName, UserNicName) != 0) {\r
475 continue;\r
476 }\r
477 NicFound = TRUE;\r
478 }\r
479\r
480 Status = CreateServiceChildAndOpenProtocol (\r
481 ControllerHandle,\r
482 &gEfiMtftp4ServiceBindingProtocolGuid,\r
483 &gEfiMtftp4ProtocolGuid,\r
484 &Mtftp4ChildHandle,\r
485 (VOID**)&Mtftp4\r
486 );\r
487 if (EFI_ERROR (Status)) {\r
488 ShellPrintHiiEx (\r
489 -1, -1, NULL, STRING_TOKEN (STR_TFTP_ERR_OPEN_PROTOCOL),\r
09610023 490 mTftpHiiHandle, NicName, Status\r
68074260
RC
491 );\r
492 continue;\r
493 }\r
494\r
495 Status = Mtftp4->Configure (Mtftp4, &Mtftp4ConfigData);\r
496 if (EFI_ERROR (Status)) {\r
497 ShellPrintHiiEx (\r
498 -1, -1, NULL, STRING_TOKEN (STR_TFTP_ERR_CONFIGURE),\r
09610023 499 mTftpHiiHandle, NicName, Status\r
68074260
RC
500 );\r
501 goto NextHandle;\r
502 }\r
503\r
504 Status = GetFileSize (Mtftp4, AsciiRemoteFilePath, &FileSize);\r
505 if (EFI_ERROR (Status)) {\r
506 ShellPrintHiiEx (\r
507 -1, -1, NULL, STRING_TOKEN (STR_TFTP_ERR_FILE_SIZE),\r
09610023 508 mTftpHiiHandle, RemoteFilePath, NicName, Status\r
68074260
RC
509 );\r
510 goto NextHandle;\r
511 }\r
512\r
2be45bfe 513 Status = DownloadFile (Mtftp4, RemoteFilePath, AsciiRemoteFilePath, FileSize, BlockSize, &Data);\r
68074260
RC
514 if (EFI_ERROR (Status)) {\r
515 ShellPrintHiiEx (\r
516 -1, -1, NULL, STRING_TOKEN (STR_TFTP_ERR_DOWNLOAD),\r
09610023 517 mTftpHiiHandle, RemoteFilePath, NicName, Status\r
68074260
RC
518 );\r
519 goto NextHandle;\r
520 }\r
521\r
280f49b8
JW
522 DataSize = FileSize;\r
523\r
68074260
RC
524 if (!EFI_ERROR (ShellFileExists (LocalFilePath))) {\r
525 ShellDeleteFileByName (LocalFilePath);\r
526 }\r
527\r
528 Status = ShellOpenFileByName (\r
529 LocalFilePath,\r
530 &FileHandle,\r
531 EFI_FILE_MODE_CREATE |\r
532 EFI_FILE_MODE_WRITE |\r
533 EFI_FILE_MODE_READ,\r
534 0\r
535 );\r
536 if (EFI_ERROR (Status)) {\r
537 ShellPrintHiiEx (\r
538 -1, -1, NULL, STRING_TOKEN (STR_GEN_FILE_OPEN_FAIL),\r
09610023 539 mTftpHiiHandle, L"tftp", LocalFilePath\r
68074260
RC
540 );\r
541 goto NextHandle;\r
542 }\r
543\r
544 Status = ShellWriteFile (FileHandle, &FileSize, Data);\r
545 if (!EFI_ERROR (Status)) {\r
546 ShellStatus = SHELL_SUCCESS;\r
547 } else {\r
548 ShellPrintHiiEx (\r
549 -1, -1, NULL, STRING_TOKEN (STR_TFTP_ERR_WRITE),\r
09610023 550 mTftpHiiHandle, LocalFilePath, Status\r
68074260
RC
551 );\r
552 }\r
553 ShellCloseFile (&FileHandle);\r
554\r
555 NextHandle:\r
556\r
557 if (Data != NULL) {\r
34cd44d4 558 gBS->FreePages ((EFI_PHYSICAL_ADDRESS)(UINTN)Data, EFI_SIZE_TO_PAGES (DataSize));\r
68074260
RC
559 }\r
560\r
561 CloseProtocolAndDestroyServiceChild (\r
562 ControllerHandle,\r
563 &gEfiMtftp4ServiceBindingProtocolGuid,\r
564 &gEfiMtftp4ProtocolGuid,\r
565 Mtftp4ChildHandle\r
566 );\r
567 }\r
568\r
a0f2af3a 569 if ((UserNicName != NULL) && (!NicFound)) {\r
68074260
RC
570 ShellPrintHiiEx (\r
571 -1, -1, NULL, STRING_TOKEN (STR_TFTP_ERR_NIC_NOT_FOUND),\r
09610023 572 mTftpHiiHandle, UserNicName\r
68074260
RC
573 );\r
574 }\r
575\r
576 Error:\r
577\r
578 ShellCommandLineFreeVarList (CheckPackage);\r
579 if (AsciiRemoteFilePath != NULL) {\r
580 FreePool (AsciiRemoteFilePath);\r
581 }\r
582 if (Handles != NULL) {\r
583 FreePool (Handles);\r
584 }\r
585\r
586 return ShellStatus;\r
587}\r
588\r
589/**\r
590 Check and convert the UINT16 option values of the 'tftp' command\r
591\r
592 @param[in] ValueStr Value as an Unicode encoded string\r
593 @param[out] Value UINT16 value\r
594\r
595 @return TRUE The value was returned.\r
596 @return FALSE A parsing error occured.\r
597**/\r
598STATIC\r
599BOOLEAN\r
600StringToUint16 (\r
601 IN CONST CHAR16 *ValueStr,\r
602 OUT UINT16 *Value\r
603 )\r
604{\r
605 UINTN Val;\r
606\r
607 Val = ShellStrToUintn (ValueStr);\r
608 if (Val > MAX_UINT16) {\r
609 ShellPrintHiiEx (\r
610 -1, -1, NULL, STRING_TOKEN (STR_GEN_PARAM_INV),\r
09610023 611 mTftpHiiHandle, L"tftp", ValueStr\r
68074260
RC
612 );\r
613 return FALSE;\r
614 }\r
615\r
6add86ab 616 *Value = (UINT16)Val;\r
68074260
RC
617 return TRUE;\r
618}\r
619\r
620/**\r
621 Get the name of the NIC.\r
622\r
623 @param[in] ControllerHandle The network physical device handle.\r
624 @param[in] NicNumber The network physical device number.\r
625 @param[out] NicName Address where to store the NIC name.\r
626 The memory area has to be at least\r
ba0014b9 627 IP4_CONFIG2_INTERFACE_INFO_NAME_LENGTH\r
6add86ab 628 double byte wide.\r
68074260
RC
629\r
630 @return EFI_SUCCESS The name of the NIC was returned.\r
631 @return Others The creation of the child for the Managed\r
632 Network Service failed or the opening of\r
633 the Managed Network Protocol failed or\r
634 the operational parameters for the\r
635 Managed Network Protocol could not be\r
636 read.\r
637**/\r
638STATIC\r
639EFI_STATUS\r
640GetNicName (\r
641 IN EFI_HANDLE ControllerHandle,\r
642 IN UINTN NicNumber,\r
643 OUT CHAR16 *NicName\r
644 )\r
645{\r
646 EFI_STATUS Status;\r
647 EFI_HANDLE MnpHandle;\r
648 EFI_MANAGED_NETWORK_PROTOCOL *Mnp;\r
649 EFI_SIMPLE_NETWORK_MODE SnpMode;\r
650\r
651 Status = CreateServiceChildAndOpenProtocol (\r
652 ControllerHandle,\r
653 &gEfiManagedNetworkServiceBindingProtocolGuid,\r
654 &gEfiManagedNetworkProtocolGuid,\r
655 &MnpHandle,\r
656 (VOID**)&Mnp\r
657 );\r
658 if (EFI_ERROR (Status)) {\r
659 goto Error;\r
660 }\r
661\r
662 Status = Mnp->GetModeData (Mnp, NULL, &SnpMode);\r
663 if (EFI_ERROR (Status) && (Status != EFI_NOT_STARTED)) {\r
664 goto Error;\r
665 }\r
666\r
667 UnicodeSPrint (\r
668 NicName,\r
6add86ab 669 IP4_CONFIG2_INTERFACE_INFO_NAME_LENGTH,\r
68074260
RC
670 SnpMode.IfType == NET_IFTYPE_ETHERNET ?\r
671 L"eth%d" :\r
672 L"unk%d" ,\r
673 NicNumber\r
674 );\r
675\r
676 Status = EFI_SUCCESS;\r
677\r
678Error:\r
679\r
680 if (MnpHandle != NULL) {\r
681 CloseProtocolAndDestroyServiceChild (\r
682 ControllerHandle,\r
683 &gEfiManagedNetworkServiceBindingProtocolGuid,\r
684 &gEfiManagedNetworkProtocolGuid,\r
685 MnpHandle\r
686 );\r
687 }\r
688\r
689 return Status;\r
690}\r
691\r
692/**\r
693 Create a child for the service identified by its service binding protocol GUID\r
694 and get from the child the interface of the protocol identified by its GUID.\r
695\r
696 @param[in] ControllerHandle Controller handle.\r
697 @param[in] ServiceBindingProtocolGuid Service binding protocol GUID of the\r
698 service to be created.\r
699 @param[in] ProtocolGuid GUID of the protocol to be open.\r
700 @param[out] ChildHandle Address where the handler of the\r
701 created child is returned. NULL is\r
702 returned in case of error.\r
703 @param[out] Interface Address where a pointer to the\r
704 protocol interface is returned in\r
705 case of success.\r
706\r
707 @return EFI_SUCCESS The child was created and the protocol opened.\r
708 @return Others Either the creation of the child or the opening\r
709 of the protocol failed.\r
710**/\r
711STATIC\r
712EFI_STATUS\r
713CreateServiceChildAndOpenProtocol (\r
714 IN EFI_HANDLE ControllerHandle,\r
715 IN EFI_GUID *ServiceBindingProtocolGuid,\r
716 IN EFI_GUID *ProtocolGuid,\r
717 OUT EFI_HANDLE *ChildHandle,\r
718 OUT VOID **Interface\r
719 )\r
720{\r
721 EFI_STATUS Status;\r
722\r
723 *ChildHandle = NULL;\r
724 Status = NetLibCreateServiceChild (\r
725 ControllerHandle,\r
726 gImageHandle,\r
727 ServiceBindingProtocolGuid,\r
728 ChildHandle\r
729 );\r
730 if (!EFI_ERROR (Status)) {\r
731 Status = gBS->OpenProtocol (\r
732 *ChildHandle,\r
733 ProtocolGuid,\r
734 Interface,\r
735 gImageHandle,\r
736 ControllerHandle,\r
737 EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
738 );\r
739 if (EFI_ERROR (Status)) {\r
740 NetLibDestroyServiceChild (\r
741 ControllerHandle,\r
742 gImageHandle,\r
743 ServiceBindingProtocolGuid,\r
744 *ChildHandle\r
745 );\r
746 *ChildHandle = NULL;\r
747 }\r
748 }\r
749\r
750 return Status;\r
751}\r
752\r
753/**\r
754 Close the protocol identified by its GUID on the child handle of the service\r
755 identified by its service binding protocol GUID, then destroy the child\r
756 handle.\r
757\r
758 @param[in] ControllerHandle Controller handle.\r
759 @param[in] ServiceBindingProtocolGuid Service binding protocol GUID of the\r
760 service to be destroyed.\r
761 @param[in] ProtocolGuid GUID of the protocol to be closed.\r
762 @param[in] ChildHandle Handle of the child to be destroyed.\r
763\r
764**/\r
765STATIC\r
766VOID\r
767CloseProtocolAndDestroyServiceChild (\r
768 IN EFI_HANDLE ControllerHandle,\r
769 IN EFI_GUID *ServiceBindingProtocolGuid,\r
770 IN EFI_GUID *ProtocolGuid,\r
771 IN EFI_HANDLE ChildHandle\r
772 )\r
773{\r
774 gBS->CloseProtocol (\r
775 ChildHandle,\r
776 ProtocolGuid,\r
777 gImageHandle,\r
778 ControllerHandle\r
779 );\r
780\r
781 NetLibDestroyServiceChild (\r
782 ControllerHandle,\r
783 gImageHandle,\r
784 ServiceBindingProtocolGuid,\r
785 ChildHandle\r
786 );\r
787}\r
788\r
789/**\r
790 Worker function that gets the size in numbers of bytes of a file from a TFTP\r
791 server before to download the file.\r
792\r
793 @param[in] Mtftp4 MTFTP4 protocol interface\r
794 @param[in] FilePath Path of the file, ASCII encoded\r
795 @param[out] FileSize Address where to store the file size in number of\r
796 bytes.\r
797\r
798 @retval EFI_SUCCESS The size of the file was returned.\r
799 @retval EFI_UNSUPPORTED The server does not support the "tsize" option.\r
800 @retval Others Error when retrieving the information from the server\r
801 (see EFI_MTFTP4_PROTOCOL.GetInfo() status codes)\r
802 or error when parsing the response of the server.\r
803**/\r
804STATIC\r
805EFI_STATUS\r
806GetFileSize (\r
807 IN EFI_MTFTP4_PROTOCOL *Mtftp4,\r
808 IN CONST CHAR8 *FilePath,\r
809 OUT UINTN *FileSize\r
810 )\r
811{\r
812 EFI_STATUS Status;\r
813 EFI_MTFTP4_OPTION ReqOpt[1];\r
814 EFI_MTFTP4_PACKET *Packet;\r
815 UINT32 PktLen;\r
816 EFI_MTFTP4_OPTION *TableOfOptions;\r
817 EFI_MTFTP4_OPTION *Option;\r
818 UINT32 OptCnt;\r
819 UINT8 OptBuf[128];\r
820\r
821 ReqOpt[0].OptionStr = (UINT8*)"tsize";\r
822 OptBuf[0] = '0';\r
823 OptBuf[1] = 0;\r
824 ReqOpt[0].ValueStr = OptBuf;\r
825\r
826 Status = Mtftp4->GetInfo (\r
827 Mtftp4,\r
828 NULL,\r
829 (UINT8*)FilePath,\r
830 NULL,\r
831 1,\r
832 ReqOpt,\r
833 &PktLen,\r
834 &Packet\r
835 );\r
836\r
837 if (EFI_ERROR (Status)) {\r
838 goto Error;\r
839 }\r
840\r
841 Status = Mtftp4->ParseOptions (\r
842 Mtftp4,\r
843 PktLen,\r
844 Packet,\r
845 (UINT32 *) &OptCnt,\r
846 &TableOfOptions\r
847 );\r
848 if (EFI_ERROR (Status)) {\r
849 goto Error;\r
850 }\r
851\r
852 Option = TableOfOptions;\r
853 while (OptCnt != 0) {\r
854 if (AsciiStrnCmp ((CHAR8 *)Option->OptionStr, "tsize", 5) == 0) {\r
855 *FileSize = AsciiStrDecimalToUintn ((CHAR8 *)Option->ValueStr);\r
856 break;\r
857 }\r
858 OptCnt--;\r
859 Option++;\r
860 }\r
861 FreePool (TableOfOptions);\r
862\r
863 if (OptCnt == 0) {\r
864 Status = EFI_UNSUPPORTED;\r
865 }\r
866\r
867Error :\r
868\r
869 return Status;\r
870}\r
871\r
872/**\r
873 Worker function that download the data of a file from a TFTP server given\r
874 the path of the file and its size.\r
875\r
876 @param[in] Mtftp4 MTFTP4 protocol interface\r
877 @param[in] FilePath Path of the file, Unicode encoded\r
878 @param[in] AsciiFilePath Path of the file, ASCII encoded\r
879 @param[in] FileSize Size of the file in number of bytes\r
2be45bfe 880 @param[in] BlockSize Value of the TFTP blksize option\r
68074260
RC
881 @param[out] Data Address where to store the address of the buffer\r
882 where the data of the file were downloaded in\r
883 case of success.\r
884\r
885 @retval EFI_SUCCESS The file was downloaded.\r
886 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.\r
887 @retval Others The downloading of the file from the server failed\r
888 (see EFI_MTFTP4_PROTOCOL.ReadFile() status codes).\r
889\r
890**/\r
891STATIC\r
892EFI_STATUS\r
893DownloadFile (\r
894 IN EFI_MTFTP4_PROTOCOL *Mtftp4,\r
895 IN CONST CHAR16 *FilePath,\r
896 IN CONST CHAR8 *AsciiFilePath,\r
897 IN UINTN FileSize,\r
2be45bfe 898 IN UINT16 BlockSize,\r
68074260
RC
899 OUT VOID **Data\r
900 )\r
901{\r
902 EFI_STATUS Status;\r
903 EFI_PHYSICAL_ADDRESS PagesAddress;\r
904 VOID *Buffer;\r
905 DOWNLOAD_CONTEXT *TftpContext;\r
906 EFI_MTFTP4_TOKEN Mtftp4Token;\r
2be45bfe
FS
907 EFI_MTFTP4_OPTION ReqOpt;\r
908 UINT8 OptBuf[10];\r
68074260
RC
909\r
910 // Downloaded file can be large. BS.AllocatePages() is more faster\r
911 // than AllocatePool() and avoid fragmentation.\r
912 // The downloaded file could be an EFI application. Marking the\r
913 // allocated page as EfiBootServicesCode would allow to execute a\r
914 // potential downloaded EFI application.\r
915 Status = gBS->AllocatePages (\r
916 AllocateAnyPages,\r
917 EfiBootServicesCode,\r
918 EFI_SIZE_TO_PAGES (FileSize),\r
919 &PagesAddress\r
920 );\r
921 if (EFI_ERROR (Status)) {\r
922 return Status;\r
923 }\r
924\r
925 Buffer = (VOID*)(UINTN)PagesAddress;\r
926 TftpContext = AllocatePool (sizeof (DOWNLOAD_CONTEXT));\r
927 if (TftpContext == NULL) {\r
928 Status = EFI_OUT_OF_RESOURCES;\r
929 goto Error;\r
930 }\r
931 TftpContext->FileSize = FileSize;\r
932 TftpContext->DownloadedNbOfBytes = 0;\r
933 TftpContext->LastReportedNbOfBytes = 0;\r
934\r
935 ZeroMem (&Mtftp4Token, sizeof (EFI_MTFTP4_TOKEN));\r
936 Mtftp4Token.Filename = (UINT8*)AsciiFilePath;\r
937 Mtftp4Token.BufferSize = FileSize;\r
938 Mtftp4Token.Buffer = Buffer;\r
939 Mtftp4Token.CheckPacket = CheckPacket;\r
940 Mtftp4Token.Context = (VOID*)TftpContext;\r
2be45bfe 941 if (BlockSize != MTFTP_DEFAULT_BLKSIZE) {\r
6987d7aa
FS
942 ReqOpt.OptionStr = (UINT8 *) "blksize";\r
943 AsciiSPrint ((CHAR8 *)OptBuf, sizeof (OptBuf), "%d", BlockSize);\r
2be45bfe
FS
944 ReqOpt.ValueStr = OptBuf;\r
945\r
946 Mtftp4Token.OptionCount = 1;\r
947 Mtftp4Token.OptionList = &ReqOpt;\r
948 }\r
68074260
RC
949\r
950 ShellPrintHiiEx (\r
951 -1, -1, NULL, STRING_TOKEN (STR_TFTP_DOWNLOADING),\r
09610023 952 mTftpHiiHandle, FilePath\r
68074260
RC
953 );\r
954\r
955 Status = Mtftp4->ReadFile (Mtftp4, &Mtftp4Token);\r
956 ShellPrintHiiEx (\r
957 -1, -1, NULL, STRING_TOKEN (STR_GEN_CRLF),\r
09610023 958 mTftpHiiHandle\r
68074260
RC
959 );\r
960\r
961Error :\r
962\r
963 if (TftpContext == NULL) {\r
964 FreePool (TftpContext);\r
965 }\r
966\r
967 if (EFI_ERROR (Status)) {\r
968 gBS->FreePages (PagesAddress, EFI_SIZE_TO_PAGES (FileSize));\r
969 return Status;\r
970 }\r
971\r
972 *Data = Buffer;\r
973\r
974 return EFI_SUCCESS;\r
975}\r
976\r
977/**\r
978 Update the progress of a file download\r
979 This procedure is called each time a new TFTP packet is received.\r
980\r
981 @param[in] This MTFTP4 protocol interface\r
982 @param[in] Token Parameters for the download of the file\r
983 @param[in] PacketLen Length of the packet\r
984 @param[in] Packet Address of the packet\r
985\r
986 @retval EFI_SUCCESS All packets are accepted.\r
987\r
988**/\r
989STATIC\r
990EFI_STATUS\r
7367e66f 991EFIAPI\r
68074260
RC
992CheckPacket (\r
993 IN EFI_MTFTP4_PROTOCOL *This,\r
994 IN EFI_MTFTP4_TOKEN *Token,\r
995 IN UINT16 PacketLen,\r
996 IN EFI_MTFTP4_PACKET *Packet\r
997 )\r
998{\r
999 DOWNLOAD_CONTEXT *Context;\r
1000 CHAR16 Progress[TFTP_PROGRESS_MESSAGE_SIZE];\r
6add86ab 1001 UINTN NbOfKb;\r
68074260 1002 UINTN Index;\r
310b053a
JC
1003 UINTN LastStep;\r
1004 UINTN Step;\r
da6b8feb 1005 EFI_STATUS Status;\r
68074260
RC
1006\r
1007 if ((NTOHS (Packet->OpCode)) != EFI_MTFTP4_OPCODE_DATA) {\r
1008 return EFI_SUCCESS;\r
1009 }\r
1010\r
1011 Context = (DOWNLOAD_CONTEXT*)Token->Context;\r
1012 if (Context->DownloadedNbOfBytes == 0) {\r
1013 ShellPrintEx (-1, -1, L"%s 0 Kb", mTftpProgressFrame);\r
1014 }\r
1015\r
1016 //\r
1017 // The data in the packet are prepended with two UINT16 :\r
1018 // . OpCode = EFI_MTFTP4_OPCODE_DATA\r
1019 // . Block = the number of this block of data\r
1020 //\r
1021 Context->DownloadedNbOfBytes += PacketLen - sizeof (Packet->OpCode)\r
1022 - sizeof (Packet->Data.Block);\r
1023 NbOfKb = Context->DownloadedNbOfBytes / 1024;\r
1024\r
1025 Progress[0] = L'\0';\r
6add86ab
JC
1026 LastStep = (Context->LastReportedNbOfBytes * TFTP_PROGRESS_SLIDER_STEPS) / Context->FileSize;\r
1027 Step = (Context->DownloadedNbOfBytes * TFTP_PROGRESS_SLIDER_STEPS) / Context->FileSize;\r
1028\r
68074260
RC
1029 if (Step <= LastStep) {\r
1030 return EFI_SUCCESS;\r
1031 }\r
1032\r
1033 ShellPrintEx (-1, -1, L"%s", mTftpProgressDelete);\r
1034\r
da6b8feb
QS
1035 Status = StrCpyS (Progress, TFTP_PROGRESS_MESSAGE_SIZE, mTftpProgressFrame);\r
1036 if (EFI_ERROR(Status)) {\r
1037 return Status;\r
1038 }\r
68074260
RC
1039 for (Index = 1; Index < Step; Index++) {\r
1040 Progress[Index] = L'=';\r
1041 }\r
1042 Progress[Step] = L'>';\r
1043\r
1044 UnicodeSPrint (\r
1045 Progress + (sizeof (mTftpProgressFrame) / sizeof (CHAR16)) - 1,\r
1046 sizeof (Progress) - sizeof (mTftpProgressFrame),\r
1047 L" %7d Kb",\r
1048 NbOfKb\r
1049 );\r
1050 Context->LastReportedNbOfBytes = Context->DownloadedNbOfBytes;\r
1051\r
1052 ShellPrintEx (-1, -1, L"%s", Progress);\r
1053\r
1054 return EFI_SUCCESS;\r
1055}\r
09610023
RN
1056\r
1057/**\r
1058 Retrive HII package list from ImageHandle and publish to HII database.\r
1059\r
1060 @param ImageHandle The image handle of the process.\r
1061\r
1062 @return HII handle.\r
1063**/\r
1064EFI_HANDLE\r
1065InitializeHiiPackage (\r
1066 EFI_HANDLE ImageHandle\r
1067 )\r
1068{\r
1069 EFI_STATUS Status;\r
1070 EFI_HII_PACKAGE_LIST_HEADER *PackageList;\r
1071 EFI_HANDLE HiiHandle;\r
1072\r
1073 //\r
1074 // Retrieve HII package list from ImageHandle\r
1075 //\r
1076 Status = gBS->OpenProtocol (\r
1077 ImageHandle,\r
1078 &gEfiHiiPackageListProtocolGuid,\r
1079 (VOID **)&PackageList,\r
1080 ImageHandle,\r
1081 NULL,\r
1082 EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
1083 );\r
1084 ASSERT_EFI_ERROR (Status);\r
1085 if (EFI_ERROR (Status)) {\r
1086 return NULL;\r
1087 }\r
1088\r
1089 //\r
1090 // Publish HII package list to HII Database.\r
1091 //\r
1092 Status = gHiiDatabase->NewPackageList (\r
1093 gHiiDatabase,\r
1094 PackageList,\r
1095 NULL,\r
1096 &HiiHandle\r
1097 );\r
1098 ASSERT_EFI_ERROR (Status);\r
1099 if (EFI_ERROR (Status)) {\r
1100 return NULL;\r
1101 }\r
1102 return HiiHandle;\r
1103}\r