]> git.proxmox.com Git - mirror_edk2.git/blame_incremental - ShellPkg/DynamicCommand/TftpDynamicCommand/Tftp.c
ShellPkg/DynamicCommand: Fix bug that cannot start in boot
[mirror_edk2.git] / ShellPkg / DynamicCommand / TftpDynamicCommand / Tftp.c
... / ...
CommitLineData
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 Copyright (c) 2015 - 2017, Intel Corporation. All rights reserved. <BR>\r
6 (C) Copyright 2015 Hewlett Packard Enterprise Development LP<BR>\r
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
17#include "Tftp.h"\r
18\r
19#define IP4_CONFIG2_INTERFACE_INFO_NAME_LENGTH 32\r
20EFI_HANDLE mTftpHiiHandle;\r
21\r
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
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
53STATIC \r
54BOOLEAN \r
55StringToUint16 (\r
56 IN CONST CHAR16 *ValueStr,\r
57 OUT UINT16 *Value\r
58 );\r
59\r
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
67 IP4_CONFIG2_INTERFACE_INFO_NAME_LENGTH \r
68 double byte wide.\r
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
78STATIC \r
79EFI_STATUS \r
80GetNicName (\r
81 IN EFI_HANDLE ControllerHandle,\r
82 IN UINTN NicNumber,\r
83 OUT CHAR16 *NicName\r
84 );\r
85\r
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
105STATIC \r
106EFI_STATUS \r
107CreateServiceChildAndOpenProtocol (\r
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
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
127STATIC \r
128VOID \r
129CloseProtocolAndDestroyServiceChild (\r
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
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
151STATIC \r
152EFI_STATUS \r
153GetFileSize (\r
154 IN EFI_MTFTP4_PROTOCOL *Mtftp4,\r
155 IN CONST CHAR8 *FilePath,\r
156 OUT UINTN *FileSize\r
157 );\r
158\r
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
167 @param[in] BlockSize Value of the TFTP blksize option\r
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
178STATIC \r
179EFI_STATUS \r
180DownloadFile (\r
181 IN EFI_MTFTP4_PROTOCOL *Mtftp4,\r
182 IN CONST CHAR16 *FilePath,\r
183 IN CONST CHAR8 *AsciiFilePath,\r
184 IN UINTN FileSize,\r
185 IN UINT16 BlockSize,\r
186 OUT VOID **Data\r
187 );\r
188\r
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
201STATIC \r
202EFI_STATUS \r
203EFIAPI\r
204CheckPacket (\r
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
229 {L"-s", TypeValue},\r
230 {NULL , TypeMax}\r
231 };\r
232\r
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
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
260RunTftp (\r
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
275 UINTN FilePathSize;\r
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
282 CHAR16 NicName[IP4_CONFIG2_INTERFACE_INFO_NAME_LENGTH];\r
283 EFI_HANDLE ControllerHandle;\r
284 EFI_HANDLE Mtftp4ChildHandle;\r
285 EFI_MTFTP4_PROTOCOL *Mtftp4;\r
286 UINTN FileSize;\r
287 UINTN DataSize;\r
288 VOID *Data;\r
289 SHELL_FILE_HANDLE FileHandle;\r
290 UINT16 BlockSize;\r
291\r
292 ShellStatus = SHELL_INVALID_PARAMETER;\r
293 ProblemParam = NULL;\r
294 NicFound = FALSE;\r
295 AsciiRemoteFilePath = NULL;\r
296 Handles = NULL;\r
297 FileSize = 0;\r
298 DataSize = 0;\r
299 BlockSize = MTFTP_DEFAULT_BLKSIZE;\r
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
318 -1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM), mTftpHiiHandle,\r
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
335 mTftpHiiHandle, L"tftp"\r
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
342 mTftpHiiHandle, L"tftp"\r
343 );\r
344 goto Error;\r
345 }\r
346\r
347 CopyMem (&Mtftp4ConfigData, &DefaultMtftp4ConfigData, sizeof (EFI_MTFTP4_CONFIG_DATA));\r
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
357 mTftpHiiHandle, L"tftp", ValueStr\r
358 );\r
359 goto Error;\r
360 }\r
361\r
362 RemoteFilePath = ShellCommandLineGetRawValue (CheckPackage, 2);\r
363 ASSERT(RemoteFilePath != NULL);\r
364 FilePathSize = StrLen (RemoteFilePath) + 1;\r
365 AsciiRemoteFilePath = AllocatePool (FilePathSize);\r
366 if (AsciiRemoteFilePath == NULL) {\r
367 ShellStatus = SHELL_OUT_OF_RESOURCES;\r
368 goto Error;\r
369 }\r
370 UnicodeStrToAsciiStrS (RemoteFilePath, AsciiRemoteFilePath, FilePathSize);\r
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
419 mTftpHiiHandle, L"tftp", ValueStr\r
420 );\r
421 goto Error;\r
422 }\r
423 }\r
424\r
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
433 mTftpHiiHandle, L"tftp", ValueStr\r
434 );\r
435 goto Error;\r
436 }\r
437 }\r
438\r
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
453 mTftpHiiHandle\r
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
468 mTftpHiiHandle, NicNumber, Status\r
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
490 mTftpHiiHandle, NicName, Status\r
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
499 mTftpHiiHandle, NicName, Status\r
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
508 mTftpHiiHandle, RemoteFilePath, NicName, Status\r
509 );\r
510 goto NextHandle;\r
511 }\r
512\r
513 Status = DownloadFile (Mtftp4, RemoteFilePath, AsciiRemoteFilePath, FileSize, BlockSize, &Data);\r
514 if (EFI_ERROR (Status)) {\r
515 ShellPrintHiiEx (\r
516 -1, -1, NULL, STRING_TOKEN (STR_TFTP_ERR_DOWNLOAD),\r
517 mTftpHiiHandle, RemoteFilePath, NicName, Status\r
518 );\r
519 goto NextHandle;\r
520 }\r
521\r
522 if (!EFI_ERROR (ShellFileExists (LocalFilePath))) {\r
523 ShellDeleteFileByName (LocalFilePath);\r
524 }\r
525\r
526 Status = ShellOpenFileByName (\r
527 LocalFilePath,\r
528 &FileHandle,\r
529 EFI_FILE_MODE_CREATE |\r
530 EFI_FILE_MODE_WRITE |\r
531 EFI_FILE_MODE_READ,\r
532 0\r
533 );\r
534 if (EFI_ERROR (Status)) {\r
535 ShellPrintHiiEx (\r
536 -1, -1, NULL, STRING_TOKEN (STR_GEN_FILE_OPEN_FAIL),\r
537 mTftpHiiHandle, L"tftp", LocalFilePath\r
538 );\r
539 goto NextHandle;\r
540 }\r
541\r
542 DataSize = FileSize;\r
543 Status = ShellWriteFile (FileHandle, &FileSize, Data);\r
544 if (!EFI_ERROR (Status)) {\r
545 ShellStatus = SHELL_SUCCESS;\r
546 } else {\r
547 ShellPrintHiiEx (\r
548 -1, -1, NULL, STRING_TOKEN (STR_TFTP_ERR_WRITE),\r
549 mTftpHiiHandle, LocalFilePath, Status\r
550 );\r
551 }\r
552 ShellCloseFile (&FileHandle);\r
553\r
554 NextHandle:\r
555\r
556 if (Data != NULL) {\r
557 gBS->FreePages ((EFI_PHYSICAL_ADDRESS)(UINTN)Data, EFI_SIZE_TO_PAGES (DataSize));\r
558 }\r
559\r
560 CloseProtocolAndDestroyServiceChild (\r
561 ControllerHandle,\r
562 &gEfiMtftp4ServiceBindingProtocolGuid,\r
563 &gEfiMtftp4ProtocolGuid,\r
564 Mtftp4ChildHandle\r
565 );\r
566 }\r
567\r
568 if ((UserNicName != NULL) && (!NicFound)) {\r
569 ShellPrintHiiEx (\r
570 -1, -1, NULL, STRING_TOKEN (STR_TFTP_ERR_NIC_NOT_FOUND),\r
571 mTftpHiiHandle, UserNicName\r
572 );\r
573 }\r
574\r
575 Error:\r
576\r
577 ShellCommandLineFreeVarList (CheckPackage);\r
578 if (AsciiRemoteFilePath != NULL) {\r
579 FreePool (AsciiRemoteFilePath);\r
580 }\r
581 if (Handles != NULL) {\r
582 FreePool (Handles);\r
583 }\r
584\r
585 return ShellStatus;\r
586}\r
587\r
588/**\r
589 Check and convert the UINT16 option values of the 'tftp' command\r
590\r
591 @param[in] ValueStr Value as an Unicode encoded string\r
592 @param[out] Value UINT16 value\r
593\r
594 @return TRUE The value was returned.\r
595 @return FALSE A parsing error occured.\r
596**/\r
597STATIC\r
598BOOLEAN\r
599StringToUint16 (\r
600 IN CONST CHAR16 *ValueStr,\r
601 OUT UINT16 *Value\r
602 )\r
603{\r
604 UINTN Val;\r
605\r
606 Val = ShellStrToUintn (ValueStr);\r
607 if (Val > MAX_UINT16) {\r
608 ShellPrintHiiEx (\r
609 -1, -1, NULL, STRING_TOKEN (STR_GEN_PARAM_INV),\r
610 mTftpHiiHandle, L"tftp", ValueStr\r
611 );\r
612 return FALSE;\r
613 }\r
614\r
615 *Value = (UINT16)Val;\r
616 return TRUE;\r
617}\r
618\r
619/**\r
620 Get the name of the NIC.\r
621\r
622 @param[in] ControllerHandle The network physical device handle.\r
623 @param[in] NicNumber The network physical device number.\r
624 @param[out] NicName Address where to store the NIC name.\r
625 The memory area has to be at least\r
626 IP4_CONFIG2_INTERFACE_INFO_NAME_LENGTH \r
627 double byte wide.\r
628\r
629 @return EFI_SUCCESS The name of the NIC was returned.\r
630 @return Others The creation of the child for the Managed\r
631 Network Service failed or the opening of\r
632 the Managed Network Protocol failed or\r
633 the operational parameters for the\r
634 Managed Network Protocol could not be\r
635 read.\r
636**/\r
637STATIC\r
638EFI_STATUS\r
639GetNicName (\r
640 IN EFI_HANDLE ControllerHandle,\r
641 IN UINTN NicNumber,\r
642 OUT CHAR16 *NicName\r
643 )\r
644{\r
645 EFI_STATUS Status;\r
646 EFI_HANDLE MnpHandle;\r
647 EFI_MANAGED_NETWORK_PROTOCOL *Mnp;\r
648 EFI_SIMPLE_NETWORK_MODE SnpMode;\r
649\r
650 Status = CreateServiceChildAndOpenProtocol (\r
651 ControllerHandle,\r
652 &gEfiManagedNetworkServiceBindingProtocolGuid,\r
653 &gEfiManagedNetworkProtocolGuid,\r
654 &MnpHandle,\r
655 (VOID**)&Mnp\r
656 );\r
657 if (EFI_ERROR (Status)) {\r
658 goto Error;\r
659 }\r
660\r
661 Status = Mnp->GetModeData (Mnp, NULL, &SnpMode);\r
662 if (EFI_ERROR (Status) && (Status != EFI_NOT_STARTED)) {\r
663 goto Error;\r
664 }\r
665\r
666 UnicodeSPrint (\r
667 NicName,\r
668 IP4_CONFIG2_INTERFACE_INFO_NAME_LENGTH,\r
669 SnpMode.IfType == NET_IFTYPE_ETHERNET ?\r
670 L"eth%d" :\r
671 L"unk%d" ,\r
672 NicNumber\r
673 );\r
674\r
675 Status = EFI_SUCCESS;\r
676\r
677Error:\r
678\r
679 if (MnpHandle != NULL) {\r
680 CloseProtocolAndDestroyServiceChild (\r
681 ControllerHandle,\r
682 &gEfiManagedNetworkServiceBindingProtocolGuid,\r
683 &gEfiManagedNetworkProtocolGuid,\r
684 MnpHandle\r
685 );\r
686 }\r
687\r
688 return Status;\r
689}\r
690\r
691/**\r
692 Create a child for the service identified by its service binding protocol GUID\r
693 and get from the child the interface of the protocol identified by its GUID.\r
694\r
695 @param[in] ControllerHandle Controller handle.\r
696 @param[in] ServiceBindingProtocolGuid Service binding protocol GUID of the\r
697 service to be created.\r
698 @param[in] ProtocolGuid GUID of the protocol to be open.\r
699 @param[out] ChildHandle Address where the handler of the\r
700 created child is returned. NULL is\r
701 returned in case of error.\r
702 @param[out] Interface Address where a pointer to the\r
703 protocol interface is returned in\r
704 case of success.\r
705\r
706 @return EFI_SUCCESS The child was created and the protocol opened.\r
707 @return Others Either the creation of the child or the opening\r
708 of the protocol failed.\r
709**/\r
710STATIC\r
711EFI_STATUS\r
712CreateServiceChildAndOpenProtocol (\r
713 IN EFI_HANDLE ControllerHandle,\r
714 IN EFI_GUID *ServiceBindingProtocolGuid,\r
715 IN EFI_GUID *ProtocolGuid,\r
716 OUT EFI_HANDLE *ChildHandle,\r
717 OUT VOID **Interface\r
718 )\r
719{\r
720 EFI_STATUS Status;\r
721\r
722 *ChildHandle = NULL;\r
723 Status = NetLibCreateServiceChild (\r
724 ControllerHandle,\r
725 gImageHandle,\r
726 ServiceBindingProtocolGuid,\r
727 ChildHandle\r
728 );\r
729 if (!EFI_ERROR (Status)) {\r
730 Status = gBS->OpenProtocol (\r
731 *ChildHandle,\r
732 ProtocolGuid,\r
733 Interface,\r
734 gImageHandle,\r
735 ControllerHandle,\r
736 EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
737 );\r
738 if (EFI_ERROR (Status)) {\r
739 NetLibDestroyServiceChild (\r
740 ControllerHandle,\r
741 gImageHandle,\r
742 ServiceBindingProtocolGuid,\r
743 *ChildHandle\r
744 );\r
745 *ChildHandle = NULL;\r
746 }\r
747 }\r
748\r
749 return Status;\r
750}\r
751\r
752/**\r
753 Close the protocol identified by its GUID on the child handle of the service\r
754 identified by its service binding protocol GUID, then destroy the child\r
755 handle.\r
756\r
757 @param[in] ControllerHandle Controller handle.\r
758 @param[in] ServiceBindingProtocolGuid Service binding protocol GUID of the\r
759 service to be destroyed.\r
760 @param[in] ProtocolGuid GUID of the protocol to be closed.\r
761 @param[in] ChildHandle Handle of the child to be destroyed.\r
762\r
763**/\r
764STATIC\r
765VOID\r
766CloseProtocolAndDestroyServiceChild (\r
767 IN EFI_HANDLE ControllerHandle,\r
768 IN EFI_GUID *ServiceBindingProtocolGuid,\r
769 IN EFI_GUID *ProtocolGuid,\r
770 IN EFI_HANDLE ChildHandle\r
771 )\r
772{\r
773 gBS->CloseProtocol (\r
774 ChildHandle,\r
775 ProtocolGuid,\r
776 gImageHandle,\r
777 ControllerHandle\r
778 );\r
779\r
780 NetLibDestroyServiceChild (\r
781 ControllerHandle,\r
782 gImageHandle,\r
783 ServiceBindingProtocolGuid,\r
784 ChildHandle\r
785 );\r
786}\r
787\r
788/**\r
789 Worker function that gets the size in numbers of bytes of a file from a TFTP\r
790 server before to download the file.\r
791\r
792 @param[in] Mtftp4 MTFTP4 protocol interface\r
793 @param[in] FilePath Path of the file, ASCII encoded\r
794 @param[out] FileSize Address where to store the file size in number of\r
795 bytes.\r
796\r
797 @retval EFI_SUCCESS The size of the file was returned.\r
798 @retval EFI_UNSUPPORTED The server does not support the "tsize" option.\r
799 @retval Others Error when retrieving the information from the server\r
800 (see EFI_MTFTP4_PROTOCOL.GetInfo() status codes)\r
801 or error when parsing the response of the server.\r
802**/\r
803STATIC\r
804EFI_STATUS\r
805GetFileSize (\r
806 IN EFI_MTFTP4_PROTOCOL *Mtftp4,\r
807 IN CONST CHAR8 *FilePath,\r
808 OUT UINTN *FileSize\r
809 )\r
810{\r
811 EFI_STATUS Status;\r
812 EFI_MTFTP4_OPTION ReqOpt[1];\r
813 EFI_MTFTP4_PACKET *Packet;\r
814 UINT32 PktLen;\r
815 EFI_MTFTP4_OPTION *TableOfOptions;\r
816 EFI_MTFTP4_OPTION *Option;\r
817 UINT32 OptCnt;\r
818 UINT8 OptBuf[128];\r
819\r
820 ReqOpt[0].OptionStr = (UINT8*)"tsize";\r
821 OptBuf[0] = '0';\r
822 OptBuf[1] = 0;\r
823 ReqOpt[0].ValueStr = OptBuf;\r
824\r
825 Status = Mtftp4->GetInfo (\r
826 Mtftp4,\r
827 NULL,\r
828 (UINT8*)FilePath,\r
829 NULL,\r
830 1,\r
831 ReqOpt,\r
832 &PktLen,\r
833 &Packet\r
834 );\r
835\r
836 if (EFI_ERROR (Status)) {\r
837 goto Error;\r
838 }\r
839\r
840 Status = Mtftp4->ParseOptions (\r
841 Mtftp4,\r
842 PktLen,\r
843 Packet,\r
844 (UINT32 *) &OptCnt,\r
845 &TableOfOptions\r
846 );\r
847 if (EFI_ERROR (Status)) {\r
848 goto Error;\r
849 }\r
850\r
851 Option = TableOfOptions;\r
852 while (OptCnt != 0) {\r
853 if (AsciiStrnCmp ((CHAR8 *)Option->OptionStr, "tsize", 5) == 0) {\r
854 *FileSize = AsciiStrDecimalToUintn ((CHAR8 *)Option->ValueStr);\r
855 break;\r
856 }\r
857 OptCnt--;\r
858 Option++;\r
859 }\r
860 FreePool (TableOfOptions);\r
861\r
862 if (OptCnt == 0) {\r
863 Status = EFI_UNSUPPORTED;\r
864 }\r
865\r
866Error :\r
867\r
868 return Status;\r
869}\r
870\r
871/**\r
872 Worker function that download the data of a file from a TFTP server given\r
873 the path of the file and its size.\r
874\r
875 @param[in] Mtftp4 MTFTP4 protocol interface\r
876 @param[in] FilePath Path of the file, Unicode encoded\r
877 @param[in] AsciiFilePath Path of the file, ASCII encoded\r
878 @param[in] FileSize Size of the file in number of bytes\r
879 @param[in] BlockSize Value of the TFTP blksize option\r
880 @param[out] Data Address where to store the address of the buffer\r
881 where the data of the file were downloaded in\r
882 case of success.\r
883\r
884 @retval EFI_SUCCESS The file was downloaded.\r
885 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.\r
886 @retval Others The downloading of the file from the server failed\r
887 (see EFI_MTFTP4_PROTOCOL.ReadFile() status codes).\r
888\r
889**/\r
890STATIC\r
891EFI_STATUS\r
892DownloadFile (\r
893 IN EFI_MTFTP4_PROTOCOL *Mtftp4,\r
894 IN CONST CHAR16 *FilePath,\r
895 IN CONST CHAR8 *AsciiFilePath,\r
896 IN UINTN FileSize,\r
897 IN UINT16 BlockSize,\r
898 OUT VOID **Data\r
899 )\r
900{\r
901 EFI_STATUS Status;\r
902 EFI_PHYSICAL_ADDRESS PagesAddress;\r
903 VOID *Buffer;\r
904 DOWNLOAD_CONTEXT *TftpContext;\r
905 EFI_MTFTP4_TOKEN Mtftp4Token;\r
906 EFI_MTFTP4_OPTION ReqOpt;\r
907 UINT8 OptBuf[10];\r
908\r
909 // Downloaded file can be large. BS.AllocatePages() is more faster\r
910 // than AllocatePool() and avoid fragmentation.\r
911 // The downloaded file could be an EFI application. Marking the\r
912 // allocated page as EfiBootServicesCode would allow to execute a\r
913 // potential downloaded EFI application.\r
914 Status = gBS->AllocatePages (\r
915 AllocateAnyPages,\r
916 EfiBootServicesCode,\r
917 EFI_SIZE_TO_PAGES (FileSize),\r
918 &PagesAddress\r
919 );\r
920 if (EFI_ERROR (Status)) {\r
921 return Status;\r
922 }\r
923\r
924 Buffer = (VOID*)(UINTN)PagesAddress;\r
925 TftpContext = AllocatePool (sizeof (DOWNLOAD_CONTEXT));\r
926 if (TftpContext == NULL) {\r
927 Status = EFI_OUT_OF_RESOURCES;\r
928 goto Error;\r
929 }\r
930 TftpContext->FileSize = FileSize;\r
931 TftpContext->DownloadedNbOfBytes = 0;\r
932 TftpContext->LastReportedNbOfBytes = 0;\r
933\r
934 ZeroMem (&Mtftp4Token, sizeof (EFI_MTFTP4_TOKEN));\r
935 Mtftp4Token.Filename = (UINT8*)AsciiFilePath;\r
936 Mtftp4Token.BufferSize = FileSize;\r
937 Mtftp4Token.Buffer = Buffer;\r
938 Mtftp4Token.CheckPacket = CheckPacket;\r
939 Mtftp4Token.Context = (VOID*)TftpContext;\r
940 if (BlockSize != MTFTP_DEFAULT_BLKSIZE) {\r
941 ReqOpt.OptionStr = (UINT8 *) "blksize";\r
942 AsciiSPrint ((CHAR8 *)OptBuf, sizeof (OptBuf), "%d", BlockSize);\r
943 ReqOpt.ValueStr = OptBuf;\r
944\r
945 Mtftp4Token.OptionCount = 1;\r
946 Mtftp4Token.OptionList = &ReqOpt;\r
947 }\r
948\r
949 ShellPrintHiiEx (\r
950 -1, -1, NULL, STRING_TOKEN (STR_TFTP_DOWNLOADING),\r
951 mTftpHiiHandle, FilePath\r
952 );\r
953\r
954 Status = Mtftp4->ReadFile (Mtftp4, &Mtftp4Token);\r
955 ShellPrintHiiEx (\r
956 -1, -1, NULL, STRING_TOKEN (STR_GEN_CRLF),\r
957 mTftpHiiHandle\r
958 );\r
959\r
960Error :\r
961\r
962 if (TftpContext == NULL) {\r
963 FreePool (TftpContext);\r
964 }\r
965\r
966 if (EFI_ERROR (Status)) {\r
967 gBS->FreePages (PagesAddress, EFI_SIZE_TO_PAGES (FileSize));\r
968 return Status;\r
969 }\r
970\r
971 *Data = Buffer;\r
972\r
973 return EFI_SUCCESS;\r
974}\r
975\r
976/**\r
977 Update the progress of a file download\r
978 This procedure is called each time a new TFTP packet is received.\r
979\r
980 @param[in] This MTFTP4 protocol interface\r
981 @param[in] Token Parameters for the download of the file\r
982 @param[in] PacketLen Length of the packet\r
983 @param[in] Packet Address of the packet\r
984\r
985 @retval EFI_SUCCESS All packets are accepted.\r
986\r
987**/\r
988STATIC\r
989EFI_STATUS\r
990EFIAPI\r
991CheckPacket (\r
992 IN EFI_MTFTP4_PROTOCOL *This,\r
993 IN EFI_MTFTP4_TOKEN *Token,\r
994 IN UINT16 PacketLen,\r
995 IN EFI_MTFTP4_PACKET *Packet\r
996 )\r
997{\r
998 DOWNLOAD_CONTEXT *Context;\r
999 CHAR16 Progress[TFTP_PROGRESS_MESSAGE_SIZE];\r
1000 UINTN NbOfKb;\r
1001 UINTN Index;\r
1002 UINTN LastStep;\r
1003 UINTN Step;\r
1004 EFI_STATUS Status;\r
1005\r
1006 if ((NTOHS (Packet->OpCode)) != EFI_MTFTP4_OPCODE_DATA) {\r
1007 return EFI_SUCCESS;\r
1008 }\r
1009\r
1010 Context = (DOWNLOAD_CONTEXT*)Token->Context;\r
1011 if (Context->DownloadedNbOfBytes == 0) {\r
1012 ShellPrintEx (-1, -1, L"%s 0 Kb", mTftpProgressFrame);\r
1013 }\r
1014\r
1015 //\r
1016 // The data in the packet are prepended with two UINT16 :\r
1017 // . OpCode = EFI_MTFTP4_OPCODE_DATA\r
1018 // . Block = the number of this block of data\r
1019 //\r
1020 Context->DownloadedNbOfBytes += PacketLen - sizeof (Packet->OpCode)\r
1021 - sizeof (Packet->Data.Block);\r
1022 NbOfKb = Context->DownloadedNbOfBytes / 1024;\r
1023\r
1024 Progress[0] = L'\0';\r
1025 LastStep = (Context->LastReportedNbOfBytes * TFTP_PROGRESS_SLIDER_STEPS) / Context->FileSize;\r
1026 Step = (Context->DownloadedNbOfBytes * TFTP_PROGRESS_SLIDER_STEPS) / Context->FileSize;\r
1027\r
1028 if (Step <= LastStep) {\r
1029 return EFI_SUCCESS;\r
1030 }\r
1031\r
1032 ShellPrintEx (-1, -1, L"%s", mTftpProgressDelete);\r
1033\r
1034 Status = StrCpyS (Progress, TFTP_PROGRESS_MESSAGE_SIZE, mTftpProgressFrame);\r
1035 if (EFI_ERROR(Status)) {\r
1036 return Status;\r
1037 }\r
1038 for (Index = 1; Index < Step; Index++) {\r
1039 Progress[Index] = L'=';\r
1040 }\r
1041 Progress[Step] = L'>';\r
1042\r
1043 UnicodeSPrint (\r
1044 Progress + (sizeof (mTftpProgressFrame) / sizeof (CHAR16)) - 1,\r
1045 sizeof (Progress) - sizeof (mTftpProgressFrame),\r
1046 L" %7d Kb",\r
1047 NbOfKb\r
1048 );\r
1049 Context->LastReportedNbOfBytes = Context->DownloadedNbOfBytes;\r
1050\r
1051 ShellPrintEx (-1, -1, L"%s", Progress);\r
1052\r
1053 return EFI_SUCCESS;\r
1054}\r
1055\r
1056/**\r
1057 Retrive HII package list from ImageHandle and publish to HII database.\r
1058\r
1059 @param ImageHandle The image handle of the process.\r
1060\r
1061 @return HII handle.\r
1062**/\r
1063EFI_HANDLE\r
1064InitializeHiiPackage (\r
1065 EFI_HANDLE ImageHandle\r
1066 )\r
1067{\r
1068 EFI_STATUS Status;\r
1069 EFI_HII_PACKAGE_LIST_HEADER *PackageList;\r
1070 EFI_HANDLE HiiHandle;\r
1071\r
1072 //\r
1073 // Retrieve HII package list from ImageHandle\r
1074 //\r
1075 Status = gBS->OpenProtocol (\r
1076 ImageHandle,\r
1077 &gEfiHiiPackageListProtocolGuid,\r
1078 (VOID **)&PackageList,\r
1079 ImageHandle,\r
1080 NULL,\r
1081 EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
1082 );\r
1083 ASSERT_EFI_ERROR (Status);\r
1084 if (EFI_ERROR (Status)) {\r
1085 return NULL;\r
1086 }\r
1087\r
1088 //\r
1089 // Publish HII package list to HII Database.\r
1090 //\r
1091 Status = gHiiDatabase->NewPackageList (\r
1092 gHiiDatabase,\r
1093 PackageList,\r
1094 NULL,\r
1095 &HiiHandle\r
1096 );\r
1097 ASSERT_EFI_ERROR (Status);\r
1098 if (EFI_ERROR (Status)) {\r
1099 return NULL;\r
1100 }\r
1101 return HiiHandle;\r
1102}\r