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