]> git.proxmox.com Git - mirror_edk2.git/blob - ShellPkg/Application/Shell/Shell.c
ShellPkg: Unregister hotkey callback when exiting Shell
[mirror_edk2.git] / ShellPkg / Application / Shell / Shell.c
1 /** @file
2 This is THE shell (application)
3
4 Copyright (c) 2009 - 2016, Intel Corporation. All rights reserved.<BR>
5 (C) Copyright 2013-2014 Hewlett-Packard Development Company, L.P.<BR>
6 This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php
10
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
13
14 **/
15
16 #include "Shell.h"
17
18 //
19 // Initialize the global structure
20 //
21 SHELL_INFO ShellInfoObject = {
22 NULL,
23 NULL,
24 FALSE,
25 FALSE,
26 {
27 {{
28 0,
29 0,
30 0,
31 0,
32 0,
33 0,
34 0,
35 0,
36 0,
37 0
38 }},
39 0,
40 NULL,
41 NULL
42 },
43 {{NULL, NULL}, NULL},
44 {
45 {{NULL, NULL}, NULL},
46 0,
47 0,
48 TRUE
49 },
50 NULL,
51 0,
52 NULL,
53 NULL,
54 NULL,
55 NULL,
56 NULL,
57 {{NULL, NULL}, NULL, NULL},
58 {{NULL, NULL}, NULL, NULL},
59 NULL,
60 NULL,
61 NULL,
62 NULL,
63 NULL,
64 NULL,
65 NULL,
66 NULL,
67 FALSE
68 };
69
70 STATIC CONST CHAR16 mScriptExtension[] = L".NSH";
71 STATIC CONST CHAR16 mExecutableExtensions[] = L".NSH;.EFI";
72 STATIC CONST CHAR16 mStartupScript[] = L"startup.nsh";
73 CONST CHAR16 mNoNestingEnvVarName[] = L"nonesting";
74 CONST CHAR16 mNoNestingTrue[] = L"True";
75 CONST CHAR16 mNoNestingFalse[] = L"False";
76
77 /**
78 Cleans off leading and trailing spaces and tabs.
79
80 @param[in] String pointer to the string to trim them off.
81 **/
82 EFI_STATUS
83 EFIAPI
84 TrimSpaces(
85 IN CHAR16 **String
86 )
87 {
88 ASSERT(String != NULL);
89 ASSERT(*String!= NULL);
90 //
91 // Remove any spaces and tabs at the beginning of the (*String).
92 //
93 while (((*String)[0] == L' ') || ((*String)[0] == L'\t')) {
94 CopyMem((*String), (*String)+1, StrSize((*String)) - sizeof((*String)[0]));
95 }
96
97 //
98 // Remove any spaces and tabs at the end of the (*String).
99 //
100 while ((StrLen (*String) > 0) && (((*String)[StrLen((*String))-1] == L' ') || ((*String)[StrLen((*String))-1] == L'\t'))) {
101 (*String)[StrLen((*String))-1] = CHAR_NULL;
102 }
103
104 return (EFI_SUCCESS);
105 }
106
107 /**
108 Parse for the next instance of one string within another string. Can optionally make sure that
109 the string was not escaped (^ character) per the shell specification.
110
111 @param[in] SourceString The string to search within
112 @param[in] FindString The string to look for
113 @param[in] CheckForEscapeCharacter TRUE to skip escaped instances of FinfString, otherwise will return even escaped instances
114 **/
115 CHAR16*
116 EFIAPI
117 FindNextInstance(
118 IN CONST CHAR16 *SourceString,
119 IN CONST CHAR16 *FindString,
120 IN CONST BOOLEAN CheckForEscapeCharacter
121 )
122 {
123 CHAR16 *Temp;
124 if (SourceString == NULL) {
125 return (NULL);
126 }
127 Temp = StrStr(SourceString, FindString);
128
129 //
130 // If nothing found, or we don't care about escape characters
131 //
132 if (Temp == NULL || !CheckForEscapeCharacter) {
133 return (Temp);
134 }
135
136 //
137 // If we found an escaped character, try again on the remainder of the string
138 //
139 if ((Temp > (SourceString)) && *(Temp-1) == L'^') {
140 return FindNextInstance(Temp+1, FindString, CheckForEscapeCharacter);
141 }
142
143 //
144 // we found the right character
145 //
146 return (Temp);
147 }
148
149 /**
150 Check whether the string between a pair of % is a valid environment variable name.
151
152 @param[in] BeginPercent pointer to the first percent.
153 @param[in] EndPercent pointer to the last percent.
154
155 @retval TRUE is a valid environment variable name.
156 @retval FALSE is NOT a valid environment variable name.
157 **/
158 BOOLEAN
159 IsValidEnvironmentVariableName(
160 IN CONST CHAR16 *BeginPercent,
161 IN CONST CHAR16 *EndPercent
162 )
163 {
164 CONST CHAR16 *Walker;
165
166 Walker = NULL;
167
168 ASSERT (BeginPercent != NULL);
169 ASSERT (EndPercent != NULL);
170 ASSERT (BeginPercent < EndPercent);
171
172 if ((BeginPercent + 1) == EndPercent) {
173 return FALSE;
174 }
175
176 for (Walker = BeginPercent + 1; Walker < EndPercent; Walker++) {
177 if (
178 (*Walker >= L'0' && *Walker <= L'9') ||
179 (*Walker >= L'A' && *Walker <= L'Z') ||
180 (*Walker >= L'a' && *Walker <= L'z') ||
181 (*Walker == L'_')
182 ) {
183 if (Walker == BeginPercent + 1 && (*Walker >= L'0' && *Walker <= L'9')) {
184 return FALSE;
185 } else {
186 continue;
187 }
188 } else {
189 return FALSE;
190 }
191 }
192
193 return TRUE;
194 }
195
196 /**
197 Determine if a command line contains a split operation
198
199 @param[in] CmdLine The command line to parse.
200
201 @retval TRUE CmdLine has a valid split.
202 @retval FALSE CmdLine does not have a valid split.
203 **/
204 BOOLEAN
205 EFIAPI
206 ContainsSplit(
207 IN CONST CHAR16 *CmdLine
208 )
209 {
210 CONST CHAR16 *TempSpot;
211 CONST CHAR16 *FirstQuote;
212 CONST CHAR16 *SecondQuote;
213
214 FirstQuote = FindNextInstance (CmdLine, L"\"", TRUE);
215 SecondQuote = NULL;
216 TempSpot = ShellFindFirstCharacter(CmdLine, L"|", TRUE);
217
218 if (FirstQuote == NULL ||
219 TempSpot == NULL ||
220 TempSpot == CHAR_NULL ||
221 FirstQuote > TempSpot
222 ) {
223 return (BOOLEAN) ((TempSpot != NULL) && (*TempSpot != CHAR_NULL));
224 }
225
226 while ((TempSpot != NULL) && (*TempSpot != CHAR_NULL)) {
227 if (FirstQuote == NULL || FirstQuote > TempSpot) {
228 break;
229 }
230 SecondQuote = FindNextInstance (FirstQuote + 1, L"\"", TRUE);
231 if (SecondQuote == NULL) {
232 break;
233 }
234 if (SecondQuote < TempSpot) {
235 FirstQuote = FindNextInstance (SecondQuote + 1, L"\"", TRUE);
236 continue;
237 } else {
238 FirstQuote = FindNextInstance (SecondQuote + 1, L"\"", TRUE);
239 TempSpot = ShellFindFirstCharacter(TempSpot + 1, L"|", TRUE);
240 continue;
241 }
242 }
243
244 return (BOOLEAN) ((TempSpot != NULL) && (*TempSpot != CHAR_NULL));
245 }
246
247 /**
248 Function to start monitoring for CTRL-S using SimpleTextInputEx. This
249 feature's enabled state was not known when the shell initially launched.
250
251 @retval EFI_SUCCESS The feature is enabled.
252 @retval EFI_OUT_OF_RESOURCES There is not enough memory available.
253 **/
254 EFI_STATUS
255 EFIAPI
256 InternalEfiShellStartCtrlSMonitor(
257 VOID
258 )
259 {
260 EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL *SimpleEx;
261 EFI_KEY_DATA KeyData;
262 EFI_STATUS Status;
263
264 Status = gBS->OpenProtocol(
265 gST->ConsoleInHandle,
266 &gEfiSimpleTextInputExProtocolGuid,
267 (VOID**)&SimpleEx,
268 gImageHandle,
269 NULL,
270 EFI_OPEN_PROTOCOL_GET_PROTOCOL);
271 if (EFI_ERROR(Status)) {
272 ShellPrintHiiEx(
273 -1,
274 -1,
275 NULL,
276 STRING_TOKEN (STR_SHELL_NO_IN_EX),
277 ShellInfoObject.HiiHandle);
278 return (EFI_SUCCESS);
279 }
280
281 KeyData.KeyState.KeyToggleState = 0;
282 KeyData.Key.ScanCode = 0;
283 KeyData.KeyState.KeyShiftState = EFI_SHIFT_STATE_VALID|EFI_LEFT_CONTROL_PRESSED;
284 KeyData.Key.UnicodeChar = L's';
285
286 Status = SimpleEx->RegisterKeyNotify(
287 SimpleEx,
288 &KeyData,
289 NotificationFunction,
290 &ShellInfoObject.CtrlSNotifyHandle1);
291
292 KeyData.KeyState.KeyShiftState = EFI_SHIFT_STATE_VALID|EFI_RIGHT_CONTROL_PRESSED;
293 if (!EFI_ERROR(Status)) {
294 Status = SimpleEx->RegisterKeyNotify(
295 SimpleEx,
296 &KeyData,
297 NotificationFunction,
298 &ShellInfoObject.CtrlSNotifyHandle2);
299 }
300 KeyData.KeyState.KeyShiftState = EFI_SHIFT_STATE_VALID|EFI_LEFT_CONTROL_PRESSED;
301 KeyData.Key.UnicodeChar = 19;
302
303 if (!EFI_ERROR(Status)) {
304 Status = SimpleEx->RegisterKeyNotify(
305 SimpleEx,
306 &KeyData,
307 NotificationFunction,
308 &ShellInfoObject.CtrlSNotifyHandle3);
309 }
310 KeyData.KeyState.KeyShiftState = EFI_SHIFT_STATE_VALID|EFI_RIGHT_CONTROL_PRESSED;
311 if (!EFI_ERROR(Status)) {
312 Status = SimpleEx->RegisterKeyNotify(
313 SimpleEx,
314 &KeyData,
315 NotificationFunction,
316 &ShellInfoObject.CtrlSNotifyHandle4);
317 }
318 return (Status);
319 }
320
321
322
323 /**
324 The entry point for the application.
325
326 @param[in] ImageHandle The firmware allocated handle for the EFI image.
327 @param[in] SystemTable A pointer to the EFI System Table.
328
329 @retval EFI_SUCCESS The entry point is executed successfully.
330 @retval other Some error occurs when executing this entry point.
331
332 **/
333 EFI_STATUS
334 EFIAPI
335 UefiMain (
336 IN EFI_HANDLE ImageHandle,
337 IN EFI_SYSTEM_TABLE *SystemTable
338 )
339 {
340 EFI_STATUS Status;
341 CHAR16 *TempString;
342 UINTN Size;
343 EFI_HANDLE ConInHandle;
344 EFI_SIMPLE_TEXT_INPUT_PROTOCOL *OldConIn;
345 SPLIT_LIST *Split;
346
347 if (PcdGet8(PcdShellSupportLevel) > 3) {
348 return (EFI_UNSUPPORTED);
349 }
350
351 //
352 // Clear the screen
353 //
354 Status = gST->ConOut->ClearScreen(gST->ConOut);
355 if (EFI_ERROR(Status)) {
356 return (Status);
357 }
358
359 //
360 // Populate the global structure from PCDs
361 //
362 ShellInfoObject.ImageDevPath = NULL;
363 ShellInfoObject.FileDevPath = NULL;
364 ShellInfoObject.PageBreakEnabled = PcdGetBool(PcdShellPageBreakDefault);
365 ShellInfoObject.ViewingSettings.InsertMode = PcdGetBool(PcdShellInsertModeDefault);
366 ShellInfoObject.LogScreenCount = PcdGet8 (PcdShellScreenLogCount );
367
368 //
369 // verify we dont allow for spec violation
370 //
371 ASSERT(ShellInfoObject.LogScreenCount >= 3);
372
373 //
374 // Initialize the LIST ENTRY objects...
375 //
376 InitializeListHead(&ShellInfoObject.BufferToFreeList.Link);
377 InitializeListHead(&ShellInfoObject.ViewingSettings.CommandHistory.Link);
378 InitializeListHead(&ShellInfoObject.SplitList.Link);
379
380 //
381 // Check PCDs for optional features that are not implemented yet.
382 //
383 if ( PcdGetBool(PcdShellSupportOldProtocols)
384 || !FeaturePcdGet(PcdShellRequireHiiPlatform)
385 || FeaturePcdGet(PcdShellSupportFrameworkHii)
386 ) {
387 return (EFI_UNSUPPORTED);
388 }
389
390 //
391 // turn off the watchdog timer
392 //
393 gBS->SetWatchdogTimer (0, 0, 0, NULL);
394
395 //
396 // install our console logger. This will keep a log of the output for back-browsing
397 //
398 Status = ConsoleLoggerInstall(ShellInfoObject.LogScreenCount, &ShellInfoObject.ConsoleInfo);
399 if (!EFI_ERROR(Status)) {
400 //
401 // Enable the cursor to be visible
402 //
403 gST->ConOut->EnableCursor (gST->ConOut, TRUE);
404
405 //
406 // If supporting EFI 1.1 we need to install HII protocol
407 // only do this if PcdShellRequireHiiPlatform == FALSE
408 //
409 // remove EFI_UNSUPPORTED check above when complete.
410 ///@todo add support for Framework HII
411
412 //
413 // install our (solitary) HII package
414 //
415 ShellInfoObject.HiiHandle = HiiAddPackages (&gEfiCallerIdGuid, gImageHandle, ShellStrings, NULL);
416 if (ShellInfoObject.HiiHandle == NULL) {
417 if (PcdGetBool(PcdShellSupportFrameworkHii)) {
418 ///@todo Add our package into Framework HII
419 }
420 if (ShellInfoObject.HiiHandle == NULL) {
421 Status = EFI_NOT_STARTED;
422 goto FreeResources;
423 }
424 }
425
426 //
427 // create and install the EfiShellParametersProtocol
428 //
429 Status = CreatePopulateInstallShellParametersProtocol(&ShellInfoObject.NewShellParametersProtocol, &ShellInfoObject.RootShellInstance);
430 ASSERT_EFI_ERROR(Status);
431 ASSERT(ShellInfoObject.NewShellParametersProtocol != NULL);
432
433 //
434 // create and install the EfiShellProtocol
435 //
436 Status = CreatePopulateInstallShellProtocol(&ShellInfoObject.NewEfiShellProtocol);
437 ASSERT_EFI_ERROR(Status);
438 ASSERT(ShellInfoObject.NewEfiShellProtocol != NULL);
439
440 //
441 // Now initialize the shell library (it requires Shell Parameters protocol)
442 //
443 Status = ShellInitialize();
444 ASSERT_EFI_ERROR(Status);
445
446 Status = CommandInit();
447 ASSERT_EFI_ERROR(Status);
448
449 Status = ShellInitEnvVarList ();
450
451 //
452 // Check the command line
453 //
454 Status = ProcessCommandLine ();
455 if (EFI_ERROR (Status)) {
456 goto FreeResources;
457 }
458
459 //
460 // If shell support level is >= 1 create the mappings and paths
461 //
462 if (PcdGet8(PcdShellSupportLevel) >= 1) {
463 Status = ShellCommandCreateInitialMappingsAndPaths();
464 }
465
466 //
467 // Set the environment variable for nesting support
468 //
469 Size = 0;
470 TempString = NULL;
471 if (!ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoNest) {
472 //
473 // No change. require nesting in Shell Protocol Execute()
474 //
475 StrnCatGrow(&TempString,
476 &Size,
477 L"False",
478 0);
479 } else {
480 StrnCatGrow(&TempString,
481 &Size,
482 mNoNestingTrue,
483 0);
484 }
485 Status = InternalEfiShellSetEnv(mNoNestingEnvVarName, TempString, TRUE);
486 SHELL_FREE_NON_NULL(TempString);
487 Size = 0;
488
489 //
490 // save the device path for the loaded image and the device path for the filepath (under loaded image)
491 // These are where to look for the startup.nsh file
492 //
493 Status = GetDevicePathsForImageAndFile(&ShellInfoObject.ImageDevPath, &ShellInfoObject.FileDevPath);
494 ASSERT_EFI_ERROR(Status);
495
496 //
497 // Display the version
498 //
499 if (!ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoVersion) {
500 ShellPrintHiiEx (
501 0,
502 gST->ConOut->Mode->CursorRow,
503 NULL,
504 STRING_TOKEN (STR_VER_OUTPUT_MAIN_SHELL),
505 ShellInfoObject.HiiHandle,
506 SupportLevel[PcdGet8(PcdShellSupportLevel)],
507 gEfiShellProtocol->MajorVersion,
508 gEfiShellProtocol->MinorVersion
509 );
510
511 ShellPrintHiiEx (
512 -1,
513 -1,
514 NULL,
515 STRING_TOKEN (STR_VER_OUTPUT_MAIN_SUPPLIER),
516 ShellInfoObject.HiiHandle,
517 (CHAR16 *) PcdGetPtr (PcdShellSupplier)
518 );
519
520 ShellPrintHiiEx (
521 -1,
522 -1,
523 NULL,
524 STRING_TOKEN (STR_VER_OUTPUT_MAIN_UEFI),
525 ShellInfoObject.HiiHandle,
526 (gST->Hdr.Revision&0xffff0000)>>16,
527 (gST->Hdr.Revision&0x0000ffff),
528 gST->FirmwareVendor,
529 gST->FirmwareRevision
530 );
531 }
532
533 //
534 // Display the mapping
535 //
536 if (PcdGet8(PcdShellSupportLevel) >= 2 && !ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoMap) {
537 Status = RunCommand(L"map");
538 ASSERT_EFI_ERROR(Status);
539 }
540
541 //
542 // init all the built in alias'
543 //
544 Status = SetBuiltInAlias();
545 ASSERT_EFI_ERROR(Status);
546
547 //
548 // Initialize environment variables
549 //
550 if (ShellCommandGetProfileList() != NULL) {
551 Status = InternalEfiShellSetEnv(L"profiles", ShellCommandGetProfileList(), TRUE);
552 ASSERT_EFI_ERROR(Status);
553 }
554
555 Size = 100;
556 TempString = AllocateZeroPool(Size);
557
558 UnicodeSPrint(TempString, Size, L"%d", PcdGet8(PcdShellSupportLevel));
559 Status = InternalEfiShellSetEnv(L"uefishellsupport", TempString, TRUE);
560 ASSERT_EFI_ERROR(Status);
561
562 UnicodeSPrint(TempString, Size, L"%d.%d", ShellInfoObject.NewEfiShellProtocol->MajorVersion, ShellInfoObject.NewEfiShellProtocol->MinorVersion);
563 Status = InternalEfiShellSetEnv(L"uefishellversion", TempString, TRUE);
564 ASSERT_EFI_ERROR(Status);
565
566 UnicodeSPrint(TempString, Size, L"%d.%d", (gST->Hdr.Revision & 0xFFFF0000) >> 16, gST->Hdr.Revision & 0x0000FFFF);
567 Status = InternalEfiShellSetEnv(L"uefiversion", TempString, TRUE);
568 ASSERT_EFI_ERROR(Status);
569
570 FreePool(TempString);
571
572 if (!EFI_ERROR(Status)) {
573 if (!ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoInterrupt) {
574 //
575 // Set up the event for CTRL-C monitoring...
576 //
577 Status = InernalEfiShellStartMonitor();
578 }
579
580 if (!EFI_ERROR(Status) && !ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoConsoleIn) {
581 //
582 // Set up the event for CTRL-S monitoring...
583 //
584 Status = InternalEfiShellStartCtrlSMonitor();
585 }
586
587 if (!EFI_ERROR(Status) && ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoConsoleIn) {
588 //
589 // close off the gST->ConIn
590 //
591 OldConIn = gST->ConIn;
592 ConInHandle = gST->ConsoleInHandle;
593 gST->ConIn = CreateSimpleTextInOnFile((SHELL_FILE_HANDLE)&FileInterfaceNulFile, &gST->ConsoleInHandle);
594 } else {
595 OldConIn = NULL;
596 ConInHandle = NULL;
597 }
598
599 if (!EFI_ERROR(Status) && PcdGet8(PcdShellSupportLevel) >= 1) {
600 //
601 // process the startup script or launch the called app.
602 //
603 Status = DoStartupScript(ShellInfoObject.ImageDevPath, ShellInfoObject.FileDevPath);
604 }
605
606 if (!ShellInfoObject.ShellInitSettings.BitUnion.Bits.Exit && !ShellCommandGetExit() && (PcdGet8(PcdShellSupportLevel) >= 3 || PcdGetBool(PcdShellForceConsole)) && !EFI_ERROR(Status) && !ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoConsoleIn) {
607 //
608 // begin the UI waiting loop
609 //
610 do {
611 //
612 // clean out all the memory allocated for CONST <something> * return values
613 // between each shell prompt presentation
614 //
615 if (!IsListEmpty(&ShellInfoObject.BufferToFreeList.Link)){
616 FreeBufferList(&ShellInfoObject.BufferToFreeList);
617 }
618
619 //
620 // Reset page break back to default.
621 //
622 ShellInfoObject.PageBreakEnabled = PcdGetBool(PcdShellPageBreakDefault);
623 ASSERT (ShellInfoObject.ConsoleInfo != NULL);
624 ShellInfoObject.ConsoleInfo->Enabled = TRUE;
625 ShellInfoObject.ConsoleInfo->RowCounter = 0;
626
627 //
628 // Reset the CTRL-C event (yes we ignore the return values)
629 //
630 Status = gBS->CheckEvent (ShellInfoObject.NewEfiShellProtocol->ExecutionBreak);
631
632 //
633 // Display Prompt
634 //
635 Status = DoShellPrompt();
636 } while (!ShellCommandGetExit());
637 }
638 if (OldConIn != NULL && ConInHandle != NULL) {
639 CloseSimpleTextInOnFile (gST->ConIn);
640 gST->ConIn = OldConIn;
641 gST->ConsoleInHandle = ConInHandle;
642 }
643 }
644 }
645
646 FreeResources:
647 //
648 // uninstall protocols / free memory / etc...
649 //
650 if (ShellInfoObject.UserBreakTimer != NULL) {
651 gBS->CloseEvent(ShellInfoObject.UserBreakTimer);
652 DEBUG_CODE(ShellInfoObject.UserBreakTimer = NULL;);
653 }
654 if (ShellInfoObject.ImageDevPath != NULL) {
655 FreePool(ShellInfoObject.ImageDevPath);
656 DEBUG_CODE(ShellInfoObject.ImageDevPath = NULL;);
657 }
658 if (ShellInfoObject.FileDevPath != NULL) {
659 FreePool(ShellInfoObject.FileDevPath);
660 DEBUG_CODE(ShellInfoObject.FileDevPath = NULL;);
661 }
662 if (ShellInfoObject.NewShellParametersProtocol != NULL) {
663 CleanUpShellParametersProtocol(ShellInfoObject.NewShellParametersProtocol);
664 DEBUG_CODE(ShellInfoObject.NewShellParametersProtocol = NULL;);
665 }
666 if (ShellInfoObject.NewEfiShellProtocol != NULL){
667 if (ShellInfoObject.NewEfiShellProtocol->IsRootShell()){
668 InternalEfiShellSetEnv(L"cwd", NULL, TRUE);
669 }
670 CleanUpShellEnvironment (ShellInfoObject.NewEfiShellProtocol);
671 DEBUG_CODE(ShellInfoObject.NewEfiShellProtocol = NULL;);
672 }
673
674 if (!IsListEmpty(&ShellInfoObject.BufferToFreeList.Link)){
675 FreeBufferList(&ShellInfoObject.BufferToFreeList);
676 }
677
678 if (!IsListEmpty(&ShellInfoObject.SplitList.Link)){
679 ASSERT(FALSE); ///@todo finish this de-allocation (free SplitStdIn/Out when needed).
680
681 for ( Split = (SPLIT_LIST*)GetFirstNode (&ShellInfoObject.SplitList.Link)
682 ; !IsNull (&ShellInfoObject.SplitList.Link, &Split->Link)
683 ; Split = (SPLIT_LIST *)GetNextNode (&ShellInfoObject.SplitList.Link, &Split->Link)
684 ) {
685 RemoveEntryList (&Split->Link);
686 FreePool (Split);
687 }
688
689 DEBUG_CODE (InitializeListHead (&ShellInfoObject.SplitList.Link););
690 }
691
692 if (ShellInfoObject.ShellInitSettings.FileName != NULL) {
693 FreePool(ShellInfoObject.ShellInitSettings.FileName);
694 DEBUG_CODE(ShellInfoObject.ShellInitSettings.FileName = NULL;);
695 }
696
697 if (ShellInfoObject.ShellInitSettings.FileOptions != NULL) {
698 FreePool(ShellInfoObject.ShellInitSettings.FileOptions);
699 DEBUG_CODE(ShellInfoObject.ShellInitSettings.FileOptions = NULL;);
700 }
701
702 if (ShellInfoObject.HiiHandle != NULL) {
703 HiiRemovePackages(ShellInfoObject.HiiHandle);
704 DEBUG_CODE(ShellInfoObject.HiiHandle = NULL;);
705 }
706
707 if (!IsListEmpty(&ShellInfoObject.ViewingSettings.CommandHistory.Link)){
708 FreeBufferList(&ShellInfoObject.ViewingSettings.CommandHistory);
709 }
710
711 ASSERT(ShellInfoObject.ConsoleInfo != NULL);
712 if (ShellInfoObject.ConsoleInfo != NULL) {
713 ConsoleLoggerUninstall(ShellInfoObject.ConsoleInfo);
714 FreePool(ShellInfoObject.ConsoleInfo);
715 DEBUG_CODE(ShellInfoObject.ConsoleInfo = NULL;);
716 }
717
718 ShellFreeEnvVarList ();
719 ShellSetRawCmdLine (NULL);
720
721 if (ShellCommandGetExit()) {
722 return ((EFI_STATUS)ShellCommandGetExitCode());
723 }
724 return (Status);
725 }
726
727 /**
728 Sets all the alias' that were registered with the ShellCommandLib library.
729
730 @retval EFI_SUCCESS all init commands were run successfully.
731 **/
732 EFI_STATUS
733 EFIAPI
734 SetBuiltInAlias(
735 )
736 {
737 EFI_STATUS Status;
738 CONST ALIAS_LIST *List;
739 ALIAS_LIST *Node;
740
741 //
742 // Get all the commands we want to run
743 //
744 List = ShellCommandGetInitAliasList();
745
746 //
747 // for each command in the List
748 //
749 for ( Node = (ALIAS_LIST*)GetFirstNode(&List->Link)
750 ; !IsNull (&List->Link, &Node->Link)
751 ; Node = (ALIAS_LIST *)GetNextNode(&List->Link, &Node->Link)
752 ){
753 //
754 // install the alias'
755 //
756 Status = InternalSetAlias(Node->CommandString, Node->Alias, TRUE);
757 ASSERT_EFI_ERROR(Status);
758 }
759 return (EFI_SUCCESS);
760 }
761
762 /**
763 Internal function to determine if 2 command names are really the same.
764
765 @param[in] Command1 The pointer to the first command name.
766 @param[in] Command2 The pointer to the second command name.
767
768 @retval TRUE The 2 command names are the same.
769 @retval FALSE The 2 command names are not the same.
770 **/
771 BOOLEAN
772 EFIAPI
773 IsCommand(
774 IN CONST CHAR16 *Command1,
775 IN CONST CHAR16 *Command2
776 )
777 {
778 if (StringNoCaseCompare(&Command1, &Command2) == 0) {
779 return (TRUE);
780 }
781 return (FALSE);
782 }
783
784 /**
785 Internal function to determine if a command is a script only command.
786
787 @param[in] CommandName The pointer to the command name.
788
789 @retval TRUE The command is a script only command.
790 @retval FALSE The command is not a script only command.
791 **/
792 BOOLEAN
793 EFIAPI
794 IsScriptOnlyCommand(
795 IN CONST CHAR16 *CommandName
796 )
797 {
798 if (IsCommand(CommandName, L"for")
799 ||IsCommand(CommandName, L"endfor")
800 ||IsCommand(CommandName, L"if")
801 ||IsCommand(CommandName, L"else")
802 ||IsCommand(CommandName, L"endif")
803 ||IsCommand(CommandName, L"goto")) {
804 return (TRUE);
805 }
806 return (FALSE);
807 }
808
809 /**
810 This function will populate the 2 device path protocol parameters based on the
811 global gImageHandle. The DevPath will point to the device path for the handle that has
812 loaded image protocol installed on it. The FilePath will point to the device path
813 for the file that was loaded.
814
815 @param[in, out] DevPath On a successful return the device path to the loaded image.
816 @param[in, out] FilePath On a successful return the device path to the file.
817
818 @retval EFI_SUCCESS The 2 device paths were successfully returned.
819 @retval other A error from gBS->HandleProtocol.
820
821 @sa HandleProtocol
822 **/
823 EFI_STATUS
824 EFIAPI
825 GetDevicePathsForImageAndFile (
826 IN OUT EFI_DEVICE_PATH_PROTOCOL **DevPath,
827 IN OUT EFI_DEVICE_PATH_PROTOCOL **FilePath
828 )
829 {
830 EFI_STATUS Status;
831 EFI_LOADED_IMAGE_PROTOCOL *LoadedImage;
832 EFI_DEVICE_PATH_PROTOCOL *ImageDevicePath;
833
834 ASSERT(DevPath != NULL);
835 ASSERT(FilePath != NULL);
836
837 Status = gBS->OpenProtocol (
838 gImageHandle,
839 &gEfiLoadedImageProtocolGuid,
840 (VOID**)&LoadedImage,
841 gImageHandle,
842 NULL,
843 EFI_OPEN_PROTOCOL_GET_PROTOCOL
844 );
845 if (!EFI_ERROR (Status)) {
846 Status = gBS->OpenProtocol (
847 LoadedImage->DeviceHandle,
848 &gEfiDevicePathProtocolGuid,
849 (VOID**)&ImageDevicePath,
850 gImageHandle,
851 NULL,
852 EFI_OPEN_PROTOCOL_GET_PROTOCOL
853 );
854 if (!EFI_ERROR (Status)) {
855 *DevPath = DuplicateDevicePath (ImageDevicePath);
856 *FilePath = DuplicateDevicePath (LoadedImage->FilePath);
857 gBS->CloseProtocol(
858 LoadedImage->DeviceHandle,
859 &gEfiDevicePathProtocolGuid,
860 gImageHandle,
861 NULL);
862 }
863 gBS->CloseProtocol(
864 gImageHandle,
865 &gEfiLoadedImageProtocolGuid,
866 gImageHandle,
867 NULL);
868 }
869 return (Status);
870 }
871
872 /**
873 Process all Uefi Shell 2.0 command line options.
874
875 see Uefi Shell 2.0 section 3.2 for full details.
876
877 the command line must resemble the following:
878
879 shell.efi [ShellOpt-options] [options] [file-name [file-name-options]]
880
881 ShellOpt-options Options which control the initialization behavior of the shell.
882 These options are read from the EFI global variable "ShellOpt"
883 and are processed before options or file-name.
884
885 options Options which control the initialization behavior of the shell.
886
887 file-name The name of a UEFI shell application or script to be executed
888 after initialization is complete. By default, if file-name is
889 specified, then -nostartup is implied. Scripts are not supported
890 by level 0.
891
892 file-name-options The command-line options that are passed to file-name when it
893 is invoked.
894
895 This will initialize the ShellInfoObject.ShellInitSettings global variable.
896
897 @retval EFI_SUCCESS The variable is initialized.
898 **/
899 EFI_STATUS
900 EFIAPI
901 ProcessCommandLine(
902 VOID
903 )
904 {
905 UINTN Size;
906 UINTN LoopVar;
907 CHAR16 *CurrentArg;
908 CHAR16 *DelayValueStr;
909 UINT64 DelayValue;
910 EFI_STATUS Status;
911 EFI_UNICODE_COLLATION_PROTOCOL *UnicodeCollation;
912
913 // `file-name-options` will contain arguments to `file-name` that we don't
914 // know about. This would cause ShellCommandLineParse to error, so we parse
915 // arguments manually, ignoring those after the first thing that doesn't look
916 // like a shell option (which is assumed to be `file-name`).
917
918 Status = gBS->LocateProtocol (
919 &gEfiUnicodeCollation2ProtocolGuid,
920 NULL,
921 (VOID **) &UnicodeCollation
922 );
923 if (EFI_ERROR (Status)) {
924 Status = gBS->LocateProtocol (
925 &gEfiUnicodeCollationProtocolGuid,
926 NULL,
927 (VOID **) &UnicodeCollation
928 );
929 if (EFI_ERROR (Status)) {
930 return Status;
931 }
932 }
933
934 // Set default options
935 ShellInfoObject.ShellInitSettings.BitUnion.Bits.Startup = FALSE;
936 ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoStartup = FALSE;
937 ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoConsoleOut = FALSE;
938 ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoConsoleIn = FALSE;
939 ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoInterrupt = FALSE;
940 ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoMap = FALSE;
941 ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoVersion = FALSE;
942 ShellInfoObject.ShellInitSettings.BitUnion.Bits.Delay = FALSE;
943 ShellInfoObject.ShellInitSettings.BitUnion.Bits.Exit = FALSE;
944 ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoNest = FALSE;
945 ShellInfoObject.ShellInitSettings.Delay = 5;
946
947 //
948 // Start LoopVar at 0 to parse only optional arguments at Argv[0]
949 // and parse other parameters from Argv[1]. This is for use case that
950 // UEFI Shell boot option is created, and OptionalData is provided
951 // that starts with shell command-line options.
952 //
953 for (LoopVar = 0 ; LoopVar < gEfiShellParametersProtocol->Argc ; LoopVar++) {
954 CurrentArg = gEfiShellParametersProtocol->Argv[LoopVar];
955 if (UnicodeCollation->StriColl (
956 UnicodeCollation,
957 L"-startup",
958 CurrentArg
959 ) == 0) {
960 ShellInfoObject.ShellInitSettings.BitUnion.Bits.Startup = TRUE;
961 }
962 else if (UnicodeCollation->StriColl (
963 UnicodeCollation,
964 L"-nostartup",
965 CurrentArg
966 ) == 0) {
967 ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoStartup = TRUE;
968 }
969 else if (UnicodeCollation->StriColl (
970 UnicodeCollation,
971 L"-noconsoleout",
972 CurrentArg
973 ) == 0) {
974 ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoConsoleOut = TRUE;
975 }
976 else if (UnicodeCollation->StriColl (
977 UnicodeCollation,
978 L"-noconsolein",
979 CurrentArg
980 ) == 0) {
981 ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoConsoleIn = TRUE;
982 }
983 else if (UnicodeCollation->StriColl (
984 UnicodeCollation,
985 L"-nointerrupt",
986 CurrentArg
987 ) == 0) {
988 ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoInterrupt = TRUE;
989 }
990 else if (UnicodeCollation->StriColl (
991 UnicodeCollation,
992 L"-nomap",
993 CurrentArg
994 ) == 0) {
995 ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoMap = TRUE;
996 }
997 else if (UnicodeCollation->StriColl (
998 UnicodeCollation,
999 L"-noversion",
1000 CurrentArg
1001 ) == 0) {
1002 ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoVersion = TRUE;
1003 }
1004 else if (UnicodeCollation->StriColl (
1005 UnicodeCollation,
1006 L"-nonest",
1007 CurrentArg
1008 ) == 0) {
1009 ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoNest = TRUE;
1010 }
1011 else if (UnicodeCollation->StriColl (
1012 UnicodeCollation,
1013 L"-delay",
1014 CurrentArg
1015 ) == 0) {
1016 ShellInfoObject.ShellInitSettings.BitUnion.Bits.Delay = TRUE;
1017 // Check for optional delay value following "-delay"
1018 DelayValueStr = gEfiShellParametersProtocol->Argv[LoopVar + 1];
1019 if (DelayValueStr != NULL){
1020 if (*DelayValueStr == L':') {
1021 DelayValueStr++;
1022 }
1023 if (!EFI_ERROR(ShellConvertStringToUint64 (
1024 DelayValueStr,
1025 &DelayValue,
1026 FALSE,
1027 FALSE
1028 ))) {
1029 ShellInfoObject.ShellInitSettings.Delay = (UINTN)DelayValue;
1030 LoopVar++;
1031 }
1032 }
1033 } else if (UnicodeCollation->StriColl (
1034 UnicodeCollation,
1035 L"-_exit",
1036 CurrentArg
1037 ) == 0) {
1038 ShellInfoObject.ShellInitSettings.BitUnion.Bits.Exit = TRUE;
1039 } else if (StrnCmp (L"-", CurrentArg, 1) == 0) {
1040 // Unrecognized option
1041 ShellPrintHiiEx(-1, -1, NULL,
1042 STRING_TOKEN (STR_GEN_PROBLEM),
1043 ShellInfoObject.HiiHandle,
1044 CurrentArg
1045 );
1046 return EFI_INVALID_PARAMETER;
1047 } else {
1048 //
1049 // First argument should be Shell.efi image name
1050 //
1051 if (LoopVar == 0) {
1052 continue;
1053 }
1054
1055 ShellInfoObject.ShellInitSettings.FileName = AllocateCopyPool(StrSize(CurrentArg), CurrentArg);
1056 if (ShellInfoObject.ShellInitSettings.FileName == NULL) {
1057 return (EFI_OUT_OF_RESOURCES);
1058 }
1059 //
1060 // We found `file-name`.
1061 //
1062 ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoStartup = 1;
1063 LoopVar++;
1064
1065 // Add `file-name-options`
1066 for (Size = 0 ; LoopVar < gEfiShellParametersProtocol->Argc ; LoopVar++) {
1067 ASSERT((ShellInfoObject.ShellInitSettings.FileOptions == NULL && Size == 0) || (ShellInfoObject.ShellInitSettings.FileOptions != NULL));
1068 StrnCatGrow(&ShellInfoObject.ShellInitSettings.FileOptions,
1069 &Size,
1070 L" ",
1071 0);
1072 if (ShellInfoObject.ShellInitSettings.FileOptions == NULL) {
1073 SHELL_FREE_NON_NULL(ShellInfoObject.ShellInitSettings.FileName);
1074 return (EFI_OUT_OF_RESOURCES);
1075 }
1076 StrnCatGrow(&ShellInfoObject.ShellInitSettings.FileOptions,
1077 &Size,
1078 gEfiShellParametersProtocol->Argv[LoopVar],
1079 0);
1080 if (ShellInfoObject.ShellInitSettings.FileOptions == NULL) {
1081 SHELL_FREE_NON_NULL(ShellInfoObject.ShellInitSettings.FileName);
1082 return (EFI_OUT_OF_RESOURCES);
1083 }
1084 }
1085 }
1086 }
1087
1088 // "-nointerrupt" overrides "-delay"
1089 if (ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoInterrupt) {
1090 ShellInfoObject.ShellInitSettings.Delay = 0;
1091 }
1092
1093 return EFI_SUCCESS;
1094 }
1095
1096 /**
1097 Handles all interaction with the default startup script.
1098
1099 this will check that the correct command line parameters were passed, handle the delay, and then start running the script.
1100
1101 @param ImagePath the path to the image for shell. first place to look for the startup script
1102 @param FilePath the path to the file for shell. second place to look for the startup script.
1103
1104 @retval EFI_SUCCESS the variable is initialized.
1105 **/
1106 EFI_STATUS
1107 EFIAPI
1108 DoStartupScript(
1109 IN EFI_DEVICE_PATH_PROTOCOL *ImagePath,
1110 IN EFI_DEVICE_PATH_PROTOCOL *FilePath
1111 )
1112 {
1113 EFI_STATUS Status;
1114 EFI_STATUS CalleeStatus;
1115 UINTN Delay;
1116 EFI_INPUT_KEY Key;
1117 SHELL_FILE_HANDLE FileHandle;
1118 EFI_DEVICE_PATH_PROTOCOL *NewPath;
1119 EFI_DEVICE_PATH_PROTOCOL *NamePath;
1120 CHAR16 *FileStringPath;
1121 CHAR16 *TempSpot;
1122 UINTN NewSize;
1123 CONST CHAR16 *MapName;
1124
1125 Key.UnicodeChar = CHAR_NULL;
1126 Key.ScanCode = 0;
1127 FileHandle = NULL;
1128
1129 if (!ShellInfoObject.ShellInitSettings.BitUnion.Bits.Startup && ShellInfoObject.ShellInitSettings.FileName != NULL) {
1130 //
1131 // launch something else instead
1132 //
1133 NewSize = StrSize(ShellInfoObject.ShellInitSettings.FileName);
1134 if (ShellInfoObject.ShellInitSettings.FileOptions != NULL) {
1135 NewSize += StrSize(ShellInfoObject.ShellInitSettings.FileOptions) + sizeof(CHAR16);
1136 }
1137 FileStringPath = AllocateZeroPool(NewSize);
1138 if (FileStringPath == NULL) {
1139 return (EFI_OUT_OF_RESOURCES);
1140 }
1141 StrCpyS(FileStringPath, NewSize/sizeof(CHAR16), ShellInfoObject.ShellInitSettings.FileName);
1142 if (ShellInfoObject.ShellInitSettings.FileOptions != NULL) {
1143 StrnCatS(FileStringPath, NewSize/sizeof(CHAR16), L" ", NewSize/sizeof(CHAR16) - StrLen(FileStringPath) -1);
1144 StrnCatS(FileStringPath, NewSize/sizeof(CHAR16), ShellInfoObject.ShellInitSettings.FileOptions, NewSize/sizeof(CHAR16) - StrLen(FileStringPath) -1);
1145 }
1146 Status = RunShellCommand(FileStringPath, &CalleeStatus);
1147 if (ShellInfoObject.ShellInitSettings.BitUnion.Bits.Exit == TRUE) {
1148 ShellCommandRegisterExit(gEfiShellProtocol->BatchIsActive(), (UINT64)CalleeStatus);
1149 }
1150 FreePool(FileStringPath);
1151 return (Status);
1152
1153 }
1154
1155 //
1156 // for shell level 0 we do no scripts
1157 // Without the Startup bit overriding we allow for nostartup to prevent scripts
1158 //
1159 if ( (PcdGet8(PcdShellSupportLevel) < 1)
1160 || (ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoStartup && !ShellInfoObject.ShellInitSettings.BitUnion.Bits.Startup)
1161 ){
1162 return (EFI_SUCCESS);
1163 }
1164
1165 gST->ConOut->EnableCursor(gST->ConOut, FALSE);
1166 //
1167 // print out our warning and see if they press a key
1168 //
1169 for ( Status = EFI_UNSUPPORTED, Delay = ShellInfoObject.ShellInitSettings.Delay
1170 ; Delay != 0 && EFI_ERROR(Status)
1171 ; Delay--
1172 ){
1173 ShellPrintHiiEx(0, gST->ConOut->Mode->CursorRow, NULL, STRING_TOKEN (STR_SHELL_STARTUP_QUESTION), ShellInfoObject.HiiHandle, Delay);
1174 gBS->Stall (1000000);
1175 if (!ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoConsoleIn) {
1176 Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);
1177 }
1178 }
1179 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SHELL_CRLF), ShellInfoObject.HiiHandle);
1180 gST->ConOut->EnableCursor(gST->ConOut, TRUE);
1181
1182 //
1183 // ESC was pressed
1184 //
1185 if (Status == EFI_SUCCESS && Key.UnicodeChar == 0 && Key.ScanCode == SCAN_ESC) {
1186 return (EFI_SUCCESS);
1187 }
1188
1189 //
1190 // Try the first location (must be file system)
1191 //
1192 MapName = ShellInfoObject.NewEfiShellProtocol->GetMapFromDevicePath(&ImagePath);
1193 if (MapName != NULL) {
1194 FileStringPath = NULL;
1195 NewSize = 0;
1196 FileStringPath = StrnCatGrow(&FileStringPath, &NewSize, MapName, 0);
1197 if (FileStringPath == NULL) {
1198 Status = EFI_OUT_OF_RESOURCES;
1199 } else {
1200 TempSpot = StrStr(FileStringPath, L";");
1201 if (TempSpot != NULL) {
1202 *TempSpot = CHAR_NULL;
1203 }
1204 FileStringPath = StrnCatGrow(&FileStringPath, &NewSize, ((FILEPATH_DEVICE_PATH*)FilePath)->PathName, 0);
1205 PathRemoveLastItem(FileStringPath);
1206 FileStringPath = StrnCatGrow(&FileStringPath, &NewSize, mStartupScript, 0);
1207 Status = ShellInfoObject.NewEfiShellProtocol->OpenFileByName(FileStringPath, &FileHandle, EFI_FILE_MODE_READ);
1208 FreePool(FileStringPath);
1209 }
1210 }
1211 if (EFI_ERROR(Status)) {
1212 NamePath = FileDevicePath (NULL, mStartupScript);
1213 NewPath = AppendDevicePathNode (ImagePath, NamePath);
1214 FreePool(NamePath);
1215
1216 //
1217 // Try the location
1218 //
1219 Status = InternalOpenFileDevicePath(NewPath, &FileHandle, EFI_FILE_MODE_READ, 0);
1220 FreePool(NewPath);
1221 }
1222 //
1223 // If we got a file, run it
1224 //
1225 if (!EFI_ERROR(Status) && FileHandle != NULL) {
1226 Status = RunScriptFile (mStartupScript, FileHandle, L"", ShellInfoObject.NewShellParametersProtocol);
1227 ShellInfoObject.NewEfiShellProtocol->CloseFile(FileHandle);
1228 } else {
1229 FileStringPath = ShellFindFilePath(mStartupScript);
1230 if (FileStringPath == NULL) {
1231 //
1232 // we return success since we don't need to have a startup script
1233 //
1234 Status = EFI_SUCCESS;
1235 ASSERT(FileHandle == NULL);
1236 } else {
1237 Status = RunScriptFile(FileStringPath, NULL, L"", ShellInfoObject.NewShellParametersProtocol);
1238 FreePool(FileStringPath);
1239 }
1240 }
1241
1242
1243 return (Status);
1244 }
1245
1246 /**
1247 Function to perform the shell prompt looping. It will do a single prompt,
1248 dispatch the result, and then return. It is expected that the caller will
1249 call this function in a loop many times.
1250
1251 @retval EFI_SUCCESS
1252 @retval RETURN_ABORTED
1253 **/
1254 EFI_STATUS
1255 EFIAPI
1256 DoShellPrompt (
1257 VOID
1258 )
1259 {
1260 UINTN Column;
1261 UINTN Row;
1262 CHAR16 *CmdLine;
1263 CONST CHAR16 *CurDir;
1264 UINTN BufferSize;
1265 EFI_STATUS Status;
1266 LIST_ENTRY OldBufferList;
1267
1268 CurDir = NULL;
1269
1270 //
1271 // Get screen setting to decide size of the command line buffer
1272 //
1273 gST->ConOut->QueryMode (gST->ConOut, gST->ConOut->Mode->Mode, &Column, &Row);
1274 BufferSize = Column * Row * sizeof (CHAR16);
1275 CmdLine = AllocateZeroPool (BufferSize);
1276 if (CmdLine == NULL) {
1277 return EFI_OUT_OF_RESOURCES;
1278 }
1279
1280 SaveBufferList(&OldBufferList);
1281 CurDir = ShellInfoObject.NewEfiShellProtocol->GetEnv(L"cwd");
1282
1283 //
1284 // Prompt for input
1285 //
1286 gST->ConOut->SetCursorPosition (gST->ConOut, 0, gST->ConOut->Mode->CursorRow);
1287
1288 if (CurDir != NULL && StrLen(CurDir) > 1) {
1289 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SHELL_CURDIR), ShellInfoObject.HiiHandle, CurDir);
1290 } else {
1291 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SHELL_SHELL), ShellInfoObject.HiiHandle);
1292 }
1293
1294 //
1295 // Read a line from the console
1296 //
1297 Status = ShellInfoObject.NewEfiShellProtocol->ReadFile(ShellInfoObject.NewShellParametersProtocol->StdIn, &BufferSize, CmdLine);
1298
1299 //
1300 // Null terminate the string and parse it
1301 //
1302 if (!EFI_ERROR (Status)) {
1303 CmdLine[BufferSize / sizeof (CHAR16)] = CHAR_NULL;
1304 Status = RunCommand(CmdLine);
1305 }
1306
1307 //
1308 // Done with this command
1309 //
1310 RestoreBufferList(&OldBufferList);
1311 FreePool (CmdLine);
1312 return Status;
1313 }
1314
1315 /**
1316 Add a buffer to the Buffer To Free List for safely returning buffers to other
1317 places without risking letting them modify internal shell information.
1318
1319 @param Buffer Something to pass to FreePool when the shell is exiting.
1320 **/
1321 VOID*
1322 EFIAPI
1323 AddBufferToFreeList (
1324 VOID *Buffer
1325 )
1326 {
1327 BUFFER_LIST *BufferListEntry;
1328
1329 if (Buffer == NULL) {
1330 return (NULL);
1331 }
1332
1333 BufferListEntry = AllocateZeroPool (sizeof (BUFFER_LIST));
1334 if (BufferListEntry == NULL) {
1335 return NULL;
1336 }
1337
1338 BufferListEntry->Buffer = Buffer;
1339 InsertTailList (&ShellInfoObject.BufferToFreeList.Link, &BufferListEntry->Link);
1340 return (Buffer);
1341 }
1342
1343
1344 /**
1345 Create a new buffer list and stores the old one to OldBufferList
1346
1347 @param OldBufferList The temporary list head used to store the nodes in BufferToFreeList.
1348 **/
1349 VOID
1350 SaveBufferList (
1351 OUT LIST_ENTRY *OldBufferList
1352 )
1353 {
1354 CopyMem (OldBufferList, &ShellInfoObject.BufferToFreeList.Link, sizeof (LIST_ENTRY));
1355 InitializeListHead (&ShellInfoObject.BufferToFreeList.Link);
1356 }
1357
1358 /**
1359 Restore previous nodes into BufferToFreeList .
1360
1361 @param OldBufferList The temporary list head used to store the nodes in BufferToFreeList.
1362 **/
1363 VOID
1364 RestoreBufferList (
1365 IN OUT LIST_ENTRY *OldBufferList
1366 )
1367 {
1368 FreeBufferList (&ShellInfoObject.BufferToFreeList);
1369 CopyMem (&ShellInfoObject.BufferToFreeList.Link, OldBufferList, sizeof (LIST_ENTRY));
1370 }
1371
1372
1373 /**
1374 Add a buffer to the Line History List
1375
1376 @param Buffer The line buffer to add.
1377 **/
1378 VOID
1379 EFIAPI
1380 AddLineToCommandHistory(
1381 IN CONST CHAR16 *Buffer
1382 )
1383 {
1384 BUFFER_LIST *Node;
1385 BUFFER_LIST *Walker;
1386 UINT16 MaxHistoryCmdCount;
1387 UINT16 Count;
1388
1389 Count = 0;
1390 MaxHistoryCmdCount = PcdGet16(PcdShellMaxHistoryCommandCount);
1391
1392 if (MaxHistoryCmdCount == 0) {
1393 return ;
1394 }
1395
1396
1397 Node = AllocateZeroPool(sizeof(BUFFER_LIST));
1398 if (Node == NULL) {
1399 return;
1400 }
1401
1402 Node->Buffer = AllocateCopyPool (StrSize (Buffer), Buffer);
1403 if (Node->Buffer == NULL) {
1404 FreePool (Node);
1405 return;
1406 }
1407
1408 for ( Walker = (BUFFER_LIST*)GetFirstNode(&ShellInfoObject.ViewingSettings.CommandHistory.Link)
1409 ; !IsNull(&ShellInfoObject.ViewingSettings.CommandHistory.Link, &Walker->Link)
1410 ; Walker = (BUFFER_LIST*)GetNextNode(&ShellInfoObject.ViewingSettings.CommandHistory.Link, &Walker->Link)
1411 ){
1412 Count++;
1413 }
1414 if (Count < MaxHistoryCmdCount){
1415 InsertTailList(&ShellInfoObject.ViewingSettings.CommandHistory.Link, &Node->Link);
1416 } else {
1417 Walker = (BUFFER_LIST*)GetFirstNode(&ShellInfoObject.ViewingSettings.CommandHistory.Link);
1418 RemoveEntryList(&Walker->Link);
1419 if (Walker->Buffer != NULL) {
1420 FreePool(Walker->Buffer);
1421 }
1422 FreePool(Walker);
1423 InsertTailList(&ShellInfoObject.ViewingSettings.CommandHistory.Link, &Node->Link);
1424 }
1425 }
1426
1427 /**
1428 Checks if a string is an alias for another command. If yes, then it replaces the alias name
1429 with the correct command name.
1430
1431 @param[in, out] CommandString Upon entry the potential alias. Upon return the
1432 command name if it was an alias. If it was not
1433 an alias it will be unchanged. This function may
1434 change the buffer to fit the command name.
1435
1436 @retval EFI_SUCCESS The name was changed.
1437 @retval EFI_SUCCESS The name was not an alias.
1438 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
1439 **/
1440 EFI_STATUS
1441 EFIAPI
1442 ShellConvertAlias(
1443 IN OUT CHAR16 **CommandString
1444 )
1445 {
1446 CONST CHAR16 *NewString;
1447
1448 NewString = ShellInfoObject.NewEfiShellProtocol->GetAlias(*CommandString, NULL);
1449 if (NewString == NULL) {
1450 return (EFI_SUCCESS);
1451 }
1452 FreePool(*CommandString);
1453 *CommandString = AllocateCopyPool(StrSize(NewString), NewString);
1454 if (*CommandString == NULL) {
1455 return (EFI_OUT_OF_RESOURCES);
1456 }
1457 return (EFI_SUCCESS);
1458 }
1459
1460 /**
1461 This function will eliminate unreplaced (and therefore non-found) environment variables.
1462
1463 @param[in,out] CmdLine The command line to update.
1464 **/
1465 EFI_STATUS
1466 EFIAPI
1467 StripUnreplacedEnvironmentVariables(
1468 IN OUT CHAR16 *CmdLine
1469 )
1470 {
1471 CHAR16 *FirstPercent;
1472 CHAR16 *FirstQuote;
1473 CHAR16 *SecondPercent;
1474 CHAR16 *SecondQuote;
1475 CHAR16 *CurrentLocator;
1476
1477 for (CurrentLocator = CmdLine ; CurrentLocator != NULL ; ) {
1478 FirstQuote = FindNextInstance(CurrentLocator, L"\"", TRUE);
1479 FirstPercent = FindNextInstance(CurrentLocator, L"%", TRUE);
1480 SecondPercent = FirstPercent!=NULL?FindNextInstance(FirstPercent+1, L"%", TRUE):NULL;
1481 if (FirstPercent == NULL || SecondPercent == NULL) {
1482 //
1483 // If we ever don't have 2 % we are done.
1484 //
1485 break;
1486 }
1487
1488 if (FirstQuote!= NULL && FirstQuote < FirstPercent) {
1489 SecondQuote = FindNextInstance(FirstQuote+1, L"\"", TRUE);
1490 //
1491 // Quote is first found
1492 //
1493
1494 if (SecondQuote < FirstPercent) {
1495 //
1496 // restart after the pair of "
1497 //
1498 CurrentLocator = SecondQuote + 1;
1499 } else /* FirstPercent < SecondQuote */{
1500 //
1501 // Restart on the first percent
1502 //
1503 CurrentLocator = FirstPercent;
1504 }
1505 continue;
1506 }
1507
1508 if (FirstQuote == NULL || SecondPercent < FirstQuote) {
1509 if (IsValidEnvironmentVariableName(FirstPercent, SecondPercent)) {
1510 //
1511 // We need to remove from FirstPercent to SecondPercent
1512 //
1513 CopyMem(FirstPercent, SecondPercent + 1, StrSize(SecondPercent + 1));
1514 //
1515 // don't need to update the locator. both % characters are gone.
1516 //
1517 } else {
1518 CurrentLocator = SecondPercent + 1;
1519 }
1520 continue;
1521 }
1522 CurrentLocator = FirstQuote;
1523 }
1524 return (EFI_SUCCESS);
1525 }
1526
1527 /**
1528 Function allocates a new command line and replaces all instances of environment
1529 variable names that are correctly preset to their values.
1530
1531 If the return value is not NULL the memory must be caller freed.
1532
1533 @param[in] OriginalCommandLine The original command line
1534
1535 @retval NULL An error occurred.
1536 @return The new command line with no environment variables present.
1537 **/
1538 CHAR16*
1539 EFIAPI
1540 ShellConvertVariables (
1541 IN CONST CHAR16 *OriginalCommandLine
1542 )
1543 {
1544 CONST CHAR16 *MasterEnvList;
1545 UINTN NewSize;
1546 CHAR16 *NewCommandLine1;
1547 CHAR16 *NewCommandLine2;
1548 CHAR16 *Temp;
1549 UINTN ItemSize;
1550 CHAR16 *ItemTemp;
1551 SCRIPT_FILE *CurrentScriptFile;
1552 ALIAS_LIST *AliasListNode;
1553
1554 ASSERT(OriginalCommandLine != NULL);
1555
1556 ItemSize = 0;
1557 NewSize = StrSize(OriginalCommandLine);
1558 CurrentScriptFile = ShellCommandGetCurrentScriptFile();
1559 Temp = NULL;
1560
1561 ///@todo update this to handle the %0 - %9 for scripting only (borrow from line 1256 area) ? ? ?
1562
1563 //
1564 // calculate the size required for the post-conversion string...
1565 //
1566 if (CurrentScriptFile != NULL) {
1567 for (AliasListNode = (ALIAS_LIST*)GetFirstNode(&CurrentScriptFile->SubstList)
1568 ; !IsNull(&CurrentScriptFile->SubstList, &AliasListNode->Link)
1569 ; AliasListNode = (ALIAS_LIST*)GetNextNode(&CurrentScriptFile->SubstList, &AliasListNode->Link)
1570 ){
1571 for (Temp = StrStr(OriginalCommandLine, AliasListNode->Alias)
1572 ; Temp != NULL
1573 ; Temp = StrStr(Temp+1, AliasListNode->Alias)
1574 ){
1575 //
1576 // we need a preceding and if there is space no ^ preceding (if no space ignore)
1577 //
1578 if ((((Temp-OriginalCommandLine)>2) && *(Temp-2) != L'^') || ((Temp-OriginalCommandLine)<=2)) {
1579 NewSize += StrSize(AliasListNode->CommandString);
1580 }
1581 }
1582 }
1583 }
1584
1585 for (MasterEnvList = EfiShellGetEnv(NULL)
1586 ; MasterEnvList != NULL && *MasterEnvList != CHAR_NULL //&& *(MasterEnvList+1) != CHAR_NULL
1587 ; MasterEnvList += StrLen(MasterEnvList) + 1
1588 ){
1589 if (StrSize(MasterEnvList) > ItemSize) {
1590 ItemSize = StrSize(MasterEnvList);
1591 }
1592 for (Temp = StrStr(OriginalCommandLine, MasterEnvList)
1593 ; Temp != NULL
1594 ; Temp = StrStr(Temp+1, MasterEnvList)
1595 ){
1596 //
1597 // we need a preceding and following % and if there is space no ^ preceding (if no space ignore)
1598 //
1599 if (*(Temp-1) == L'%' && *(Temp+StrLen(MasterEnvList)) == L'%' &&
1600 ((((Temp-OriginalCommandLine)>2) && *(Temp-2) != L'^') || ((Temp-OriginalCommandLine)<=2))) {
1601 NewSize+=StrSize(EfiShellGetEnv(MasterEnvList));
1602 }
1603 }
1604 }
1605
1606 //
1607 // now do the replacements...
1608 //
1609 NewCommandLine1 = AllocateCopyPool(NewSize, OriginalCommandLine);
1610 NewCommandLine2 = AllocateZeroPool(NewSize);
1611 ItemTemp = AllocateZeroPool(ItemSize+(2*sizeof(CHAR16)));
1612 if (NewCommandLine1 == NULL || NewCommandLine2 == NULL || ItemTemp == NULL) {
1613 SHELL_FREE_NON_NULL(NewCommandLine1);
1614 SHELL_FREE_NON_NULL(NewCommandLine2);
1615 SHELL_FREE_NON_NULL(ItemTemp);
1616 return (NULL);
1617 }
1618 for (MasterEnvList = EfiShellGetEnv(NULL)
1619 ; MasterEnvList != NULL && *MasterEnvList != CHAR_NULL
1620 ; MasterEnvList += StrLen(MasterEnvList) + 1
1621 ){
1622 StrCpyS( ItemTemp,
1623 ((ItemSize+(2*sizeof(CHAR16)))/sizeof(CHAR16)),
1624 L"%"
1625 );
1626 StrCatS( ItemTemp,
1627 ((ItemSize+(2*sizeof(CHAR16)))/sizeof(CHAR16)),
1628 MasterEnvList
1629 );
1630 StrCatS( ItemTemp,
1631 ((ItemSize+(2*sizeof(CHAR16)))/sizeof(CHAR16)),
1632 L"%"
1633 );
1634 ShellCopySearchAndReplace(NewCommandLine1, NewCommandLine2, NewSize, ItemTemp, EfiShellGetEnv(MasterEnvList), TRUE, FALSE);
1635 StrCpyS(NewCommandLine1, NewSize/sizeof(CHAR16), NewCommandLine2);
1636 }
1637 if (CurrentScriptFile != NULL) {
1638 for (AliasListNode = (ALIAS_LIST*)GetFirstNode(&CurrentScriptFile->SubstList)
1639 ; !IsNull(&CurrentScriptFile->SubstList, &AliasListNode->Link)
1640 ; AliasListNode = (ALIAS_LIST*)GetNextNode(&CurrentScriptFile->SubstList, &AliasListNode->Link)
1641 ){
1642 ShellCopySearchAndReplace(NewCommandLine1, NewCommandLine2, NewSize, AliasListNode->Alias, AliasListNode->CommandString, TRUE, FALSE);
1643 StrCpyS(NewCommandLine1, NewSize/sizeof(CHAR16), NewCommandLine2);
1644 }
1645 }
1646
1647 //
1648 // Remove non-existent environment variables
1649 //
1650 StripUnreplacedEnvironmentVariables(NewCommandLine1);
1651
1652 //
1653 // Now cleanup any straggler intentionally ignored "%" characters
1654 //
1655 ShellCopySearchAndReplace(NewCommandLine1, NewCommandLine2, NewSize, L"^%", L"%", TRUE, FALSE);
1656 StrCpyS(NewCommandLine1, NewSize/sizeof(CHAR16), NewCommandLine2);
1657
1658 FreePool(NewCommandLine2);
1659 FreePool(ItemTemp);
1660
1661 return (NewCommandLine1);
1662 }
1663
1664 /**
1665 Internal function to run a command line with pipe usage.
1666
1667 @param[in] CmdLine The pointer to the command line.
1668 @param[in] StdIn The pointer to the Standard input.
1669 @param[in] StdOut The pointer to the Standard output.
1670
1671 @retval EFI_SUCCESS The split command is executed successfully.
1672 @retval other Some error occurs when executing the split command.
1673 **/
1674 EFI_STATUS
1675 EFIAPI
1676 RunSplitCommand(
1677 IN CONST CHAR16 *CmdLine,
1678 IN SHELL_FILE_HANDLE *StdIn,
1679 IN SHELL_FILE_HANDLE *StdOut
1680 )
1681 {
1682 EFI_STATUS Status;
1683 CHAR16 *NextCommandLine;
1684 CHAR16 *OurCommandLine;
1685 UINTN Size1;
1686 UINTN Size2;
1687 SPLIT_LIST *Split;
1688 SHELL_FILE_HANDLE *TempFileHandle;
1689 BOOLEAN Unicode;
1690
1691 ASSERT(StdOut == NULL);
1692
1693 ASSERT(StrStr(CmdLine, L"|") != NULL);
1694
1695 Status = EFI_SUCCESS;
1696 NextCommandLine = NULL;
1697 OurCommandLine = NULL;
1698 Size1 = 0;
1699 Size2 = 0;
1700
1701 NextCommandLine = StrnCatGrow(&NextCommandLine, &Size1, StrStr(CmdLine, L"|")+1, 0);
1702 OurCommandLine = StrnCatGrow(&OurCommandLine , &Size2, CmdLine , StrStr(CmdLine, L"|") - CmdLine);
1703
1704 if (NextCommandLine == NULL || OurCommandLine == NULL) {
1705 SHELL_FREE_NON_NULL(OurCommandLine);
1706 SHELL_FREE_NON_NULL(NextCommandLine);
1707 return (EFI_OUT_OF_RESOURCES);
1708 } else if (StrStr(OurCommandLine, L"|") != NULL || Size1 == 0 || Size2 == 0) {
1709 SHELL_FREE_NON_NULL(OurCommandLine);
1710 SHELL_FREE_NON_NULL(NextCommandLine);
1711 return (EFI_INVALID_PARAMETER);
1712 } else if (NextCommandLine[0] == L'a' &&
1713 (NextCommandLine[1] == L' ' || NextCommandLine[1] == CHAR_NULL)
1714 ){
1715 CopyMem(NextCommandLine, NextCommandLine+1, StrSize(NextCommandLine) - sizeof(NextCommandLine[0]));
1716 while (NextCommandLine[0] == L' ') {
1717 CopyMem(NextCommandLine, NextCommandLine+1, StrSize(NextCommandLine) - sizeof(NextCommandLine[0]));
1718 }
1719 if (NextCommandLine[0] == CHAR_NULL) {
1720 SHELL_FREE_NON_NULL(OurCommandLine);
1721 SHELL_FREE_NON_NULL(NextCommandLine);
1722 return (EFI_INVALID_PARAMETER);
1723 }
1724 Unicode = FALSE;
1725 } else {
1726 Unicode = TRUE;
1727 }
1728
1729
1730 //
1731 // make a SPLIT_LIST item and add to list
1732 //
1733 Split = AllocateZeroPool(sizeof(SPLIT_LIST));
1734 if (Split == NULL) {
1735 return EFI_OUT_OF_RESOURCES;
1736 }
1737 Split->SplitStdIn = StdIn;
1738 Split->SplitStdOut = ConvertEfiFileProtocolToShellHandle(CreateFileInterfaceMem(Unicode), NULL);
1739 ASSERT(Split->SplitStdOut != NULL);
1740 InsertHeadList(&ShellInfoObject.SplitList.Link, &Split->Link);
1741
1742 Status = RunCommand(OurCommandLine);
1743
1744 //
1745 // move the output from the first to the in to the second.
1746 //
1747 TempFileHandle = Split->SplitStdOut;
1748 if (Split->SplitStdIn == StdIn) {
1749 Split->SplitStdOut = NULL;
1750 } else {
1751 Split->SplitStdOut = Split->SplitStdIn;
1752 }
1753 Split->SplitStdIn = TempFileHandle;
1754 ShellInfoObject.NewEfiShellProtocol->SetFilePosition(ConvertShellHandleToEfiFileProtocol(Split->SplitStdIn), 0);
1755
1756 if (!EFI_ERROR(Status)) {
1757 Status = RunCommand(NextCommandLine);
1758 }
1759
1760 //
1761 // remove the top level from the ScriptList
1762 //
1763 ASSERT((SPLIT_LIST*)GetFirstNode(&ShellInfoObject.SplitList.Link) == Split);
1764 RemoveEntryList(&Split->Link);
1765
1766 //
1767 // Note that the original StdIn is now the StdOut...
1768 //
1769 if (Split->SplitStdOut != NULL) {
1770 ShellInfoObject.NewEfiShellProtocol->CloseFile(ConvertShellHandleToEfiFileProtocol(Split->SplitStdOut));
1771 }
1772 if (Split->SplitStdIn != NULL) {
1773 ShellInfoObject.NewEfiShellProtocol->CloseFile(ConvertShellHandleToEfiFileProtocol(Split->SplitStdIn));
1774 FreePool (Split->SplitStdIn);
1775 }
1776
1777 FreePool(Split);
1778 FreePool(NextCommandLine);
1779 FreePool(OurCommandLine);
1780
1781 return (Status);
1782 }
1783
1784 /**
1785 Take the original command line, substitute any variables, free
1786 the original string, return the modified copy.
1787
1788 @param[in] CmdLine pointer to the command line to update.
1789
1790 @retval EFI_SUCCESS the function was successful.
1791 @retval EFI_OUT_OF_RESOURCES a memory allocation failed.
1792 **/
1793 EFI_STATUS
1794 EFIAPI
1795 ShellSubstituteVariables(
1796 IN CHAR16 **CmdLine
1797 )
1798 {
1799 CHAR16 *NewCmdLine;
1800 NewCmdLine = ShellConvertVariables(*CmdLine);
1801 SHELL_FREE_NON_NULL(*CmdLine);
1802 if (NewCmdLine == NULL) {
1803 return (EFI_OUT_OF_RESOURCES);
1804 }
1805 *CmdLine = NewCmdLine;
1806 return (EFI_SUCCESS);
1807 }
1808
1809 /**
1810 Take the original command line, substitute any alias in the first group of space delimited characters, free
1811 the original string, return the modified copy.
1812
1813 @param[in] CmdLine pointer to the command line to update.
1814
1815 @retval EFI_SUCCESS the function was successful.
1816 @retval EFI_OUT_OF_RESOURCES a memory allocation failed.
1817 **/
1818 EFI_STATUS
1819 EFIAPI
1820 ShellSubstituteAliases(
1821 IN CHAR16 **CmdLine
1822 )
1823 {
1824 CHAR16 *NewCmdLine;
1825 CHAR16 *CommandName;
1826 EFI_STATUS Status;
1827 UINTN PostAliasSize;
1828 ASSERT(CmdLine != NULL);
1829 ASSERT(*CmdLine!= NULL);
1830
1831
1832 CommandName = NULL;
1833 if (StrStr((*CmdLine), L" ") == NULL){
1834 StrnCatGrow(&CommandName, NULL, (*CmdLine), 0);
1835 } else {
1836 StrnCatGrow(&CommandName, NULL, (*CmdLine), StrStr((*CmdLine), L" ") - (*CmdLine));
1837 }
1838
1839 //
1840 // This cannot happen 'inline' since the CmdLine can need extra space.
1841 //
1842 NewCmdLine = NULL;
1843 if (!ShellCommandIsCommandOnList(CommandName)) {
1844 //
1845 // Convert via alias
1846 //
1847 Status = ShellConvertAlias(&CommandName);
1848 if (EFI_ERROR(Status)){
1849 return (Status);
1850 }
1851 PostAliasSize = 0;
1852 NewCmdLine = StrnCatGrow(&NewCmdLine, &PostAliasSize, CommandName, 0);
1853 if (NewCmdLine == NULL) {
1854 SHELL_FREE_NON_NULL(CommandName);
1855 SHELL_FREE_NON_NULL(*CmdLine);
1856 return (EFI_OUT_OF_RESOURCES);
1857 }
1858 NewCmdLine = StrnCatGrow(&NewCmdLine, &PostAliasSize, StrStr((*CmdLine), L" "), 0);
1859 if (NewCmdLine == NULL) {
1860 SHELL_FREE_NON_NULL(CommandName);
1861 SHELL_FREE_NON_NULL(*CmdLine);
1862 return (EFI_OUT_OF_RESOURCES);
1863 }
1864 } else {
1865 NewCmdLine = StrnCatGrow(&NewCmdLine, NULL, (*CmdLine), 0);
1866 }
1867
1868 SHELL_FREE_NON_NULL(*CmdLine);
1869 SHELL_FREE_NON_NULL(CommandName);
1870
1871 //
1872 // re-assign the passed in double pointer to point to our newly allocated buffer
1873 //
1874 *CmdLine = NewCmdLine;
1875
1876 return (EFI_SUCCESS);
1877 }
1878
1879 /**
1880 Takes the Argv[0] part of the command line and determine the meaning of it.
1881
1882 @param[in] CmdName pointer to the command line to update.
1883
1884 @retval Internal_Command The name is an internal command.
1885 @retval File_Sys_Change the name is a file system change.
1886 @retval Script_File_Name the name is a NSH script file.
1887 @retval Unknown_Invalid the name is unknown.
1888 @retval Efi_Application the name is an application (.EFI).
1889 **/
1890 SHELL_OPERATION_TYPES
1891 EFIAPI
1892 GetOperationType(
1893 IN CONST CHAR16 *CmdName
1894 )
1895 {
1896 CHAR16* FileWithPath;
1897 CONST CHAR16* TempLocation;
1898 CONST CHAR16* TempLocation2;
1899
1900 FileWithPath = NULL;
1901 //
1902 // test for an internal command.
1903 //
1904 if (ShellCommandIsCommandOnList(CmdName)) {
1905 return (Internal_Command);
1906 }
1907
1908 //
1909 // Test for file system change request. anything ending with first : and cant have spaces.
1910 //
1911 if (CmdName[(StrLen(CmdName)-1)] == L':') {
1912 if ( StrStr(CmdName, L" ") != NULL
1913 || StrLen(StrStr(CmdName, L":")) > 1
1914 ) {
1915 return (Unknown_Invalid);
1916 }
1917 return (File_Sys_Change);
1918 }
1919
1920 //
1921 // Test for a file
1922 //
1923 if ((FileWithPath = ShellFindFilePathEx(CmdName, mExecutableExtensions)) != NULL) {
1924 //
1925 // See if that file has a script file extension
1926 //
1927 if (StrLen(FileWithPath) > 4) {
1928 TempLocation = FileWithPath+StrLen(FileWithPath)-4;
1929 TempLocation2 = mScriptExtension;
1930 if (StringNoCaseCompare((VOID*)(&TempLocation), (VOID*)(&TempLocation2)) == 0) {
1931 SHELL_FREE_NON_NULL(FileWithPath);
1932 return (Script_File_Name);
1933 }
1934 }
1935
1936 //
1937 // Was a file, but not a script. we treat this as an application.
1938 //
1939 SHELL_FREE_NON_NULL(FileWithPath);
1940 return (Efi_Application);
1941 }
1942
1943 SHELL_FREE_NON_NULL(FileWithPath);
1944 //
1945 // No clue what this is... return invalid flag...
1946 //
1947 return (Unknown_Invalid);
1948 }
1949
1950 /**
1951 Determine if the first item in a command line is valid.
1952
1953 @param[in] CmdLine The command line to parse.
1954
1955 @retval EFI_SUCCESS The item is valid.
1956 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
1957 @retval EFI_NOT_FOUND The operation type is unknown or invalid.
1958 **/
1959 EFI_STATUS
1960 EFIAPI
1961 IsValidSplit(
1962 IN CONST CHAR16 *CmdLine
1963 )
1964 {
1965 CHAR16 *Temp;
1966 CHAR16 *FirstParameter;
1967 CHAR16 *TempWalker;
1968 EFI_STATUS Status;
1969
1970 Temp = NULL;
1971
1972 Temp = StrnCatGrow(&Temp, NULL, CmdLine, 0);
1973 if (Temp == NULL) {
1974 return (EFI_OUT_OF_RESOURCES);
1975 }
1976
1977 FirstParameter = StrStr(Temp, L"|");
1978 if (FirstParameter != NULL) {
1979 *FirstParameter = CHAR_NULL;
1980 }
1981
1982 FirstParameter = NULL;
1983
1984 //
1985 // Process the command line
1986 //
1987 Status = ProcessCommandLineToFinal(&Temp);
1988
1989 if (!EFI_ERROR(Status)) {
1990 FirstParameter = AllocateZeroPool(StrSize(CmdLine));
1991 if (FirstParameter == NULL) {
1992 SHELL_FREE_NON_NULL(Temp);
1993 return (EFI_OUT_OF_RESOURCES);
1994 }
1995 TempWalker = (CHAR16*)Temp;
1996 if (!EFI_ERROR (ShellGetNextParameter (&TempWalker, FirstParameter, StrSize(CmdLine), TRUE))) {
1997 if (GetOperationType(FirstParameter) == Unknown_Invalid) {
1998 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SHELL_NOT_FOUND), ShellInfoObject.HiiHandle, FirstParameter);
1999 SetLastError(SHELL_NOT_FOUND);
2000 Status = EFI_NOT_FOUND;
2001 }
2002 }
2003 }
2004
2005 SHELL_FREE_NON_NULL(Temp);
2006 SHELL_FREE_NON_NULL(FirstParameter);
2007 return Status;
2008 }
2009
2010 /**
2011 Determine if a command line contains with a split contains only valid commands.
2012
2013 @param[in] CmdLine The command line to parse.
2014
2015 @retval EFI_SUCCESS CmdLine has only valid commands, application, or has no split.
2016 @retval EFI_ABORTED CmdLine has at least one invalid command or application.
2017 **/
2018 EFI_STATUS
2019 EFIAPI
2020 VerifySplit(
2021 IN CONST CHAR16 *CmdLine
2022 )
2023 {
2024 CONST CHAR16 *TempSpot;
2025 EFI_STATUS Status;
2026
2027 //
2028 // If this was the only item, then get out
2029 //
2030 if (!ContainsSplit(CmdLine)) {
2031 return (EFI_SUCCESS);
2032 }
2033
2034 //
2035 // Verify up to the pipe or end character
2036 //
2037 Status = IsValidSplit(CmdLine);
2038 if (EFI_ERROR(Status)) {
2039 return (Status);
2040 }
2041
2042 //
2043 // recurse to verify the next item
2044 //
2045 TempSpot = ShellFindFirstCharacter(CmdLine, L"|", TRUE) + 1;
2046 if (*TempSpot == L'a' &&
2047 (*(TempSpot + 1) == L' ' || *(TempSpot + 1) == CHAR_NULL)
2048 ) {
2049 // If it's an ASCII pipe '|a'
2050 TempSpot += 1;
2051 }
2052
2053 return (VerifySplit(TempSpot));
2054 }
2055
2056 /**
2057 Process a split based operation.
2058
2059 @param[in] CmdLine pointer to the command line to process
2060
2061 @retval EFI_SUCCESS The operation was successful
2062 @return an error occurred.
2063 **/
2064 EFI_STATUS
2065 EFIAPI
2066 ProcessNewSplitCommandLine(
2067 IN CONST CHAR16 *CmdLine
2068 )
2069 {
2070 SPLIT_LIST *Split;
2071 EFI_STATUS Status;
2072
2073 Status = VerifySplit(CmdLine);
2074 if (EFI_ERROR(Status)) {
2075 return (Status);
2076 }
2077
2078 Split = NULL;
2079
2080 //
2081 // are we in an existing split???
2082 //
2083 if (!IsListEmpty(&ShellInfoObject.SplitList.Link)) {
2084 Split = (SPLIT_LIST*)GetFirstNode(&ShellInfoObject.SplitList.Link);
2085 }
2086
2087 if (Split == NULL) {
2088 Status = RunSplitCommand(CmdLine, NULL, NULL);
2089 } else {
2090 Status = RunSplitCommand(CmdLine, Split->SplitStdIn, Split->SplitStdOut);
2091 }
2092 if (EFI_ERROR(Status)) {
2093 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SHELL_INVALID_SPLIT), ShellInfoObject.HiiHandle, CmdLine);
2094 }
2095 return (Status);
2096 }
2097
2098 /**
2099 Handle a request to change the current file system.
2100
2101 @param[in] CmdLine The passed in command line.
2102
2103 @retval EFI_SUCCESS The operation was successful.
2104 **/
2105 EFI_STATUS
2106 EFIAPI
2107 ChangeMappedDrive(
2108 IN CONST CHAR16 *CmdLine
2109 )
2110 {
2111 EFI_STATUS Status;
2112 Status = EFI_SUCCESS;
2113
2114 //
2115 // make sure we are the right operation
2116 //
2117 ASSERT(CmdLine[(StrLen(CmdLine)-1)] == L':' && StrStr(CmdLine, L" ") == NULL);
2118
2119 //
2120 // Call the protocol API to do the work
2121 //
2122 Status = ShellInfoObject.NewEfiShellProtocol->SetCurDir(NULL, CmdLine);
2123
2124 //
2125 // Report any errors
2126 //
2127 if (EFI_ERROR(Status)) {
2128 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SHELL_INVALID_MAPPING), ShellInfoObject.HiiHandle, CmdLine);
2129 }
2130
2131 return (Status);
2132 }
2133
2134 /**
2135 Reprocess the command line to direct all -? to the help command.
2136
2137 if found, will add "help" as argv[0], and move the rest later.
2138
2139 @param[in,out] CmdLine pointer to the command line to update
2140 **/
2141 EFI_STATUS
2142 EFIAPI
2143 DoHelpUpdate(
2144 IN OUT CHAR16 **CmdLine
2145 )
2146 {
2147 CHAR16 *CurrentParameter;
2148 CHAR16 *Walker;
2149 CHAR16 *NewCommandLine;
2150 EFI_STATUS Status;
2151 UINTN NewCmdLineSize;
2152
2153 Status = EFI_SUCCESS;
2154
2155 CurrentParameter = AllocateZeroPool(StrSize(*CmdLine));
2156 if (CurrentParameter == NULL) {
2157 return (EFI_OUT_OF_RESOURCES);
2158 }
2159
2160 Walker = *CmdLine;
2161 while(Walker != NULL && *Walker != CHAR_NULL) {
2162 if (!EFI_ERROR (ShellGetNextParameter (&Walker, CurrentParameter, StrSize(*CmdLine), TRUE))) {
2163 if (StrStr(CurrentParameter, L"-?") == CurrentParameter) {
2164 CurrentParameter[0] = L' ';
2165 CurrentParameter[1] = L' ';
2166 NewCmdLineSize = StrSize(L"help ") + StrSize(*CmdLine);
2167 NewCommandLine = AllocateZeroPool(NewCmdLineSize);
2168 if (NewCommandLine == NULL) {
2169 Status = EFI_OUT_OF_RESOURCES;
2170 break;
2171 }
2172
2173 //
2174 // We know the space is sufficient since we just calculated it.
2175 //
2176 StrnCpyS(NewCommandLine, NewCmdLineSize/sizeof(CHAR16), L"help ", 5);
2177 StrnCatS(NewCommandLine, NewCmdLineSize/sizeof(CHAR16), *CmdLine, StrLen(*CmdLine));
2178 SHELL_FREE_NON_NULL(*CmdLine);
2179 *CmdLine = NewCommandLine;
2180 break;
2181 }
2182 }
2183 }
2184
2185 SHELL_FREE_NON_NULL(CurrentParameter);
2186
2187 return (Status);
2188 }
2189
2190 /**
2191 Function to update the shell variable "lasterror".
2192
2193 @param[in] ErrorCode the error code to put into lasterror.
2194 **/
2195 EFI_STATUS
2196 EFIAPI
2197 SetLastError(
2198 IN CONST SHELL_STATUS ErrorCode
2199 )
2200 {
2201 CHAR16 LeString[19];
2202 if (sizeof(EFI_STATUS) == sizeof(UINT64)) {
2203 UnicodeSPrint(LeString, sizeof(LeString), L"0x%Lx", ErrorCode);
2204 } else {
2205 UnicodeSPrint(LeString, sizeof(LeString), L"0x%x", ErrorCode);
2206 }
2207 DEBUG_CODE(InternalEfiShellSetEnv(L"debuglasterror", LeString, TRUE););
2208 InternalEfiShellSetEnv(L"lasterror", LeString, TRUE);
2209
2210 return (EFI_SUCCESS);
2211 }
2212
2213 /**
2214 Converts the command line to it's post-processed form. this replaces variables and alias' per UEFI Shell spec.
2215
2216 @param[in,out] CmdLine pointer to the command line to update
2217
2218 @retval EFI_SUCCESS The operation was successful
2219 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
2220 @return some other error occurred
2221 **/
2222 EFI_STATUS
2223 EFIAPI
2224 ProcessCommandLineToFinal(
2225 IN OUT CHAR16 **CmdLine
2226 )
2227 {
2228 EFI_STATUS Status;
2229 TrimSpaces(CmdLine);
2230
2231 Status = ShellSubstituteAliases(CmdLine);
2232 if (EFI_ERROR(Status)) {
2233 return (Status);
2234 }
2235
2236 TrimSpaces(CmdLine);
2237
2238 Status = ShellSubstituteVariables(CmdLine);
2239 if (EFI_ERROR(Status)) {
2240 return (Status);
2241 }
2242 ASSERT (*CmdLine != NULL);
2243
2244 TrimSpaces(CmdLine);
2245
2246 //
2247 // update for help parsing
2248 //
2249 if (StrStr(*CmdLine, L"?") != NULL) {
2250 //
2251 // This may do nothing if the ? does not indicate help.
2252 // Save all the details for in the API below.
2253 //
2254 Status = DoHelpUpdate(CmdLine);
2255 }
2256
2257 TrimSpaces(CmdLine);
2258
2259 return (EFI_SUCCESS);
2260 }
2261
2262 /**
2263 Run an internal shell command.
2264
2265 This API will update the shell's environment since these commands are libraries.
2266
2267 @param[in] CmdLine the command line to run.
2268 @param[in] FirstParameter the first parameter on the command line
2269 @param[in] ParamProtocol the shell parameters protocol pointer
2270 @param[out] CommandStatus the status from the command line.
2271
2272 @retval EFI_SUCCESS The command was completed.
2273 @retval EFI_ABORTED The command's operation was aborted.
2274 **/
2275 EFI_STATUS
2276 EFIAPI
2277 RunInternalCommand(
2278 IN CONST CHAR16 *CmdLine,
2279 IN CHAR16 *FirstParameter,
2280 IN EFI_SHELL_PARAMETERS_PROTOCOL *ParamProtocol,
2281 OUT EFI_STATUS *CommandStatus
2282 )
2283 {
2284 EFI_STATUS Status;
2285 UINTN Argc;
2286 CHAR16 **Argv;
2287 SHELL_STATUS CommandReturnedStatus;
2288 BOOLEAN LastError;
2289 CHAR16 *Walker;
2290 CHAR16 *NewCmdLine;
2291
2292 NewCmdLine = AllocateCopyPool (StrSize (CmdLine), CmdLine);
2293 if (NewCmdLine == NULL) {
2294 return EFI_OUT_OF_RESOURCES;
2295 }
2296
2297 for (Walker = NewCmdLine; Walker != NULL && *Walker != CHAR_NULL ; Walker++) {
2298 if (*Walker == L'^' && *(Walker+1) == L'#') {
2299 CopyMem(Walker, Walker+1, StrSize(Walker) - sizeof(Walker[0]));
2300 }
2301 }
2302
2303 //
2304 // get the argc and argv updated for internal commands
2305 //
2306 Status = UpdateArgcArgv(ParamProtocol, NewCmdLine, Internal_Command, &Argv, &Argc);
2307 if (!EFI_ERROR(Status)) {
2308 //
2309 // Run the internal command.
2310 //
2311 Status = ShellCommandRunCommandHandler(FirstParameter, &CommandReturnedStatus, &LastError);
2312
2313 if (!EFI_ERROR(Status)) {
2314 if (CommandStatus != NULL) {
2315 if (CommandReturnedStatus != SHELL_SUCCESS) {
2316 *CommandStatus = (EFI_STATUS)(CommandReturnedStatus | MAX_BIT);
2317 } else {
2318 *CommandStatus = EFI_SUCCESS;
2319 }
2320 }
2321
2322 //
2323 // Update last error status.
2324 // some commands do not update last error.
2325 //
2326 if (LastError) {
2327 SetLastError(CommandReturnedStatus);
2328 }
2329
2330 //
2331 // Pass thru the exitcode from the app.
2332 //
2333 if (ShellCommandGetExit()) {
2334 //
2335 // An Exit was requested ("exit" command), pass its value up.
2336 //
2337 Status = CommandReturnedStatus;
2338 } else if (CommandReturnedStatus != SHELL_SUCCESS && IsScriptOnlyCommand(FirstParameter)) {
2339 //
2340 // Always abort when a script only command fails for any reason
2341 //
2342 Status = EFI_ABORTED;
2343 } else if (ShellCommandGetCurrentScriptFile() != NULL && CommandReturnedStatus == SHELL_ABORTED) {
2344 //
2345 // Abort when in a script and a command aborted
2346 //
2347 Status = EFI_ABORTED;
2348 }
2349 }
2350 }
2351
2352 //
2353 // This is guaranteed to be called after UpdateArgcArgv no matter what else happened.
2354 // This is safe even if the update API failed. In this case, it may be a no-op.
2355 //
2356 RestoreArgcArgv(ParamProtocol, &Argv, &Argc);
2357
2358 //
2359 // If a script is running and the command is not a script only command, then
2360 // change return value to success so the script won't halt (unless aborted).
2361 //
2362 // Script only commands have to be able halt the script since the script will
2363 // not operate if they are failing.
2364 //
2365 if ( ShellCommandGetCurrentScriptFile() != NULL
2366 && !IsScriptOnlyCommand(FirstParameter)
2367 && Status != EFI_ABORTED
2368 ) {
2369 Status = EFI_SUCCESS;
2370 }
2371
2372 FreePool (NewCmdLine);
2373 return (Status);
2374 }
2375
2376 /**
2377 Function to run the command or file.
2378
2379 @param[in] Type the type of operation being run.
2380 @param[in] CmdLine the command line to run.
2381 @param[in] FirstParameter the first parameter on the command line
2382 @param[in] ParamProtocol the shell parameters protocol pointer
2383 @param[out] CommandStatus the status from the command line.
2384
2385 @retval EFI_SUCCESS The command was completed.
2386 @retval EFI_ABORTED The command's operation was aborted.
2387 **/
2388 EFI_STATUS
2389 EFIAPI
2390 RunCommandOrFile(
2391 IN SHELL_OPERATION_TYPES Type,
2392 IN CONST CHAR16 *CmdLine,
2393 IN CHAR16 *FirstParameter,
2394 IN EFI_SHELL_PARAMETERS_PROTOCOL *ParamProtocol,
2395 OUT EFI_STATUS *CommandStatus
2396 )
2397 {
2398 EFI_STATUS Status;
2399 EFI_STATUS StartStatus;
2400 CHAR16 *CommandWithPath;
2401 EFI_DEVICE_PATH_PROTOCOL *DevPath;
2402 SHELL_STATUS CalleeExitStatus;
2403
2404 Status = EFI_SUCCESS;
2405 CommandWithPath = NULL;
2406 DevPath = NULL;
2407 CalleeExitStatus = SHELL_INVALID_PARAMETER;
2408
2409 switch (Type) {
2410 case Internal_Command:
2411 Status = RunInternalCommand(CmdLine, FirstParameter, ParamProtocol, CommandStatus);
2412 break;
2413 case Script_File_Name:
2414 case Efi_Application:
2415 //
2416 // Process a fully qualified path
2417 //
2418 if (StrStr(FirstParameter, L":") != NULL) {
2419 ASSERT (CommandWithPath == NULL);
2420 if (ShellIsFile(FirstParameter) == EFI_SUCCESS) {
2421 CommandWithPath = StrnCatGrow(&CommandWithPath, NULL, FirstParameter, 0);
2422 }
2423 }
2424
2425 //
2426 // Process a relative path and also check in the path environment variable
2427 //
2428 if (CommandWithPath == NULL) {
2429 CommandWithPath = ShellFindFilePathEx(FirstParameter, mExecutableExtensions);
2430 }
2431
2432 //
2433 // This should be impossible now.
2434 //
2435 ASSERT(CommandWithPath != NULL);
2436
2437 //
2438 // Make sure that path is not just a directory (or not found)
2439 //
2440 if (!EFI_ERROR(ShellIsDirectory(CommandWithPath))) {
2441 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SHELL_NOT_FOUND), ShellInfoObject.HiiHandle, FirstParameter);
2442 SetLastError(SHELL_NOT_FOUND);
2443 }
2444 switch (Type) {
2445 case Script_File_Name:
2446 Status = RunScriptFile (CommandWithPath, NULL, CmdLine, ParamProtocol);
2447 break;
2448 case Efi_Application:
2449 //
2450 // Get the device path of the application image
2451 //
2452 DevPath = ShellInfoObject.NewEfiShellProtocol->GetDevicePathFromFilePath(CommandWithPath);
2453 if (DevPath == NULL){
2454 Status = EFI_OUT_OF_RESOURCES;
2455 break;
2456 }
2457
2458 //
2459 // Execute the device path
2460 //
2461 Status = InternalShellExecuteDevicePath(
2462 &gImageHandle,
2463 DevPath,
2464 CmdLine,
2465 NULL,
2466 &StartStatus
2467 );
2468
2469 SHELL_FREE_NON_NULL(DevPath);
2470
2471 if(EFI_ERROR (Status)) {
2472 CalleeExitStatus = (SHELL_STATUS) (Status & (~MAX_BIT));
2473 } else {
2474 CalleeExitStatus = (SHELL_STATUS) StartStatus;
2475 }
2476
2477 if (CommandStatus != NULL) {
2478 *CommandStatus = CalleeExitStatus;
2479 }
2480
2481 //
2482 // Update last error status.
2483 //
2484 // Status is an EFI_STATUS. Clear top bit to convert to SHELL_STATUS
2485 SetLastError(CalleeExitStatus);
2486 break;
2487 default:
2488 //
2489 // Do nothing.
2490 //
2491 break;
2492 }
2493 break;
2494 default:
2495 //
2496 // Do nothing.
2497 //
2498 break;
2499 }
2500
2501 SHELL_FREE_NON_NULL(CommandWithPath);
2502
2503 return (Status);
2504 }
2505
2506 /**
2507 Function to setup StdIn, StdErr, StdOut, and then run the command or file.
2508
2509 @param[in] Type the type of operation being run.
2510 @param[in] CmdLine the command line to run.
2511 @param[in] FirstParameter the first parameter on the command line.
2512 @param[in] ParamProtocol the shell parameters protocol pointer
2513 @param[out] CommandStatus the status from the command line.
2514
2515 @retval EFI_SUCCESS The command was completed.
2516 @retval EFI_ABORTED The command's operation was aborted.
2517 **/
2518 EFI_STATUS
2519 EFIAPI
2520 SetupAndRunCommandOrFile(
2521 IN SHELL_OPERATION_TYPES Type,
2522 IN CHAR16 *CmdLine,
2523 IN CHAR16 *FirstParameter,
2524 IN EFI_SHELL_PARAMETERS_PROTOCOL *ParamProtocol,
2525 OUT EFI_STATUS *CommandStatus
2526 )
2527 {
2528 EFI_STATUS Status;
2529 SHELL_FILE_HANDLE OriginalStdIn;
2530 SHELL_FILE_HANDLE OriginalStdOut;
2531 SHELL_FILE_HANDLE OriginalStdErr;
2532 SYSTEM_TABLE_INFO OriginalSystemTableInfo;
2533 CONST SCRIPT_FILE *ConstScriptFile;
2534
2535 //
2536 // Update the StdIn, StdOut, and StdErr for redirection to environment variables, files, etc... unicode and ASCII
2537 //
2538 Status = UpdateStdInStdOutStdErr(ParamProtocol, CmdLine, &OriginalStdIn, &OriginalStdOut, &OriginalStdErr, &OriginalSystemTableInfo);
2539
2540 //
2541 // The StdIn, StdOut, and StdErr are set up.
2542 // Now run the command, script, or application
2543 //
2544 if (!EFI_ERROR(Status)) {
2545 TrimSpaces(&CmdLine);
2546 Status = RunCommandOrFile(Type, CmdLine, FirstParameter, ParamProtocol, CommandStatus);
2547 }
2548
2549 //
2550 // Now print errors
2551 //
2552 if (EFI_ERROR(Status)) {
2553 ConstScriptFile = ShellCommandGetCurrentScriptFile();
2554 if (ConstScriptFile == NULL || ConstScriptFile->CurrentCommand == NULL) {
2555 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SHELL_ERROR), ShellInfoObject.HiiHandle, (VOID*)(Status));
2556 } else {
2557 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SHELL_ERROR_SCRIPT), ShellInfoObject.HiiHandle, (VOID*)(Status), ConstScriptFile->CurrentCommand->Line);
2558 }
2559 }
2560
2561 //
2562 // put back the original StdIn, StdOut, and StdErr
2563 //
2564 RestoreStdInStdOutStdErr(ParamProtocol, &OriginalStdIn, &OriginalStdOut, &OriginalStdErr, &OriginalSystemTableInfo);
2565
2566 return (Status);
2567 }
2568
2569 /**
2570 Function will process and run a command line.
2571
2572 This will determine if the command line represents an internal shell
2573 command or dispatch an external application.
2574
2575 @param[in] CmdLine The command line to parse.
2576 @param[out] CommandStatus The status from the command line.
2577
2578 @retval EFI_SUCCESS The command was completed.
2579 @retval EFI_ABORTED The command's operation was aborted.
2580 **/
2581 EFI_STATUS
2582 EFIAPI
2583 RunShellCommand(
2584 IN CONST CHAR16 *CmdLine,
2585 OUT EFI_STATUS *CommandStatus
2586 )
2587 {
2588 EFI_STATUS Status;
2589 CHAR16 *CleanOriginal;
2590 CHAR16 *FirstParameter;
2591 CHAR16 *TempWalker;
2592 SHELL_OPERATION_TYPES Type;
2593 CHAR16 *OldCmdLine;
2594
2595 ASSERT(CmdLine != NULL);
2596 if (StrLen(CmdLine) == 0) {
2597 return (EFI_SUCCESS);
2598 }
2599
2600 Status = EFI_SUCCESS;
2601 FirstParameter = NULL;
2602 CleanOriginal = NULL;
2603 OldCmdLine = NULL;
2604
2605 CleanOriginal = StrnCatGrow(&CleanOriginal, NULL, CmdLine, 0);
2606 if (CleanOriginal == NULL) {
2607 Status = EFI_OUT_OF_RESOURCES;
2608 goto Done;
2609 }
2610
2611 TrimSpaces(&CleanOriginal);
2612
2613 //
2614 // NULL out comments (leveraged from RunScriptFileHandle() ).
2615 // The # character on a line is used to denote that all characters on the same line
2616 // and to the right of the # are to be ignored by the shell.
2617 // Afterwards, again remove spaces, in case any were between the last command-parameter and '#'.
2618 //
2619 for (TempWalker = CleanOriginal; TempWalker != NULL && *TempWalker != CHAR_NULL; TempWalker++) {
2620 if (*TempWalker == L'^') {
2621 if (*(TempWalker + 1) == L'#') {
2622 TempWalker++;
2623 }
2624 } else if (*TempWalker == L'#') {
2625 *TempWalker = CHAR_NULL;
2626 }
2627 }
2628
2629 TrimSpaces(&CleanOriginal);
2630
2631 //
2632 // Handle case that passed in command line is just 1 or more " " characters.
2633 //
2634 if (StrLen (CleanOriginal) == 0) {
2635 Status = EFI_SUCCESS;
2636 goto Done;
2637 }
2638
2639 Status = ProcessCommandLineToFinal(&CleanOriginal);
2640 if (EFI_ERROR(Status)) {
2641 goto Done;
2642 }
2643
2644 OldCmdLine = ShellGetRawCmdLine ();
2645 ShellSetRawCmdLine (CleanOriginal);
2646
2647 //
2648 // We don't do normal processing with a split command line (output from one command input to another)
2649 //
2650 if (ContainsSplit(CleanOriginal)) {
2651 Status = ProcessNewSplitCommandLine(CleanOriginal);
2652 goto Done;
2653 }
2654
2655 //
2656 // We need the first parameter information so we can determine the operation type
2657 //
2658 FirstParameter = AllocateZeroPool(StrSize(CleanOriginal));
2659 if (FirstParameter == NULL) {
2660 Status = EFI_OUT_OF_RESOURCES;
2661 goto Done;
2662 }
2663 TempWalker = CleanOriginal;
2664 if (!EFI_ERROR (ShellGetNextParameter (&TempWalker, FirstParameter, StrSize(CleanOriginal), TRUE))) {
2665 //
2666 // Depending on the first parameter we change the behavior
2667 //
2668 switch (Type = GetOperationType(FirstParameter)) {
2669 case File_Sys_Change:
2670 Status = ChangeMappedDrive (FirstParameter);
2671 break;
2672 case Internal_Command:
2673 case Script_File_Name:
2674 case Efi_Application:
2675 Status = SetupAndRunCommandOrFile(Type, CleanOriginal, FirstParameter, ShellInfoObject.NewShellParametersProtocol, CommandStatus);
2676 break;
2677 default:
2678 //
2679 // Whatever was typed, it was invalid.
2680 //
2681 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SHELL_NOT_FOUND), ShellInfoObject.HiiHandle, FirstParameter);
2682 SetLastError(SHELL_NOT_FOUND);
2683 break;
2684 }
2685 } else {
2686 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SHELL_NOT_FOUND), ShellInfoObject.HiiHandle, FirstParameter);
2687 SetLastError(SHELL_NOT_FOUND);
2688 }
2689
2690 Done:
2691 ShellSetRawCmdLine (OldCmdLine);
2692 SHELL_FREE_NON_NULL (OldCmdLine);
2693 SHELL_FREE_NON_NULL (CleanOriginal);
2694 SHELL_FREE_NON_NULL (FirstParameter);
2695
2696 return (Status);
2697 }
2698
2699 /**
2700 Function will process and run a command line.
2701
2702 This will determine if the command line represents an internal shell
2703 command or dispatch an external application.
2704
2705 @param[in] CmdLine The command line to parse.
2706
2707 @retval EFI_SUCCESS The command was completed.
2708 @retval EFI_ABORTED The command's operation was aborted.
2709 **/
2710 EFI_STATUS
2711 EFIAPI
2712 RunCommand(
2713 IN CONST CHAR16 *CmdLine
2714 )
2715 {
2716 return (RunShellCommand(CmdLine, NULL));
2717 }
2718
2719
2720 STATIC CONST UINT16 InvalidChars[] = {L'*', L'?', L'<', L'>', L'\\', L'/', L'\"', 0x0001, 0x0002};
2721 /**
2722 Function determines if the CommandName COULD be a valid command. It does not determine whether
2723 this is a valid command. It only checks for invalid characters.
2724
2725 @param[in] CommandName The name to check
2726
2727 @retval TRUE CommandName could be a command name
2728 @retval FALSE CommandName could not be a valid command name
2729 **/
2730 BOOLEAN
2731 EFIAPI
2732 IsValidCommandName(
2733 IN CONST CHAR16 *CommandName
2734 )
2735 {
2736 UINTN Count;
2737 if (CommandName == NULL) {
2738 ASSERT(FALSE);
2739 return (FALSE);
2740 }
2741 for ( Count = 0
2742 ; Count < sizeof(InvalidChars) / sizeof(InvalidChars[0])
2743 ; Count++
2744 ){
2745 if (ScanMem16(CommandName, StrSize(CommandName), InvalidChars[Count]) != NULL) {
2746 return (FALSE);
2747 }
2748 }
2749 return (TRUE);
2750 }
2751
2752 /**
2753 Function to process a NSH script file via SHELL_FILE_HANDLE.
2754
2755 @param[in] Handle The handle to the already opened file.
2756 @param[in] Name The name of the script file.
2757
2758 @retval EFI_SUCCESS the script completed successfully
2759 **/
2760 EFI_STATUS
2761 EFIAPI
2762 RunScriptFileHandle (
2763 IN SHELL_FILE_HANDLE Handle,
2764 IN CONST CHAR16 *Name
2765 )
2766 {
2767 EFI_STATUS Status;
2768 SCRIPT_FILE *NewScriptFile;
2769 UINTN LoopVar;
2770 UINTN PrintBuffSize;
2771 CHAR16 *CommandLine;
2772 CHAR16 *CommandLine2;
2773 CHAR16 *CommandLine3;
2774 SCRIPT_COMMAND_LIST *LastCommand;
2775 BOOLEAN Ascii;
2776 BOOLEAN PreScriptEchoState;
2777 BOOLEAN PreCommandEchoState;
2778 CONST CHAR16 *CurDir;
2779 UINTN LineCount;
2780 CHAR16 LeString[50];
2781 LIST_ENTRY OldBufferList;
2782
2783 ASSERT(!ShellCommandGetScriptExit());
2784
2785 PreScriptEchoState = ShellCommandGetEchoState();
2786 PrintBuffSize = PcdGet16(PcdShellPrintBufferSize);
2787
2788 NewScriptFile = (SCRIPT_FILE*)AllocateZeroPool(sizeof(SCRIPT_FILE));
2789 if (NewScriptFile == NULL) {
2790 return (EFI_OUT_OF_RESOURCES);
2791 }
2792
2793 //
2794 // Set up the name
2795 //
2796 ASSERT(NewScriptFile->ScriptName == NULL);
2797 NewScriptFile->ScriptName = StrnCatGrow(&NewScriptFile->ScriptName, NULL, Name, 0);
2798 if (NewScriptFile->ScriptName == NULL) {
2799 DeleteScriptFileStruct(NewScriptFile);
2800 return (EFI_OUT_OF_RESOURCES);
2801 }
2802
2803 //
2804 // Save the parameters (used to replace %0 to %9 later on)
2805 //
2806 NewScriptFile->Argc = ShellInfoObject.NewShellParametersProtocol->Argc;
2807 if (NewScriptFile->Argc != 0) {
2808 NewScriptFile->Argv = (CHAR16**)AllocateZeroPool(NewScriptFile->Argc * sizeof(CHAR16*));
2809 if (NewScriptFile->Argv == NULL) {
2810 DeleteScriptFileStruct(NewScriptFile);
2811 return (EFI_OUT_OF_RESOURCES);
2812 }
2813 for (LoopVar = 0 ; LoopVar < 10 && LoopVar < NewScriptFile->Argc; LoopVar++) {
2814 ASSERT(NewScriptFile->Argv[LoopVar] == NULL);
2815 NewScriptFile->Argv[LoopVar] = StrnCatGrow(&NewScriptFile->Argv[LoopVar], NULL, ShellInfoObject.NewShellParametersProtocol->Argv[LoopVar], 0);
2816 if (NewScriptFile->Argv[LoopVar] == NULL) {
2817 DeleteScriptFileStruct(NewScriptFile);
2818 return (EFI_OUT_OF_RESOURCES);
2819 }
2820 }
2821 } else {
2822 NewScriptFile->Argv = NULL;
2823 }
2824
2825 InitializeListHead(&NewScriptFile->CommandList);
2826 InitializeListHead(&NewScriptFile->SubstList);
2827
2828 //
2829 // Now build the list of all script commands.
2830 //
2831 LineCount = 0;
2832 while(!ShellFileHandleEof(Handle)) {
2833 CommandLine = ShellFileHandleReturnLine(Handle, &Ascii);
2834 LineCount++;
2835 if (CommandLine == NULL || StrLen(CommandLine) == 0 || CommandLine[0] == '#') {
2836 SHELL_FREE_NON_NULL(CommandLine);
2837 continue;
2838 }
2839 NewScriptFile->CurrentCommand = AllocateZeroPool(sizeof(SCRIPT_COMMAND_LIST));
2840 if (NewScriptFile->CurrentCommand == NULL) {
2841 SHELL_FREE_NON_NULL(CommandLine);
2842 DeleteScriptFileStruct(NewScriptFile);
2843 return (EFI_OUT_OF_RESOURCES);
2844 }
2845
2846 NewScriptFile->CurrentCommand->Cl = CommandLine;
2847 NewScriptFile->CurrentCommand->Data = NULL;
2848 NewScriptFile->CurrentCommand->Line = LineCount;
2849
2850 InsertTailList(&NewScriptFile->CommandList, &NewScriptFile->CurrentCommand->Link);
2851 }
2852
2853 //
2854 // Add this as the topmost script file
2855 //
2856 ShellCommandSetNewScript (NewScriptFile);
2857
2858 //
2859 // Now enumerate through the commands and run each one.
2860 //
2861 CommandLine = AllocateZeroPool(PrintBuffSize);
2862 if (CommandLine == NULL) {
2863 DeleteScriptFileStruct(NewScriptFile);
2864 return (EFI_OUT_OF_RESOURCES);
2865 }
2866 CommandLine2 = AllocateZeroPool(PrintBuffSize);
2867 if (CommandLine2 == NULL) {
2868 FreePool(CommandLine);
2869 DeleteScriptFileStruct(NewScriptFile);
2870 return (EFI_OUT_OF_RESOURCES);
2871 }
2872
2873 for ( NewScriptFile->CurrentCommand = (SCRIPT_COMMAND_LIST *)GetFirstNode(&NewScriptFile->CommandList)
2874 ; !IsNull(&NewScriptFile->CommandList, &NewScriptFile->CurrentCommand->Link)
2875 ; // conditional increment in the body of the loop
2876 ){
2877 ASSERT(CommandLine2 != NULL);
2878 StrnCpyS( CommandLine2,
2879 PrintBuffSize/sizeof(CHAR16),
2880 NewScriptFile->CurrentCommand->Cl,
2881 PrintBuffSize/sizeof(CHAR16) - 1
2882 );
2883
2884 SaveBufferList(&OldBufferList);
2885
2886 //
2887 // NULL out comments
2888 //
2889 for (CommandLine3 = CommandLine2 ; CommandLine3 != NULL && *CommandLine3 != CHAR_NULL ; CommandLine3++) {
2890 if (*CommandLine3 == L'^') {
2891 if ( *(CommandLine3+1) == L':') {
2892 CopyMem(CommandLine3, CommandLine3+1, StrSize(CommandLine3) - sizeof(CommandLine3[0]));
2893 } else if (*(CommandLine3+1) == L'#') {
2894 CommandLine3++;
2895 }
2896 } else if (*CommandLine3 == L'#') {
2897 *CommandLine3 = CHAR_NULL;
2898 }
2899 }
2900
2901 if (CommandLine2 != NULL && StrLen(CommandLine2) >= 1) {
2902 //
2903 // Due to variability in starting the find and replace action we need to have both buffers the same.
2904 //
2905 StrnCpyS( CommandLine,
2906 PrintBuffSize/sizeof(CHAR16),
2907 CommandLine2,
2908 PrintBuffSize/sizeof(CHAR16) - 1
2909 );
2910
2911 //
2912 // Remove the %0 to %9 from the command line (if we have some arguments)
2913 //
2914 if (NewScriptFile->Argv != NULL) {
2915 switch (NewScriptFile->Argc) {
2916 default:
2917 Status = ShellCopySearchAndReplace(CommandLine2, CommandLine, PrintBuffSize, L"%9", NewScriptFile->Argv[9], FALSE, FALSE);
2918 ASSERT_EFI_ERROR(Status);
2919 case 9:
2920 Status = ShellCopySearchAndReplace(CommandLine, CommandLine2, PrintBuffSize, L"%8", NewScriptFile->Argv[8], FALSE, FALSE);
2921 ASSERT_EFI_ERROR(Status);
2922 case 8:
2923 Status = ShellCopySearchAndReplace(CommandLine2, CommandLine, PrintBuffSize, L"%7", NewScriptFile->Argv[7], FALSE, FALSE);
2924 ASSERT_EFI_ERROR(Status);
2925 case 7:
2926 Status = ShellCopySearchAndReplace(CommandLine, CommandLine2, PrintBuffSize, L"%6", NewScriptFile->Argv[6], FALSE, FALSE);
2927 ASSERT_EFI_ERROR(Status);
2928 case 6:
2929 Status = ShellCopySearchAndReplace(CommandLine2, CommandLine, PrintBuffSize, L"%5", NewScriptFile->Argv[5], FALSE, FALSE);
2930 ASSERT_EFI_ERROR(Status);
2931 case 5:
2932 Status = ShellCopySearchAndReplace(CommandLine, CommandLine2, PrintBuffSize, L"%4", NewScriptFile->Argv[4], FALSE, FALSE);
2933 ASSERT_EFI_ERROR(Status);
2934 case 4:
2935 Status = ShellCopySearchAndReplace(CommandLine2, CommandLine, PrintBuffSize, L"%3", NewScriptFile->Argv[3], FALSE, FALSE);
2936 ASSERT_EFI_ERROR(Status);
2937 case 3:
2938 Status = ShellCopySearchAndReplace(CommandLine, CommandLine2, PrintBuffSize, L"%2", NewScriptFile->Argv[2], FALSE, FALSE);
2939 ASSERT_EFI_ERROR(Status);
2940 case 2:
2941 Status = ShellCopySearchAndReplace(CommandLine2, CommandLine, PrintBuffSize, L"%1", NewScriptFile->Argv[1], FALSE, FALSE);
2942 ASSERT_EFI_ERROR(Status);
2943 case 1:
2944 Status = ShellCopySearchAndReplace(CommandLine, CommandLine2, PrintBuffSize, L"%0", NewScriptFile->Argv[0], FALSE, FALSE);
2945 ASSERT_EFI_ERROR(Status);
2946 break;
2947 case 0:
2948 break;
2949 }
2950 }
2951 Status = ShellCopySearchAndReplace(CommandLine2, CommandLine, PrintBuffSize, L"%1", L"\"\"", FALSE, FALSE);
2952 Status = ShellCopySearchAndReplace(CommandLine, CommandLine2, PrintBuffSize, L"%2", L"\"\"", FALSE, FALSE);
2953 Status = ShellCopySearchAndReplace(CommandLine2, CommandLine, PrintBuffSize, L"%3", L"\"\"", FALSE, FALSE);
2954 Status = ShellCopySearchAndReplace(CommandLine, CommandLine2, PrintBuffSize, L"%4", L"\"\"", FALSE, FALSE);
2955 Status = ShellCopySearchAndReplace(CommandLine2, CommandLine, PrintBuffSize, L"%5", L"\"\"", FALSE, FALSE);
2956 Status = ShellCopySearchAndReplace(CommandLine, CommandLine2, PrintBuffSize, L"%6", L"\"\"", FALSE, FALSE);
2957 Status = ShellCopySearchAndReplace(CommandLine2, CommandLine, PrintBuffSize, L"%7", L"\"\"", FALSE, FALSE);
2958 Status = ShellCopySearchAndReplace(CommandLine, CommandLine2, PrintBuffSize, L"%8", L"\"\"", FALSE, FALSE);
2959 Status = ShellCopySearchAndReplace(CommandLine2, CommandLine, PrintBuffSize, L"%9", L"\"\"", FALSE, FALSE);
2960
2961 StrnCpyS( CommandLine2,
2962 PrintBuffSize/sizeof(CHAR16),
2963 CommandLine,
2964 PrintBuffSize/sizeof(CHAR16) - 1
2965 );
2966
2967 LastCommand = NewScriptFile->CurrentCommand;
2968
2969 for (CommandLine3 = CommandLine2 ; CommandLine3[0] == L' ' ; CommandLine3++);
2970
2971 if (CommandLine3 != NULL && CommandLine3[0] == L':' ) {
2972 //
2973 // This line is a goto target / label
2974 //
2975 } else {
2976 if (CommandLine3 != NULL && StrLen(CommandLine3) > 0) {
2977 if (CommandLine3[0] == L'@') {
2978 //
2979 // We need to save the current echo state
2980 // and disable echo for just this command.
2981 //
2982 PreCommandEchoState = ShellCommandGetEchoState();
2983 ShellCommandSetEchoState(FALSE);
2984 Status = RunCommand(CommandLine3+1);
2985
2986 //
2987 // If command was "@echo -off" or "@echo -on" then don't restore echo state
2988 //
2989 if (StrCmp (L"@echo -off", CommandLine3) != 0 &&
2990 StrCmp (L"@echo -on", CommandLine3) != 0) {
2991 //
2992 // Now restore the pre-'@' echo state.
2993 //
2994 ShellCommandSetEchoState(PreCommandEchoState);
2995 }
2996 } else {
2997 if (ShellCommandGetEchoState()) {
2998 CurDir = ShellInfoObject.NewEfiShellProtocol->GetEnv(L"cwd");
2999 if (CurDir != NULL && StrLen(CurDir) > 1) {
3000 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SHELL_CURDIR), ShellInfoObject.HiiHandle, CurDir);
3001 } else {
3002 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SHELL_SHELL), ShellInfoObject.HiiHandle);
3003 }
3004 ShellPrintEx(-1, -1, L"%s\r\n", CommandLine2);
3005 }
3006 Status = RunCommand(CommandLine3);
3007 }
3008 }
3009
3010 if (ShellCommandGetScriptExit()) {
3011 //
3012 // ShellCommandGetExitCode() always returns a UINT64
3013 //
3014 UnicodeSPrint(LeString, sizeof(LeString), L"0x%Lx", ShellCommandGetExitCode());
3015 DEBUG_CODE(InternalEfiShellSetEnv(L"debuglasterror", LeString, TRUE););
3016 InternalEfiShellSetEnv(L"lasterror", LeString, TRUE);
3017
3018 ShellCommandRegisterExit(FALSE, 0);
3019 Status = EFI_SUCCESS;
3020 RestoreBufferList(&OldBufferList);
3021 break;
3022 }
3023 if (ShellGetExecutionBreakFlag()) {
3024 RestoreBufferList(&OldBufferList);
3025 break;
3026 }
3027 if (EFI_ERROR(Status)) {
3028 RestoreBufferList(&OldBufferList);
3029 break;
3030 }
3031 if (ShellCommandGetExit()) {
3032 RestoreBufferList(&OldBufferList);
3033 break;
3034 }
3035 }
3036 //
3037 // If that commend did not update the CurrentCommand then we need to advance it...
3038 //
3039 if (LastCommand == NewScriptFile->CurrentCommand) {
3040 NewScriptFile->CurrentCommand = (SCRIPT_COMMAND_LIST *)GetNextNode(&NewScriptFile->CommandList, &NewScriptFile->CurrentCommand->Link);
3041 if (!IsNull(&NewScriptFile->CommandList, &NewScriptFile->CurrentCommand->Link)) {
3042 NewScriptFile->CurrentCommand->Reset = TRUE;
3043 }
3044 }
3045 } else {
3046 NewScriptFile->CurrentCommand = (SCRIPT_COMMAND_LIST *)GetNextNode(&NewScriptFile->CommandList, &NewScriptFile->CurrentCommand->Link);
3047 if (!IsNull(&NewScriptFile->CommandList, &NewScriptFile->CurrentCommand->Link)) {
3048 NewScriptFile->CurrentCommand->Reset = TRUE;
3049 }
3050 }
3051 RestoreBufferList(&OldBufferList);
3052 }
3053
3054
3055 FreePool(CommandLine);
3056 FreePool(CommandLine2);
3057 ShellCommandSetNewScript (NULL);
3058
3059 //
3060 // Only if this was the last script reset the state.
3061 //
3062 if (ShellCommandGetCurrentScriptFile()==NULL) {
3063 ShellCommandSetEchoState(PreScriptEchoState);
3064 }
3065 return (EFI_SUCCESS);
3066 }
3067
3068 /**
3069 Function to process a NSH script file.
3070
3071 @param[in] ScriptPath Pointer to the script file name (including file system path).
3072 @param[in] Handle the handle of the script file already opened.
3073 @param[in] CmdLine the command line to run.
3074 @param[in] ParamProtocol the shell parameters protocol pointer
3075
3076 @retval EFI_SUCCESS the script completed successfully
3077 **/
3078 EFI_STATUS
3079 EFIAPI
3080 RunScriptFile (
3081 IN CONST CHAR16 *ScriptPath,
3082 IN SHELL_FILE_HANDLE Handle OPTIONAL,
3083 IN CONST CHAR16 *CmdLine,
3084 IN EFI_SHELL_PARAMETERS_PROTOCOL *ParamProtocol
3085 )
3086 {
3087 EFI_STATUS Status;
3088 SHELL_FILE_HANDLE FileHandle;
3089 UINTN Argc;
3090 CHAR16 **Argv;
3091
3092 if (ShellIsFile(ScriptPath) != EFI_SUCCESS) {
3093 return (EFI_INVALID_PARAMETER);
3094 }
3095
3096 //
3097 // get the argc and argv updated for scripts
3098 //
3099 Status = UpdateArgcArgv(ParamProtocol, CmdLine, Script_File_Name, &Argv, &Argc);
3100 if (!EFI_ERROR(Status)) {
3101
3102 if (Handle == NULL) {
3103 //
3104 // open the file
3105 //
3106 Status = ShellOpenFileByName(ScriptPath, &FileHandle, EFI_FILE_MODE_READ, 0);
3107 if (!EFI_ERROR(Status)) {
3108 //
3109 // run it
3110 //
3111 Status = RunScriptFileHandle(FileHandle, ScriptPath);
3112
3113 //
3114 // now close the file
3115 //
3116 ShellCloseFile(&FileHandle);
3117 }
3118 } else {
3119 Status = RunScriptFileHandle(Handle, ScriptPath);
3120 }
3121 }
3122
3123 //
3124 // This is guaranteed to be called after UpdateArgcArgv no matter what else happened.
3125 // This is safe even if the update API failed. In this case, it may be a no-op.
3126 //
3127 RestoreArgcArgv(ParamProtocol, &Argv, &Argc);
3128
3129 return (Status);
3130 }
3131