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