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