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