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