]> git.proxmox.com Git - mirror_edk2.git/blob - ShellPkg/Application/Shell/Shell.c
ShellPkg/App: Fix memory leak and save resources.
[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 = FindFirstCharacter(CmdLine, L"|", L'^');
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 = FindFirstCharacter(TempSpot + 1, L"|", L'^');
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 CleanUpShellProtocol(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
720 if (ShellCommandGetExit()) {
721 return ((EFI_STATUS)ShellCommandGetExitCode());
722 }
723 return (Status);
724 }
725
726 /**
727 Sets all the alias' that were registered with the ShellCommandLib library.
728
729 @retval EFI_SUCCESS all init commands were run successfully.
730 **/
731 EFI_STATUS
732 EFIAPI
733 SetBuiltInAlias(
734 )
735 {
736 EFI_STATUS Status;
737 CONST ALIAS_LIST *List;
738 ALIAS_LIST *Node;
739
740 //
741 // Get all the commands we want to run
742 //
743 List = ShellCommandGetInitAliasList();
744
745 //
746 // for each command in the List
747 //
748 for ( Node = (ALIAS_LIST*)GetFirstNode(&List->Link)
749 ; !IsNull (&List->Link, &Node->Link)
750 ; Node = (ALIAS_LIST *)GetNextNode(&List->Link, &Node->Link)
751 ){
752 //
753 // install the alias'
754 //
755 Status = InternalSetAlias(Node->CommandString, Node->Alias, TRUE);
756 ASSERT_EFI_ERROR(Status);
757 }
758 return (EFI_SUCCESS);
759 }
760
761 /**
762 Internal function to determine if 2 command names are really the same.
763
764 @param[in] Command1 The pointer to the first command name.
765 @param[in] Command2 The pointer to the second command name.
766
767 @retval TRUE The 2 command names are the same.
768 @retval FALSE The 2 command names are not the same.
769 **/
770 BOOLEAN
771 EFIAPI
772 IsCommand(
773 IN CONST CHAR16 *Command1,
774 IN CONST CHAR16 *Command2
775 )
776 {
777 if (StringNoCaseCompare(&Command1, &Command2) == 0) {
778 return (TRUE);
779 }
780 return (FALSE);
781 }
782
783 /**
784 Internal function to determine if a command is a script only command.
785
786 @param[in] CommandName The pointer to the command name.
787
788 @retval TRUE The command is a script only command.
789 @retval FALSE The command is not a script only command.
790 **/
791 BOOLEAN
792 EFIAPI
793 IsScriptOnlyCommand(
794 IN CONST CHAR16 *CommandName
795 )
796 {
797 if (IsCommand(CommandName, L"for")
798 ||IsCommand(CommandName, L"endfor")
799 ||IsCommand(CommandName, L"if")
800 ||IsCommand(CommandName, L"else")
801 ||IsCommand(CommandName, L"endif")
802 ||IsCommand(CommandName, L"goto")) {
803 return (TRUE);
804 }
805 return (FALSE);
806 }
807
808 /**
809 This function will populate the 2 device path protocol parameters based on the
810 global gImageHandle. The DevPath will point to the device path for the handle that has
811 loaded image protocol installed on it. The FilePath will point to the device path
812 for the file that was loaded.
813
814 @param[in, out] DevPath On a successful return the device path to the loaded image.
815 @param[in, out] FilePath On a successful return the device path to the file.
816
817 @retval EFI_SUCCESS The 2 device paths were successfully returned.
818 @retval other A error from gBS->HandleProtocol.
819
820 @sa HandleProtocol
821 **/
822 EFI_STATUS
823 EFIAPI
824 GetDevicePathsForImageAndFile (
825 IN OUT EFI_DEVICE_PATH_PROTOCOL **DevPath,
826 IN OUT EFI_DEVICE_PATH_PROTOCOL **FilePath
827 )
828 {
829 EFI_STATUS Status;
830 EFI_LOADED_IMAGE_PROTOCOL *LoadedImage;
831 EFI_DEVICE_PATH_PROTOCOL *ImageDevicePath;
832
833 ASSERT(DevPath != NULL);
834 ASSERT(FilePath != NULL);
835
836 Status = gBS->OpenProtocol (
837 gImageHandle,
838 &gEfiLoadedImageProtocolGuid,
839 (VOID**)&LoadedImage,
840 gImageHandle,
841 NULL,
842 EFI_OPEN_PROTOCOL_GET_PROTOCOL
843 );
844 if (!EFI_ERROR (Status)) {
845 Status = gBS->OpenProtocol (
846 LoadedImage->DeviceHandle,
847 &gEfiDevicePathProtocolGuid,
848 (VOID**)&ImageDevicePath,
849 gImageHandle,
850 NULL,
851 EFI_OPEN_PROTOCOL_GET_PROTOCOL
852 );
853 if (!EFI_ERROR (Status)) {
854 *DevPath = DuplicateDevicePath (ImageDevicePath);
855 *FilePath = DuplicateDevicePath (LoadedImage->FilePath);
856 gBS->CloseProtocol(
857 LoadedImage->DeviceHandle,
858 &gEfiDevicePathProtocolGuid,
859 gImageHandle,
860 NULL);
861 }
862 gBS->CloseProtocol(
863 gImageHandle,
864 &gEfiLoadedImageProtocolGuid,
865 gImageHandle,
866 NULL);
867 }
868 return (Status);
869 }
870
871 /**
872 Process all Uefi Shell 2.0 command line options.
873
874 see Uefi Shell 2.0 section 3.2 for full details.
875
876 the command line must resemble the following:
877
878 shell.efi [ShellOpt-options] [options] [file-name [file-name-options]]
879
880 ShellOpt-options Options which control the initialization behavior of the shell.
881 These options are read from the EFI global variable "ShellOpt"
882 and are processed before options or file-name.
883
884 options Options which control the initialization behavior of the shell.
885
886 file-name The name of a UEFI shell application or script to be executed
887 after initialization is complete. By default, if file-name is
888 specified, then -nostartup is implied. Scripts are not supported
889 by level 0.
890
891 file-name-options The command-line options that are passed to file-name when it
892 is invoked.
893
894 This will initialize the ShellInfoObject.ShellInitSettings global variable.
895
896 @retval EFI_SUCCESS The variable is initialized.
897 **/
898 EFI_STATUS
899 EFIAPI
900 ProcessCommandLine(
901 VOID
902 )
903 {
904 UINTN Size;
905 UINTN LoopVar;
906 CHAR16 *CurrentArg;
907 CHAR16 *DelayValueStr;
908 UINT64 DelayValue;
909 EFI_STATUS Status;
910 EFI_UNICODE_COLLATION_PROTOCOL *UnicodeCollation;
911
912 // `file-name-options` will contain arguments to `file-name` that we don't
913 // know about. This would cause ShellCommandLineParse to error, so we parse
914 // arguments manually, ignoring those after the first thing that doesn't look
915 // like a shell option (which is assumed to be `file-name`).
916
917 Status = gBS->LocateProtocol (
918 &gEfiUnicodeCollation2ProtocolGuid,
919 NULL,
920 (VOID **) &UnicodeCollation
921 );
922 if (EFI_ERROR (Status)) {
923 Status = gBS->LocateProtocol (
924 &gEfiUnicodeCollationProtocolGuid,
925 NULL,
926 (VOID **) &UnicodeCollation
927 );
928 if (EFI_ERROR (Status)) {
929 return Status;
930 }
931 }
932
933 // Set default options
934 ShellInfoObject.ShellInitSettings.BitUnion.Bits.Startup = FALSE;
935 ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoStartup = FALSE;
936 ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoConsoleOut = FALSE;
937 ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoConsoleIn = FALSE;
938 ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoInterrupt = FALSE;
939 ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoMap = FALSE;
940 ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoVersion = FALSE;
941 ShellInfoObject.ShellInitSettings.BitUnion.Bits.Delay = FALSE;
942 ShellInfoObject.ShellInitSettings.BitUnion.Bits.Exit = FALSE;
943 ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoNest = FALSE;
944 ShellInfoObject.ShellInitSettings.Delay = 5;
945
946 //
947 // Start LoopVar at 0 to parse only optional arguments at Argv[0]
948 // and parse other parameters from Argv[1]. This is for use case that
949 // UEFI Shell boot option is created, and OptionalData is provided
950 // that starts with shell command-line options.
951 //
952 for (LoopVar = 0 ; LoopVar < gEfiShellParametersProtocol->Argc ; LoopVar++) {
953 CurrentArg = gEfiShellParametersProtocol->Argv[LoopVar];
954 if (UnicodeCollation->StriColl (
955 UnicodeCollation,
956 L"-startup",
957 CurrentArg
958 ) == 0) {
959 ShellInfoObject.ShellInitSettings.BitUnion.Bits.Startup = TRUE;
960 }
961 else if (UnicodeCollation->StriColl (
962 UnicodeCollation,
963 L"-nostartup",
964 CurrentArg
965 ) == 0) {
966 ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoStartup = TRUE;
967 }
968 else if (UnicodeCollation->StriColl (
969 UnicodeCollation,
970 L"-noconsoleout",
971 CurrentArg
972 ) == 0) {
973 ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoConsoleOut = TRUE;
974 }
975 else if (UnicodeCollation->StriColl (
976 UnicodeCollation,
977 L"-noconsolein",
978 CurrentArg
979 ) == 0) {
980 ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoConsoleIn = TRUE;
981 }
982 else if (UnicodeCollation->StriColl (
983 UnicodeCollation,
984 L"-nointerrupt",
985 CurrentArg
986 ) == 0) {
987 ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoInterrupt = TRUE;
988 }
989 else if (UnicodeCollation->StriColl (
990 UnicodeCollation,
991 L"-nomap",
992 CurrentArg
993 ) == 0) {
994 ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoMap = TRUE;
995 }
996 else if (UnicodeCollation->StriColl (
997 UnicodeCollation,
998 L"-noversion",
999 CurrentArg
1000 ) == 0) {
1001 ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoVersion = TRUE;
1002 }
1003 else if (UnicodeCollation->StriColl (
1004 UnicodeCollation,
1005 L"-nonest",
1006 CurrentArg
1007 ) == 0) {
1008 ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoNest = TRUE;
1009 }
1010 else if (UnicodeCollation->StriColl (
1011 UnicodeCollation,
1012 L"-delay",
1013 CurrentArg
1014 ) == 0) {
1015 ShellInfoObject.ShellInitSettings.BitUnion.Bits.Delay = TRUE;
1016 // Check for optional delay value following "-delay"
1017 DelayValueStr = gEfiShellParametersProtocol->Argv[LoopVar + 1];
1018 if (DelayValueStr != NULL){
1019 if (*DelayValueStr == L':') {
1020 DelayValueStr++;
1021 }
1022 if (!EFI_ERROR(ShellConvertStringToUint64 (
1023 DelayValueStr,
1024 &DelayValue,
1025 FALSE,
1026 FALSE
1027 ))) {
1028 ShellInfoObject.ShellInitSettings.Delay = (UINTN)DelayValue;
1029 LoopVar++;
1030 }
1031 }
1032 } else if (UnicodeCollation->StriColl (
1033 UnicodeCollation,
1034 L"-_exit",
1035 CurrentArg
1036 ) == 0) {
1037 ShellInfoObject.ShellInitSettings.BitUnion.Bits.Exit = TRUE;
1038 } else if (StrnCmp (L"-", CurrentArg, 1) == 0) {
1039 // Unrecognized option
1040 ShellPrintHiiEx(-1, -1, NULL,
1041 STRING_TOKEN (STR_GEN_PROBLEM),
1042 ShellInfoObject.HiiHandle,
1043 CurrentArg
1044 );
1045 return EFI_INVALID_PARAMETER;
1046 } else {
1047 //
1048 // First argument should be Shell.efi image name
1049 //
1050 if (LoopVar == 0) {
1051 continue;
1052 }
1053
1054 ShellInfoObject.ShellInitSettings.FileName = AllocateCopyPool(StrSize(CurrentArg), CurrentArg);
1055 if (ShellInfoObject.ShellInitSettings.FileName == NULL) {
1056 return (EFI_OUT_OF_RESOURCES);
1057 }
1058 //
1059 // We found `file-name`.
1060 //
1061 ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoStartup = 1;
1062 LoopVar++;
1063
1064 // Add `file-name-options`
1065 for (Size = 0 ; LoopVar < gEfiShellParametersProtocol->Argc ; LoopVar++) {
1066 ASSERT((ShellInfoObject.ShellInitSettings.FileOptions == NULL && Size == 0) || (ShellInfoObject.ShellInitSettings.FileOptions != NULL));
1067 StrnCatGrow(&ShellInfoObject.ShellInitSettings.FileOptions,
1068 &Size,
1069 L" ",
1070 0);
1071 if (ShellInfoObject.ShellInitSettings.FileOptions == NULL) {
1072 SHELL_FREE_NON_NULL(ShellInfoObject.ShellInitSettings.FileName);
1073 return (EFI_OUT_OF_RESOURCES);
1074 }
1075 StrnCatGrow(&ShellInfoObject.ShellInitSettings.FileOptions,
1076 &Size,
1077 gEfiShellParametersProtocol->Argv[LoopVar],
1078 0);
1079 if (ShellInfoObject.ShellInitSettings.FileOptions == NULL) {
1080 SHELL_FREE_NON_NULL(ShellInfoObject.ShellInitSettings.FileName);
1081 return (EFI_OUT_OF_RESOURCES);
1082 }
1083 }
1084 }
1085 }
1086
1087 // "-nointerrupt" overrides "-delay"
1088 if (ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoInterrupt) {
1089 ShellInfoObject.ShellInitSettings.Delay = 0;
1090 }
1091
1092 return EFI_SUCCESS;
1093 }
1094
1095 /**
1096 Handles all interaction with the default startup script.
1097
1098 this will check that the correct command line parameters were passed, handle the delay, and then start running the script.
1099
1100 @param ImagePath the path to the image for shell. first place to look for the startup script
1101 @param FilePath the path to the file for shell. second place to look for the startup script.
1102
1103 @retval EFI_SUCCESS the variable is initialized.
1104 **/
1105 EFI_STATUS
1106 EFIAPI
1107 DoStartupScript(
1108 IN EFI_DEVICE_PATH_PROTOCOL *ImagePath,
1109 IN EFI_DEVICE_PATH_PROTOCOL *FilePath
1110 )
1111 {
1112 EFI_STATUS Status;
1113 EFI_STATUS CalleeStatus;
1114 UINTN Delay;
1115 EFI_INPUT_KEY Key;
1116 SHELL_FILE_HANDLE FileHandle;
1117 EFI_DEVICE_PATH_PROTOCOL *NewPath;
1118 EFI_DEVICE_PATH_PROTOCOL *NamePath;
1119 CHAR16 *FileStringPath;
1120 CHAR16 *TempSpot;
1121 UINTN NewSize;
1122 CONST CHAR16 *MapName;
1123
1124 Key.UnicodeChar = CHAR_NULL;
1125 Key.ScanCode = 0;
1126 FileHandle = NULL;
1127
1128 if (!ShellInfoObject.ShellInitSettings.BitUnion.Bits.Startup && ShellInfoObject.ShellInitSettings.FileName != NULL) {
1129 //
1130 // launch something else instead
1131 //
1132 NewSize = StrSize(ShellInfoObject.ShellInitSettings.FileName);
1133 if (ShellInfoObject.ShellInitSettings.FileOptions != NULL) {
1134 NewSize += StrSize(ShellInfoObject.ShellInitSettings.FileOptions) + sizeof(CHAR16);
1135 }
1136 FileStringPath = AllocateZeroPool(NewSize);
1137 if (FileStringPath == NULL) {
1138 return (EFI_OUT_OF_RESOURCES);
1139 }
1140 StrCpyS(FileStringPath, NewSize/sizeof(CHAR16), ShellInfoObject.ShellInitSettings.FileName);
1141 if (ShellInfoObject.ShellInitSettings.FileOptions != NULL) {
1142 StrnCatS(FileStringPath, NewSize/sizeof(CHAR16), L" ", NewSize/sizeof(CHAR16) - StrLen(FileStringPath) -1);
1143 StrnCatS(FileStringPath, NewSize/sizeof(CHAR16), ShellInfoObject.ShellInitSettings.FileOptions, NewSize/sizeof(CHAR16) - StrLen(FileStringPath) -1);
1144 }
1145 Status = RunShellCommand(FileStringPath, &CalleeStatus);
1146 if (ShellInfoObject.ShellInitSettings.BitUnion.Bits.Exit == TRUE) {
1147 ShellCommandRegisterExit(gEfiShellProtocol->BatchIsActive(), (UINT64)CalleeStatus);
1148 }
1149 FreePool(FileStringPath);
1150 return (Status);
1151
1152 }
1153
1154 //
1155 // for shell level 0 we do no scripts
1156 // Without the Startup bit overriding we allow for nostartup to prevent scripts
1157 //
1158 if ( (PcdGet8(PcdShellSupportLevel) < 1)
1159 || (ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoStartup && !ShellInfoObject.ShellInitSettings.BitUnion.Bits.Startup)
1160 ){
1161 return (EFI_SUCCESS);
1162 }
1163
1164 gST->ConOut->EnableCursor(gST->ConOut, FALSE);
1165 //
1166 // print out our warning and see if they press a key
1167 //
1168 for ( Status = EFI_UNSUPPORTED, Delay = ShellInfoObject.ShellInitSettings.Delay
1169 ; Delay != 0 && EFI_ERROR(Status)
1170 ; Delay--
1171 ){
1172 ShellPrintHiiEx(0, gST->ConOut->Mode->CursorRow, NULL, STRING_TOKEN (STR_SHELL_STARTUP_QUESTION), ShellInfoObject.HiiHandle, Delay);
1173 gBS->Stall (1000000);
1174 if (!ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoConsoleIn) {
1175 Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);
1176 }
1177 }
1178 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SHELL_CRLF), ShellInfoObject.HiiHandle);
1179 gST->ConOut->EnableCursor(gST->ConOut, TRUE);
1180
1181 //
1182 // ESC was pressed
1183 //
1184 if (Status == EFI_SUCCESS && Key.UnicodeChar == 0 && Key.ScanCode == SCAN_ESC) {
1185 return (EFI_SUCCESS);
1186 }
1187
1188 //
1189 // Try the first location (must be file system)
1190 //
1191 MapName = ShellInfoObject.NewEfiShellProtocol->GetMapFromDevicePath(&ImagePath);
1192 if (MapName != NULL) {
1193 FileStringPath = NULL;
1194 NewSize = 0;
1195 FileStringPath = StrnCatGrow(&FileStringPath, &NewSize, MapName, 0);
1196 if (FileStringPath == NULL) {
1197 Status = EFI_OUT_OF_RESOURCES;
1198 } else {
1199 TempSpot = StrStr(FileStringPath, L";");
1200 if (TempSpot != NULL) {
1201 *TempSpot = CHAR_NULL;
1202 }
1203 FileStringPath = StrnCatGrow(&FileStringPath, &NewSize, ((FILEPATH_DEVICE_PATH*)FilePath)->PathName, 0);
1204 PathRemoveLastItem(FileStringPath);
1205 FileStringPath = StrnCatGrow(&FileStringPath, &NewSize, mStartupScript, 0);
1206 Status = ShellInfoObject.NewEfiShellProtocol->OpenFileByName(FileStringPath, &FileHandle, EFI_FILE_MODE_READ);
1207 FreePool(FileStringPath);
1208 }
1209 }
1210 if (EFI_ERROR(Status)) {
1211 NamePath = FileDevicePath (NULL, mStartupScript);
1212 NewPath = AppendDevicePathNode (ImagePath, NamePath);
1213 FreePool(NamePath);
1214
1215 //
1216 // Try the location
1217 //
1218 Status = InternalOpenFileDevicePath(NewPath, &FileHandle, EFI_FILE_MODE_READ, 0);
1219 FreePool(NewPath);
1220 }
1221 //
1222 // If we got a file, run it
1223 //
1224 if (!EFI_ERROR(Status) && FileHandle != NULL) {
1225 Status = RunScriptFile (mStartupScript, FileHandle, L"", ShellInfoObject.NewShellParametersProtocol);
1226 ShellInfoObject.NewEfiShellProtocol->CloseFile(FileHandle);
1227 } else {
1228 FileStringPath = ShellFindFilePath(mStartupScript);
1229 if (FileStringPath == NULL) {
1230 //
1231 // we return success since we don't need to have a startup script
1232 //
1233 Status = EFI_SUCCESS;
1234 ASSERT(FileHandle == NULL);
1235 } else {
1236 Status = RunScriptFile(FileStringPath, NULL, L"", ShellInfoObject.NewShellParametersProtocol);
1237 FreePool(FileStringPath);
1238 }
1239 }
1240
1241
1242 return (Status);
1243 }
1244
1245 /**
1246 Function to perform the shell prompt looping. It will do a single prompt,
1247 dispatch the result, and then return. It is expected that the caller will
1248 call this function in a loop many times.
1249
1250 @retval EFI_SUCCESS
1251 @retval RETURN_ABORTED
1252 **/
1253 EFI_STATUS
1254 EFIAPI
1255 DoShellPrompt (
1256 VOID
1257 )
1258 {
1259 UINTN Column;
1260 UINTN Row;
1261 CHAR16 *CmdLine;
1262 CONST CHAR16 *CurDir;
1263 UINTN BufferSize;
1264 EFI_STATUS Status;
1265 LIST_ENTRY OldBufferList;
1266
1267 CurDir = NULL;
1268
1269 //
1270 // Get screen setting to decide size of the command line buffer
1271 //
1272 gST->ConOut->QueryMode (gST->ConOut, gST->ConOut->Mode->Mode, &Column, &Row);
1273 BufferSize = Column * Row * sizeof (CHAR16);
1274 CmdLine = AllocateZeroPool (BufferSize);
1275 if (CmdLine == NULL) {
1276 return EFI_OUT_OF_RESOURCES;
1277 }
1278
1279 SaveBufferList(&OldBufferList);
1280 CurDir = ShellInfoObject.NewEfiShellProtocol->GetEnv(L"cwd");
1281
1282 //
1283 // Prompt for input
1284 //
1285 gST->ConOut->SetCursorPosition (gST->ConOut, 0, gST->ConOut->Mode->CursorRow);
1286
1287 if (CurDir != NULL && StrLen(CurDir) > 1) {
1288 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SHELL_CURDIR), ShellInfoObject.HiiHandle, CurDir);
1289 } else {
1290 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SHELL_SHELL), ShellInfoObject.HiiHandle);
1291 }
1292
1293 //
1294 // Read a line from the console
1295 //
1296 Status = ShellInfoObject.NewEfiShellProtocol->ReadFile(ShellInfoObject.NewShellParametersProtocol->StdIn, &BufferSize, CmdLine);
1297
1298 //
1299 // Null terminate the string and parse it
1300 //
1301 if (!EFI_ERROR (Status)) {
1302 CmdLine[BufferSize / sizeof (CHAR16)] = CHAR_NULL;
1303 Status = RunCommand(CmdLine);
1304 }
1305
1306 //
1307 // Done with this command
1308 //
1309 RestoreBufferList(&OldBufferList);
1310 FreePool (CmdLine);
1311 return Status;
1312 }
1313
1314 /**
1315 Add a buffer to the Buffer To Free List for safely returning buffers to other
1316 places without risking letting them modify internal shell information.
1317
1318 @param Buffer Something to pass to FreePool when the shell is exiting.
1319 **/
1320 VOID*
1321 EFIAPI
1322 AddBufferToFreeList(
1323 VOID *Buffer
1324 )
1325 {
1326 BUFFER_LIST *BufferListEntry;
1327
1328 if (Buffer == NULL) {
1329 return (NULL);
1330 }
1331
1332 BufferListEntry = AllocateZeroPool(sizeof(BUFFER_LIST));
1333 ASSERT(BufferListEntry != NULL);
1334 BufferListEntry->Buffer = Buffer;
1335 InsertTailList(&ShellInfoObject.BufferToFreeList.Link, &BufferListEntry->Link);
1336 return (Buffer);
1337 }
1338
1339
1340 /**
1341 Create a new buffer list and stores the old one to OldBufferList
1342
1343 @param OldBufferList The temporary list head used to store the nodes in BufferToFreeList.
1344 **/
1345 VOID
1346 SaveBufferList (
1347 OUT LIST_ENTRY *OldBufferList
1348 )
1349 {
1350 CopyMem (OldBufferList, &ShellInfoObject.BufferToFreeList.Link, sizeof (LIST_ENTRY));
1351 InitializeListHead (&ShellInfoObject.BufferToFreeList.Link);
1352 }
1353
1354 /**
1355 Restore previous nodes into BufferToFreeList .
1356
1357 @param OldBufferList The temporary list head used to store the nodes in BufferToFreeList.
1358 **/
1359 VOID
1360 RestoreBufferList (
1361 IN OUT LIST_ENTRY *OldBufferList
1362 )
1363 {
1364 FreeBufferList (&ShellInfoObject.BufferToFreeList);
1365 CopyMem (&ShellInfoObject.BufferToFreeList.Link, OldBufferList, sizeof (LIST_ENTRY));
1366 }
1367
1368
1369 /**
1370 Add a buffer to the Line History List
1371
1372 @param Buffer The line buffer to add.
1373 **/
1374 VOID
1375 EFIAPI
1376 AddLineToCommandHistory(
1377 IN CONST CHAR16 *Buffer
1378 )
1379 {
1380 BUFFER_LIST *Node;
1381 BUFFER_LIST *Walker;
1382 UINT16 MaxHistoryCmdCount;
1383 UINT16 Count;
1384
1385 Count = 0;
1386 MaxHistoryCmdCount = PcdGet16(PcdShellMaxHistoryCommandCount);
1387
1388 if (MaxHistoryCmdCount == 0) {
1389 return ;
1390 }
1391
1392
1393 Node = AllocateZeroPool(sizeof(BUFFER_LIST));
1394 ASSERT(Node != NULL);
1395 Node->Buffer = AllocateCopyPool(StrSize(Buffer), Buffer);
1396 ASSERT(Node->Buffer != NULL);
1397
1398 for ( Walker = (BUFFER_LIST*)GetFirstNode(&ShellInfoObject.ViewingSettings.CommandHistory.Link)
1399 ; !IsNull(&ShellInfoObject.ViewingSettings.CommandHistory.Link, &Walker->Link)
1400 ; Walker = (BUFFER_LIST*)GetNextNode(&ShellInfoObject.ViewingSettings.CommandHistory.Link, &Walker->Link)
1401 ){
1402 Count++;
1403 }
1404 if (Count < MaxHistoryCmdCount){
1405 InsertTailList(&ShellInfoObject.ViewingSettings.CommandHistory.Link, &Node->Link);
1406 } else {
1407 Walker = (BUFFER_LIST*)GetFirstNode(&ShellInfoObject.ViewingSettings.CommandHistory.Link);
1408 RemoveEntryList(&Walker->Link);
1409 if (Walker->Buffer != NULL) {
1410 FreePool(Walker->Buffer);
1411 }
1412 FreePool(Walker);
1413 InsertTailList(&ShellInfoObject.ViewingSettings.CommandHistory.Link, &Node->Link);
1414 }
1415 }
1416
1417 /**
1418 Checks if a string is an alias for another command. If yes, then it replaces the alias name
1419 with the correct command name.
1420
1421 @param[in, out] CommandString Upon entry the potential alias. Upon return the
1422 command name if it was an alias. If it was not
1423 an alias it will be unchanged. This function may
1424 change the buffer to fit the command name.
1425
1426 @retval EFI_SUCCESS The name was changed.
1427 @retval EFI_SUCCESS The name was not an alias.
1428 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
1429 **/
1430 EFI_STATUS
1431 EFIAPI
1432 ShellConvertAlias(
1433 IN OUT CHAR16 **CommandString
1434 )
1435 {
1436 CONST CHAR16 *NewString;
1437
1438 NewString = ShellInfoObject.NewEfiShellProtocol->GetAlias(*CommandString, NULL);
1439 if (NewString == NULL) {
1440 return (EFI_SUCCESS);
1441 }
1442 FreePool(*CommandString);
1443 *CommandString = AllocateCopyPool(StrSize(NewString), NewString);
1444 if (*CommandString == NULL) {
1445 return (EFI_OUT_OF_RESOURCES);
1446 }
1447 return (EFI_SUCCESS);
1448 }
1449
1450 /**
1451 This function will eliminate unreplaced (and therefore non-found) environment variables.
1452
1453 @param[in,out] CmdLine The command line to update.
1454 **/
1455 EFI_STATUS
1456 EFIAPI
1457 StripUnreplacedEnvironmentVariables(
1458 IN OUT CHAR16 *CmdLine
1459 )
1460 {
1461 CHAR16 *FirstPercent;
1462 CHAR16 *FirstQuote;
1463 CHAR16 *SecondPercent;
1464 CHAR16 *SecondQuote;
1465 CHAR16 *CurrentLocator;
1466
1467 for (CurrentLocator = CmdLine ; CurrentLocator != NULL ; ) {
1468 FirstQuote = FindNextInstance(CurrentLocator, L"\"", TRUE);
1469 FirstPercent = FindNextInstance(CurrentLocator, L"%", TRUE);
1470 SecondPercent = FirstPercent!=NULL?FindNextInstance(FirstPercent+1, L"%", TRUE):NULL;
1471 if (FirstPercent == NULL || SecondPercent == NULL) {
1472 //
1473 // If we ever don't have 2 % we are done.
1474 //
1475 break;
1476 }
1477
1478 if (FirstQuote!= NULL && FirstQuote < FirstPercent) {
1479 SecondQuote = FindNextInstance(FirstQuote+1, L"\"", TRUE);
1480 //
1481 // Quote is first found
1482 //
1483
1484 if (SecondQuote < FirstPercent) {
1485 //
1486 // restart after the pair of "
1487 //
1488 CurrentLocator = SecondQuote + 1;
1489 } else /* FirstPercent < SecondQuote */{
1490 //
1491 // Restart on the first percent
1492 //
1493 CurrentLocator = FirstPercent;
1494 }
1495 continue;
1496 }
1497
1498 if (FirstQuote == NULL || SecondPercent < FirstQuote) {
1499 if (IsValidEnvironmentVariableName(FirstPercent, SecondPercent)) {
1500 //
1501 // We need to remove from FirstPercent to SecondPercent
1502 //
1503 CopyMem(FirstPercent, SecondPercent + 1, StrSize(SecondPercent + 1));
1504 //
1505 // don't need to update the locator. both % characters are gone.
1506 //
1507 } else {
1508 CurrentLocator = SecondPercent + 1;
1509 }
1510 continue;
1511 }
1512 CurrentLocator = FirstQuote;
1513 }
1514 return (EFI_SUCCESS);
1515 }
1516
1517 /**
1518 Function allocates a new command line and replaces all instances of environment
1519 variable names that are correctly preset to their values.
1520
1521 If the return value is not NULL the memory must be caller freed.
1522
1523 @param[in] OriginalCommandLine The original command line
1524
1525 @retval NULL An error occurred.
1526 @return The new command line with no environment variables present.
1527 **/
1528 CHAR16*
1529 EFIAPI
1530 ShellConvertVariables (
1531 IN CONST CHAR16 *OriginalCommandLine
1532 )
1533 {
1534 CONST CHAR16 *MasterEnvList;
1535 UINTN NewSize;
1536 CHAR16 *NewCommandLine1;
1537 CHAR16 *NewCommandLine2;
1538 CHAR16 *Temp;
1539 UINTN ItemSize;
1540 CHAR16 *ItemTemp;
1541 SCRIPT_FILE *CurrentScriptFile;
1542 ALIAS_LIST *AliasListNode;
1543
1544 ASSERT(OriginalCommandLine != NULL);
1545
1546 ItemSize = 0;
1547 NewSize = StrSize(OriginalCommandLine);
1548 CurrentScriptFile = ShellCommandGetCurrentScriptFile();
1549 Temp = NULL;
1550
1551 ///@todo update this to handle the %0 - %9 for scripting only (borrow from line 1256 area) ? ? ?
1552
1553 //
1554 // calculate the size required for the post-conversion string...
1555 //
1556 if (CurrentScriptFile != NULL) {
1557 for (AliasListNode = (ALIAS_LIST*)GetFirstNode(&CurrentScriptFile->SubstList)
1558 ; !IsNull(&CurrentScriptFile->SubstList, &AliasListNode->Link)
1559 ; AliasListNode = (ALIAS_LIST*)GetNextNode(&CurrentScriptFile->SubstList, &AliasListNode->Link)
1560 ){
1561 for (Temp = StrStr(OriginalCommandLine, AliasListNode->Alias)
1562 ; Temp != NULL
1563 ; Temp = StrStr(Temp+1, AliasListNode->Alias)
1564 ){
1565 //
1566 // we need a preceding and if there is space no ^ preceding (if no space ignore)
1567 //
1568 if ((((Temp-OriginalCommandLine)>2) && *(Temp-2) != L'^') || ((Temp-OriginalCommandLine)<=2)) {
1569 NewSize += StrSize(AliasListNode->CommandString);
1570 }
1571 }
1572 }
1573 }
1574
1575 for (MasterEnvList = EfiShellGetEnv(NULL)
1576 ; MasterEnvList != NULL && *MasterEnvList != CHAR_NULL //&& *(MasterEnvList+1) != CHAR_NULL
1577 ; MasterEnvList += StrLen(MasterEnvList) + 1
1578 ){
1579 if (StrSize(MasterEnvList) > ItemSize) {
1580 ItemSize = StrSize(MasterEnvList);
1581 }
1582 for (Temp = StrStr(OriginalCommandLine, MasterEnvList)
1583 ; Temp != NULL
1584 ; Temp = StrStr(Temp+1, MasterEnvList)
1585 ){
1586 //
1587 // we need a preceding and following % and if there is space no ^ preceding (if no space ignore)
1588 //
1589 if (*(Temp-1) == L'%' && *(Temp+StrLen(MasterEnvList)) == L'%' &&
1590 ((((Temp-OriginalCommandLine)>2) && *(Temp-2) != L'^') || ((Temp-OriginalCommandLine)<=2))) {
1591 NewSize+=StrSize(EfiShellGetEnv(MasterEnvList));
1592 }
1593 }
1594 }
1595
1596 //
1597 // now do the replacements...
1598 //
1599 NewCommandLine1 = AllocateCopyPool(NewSize, OriginalCommandLine);
1600 NewCommandLine2 = AllocateZeroPool(NewSize);
1601 ItemTemp = AllocateZeroPool(ItemSize+(2*sizeof(CHAR16)));
1602 if (NewCommandLine1 == NULL || NewCommandLine2 == NULL || ItemTemp == NULL) {
1603 SHELL_FREE_NON_NULL(NewCommandLine1);
1604 SHELL_FREE_NON_NULL(NewCommandLine2);
1605 SHELL_FREE_NON_NULL(ItemTemp);
1606 return (NULL);
1607 }
1608 for (MasterEnvList = EfiShellGetEnv(NULL)
1609 ; MasterEnvList != NULL && *MasterEnvList != CHAR_NULL
1610 ; MasterEnvList += StrLen(MasterEnvList) + 1
1611 ){
1612 StrCpyS( ItemTemp,
1613 ((ItemSize+(2*sizeof(CHAR16)))/sizeof(CHAR16)),
1614 L"%"
1615 );
1616 StrCatS( ItemTemp,
1617 ((ItemSize+(2*sizeof(CHAR16)))/sizeof(CHAR16)),
1618 MasterEnvList
1619 );
1620 StrCatS( ItemTemp,
1621 ((ItemSize+(2*sizeof(CHAR16)))/sizeof(CHAR16)),
1622 L"%"
1623 );
1624 ShellCopySearchAndReplace(NewCommandLine1, NewCommandLine2, NewSize, ItemTemp, EfiShellGetEnv(MasterEnvList), TRUE, FALSE);
1625 StrCpyS(NewCommandLine1, NewSize/sizeof(CHAR16), NewCommandLine2);
1626 }
1627 if (CurrentScriptFile != NULL) {
1628 for (AliasListNode = (ALIAS_LIST*)GetFirstNode(&CurrentScriptFile->SubstList)
1629 ; !IsNull(&CurrentScriptFile->SubstList, &AliasListNode->Link)
1630 ; AliasListNode = (ALIAS_LIST*)GetNextNode(&CurrentScriptFile->SubstList, &AliasListNode->Link)
1631 ){
1632 ShellCopySearchAndReplace(NewCommandLine1, NewCommandLine2, NewSize, AliasListNode->Alias, AliasListNode->CommandString, TRUE, FALSE);
1633 StrCpyS(NewCommandLine1, NewSize/sizeof(CHAR16), NewCommandLine2);
1634 }
1635 }
1636
1637 //
1638 // Remove non-existent environment variables
1639 //
1640 StripUnreplacedEnvironmentVariables(NewCommandLine1);
1641
1642 //
1643 // Now cleanup any straggler intentionally ignored "%" characters
1644 //
1645 ShellCopySearchAndReplace(NewCommandLine1, NewCommandLine2, NewSize, L"^%", L"%", TRUE, FALSE);
1646 StrCpyS(NewCommandLine1, NewSize/sizeof(CHAR16), NewCommandLine2);
1647
1648 FreePool(NewCommandLine2);
1649 FreePool(ItemTemp);
1650
1651 return (NewCommandLine1);
1652 }
1653
1654 /**
1655 Internal function to run a command line with pipe usage.
1656
1657 @param[in] CmdLine The pointer to the command line.
1658 @param[in] StdIn The pointer to the Standard input.
1659 @param[in] StdOut The pointer to the Standard output.
1660
1661 @retval EFI_SUCCESS The split command is executed successfully.
1662 @retval other Some error occurs when executing the split command.
1663 **/
1664 EFI_STATUS
1665 EFIAPI
1666 RunSplitCommand(
1667 IN CONST CHAR16 *CmdLine,
1668 IN SHELL_FILE_HANDLE *StdIn,
1669 IN SHELL_FILE_HANDLE *StdOut
1670 )
1671 {
1672 EFI_STATUS Status;
1673 CHAR16 *NextCommandLine;
1674 CHAR16 *OurCommandLine;
1675 UINTN Size1;
1676 UINTN Size2;
1677 SPLIT_LIST *Split;
1678 SHELL_FILE_HANDLE *TempFileHandle;
1679 BOOLEAN Unicode;
1680
1681 ASSERT(StdOut == NULL);
1682
1683 ASSERT(StrStr(CmdLine, L"|") != NULL);
1684
1685 Status = EFI_SUCCESS;
1686 NextCommandLine = NULL;
1687 OurCommandLine = NULL;
1688 Size1 = 0;
1689 Size2 = 0;
1690
1691 NextCommandLine = StrnCatGrow(&NextCommandLine, &Size1, StrStr(CmdLine, L"|")+1, 0);
1692 OurCommandLine = StrnCatGrow(&OurCommandLine , &Size2, CmdLine , StrStr(CmdLine, L"|") - CmdLine);
1693
1694 if (NextCommandLine == NULL || OurCommandLine == NULL) {
1695 SHELL_FREE_NON_NULL(OurCommandLine);
1696 SHELL_FREE_NON_NULL(NextCommandLine);
1697 return (EFI_OUT_OF_RESOURCES);
1698 } else if (StrStr(OurCommandLine, L"|") != NULL || Size1 == 0 || Size2 == 0) {
1699 SHELL_FREE_NON_NULL(OurCommandLine);
1700 SHELL_FREE_NON_NULL(NextCommandLine);
1701 return (EFI_INVALID_PARAMETER);
1702 } else if (NextCommandLine[0] == L'a' &&
1703 (NextCommandLine[1] == L' ' || NextCommandLine[1] == CHAR_NULL)
1704 ){
1705 CopyMem(NextCommandLine, NextCommandLine+1, StrSize(NextCommandLine) - sizeof(NextCommandLine[0]));
1706 while (NextCommandLine[0] == L' ') {
1707 CopyMem(NextCommandLine, NextCommandLine+1, StrSize(NextCommandLine) - sizeof(NextCommandLine[0]));
1708 }
1709 if (NextCommandLine[0] == CHAR_NULL) {
1710 SHELL_FREE_NON_NULL(OurCommandLine);
1711 SHELL_FREE_NON_NULL(NextCommandLine);
1712 return (EFI_INVALID_PARAMETER);
1713 }
1714 Unicode = FALSE;
1715 } else {
1716 Unicode = TRUE;
1717 }
1718
1719
1720 //
1721 // make a SPLIT_LIST item and add to list
1722 //
1723 Split = AllocateZeroPool(sizeof(SPLIT_LIST));
1724 ASSERT(Split != NULL);
1725 Split->SplitStdIn = StdIn;
1726 Split->SplitStdOut = ConvertEfiFileProtocolToShellHandle(CreateFileInterfaceMem(Unicode), NULL);
1727 ASSERT(Split->SplitStdOut != NULL);
1728 InsertHeadList(&ShellInfoObject.SplitList.Link, &Split->Link);
1729
1730 Status = RunCommand(OurCommandLine);
1731
1732 //
1733 // move the output from the first to the in to the second.
1734 //
1735 TempFileHandle = Split->SplitStdOut;
1736 if (Split->SplitStdIn == StdIn) {
1737 Split->SplitStdOut = NULL;
1738 } else {
1739 Split->SplitStdOut = Split->SplitStdIn;
1740 }
1741 Split->SplitStdIn = TempFileHandle;
1742 ShellInfoObject.NewEfiShellProtocol->SetFilePosition(ConvertShellHandleToEfiFileProtocol(Split->SplitStdIn), 0);
1743
1744 if (!EFI_ERROR(Status)) {
1745 Status = RunCommand(NextCommandLine);
1746 }
1747
1748 //
1749 // remove the top level from the ScriptList
1750 //
1751 ASSERT((SPLIT_LIST*)GetFirstNode(&ShellInfoObject.SplitList.Link) == Split);
1752 RemoveEntryList(&Split->Link);
1753
1754 //
1755 // Note that the original StdIn is now the StdOut...
1756 //
1757 if (Split->SplitStdOut != NULL) {
1758 ShellInfoObject.NewEfiShellProtocol->CloseFile(ConvertShellHandleToEfiFileProtocol(Split->SplitStdOut));
1759 }
1760 if (Split->SplitStdIn != NULL) {
1761 ShellInfoObject.NewEfiShellProtocol->CloseFile(ConvertShellHandleToEfiFileProtocol(Split->SplitStdIn));
1762 FreePool (Split->SplitStdIn);
1763 }
1764
1765 FreePool(Split);
1766 FreePool(NextCommandLine);
1767 FreePool(OurCommandLine);
1768
1769 return (Status);
1770 }
1771
1772 /**
1773 Take the original command line, substitute any variables, free
1774 the original string, return the modified copy.
1775
1776 @param[in] CmdLine pointer to the command line to update.
1777
1778 @retval EFI_SUCCESS the function was successful.
1779 @retval EFI_OUT_OF_RESOURCES a memory allocation failed.
1780 **/
1781 EFI_STATUS
1782 EFIAPI
1783 ShellSubstituteVariables(
1784 IN CHAR16 **CmdLine
1785 )
1786 {
1787 CHAR16 *NewCmdLine;
1788 NewCmdLine = ShellConvertVariables(*CmdLine);
1789 SHELL_FREE_NON_NULL(*CmdLine);
1790 if (NewCmdLine == NULL) {
1791 return (EFI_OUT_OF_RESOURCES);
1792 }
1793 *CmdLine = NewCmdLine;
1794 return (EFI_SUCCESS);
1795 }
1796
1797 /**
1798 Take the original command line, substitute any alias in the first group of space delimited characters, free
1799 the original string, return the modified copy.
1800
1801 @param[in] CmdLine pointer to the command line to update.
1802
1803 @retval EFI_SUCCESS the function was successful.
1804 @retval EFI_OUT_OF_RESOURCES a memory allocation failed.
1805 **/
1806 EFI_STATUS
1807 EFIAPI
1808 ShellSubstituteAliases(
1809 IN CHAR16 **CmdLine
1810 )
1811 {
1812 CHAR16 *NewCmdLine;
1813 CHAR16 *CommandName;
1814 EFI_STATUS Status;
1815 UINTN PostAliasSize;
1816 ASSERT(CmdLine != NULL);
1817 ASSERT(*CmdLine!= NULL);
1818
1819
1820 CommandName = NULL;
1821 if (StrStr((*CmdLine), L" ") == NULL){
1822 StrnCatGrow(&CommandName, NULL, (*CmdLine), 0);
1823 } else {
1824 StrnCatGrow(&CommandName, NULL, (*CmdLine), StrStr((*CmdLine), L" ") - (*CmdLine));
1825 }
1826
1827 //
1828 // This cannot happen 'inline' since the CmdLine can need extra space.
1829 //
1830 NewCmdLine = NULL;
1831 if (!ShellCommandIsCommandOnList(CommandName)) {
1832 //
1833 // Convert via alias
1834 //
1835 Status = ShellConvertAlias(&CommandName);
1836 if (EFI_ERROR(Status)){
1837 return (Status);
1838 }
1839 PostAliasSize = 0;
1840 NewCmdLine = StrnCatGrow(&NewCmdLine, &PostAliasSize, CommandName, 0);
1841 if (NewCmdLine == NULL) {
1842 SHELL_FREE_NON_NULL(CommandName);
1843 SHELL_FREE_NON_NULL(*CmdLine);
1844 return (EFI_OUT_OF_RESOURCES);
1845 }
1846 NewCmdLine = StrnCatGrow(&NewCmdLine, &PostAliasSize, StrStr((*CmdLine), L" "), 0);
1847 if (NewCmdLine == NULL) {
1848 SHELL_FREE_NON_NULL(CommandName);
1849 SHELL_FREE_NON_NULL(*CmdLine);
1850 return (EFI_OUT_OF_RESOURCES);
1851 }
1852 } else {
1853 NewCmdLine = StrnCatGrow(&NewCmdLine, NULL, (*CmdLine), 0);
1854 }
1855
1856 SHELL_FREE_NON_NULL(*CmdLine);
1857 SHELL_FREE_NON_NULL(CommandName);
1858
1859 //
1860 // re-assign the passed in double pointer to point to our newly allocated buffer
1861 //
1862 *CmdLine = NewCmdLine;
1863
1864 return (EFI_SUCCESS);
1865 }
1866
1867 /**
1868 Takes the Argv[0] part of the command line and determine the meaning of it.
1869
1870 @param[in] CmdName pointer to the command line to update.
1871
1872 @retval Internal_Command The name is an internal command.
1873 @retval File_Sys_Change the name is a file system change.
1874 @retval Script_File_Name the name is a NSH script file.
1875 @retval Unknown_Invalid the name is unknown.
1876 @retval Efi_Application the name is an application (.EFI).
1877 **/
1878 SHELL_OPERATION_TYPES
1879 EFIAPI
1880 GetOperationType(
1881 IN CONST CHAR16 *CmdName
1882 )
1883 {
1884 CHAR16* FileWithPath;
1885 CONST CHAR16* TempLocation;
1886 CONST CHAR16* TempLocation2;
1887
1888 FileWithPath = NULL;
1889 //
1890 // test for an internal command.
1891 //
1892 if (ShellCommandIsCommandOnList(CmdName)) {
1893 return (Internal_Command);
1894 }
1895
1896 //
1897 // Test for file system change request. anything ending with first : and cant have spaces.
1898 //
1899 if (CmdName[(StrLen(CmdName)-1)] == L':') {
1900 if ( StrStr(CmdName, L" ") != NULL
1901 || StrLen(StrStr(CmdName, L":")) > 1
1902 ) {
1903 return (Unknown_Invalid);
1904 }
1905 return (File_Sys_Change);
1906 }
1907
1908 //
1909 // Test for a file
1910 //
1911 if ((FileWithPath = ShellFindFilePathEx(CmdName, mExecutableExtensions)) != NULL) {
1912 //
1913 // See if that file has a script file extension
1914 //
1915 if (StrLen(FileWithPath) > 4) {
1916 TempLocation = FileWithPath+StrLen(FileWithPath)-4;
1917 TempLocation2 = mScriptExtension;
1918 if (StringNoCaseCompare((VOID*)(&TempLocation), (VOID*)(&TempLocation2)) == 0) {
1919 SHELL_FREE_NON_NULL(FileWithPath);
1920 return (Script_File_Name);
1921 }
1922 }
1923
1924 //
1925 // Was a file, but not a script. we treat this as an application.
1926 //
1927 SHELL_FREE_NON_NULL(FileWithPath);
1928 return (Efi_Application);
1929 }
1930
1931 SHELL_FREE_NON_NULL(FileWithPath);
1932 //
1933 // No clue what this is... return invalid flag...
1934 //
1935 return (Unknown_Invalid);
1936 }
1937
1938 /**
1939 Determine if the first item in a command line is valid.
1940
1941 @param[in] CmdLine The command line to parse.
1942
1943 @retval EFI_SUCCESS The item is valid.
1944 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
1945 @retval EFI_NOT_FOUND The operation type is unknown or invalid.
1946 **/
1947 EFI_STATUS
1948 EFIAPI
1949 IsValidSplit(
1950 IN CONST CHAR16 *CmdLine
1951 )
1952 {
1953 CHAR16 *Temp;
1954 CHAR16 *FirstParameter;
1955 CHAR16 *TempWalker;
1956 EFI_STATUS Status;
1957
1958 Temp = NULL;
1959
1960 Temp = StrnCatGrow(&Temp, NULL, CmdLine, 0);
1961 if (Temp == NULL) {
1962 return (EFI_OUT_OF_RESOURCES);
1963 }
1964
1965 FirstParameter = StrStr(Temp, L"|");
1966 if (FirstParameter != NULL) {
1967 *FirstParameter = CHAR_NULL;
1968 }
1969
1970 FirstParameter = NULL;
1971
1972 //
1973 // Process the command line
1974 //
1975 Status = ProcessCommandLineToFinal(&Temp);
1976
1977 if (!EFI_ERROR(Status)) {
1978 FirstParameter = AllocateZeroPool(StrSize(CmdLine));
1979 if (FirstParameter == NULL) {
1980 SHELL_FREE_NON_NULL(Temp);
1981 return (EFI_OUT_OF_RESOURCES);
1982 }
1983 TempWalker = (CHAR16*)Temp;
1984 if (!EFI_ERROR(GetNextParameter(&TempWalker, &FirstParameter, StrSize(CmdLine), TRUE))) {
1985 if (GetOperationType(FirstParameter) == Unknown_Invalid) {
1986 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SHELL_NOT_FOUND), ShellInfoObject.HiiHandle, FirstParameter);
1987 SetLastError(SHELL_NOT_FOUND);
1988 Status = EFI_NOT_FOUND;
1989 }
1990 }
1991 }
1992
1993 SHELL_FREE_NON_NULL(Temp);
1994 SHELL_FREE_NON_NULL(FirstParameter);
1995 return Status;
1996 }
1997
1998 /**
1999 Determine if a command line contains with a split contains only valid commands.
2000
2001 @param[in] CmdLine The command line to parse.
2002
2003 @retval EFI_SUCCESS CmdLine has only valid commands, application, or has no split.
2004 @retval EFI_ABORTED CmdLine has at least one invalid command or application.
2005 **/
2006 EFI_STATUS
2007 EFIAPI
2008 VerifySplit(
2009 IN CONST CHAR16 *CmdLine
2010 )
2011 {
2012 CONST CHAR16 *TempSpot;
2013 EFI_STATUS Status;
2014
2015 //
2016 // If this was the only item, then get out
2017 //
2018 if (!ContainsSplit(CmdLine)) {
2019 return (EFI_SUCCESS);
2020 }
2021
2022 //
2023 // Verify up to the pipe or end character
2024 //
2025 Status = IsValidSplit(CmdLine);
2026 if (EFI_ERROR(Status)) {
2027 return (Status);
2028 }
2029
2030 //
2031 // recurse to verify the next item
2032 //
2033 TempSpot = FindFirstCharacter(CmdLine, L"|", L'^') + 1;
2034 if (*TempSpot == L'a' &&
2035 (*(TempSpot + 1) == L' ' || *(TempSpot + 1) == CHAR_NULL)
2036 ) {
2037 // If it's an ASCII pipe '|a'
2038 TempSpot += 1;
2039 }
2040
2041 return (VerifySplit(TempSpot));
2042 }
2043
2044 /**
2045 Process a split based operation.
2046
2047 @param[in] CmdLine pointer to the command line to process
2048
2049 @retval EFI_SUCCESS The operation was successful
2050 @return an error occurred.
2051 **/
2052 EFI_STATUS
2053 EFIAPI
2054 ProcessNewSplitCommandLine(
2055 IN CONST CHAR16 *CmdLine
2056 )
2057 {
2058 SPLIT_LIST *Split;
2059 EFI_STATUS Status;
2060
2061 Status = VerifySplit(CmdLine);
2062 if (EFI_ERROR(Status)) {
2063 return (Status);
2064 }
2065
2066 Split = NULL;
2067
2068 //
2069 // are we in an existing split???
2070 //
2071 if (!IsListEmpty(&ShellInfoObject.SplitList.Link)) {
2072 Split = (SPLIT_LIST*)GetFirstNode(&ShellInfoObject.SplitList.Link);
2073 }
2074
2075 if (Split == NULL) {
2076 Status = RunSplitCommand(CmdLine, NULL, NULL);
2077 } else {
2078 Status = RunSplitCommand(CmdLine, Split->SplitStdIn, Split->SplitStdOut);
2079 }
2080 if (EFI_ERROR(Status)) {
2081 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SHELL_INVALID_SPLIT), ShellInfoObject.HiiHandle, CmdLine);
2082 }
2083 return (Status);
2084 }
2085
2086 /**
2087 Handle a request to change the current file system.
2088
2089 @param[in] CmdLine The passed in command line.
2090
2091 @retval EFI_SUCCESS The operation was successful.
2092 **/
2093 EFI_STATUS
2094 EFIAPI
2095 ChangeMappedDrive(
2096 IN CONST CHAR16 *CmdLine
2097 )
2098 {
2099 EFI_STATUS Status;
2100 Status = EFI_SUCCESS;
2101
2102 //
2103 // make sure we are the right operation
2104 //
2105 ASSERT(CmdLine[(StrLen(CmdLine)-1)] == L':' && StrStr(CmdLine, L" ") == NULL);
2106
2107 //
2108 // Call the protocol API to do the work
2109 //
2110 Status = ShellInfoObject.NewEfiShellProtocol->SetCurDir(NULL, CmdLine);
2111
2112 //
2113 // Report any errors
2114 //
2115 if (EFI_ERROR(Status)) {
2116 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SHELL_INVALID_MAPPING), ShellInfoObject.HiiHandle, CmdLine);
2117 }
2118
2119 return (Status);
2120 }
2121
2122 /**
2123 Reprocess the command line to direct all -? to the help command.
2124
2125 if found, will add "help" as argv[0], and move the rest later.
2126
2127 @param[in,out] CmdLine pointer to the command line to update
2128 **/
2129 EFI_STATUS
2130 EFIAPI
2131 DoHelpUpdate(
2132 IN OUT CHAR16 **CmdLine
2133 )
2134 {
2135 CHAR16 *CurrentParameter;
2136 CHAR16 *Walker;
2137 CHAR16 *NewCommandLine;
2138 EFI_STATUS Status;
2139 UINTN NewCmdLineSize;
2140
2141 Status = EFI_SUCCESS;
2142
2143 CurrentParameter = AllocateZeroPool(StrSize(*CmdLine));
2144 if (CurrentParameter == NULL) {
2145 return (EFI_OUT_OF_RESOURCES);
2146 }
2147
2148 Walker = *CmdLine;
2149 while(Walker != NULL && *Walker != CHAR_NULL) {
2150 if (!EFI_ERROR(GetNextParameter(&Walker, &CurrentParameter, StrSize(*CmdLine), TRUE))) {
2151 if (StrStr(CurrentParameter, L"-?") == CurrentParameter) {
2152 CurrentParameter[0] = L' ';
2153 CurrentParameter[1] = L' ';
2154 NewCmdLineSize = StrSize(L"help ") + StrSize(*CmdLine);
2155 NewCommandLine = AllocateZeroPool(NewCmdLineSize);
2156 if (NewCommandLine == NULL) {
2157 Status = EFI_OUT_OF_RESOURCES;
2158 break;
2159 }
2160
2161 //
2162 // We know the space is sufficient since we just calculated it.
2163 //
2164 StrnCpyS(NewCommandLine, NewCmdLineSize/sizeof(CHAR16), L"help ", 5);
2165 StrnCatS(NewCommandLine, NewCmdLineSize/sizeof(CHAR16), *CmdLine, StrLen(*CmdLine));
2166 SHELL_FREE_NON_NULL(*CmdLine);
2167 *CmdLine = NewCommandLine;
2168 break;
2169 }
2170 }
2171 }
2172
2173 SHELL_FREE_NON_NULL(CurrentParameter);
2174
2175 return (Status);
2176 }
2177
2178 /**
2179 Function to update the shell variable "lasterror".
2180
2181 @param[in] ErrorCode the error code to put into lasterror.
2182 **/
2183 EFI_STATUS
2184 EFIAPI
2185 SetLastError(
2186 IN CONST SHELL_STATUS ErrorCode
2187 )
2188 {
2189 CHAR16 LeString[19];
2190 if (sizeof(EFI_STATUS) == sizeof(UINT64)) {
2191 UnicodeSPrint(LeString, sizeof(LeString), L"0x%Lx", ErrorCode);
2192 } else {
2193 UnicodeSPrint(LeString, sizeof(LeString), L"0x%x", ErrorCode);
2194 }
2195 DEBUG_CODE(InternalEfiShellSetEnv(L"debuglasterror", LeString, TRUE););
2196 InternalEfiShellSetEnv(L"lasterror", LeString, TRUE);
2197
2198 return (EFI_SUCCESS);
2199 }
2200
2201 /**
2202 Converts the command line to it's post-processed form. this replaces variables and alias' per UEFI Shell spec.
2203
2204 @param[in,out] CmdLine pointer to the command line to update
2205
2206 @retval EFI_SUCCESS The operation was successful
2207 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
2208 @return some other error occurred
2209 **/
2210 EFI_STATUS
2211 EFIAPI
2212 ProcessCommandLineToFinal(
2213 IN OUT CHAR16 **CmdLine
2214 )
2215 {
2216 EFI_STATUS Status;
2217 TrimSpaces(CmdLine);
2218
2219 Status = ShellSubstituteAliases(CmdLine);
2220 if (EFI_ERROR(Status)) {
2221 return (Status);
2222 }
2223
2224 TrimSpaces(CmdLine);
2225
2226 Status = ShellSubstituteVariables(CmdLine);
2227 if (EFI_ERROR(Status)) {
2228 return (Status);
2229 }
2230 ASSERT (*CmdLine != NULL);
2231
2232 TrimSpaces(CmdLine);
2233
2234 //
2235 // update for help parsing
2236 //
2237 if (StrStr(*CmdLine, L"?") != NULL) {
2238 //
2239 // This may do nothing if the ? does not indicate help.
2240 // Save all the details for in the API below.
2241 //
2242 Status = DoHelpUpdate(CmdLine);
2243 }
2244
2245 TrimSpaces(CmdLine);
2246
2247 return (EFI_SUCCESS);
2248 }
2249
2250 /**
2251 Run an internal shell command.
2252
2253 This API will update the shell's environment since these commands are libraries.
2254
2255 @param[in] CmdLine the command line to run.
2256 @param[in] FirstParameter the first parameter on the command line
2257 @param[in] ParamProtocol the shell parameters protocol pointer
2258 @param[out] CommandStatus the status from the command line.
2259
2260 @retval EFI_SUCCESS The command was completed.
2261 @retval EFI_ABORTED The command's operation was aborted.
2262 **/
2263 EFI_STATUS
2264 EFIAPI
2265 RunInternalCommand(
2266 IN CONST CHAR16 *CmdLine,
2267 IN CHAR16 *FirstParameter,
2268 IN EFI_SHELL_PARAMETERS_PROTOCOL *ParamProtocol,
2269 OUT EFI_STATUS *CommandStatus
2270 )
2271 {
2272 EFI_STATUS Status;
2273 UINTN Argc;
2274 CHAR16 **Argv;
2275 SHELL_STATUS CommandReturnedStatus;
2276 BOOLEAN LastError;
2277 CHAR16 *Walker;
2278 CHAR16 *NewCmdLine;
2279
2280 NewCmdLine = AllocateCopyPool (StrSize (CmdLine), CmdLine);
2281 if (NewCmdLine == NULL) {
2282 return EFI_OUT_OF_RESOURCES;
2283 }
2284
2285 for (Walker = NewCmdLine; Walker != NULL && *Walker != CHAR_NULL ; Walker++) {
2286 if (*Walker == L'^' && *(Walker+1) == L'#') {
2287 CopyMem(Walker, Walker+1, StrSize(Walker) - sizeof(Walker[0]));
2288 }
2289 }
2290
2291 //
2292 // get the argc and argv updated for internal commands
2293 //
2294 Status = UpdateArgcArgv(ParamProtocol, NewCmdLine, Internal_Command, &Argv, &Argc);
2295 if (!EFI_ERROR(Status)) {
2296 //
2297 // Run the internal command.
2298 //
2299 Status = ShellCommandRunCommandHandler(FirstParameter, &CommandReturnedStatus, &LastError);
2300
2301 if (!EFI_ERROR(Status)) {
2302 if (CommandStatus != NULL) {
2303 if (CommandReturnedStatus != SHELL_SUCCESS) {
2304 *CommandStatus = (EFI_STATUS)(CommandReturnedStatus | MAX_BIT);
2305 } else {
2306 *CommandStatus = EFI_SUCCESS;
2307 }
2308 }
2309
2310 //
2311 // Update last error status.
2312 // some commands do not update last error.
2313 //
2314 if (LastError) {
2315 SetLastError(CommandReturnedStatus);
2316 }
2317
2318 //
2319 // Pass thru the exitcode from the app.
2320 //
2321 if (ShellCommandGetExit()) {
2322 //
2323 // An Exit was requested ("exit" command), pass its value up.
2324 //
2325 Status = CommandReturnedStatus;
2326 } else if (CommandReturnedStatus != SHELL_SUCCESS && IsScriptOnlyCommand(FirstParameter)) {
2327 //
2328 // Always abort when a script only command fails for any reason
2329 //
2330 Status = EFI_ABORTED;
2331 } else if (ShellCommandGetCurrentScriptFile() != NULL && CommandReturnedStatus == SHELL_ABORTED) {
2332 //
2333 // Abort when in a script and a command aborted
2334 //
2335 Status = EFI_ABORTED;
2336 }
2337 }
2338 }
2339
2340 //
2341 // This is guaranteed to be called after UpdateArgcArgv no matter what else happened.
2342 // This is safe even if the update API failed. In this case, it may be a no-op.
2343 //
2344 RestoreArgcArgv(ParamProtocol, &Argv, &Argc);
2345
2346 //
2347 // If a script is running and the command is not a script only command, then
2348 // change return value to success so the script won't halt (unless aborted).
2349 //
2350 // Script only commands have to be able halt the script since the script will
2351 // not operate if they are failing.
2352 //
2353 if ( ShellCommandGetCurrentScriptFile() != NULL
2354 && !IsScriptOnlyCommand(FirstParameter)
2355 && Status != EFI_ABORTED
2356 ) {
2357 Status = EFI_SUCCESS;
2358 }
2359
2360 FreePool (NewCmdLine);
2361 return (Status);
2362 }
2363
2364 /**
2365 Function to run the command or file.
2366
2367 @param[in] Type the type of operation being run.
2368 @param[in] CmdLine the command line to run.
2369 @param[in] FirstParameter the first parameter on the command line
2370 @param[in] ParamProtocol the shell parameters protocol pointer
2371 @param[out] CommandStatus the status from the command line.
2372
2373 @retval EFI_SUCCESS The command was completed.
2374 @retval EFI_ABORTED The command's operation was aborted.
2375 **/
2376 EFI_STATUS
2377 EFIAPI
2378 RunCommandOrFile(
2379 IN SHELL_OPERATION_TYPES Type,
2380 IN CONST CHAR16 *CmdLine,
2381 IN CHAR16 *FirstParameter,
2382 IN EFI_SHELL_PARAMETERS_PROTOCOL *ParamProtocol,
2383 OUT EFI_STATUS *CommandStatus
2384 )
2385 {
2386 EFI_STATUS Status;
2387 EFI_STATUS StartStatus;
2388 CHAR16 *CommandWithPath;
2389 EFI_DEVICE_PATH_PROTOCOL *DevPath;
2390 SHELL_STATUS CalleeExitStatus;
2391
2392 Status = EFI_SUCCESS;
2393 CommandWithPath = NULL;
2394 DevPath = NULL;
2395 CalleeExitStatus = SHELL_INVALID_PARAMETER;
2396
2397 switch (Type) {
2398 case Internal_Command:
2399 Status = RunInternalCommand(CmdLine, FirstParameter, ParamProtocol, CommandStatus);
2400 break;
2401 case Script_File_Name:
2402 case Efi_Application:
2403 //
2404 // Process a fully qualified path
2405 //
2406 if (StrStr(FirstParameter, L":") != NULL) {
2407 ASSERT (CommandWithPath == NULL);
2408 if (ShellIsFile(FirstParameter) == EFI_SUCCESS) {
2409 CommandWithPath = StrnCatGrow(&CommandWithPath, NULL, FirstParameter, 0);
2410 }
2411 }
2412
2413 //
2414 // Process a relative path and also check in the path environment variable
2415 //
2416 if (CommandWithPath == NULL) {
2417 CommandWithPath = ShellFindFilePathEx(FirstParameter, mExecutableExtensions);
2418 }
2419
2420 //
2421 // This should be impossible now.
2422 //
2423 ASSERT(CommandWithPath != NULL);
2424
2425 //
2426 // Make sure that path is not just a directory (or not found)
2427 //
2428 if (!EFI_ERROR(ShellIsDirectory(CommandWithPath))) {
2429 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SHELL_NOT_FOUND), ShellInfoObject.HiiHandle, FirstParameter);
2430 SetLastError(SHELL_NOT_FOUND);
2431 }
2432 switch (Type) {
2433 case Script_File_Name:
2434 Status = RunScriptFile (CommandWithPath, NULL, CmdLine, ParamProtocol);
2435 break;
2436 case Efi_Application:
2437 //
2438 // Get the device path of the application image
2439 //
2440 DevPath = ShellInfoObject.NewEfiShellProtocol->GetDevicePathFromFilePath(CommandWithPath);
2441 if (DevPath == NULL){
2442 Status = EFI_OUT_OF_RESOURCES;
2443 break;
2444 }
2445
2446 //
2447 // Execute the device path
2448 //
2449 Status = InternalShellExecuteDevicePath(
2450 &gImageHandle,
2451 DevPath,
2452 CmdLine,
2453 NULL,
2454 &StartStatus
2455 );
2456
2457 SHELL_FREE_NON_NULL(DevPath);
2458
2459 if(EFI_ERROR (Status)) {
2460 CalleeExitStatus = (SHELL_STATUS) (Status & (~MAX_BIT));
2461 } else {
2462 CalleeExitStatus = (SHELL_STATUS) StartStatus;
2463 }
2464
2465 if (CommandStatus != NULL) {
2466 *CommandStatus = CalleeExitStatus;
2467 }
2468
2469 //
2470 // Update last error status.
2471 //
2472 // Status is an EFI_STATUS. Clear top bit to convert to SHELL_STATUS
2473 SetLastError(CalleeExitStatus);
2474 break;
2475 default:
2476 //
2477 // Do nothing.
2478 //
2479 break;
2480 }
2481 break;
2482 default:
2483 //
2484 // Do nothing.
2485 //
2486 break;
2487 }
2488
2489 SHELL_FREE_NON_NULL(CommandWithPath);
2490
2491 return (Status);
2492 }
2493
2494 /**
2495 Function to setup StdIn, StdErr, StdOut, and then run the command or file.
2496
2497 @param[in] Type the type of operation being run.
2498 @param[in] CmdLine the command line to run.
2499 @param[in] FirstParameter the first parameter on the command line.
2500 @param[in] ParamProtocol the shell parameters protocol pointer
2501 @param[out] CommandStatus the status from the command line.
2502
2503 @retval EFI_SUCCESS The command was completed.
2504 @retval EFI_ABORTED The command's operation was aborted.
2505 **/
2506 EFI_STATUS
2507 EFIAPI
2508 SetupAndRunCommandOrFile(
2509 IN SHELL_OPERATION_TYPES Type,
2510 IN CHAR16 *CmdLine,
2511 IN CHAR16 *FirstParameter,
2512 IN EFI_SHELL_PARAMETERS_PROTOCOL *ParamProtocol,
2513 OUT EFI_STATUS *CommandStatus
2514 )
2515 {
2516 EFI_STATUS Status;
2517 SHELL_FILE_HANDLE OriginalStdIn;
2518 SHELL_FILE_HANDLE OriginalStdOut;
2519 SHELL_FILE_HANDLE OriginalStdErr;
2520 SYSTEM_TABLE_INFO OriginalSystemTableInfo;
2521 CONST SCRIPT_FILE *ConstScriptFile;
2522
2523 //
2524 // Update the StdIn, StdOut, and StdErr for redirection to environment variables, files, etc... unicode and ASCII
2525 //
2526 Status = UpdateStdInStdOutStdErr(ParamProtocol, CmdLine, &OriginalStdIn, &OriginalStdOut, &OriginalStdErr, &OriginalSystemTableInfo);
2527
2528 //
2529 // The StdIn, StdOut, and StdErr are set up.
2530 // Now run the command, script, or application
2531 //
2532 if (!EFI_ERROR(Status)) {
2533 TrimSpaces(&CmdLine);
2534 Status = RunCommandOrFile(Type, CmdLine, FirstParameter, ParamProtocol, CommandStatus);
2535 }
2536
2537 //
2538 // Now print errors
2539 //
2540 if (EFI_ERROR(Status)) {
2541 ConstScriptFile = ShellCommandGetCurrentScriptFile();
2542 if (ConstScriptFile == NULL || ConstScriptFile->CurrentCommand == NULL) {
2543 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SHELL_ERROR), ShellInfoObject.HiiHandle, (VOID*)(Status));
2544 } else {
2545 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SHELL_ERROR_SCRIPT), ShellInfoObject.HiiHandle, (VOID*)(Status), ConstScriptFile->CurrentCommand->Line);
2546 }
2547 }
2548
2549 //
2550 // put back the original StdIn, StdOut, and StdErr
2551 //
2552 RestoreStdInStdOutStdErr(ParamProtocol, &OriginalStdIn, &OriginalStdOut, &OriginalStdErr, &OriginalSystemTableInfo);
2553
2554 return (Status);
2555 }
2556
2557 /**
2558 Function will process and run a command line.
2559
2560 This will determine if the command line represents an internal shell
2561 command or dispatch an external application.
2562
2563 @param[in] CmdLine The command line to parse.
2564 @param[out] CommandStatus The status from the command line.
2565
2566 @retval EFI_SUCCESS The command was completed.
2567 @retval EFI_ABORTED The command's operation was aborted.
2568 **/
2569 EFI_STATUS
2570 EFIAPI
2571 RunShellCommand(
2572 IN CONST CHAR16 *CmdLine,
2573 OUT EFI_STATUS *CommandStatus
2574 )
2575 {
2576 EFI_STATUS Status;
2577 CHAR16 *CleanOriginal;
2578 CHAR16 *FirstParameter;
2579 CHAR16 *TempWalker;
2580 SHELL_OPERATION_TYPES Type;
2581
2582 ASSERT(CmdLine != NULL);
2583 if (StrLen(CmdLine) == 0) {
2584 return (EFI_SUCCESS);
2585 }
2586
2587 Status = EFI_SUCCESS;
2588 CleanOriginal = NULL;
2589
2590 CleanOriginal = StrnCatGrow(&CleanOriginal, NULL, CmdLine, 0);
2591 if (CleanOriginal == NULL) {
2592 return (EFI_OUT_OF_RESOURCES);
2593 }
2594
2595 TrimSpaces(&CleanOriginal);
2596
2597 //
2598 // NULL out comments (leveraged from RunScriptFileHandle() ).
2599 // The # character on a line is used to denote that all characters on the same line
2600 // and to the right of the # are to be ignored by the shell.
2601 // Afterwards, again remove spaces, in case any were between the last command-parameter and '#'.
2602 //
2603 for (TempWalker = CleanOriginal; TempWalker != NULL && *TempWalker != CHAR_NULL; TempWalker++) {
2604 if (*TempWalker == L'^') {
2605 if (*(TempWalker + 1) == L'#') {
2606 TempWalker++;
2607 }
2608 } else if (*TempWalker == L'#') {
2609 *TempWalker = CHAR_NULL;
2610 }
2611 }
2612
2613 TrimSpaces(&CleanOriginal);
2614
2615 //
2616 // Handle case that passed in command line is just 1 or more " " characters.
2617 //
2618 if (StrLen (CleanOriginal) == 0) {
2619 SHELL_FREE_NON_NULL(CleanOriginal);
2620 return (EFI_SUCCESS);
2621 }
2622
2623 Status = ProcessCommandLineToFinal(&CleanOriginal);
2624 if (EFI_ERROR(Status)) {
2625 SHELL_FREE_NON_NULL(CleanOriginal);
2626 return (Status);
2627 }
2628
2629 //
2630 // We don't do normal processing with a split command line (output from one command input to another)
2631 //
2632 if (ContainsSplit(CleanOriginal)) {
2633 Status = ProcessNewSplitCommandLine(CleanOriginal);
2634 SHELL_FREE_NON_NULL(CleanOriginal);
2635 return (Status);
2636 }
2637
2638 //
2639 // We need the first parameter information so we can determine the operation type
2640 //
2641 FirstParameter = AllocateZeroPool(StrSize(CleanOriginal));
2642 if (FirstParameter == NULL) {
2643 SHELL_FREE_NON_NULL(CleanOriginal);
2644 return (EFI_OUT_OF_RESOURCES);
2645 }
2646 TempWalker = CleanOriginal;
2647 if (!EFI_ERROR(GetNextParameter(&TempWalker, &FirstParameter, StrSize(CleanOriginal), TRUE))) {
2648 //
2649 // Depending on the first parameter we change the behavior
2650 //
2651 switch (Type = GetOperationType(FirstParameter)) {
2652 case File_Sys_Change:
2653 Status = ChangeMappedDrive (FirstParameter);
2654 break;
2655 case Internal_Command:
2656 case Script_File_Name:
2657 case Efi_Application:
2658 Status = SetupAndRunCommandOrFile(Type, CleanOriginal, FirstParameter, ShellInfoObject.NewShellParametersProtocol, CommandStatus);
2659 break;
2660 default:
2661 //
2662 // Whatever was typed, it was invalid.
2663 //
2664 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SHELL_NOT_FOUND), ShellInfoObject.HiiHandle, FirstParameter);
2665 SetLastError(SHELL_NOT_FOUND);
2666 break;
2667 }
2668 } else {
2669 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SHELL_NOT_FOUND), ShellInfoObject.HiiHandle, FirstParameter);
2670 SetLastError(SHELL_NOT_FOUND);
2671 }
2672
2673 SHELL_FREE_NON_NULL(CleanOriginal);
2674 SHELL_FREE_NON_NULL(FirstParameter);
2675
2676 return (Status);
2677 }
2678
2679 /**
2680 Function will process and run a command line.
2681
2682 This will determine if the command line represents an internal shell
2683 command or dispatch an external application.
2684
2685 @param[in] CmdLine The command line to parse.
2686
2687 @retval EFI_SUCCESS The command was completed.
2688 @retval EFI_ABORTED The command's operation was aborted.
2689 **/
2690 EFI_STATUS
2691 EFIAPI
2692 RunCommand(
2693 IN CONST CHAR16 *CmdLine
2694 )
2695 {
2696 return (RunShellCommand(CmdLine, NULL));
2697 }
2698
2699
2700 STATIC CONST UINT16 InvalidChars[] = {L'*', L'?', L'<', L'>', L'\\', L'/', L'\"', 0x0001, 0x0002};
2701 /**
2702 Function determines if the CommandName COULD be a valid command. It does not determine whether
2703 this is a valid command. It only checks for invalid characters.
2704
2705 @param[in] CommandName The name to check
2706
2707 @retval TRUE CommandName could be a command name
2708 @retval FALSE CommandName could not be a valid command name
2709 **/
2710 BOOLEAN
2711 EFIAPI
2712 IsValidCommandName(
2713 IN CONST CHAR16 *CommandName
2714 )
2715 {
2716 UINTN Count;
2717 if (CommandName == NULL) {
2718 ASSERT(FALSE);
2719 return (FALSE);
2720 }
2721 for ( Count = 0
2722 ; Count < sizeof(InvalidChars) / sizeof(InvalidChars[0])
2723 ; Count++
2724 ){
2725 if (ScanMem16(CommandName, StrSize(CommandName), InvalidChars[Count]) != NULL) {
2726 return (FALSE);
2727 }
2728 }
2729 return (TRUE);
2730 }
2731
2732 /**
2733 Function to process a NSH script file via SHELL_FILE_HANDLE.
2734
2735 @param[in] Handle The handle to the already opened file.
2736 @param[in] Name The name of the script file.
2737
2738 @retval EFI_SUCCESS the script completed successfully
2739 **/
2740 EFI_STATUS
2741 EFIAPI
2742 RunScriptFileHandle (
2743 IN SHELL_FILE_HANDLE Handle,
2744 IN CONST CHAR16 *Name
2745 )
2746 {
2747 EFI_STATUS Status;
2748 SCRIPT_FILE *NewScriptFile;
2749 UINTN LoopVar;
2750 UINTN PrintBuffSize;
2751 CHAR16 *CommandLine;
2752 CHAR16 *CommandLine2;
2753 CHAR16 *CommandLine3;
2754 SCRIPT_COMMAND_LIST *LastCommand;
2755 BOOLEAN Ascii;
2756 BOOLEAN PreScriptEchoState;
2757 BOOLEAN PreCommandEchoState;
2758 CONST CHAR16 *CurDir;
2759 UINTN LineCount;
2760 CHAR16 LeString[50];
2761 LIST_ENTRY OldBufferList;
2762
2763 ASSERT(!ShellCommandGetScriptExit());
2764
2765 PreScriptEchoState = ShellCommandGetEchoState();
2766 PrintBuffSize = PcdGet16(PcdShellPrintBufferSize);
2767
2768 NewScriptFile = (SCRIPT_FILE*)AllocateZeroPool(sizeof(SCRIPT_FILE));
2769 if (NewScriptFile == NULL) {
2770 return (EFI_OUT_OF_RESOURCES);
2771 }
2772
2773 //
2774 // Set up the name
2775 //
2776 ASSERT(NewScriptFile->ScriptName == NULL);
2777 NewScriptFile->ScriptName = StrnCatGrow(&NewScriptFile->ScriptName, NULL, Name, 0);
2778 if (NewScriptFile->ScriptName == NULL) {
2779 DeleteScriptFileStruct(NewScriptFile);
2780 return (EFI_OUT_OF_RESOURCES);
2781 }
2782
2783 //
2784 // Save the parameters (used to replace %0 to %9 later on)
2785 //
2786 NewScriptFile->Argc = ShellInfoObject.NewShellParametersProtocol->Argc;
2787 if (NewScriptFile->Argc != 0) {
2788 NewScriptFile->Argv = (CHAR16**)AllocateZeroPool(NewScriptFile->Argc * sizeof(CHAR16*));
2789 if (NewScriptFile->Argv == NULL) {
2790 DeleteScriptFileStruct(NewScriptFile);
2791 return (EFI_OUT_OF_RESOURCES);
2792 }
2793 for (LoopVar = 0 ; LoopVar < 10 && LoopVar < NewScriptFile->Argc; LoopVar++) {
2794 ASSERT(NewScriptFile->Argv[LoopVar] == NULL);
2795 NewScriptFile->Argv[LoopVar] = StrnCatGrow(&NewScriptFile->Argv[LoopVar], NULL, ShellInfoObject.NewShellParametersProtocol->Argv[LoopVar], 0);
2796 if (NewScriptFile->Argv[LoopVar] == NULL) {
2797 DeleteScriptFileStruct(NewScriptFile);
2798 return (EFI_OUT_OF_RESOURCES);
2799 }
2800 }
2801 } else {
2802 NewScriptFile->Argv = NULL;
2803 }
2804
2805 InitializeListHead(&NewScriptFile->CommandList);
2806 InitializeListHead(&NewScriptFile->SubstList);
2807
2808 //
2809 // Now build the list of all script commands.
2810 //
2811 LineCount = 0;
2812 while(!ShellFileHandleEof(Handle)) {
2813 CommandLine = ShellFileHandleReturnLine(Handle, &Ascii);
2814 LineCount++;
2815 if (CommandLine == NULL || StrLen(CommandLine) == 0 || CommandLine[0] == '#') {
2816 SHELL_FREE_NON_NULL(CommandLine);
2817 continue;
2818 }
2819 NewScriptFile->CurrentCommand = AllocateZeroPool(sizeof(SCRIPT_COMMAND_LIST));
2820 if (NewScriptFile->CurrentCommand == NULL) {
2821 SHELL_FREE_NON_NULL(CommandLine);
2822 DeleteScriptFileStruct(NewScriptFile);
2823 return (EFI_OUT_OF_RESOURCES);
2824 }
2825
2826 NewScriptFile->CurrentCommand->Cl = CommandLine;
2827 NewScriptFile->CurrentCommand->Data = NULL;
2828 NewScriptFile->CurrentCommand->Line = LineCount;
2829
2830 InsertTailList(&NewScriptFile->CommandList, &NewScriptFile->CurrentCommand->Link);
2831 }
2832
2833 //
2834 // Add this as the topmost script file
2835 //
2836 ShellCommandSetNewScript (NewScriptFile);
2837
2838 //
2839 // Now enumerate through the commands and run each one.
2840 //
2841 CommandLine = AllocateZeroPool(PrintBuffSize);
2842 if (CommandLine == NULL) {
2843 DeleteScriptFileStruct(NewScriptFile);
2844 return (EFI_OUT_OF_RESOURCES);
2845 }
2846 CommandLine2 = AllocateZeroPool(PrintBuffSize);
2847 if (CommandLine2 == NULL) {
2848 FreePool(CommandLine);
2849 DeleteScriptFileStruct(NewScriptFile);
2850 return (EFI_OUT_OF_RESOURCES);
2851 }
2852
2853 for ( NewScriptFile->CurrentCommand = (SCRIPT_COMMAND_LIST *)GetFirstNode(&NewScriptFile->CommandList)
2854 ; !IsNull(&NewScriptFile->CommandList, &NewScriptFile->CurrentCommand->Link)
2855 ; // conditional increment in the body of the loop
2856 ){
2857 ASSERT(CommandLine2 != NULL);
2858 StrnCpyS( CommandLine2,
2859 PrintBuffSize/sizeof(CHAR16),
2860 NewScriptFile->CurrentCommand->Cl,
2861 PrintBuffSize/sizeof(CHAR16) - 1
2862 );
2863
2864 SaveBufferList(&OldBufferList);
2865
2866 //
2867 // NULL out comments
2868 //
2869 for (CommandLine3 = CommandLine2 ; CommandLine3 != NULL && *CommandLine3 != CHAR_NULL ; CommandLine3++) {
2870 if (*CommandLine3 == L'^') {
2871 if ( *(CommandLine3+1) == L':') {
2872 CopyMem(CommandLine3, CommandLine3+1, StrSize(CommandLine3) - sizeof(CommandLine3[0]));
2873 } else if (*(CommandLine3+1) == L'#') {
2874 CommandLine3++;
2875 }
2876 } else if (*CommandLine3 == L'#') {
2877 *CommandLine3 = CHAR_NULL;
2878 }
2879 }
2880
2881 if (CommandLine2 != NULL && StrLen(CommandLine2) >= 1) {
2882 //
2883 // Due to variability in starting the find and replace action we need to have both buffers the same.
2884 //
2885 StrnCpyS( CommandLine,
2886 PrintBuffSize/sizeof(CHAR16),
2887 CommandLine2,
2888 PrintBuffSize/sizeof(CHAR16) - 1
2889 );
2890
2891 //
2892 // Remove the %0 to %9 from the command line (if we have some arguments)
2893 //
2894 if (NewScriptFile->Argv != NULL) {
2895 switch (NewScriptFile->Argc) {
2896 default:
2897 Status = ShellCopySearchAndReplace(CommandLine2, CommandLine, PrintBuffSize, L"%9", NewScriptFile->Argv[9], FALSE, FALSE);
2898 ASSERT_EFI_ERROR(Status);
2899 case 9:
2900 Status = ShellCopySearchAndReplace(CommandLine, CommandLine2, PrintBuffSize, L"%8", NewScriptFile->Argv[8], FALSE, FALSE);
2901 ASSERT_EFI_ERROR(Status);
2902 case 8:
2903 Status = ShellCopySearchAndReplace(CommandLine2, CommandLine, PrintBuffSize, L"%7", NewScriptFile->Argv[7], FALSE, FALSE);
2904 ASSERT_EFI_ERROR(Status);
2905 case 7:
2906 Status = ShellCopySearchAndReplace(CommandLine, CommandLine2, PrintBuffSize, L"%6", NewScriptFile->Argv[6], FALSE, FALSE);
2907 ASSERT_EFI_ERROR(Status);
2908 case 6:
2909 Status = ShellCopySearchAndReplace(CommandLine2, CommandLine, PrintBuffSize, L"%5", NewScriptFile->Argv[5], FALSE, FALSE);
2910 ASSERT_EFI_ERROR(Status);
2911 case 5:
2912 Status = ShellCopySearchAndReplace(CommandLine, CommandLine2, PrintBuffSize, L"%4", NewScriptFile->Argv[4], FALSE, FALSE);
2913 ASSERT_EFI_ERROR(Status);
2914 case 4:
2915 Status = ShellCopySearchAndReplace(CommandLine2, CommandLine, PrintBuffSize, L"%3", NewScriptFile->Argv[3], FALSE, FALSE);
2916 ASSERT_EFI_ERROR(Status);
2917 case 3:
2918 Status = ShellCopySearchAndReplace(CommandLine, CommandLine2, PrintBuffSize, L"%2", NewScriptFile->Argv[2], FALSE, FALSE);
2919 ASSERT_EFI_ERROR(Status);
2920 case 2:
2921 Status = ShellCopySearchAndReplace(CommandLine2, CommandLine, PrintBuffSize, L"%1", NewScriptFile->Argv[1], FALSE, FALSE);
2922 ASSERT_EFI_ERROR(Status);
2923 case 1:
2924 Status = ShellCopySearchAndReplace(CommandLine, CommandLine2, PrintBuffSize, L"%0", NewScriptFile->Argv[0], FALSE, FALSE);
2925 ASSERT_EFI_ERROR(Status);
2926 break;
2927 case 0:
2928 break;
2929 }
2930 }
2931 Status = ShellCopySearchAndReplace(CommandLine2, CommandLine, PrintBuffSize, L"%1", L"\"\"", FALSE, FALSE);
2932 Status = ShellCopySearchAndReplace(CommandLine, CommandLine2, PrintBuffSize, L"%2", L"\"\"", FALSE, FALSE);
2933 Status = ShellCopySearchAndReplace(CommandLine2, CommandLine, PrintBuffSize, L"%3", L"\"\"", FALSE, FALSE);
2934 Status = ShellCopySearchAndReplace(CommandLine, CommandLine2, PrintBuffSize, L"%4", L"\"\"", FALSE, FALSE);
2935 Status = ShellCopySearchAndReplace(CommandLine2, CommandLine, PrintBuffSize, L"%5", L"\"\"", FALSE, FALSE);
2936 Status = ShellCopySearchAndReplace(CommandLine, CommandLine2, PrintBuffSize, L"%6", L"\"\"", FALSE, FALSE);
2937 Status = ShellCopySearchAndReplace(CommandLine2, CommandLine, PrintBuffSize, L"%7", L"\"\"", FALSE, FALSE);
2938 Status = ShellCopySearchAndReplace(CommandLine, CommandLine2, PrintBuffSize, L"%8", L"\"\"", FALSE, FALSE);
2939 Status = ShellCopySearchAndReplace(CommandLine2, CommandLine, PrintBuffSize, L"%9", L"\"\"", FALSE, FALSE);
2940
2941 StrnCpyS( CommandLine2,
2942 PrintBuffSize/sizeof(CHAR16),
2943 CommandLine,
2944 PrintBuffSize/sizeof(CHAR16) - 1
2945 );
2946
2947 LastCommand = NewScriptFile->CurrentCommand;
2948
2949 for (CommandLine3 = CommandLine2 ; CommandLine3[0] == L' ' ; CommandLine3++);
2950
2951 if (CommandLine3 != NULL && CommandLine3[0] == L':' ) {
2952 //
2953 // This line is a goto target / label
2954 //
2955 } else {
2956 if (CommandLine3 != NULL && StrLen(CommandLine3) > 0) {
2957 if (CommandLine3[0] == L'@') {
2958 //
2959 // We need to save the current echo state
2960 // and disable echo for just this command.
2961 //
2962 PreCommandEchoState = ShellCommandGetEchoState();
2963 ShellCommandSetEchoState(FALSE);
2964 Status = RunCommand(CommandLine3+1);
2965
2966 //
2967 // If command was "@echo -off" or "@echo -on" then don't restore echo state
2968 //
2969 if (StrCmp (L"@echo -off", CommandLine3) != 0 &&
2970 StrCmp (L"@echo -on", CommandLine3) != 0) {
2971 //
2972 // Now restore the pre-'@' echo state.
2973 //
2974 ShellCommandSetEchoState(PreCommandEchoState);
2975 }
2976 } else {
2977 if (ShellCommandGetEchoState()) {
2978 CurDir = ShellInfoObject.NewEfiShellProtocol->GetEnv(L"cwd");
2979 if (CurDir != NULL && StrLen(CurDir) > 1) {
2980 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SHELL_CURDIR), ShellInfoObject.HiiHandle, CurDir);
2981 } else {
2982 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SHELL_SHELL), ShellInfoObject.HiiHandle);
2983 }
2984 ShellPrintEx(-1, -1, L"%s\r\n", CommandLine2);
2985 }
2986 Status = RunCommand(CommandLine3);
2987 }
2988 }
2989
2990 if (ShellCommandGetScriptExit()) {
2991 //
2992 // ShellCommandGetExitCode() always returns a UINT64
2993 //
2994 UnicodeSPrint(LeString, sizeof(LeString), L"0x%Lx", ShellCommandGetExitCode());
2995 DEBUG_CODE(InternalEfiShellSetEnv(L"debuglasterror", LeString, TRUE););
2996 InternalEfiShellSetEnv(L"lasterror", LeString, TRUE);
2997
2998 ShellCommandRegisterExit(FALSE, 0);
2999 Status = EFI_SUCCESS;
3000 RestoreBufferList(&OldBufferList);
3001 break;
3002 }
3003 if (ShellGetExecutionBreakFlag()) {
3004 RestoreBufferList(&OldBufferList);
3005 break;
3006 }
3007 if (EFI_ERROR(Status)) {
3008 RestoreBufferList(&OldBufferList);
3009 break;
3010 }
3011 if (ShellCommandGetExit()) {
3012 RestoreBufferList(&OldBufferList);
3013 break;
3014 }
3015 }
3016 //
3017 // If that commend did not update the CurrentCommand then we need to advance it...
3018 //
3019 if (LastCommand == NewScriptFile->CurrentCommand) {
3020 NewScriptFile->CurrentCommand = (SCRIPT_COMMAND_LIST *)GetNextNode(&NewScriptFile->CommandList, &NewScriptFile->CurrentCommand->Link);
3021 if (!IsNull(&NewScriptFile->CommandList, &NewScriptFile->CurrentCommand->Link)) {
3022 NewScriptFile->CurrentCommand->Reset = TRUE;
3023 }
3024 }
3025 } else {
3026 NewScriptFile->CurrentCommand = (SCRIPT_COMMAND_LIST *)GetNextNode(&NewScriptFile->CommandList, &NewScriptFile->CurrentCommand->Link);
3027 if (!IsNull(&NewScriptFile->CommandList, &NewScriptFile->CurrentCommand->Link)) {
3028 NewScriptFile->CurrentCommand->Reset = TRUE;
3029 }
3030 }
3031 RestoreBufferList(&OldBufferList);
3032 }
3033
3034
3035 FreePool(CommandLine);
3036 FreePool(CommandLine2);
3037 ShellCommandSetNewScript (NULL);
3038
3039 //
3040 // Only if this was the last script reset the state.
3041 //
3042 if (ShellCommandGetCurrentScriptFile()==NULL) {
3043 ShellCommandSetEchoState(PreScriptEchoState);
3044 }
3045 return (EFI_SUCCESS);
3046 }
3047
3048 /**
3049 Function to process a NSH script file.
3050
3051 @param[in] ScriptPath Pointer to the script file name (including file system path).
3052 @param[in] Handle the handle of the script file already opened.
3053 @param[in] CmdLine the command line to run.
3054 @param[in] ParamProtocol the shell parameters protocol pointer
3055
3056 @retval EFI_SUCCESS the script completed successfully
3057 **/
3058 EFI_STATUS
3059 EFIAPI
3060 RunScriptFile (
3061 IN CONST CHAR16 *ScriptPath,
3062 IN SHELL_FILE_HANDLE Handle OPTIONAL,
3063 IN CONST CHAR16 *CmdLine,
3064 IN EFI_SHELL_PARAMETERS_PROTOCOL *ParamProtocol
3065 )
3066 {
3067 EFI_STATUS Status;
3068 SHELL_FILE_HANDLE FileHandle;
3069 UINTN Argc;
3070 CHAR16 **Argv;
3071
3072 if (ShellIsFile(ScriptPath) != EFI_SUCCESS) {
3073 return (EFI_INVALID_PARAMETER);
3074 }
3075
3076 //
3077 // get the argc and argv updated for scripts
3078 //
3079 Status = UpdateArgcArgv(ParamProtocol, CmdLine, Script_File_Name, &Argv, &Argc);
3080 if (!EFI_ERROR(Status)) {
3081
3082 if (Handle == NULL) {
3083 //
3084 // open the file
3085 //
3086 Status = ShellOpenFileByName(ScriptPath, &FileHandle, EFI_FILE_MODE_READ, 0);
3087 if (!EFI_ERROR(Status)) {
3088 //
3089 // run it
3090 //
3091 Status = RunScriptFileHandle(FileHandle, ScriptPath);
3092
3093 //
3094 // now close the file
3095 //
3096 ShellCloseFile(&FileHandle);
3097 }
3098 } else {
3099 Status = RunScriptFileHandle(Handle, ScriptPath);
3100 }
3101 }
3102
3103 //
3104 // This is guaranteed to be called after UpdateArgcArgv no matter what else happened.
3105 // This is safe even if the update API failed. In this case, it may be a no-op.
3106 //
3107 RestoreArgcArgv(ParamProtocol, &Argv, &Argc);
3108
3109 return (Status);
3110 }
3111
3112 /**
3113 Return the pointer to the first occurrence of any character from a list of characters.
3114
3115 @param[in] String the string to parse
3116 @param[in] CharacterList the list of character to look for
3117 @param[in] EscapeCharacter An escape character to skip
3118
3119 @return the location of the first character in the string
3120 @retval CHAR_NULL no instance of any character in CharacterList was found in String
3121 **/
3122 CONST CHAR16*
3123 EFIAPI
3124 FindFirstCharacter(
3125 IN CONST CHAR16 *String,
3126 IN CONST CHAR16 *CharacterList,
3127 IN CONST CHAR16 EscapeCharacter
3128 )
3129 {
3130 UINT32 WalkChar;
3131 UINT32 WalkStr;
3132
3133 for (WalkStr = 0; WalkStr < StrLen(String); WalkStr++) {
3134 if (String[WalkStr] == EscapeCharacter) {
3135 WalkStr++;
3136 continue;
3137 }
3138 for (WalkChar = 0; WalkChar < StrLen(CharacterList); WalkChar++) {
3139 if (String[WalkStr] == CharacterList[WalkChar]) {
3140 return (&String[WalkStr]);
3141 }
3142 }
3143 }
3144 return (String + StrLen(String));
3145 }