2 This is THE shell (application)
4 Copyright (c) 2009 - 2016, Intel Corporation. All rights reserved.<BR>
5 (C) Copyright 2013-2014 Hewlett-Packard Development Company, L.P.<BR>
6 This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php
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
= {
57 {{NULL
, NULL
}, NULL
, NULL
},
58 {{NULL
, NULL
}, NULL
, NULL
},
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";
78 Cleans off leading and trailing spaces and tabs.
80 @param[in] String pointer to the string to trim them off.
88 ASSERT(String
!= NULL
);
89 ASSERT(*String
!= NULL
);
91 // Remove any spaces and tabs at the beginning of the (*String).
93 while (((*String
)[0] == L
' ') || ((*String
)[0] == L
'\t')) {
94 CopyMem((*String
), (*String
)+1, StrSize((*String
)) - sizeof((*String
)[0]));
98 // Remove any spaces and tabs at the end of the (*String).
100 while ((StrLen (*String
) > 0) && (((*String
)[StrLen((*String
))-1] == L
' ') || ((*String
)[StrLen((*String
))-1] == L
'\t'))) {
101 (*String
)[StrLen((*String
))-1] = CHAR_NULL
;
104 return (EFI_SUCCESS
);
108 Parse for the next instance of one string within another string. Can optionally make sure that
109 the string was not escaped (^ character) per the shell specification.
111 @param[in] SourceString The string to search within
112 @param[in] FindString The string to look for
113 @param[in] CheckForEscapeCharacter TRUE to skip escaped instances of FinfString, otherwise will return even escaped instances
118 IN CONST CHAR16
*SourceString
,
119 IN CONST CHAR16
*FindString
,
120 IN CONST BOOLEAN CheckForEscapeCharacter
124 if (SourceString
== NULL
) {
127 Temp
= StrStr(SourceString
, FindString
);
130 // If nothing found, or we don't care about escape characters
132 if (Temp
== NULL
|| !CheckForEscapeCharacter
) {
137 // If we found an escaped character, try again on the remainder of the string
139 if ((Temp
> (SourceString
)) && *(Temp
-1) == L
'^') {
140 return FindNextInstance(Temp
+1, FindString
, CheckForEscapeCharacter
);
144 // we found the right character
150 Check whether the string between a pair of % is a valid environment variable name.
152 @param[in] BeginPercent pointer to the first percent.
153 @param[in] EndPercent pointer to the last percent.
155 @retval TRUE is a valid environment variable name.
156 @retval FALSE is NOT a valid environment variable name.
159 IsValidEnvironmentVariableName(
160 IN CONST CHAR16
*BeginPercent
,
161 IN CONST CHAR16
*EndPercent
164 CONST CHAR16
*Walker
;
168 ASSERT (BeginPercent
!= NULL
);
169 ASSERT (EndPercent
!= NULL
);
170 ASSERT (BeginPercent
< EndPercent
);
172 if ((BeginPercent
+ 1) == EndPercent
) {
176 for (Walker
= BeginPercent
+ 1; Walker
< EndPercent
; Walker
++) {
178 (*Walker
>= L
'0' && *Walker
<= L
'9') ||
179 (*Walker
>= L
'A' && *Walker
<= L
'Z') ||
180 (*Walker
>= L
'a' && *Walker
<= L
'z') ||
183 if (Walker
== BeginPercent
+ 1 && (*Walker
>= L
'0' && *Walker
<= L
'9')) {
197 Determine if a command line contains a split operation
199 @param[in] CmdLine The command line to parse.
201 @retval TRUE CmdLine has a valid split.
202 @retval FALSE CmdLine does not have a valid split.
207 IN CONST CHAR16
*CmdLine
210 CONST CHAR16
*TempSpot
;
211 CONST CHAR16
*FirstQuote
;
212 CONST CHAR16
*SecondQuote
;
214 FirstQuote
= FindNextInstance (CmdLine
, L
"\"", TRUE
);
216 TempSpot
= FindFirstCharacter(CmdLine
, L
"|", L
'^');
218 if (FirstQuote
== NULL
||
220 TempSpot
== CHAR_NULL
||
221 FirstQuote
> TempSpot
223 return (BOOLEAN
) ((TempSpot
!= NULL
) && (*TempSpot
!= CHAR_NULL
));
226 while ((TempSpot
!= NULL
) && (*TempSpot
!= CHAR_NULL
)) {
227 if (FirstQuote
== NULL
|| FirstQuote
> TempSpot
) {
230 SecondQuote
= FindNextInstance (FirstQuote
+ 1, L
"\"", TRUE
);
231 if (SecondQuote
== NULL
) {
234 if (SecondQuote
< TempSpot
) {
235 FirstQuote
= FindNextInstance (SecondQuote
+ 1, L
"\"", TRUE
);
238 FirstQuote
= FindNextInstance (SecondQuote
+ 1, L
"\"", TRUE
);
239 TempSpot
= FindFirstCharacter(TempSpot
+ 1, L
"|", L
'^');
244 return (BOOLEAN
) ((TempSpot
!= NULL
) && (*TempSpot
!= CHAR_NULL
));
248 Function to start monitoring for CTRL-S using SimpleTextInputEx. This
249 feature's enabled state was not known when the shell initially launched.
251 @retval EFI_SUCCESS The feature is enabled.
252 @retval EFI_OUT_OF_RESOURCES There is not enough memory available.
256 InternalEfiShellStartCtrlSMonitor(
260 EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL
*SimpleEx
;
261 EFI_KEY_DATA KeyData
;
264 Status
= gBS
->OpenProtocol(
265 gST
->ConsoleInHandle
,
266 &gEfiSimpleTextInputExProtocolGuid
,
270 EFI_OPEN_PROTOCOL_GET_PROTOCOL
);
271 if (EFI_ERROR(Status
)) {
276 STRING_TOKEN (STR_SHELL_NO_IN_EX
),
277 ShellInfoObject
.HiiHandle
);
278 return (EFI_SUCCESS
);
281 KeyData
.KeyState
.KeyToggleState
= 0;
282 KeyData
.Key
.ScanCode
= 0;
283 KeyData
.KeyState
.KeyShiftState
= EFI_SHIFT_STATE_VALID
|EFI_LEFT_CONTROL_PRESSED
;
284 KeyData
.Key
.UnicodeChar
= L
's';
286 Status
= SimpleEx
->RegisterKeyNotify(
289 NotificationFunction
,
290 &ShellInfoObject
.CtrlSNotifyHandle1
);
292 KeyData
.KeyState
.KeyShiftState
= EFI_SHIFT_STATE_VALID
|EFI_RIGHT_CONTROL_PRESSED
;
293 if (!EFI_ERROR(Status
)) {
294 Status
= SimpleEx
->RegisterKeyNotify(
297 NotificationFunction
,
298 &ShellInfoObject
.CtrlSNotifyHandle2
);
300 KeyData
.KeyState
.KeyShiftState
= EFI_SHIFT_STATE_VALID
|EFI_LEFT_CONTROL_PRESSED
;
301 KeyData
.Key
.UnicodeChar
= 19;
303 if (!EFI_ERROR(Status
)) {
304 Status
= SimpleEx
->RegisterKeyNotify(
307 NotificationFunction
,
308 &ShellInfoObject
.CtrlSNotifyHandle3
);
310 KeyData
.KeyState
.KeyShiftState
= EFI_SHIFT_STATE_VALID
|EFI_RIGHT_CONTROL_PRESSED
;
311 if (!EFI_ERROR(Status
)) {
312 Status
= SimpleEx
->RegisterKeyNotify(
315 NotificationFunction
,
316 &ShellInfoObject
.CtrlSNotifyHandle4
);
324 The entry point for the application.
326 @param[in] ImageHandle The firmware allocated handle for the EFI image.
327 @param[in] SystemTable A pointer to the EFI System Table.
329 @retval EFI_SUCCESS The entry point is executed successfully.
330 @retval other Some error occurs when executing this entry point.
336 IN EFI_HANDLE ImageHandle
,
337 IN EFI_SYSTEM_TABLE
*SystemTable
343 EFI_HANDLE ConInHandle
;
344 EFI_SIMPLE_TEXT_INPUT_PROTOCOL
*OldConIn
;
346 if (PcdGet8(PcdShellSupportLevel
) > 3) {
347 return (EFI_UNSUPPORTED
);
353 Status
= gST
->ConOut
->ClearScreen(gST
->ConOut
);
354 if (EFI_ERROR(Status
)) {
359 // Populate the global structure from PCDs
361 ShellInfoObject
.ImageDevPath
= NULL
;
362 ShellInfoObject
.FileDevPath
= NULL
;
363 ShellInfoObject
.PageBreakEnabled
= PcdGetBool(PcdShellPageBreakDefault
);
364 ShellInfoObject
.ViewingSettings
.InsertMode
= PcdGetBool(PcdShellInsertModeDefault
);
365 ShellInfoObject
.LogScreenCount
= PcdGet8 (PcdShellScreenLogCount
);
368 // verify we dont allow for spec violation
370 ASSERT(ShellInfoObject
.LogScreenCount
>= 3);
373 // Initialize the LIST ENTRY objects...
375 InitializeListHead(&ShellInfoObject
.BufferToFreeList
.Link
);
376 InitializeListHead(&ShellInfoObject
.ViewingSettings
.CommandHistory
.Link
);
377 InitializeListHead(&ShellInfoObject
.SplitList
.Link
);
380 // Check PCDs for optional features that are not implemented yet.
382 if ( PcdGetBool(PcdShellSupportOldProtocols
)
383 || !FeaturePcdGet(PcdShellRequireHiiPlatform
)
384 || FeaturePcdGet(PcdShellSupportFrameworkHii
)
386 return (EFI_UNSUPPORTED
);
390 // turn off the watchdog timer
392 gBS
->SetWatchdogTimer (0, 0, 0, NULL
);
395 // install our console logger. This will keep a log of the output for back-browsing
397 Status
= ConsoleLoggerInstall(ShellInfoObject
.LogScreenCount
, &ShellInfoObject
.ConsoleInfo
);
398 if (!EFI_ERROR(Status
)) {
400 // Enable the cursor to be visible
402 gST
->ConOut
->EnableCursor (gST
->ConOut
, TRUE
);
405 // If supporting EFI 1.1 we need to install HII protocol
406 // only do this if PcdShellRequireHiiPlatform == FALSE
408 // remove EFI_UNSUPPORTED check above when complete.
409 ///@todo add support for Framework HII
412 // install our (solitary) HII package
414 ShellInfoObject
.HiiHandle
= HiiAddPackages (&gEfiCallerIdGuid
, gImageHandle
, ShellStrings
, NULL
);
415 if (ShellInfoObject
.HiiHandle
== NULL
) {
416 if (PcdGetBool(PcdShellSupportFrameworkHii
)) {
417 ///@todo Add our package into Framework HII
419 if (ShellInfoObject
.HiiHandle
== NULL
) {
420 Status
= EFI_NOT_STARTED
;
426 // create and install the EfiShellParametersProtocol
428 Status
= CreatePopulateInstallShellParametersProtocol(&ShellInfoObject
.NewShellParametersProtocol
, &ShellInfoObject
.RootShellInstance
);
429 ASSERT_EFI_ERROR(Status
);
430 ASSERT(ShellInfoObject
.NewShellParametersProtocol
!= NULL
);
433 // create and install the EfiShellProtocol
435 Status
= CreatePopulateInstallShellProtocol(&ShellInfoObject
.NewEfiShellProtocol
);
436 ASSERT_EFI_ERROR(Status
);
437 ASSERT(ShellInfoObject
.NewEfiShellProtocol
!= NULL
);
440 // Now initialize the shell library (it requires Shell Parameters protocol)
442 Status
= ShellInitialize();
443 ASSERT_EFI_ERROR(Status
);
445 Status
= CommandInit();
446 ASSERT_EFI_ERROR(Status
);
448 Status
= ShellInitEnvVarList ();
451 // Check the command line
453 Status
= ProcessCommandLine ();
454 if (EFI_ERROR (Status
)) {
459 // If shell support level is >= 1 create the mappings and paths
461 if (PcdGet8(PcdShellSupportLevel
) >= 1) {
462 Status
= ShellCommandCreateInitialMappingsAndPaths();
466 // Set the environment variable for nesting support
470 if (!ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoNest
) {
472 // No change. require nesting in Shell Protocol Execute()
474 StrnCatGrow(&TempString
,
479 StrnCatGrow(&TempString
,
484 Status
= InternalEfiShellSetEnv(mNoNestingEnvVarName
, TempString
, TRUE
);
485 SHELL_FREE_NON_NULL(TempString
);
489 // save the device path for the loaded image and the device path for the filepath (under loaded image)
490 // These are where to look for the startup.nsh file
492 Status
= GetDevicePathsForImageAndFile(&ShellInfoObject
.ImageDevPath
, &ShellInfoObject
.FileDevPath
);
493 ASSERT_EFI_ERROR(Status
);
496 // Display the version
498 if (!ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoVersion
) {
501 gST
->ConOut
->Mode
->CursorRow
,
503 STRING_TOKEN (STR_VER_OUTPUT_MAIN_SHELL
),
504 ShellInfoObject
.HiiHandle
,
505 SupportLevel
[PcdGet8(PcdShellSupportLevel
)],
506 gEfiShellProtocol
->MajorVersion
,
507 gEfiShellProtocol
->MinorVersion
514 STRING_TOKEN (STR_VER_OUTPUT_MAIN_SUPPLIER
),
515 ShellInfoObject
.HiiHandle
,
516 (CHAR16
*) PcdGetPtr (PcdShellSupplier
)
523 STRING_TOKEN (STR_VER_OUTPUT_MAIN_UEFI
),
524 ShellInfoObject
.HiiHandle
,
525 (gST
->Hdr
.Revision
&0xffff0000)>>16,
526 (gST
->Hdr
.Revision
&0x0000ffff),
528 gST
->FirmwareRevision
533 // Display the mapping
535 if (PcdGet8(PcdShellSupportLevel
) >= 2 && !ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoMap
) {
536 Status
= RunCommand(L
"map");
537 ASSERT_EFI_ERROR(Status
);
541 // init all the built in alias'
543 Status
= SetBuiltInAlias();
544 ASSERT_EFI_ERROR(Status
);
547 // Initialize environment variables
549 if (ShellCommandGetProfileList() != NULL
) {
550 Status
= InternalEfiShellSetEnv(L
"profiles", ShellCommandGetProfileList(), TRUE
);
551 ASSERT_EFI_ERROR(Status
);
555 TempString
= AllocateZeroPool(Size
);
557 UnicodeSPrint(TempString
, Size
, L
"%d", PcdGet8(PcdShellSupportLevel
));
558 Status
= InternalEfiShellSetEnv(L
"uefishellsupport", TempString
, TRUE
);
559 ASSERT_EFI_ERROR(Status
);
561 UnicodeSPrint(TempString
, Size
, L
"%d.%d", ShellInfoObject
.NewEfiShellProtocol
->MajorVersion
, ShellInfoObject
.NewEfiShellProtocol
->MinorVersion
);
562 Status
= InternalEfiShellSetEnv(L
"uefishellversion", TempString
, TRUE
);
563 ASSERT_EFI_ERROR(Status
);
565 UnicodeSPrint(TempString
, Size
, L
"%d.%d", (gST
->Hdr
.Revision
& 0xFFFF0000) >> 16, gST
->Hdr
.Revision
& 0x0000FFFF);
566 Status
= InternalEfiShellSetEnv(L
"uefiversion", TempString
, TRUE
);
567 ASSERT_EFI_ERROR(Status
);
569 FreePool(TempString
);
571 if (!EFI_ERROR(Status
)) {
572 if (!ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoInterrupt
) {
574 // Set up the event for CTRL-C monitoring...
576 Status
= InernalEfiShellStartMonitor();
579 if (!EFI_ERROR(Status
) && !ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoConsoleIn
) {
581 // Set up the event for CTRL-S monitoring...
583 Status
= InternalEfiShellStartCtrlSMonitor();
586 if (!EFI_ERROR(Status
) && ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoConsoleIn
) {
588 // close off the gST->ConIn
590 OldConIn
= gST
->ConIn
;
591 ConInHandle
= gST
->ConsoleInHandle
;
592 gST
->ConIn
= CreateSimpleTextInOnFile((SHELL_FILE_HANDLE
)&FileInterfaceNulFile
, &gST
->ConsoleInHandle
);
598 if (!EFI_ERROR(Status
) && PcdGet8(PcdShellSupportLevel
) >= 1) {
600 // process the startup script or launch the called app.
602 Status
= DoStartupScript(ShellInfoObject
.ImageDevPath
, ShellInfoObject
.FileDevPath
);
605 if (!ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.Exit
&& !ShellCommandGetExit() && (PcdGet8(PcdShellSupportLevel
) >= 3 || PcdGetBool(PcdShellForceConsole
)) && !EFI_ERROR(Status
) && !ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoConsoleIn
) {
607 // begin the UI waiting loop
611 // clean out all the memory allocated for CONST <something> * return values
612 // between each shell prompt presentation
614 if (!IsListEmpty(&ShellInfoObject
.BufferToFreeList
.Link
)){
615 FreeBufferList(&ShellInfoObject
.BufferToFreeList
);
619 // Reset page break back to default.
621 ShellInfoObject
.PageBreakEnabled
= PcdGetBool(PcdShellPageBreakDefault
);
622 ASSERT (ShellInfoObject
.ConsoleInfo
!= NULL
);
623 ShellInfoObject
.ConsoleInfo
->Enabled
= TRUE
;
624 ShellInfoObject
.ConsoleInfo
->RowCounter
= 0;
627 // Reset the CTRL-C event (yes we ignore the return values)
629 Status
= gBS
->CheckEvent (ShellInfoObject
.NewEfiShellProtocol
->ExecutionBreak
);
634 Status
= DoShellPrompt();
635 } while (!ShellCommandGetExit());
637 if (OldConIn
!= NULL
&& ConInHandle
!= NULL
) {
638 CloseSimpleTextInOnFile (gST
->ConIn
);
639 gST
->ConIn
= OldConIn
;
640 gST
->ConsoleInHandle
= ConInHandle
;
647 // uninstall protocols / free memory / etc...
649 if (ShellInfoObject
.UserBreakTimer
!= NULL
) {
650 gBS
->CloseEvent(ShellInfoObject
.UserBreakTimer
);
651 DEBUG_CODE(ShellInfoObject
.UserBreakTimer
= NULL
;);
653 if (ShellInfoObject
.ImageDevPath
!= NULL
) {
654 FreePool(ShellInfoObject
.ImageDevPath
);
655 DEBUG_CODE(ShellInfoObject
.ImageDevPath
= NULL
;);
657 if (ShellInfoObject
.FileDevPath
!= NULL
) {
658 FreePool(ShellInfoObject
.FileDevPath
);
659 DEBUG_CODE(ShellInfoObject
.FileDevPath
= NULL
;);
661 if (ShellInfoObject
.NewShellParametersProtocol
!= NULL
) {
662 CleanUpShellParametersProtocol(ShellInfoObject
.NewShellParametersProtocol
);
663 DEBUG_CODE(ShellInfoObject
.NewShellParametersProtocol
= NULL
;);
665 if (ShellInfoObject
.NewEfiShellProtocol
!= NULL
){
666 if (ShellInfoObject
.NewEfiShellProtocol
->IsRootShell()){
667 InternalEfiShellSetEnv(L
"cwd", NULL
, TRUE
);
669 CleanUpShellProtocol(ShellInfoObject
.NewEfiShellProtocol
);
670 DEBUG_CODE(ShellInfoObject
.NewEfiShellProtocol
= NULL
;);
673 if (!IsListEmpty(&ShellInfoObject
.BufferToFreeList
.Link
)){
674 FreeBufferList(&ShellInfoObject
.BufferToFreeList
);
677 if (!IsListEmpty(&ShellInfoObject
.SplitList
.Link
)){
678 ASSERT(FALSE
); ///@todo finish this de-allocation.
681 if (ShellInfoObject
.ShellInitSettings
.FileName
!= NULL
) {
682 FreePool(ShellInfoObject
.ShellInitSettings
.FileName
);
683 DEBUG_CODE(ShellInfoObject
.ShellInitSettings
.FileName
= NULL
;);
686 if (ShellInfoObject
.ShellInitSettings
.FileOptions
!= NULL
) {
687 FreePool(ShellInfoObject
.ShellInitSettings
.FileOptions
);
688 DEBUG_CODE(ShellInfoObject
.ShellInitSettings
.FileOptions
= NULL
;);
691 if (ShellInfoObject
.HiiHandle
!= NULL
) {
692 HiiRemovePackages(ShellInfoObject
.HiiHandle
);
693 DEBUG_CODE(ShellInfoObject
.HiiHandle
= NULL
;);
696 if (!IsListEmpty(&ShellInfoObject
.ViewingSettings
.CommandHistory
.Link
)){
697 FreeBufferList(&ShellInfoObject
.ViewingSettings
.CommandHistory
);
700 ASSERT(ShellInfoObject
.ConsoleInfo
!= NULL
);
701 if (ShellInfoObject
.ConsoleInfo
!= NULL
) {
702 ConsoleLoggerUninstall(ShellInfoObject
.ConsoleInfo
);
703 FreePool(ShellInfoObject
.ConsoleInfo
);
704 DEBUG_CODE(ShellInfoObject
.ConsoleInfo
= NULL
;);
707 ShellFreeEnvVarList ();
709 if (ShellCommandGetExit()) {
710 return ((EFI_STATUS
)ShellCommandGetExitCode());
716 Sets all the alias' that were registered with the ShellCommandLib library.
718 @retval EFI_SUCCESS all init commands were run successfully.
726 CONST ALIAS_LIST
*List
;
730 // Get all the commands we want to run
732 List
= ShellCommandGetInitAliasList();
735 // for each command in the List
737 for ( Node
= (ALIAS_LIST
*)GetFirstNode(&List
->Link
)
738 ; !IsNull (&List
->Link
, &Node
->Link
)
739 ; Node
= (ALIAS_LIST
*)GetNextNode(&List
->Link
, &Node
->Link
)
742 // install the alias'
744 Status
= InternalSetAlias(Node
->CommandString
, Node
->Alias
, TRUE
);
745 ASSERT_EFI_ERROR(Status
);
747 return (EFI_SUCCESS
);
751 Internal function to determine if 2 command names are really the same.
753 @param[in] Command1 The pointer to the first command name.
754 @param[in] Command2 The pointer to the second command name.
756 @retval TRUE The 2 command names are the same.
757 @retval FALSE The 2 command names are not the same.
762 IN CONST CHAR16
*Command1
,
763 IN CONST CHAR16
*Command2
766 if (StringNoCaseCompare(&Command1
, &Command2
) == 0) {
773 Internal function to determine if a command is a script only command.
775 @param[in] CommandName The pointer to the command name.
777 @retval TRUE The command is a script only command.
778 @retval FALSE The command is not a script only command.
783 IN CONST CHAR16
*CommandName
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")) {
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.
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.
806 @retval EFI_SUCCESS The 2 device paths were successfully returned.
807 @retval other A error from gBS->HandleProtocol.
813 GetDevicePathsForImageAndFile (
814 IN OUT EFI_DEVICE_PATH_PROTOCOL
**DevPath
,
815 IN OUT EFI_DEVICE_PATH_PROTOCOL
**FilePath
819 EFI_LOADED_IMAGE_PROTOCOL
*LoadedImage
;
820 EFI_DEVICE_PATH_PROTOCOL
*ImageDevicePath
;
822 ASSERT(DevPath
!= NULL
);
823 ASSERT(FilePath
!= NULL
);
825 Status
= gBS
->OpenProtocol (
827 &gEfiLoadedImageProtocolGuid
,
828 (VOID
**)&LoadedImage
,
831 EFI_OPEN_PROTOCOL_GET_PROTOCOL
833 if (!EFI_ERROR (Status
)) {
834 Status
= gBS
->OpenProtocol (
835 LoadedImage
->DeviceHandle
,
836 &gEfiDevicePathProtocolGuid
,
837 (VOID
**)&ImageDevicePath
,
840 EFI_OPEN_PROTOCOL_GET_PROTOCOL
842 if (!EFI_ERROR (Status
)) {
843 *DevPath
= DuplicateDevicePath (ImageDevicePath
);
844 *FilePath
= DuplicateDevicePath (LoadedImage
->FilePath
);
846 LoadedImage
->DeviceHandle
,
847 &gEfiDevicePathProtocolGuid
,
853 &gEfiLoadedImageProtocolGuid
,
861 Process all Uefi Shell 2.0 command line options.
863 see Uefi Shell 2.0 section 3.2 for full details.
865 the command line must resemble the following:
867 shell.efi [ShellOpt-options] [options] [file-name [file-name-options]]
869 ShellOpt-options Options which control the initialization behavior of the shell.
870 These options are read from the EFI global variable "ShellOpt"
871 and are processed before options or file-name.
873 options Options which control the initialization behavior of the shell.
875 file-name The name of a UEFI shell application or script to be executed
876 after initialization is complete. By default, if file-name is
877 specified, then -nostartup is implied. Scripts are not supported
880 file-name-options The command-line options that are passed to file-name when it
883 This will initialize the ShellInfoObject.ShellInitSettings global variable.
885 @retval EFI_SUCCESS The variable is initialized.
896 CHAR16
*DelayValueStr
;
899 EFI_UNICODE_COLLATION_PROTOCOL
*UnicodeCollation
;
901 // `file-name-options` will contain arguments to `file-name` that we don't
902 // know about. This would cause ShellCommandLineParse to error, so we parse
903 // arguments manually, ignoring those after the first thing that doesn't look
904 // like a shell option (which is assumed to be `file-name`).
906 Status
= gBS
->LocateProtocol (
907 &gEfiUnicodeCollation2ProtocolGuid
,
909 (VOID
**) &UnicodeCollation
911 if (EFI_ERROR (Status
)) {
912 Status
= gBS
->LocateProtocol (
913 &gEfiUnicodeCollationProtocolGuid
,
915 (VOID
**) &UnicodeCollation
917 if (EFI_ERROR (Status
)) {
922 // Set default options
923 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.Startup
= FALSE
;
924 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoStartup
= FALSE
;
925 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoConsoleOut
= FALSE
;
926 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoConsoleIn
= FALSE
;
927 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoInterrupt
= FALSE
;
928 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoMap
= FALSE
;
929 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoVersion
= FALSE
;
930 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.Delay
= FALSE
;
931 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.Exit
= FALSE
;
932 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoNest
= FALSE
;
933 ShellInfoObject
.ShellInitSettings
.Delay
= 5;
936 // Start LoopVar at 0 to parse only optional arguments at Argv[0]
937 // and parse other parameters from Argv[1]. This is for use case that
938 // UEFI Shell boot option is created, and OptionalData is provided
939 // that starts with shell command-line options.
941 for (LoopVar
= 0 ; LoopVar
< gEfiShellParametersProtocol
->Argc
; LoopVar
++) {
942 CurrentArg
= gEfiShellParametersProtocol
->Argv
[LoopVar
];
943 if (UnicodeCollation
->StriColl (
948 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.Startup
= TRUE
;
950 else if (UnicodeCollation
->StriColl (
955 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoStartup
= TRUE
;
957 else if (UnicodeCollation
->StriColl (
962 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoConsoleOut
= TRUE
;
964 else if (UnicodeCollation
->StriColl (
969 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoConsoleIn
= TRUE
;
971 else if (UnicodeCollation
->StriColl (
976 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoInterrupt
= TRUE
;
978 else if (UnicodeCollation
->StriColl (
983 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoMap
= TRUE
;
985 else if (UnicodeCollation
->StriColl (
990 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoVersion
= TRUE
;
992 else if (UnicodeCollation
->StriColl (
997 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoNest
= TRUE
;
999 else if (UnicodeCollation
->StriColl (
1004 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.Delay
= TRUE
;
1005 // Check for optional delay value following "-delay"
1006 DelayValueStr
= gEfiShellParametersProtocol
->Argv
[LoopVar
+ 1];
1007 if (DelayValueStr
!= NULL
){
1008 if (*DelayValueStr
== L
':') {
1011 if (!EFI_ERROR(ShellConvertStringToUint64 (
1017 ShellInfoObject
.ShellInitSettings
.Delay
= (UINTN
)DelayValue
;
1021 } else if (UnicodeCollation
->StriColl (
1026 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.Exit
= TRUE
;
1027 } else if (StrnCmp (L
"-", CurrentArg
, 1) == 0) {
1028 // Unrecognized option
1029 ShellPrintHiiEx(-1, -1, NULL
,
1030 STRING_TOKEN (STR_GEN_PROBLEM
),
1031 ShellInfoObject
.HiiHandle
,
1034 return EFI_INVALID_PARAMETER
;
1037 // First argument should be Shell.efi image name
1043 ShellInfoObject
.ShellInitSettings
.FileName
= AllocateCopyPool(StrSize(CurrentArg
), CurrentArg
);
1044 if (ShellInfoObject
.ShellInitSettings
.FileName
== NULL
) {
1045 return (EFI_OUT_OF_RESOURCES
);
1048 // We found `file-name`.
1050 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoStartup
= 1;
1053 // Add `file-name-options`
1054 for (Size
= 0 ; LoopVar
< gEfiShellParametersProtocol
->Argc
; LoopVar
++) {
1055 ASSERT((ShellInfoObject
.ShellInitSettings
.FileOptions
== NULL
&& Size
== 0) || (ShellInfoObject
.ShellInitSettings
.FileOptions
!= NULL
));
1056 StrnCatGrow(&ShellInfoObject
.ShellInitSettings
.FileOptions
,
1060 if (ShellInfoObject
.ShellInitSettings
.FileOptions
== NULL
) {
1061 SHELL_FREE_NON_NULL(ShellInfoObject
.ShellInitSettings
.FileName
);
1062 return (EFI_OUT_OF_RESOURCES
);
1064 StrnCatGrow(&ShellInfoObject
.ShellInitSettings
.FileOptions
,
1066 gEfiShellParametersProtocol
->Argv
[LoopVar
],
1068 if (ShellInfoObject
.ShellInitSettings
.FileOptions
== NULL
) {
1069 SHELL_FREE_NON_NULL(ShellInfoObject
.ShellInitSettings
.FileName
);
1070 return (EFI_OUT_OF_RESOURCES
);
1076 // "-nointerrupt" overrides "-delay"
1077 if (ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoInterrupt
) {
1078 ShellInfoObject
.ShellInitSettings
.Delay
= 0;
1085 Handles all interaction with the default startup script.
1087 this will check that the correct command line parameters were passed, handle the delay, and then start running the script.
1089 @param ImagePath the path to the image for shell. first place to look for the startup script
1090 @param FilePath the path to the file for shell. second place to look for the startup script.
1092 @retval EFI_SUCCESS the variable is initialized.
1097 IN EFI_DEVICE_PATH_PROTOCOL
*ImagePath
,
1098 IN EFI_DEVICE_PATH_PROTOCOL
*FilePath
1102 EFI_STATUS CalleeStatus
;
1105 SHELL_FILE_HANDLE FileHandle
;
1106 EFI_DEVICE_PATH_PROTOCOL
*NewPath
;
1107 EFI_DEVICE_PATH_PROTOCOL
*NamePath
;
1108 CHAR16
*FileStringPath
;
1111 CONST CHAR16
*MapName
;
1113 Key
.UnicodeChar
= CHAR_NULL
;
1117 if (!ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.Startup
&& ShellInfoObject
.ShellInitSettings
.FileName
!= NULL
) {
1119 // launch something else instead
1121 NewSize
= StrSize(ShellInfoObject
.ShellInitSettings
.FileName
);
1122 if (ShellInfoObject
.ShellInitSettings
.FileOptions
!= NULL
) {
1123 NewSize
+= StrSize(ShellInfoObject
.ShellInitSettings
.FileOptions
) + sizeof(CHAR16
);
1125 FileStringPath
= AllocateZeroPool(NewSize
);
1126 if (FileStringPath
== NULL
) {
1127 return (EFI_OUT_OF_RESOURCES
);
1129 StrCpyS(FileStringPath
, NewSize
/sizeof(CHAR16
), ShellInfoObject
.ShellInitSettings
.FileName
);
1130 if (ShellInfoObject
.ShellInitSettings
.FileOptions
!= NULL
) {
1131 StrnCatS(FileStringPath
, NewSize
/sizeof(CHAR16
), L
" ", NewSize
/sizeof(CHAR16
) - StrLen(FileStringPath
) -1);
1132 StrnCatS(FileStringPath
, NewSize
/sizeof(CHAR16
), ShellInfoObject
.ShellInitSettings
.FileOptions
, NewSize
/sizeof(CHAR16
) - StrLen(FileStringPath
) -1);
1134 Status
= RunShellCommand(FileStringPath
, &CalleeStatus
);
1135 if (ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.Exit
== TRUE
) {
1136 ShellCommandRegisterExit(gEfiShellProtocol
->BatchIsActive(), (UINT64
)CalleeStatus
);
1138 FreePool(FileStringPath
);
1144 // for shell level 0 we do no scripts
1145 // Without the Startup bit overriding we allow for nostartup to prevent scripts
1147 if ( (PcdGet8(PcdShellSupportLevel
) < 1)
1148 || (ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoStartup
&& !ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.Startup
)
1150 return (EFI_SUCCESS
);
1153 gST
->ConOut
->EnableCursor(gST
->ConOut
, FALSE
);
1155 // print out our warning and see if they press a key
1157 for ( Status
= EFI_UNSUPPORTED
, Delay
= ShellInfoObject
.ShellInitSettings
.Delay
1158 ; Delay
!= 0 && EFI_ERROR(Status
)
1161 ShellPrintHiiEx(0, gST
->ConOut
->Mode
->CursorRow
, NULL
, STRING_TOKEN (STR_SHELL_STARTUP_QUESTION
), ShellInfoObject
.HiiHandle
, Delay
);
1162 gBS
->Stall (1000000);
1163 if (!ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoConsoleIn
) {
1164 Status
= gST
->ConIn
->ReadKeyStroke (gST
->ConIn
, &Key
);
1167 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_CRLF
), ShellInfoObject
.HiiHandle
);
1168 gST
->ConOut
->EnableCursor(gST
->ConOut
, TRUE
);
1173 if (Status
== EFI_SUCCESS
&& Key
.UnicodeChar
== 0 && Key
.ScanCode
== SCAN_ESC
) {
1174 return (EFI_SUCCESS
);
1178 // Try the first location (must be file system)
1180 MapName
= ShellInfoObject
.NewEfiShellProtocol
->GetMapFromDevicePath(&ImagePath
);
1181 if (MapName
!= NULL
) {
1182 FileStringPath
= NULL
;
1184 FileStringPath
= StrnCatGrow(&FileStringPath
, &NewSize
, MapName
, 0);
1185 if (FileStringPath
== NULL
) {
1186 Status
= EFI_OUT_OF_RESOURCES
;
1188 TempSpot
= StrStr(FileStringPath
, L
";");
1189 if (TempSpot
!= NULL
) {
1190 *TempSpot
= CHAR_NULL
;
1192 FileStringPath
= StrnCatGrow(&FileStringPath
, &NewSize
, ((FILEPATH_DEVICE_PATH
*)FilePath
)->PathName
, 0);
1193 PathRemoveLastItem(FileStringPath
);
1194 FileStringPath
= StrnCatGrow(&FileStringPath
, &NewSize
, mStartupScript
, 0);
1195 Status
= ShellInfoObject
.NewEfiShellProtocol
->OpenFileByName(FileStringPath
, &FileHandle
, EFI_FILE_MODE_READ
);
1196 FreePool(FileStringPath
);
1199 if (EFI_ERROR(Status
)) {
1200 NamePath
= FileDevicePath (NULL
, mStartupScript
);
1201 NewPath
= AppendDevicePathNode (ImagePath
, NamePath
);
1207 Status
= InternalOpenFileDevicePath(NewPath
, &FileHandle
, EFI_FILE_MODE_READ
, 0);
1211 // If we got a file, run it
1213 if (!EFI_ERROR(Status
) && FileHandle
!= NULL
) {
1214 Status
= RunScriptFile (mStartupScript
, FileHandle
, L
"", ShellInfoObject
.NewShellParametersProtocol
);
1215 ShellInfoObject
.NewEfiShellProtocol
->CloseFile(FileHandle
);
1217 FileStringPath
= ShellFindFilePath(mStartupScript
);
1218 if (FileStringPath
== NULL
) {
1220 // we return success since we don't need to have a startup script
1222 Status
= EFI_SUCCESS
;
1223 ASSERT(FileHandle
== NULL
);
1225 Status
= RunScriptFile(FileStringPath
, NULL
, L
"", ShellInfoObject
.NewShellParametersProtocol
);
1226 FreePool(FileStringPath
);
1235 Function to perform the shell prompt looping. It will do a single prompt,
1236 dispatch the result, and then return. It is expected that the caller will
1237 call this function in a loop many times.
1240 @retval RETURN_ABORTED
1251 CONST CHAR16
*CurDir
;
1254 LIST_ENTRY OldBufferList
;
1259 // Get screen setting to decide size of the command line buffer
1261 gST
->ConOut
->QueryMode (gST
->ConOut
, gST
->ConOut
->Mode
->Mode
, &Column
, &Row
);
1262 BufferSize
= Column
* Row
* sizeof (CHAR16
);
1263 CmdLine
= AllocateZeroPool (BufferSize
);
1264 if (CmdLine
== NULL
) {
1265 return EFI_OUT_OF_RESOURCES
;
1268 SaveBufferList(&OldBufferList
);
1269 CurDir
= ShellInfoObject
.NewEfiShellProtocol
->GetEnv(L
"cwd");
1274 gST
->ConOut
->SetCursorPosition (gST
->ConOut
, 0, gST
->ConOut
->Mode
->CursorRow
);
1276 if (CurDir
!= NULL
&& StrLen(CurDir
) > 1) {
1277 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_CURDIR
), ShellInfoObject
.HiiHandle
, CurDir
);
1279 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_SHELL
), ShellInfoObject
.HiiHandle
);
1283 // Read a line from the console
1285 Status
= ShellInfoObject
.NewEfiShellProtocol
->ReadFile(ShellInfoObject
.NewShellParametersProtocol
->StdIn
, &BufferSize
, CmdLine
);
1288 // Null terminate the string and parse it
1290 if (!EFI_ERROR (Status
)) {
1291 CmdLine
[BufferSize
/ sizeof (CHAR16
)] = CHAR_NULL
;
1292 Status
= RunCommand(CmdLine
);
1296 // Done with this command
1298 RestoreBufferList(&OldBufferList
);
1304 Add a buffer to the Buffer To Free List for safely returning buffers to other
1305 places without risking letting them modify internal shell information.
1307 @param Buffer Something to pass to FreePool when the shell is exiting.
1311 AddBufferToFreeList(
1315 BUFFER_LIST
*BufferListEntry
;
1317 if (Buffer
== NULL
) {
1321 BufferListEntry
= AllocateZeroPool(sizeof(BUFFER_LIST
));
1322 ASSERT(BufferListEntry
!= NULL
);
1323 BufferListEntry
->Buffer
= Buffer
;
1324 InsertTailList(&ShellInfoObject
.BufferToFreeList
.Link
, &BufferListEntry
->Link
);
1330 Create a new buffer list and stores the old one to OldBufferList
1332 @param OldBufferList The temporary list head used to store the nodes in BufferToFreeList.
1336 OUT LIST_ENTRY
*OldBufferList
1339 CopyMem (OldBufferList
, &ShellInfoObject
.BufferToFreeList
.Link
, sizeof (LIST_ENTRY
));
1340 InitializeListHead (&ShellInfoObject
.BufferToFreeList
.Link
);
1344 Restore previous nodes into BufferToFreeList .
1346 @param OldBufferList The temporary list head used to store the nodes in BufferToFreeList.
1350 IN OUT LIST_ENTRY
*OldBufferList
1353 FreeBufferList (&ShellInfoObject
.BufferToFreeList
);
1354 CopyMem (&ShellInfoObject
.BufferToFreeList
.Link
, OldBufferList
, sizeof (LIST_ENTRY
));
1359 Add a buffer to the Line History List
1361 @param Buffer The line buffer to add.
1365 AddLineToCommandHistory(
1366 IN CONST CHAR16
*Buffer
1370 BUFFER_LIST
*Walker
;
1371 UINT16 MaxHistoryCmdCount
;
1375 MaxHistoryCmdCount
= PcdGet16(PcdShellMaxHistoryCommandCount
);
1377 if (MaxHistoryCmdCount
== 0) {
1382 Node
= AllocateZeroPool(sizeof(BUFFER_LIST
));
1383 ASSERT(Node
!= NULL
);
1384 Node
->Buffer
= AllocateCopyPool(StrSize(Buffer
), Buffer
);
1385 ASSERT(Node
->Buffer
!= NULL
);
1387 for ( Walker
= (BUFFER_LIST
*)GetFirstNode(&ShellInfoObject
.ViewingSettings
.CommandHistory
.Link
)
1388 ; !IsNull(&ShellInfoObject
.ViewingSettings
.CommandHistory
.Link
, &Walker
->Link
)
1389 ; Walker
= (BUFFER_LIST
*)GetNextNode(&ShellInfoObject
.ViewingSettings
.CommandHistory
.Link
, &Walker
->Link
)
1393 if (Count
< MaxHistoryCmdCount
){
1394 InsertTailList(&ShellInfoObject
.ViewingSettings
.CommandHistory
.Link
, &Node
->Link
);
1396 Walker
= (BUFFER_LIST
*)GetFirstNode(&ShellInfoObject
.ViewingSettings
.CommandHistory
.Link
);
1397 RemoveEntryList(&Walker
->Link
);
1398 if (Walker
->Buffer
!= NULL
) {
1399 FreePool(Walker
->Buffer
);
1402 InsertTailList(&ShellInfoObject
.ViewingSettings
.CommandHistory
.Link
, &Node
->Link
);
1407 Checks if a string is an alias for another command. If yes, then it replaces the alias name
1408 with the correct command name.
1410 @param[in, out] CommandString Upon entry the potential alias. Upon return the
1411 command name if it was an alias. If it was not
1412 an alias it will be unchanged. This function may
1413 change the buffer to fit the command name.
1415 @retval EFI_SUCCESS The name was changed.
1416 @retval EFI_SUCCESS The name was not an alias.
1417 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
1422 IN OUT CHAR16
**CommandString
1425 CONST CHAR16
*NewString
;
1427 NewString
= ShellInfoObject
.NewEfiShellProtocol
->GetAlias(*CommandString
, NULL
);
1428 if (NewString
== NULL
) {
1429 return (EFI_SUCCESS
);
1431 FreePool(*CommandString
);
1432 *CommandString
= AllocateCopyPool(StrSize(NewString
), NewString
);
1433 if (*CommandString
== NULL
) {
1434 return (EFI_OUT_OF_RESOURCES
);
1436 return (EFI_SUCCESS
);
1440 This function will eliminate unreplaced (and therefore non-found) environment variables.
1442 @param[in,out] CmdLine The command line to update.
1446 StripUnreplacedEnvironmentVariables(
1447 IN OUT CHAR16
*CmdLine
1450 CHAR16
*FirstPercent
;
1452 CHAR16
*SecondPercent
;
1453 CHAR16
*SecondQuote
;
1454 CHAR16
*CurrentLocator
;
1456 for (CurrentLocator
= CmdLine
; CurrentLocator
!= NULL
; ) {
1457 FirstQuote
= FindNextInstance(CurrentLocator
, L
"\"", TRUE
);
1458 FirstPercent
= FindNextInstance(CurrentLocator
, L
"%", TRUE
);
1459 SecondPercent
= FirstPercent
!=NULL
?FindNextInstance(FirstPercent
+1, L
"%", TRUE
):NULL
;
1460 if (FirstPercent
== NULL
|| SecondPercent
== NULL
) {
1462 // If we ever don't have 2 % we are done.
1467 if (FirstQuote
!= NULL
&& FirstQuote
< FirstPercent
) {
1468 SecondQuote
= FindNextInstance(FirstQuote
+1, L
"\"", TRUE
);
1470 // Quote is first found
1473 if (SecondQuote
< FirstPercent
) {
1475 // restart after the pair of "
1477 CurrentLocator
= SecondQuote
+ 1;
1478 } else /* FirstPercent < SecondQuote */{
1480 // Restart on the first percent
1482 CurrentLocator
= FirstPercent
;
1487 if (FirstQuote
== NULL
|| SecondPercent
< FirstQuote
) {
1488 if (IsValidEnvironmentVariableName(FirstPercent
, SecondPercent
)) {
1490 // We need to remove from FirstPercent to SecondPercent
1492 CopyMem(FirstPercent
, SecondPercent
+ 1, StrSize(SecondPercent
+ 1));
1494 // don't need to update the locator. both % characters are gone.
1497 CurrentLocator
= SecondPercent
+ 1;
1501 CurrentLocator
= FirstQuote
;
1503 return (EFI_SUCCESS
);
1507 Function allocates a new command line and replaces all instances of environment
1508 variable names that are correctly preset to their values.
1510 If the return value is not NULL the memory must be caller freed.
1512 @param[in] OriginalCommandLine The original command line
1514 @retval NULL An error occurred.
1515 @return The new command line with no environment variables present.
1519 ShellConvertVariables (
1520 IN CONST CHAR16
*OriginalCommandLine
1523 CONST CHAR16
*MasterEnvList
;
1525 CHAR16
*NewCommandLine1
;
1526 CHAR16
*NewCommandLine2
;
1530 SCRIPT_FILE
*CurrentScriptFile
;
1531 ALIAS_LIST
*AliasListNode
;
1533 ASSERT(OriginalCommandLine
!= NULL
);
1536 NewSize
= StrSize(OriginalCommandLine
);
1537 CurrentScriptFile
= ShellCommandGetCurrentScriptFile();
1540 ///@todo update this to handle the %0 - %9 for scripting only (borrow from line 1256 area) ? ? ?
1543 // calculate the size required for the post-conversion string...
1545 if (CurrentScriptFile
!= NULL
) {
1546 for (AliasListNode
= (ALIAS_LIST
*)GetFirstNode(&CurrentScriptFile
->SubstList
)
1547 ; !IsNull(&CurrentScriptFile
->SubstList
, &AliasListNode
->Link
)
1548 ; AliasListNode
= (ALIAS_LIST
*)GetNextNode(&CurrentScriptFile
->SubstList
, &AliasListNode
->Link
)
1550 for (Temp
= StrStr(OriginalCommandLine
, AliasListNode
->Alias
)
1552 ; Temp
= StrStr(Temp
+1, AliasListNode
->Alias
)
1555 // we need a preceding and if there is space no ^ preceding (if no space ignore)
1557 if ((((Temp
-OriginalCommandLine
)>2) && *(Temp
-2) != L
'^') || ((Temp
-OriginalCommandLine
)<=2)) {
1558 NewSize
+= StrSize(AliasListNode
->CommandString
);
1564 for (MasterEnvList
= EfiShellGetEnv(NULL
)
1565 ; MasterEnvList
!= NULL
&& *MasterEnvList
!= CHAR_NULL
//&& *(MasterEnvList+1) != CHAR_NULL
1566 ; MasterEnvList
+= StrLen(MasterEnvList
) + 1
1568 if (StrSize(MasterEnvList
) > ItemSize
) {
1569 ItemSize
= StrSize(MasterEnvList
);
1571 for (Temp
= StrStr(OriginalCommandLine
, MasterEnvList
)
1573 ; Temp
= StrStr(Temp
+1, MasterEnvList
)
1576 // we need a preceding and following % and if there is space no ^ preceding (if no space ignore)
1578 if (*(Temp
-1) == L
'%' && *(Temp
+StrLen(MasterEnvList
)) == L
'%' &&
1579 ((((Temp
-OriginalCommandLine
)>2) && *(Temp
-2) != L
'^') || ((Temp
-OriginalCommandLine
)<=2))) {
1580 NewSize
+=StrSize(EfiShellGetEnv(MasterEnvList
));
1586 // now do the replacements...
1588 NewCommandLine1
= AllocateCopyPool(NewSize
, OriginalCommandLine
);
1589 NewCommandLine2
= AllocateZeroPool(NewSize
);
1590 ItemTemp
= AllocateZeroPool(ItemSize
+(2*sizeof(CHAR16
)));
1591 if (NewCommandLine1
== NULL
|| NewCommandLine2
== NULL
|| ItemTemp
== NULL
) {
1592 SHELL_FREE_NON_NULL(NewCommandLine1
);
1593 SHELL_FREE_NON_NULL(NewCommandLine2
);
1594 SHELL_FREE_NON_NULL(ItemTemp
);
1597 for (MasterEnvList
= EfiShellGetEnv(NULL
)
1598 ; MasterEnvList
!= NULL
&& *MasterEnvList
!= CHAR_NULL
1599 ; MasterEnvList
+= StrLen(MasterEnvList
) + 1
1602 ((ItemSize
+(2*sizeof(CHAR16
)))/sizeof(CHAR16
)),
1606 ((ItemSize
+(2*sizeof(CHAR16
)))/sizeof(CHAR16
)),
1610 ((ItemSize
+(2*sizeof(CHAR16
)))/sizeof(CHAR16
)),
1613 ShellCopySearchAndReplace(NewCommandLine1
, NewCommandLine2
, NewSize
, ItemTemp
, EfiShellGetEnv(MasterEnvList
), TRUE
, FALSE
);
1614 StrCpyS(NewCommandLine1
, NewSize
/sizeof(CHAR16
), NewCommandLine2
);
1616 if (CurrentScriptFile
!= NULL
) {
1617 for (AliasListNode
= (ALIAS_LIST
*)GetFirstNode(&CurrentScriptFile
->SubstList
)
1618 ; !IsNull(&CurrentScriptFile
->SubstList
, &AliasListNode
->Link
)
1619 ; AliasListNode
= (ALIAS_LIST
*)GetNextNode(&CurrentScriptFile
->SubstList
, &AliasListNode
->Link
)
1621 ShellCopySearchAndReplace(NewCommandLine1
, NewCommandLine2
, NewSize
, AliasListNode
->Alias
, AliasListNode
->CommandString
, TRUE
, FALSE
);
1622 StrCpyS(NewCommandLine1
, NewSize
/sizeof(CHAR16
), NewCommandLine2
);
1627 // Remove non-existent environment variables
1629 StripUnreplacedEnvironmentVariables(NewCommandLine1
);
1632 // Now cleanup any straggler intentionally ignored "%" characters
1634 ShellCopySearchAndReplace(NewCommandLine1
, NewCommandLine2
, NewSize
, L
"^%", L
"%", TRUE
, FALSE
);
1635 StrCpyS(NewCommandLine1
, NewSize
/sizeof(CHAR16
), NewCommandLine2
);
1637 FreePool(NewCommandLine2
);
1640 return (NewCommandLine1
);
1644 Internal function to run a command line with pipe usage.
1646 @param[in] CmdLine The pointer to the command line.
1647 @param[in] StdIn The pointer to the Standard input.
1648 @param[in] StdOut The pointer to the Standard output.
1650 @retval EFI_SUCCESS The split command is executed successfully.
1651 @retval other Some error occurs when executing the split command.
1656 IN CONST CHAR16
*CmdLine
,
1657 IN SHELL_FILE_HANDLE
*StdIn
,
1658 IN SHELL_FILE_HANDLE
*StdOut
1662 CHAR16
*NextCommandLine
;
1663 CHAR16
*OurCommandLine
;
1667 SHELL_FILE_HANDLE
*TempFileHandle
;
1670 ASSERT(StdOut
== NULL
);
1672 ASSERT(StrStr(CmdLine
, L
"|") != NULL
);
1674 Status
= EFI_SUCCESS
;
1675 NextCommandLine
= NULL
;
1676 OurCommandLine
= NULL
;
1680 NextCommandLine
= StrnCatGrow(&NextCommandLine
, &Size1
, StrStr(CmdLine
, L
"|")+1, 0);
1681 OurCommandLine
= StrnCatGrow(&OurCommandLine
, &Size2
, CmdLine
, StrStr(CmdLine
, L
"|") - CmdLine
);
1683 if (NextCommandLine
== NULL
|| OurCommandLine
== NULL
) {
1684 SHELL_FREE_NON_NULL(OurCommandLine
);
1685 SHELL_FREE_NON_NULL(NextCommandLine
);
1686 return (EFI_OUT_OF_RESOURCES
);
1687 } else if (StrStr(OurCommandLine
, L
"|") != NULL
|| Size1
== 0 || Size2
== 0) {
1688 SHELL_FREE_NON_NULL(OurCommandLine
);
1689 SHELL_FREE_NON_NULL(NextCommandLine
);
1690 return (EFI_INVALID_PARAMETER
);
1691 } else if (NextCommandLine
[0] == L
'a' &&
1692 (NextCommandLine
[1] == L
' ' || NextCommandLine
[1] == CHAR_NULL
)
1694 CopyMem(NextCommandLine
, NextCommandLine
+1, StrSize(NextCommandLine
) - sizeof(NextCommandLine
[0]));
1695 while (NextCommandLine
[0] == L
' ') {
1696 CopyMem(NextCommandLine
, NextCommandLine
+1, StrSize(NextCommandLine
) - sizeof(NextCommandLine
[0]));
1698 if (NextCommandLine
[0] == CHAR_NULL
) {
1699 SHELL_FREE_NON_NULL(OurCommandLine
);
1700 SHELL_FREE_NON_NULL(NextCommandLine
);
1701 return (EFI_INVALID_PARAMETER
);
1710 // make a SPLIT_LIST item and add to list
1712 Split
= AllocateZeroPool(sizeof(SPLIT_LIST
));
1713 ASSERT(Split
!= NULL
);
1714 Split
->SplitStdIn
= StdIn
;
1715 Split
->SplitStdOut
= ConvertEfiFileProtocolToShellHandle(CreateFileInterfaceMem(Unicode
), NULL
);
1716 ASSERT(Split
->SplitStdOut
!= NULL
);
1717 InsertHeadList(&ShellInfoObject
.SplitList
.Link
, &Split
->Link
);
1719 Status
= RunCommand(OurCommandLine
);
1722 // move the output from the first to the in to the second.
1724 TempFileHandle
= Split
->SplitStdOut
;
1725 if (Split
->SplitStdIn
== StdIn
) {
1726 Split
->SplitStdOut
= NULL
;
1728 Split
->SplitStdOut
= Split
->SplitStdIn
;
1730 Split
->SplitStdIn
= TempFileHandle
;
1731 ShellInfoObject
.NewEfiShellProtocol
->SetFilePosition(ConvertShellHandleToEfiFileProtocol(Split
->SplitStdIn
), 0);
1733 if (!EFI_ERROR(Status
)) {
1734 Status
= RunCommand(NextCommandLine
);
1738 // remove the top level from the ScriptList
1740 ASSERT((SPLIT_LIST
*)GetFirstNode(&ShellInfoObject
.SplitList
.Link
) == Split
);
1741 RemoveEntryList(&Split
->Link
);
1744 // Note that the original StdIn is now the StdOut...
1746 if (Split
->SplitStdOut
!= NULL
&& Split
->SplitStdOut
!= StdIn
) {
1747 ShellInfoObject
.NewEfiShellProtocol
->CloseFile(ConvertShellHandleToEfiFileProtocol(Split
->SplitStdOut
));
1749 if (Split
->SplitStdIn
!= NULL
) {
1750 ShellInfoObject
.NewEfiShellProtocol
->CloseFile(ConvertShellHandleToEfiFileProtocol(Split
->SplitStdIn
));
1754 FreePool(NextCommandLine
);
1755 FreePool(OurCommandLine
);
1761 Take the original command line, substitute any variables, free
1762 the original string, return the modified copy.
1764 @param[in] CmdLine pointer to the command line to update.
1766 @retval EFI_SUCCESS the function was successful.
1767 @retval EFI_OUT_OF_RESOURCES a memory allocation failed.
1771 ShellSubstituteVariables(
1776 NewCmdLine
= ShellConvertVariables(*CmdLine
);
1777 SHELL_FREE_NON_NULL(*CmdLine
);
1778 if (NewCmdLine
== NULL
) {
1779 return (EFI_OUT_OF_RESOURCES
);
1781 *CmdLine
= NewCmdLine
;
1782 return (EFI_SUCCESS
);
1786 Take the original command line, substitute any alias in the first group of space delimited characters, free
1787 the original string, return the modified copy.
1789 @param[in] CmdLine pointer to the command line to update.
1791 @retval EFI_SUCCESS the function was successful.
1792 @retval EFI_OUT_OF_RESOURCES a memory allocation failed.
1796 ShellSubstituteAliases(
1801 CHAR16
*CommandName
;
1803 UINTN PostAliasSize
;
1804 ASSERT(CmdLine
!= NULL
);
1805 ASSERT(*CmdLine
!= NULL
);
1809 if (StrStr((*CmdLine
), L
" ") == NULL
){
1810 StrnCatGrow(&CommandName
, NULL
, (*CmdLine
), 0);
1812 StrnCatGrow(&CommandName
, NULL
, (*CmdLine
), StrStr((*CmdLine
), L
" ") - (*CmdLine
));
1816 // This cannot happen 'inline' since the CmdLine can need extra space.
1819 if (!ShellCommandIsCommandOnList(CommandName
)) {
1821 // Convert via alias
1823 Status
= ShellConvertAlias(&CommandName
);
1824 if (EFI_ERROR(Status
)){
1828 NewCmdLine
= StrnCatGrow(&NewCmdLine
, &PostAliasSize
, CommandName
, 0);
1829 if (NewCmdLine
== NULL
) {
1830 SHELL_FREE_NON_NULL(CommandName
);
1831 SHELL_FREE_NON_NULL(*CmdLine
);
1832 return (EFI_OUT_OF_RESOURCES
);
1834 NewCmdLine
= StrnCatGrow(&NewCmdLine
, &PostAliasSize
, StrStr((*CmdLine
), L
" "), 0);
1835 if (NewCmdLine
== NULL
) {
1836 SHELL_FREE_NON_NULL(CommandName
);
1837 SHELL_FREE_NON_NULL(*CmdLine
);
1838 return (EFI_OUT_OF_RESOURCES
);
1841 NewCmdLine
= StrnCatGrow(&NewCmdLine
, NULL
, (*CmdLine
), 0);
1844 SHELL_FREE_NON_NULL(*CmdLine
);
1845 SHELL_FREE_NON_NULL(CommandName
);
1848 // re-assign the passed in double pointer to point to our newly allocated buffer
1850 *CmdLine
= NewCmdLine
;
1852 return (EFI_SUCCESS
);
1856 Takes the Argv[0] part of the command line and determine the meaning of it.
1858 @param[in] CmdName pointer to the command line to update.
1860 @retval Internal_Command The name is an internal command.
1861 @retval File_Sys_Change the name is a file system change.
1862 @retval Script_File_Name the name is a NSH script file.
1863 @retval Unknown_Invalid the name is unknown.
1864 @retval Efi_Application the name is an application (.EFI).
1866 SHELL_OPERATION_TYPES
1869 IN CONST CHAR16
*CmdName
1872 CHAR16
* FileWithPath
;
1873 CONST CHAR16
* TempLocation
;
1874 CONST CHAR16
* TempLocation2
;
1876 FileWithPath
= NULL
;
1878 // test for an internal command.
1880 if (ShellCommandIsCommandOnList(CmdName
)) {
1881 return (Internal_Command
);
1885 // Test for file system change request. anything ending with first : and cant have spaces.
1887 if (CmdName
[(StrLen(CmdName
)-1)] == L
':') {
1888 if ( StrStr(CmdName
, L
" ") != NULL
1889 || StrLen(StrStr(CmdName
, L
":")) > 1
1891 return (Unknown_Invalid
);
1893 return (File_Sys_Change
);
1899 if ((FileWithPath
= ShellFindFilePathEx(CmdName
, mExecutableExtensions
)) != NULL
) {
1901 // See if that file has a script file extension
1903 if (StrLen(FileWithPath
) > 4) {
1904 TempLocation
= FileWithPath
+StrLen(FileWithPath
)-4;
1905 TempLocation2
= mScriptExtension
;
1906 if (StringNoCaseCompare((VOID
*)(&TempLocation
), (VOID
*)(&TempLocation2
)) == 0) {
1907 SHELL_FREE_NON_NULL(FileWithPath
);
1908 return (Script_File_Name
);
1913 // Was a file, but not a script. we treat this as an application.
1915 SHELL_FREE_NON_NULL(FileWithPath
);
1916 return (Efi_Application
);
1919 SHELL_FREE_NON_NULL(FileWithPath
);
1921 // No clue what this is... return invalid flag...
1923 return (Unknown_Invalid
);
1927 Determine if the first item in a command line is valid.
1929 @param[in] CmdLine The command line to parse.
1931 @retval EFI_SUCCESS The item is valid.
1932 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
1933 @retval EFI_NOT_FOUND The operation type is unknown or invalid.
1938 IN CONST CHAR16
*CmdLine
1942 CHAR16
*FirstParameter
;
1948 Temp
= StrnCatGrow(&Temp
, NULL
, CmdLine
, 0);
1950 return (EFI_OUT_OF_RESOURCES
);
1953 FirstParameter
= StrStr(Temp
, L
"|");
1954 if (FirstParameter
!= NULL
) {
1955 *FirstParameter
= CHAR_NULL
;
1958 FirstParameter
= NULL
;
1961 // Process the command line
1963 Status
= ProcessCommandLineToFinal(&Temp
);
1965 if (!EFI_ERROR(Status
)) {
1966 FirstParameter
= AllocateZeroPool(StrSize(CmdLine
));
1967 if (FirstParameter
== NULL
) {
1968 SHELL_FREE_NON_NULL(Temp
);
1969 return (EFI_OUT_OF_RESOURCES
);
1971 TempWalker
= (CHAR16
*)Temp
;
1972 if (!EFI_ERROR(GetNextParameter(&TempWalker
, &FirstParameter
, StrSize(CmdLine
), TRUE
))) {
1973 if (GetOperationType(FirstParameter
) == Unknown_Invalid
) {
1974 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_NOT_FOUND
), ShellInfoObject
.HiiHandle
, FirstParameter
);
1975 SetLastError(SHELL_NOT_FOUND
);
1976 Status
= EFI_NOT_FOUND
;
1981 SHELL_FREE_NON_NULL(Temp
);
1982 SHELL_FREE_NON_NULL(FirstParameter
);
1987 Determine if a command line contains with a split contains only valid commands.
1989 @param[in] CmdLine The command line to parse.
1991 @retval EFI_SUCCESS CmdLine has only valid commands, application, or has no split.
1992 @retval EFI_ABORTED CmdLine has at least one invalid command or application.
1997 IN CONST CHAR16
*CmdLine
2000 CONST CHAR16
*TempSpot
;
2004 // If this was the only item, then get out
2006 if (!ContainsSplit(CmdLine
)) {
2007 return (EFI_SUCCESS
);
2011 // Verify up to the pipe or end character
2013 Status
= IsValidSplit(CmdLine
);
2014 if (EFI_ERROR(Status
)) {
2019 // recurse to verify the next item
2021 TempSpot
= FindFirstCharacter(CmdLine
, L
"|", L
'^') + 1;
2022 if (*TempSpot
== L
'a' &&
2023 (*(TempSpot
+ 1) == L
' ' || *(TempSpot
+ 1) == CHAR_NULL
)
2025 // If it's an ASCII pipe '|a'
2029 return (VerifySplit(TempSpot
));
2033 Process a split based operation.
2035 @param[in] CmdLine pointer to the command line to process
2037 @retval EFI_SUCCESS The operation was successful
2038 @return an error occurred.
2042 ProcessNewSplitCommandLine(
2043 IN CONST CHAR16
*CmdLine
2049 Status
= VerifySplit(CmdLine
);
2050 if (EFI_ERROR(Status
)) {
2057 // are we in an existing split???
2059 if (!IsListEmpty(&ShellInfoObject
.SplitList
.Link
)) {
2060 Split
= (SPLIT_LIST
*)GetFirstNode(&ShellInfoObject
.SplitList
.Link
);
2063 if (Split
== NULL
) {
2064 Status
= RunSplitCommand(CmdLine
, NULL
, NULL
);
2066 Status
= RunSplitCommand(CmdLine
, Split
->SplitStdIn
, Split
->SplitStdOut
);
2068 if (EFI_ERROR(Status
)) {
2069 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_INVALID_SPLIT
), ShellInfoObject
.HiiHandle
, CmdLine
);
2075 Handle a request to change the current file system.
2077 @param[in] CmdLine The passed in command line.
2079 @retval EFI_SUCCESS The operation was successful.
2084 IN CONST CHAR16
*CmdLine
2088 Status
= EFI_SUCCESS
;
2091 // make sure we are the right operation
2093 ASSERT(CmdLine
[(StrLen(CmdLine
)-1)] == L
':' && StrStr(CmdLine
, L
" ") == NULL
);
2096 // Call the protocol API to do the work
2098 Status
= ShellInfoObject
.NewEfiShellProtocol
->SetCurDir(NULL
, CmdLine
);
2101 // Report any errors
2103 if (EFI_ERROR(Status
)) {
2104 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_INVALID_MAPPING
), ShellInfoObject
.HiiHandle
, CmdLine
);
2111 Reprocess the command line to direct all -? to the help command.
2113 if found, will add "help" as argv[0], and move the rest later.
2115 @param[in,out] CmdLine pointer to the command line to update
2120 IN OUT CHAR16
**CmdLine
2123 CHAR16
*CurrentParameter
;
2125 CHAR16
*NewCommandLine
;
2127 UINTN NewCmdLineSize
;
2129 Status
= EFI_SUCCESS
;
2131 CurrentParameter
= AllocateZeroPool(StrSize(*CmdLine
));
2132 if (CurrentParameter
== NULL
) {
2133 return (EFI_OUT_OF_RESOURCES
);
2137 while(Walker
!= NULL
&& *Walker
!= CHAR_NULL
) {
2138 if (!EFI_ERROR(GetNextParameter(&Walker
, &CurrentParameter
, StrSize(*CmdLine
), TRUE
))) {
2139 if (StrStr(CurrentParameter
, L
"-?") == CurrentParameter
) {
2140 CurrentParameter
[0] = L
' ';
2141 CurrentParameter
[1] = L
' ';
2142 NewCmdLineSize
= StrSize(L
"help ") + StrSize(*CmdLine
);
2143 NewCommandLine
= AllocateZeroPool(NewCmdLineSize
);
2144 if (NewCommandLine
== NULL
) {
2145 Status
= EFI_OUT_OF_RESOURCES
;
2150 // We know the space is sufficient since we just calculated it.
2152 StrnCpyS(NewCommandLine
, NewCmdLineSize
/sizeof(CHAR16
), L
"help ", 5);
2153 StrnCatS(NewCommandLine
, NewCmdLineSize
/sizeof(CHAR16
), *CmdLine
, StrLen(*CmdLine
));
2154 SHELL_FREE_NON_NULL(*CmdLine
);
2155 *CmdLine
= NewCommandLine
;
2161 SHELL_FREE_NON_NULL(CurrentParameter
);
2167 Function to update the shell variable "lasterror".
2169 @param[in] ErrorCode the error code to put into lasterror.
2174 IN CONST SHELL_STATUS ErrorCode
2177 CHAR16 LeString
[19];
2178 if (sizeof(EFI_STATUS
) == sizeof(UINT64
)) {
2179 UnicodeSPrint(LeString
, sizeof(LeString
), L
"0x%Lx", ErrorCode
);
2181 UnicodeSPrint(LeString
, sizeof(LeString
), L
"0x%x", ErrorCode
);
2183 DEBUG_CODE(InternalEfiShellSetEnv(L
"debuglasterror", LeString
, TRUE
););
2184 InternalEfiShellSetEnv(L
"lasterror", LeString
, TRUE
);
2186 return (EFI_SUCCESS
);
2190 Converts the command line to it's post-processed form. this replaces variables and alias' per UEFI Shell spec.
2192 @param[in,out] CmdLine pointer to the command line to update
2194 @retval EFI_SUCCESS The operation was successful
2195 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
2196 @return some other error occurred
2200 ProcessCommandLineToFinal(
2201 IN OUT CHAR16
**CmdLine
2205 TrimSpaces(CmdLine
);
2207 Status
= ShellSubstituteAliases(CmdLine
);
2208 if (EFI_ERROR(Status
)) {
2212 TrimSpaces(CmdLine
);
2214 Status
= ShellSubstituteVariables(CmdLine
);
2215 if (EFI_ERROR(Status
)) {
2218 ASSERT (*CmdLine
!= NULL
);
2220 TrimSpaces(CmdLine
);
2223 // update for help parsing
2225 if (StrStr(*CmdLine
, L
"?") != NULL
) {
2227 // This may do nothing if the ? does not indicate help.
2228 // Save all the details for in the API below.
2230 Status
= DoHelpUpdate(CmdLine
);
2233 TrimSpaces(CmdLine
);
2235 return (EFI_SUCCESS
);
2239 Run an internal shell command.
2241 This API will update the shell's environment since these commands are libraries.
2243 @param[in] CmdLine the command line to run.
2244 @param[in] FirstParameter the first parameter on the command line
2245 @param[in] ParamProtocol the shell parameters protocol pointer
2246 @param[out] CommandStatus the status from the command line.
2248 @retval EFI_SUCCESS The command was completed.
2249 @retval EFI_ABORTED The command's operation was aborted.
2254 IN CONST CHAR16
*CmdLine
,
2255 IN CHAR16
*FirstParameter
,
2256 IN EFI_SHELL_PARAMETERS_PROTOCOL
*ParamProtocol
,
2257 OUT EFI_STATUS
*CommandStatus
2263 SHELL_STATUS CommandReturnedStatus
;
2268 NewCmdLine
= AllocateCopyPool (StrSize (CmdLine
), CmdLine
);
2269 if (NewCmdLine
== NULL
) {
2270 return EFI_OUT_OF_RESOURCES
;
2273 for (Walker
= NewCmdLine
; Walker
!= NULL
&& *Walker
!= CHAR_NULL
; Walker
++) {
2274 if (*Walker
== L
'^' && *(Walker
+1) == L
'#') {
2275 CopyMem(Walker
, Walker
+1, StrSize(Walker
) - sizeof(Walker
[0]));
2280 // get the argc and argv updated for internal commands
2282 Status
= UpdateArgcArgv(ParamProtocol
, NewCmdLine
, Internal_Command
, &Argv
, &Argc
);
2283 if (!EFI_ERROR(Status
)) {
2285 // Run the internal command.
2287 Status
= ShellCommandRunCommandHandler(FirstParameter
, &CommandReturnedStatus
, &LastError
);
2289 if (!EFI_ERROR(Status
)) {
2290 if (CommandStatus
!= NULL
) {
2291 if (CommandReturnedStatus
!= SHELL_SUCCESS
) {
2292 *CommandStatus
= (EFI_STATUS
)(CommandReturnedStatus
| MAX_BIT
);
2294 *CommandStatus
= EFI_SUCCESS
;
2299 // Update last error status.
2300 // some commands do not update last error.
2303 SetLastError(CommandReturnedStatus
);
2307 // Pass thru the exitcode from the app.
2309 if (ShellCommandGetExit()) {
2311 // An Exit was requested ("exit" command), pass its value up.
2313 Status
= CommandReturnedStatus
;
2314 } else if (CommandReturnedStatus
!= SHELL_SUCCESS
&& IsScriptOnlyCommand(FirstParameter
)) {
2316 // Always abort when a script only command fails for any reason
2318 Status
= EFI_ABORTED
;
2319 } else if (ShellCommandGetCurrentScriptFile() != NULL
&& CommandReturnedStatus
== SHELL_ABORTED
) {
2321 // Abort when in a script and a command aborted
2323 Status
= EFI_ABORTED
;
2329 // This is guaranteed to be called after UpdateArgcArgv no matter what else happened.
2330 // This is safe even if the update API failed. In this case, it may be a no-op.
2332 RestoreArgcArgv(ParamProtocol
, &Argv
, &Argc
);
2335 // If a script is running and the command is not a script only command, then
2336 // change return value to success so the script won't halt (unless aborted).
2338 // Script only commands have to be able halt the script since the script will
2339 // not operate if they are failing.
2341 if ( ShellCommandGetCurrentScriptFile() != NULL
2342 && !IsScriptOnlyCommand(FirstParameter
)
2343 && Status
!= EFI_ABORTED
2345 Status
= EFI_SUCCESS
;
2348 FreePool (NewCmdLine
);
2353 Function to run the command or file.
2355 @param[in] Type the type of operation being run.
2356 @param[in] CmdLine the command line to run.
2357 @param[in] FirstParameter the first parameter on the command line
2358 @param[in] ParamProtocol the shell parameters protocol pointer
2359 @param[out] CommandStatus the status from the command line.
2361 @retval EFI_SUCCESS The command was completed.
2362 @retval EFI_ABORTED The command's operation was aborted.
2367 IN SHELL_OPERATION_TYPES Type
,
2368 IN CONST CHAR16
*CmdLine
,
2369 IN CHAR16
*FirstParameter
,
2370 IN EFI_SHELL_PARAMETERS_PROTOCOL
*ParamProtocol
,
2371 OUT EFI_STATUS
*CommandStatus
2375 EFI_STATUS StartStatus
;
2376 CHAR16
*CommandWithPath
;
2377 EFI_DEVICE_PATH_PROTOCOL
*DevPath
;
2378 SHELL_STATUS CalleeExitStatus
;
2380 Status
= EFI_SUCCESS
;
2381 CommandWithPath
= NULL
;
2383 CalleeExitStatus
= SHELL_INVALID_PARAMETER
;
2386 case Internal_Command
:
2387 Status
= RunInternalCommand(CmdLine
, FirstParameter
, ParamProtocol
, CommandStatus
);
2389 case Script_File_Name
:
2390 case Efi_Application
:
2392 // Process a fully qualified path
2394 if (StrStr(FirstParameter
, L
":") != NULL
) {
2395 ASSERT (CommandWithPath
== NULL
);
2396 if (ShellIsFile(FirstParameter
) == EFI_SUCCESS
) {
2397 CommandWithPath
= StrnCatGrow(&CommandWithPath
, NULL
, FirstParameter
, 0);
2402 // Process a relative path and also check in the path environment variable
2404 if (CommandWithPath
== NULL
) {
2405 CommandWithPath
= ShellFindFilePathEx(FirstParameter
, mExecutableExtensions
);
2409 // This should be impossible now.
2411 ASSERT(CommandWithPath
!= NULL
);
2414 // Make sure that path is not just a directory (or not found)
2416 if (!EFI_ERROR(ShellIsDirectory(CommandWithPath
))) {
2417 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_NOT_FOUND
), ShellInfoObject
.HiiHandle
, FirstParameter
);
2418 SetLastError(SHELL_NOT_FOUND
);
2421 case Script_File_Name
:
2422 Status
= RunScriptFile (CommandWithPath
, NULL
, CmdLine
, ParamProtocol
);
2424 case Efi_Application
:
2426 // Get the device path of the application image
2428 DevPath
= ShellInfoObject
.NewEfiShellProtocol
->GetDevicePathFromFilePath(CommandWithPath
);
2429 if (DevPath
== NULL
){
2430 Status
= EFI_OUT_OF_RESOURCES
;
2435 // Execute the device path
2437 Status
= InternalShellExecuteDevicePath(
2445 SHELL_FREE_NON_NULL(DevPath
);
2447 if(EFI_ERROR (Status
)) {
2448 CalleeExitStatus
= (SHELL_STATUS
) (Status
& (~MAX_BIT
));
2450 CalleeExitStatus
= (SHELL_STATUS
) StartStatus
;
2453 if (CommandStatus
!= NULL
) {
2454 *CommandStatus
= CalleeExitStatus
;
2458 // Update last error status.
2460 // Status is an EFI_STATUS. Clear top bit to convert to SHELL_STATUS
2461 SetLastError(CalleeExitStatus
);
2477 SHELL_FREE_NON_NULL(CommandWithPath
);
2483 Function to setup StdIn, StdErr, StdOut, and then run the command or file.
2485 @param[in] Type the type of operation being run.
2486 @param[in] CmdLine the command line to run.
2487 @param[in] FirstParameter the first parameter on the command line.
2488 @param[in] ParamProtocol the shell parameters protocol pointer
2489 @param[out] CommandStatus the status from the command line.
2491 @retval EFI_SUCCESS The command was completed.
2492 @retval EFI_ABORTED The command's operation was aborted.
2496 SetupAndRunCommandOrFile(
2497 IN SHELL_OPERATION_TYPES Type
,
2499 IN CHAR16
*FirstParameter
,
2500 IN EFI_SHELL_PARAMETERS_PROTOCOL
*ParamProtocol
,
2501 OUT EFI_STATUS
*CommandStatus
2505 SHELL_FILE_HANDLE OriginalStdIn
;
2506 SHELL_FILE_HANDLE OriginalStdOut
;
2507 SHELL_FILE_HANDLE OriginalStdErr
;
2508 SYSTEM_TABLE_INFO OriginalSystemTableInfo
;
2509 CONST SCRIPT_FILE
*ConstScriptFile
;
2512 // Update the StdIn, StdOut, and StdErr for redirection to environment variables, files, etc... unicode and ASCII
2514 Status
= UpdateStdInStdOutStdErr(ParamProtocol
, CmdLine
, &OriginalStdIn
, &OriginalStdOut
, &OriginalStdErr
, &OriginalSystemTableInfo
);
2517 // The StdIn, StdOut, and StdErr are set up.
2518 // Now run the command, script, or application
2520 if (!EFI_ERROR(Status
)) {
2521 TrimSpaces(&CmdLine
);
2522 Status
= RunCommandOrFile(Type
, CmdLine
, FirstParameter
, ParamProtocol
, CommandStatus
);
2528 if (EFI_ERROR(Status
)) {
2529 ConstScriptFile
= ShellCommandGetCurrentScriptFile();
2530 if (ConstScriptFile
== NULL
|| ConstScriptFile
->CurrentCommand
== NULL
) {
2531 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_ERROR
), ShellInfoObject
.HiiHandle
, (VOID
*)(Status
));
2533 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_ERROR_SCRIPT
), ShellInfoObject
.HiiHandle
, (VOID
*)(Status
), ConstScriptFile
->CurrentCommand
->Line
);
2538 // put back the original StdIn, StdOut, and StdErr
2540 RestoreStdInStdOutStdErr(ParamProtocol
, &OriginalStdIn
, &OriginalStdOut
, &OriginalStdErr
, &OriginalSystemTableInfo
);
2546 Function will process and run a command line.
2548 This will determine if the command line represents an internal shell
2549 command or dispatch an external application.
2551 @param[in] CmdLine The command line to parse.
2552 @param[out] CommandStatus The status from the command line.
2554 @retval EFI_SUCCESS The command was completed.
2555 @retval EFI_ABORTED The command's operation was aborted.
2560 IN CONST CHAR16
*CmdLine
,
2561 OUT EFI_STATUS
*CommandStatus
2565 CHAR16
*CleanOriginal
;
2566 CHAR16
*FirstParameter
;
2568 SHELL_OPERATION_TYPES Type
;
2570 ASSERT(CmdLine
!= NULL
);
2571 if (StrLen(CmdLine
) == 0) {
2572 return (EFI_SUCCESS
);
2575 Status
= EFI_SUCCESS
;
2576 CleanOriginal
= NULL
;
2578 CleanOriginal
= StrnCatGrow(&CleanOriginal
, NULL
, CmdLine
, 0);
2579 if (CleanOriginal
== NULL
) {
2580 return (EFI_OUT_OF_RESOURCES
);
2583 TrimSpaces(&CleanOriginal
);
2586 // NULL out comments (leveraged from RunScriptFileHandle() ).
2587 // The # character on a line is used to denote that all characters on the same line
2588 // and to the right of the # are to be ignored by the shell.
2589 // Afterwards, again remove spaces, in case any were between the last command-parameter and '#'.
2591 for (TempWalker
= CleanOriginal
; TempWalker
!= NULL
&& *TempWalker
!= CHAR_NULL
; TempWalker
++) {
2592 if (*TempWalker
== L
'^') {
2593 if (*(TempWalker
+ 1) == L
'#') {
2596 } else if (*TempWalker
== L
'#') {
2597 *TempWalker
= CHAR_NULL
;
2601 TrimSpaces(&CleanOriginal
);
2604 // Handle case that passed in command line is just 1 or more " " characters.
2606 if (StrLen (CleanOriginal
) == 0) {
2607 SHELL_FREE_NON_NULL(CleanOriginal
);
2608 return (EFI_SUCCESS
);
2611 Status
= ProcessCommandLineToFinal(&CleanOriginal
);
2612 if (EFI_ERROR(Status
)) {
2613 SHELL_FREE_NON_NULL(CleanOriginal
);
2618 // We don't do normal processing with a split command line (output from one command input to another)
2620 if (ContainsSplit(CleanOriginal
)) {
2621 Status
= ProcessNewSplitCommandLine(CleanOriginal
);
2622 SHELL_FREE_NON_NULL(CleanOriginal
);
2627 // We need the first parameter information so we can determine the operation type
2629 FirstParameter
= AllocateZeroPool(StrSize(CleanOriginal
));
2630 if (FirstParameter
== NULL
) {
2631 SHELL_FREE_NON_NULL(CleanOriginal
);
2632 return (EFI_OUT_OF_RESOURCES
);
2634 TempWalker
= CleanOriginal
;
2635 if (!EFI_ERROR(GetNextParameter(&TempWalker
, &FirstParameter
, StrSize(CleanOriginal
), TRUE
))) {
2637 // Depending on the first parameter we change the behavior
2639 switch (Type
= GetOperationType(FirstParameter
)) {
2640 case File_Sys_Change
:
2641 Status
= ChangeMappedDrive (FirstParameter
);
2643 case Internal_Command
:
2644 case Script_File_Name
:
2645 case Efi_Application
:
2646 Status
= SetupAndRunCommandOrFile(Type
, CleanOriginal
, FirstParameter
, ShellInfoObject
.NewShellParametersProtocol
, CommandStatus
);
2650 // Whatever was typed, it was invalid.
2652 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_NOT_FOUND
), ShellInfoObject
.HiiHandle
, FirstParameter
);
2653 SetLastError(SHELL_NOT_FOUND
);
2657 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_NOT_FOUND
), ShellInfoObject
.HiiHandle
, FirstParameter
);
2658 SetLastError(SHELL_NOT_FOUND
);
2661 SHELL_FREE_NON_NULL(CleanOriginal
);
2662 SHELL_FREE_NON_NULL(FirstParameter
);
2668 Function will process and run a command line.
2670 This will determine if the command line represents an internal shell
2671 command or dispatch an external application.
2673 @param[in] CmdLine The command line to parse.
2675 @retval EFI_SUCCESS The command was completed.
2676 @retval EFI_ABORTED The command's operation was aborted.
2681 IN CONST CHAR16
*CmdLine
2684 return (RunShellCommand(CmdLine
, NULL
));
2688 STATIC CONST UINT16 InvalidChars
[] = {L
'*', L
'?', L
'<', L
'>', L
'\\', L
'/', L
'\"', 0x0001, 0x0002};
2690 Function determines if the CommandName COULD be a valid command. It does not determine whether
2691 this is a valid command. It only checks for invalid characters.
2693 @param[in] CommandName The name to check
2695 @retval TRUE CommandName could be a command name
2696 @retval FALSE CommandName could not be a valid command name
2701 IN CONST CHAR16
*CommandName
2705 if (CommandName
== NULL
) {
2710 ; Count
< sizeof(InvalidChars
) / sizeof(InvalidChars
[0])
2713 if (ScanMem16(CommandName
, StrSize(CommandName
), InvalidChars
[Count
]) != NULL
) {
2721 Function to process a NSH script file via SHELL_FILE_HANDLE.
2723 @param[in] Handle The handle to the already opened file.
2724 @param[in] Name The name of the script file.
2726 @retval EFI_SUCCESS the script completed successfully
2730 RunScriptFileHandle (
2731 IN SHELL_FILE_HANDLE Handle
,
2732 IN CONST CHAR16
*Name
2736 SCRIPT_FILE
*NewScriptFile
;
2738 UINTN PrintBuffSize
;
2739 CHAR16
*CommandLine
;
2740 CHAR16
*CommandLine2
;
2741 CHAR16
*CommandLine3
;
2742 SCRIPT_COMMAND_LIST
*LastCommand
;
2744 BOOLEAN PreScriptEchoState
;
2745 BOOLEAN PreCommandEchoState
;
2746 CONST CHAR16
*CurDir
;
2748 CHAR16 LeString
[50];
2749 LIST_ENTRY OldBufferList
;
2751 ASSERT(!ShellCommandGetScriptExit());
2753 PreScriptEchoState
= ShellCommandGetEchoState();
2754 PrintBuffSize
= PcdGet16(PcdShellPrintBufferSize
);
2756 NewScriptFile
= (SCRIPT_FILE
*)AllocateZeroPool(sizeof(SCRIPT_FILE
));
2757 if (NewScriptFile
== NULL
) {
2758 return (EFI_OUT_OF_RESOURCES
);
2764 ASSERT(NewScriptFile
->ScriptName
== NULL
);
2765 NewScriptFile
->ScriptName
= StrnCatGrow(&NewScriptFile
->ScriptName
, NULL
, Name
, 0);
2766 if (NewScriptFile
->ScriptName
== NULL
) {
2767 DeleteScriptFileStruct(NewScriptFile
);
2768 return (EFI_OUT_OF_RESOURCES
);
2772 // Save the parameters (used to replace %0 to %9 later on)
2774 NewScriptFile
->Argc
= ShellInfoObject
.NewShellParametersProtocol
->Argc
;
2775 if (NewScriptFile
->Argc
!= 0) {
2776 NewScriptFile
->Argv
= (CHAR16
**)AllocateZeroPool(NewScriptFile
->Argc
* sizeof(CHAR16
*));
2777 if (NewScriptFile
->Argv
== NULL
) {
2778 DeleteScriptFileStruct(NewScriptFile
);
2779 return (EFI_OUT_OF_RESOURCES
);
2781 for (LoopVar
= 0 ; LoopVar
< 10 && LoopVar
< NewScriptFile
->Argc
; LoopVar
++) {
2782 ASSERT(NewScriptFile
->Argv
[LoopVar
] == NULL
);
2783 NewScriptFile
->Argv
[LoopVar
] = StrnCatGrow(&NewScriptFile
->Argv
[LoopVar
], NULL
, ShellInfoObject
.NewShellParametersProtocol
->Argv
[LoopVar
], 0);
2784 if (NewScriptFile
->Argv
[LoopVar
] == NULL
) {
2785 DeleteScriptFileStruct(NewScriptFile
);
2786 return (EFI_OUT_OF_RESOURCES
);
2790 NewScriptFile
->Argv
= NULL
;
2793 InitializeListHead(&NewScriptFile
->CommandList
);
2794 InitializeListHead(&NewScriptFile
->SubstList
);
2797 // Now build the list of all script commands.
2800 while(!ShellFileHandleEof(Handle
)) {
2801 CommandLine
= ShellFileHandleReturnLine(Handle
, &Ascii
);
2803 if (CommandLine
== NULL
|| StrLen(CommandLine
) == 0 || CommandLine
[0] == '#') {
2804 SHELL_FREE_NON_NULL(CommandLine
);
2807 NewScriptFile
->CurrentCommand
= AllocateZeroPool(sizeof(SCRIPT_COMMAND_LIST
));
2808 if (NewScriptFile
->CurrentCommand
== NULL
) {
2809 SHELL_FREE_NON_NULL(CommandLine
);
2810 DeleteScriptFileStruct(NewScriptFile
);
2811 return (EFI_OUT_OF_RESOURCES
);
2814 NewScriptFile
->CurrentCommand
->Cl
= CommandLine
;
2815 NewScriptFile
->CurrentCommand
->Data
= NULL
;
2816 NewScriptFile
->CurrentCommand
->Line
= LineCount
;
2818 InsertTailList(&NewScriptFile
->CommandList
, &NewScriptFile
->CurrentCommand
->Link
);
2822 // Add this as the topmost script file
2824 ShellCommandSetNewScript (NewScriptFile
);
2827 // Now enumerate through the commands and run each one.
2829 CommandLine
= AllocateZeroPool(PrintBuffSize
);
2830 if (CommandLine
== NULL
) {
2831 DeleteScriptFileStruct(NewScriptFile
);
2832 return (EFI_OUT_OF_RESOURCES
);
2834 CommandLine2
= AllocateZeroPool(PrintBuffSize
);
2835 if (CommandLine2
== NULL
) {
2836 FreePool(CommandLine
);
2837 DeleteScriptFileStruct(NewScriptFile
);
2838 return (EFI_OUT_OF_RESOURCES
);
2841 for ( NewScriptFile
->CurrentCommand
= (SCRIPT_COMMAND_LIST
*)GetFirstNode(&NewScriptFile
->CommandList
)
2842 ; !IsNull(&NewScriptFile
->CommandList
, &NewScriptFile
->CurrentCommand
->Link
)
2843 ; // conditional increment in the body of the loop
2845 ASSERT(CommandLine2
!= NULL
);
2846 StrnCpyS( CommandLine2
,
2847 PrintBuffSize
/sizeof(CHAR16
),
2848 NewScriptFile
->CurrentCommand
->Cl
,
2849 PrintBuffSize
/sizeof(CHAR16
) - 1
2852 SaveBufferList(&OldBufferList
);
2855 // NULL out comments
2857 for (CommandLine3
= CommandLine2
; CommandLine3
!= NULL
&& *CommandLine3
!= CHAR_NULL
; CommandLine3
++) {
2858 if (*CommandLine3
== L
'^') {
2859 if ( *(CommandLine3
+1) == L
':') {
2860 CopyMem(CommandLine3
, CommandLine3
+1, StrSize(CommandLine3
) - sizeof(CommandLine3
[0]));
2861 } else if (*(CommandLine3
+1) == L
'#') {
2864 } else if (*CommandLine3
== L
'#') {
2865 *CommandLine3
= CHAR_NULL
;
2869 if (CommandLine2
!= NULL
&& StrLen(CommandLine2
) >= 1) {
2871 // Due to variability in starting the find and replace action we need to have both buffers the same.
2873 StrnCpyS( CommandLine
,
2874 PrintBuffSize
/sizeof(CHAR16
),
2876 PrintBuffSize
/sizeof(CHAR16
) - 1
2880 // Remove the %0 to %9 from the command line (if we have some arguments)
2882 if (NewScriptFile
->Argv
!= NULL
) {
2883 switch (NewScriptFile
->Argc
) {
2885 Status
= ShellCopySearchAndReplace(CommandLine2
, CommandLine
, PrintBuffSize
, L
"%9", NewScriptFile
->Argv
[9], FALSE
, FALSE
);
2886 ASSERT_EFI_ERROR(Status
);
2888 Status
= ShellCopySearchAndReplace(CommandLine
, CommandLine2
, PrintBuffSize
, L
"%8", NewScriptFile
->Argv
[8], FALSE
, FALSE
);
2889 ASSERT_EFI_ERROR(Status
);
2891 Status
= ShellCopySearchAndReplace(CommandLine2
, CommandLine
, PrintBuffSize
, L
"%7", NewScriptFile
->Argv
[7], FALSE
, FALSE
);
2892 ASSERT_EFI_ERROR(Status
);
2894 Status
= ShellCopySearchAndReplace(CommandLine
, CommandLine2
, PrintBuffSize
, L
"%6", NewScriptFile
->Argv
[6], FALSE
, FALSE
);
2895 ASSERT_EFI_ERROR(Status
);
2897 Status
= ShellCopySearchAndReplace(CommandLine2
, CommandLine
, PrintBuffSize
, L
"%5", NewScriptFile
->Argv
[5], FALSE
, FALSE
);
2898 ASSERT_EFI_ERROR(Status
);
2900 Status
= ShellCopySearchAndReplace(CommandLine
, CommandLine2
, PrintBuffSize
, L
"%4", NewScriptFile
->Argv
[4], FALSE
, FALSE
);
2901 ASSERT_EFI_ERROR(Status
);
2903 Status
= ShellCopySearchAndReplace(CommandLine2
, CommandLine
, PrintBuffSize
, L
"%3", NewScriptFile
->Argv
[3], FALSE
, FALSE
);
2904 ASSERT_EFI_ERROR(Status
);
2906 Status
= ShellCopySearchAndReplace(CommandLine
, CommandLine2
, PrintBuffSize
, L
"%2", NewScriptFile
->Argv
[2], FALSE
, FALSE
);
2907 ASSERT_EFI_ERROR(Status
);
2909 Status
= ShellCopySearchAndReplace(CommandLine2
, CommandLine
, PrintBuffSize
, L
"%1", NewScriptFile
->Argv
[1], FALSE
, FALSE
);
2910 ASSERT_EFI_ERROR(Status
);
2912 Status
= ShellCopySearchAndReplace(CommandLine
, CommandLine2
, PrintBuffSize
, L
"%0", NewScriptFile
->Argv
[0], FALSE
, FALSE
);
2913 ASSERT_EFI_ERROR(Status
);
2919 Status
= ShellCopySearchAndReplace(CommandLine2
, CommandLine
, PrintBuffSize
, L
"%1", L
"\"\"", FALSE
, FALSE
);
2920 Status
= ShellCopySearchAndReplace(CommandLine
, CommandLine2
, PrintBuffSize
, L
"%2", L
"\"\"", FALSE
, FALSE
);
2921 Status
= ShellCopySearchAndReplace(CommandLine2
, CommandLine
, PrintBuffSize
, L
"%3", L
"\"\"", FALSE
, FALSE
);
2922 Status
= ShellCopySearchAndReplace(CommandLine
, CommandLine2
, PrintBuffSize
, L
"%4", L
"\"\"", FALSE
, FALSE
);
2923 Status
= ShellCopySearchAndReplace(CommandLine2
, CommandLine
, PrintBuffSize
, L
"%5", L
"\"\"", FALSE
, FALSE
);
2924 Status
= ShellCopySearchAndReplace(CommandLine
, CommandLine2
, PrintBuffSize
, L
"%6", L
"\"\"", FALSE
, FALSE
);
2925 Status
= ShellCopySearchAndReplace(CommandLine2
, CommandLine
, PrintBuffSize
, L
"%7", L
"\"\"", FALSE
, FALSE
);
2926 Status
= ShellCopySearchAndReplace(CommandLine
, CommandLine2
, PrintBuffSize
, L
"%8", L
"\"\"", FALSE
, FALSE
);
2927 Status
= ShellCopySearchAndReplace(CommandLine2
, CommandLine
, PrintBuffSize
, L
"%9", L
"\"\"", FALSE
, FALSE
);
2929 StrnCpyS( CommandLine2
,
2930 PrintBuffSize
/sizeof(CHAR16
),
2932 PrintBuffSize
/sizeof(CHAR16
) - 1
2935 LastCommand
= NewScriptFile
->CurrentCommand
;
2937 for (CommandLine3
= CommandLine2
; CommandLine3
[0] == L
' ' ; CommandLine3
++);
2939 if (CommandLine3
!= NULL
&& CommandLine3
[0] == L
':' ) {
2941 // This line is a goto target / label
2944 if (CommandLine3
!= NULL
&& StrLen(CommandLine3
) > 0) {
2945 if (CommandLine3
[0] == L
'@') {
2947 // We need to save the current echo state
2948 // and disable echo for just this command.
2950 PreCommandEchoState
= ShellCommandGetEchoState();
2951 ShellCommandSetEchoState(FALSE
);
2952 Status
= RunCommand(CommandLine3
+1);
2955 // If command was "@echo -off" or "@echo -on" then don't restore echo state
2957 if (StrCmp (L
"@echo -off", CommandLine3
) != 0 &&
2958 StrCmp (L
"@echo -on", CommandLine3
) != 0) {
2960 // Now restore the pre-'@' echo state.
2962 ShellCommandSetEchoState(PreCommandEchoState
);
2965 if (ShellCommandGetEchoState()) {
2966 CurDir
= ShellInfoObject
.NewEfiShellProtocol
->GetEnv(L
"cwd");
2967 if (CurDir
!= NULL
&& StrLen(CurDir
) > 1) {
2968 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_CURDIR
), ShellInfoObject
.HiiHandle
, CurDir
);
2970 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_SHELL
), ShellInfoObject
.HiiHandle
);
2972 ShellPrintEx(-1, -1, L
"%s\r\n", CommandLine2
);
2974 Status
= RunCommand(CommandLine3
);
2978 if (ShellCommandGetScriptExit()) {
2980 // ShellCommandGetExitCode() always returns a UINT64
2982 UnicodeSPrint(LeString
, sizeof(LeString
), L
"0x%Lx", ShellCommandGetExitCode());
2983 DEBUG_CODE(InternalEfiShellSetEnv(L
"debuglasterror", LeString
, TRUE
););
2984 InternalEfiShellSetEnv(L
"lasterror", LeString
, TRUE
);
2986 ShellCommandRegisterExit(FALSE
, 0);
2987 Status
= EFI_SUCCESS
;
2988 RestoreBufferList(&OldBufferList
);
2991 if (ShellGetExecutionBreakFlag()) {
2992 RestoreBufferList(&OldBufferList
);
2995 if (EFI_ERROR(Status
)) {
2996 RestoreBufferList(&OldBufferList
);
2999 if (ShellCommandGetExit()) {
3000 RestoreBufferList(&OldBufferList
);
3005 // If that commend did not update the CurrentCommand then we need to advance it...
3007 if (LastCommand
== NewScriptFile
->CurrentCommand
) {
3008 NewScriptFile
->CurrentCommand
= (SCRIPT_COMMAND_LIST
*)GetNextNode(&NewScriptFile
->CommandList
, &NewScriptFile
->CurrentCommand
->Link
);
3009 if (!IsNull(&NewScriptFile
->CommandList
, &NewScriptFile
->CurrentCommand
->Link
)) {
3010 NewScriptFile
->CurrentCommand
->Reset
= TRUE
;
3014 NewScriptFile
->CurrentCommand
= (SCRIPT_COMMAND_LIST
*)GetNextNode(&NewScriptFile
->CommandList
, &NewScriptFile
->CurrentCommand
->Link
);
3015 if (!IsNull(&NewScriptFile
->CommandList
, &NewScriptFile
->CurrentCommand
->Link
)) {
3016 NewScriptFile
->CurrentCommand
->Reset
= TRUE
;
3019 RestoreBufferList(&OldBufferList
);
3023 FreePool(CommandLine
);
3024 FreePool(CommandLine2
);
3025 ShellCommandSetNewScript (NULL
);
3028 // Only if this was the last script reset the state.
3030 if (ShellCommandGetCurrentScriptFile()==NULL
) {
3031 ShellCommandSetEchoState(PreScriptEchoState
);
3033 return (EFI_SUCCESS
);
3037 Function to process a NSH script file.
3039 @param[in] ScriptPath Pointer to the script file name (including file system path).
3040 @param[in] Handle the handle of the script file already opened.
3041 @param[in] CmdLine the command line to run.
3042 @param[in] ParamProtocol the shell parameters protocol pointer
3044 @retval EFI_SUCCESS the script completed successfully
3049 IN CONST CHAR16
*ScriptPath
,
3050 IN SHELL_FILE_HANDLE Handle OPTIONAL
,
3051 IN CONST CHAR16
*CmdLine
,
3052 IN EFI_SHELL_PARAMETERS_PROTOCOL
*ParamProtocol
3056 SHELL_FILE_HANDLE FileHandle
;
3060 if (ShellIsFile(ScriptPath
) != EFI_SUCCESS
) {
3061 return (EFI_INVALID_PARAMETER
);
3065 // get the argc and argv updated for scripts
3067 Status
= UpdateArgcArgv(ParamProtocol
, CmdLine
, Script_File_Name
, &Argv
, &Argc
);
3068 if (!EFI_ERROR(Status
)) {
3070 if (Handle
== NULL
) {
3074 Status
= ShellOpenFileByName(ScriptPath
, &FileHandle
, EFI_FILE_MODE_READ
, 0);
3075 if (!EFI_ERROR(Status
)) {
3079 Status
= RunScriptFileHandle(FileHandle
, ScriptPath
);
3082 // now close the file
3084 ShellCloseFile(&FileHandle
);
3087 Status
= RunScriptFileHandle(Handle
, ScriptPath
);
3092 // This is guaranteed to be called after UpdateArgcArgv no matter what else happened.
3093 // This is safe even if the update API failed. In this case, it may be a no-op.
3095 RestoreArgcArgv(ParamProtocol
, &Argv
, &Argc
);
3101 Return the pointer to the first occurrence of any character from a list of characters.
3103 @param[in] String the string to parse
3104 @param[in] CharacterList the list of character to look for
3105 @param[in] EscapeCharacter An escape character to skip
3107 @return the location of the first character in the string
3108 @retval CHAR_NULL no instance of any character in CharacterList was found in String
3113 IN CONST CHAR16
*String
,
3114 IN CONST CHAR16
*CharacterList
,
3115 IN CONST CHAR16 EscapeCharacter
3121 for (WalkStr
= 0; WalkStr
< StrLen(String
); WalkStr
++) {
3122 if (String
[WalkStr
] == EscapeCharacter
) {
3126 for (WalkChar
= 0; WalkChar
< StrLen(CharacterList
); WalkChar
++) {
3127 if (String
[WalkStr
] == CharacterList
[WalkChar
]) {
3128 return (&String
[WalkStr
]);
3132 return (String
+ StrLen(String
));