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