2 This is THE shell (application)
4 Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.<BR>
5 (C) Copyright 2013-2014 Hewlett-Packard Development Company, L.P.<BR>
6 This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php
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.
87 ASSERT(String
!= NULL
);
88 ASSERT(*String
!= NULL
);
90 // Remove any spaces and tabs at the beginning of the (*String).
92 while (((*String
)[0] == L
' ') || ((*String
)[0] == L
'\t')) {
93 CopyMem((*String
), (*String
)+1, StrSize((*String
)) - sizeof((*String
)[0]));
97 // Remove any spaces and tabs at the end of the (*String).
99 while ((StrLen (*String
) > 0) && (((*String
)[StrLen((*String
))-1] == L
' ') || ((*String
)[StrLen((*String
))-1] == L
'\t'))) {
100 (*String
)[StrLen((*String
))-1] = CHAR_NULL
;
103 return (EFI_SUCCESS
);
107 Parse for the next instance of one string within another string. Can optionally make sure that
108 the string was not escaped (^ character) per the shell specification.
110 @param[in] SourceString The string to search within
111 @param[in] FindString The string to look for
112 @param[in] CheckForEscapeCharacter TRUE to skip escaped instances of FinfString, otherwise will return even escaped instances
116 IN CONST CHAR16
*SourceString
,
117 IN CONST CHAR16
*FindString
,
118 IN CONST BOOLEAN CheckForEscapeCharacter
122 if (SourceString
== NULL
) {
125 Temp
= StrStr(SourceString
, FindString
);
128 // If nothing found, or we don't care about escape characters
130 if (Temp
== NULL
|| !CheckForEscapeCharacter
) {
135 // If we found an escaped character, try again on the remainder of the string
137 if ((Temp
> (SourceString
)) && *(Temp
-1) == L
'^') {
138 return FindNextInstance(Temp
+1, FindString
, CheckForEscapeCharacter
);
142 // we found the right character
148 Check whether the string between a pair of % is a valid environment variable name.
150 @param[in] BeginPercent pointer to the first percent.
151 @param[in] EndPercent pointer to the last percent.
153 @retval TRUE is a valid environment variable name.
154 @retval FALSE is NOT a valid environment variable name.
157 IsValidEnvironmentVariableName(
158 IN CONST CHAR16
*BeginPercent
,
159 IN CONST CHAR16
*EndPercent
162 CONST CHAR16
*Walker
;
166 ASSERT (BeginPercent
!= NULL
);
167 ASSERT (EndPercent
!= NULL
);
168 ASSERT (BeginPercent
< EndPercent
);
170 if ((BeginPercent
+ 1) == EndPercent
) {
174 for (Walker
= BeginPercent
+ 1; Walker
< EndPercent
; Walker
++) {
176 (*Walker
>= L
'0' && *Walker
<= L
'9') ||
177 (*Walker
>= L
'A' && *Walker
<= L
'Z') ||
178 (*Walker
>= L
'a' && *Walker
<= L
'z') ||
181 if (Walker
== BeginPercent
+ 1 && (*Walker
>= L
'0' && *Walker
<= L
'9')) {
195 Determine if a command line contains a split operation
197 @param[in] CmdLine The command line to parse.
199 @retval TRUE CmdLine has a valid split.
200 @retval FALSE CmdLine does not have a valid split.
204 IN CONST CHAR16
*CmdLine
207 CONST CHAR16
*TempSpot
;
208 CONST CHAR16
*FirstQuote
;
209 CONST CHAR16
*SecondQuote
;
211 FirstQuote
= FindNextInstance (CmdLine
, L
"\"", TRUE
);
213 TempSpot
= FindFirstCharacter(CmdLine
, L
"|", L
'^');
215 if (FirstQuote
== NULL
||
217 TempSpot
== CHAR_NULL
||
218 FirstQuote
> TempSpot
220 return (BOOLEAN
) ((TempSpot
!= NULL
) && (*TempSpot
!= CHAR_NULL
));
223 while ((TempSpot
!= NULL
) && (*TempSpot
!= CHAR_NULL
)) {
224 if (FirstQuote
== NULL
|| FirstQuote
> TempSpot
) {
227 SecondQuote
= FindNextInstance (FirstQuote
+ 1, L
"\"", TRUE
);
228 if (SecondQuote
== NULL
) {
231 if (SecondQuote
< TempSpot
) {
232 FirstQuote
= FindNextInstance (SecondQuote
+ 1, L
"\"", TRUE
);
235 FirstQuote
= FindNextInstance (SecondQuote
+ 1, L
"\"", TRUE
);
236 TempSpot
= FindFirstCharacter(TempSpot
+ 1, L
"|", L
'^');
241 return (BOOLEAN
) ((TempSpot
!= NULL
) && (*TempSpot
!= CHAR_NULL
));
245 Function to start monitoring for CTRL-S using SimpleTextInputEx. This
246 feature's enabled state was not known when the shell initially launched.
248 @retval EFI_SUCCESS The feature is enabled.
249 @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
;
343 if (PcdGet8(PcdShellSupportLevel
) > 3) {
344 return (EFI_UNSUPPORTED
);
350 Status
= gST
->ConOut
->ClearScreen(gST
->ConOut
);
351 if (EFI_ERROR(Status
)) {
356 // Populate the global structure from PCDs
358 ShellInfoObject
.ImageDevPath
= NULL
;
359 ShellInfoObject
.FileDevPath
= NULL
;
360 ShellInfoObject
.PageBreakEnabled
= PcdGetBool(PcdShellPageBreakDefault
);
361 ShellInfoObject
.ViewingSettings
.InsertMode
= PcdGetBool(PcdShellInsertModeDefault
);
362 ShellInfoObject
.LogScreenCount
= PcdGet8 (PcdShellScreenLogCount
);
365 // verify we dont allow for spec violation
367 ASSERT(ShellInfoObject
.LogScreenCount
>= 3);
370 // Initialize the LIST ENTRY objects...
372 InitializeListHead(&ShellInfoObject
.BufferToFreeList
.Link
);
373 InitializeListHead(&ShellInfoObject
.ViewingSettings
.CommandHistory
.Link
);
374 InitializeListHead(&ShellInfoObject
.SplitList
.Link
);
377 // Check PCDs for optional features that are not implemented yet.
379 if ( PcdGetBool(PcdShellSupportOldProtocols
)
380 || !FeaturePcdGet(PcdShellRequireHiiPlatform
)
381 || FeaturePcdGet(PcdShellSupportFrameworkHii
)
383 return (EFI_UNSUPPORTED
);
387 // turn off the watchdog timer
389 gBS
->SetWatchdogTimer (0, 0, 0, NULL
);
392 // install our console logger. This will keep a log of the output for back-browsing
394 Status
= ConsoleLoggerInstall(ShellInfoObject
.LogScreenCount
, &ShellInfoObject
.ConsoleInfo
);
395 if (!EFI_ERROR(Status
)) {
397 // Enable the cursor to be visible
399 gST
->ConOut
->EnableCursor (gST
->ConOut
, TRUE
);
402 // If supporting EFI 1.1 we need to install HII protocol
403 // only do this if PcdShellRequireHiiPlatform == FALSE
405 // remove EFI_UNSUPPORTED check above when complete.
406 ///@todo add support for Framework HII
409 // install our (solitary) HII package
411 ShellInfoObject
.HiiHandle
= HiiAddPackages (&gEfiCallerIdGuid
, gImageHandle
, ShellStrings
, NULL
);
412 if (ShellInfoObject
.HiiHandle
== NULL
) {
413 if (PcdGetBool(PcdShellSupportFrameworkHii
)) {
414 ///@todo Add our package into Framework HII
416 if (ShellInfoObject
.HiiHandle
== NULL
) {
417 Status
= EFI_NOT_STARTED
;
423 // create and install the EfiShellParametersProtocol
425 Status
= CreatePopulateInstallShellParametersProtocol(&ShellInfoObject
.NewShellParametersProtocol
, &ShellInfoObject
.RootShellInstance
);
426 ASSERT_EFI_ERROR(Status
);
427 ASSERT(ShellInfoObject
.NewShellParametersProtocol
!= NULL
);
430 // create and install the EfiShellProtocol
432 Status
= CreatePopulateInstallShellProtocol(&ShellInfoObject
.NewEfiShellProtocol
);
433 ASSERT_EFI_ERROR(Status
);
434 ASSERT(ShellInfoObject
.NewEfiShellProtocol
!= NULL
);
437 // Now initialize the shell library (it requires Shell Parameters protocol)
439 Status
= ShellInitialize();
440 ASSERT_EFI_ERROR(Status
);
442 Status
= CommandInit();
443 ASSERT_EFI_ERROR(Status
);
445 Status
= ShellInitEnvVarList ();
448 // Check the command line
450 Status
= ProcessCommandLine ();
451 if (EFI_ERROR (Status
)) {
456 // If shell support level is >= 1 create the mappings and paths
458 if (PcdGet8(PcdShellSupportLevel
) >= 1) {
459 Status
= ShellCommandCreateInitialMappingsAndPaths();
463 // Set the environment variable for nesting support
467 if (!ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoNest
) {
469 // No change. require nesting in Shell Protocol Execute()
471 StrnCatGrow(&TempString
,
476 StrnCatGrow(&TempString
,
481 Status
= InternalEfiShellSetEnv(mNoNestingEnvVarName
, TempString
, TRUE
);
482 SHELL_FREE_NON_NULL(TempString
);
486 // save the device path for the loaded image and the device path for the filepath (under loaded image)
487 // These are where to look for the startup.nsh file
489 Status
= GetDevicePathsForImageAndFile(&ShellInfoObject
.ImageDevPath
, &ShellInfoObject
.FileDevPath
);
490 ASSERT_EFI_ERROR(Status
);
493 // Display the version
495 if (!ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoVersion
) {
498 gST
->ConOut
->Mode
->CursorRow
,
500 STRING_TOKEN (STR_VER_OUTPUT_MAIN_SHELL
),
501 ShellInfoObject
.HiiHandle
,
502 SupportLevel
[PcdGet8(PcdShellSupportLevel
)],
503 gEfiShellProtocol
->MajorVersion
,
504 gEfiShellProtocol
->MinorVersion
511 STRING_TOKEN (STR_VER_OUTPUT_MAIN_SUPPLIER
),
512 ShellInfoObject
.HiiHandle
,
513 (CHAR16
*) PcdGetPtr (PcdShellSupplier
)
520 STRING_TOKEN (STR_VER_OUTPUT_MAIN_UEFI
),
521 ShellInfoObject
.HiiHandle
,
522 (gST
->Hdr
.Revision
&0xffff0000)>>16,
523 (gST
->Hdr
.Revision
&0x0000ffff),
525 gST
->FirmwareRevision
530 // Display the mapping
532 if (PcdGet8(PcdShellSupportLevel
) >= 2 && !ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoMap
) {
533 Status
= RunCommand(L
"map");
534 ASSERT_EFI_ERROR(Status
);
538 // init all the built in alias'
540 Status
= SetBuiltInAlias();
541 ASSERT_EFI_ERROR(Status
);
544 // Initialize environment variables
546 if (ShellCommandGetProfileList() != NULL
) {
547 Status
= InternalEfiShellSetEnv(L
"profiles", ShellCommandGetProfileList(), TRUE
);
548 ASSERT_EFI_ERROR(Status
);
552 TempString
= AllocateZeroPool(Size
);
554 UnicodeSPrint(TempString
, Size
, L
"%d", PcdGet8(PcdShellSupportLevel
));
555 Status
= InternalEfiShellSetEnv(L
"uefishellsupport", TempString
, TRUE
);
556 ASSERT_EFI_ERROR(Status
);
558 UnicodeSPrint(TempString
, Size
, L
"%d.%d", ShellInfoObject
.NewEfiShellProtocol
->MajorVersion
, ShellInfoObject
.NewEfiShellProtocol
->MinorVersion
);
559 Status
= InternalEfiShellSetEnv(L
"uefishellversion", TempString
, TRUE
);
560 ASSERT_EFI_ERROR(Status
);
562 UnicodeSPrint(TempString
, Size
, L
"%d.%d", (gST
->Hdr
.Revision
& 0xFFFF0000) >> 16, gST
->Hdr
.Revision
& 0x0000FFFF);
563 Status
= InternalEfiShellSetEnv(L
"uefiversion", TempString
, TRUE
);
564 ASSERT_EFI_ERROR(Status
);
566 FreePool(TempString
);
568 if (!EFI_ERROR(Status
)) {
569 if (!ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoInterrupt
) {
571 // Set up the event for CTRL-C monitoring...
573 Status
= InernalEfiShellStartMonitor();
576 if (!EFI_ERROR(Status
) && !ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoConsoleIn
) {
578 // Set up the event for CTRL-S monitoring...
580 Status
= InternalEfiShellStartCtrlSMonitor();
583 if (!EFI_ERROR(Status
) && ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoConsoleIn
) {
585 // close off the gST->ConIn
587 OldConIn
= gST
->ConIn
;
588 ConInHandle
= gST
->ConsoleInHandle
;
589 gST
->ConIn
= CreateSimpleTextInOnFile((SHELL_FILE_HANDLE
)&FileInterfaceNulFile
, &gST
->ConsoleInHandle
);
595 if (!EFI_ERROR(Status
) && PcdGet8(PcdShellSupportLevel
) >= 1) {
597 // process the startup script or launch the called app.
599 Status
= DoStartupScript(ShellInfoObject
.ImageDevPath
, ShellInfoObject
.FileDevPath
);
602 if (!ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.Exit
&& !ShellCommandGetExit() && (PcdGet8(PcdShellSupportLevel
) >= 3 || PcdGetBool(PcdShellForceConsole
)) && !EFI_ERROR(Status
) && !ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoConsoleIn
) {
604 // begin the UI waiting loop
608 // clean out all the memory allocated for CONST <something> * return values
609 // between each shell prompt presentation
611 if (!IsListEmpty(&ShellInfoObject
.BufferToFreeList
.Link
)){
612 FreeBufferList(&ShellInfoObject
.BufferToFreeList
);
616 // Reset page break back to default.
618 ShellInfoObject
.PageBreakEnabled
= PcdGetBool(PcdShellPageBreakDefault
);
619 ASSERT (ShellInfoObject
.ConsoleInfo
!= NULL
);
620 ShellInfoObject
.ConsoleInfo
->Enabled
= TRUE
;
621 ShellInfoObject
.ConsoleInfo
->RowCounter
= 0;
626 Status
= DoShellPrompt();
627 } while (!ShellCommandGetExit());
629 if (OldConIn
!= NULL
&& ConInHandle
!= NULL
) {
630 CloseSimpleTextInOnFile (gST
->ConIn
);
631 gST
->ConIn
= OldConIn
;
632 gST
->ConsoleInHandle
= ConInHandle
;
639 // uninstall protocols / free memory / etc...
641 if (ShellInfoObject
.UserBreakTimer
!= NULL
) {
642 gBS
->CloseEvent(ShellInfoObject
.UserBreakTimer
);
643 DEBUG_CODE(ShellInfoObject
.UserBreakTimer
= NULL
;);
645 if (ShellInfoObject
.ImageDevPath
!= NULL
) {
646 FreePool(ShellInfoObject
.ImageDevPath
);
647 DEBUG_CODE(ShellInfoObject
.ImageDevPath
= NULL
;);
649 if (ShellInfoObject
.FileDevPath
!= NULL
) {
650 FreePool(ShellInfoObject
.FileDevPath
);
651 DEBUG_CODE(ShellInfoObject
.FileDevPath
= NULL
;);
653 if (ShellInfoObject
.NewShellParametersProtocol
!= NULL
) {
654 CleanUpShellParametersProtocol(ShellInfoObject
.NewShellParametersProtocol
);
655 DEBUG_CODE(ShellInfoObject
.NewShellParametersProtocol
= NULL
;);
657 if (ShellInfoObject
.NewEfiShellProtocol
!= NULL
){
658 if (ShellInfoObject
.NewEfiShellProtocol
->IsRootShell()){
659 InternalEfiShellSetEnv(L
"cwd", NULL
, TRUE
);
661 CleanUpShellEnvironment (ShellInfoObject
.NewEfiShellProtocol
);
662 DEBUG_CODE(ShellInfoObject
.NewEfiShellProtocol
= NULL
;);
665 if (!IsListEmpty(&ShellInfoObject
.BufferToFreeList
.Link
)){
666 FreeBufferList(&ShellInfoObject
.BufferToFreeList
);
669 if (!IsListEmpty(&ShellInfoObject
.SplitList
.Link
)){
670 ASSERT(FALSE
); ///@todo finish this de-allocation (free SplitStdIn/Out when needed).
672 for ( Split
= (SPLIT_LIST
*)GetFirstNode (&ShellInfoObject
.SplitList
.Link
)
673 ; !IsNull (&ShellInfoObject
.SplitList
.Link
, &Split
->Link
)
674 ; Split
= (SPLIT_LIST
*)GetNextNode (&ShellInfoObject
.SplitList
.Link
, &Split
->Link
)
676 RemoveEntryList (&Split
->Link
);
680 DEBUG_CODE (InitializeListHead (&ShellInfoObject
.SplitList
.Link
););
683 if (ShellInfoObject
.ShellInitSettings
.FileName
!= NULL
) {
684 FreePool(ShellInfoObject
.ShellInitSettings
.FileName
);
685 DEBUG_CODE(ShellInfoObject
.ShellInitSettings
.FileName
= NULL
;);
688 if (ShellInfoObject
.ShellInitSettings
.FileOptions
!= NULL
) {
689 FreePool(ShellInfoObject
.ShellInitSettings
.FileOptions
);
690 DEBUG_CODE(ShellInfoObject
.ShellInitSettings
.FileOptions
= NULL
;);
693 if (ShellInfoObject
.HiiHandle
!= NULL
) {
694 HiiRemovePackages(ShellInfoObject
.HiiHandle
);
695 DEBUG_CODE(ShellInfoObject
.HiiHandle
= NULL
;);
698 if (!IsListEmpty(&ShellInfoObject
.ViewingSettings
.CommandHistory
.Link
)){
699 FreeBufferList(&ShellInfoObject
.ViewingSettings
.CommandHistory
);
702 ASSERT(ShellInfoObject
.ConsoleInfo
!= NULL
);
703 if (ShellInfoObject
.ConsoleInfo
!= NULL
) {
704 ConsoleLoggerUninstall(ShellInfoObject
.ConsoleInfo
);
705 FreePool(ShellInfoObject
.ConsoleInfo
);
706 DEBUG_CODE(ShellInfoObject
.ConsoleInfo
= NULL
;);
709 ShellFreeEnvVarList ();
711 if (ShellCommandGetExit()) {
712 return ((EFI_STATUS
)ShellCommandGetExitCode());
718 Sets all the alias' that were registered with the ShellCommandLib library.
720 @retval EFI_SUCCESS all init commands were run successfully.
728 CONST ALIAS_LIST
*List
;
732 // Get all the commands we want to run
734 List
= ShellCommandGetInitAliasList();
737 // for each command in the List
739 for ( Node
= (ALIAS_LIST
*)GetFirstNode(&List
->Link
)
740 ; !IsNull (&List
->Link
, &Node
->Link
)
741 ; Node
= (ALIAS_LIST
*)GetNextNode(&List
->Link
, &Node
->Link
)
744 // install the alias'
746 Status
= InternalSetAlias(Node
->CommandString
, Node
->Alias
, TRUE
);
747 ASSERT_EFI_ERROR(Status
);
749 return (EFI_SUCCESS
);
753 Internal function to determine if 2 command names are really the same.
755 @param[in] Command1 The pointer to the first command name.
756 @param[in] Command2 The pointer to the second command name.
758 @retval TRUE The 2 command names are the same.
759 @retval FALSE The 2 command names are not the same.
763 IN CONST CHAR16
*Command1
,
764 IN CONST CHAR16
*Command2
767 if (StringNoCaseCompare(&Command1
, &Command2
) == 0) {
774 Internal function to determine if a command is a script only command.
776 @param[in] CommandName The pointer to the command name.
778 @retval TRUE The command is a script only command.
779 @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.
812 GetDevicePathsForImageAndFile (
813 IN OUT EFI_DEVICE_PATH_PROTOCOL
**DevPath
,
814 IN OUT EFI_DEVICE_PATH_PROTOCOL
**FilePath
818 EFI_LOADED_IMAGE_PROTOCOL
*LoadedImage
;
819 EFI_DEVICE_PATH_PROTOCOL
*ImageDevicePath
;
821 ASSERT(DevPath
!= NULL
);
822 ASSERT(FilePath
!= NULL
);
824 Status
= gBS
->OpenProtocol (
826 &gEfiLoadedImageProtocolGuid
,
827 (VOID
**)&LoadedImage
,
830 EFI_OPEN_PROTOCOL_GET_PROTOCOL
832 if (!EFI_ERROR (Status
)) {
833 Status
= gBS
->OpenProtocol (
834 LoadedImage
->DeviceHandle
,
835 &gEfiDevicePathProtocolGuid
,
836 (VOID
**)&ImageDevicePath
,
839 EFI_OPEN_PROTOCOL_GET_PROTOCOL
841 if (!EFI_ERROR (Status
)) {
842 *DevPath
= DuplicateDevicePath (ImageDevicePath
);
843 *FilePath
= DuplicateDevicePath (LoadedImage
->FilePath
);
845 LoadedImage
->DeviceHandle
,
846 &gEfiDevicePathProtocolGuid
,
852 &gEfiLoadedImageProtocolGuid
,
860 Process all Uefi Shell 2.0 command line options.
862 see Uefi Shell 2.0 section 3.2 for full details.
864 the command line must resemble the following:
866 shell.efi [ShellOpt-options] [options] [file-name [file-name-options]]
868 ShellOpt-options Options which control the initialization behavior of the shell.
869 These options are read from the EFI global variable "ShellOpt"
870 and are processed before options or file-name.
872 options Options which control the initialization behavior of the shell.
874 file-name The name of a UEFI shell application or script to be executed
875 after initialization is complete. By default, if file-name is
876 specified, then -nostartup is implied. Scripts are not supported
879 file-name-options The command-line options that are passed to file-name when it
882 This will initialize the ShellInfoObject.ShellInitSettings global variable.
884 @retval EFI_SUCCESS The variable is initialized.
894 CHAR16
*DelayValueStr
;
897 EFI_UNICODE_COLLATION_PROTOCOL
*UnicodeCollation
;
899 // `file-name-options` will contain arguments to `file-name` that we don't
900 // know about. This would cause ShellCommandLineParse to error, so we parse
901 // arguments manually, ignoring those after the first thing that doesn't look
902 // like a shell option (which is assumed to be `file-name`).
904 Status
= gBS
->LocateProtocol (
905 &gEfiUnicodeCollation2ProtocolGuid
,
907 (VOID
**) &UnicodeCollation
909 if (EFI_ERROR (Status
)) {
910 Status
= gBS
->LocateProtocol (
911 &gEfiUnicodeCollationProtocolGuid
,
913 (VOID
**) &UnicodeCollation
915 if (EFI_ERROR (Status
)) {
920 // Set default options
921 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.Startup
= FALSE
;
922 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoStartup
= FALSE
;
923 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoConsoleOut
= FALSE
;
924 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoConsoleIn
= FALSE
;
925 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoInterrupt
= FALSE
;
926 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoMap
= FALSE
;
927 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoVersion
= FALSE
;
928 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.Delay
= FALSE
;
929 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.Exit
= FALSE
;
930 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoNest
= FALSE
;
931 ShellInfoObject
.ShellInitSettings
.Delay
= 5;
934 // Start LoopVar at 0 to parse only optional arguments at Argv[0]
935 // and parse other parameters from Argv[1]. This is for use case that
936 // UEFI Shell boot option is created, and OptionalData is provided
937 // that starts with shell command-line options.
939 for (LoopVar
= 0 ; LoopVar
< gEfiShellParametersProtocol
->Argc
; LoopVar
++) {
940 CurrentArg
= gEfiShellParametersProtocol
->Argv
[LoopVar
];
941 if (UnicodeCollation
->StriColl (
946 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.Startup
= TRUE
;
948 else if (UnicodeCollation
->StriColl (
953 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoStartup
= TRUE
;
955 else if (UnicodeCollation
->StriColl (
960 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoConsoleOut
= TRUE
;
962 else if (UnicodeCollation
->StriColl (
967 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoConsoleIn
= TRUE
;
969 else if (UnicodeCollation
->StriColl (
974 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoInterrupt
= TRUE
;
976 else if (UnicodeCollation
->StriColl (
981 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoMap
= TRUE
;
983 else if (UnicodeCollation
->StriColl (
988 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoVersion
= TRUE
;
990 else if (UnicodeCollation
->StriColl (
995 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoNest
= TRUE
;
997 else if (UnicodeCollation
->StriColl (
1002 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.Delay
= TRUE
;
1003 // Check for optional delay value following "-delay"
1004 DelayValueStr
= gEfiShellParametersProtocol
->Argv
[LoopVar
+ 1];
1005 if (DelayValueStr
!= NULL
){
1006 if (*DelayValueStr
== L
':') {
1009 if (!EFI_ERROR(ShellConvertStringToUint64 (
1015 ShellInfoObject
.ShellInitSettings
.Delay
= (UINTN
)DelayValue
;
1019 } else if (UnicodeCollation
->StriColl (
1024 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.Exit
= TRUE
;
1025 } else if (StrnCmp (L
"-", CurrentArg
, 1) == 0) {
1026 // Unrecognized option
1027 ShellPrintHiiEx(-1, -1, NULL
,
1028 STRING_TOKEN (STR_GEN_PROBLEM
),
1029 ShellInfoObject
.HiiHandle
,
1032 return EFI_INVALID_PARAMETER
;
1035 // First argument should be Shell.efi image name
1041 ShellInfoObject
.ShellInitSettings
.FileName
= NULL
;
1044 // If first argument contains a space, then add double quotes before the argument
1046 if (StrStr (CurrentArg
, L
" ") != NULL
) {
1047 StrnCatGrow(&ShellInfoObject
.ShellInitSettings
.FileName
, &Size
, L
"\"", 0);
1048 if (ShellInfoObject
.ShellInitSettings
.FileName
== NULL
) {
1049 return (EFI_OUT_OF_RESOURCES
);
1052 StrnCatGrow(&ShellInfoObject
.ShellInitSettings
.FileName
, &Size
, CurrentArg
, 0);
1053 if (ShellInfoObject
.ShellInitSettings
.FileName
== NULL
) {
1054 return (EFI_OUT_OF_RESOURCES
);
1057 // If first argument contains a space, then add double quotes after the argument
1059 if (StrStr (CurrentArg
, L
" ") != NULL
) {
1060 StrnCatGrow(&ShellInfoObject
.ShellInitSettings
.FileName
, &Size
, L
"\"", 0);
1061 if (ShellInfoObject
.ShellInitSettings
.FileName
== NULL
) {
1062 return (EFI_OUT_OF_RESOURCES
);
1066 // We found `file-name`.
1068 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoStartup
= 1;
1071 // Add `file-name-options`
1072 for (Size
= 0 ; LoopVar
< gEfiShellParametersProtocol
->Argc
; LoopVar
++) {
1073 ASSERT((ShellInfoObject
.ShellInitSettings
.FileOptions
== NULL
&& Size
== 0) || (ShellInfoObject
.ShellInitSettings
.FileOptions
!= NULL
));
1075 // Add a space between arguments
1077 if (ShellInfoObject
.ShellInitSettings
.FileOptions
!= NULL
) {
1078 StrnCatGrow(&ShellInfoObject
.ShellInitSettings
.FileOptions
, &Size
, L
" ", 0);
1079 if (ShellInfoObject
.ShellInitSettings
.FileOptions
== NULL
) {
1080 SHELL_FREE_NON_NULL(ShellInfoObject
.ShellInitSettings
.FileName
);
1081 return (EFI_OUT_OF_RESOURCES
);
1085 // If an argumnent contains a space, then add double quotes before the argument
1087 if (StrStr (gEfiShellParametersProtocol
->Argv
[LoopVar
], L
" ") != NULL
) {
1088 StrnCatGrow(&ShellInfoObject
.ShellInitSettings
.FileOptions
,
1092 if (ShellInfoObject
.ShellInitSettings
.FileOptions
== NULL
) {
1093 SHELL_FREE_NON_NULL(ShellInfoObject
.ShellInitSettings
.FileName
);
1094 return (EFI_OUT_OF_RESOURCES
);
1097 StrnCatGrow(&ShellInfoObject
.ShellInitSettings
.FileOptions
,
1099 gEfiShellParametersProtocol
->Argv
[LoopVar
],
1101 if (ShellInfoObject
.ShellInitSettings
.FileOptions
== NULL
) {
1102 SHELL_FREE_NON_NULL(ShellInfoObject
.ShellInitSettings
.FileName
);
1103 return (EFI_OUT_OF_RESOURCES
);
1106 // If an argumnent contains a space, then add double quotes after the argument
1108 if (StrStr (gEfiShellParametersProtocol
->Argv
[LoopVar
], L
" ") != NULL
) {
1109 StrnCatGrow(&ShellInfoObject
.ShellInitSettings
.FileOptions
,
1113 if (ShellInfoObject
.ShellInitSettings
.FileOptions
== NULL
) {
1114 SHELL_FREE_NON_NULL(ShellInfoObject
.ShellInitSettings
.FileName
);
1115 return (EFI_OUT_OF_RESOURCES
);
1122 // "-nointerrupt" overrides "-delay"
1123 if (ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoInterrupt
) {
1124 ShellInfoObject
.ShellInitSettings
.Delay
= 0;
1131 Function try to find location of the Startup.nsh file.
1133 The buffer is callee allocated and should be freed by the caller.
1135 @param ImageDevicePath The path to the image for shell. first place to look for the startup script
1136 @param FileDevicePath The path to the file for shell. second place to look for the startup script.
1138 @retval NULL No Startup.nsh file was found.
1139 @return !=NULL Pointer to NULL-terminated path.
1142 LocateStartupScript (
1143 IN EFI_DEVICE_PATH_PROTOCOL
*ImageDevicePath
,
1144 IN EFI_DEVICE_PATH_PROTOCOL
*FileDevicePath
1147 CHAR16
*StartupScriptPath
;
1149 CONST CHAR16
*MapName
;
1152 StartupScriptPath
= NULL
;
1156 // Try to find 'Startup.nsh' in the directory where the shell itself was launched.
1158 MapName
= ShellInfoObject
.NewEfiShellProtocol
->GetMapFromDevicePath (&ImageDevicePath
);
1159 if (MapName
!= NULL
) {
1160 StartupScriptPath
= StrnCatGrow (&StartupScriptPath
, &Size
, MapName
, 0);
1161 if (StartupScriptPath
== NULL
) {
1163 // Do not locate the startup script in sys path when out of resource.
1167 TempSpot
= StrStr (StartupScriptPath
, L
";");
1168 if (TempSpot
!= NULL
) {
1169 *TempSpot
= CHAR_NULL
;
1172 InternalEfiShellSetEnv(L
"homefilesystem", StartupScriptPath
, TRUE
);
1174 StartupScriptPath
= StrnCatGrow (&StartupScriptPath
, &Size
, ((FILEPATH_DEVICE_PATH
*)FileDevicePath
)->PathName
, 0);
1175 PathRemoveLastItem (StartupScriptPath
);
1176 StartupScriptPath
= StrnCatGrow (&StartupScriptPath
, &Size
, mStartupScript
, 0);
1180 // Try to find 'Startup.nsh' in the execution path defined by the envrionment variable PATH.
1182 if ((StartupScriptPath
== NULL
) || EFI_ERROR (ShellIsFile (StartupScriptPath
))) {
1183 SHELL_FREE_NON_NULL (StartupScriptPath
);
1184 StartupScriptPath
= ShellFindFilePath (mStartupScript
);
1187 return StartupScriptPath
;
1191 Handles all interaction with the default startup script.
1193 this will check that the correct command line parameters were passed, handle the delay, and then start running the script.
1195 @param ImagePath the path to the image for shell. first place to look for the startup script
1196 @param FilePath the path to the file for shell. second place to look for the startup script.
1198 @retval EFI_SUCCESS the variable is initialized.
1202 IN EFI_DEVICE_PATH_PROTOCOL
*ImagePath
,
1203 IN EFI_DEVICE_PATH_PROTOCOL
*FilePath
1207 EFI_STATUS CalleeStatus
;
1210 CHAR16
*FileStringPath
;
1213 Key
.UnicodeChar
= CHAR_NULL
;
1216 if (!ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.Startup
&& ShellInfoObject
.ShellInitSettings
.FileName
!= NULL
) {
1218 // launch something else instead
1220 NewSize
= StrSize(ShellInfoObject
.ShellInitSettings
.FileName
);
1221 if (ShellInfoObject
.ShellInitSettings
.FileOptions
!= NULL
) {
1222 NewSize
+= StrSize(ShellInfoObject
.ShellInitSettings
.FileOptions
) + sizeof(CHAR16
);
1224 FileStringPath
= AllocateZeroPool(NewSize
);
1225 if (FileStringPath
== NULL
) {
1226 return (EFI_OUT_OF_RESOURCES
);
1228 StrCpyS(FileStringPath
, NewSize
/sizeof(CHAR16
), ShellInfoObject
.ShellInitSettings
.FileName
);
1229 if (ShellInfoObject
.ShellInitSettings
.FileOptions
!= NULL
) {
1230 StrnCatS(FileStringPath
, NewSize
/sizeof(CHAR16
), L
" ", NewSize
/sizeof(CHAR16
) - StrLen(FileStringPath
) -1);
1231 StrnCatS(FileStringPath
, NewSize
/sizeof(CHAR16
), ShellInfoObject
.ShellInitSettings
.FileOptions
, NewSize
/sizeof(CHAR16
) - StrLen(FileStringPath
) -1);
1233 Status
= RunShellCommand(FileStringPath
, &CalleeStatus
);
1234 if (ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.Exit
== TRUE
) {
1235 ShellCommandRegisterExit(gEfiShellProtocol
->BatchIsActive(), (UINT64
)CalleeStatus
);
1237 FreePool(FileStringPath
);
1243 // for shell level 0 we do no scripts
1244 // Without the Startup bit overriding we allow for nostartup to prevent scripts
1246 if ( (PcdGet8(PcdShellSupportLevel
) < 1)
1247 || (ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoStartup
&& !ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.Startup
)
1249 return (EFI_SUCCESS
);
1252 gST
->ConOut
->EnableCursor(gST
->ConOut
, FALSE
);
1254 // print out our warning and see if they press a key
1256 for ( Status
= EFI_UNSUPPORTED
, Delay
= ShellInfoObject
.ShellInitSettings
.Delay
1257 ; Delay
!= 0 && EFI_ERROR(Status
)
1260 ShellPrintHiiEx(0, gST
->ConOut
->Mode
->CursorRow
, NULL
, STRING_TOKEN (STR_SHELL_STARTUP_QUESTION
), ShellInfoObject
.HiiHandle
, Delay
);
1261 gBS
->Stall (1000000);
1262 if (!ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoConsoleIn
) {
1263 Status
= gST
->ConIn
->ReadKeyStroke (gST
->ConIn
, &Key
);
1266 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_CRLF
), ShellInfoObject
.HiiHandle
);
1267 gST
->ConOut
->EnableCursor(gST
->ConOut
, TRUE
);
1272 if (Status
== EFI_SUCCESS
&& Key
.UnicodeChar
== 0 && Key
.ScanCode
== SCAN_ESC
) {
1273 return (EFI_SUCCESS
);
1276 FileStringPath
= LocateStartupScript (ImagePath
, FilePath
);
1277 if (FileStringPath
!= NULL
) {
1278 Status
= RunScriptFile (FileStringPath
, NULL
, L
"", ShellInfoObject
.NewShellParametersProtocol
);
1279 FreePool (FileStringPath
);
1282 // we return success since startup script is not mandatory.
1284 Status
= EFI_SUCCESS
;
1291 Function to perform the shell prompt looping. It will do a single prompt,
1292 dispatch the result, and then return. It is expected that the caller will
1293 call this function in a loop many times.
1296 @retval RETURN_ABORTED
1306 CONST CHAR16
*CurDir
;
1309 LIST_ENTRY OldBufferList
;
1314 // Get screen setting to decide size of the command line buffer
1316 gST
->ConOut
->QueryMode (gST
->ConOut
, gST
->ConOut
->Mode
->Mode
, &Column
, &Row
);
1317 BufferSize
= Column
* Row
* sizeof (CHAR16
);
1318 CmdLine
= AllocateZeroPool (BufferSize
);
1319 if (CmdLine
== NULL
) {
1320 return EFI_OUT_OF_RESOURCES
;
1323 SaveBufferList(&OldBufferList
);
1324 CurDir
= ShellInfoObject
.NewEfiShellProtocol
->GetEnv(L
"cwd");
1329 gST
->ConOut
->SetCursorPosition (gST
->ConOut
, 0, gST
->ConOut
->Mode
->CursorRow
);
1331 if (CurDir
!= NULL
&& StrLen(CurDir
) > 1) {
1332 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_CURDIR
), ShellInfoObject
.HiiHandle
, CurDir
);
1334 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_SHELL
), ShellInfoObject
.HiiHandle
);
1338 // Read a line from the console
1340 Status
= ShellInfoObject
.NewEfiShellProtocol
->ReadFile(ShellInfoObject
.NewShellParametersProtocol
->StdIn
, &BufferSize
, CmdLine
);
1343 // Null terminate the string and parse it
1345 if (!EFI_ERROR (Status
)) {
1347 // Reset the CTRL-C event just before running the command (yes we ignore the return values)
1349 Status
= gBS
->CheckEvent (ShellInfoObject
.NewEfiShellProtocol
->ExecutionBreak
);
1351 CmdLine
[BufferSize
/ sizeof (CHAR16
)] = CHAR_NULL
;
1352 Status
= RunCommand(CmdLine
);
1356 // Done with this command
1358 RestoreBufferList(&OldBufferList
);
1364 Add a buffer to the Buffer To Free List for safely returning buffers to other
1365 places without risking letting them modify internal shell information.
1367 @param Buffer Something to pass to FreePool when the shell is exiting.
1370 AddBufferToFreeList (
1374 BUFFER_LIST
*BufferListEntry
;
1376 if (Buffer
== NULL
) {
1380 BufferListEntry
= AllocateZeroPool (sizeof (BUFFER_LIST
));
1381 if (BufferListEntry
== NULL
) {
1385 BufferListEntry
->Buffer
= Buffer
;
1386 InsertTailList (&ShellInfoObject
.BufferToFreeList
.Link
, &BufferListEntry
->Link
);
1392 Create a new buffer list and stores the old one to OldBufferList
1394 @param OldBufferList The temporary list head used to store the nodes in BufferToFreeList.
1398 OUT LIST_ENTRY
*OldBufferList
1401 CopyMem (OldBufferList
, &ShellInfoObject
.BufferToFreeList
.Link
, sizeof (LIST_ENTRY
));
1402 InitializeListHead (&ShellInfoObject
.BufferToFreeList
.Link
);
1406 Restore previous nodes into BufferToFreeList .
1408 @param OldBufferList The temporary list head used to store the nodes in BufferToFreeList.
1412 IN OUT LIST_ENTRY
*OldBufferList
1415 FreeBufferList (&ShellInfoObject
.BufferToFreeList
);
1416 CopyMem (&ShellInfoObject
.BufferToFreeList
.Link
, OldBufferList
, sizeof (LIST_ENTRY
));
1421 Add a buffer to the Line History List
1423 @param Buffer The line buffer to add.
1426 AddLineToCommandHistory(
1427 IN CONST CHAR16
*Buffer
1431 BUFFER_LIST
*Walker
;
1432 UINT16 MaxHistoryCmdCount
;
1436 MaxHistoryCmdCount
= PcdGet16(PcdShellMaxHistoryCommandCount
);
1438 if (MaxHistoryCmdCount
== 0) {
1443 Node
= AllocateZeroPool(sizeof(BUFFER_LIST
));
1448 Node
->Buffer
= AllocateCopyPool (StrSize (Buffer
), Buffer
);
1449 if (Node
->Buffer
== NULL
) {
1454 for ( Walker
= (BUFFER_LIST
*)GetFirstNode(&ShellInfoObject
.ViewingSettings
.CommandHistory
.Link
)
1455 ; !IsNull(&ShellInfoObject
.ViewingSettings
.CommandHistory
.Link
, &Walker
->Link
)
1456 ; Walker
= (BUFFER_LIST
*)GetNextNode(&ShellInfoObject
.ViewingSettings
.CommandHistory
.Link
, &Walker
->Link
)
1460 if (Count
< MaxHistoryCmdCount
){
1461 InsertTailList(&ShellInfoObject
.ViewingSettings
.CommandHistory
.Link
, &Node
->Link
);
1463 Walker
= (BUFFER_LIST
*)GetFirstNode(&ShellInfoObject
.ViewingSettings
.CommandHistory
.Link
);
1464 RemoveEntryList(&Walker
->Link
);
1465 if (Walker
->Buffer
!= NULL
) {
1466 FreePool(Walker
->Buffer
);
1469 InsertTailList(&ShellInfoObject
.ViewingSettings
.CommandHistory
.Link
, &Node
->Link
);
1474 Checks if a string is an alias for another command. If yes, then it replaces the alias name
1475 with the correct command name.
1477 @param[in, out] CommandString Upon entry the potential alias. Upon return the
1478 command name if it was an alias. If it was not
1479 an alias it will be unchanged. This function may
1480 change the buffer to fit the command name.
1482 @retval EFI_SUCCESS The name was changed.
1483 @retval EFI_SUCCESS The name was not an alias.
1484 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
1488 IN OUT CHAR16
**CommandString
1491 CONST CHAR16
*NewString
;
1493 NewString
= ShellInfoObject
.NewEfiShellProtocol
->GetAlias(*CommandString
, NULL
);
1494 if (NewString
== NULL
) {
1495 return (EFI_SUCCESS
);
1497 FreePool(*CommandString
);
1498 *CommandString
= AllocateCopyPool(StrSize(NewString
), NewString
);
1499 if (*CommandString
== NULL
) {
1500 return (EFI_OUT_OF_RESOURCES
);
1502 return (EFI_SUCCESS
);
1506 This function will eliminate unreplaced (and therefore non-found) environment variables.
1508 @param[in,out] CmdLine The command line to update.
1511 StripUnreplacedEnvironmentVariables(
1512 IN OUT CHAR16
*CmdLine
1515 CHAR16
*FirstPercent
;
1517 CHAR16
*SecondPercent
;
1518 CHAR16
*SecondQuote
;
1519 CHAR16
*CurrentLocator
;
1521 for (CurrentLocator
= CmdLine
; CurrentLocator
!= NULL
; ) {
1522 FirstQuote
= FindNextInstance(CurrentLocator
, L
"\"", TRUE
);
1523 FirstPercent
= FindNextInstance(CurrentLocator
, L
"%", TRUE
);
1524 SecondPercent
= FirstPercent
!=NULL
?FindNextInstance(FirstPercent
+1, L
"%", TRUE
):NULL
;
1525 if (FirstPercent
== NULL
|| SecondPercent
== NULL
) {
1527 // If we ever don't have 2 % we are done.
1532 if (FirstQuote
!= NULL
&& FirstQuote
< FirstPercent
) {
1533 SecondQuote
= FindNextInstance(FirstQuote
+1, L
"\"", TRUE
);
1535 // Quote is first found
1538 if (SecondQuote
< FirstPercent
) {
1540 // restart after the pair of "
1542 CurrentLocator
= SecondQuote
+ 1;
1543 } else /* FirstPercent < SecondQuote */{
1545 // Restart on the first percent
1547 CurrentLocator
= FirstPercent
;
1552 if (FirstQuote
== NULL
|| SecondPercent
< FirstQuote
) {
1553 if (IsValidEnvironmentVariableName(FirstPercent
, SecondPercent
)) {
1555 // We need to remove from FirstPercent to SecondPercent
1557 CopyMem(FirstPercent
, SecondPercent
+ 1, StrSize(SecondPercent
+ 1));
1559 // don't need to update the locator. both % characters are gone.
1562 CurrentLocator
= SecondPercent
+ 1;
1566 CurrentLocator
= FirstQuote
;
1568 return (EFI_SUCCESS
);
1572 Function allocates a new command line and replaces all instances of environment
1573 variable names that are correctly preset to their values.
1575 If the return value is not NULL the memory must be caller freed.
1577 @param[in] OriginalCommandLine The original command line
1579 @retval NULL An error occurred.
1580 @return The new command line with no environment variables present.
1583 ShellConvertVariables (
1584 IN CONST CHAR16
*OriginalCommandLine
1587 CONST CHAR16
*MasterEnvList
;
1589 CHAR16
*NewCommandLine1
;
1590 CHAR16
*NewCommandLine2
;
1594 SCRIPT_FILE
*CurrentScriptFile
;
1595 ALIAS_LIST
*AliasListNode
;
1597 ASSERT(OriginalCommandLine
!= NULL
);
1600 NewSize
= StrSize(OriginalCommandLine
);
1601 CurrentScriptFile
= ShellCommandGetCurrentScriptFile();
1604 ///@todo update this to handle the %0 - %9 for scripting only (borrow from line 1256 area) ? ? ?
1607 // calculate the size required for the post-conversion string...
1609 if (CurrentScriptFile
!= NULL
) {
1610 for (AliasListNode
= (ALIAS_LIST
*)GetFirstNode(&CurrentScriptFile
->SubstList
)
1611 ; !IsNull(&CurrentScriptFile
->SubstList
, &AliasListNode
->Link
)
1612 ; AliasListNode
= (ALIAS_LIST
*)GetNextNode(&CurrentScriptFile
->SubstList
, &AliasListNode
->Link
)
1614 for (Temp
= StrStr(OriginalCommandLine
, AliasListNode
->Alias
)
1616 ; Temp
= StrStr(Temp
+1, AliasListNode
->Alias
)
1619 // we need a preceding and if there is space no ^ preceding (if no space ignore)
1621 if ((((Temp
-OriginalCommandLine
)>2) && *(Temp
-2) != L
'^') || ((Temp
-OriginalCommandLine
)<=2)) {
1622 NewSize
+= StrSize(AliasListNode
->CommandString
);
1628 for (MasterEnvList
= EfiShellGetEnv(NULL
)
1629 ; MasterEnvList
!= NULL
&& *MasterEnvList
!= CHAR_NULL
//&& *(MasterEnvList+1) != CHAR_NULL
1630 ; MasterEnvList
+= StrLen(MasterEnvList
) + 1
1632 if (StrSize(MasterEnvList
) > ItemSize
) {
1633 ItemSize
= StrSize(MasterEnvList
);
1635 for (Temp
= StrStr(OriginalCommandLine
, MasterEnvList
)
1637 ; Temp
= StrStr(Temp
+1, MasterEnvList
)
1640 // we need a preceding and following % and if there is space no ^ preceding (if no space ignore)
1642 if (*(Temp
-1) == L
'%' && *(Temp
+StrLen(MasterEnvList
)) == L
'%' &&
1643 ((((Temp
-OriginalCommandLine
)>2) && *(Temp
-2) != L
'^') || ((Temp
-OriginalCommandLine
)<=2))) {
1644 NewSize
+=StrSize(EfiShellGetEnv(MasterEnvList
));
1650 // now do the replacements...
1652 NewCommandLine1
= AllocateZeroPool (NewSize
);
1653 NewCommandLine2
= AllocateZeroPool(NewSize
);
1654 ItemTemp
= AllocateZeroPool(ItemSize
+(2*sizeof(CHAR16
)));
1655 if (NewCommandLine1
== NULL
|| NewCommandLine2
== NULL
|| ItemTemp
== NULL
) {
1656 SHELL_FREE_NON_NULL(NewCommandLine1
);
1657 SHELL_FREE_NON_NULL(NewCommandLine2
);
1658 SHELL_FREE_NON_NULL(ItemTemp
);
1661 CopyMem (NewCommandLine1
, OriginalCommandLine
, StrSize (OriginalCommandLine
));
1663 for (MasterEnvList
= EfiShellGetEnv(NULL
)
1664 ; MasterEnvList
!= NULL
&& *MasterEnvList
!= CHAR_NULL
1665 ; MasterEnvList
+= StrLen(MasterEnvList
) + 1
1668 ((ItemSize
+(2*sizeof(CHAR16
)))/sizeof(CHAR16
)),
1672 ((ItemSize
+(2*sizeof(CHAR16
)))/sizeof(CHAR16
)),
1676 ((ItemSize
+(2*sizeof(CHAR16
)))/sizeof(CHAR16
)),
1679 ShellCopySearchAndReplace(NewCommandLine1
, NewCommandLine2
, NewSize
, ItemTemp
, EfiShellGetEnv(MasterEnvList
), TRUE
, FALSE
);
1680 StrCpyS(NewCommandLine1
, NewSize
/sizeof(CHAR16
), NewCommandLine2
);
1682 if (CurrentScriptFile
!= NULL
) {
1683 for (AliasListNode
= (ALIAS_LIST
*)GetFirstNode(&CurrentScriptFile
->SubstList
)
1684 ; !IsNull(&CurrentScriptFile
->SubstList
, &AliasListNode
->Link
)
1685 ; AliasListNode
= (ALIAS_LIST
*)GetNextNode(&CurrentScriptFile
->SubstList
, &AliasListNode
->Link
)
1687 ShellCopySearchAndReplace(NewCommandLine1
, NewCommandLine2
, NewSize
, AliasListNode
->Alias
, AliasListNode
->CommandString
, TRUE
, FALSE
);
1688 StrCpyS(NewCommandLine1
, NewSize
/sizeof(CHAR16
), NewCommandLine2
);
1693 // Remove non-existent environment variables
1695 StripUnreplacedEnvironmentVariables(NewCommandLine1
);
1698 // Now cleanup any straggler intentionally ignored "%" characters
1700 ShellCopySearchAndReplace(NewCommandLine1
, NewCommandLine2
, NewSize
, L
"^%", L
"%", TRUE
, FALSE
);
1701 StrCpyS(NewCommandLine1
, NewSize
/sizeof(CHAR16
), NewCommandLine2
);
1703 FreePool(NewCommandLine2
);
1706 return (NewCommandLine1
);
1710 Internal function to run a command line with pipe usage.
1712 @param[in] CmdLine The pointer to the command line.
1713 @param[in] StdIn The pointer to the Standard input.
1714 @param[in] StdOut The pointer to the Standard output.
1716 @retval EFI_SUCCESS The split command is executed successfully.
1717 @retval other Some error occurs when executing the split command.
1721 IN CONST CHAR16
*CmdLine
,
1722 IN SHELL_FILE_HANDLE StdIn
,
1723 IN SHELL_FILE_HANDLE StdOut
1727 CHAR16
*NextCommandLine
;
1728 CHAR16
*OurCommandLine
;
1732 SHELL_FILE_HANDLE TempFileHandle
;
1735 ASSERT(StdOut
== NULL
);
1737 ASSERT(StrStr(CmdLine
, L
"|") != NULL
);
1739 Status
= EFI_SUCCESS
;
1740 NextCommandLine
= NULL
;
1741 OurCommandLine
= NULL
;
1745 NextCommandLine
= StrnCatGrow(&NextCommandLine
, &Size1
, StrStr(CmdLine
, L
"|")+1, 0);
1746 OurCommandLine
= StrnCatGrow(&OurCommandLine
, &Size2
, CmdLine
, StrStr(CmdLine
, L
"|") - CmdLine
);
1748 if (NextCommandLine
== NULL
|| OurCommandLine
== NULL
) {
1749 SHELL_FREE_NON_NULL(OurCommandLine
);
1750 SHELL_FREE_NON_NULL(NextCommandLine
);
1751 return (EFI_OUT_OF_RESOURCES
);
1752 } else if (StrStr(OurCommandLine
, L
"|") != NULL
|| Size1
== 0 || Size2
== 0) {
1753 SHELL_FREE_NON_NULL(OurCommandLine
);
1754 SHELL_FREE_NON_NULL(NextCommandLine
);
1755 return (EFI_INVALID_PARAMETER
);
1756 } else if (NextCommandLine
[0] == L
'a' &&
1757 (NextCommandLine
[1] == L
' ' || NextCommandLine
[1] == CHAR_NULL
)
1759 CopyMem(NextCommandLine
, NextCommandLine
+1, StrSize(NextCommandLine
) - sizeof(NextCommandLine
[0]));
1760 while (NextCommandLine
[0] == L
' ') {
1761 CopyMem(NextCommandLine
, NextCommandLine
+1, StrSize(NextCommandLine
) - sizeof(NextCommandLine
[0]));
1763 if (NextCommandLine
[0] == CHAR_NULL
) {
1764 SHELL_FREE_NON_NULL(OurCommandLine
);
1765 SHELL_FREE_NON_NULL(NextCommandLine
);
1766 return (EFI_INVALID_PARAMETER
);
1775 // make a SPLIT_LIST item and add to list
1777 Split
= AllocateZeroPool(sizeof(SPLIT_LIST
));
1778 if (Split
== NULL
) {
1779 return EFI_OUT_OF_RESOURCES
;
1781 Split
->SplitStdIn
= StdIn
;
1782 Split
->SplitStdOut
= ConvertEfiFileProtocolToShellHandle(CreateFileInterfaceMem(Unicode
), NULL
);
1783 ASSERT(Split
->SplitStdOut
!= NULL
);
1784 InsertHeadList(&ShellInfoObject
.SplitList
.Link
, &Split
->Link
);
1786 Status
= RunCommand(OurCommandLine
);
1789 // move the output from the first to the in to the second.
1791 TempFileHandle
= Split
->SplitStdOut
;
1792 if (Split
->SplitStdIn
== StdIn
) {
1793 Split
->SplitStdOut
= NULL
;
1795 Split
->SplitStdOut
= Split
->SplitStdIn
;
1797 Split
->SplitStdIn
= TempFileHandle
;
1798 ShellInfoObject
.NewEfiShellProtocol
->SetFilePosition (Split
->SplitStdIn
, 0);
1800 if (!EFI_ERROR(Status
)) {
1801 Status
= RunCommand(NextCommandLine
);
1805 // remove the top level from the ScriptList
1807 ASSERT((SPLIT_LIST
*)GetFirstNode(&ShellInfoObject
.SplitList
.Link
) == Split
);
1808 RemoveEntryList(&Split
->Link
);
1811 // Note that the original StdIn is now the StdOut...
1813 if (Split
->SplitStdOut
!= NULL
) {
1814 ShellInfoObject
.NewEfiShellProtocol
->CloseFile (Split
->SplitStdOut
);
1816 if (Split
->SplitStdIn
!= NULL
) {
1817 ShellInfoObject
.NewEfiShellProtocol
->CloseFile (Split
->SplitStdIn
);
1821 FreePool(NextCommandLine
);
1822 FreePool(OurCommandLine
);
1828 Take the original command line, substitute any variables, free
1829 the original string, return the modified copy.
1831 @param[in] CmdLine pointer to the command line to update.
1833 @retval EFI_SUCCESS the function was successful.
1834 @retval EFI_OUT_OF_RESOURCES a memory allocation failed.
1837 ShellSubstituteVariables(
1842 NewCmdLine
= ShellConvertVariables(*CmdLine
);
1843 SHELL_FREE_NON_NULL(*CmdLine
);
1844 if (NewCmdLine
== NULL
) {
1845 return (EFI_OUT_OF_RESOURCES
);
1847 *CmdLine
= NewCmdLine
;
1848 return (EFI_SUCCESS
);
1852 Take the original command line, substitute any alias in the first group of space delimited characters, free
1853 the original string, return the modified copy.
1855 @param[in] CmdLine pointer to the command line to update.
1857 @retval EFI_SUCCESS the function was successful.
1858 @retval EFI_OUT_OF_RESOURCES a memory allocation failed.
1861 ShellSubstituteAliases(
1866 CHAR16
*CommandName
;
1868 UINTN PostAliasSize
;
1869 ASSERT(CmdLine
!= NULL
);
1870 ASSERT(*CmdLine
!= NULL
);
1874 if (StrStr((*CmdLine
), L
" ") == NULL
){
1875 StrnCatGrow(&CommandName
, NULL
, (*CmdLine
), 0);
1877 StrnCatGrow(&CommandName
, NULL
, (*CmdLine
), StrStr((*CmdLine
), L
" ") - (*CmdLine
));
1881 // This cannot happen 'inline' since the CmdLine can need extra space.
1884 if (!ShellCommandIsCommandOnList(CommandName
)) {
1886 // Convert via alias
1888 Status
= ShellConvertAlias(&CommandName
);
1889 if (EFI_ERROR(Status
)){
1893 NewCmdLine
= StrnCatGrow(&NewCmdLine
, &PostAliasSize
, CommandName
, 0);
1894 if (NewCmdLine
== NULL
) {
1895 SHELL_FREE_NON_NULL(CommandName
);
1896 SHELL_FREE_NON_NULL(*CmdLine
);
1897 return (EFI_OUT_OF_RESOURCES
);
1899 NewCmdLine
= StrnCatGrow(&NewCmdLine
, &PostAliasSize
, StrStr((*CmdLine
), L
" "), 0);
1900 if (NewCmdLine
== NULL
) {
1901 SHELL_FREE_NON_NULL(CommandName
);
1902 SHELL_FREE_NON_NULL(*CmdLine
);
1903 return (EFI_OUT_OF_RESOURCES
);
1906 NewCmdLine
= StrnCatGrow(&NewCmdLine
, NULL
, (*CmdLine
), 0);
1909 SHELL_FREE_NON_NULL(*CmdLine
);
1910 SHELL_FREE_NON_NULL(CommandName
);
1913 // re-assign the passed in double pointer to point to our newly allocated buffer
1915 *CmdLine
= NewCmdLine
;
1917 return (EFI_SUCCESS
);
1921 Takes the Argv[0] part of the command line and determine the meaning of it.
1923 @param[in] CmdName pointer to the command line to update.
1925 @retval Internal_Command The name is an internal command.
1926 @retval File_Sys_Change the name is a file system change.
1927 @retval Script_File_Name the name is a NSH script file.
1928 @retval Unknown_Invalid the name is unknown.
1929 @retval Efi_Application the name is an application (.EFI).
1931 SHELL_OPERATION_TYPES
1933 IN CONST CHAR16
*CmdName
1936 CHAR16
* FileWithPath
;
1937 CONST CHAR16
* TempLocation
;
1938 CONST CHAR16
* TempLocation2
;
1940 FileWithPath
= NULL
;
1942 // test for an internal command.
1944 if (ShellCommandIsCommandOnList(CmdName
)) {
1945 return (Internal_Command
);
1949 // Test for file system change request. anything ending with first : and cant have spaces.
1951 if (CmdName
[(StrLen(CmdName
)-1)] == L
':') {
1952 if ( StrStr(CmdName
, L
" ") != NULL
1953 || StrLen(StrStr(CmdName
, L
":")) > 1
1955 return (Unknown_Invalid
);
1957 return (File_Sys_Change
);
1963 if ((FileWithPath
= ShellFindFilePathEx(CmdName
, mExecutableExtensions
)) != NULL
) {
1965 // See if that file has a script file extension
1967 if (StrLen(FileWithPath
) > 4) {
1968 TempLocation
= FileWithPath
+StrLen(FileWithPath
)-4;
1969 TempLocation2
= mScriptExtension
;
1970 if (StringNoCaseCompare((VOID
*)(&TempLocation
), (VOID
*)(&TempLocation2
)) == 0) {
1971 SHELL_FREE_NON_NULL(FileWithPath
);
1972 return (Script_File_Name
);
1977 // Was a file, but not a script. we treat this as an application.
1979 SHELL_FREE_NON_NULL(FileWithPath
);
1980 return (Efi_Application
);
1983 SHELL_FREE_NON_NULL(FileWithPath
);
1985 // No clue what this is... return invalid flag...
1987 return (Unknown_Invalid
);
1991 Determine if the first item in a command line is valid.
1993 @param[in] CmdLine The command line to parse.
1995 @retval EFI_SUCCESS The item is valid.
1996 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
1997 @retval EFI_NOT_FOUND The operation type is unknown or invalid.
2001 IN CONST CHAR16
*CmdLine
2005 CHAR16
*FirstParameter
;
2011 Temp
= StrnCatGrow(&Temp
, NULL
, CmdLine
, 0);
2013 return (EFI_OUT_OF_RESOURCES
);
2016 FirstParameter
= StrStr(Temp
, L
"|");
2017 if (FirstParameter
!= NULL
) {
2018 *FirstParameter
= CHAR_NULL
;
2021 FirstParameter
= NULL
;
2024 // Process the command line
2026 Status
= ProcessCommandLineToFinal(&Temp
);
2028 if (!EFI_ERROR(Status
)) {
2029 FirstParameter
= AllocateZeroPool(StrSize(CmdLine
));
2030 if (FirstParameter
== NULL
) {
2031 SHELL_FREE_NON_NULL(Temp
);
2032 return (EFI_OUT_OF_RESOURCES
);
2034 TempWalker
= (CHAR16
*)Temp
;
2035 if (!EFI_ERROR(GetNextParameter(&TempWalker
, &FirstParameter
, StrSize(CmdLine
), TRUE
))) {
2036 if (GetOperationType(FirstParameter
) == Unknown_Invalid
) {
2037 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_NOT_FOUND
), ShellInfoObject
.HiiHandle
, FirstParameter
);
2038 SetLastError(SHELL_NOT_FOUND
);
2039 Status
= EFI_NOT_FOUND
;
2044 SHELL_FREE_NON_NULL(Temp
);
2045 SHELL_FREE_NON_NULL(FirstParameter
);
2050 Determine if a command line contains with a split contains only valid commands.
2052 @param[in] CmdLine The command line to parse.
2054 @retval EFI_SUCCESS CmdLine has only valid commands, application, or has no split.
2055 @retval EFI_ABORTED CmdLine has at least one invalid command or application.
2059 IN CONST CHAR16
*CmdLine
2062 CONST CHAR16
*TempSpot
;
2066 // If this was the only item, then get out
2068 if (!ContainsSplit(CmdLine
)) {
2069 return (EFI_SUCCESS
);
2073 // Verify up to the pipe or end character
2075 Status
= IsValidSplit(CmdLine
);
2076 if (EFI_ERROR(Status
)) {
2081 // recurse to verify the next item
2083 TempSpot
= FindFirstCharacter(CmdLine
, L
"|", L
'^') + 1;
2084 if (*TempSpot
== L
'a' &&
2085 (*(TempSpot
+ 1) == L
' ' || *(TempSpot
+ 1) == CHAR_NULL
)
2087 // If it's an ASCII pipe '|a'
2091 return (VerifySplit(TempSpot
));
2095 Process a split based operation.
2097 @param[in] CmdLine pointer to the command line to process
2099 @retval EFI_SUCCESS The operation was successful
2100 @return an error occurred.
2103 ProcessNewSplitCommandLine(
2104 IN CONST CHAR16
*CmdLine
2110 Status
= VerifySplit(CmdLine
);
2111 if (EFI_ERROR(Status
)) {
2118 // are we in an existing split???
2120 if (!IsListEmpty(&ShellInfoObject
.SplitList
.Link
)) {
2121 Split
= (SPLIT_LIST
*)GetFirstNode(&ShellInfoObject
.SplitList
.Link
);
2124 if (Split
== NULL
) {
2125 Status
= RunSplitCommand(CmdLine
, NULL
, NULL
);
2127 Status
= RunSplitCommand(CmdLine
, Split
->SplitStdIn
, Split
->SplitStdOut
);
2129 if (EFI_ERROR(Status
)) {
2130 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_INVALID_SPLIT
), ShellInfoObject
.HiiHandle
, CmdLine
);
2136 Handle a request to change the current file system.
2138 @param[in] CmdLine The passed in command line.
2140 @retval EFI_SUCCESS The operation was successful.
2144 IN CONST CHAR16
*CmdLine
2148 Status
= EFI_SUCCESS
;
2151 // make sure we are the right operation
2153 ASSERT(CmdLine
[(StrLen(CmdLine
)-1)] == L
':' && StrStr(CmdLine
, L
" ") == NULL
);
2156 // Call the protocol API to do the work
2158 Status
= ShellInfoObject
.NewEfiShellProtocol
->SetCurDir(NULL
, CmdLine
);
2161 // Report any errors
2163 if (EFI_ERROR(Status
)) {
2164 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_INVALID_MAPPING
), ShellInfoObject
.HiiHandle
, CmdLine
);
2171 Reprocess the command line to direct all -? to the help command.
2173 if found, will add "help" as argv[0], and move the rest later.
2175 @param[in,out] CmdLine pointer to the command line to update
2179 IN OUT CHAR16
**CmdLine
2182 CHAR16
*CurrentParameter
;
2184 CHAR16
*NewCommandLine
;
2186 UINTN NewCmdLineSize
;
2188 Status
= EFI_SUCCESS
;
2190 CurrentParameter
= AllocateZeroPool(StrSize(*CmdLine
));
2191 if (CurrentParameter
== NULL
) {
2192 return (EFI_OUT_OF_RESOURCES
);
2196 while(Walker
!= NULL
&& *Walker
!= CHAR_NULL
) {
2197 if (!EFI_ERROR(GetNextParameter(&Walker
, &CurrentParameter
, StrSize(*CmdLine
), TRUE
))) {
2198 if (StrStr(CurrentParameter
, L
"-?") == CurrentParameter
) {
2199 CurrentParameter
[0] = L
' ';
2200 CurrentParameter
[1] = L
' ';
2201 NewCmdLineSize
= StrSize(L
"help ") + StrSize(*CmdLine
);
2202 NewCommandLine
= AllocateZeroPool(NewCmdLineSize
);
2203 if (NewCommandLine
== NULL
) {
2204 Status
= EFI_OUT_OF_RESOURCES
;
2209 // We know the space is sufficient since we just calculated it.
2211 StrnCpyS(NewCommandLine
, NewCmdLineSize
/sizeof(CHAR16
), L
"help ", 5);
2212 StrnCatS(NewCommandLine
, NewCmdLineSize
/sizeof(CHAR16
), *CmdLine
, StrLen(*CmdLine
));
2213 SHELL_FREE_NON_NULL(*CmdLine
);
2214 *CmdLine
= NewCommandLine
;
2220 SHELL_FREE_NON_NULL(CurrentParameter
);
2226 Function to update the shell variable "lasterror".
2228 @param[in] ErrorCode the error code to put into lasterror.
2232 IN CONST SHELL_STATUS ErrorCode
2235 CHAR16 LeString
[19];
2236 if (sizeof(EFI_STATUS
) == sizeof(UINT64
)) {
2237 UnicodeSPrint(LeString
, sizeof(LeString
), L
"0x%Lx", ErrorCode
);
2239 UnicodeSPrint(LeString
, sizeof(LeString
), L
"0x%x", ErrorCode
);
2241 DEBUG_CODE(InternalEfiShellSetEnv(L
"debuglasterror", LeString
, TRUE
););
2242 InternalEfiShellSetEnv(L
"lasterror", LeString
, TRUE
);
2244 return (EFI_SUCCESS
);
2248 Converts the command line to it's post-processed form. this replaces variables and alias' per UEFI Shell spec.
2250 @param[in,out] CmdLine pointer to the command line to update
2252 @retval EFI_SUCCESS The operation was successful
2253 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
2254 @return some other error occurred
2257 ProcessCommandLineToFinal(
2258 IN OUT CHAR16
**CmdLine
2262 TrimSpaces(CmdLine
);
2264 Status
= ShellSubstituteAliases(CmdLine
);
2265 if (EFI_ERROR(Status
)) {
2269 TrimSpaces(CmdLine
);
2271 Status
= ShellSubstituteVariables(CmdLine
);
2272 if (EFI_ERROR(Status
)) {
2275 ASSERT (*CmdLine
!= NULL
);
2277 TrimSpaces(CmdLine
);
2280 // update for help parsing
2282 if (StrStr(*CmdLine
, L
"?") != NULL
) {
2284 // This may do nothing if the ? does not indicate help.
2285 // Save all the details for in the API below.
2287 Status
= DoHelpUpdate(CmdLine
);
2290 TrimSpaces(CmdLine
);
2292 return (EFI_SUCCESS
);
2296 Run an internal shell command.
2298 This API will update the shell's environment since these commands are libraries.
2300 @param[in] CmdLine the command line to run.
2301 @param[in] FirstParameter the first parameter on the command line
2302 @param[in] ParamProtocol the shell parameters protocol pointer
2303 @param[out] CommandStatus the status from the command line.
2305 @retval EFI_SUCCESS The command was completed.
2306 @retval EFI_ABORTED The command's operation was aborted.
2310 IN CONST CHAR16
*CmdLine
,
2311 IN CHAR16
*FirstParameter
,
2312 IN EFI_SHELL_PARAMETERS_PROTOCOL
*ParamProtocol
,
2313 OUT EFI_STATUS
*CommandStatus
2319 SHELL_STATUS CommandReturnedStatus
;
2324 NewCmdLine
= AllocateCopyPool (StrSize (CmdLine
), CmdLine
);
2325 if (NewCmdLine
== NULL
) {
2326 return EFI_OUT_OF_RESOURCES
;
2329 for (Walker
= NewCmdLine
; Walker
!= NULL
&& *Walker
!= CHAR_NULL
; Walker
++) {
2330 if (*Walker
== L
'^' && *(Walker
+1) == L
'#') {
2331 CopyMem(Walker
, Walker
+1, StrSize(Walker
) - sizeof(Walker
[0]));
2336 // get the argc and argv updated for internal commands
2338 Status
= UpdateArgcArgv(ParamProtocol
, NewCmdLine
, Internal_Command
, &Argv
, &Argc
);
2339 if (!EFI_ERROR(Status
)) {
2341 // Run the internal command.
2343 Status
= ShellCommandRunCommandHandler(FirstParameter
, &CommandReturnedStatus
, &LastError
);
2345 if (!EFI_ERROR(Status
)) {
2346 if (CommandStatus
!= NULL
) {
2347 if (CommandReturnedStatus
!= SHELL_SUCCESS
) {
2348 *CommandStatus
= (EFI_STATUS
)(CommandReturnedStatus
| MAX_BIT
);
2350 *CommandStatus
= EFI_SUCCESS
;
2355 // Update last error status.
2356 // some commands do not update last error.
2359 SetLastError(CommandReturnedStatus
);
2363 // Pass thru the exitcode from the app.
2365 if (ShellCommandGetExit()) {
2367 // An Exit was requested ("exit" command), pass its value up.
2369 Status
= CommandReturnedStatus
;
2370 } else if (CommandReturnedStatus
!= SHELL_SUCCESS
&& IsScriptOnlyCommand(FirstParameter
)) {
2372 // Always abort when a script only command fails for any reason
2374 Status
= EFI_ABORTED
;
2375 } else if (ShellCommandGetCurrentScriptFile() != NULL
&& CommandReturnedStatus
== SHELL_ABORTED
) {
2377 // Abort when in a script and a command aborted
2379 Status
= EFI_ABORTED
;
2385 // This is guaranteed to be called after UpdateArgcArgv no matter what else happened.
2386 // This is safe even if the update API failed. In this case, it may be a no-op.
2388 RestoreArgcArgv(ParamProtocol
, &Argv
, &Argc
);
2391 // If a script is running and the command is not a script only command, then
2392 // change return value to success so the script won't halt (unless aborted).
2394 // Script only commands have to be able halt the script since the script will
2395 // not operate if they are failing.
2397 if ( ShellCommandGetCurrentScriptFile() != NULL
2398 && !IsScriptOnlyCommand(FirstParameter
)
2399 && Status
!= EFI_ABORTED
2401 Status
= EFI_SUCCESS
;
2404 FreePool (NewCmdLine
);
2409 Function to run the command or file.
2411 @param[in] Type the type of operation being run.
2412 @param[in] CmdLine the command line to run.
2413 @param[in] FirstParameter the first parameter on the command line
2414 @param[in] ParamProtocol the shell parameters protocol pointer
2415 @param[out] CommandStatus the status from the command line.
2417 @retval EFI_SUCCESS The command was completed.
2418 @retval EFI_ABORTED The command's operation was aborted.
2422 IN SHELL_OPERATION_TYPES Type
,
2423 IN CONST CHAR16
*CmdLine
,
2424 IN CHAR16
*FirstParameter
,
2425 IN EFI_SHELL_PARAMETERS_PROTOCOL
*ParamProtocol
,
2426 OUT EFI_STATUS
*CommandStatus
2430 EFI_STATUS StartStatus
;
2431 CHAR16
*CommandWithPath
;
2432 EFI_DEVICE_PATH_PROTOCOL
*DevPath
;
2433 SHELL_STATUS CalleeExitStatus
;
2435 Status
= EFI_SUCCESS
;
2436 CommandWithPath
= NULL
;
2438 CalleeExitStatus
= SHELL_INVALID_PARAMETER
;
2441 case Internal_Command
:
2442 Status
= RunInternalCommand(CmdLine
, FirstParameter
, ParamProtocol
, CommandStatus
);
2444 case Script_File_Name
:
2445 case Efi_Application
:
2447 // Process a fully qualified path
2449 if (StrStr(FirstParameter
, L
":") != NULL
) {
2450 ASSERT (CommandWithPath
== NULL
);
2451 if (ShellIsFile(FirstParameter
) == EFI_SUCCESS
) {
2452 CommandWithPath
= StrnCatGrow(&CommandWithPath
, NULL
, FirstParameter
, 0);
2457 // Process a relative path and also check in the path environment variable
2459 if (CommandWithPath
== NULL
) {
2460 CommandWithPath
= ShellFindFilePathEx(FirstParameter
, mExecutableExtensions
);
2464 // This should be impossible now.
2466 ASSERT(CommandWithPath
!= NULL
);
2469 // Make sure that path is not just a directory (or not found)
2471 if (!EFI_ERROR(ShellIsDirectory(CommandWithPath
))) {
2472 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_NOT_FOUND
), ShellInfoObject
.HiiHandle
, FirstParameter
);
2473 SetLastError(SHELL_NOT_FOUND
);
2476 case Script_File_Name
:
2477 Status
= RunScriptFile (CommandWithPath
, NULL
, CmdLine
, ParamProtocol
);
2479 case Efi_Application
:
2481 // Get the device path of the application image
2483 DevPath
= ShellInfoObject
.NewEfiShellProtocol
->GetDevicePathFromFilePath(CommandWithPath
);
2484 if (DevPath
== NULL
){
2485 Status
= EFI_OUT_OF_RESOURCES
;
2490 // Execute the device path
2492 Status
= InternalShellExecuteDevicePath(
2500 SHELL_FREE_NON_NULL(DevPath
);
2502 if(EFI_ERROR (Status
)) {
2503 CalleeExitStatus
= (SHELL_STATUS
) (Status
& (~MAX_BIT
));
2505 CalleeExitStatus
= (SHELL_STATUS
) StartStatus
;
2508 if (CommandStatus
!= NULL
) {
2509 *CommandStatus
= CalleeExitStatus
;
2513 // Update last error status.
2515 // Status is an EFI_STATUS. Clear top bit to convert to SHELL_STATUS
2516 SetLastError(CalleeExitStatus
);
2532 SHELL_FREE_NON_NULL(CommandWithPath
);
2538 Function to setup StdIn, StdErr, StdOut, and then run the command or file.
2540 @param[in] Type the type of operation being run.
2541 @param[in] CmdLine the command line to run.
2542 @param[in] FirstParameter the first parameter on the command line.
2543 @param[in] ParamProtocol the shell parameters protocol pointer
2544 @param[out] CommandStatus the status from the command line.
2546 @retval EFI_SUCCESS The command was completed.
2547 @retval EFI_ABORTED The command's operation was aborted.
2550 SetupAndRunCommandOrFile(
2551 IN SHELL_OPERATION_TYPES Type
,
2553 IN CHAR16
*FirstParameter
,
2554 IN EFI_SHELL_PARAMETERS_PROTOCOL
*ParamProtocol
,
2555 OUT EFI_STATUS
*CommandStatus
2559 SHELL_FILE_HANDLE OriginalStdIn
;
2560 SHELL_FILE_HANDLE OriginalStdOut
;
2561 SHELL_FILE_HANDLE OriginalStdErr
;
2562 SYSTEM_TABLE_INFO OriginalSystemTableInfo
;
2563 CONST SCRIPT_FILE
*ConstScriptFile
;
2566 // Update the StdIn, StdOut, and StdErr for redirection to environment variables, files, etc... unicode and ASCII
2568 Status
= UpdateStdInStdOutStdErr(ParamProtocol
, CmdLine
, &OriginalStdIn
, &OriginalStdOut
, &OriginalStdErr
, &OriginalSystemTableInfo
);
2571 // The StdIn, StdOut, and StdErr are set up.
2572 // Now run the command, script, or application
2574 if (!EFI_ERROR(Status
)) {
2575 TrimSpaces(&CmdLine
);
2576 Status
= RunCommandOrFile(Type
, CmdLine
, FirstParameter
, ParamProtocol
, CommandStatus
);
2582 if (EFI_ERROR(Status
)) {
2583 ConstScriptFile
= ShellCommandGetCurrentScriptFile();
2584 if (ConstScriptFile
== NULL
|| ConstScriptFile
->CurrentCommand
== NULL
) {
2585 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_ERROR
), ShellInfoObject
.HiiHandle
, (VOID
*)(Status
));
2587 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_ERROR_SCRIPT
), ShellInfoObject
.HiiHandle
, (VOID
*)(Status
), ConstScriptFile
->CurrentCommand
->Line
);
2592 // put back the original StdIn, StdOut, and StdErr
2594 RestoreStdInStdOutStdErr(ParamProtocol
, &OriginalStdIn
, &OriginalStdOut
, &OriginalStdErr
, &OriginalSystemTableInfo
);
2600 Function will process and run a command line.
2602 This will determine if the command line represents an internal shell
2603 command or dispatch an external application.
2605 @param[in] CmdLine The command line to parse.
2606 @param[out] CommandStatus The status from the command line.
2608 @retval EFI_SUCCESS The command was completed.
2609 @retval EFI_ABORTED The command's operation was aborted.
2613 IN CONST CHAR16
*CmdLine
,
2614 OUT EFI_STATUS
*CommandStatus
2618 CHAR16
*CleanOriginal
;
2619 CHAR16
*FirstParameter
;
2621 SHELL_OPERATION_TYPES Type
;
2622 CONST CHAR16
*CurDir
;
2624 ASSERT(CmdLine
!= NULL
);
2625 if (StrLen(CmdLine
) == 0) {
2626 return (EFI_SUCCESS
);
2629 Status
= EFI_SUCCESS
;
2630 CleanOriginal
= NULL
;
2632 CleanOriginal
= StrnCatGrow(&CleanOriginal
, NULL
, CmdLine
, 0);
2633 if (CleanOriginal
== NULL
) {
2634 return (EFI_OUT_OF_RESOURCES
);
2637 TrimSpaces(&CleanOriginal
);
2640 // NULL out comments (leveraged from RunScriptFileHandle() ).
2641 // The # character on a line is used to denote that all characters on the same line
2642 // and to the right of the # are to be ignored by the shell.
2643 // Afterwards, again remove spaces, in case any were between the last command-parameter and '#'.
2645 for (TempWalker
= CleanOriginal
; TempWalker
!= NULL
&& *TempWalker
!= CHAR_NULL
; TempWalker
++) {
2646 if (*TempWalker
== L
'^') {
2647 if (*(TempWalker
+ 1) == L
'#') {
2650 } else if (*TempWalker
== L
'#') {
2651 *TempWalker
= CHAR_NULL
;
2655 TrimSpaces(&CleanOriginal
);
2658 // Handle case that passed in command line is just 1 or more " " characters.
2660 if (StrLen (CleanOriginal
) == 0) {
2661 SHELL_FREE_NON_NULL(CleanOriginal
);
2662 return (EFI_SUCCESS
);
2665 Status
= ProcessCommandLineToFinal(&CleanOriginal
);
2666 if (EFI_ERROR(Status
)) {
2667 SHELL_FREE_NON_NULL(CleanOriginal
);
2672 // We don't do normal processing with a split command line (output from one command input to another)
2674 if (ContainsSplit(CleanOriginal
)) {
2675 Status
= ProcessNewSplitCommandLine(CleanOriginal
);
2676 SHELL_FREE_NON_NULL(CleanOriginal
);
2681 // We need the first parameter information so we can determine the operation type
2683 FirstParameter
= AllocateZeroPool(StrSize(CleanOriginal
));
2684 if (FirstParameter
== NULL
) {
2685 SHELL_FREE_NON_NULL(CleanOriginal
);
2686 return (EFI_OUT_OF_RESOURCES
);
2688 TempWalker
= CleanOriginal
;
2689 if (!EFI_ERROR(GetNextParameter(&TempWalker
, &FirstParameter
, StrSize(CleanOriginal
), TRUE
))) {
2691 // Depending on the first parameter we change the behavior
2693 switch (Type
= GetOperationType(FirstParameter
)) {
2694 case File_Sys_Change
:
2695 Status
= ChangeMappedDrive (FirstParameter
);
2697 case Internal_Command
:
2698 case Script_File_Name
:
2699 case Efi_Application
:
2700 Status
= SetupAndRunCommandOrFile(Type
, CleanOriginal
, FirstParameter
, ShellInfoObject
.NewShellParametersProtocol
, CommandStatus
);
2704 // Whatever was typed, it was invalid.
2706 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_NOT_FOUND
), ShellInfoObject
.HiiHandle
, FirstParameter
);
2707 SetLastError(SHELL_NOT_FOUND
);
2711 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_NOT_FOUND
), ShellInfoObject
.HiiHandle
, FirstParameter
);
2712 SetLastError(SHELL_NOT_FOUND
);
2715 // Check whether the current file system still exists. If not exist, we need update "cwd" and gShellCurMapping.
2717 CurDir
= EfiShellGetCurDir (NULL
);
2718 if (CurDir
!= NULL
) {
2719 if (EFI_ERROR(ShellFileExists (CurDir
))) {
2721 // EfiShellSetCurDir() cannot set current directory to NULL.
2722 // EfiShellSetEnv() is not allowed to set the "cwd" variable.
2723 // Only InternalEfiShellSetEnv () is allowed setting the "cwd" variable.
2725 InternalEfiShellSetEnv (L
"cwd", NULL
, TRUE
);
2726 gShellCurMapping
= NULL
;
2730 SHELL_FREE_NON_NULL(CleanOriginal
);
2731 SHELL_FREE_NON_NULL(FirstParameter
);
2737 Function will process and run a command line.
2739 This will determine if the command line represents an internal shell
2740 command or dispatch an external application.
2742 @param[in] CmdLine The command line to parse.
2744 @retval EFI_SUCCESS The command was completed.
2745 @retval EFI_ABORTED The command's operation was aborted.
2749 IN CONST CHAR16
*CmdLine
2752 return (RunShellCommand(CmdLine
, NULL
));
2756 Function to process a NSH script file via SHELL_FILE_HANDLE.
2758 @param[in] Handle The handle to the already opened file.
2759 @param[in] Name The name of the script file.
2761 @retval EFI_SUCCESS the script completed successfully
2764 RunScriptFileHandle (
2765 IN SHELL_FILE_HANDLE Handle
,
2766 IN CONST CHAR16
*Name
2770 SCRIPT_FILE
*NewScriptFile
;
2772 UINTN PrintBuffSize
;
2773 CHAR16
*CommandLine
;
2774 CHAR16
*CommandLine2
;
2775 CHAR16
*CommandLine3
;
2776 SCRIPT_COMMAND_LIST
*LastCommand
;
2778 BOOLEAN PreScriptEchoState
;
2779 BOOLEAN PreCommandEchoState
;
2780 CONST CHAR16
*CurDir
;
2782 CHAR16 LeString
[50];
2783 LIST_ENTRY OldBufferList
;
2785 ASSERT(!ShellCommandGetScriptExit());
2787 PreScriptEchoState
= ShellCommandGetEchoState();
2788 PrintBuffSize
= PcdGet16(PcdShellPrintBufferSize
);
2790 NewScriptFile
= (SCRIPT_FILE
*)AllocateZeroPool(sizeof(SCRIPT_FILE
));
2791 if (NewScriptFile
== NULL
) {
2792 return (EFI_OUT_OF_RESOURCES
);
2798 ASSERT(NewScriptFile
->ScriptName
== NULL
);
2799 NewScriptFile
->ScriptName
= StrnCatGrow(&NewScriptFile
->ScriptName
, NULL
, Name
, 0);
2800 if (NewScriptFile
->ScriptName
== NULL
) {
2801 DeleteScriptFileStruct(NewScriptFile
);
2802 return (EFI_OUT_OF_RESOURCES
);
2806 // Save the parameters (used to replace %0 to %9 later on)
2808 NewScriptFile
->Argc
= ShellInfoObject
.NewShellParametersProtocol
->Argc
;
2809 if (NewScriptFile
->Argc
!= 0) {
2810 NewScriptFile
->Argv
= (CHAR16
**)AllocateZeroPool(NewScriptFile
->Argc
* sizeof(CHAR16
*));
2811 if (NewScriptFile
->Argv
== NULL
) {
2812 DeleteScriptFileStruct(NewScriptFile
);
2813 return (EFI_OUT_OF_RESOURCES
);
2815 for (LoopVar
= 0 ; LoopVar
< 10 && LoopVar
< NewScriptFile
->Argc
; LoopVar
++) {
2816 ASSERT(NewScriptFile
->Argv
[LoopVar
] == NULL
);
2817 NewScriptFile
->Argv
[LoopVar
] = StrnCatGrow(&NewScriptFile
->Argv
[LoopVar
], NULL
, ShellInfoObject
.NewShellParametersProtocol
->Argv
[LoopVar
], 0);
2818 if (NewScriptFile
->Argv
[LoopVar
] == NULL
) {
2819 DeleteScriptFileStruct(NewScriptFile
);
2820 return (EFI_OUT_OF_RESOURCES
);
2824 NewScriptFile
->Argv
= NULL
;
2827 InitializeListHead(&NewScriptFile
->CommandList
);
2828 InitializeListHead(&NewScriptFile
->SubstList
);
2831 // Now build the list of all script commands.
2834 while(!ShellFileHandleEof(Handle
)) {
2835 CommandLine
= ShellFileHandleReturnLine(Handle
, &Ascii
);
2837 if (CommandLine
== NULL
|| StrLen(CommandLine
) == 0 || CommandLine
[0] == '#') {
2838 SHELL_FREE_NON_NULL(CommandLine
);
2841 NewScriptFile
->CurrentCommand
= AllocateZeroPool(sizeof(SCRIPT_COMMAND_LIST
));
2842 if (NewScriptFile
->CurrentCommand
== NULL
) {
2843 SHELL_FREE_NON_NULL(CommandLine
);
2844 DeleteScriptFileStruct(NewScriptFile
);
2845 return (EFI_OUT_OF_RESOURCES
);
2848 NewScriptFile
->CurrentCommand
->Cl
= CommandLine
;
2849 NewScriptFile
->CurrentCommand
->Data
= NULL
;
2850 NewScriptFile
->CurrentCommand
->Line
= LineCount
;
2852 InsertTailList(&NewScriptFile
->CommandList
, &NewScriptFile
->CurrentCommand
->Link
);
2856 // Add this as the topmost script file
2858 ShellCommandSetNewScript (NewScriptFile
);
2861 // Now enumerate through the commands and run each one.
2863 CommandLine
= AllocateZeroPool(PrintBuffSize
);
2864 if (CommandLine
== NULL
) {
2865 DeleteScriptFileStruct(NewScriptFile
);
2866 return (EFI_OUT_OF_RESOURCES
);
2868 CommandLine2
= AllocateZeroPool(PrintBuffSize
);
2869 if (CommandLine2
== NULL
) {
2870 FreePool(CommandLine
);
2871 DeleteScriptFileStruct(NewScriptFile
);
2872 return (EFI_OUT_OF_RESOURCES
);
2875 for ( NewScriptFile
->CurrentCommand
= (SCRIPT_COMMAND_LIST
*)GetFirstNode(&NewScriptFile
->CommandList
)
2876 ; !IsNull(&NewScriptFile
->CommandList
, &NewScriptFile
->CurrentCommand
->Link
)
2877 ; // conditional increment in the body of the loop
2879 ASSERT(CommandLine2
!= NULL
);
2880 StrnCpyS( CommandLine2
,
2881 PrintBuffSize
/sizeof(CHAR16
),
2882 NewScriptFile
->CurrentCommand
->Cl
,
2883 PrintBuffSize
/sizeof(CHAR16
) - 1
2886 SaveBufferList(&OldBufferList
);
2889 // NULL out comments
2891 for (CommandLine3
= CommandLine2
; CommandLine3
!= NULL
&& *CommandLine3
!= CHAR_NULL
; CommandLine3
++) {
2892 if (*CommandLine3
== L
'^') {
2893 if ( *(CommandLine3
+1) == L
':') {
2894 CopyMem(CommandLine3
, CommandLine3
+1, StrSize(CommandLine3
) - sizeof(CommandLine3
[0]));
2895 } else if (*(CommandLine3
+1) == L
'#') {
2898 } else if (*CommandLine3
== L
'#') {
2899 *CommandLine3
= CHAR_NULL
;
2903 if (CommandLine2
!= NULL
&& StrLen(CommandLine2
) >= 1) {
2905 // Due to variability in starting the find and replace action we need to have both buffers the same.
2907 StrnCpyS( CommandLine
,
2908 PrintBuffSize
/sizeof(CHAR16
),
2910 PrintBuffSize
/sizeof(CHAR16
) - 1
2914 // Remove the %0 to %9 from the command line (if we have some arguments)
2916 if (NewScriptFile
->Argv
!= NULL
) {
2917 switch (NewScriptFile
->Argc
) {
2919 Status
= ShellCopySearchAndReplace(CommandLine2
, CommandLine
, PrintBuffSize
, L
"%9", NewScriptFile
->Argv
[9], FALSE
, FALSE
);
2920 ASSERT_EFI_ERROR(Status
);
2922 Status
= ShellCopySearchAndReplace(CommandLine
, CommandLine2
, PrintBuffSize
, L
"%8", NewScriptFile
->Argv
[8], FALSE
, FALSE
);
2923 ASSERT_EFI_ERROR(Status
);
2925 Status
= ShellCopySearchAndReplace(CommandLine2
, CommandLine
, PrintBuffSize
, L
"%7", NewScriptFile
->Argv
[7], FALSE
, FALSE
);
2926 ASSERT_EFI_ERROR(Status
);
2928 Status
= ShellCopySearchAndReplace(CommandLine
, CommandLine2
, PrintBuffSize
, L
"%6", NewScriptFile
->Argv
[6], FALSE
, FALSE
);
2929 ASSERT_EFI_ERROR(Status
);
2931 Status
= ShellCopySearchAndReplace(CommandLine2
, CommandLine
, PrintBuffSize
, L
"%5", NewScriptFile
->Argv
[5], FALSE
, FALSE
);
2932 ASSERT_EFI_ERROR(Status
);
2934 Status
= ShellCopySearchAndReplace(CommandLine
, CommandLine2
, PrintBuffSize
, L
"%4", NewScriptFile
->Argv
[4], FALSE
, FALSE
);
2935 ASSERT_EFI_ERROR(Status
);
2937 Status
= ShellCopySearchAndReplace(CommandLine2
, CommandLine
, PrintBuffSize
, L
"%3", NewScriptFile
->Argv
[3], FALSE
, FALSE
);
2938 ASSERT_EFI_ERROR(Status
);
2940 Status
= ShellCopySearchAndReplace(CommandLine
, CommandLine2
, PrintBuffSize
, L
"%2", NewScriptFile
->Argv
[2], FALSE
, FALSE
);
2941 ASSERT_EFI_ERROR(Status
);
2943 Status
= ShellCopySearchAndReplace(CommandLine2
, CommandLine
, PrintBuffSize
, L
"%1", NewScriptFile
->Argv
[1], FALSE
, FALSE
);
2944 ASSERT_EFI_ERROR(Status
);
2946 Status
= ShellCopySearchAndReplace(CommandLine
, CommandLine2
, PrintBuffSize
, L
"%0", NewScriptFile
->Argv
[0], FALSE
, FALSE
);
2947 ASSERT_EFI_ERROR(Status
);
2953 Status
= ShellCopySearchAndReplace(CommandLine2
, CommandLine
, PrintBuffSize
, L
"%1", L
"\"\"", FALSE
, FALSE
);
2954 Status
= ShellCopySearchAndReplace(CommandLine
, CommandLine2
, PrintBuffSize
, L
"%2", L
"\"\"", FALSE
, FALSE
);
2955 Status
= ShellCopySearchAndReplace(CommandLine2
, CommandLine
, PrintBuffSize
, L
"%3", L
"\"\"", FALSE
, FALSE
);
2956 Status
= ShellCopySearchAndReplace(CommandLine
, CommandLine2
, PrintBuffSize
, L
"%4", L
"\"\"", FALSE
, FALSE
);
2957 Status
= ShellCopySearchAndReplace(CommandLine2
, CommandLine
, PrintBuffSize
, L
"%5", L
"\"\"", FALSE
, FALSE
);
2958 Status
= ShellCopySearchAndReplace(CommandLine
, CommandLine2
, PrintBuffSize
, L
"%6", L
"\"\"", FALSE
, FALSE
);
2959 Status
= ShellCopySearchAndReplace(CommandLine2
, CommandLine
, PrintBuffSize
, L
"%7", L
"\"\"", FALSE
, FALSE
);
2960 Status
= ShellCopySearchAndReplace(CommandLine
, CommandLine2
, PrintBuffSize
, L
"%8", L
"\"\"", FALSE
, FALSE
);
2961 Status
= ShellCopySearchAndReplace(CommandLine2
, CommandLine
, PrintBuffSize
, L
"%9", L
"\"\"", FALSE
, FALSE
);
2963 StrnCpyS( CommandLine2
,
2964 PrintBuffSize
/sizeof(CHAR16
),
2966 PrintBuffSize
/sizeof(CHAR16
) - 1
2969 LastCommand
= NewScriptFile
->CurrentCommand
;
2971 for (CommandLine3
= CommandLine2
; CommandLine3
[0] == L
' ' ; CommandLine3
++);
2973 if (CommandLine3
!= NULL
&& CommandLine3
[0] == L
':' ) {
2975 // This line is a goto target / label
2978 if (CommandLine3
!= NULL
&& StrLen(CommandLine3
) > 0) {
2979 if (CommandLine3
[0] == L
'@') {
2981 // We need to save the current echo state
2982 // and disable echo for just this command.
2984 PreCommandEchoState
= ShellCommandGetEchoState();
2985 ShellCommandSetEchoState(FALSE
);
2986 Status
= RunCommand(CommandLine3
+1);
2989 // If command was "@echo -off" or "@echo -on" then don't restore echo state
2991 if (StrCmp (L
"@echo -off", CommandLine3
) != 0 &&
2992 StrCmp (L
"@echo -on", CommandLine3
) != 0) {
2994 // Now restore the pre-'@' echo state.
2996 ShellCommandSetEchoState(PreCommandEchoState
);
2999 if (ShellCommandGetEchoState()) {
3000 CurDir
= ShellInfoObject
.NewEfiShellProtocol
->GetEnv(L
"cwd");
3001 if (CurDir
!= NULL
&& StrLen(CurDir
) > 1) {
3002 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_CURDIR
), ShellInfoObject
.HiiHandle
, CurDir
);
3004 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_SHELL
), ShellInfoObject
.HiiHandle
);
3006 ShellPrintEx(-1, -1, L
"%s\r\n", CommandLine2
);
3008 Status
= RunCommand(CommandLine3
);
3012 if (ShellCommandGetScriptExit()) {
3014 // ShellCommandGetExitCode() always returns a UINT64
3016 UnicodeSPrint(LeString
, sizeof(LeString
), L
"0x%Lx", ShellCommandGetExitCode());
3017 DEBUG_CODE(InternalEfiShellSetEnv(L
"debuglasterror", LeString
, TRUE
););
3018 InternalEfiShellSetEnv(L
"lasterror", LeString
, TRUE
);
3020 ShellCommandRegisterExit(FALSE
, 0);
3021 Status
= EFI_SUCCESS
;
3022 RestoreBufferList(&OldBufferList
);
3025 if (ShellGetExecutionBreakFlag()) {
3026 RestoreBufferList(&OldBufferList
);
3029 if (EFI_ERROR(Status
)) {
3030 RestoreBufferList(&OldBufferList
);
3033 if (ShellCommandGetExit()) {
3034 RestoreBufferList(&OldBufferList
);
3039 // If that commend did not update the CurrentCommand then we need to advance it...
3041 if (LastCommand
== NewScriptFile
->CurrentCommand
) {
3042 NewScriptFile
->CurrentCommand
= (SCRIPT_COMMAND_LIST
*)GetNextNode(&NewScriptFile
->CommandList
, &NewScriptFile
->CurrentCommand
->Link
);
3043 if (!IsNull(&NewScriptFile
->CommandList
, &NewScriptFile
->CurrentCommand
->Link
)) {
3044 NewScriptFile
->CurrentCommand
->Reset
= TRUE
;
3048 NewScriptFile
->CurrentCommand
= (SCRIPT_COMMAND_LIST
*)GetNextNode(&NewScriptFile
->CommandList
, &NewScriptFile
->CurrentCommand
->Link
);
3049 if (!IsNull(&NewScriptFile
->CommandList
, &NewScriptFile
->CurrentCommand
->Link
)) {
3050 NewScriptFile
->CurrentCommand
->Reset
= TRUE
;
3053 RestoreBufferList(&OldBufferList
);
3057 FreePool(CommandLine
);
3058 FreePool(CommandLine2
);
3059 ShellCommandSetNewScript (NULL
);
3062 // Only if this was the last script reset the state.
3064 if (ShellCommandGetCurrentScriptFile()==NULL
) {
3065 ShellCommandSetEchoState(PreScriptEchoState
);
3067 return (EFI_SUCCESS
);
3071 Function to process a NSH script file.
3073 @param[in] ScriptPath Pointer to the script file name (including file system path).
3074 @param[in] Handle the handle of the script file already opened.
3075 @param[in] CmdLine the command line to run.
3076 @param[in] ParamProtocol the shell parameters protocol pointer
3078 @retval EFI_SUCCESS the script completed successfully
3082 IN CONST CHAR16
*ScriptPath
,
3083 IN SHELL_FILE_HANDLE Handle OPTIONAL
,
3084 IN CONST CHAR16
*CmdLine
,
3085 IN EFI_SHELL_PARAMETERS_PROTOCOL
*ParamProtocol
3089 SHELL_FILE_HANDLE FileHandle
;
3093 if (ShellIsFile(ScriptPath
) != EFI_SUCCESS
) {
3094 return (EFI_INVALID_PARAMETER
);
3098 // get the argc and argv updated for scripts
3100 Status
= UpdateArgcArgv(ParamProtocol
, CmdLine
, Script_File_Name
, &Argv
, &Argc
);
3101 if (!EFI_ERROR(Status
)) {
3103 if (Handle
== NULL
) {
3107 Status
= ShellOpenFileByName(ScriptPath
, &FileHandle
, EFI_FILE_MODE_READ
, 0);
3108 if (!EFI_ERROR(Status
)) {
3112 Status
= RunScriptFileHandle(FileHandle
, ScriptPath
);
3115 // now close the file
3117 ShellCloseFile(&FileHandle
);
3120 Status
= RunScriptFileHandle(Handle
, ScriptPath
);
3125 // This is guaranteed to be called after UpdateArgcArgv no matter what else happened.
3126 // This is safe even if the update API failed. In this case, it may be a no-op.
3128 RestoreArgcArgv(ParamProtocol
, &Argv
, &Argc
);
3134 Return the pointer to the first occurrence of any character from a list of characters.
3136 @param[in] String the string to parse
3137 @param[in] CharacterList the list of character to look for
3138 @param[in] EscapeCharacter An escape character to skip
3140 @return the location of the first character in the string
3141 @retval CHAR_NULL no instance of any character in CharacterList was found in String
3145 IN CONST CHAR16
*String
,
3146 IN CONST CHAR16
*CharacterList
,
3147 IN CONST CHAR16 EscapeCharacter
3153 for (WalkStr
= 0; WalkStr
< StrLen(String
); WalkStr
++) {
3154 if (String
[WalkStr
] == EscapeCharacter
) {
3158 for (WalkChar
= 0; WalkChar
< StrLen(CharacterList
); WalkChar
++) {
3159 if (String
[WalkStr
] == CharacterList
[WalkChar
]) {
3160 return (&String
[WalkStr
]);
3164 return (String
+ StrLen(String
));