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 Copyright 2015-2018 Dell Technologies.<BR>
7 This program and the accompanying materials
8 are licensed and made available under the terms and conditions of the BSD License
9 which accompanies this distribution. The full text of the license may be found at
10 http://opensource.org/licenses/bsd-license.php
12 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
13 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
20 // Initialize the global structure
22 SHELL_INFO ShellInfoObject
= {
58 {{NULL
, NULL
}, NULL
, NULL
},
59 {{NULL
, NULL
}, NULL
, NULL
},
71 STATIC CONST CHAR16 mScriptExtension
[] = L
".NSH";
72 STATIC CONST CHAR16 mExecutableExtensions
[] = L
".NSH;.EFI";
73 STATIC CONST CHAR16 mStartupScript
[] = L
"startup.nsh";
74 CONST CHAR16 mNoNestingEnvVarName
[] = L
"nonesting";
75 CONST CHAR16 mNoNestingTrue
[] = L
"True";
76 CONST CHAR16 mNoNestingFalse
[] = L
"False";
79 Cleans off leading and trailing spaces and tabs.
81 @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
117 IN CONST CHAR16
*SourceString
,
118 IN CONST CHAR16
*FindString
,
119 IN CONST BOOLEAN CheckForEscapeCharacter
123 if (SourceString
== NULL
) {
126 Temp
= StrStr(SourceString
, FindString
);
129 // If nothing found, or we don't care about escape characters
131 if (Temp
== NULL
|| !CheckForEscapeCharacter
) {
136 // If we found an escaped character, try again on the remainder of the string
138 if ((Temp
> (SourceString
)) && *(Temp
-1) == L
'^') {
139 return FindNextInstance(Temp
+1, FindString
, CheckForEscapeCharacter
);
143 // we found the right character
149 Check whether the string between a pair of % is a valid environment variable name.
151 @param[in] BeginPercent pointer to the first percent.
152 @param[in] EndPercent pointer to the last percent.
154 @retval TRUE is a valid environment variable name.
155 @retval FALSE is NOT a valid environment variable name.
158 IsValidEnvironmentVariableName(
159 IN CONST CHAR16
*BeginPercent
,
160 IN CONST CHAR16
*EndPercent
163 CONST CHAR16
*Walker
;
167 ASSERT (BeginPercent
!= NULL
);
168 ASSERT (EndPercent
!= NULL
);
169 ASSERT (BeginPercent
< EndPercent
);
171 if ((BeginPercent
+ 1) == EndPercent
) {
175 for (Walker
= BeginPercent
+ 1; Walker
< EndPercent
; Walker
++) {
177 (*Walker
>= L
'0' && *Walker
<= L
'9') ||
178 (*Walker
>= L
'A' && *Walker
<= L
'Z') ||
179 (*Walker
>= L
'a' && *Walker
<= L
'z') ||
182 if (Walker
== BeginPercent
+ 1 && (*Walker
>= L
'0' && *Walker
<= L
'9')) {
196 Determine if a command line contains a split operation
198 @param[in] CmdLine The command line to parse.
200 @retval TRUE CmdLine has a valid split.
201 @retval FALSE CmdLine does not have a valid split.
205 IN CONST CHAR16
*CmdLine
208 CONST CHAR16
*TempSpot
;
209 CONST CHAR16
*FirstQuote
;
210 CONST CHAR16
*SecondQuote
;
212 FirstQuote
= FindNextInstance (CmdLine
, L
"\"", TRUE
);
214 TempSpot
= FindFirstCharacter(CmdLine
, L
"|", L
'^');
216 if (FirstQuote
== NULL
||
218 TempSpot
== CHAR_NULL
||
219 FirstQuote
> TempSpot
221 return (BOOLEAN
) ((TempSpot
!= NULL
) && (*TempSpot
!= CHAR_NULL
));
224 while ((TempSpot
!= NULL
) && (*TempSpot
!= CHAR_NULL
)) {
225 if (FirstQuote
== NULL
|| FirstQuote
> TempSpot
) {
228 SecondQuote
= FindNextInstance (FirstQuote
+ 1, L
"\"", TRUE
);
229 if (SecondQuote
== NULL
) {
232 if (SecondQuote
< TempSpot
) {
233 FirstQuote
= FindNextInstance (SecondQuote
+ 1, L
"\"", TRUE
);
236 FirstQuote
= FindNextInstance (SecondQuote
+ 1, L
"\"", TRUE
);
237 TempSpot
= FindFirstCharacter(TempSpot
+ 1, L
"|", L
'^');
242 return (BOOLEAN
) ((TempSpot
!= NULL
) && (*TempSpot
!= CHAR_NULL
));
246 Function to start monitoring for CTRL-S using SimpleTextInputEx. This
247 feature's enabled state was not known when the shell initially launched.
249 @retval EFI_SUCCESS The feature is enabled.
250 @retval EFI_OUT_OF_RESOURCES There is not enough memory available.
253 InternalEfiShellStartCtrlSMonitor(
257 EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL
*SimpleEx
;
258 EFI_KEY_DATA KeyData
;
261 Status
= gBS
->OpenProtocol(
262 gST
->ConsoleInHandle
,
263 &gEfiSimpleTextInputExProtocolGuid
,
267 EFI_OPEN_PROTOCOL_GET_PROTOCOL
);
268 if (EFI_ERROR(Status
)) {
273 STRING_TOKEN (STR_SHELL_NO_IN_EX
),
274 ShellInfoObject
.HiiHandle
);
275 return (EFI_SUCCESS
);
278 KeyData
.KeyState
.KeyToggleState
= 0;
279 KeyData
.Key
.ScanCode
= 0;
280 KeyData
.KeyState
.KeyShiftState
= EFI_SHIFT_STATE_VALID
|EFI_LEFT_CONTROL_PRESSED
;
281 KeyData
.Key
.UnicodeChar
= L
's';
283 Status
= SimpleEx
->RegisterKeyNotify(
286 NotificationFunction
,
287 &ShellInfoObject
.CtrlSNotifyHandle1
);
289 KeyData
.KeyState
.KeyShiftState
= EFI_SHIFT_STATE_VALID
|EFI_RIGHT_CONTROL_PRESSED
;
290 if (!EFI_ERROR(Status
)) {
291 Status
= SimpleEx
->RegisterKeyNotify(
294 NotificationFunction
,
295 &ShellInfoObject
.CtrlSNotifyHandle2
);
297 KeyData
.KeyState
.KeyShiftState
= EFI_SHIFT_STATE_VALID
|EFI_LEFT_CONTROL_PRESSED
;
298 KeyData
.Key
.UnicodeChar
= 19;
300 if (!EFI_ERROR(Status
)) {
301 Status
= SimpleEx
->RegisterKeyNotify(
304 NotificationFunction
,
305 &ShellInfoObject
.CtrlSNotifyHandle3
);
307 KeyData
.KeyState
.KeyShiftState
= EFI_SHIFT_STATE_VALID
|EFI_RIGHT_CONTROL_PRESSED
;
308 if (!EFI_ERROR(Status
)) {
309 Status
= SimpleEx
->RegisterKeyNotify(
312 NotificationFunction
,
313 &ShellInfoObject
.CtrlSNotifyHandle4
);
321 The entry point for the application.
323 @param[in] ImageHandle The firmware allocated handle for the EFI image.
324 @param[in] SystemTable A pointer to the EFI System Table.
326 @retval EFI_SUCCESS The entry point is executed successfully.
327 @retval other Some error occurs when executing this entry point.
333 IN EFI_HANDLE ImageHandle
,
334 IN EFI_SYSTEM_TABLE
*SystemTable
340 EFI_HANDLE ConInHandle
;
341 EFI_SIMPLE_TEXT_INPUT_PROTOCOL
*OldConIn
;
344 if (PcdGet8(PcdShellSupportLevel
) > 3) {
345 return (EFI_UNSUPPORTED
);
351 Status
= gST
->ConOut
->ClearScreen(gST
->ConOut
);
352 if (EFI_ERROR(Status
)) {
357 // Populate the global structure from PCDs
359 ShellInfoObject
.ImageDevPath
= NULL
;
360 ShellInfoObject
.FileDevPath
= NULL
;
361 ShellInfoObject
.PageBreakEnabled
= PcdGetBool(PcdShellPageBreakDefault
);
362 ShellInfoObject
.ViewingSettings
.InsertMode
= PcdGetBool(PcdShellInsertModeDefault
);
363 ShellInfoObject
.LogScreenCount
= PcdGet8 (PcdShellScreenLogCount
);
366 // verify we dont allow for spec violation
368 ASSERT(ShellInfoObject
.LogScreenCount
>= 3);
371 // Initialize the LIST ENTRY objects...
373 InitializeListHead(&ShellInfoObject
.BufferToFreeList
.Link
);
374 InitializeListHead(&ShellInfoObject
.ViewingSettings
.CommandHistory
.Link
);
375 InitializeListHead(&ShellInfoObject
.SplitList
.Link
);
378 // Check PCDs for optional features that are not implemented yet.
380 if ( PcdGetBool(PcdShellSupportOldProtocols
)
381 || !FeaturePcdGet(PcdShellRequireHiiPlatform
)
382 || FeaturePcdGet(PcdShellSupportFrameworkHii
)
384 return (EFI_UNSUPPORTED
);
388 // turn off the watchdog timer
390 gBS
->SetWatchdogTimer (0, 0, 0, NULL
);
393 // install our console logger. This will keep a log of the output for back-browsing
395 Status
= ConsoleLoggerInstall(ShellInfoObject
.LogScreenCount
, &ShellInfoObject
.ConsoleInfo
);
396 if (!EFI_ERROR(Status
)) {
398 // Enable the cursor to be visible
400 gST
->ConOut
->EnableCursor (gST
->ConOut
, TRUE
);
403 // If supporting EFI 1.1 we need to install HII protocol
404 // only do this if PcdShellRequireHiiPlatform == FALSE
406 // remove EFI_UNSUPPORTED check above when complete.
407 ///@todo add support for Framework HII
410 // install our (solitary) HII package
412 ShellInfoObject
.HiiHandle
= HiiAddPackages (&gEfiCallerIdGuid
, gImageHandle
, ShellStrings
, NULL
);
413 if (ShellInfoObject
.HiiHandle
== NULL
) {
414 if (PcdGetBool(PcdShellSupportFrameworkHii
)) {
415 ///@todo Add our package into Framework HII
417 if (ShellInfoObject
.HiiHandle
== NULL
) {
418 Status
= EFI_NOT_STARTED
;
424 // create and install the EfiShellParametersProtocol
426 Status
= CreatePopulateInstallShellParametersProtocol(&ShellInfoObject
.NewShellParametersProtocol
, &ShellInfoObject
.RootShellInstance
);
427 ASSERT_EFI_ERROR(Status
);
428 ASSERT(ShellInfoObject
.NewShellParametersProtocol
!= NULL
);
431 // create and install the EfiShellProtocol
433 Status
= CreatePopulateInstallShellProtocol(&ShellInfoObject
.NewEfiShellProtocol
);
434 ASSERT_EFI_ERROR(Status
);
435 ASSERT(ShellInfoObject
.NewEfiShellProtocol
!= NULL
);
438 // Now initialize the shell library (it requires Shell Parameters protocol)
440 Status
= ShellInitialize();
441 ASSERT_EFI_ERROR(Status
);
443 Status
= CommandInit();
444 ASSERT_EFI_ERROR(Status
);
446 Status
= ShellInitEnvVarList ();
449 // Check the command line
451 Status
= ProcessCommandLine ();
452 if (EFI_ERROR (Status
)) {
457 // If shell support level is >= 1 create the mappings and paths
459 if (PcdGet8(PcdShellSupportLevel
) >= 1) {
460 Status
= ShellCommandCreateInitialMappingsAndPaths();
464 // Set the environment variable for nesting support
468 if (!ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoNest
) {
470 // No change. require nesting in Shell Protocol Execute()
472 StrnCatGrow(&TempString
,
477 StrnCatGrow(&TempString
,
482 Status
= InternalEfiShellSetEnv(mNoNestingEnvVarName
, TempString
, TRUE
);
483 SHELL_FREE_NON_NULL(TempString
);
487 // save the device path for the loaded image and the device path for the filepath (under loaded image)
488 // These are where to look for the startup.nsh file
490 Status
= GetDevicePathsForImageAndFile(&ShellInfoObject
.ImageDevPath
, &ShellInfoObject
.FileDevPath
);
491 ASSERT_EFI_ERROR(Status
);
494 // Display the version
496 if (!ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoVersion
) {
499 gST
->ConOut
->Mode
->CursorRow
,
501 STRING_TOKEN (STR_VER_OUTPUT_MAIN_SHELL
),
502 ShellInfoObject
.HiiHandle
,
503 SupportLevel
[PcdGet8(PcdShellSupportLevel
)],
504 gEfiShellProtocol
->MajorVersion
,
505 gEfiShellProtocol
->MinorVersion
512 STRING_TOKEN (STR_VER_OUTPUT_MAIN_SUPPLIER
),
513 ShellInfoObject
.HiiHandle
,
514 (CHAR16
*) PcdGetPtr (PcdShellSupplier
)
521 STRING_TOKEN (STR_VER_OUTPUT_MAIN_UEFI
),
522 ShellInfoObject
.HiiHandle
,
523 (gST
->Hdr
.Revision
&0xffff0000)>>16,
524 (gST
->Hdr
.Revision
&0x0000ffff),
526 gST
->FirmwareRevision
531 // Display the mapping
533 if (PcdGet8(PcdShellSupportLevel
) >= 2 && !ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoMap
) {
534 Status
= RunCommand(L
"map");
535 ASSERT_EFI_ERROR(Status
);
539 // init all the built in alias'
541 Status
= SetBuiltInAlias();
542 ASSERT_EFI_ERROR(Status
);
545 // Initialize environment variables
547 if (ShellCommandGetProfileList() != NULL
) {
548 Status
= InternalEfiShellSetEnv(L
"profiles", ShellCommandGetProfileList(), TRUE
);
549 ASSERT_EFI_ERROR(Status
);
553 TempString
= AllocateZeroPool(Size
);
555 UnicodeSPrint(TempString
, Size
, L
"%d", PcdGet8(PcdShellSupportLevel
));
556 Status
= InternalEfiShellSetEnv(L
"uefishellsupport", TempString
, TRUE
);
557 ASSERT_EFI_ERROR(Status
);
559 UnicodeSPrint(TempString
, Size
, L
"%d.%d", ShellInfoObject
.NewEfiShellProtocol
->MajorVersion
, ShellInfoObject
.NewEfiShellProtocol
->MinorVersion
);
560 Status
= InternalEfiShellSetEnv(L
"uefishellversion", TempString
, TRUE
);
561 ASSERT_EFI_ERROR(Status
);
563 UnicodeSPrint(TempString
, Size
, L
"%d.%d", (gST
->Hdr
.Revision
& 0xFFFF0000) >> 16, gST
->Hdr
.Revision
& 0x0000FFFF);
564 Status
= InternalEfiShellSetEnv(L
"uefiversion", TempString
, TRUE
);
565 ASSERT_EFI_ERROR(Status
);
567 FreePool(TempString
);
569 if (!EFI_ERROR(Status
)) {
570 if (!ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoInterrupt
) {
572 // Set up the event for CTRL-C monitoring...
574 Status
= InernalEfiShellStartMonitor();
577 if (!EFI_ERROR(Status
) && !ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoConsoleIn
) {
579 // Set up the event for CTRL-S monitoring...
581 Status
= InternalEfiShellStartCtrlSMonitor();
584 if (!EFI_ERROR(Status
) && ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoConsoleIn
) {
586 // close off the gST->ConIn
588 OldConIn
= gST
->ConIn
;
589 ConInHandle
= gST
->ConsoleInHandle
;
590 gST
->ConIn
= CreateSimpleTextInOnFile((SHELL_FILE_HANDLE
)&FileInterfaceNulFile
, &gST
->ConsoleInHandle
);
596 if (!EFI_ERROR(Status
) && PcdGet8(PcdShellSupportLevel
) >= 1) {
598 // process the startup script or launch the called app.
600 Status
= DoStartupScript(ShellInfoObject
.ImageDevPath
, ShellInfoObject
.FileDevPath
);
603 if (!ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.Exit
&& !ShellCommandGetExit() && (PcdGet8(PcdShellSupportLevel
) >= 3 || PcdGetBool(PcdShellForceConsole
)) && !EFI_ERROR(Status
) && !ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoConsoleIn
) {
605 // begin the UI waiting loop
609 // clean out all the memory allocated for CONST <something> * return values
610 // between each shell prompt presentation
612 if (!IsListEmpty(&ShellInfoObject
.BufferToFreeList
.Link
)){
613 FreeBufferList(&ShellInfoObject
.BufferToFreeList
);
617 // Reset page break back to default.
619 ShellInfoObject
.PageBreakEnabled
= PcdGetBool(PcdShellPageBreakDefault
);
620 ASSERT (ShellInfoObject
.ConsoleInfo
!= NULL
);
621 ShellInfoObject
.ConsoleInfo
->Enabled
= TRUE
;
622 ShellInfoObject
.ConsoleInfo
->RowCounter
= 0;
627 Status
= DoShellPrompt();
628 } while (!ShellCommandGetExit());
630 if (OldConIn
!= NULL
&& ConInHandle
!= NULL
) {
631 CloseSimpleTextInOnFile (gST
->ConIn
);
632 gST
->ConIn
= OldConIn
;
633 gST
->ConsoleInHandle
= ConInHandle
;
640 // uninstall protocols / free memory / etc...
642 if (ShellInfoObject
.UserBreakTimer
!= NULL
) {
643 gBS
->CloseEvent(ShellInfoObject
.UserBreakTimer
);
644 DEBUG_CODE(ShellInfoObject
.UserBreakTimer
= NULL
;);
646 if (ShellInfoObject
.ImageDevPath
!= NULL
) {
647 FreePool(ShellInfoObject
.ImageDevPath
);
648 DEBUG_CODE(ShellInfoObject
.ImageDevPath
= NULL
;);
650 if (ShellInfoObject
.FileDevPath
!= NULL
) {
651 FreePool(ShellInfoObject
.FileDevPath
);
652 DEBUG_CODE(ShellInfoObject
.FileDevPath
= NULL
;);
654 if (ShellInfoObject
.NewShellParametersProtocol
!= NULL
) {
655 CleanUpShellParametersProtocol(ShellInfoObject
.NewShellParametersProtocol
);
656 DEBUG_CODE(ShellInfoObject
.NewShellParametersProtocol
= NULL
;);
658 if (ShellInfoObject
.NewEfiShellProtocol
!= NULL
){
659 if (ShellInfoObject
.NewEfiShellProtocol
->IsRootShell()){
660 InternalEfiShellSetEnv(L
"cwd", NULL
, TRUE
);
662 CleanUpShellEnvironment (ShellInfoObject
.NewEfiShellProtocol
);
663 DEBUG_CODE(ShellInfoObject
.NewEfiShellProtocol
= NULL
;);
666 if (!IsListEmpty(&ShellInfoObject
.BufferToFreeList
.Link
)){
667 FreeBufferList(&ShellInfoObject
.BufferToFreeList
);
670 if (!IsListEmpty(&ShellInfoObject
.SplitList
.Link
)){
671 ASSERT(FALSE
); ///@todo finish this de-allocation (free SplitStdIn/Out when needed).
673 for ( Split
= (SPLIT_LIST
*)GetFirstNode (&ShellInfoObject
.SplitList
.Link
)
674 ; !IsNull (&ShellInfoObject
.SplitList
.Link
, &Split
->Link
)
675 ; Split
= (SPLIT_LIST
*)GetNextNode (&ShellInfoObject
.SplitList
.Link
, &Split
->Link
)
677 RemoveEntryList (&Split
->Link
);
681 DEBUG_CODE (InitializeListHead (&ShellInfoObject
.SplitList
.Link
););
684 if (ShellInfoObject
.ShellInitSettings
.FileName
!= NULL
) {
685 FreePool(ShellInfoObject
.ShellInitSettings
.FileName
);
686 DEBUG_CODE(ShellInfoObject
.ShellInitSettings
.FileName
= NULL
;);
689 if (ShellInfoObject
.ShellInitSettings
.FileOptions
!= NULL
) {
690 FreePool(ShellInfoObject
.ShellInitSettings
.FileOptions
);
691 DEBUG_CODE(ShellInfoObject
.ShellInitSettings
.FileOptions
= NULL
;);
694 if (ShellInfoObject
.HiiHandle
!= NULL
) {
695 HiiRemovePackages(ShellInfoObject
.HiiHandle
);
696 DEBUG_CODE(ShellInfoObject
.HiiHandle
= NULL
;);
699 if (!IsListEmpty(&ShellInfoObject
.ViewingSettings
.CommandHistory
.Link
)){
700 FreeBufferList(&ShellInfoObject
.ViewingSettings
.CommandHistory
);
703 ASSERT(ShellInfoObject
.ConsoleInfo
!= NULL
);
704 if (ShellInfoObject
.ConsoleInfo
!= NULL
) {
705 ConsoleLoggerUninstall(ShellInfoObject
.ConsoleInfo
);
706 FreePool(ShellInfoObject
.ConsoleInfo
);
707 DEBUG_CODE(ShellInfoObject
.ConsoleInfo
= NULL
;);
710 ShellFreeEnvVarList ();
712 if (ShellCommandGetExit()) {
713 return ((EFI_STATUS
)ShellCommandGetExitCode());
719 Sets all the alias' that were registered with the ShellCommandLib library.
721 @retval EFI_SUCCESS all init commands were run successfully.
729 CONST ALIAS_LIST
*List
;
733 // Get all the commands we want to run
735 List
= ShellCommandGetInitAliasList();
738 // for each command in the List
740 for ( Node
= (ALIAS_LIST
*)GetFirstNode(&List
->Link
)
741 ; !IsNull (&List
->Link
, &Node
->Link
)
742 ; Node
= (ALIAS_LIST
*)GetNextNode(&List
->Link
, &Node
->Link
)
745 // install the alias'
747 Status
= InternalSetAlias(Node
->CommandString
, Node
->Alias
, TRUE
);
748 ASSERT_EFI_ERROR(Status
);
750 return (EFI_SUCCESS
);
754 Internal function to determine if 2 command names are really the same.
756 @param[in] Command1 The pointer to the first command name.
757 @param[in] Command2 The pointer to the second command name.
759 @retval TRUE The 2 command names are the same.
760 @retval FALSE The 2 command names are not the same.
764 IN CONST CHAR16
*Command1
,
765 IN CONST CHAR16
*Command2
768 if (StringNoCaseCompare(&Command1
, &Command2
) == 0) {
775 Internal function to determine if a command is a script only command.
777 @param[in] CommandName The pointer to the command name.
779 @retval TRUE The command is a script only command.
780 @retval FALSE The command is not a script only command.
784 IN CONST CHAR16
*CommandName
787 if (IsCommand(CommandName
, L
"for")
788 ||IsCommand(CommandName
, L
"endfor")
789 ||IsCommand(CommandName
, L
"if")
790 ||IsCommand(CommandName
, L
"else")
791 ||IsCommand(CommandName
, L
"endif")
792 ||IsCommand(CommandName
, L
"goto")) {
799 This function will populate the 2 device path protocol parameters based on the
800 global gImageHandle. The DevPath will point to the device path for the handle that has
801 loaded image protocol installed on it. The FilePath will point to the device path
802 for the file that was loaded.
804 @param[in, out] DevPath On a successful return the device path to the loaded image.
805 @param[in, out] FilePath On a successful return the device path to the file.
807 @retval EFI_SUCCESS The 2 device paths were successfully returned.
808 @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.
895 CHAR16
*DelayValueStr
;
898 EFI_UNICODE_COLLATION_PROTOCOL
*UnicodeCollation
;
900 // `file-name-options` will contain arguments to `file-name` that we don't
901 // know about. This would cause ShellCommandLineParse to error, so we parse
902 // arguments manually, ignoring those after the first thing that doesn't look
903 // like a shell option (which is assumed to be `file-name`).
905 Status
= gBS
->LocateProtocol (
906 &gEfiUnicodeCollation2ProtocolGuid
,
908 (VOID
**) &UnicodeCollation
910 if (EFI_ERROR (Status
)) {
911 Status
= gBS
->LocateProtocol (
912 &gEfiUnicodeCollationProtocolGuid
,
914 (VOID
**) &UnicodeCollation
916 if (EFI_ERROR (Status
)) {
921 // Set default options
922 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.Startup
= FALSE
;
923 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoStartup
= FALSE
;
924 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoConsoleOut
= FALSE
;
925 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoConsoleIn
= FALSE
;
926 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoInterrupt
= FALSE
;
927 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoMap
= FALSE
;
928 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoVersion
= FALSE
;
929 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.Delay
= FALSE
;
930 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.Exit
= FALSE
;
931 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoNest
= FALSE
;
932 ShellInfoObject
.ShellInitSettings
.Delay
= 5;
935 // Start LoopVar at 0 to parse only optional arguments at Argv[0]
936 // and parse other parameters from Argv[1]. This is for use case that
937 // UEFI Shell boot option is created, and OptionalData is provided
938 // that starts with shell command-line options.
940 for (LoopVar
= 0 ; LoopVar
< gEfiShellParametersProtocol
->Argc
; LoopVar
++) {
941 CurrentArg
= gEfiShellParametersProtocol
->Argv
[LoopVar
];
942 if (UnicodeCollation
->StriColl (
947 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.Startup
= TRUE
;
949 else if (UnicodeCollation
->StriColl (
954 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoStartup
= TRUE
;
956 else if (UnicodeCollation
->StriColl (
961 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoConsoleOut
= TRUE
;
963 else if (UnicodeCollation
->StriColl (
968 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoConsoleIn
= TRUE
;
970 else if (UnicodeCollation
->StriColl (
975 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoInterrupt
= TRUE
;
977 else if (UnicodeCollation
->StriColl (
982 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoMap
= TRUE
;
984 else if (UnicodeCollation
->StriColl (
989 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoVersion
= TRUE
;
991 else if (UnicodeCollation
->StriColl (
996 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoNest
= TRUE
;
998 else if (UnicodeCollation
->StriColl (
1003 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.Delay
= TRUE
;
1004 // Check for optional delay value following "-delay"
1005 DelayValueStr
= gEfiShellParametersProtocol
->Argv
[LoopVar
+ 1];
1006 if (DelayValueStr
!= NULL
){
1007 if (*DelayValueStr
== L
':') {
1010 if (!EFI_ERROR(ShellConvertStringToUint64 (
1016 ShellInfoObject
.ShellInitSettings
.Delay
= (UINTN
)DelayValue
;
1020 } else if (UnicodeCollation
->StriColl (
1025 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.Exit
= TRUE
;
1026 } else if (StrnCmp (L
"-", CurrentArg
, 1) == 0) {
1027 // Unrecognized option
1028 ShellPrintHiiEx(-1, -1, NULL
,
1029 STRING_TOKEN (STR_GEN_PROBLEM
),
1030 ShellInfoObject
.HiiHandle
,
1033 return EFI_INVALID_PARAMETER
;
1036 // First argument should be Shell.efi image name
1042 ShellInfoObject
.ShellInitSettings
.FileName
= NULL
;
1045 // If first argument contains a space, then add double quotes before the argument
1047 if (StrStr (CurrentArg
, L
" ") != NULL
) {
1048 StrnCatGrow(&ShellInfoObject
.ShellInitSettings
.FileName
, &Size
, L
"\"", 0);
1049 if (ShellInfoObject
.ShellInitSettings
.FileName
== NULL
) {
1050 return (EFI_OUT_OF_RESOURCES
);
1053 StrnCatGrow(&ShellInfoObject
.ShellInitSettings
.FileName
, &Size
, CurrentArg
, 0);
1054 if (ShellInfoObject
.ShellInitSettings
.FileName
== NULL
) {
1055 return (EFI_OUT_OF_RESOURCES
);
1058 // If first argument contains a space, then add double quotes after the argument
1060 if (StrStr (CurrentArg
, L
" ") != NULL
) {
1061 StrnCatGrow(&ShellInfoObject
.ShellInitSettings
.FileName
, &Size
, L
"\"", 0);
1062 if (ShellInfoObject
.ShellInitSettings
.FileName
== NULL
) {
1063 return (EFI_OUT_OF_RESOURCES
);
1067 // We found `file-name`.
1069 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoStartup
= 1;
1072 // Add `file-name-options`
1073 for (Size
= 0 ; LoopVar
< gEfiShellParametersProtocol
->Argc
; LoopVar
++) {
1074 ASSERT((ShellInfoObject
.ShellInitSettings
.FileOptions
== NULL
&& Size
== 0) || (ShellInfoObject
.ShellInitSettings
.FileOptions
!= NULL
));
1076 // Add a space between arguments
1078 if (ShellInfoObject
.ShellInitSettings
.FileOptions
!= NULL
) {
1079 StrnCatGrow(&ShellInfoObject
.ShellInitSettings
.FileOptions
, &Size
, L
" ", 0);
1080 if (ShellInfoObject
.ShellInitSettings
.FileOptions
== NULL
) {
1081 SHELL_FREE_NON_NULL(ShellInfoObject
.ShellInitSettings
.FileName
);
1082 return (EFI_OUT_OF_RESOURCES
);
1086 // If an argumnent contains a space, then add double quotes before the argument
1088 if (StrStr (gEfiShellParametersProtocol
->Argv
[LoopVar
], L
" ") != NULL
) {
1089 StrnCatGrow(&ShellInfoObject
.ShellInitSettings
.FileOptions
,
1093 if (ShellInfoObject
.ShellInitSettings
.FileOptions
== NULL
) {
1094 SHELL_FREE_NON_NULL(ShellInfoObject
.ShellInitSettings
.FileName
);
1095 return (EFI_OUT_OF_RESOURCES
);
1098 StrnCatGrow(&ShellInfoObject
.ShellInitSettings
.FileOptions
,
1100 gEfiShellParametersProtocol
->Argv
[LoopVar
],
1102 if (ShellInfoObject
.ShellInitSettings
.FileOptions
== NULL
) {
1103 SHELL_FREE_NON_NULL(ShellInfoObject
.ShellInitSettings
.FileName
);
1104 return (EFI_OUT_OF_RESOURCES
);
1107 // If an argumnent contains a space, then add double quotes after the argument
1109 if (StrStr (gEfiShellParametersProtocol
->Argv
[LoopVar
], L
" ") != NULL
) {
1110 StrnCatGrow(&ShellInfoObject
.ShellInitSettings
.FileOptions
,
1114 if (ShellInfoObject
.ShellInitSettings
.FileOptions
== NULL
) {
1115 SHELL_FREE_NON_NULL(ShellInfoObject
.ShellInitSettings
.FileName
);
1116 return (EFI_OUT_OF_RESOURCES
);
1123 // "-nointerrupt" overrides "-delay"
1124 if (ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoInterrupt
) {
1125 ShellInfoObject
.ShellInitSettings
.Delay
= 0;
1132 Function try to find location of the Startup.nsh file.
1134 The buffer is callee allocated and should be freed by the caller.
1136 @param ImageDevicePath The path to the image for shell. first place to look for the startup script
1137 @param FileDevicePath The path to the file for shell. second place to look for the startup script.
1139 @retval NULL No Startup.nsh file was found.
1140 @return !=NULL Pointer to NULL-terminated path.
1143 LocateStartupScript (
1144 IN EFI_DEVICE_PATH_PROTOCOL
*ImageDevicePath
,
1145 IN EFI_DEVICE_PATH_PROTOCOL
*FileDevicePath
1148 CHAR16
*StartupScriptPath
;
1150 CONST CHAR16
*MapName
;
1153 StartupScriptPath
= NULL
;
1157 // Try to find 'Startup.nsh' in the directory where the shell itself was launched.
1159 MapName
= ShellInfoObject
.NewEfiShellProtocol
->GetMapFromDevicePath (&ImageDevicePath
);
1160 if (MapName
!= NULL
) {
1161 StartupScriptPath
= StrnCatGrow (&StartupScriptPath
, &Size
, MapName
, 0);
1162 if (StartupScriptPath
== NULL
) {
1164 // Do not locate the startup script in sys path when out of resource.
1168 TempSpot
= StrStr (StartupScriptPath
, L
";");
1169 if (TempSpot
!= NULL
) {
1170 *TempSpot
= CHAR_NULL
;
1173 InternalEfiShellSetEnv(L
"homefilesystem", StartupScriptPath
, TRUE
);
1175 StartupScriptPath
= StrnCatGrow (&StartupScriptPath
, &Size
, ((FILEPATH_DEVICE_PATH
*)FileDevicePath
)->PathName
, 0);
1176 PathRemoveLastItem (StartupScriptPath
);
1177 StartupScriptPath
= StrnCatGrow (&StartupScriptPath
, &Size
, mStartupScript
, 0);
1181 // Try to find 'Startup.nsh' in the execution path defined by the envrionment variable PATH.
1183 if ((StartupScriptPath
== NULL
) || EFI_ERROR (ShellIsFile (StartupScriptPath
))) {
1184 SHELL_FREE_NON_NULL (StartupScriptPath
);
1185 StartupScriptPath
= ShellFindFilePath (mStartupScript
);
1188 return StartupScriptPath
;
1192 Handles all interaction with the default startup script.
1194 this will check that the correct command line parameters were passed, handle the delay, and then start running the script.
1196 @param ImagePath the path to the image for shell. first place to look for the startup script
1197 @param FilePath the path to the file for shell. second place to look for the startup script.
1199 @retval EFI_SUCCESS the variable is initialized.
1203 IN EFI_DEVICE_PATH_PROTOCOL
*ImagePath
,
1204 IN EFI_DEVICE_PATH_PROTOCOL
*FilePath
1208 EFI_STATUS CalleeStatus
;
1211 CHAR16
*FileStringPath
;
1212 CHAR16
*FullFileStringPath
;
1215 Key
.UnicodeChar
= CHAR_NULL
;
1218 if (!ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.Startup
&& ShellInfoObject
.ShellInitSettings
.FileName
!= NULL
) {
1220 // launch something else instead
1222 NewSize
= StrSize(ShellInfoObject
.ShellInitSettings
.FileName
);
1223 if (ShellInfoObject
.ShellInitSettings
.FileOptions
!= NULL
) {
1224 NewSize
+= StrSize(ShellInfoObject
.ShellInitSettings
.FileOptions
) + sizeof(CHAR16
);
1226 FileStringPath
= AllocateZeroPool(NewSize
);
1227 if (FileStringPath
== NULL
) {
1228 return (EFI_OUT_OF_RESOURCES
);
1230 StrCpyS(FileStringPath
, NewSize
/sizeof(CHAR16
), ShellInfoObject
.ShellInitSettings
.FileName
);
1231 if (ShellInfoObject
.ShellInitSettings
.FileOptions
!= NULL
) {
1232 StrnCatS(FileStringPath
, NewSize
/sizeof(CHAR16
), L
" ", NewSize
/sizeof(CHAR16
) - StrLen(FileStringPath
) -1);
1233 StrnCatS(FileStringPath
, NewSize
/sizeof(CHAR16
), ShellInfoObject
.ShellInitSettings
.FileOptions
, NewSize
/sizeof(CHAR16
) - StrLen(FileStringPath
) -1);
1235 Status
= RunShellCommand(FileStringPath
, &CalleeStatus
);
1236 if (ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.Exit
== TRUE
) {
1237 ShellCommandRegisterExit(gEfiShellProtocol
->BatchIsActive(), (UINT64
)CalleeStatus
);
1239 FreePool(FileStringPath
);
1245 // for shell level 0 we do no scripts
1246 // Without the Startup bit overriding we allow for nostartup to prevent scripts
1248 if ( (PcdGet8(PcdShellSupportLevel
) < 1)
1249 || (ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoStartup
&& !ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.Startup
)
1251 return (EFI_SUCCESS
);
1254 gST
->ConOut
->EnableCursor(gST
->ConOut
, FALSE
);
1256 // print out our warning and see if they press a key
1258 for ( Status
= EFI_UNSUPPORTED
, Delay
= ShellInfoObject
.ShellInitSettings
.Delay
1259 ; Delay
!= 0 && EFI_ERROR(Status
)
1262 ShellPrintHiiEx(0, gST
->ConOut
->Mode
->CursorRow
, NULL
, STRING_TOKEN (STR_SHELL_STARTUP_QUESTION
), ShellInfoObject
.HiiHandle
, Delay
);
1263 gBS
->Stall (1000000);
1264 if (!ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoConsoleIn
) {
1265 Status
= gST
->ConIn
->ReadKeyStroke (gST
->ConIn
, &Key
);
1268 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_CRLF
), ShellInfoObject
.HiiHandle
);
1269 gST
->ConOut
->EnableCursor(gST
->ConOut
, TRUE
);
1274 if (Status
== EFI_SUCCESS
&& Key
.UnicodeChar
== 0 && Key
.ScanCode
== SCAN_ESC
) {
1275 return (EFI_SUCCESS
);
1278 FileStringPath
= LocateStartupScript (ImagePath
, FilePath
);
1279 if (FileStringPath
!= NULL
) {
1280 FullFileStringPath
= FullyQualifyPath(FileStringPath
);
1281 if (FullFileStringPath
== NULL
) {
1282 Status
= RunScriptFile (FileStringPath
, NULL
, FileStringPath
, ShellInfoObject
.NewShellParametersProtocol
);
1284 Status
= RunScriptFile (FullFileStringPath
, NULL
, FullFileStringPath
, ShellInfoObject
.NewShellParametersProtocol
);
1285 FreePool(FullFileStringPath
);
1287 FreePool (FileStringPath
);
1290 // we return success since startup script is not mandatory.
1292 Status
= EFI_SUCCESS
;
1299 Function to perform the shell prompt looping. It will do a single prompt,
1300 dispatch the result, and then return. It is expected that the caller will
1301 call this function in a loop many times.
1304 @retval RETURN_ABORTED
1314 CONST CHAR16
*CurDir
;
1317 LIST_ENTRY OldBufferList
;
1322 // Get screen setting to decide size of the command line buffer
1324 gST
->ConOut
->QueryMode (gST
->ConOut
, gST
->ConOut
->Mode
->Mode
, &Column
, &Row
);
1325 BufferSize
= Column
* Row
* sizeof (CHAR16
);
1326 CmdLine
= AllocateZeroPool (BufferSize
);
1327 if (CmdLine
== NULL
) {
1328 return EFI_OUT_OF_RESOURCES
;
1331 SaveBufferList(&OldBufferList
);
1332 CurDir
= ShellInfoObject
.NewEfiShellProtocol
->GetEnv(L
"cwd");
1337 gST
->ConOut
->SetCursorPosition (gST
->ConOut
, 0, gST
->ConOut
->Mode
->CursorRow
);
1339 if (CurDir
!= NULL
&& StrLen(CurDir
) > 1) {
1340 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_CURDIR
), ShellInfoObject
.HiiHandle
, CurDir
);
1342 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_SHELL
), ShellInfoObject
.HiiHandle
);
1346 // Read a line from the console
1348 Status
= ShellInfoObject
.NewEfiShellProtocol
->ReadFile(ShellInfoObject
.NewShellParametersProtocol
->StdIn
, &BufferSize
, CmdLine
);
1351 // Null terminate the string and parse it
1353 if (!EFI_ERROR (Status
)) {
1355 // Reset the CTRL-C event just before running the command (yes we ignore the return values)
1357 Status
= gBS
->CheckEvent (ShellInfoObject
.NewEfiShellProtocol
->ExecutionBreak
);
1359 CmdLine
[BufferSize
/ sizeof (CHAR16
)] = CHAR_NULL
;
1360 Status
= RunCommand(CmdLine
);
1364 // Done with this command
1366 RestoreBufferList(&OldBufferList
);
1372 Add a buffer to the Buffer To Free List for safely returning buffers to other
1373 places without risking letting them modify internal shell information.
1375 @param Buffer Something to pass to FreePool when the shell is exiting.
1378 AddBufferToFreeList (
1382 BUFFER_LIST
*BufferListEntry
;
1384 if (Buffer
== NULL
) {
1388 BufferListEntry
= AllocateZeroPool (sizeof (BUFFER_LIST
));
1389 if (BufferListEntry
== NULL
) {
1393 BufferListEntry
->Buffer
= Buffer
;
1394 InsertTailList (&ShellInfoObject
.BufferToFreeList
.Link
, &BufferListEntry
->Link
);
1400 Create a new buffer list and stores the old one to OldBufferList
1402 @param OldBufferList The temporary list head used to store the nodes in BufferToFreeList.
1406 OUT LIST_ENTRY
*OldBufferList
1409 CopyMem (OldBufferList
, &ShellInfoObject
.BufferToFreeList
.Link
, sizeof (LIST_ENTRY
));
1410 InitializeListHead (&ShellInfoObject
.BufferToFreeList
.Link
);
1414 Restore previous nodes into BufferToFreeList .
1416 @param OldBufferList The temporary list head used to store the nodes in BufferToFreeList.
1420 IN OUT LIST_ENTRY
*OldBufferList
1423 FreeBufferList (&ShellInfoObject
.BufferToFreeList
);
1424 CopyMem (&ShellInfoObject
.BufferToFreeList
.Link
, OldBufferList
, sizeof (LIST_ENTRY
));
1429 Add a buffer to the Line History List
1431 @param Buffer The line buffer to add.
1434 AddLineToCommandHistory(
1435 IN CONST CHAR16
*Buffer
1439 BUFFER_LIST
*Walker
;
1440 UINT16 MaxHistoryCmdCount
;
1444 MaxHistoryCmdCount
= PcdGet16(PcdShellMaxHistoryCommandCount
);
1446 if (MaxHistoryCmdCount
== 0) {
1451 Node
= AllocateZeroPool(sizeof(BUFFER_LIST
));
1456 Node
->Buffer
= AllocateCopyPool (StrSize (Buffer
), Buffer
);
1457 if (Node
->Buffer
== NULL
) {
1462 for ( Walker
= (BUFFER_LIST
*)GetFirstNode(&ShellInfoObject
.ViewingSettings
.CommandHistory
.Link
)
1463 ; !IsNull(&ShellInfoObject
.ViewingSettings
.CommandHistory
.Link
, &Walker
->Link
)
1464 ; Walker
= (BUFFER_LIST
*)GetNextNode(&ShellInfoObject
.ViewingSettings
.CommandHistory
.Link
, &Walker
->Link
)
1468 if (Count
< MaxHistoryCmdCount
){
1469 InsertTailList(&ShellInfoObject
.ViewingSettings
.CommandHistory
.Link
, &Node
->Link
);
1471 Walker
= (BUFFER_LIST
*)GetFirstNode(&ShellInfoObject
.ViewingSettings
.CommandHistory
.Link
);
1472 RemoveEntryList(&Walker
->Link
);
1473 if (Walker
->Buffer
!= NULL
) {
1474 FreePool(Walker
->Buffer
);
1477 InsertTailList(&ShellInfoObject
.ViewingSettings
.CommandHistory
.Link
, &Node
->Link
);
1482 Checks if a string is an alias for another command. If yes, then it replaces the alias name
1483 with the correct command name.
1485 @param[in, out] CommandString Upon entry the potential alias. Upon return the
1486 command name if it was an alias. If it was not
1487 an alias it will be unchanged. This function may
1488 change the buffer to fit the command name.
1490 @retval EFI_SUCCESS The name was changed.
1491 @retval EFI_SUCCESS The name was not an alias.
1492 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
1496 IN OUT CHAR16
**CommandString
1499 CONST CHAR16
*NewString
;
1501 NewString
= ShellInfoObject
.NewEfiShellProtocol
->GetAlias(*CommandString
, NULL
);
1502 if (NewString
== NULL
) {
1503 return (EFI_SUCCESS
);
1505 FreePool(*CommandString
);
1506 *CommandString
= AllocateCopyPool(StrSize(NewString
), NewString
);
1507 if (*CommandString
== NULL
) {
1508 return (EFI_OUT_OF_RESOURCES
);
1510 return (EFI_SUCCESS
);
1514 This function will eliminate unreplaced (and therefore non-found) environment variables.
1516 @param[in,out] CmdLine The command line to update.
1519 StripUnreplacedEnvironmentVariables(
1520 IN OUT CHAR16
*CmdLine
1523 CHAR16
*FirstPercent
;
1525 CHAR16
*SecondPercent
;
1526 CHAR16
*SecondQuote
;
1527 CHAR16
*CurrentLocator
;
1529 for (CurrentLocator
= CmdLine
; CurrentLocator
!= NULL
; ) {
1530 FirstQuote
= FindNextInstance(CurrentLocator
, L
"\"", TRUE
);
1531 FirstPercent
= FindNextInstance(CurrentLocator
, L
"%", TRUE
);
1532 SecondPercent
= FirstPercent
!=NULL
?FindNextInstance(FirstPercent
+1, L
"%", TRUE
):NULL
;
1533 if (FirstPercent
== NULL
|| SecondPercent
== NULL
) {
1535 // If we ever don't have 2 % we are done.
1540 if (FirstQuote
!= NULL
&& FirstQuote
< FirstPercent
) {
1541 SecondQuote
= FindNextInstance(FirstQuote
+1, L
"\"", TRUE
);
1543 // Quote is first found
1546 if (SecondQuote
< FirstPercent
) {
1548 // restart after the pair of "
1550 CurrentLocator
= SecondQuote
+ 1;
1551 } else /* FirstPercent < SecondQuote */{
1553 // Restart on the first percent
1555 CurrentLocator
= FirstPercent
;
1560 if (FirstQuote
== NULL
|| SecondPercent
< FirstQuote
) {
1561 if (IsValidEnvironmentVariableName(FirstPercent
, SecondPercent
)) {
1563 // We need to remove from FirstPercent to SecondPercent
1565 CopyMem(FirstPercent
, SecondPercent
+ 1, StrSize(SecondPercent
+ 1));
1567 // don't need to update the locator. both % characters are gone.
1570 CurrentLocator
= SecondPercent
+ 1;
1574 CurrentLocator
= FirstQuote
;
1576 return (EFI_SUCCESS
);
1580 Function allocates a new command line and replaces all instances of environment
1581 variable names that are correctly preset to their values.
1583 If the return value is not NULL the memory must be caller freed.
1585 @param[in] OriginalCommandLine The original command line
1587 @retval NULL An error occurred.
1588 @return The new command line with no environment variables present.
1591 ShellConvertVariables (
1592 IN CONST CHAR16
*OriginalCommandLine
1595 CONST CHAR16
*MasterEnvList
;
1597 CHAR16
*NewCommandLine1
;
1598 CHAR16
*NewCommandLine2
;
1602 SCRIPT_FILE
*CurrentScriptFile
;
1603 ALIAS_LIST
*AliasListNode
;
1605 ASSERT(OriginalCommandLine
!= NULL
);
1608 NewSize
= StrSize(OriginalCommandLine
);
1609 CurrentScriptFile
= ShellCommandGetCurrentScriptFile();
1612 ///@todo update this to handle the %0 - %9 for scripting only (borrow from line 1256 area) ? ? ?
1615 // calculate the size required for the post-conversion string...
1617 if (CurrentScriptFile
!= NULL
) {
1618 for (AliasListNode
= (ALIAS_LIST
*)GetFirstNode(&CurrentScriptFile
->SubstList
)
1619 ; !IsNull(&CurrentScriptFile
->SubstList
, &AliasListNode
->Link
)
1620 ; AliasListNode
= (ALIAS_LIST
*)GetNextNode(&CurrentScriptFile
->SubstList
, &AliasListNode
->Link
)
1622 for (Temp
= StrStr(OriginalCommandLine
, AliasListNode
->Alias
)
1624 ; Temp
= StrStr(Temp
+1, AliasListNode
->Alias
)
1627 // we need a preceding and if there is space no ^ preceding (if no space ignore)
1629 if ((((Temp
-OriginalCommandLine
)>2) && *(Temp
-2) != L
'^') || ((Temp
-OriginalCommandLine
)<=2)) {
1630 NewSize
+= StrSize(AliasListNode
->CommandString
);
1636 for (MasterEnvList
= EfiShellGetEnv(NULL
)
1637 ; MasterEnvList
!= NULL
&& *MasterEnvList
!= CHAR_NULL
//&& *(MasterEnvList+1) != CHAR_NULL
1638 ; MasterEnvList
+= StrLen(MasterEnvList
) + 1
1640 if (StrSize(MasterEnvList
) > ItemSize
) {
1641 ItemSize
= StrSize(MasterEnvList
);
1643 for (Temp
= StrStr(OriginalCommandLine
, MasterEnvList
)
1645 ; Temp
= StrStr(Temp
+1, MasterEnvList
)
1648 // we need a preceding and following % and if there is space no ^ preceding (if no space ignore)
1650 if (*(Temp
-1) == L
'%' && *(Temp
+StrLen(MasterEnvList
)) == L
'%' &&
1651 ((((Temp
-OriginalCommandLine
)>2) && *(Temp
-2) != L
'^') || ((Temp
-OriginalCommandLine
)<=2))) {
1652 NewSize
+=StrSize(EfiShellGetEnv(MasterEnvList
));
1658 // now do the replacements...
1660 NewCommandLine1
= AllocateZeroPool (NewSize
);
1661 NewCommandLine2
= AllocateZeroPool(NewSize
);
1662 ItemTemp
= AllocateZeroPool(ItemSize
+(2*sizeof(CHAR16
)));
1663 if (NewCommandLine1
== NULL
|| NewCommandLine2
== NULL
|| ItemTemp
== NULL
) {
1664 SHELL_FREE_NON_NULL(NewCommandLine1
);
1665 SHELL_FREE_NON_NULL(NewCommandLine2
);
1666 SHELL_FREE_NON_NULL(ItemTemp
);
1669 CopyMem (NewCommandLine1
, OriginalCommandLine
, StrSize (OriginalCommandLine
));
1671 for (MasterEnvList
= EfiShellGetEnv(NULL
)
1672 ; MasterEnvList
!= NULL
&& *MasterEnvList
!= CHAR_NULL
1673 ; MasterEnvList
+= StrLen(MasterEnvList
) + 1
1676 ((ItemSize
+(2*sizeof(CHAR16
)))/sizeof(CHAR16
)),
1680 ((ItemSize
+(2*sizeof(CHAR16
)))/sizeof(CHAR16
)),
1684 ((ItemSize
+(2*sizeof(CHAR16
)))/sizeof(CHAR16
)),
1687 ShellCopySearchAndReplace(NewCommandLine1
, NewCommandLine2
, NewSize
, ItemTemp
, EfiShellGetEnv(MasterEnvList
), TRUE
, FALSE
);
1688 StrCpyS(NewCommandLine1
, NewSize
/sizeof(CHAR16
), NewCommandLine2
);
1690 if (CurrentScriptFile
!= NULL
) {
1691 for (AliasListNode
= (ALIAS_LIST
*)GetFirstNode(&CurrentScriptFile
->SubstList
)
1692 ; !IsNull(&CurrentScriptFile
->SubstList
, &AliasListNode
->Link
)
1693 ; AliasListNode
= (ALIAS_LIST
*)GetNextNode(&CurrentScriptFile
->SubstList
, &AliasListNode
->Link
)
1695 ShellCopySearchAndReplace(NewCommandLine1
, NewCommandLine2
, NewSize
, AliasListNode
->Alias
, AliasListNode
->CommandString
, TRUE
, FALSE
);
1696 StrCpyS(NewCommandLine1
, NewSize
/sizeof(CHAR16
), NewCommandLine2
);
1701 // Remove non-existent environment variables
1703 StripUnreplacedEnvironmentVariables(NewCommandLine1
);
1706 // Now cleanup any straggler intentionally ignored "%" characters
1708 ShellCopySearchAndReplace(NewCommandLine1
, NewCommandLine2
, NewSize
, L
"^%", L
"%", TRUE
, FALSE
);
1709 StrCpyS(NewCommandLine1
, NewSize
/sizeof(CHAR16
), NewCommandLine2
);
1711 FreePool(NewCommandLine2
);
1714 return (NewCommandLine1
);
1718 Internal function to run a command line with pipe usage.
1720 @param[in] CmdLine The pointer to the command line.
1721 @param[in] StdIn The pointer to the Standard input.
1722 @param[in] StdOut The pointer to the Standard output.
1724 @retval EFI_SUCCESS The split command is executed successfully.
1725 @retval other Some error occurs when executing the split command.
1729 IN CONST CHAR16
*CmdLine
,
1730 IN SHELL_FILE_HANDLE StdIn
,
1731 IN SHELL_FILE_HANDLE StdOut
1735 CHAR16
*NextCommandLine
;
1736 CHAR16
*OurCommandLine
;
1740 SHELL_FILE_HANDLE TempFileHandle
;
1743 ASSERT(StdOut
== NULL
);
1745 ASSERT(StrStr(CmdLine
, L
"|") != NULL
);
1747 Status
= EFI_SUCCESS
;
1748 NextCommandLine
= NULL
;
1749 OurCommandLine
= NULL
;
1753 NextCommandLine
= StrnCatGrow(&NextCommandLine
, &Size1
, StrStr(CmdLine
, L
"|")+1, 0);
1754 OurCommandLine
= StrnCatGrow(&OurCommandLine
, &Size2
, CmdLine
, StrStr(CmdLine
, L
"|") - CmdLine
);
1756 if (NextCommandLine
== NULL
|| OurCommandLine
== NULL
) {
1757 SHELL_FREE_NON_NULL(OurCommandLine
);
1758 SHELL_FREE_NON_NULL(NextCommandLine
);
1759 return (EFI_OUT_OF_RESOURCES
);
1760 } else if (StrStr(OurCommandLine
, L
"|") != NULL
|| Size1
== 0 || Size2
== 0) {
1761 SHELL_FREE_NON_NULL(OurCommandLine
);
1762 SHELL_FREE_NON_NULL(NextCommandLine
);
1763 return (EFI_INVALID_PARAMETER
);
1764 } else if (NextCommandLine
[0] == L
'a' &&
1765 (NextCommandLine
[1] == L
' ' || NextCommandLine
[1] == CHAR_NULL
)
1767 CopyMem(NextCommandLine
, NextCommandLine
+1, StrSize(NextCommandLine
) - sizeof(NextCommandLine
[0]));
1768 while (NextCommandLine
[0] == L
' ') {
1769 CopyMem(NextCommandLine
, NextCommandLine
+1, StrSize(NextCommandLine
) - sizeof(NextCommandLine
[0]));
1771 if (NextCommandLine
[0] == CHAR_NULL
) {
1772 SHELL_FREE_NON_NULL(OurCommandLine
);
1773 SHELL_FREE_NON_NULL(NextCommandLine
);
1774 return (EFI_INVALID_PARAMETER
);
1783 // make a SPLIT_LIST item and add to list
1785 Split
= AllocateZeroPool(sizeof(SPLIT_LIST
));
1786 if (Split
== NULL
) {
1787 return EFI_OUT_OF_RESOURCES
;
1789 Split
->SplitStdIn
= StdIn
;
1790 Split
->SplitStdOut
= ConvertEfiFileProtocolToShellHandle(CreateFileInterfaceMem(Unicode
), NULL
);
1791 ASSERT(Split
->SplitStdOut
!= NULL
);
1792 InsertHeadList(&ShellInfoObject
.SplitList
.Link
, &Split
->Link
);
1794 Status
= RunCommand(OurCommandLine
);
1797 // move the output from the first to the in to the second.
1799 TempFileHandle
= Split
->SplitStdOut
;
1800 if (Split
->SplitStdIn
== StdIn
) {
1801 Split
->SplitStdOut
= NULL
;
1803 Split
->SplitStdOut
= Split
->SplitStdIn
;
1805 Split
->SplitStdIn
= TempFileHandle
;
1806 ShellInfoObject
.NewEfiShellProtocol
->SetFilePosition (Split
->SplitStdIn
, 0);
1808 if (!EFI_ERROR(Status
)) {
1809 Status
= RunCommand(NextCommandLine
);
1813 // remove the top level from the ScriptList
1815 ASSERT((SPLIT_LIST
*)GetFirstNode(&ShellInfoObject
.SplitList
.Link
) == Split
);
1816 RemoveEntryList(&Split
->Link
);
1819 // Note that the original StdIn is now the StdOut...
1821 if (Split
->SplitStdOut
!= NULL
) {
1822 ShellInfoObject
.NewEfiShellProtocol
->CloseFile (Split
->SplitStdOut
);
1824 if (Split
->SplitStdIn
!= NULL
) {
1825 ShellInfoObject
.NewEfiShellProtocol
->CloseFile (Split
->SplitStdIn
);
1829 FreePool(NextCommandLine
);
1830 FreePool(OurCommandLine
);
1836 Take the original command line, substitute any variables, free
1837 the original string, return the modified copy.
1839 @param[in] CmdLine pointer to the command line to update.
1841 @retval EFI_SUCCESS the function was successful.
1842 @retval EFI_OUT_OF_RESOURCES a memory allocation failed.
1845 ShellSubstituteVariables(
1850 NewCmdLine
= ShellConvertVariables(*CmdLine
);
1851 SHELL_FREE_NON_NULL(*CmdLine
);
1852 if (NewCmdLine
== NULL
) {
1853 return (EFI_OUT_OF_RESOURCES
);
1855 *CmdLine
= NewCmdLine
;
1856 return (EFI_SUCCESS
);
1860 Take the original command line, substitute any alias in the first group of space delimited characters, free
1861 the original string, return the modified copy.
1863 @param[in] CmdLine pointer to the command line to update.
1865 @retval EFI_SUCCESS the function was successful.
1866 @retval EFI_OUT_OF_RESOURCES a memory allocation failed.
1869 ShellSubstituteAliases(
1874 CHAR16
*CommandName
;
1876 UINTN PostAliasSize
;
1877 ASSERT(CmdLine
!= NULL
);
1878 ASSERT(*CmdLine
!= NULL
);
1882 if (StrStr((*CmdLine
), L
" ") == NULL
){
1883 StrnCatGrow(&CommandName
, NULL
, (*CmdLine
), 0);
1885 StrnCatGrow(&CommandName
, NULL
, (*CmdLine
), StrStr((*CmdLine
), L
" ") - (*CmdLine
));
1889 // This cannot happen 'inline' since the CmdLine can need extra space.
1892 if (!ShellCommandIsCommandOnList(CommandName
)) {
1894 // Convert via alias
1896 Status
= ShellConvertAlias(&CommandName
);
1897 if (EFI_ERROR(Status
)){
1901 NewCmdLine
= StrnCatGrow(&NewCmdLine
, &PostAliasSize
, CommandName
, 0);
1902 if (NewCmdLine
== NULL
) {
1903 SHELL_FREE_NON_NULL(CommandName
);
1904 SHELL_FREE_NON_NULL(*CmdLine
);
1905 return (EFI_OUT_OF_RESOURCES
);
1907 NewCmdLine
= StrnCatGrow(&NewCmdLine
, &PostAliasSize
, StrStr((*CmdLine
), L
" "), 0);
1908 if (NewCmdLine
== NULL
) {
1909 SHELL_FREE_NON_NULL(CommandName
);
1910 SHELL_FREE_NON_NULL(*CmdLine
);
1911 return (EFI_OUT_OF_RESOURCES
);
1914 NewCmdLine
= StrnCatGrow(&NewCmdLine
, NULL
, (*CmdLine
), 0);
1917 SHELL_FREE_NON_NULL(*CmdLine
);
1918 SHELL_FREE_NON_NULL(CommandName
);
1921 // re-assign the passed in double pointer to point to our newly allocated buffer
1923 *CmdLine
= NewCmdLine
;
1925 return (EFI_SUCCESS
);
1929 Takes the Argv[0] part of the command line and determine the meaning of it.
1931 @param[in] CmdName pointer to the command line to update.
1933 @retval Internal_Command The name is an internal command.
1934 @retval File_Sys_Change the name is a file system change.
1935 @retval Script_File_Name the name is a NSH script file.
1936 @retval Unknown_Invalid the name is unknown.
1937 @retval Efi_Application the name is an application (.EFI).
1939 SHELL_OPERATION_TYPES
1941 IN CONST CHAR16
*CmdName
1944 CHAR16
* FileWithPath
;
1945 CONST CHAR16
* TempLocation
;
1946 CONST CHAR16
* TempLocation2
;
1948 FileWithPath
= NULL
;
1950 // test for an internal command.
1952 if (ShellCommandIsCommandOnList(CmdName
)) {
1953 return (Internal_Command
);
1957 // Test for file system change request. anything ending with first : and cant have spaces.
1959 if (CmdName
[(StrLen(CmdName
)-1)] == L
':') {
1960 if ( StrStr(CmdName
, L
" ") != NULL
1961 || StrLen(StrStr(CmdName
, L
":")) > 1
1963 return (Unknown_Invalid
);
1965 return (File_Sys_Change
);
1971 if ((FileWithPath
= ShellFindFilePathEx(CmdName
, mExecutableExtensions
)) != NULL
) {
1973 // See if that file has a script file extension
1975 if (StrLen(FileWithPath
) > 4) {
1976 TempLocation
= FileWithPath
+StrLen(FileWithPath
)-4;
1977 TempLocation2
= mScriptExtension
;
1978 if (StringNoCaseCompare((VOID
*)(&TempLocation
), (VOID
*)(&TempLocation2
)) == 0) {
1979 SHELL_FREE_NON_NULL(FileWithPath
);
1980 return (Script_File_Name
);
1985 // Was a file, but not a script. we treat this as an application.
1987 SHELL_FREE_NON_NULL(FileWithPath
);
1988 return (Efi_Application
);
1991 SHELL_FREE_NON_NULL(FileWithPath
);
1993 // No clue what this is... return invalid flag...
1995 return (Unknown_Invalid
);
1999 Determine if the first item in a command line is valid.
2001 @param[in] CmdLine The command line to parse.
2003 @retval EFI_SUCCESS The item is valid.
2004 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
2005 @retval EFI_NOT_FOUND The operation type is unknown or invalid.
2009 IN CONST CHAR16
*CmdLine
2013 CHAR16
*FirstParameter
;
2019 Temp
= StrnCatGrow(&Temp
, NULL
, CmdLine
, 0);
2021 return (EFI_OUT_OF_RESOURCES
);
2024 FirstParameter
= StrStr(Temp
, L
"|");
2025 if (FirstParameter
!= NULL
) {
2026 *FirstParameter
= CHAR_NULL
;
2029 FirstParameter
= NULL
;
2032 // Process the command line
2034 Status
= ProcessCommandLineToFinal(&Temp
);
2036 if (!EFI_ERROR(Status
)) {
2037 FirstParameter
= AllocateZeroPool(StrSize(CmdLine
));
2038 if (FirstParameter
== NULL
) {
2039 SHELL_FREE_NON_NULL(Temp
);
2040 return (EFI_OUT_OF_RESOURCES
);
2042 TempWalker
= (CHAR16
*)Temp
;
2043 if (!EFI_ERROR(GetNextParameter(&TempWalker
, &FirstParameter
, StrSize(CmdLine
), TRUE
))) {
2044 if (GetOperationType(FirstParameter
) == Unknown_Invalid
) {
2045 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_NOT_FOUND
), ShellInfoObject
.HiiHandle
, FirstParameter
);
2046 SetLastError(SHELL_NOT_FOUND
);
2047 Status
= EFI_NOT_FOUND
;
2052 SHELL_FREE_NON_NULL(Temp
);
2053 SHELL_FREE_NON_NULL(FirstParameter
);
2058 Determine if a command line contains with a split contains only valid commands.
2060 @param[in] CmdLine The command line to parse.
2062 @retval EFI_SUCCESS CmdLine has only valid commands, application, or has no split.
2063 @retval EFI_ABORTED CmdLine has at least one invalid command or application.
2067 IN CONST CHAR16
*CmdLine
2070 CONST CHAR16
*TempSpot
;
2074 // If this was the only item, then get out
2076 if (!ContainsSplit(CmdLine
)) {
2077 return (EFI_SUCCESS
);
2081 // Verify up to the pipe or end character
2083 Status
= IsValidSplit(CmdLine
);
2084 if (EFI_ERROR(Status
)) {
2089 // recurse to verify the next item
2091 TempSpot
= FindFirstCharacter(CmdLine
, L
"|", L
'^') + 1;
2092 if (*TempSpot
== L
'a' &&
2093 (*(TempSpot
+ 1) == L
' ' || *(TempSpot
+ 1) == CHAR_NULL
)
2095 // If it's an ASCII pipe '|a'
2099 return (VerifySplit(TempSpot
));
2103 Process a split based operation.
2105 @param[in] CmdLine pointer to the command line to process
2107 @retval EFI_SUCCESS The operation was successful
2108 @return an error occurred.
2111 ProcessNewSplitCommandLine(
2112 IN CONST CHAR16
*CmdLine
2118 Status
= VerifySplit(CmdLine
);
2119 if (EFI_ERROR(Status
)) {
2126 // are we in an existing split???
2128 if (!IsListEmpty(&ShellInfoObject
.SplitList
.Link
)) {
2129 Split
= (SPLIT_LIST
*)GetFirstNode(&ShellInfoObject
.SplitList
.Link
);
2132 if (Split
== NULL
) {
2133 Status
= RunSplitCommand(CmdLine
, NULL
, NULL
);
2135 Status
= RunSplitCommand(CmdLine
, Split
->SplitStdIn
, Split
->SplitStdOut
);
2137 if (EFI_ERROR(Status
)) {
2138 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_INVALID_SPLIT
), ShellInfoObject
.HiiHandle
, CmdLine
);
2144 Handle a request to change the current file system.
2146 @param[in] CmdLine The passed in command line.
2148 @retval EFI_SUCCESS The operation was successful.
2152 IN CONST CHAR16
*CmdLine
2156 Status
= EFI_SUCCESS
;
2159 // make sure we are the right operation
2161 ASSERT(CmdLine
[(StrLen(CmdLine
)-1)] == L
':' && StrStr(CmdLine
, L
" ") == NULL
);
2164 // Call the protocol API to do the work
2166 Status
= ShellInfoObject
.NewEfiShellProtocol
->SetCurDir(NULL
, CmdLine
);
2169 // Report any errors
2171 if (EFI_ERROR(Status
)) {
2172 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_INVALID_MAPPING
), ShellInfoObject
.HiiHandle
, CmdLine
);
2179 Reprocess the command line to direct all -? to the help command.
2181 if found, will add "help" as argv[0], and move the rest later.
2183 @param[in,out] CmdLine pointer to the command line to update
2187 IN OUT CHAR16
**CmdLine
2190 CHAR16
*CurrentParameter
;
2192 CHAR16
*NewCommandLine
;
2194 UINTN NewCmdLineSize
;
2196 Status
= EFI_SUCCESS
;
2198 CurrentParameter
= AllocateZeroPool(StrSize(*CmdLine
));
2199 if (CurrentParameter
== NULL
) {
2200 return (EFI_OUT_OF_RESOURCES
);
2204 while(Walker
!= NULL
&& *Walker
!= CHAR_NULL
) {
2205 if (!EFI_ERROR(GetNextParameter(&Walker
, &CurrentParameter
, StrSize(*CmdLine
), TRUE
))) {
2206 if (StrStr(CurrentParameter
, L
"-?") == CurrentParameter
) {
2207 CurrentParameter
[0] = L
' ';
2208 CurrentParameter
[1] = L
' ';
2209 NewCmdLineSize
= StrSize(L
"help ") + StrSize(*CmdLine
);
2210 NewCommandLine
= AllocateZeroPool(NewCmdLineSize
);
2211 if (NewCommandLine
== NULL
) {
2212 Status
= EFI_OUT_OF_RESOURCES
;
2217 // We know the space is sufficient since we just calculated it.
2219 StrnCpyS(NewCommandLine
, NewCmdLineSize
/sizeof(CHAR16
), L
"help ", 5);
2220 StrnCatS(NewCommandLine
, NewCmdLineSize
/sizeof(CHAR16
), *CmdLine
, StrLen(*CmdLine
));
2221 SHELL_FREE_NON_NULL(*CmdLine
);
2222 *CmdLine
= NewCommandLine
;
2228 SHELL_FREE_NON_NULL(CurrentParameter
);
2234 Function to update the shell variable "lasterror".
2236 @param[in] ErrorCode the error code to put into lasterror.
2240 IN CONST SHELL_STATUS ErrorCode
2243 CHAR16 LeString
[19];
2244 if (sizeof(EFI_STATUS
) == sizeof(UINT64
)) {
2245 UnicodeSPrint(LeString
, sizeof(LeString
), L
"0x%Lx", ErrorCode
);
2247 UnicodeSPrint(LeString
, sizeof(LeString
), L
"0x%x", ErrorCode
);
2249 DEBUG_CODE(InternalEfiShellSetEnv(L
"debuglasterror", LeString
, TRUE
););
2250 InternalEfiShellSetEnv(L
"lasterror", LeString
, TRUE
);
2252 return (EFI_SUCCESS
);
2256 Converts the command line to it's post-processed form. this replaces variables and alias' per UEFI Shell spec.
2258 @param[in,out] CmdLine pointer to the command line to update
2260 @retval EFI_SUCCESS The operation was successful
2261 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
2262 @return some other error occurred
2265 ProcessCommandLineToFinal(
2266 IN OUT CHAR16
**CmdLine
2270 TrimSpaces(CmdLine
);
2272 Status
= ShellSubstituteAliases(CmdLine
);
2273 if (EFI_ERROR(Status
)) {
2277 TrimSpaces(CmdLine
);
2279 Status
= ShellSubstituteVariables(CmdLine
);
2280 if (EFI_ERROR(Status
)) {
2283 ASSERT (*CmdLine
!= NULL
);
2285 TrimSpaces(CmdLine
);
2288 // update for help parsing
2290 if (StrStr(*CmdLine
, L
"?") != NULL
) {
2292 // This may do nothing if the ? does not indicate help.
2293 // Save all the details for in the API below.
2295 Status
= DoHelpUpdate(CmdLine
);
2298 TrimSpaces(CmdLine
);
2300 return (EFI_SUCCESS
);
2304 Run an internal shell command.
2306 This API will update the shell's environment since these commands are libraries.
2308 @param[in] CmdLine the command line to run.
2309 @param[in] FirstParameter the first parameter on the command line
2310 @param[in] ParamProtocol the shell parameters protocol pointer
2311 @param[out] CommandStatus the status from the command line.
2313 @retval EFI_SUCCESS The command was completed.
2314 @retval EFI_ABORTED The command's operation was aborted.
2318 IN CONST CHAR16
*CmdLine
,
2319 IN CHAR16
*FirstParameter
,
2320 IN EFI_SHELL_PARAMETERS_PROTOCOL
*ParamProtocol
,
2321 OUT EFI_STATUS
*CommandStatus
2327 SHELL_STATUS CommandReturnedStatus
;
2332 NewCmdLine
= AllocateCopyPool (StrSize (CmdLine
), CmdLine
);
2333 if (NewCmdLine
== NULL
) {
2334 return EFI_OUT_OF_RESOURCES
;
2337 for (Walker
= NewCmdLine
; Walker
!= NULL
&& *Walker
!= CHAR_NULL
; Walker
++) {
2338 if (*Walker
== L
'^' && *(Walker
+1) == L
'#') {
2339 CopyMem(Walker
, Walker
+1, StrSize(Walker
) - sizeof(Walker
[0]));
2344 // get the argc and argv updated for internal commands
2346 Status
= UpdateArgcArgv(ParamProtocol
, NewCmdLine
, Internal_Command
, &Argv
, &Argc
);
2347 if (!EFI_ERROR(Status
)) {
2349 // Run the internal command.
2351 Status
= ShellCommandRunCommandHandler(FirstParameter
, &CommandReturnedStatus
, &LastError
);
2353 if (!EFI_ERROR(Status
)) {
2354 if (CommandStatus
!= NULL
) {
2355 if (CommandReturnedStatus
!= SHELL_SUCCESS
) {
2356 *CommandStatus
= (EFI_STATUS
)(CommandReturnedStatus
| MAX_BIT
);
2358 *CommandStatus
= EFI_SUCCESS
;
2363 // Update last error status.
2364 // some commands do not update last error.
2367 SetLastError(CommandReturnedStatus
);
2371 // Pass thru the exitcode from the app.
2373 if (ShellCommandGetExit()) {
2375 // An Exit was requested ("exit" command), pass its value up.
2377 Status
= CommandReturnedStatus
;
2378 } else if (CommandReturnedStatus
!= SHELL_SUCCESS
&& IsScriptOnlyCommand(FirstParameter
)) {
2380 // Always abort when a script only command fails for any reason
2382 Status
= EFI_ABORTED
;
2383 } else if (ShellCommandGetCurrentScriptFile() != NULL
&& CommandReturnedStatus
== SHELL_ABORTED
) {
2385 // Abort when in a script and a command aborted
2387 Status
= EFI_ABORTED
;
2393 // This is guaranteed to be called after UpdateArgcArgv no matter what else happened.
2394 // This is safe even if the update API failed. In this case, it may be a no-op.
2396 RestoreArgcArgv(ParamProtocol
, &Argv
, &Argc
);
2399 // If a script is running and the command is not a script only command, then
2400 // change return value to success so the script won't halt (unless aborted).
2402 // Script only commands have to be able halt the script since the script will
2403 // not operate if they are failing.
2405 if ( ShellCommandGetCurrentScriptFile() != NULL
2406 && !IsScriptOnlyCommand(FirstParameter
)
2407 && Status
!= EFI_ABORTED
2409 Status
= EFI_SUCCESS
;
2412 FreePool (NewCmdLine
);
2417 Function to run the command or file.
2419 @param[in] Type the type of operation being run.
2420 @param[in] CmdLine the command line to run.
2421 @param[in] FirstParameter the first parameter on the command line
2422 @param[in] ParamProtocol the shell parameters protocol pointer
2423 @param[out] CommandStatus the status from the command line.
2425 @retval EFI_SUCCESS The command was completed.
2426 @retval EFI_ABORTED The command's operation was aborted.
2430 IN SHELL_OPERATION_TYPES Type
,
2431 IN CONST CHAR16
*CmdLine
,
2432 IN CHAR16
*FirstParameter
,
2433 IN EFI_SHELL_PARAMETERS_PROTOCOL
*ParamProtocol
,
2434 OUT EFI_STATUS
*CommandStatus
2438 EFI_STATUS StartStatus
;
2439 CHAR16
*CommandWithPath
;
2440 CHAR16
*FullCommandWithPath
;
2441 EFI_DEVICE_PATH_PROTOCOL
*DevPath
;
2442 SHELL_STATUS CalleeExitStatus
;
2444 Status
= EFI_SUCCESS
;
2445 CommandWithPath
= NULL
;
2447 CalleeExitStatus
= SHELL_INVALID_PARAMETER
;
2450 case Internal_Command
:
2451 Status
= RunInternalCommand(CmdLine
, FirstParameter
, ParamProtocol
, CommandStatus
);
2453 case Script_File_Name
:
2454 case Efi_Application
:
2456 // Process a fully qualified path
2458 if (StrStr(FirstParameter
, L
":") != NULL
) {
2459 ASSERT (CommandWithPath
== NULL
);
2460 if (ShellIsFile(FirstParameter
) == EFI_SUCCESS
) {
2461 CommandWithPath
= StrnCatGrow(&CommandWithPath
, NULL
, FirstParameter
, 0);
2466 // Process a relative path and also check in the path environment variable
2468 if (CommandWithPath
== NULL
) {
2469 CommandWithPath
= ShellFindFilePathEx(FirstParameter
, mExecutableExtensions
);
2473 // This should be impossible now.
2475 ASSERT(CommandWithPath
!= NULL
);
2478 // Make sure that path is not just a directory (or not found)
2480 if (!EFI_ERROR(ShellIsDirectory(CommandWithPath
))) {
2481 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_NOT_FOUND
), ShellInfoObject
.HiiHandle
, FirstParameter
);
2482 SetLastError(SHELL_NOT_FOUND
);
2485 case Script_File_Name
:
2486 FullCommandWithPath
= FullyQualifyPath(CommandWithPath
);
2487 if (FullCommandWithPath
== NULL
) {
2488 Status
= RunScriptFile (CommandWithPath
, NULL
, CmdLine
, ParamProtocol
);
2490 Status
= RunScriptFile (FullCommandWithPath
, NULL
, CmdLine
, ParamProtocol
);
2491 FreePool(FullCommandWithPath
);
2494 case Efi_Application
:
2496 // Get the device path of the application image
2498 DevPath
= ShellInfoObject
.NewEfiShellProtocol
->GetDevicePathFromFilePath(CommandWithPath
);
2499 if (DevPath
== NULL
){
2500 Status
= EFI_OUT_OF_RESOURCES
;
2505 // Execute the device path
2507 Status
= InternalShellExecuteDevicePath(
2515 SHELL_FREE_NON_NULL(DevPath
);
2517 if(EFI_ERROR (Status
)) {
2518 CalleeExitStatus
= (SHELL_STATUS
) (Status
& (~MAX_BIT
));
2520 CalleeExitStatus
= (SHELL_STATUS
) StartStatus
;
2523 if (CommandStatus
!= NULL
) {
2524 *CommandStatus
= CalleeExitStatus
;
2528 // Update last error status.
2530 // Status is an EFI_STATUS. Clear top bit to convert to SHELL_STATUS
2531 SetLastError(CalleeExitStatus
);
2547 SHELL_FREE_NON_NULL(CommandWithPath
);
2553 Function to setup StdIn, StdErr, StdOut, and then run the command or file.
2555 @param[in] Type the type of operation being run.
2556 @param[in] CmdLine the command line to run.
2557 @param[in] FirstParameter the first parameter on the command line.
2558 @param[in] ParamProtocol the shell parameters protocol pointer
2559 @param[out] CommandStatus the status from the command line.
2561 @retval EFI_SUCCESS The command was completed.
2562 @retval EFI_ABORTED The command's operation was aborted.
2565 SetupAndRunCommandOrFile(
2566 IN SHELL_OPERATION_TYPES Type
,
2568 IN CHAR16
*FirstParameter
,
2569 IN EFI_SHELL_PARAMETERS_PROTOCOL
*ParamProtocol
,
2570 OUT EFI_STATUS
*CommandStatus
2574 SHELL_FILE_HANDLE OriginalStdIn
;
2575 SHELL_FILE_HANDLE OriginalStdOut
;
2576 SHELL_FILE_HANDLE OriginalStdErr
;
2577 SYSTEM_TABLE_INFO OriginalSystemTableInfo
;
2578 CONST SCRIPT_FILE
*ConstScriptFile
;
2581 // Update the StdIn, StdOut, and StdErr for redirection to environment variables, files, etc... unicode and ASCII
2583 Status
= UpdateStdInStdOutStdErr(ParamProtocol
, CmdLine
, &OriginalStdIn
, &OriginalStdOut
, &OriginalStdErr
, &OriginalSystemTableInfo
);
2586 // The StdIn, StdOut, and StdErr are set up.
2587 // Now run the command, script, or application
2589 if (!EFI_ERROR(Status
)) {
2590 TrimSpaces(&CmdLine
);
2591 Status
= RunCommandOrFile(Type
, CmdLine
, FirstParameter
, ParamProtocol
, CommandStatus
);
2597 if (EFI_ERROR(Status
)) {
2598 ConstScriptFile
= ShellCommandGetCurrentScriptFile();
2599 if (ConstScriptFile
== NULL
|| ConstScriptFile
->CurrentCommand
== NULL
) {
2600 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_ERROR
), ShellInfoObject
.HiiHandle
, (VOID
*)(Status
));
2602 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_ERROR_SCRIPT
), ShellInfoObject
.HiiHandle
, (VOID
*)(Status
), ConstScriptFile
->CurrentCommand
->Line
);
2607 // put back the original StdIn, StdOut, and StdErr
2609 RestoreStdInStdOutStdErr(ParamProtocol
, &OriginalStdIn
, &OriginalStdOut
, &OriginalStdErr
, &OriginalSystemTableInfo
);
2615 Function will process and run a command line.
2617 This will determine if the command line represents an internal shell
2618 command or dispatch an external application.
2620 @param[in] CmdLine The command line to parse.
2621 @param[out] CommandStatus The status from the command line.
2623 @retval EFI_SUCCESS The command was completed.
2624 @retval EFI_ABORTED The command's operation was aborted.
2628 IN CONST CHAR16
*CmdLine
,
2629 OUT EFI_STATUS
*CommandStatus
2633 CHAR16
*CleanOriginal
;
2634 CHAR16
*FirstParameter
;
2636 SHELL_OPERATION_TYPES Type
;
2637 CONST CHAR16
*CurDir
;
2639 ASSERT(CmdLine
!= NULL
);
2640 if (StrLen(CmdLine
) == 0) {
2641 return (EFI_SUCCESS
);
2644 Status
= EFI_SUCCESS
;
2645 CleanOriginal
= NULL
;
2647 CleanOriginal
= StrnCatGrow(&CleanOriginal
, NULL
, CmdLine
, 0);
2648 if (CleanOriginal
== NULL
) {
2649 return (EFI_OUT_OF_RESOURCES
);
2652 TrimSpaces(&CleanOriginal
);
2655 // NULL out comments (leveraged from RunScriptFileHandle() ).
2656 // The # character on a line is used to denote that all characters on the same line
2657 // and to the right of the # are to be ignored by the shell.
2658 // Afterwards, again remove spaces, in case any were between the last command-parameter and '#'.
2660 for (TempWalker
= CleanOriginal
; TempWalker
!= NULL
&& *TempWalker
!= CHAR_NULL
; TempWalker
++) {
2661 if (*TempWalker
== L
'^') {
2662 if (*(TempWalker
+ 1) == L
'#') {
2665 } else if (*TempWalker
== L
'#') {
2666 *TempWalker
= CHAR_NULL
;
2670 TrimSpaces(&CleanOriginal
);
2673 // Handle case that passed in command line is just 1 or more " " characters.
2675 if (StrLen (CleanOriginal
) == 0) {
2676 SHELL_FREE_NON_NULL(CleanOriginal
);
2677 return (EFI_SUCCESS
);
2680 Status
= ProcessCommandLineToFinal(&CleanOriginal
);
2681 if (EFI_ERROR(Status
)) {
2682 SHELL_FREE_NON_NULL(CleanOriginal
);
2687 // We don't do normal processing with a split command line (output from one command input to another)
2689 if (ContainsSplit(CleanOriginal
)) {
2690 Status
= ProcessNewSplitCommandLine(CleanOriginal
);
2691 SHELL_FREE_NON_NULL(CleanOriginal
);
2696 // We need the first parameter information so we can determine the operation type
2698 FirstParameter
= AllocateZeroPool(StrSize(CleanOriginal
));
2699 if (FirstParameter
== NULL
) {
2700 SHELL_FREE_NON_NULL(CleanOriginal
);
2701 return (EFI_OUT_OF_RESOURCES
);
2703 TempWalker
= CleanOriginal
;
2704 if (!EFI_ERROR(GetNextParameter(&TempWalker
, &FirstParameter
, StrSize(CleanOriginal
), TRUE
))) {
2706 // Depending on the first parameter we change the behavior
2708 switch (Type
= GetOperationType(FirstParameter
)) {
2709 case File_Sys_Change
:
2710 Status
= ChangeMappedDrive (FirstParameter
);
2712 case Internal_Command
:
2713 case Script_File_Name
:
2714 case Efi_Application
:
2715 Status
= SetupAndRunCommandOrFile(Type
, CleanOriginal
, FirstParameter
, ShellInfoObject
.NewShellParametersProtocol
, CommandStatus
);
2719 // Whatever was typed, it was invalid.
2721 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_NOT_FOUND
), ShellInfoObject
.HiiHandle
, FirstParameter
);
2722 SetLastError(SHELL_NOT_FOUND
);
2726 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_NOT_FOUND
), ShellInfoObject
.HiiHandle
, FirstParameter
);
2727 SetLastError(SHELL_NOT_FOUND
);
2730 // Check whether the current file system still exists. If not exist, we need update "cwd" and gShellCurMapping.
2732 CurDir
= EfiShellGetCurDir (NULL
);
2733 if (CurDir
!= NULL
) {
2734 if (EFI_ERROR(ShellFileExists (CurDir
))) {
2736 // EfiShellSetCurDir() cannot set current directory to NULL.
2737 // EfiShellSetEnv() is not allowed to set the "cwd" variable.
2738 // Only InternalEfiShellSetEnv () is allowed setting the "cwd" variable.
2740 InternalEfiShellSetEnv (L
"cwd", NULL
, TRUE
);
2741 gShellCurMapping
= NULL
;
2745 SHELL_FREE_NON_NULL(CleanOriginal
);
2746 SHELL_FREE_NON_NULL(FirstParameter
);
2752 Function will process and run a command line.
2754 This will determine if the command line represents an internal shell
2755 command or dispatch an external application.
2757 @param[in] CmdLine The command line to parse.
2759 @retval EFI_SUCCESS The command was completed.
2760 @retval EFI_ABORTED The command's operation was aborted.
2764 IN CONST CHAR16
*CmdLine
2767 return (RunShellCommand(CmdLine
, NULL
));
2771 Function to process a NSH script file via SHELL_FILE_HANDLE.
2773 @param[in] Handle The handle to the already opened file.
2774 @param[in] Name The name of the script file.
2776 @retval EFI_SUCCESS the script completed successfully
2779 RunScriptFileHandle (
2780 IN SHELL_FILE_HANDLE Handle
,
2781 IN CONST CHAR16
*Name
2785 SCRIPT_FILE
*NewScriptFile
;
2787 UINTN PrintBuffSize
;
2788 CHAR16
*CommandLine
;
2789 CHAR16
*CommandLine2
;
2790 CHAR16
*CommandLine3
;
2791 SCRIPT_COMMAND_LIST
*LastCommand
;
2793 BOOLEAN PreScriptEchoState
;
2794 BOOLEAN PreCommandEchoState
;
2795 CONST CHAR16
*CurDir
;
2797 CHAR16 LeString
[50];
2798 LIST_ENTRY OldBufferList
;
2800 ASSERT(!ShellCommandGetScriptExit());
2802 PreScriptEchoState
= ShellCommandGetEchoState();
2803 PrintBuffSize
= PcdGet16(PcdShellPrintBufferSize
);
2805 NewScriptFile
= (SCRIPT_FILE
*)AllocateZeroPool(sizeof(SCRIPT_FILE
));
2806 if (NewScriptFile
== NULL
) {
2807 return (EFI_OUT_OF_RESOURCES
);
2813 ASSERT(NewScriptFile
->ScriptName
== NULL
);
2814 NewScriptFile
->ScriptName
= StrnCatGrow(&NewScriptFile
->ScriptName
, NULL
, Name
, 0);
2815 if (NewScriptFile
->ScriptName
== NULL
) {
2816 DeleteScriptFileStruct(NewScriptFile
);
2817 return (EFI_OUT_OF_RESOURCES
);
2821 // Save the parameters (used to replace %0 to %9 later on)
2823 NewScriptFile
->Argc
= ShellInfoObject
.NewShellParametersProtocol
->Argc
;
2824 if (NewScriptFile
->Argc
!= 0) {
2825 NewScriptFile
->Argv
= (CHAR16
**)AllocateZeroPool(NewScriptFile
->Argc
* sizeof(CHAR16
*));
2826 if (NewScriptFile
->Argv
== NULL
) {
2827 DeleteScriptFileStruct(NewScriptFile
);
2828 return (EFI_OUT_OF_RESOURCES
);
2831 // Put the full path of the script file into Argv[0] as required by section
2832 // 3.6.2 of version 2.2 of the shell specification.
2834 NewScriptFile
->Argv
[0] = StrnCatGrow(&NewScriptFile
->Argv
[0], NULL
, NewScriptFile
->ScriptName
, 0);
2835 for (LoopVar
= 1 ; LoopVar
< 10 && LoopVar
< NewScriptFile
->Argc
; LoopVar
++) {
2836 ASSERT(NewScriptFile
->Argv
[LoopVar
] == NULL
);
2837 NewScriptFile
->Argv
[LoopVar
] = StrnCatGrow(&NewScriptFile
->Argv
[LoopVar
], NULL
, ShellInfoObject
.NewShellParametersProtocol
->Argv
[LoopVar
], 0);
2838 if (NewScriptFile
->Argv
[LoopVar
] == NULL
) {
2839 DeleteScriptFileStruct(NewScriptFile
);
2840 return (EFI_OUT_OF_RESOURCES
);
2844 NewScriptFile
->Argv
= NULL
;
2847 InitializeListHead(&NewScriptFile
->CommandList
);
2848 InitializeListHead(&NewScriptFile
->SubstList
);
2851 // Now build the list of all script commands.
2854 while(!ShellFileHandleEof(Handle
)) {
2855 CommandLine
= ShellFileHandleReturnLine(Handle
, &Ascii
);
2857 if (CommandLine
== NULL
|| StrLen(CommandLine
) == 0 || CommandLine
[0] == '#') {
2858 SHELL_FREE_NON_NULL(CommandLine
);
2861 NewScriptFile
->CurrentCommand
= AllocateZeroPool(sizeof(SCRIPT_COMMAND_LIST
));
2862 if (NewScriptFile
->CurrentCommand
== NULL
) {
2863 SHELL_FREE_NON_NULL(CommandLine
);
2864 DeleteScriptFileStruct(NewScriptFile
);
2865 return (EFI_OUT_OF_RESOURCES
);
2868 NewScriptFile
->CurrentCommand
->Cl
= CommandLine
;
2869 NewScriptFile
->CurrentCommand
->Data
= NULL
;
2870 NewScriptFile
->CurrentCommand
->Line
= LineCount
;
2872 InsertTailList(&NewScriptFile
->CommandList
, &NewScriptFile
->CurrentCommand
->Link
);
2876 // Add this as the topmost script file
2878 ShellCommandSetNewScript (NewScriptFile
);
2881 // Now enumerate through the commands and run each one.
2883 CommandLine
= AllocateZeroPool(PrintBuffSize
);
2884 if (CommandLine
== NULL
) {
2885 DeleteScriptFileStruct(NewScriptFile
);
2886 return (EFI_OUT_OF_RESOURCES
);
2888 CommandLine2
= AllocateZeroPool(PrintBuffSize
);
2889 if (CommandLine2
== NULL
) {
2890 FreePool(CommandLine
);
2891 DeleteScriptFileStruct(NewScriptFile
);
2892 return (EFI_OUT_OF_RESOURCES
);
2895 for ( NewScriptFile
->CurrentCommand
= (SCRIPT_COMMAND_LIST
*)GetFirstNode(&NewScriptFile
->CommandList
)
2896 ; !IsNull(&NewScriptFile
->CommandList
, &NewScriptFile
->CurrentCommand
->Link
)
2897 ; // conditional increment in the body of the loop
2899 ASSERT(CommandLine2
!= NULL
);
2900 StrnCpyS( CommandLine2
,
2901 PrintBuffSize
/sizeof(CHAR16
),
2902 NewScriptFile
->CurrentCommand
->Cl
,
2903 PrintBuffSize
/sizeof(CHAR16
) - 1
2906 SaveBufferList(&OldBufferList
);
2909 // NULL out comments
2911 for (CommandLine3
= CommandLine2
; CommandLine3
!= NULL
&& *CommandLine3
!= CHAR_NULL
; CommandLine3
++) {
2912 if (*CommandLine3
== L
'^') {
2913 if ( *(CommandLine3
+1) == L
':') {
2914 CopyMem(CommandLine3
, CommandLine3
+1, StrSize(CommandLine3
) - sizeof(CommandLine3
[0]));
2915 } else if (*(CommandLine3
+1) == L
'#') {
2918 } else if (*CommandLine3
== L
'#') {
2919 *CommandLine3
= CHAR_NULL
;
2923 if (CommandLine2
!= NULL
&& StrLen(CommandLine2
) >= 1) {
2925 // Due to variability in starting the find and replace action we need to have both buffers the same.
2927 StrnCpyS( CommandLine
,
2928 PrintBuffSize
/sizeof(CHAR16
),
2930 PrintBuffSize
/sizeof(CHAR16
) - 1
2934 // Remove the %0 to %9 from the command line (if we have some arguments)
2936 if (NewScriptFile
->Argv
!= NULL
) {
2937 switch (NewScriptFile
->Argc
) {
2939 Status
= ShellCopySearchAndReplace(CommandLine2
, CommandLine
, PrintBuffSize
, L
"%9", NewScriptFile
->Argv
[9], FALSE
, FALSE
);
2940 ASSERT_EFI_ERROR(Status
);
2942 Status
= ShellCopySearchAndReplace(CommandLine
, CommandLine2
, PrintBuffSize
, L
"%8", NewScriptFile
->Argv
[8], FALSE
, FALSE
);
2943 ASSERT_EFI_ERROR(Status
);
2945 Status
= ShellCopySearchAndReplace(CommandLine2
, CommandLine
, PrintBuffSize
, L
"%7", NewScriptFile
->Argv
[7], FALSE
, FALSE
);
2946 ASSERT_EFI_ERROR(Status
);
2948 Status
= ShellCopySearchAndReplace(CommandLine
, CommandLine2
, PrintBuffSize
, L
"%6", NewScriptFile
->Argv
[6], FALSE
, FALSE
);
2949 ASSERT_EFI_ERROR(Status
);
2951 Status
= ShellCopySearchAndReplace(CommandLine2
, CommandLine
, PrintBuffSize
, L
"%5", NewScriptFile
->Argv
[5], FALSE
, FALSE
);
2952 ASSERT_EFI_ERROR(Status
);
2954 Status
= ShellCopySearchAndReplace(CommandLine
, CommandLine2
, PrintBuffSize
, L
"%4", NewScriptFile
->Argv
[4], FALSE
, FALSE
);
2955 ASSERT_EFI_ERROR(Status
);
2957 Status
= ShellCopySearchAndReplace(CommandLine2
, CommandLine
, PrintBuffSize
, L
"%3", NewScriptFile
->Argv
[3], FALSE
, FALSE
);
2958 ASSERT_EFI_ERROR(Status
);
2960 Status
= ShellCopySearchAndReplace(CommandLine
, CommandLine2
, PrintBuffSize
, L
"%2", NewScriptFile
->Argv
[2], FALSE
, FALSE
);
2961 ASSERT_EFI_ERROR(Status
);
2963 Status
= ShellCopySearchAndReplace(CommandLine2
, CommandLine
, PrintBuffSize
, L
"%1", NewScriptFile
->Argv
[1], FALSE
, FALSE
);
2964 ASSERT_EFI_ERROR(Status
);
2966 Status
= ShellCopySearchAndReplace(CommandLine
, CommandLine2
, PrintBuffSize
, L
"%0", NewScriptFile
->Argv
[0], FALSE
, FALSE
);
2967 ASSERT_EFI_ERROR(Status
);
2973 Status
= ShellCopySearchAndReplace(CommandLine2
, CommandLine
, PrintBuffSize
, L
"%1", L
"\"\"", FALSE
, FALSE
);
2974 Status
= ShellCopySearchAndReplace(CommandLine
, CommandLine2
, PrintBuffSize
, L
"%2", L
"\"\"", FALSE
, FALSE
);
2975 Status
= ShellCopySearchAndReplace(CommandLine2
, CommandLine
, PrintBuffSize
, L
"%3", L
"\"\"", FALSE
, FALSE
);
2976 Status
= ShellCopySearchAndReplace(CommandLine
, CommandLine2
, PrintBuffSize
, L
"%4", L
"\"\"", FALSE
, FALSE
);
2977 Status
= ShellCopySearchAndReplace(CommandLine2
, CommandLine
, PrintBuffSize
, L
"%5", L
"\"\"", FALSE
, FALSE
);
2978 Status
= ShellCopySearchAndReplace(CommandLine
, CommandLine2
, PrintBuffSize
, L
"%6", L
"\"\"", FALSE
, FALSE
);
2979 Status
= ShellCopySearchAndReplace(CommandLine2
, CommandLine
, PrintBuffSize
, L
"%7", L
"\"\"", FALSE
, FALSE
);
2980 Status
= ShellCopySearchAndReplace(CommandLine
, CommandLine2
, PrintBuffSize
, L
"%8", L
"\"\"", FALSE
, FALSE
);
2981 Status
= ShellCopySearchAndReplace(CommandLine2
, CommandLine
, PrintBuffSize
, L
"%9", L
"\"\"", FALSE
, FALSE
);
2983 StrnCpyS( CommandLine2
,
2984 PrintBuffSize
/sizeof(CHAR16
),
2986 PrintBuffSize
/sizeof(CHAR16
) - 1
2989 LastCommand
= NewScriptFile
->CurrentCommand
;
2991 for (CommandLine3
= CommandLine2
; CommandLine3
[0] == L
' ' ; CommandLine3
++);
2993 if (CommandLine3
!= NULL
&& CommandLine3
[0] == L
':' ) {
2995 // This line is a goto target / label
2998 if (CommandLine3
!= NULL
&& StrLen(CommandLine3
) > 0) {
2999 if (CommandLine3
[0] == L
'@') {
3001 // We need to save the current echo state
3002 // and disable echo for just this command.
3004 PreCommandEchoState
= ShellCommandGetEchoState();
3005 ShellCommandSetEchoState(FALSE
);
3006 Status
= RunCommand(CommandLine3
+1);
3009 // If command was "@echo -off" or "@echo -on" then don't restore echo state
3011 if (StrCmp (L
"@echo -off", CommandLine3
) != 0 &&
3012 StrCmp (L
"@echo -on", CommandLine3
) != 0) {
3014 // Now restore the pre-'@' echo state.
3016 ShellCommandSetEchoState(PreCommandEchoState
);
3019 if (ShellCommandGetEchoState()) {
3020 CurDir
= ShellInfoObject
.NewEfiShellProtocol
->GetEnv(L
"cwd");
3021 if (CurDir
!= NULL
&& StrLen(CurDir
) > 1) {
3022 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_CURDIR
), ShellInfoObject
.HiiHandle
, CurDir
);
3024 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_SHELL
), ShellInfoObject
.HiiHandle
);
3026 ShellPrintEx(-1, -1, L
"%s\r\n", CommandLine2
);
3028 Status
= RunCommand(CommandLine3
);
3032 if (ShellCommandGetScriptExit()) {
3034 // ShellCommandGetExitCode() always returns a UINT64
3036 UnicodeSPrint(LeString
, sizeof(LeString
), L
"0x%Lx", ShellCommandGetExitCode());
3037 DEBUG_CODE(InternalEfiShellSetEnv(L
"debuglasterror", LeString
, TRUE
););
3038 InternalEfiShellSetEnv(L
"lasterror", LeString
, TRUE
);
3040 ShellCommandRegisterExit(FALSE
, 0);
3041 Status
= EFI_SUCCESS
;
3042 RestoreBufferList(&OldBufferList
);
3045 if (ShellGetExecutionBreakFlag()) {
3046 RestoreBufferList(&OldBufferList
);
3049 if (EFI_ERROR(Status
)) {
3050 RestoreBufferList(&OldBufferList
);
3053 if (ShellCommandGetExit()) {
3054 RestoreBufferList(&OldBufferList
);
3059 // If that commend did not update the CurrentCommand then we need to advance it...
3061 if (LastCommand
== NewScriptFile
->CurrentCommand
) {
3062 NewScriptFile
->CurrentCommand
= (SCRIPT_COMMAND_LIST
*)GetNextNode(&NewScriptFile
->CommandList
, &NewScriptFile
->CurrentCommand
->Link
);
3063 if (!IsNull(&NewScriptFile
->CommandList
, &NewScriptFile
->CurrentCommand
->Link
)) {
3064 NewScriptFile
->CurrentCommand
->Reset
= TRUE
;
3068 NewScriptFile
->CurrentCommand
= (SCRIPT_COMMAND_LIST
*)GetNextNode(&NewScriptFile
->CommandList
, &NewScriptFile
->CurrentCommand
->Link
);
3069 if (!IsNull(&NewScriptFile
->CommandList
, &NewScriptFile
->CurrentCommand
->Link
)) {
3070 NewScriptFile
->CurrentCommand
->Reset
= TRUE
;
3073 RestoreBufferList(&OldBufferList
);
3077 FreePool(CommandLine
);
3078 FreePool(CommandLine2
);
3079 ShellCommandSetNewScript (NULL
);
3082 // Only if this was the last script reset the state.
3084 if (ShellCommandGetCurrentScriptFile()==NULL
) {
3085 ShellCommandSetEchoState(PreScriptEchoState
);
3087 return (EFI_SUCCESS
);
3091 Function to process a NSH script file.
3093 @param[in] ScriptPath Pointer to the script file name (including file system path).
3094 @param[in] Handle the handle of the script file already opened.
3095 @param[in] CmdLine the command line to run.
3096 @param[in] ParamProtocol the shell parameters protocol pointer
3098 @retval EFI_SUCCESS the script completed successfully
3102 IN CONST CHAR16
*ScriptPath
,
3103 IN SHELL_FILE_HANDLE Handle OPTIONAL
,
3104 IN CONST CHAR16
*CmdLine
,
3105 IN EFI_SHELL_PARAMETERS_PROTOCOL
*ParamProtocol
3109 SHELL_FILE_HANDLE FileHandle
;
3113 if (ShellIsFile(ScriptPath
) != EFI_SUCCESS
) {
3114 return (EFI_INVALID_PARAMETER
);
3118 // get the argc and argv updated for scripts
3120 Status
= UpdateArgcArgv(ParamProtocol
, CmdLine
, Script_File_Name
, &Argv
, &Argc
);
3121 if (!EFI_ERROR(Status
)) {
3123 if (Handle
== NULL
) {
3127 Status
= ShellOpenFileByName(ScriptPath
, &FileHandle
, EFI_FILE_MODE_READ
, 0);
3128 if (!EFI_ERROR(Status
)) {
3132 Status
= RunScriptFileHandle(FileHandle
, ScriptPath
);
3135 // now close the file
3137 ShellCloseFile(&FileHandle
);
3140 Status
= RunScriptFileHandle(Handle
, ScriptPath
);
3145 // This is guaranteed to be called after UpdateArgcArgv no matter what else happened.
3146 // This is safe even if the update API failed. In this case, it may be a no-op.
3148 RestoreArgcArgv(ParamProtocol
, &Argv
, &Argc
);
3154 Return the pointer to the first occurrence of any character from a list of characters.
3156 @param[in] String the string to parse
3157 @param[in] CharacterList the list of character to look for
3158 @param[in] EscapeCharacter An escape character to skip
3160 @return the location of the first character in the string
3161 @retval CHAR_NULL no instance of any character in CharacterList was found in String
3165 IN CONST CHAR16
*String
,
3166 IN CONST CHAR16
*CharacterList
,
3167 IN CONST CHAR16 EscapeCharacter
3173 for (WalkStr
= 0; WalkStr
< StrLen(String
); WalkStr
++) {
3174 if (String
[WalkStr
] == EscapeCharacter
) {
3178 for (WalkChar
= 0; WalkChar
< StrLen(CharacterList
); WalkChar
++) {
3179 if (String
[WalkStr
] == CharacterList
[WalkChar
]) {
3180 return (&String
[WalkStr
]);
3184 return (String
+ StrLen(String
));