2 This is THE shell (application)
4 Copyright (c) 2009 - 2015, 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
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.
19 // Initialize the global structure
21 SHELL_INFO ShellInfoObject
= {
56 {{NULL
, NULL
}, NULL
, NULL
},
57 {{NULL
, NULL
}, NULL
, NULL
},
69 STATIC CONST CHAR16 mScriptExtension
[] = L
".NSH";
70 STATIC CONST CHAR16 mExecutableExtensions
[] = L
".NSH;.EFI";
71 STATIC CONST CHAR16 mStartupScript
[] = L
"startup.nsh";
74 Cleans off leading and trailing spaces and tabs.
76 @param[in] String pointer to the string to trim them off.
84 ASSERT(String
!= NULL
);
85 ASSERT(*String
!= NULL
);
87 // Remove any spaces and tabs at the beginning of the (*String).
89 while (((*String
)[0] == L
' ') || ((*String
)[0] == L
'\t')) {
90 CopyMem((*String
), (*String
)+1, StrSize((*String
)) - sizeof((*String
)[0]));
94 // Remove any spaces and tabs at the end of the (*String).
96 while ((StrLen (*String
) > 0) && (((*String
)[StrLen((*String
))-1] == L
' ') || ((*String
)[StrLen((*String
))-1] == L
'\t'))) {
97 (*String
)[StrLen((*String
))-1] = CHAR_NULL
;
100 return (EFI_SUCCESS
);
104 Parse for the next instance of one string within another string. Can optionally make sure that
105 the string was not escaped (^ character) per the shell specification.
107 @param[in] SourceString The string to search within
108 @param[in] FindString The string to look for
109 @param[in] CheckForEscapeCharacter TRUE to skip escaped instances of FinfString, otherwise will return even escaped instances
114 IN CONST CHAR16
*SourceString
,
115 IN CONST CHAR16
*FindString
,
116 IN CONST BOOLEAN CheckForEscapeCharacter
120 if (SourceString
== NULL
) {
123 Temp
= StrStr(SourceString
, FindString
);
126 // If nothing found, or we don't care about escape characters
128 if (Temp
== NULL
|| !CheckForEscapeCharacter
) {
133 // If we found an escaped character, try again on the remainder of the string
135 if ((Temp
> (SourceString
)) && *(Temp
-1) == L
'^') {
136 return FindNextInstance(Temp
+1, FindString
, CheckForEscapeCharacter
);
140 // we found the right character
146 Check whether the string between a pair of % is a valid environment variable name.
148 @param[in] BeginPercent pointer to the first percent.
149 @param[in] EndPercent pointer to the last percent.
151 @retval TRUE is a valid environment variable name.
152 @retval FALSE is NOT a valid environment variable name.
155 IsValidEnvironmentVariableName(
156 IN CONST CHAR16
*BeginPercent
,
157 IN CONST CHAR16
*EndPercent
160 CONST CHAR16
*Walker
;
164 ASSERT (BeginPercent
!= NULL
);
165 ASSERT (EndPercent
!= NULL
);
166 ASSERT (BeginPercent
< EndPercent
);
168 if ((BeginPercent
+ 1) == EndPercent
) {
172 for (Walker
= BeginPercent
+ 1; Walker
< EndPercent
; Walker
++) {
174 (*Walker
>= L
'0' && *Walker
<= L
'9') ||
175 (*Walker
>= L
'A' && *Walker
<= L
'Z') ||
176 (*Walker
>= L
'a' && *Walker
<= L
'z') ||
179 if (Walker
== BeginPercent
+ 1 && (*Walker
>= L
'0' && *Walker
<= L
'9')) {
193 Determine if a command line contains a split operation
195 @param[in] CmdLine The command line to parse.
197 @retval TRUE CmdLine has a valid split.
198 @retval FALSE CmdLine does not have a valid split.
203 IN CONST CHAR16
*CmdLine
206 CONST CHAR16
*TempSpot
;
207 CONST CHAR16
*FirstQuote
;
208 CONST CHAR16
*SecondQuote
;
210 FirstQuote
= FindNextInstance (CmdLine
, L
"\"", TRUE
);
212 TempSpot
= FindFirstCharacter(CmdLine
, L
"|", L
'^');
214 if (FirstQuote
== NULL
||
216 TempSpot
== CHAR_NULL
||
217 FirstQuote
> TempSpot
219 return (BOOLEAN
) ((TempSpot
!= NULL
) && (*TempSpot
!= CHAR_NULL
));
222 while ((TempSpot
!= NULL
) && (*TempSpot
!= CHAR_NULL
)) {
223 if (FirstQuote
== NULL
|| FirstQuote
> TempSpot
) {
226 SecondQuote
= FindNextInstance (FirstQuote
+ 1, L
"\"", TRUE
);
227 if (SecondQuote
== NULL
) {
230 if (SecondQuote
< TempSpot
) {
231 FirstQuote
= FindNextInstance (SecondQuote
+ 1, L
"\"", TRUE
);
234 FirstQuote
= FindNextInstance (SecondQuote
+ 1, L
"\"", TRUE
);
235 TempSpot
= FindFirstCharacter(TempSpot
+ 1, L
"|", L
'^');
240 return (BOOLEAN
) ((TempSpot
!= NULL
) && (*TempSpot
!= CHAR_NULL
));
244 Function to start monitoring for CTRL-S using SimpleTextInputEx. This
245 feature's enabled state was not known when the shell initially launched.
247 @retval EFI_SUCCESS The feature is enabled.
248 @retval EFI_OUT_OF_RESOURCES There is not enough memory available.
252 InternalEfiShellStartCtrlSMonitor(
256 EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL
*SimpleEx
;
257 EFI_KEY_DATA KeyData
;
260 Status
= gBS
->OpenProtocol(
261 gST
->ConsoleInHandle
,
262 &gEfiSimpleTextInputExProtocolGuid
,
266 EFI_OPEN_PROTOCOL_GET_PROTOCOL
);
267 if (EFI_ERROR(Status
)) {
272 STRING_TOKEN (STR_SHELL_NO_IN_EX
),
273 ShellInfoObject
.HiiHandle
);
274 return (EFI_SUCCESS
);
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';
282 Status
= SimpleEx
->RegisterKeyNotify(
285 NotificationFunction
,
286 &ShellInfoObject
.CtrlSNotifyHandle1
);
288 KeyData
.KeyState
.KeyShiftState
= EFI_SHIFT_STATE_VALID
|EFI_RIGHT_CONTROL_PRESSED
;
289 if (!EFI_ERROR(Status
)) {
290 Status
= SimpleEx
->RegisterKeyNotify(
293 NotificationFunction
,
294 &ShellInfoObject
.CtrlSNotifyHandle2
);
296 KeyData
.KeyState
.KeyShiftState
= EFI_SHIFT_STATE_VALID
|EFI_LEFT_CONTROL_PRESSED
;
297 KeyData
.Key
.UnicodeChar
= 19;
299 if (!EFI_ERROR(Status
)) {
300 Status
= SimpleEx
->RegisterKeyNotify(
303 NotificationFunction
,
304 &ShellInfoObject
.CtrlSNotifyHandle3
);
306 KeyData
.KeyState
.KeyShiftState
= EFI_SHIFT_STATE_VALID
|EFI_RIGHT_CONTROL_PRESSED
;
307 if (!EFI_ERROR(Status
)) {
308 Status
= SimpleEx
->RegisterKeyNotify(
311 NotificationFunction
,
312 &ShellInfoObject
.CtrlSNotifyHandle4
);
320 The entry point for the application.
322 @param[in] ImageHandle The firmware allocated handle for the EFI image.
323 @param[in] SystemTable A pointer to the EFI System Table.
325 @retval EFI_SUCCESS The entry point is executed successfully.
326 @retval other Some error occurs when executing this entry point.
332 IN EFI_HANDLE ImageHandle
,
333 IN EFI_SYSTEM_TABLE
*SystemTable
339 EFI_HANDLE ConInHandle
;
340 EFI_SIMPLE_TEXT_INPUT_PROTOCOL
*OldConIn
;
342 if (PcdGet8(PcdShellSupportLevel
) > 3) {
343 return (EFI_UNSUPPORTED
);
349 Status
= gST
->ConOut
->ClearScreen(gST
->ConOut
);
350 if (EFI_ERROR(Status
)) {
355 // Populate the global structure from PCDs
357 ShellInfoObject
.ImageDevPath
= NULL
;
358 ShellInfoObject
.FileDevPath
= NULL
;
359 ShellInfoObject
.PageBreakEnabled
= PcdGetBool(PcdShellPageBreakDefault
);
360 ShellInfoObject
.ViewingSettings
.InsertMode
= PcdGetBool(PcdShellInsertModeDefault
);
361 ShellInfoObject
.LogScreenCount
= PcdGet8 (PcdShellScreenLogCount
);
364 // verify we dont allow for spec violation
366 ASSERT(ShellInfoObject
.LogScreenCount
>= 3);
369 // Initialize the LIST ENTRY objects...
371 InitializeListHead(&ShellInfoObject
.BufferToFreeList
.Link
);
372 InitializeListHead(&ShellInfoObject
.ViewingSettings
.CommandHistory
.Link
);
373 InitializeListHead(&ShellInfoObject
.SplitList
.Link
);
376 // Check PCDs for optional features that are not implemented yet.
378 if ( PcdGetBool(PcdShellSupportOldProtocols
)
379 || !FeaturePcdGet(PcdShellRequireHiiPlatform
)
380 || FeaturePcdGet(PcdShellSupportFrameworkHii
)
382 return (EFI_UNSUPPORTED
);
386 // turn off the watchdog timer
388 gBS
->SetWatchdogTimer (0, 0, 0, NULL
);
391 // install our console logger. This will keep a log of the output for back-browsing
393 Status
= ConsoleLoggerInstall(ShellInfoObject
.LogScreenCount
, &ShellInfoObject
.ConsoleInfo
);
394 if (!EFI_ERROR(Status
)) {
396 // Enable the cursor to be visible
398 gST
->ConOut
->EnableCursor (gST
->ConOut
, TRUE
);
401 // If supporting EFI 1.1 we need to install HII protocol
402 // only do this if PcdShellRequireHiiPlatform == FALSE
404 // remove EFI_UNSUPPORTED check above when complete.
405 ///@todo add support for Framework HII
408 // install our (solitary) HII package
410 ShellInfoObject
.HiiHandle
= HiiAddPackages (&gEfiCallerIdGuid
, gImageHandle
, ShellStrings
, NULL
);
411 if (ShellInfoObject
.HiiHandle
== NULL
) {
412 if (PcdGetBool(PcdShellSupportFrameworkHii
)) {
413 ///@todo Add our package into Framework HII
415 if (ShellInfoObject
.HiiHandle
== NULL
) {
416 Status
= EFI_NOT_STARTED
;
422 // create and install the EfiShellParametersProtocol
424 Status
= CreatePopulateInstallShellParametersProtocol(&ShellInfoObject
.NewShellParametersProtocol
, &ShellInfoObject
.RootShellInstance
);
425 ASSERT_EFI_ERROR(Status
);
426 ASSERT(ShellInfoObject
.NewShellParametersProtocol
!= NULL
);
429 // create and install the EfiShellProtocol
431 Status
= CreatePopulateInstallShellProtocol(&ShellInfoObject
.NewEfiShellProtocol
);
432 ASSERT_EFI_ERROR(Status
);
433 ASSERT(ShellInfoObject
.NewEfiShellProtocol
!= NULL
);
436 // Now initialize the shell library (it requires Shell Parameters protocol)
438 Status
= ShellInitialize();
439 ASSERT_EFI_ERROR(Status
);
441 Status
= CommandInit();
442 ASSERT_EFI_ERROR(Status
);
445 // Check the command line
447 Status
= ProcessCommandLine ();
448 if (EFI_ERROR (Status
)) {
453 // If shell support level is >= 1 create the mappings and paths
455 if (PcdGet8(PcdShellSupportLevel
) >= 1) {
456 Status
= ShellCommandCreateInitialMappingsAndPaths();
460 // save the device path for the loaded image and the device path for the filepath (under loaded image)
461 // These are where to look for the startup.nsh file
463 Status
= GetDevicePathsForImageAndFile(&ShellInfoObject
.ImageDevPath
, &ShellInfoObject
.FileDevPath
);
464 ASSERT_EFI_ERROR(Status
);
467 // Display the version
469 if (!ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoVersion
) {
472 gST
->ConOut
->Mode
->CursorRow
,
474 STRING_TOKEN (STR_VER_OUTPUT_MAIN_SHELL
),
475 ShellInfoObject
.HiiHandle
,
476 SupportLevel
[PcdGet8(PcdShellSupportLevel
)],
477 gEfiShellProtocol
->MajorVersion
,
478 gEfiShellProtocol
->MinorVersion
485 STRING_TOKEN (STR_VER_OUTPUT_MAIN_SUPPLIER
),
486 ShellInfoObject
.HiiHandle
,
487 (CHAR16
*) PcdGetPtr (PcdShellSupplier
)
494 STRING_TOKEN (STR_VER_OUTPUT_MAIN_UEFI
),
495 ShellInfoObject
.HiiHandle
,
496 (gST
->Hdr
.Revision
&0xffff0000)>>16,
497 (gST
->Hdr
.Revision
&0x0000ffff),
499 gST
->FirmwareRevision
504 // Display the mapping
506 if (PcdGet8(PcdShellSupportLevel
) >= 2 && !ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoMap
) {
507 Status
= RunCommand(L
"map");
508 ASSERT_EFI_ERROR(Status
);
512 // init all the built in alias'
514 Status
= SetBuiltInAlias();
515 ASSERT_EFI_ERROR(Status
);
518 // Initialize environment variables
520 if (ShellCommandGetProfileList() != NULL
) {
521 Status
= InternalEfiShellSetEnv(L
"profiles", ShellCommandGetProfileList(), TRUE
);
522 ASSERT_EFI_ERROR(Status
);
526 TempString
= AllocateZeroPool(Size
);
528 UnicodeSPrint(TempString
, Size
, L
"%d", PcdGet8(PcdShellSupportLevel
));
529 Status
= InternalEfiShellSetEnv(L
"uefishellsupport", TempString
, TRUE
);
530 ASSERT_EFI_ERROR(Status
);
532 UnicodeSPrint(TempString
, Size
, L
"%d.%d", ShellInfoObject
.NewEfiShellProtocol
->MajorVersion
, ShellInfoObject
.NewEfiShellProtocol
->MinorVersion
);
533 Status
= InternalEfiShellSetEnv(L
"uefishellversion", TempString
, TRUE
);
534 ASSERT_EFI_ERROR(Status
);
536 UnicodeSPrint(TempString
, Size
, L
"%d.%d", (gST
->Hdr
.Revision
& 0xFFFF0000) >> 16, gST
->Hdr
.Revision
& 0x0000FFFF);
537 Status
= InternalEfiShellSetEnv(L
"uefiversion", TempString
, TRUE
);
538 ASSERT_EFI_ERROR(Status
);
540 FreePool(TempString
);
542 if (!EFI_ERROR(Status
)) {
543 if (!ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoInterrupt
) {
545 // Set up the event for CTRL-C monitoring...
547 Status
= InernalEfiShellStartMonitor();
550 if (!EFI_ERROR(Status
) && !ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoConsoleIn
) {
552 // Set up the event for CTRL-S monitoring...
554 Status
= InternalEfiShellStartCtrlSMonitor();
557 if (!EFI_ERROR(Status
) && ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoConsoleIn
) {
559 // close off the gST->ConIn
561 OldConIn
= gST
->ConIn
;
562 ConInHandle
= gST
->ConsoleInHandle
;
563 gST
->ConIn
= CreateSimpleTextInOnFile((SHELL_FILE_HANDLE
)&FileInterfaceNulFile
, &gST
->ConsoleInHandle
);
569 if (!EFI_ERROR(Status
) && PcdGet8(PcdShellSupportLevel
) >= 1) {
571 // process the startup script or launch the called app.
573 Status
= DoStartupScript(ShellInfoObject
.ImageDevPath
, ShellInfoObject
.FileDevPath
);
576 if (!ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.Exit
&& !ShellCommandGetExit() && (PcdGet8(PcdShellSupportLevel
) >= 3 || PcdGetBool(PcdShellForceConsole
)) && !EFI_ERROR(Status
) && !ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoConsoleIn
) {
578 // begin the UI waiting loop
582 // clean out all the memory allocated for CONST <something> * return values
583 // between each shell prompt presentation
585 if (!IsListEmpty(&ShellInfoObject
.BufferToFreeList
.Link
)){
586 FreeBufferList(&ShellInfoObject
.BufferToFreeList
);
590 // Reset page break back to default.
592 ShellInfoObject
.PageBreakEnabled
= PcdGetBool(PcdShellPageBreakDefault
);
593 ASSERT (ShellInfoObject
.ConsoleInfo
!= NULL
);
594 ShellInfoObject
.ConsoleInfo
->Enabled
= TRUE
;
595 ShellInfoObject
.ConsoleInfo
->RowCounter
= 0;
598 // Reset the CTRL-C event (yes we ignore the return values)
600 Status
= gBS
->CheckEvent (ShellInfoObject
.NewEfiShellProtocol
->ExecutionBreak
);
605 Status
= DoShellPrompt();
606 } while (!ShellCommandGetExit());
608 if (OldConIn
!= NULL
&& ConInHandle
!= NULL
) {
609 CloseSimpleTextInOnFile (gST
->ConIn
);
610 gST
->ConIn
= OldConIn
;
611 gST
->ConsoleInHandle
= ConInHandle
;
618 // uninstall protocols / free memory / etc...
620 if (ShellInfoObject
.UserBreakTimer
!= NULL
) {
621 gBS
->CloseEvent(ShellInfoObject
.UserBreakTimer
);
622 DEBUG_CODE(ShellInfoObject
.UserBreakTimer
= NULL
;);
624 if (ShellInfoObject
.ImageDevPath
!= NULL
) {
625 FreePool(ShellInfoObject
.ImageDevPath
);
626 DEBUG_CODE(ShellInfoObject
.ImageDevPath
= NULL
;);
628 if (ShellInfoObject
.FileDevPath
!= NULL
) {
629 FreePool(ShellInfoObject
.FileDevPath
);
630 DEBUG_CODE(ShellInfoObject
.FileDevPath
= NULL
;);
632 if (ShellInfoObject
.NewShellParametersProtocol
!= NULL
) {
633 CleanUpShellParametersProtocol(ShellInfoObject
.NewShellParametersProtocol
);
634 DEBUG_CODE(ShellInfoObject
.NewShellParametersProtocol
= NULL
;);
636 if (ShellInfoObject
.NewEfiShellProtocol
!= NULL
){
637 if (ShellInfoObject
.NewEfiShellProtocol
->IsRootShell()){
638 InternalEfiShellSetEnv(L
"cwd", NULL
, TRUE
);
640 CleanUpShellProtocol(ShellInfoObject
.NewEfiShellProtocol
);
641 DEBUG_CODE(ShellInfoObject
.NewEfiShellProtocol
= NULL
;);
644 if (!IsListEmpty(&ShellInfoObject
.BufferToFreeList
.Link
)){
645 FreeBufferList(&ShellInfoObject
.BufferToFreeList
);
648 if (!IsListEmpty(&ShellInfoObject
.SplitList
.Link
)){
649 ASSERT(FALSE
); ///@todo finish this de-allocation.
652 if (ShellInfoObject
.ShellInitSettings
.FileName
!= NULL
) {
653 FreePool(ShellInfoObject
.ShellInitSettings
.FileName
);
654 DEBUG_CODE(ShellInfoObject
.ShellInitSettings
.FileName
= NULL
;);
657 if (ShellInfoObject
.ShellInitSettings
.FileOptions
!= NULL
) {
658 FreePool(ShellInfoObject
.ShellInitSettings
.FileOptions
);
659 DEBUG_CODE(ShellInfoObject
.ShellInitSettings
.FileOptions
= NULL
;);
662 if (ShellInfoObject
.HiiHandle
!= NULL
) {
663 HiiRemovePackages(ShellInfoObject
.HiiHandle
);
664 DEBUG_CODE(ShellInfoObject
.HiiHandle
= NULL
;);
667 if (!IsListEmpty(&ShellInfoObject
.ViewingSettings
.CommandHistory
.Link
)){
668 FreeBufferList(&ShellInfoObject
.ViewingSettings
.CommandHistory
);
671 ASSERT(ShellInfoObject
.ConsoleInfo
!= NULL
);
672 if (ShellInfoObject
.ConsoleInfo
!= NULL
) {
673 ConsoleLoggerUninstall(ShellInfoObject
.ConsoleInfo
);
674 FreePool(ShellInfoObject
.ConsoleInfo
);
675 DEBUG_CODE(ShellInfoObject
.ConsoleInfo
= NULL
;);
678 if (ShellCommandGetExit()) {
679 return ((EFI_STATUS
)ShellCommandGetExitCode());
685 Sets all the alias' that were registered with the ShellCommandLib library.
687 @retval EFI_SUCCESS all init commands were run successfully.
695 CONST ALIAS_LIST
*List
;
699 // Get all the commands we want to run
701 List
= ShellCommandGetInitAliasList();
704 // for each command in the List
706 for ( Node
= (ALIAS_LIST
*)GetFirstNode(&List
->Link
)
707 ; !IsNull (&List
->Link
, &Node
->Link
)
708 ; Node
= (ALIAS_LIST
*)GetNextNode(&List
->Link
, &Node
->Link
)
711 // install the alias'
713 Status
= InternalSetAlias(Node
->CommandString
, Node
->Alias
, TRUE
);
714 ASSERT_EFI_ERROR(Status
);
716 return (EFI_SUCCESS
);
720 Internal function to determine if 2 command names are really the same.
722 @param[in] Command1 The pointer to the first command name.
723 @param[in] Command2 The pointer to the second command name.
725 @retval TRUE The 2 command names are the same.
726 @retval FALSE The 2 command names are not the same.
731 IN CONST CHAR16
*Command1
,
732 IN CONST CHAR16
*Command2
735 if (StringNoCaseCompare(&Command1
, &Command2
) == 0) {
742 Internal function to determine if a command is a script only command.
744 @param[in] CommandName The pointer to the command name.
746 @retval TRUE The command is a script only command.
747 @retval FALSE The command is not a script only command.
752 IN CONST CHAR16
*CommandName
755 if (IsCommand(CommandName
, L
"for")
756 ||IsCommand(CommandName
, L
"endfor")
757 ||IsCommand(CommandName
, L
"if")
758 ||IsCommand(CommandName
, L
"else")
759 ||IsCommand(CommandName
, L
"endif")
760 ||IsCommand(CommandName
, L
"goto")) {
767 This function will populate the 2 device path protocol parameters based on the
768 global gImageHandle. The DevPath will point to the device path for the handle that has
769 loaded image protocol installed on it. The FilePath will point to the device path
770 for the file that was loaded.
772 @param[in, out] DevPath On a successful return the device path to the loaded image.
773 @param[in, out] FilePath On a successful return the device path to the file.
775 @retval EFI_SUCCESS The 2 device paths were successfully returned.
776 @retval other A error from gBS->HandleProtocol.
782 GetDevicePathsForImageAndFile (
783 IN OUT EFI_DEVICE_PATH_PROTOCOL
**DevPath
,
784 IN OUT EFI_DEVICE_PATH_PROTOCOL
**FilePath
788 EFI_LOADED_IMAGE_PROTOCOL
*LoadedImage
;
789 EFI_DEVICE_PATH_PROTOCOL
*ImageDevicePath
;
791 ASSERT(DevPath
!= NULL
);
792 ASSERT(FilePath
!= NULL
);
794 Status
= gBS
->OpenProtocol (
796 &gEfiLoadedImageProtocolGuid
,
797 (VOID
**)&LoadedImage
,
800 EFI_OPEN_PROTOCOL_GET_PROTOCOL
802 if (!EFI_ERROR (Status
)) {
803 Status
= gBS
->OpenProtocol (
804 LoadedImage
->DeviceHandle
,
805 &gEfiDevicePathProtocolGuid
,
806 (VOID
**)&ImageDevicePath
,
809 EFI_OPEN_PROTOCOL_GET_PROTOCOL
811 if (!EFI_ERROR (Status
)) {
812 *DevPath
= DuplicateDevicePath (ImageDevicePath
);
813 *FilePath
= DuplicateDevicePath (LoadedImage
->FilePath
);
815 LoadedImage
->DeviceHandle
,
816 &gEfiDevicePathProtocolGuid
,
822 &gEfiLoadedImageProtocolGuid
,
830 Process all Uefi Shell 2.0 command line options.
832 see Uefi Shell 2.0 section 3.2 for full details.
834 the command line must resemble the following:
836 shell.efi [ShellOpt-options] [options] [file-name [file-name-options]]
838 ShellOpt-options Options which control the initialization behavior of the shell.
839 These options are read from the EFI global variable "ShellOpt"
840 and are processed before options or file-name.
842 options Options which control the initialization behavior of the shell.
844 file-name The name of a UEFI shell application or script to be executed
845 after initialization is complete. By default, if file-name is
846 specified, then -nostartup is implied. Scripts are not supported
849 file-name-options The command-line options that are passed to file-name when it
852 This will initialize the ShellInfoObject.ShellInitSettings global variable.
854 @retval EFI_SUCCESS The variable is initialized.
865 CHAR16
*DelayValueStr
;
868 EFI_UNICODE_COLLATION_PROTOCOL
*UnicodeCollation
;
870 // `file-name-options` will contain arguments to `file-name` that we don't
871 // know about. This would cause ShellCommandLineParse to error, so we parse
872 // arguments manually, ignoring those after the first thing that doesn't look
873 // like a shell option (which is assumed to be `file-name`).
875 Status
= gBS
->LocateProtocol (
876 &gEfiUnicodeCollationProtocolGuid
,
878 (VOID
**) &UnicodeCollation
880 if (EFI_ERROR (Status
)) {
884 // Set default options
885 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.Startup
= FALSE
;
886 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoStartup
= FALSE
;
887 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoConsoleOut
= FALSE
;
888 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoConsoleIn
= FALSE
;
889 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoInterrupt
= FALSE
;
890 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoMap
= FALSE
;
891 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoVersion
= FALSE
;
892 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.Delay
= FALSE
;
893 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.Exit
= FALSE
;
894 ShellInfoObject
.ShellInitSettings
.Delay
= 5;
897 // Start LoopVar at 0 to parse only optional arguments at Argv[0]
898 // and parse other parameters from Argv[1]. This is for use case that
899 // UEFI Shell boot option is created, and OptionalData is provided
900 // that starts with shell command-line options.
902 for (LoopVar
= 0 ; LoopVar
< gEfiShellParametersProtocol
->Argc
; LoopVar
++) {
903 CurrentArg
= gEfiShellParametersProtocol
->Argv
[LoopVar
];
904 if (UnicodeCollation
->StriColl (
909 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.Startup
= TRUE
;
911 else if (UnicodeCollation
->StriColl (
916 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoStartup
= TRUE
;
918 else if (UnicodeCollation
->StriColl (
923 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoConsoleOut
= TRUE
;
925 else if (UnicodeCollation
->StriColl (
930 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoConsoleIn
= TRUE
;
932 else if (UnicodeCollation
->StriColl (
937 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoInterrupt
= TRUE
;
939 else if (UnicodeCollation
->StriColl (
944 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoMap
= TRUE
;
946 else if (UnicodeCollation
->StriColl (
951 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoVersion
= TRUE
;
953 else if (UnicodeCollation
->StriColl (
958 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.Delay
= TRUE
;
959 // Check for optional delay value following "-delay"
960 DelayValueStr
= gEfiShellParametersProtocol
->Argv
[LoopVar
+ 1];
961 if (DelayValueStr
!= NULL
){
962 if (*DelayValueStr
== L
':') {
965 if (!EFI_ERROR(ShellConvertStringToUint64 (
971 ShellInfoObject
.ShellInitSettings
.Delay
= (UINTN
)DelayValue
;
975 } else if (UnicodeCollation
->StriColl (
980 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.Exit
= TRUE
;
981 } else if (StrnCmp (L
"-", CurrentArg
, 1) == 0) {
982 // Unrecognized option
983 ShellPrintHiiEx(-1, -1, NULL
,
984 STRING_TOKEN (STR_GEN_PROBLEM
),
985 ShellInfoObject
.HiiHandle
,
988 return EFI_INVALID_PARAMETER
;
991 // First argument should be Shell.efi image name
997 ShellInfoObject
.ShellInitSettings
.FileName
= AllocateCopyPool(StrSize(CurrentArg
), CurrentArg
);
998 if (ShellInfoObject
.ShellInitSettings
.FileName
== NULL
) {
999 return (EFI_OUT_OF_RESOURCES
);
1002 // We found `file-name`.
1004 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoStartup
= 1;
1007 // Add `file-name-options`
1008 for (Size
= 0 ; LoopVar
< gEfiShellParametersProtocol
->Argc
; LoopVar
++) {
1009 ASSERT((ShellInfoObject
.ShellInitSettings
.FileOptions
== NULL
&& Size
== 0) || (ShellInfoObject
.ShellInitSettings
.FileOptions
!= NULL
));
1010 StrnCatGrow(&ShellInfoObject
.ShellInitSettings
.FileOptions
,
1014 if (ShellInfoObject
.ShellInitSettings
.FileOptions
== NULL
) {
1015 SHELL_FREE_NON_NULL(ShellInfoObject
.ShellInitSettings
.FileName
);
1016 return (EFI_OUT_OF_RESOURCES
);
1018 StrnCatGrow(&ShellInfoObject
.ShellInitSettings
.FileOptions
,
1020 gEfiShellParametersProtocol
->Argv
[LoopVar
],
1022 if (ShellInfoObject
.ShellInitSettings
.FileOptions
== NULL
) {
1023 SHELL_FREE_NON_NULL(ShellInfoObject
.ShellInitSettings
.FileName
);
1024 return (EFI_OUT_OF_RESOURCES
);
1030 // "-nointerrupt" overrides "-delay"
1031 if (ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoInterrupt
) {
1032 ShellInfoObject
.ShellInitSettings
.Delay
= 0;
1039 Handles all interaction with the default startup script.
1041 this will check that the correct command line parameters were passed, handle the delay, and then start running the script.
1043 @param ImagePath the path to the image for shell. first place to look for the startup script
1044 @param FilePath the path to the file for shell. second place to look for the startup script.
1046 @retval EFI_SUCCESS the variable is initialized.
1051 IN EFI_DEVICE_PATH_PROTOCOL
*ImagePath
,
1052 IN EFI_DEVICE_PATH_PROTOCOL
*FilePath
1056 EFI_STATUS CalleeStatus
;
1059 SHELL_FILE_HANDLE FileHandle
;
1060 EFI_DEVICE_PATH_PROTOCOL
*NewPath
;
1061 EFI_DEVICE_PATH_PROTOCOL
*NamePath
;
1062 CHAR16
*FileStringPath
;
1065 CONST CHAR16
*MapName
;
1067 Key
.UnicodeChar
= CHAR_NULL
;
1071 if (!ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.Startup
&& ShellInfoObject
.ShellInitSettings
.FileName
!= NULL
) {
1073 // launch something else instead
1075 NewSize
= StrSize(ShellInfoObject
.ShellInitSettings
.FileName
);
1076 if (ShellInfoObject
.ShellInitSettings
.FileOptions
!= NULL
) {
1077 NewSize
+= StrSize(ShellInfoObject
.ShellInitSettings
.FileOptions
) + sizeof(CHAR16
);
1079 FileStringPath
= AllocateZeroPool(NewSize
);
1080 if (FileStringPath
== NULL
) {
1081 return (EFI_OUT_OF_RESOURCES
);
1083 StrCpyS(FileStringPath
, NewSize
/sizeof(CHAR16
), ShellInfoObject
.ShellInitSettings
.FileName
);
1084 if (ShellInfoObject
.ShellInitSettings
.FileOptions
!= NULL
) {
1085 StrnCatS(FileStringPath
, NewSize
/sizeof(CHAR16
), L
" ", NewSize
/sizeof(CHAR16
) - StrLen(FileStringPath
) -1);
1086 StrnCatS(FileStringPath
, NewSize
/sizeof(CHAR16
), ShellInfoObject
.ShellInitSettings
.FileOptions
, NewSize
/sizeof(CHAR16
) - StrLen(FileStringPath
) -1);
1088 Status
= RunShellCommand(FileStringPath
, &CalleeStatus
);
1089 if (ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.Exit
== TRUE
) {
1090 ShellCommandRegisterExit(gEfiShellProtocol
->BatchIsActive(), (UINT64
)CalleeStatus
);
1092 FreePool(FileStringPath
);
1098 // for shell level 0 we do no scripts
1099 // Without the Startup bit overriding we allow for nostartup to prevent scripts
1101 if ( (PcdGet8(PcdShellSupportLevel
) < 1)
1102 || (ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoStartup
&& !ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.Startup
)
1104 return (EFI_SUCCESS
);
1107 gST
->ConOut
->EnableCursor(gST
->ConOut
, FALSE
);
1109 // print out our warning and see if they press a key
1111 for ( Status
= EFI_UNSUPPORTED
, Delay
= ShellInfoObject
.ShellInitSettings
.Delay
1112 ; Delay
!= 0 && EFI_ERROR(Status
)
1115 ShellPrintHiiEx(0, gST
->ConOut
->Mode
->CursorRow
, NULL
, STRING_TOKEN (STR_SHELL_STARTUP_QUESTION
), ShellInfoObject
.HiiHandle
, Delay
);
1116 gBS
->Stall (1000000);
1117 if (!ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoConsoleIn
) {
1118 Status
= gST
->ConIn
->ReadKeyStroke (gST
->ConIn
, &Key
);
1121 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_CRLF
), ShellInfoObject
.HiiHandle
);
1122 gST
->ConOut
->EnableCursor(gST
->ConOut
, TRUE
);
1127 if (Status
== EFI_SUCCESS
&& Key
.UnicodeChar
== 0 && Key
.ScanCode
== SCAN_ESC
) {
1128 return (EFI_SUCCESS
);
1132 // Try the first location (must be file system)
1134 MapName
= ShellInfoObject
.NewEfiShellProtocol
->GetMapFromDevicePath(&ImagePath
);
1135 if (MapName
!= NULL
) {
1136 FileStringPath
= NULL
;
1138 FileStringPath
= StrnCatGrow(&FileStringPath
, &NewSize
, MapName
, 0);
1139 if (FileStringPath
== NULL
) {
1140 Status
= EFI_OUT_OF_RESOURCES
;
1142 TempSpot
= StrStr(FileStringPath
, L
";");
1143 if (TempSpot
!= NULL
) {
1144 *TempSpot
= CHAR_NULL
;
1146 FileStringPath
= StrnCatGrow(&FileStringPath
, &NewSize
, ((FILEPATH_DEVICE_PATH
*)FilePath
)->PathName
, 0);
1147 PathRemoveLastItem(FileStringPath
);
1148 FileStringPath
= StrnCatGrow(&FileStringPath
, &NewSize
, mStartupScript
, 0);
1149 Status
= ShellInfoObject
.NewEfiShellProtocol
->OpenFileByName(FileStringPath
, &FileHandle
, EFI_FILE_MODE_READ
);
1150 FreePool(FileStringPath
);
1153 if (EFI_ERROR(Status
)) {
1154 NamePath
= FileDevicePath (NULL
, mStartupScript
);
1155 NewPath
= AppendDevicePathNode (ImagePath
, NamePath
);
1161 Status
= InternalOpenFileDevicePath(NewPath
, &FileHandle
, EFI_FILE_MODE_READ
, 0);
1165 // If we got a file, run it
1167 if (!EFI_ERROR(Status
) && FileHandle
!= NULL
) {
1168 Status
= RunScriptFile (mStartupScript
, FileHandle
, L
"", ShellInfoObject
.NewShellParametersProtocol
);
1169 ShellInfoObject
.NewEfiShellProtocol
->CloseFile(FileHandle
);
1171 FileStringPath
= ShellFindFilePath(mStartupScript
);
1172 if (FileStringPath
== NULL
) {
1174 // we return success since we don't need to have a startup script
1176 Status
= EFI_SUCCESS
;
1177 ASSERT(FileHandle
== NULL
);
1179 Status
= RunScriptFile(FileStringPath
, NULL
, L
"", ShellInfoObject
.NewShellParametersProtocol
);
1180 FreePool(FileStringPath
);
1189 Function to perform the shell prompt looping. It will do a single prompt,
1190 dispatch the result, and then return. It is expected that the caller will
1191 call this function in a loop many times.
1194 @retval RETURN_ABORTED
1205 CONST CHAR16
*CurDir
;
1212 // Get screen setting to decide size of the command line buffer
1214 gST
->ConOut
->QueryMode (gST
->ConOut
, gST
->ConOut
->Mode
->Mode
, &Column
, &Row
);
1215 BufferSize
= Column
* Row
* sizeof (CHAR16
);
1216 CmdLine
= AllocateZeroPool (BufferSize
);
1217 if (CmdLine
== NULL
) {
1218 return EFI_OUT_OF_RESOURCES
;
1221 CurDir
= ShellInfoObject
.NewEfiShellProtocol
->GetEnv(L
"cwd");
1226 gST
->ConOut
->SetCursorPosition (gST
->ConOut
, 0, gST
->ConOut
->Mode
->CursorRow
);
1228 if (CurDir
!= NULL
&& StrLen(CurDir
) > 1) {
1229 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_CURDIR
), ShellInfoObject
.HiiHandle
, CurDir
);
1231 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_SHELL
), ShellInfoObject
.HiiHandle
);
1235 // Read a line from the console
1237 Status
= ShellInfoObject
.NewEfiShellProtocol
->ReadFile(ShellInfoObject
.NewShellParametersProtocol
->StdIn
, &BufferSize
, CmdLine
);
1240 // Null terminate the string and parse it
1242 if (!EFI_ERROR (Status
)) {
1243 CmdLine
[BufferSize
/ sizeof (CHAR16
)] = CHAR_NULL
;
1244 Status
= RunCommand(CmdLine
);
1248 // Done with this command
1255 Add a buffer to the Buffer To Free List for safely returning buffers to other
1256 places without risking letting them modify internal shell information.
1258 @param Buffer Something to pass to FreePool when the shell is exiting.
1262 AddBufferToFreeList(
1266 BUFFER_LIST
*BufferListEntry
;
1268 if (Buffer
== NULL
) {
1272 BufferListEntry
= AllocateZeroPool(sizeof(BUFFER_LIST
));
1273 ASSERT(BufferListEntry
!= NULL
);
1274 BufferListEntry
->Buffer
= Buffer
;
1275 InsertTailList(&ShellInfoObject
.BufferToFreeList
.Link
, &BufferListEntry
->Link
);
1281 Create a new buffer list and stores the old one to OldBufferList
1283 @param OldBufferList The temporary list head used to store the nodes in BufferToFreeList.
1287 OUT LIST_ENTRY
*OldBufferList
1290 CopyMem (OldBufferList
, &ShellInfoObject
.BufferToFreeList
.Link
, sizeof (LIST_ENTRY
));
1291 InitializeListHead (&ShellInfoObject
.BufferToFreeList
.Link
);
1295 Restore previous nodes into BufferToFreeList .
1297 @param OldBufferList The temporary list head used to store the nodes in BufferToFreeList.
1301 IN OUT LIST_ENTRY
*OldBufferList
1304 FreeBufferList (&ShellInfoObject
.BufferToFreeList
);
1305 CopyMem (&ShellInfoObject
.BufferToFreeList
.Link
, OldBufferList
, sizeof (LIST_ENTRY
));
1310 Add a buffer to the Line History List
1312 @param Buffer The line buffer to add.
1316 AddLineToCommandHistory(
1317 IN CONST CHAR16
*Buffer
1321 BUFFER_LIST
*Walker
;
1322 UINT16 MaxHistoryCmdCount
;
1326 MaxHistoryCmdCount
= PcdGet16(PcdShellMaxHistoryCommandCount
);
1328 if (MaxHistoryCmdCount
== 0) {
1333 Node
= AllocateZeroPool(sizeof(BUFFER_LIST
));
1334 ASSERT(Node
!= NULL
);
1335 Node
->Buffer
= AllocateCopyPool(StrSize(Buffer
), Buffer
);
1336 ASSERT(Node
->Buffer
!= NULL
);
1338 for ( Walker
= (BUFFER_LIST
*)GetFirstNode(&ShellInfoObject
.ViewingSettings
.CommandHistory
.Link
)
1339 ; !IsNull(&ShellInfoObject
.ViewingSettings
.CommandHistory
.Link
, &Walker
->Link
)
1340 ; Walker
= (BUFFER_LIST
*)GetNextNode(&ShellInfoObject
.ViewingSettings
.CommandHistory
.Link
, &Walker
->Link
)
1344 if (Count
< MaxHistoryCmdCount
){
1345 InsertTailList(&ShellInfoObject
.ViewingSettings
.CommandHistory
.Link
, &Node
->Link
);
1347 Walker
= (BUFFER_LIST
*)GetFirstNode(&ShellInfoObject
.ViewingSettings
.CommandHistory
.Link
);
1348 RemoveEntryList(&Walker
->Link
);
1349 if (Walker
->Buffer
!= NULL
) {
1350 FreePool(Walker
->Buffer
);
1353 InsertTailList(&ShellInfoObject
.ViewingSettings
.CommandHistory
.Link
, &Node
->Link
);
1358 Checks if a string is an alias for another command. If yes, then it replaces the alias name
1359 with the correct command name.
1361 @param[in, out] CommandString Upon entry the potential alias. Upon return the
1362 command name if it was an alias. If it was not
1363 an alias it will be unchanged. This function may
1364 change the buffer to fit the command name.
1366 @retval EFI_SUCCESS The name was changed.
1367 @retval EFI_SUCCESS The name was not an alias.
1368 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
1373 IN OUT CHAR16
**CommandString
1376 CONST CHAR16
*NewString
;
1378 NewString
= ShellInfoObject
.NewEfiShellProtocol
->GetAlias(*CommandString
, NULL
);
1379 if (NewString
== NULL
) {
1380 return (EFI_SUCCESS
);
1382 FreePool(*CommandString
);
1383 *CommandString
= AllocateCopyPool(StrSize(NewString
), NewString
);
1384 if (*CommandString
== NULL
) {
1385 return (EFI_OUT_OF_RESOURCES
);
1387 return (EFI_SUCCESS
);
1391 This function will eliminate unreplaced (and therefore non-found) environment variables.
1393 @param[in,out] CmdLine The command line to update.
1397 StripUnreplacedEnvironmentVariables(
1398 IN OUT CHAR16
*CmdLine
1401 CHAR16
*FirstPercent
;
1403 CHAR16
*SecondPercent
;
1404 CHAR16
*SecondQuote
;
1405 CHAR16
*CurrentLocator
;
1407 for (CurrentLocator
= CmdLine
; CurrentLocator
!= NULL
; ) {
1408 FirstQuote
= FindNextInstance(CurrentLocator
, L
"\"", TRUE
);
1409 FirstPercent
= FindNextInstance(CurrentLocator
, L
"%", TRUE
);
1410 SecondPercent
= FirstPercent
!=NULL
?FindNextInstance(FirstPercent
+1, L
"%", TRUE
):NULL
;
1411 if (FirstPercent
== NULL
|| SecondPercent
== NULL
) {
1413 // If we ever don't have 2 % we are done.
1418 if (FirstQuote
!= NULL
&& FirstQuote
< FirstPercent
) {
1419 SecondQuote
= FindNextInstance(FirstQuote
+1, L
"\"", TRUE
);
1421 // Quote is first found
1424 if (SecondQuote
< FirstPercent
) {
1426 // restart after the pair of "
1428 CurrentLocator
= SecondQuote
+ 1;
1429 } else /* FirstPercent < SecondQuote */{
1431 // Restart on the first percent
1433 CurrentLocator
= FirstPercent
;
1438 if (FirstQuote
== NULL
|| SecondPercent
< FirstQuote
) {
1439 if (IsValidEnvironmentVariableName(FirstPercent
, SecondPercent
)) {
1441 // We need to remove from FirstPercent to SecondPercent
1443 CopyMem(FirstPercent
, SecondPercent
+ 1, StrSize(SecondPercent
+ 1));
1445 // don't need to update the locator. both % characters are gone.
1448 CurrentLocator
= SecondPercent
+ 1;
1452 CurrentLocator
= FirstQuote
;
1454 return (EFI_SUCCESS
);
1458 Function allocates a new command line and replaces all instances of environment
1459 variable names that are correctly preset to their values.
1461 If the return value is not NULL the memory must be caller freed.
1463 @param[in] OriginalCommandLine The original command line
1465 @retval NULL An error occurred.
1466 @return The new command line with no environment variables present.
1470 ShellConvertVariables (
1471 IN CONST CHAR16
*OriginalCommandLine
1474 CONST CHAR16
*MasterEnvList
;
1476 CHAR16
*NewCommandLine1
;
1477 CHAR16
*NewCommandLine2
;
1481 SCRIPT_FILE
*CurrentScriptFile
;
1482 ALIAS_LIST
*AliasListNode
;
1484 ASSERT(OriginalCommandLine
!= NULL
);
1487 NewSize
= StrSize(OriginalCommandLine
);
1488 CurrentScriptFile
= ShellCommandGetCurrentScriptFile();
1491 ///@todo update this to handle the %0 - %9 for scripting only (borrow from line 1256 area) ? ? ?
1494 // calculate the size required for the post-conversion string...
1496 if (CurrentScriptFile
!= NULL
) {
1497 for (AliasListNode
= (ALIAS_LIST
*)GetFirstNode(&CurrentScriptFile
->SubstList
)
1498 ; !IsNull(&CurrentScriptFile
->SubstList
, &AliasListNode
->Link
)
1499 ; AliasListNode
= (ALIAS_LIST
*)GetNextNode(&CurrentScriptFile
->SubstList
, &AliasListNode
->Link
)
1501 for (Temp
= StrStr(OriginalCommandLine
, AliasListNode
->Alias
)
1503 ; Temp
= StrStr(Temp
+1, AliasListNode
->Alias
)
1506 // we need a preceding and if there is space no ^ preceding (if no space ignore)
1508 if ((((Temp
-OriginalCommandLine
)>2) && *(Temp
-2) != L
'^') || ((Temp
-OriginalCommandLine
)<=2)) {
1509 NewSize
+= StrSize(AliasListNode
->CommandString
);
1515 for (MasterEnvList
= EfiShellGetEnv(NULL
)
1516 ; MasterEnvList
!= NULL
&& *MasterEnvList
!= CHAR_NULL
//&& *(MasterEnvList+1) != CHAR_NULL
1517 ; MasterEnvList
+= StrLen(MasterEnvList
) + 1
1519 if (StrSize(MasterEnvList
) > ItemSize
) {
1520 ItemSize
= StrSize(MasterEnvList
);
1522 for (Temp
= StrStr(OriginalCommandLine
, MasterEnvList
)
1524 ; Temp
= StrStr(Temp
+1, MasterEnvList
)
1527 // we need a preceding and following % and if there is space no ^ preceding (if no space ignore)
1529 if (*(Temp
-1) == L
'%' && *(Temp
+StrLen(MasterEnvList
)) == L
'%' &&
1530 ((((Temp
-OriginalCommandLine
)>2) && *(Temp
-2) != L
'^') || ((Temp
-OriginalCommandLine
)<=2))) {
1531 NewSize
+=StrSize(EfiShellGetEnv(MasterEnvList
));
1537 // now do the replacements...
1539 NewCommandLine1
= AllocateCopyPool(NewSize
, OriginalCommandLine
);
1540 NewCommandLine2
= AllocateZeroPool(NewSize
);
1541 ItemTemp
= AllocateZeroPool(ItemSize
+(2*sizeof(CHAR16
)));
1542 if (NewCommandLine1
== NULL
|| NewCommandLine2
== NULL
|| ItemTemp
== NULL
) {
1543 SHELL_FREE_NON_NULL(NewCommandLine1
);
1544 SHELL_FREE_NON_NULL(NewCommandLine2
);
1545 SHELL_FREE_NON_NULL(ItemTemp
);
1548 for (MasterEnvList
= EfiShellGetEnv(NULL
)
1549 ; MasterEnvList
!= NULL
&& *MasterEnvList
!= CHAR_NULL
1550 ; MasterEnvList
+= StrLen(MasterEnvList
) + 1
1553 ((ItemSize
+(2*sizeof(CHAR16
)))/sizeof(CHAR16
)),
1557 ((ItemSize
+(2*sizeof(CHAR16
)))/sizeof(CHAR16
)),
1561 ((ItemSize
+(2*sizeof(CHAR16
)))/sizeof(CHAR16
)),
1564 ShellCopySearchAndReplace(NewCommandLine1
, NewCommandLine2
, NewSize
, ItemTemp
, EfiShellGetEnv(MasterEnvList
), TRUE
, FALSE
);
1565 StrCpyS(NewCommandLine1
, NewSize
/sizeof(CHAR16
), NewCommandLine2
);
1567 if (CurrentScriptFile
!= NULL
) {
1568 for (AliasListNode
= (ALIAS_LIST
*)GetFirstNode(&CurrentScriptFile
->SubstList
)
1569 ; !IsNull(&CurrentScriptFile
->SubstList
, &AliasListNode
->Link
)
1570 ; AliasListNode
= (ALIAS_LIST
*)GetNextNode(&CurrentScriptFile
->SubstList
, &AliasListNode
->Link
)
1572 ShellCopySearchAndReplace(NewCommandLine1
, NewCommandLine2
, NewSize
, AliasListNode
->Alias
, AliasListNode
->CommandString
, TRUE
, FALSE
);
1573 StrCpyS(NewCommandLine1
, NewSize
/sizeof(CHAR16
), NewCommandLine2
);
1578 // Remove non-existent environment variables
1580 StripUnreplacedEnvironmentVariables(NewCommandLine1
);
1583 // Now cleanup any straggler intentionally ignored "%" characters
1585 ShellCopySearchAndReplace(NewCommandLine1
, NewCommandLine2
, NewSize
, L
"^%", L
"%", TRUE
, FALSE
);
1586 StrCpyS(NewCommandLine1
, NewSize
/sizeof(CHAR16
), NewCommandLine2
);
1588 FreePool(NewCommandLine2
);
1591 return (NewCommandLine1
);
1595 Internal function to run a command line with pipe usage.
1597 @param[in] CmdLine The pointer to the command line.
1598 @param[in] StdIn The pointer to the Standard input.
1599 @param[in] StdOut The pointer to the Standard output.
1601 @retval EFI_SUCCESS The split command is executed successfully.
1602 @retval other Some error occurs when executing the split command.
1607 IN CONST CHAR16
*CmdLine
,
1608 IN SHELL_FILE_HANDLE
*StdIn
,
1609 IN SHELL_FILE_HANDLE
*StdOut
1613 CHAR16
*NextCommandLine
;
1614 CHAR16
*OurCommandLine
;
1618 SHELL_FILE_HANDLE
*TempFileHandle
;
1621 ASSERT(StdOut
== NULL
);
1623 ASSERT(StrStr(CmdLine
, L
"|") != NULL
);
1625 Status
= EFI_SUCCESS
;
1626 NextCommandLine
= NULL
;
1627 OurCommandLine
= NULL
;
1631 NextCommandLine
= StrnCatGrow(&NextCommandLine
, &Size1
, StrStr(CmdLine
, L
"|")+1, 0);
1632 OurCommandLine
= StrnCatGrow(&OurCommandLine
, &Size2
, CmdLine
, StrStr(CmdLine
, L
"|") - CmdLine
);
1634 if (NextCommandLine
== NULL
|| OurCommandLine
== NULL
) {
1635 SHELL_FREE_NON_NULL(OurCommandLine
);
1636 SHELL_FREE_NON_NULL(NextCommandLine
);
1637 return (EFI_OUT_OF_RESOURCES
);
1638 } else if (StrStr(OurCommandLine
, L
"|") != NULL
|| Size1
== 0 || Size2
== 0) {
1639 SHELL_FREE_NON_NULL(OurCommandLine
);
1640 SHELL_FREE_NON_NULL(NextCommandLine
);
1641 return (EFI_INVALID_PARAMETER
);
1642 } else if (NextCommandLine
[0] == L
'a' &&
1643 (NextCommandLine
[1] == L
' ' || NextCommandLine
[1] == CHAR_NULL
)
1645 CopyMem(NextCommandLine
, NextCommandLine
+1, StrSize(NextCommandLine
) - sizeof(NextCommandLine
[0]));
1646 while (NextCommandLine
[0] == L
' ') {
1647 CopyMem(NextCommandLine
, NextCommandLine
+1, StrSize(NextCommandLine
) - sizeof(NextCommandLine
[0]));
1649 if (NextCommandLine
[0] == CHAR_NULL
) {
1650 SHELL_FREE_NON_NULL(OurCommandLine
);
1651 SHELL_FREE_NON_NULL(NextCommandLine
);
1652 return (EFI_INVALID_PARAMETER
);
1661 // make a SPLIT_LIST item and add to list
1663 Split
= AllocateZeroPool(sizeof(SPLIT_LIST
));
1664 ASSERT(Split
!= NULL
);
1665 Split
->SplitStdIn
= StdIn
;
1666 Split
->SplitStdOut
= ConvertEfiFileProtocolToShellHandle(CreateFileInterfaceMem(Unicode
), NULL
);
1667 ASSERT(Split
->SplitStdOut
!= NULL
);
1668 InsertHeadList(&ShellInfoObject
.SplitList
.Link
, &Split
->Link
);
1670 Status
= RunCommand(OurCommandLine
);
1673 // move the output from the first to the in to the second.
1675 TempFileHandle
= Split
->SplitStdOut
;
1676 if (Split
->SplitStdIn
== StdIn
) {
1677 Split
->SplitStdOut
= NULL
;
1679 Split
->SplitStdOut
= Split
->SplitStdIn
;
1681 Split
->SplitStdIn
= TempFileHandle
;
1682 ShellInfoObject
.NewEfiShellProtocol
->SetFilePosition(ConvertShellHandleToEfiFileProtocol(Split
->SplitStdIn
), 0);
1684 if (!EFI_ERROR(Status
)) {
1685 Status
= RunCommand(NextCommandLine
);
1689 // remove the top level from the ScriptList
1691 ASSERT((SPLIT_LIST
*)GetFirstNode(&ShellInfoObject
.SplitList
.Link
) == Split
);
1692 RemoveEntryList(&Split
->Link
);
1695 // Note that the original StdIn is now the StdOut...
1697 if (Split
->SplitStdOut
!= NULL
&& Split
->SplitStdOut
!= StdIn
) {
1698 ShellInfoObject
.NewEfiShellProtocol
->CloseFile(ConvertShellHandleToEfiFileProtocol(Split
->SplitStdOut
));
1700 if (Split
->SplitStdIn
!= NULL
) {
1701 ShellInfoObject
.NewEfiShellProtocol
->CloseFile(ConvertShellHandleToEfiFileProtocol(Split
->SplitStdIn
));
1705 FreePool(NextCommandLine
);
1706 FreePool(OurCommandLine
);
1712 Take the original command line, substitute any variables, free
1713 the original string, return the modified copy.
1715 @param[in] CmdLine pointer to the command line to update.
1717 @retval EFI_SUCCESS the function was successful.
1718 @retval EFI_OUT_OF_RESOURCES a memory allocation failed.
1722 ShellSubstituteVariables(
1727 NewCmdLine
= ShellConvertVariables(*CmdLine
);
1728 SHELL_FREE_NON_NULL(*CmdLine
);
1729 if (NewCmdLine
== NULL
) {
1730 return (EFI_OUT_OF_RESOURCES
);
1732 *CmdLine
= NewCmdLine
;
1733 return (EFI_SUCCESS
);
1737 Take the original command line, substitute any alias in the first group of space delimited characters, free
1738 the original string, return the modified copy.
1740 @param[in] CmdLine pointer to the command line to update.
1742 @retval EFI_SUCCESS the function was successful.
1743 @retval EFI_OUT_OF_RESOURCES a memory allocation failed.
1747 ShellSubstituteAliases(
1752 CHAR16
*CommandName
;
1754 UINTN PostAliasSize
;
1755 ASSERT(CmdLine
!= NULL
);
1756 ASSERT(*CmdLine
!= NULL
);
1760 if (StrStr((*CmdLine
), L
" ") == NULL
){
1761 StrnCatGrow(&CommandName
, NULL
, (*CmdLine
), 0);
1763 StrnCatGrow(&CommandName
, NULL
, (*CmdLine
), StrStr((*CmdLine
), L
" ") - (*CmdLine
));
1767 // This cannot happen 'inline' since the CmdLine can need extra space.
1770 if (!ShellCommandIsCommandOnList(CommandName
)) {
1772 // Convert via alias
1774 Status
= ShellConvertAlias(&CommandName
);
1775 if (EFI_ERROR(Status
)){
1779 NewCmdLine
= StrnCatGrow(&NewCmdLine
, &PostAliasSize
, CommandName
, 0);
1780 if (NewCmdLine
== NULL
) {
1781 SHELL_FREE_NON_NULL(CommandName
);
1782 SHELL_FREE_NON_NULL(*CmdLine
);
1783 return (EFI_OUT_OF_RESOURCES
);
1785 NewCmdLine
= StrnCatGrow(&NewCmdLine
, &PostAliasSize
, StrStr((*CmdLine
), L
" "), 0);
1786 if (NewCmdLine
== NULL
) {
1787 SHELL_FREE_NON_NULL(CommandName
);
1788 SHELL_FREE_NON_NULL(*CmdLine
);
1789 return (EFI_OUT_OF_RESOURCES
);
1792 NewCmdLine
= StrnCatGrow(&NewCmdLine
, NULL
, (*CmdLine
), 0);
1795 SHELL_FREE_NON_NULL(*CmdLine
);
1796 SHELL_FREE_NON_NULL(CommandName
);
1799 // re-assign the passed in double pointer to point to our newly allocated buffer
1801 *CmdLine
= NewCmdLine
;
1803 return (EFI_SUCCESS
);
1807 Takes the Argv[0] part of the command line and determine the meaning of it.
1809 @param[in] CmdName pointer to the command line to update.
1811 @retval Internal_Command The name is an internal command.
1812 @retval File_Sys_Change the name is a file system change.
1813 @retval Script_File_Name the name is a NSH script file.
1814 @retval Unknown_Invalid the name is unknown.
1815 @retval Efi_Application the name is an application (.EFI).
1817 SHELL_OPERATION_TYPES
1820 IN CONST CHAR16
*CmdName
1823 CHAR16
* FileWithPath
;
1824 CONST CHAR16
* TempLocation
;
1825 CONST CHAR16
* TempLocation2
;
1827 FileWithPath
= NULL
;
1829 // test for an internal command.
1831 if (ShellCommandIsCommandOnList(CmdName
)) {
1832 return (Internal_Command
);
1836 // Test for file system change request. anything ending with first : and cant have spaces.
1838 if (CmdName
[(StrLen(CmdName
)-1)] == L
':') {
1839 if ( StrStr(CmdName
, L
" ") != NULL
1840 || StrLen(StrStr(CmdName
, L
":")) > 1
1842 return (Unknown_Invalid
);
1844 return (File_Sys_Change
);
1850 if ((FileWithPath
= ShellFindFilePathEx(CmdName
, mExecutableExtensions
)) != NULL
) {
1852 // See if that file has a script file extension
1854 if (StrLen(FileWithPath
) > 4) {
1855 TempLocation
= FileWithPath
+StrLen(FileWithPath
)-4;
1856 TempLocation2
= mScriptExtension
;
1857 if (StringNoCaseCompare((VOID
*)(&TempLocation
), (VOID
*)(&TempLocation2
)) == 0) {
1858 SHELL_FREE_NON_NULL(FileWithPath
);
1859 return (Script_File_Name
);
1864 // Was a file, but not a script. we treat this as an application.
1866 SHELL_FREE_NON_NULL(FileWithPath
);
1867 return (Efi_Application
);
1870 SHELL_FREE_NON_NULL(FileWithPath
);
1872 // No clue what this is... return invalid flag...
1874 return (Unknown_Invalid
);
1878 Determine if the first item in a command line is valid.
1880 @param[in] CmdLine The command line to parse.
1882 @retval EFI_SUCCESS The item is valid.
1883 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
1884 @retval EFI_NOT_FOUND The operation type is unknown or invalid.
1889 IN CONST CHAR16
*CmdLine
1893 CHAR16
*FirstParameter
;
1899 Temp
= StrnCatGrow(&Temp
, NULL
, CmdLine
, 0);
1901 return (EFI_OUT_OF_RESOURCES
);
1904 FirstParameter
= StrStr(Temp
, L
"|");
1905 if (FirstParameter
!= NULL
) {
1906 *FirstParameter
= CHAR_NULL
;
1909 FirstParameter
= NULL
;
1912 // Process the command line
1914 Status
= ProcessCommandLineToFinal(&Temp
);
1916 if (!EFI_ERROR(Status
)) {
1917 FirstParameter
= AllocateZeroPool(StrSize(CmdLine
));
1918 if (FirstParameter
== NULL
) {
1919 SHELL_FREE_NON_NULL(Temp
);
1920 return (EFI_OUT_OF_RESOURCES
);
1922 TempWalker
= (CHAR16
*)Temp
;
1923 if (!EFI_ERROR(GetNextParameter(&TempWalker
, &FirstParameter
, StrSize(CmdLine
), TRUE
))) {
1924 if (GetOperationType(FirstParameter
) == Unknown_Invalid
) {
1925 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_NOT_FOUND
), ShellInfoObject
.HiiHandle
, FirstParameter
);
1926 SetLastError(SHELL_NOT_FOUND
);
1927 Status
= EFI_NOT_FOUND
;
1932 SHELL_FREE_NON_NULL(Temp
);
1933 SHELL_FREE_NON_NULL(FirstParameter
);
1938 Determine if a command line contains with a split contains only valid commands.
1940 @param[in] CmdLine The command line to parse.
1942 @retval EFI_SUCCESS CmdLine has only valid commands, application, or has no split.
1943 @retval EFI_ABORTED CmdLine has at least one invalid command or application.
1948 IN CONST CHAR16
*CmdLine
1951 CONST CHAR16
*TempSpot
;
1955 // If this was the only item, then get out
1957 if (!ContainsSplit(CmdLine
)) {
1958 return (EFI_SUCCESS
);
1962 // Verify up to the pipe or end character
1964 Status
= IsValidSplit(CmdLine
);
1965 if (EFI_ERROR(Status
)) {
1970 // recurse to verify the next item
1972 TempSpot
= FindFirstCharacter(CmdLine
, L
"|", L
'^') + 1;
1973 if (*TempSpot
== L
'a' &&
1974 (*(TempSpot
+ 1) == L
' ' || *(TempSpot
+ 1) == CHAR_NULL
)
1976 // If it's an ASCII pipe '|a'
1980 return (VerifySplit(TempSpot
));
1984 Process a split based operation.
1986 @param[in] CmdLine pointer to the command line to process
1988 @retval EFI_SUCCESS The operation was successful
1989 @return an error occurred.
1993 ProcessNewSplitCommandLine(
1994 IN CONST CHAR16
*CmdLine
2000 Status
= VerifySplit(CmdLine
);
2001 if (EFI_ERROR(Status
)) {
2008 // are we in an existing split???
2010 if (!IsListEmpty(&ShellInfoObject
.SplitList
.Link
)) {
2011 Split
= (SPLIT_LIST
*)GetFirstNode(&ShellInfoObject
.SplitList
.Link
);
2014 if (Split
== NULL
) {
2015 Status
= RunSplitCommand(CmdLine
, NULL
, NULL
);
2017 Status
= RunSplitCommand(CmdLine
, Split
->SplitStdIn
, Split
->SplitStdOut
);
2019 if (EFI_ERROR(Status
)) {
2020 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_INVALID_SPLIT
), ShellInfoObject
.HiiHandle
, CmdLine
);
2026 Handle a request to change the current file system.
2028 @param[in] CmdLine The passed in command line.
2030 @retval EFI_SUCCESS The operation was successful.
2035 IN CONST CHAR16
*CmdLine
2039 Status
= EFI_SUCCESS
;
2042 // make sure we are the right operation
2044 ASSERT(CmdLine
[(StrLen(CmdLine
)-1)] == L
':' && StrStr(CmdLine
, L
" ") == NULL
);
2047 // Call the protocol API to do the work
2049 Status
= ShellInfoObject
.NewEfiShellProtocol
->SetCurDir(NULL
, CmdLine
);
2052 // Report any errors
2054 if (EFI_ERROR(Status
)) {
2055 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_INVALID_MAPPING
), ShellInfoObject
.HiiHandle
, CmdLine
);
2062 Reprocess the command line to direct all -? to the help command.
2064 if found, will add "help" as argv[0], and move the rest later.
2066 @param[in,out] CmdLine pointer to the command line to update
2071 IN OUT CHAR16
**CmdLine
2074 CHAR16
*CurrentParameter
;
2076 CHAR16
*NewCommandLine
;
2078 UINTN NewCmdLineSize
;
2080 Status
= EFI_SUCCESS
;
2082 CurrentParameter
= AllocateZeroPool(StrSize(*CmdLine
));
2083 if (CurrentParameter
== NULL
) {
2084 return (EFI_OUT_OF_RESOURCES
);
2088 while(Walker
!= NULL
&& *Walker
!= CHAR_NULL
) {
2089 if (!EFI_ERROR(GetNextParameter(&Walker
, &CurrentParameter
, StrSize(*CmdLine
), TRUE
))) {
2090 if (StrStr(CurrentParameter
, L
"-?") == CurrentParameter
) {
2091 CurrentParameter
[0] = L
' ';
2092 CurrentParameter
[1] = L
' ';
2093 NewCmdLineSize
= StrSize(L
"help ") + StrSize(*CmdLine
);
2094 NewCommandLine
= AllocateZeroPool(NewCmdLineSize
);
2095 if (NewCommandLine
== NULL
) {
2096 Status
= EFI_OUT_OF_RESOURCES
;
2101 // We know the space is sufficient since we just calculated it.
2103 StrnCpyS(NewCommandLine
, NewCmdLineSize
/sizeof(CHAR16
), L
"help ", 5);
2104 StrnCatS(NewCommandLine
, NewCmdLineSize
/sizeof(CHAR16
), *CmdLine
, StrLen(*CmdLine
));
2105 SHELL_FREE_NON_NULL(*CmdLine
);
2106 *CmdLine
= NewCommandLine
;
2112 SHELL_FREE_NON_NULL(CurrentParameter
);
2118 Function to update the shell variable "lasterror".
2120 @param[in] ErrorCode the error code to put into lasterror.
2125 IN CONST SHELL_STATUS ErrorCode
2128 CHAR16 LeString
[19];
2129 if (sizeof(EFI_STATUS
) == sizeof(UINT64
)) {
2130 UnicodeSPrint(LeString
, sizeof(LeString
), L
"0x%Lx", ErrorCode
);
2132 UnicodeSPrint(LeString
, sizeof(LeString
), L
"0x%x", ErrorCode
);
2134 DEBUG_CODE(InternalEfiShellSetEnv(L
"debuglasterror", LeString
, TRUE
););
2135 InternalEfiShellSetEnv(L
"lasterror", LeString
, TRUE
);
2137 return (EFI_SUCCESS
);
2141 Converts the command line to it's post-processed form. this replaces variables and alias' per UEFI Shell spec.
2143 @param[in,out] CmdLine pointer to the command line to update
2145 @retval EFI_SUCCESS The operation was successful
2146 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
2147 @return some other error occurred
2151 ProcessCommandLineToFinal(
2152 IN OUT CHAR16
**CmdLine
2156 TrimSpaces(CmdLine
);
2158 Status
= ShellSubstituteAliases(CmdLine
);
2159 if (EFI_ERROR(Status
)) {
2163 TrimSpaces(CmdLine
);
2165 Status
= ShellSubstituteVariables(CmdLine
);
2166 if (EFI_ERROR(Status
)) {
2169 ASSERT (*CmdLine
!= NULL
);
2171 TrimSpaces(CmdLine
);
2174 // update for help parsing
2176 if (StrStr(*CmdLine
, L
"?") != NULL
) {
2178 // This may do nothing if the ? does not indicate help.
2179 // Save all the details for in the API below.
2181 Status
= DoHelpUpdate(CmdLine
);
2184 TrimSpaces(CmdLine
);
2186 return (EFI_SUCCESS
);
2190 Run an internal shell command.
2192 This API will update the shell's environment since these commands are libraries.
2194 @param[in] CmdLine the command line to run.
2195 @param[in] FirstParameter the first parameter on the command line
2196 @param[in] ParamProtocol the shell parameters protocol pointer
2197 @param[out] CommandStatus the status from the command line.
2199 @retval EFI_SUCCESS The command was completed.
2200 @retval EFI_ABORTED The command's operation was aborted.
2205 IN CONST CHAR16
*CmdLine
,
2206 IN CHAR16
*FirstParameter
,
2207 IN EFI_SHELL_PARAMETERS_PROTOCOL
*ParamProtocol
,
2208 OUT EFI_STATUS
*CommandStatus
2214 SHELL_STATUS CommandReturnedStatus
;
2219 NewCmdLine
= AllocateCopyPool (StrSize (CmdLine
), CmdLine
);
2220 if (NewCmdLine
== NULL
) {
2221 return EFI_OUT_OF_RESOURCES
;
2224 for (Walker
= NewCmdLine
; Walker
!= NULL
&& *Walker
!= CHAR_NULL
; Walker
++) {
2225 if (*Walker
== L
'^' && *(Walker
+1) == L
'#') {
2226 CopyMem(Walker
, Walker
+1, StrSize(Walker
) - sizeof(Walker
[0]));
2231 // get the argc and argv updated for internal commands
2233 Status
= UpdateArgcArgv(ParamProtocol
, NewCmdLine
, Internal_Command
, &Argv
, &Argc
);
2234 if (!EFI_ERROR(Status
)) {
2236 // Run the internal command.
2238 Status
= ShellCommandRunCommandHandler(FirstParameter
, &CommandReturnedStatus
, &LastError
);
2240 if (!EFI_ERROR(Status
)) {
2241 if (CommandStatus
!= NULL
) {
2242 if (CommandReturnedStatus
!= SHELL_SUCCESS
) {
2243 *CommandStatus
= (EFI_STATUS
)(CommandReturnedStatus
| MAX_BIT
);
2245 *CommandStatus
= EFI_SUCCESS
;
2250 // Update last error status.
2251 // some commands do not update last error.
2254 SetLastError(CommandReturnedStatus
);
2258 // Pass thru the exitcode from the app.
2260 if (ShellCommandGetExit()) {
2262 // An Exit was requested ("exit" command), pass its value up.
2264 Status
= CommandReturnedStatus
;
2265 } else if (CommandReturnedStatus
!= SHELL_SUCCESS
&& IsScriptOnlyCommand(FirstParameter
)) {
2267 // Always abort when a script only command fails for any reason
2269 Status
= EFI_ABORTED
;
2270 } else if (ShellCommandGetCurrentScriptFile() != NULL
&& CommandReturnedStatus
== SHELL_ABORTED
) {
2272 // Abort when in a script and a command aborted
2274 Status
= EFI_ABORTED
;
2280 // This is guaranteed to be called after UpdateArgcArgv no matter what else happened.
2281 // This is safe even if the update API failed. In this case, it may be a no-op.
2283 RestoreArgcArgv(ParamProtocol
, &Argv
, &Argc
);
2286 // If a script is running and the command is not a script only command, then
2287 // change return value to success so the script won't halt (unless aborted).
2289 // Script only commands have to be able halt the script since the script will
2290 // not operate if they are failing.
2292 if ( ShellCommandGetCurrentScriptFile() != NULL
2293 && !IsScriptOnlyCommand(FirstParameter
)
2294 && Status
!= EFI_ABORTED
2296 Status
= EFI_SUCCESS
;
2299 FreePool (NewCmdLine
);
2304 Function to run the command or file.
2306 @param[in] Type the type of operation being run.
2307 @param[in] CmdLine the command line to run.
2308 @param[in] FirstParameter the first parameter on the command line
2309 @param[in] ParamProtocol the shell parameters protocol pointer
2310 @param[out] CommandStatus the status from the command line.
2312 @retval EFI_SUCCESS The command was completed.
2313 @retval EFI_ABORTED The command's operation was aborted.
2318 IN SHELL_OPERATION_TYPES Type
,
2319 IN CONST CHAR16
*CmdLine
,
2320 IN CHAR16
*FirstParameter
,
2321 IN EFI_SHELL_PARAMETERS_PROTOCOL
*ParamProtocol
,
2322 OUT EFI_STATUS
*CommandStatus
2326 EFI_STATUS StartStatus
;
2327 CHAR16
*CommandWithPath
;
2328 EFI_DEVICE_PATH_PROTOCOL
*DevPath
;
2329 SHELL_STATUS CalleeExitStatus
;
2331 Status
= EFI_SUCCESS
;
2332 CommandWithPath
= NULL
;
2334 CalleeExitStatus
= SHELL_INVALID_PARAMETER
;
2337 case Internal_Command
:
2338 Status
= RunInternalCommand(CmdLine
, FirstParameter
, ParamProtocol
, CommandStatus
);
2340 case Script_File_Name
:
2341 case Efi_Application
:
2343 // Process a fully qualified path
2345 if (StrStr(FirstParameter
, L
":") != NULL
) {
2346 ASSERT (CommandWithPath
== NULL
);
2347 if (ShellIsFile(FirstParameter
) == EFI_SUCCESS
) {
2348 CommandWithPath
= StrnCatGrow(&CommandWithPath
, NULL
, FirstParameter
, 0);
2353 // Process a relative path and also check in the path environment variable
2355 if (CommandWithPath
== NULL
) {
2356 CommandWithPath
= ShellFindFilePathEx(FirstParameter
, mExecutableExtensions
);
2360 // This should be impossible now.
2362 ASSERT(CommandWithPath
!= NULL
);
2365 // Make sure that path is not just a directory (or not found)
2367 if (!EFI_ERROR(ShellIsDirectory(CommandWithPath
))) {
2368 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_NOT_FOUND
), ShellInfoObject
.HiiHandle
, FirstParameter
);
2369 SetLastError(SHELL_NOT_FOUND
);
2372 case Script_File_Name
:
2373 Status
= RunScriptFile (CommandWithPath
, NULL
, CmdLine
, ParamProtocol
);
2375 case Efi_Application
:
2377 // Get the device path of the application image
2379 DevPath
= ShellInfoObject
.NewEfiShellProtocol
->GetDevicePathFromFilePath(CommandWithPath
);
2380 if (DevPath
== NULL
){
2381 Status
= EFI_OUT_OF_RESOURCES
;
2386 // Execute the device path
2388 Status
= InternalShellExecuteDevicePath(
2396 SHELL_FREE_NON_NULL(DevPath
);
2398 if(EFI_ERROR (Status
)) {
2399 CalleeExitStatus
= (SHELL_STATUS
) (Status
& (~MAX_BIT
));
2401 CalleeExitStatus
= (SHELL_STATUS
) StartStatus
;
2404 if (CommandStatus
!= NULL
) {
2405 *CommandStatus
= CalleeExitStatus
;
2409 // Update last error status.
2411 // Status is an EFI_STATUS. Clear top bit to convert to SHELL_STATUS
2412 SetLastError(CalleeExitStatus
);
2428 SHELL_FREE_NON_NULL(CommandWithPath
);
2434 Function to setup StdIn, StdErr, StdOut, and then run the command or file.
2436 @param[in] Type the type of operation being run.
2437 @param[in] CmdLine the command line to run.
2438 @param[in] FirstParameter the first parameter on the command line.
2439 @param[in] ParamProtocol the shell parameters protocol pointer
2440 @param[out] CommandStatus the status from the command line.
2442 @retval EFI_SUCCESS The command was completed.
2443 @retval EFI_ABORTED The command's operation was aborted.
2447 SetupAndRunCommandOrFile(
2448 IN SHELL_OPERATION_TYPES Type
,
2450 IN CHAR16
*FirstParameter
,
2451 IN EFI_SHELL_PARAMETERS_PROTOCOL
*ParamProtocol
,
2452 OUT EFI_STATUS
*CommandStatus
2456 SHELL_FILE_HANDLE OriginalStdIn
;
2457 SHELL_FILE_HANDLE OriginalStdOut
;
2458 SHELL_FILE_HANDLE OriginalStdErr
;
2459 SYSTEM_TABLE_INFO OriginalSystemTableInfo
;
2462 // Update the StdIn, StdOut, and StdErr for redirection to environment variables, files, etc... unicode and ASCII
2464 Status
= UpdateStdInStdOutStdErr(ParamProtocol
, CmdLine
, &OriginalStdIn
, &OriginalStdOut
, &OriginalStdErr
, &OriginalSystemTableInfo
);
2467 // The StdIn, StdOut, and StdErr are set up.
2468 // Now run the command, script, or application
2470 if (!EFI_ERROR(Status
)) {
2471 TrimSpaces(&CmdLine
);
2472 Status
= RunCommandOrFile(Type
, CmdLine
, FirstParameter
, ParamProtocol
, CommandStatus
);
2478 if (EFI_ERROR(Status
)) {
2479 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_ERROR
), ShellInfoObject
.HiiHandle
, (VOID
*)(Status
));
2483 // put back the original StdIn, StdOut, and StdErr
2485 RestoreStdInStdOutStdErr(ParamProtocol
, &OriginalStdIn
, &OriginalStdOut
, &OriginalStdErr
, &OriginalSystemTableInfo
);
2491 Function will process and run a command line.
2493 This will determine if the command line represents an internal shell
2494 command or dispatch an external application.
2496 @param[in] CmdLine The command line to parse.
2497 @param[out] CommandStatus The status from the command line.
2499 @retval EFI_SUCCESS The command was completed.
2500 @retval EFI_ABORTED The command's operation was aborted.
2505 IN CONST CHAR16
*CmdLine
,
2506 OUT EFI_STATUS
*CommandStatus
2510 CHAR16
*CleanOriginal
;
2511 CHAR16
*FirstParameter
;
2513 SHELL_OPERATION_TYPES Type
;
2515 ASSERT(CmdLine
!= NULL
);
2516 if (StrLen(CmdLine
) == 0) {
2517 return (EFI_SUCCESS
);
2520 Status
= EFI_SUCCESS
;
2521 CleanOriginal
= NULL
;
2523 CleanOriginal
= StrnCatGrow(&CleanOriginal
, NULL
, CmdLine
, 0);
2524 if (CleanOriginal
== NULL
) {
2525 return (EFI_OUT_OF_RESOURCES
);
2528 TrimSpaces(&CleanOriginal
);
2531 // NULL out comments (leveraged from RunScriptFileHandle() ).
2532 // The # character on a line is used to denote that all characters on the same line
2533 // and to the right of the # are to be ignored by the shell.
2534 // Afterwards, again remove spaces, in case any were between the last command-parameter and '#'.
2536 for (TempWalker
= CleanOriginal
; TempWalker
!= NULL
&& *TempWalker
!= CHAR_NULL
; TempWalker
++) {
2537 if (*TempWalker
== L
'^') {
2538 if (*(TempWalker
+ 1) == L
'#') {
2541 } else if (*TempWalker
== L
'#') {
2542 *TempWalker
= CHAR_NULL
;
2546 TrimSpaces(&CleanOriginal
);
2549 // Handle case that passed in command line is just 1 or more " " characters.
2551 if (StrLen (CleanOriginal
) == 0) {
2552 SHELL_FREE_NON_NULL(CleanOriginal
);
2553 return (EFI_SUCCESS
);
2556 Status
= ProcessCommandLineToFinal(&CleanOriginal
);
2557 if (EFI_ERROR(Status
)) {
2558 SHELL_FREE_NON_NULL(CleanOriginal
);
2563 // We don't do normal processing with a split command line (output from one command input to another)
2565 if (ContainsSplit(CleanOriginal
)) {
2566 Status
= ProcessNewSplitCommandLine(CleanOriginal
);
2567 SHELL_FREE_NON_NULL(CleanOriginal
);
2572 // We need the first parameter information so we can determine the operation type
2574 FirstParameter
= AllocateZeroPool(StrSize(CleanOriginal
));
2575 if (FirstParameter
== NULL
) {
2576 SHELL_FREE_NON_NULL(CleanOriginal
);
2577 return (EFI_OUT_OF_RESOURCES
);
2579 TempWalker
= CleanOriginal
;
2580 if (!EFI_ERROR(GetNextParameter(&TempWalker
, &FirstParameter
, StrSize(CleanOriginal
), TRUE
))) {
2582 // Depending on the first parameter we change the behavior
2584 switch (Type
= GetOperationType(FirstParameter
)) {
2585 case File_Sys_Change
:
2586 Status
= ChangeMappedDrive (FirstParameter
);
2588 case Internal_Command
:
2589 case Script_File_Name
:
2590 case Efi_Application
:
2591 Status
= SetupAndRunCommandOrFile(Type
, CleanOriginal
, FirstParameter
, ShellInfoObject
.NewShellParametersProtocol
, CommandStatus
);
2595 // Whatever was typed, it was invalid.
2597 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_NOT_FOUND
), ShellInfoObject
.HiiHandle
, FirstParameter
);
2598 SetLastError(SHELL_NOT_FOUND
);
2602 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_NOT_FOUND
), ShellInfoObject
.HiiHandle
, FirstParameter
);
2603 SetLastError(SHELL_NOT_FOUND
);
2606 SHELL_FREE_NON_NULL(CleanOriginal
);
2607 SHELL_FREE_NON_NULL(FirstParameter
);
2613 Function will process and run a command line.
2615 This will determine if the command line represents an internal shell
2616 command or dispatch an external application.
2618 @param[in] CmdLine The command line to parse.
2620 @retval EFI_SUCCESS The command was completed.
2621 @retval EFI_ABORTED The command's operation was aborted.
2626 IN CONST CHAR16
*CmdLine
2629 return (RunShellCommand(CmdLine
, NULL
));
2633 STATIC CONST UINT16 InvalidChars
[] = {L
'*', L
'?', L
'<', L
'>', L
'\\', L
'/', L
'\"', 0x0001, 0x0002};
2635 Function determines if the CommandName COULD be a valid command. It does not determine whether
2636 this is a valid command. It only checks for invalid characters.
2638 @param[in] CommandName The name to check
2640 @retval TRUE CommandName could be a command name
2641 @retval FALSE CommandName could not be a valid command name
2646 IN CONST CHAR16
*CommandName
2650 if (CommandName
== NULL
) {
2655 ; Count
< sizeof(InvalidChars
) / sizeof(InvalidChars
[0])
2658 if (ScanMem16(CommandName
, StrSize(CommandName
), InvalidChars
[Count
]) != NULL
) {
2666 Function to process a NSH script file via SHELL_FILE_HANDLE.
2668 @param[in] Handle The handle to the already opened file.
2669 @param[in] Name The name of the script file.
2671 @retval EFI_SUCCESS the script completed successfully
2675 RunScriptFileHandle (
2676 IN SHELL_FILE_HANDLE Handle
,
2677 IN CONST CHAR16
*Name
2681 SCRIPT_FILE
*NewScriptFile
;
2683 UINTN PrintBuffSize
;
2684 CHAR16
*CommandLine
;
2685 CHAR16
*CommandLine2
;
2686 CHAR16
*CommandLine3
;
2687 SCRIPT_COMMAND_LIST
*LastCommand
;
2689 BOOLEAN PreScriptEchoState
;
2690 BOOLEAN PreCommandEchoState
;
2691 CONST CHAR16
*CurDir
;
2693 CHAR16 LeString
[50];
2694 LIST_ENTRY OldBufferList
;
2696 ASSERT(!ShellCommandGetScriptExit());
2698 PreScriptEchoState
= ShellCommandGetEchoState();
2699 PrintBuffSize
= PcdGet16(PcdShellPrintBufferSize
);
2701 NewScriptFile
= (SCRIPT_FILE
*)AllocateZeroPool(sizeof(SCRIPT_FILE
));
2702 if (NewScriptFile
== NULL
) {
2703 return (EFI_OUT_OF_RESOURCES
);
2709 ASSERT(NewScriptFile
->ScriptName
== NULL
);
2710 NewScriptFile
->ScriptName
= StrnCatGrow(&NewScriptFile
->ScriptName
, NULL
, Name
, 0);
2711 if (NewScriptFile
->ScriptName
== NULL
) {
2712 DeleteScriptFileStruct(NewScriptFile
);
2713 return (EFI_OUT_OF_RESOURCES
);
2717 // Save the parameters (used to replace %0 to %9 later on)
2719 NewScriptFile
->Argc
= ShellInfoObject
.NewShellParametersProtocol
->Argc
;
2720 if (NewScriptFile
->Argc
!= 0) {
2721 NewScriptFile
->Argv
= (CHAR16
**)AllocateZeroPool(NewScriptFile
->Argc
* sizeof(CHAR16
*));
2722 if (NewScriptFile
->Argv
== NULL
) {
2723 DeleteScriptFileStruct(NewScriptFile
);
2724 return (EFI_OUT_OF_RESOURCES
);
2726 for (LoopVar
= 0 ; LoopVar
< 10 && LoopVar
< NewScriptFile
->Argc
; LoopVar
++) {
2727 ASSERT(NewScriptFile
->Argv
[LoopVar
] == NULL
);
2728 NewScriptFile
->Argv
[LoopVar
] = StrnCatGrow(&NewScriptFile
->Argv
[LoopVar
], NULL
, ShellInfoObject
.NewShellParametersProtocol
->Argv
[LoopVar
], 0);
2729 if (NewScriptFile
->Argv
[LoopVar
] == NULL
) {
2730 DeleteScriptFileStruct(NewScriptFile
);
2731 return (EFI_OUT_OF_RESOURCES
);
2735 NewScriptFile
->Argv
= NULL
;
2738 InitializeListHead(&NewScriptFile
->CommandList
);
2739 InitializeListHead(&NewScriptFile
->SubstList
);
2742 // Now build the list of all script commands.
2745 while(!ShellFileHandleEof(Handle
)) {
2746 CommandLine
= ShellFileHandleReturnLine(Handle
, &Ascii
);
2748 if (CommandLine
== NULL
|| StrLen(CommandLine
) == 0 || CommandLine
[0] == '#') {
2749 SHELL_FREE_NON_NULL(CommandLine
);
2752 NewScriptFile
->CurrentCommand
= AllocateZeroPool(sizeof(SCRIPT_COMMAND_LIST
));
2753 if (NewScriptFile
->CurrentCommand
== NULL
) {
2754 SHELL_FREE_NON_NULL(CommandLine
);
2755 DeleteScriptFileStruct(NewScriptFile
);
2756 return (EFI_OUT_OF_RESOURCES
);
2759 NewScriptFile
->CurrentCommand
->Cl
= CommandLine
;
2760 NewScriptFile
->CurrentCommand
->Data
= NULL
;
2761 NewScriptFile
->CurrentCommand
->Line
= LineCount
;
2763 InsertTailList(&NewScriptFile
->CommandList
, &NewScriptFile
->CurrentCommand
->Link
);
2767 // Add this as the topmost script file
2769 ShellCommandSetNewScript (NewScriptFile
);
2772 // Now enumerate through the commands and run each one.
2774 CommandLine
= AllocateZeroPool(PrintBuffSize
);
2775 if (CommandLine
== NULL
) {
2776 DeleteScriptFileStruct(NewScriptFile
);
2777 return (EFI_OUT_OF_RESOURCES
);
2779 CommandLine2
= AllocateZeroPool(PrintBuffSize
);
2780 if (CommandLine2
== NULL
) {
2781 FreePool(CommandLine
);
2782 DeleteScriptFileStruct(NewScriptFile
);
2783 return (EFI_OUT_OF_RESOURCES
);
2786 for ( NewScriptFile
->CurrentCommand
= (SCRIPT_COMMAND_LIST
*)GetFirstNode(&NewScriptFile
->CommandList
)
2787 ; !IsNull(&NewScriptFile
->CommandList
, &NewScriptFile
->CurrentCommand
->Link
)
2788 ; // conditional increment in the body of the loop
2790 ASSERT(CommandLine2
!= NULL
);
2791 StrnCpyS( CommandLine2
,
2792 PrintBuffSize
/sizeof(CHAR16
),
2793 NewScriptFile
->CurrentCommand
->Cl
,
2794 PrintBuffSize
/sizeof(CHAR16
) - 1
2797 SaveBufferList(&OldBufferList
);
2800 // NULL out comments
2802 for (CommandLine3
= CommandLine2
; CommandLine3
!= NULL
&& *CommandLine3
!= CHAR_NULL
; CommandLine3
++) {
2803 if (*CommandLine3
== L
'^') {
2804 if ( *(CommandLine3
+1) == L
':') {
2805 CopyMem(CommandLine3
, CommandLine3
+1, StrSize(CommandLine3
) - sizeof(CommandLine3
[0]));
2806 } else if (*(CommandLine3
+1) == L
'#') {
2809 } else if (*CommandLine3
== L
'#') {
2810 *CommandLine3
= CHAR_NULL
;
2814 if (CommandLine2
!= NULL
&& StrLen(CommandLine2
) >= 1) {
2816 // Due to variability in starting the find and replace action we need to have both buffers the same.
2818 StrnCpyS( CommandLine
,
2819 PrintBuffSize
/sizeof(CHAR16
),
2821 PrintBuffSize
/sizeof(CHAR16
) - 1
2825 // Remove the %0 to %9 from the command line (if we have some arguments)
2827 if (NewScriptFile
->Argv
!= NULL
) {
2828 switch (NewScriptFile
->Argc
) {
2830 Status
= ShellCopySearchAndReplace(CommandLine2
, CommandLine
, PrintBuffSize
, L
"%9", NewScriptFile
->Argv
[9], FALSE
, FALSE
);
2831 ASSERT_EFI_ERROR(Status
);
2833 Status
= ShellCopySearchAndReplace(CommandLine
, CommandLine2
, PrintBuffSize
, L
"%8", NewScriptFile
->Argv
[8], FALSE
, FALSE
);
2834 ASSERT_EFI_ERROR(Status
);
2836 Status
= ShellCopySearchAndReplace(CommandLine2
, CommandLine
, PrintBuffSize
, L
"%7", NewScriptFile
->Argv
[7], FALSE
, FALSE
);
2837 ASSERT_EFI_ERROR(Status
);
2839 Status
= ShellCopySearchAndReplace(CommandLine
, CommandLine2
, PrintBuffSize
, L
"%6", NewScriptFile
->Argv
[6], FALSE
, FALSE
);
2840 ASSERT_EFI_ERROR(Status
);
2842 Status
= ShellCopySearchAndReplace(CommandLine2
, CommandLine
, PrintBuffSize
, L
"%5", NewScriptFile
->Argv
[5], FALSE
, FALSE
);
2843 ASSERT_EFI_ERROR(Status
);
2845 Status
= ShellCopySearchAndReplace(CommandLine
, CommandLine2
, PrintBuffSize
, L
"%4", NewScriptFile
->Argv
[4], FALSE
, FALSE
);
2846 ASSERT_EFI_ERROR(Status
);
2848 Status
= ShellCopySearchAndReplace(CommandLine2
, CommandLine
, PrintBuffSize
, L
"%3", NewScriptFile
->Argv
[3], FALSE
, FALSE
);
2849 ASSERT_EFI_ERROR(Status
);
2851 Status
= ShellCopySearchAndReplace(CommandLine
, CommandLine2
, PrintBuffSize
, L
"%2", NewScriptFile
->Argv
[2], FALSE
, FALSE
);
2852 ASSERT_EFI_ERROR(Status
);
2854 Status
= ShellCopySearchAndReplace(CommandLine2
, CommandLine
, PrintBuffSize
, L
"%1", NewScriptFile
->Argv
[1], FALSE
, FALSE
);
2855 ASSERT_EFI_ERROR(Status
);
2857 Status
= ShellCopySearchAndReplace(CommandLine
, CommandLine2
, PrintBuffSize
, L
"%0", NewScriptFile
->Argv
[0], FALSE
, FALSE
);
2858 ASSERT_EFI_ERROR(Status
);
2864 Status
= ShellCopySearchAndReplace(CommandLine2
, CommandLine
, PrintBuffSize
, L
"%1", L
"\"\"", FALSE
, FALSE
);
2865 Status
= ShellCopySearchAndReplace(CommandLine
, CommandLine2
, PrintBuffSize
, L
"%2", L
"\"\"", FALSE
, FALSE
);
2866 Status
= ShellCopySearchAndReplace(CommandLine2
, CommandLine
, PrintBuffSize
, L
"%3", L
"\"\"", FALSE
, FALSE
);
2867 Status
= ShellCopySearchAndReplace(CommandLine
, CommandLine2
, PrintBuffSize
, L
"%4", L
"\"\"", FALSE
, FALSE
);
2868 Status
= ShellCopySearchAndReplace(CommandLine2
, CommandLine
, PrintBuffSize
, L
"%5", L
"\"\"", FALSE
, FALSE
);
2869 Status
= ShellCopySearchAndReplace(CommandLine
, CommandLine2
, PrintBuffSize
, L
"%6", L
"\"\"", FALSE
, FALSE
);
2870 Status
= ShellCopySearchAndReplace(CommandLine2
, CommandLine
, PrintBuffSize
, L
"%7", L
"\"\"", FALSE
, FALSE
);
2871 Status
= ShellCopySearchAndReplace(CommandLine
, CommandLine2
, PrintBuffSize
, L
"%8", L
"\"\"", FALSE
, FALSE
);
2872 Status
= ShellCopySearchAndReplace(CommandLine2
, CommandLine
, PrintBuffSize
, L
"%9", L
"\"\"", FALSE
, FALSE
);
2874 StrnCpyS( CommandLine2
,
2875 PrintBuffSize
/sizeof(CHAR16
),
2877 PrintBuffSize
/sizeof(CHAR16
) - 1
2880 LastCommand
= NewScriptFile
->CurrentCommand
;
2882 for (CommandLine3
= CommandLine2
; CommandLine3
[0] == L
' ' ; CommandLine3
++);
2884 if (CommandLine3
!= NULL
&& CommandLine3
[0] == L
':' ) {
2886 // This line is a goto target / label
2889 if (CommandLine3
!= NULL
&& StrLen(CommandLine3
) > 0) {
2890 if (CommandLine3
[0] == L
'@') {
2892 // We need to save the current echo state
2893 // and disable echo for just this command.
2895 PreCommandEchoState
= ShellCommandGetEchoState();
2896 ShellCommandSetEchoState(FALSE
);
2897 Status
= RunCommand(CommandLine3
+1);
2900 // If command was "@echo -off" or "@echo -on" then don't restore echo state
2902 if (StrCmp (L
"@echo -off", CommandLine3
) != 0 &&
2903 StrCmp (L
"@echo -on", CommandLine3
) != 0) {
2905 // Now restore the pre-'@' echo state.
2907 ShellCommandSetEchoState(PreCommandEchoState
);
2910 if (ShellCommandGetEchoState()) {
2911 CurDir
= ShellInfoObject
.NewEfiShellProtocol
->GetEnv(L
"cwd");
2912 if (CurDir
!= NULL
&& StrLen(CurDir
) > 1) {
2913 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_CURDIR
), ShellInfoObject
.HiiHandle
, CurDir
);
2915 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_SHELL
), ShellInfoObject
.HiiHandle
);
2917 ShellPrintEx(-1, -1, L
"%s\r\n", CommandLine2
);
2919 Status
= RunCommand(CommandLine3
);
2923 if (ShellCommandGetScriptExit()) {
2925 // ShellCommandGetExitCode() always returns a UINT64
2927 UnicodeSPrint(LeString
, sizeof(LeString
), L
"0x%Lx", ShellCommandGetExitCode());
2928 DEBUG_CODE(InternalEfiShellSetEnv(L
"debuglasterror", LeString
, TRUE
););
2929 InternalEfiShellSetEnv(L
"lasterror", LeString
, TRUE
);
2931 ShellCommandRegisterExit(FALSE
, 0);
2932 Status
= EFI_SUCCESS
;
2933 RestoreBufferList(&OldBufferList
);
2936 if (ShellGetExecutionBreakFlag()) {
2937 RestoreBufferList(&OldBufferList
);
2940 if (EFI_ERROR(Status
)) {
2941 RestoreBufferList(&OldBufferList
);
2944 if (ShellCommandGetExit()) {
2945 RestoreBufferList(&OldBufferList
);
2950 // If that commend did not update the CurrentCommand then we need to advance it...
2952 if (LastCommand
== NewScriptFile
->CurrentCommand
) {
2953 NewScriptFile
->CurrentCommand
= (SCRIPT_COMMAND_LIST
*)GetNextNode(&NewScriptFile
->CommandList
, &NewScriptFile
->CurrentCommand
->Link
);
2954 if (!IsNull(&NewScriptFile
->CommandList
, &NewScriptFile
->CurrentCommand
->Link
)) {
2955 NewScriptFile
->CurrentCommand
->Reset
= TRUE
;
2959 NewScriptFile
->CurrentCommand
= (SCRIPT_COMMAND_LIST
*)GetNextNode(&NewScriptFile
->CommandList
, &NewScriptFile
->CurrentCommand
->Link
);
2960 if (!IsNull(&NewScriptFile
->CommandList
, &NewScriptFile
->CurrentCommand
->Link
)) {
2961 NewScriptFile
->CurrentCommand
->Reset
= TRUE
;
2964 RestoreBufferList(&OldBufferList
);
2968 FreePool(CommandLine
);
2969 FreePool(CommandLine2
);
2970 ShellCommandSetNewScript (NULL
);
2973 // Only if this was the last script reset the state.
2975 if (ShellCommandGetCurrentScriptFile()==NULL
) {
2976 ShellCommandSetEchoState(PreScriptEchoState
);
2978 return (EFI_SUCCESS
);
2982 Function to process a NSH script file.
2984 @param[in] ScriptPath Pointer to the script file name (including file system path).
2985 @param[in] Handle the handle of the script file already opened.
2986 @param[in] CmdLine the command line to run.
2987 @param[in] ParamProtocol the shell parameters protocol pointer
2989 @retval EFI_SUCCESS the script completed successfully
2994 IN CONST CHAR16
*ScriptPath
,
2995 IN SHELL_FILE_HANDLE Handle OPTIONAL
,
2996 IN CONST CHAR16
*CmdLine
,
2997 IN EFI_SHELL_PARAMETERS_PROTOCOL
*ParamProtocol
3001 SHELL_FILE_HANDLE FileHandle
;
3005 if (ShellIsFile(ScriptPath
) != EFI_SUCCESS
) {
3006 return (EFI_INVALID_PARAMETER
);
3010 // get the argc and argv updated for scripts
3012 Status
= UpdateArgcArgv(ParamProtocol
, CmdLine
, Script_File_Name
, &Argv
, &Argc
);
3013 if (!EFI_ERROR(Status
)) {
3015 if (Handle
== NULL
) {
3019 Status
= ShellOpenFileByName(ScriptPath
, &FileHandle
, EFI_FILE_MODE_READ
, 0);
3020 if (!EFI_ERROR(Status
)) {
3024 Status
= RunScriptFileHandle(FileHandle
, ScriptPath
);
3027 // now close the file
3029 ShellCloseFile(&FileHandle
);
3032 Status
= RunScriptFileHandle(Handle
, ScriptPath
);
3037 // This is guaranteed to be called after UpdateArgcArgv no matter what else happened.
3038 // This is safe even if the update API failed. In this case, it may be a no-op.
3040 RestoreArgcArgv(ParamProtocol
, &Argv
, &Argc
);
3046 Return the pointer to the first occurrence of any character from a list of characters.
3048 @param[in] String the string to parse
3049 @param[in] CharacterList the list of character to look for
3050 @param[in] EscapeCharacter An escape character to skip
3052 @return the location of the first character in the string
3053 @retval CHAR_NULL no instance of any character in CharacterList was found in String
3058 IN CONST CHAR16
*String
,
3059 IN CONST CHAR16
*CharacterList
,
3060 IN CONST CHAR16 EscapeCharacter
3066 for (WalkStr
= 0; WalkStr
< StrLen(String
); WalkStr
++) {
3067 if (String
[WalkStr
] == EscapeCharacter
) {
3071 for (WalkChar
= 0; WalkChar
< StrLen(CharacterList
); WalkChar
++) {
3072 if (String
[WalkStr
] == CharacterList
[WalkChar
]) {
3073 return (&String
[WalkStr
]);
3077 return (String
+ StrLen(String
));