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