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