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