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