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