2 This is THE shell (application)
4 Copyright (c) 2009 - 2017, 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 StartupScriptPath
= StrnCatGrow (&StartupScriptPath
, &Size
, ((FILEPATH_DEVICE_PATH
*)FileDevicePath
)->PathName
, 0);
1173 PathRemoveLastItem (StartupScriptPath
);
1174 StartupScriptPath
= StrnCatGrow (&StartupScriptPath
, &Size
, mStartupScript
, 0);
1178 // Try to find 'Startup.nsh' in the execution path defined by the envrionment variable PATH.
1180 if ((StartupScriptPath
== NULL
) || EFI_ERROR (ShellIsFile (StartupScriptPath
))) {
1181 SHELL_FREE_NON_NULL (StartupScriptPath
);
1182 StartupScriptPath
= ShellFindFilePath (mStartupScript
);
1185 return StartupScriptPath
;
1189 Handles all interaction with the default startup script.
1191 this will check that the correct command line parameters were passed, handle the delay, and then start running the script.
1193 @param ImagePath the path to the image for shell. first place to look for the startup script
1194 @param FilePath the path to the file for shell. second place to look for the startup script.
1196 @retval EFI_SUCCESS the variable is initialized.
1200 IN EFI_DEVICE_PATH_PROTOCOL
*ImagePath
,
1201 IN EFI_DEVICE_PATH_PROTOCOL
*FilePath
1205 EFI_STATUS CalleeStatus
;
1208 CHAR16
*FileStringPath
;
1211 Key
.UnicodeChar
= CHAR_NULL
;
1214 if (!ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.Startup
&& ShellInfoObject
.ShellInitSettings
.FileName
!= NULL
) {
1216 // launch something else instead
1218 NewSize
= StrSize(ShellInfoObject
.ShellInitSettings
.FileName
);
1219 if (ShellInfoObject
.ShellInitSettings
.FileOptions
!= NULL
) {
1220 NewSize
+= StrSize(ShellInfoObject
.ShellInitSettings
.FileOptions
) + sizeof(CHAR16
);
1222 FileStringPath
= AllocateZeroPool(NewSize
);
1223 if (FileStringPath
== NULL
) {
1224 return (EFI_OUT_OF_RESOURCES
);
1226 StrCpyS(FileStringPath
, NewSize
/sizeof(CHAR16
), ShellInfoObject
.ShellInitSettings
.FileName
);
1227 if (ShellInfoObject
.ShellInitSettings
.FileOptions
!= NULL
) {
1228 StrnCatS(FileStringPath
, NewSize
/sizeof(CHAR16
), L
" ", NewSize
/sizeof(CHAR16
) - StrLen(FileStringPath
) -1);
1229 StrnCatS(FileStringPath
, NewSize
/sizeof(CHAR16
), ShellInfoObject
.ShellInitSettings
.FileOptions
, NewSize
/sizeof(CHAR16
) - StrLen(FileStringPath
) -1);
1231 Status
= RunShellCommand(FileStringPath
, &CalleeStatus
);
1232 if (ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.Exit
== TRUE
) {
1233 ShellCommandRegisterExit(gEfiShellProtocol
->BatchIsActive(), (UINT64
)CalleeStatus
);
1235 FreePool(FileStringPath
);
1241 // for shell level 0 we do no scripts
1242 // Without the Startup bit overriding we allow for nostartup to prevent scripts
1244 if ( (PcdGet8(PcdShellSupportLevel
) < 1)
1245 || (ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoStartup
&& !ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.Startup
)
1247 return (EFI_SUCCESS
);
1250 gST
->ConOut
->EnableCursor(gST
->ConOut
, FALSE
);
1252 // print out our warning and see if they press a key
1254 for ( Status
= EFI_UNSUPPORTED
, Delay
= ShellInfoObject
.ShellInitSettings
.Delay
1255 ; Delay
!= 0 && EFI_ERROR(Status
)
1258 ShellPrintHiiEx(0, gST
->ConOut
->Mode
->CursorRow
, NULL
, STRING_TOKEN (STR_SHELL_STARTUP_QUESTION
), ShellInfoObject
.HiiHandle
, Delay
);
1259 gBS
->Stall (1000000);
1260 if (!ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoConsoleIn
) {
1261 Status
= gST
->ConIn
->ReadKeyStroke (gST
->ConIn
, &Key
);
1264 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_CRLF
), ShellInfoObject
.HiiHandle
);
1265 gST
->ConOut
->EnableCursor(gST
->ConOut
, TRUE
);
1270 if (Status
== EFI_SUCCESS
&& Key
.UnicodeChar
== 0 && Key
.ScanCode
== SCAN_ESC
) {
1271 return (EFI_SUCCESS
);
1274 FileStringPath
= LocateStartupScript (ImagePath
, FilePath
);
1275 if (FileStringPath
!= NULL
) {
1276 Status
= RunScriptFile (FileStringPath
, NULL
, L
"", ShellInfoObject
.NewShellParametersProtocol
);
1277 FreePool (FileStringPath
);
1280 // we return success since startup script is not mandatory.
1282 Status
= EFI_SUCCESS
;
1289 Function to perform the shell prompt looping. It will do a single prompt,
1290 dispatch the result, and then return. It is expected that the caller will
1291 call this function in a loop many times.
1294 @retval RETURN_ABORTED
1304 CONST CHAR16
*CurDir
;
1307 LIST_ENTRY OldBufferList
;
1312 // Get screen setting to decide size of the command line buffer
1314 gST
->ConOut
->QueryMode (gST
->ConOut
, gST
->ConOut
->Mode
->Mode
, &Column
, &Row
);
1315 BufferSize
= Column
* Row
* sizeof (CHAR16
);
1316 CmdLine
= AllocateZeroPool (BufferSize
);
1317 if (CmdLine
== NULL
) {
1318 return EFI_OUT_OF_RESOURCES
;
1321 SaveBufferList(&OldBufferList
);
1322 CurDir
= ShellInfoObject
.NewEfiShellProtocol
->GetEnv(L
"cwd");
1327 gST
->ConOut
->SetCursorPosition (gST
->ConOut
, 0, gST
->ConOut
->Mode
->CursorRow
);
1329 if (CurDir
!= NULL
&& StrLen(CurDir
) > 1) {
1330 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_CURDIR
), ShellInfoObject
.HiiHandle
, CurDir
);
1332 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_SHELL
), ShellInfoObject
.HiiHandle
);
1336 // Read a line from the console
1338 Status
= ShellInfoObject
.NewEfiShellProtocol
->ReadFile(ShellInfoObject
.NewShellParametersProtocol
->StdIn
, &BufferSize
, CmdLine
);
1341 // Null terminate the string and parse it
1343 if (!EFI_ERROR (Status
)) {
1345 // Reset the CTRL-C event just before running the command (yes we ignore the return values)
1347 Status
= gBS
->CheckEvent (ShellInfoObject
.NewEfiShellProtocol
->ExecutionBreak
);
1349 CmdLine
[BufferSize
/ sizeof (CHAR16
)] = CHAR_NULL
;
1350 Status
= RunCommand(CmdLine
);
1354 // Done with this command
1356 RestoreBufferList(&OldBufferList
);
1362 Add a buffer to the Buffer To Free List for safely returning buffers to other
1363 places without risking letting them modify internal shell information.
1365 @param Buffer Something to pass to FreePool when the shell is exiting.
1368 AddBufferToFreeList (
1372 BUFFER_LIST
*BufferListEntry
;
1374 if (Buffer
== NULL
) {
1378 BufferListEntry
= AllocateZeroPool (sizeof (BUFFER_LIST
));
1379 if (BufferListEntry
== NULL
) {
1383 BufferListEntry
->Buffer
= Buffer
;
1384 InsertTailList (&ShellInfoObject
.BufferToFreeList
.Link
, &BufferListEntry
->Link
);
1390 Create a new buffer list and stores the old one to OldBufferList
1392 @param OldBufferList The temporary list head used to store the nodes in BufferToFreeList.
1396 OUT LIST_ENTRY
*OldBufferList
1399 CopyMem (OldBufferList
, &ShellInfoObject
.BufferToFreeList
.Link
, sizeof (LIST_ENTRY
));
1400 InitializeListHead (&ShellInfoObject
.BufferToFreeList
.Link
);
1404 Restore previous nodes into BufferToFreeList .
1406 @param OldBufferList The temporary list head used to store the nodes in BufferToFreeList.
1410 IN OUT LIST_ENTRY
*OldBufferList
1413 FreeBufferList (&ShellInfoObject
.BufferToFreeList
);
1414 CopyMem (&ShellInfoObject
.BufferToFreeList
.Link
, OldBufferList
, sizeof (LIST_ENTRY
));
1419 Add a buffer to the Line History List
1421 @param Buffer The line buffer to add.
1424 AddLineToCommandHistory(
1425 IN CONST CHAR16
*Buffer
1429 BUFFER_LIST
*Walker
;
1430 UINT16 MaxHistoryCmdCount
;
1434 MaxHistoryCmdCount
= PcdGet16(PcdShellMaxHistoryCommandCount
);
1436 if (MaxHistoryCmdCount
== 0) {
1441 Node
= AllocateZeroPool(sizeof(BUFFER_LIST
));
1446 Node
->Buffer
= AllocateCopyPool (StrSize (Buffer
), Buffer
);
1447 if (Node
->Buffer
== NULL
) {
1452 for ( Walker
= (BUFFER_LIST
*)GetFirstNode(&ShellInfoObject
.ViewingSettings
.CommandHistory
.Link
)
1453 ; !IsNull(&ShellInfoObject
.ViewingSettings
.CommandHistory
.Link
, &Walker
->Link
)
1454 ; Walker
= (BUFFER_LIST
*)GetNextNode(&ShellInfoObject
.ViewingSettings
.CommandHistory
.Link
, &Walker
->Link
)
1458 if (Count
< MaxHistoryCmdCount
){
1459 InsertTailList(&ShellInfoObject
.ViewingSettings
.CommandHistory
.Link
, &Node
->Link
);
1461 Walker
= (BUFFER_LIST
*)GetFirstNode(&ShellInfoObject
.ViewingSettings
.CommandHistory
.Link
);
1462 RemoveEntryList(&Walker
->Link
);
1463 if (Walker
->Buffer
!= NULL
) {
1464 FreePool(Walker
->Buffer
);
1467 InsertTailList(&ShellInfoObject
.ViewingSettings
.CommandHistory
.Link
, &Node
->Link
);
1472 Checks if a string is an alias for another command. If yes, then it replaces the alias name
1473 with the correct command name.
1475 @param[in, out] CommandString Upon entry the potential alias. Upon return the
1476 command name if it was an alias. If it was not
1477 an alias it will be unchanged. This function may
1478 change the buffer to fit the command name.
1480 @retval EFI_SUCCESS The name was changed.
1481 @retval EFI_SUCCESS The name was not an alias.
1482 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
1486 IN OUT CHAR16
**CommandString
1489 CONST CHAR16
*NewString
;
1491 NewString
= ShellInfoObject
.NewEfiShellProtocol
->GetAlias(*CommandString
, NULL
);
1492 if (NewString
== NULL
) {
1493 return (EFI_SUCCESS
);
1495 FreePool(*CommandString
);
1496 *CommandString
= AllocateCopyPool(StrSize(NewString
), NewString
);
1497 if (*CommandString
== NULL
) {
1498 return (EFI_OUT_OF_RESOURCES
);
1500 return (EFI_SUCCESS
);
1504 This function will eliminate unreplaced (and therefore non-found) environment variables.
1506 @param[in,out] CmdLine The command line to update.
1509 StripUnreplacedEnvironmentVariables(
1510 IN OUT CHAR16
*CmdLine
1513 CHAR16
*FirstPercent
;
1515 CHAR16
*SecondPercent
;
1516 CHAR16
*SecondQuote
;
1517 CHAR16
*CurrentLocator
;
1519 for (CurrentLocator
= CmdLine
; CurrentLocator
!= NULL
; ) {
1520 FirstQuote
= FindNextInstance(CurrentLocator
, L
"\"", TRUE
);
1521 FirstPercent
= FindNextInstance(CurrentLocator
, L
"%", TRUE
);
1522 SecondPercent
= FirstPercent
!=NULL
?FindNextInstance(FirstPercent
+1, L
"%", TRUE
):NULL
;
1523 if (FirstPercent
== NULL
|| SecondPercent
== NULL
) {
1525 // If we ever don't have 2 % we are done.
1530 if (FirstQuote
!= NULL
&& FirstQuote
< FirstPercent
) {
1531 SecondQuote
= FindNextInstance(FirstQuote
+1, L
"\"", TRUE
);
1533 // Quote is first found
1536 if (SecondQuote
< FirstPercent
) {
1538 // restart after the pair of "
1540 CurrentLocator
= SecondQuote
+ 1;
1541 } else /* FirstPercent < SecondQuote */{
1543 // Restart on the first percent
1545 CurrentLocator
= FirstPercent
;
1550 if (FirstQuote
== NULL
|| SecondPercent
< FirstQuote
) {
1551 if (IsValidEnvironmentVariableName(FirstPercent
, SecondPercent
)) {
1553 // We need to remove from FirstPercent to SecondPercent
1555 CopyMem(FirstPercent
, SecondPercent
+ 1, StrSize(SecondPercent
+ 1));
1557 // don't need to update the locator. both % characters are gone.
1560 CurrentLocator
= SecondPercent
+ 1;
1564 CurrentLocator
= FirstQuote
;
1566 return (EFI_SUCCESS
);
1570 Function allocates a new command line and replaces all instances of environment
1571 variable names that are correctly preset to their values.
1573 If the return value is not NULL the memory must be caller freed.
1575 @param[in] OriginalCommandLine The original command line
1577 @retval NULL An error occurred.
1578 @return The new command line with no environment variables present.
1581 ShellConvertVariables (
1582 IN CONST CHAR16
*OriginalCommandLine
1585 CONST CHAR16
*MasterEnvList
;
1587 CHAR16
*NewCommandLine1
;
1588 CHAR16
*NewCommandLine2
;
1592 SCRIPT_FILE
*CurrentScriptFile
;
1593 ALIAS_LIST
*AliasListNode
;
1595 ASSERT(OriginalCommandLine
!= NULL
);
1598 NewSize
= StrSize(OriginalCommandLine
);
1599 CurrentScriptFile
= ShellCommandGetCurrentScriptFile();
1602 ///@todo update this to handle the %0 - %9 for scripting only (borrow from line 1256 area) ? ? ?
1605 // calculate the size required for the post-conversion string...
1607 if (CurrentScriptFile
!= NULL
) {
1608 for (AliasListNode
= (ALIAS_LIST
*)GetFirstNode(&CurrentScriptFile
->SubstList
)
1609 ; !IsNull(&CurrentScriptFile
->SubstList
, &AliasListNode
->Link
)
1610 ; AliasListNode
= (ALIAS_LIST
*)GetNextNode(&CurrentScriptFile
->SubstList
, &AliasListNode
->Link
)
1612 for (Temp
= StrStr(OriginalCommandLine
, AliasListNode
->Alias
)
1614 ; Temp
= StrStr(Temp
+1, AliasListNode
->Alias
)
1617 // we need a preceding and if there is space no ^ preceding (if no space ignore)
1619 if ((((Temp
-OriginalCommandLine
)>2) && *(Temp
-2) != L
'^') || ((Temp
-OriginalCommandLine
)<=2)) {
1620 NewSize
+= StrSize(AliasListNode
->CommandString
);
1626 for (MasterEnvList
= EfiShellGetEnv(NULL
)
1627 ; MasterEnvList
!= NULL
&& *MasterEnvList
!= CHAR_NULL
//&& *(MasterEnvList+1) != CHAR_NULL
1628 ; MasterEnvList
+= StrLen(MasterEnvList
) + 1
1630 if (StrSize(MasterEnvList
) > ItemSize
) {
1631 ItemSize
= StrSize(MasterEnvList
);
1633 for (Temp
= StrStr(OriginalCommandLine
, MasterEnvList
)
1635 ; Temp
= StrStr(Temp
+1, MasterEnvList
)
1638 // we need a preceding and following % and if there is space no ^ preceding (if no space ignore)
1640 if (*(Temp
-1) == L
'%' && *(Temp
+StrLen(MasterEnvList
)) == L
'%' &&
1641 ((((Temp
-OriginalCommandLine
)>2) && *(Temp
-2) != L
'^') || ((Temp
-OriginalCommandLine
)<=2))) {
1642 NewSize
+=StrSize(EfiShellGetEnv(MasterEnvList
));
1648 // now do the replacements...
1650 NewCommandLine1
= AllocateZeroPool (NewSize
);
1651 NewCommandLine2
= AllocateZeroPool(NewSize
);
1652 ItemTemp
= AllocateZeroPool(ItemSize
+(2*sizeof(CHAR16
)));
1653 if (NewCommandLine1
== NULL
|| NewCommandLine2
== NULL
|| ItemTemp
== NULL
) {
1654 SHELL_FREE_NON_NULL(NewCommandLine1
);
1655 SHELL_FREE_NON_NULL(NewCommandLine2
);
1656 SHELL_FREE_NON_NULL(ItemTemp
);
1659 CopyMem (NewCommandLine1
, OriginalCommandLine
, StrSize (OriginalCommandLine
));
1661 for (MasterEnvList
= EfiShellGetEnv(NULL
)
1662 ; MasterEnvList
!= NULL
&& *MasterEnvList
!= CHAR_NULL
1663 ; MasterEnvList
+= StrLen(MasterEnvList
) + 1
1666 ((ItemSize
+(2*sizeof(CHAR16
)))/sizeof(CHAR16
)),
1670 ((ItemSize
+(2*sizeof(CHAR16
)))/sizeof(CHAR16
)),
1674 ((ItemSize
+(2*sizeof(CHAR16
)))/sizeof(CHAR16
)),
1677 ShellCopySearchAndReplace(NewCommandLine1
, NewCommandLine2
, NewSize
, ItemTemp
, EfiShellGetEnv(MasterEnvList
), TRUE
, FALSE
);
1678 StrCpyS(NewCommandLine1
, NewSize
/sizeof(CHAR16
), NewCommandLine2
);
1680 if (CurrentScriptFile
!= NULL
) {
1681 for (AliasListNode
= (ALIAS_LIST
*)GetFirstNode(&CurrentScriptFile
->SubstList
)
1682 ; !IsNull(&CurrentScriptFile
->SubstList
, &AliasListNode
->Link
)
1683 ; AliasListNode
= (ALIAS_LIST
*)GetNextNode(&CurrentScriptFile
->SubstList
, &AliasListNode
->Link
)
1685 ShellCopySearchAndReplace(NewCommandLine1
, NewCommandLine2
, NewSize
, AliasListNode
->Alias
, AliasListNode
->CommandString
, TRUE
, FALSE
);
1686 StrCpyS(NewCommandLine1
, NewSize
/sizeof(CHAR16
), NewCommandLine2
);
1691 // Remove non-existent environment variables
1693 StripUnreplacedEnvironmentVariables(NewCommandLine1
);
1696 // Now cleanup any straggler intentionally ignored "%" characters
1698 ShellCopySearchAndReplace(NewCommandLine1
, NewCommandLine2
, NewSize
, L
"^%", L
"%", TRUE
, FALSE
);
1699 StrCpyS(NewCommandLine1
, NewSize
/sizeof(CHAR16
), NewCommandLine2
);
1701 FreePool(NewCommandLine2
);
1704 return (NewCommandLine1
);
1708 Internal function to run a command line with pipe usage.
1710 @param[in] CmdLine The pointer to the command line.
1711 @param[in] StdIn The pointer to the Standard input.
1712 @param[in] StdOut The pointer to the Standard output.
1714 @retval EFI_SUCCESS The split command is executed successfully.
1715 @retval other Some error occurs when executing the split command.
1719 IN CONST CHAR16
*CmdLine
,
1720 IN SHELL_FILE_HANDLE StdIn
,
1721 IN SHELL_FILE_HANDLE StdOut
1725 CHAR16
*NextCommandLine
;
1726 CHAR16
*OurCommandLine
;
1730 SHELL_FILE_HANDLE TempFileHandle
;
1733 ASSERT(StdOut
== NULL
);
1735 ASSERT(StrStr(CmdLine
, L
"|") != NULL
);
1737 Status
= EFI_SUCCESS
;
1738 NextCommandLine
= NULL
;
1739 OurCommandLine
= NULL
;
1743 NextCommandLine
= StrnCatGrow(&NextCommandLine
, &Size1
, StrStr(CmdLine
, L
"|")+1, 0);
1744 OurCommandLine
= StrnCatGrow(&OurCommandLine
, &Size2
, CmdLine
, StrStr(CmdLine
, L
"|") - CmdLine
);
1746 if (NextCommandLine
== NULL
|| OurCommandLine
== NULL
) {
1747 SHELL_FREE_NON_NULL(OurCommandLine
);
1748 SHELL_FREE_NON_NULL(NextCommandLine
);
1749 return (EFI_OUT_OF_RESOURCES
);
1750 } else if (StrStr(OurCommandLine
, L
"|") != NULL
|| Size1
== 0 || Size2
== 0) {
1751 SHELL_FREE_NON_NULL(OurCommandLine
);
1752 SHELL_FREE_NON_NULL(NextCommandLine
);
1753 return (EFI_INVALID_PARAMETER
);
1754 } else if (NextCommandLine
[0] == L
'a' &&
1755 (NextCommandLine
[1] == L
' ' || NextCommandLine
[1] == CHAR_NULL
)
1757 CopyMem(NextCommandLine
, NextCommandLine
+1, StrSize(NextCommandLine
) - sizeof(NextCommandLine
[0]));
1758 while (NextCommandLine
[0] == L
' ') {
1759 CopyMem(NextCommandLine
, NextCommandLine
+1, StrSize(NextCommandLine
) - sizeof(NextCommandLine
[0]));
1761 if (NextCommandLine
[0] == CHAR_NULL
) {
1762 SHELL_FREE_NON_NULL(OurCommandLine
);
1763 SHELL_FREE_NON_NULL(NextCommandLine
);
1764 return (EFI_INVALID_PARAMETER
);
1773 // make a SPLIT_LIST item and add to list
1775 Split
= AllocateZeroPool(sizeof(SPLIT_LIST
));
1776 if (Split
== NULL
) {
1777 return EFI_OUT_OF_RESOURCES
;
1779 Split
->SplitStdIn
= StdIn
;
1780 Split
->SplitStdOut
= ConvertEfiFileProtocolToShellHandle(CreateFileInterfaceMem(Unicode
), NULL
);
1781 ASSERT(Split
->SplitStdOut
!= NULL
);
1782 InsertHeadList(&ShellInfoObject
.SplitList
.Link
, &Split
->Link
);
1784 Status
= RunCommand(OurCommandLine
);
1787 // move the output from the first to the in to the second.
1789 TempFileHandle
= Split
->SplitStdOut
;
1790 if (Split
->SplitStdIn
== StdIn
) {
1791 Split
->SplitStdOut
= NULL
;
1793 Split
->SplitStdOut
= Split
->SplitStdIn
;
1795 Split
->SplitStdIn
= TempFileHandle
;
1796 ShellInfoObject
.NewEfiShellProtocol
->SetFilePosition (Split
->SplitStdIn
, 0);
1798 if (!EFI_ERROR(Status
)) {
1799 Status
= RunCommand(NextCommandLine
);
1803 // remove the top level from the ScriptList
1805 ASSERT((SPLIT_LIST
*)GetFirstNode(&ShellInfoObject
.SplitList
.Link
) == Split
);
1806 RemoveEntryList(&Split
->Link
);
1809 // Note that the original StdIn is now the StdOut...
1811 if (Split
->SplitStdOut
!= NULL
) {
1812 ShellInfoObject
.NewEfiShellProtocol
->CloseFile (Split
->SplitStdOut
);
1814 if (Split
->SplitStdIn
!= NULL
) {
1815 ShellInfoObject
.NewEfiShellProtocol
->CloseFile (Split
->SplitStdIn
);
1819 FreePool(NextCommandLine
);
1820 FreePool(OurCommandLine
);
1826 Take the original command line, substitute any variables, free
1827 the original string, return the modified copy.
1829 @param[in] CmdLine pointer to the command line to update.
1831 @retval EFI_SUCCESS the function was successful.
1832 @retval EFI_OUT_OF_RESOURCES a memory allocation failed.
1835 ShellSubstituteVariables(
1840 NewCmdLine
= ShellConvertVariables(*CmdLine
);
1841 SHELL_FREE_NON_NULL(*CmdLine
);
1842 if (NewCmdLine
== NULL
) {
1843 return (EFI_OUT_OF_RESOURCES
);
1845 *CmdLine
= NewCmdLine
;
1846 return (EFI_SUCCESS
);
1850 Take the original command line, substitute any alias in the first group of space delimited characters, free
1851 the original string, return the modified copy.
1853 @param[in] CmdLine pointer to the command line to update.
1855 @retval EFI_SUCCESS the function was successful.
1856 @retval EFI_OUT_OF_RESOURCES a memory allocation failed.
1859 ShellSubstituteAliases(
1864 CHAR16
*CommandName
;
1866 UINTN PostAliasSize
;
1867 ASSERT(CmdLine
!= NULL
);
1868 ASSERT(*CmdLine
!= NULL
);
1872 if (StrStr((*CmdLine
), L
" ") == NULL
){
1873 StrnCatGrow(&CommandName
, NULL
, (*CmdLine
), 0);
1875 StrnCatGrow(&CommandName
, NULL
, (*CmdLine
), StrStr((*CmdLine
), L
" ") - (*CmdLine
));
1879 // This cannot happen 'inline' since the CmdLine can need extra space.
1882 if (!ShellCommandIsCommandOnList(CommandName
)) {
1884 // Convert via alias
1886 Status
= ShellConvertAlias(&CommandName
);
1887 if (EFI_ERROR(Status
)){
1891 NewCmdLine
= StrnCatGrow(&NewCmdLine
, &PostAliasSize
, CommandName
, 0);
1892 if (NewCmdLine
== NULL
) {
1893 SHELL_FREE_NON_NULL(CommandName
);
1894 SHELL_FREE_NON_NULL(*CmdLine
);
1895 return (EFI_OUT_OF_RESOURCES
);
1897 NewCmdLine
= StrnCatGrow(&NewCmdLine
, &PostAliasSize
, StrStr((*CmdLine
), L
" "), 0);
1898 if (NewCmdLine
== NULL
) {
1899 SHELL_FREE_NON_NULL(CommandName
);
1900 SHELL_FREE_NON_NULL(*CmdLine
);
1901 return (EFI_OUT_OF_RESOURCES
);
1904 NewCmdLine
= StrnCatGrow(&NewCmdLine
, NULL
, (*CmdLine
), 0);
1907 SHELL_FREE_NON_NULL(*CmdLine
);
1908 SHELL_FREE_NON_NULL(CommandName
);
1911 // re-assign the passed in double pointer to point to our newly allocated buffer
1913 *CmdLine
= NewCmdLine
;
1915 return (EFI_SUCCESS
);
1919 Takes the Argv[0] part of the command line and determine the meaning of it.
1921 @param[in] CmdName pointer to the command line to update.
1923 @retval Internal_Command The name is an internal command.
1924 @retval File_Sys_Change the name is a file system change.
1925 @retval Script_File_Name the name is a NSH script file.
1926 @retval Unknown_Invalid the name is unknown.
1927 @retval Efi_Application the name is an application (.EFI).
1929 SHELL_OPERATION_TYPES
1931 IN CONST CHAR16
*CmdName
1934 CHAR16
* FileWithPath
;
1935 CONST CHAR16
* TempLocation
;
1936 CONST CHAR16
* TempLocation2
;
1938 FileWithPath
= NULL
;
1940 // test for an internal command.
1942 if (ShellCommandIsCommandOnList(CmdName
)) {
1943 return (Internal_Command
);
1947 // Test for file system change request. anything ending with first : and cant have spaces.
1949 if (CmdName
[(StrLen(CmdName
)-1)] == L
':') {
1950 if ( StrStr(CmdName
, L
" ") != NULL
1951 || StrLen(StrStr(CmdName
, L
":")) > 1
1953 return (Unknown_Invalid
);
1955 return (File_Sys_Change
);
1961 if ((FileWithPath
= ShellFindFilePathEx(CmdName
, mExecutableExtensions
)) != NULL
) {
1963 // See if that file has a script file extension
1965 if (StrLen(FileWithPath
) > 4) {
1966 TempLocation
= FileWithPath
+StrLen(FileWithPath
)-4;
1967 TempLocation2
= mScriptExtension
;
1968 if (StringNoCaseCompare((VOID
*)(&TempLocation
), (VOID
*)(&TempLocation2
)) == 0) {
1969 SHELL_FREE_NON_NULL(FileWithPath
);
1970 return (Script_File_Name
);
1975 // Was a file, but not a script. we treat this as an application.
1977 SHELL_FREE_NON_NULL(FileWithPath
);
1978 return (Efi_Application
);
1981 SHELL_FREE_NON_NULL(FileWithPath
);
1983 // No clue what this is... return invalid flag...
1985 return (Unknown_Invalid
);
1989 Determine if the first item in a command line is valid.
1991 @param[in] CmdLine The command line to parse.
1993 @retval EFI_SUCCESS The item is valid.
1994 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
1995 @retval EFI_NOT_FOUND The operation type is unknown or invalid.
1999 IN CONST CHAR16
*CmdLine
2003 CHAR16
*FirstParameter
;
2009 Temp
= StrnCatGrow(&Temp
, NULL
, CmdLine
, 0);
2011 return (EFI_OUT_OF_RESOURCES
);
2014 FirstParameter
= StrStr(Temp
, L
"|");
2015 if (FirstParameter
!= NULL
) {
2016 *FirstParameter
= CHAR_NULL
;
2019 FirstParameter
= NULL
;
2022 // Process the command line
2024 Status
= ProcessCommandLineToFinal(&Temp
);
2026 if (!EFI_ERROR(Status
)) {
2027 FirstParameter
= AllocateZeroPool(StrSize(CmdLine
));
2028 if (FirstParameter
== NULL
) {
2029 SHELL_FREE_NON_NULL(Temp
);
2030 return (EFI_OUT_OF_RESOURCES
);
2032 TempWalker
= (CHAR16
*)Temp
;
2033 if (!EFI_ERROR(GetNextParameter(&TempWalker
, &FirstParameter
, StrSize(CmdLine
), TRUE
))) {
2034 if (GetOperationType(FirstParameter
) == Unknown_Invalid
) {
2035 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_NOT_FOUND
), ShellInfoObject
.HiiHandle
, FirstParameter
);
2036 SetLastError(SHELL_NOT_FOUND
);
2037 Status
= EFI_NOT_FOUND
;
2042 SHELL_FREE_NON_NULL(Temp
);
2043 SHELL_FREE_NON_NULL(FirstParameter
);
2048 Determine if a command line contains with a split contains only valid commands.
2050 @param[in] CmdLine The command line to parse.
2052 @retval EFI_SUCCESS CmdLine has only valid commands, application, or has no split.
2053 @retval EFI_ABORTED CmdLine has at least one invalid command or application.
2057 IN CONST CHAR16
*CmdLine
2060 CONST CHAR16
*TempSpot
;
2064 // If this was the only item, then get out
2066 if (!ContainsSplit(CmdLine
)) {
2067 return (EFI_SUCCESS
);
2071 // Verify up to the pipe or end character
2073 Status
= IsValidSplit(CmdLine
);
2074 if (EFI_ERROR(Status
)) {
2079 // recurse to verify the next item
2081 TempSpot
= FindFirstCharacter(CmdLine
, L
"|", L
'^') + 1;
2082 if (*TempSpot
== L
'a' &&
2083 (*(TempSpot
+ 1) == L
' ' || *(TempSpot
+ 1) == CHAR_NULL
)
2085 // If it's an ASCII pipe '|a'
2089 return (VerifySplit(TempSpot
));
2093 Process a split based operation.
2095 @param[in] CmdLine pointer to the command line to process
2097 @retval EFI_SUCCESS The operation was successful
2098 @return an error occurred.
2101 ProcessNewSplitCommandLine(
2102 IN CONST CHAR16
*CmdLine
2108 Status
= VerifySplit(CmdLine
);
2109 if (EFI_ERROR(Status
)) {
2116 // are we in an existing split???
2118 if (!IsListEmpty(&ShellInfoObject
.SplitList
.Link
)) {
2119 Split
= (SPLIT_LIST
*)GetFirstNode(&ShellInfoObject
.SplitList
.Link
);
2122 if (Split
== NULL
) {
2123 Status
= RunSplitCommand(CmdLine
, NULL
, NULL
);
2125 Status
= RunSplitCommand(CmdLine
, Split
->SplitStdIn
, Split
->SplitStdOut
);
2127 if (EFI_ERROR(Status
)) {
2128 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_INVALID_SPLIT
), ShellInfoObject
.HiiHandle
, CmdLine
);
2134 Handle a request to change the current file system.
2136 @param[in] CmdLine The passed in command line.
2138 @retval EFI_SUCCESS The operation was successful.
2142 IN CONST CHAR16
*CmdLine
2146 Status
= EFI_SUCCESS
;
2149 // make sure we are the right operation
2151 ASSERT(CmdLine
[(StrLen(CmdLine
)-1)] == L
':' && StrStr(CmdLine
, L
" ") == NULL
);
2154 // Call the protocol API to do the work
2156 Status
= ShellInfoObject
.NewEfiShellProtocol
->SetCurDir(NULL
, CmdLine
);
2159 // Report any errors
2161 if (EFI_ERROR(Status
)) {
2162 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_INVALID_MAPPING
), ShellInfoObject
.HiiHandle
, CmdLine
);
2169 Reprocess the command line to direct all -? to the help command.
2171 if found, will add "help" as argv[0], and move the rest later.
2173 @param[in,out] CmdLine pointer to the command line to update
2177 IN OUT CHAR16
**CmdLine
2180 CHAR16
*CurrentParameter
;
2182 CHAR16
*NewCommandLine
;
2184 UINTN NewCmdLineSize
;
2186 Status
= EFI_SUCCESS
;
2188 CurrentParameter
= AllocateZeroPool(StrSize(*CmdLine
));
2189 if (CurrentParameter
== NULL
) {
2190 return (EFI_OUT_OF_RESOURCES
);
2194 while(Walker
!= NULL
&& *Walker
!= CHAR_NULL
) {
2195 if (!EFI_ERROR(GetNextParameter(&Walker
, &CurrentParameter
, StrSize(*CmdLine
), TRUE
))) {
2196 if (StrStr(CurrentParameter
, L
"-?") == CurrentParameter
) {
2197 CurrentParameter
[0] = L
' ';
2198 CurrentParameter
[1] = L
' ';
2199 NewCmdLineSize
= StrSize(L
"help ") + StrSize(*CmdLine
);
2200 NewCommandLine
= AllocateZeroPool(NewCmdLineSize
);
2201 if (NewCommandLine
== NULL
) {
2202 Status
= EFI_OUT_OF_RESOURCES
;
2207 // We know the space is sufficient since we just calculated it.
2209 StrnCpyS(NewCommandLine
, NewCmdLineSize
/sizeof(CHAR16
), L
"help ", 5);
2210 StrnCatS(NewCommandLine
, NewCmdLineSize
/sizeof(CHAR16
), *CmdLine
, StrLen(*CmdLine
));
2211 SHELL_FREE_NON_NULL(*CmdLine
);
2212 *CmdLine
= NewCommandLine
;
2218 SHELL_FREE_NON_NULL(CurrentParameter
);
2224 Function to update the shell variable "lasterror".
2226 @param[in] ErrorCode the error code to put into lasterror.
2230 IN CONST SHELL_STATUS ErrorCode
2233 CHAR16 LeString
[19];
2234 if (sizeof(EFI_STATUS
) == sizeof(UINT64
)) {
2235 UnicodeSPrint(LeString
, sizeof(LeString
), L
"0x%Lx", ErrorCode
);
2237 UnicodeSPrint(LeString
, sizeof(LeString
), L
"0x%x", ErrorCode
);
2239 DEBUG_CODE(InternalEfiShellSetEnv(L
"debuglasterror", LeString
, TRUE
););
2240 InternalEfiShellSetEnv(L
"lasterror", LeString
, TRUE
);
2242 return (EFI_SUCCESS
);
2246 Converts the command line to it's post-processed form. this replaces variables and alias' per UEFI Shell spec.
2248 @param[in,out] CmdLine pointer to the command line to update
2250 @retval EFI_SUCCESS The operation was successful
2251 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
2252 @return some other error occurred
2255 ProcessCommandLineToFinal(
2256 IN OUT CHAR16
**CmdLine
2260 TrimSpaces(CmdLine
);
2262 Status
= ShellSubstituteAliases(CmdLine
);
2263 if (EFI_ERROR(Status
)) {
2267 TrimSpaces(CmdLine
);
2269 Status
= ShellSubstituteVariables(CmdLine
);
2270 if (EFI_ERROR(Status
)) {
2273 ASSERT (*CmdLine
!= NULL
);
2275 TrimSpaces(CmdLine
);
2278 // update for help parsing
2280 if (StrStr(*CmdLine
, L
"?") != NULL
) {
2282 // This may do nothing if the ? does not indicate help.
2283 // Save all the details for in the API below.
2285 Status
= DoHelpUpdate(CmdLine
);
2288 TrimSpaces(CmdLine
);
2290 return (EFI_SUCCESS
);
2294 Run an internal shell command.
2296 This API will update the shell's environment since these commands are libraries.
2298 @param[in] CmdLine the command line to run.
2299 @param[in] FirstParameter the first parameter on the command line
2300 @param[in] ParamProtocol the shell parameters protocol pointer
2301 @param[out] CommandStatus the status from the command line.
2303 @retval EFI_SUCCESS The command was completed.
2304 @retval EFI_ABORTED The command's operation was aborted.
2308 IN CONST CHAR16
*CmdLine
,
2309 IN CHAR16
*FirstParameter
,
2310 IN EFI_SHELL_PARAMETERS_PROTOCOL
*ParamProtocol
,
2311 OUT EFI_STATUS
*CommandStatus
2317 SHELL_STATUS CommandReturnedStatus
;
2322 NewCmdLine
= AllocateCopyPool (StrSize (CmdLine
), CmdLine
);
2323 if (NewCmdLine
== NULL
) {
2324 return EFI_OUT_OF_RESOURCES
;
2327 for (Walker
= NewCmdLine
; Walker
!= NULL
&& *Walker
!= CHAR_NULL
; Walker
++) {
2328 if (*Walker
== L
'^' && *(Walker
+1) == L
'#') {
2329 CopyMem(Walker
, Walker
+1, StrSize(Walker
) - sizeof(Walker
[0]));
2334 // get the argc and argv updated for internal commands
2336 Status
= UpdateArgcArgv(ParamProtocol
, NewCmdLine
, Internal_Command
, &Argv
, &Argc
);
2337 if (!EFI_ERROR(Status
)) {
2339 // Run the internal command.
2341 Status
= ShellCommandRunCommandHandler(FirstParameter
, &CommandReturnedStatus
, &LastError
);
2343 if (!EFI_ERROR(Status
)) {
2344 if (CommandStatus
!= NULL
) {
2345 if (CommandReturnedStatus
!= SHELL_SUCCESS
) {
2346 *CommandStatus
= (EFI_STATUS
)(CommandReturnedStatus
| MAX_BIT
);
2348 *CommandStatus
= EFI_SUCCESS
;
2353 // Update last error status.
2354 // some commands do not update last error.
2357 SetLastError(CommandReturnedStatus
);
2361 // Pass thru the exitcode from the app.
2363 if (ShellCommandGetExit()) {
2365 // An Exit was requested ("exit" command), pass its value up.
2367 Status
= CommandReturnedStatus
;
2368 } else if (CommandReturnedStatus
!= SHELL_SUCCESS
&& IsScriptOnlyCommand(FirstParameter
)) {
2370 // Always abort when a script only command fails for any reason
2372 Status
= EFI_ABORTED
;
2373 } else if (ShellCommandGetCurrentScriptFile() != NULL
&& CommandReturnedStatus
== SHELL_ABORTED
) {
2375 // Abort when in a script and a command aborted
2377 Status
= EFI_ABORTED
;
2383 // This is guaranteed to be called after UpdateArgcArgv no matter what else happened.
2384 // This is safe even if the update API failed. In this case, it may be a no-op.
2386 RestoreArgcArgv(ParamProtocol
, &Argv
, &Argc
);
2389 // If a script is running and the command is not a script only command, then
2390 // change return value to success so the script won't halt (unless aborted).
2392 // Script only commands have to be able halt the script since the script will
2393 // not operate if they are failing.
2395 if ( ShellCommandGetCurrentScriptFile() != NULL
2396 && !IsScriptOnlyCommand(FirstParameter
)
2397 && Status
!= EFI_ABORTED
2399 Status
= EFI_SUCCESS
;
2402 FreePool (NewCmdLine
);
2407 Function to run the command or file.
2409 @param[in] Type the type of operation being run.
2410 @param[in] CmdLine the command line to run.
2411 @param[in] FirstParameter the first parameter on the command line
2412 @param[in] ParamProtocol the shell parameters protocol pointer
2413 @param[out] CommandStatus the status from the command line.
2415 @retval EFI_SUCCESS The command was completed.
2416 @retval EFI_ABORTED The command's operation was aborted.
2420 IN SHELL_OPERATION_TYPES Type
,
2421 IN CONST CHAR16
*CmdLine
,
2422 IN CHAR16
*FirstParameter
,
2423 IN EFI_SHELL_PARAMETERS_PROTOCOL
*ParamProtocol
,
2424 OUT EFI_STATUS
*CommandStatus
2428 EFI_STATUS StartStatus
;
2429 CHAR16
*CommandWithPath
;
2430 EFI_DEVICE_PATH_PROTOCOL
*DevPath
;
2431 SHELL_STATUS CalleeExitStatus
;
2433 Status
= EFI_SUCCESS
;
2434 CommandWithPath
= NULL
;
2436 CalleeExitStatus
= SHELL_INVALID_PARAMETER
;
2439 case Internal_Command
:
2440 Status
= RunInternalCommand(CmdLine
, FirstParameter
, ParamProtocol
, CommandStatus
);
2442 case Script_File_Name
:
2443 case Efi_Application
:
2445 // Process a fully qualified path
2447 if (StrStr(FirstParameter
, L
":") != NULL
) {
2448 ASSERT (CommandWithPath
== NULL
);
2449 if (ShellIsFile(FirstParameter
) == EFI_SUCCESS
) {
2450 CommandWithPath
= StrnCatGrow(&CommandWithPath
, NULL
, FirstParameter
, 0);
2455 // Process a relative path and also check in the path environment variable
2457 if (CommandWithPath
== NULL
) {
2458 CommandWithPath
= ShellFindFilePathEx(FirstParameter
, mExecutableExtensions
);
2462 // This should be impossible now.
2464 ASSERT(CommandWithPath
!= NULL
);
2467 // Make sure that path is not just a directory (or not found)
2469 if (!EFI_ERROR(ShellIsDirectory(CommandWithPath
))) {
2470 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_NOT_FOUND
), ShellInfoObject
.HiiHandle
, FirstParameter
);
2471 SetLastError(SHELL_NOT_FOUND
);
2474 case Script_File_Name
:
2475 Status
= RunScriptFile (CommandWithPath
, NULL
, CmdLine
, ParamProtocol
);
2477 case Efi_Application
:
2479 // Get the device path of the application image
2481 DevPath
= ShellInfoObject
.NewEfiShellProtocol
->GetDevicePathFromFilePath(CommandWithPath
);
2482 if (DevPath
== NULL
){
2483 Status
= EFI_OUT_OF_RESOURCES
;
2488 // Execute the device path
2490 Status
= InternalShellExecuteDevicePath(
2498 SHELL_FREE_NON_NULL(DevPath
);
2500 if(EFI_ERROR (Status
)) {
2501 CalleeExitStatus
= (SHELL_STATUS
) (Status
& (~MAX_BIT
));
2503 CalleeExitStatus
= (SHELL_STATUS
) StartStatus
;
2506 if (CommandStatus
!= NULL
) {
2507 *CommandStatus
= CalleeExitStatus
;
2511 // Update last error status.
2513 // Status is an EFI_STATUS. Clear top bit to convert to SHELL_STATUS
2514 SetLastError(CalleeExitStatus
);
2530 SHELL_FREE_NON_NULL(CommandWithPath
);
2536 Function to setup StdIn, StdErr, StdOut, and then run the command or file.
2538 @param[in] Type the type of operation being run.
2539 @param[in] CmdLine the command line to run.
2540 @param[in] FirstParameter the first parameter on the command line.
2541 @param[in] ParamProtocol the shell parameters protocol pointer
2542 @param[out] CommandStatus the status from the command line.
2544 @retval EFI_SUCCESS The command was completed.
2545 @retval EFI_ABORTED The command's operation was aborted.
2548 SetupAndRunCommandOrFile(
2549 IN SHELL_OPERATION_TYPES Type
,
2551 IN CHAR16
*FirstParameter
,
2552 IN EFI_SHELL_PARAMETERS_PROTOCOL
*ParamProtocol
,
2553 OUT EFI_STATUS
*CommandStatus
2557 SHELL_FILE_HANDLE OriginalStdIn
;
2558 SHELL_FILE_HANDLE OriginalStdOut
;
2559 SHELL_FILE_HANDLE OriginalStdErr
;
2560 SYSTEM_TABLE_INFO OriginalSystemTableInfo
;
2561 CONST SCRIPT_FILE
*ConstScriptFile
;
2564 // Update the StdIn, StdOut, and StdErr for redirection to environment variables, files, etc... unicode and ASCII
2566 Status
= UpdateStdInStdOutStdErr(ParamProtocol
, CmdLine
, &OriginalStdIn
, &OriginalStdOut
, &OriginalStdErr
, &OriginalSystemTableInfo
);
2569 // The StdIn, StdOut, and StdErr are set up.
2570 // Now run the command, script, or application
2572 if (!EFI_ERROR(Status
)) {
2573 TrimSpaces(&CmdLine
);
2574 Status
= RunCommandOrFile(Type
, CmdLine
, FirstParameter
, ParamProtocol
, CommandStatus
);
2580 if (EFI_ERROR(Status
)) {
2581 ConstScriptFile
= ShellCommandGetCurrentScriptFile();
2582 if (ConstScriptFile
== NULL
|| ConstScriptFile
->CurrentCommand
== NULL
) {
2583 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_ERROR
), ShellInfoObject
.HiiHandle
, (VOID
*)(Status
));
2585 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_ERROR_SCRIPT
), ShellInfoObject
.HiiHandle
, (VOID
*)(Status
), ConstScriptFile
->CurrentCommand
->Line
);
2590 // put back the original StdIn, StdOut, and StdErr
2592 RestoreStdInStdOutStdErr(ParamProtocol
, &OriginalStdIn
, &OriginalStdOut
, &OriginalStdErr
, &OriginalSystemTableInfo
);
2598 Function will process and run a command line.
2600 This will determine if the command line represents an internal shell
2601 command or dispatch an external application.
2603 @param[in] CmdLine The command line to parse.
2604 @param[out] CommandStatus The status from the command line.
2606 @retval EFI_SUCCESS The command was completed.
2607 @retval EFI_ABORTED The command's operation was aborted.
2611 IN CONST CHAR16
*CmdLine
,
2612 OUT EFI_STATUS
*CommandStatus
2616 CHAR16
*CleanOriginal
;
2617 CHAR16
*FirstParameter
;
2619 SHELL_OPERATION_TYPES Type
;
2620 CONST CHAR16
*CurDir
;
2622 ASSERT(CmdLine
!= NULL
);
2623 if (StrLen(CmdLine
) == 0) {
2624 return (EFI_SUCCESS
);
2627 Status
= EFI_SUCCESS
;
2628 CleanOriginal
= NULL
;
2630 CleanOriginal
= StrnCatGrow(&CleanOriginal
, NULL
, CmdLine
, 0);
2631 if (CleanOriginal
== NULL
) {
2632 return (EFI_OUT_OF_RESOURCES
);
2635 TrimSpaces(&CleanOriginal
);
2638 // NULL out comments (leveraged from RunScriptFileHandle() ).
2639 // The # character on a line is used to denote that all characters on the same line
2640 // and to the right of the # are to be ignored by the shell.
2641 // Afterwards, again remove spaces, in case any were between the last command-parameter and '#'.
2643 for (TempWalker
= CleanOriginal
; TempWalker
!= NULL
&& *TempWalker
!= CHAR_NULL
; TempWalker
++) {
2644 if (*TempWalker
== L
'^') {
2645 if (*(TempWalker
+ 1) == L
'#') {
2648 } else if (*TempWalker
== L
'#') {
2649 *TempWalker
= CHAR_NULL
;
2653 TrimSpaces(&CleanOriginal
);
2656 // Handle case that passed in command line is just 1 or more " " characters.
2658 if (StrLen (CleanOriginal
) == 0) {
2659 SHELL_FREE_NON_NULL(CleanOriginal
);
2660 return (EFI_SUCCESS
);
2663 Status
= ProcessCommandLineToFinal(&CleanOriginal
);
2664 if (EFI_ERROR(Status
)) {
2665 SHELL_FREE_NON_NULL(CleanOriginal
);
2670 // We don't do normal processing with a split command line (output from one command input to another)
2672 if (ContainsSplit(CleanOriginal
)) {
2673 Status
= ProcessNewSplitCommandLine(CleanOriginal
);
2674 SHELL_FREE_NON_NULL(CleanOriginal
);
2679 // We need the first parameter information so we can determine the operation type
2681 FirstParameter
= AllocateZeroPool(StrSize(CleanOriginal
));
2682 if (FirstParameter
== NULL
) {
2683 SHELL_FREE_NON_NULL(CleanOriginal
);
2684 return (EFI_OUT_OF_RESOURCES
);
2686 TempWalker
= CleanOriginal
;
2687 if (!EFI_ERROR(GetNextParameter(&TempWalker
, &FirstParameter
, StrSize(CleanOriginal
), TRUE
))) {
2689 // Depending on the first parameter we change the behavior
2691 switch (Type
= GetOperationType(FirstParameter
)) {
2692 case File_Sys_Change
:
2693 Status
= ChangeMappedDrive (FirstParameter
);
2695 case Internal_Command
:
2696 case Script_File_Name
:
2697 case Efi_Application
:
2698 Status
= SetupAndRunCommandOrFile(Type
, CleanOriginal
, FirstParameter
, ShellInfoObject
.NewShellParametersProtocol
, CommandStatus
);
2702 // Whatever was typed, it was invalid.
2704 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_NOT_FOUND
), ShellInfoObject
.HiiHandle
, FirstParameter
);
2705 SetLastError(SHELL_NOT_FOUND
);
2709 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_NOT_FOUND
), ShellInfoObject
.HiiHandle
, FirstParameter
);
2710 SetLastError(SHELL_NOT_FOUND
);
2713 // Check whether the current file system still exists. If not exist, we need update "cwd" and gShellCurMapping.
2715 CurDir
= EfiShellGetCurDir (NULL
);
2716 if (CurDir
!= NULL
) {
2717 if (EFI_ERROR(ShellFileExists (CurDir
))) {
2719 // EfiShellSetCurDir() cannot set current directory to NULL.
2720 // EfiShellSetEnv() is not allowed to set the "cwd" variable.
2721 // Only InternalEfiShellSetEnv () is allowed setting the "cwd" variable.
2723 InternalEfiShellSetEnv (L
"cwd", NULL
, TRUE
);
2724 gShellCurMapping
= NULL
;
2728 SHELL_FREE_NON_NULL(CleanOriginal
);
2729 SHELL_FREE_NON_NULL(FirstParameter
);
2735 Function will process and run a command line.
2737 This will determine if the command line represents an internal shell
2738 command or dispatch an external application.
2740 @param[in] CmdLine The command line to parse.
2742 @retval EFI_SUCCESS The command was completed.
2743 @retval EFI_ABORTED The command's operation was aborted.
2747 IN CONST CHAR16
*CmdLine
2750 return (RunShellCommand(CmdLine
, NULL
));
2754 STATIC CONST UINT16 InvalidChars
[] = {L
'*', L
'?', L
'<', L
'>', L
'\\', L
'/', L
'\"', 0x0001, 0x0002};
2756 Function determines if the CommandName COULD be a valid command. It does not determine whether
2757 this is a valid command. It only checks for invalid characters.
2759 @param[in] CommandName The name to check
2761 @retval TRUE CommandName could be a command name
2762 @retval FALSE CommandName could not be a valid command name
2766 IN CONST CHAR16
*CommandName
2770 if (CommandName
== NULL
) {
2775 ; Count
< sizeof(InvalidChars
) / sizeof(InvalidChars
[0])
2778 if (ScanMem16(CommandName
, StrSize(CommandName
), InvalidChars
[Count
]) != NULL
) {
2786 Function to process a NSH script file via SHELL_FILE_HANDLE.
2788 @param[in] Handle The handle to the already opened file.
2789 @param[in] Name The name of the script file.
2791 @retval EFI_SUCCESS the script completed successfully
2794 RunScriptFileHandle (
2795 IN SHELL_FILE_HANDLE Handle
,
2796 IN CONST CHAR16
*Name
2800 SCRIPT_FILE
*NewScriptFile
;
2802 UINTN PrintBuffSize
;
2803 CHAR16
*CommandLine
;
2804 CHAR16
*CommandLine2
;
2805 CHAR16
*CommandLine3
;
2806 SCRIPT_COMMAND_LIST
*LastCommand
;
2808 BOOLEAN PreScriptEchoState
;
2809 BOOLEAN PreCommandEchoState
;
2810 CONST CHAR16
*CurDir
;
2812 CHAR16 LeString
[50];
2813 LIST_ENTRY OldBufferList
;
2815 ASSERT(!ShellCommandGetScriptExit());
2817 PreScriptEchoState
= ShellCommandGetEchoState();
2818 PrintBuffSize
= PcdGet16(PcdShellPrintBufferSize
);
2820 NewScriptFile
= (SCRIPT_FILE
*)AllocateZeroPool(sizeof(SCRIPT_FILE
));
2821 if (NewScriptFile
== NULL
) {
2822 return (EFI_OUT_OF_RESOURCES
);
2828 ASSERT(NewScriptFile
->ScriptName
== NULL
);
2829 NewScriptFile
->ScriptName
= StrnCatGrow(&NewScriptFile
->ScriptName
, NULL
, Name
, 0);
2830 if (NewScriptFile
->ScriptName
== NULL
) {
2831 DeleteScriptFileStruct(NewScriptFile
);
2832 return (EFI_OUT_OF_RESOURCES
);
2836 // Save the parameters (used to replace %0 to %9 later on)
2838 NewScriptFile
->Argc
= ShellInfoObject
.NewShellParametersProtocol
->Argc
;
2839 if (NewScriptFile
->Argc
!= 0) {
2840 NewScriptFile
->Argv
= (CHAR16
**)AllocateZeroPool(NewScriptFile
->Argc
* sizeof(CHAR16
*));
2841 if (NewScriptFile
->Argv
== NULL
) {
2842 DeleteScriptFileStruct(NewScriptFile
);
2843 return (EFI_OUT_OF_RESOURCES
);
2845 for (LoopVar
= 0 ; LoopVar
< 10 && LoopVar
< NewScriptFile
->Argc
; LoopVar
++) {
2846 ASSERT(NewScriptFile
->Argv
[LoopVar
] == NULL
);
2847 NewScriptFile
->Argv
[LoopVar
] = StrnCatGrow(&NewScriptFile
->Argv
[LoopVar
], NULL
, ShellInfoObject
.NewShellParametersProtocol
->Argv
[LoopVar
], 0);
2848 if (NewScriptFile
->Argv
[LoopVar
] == NULL
) {
2849 DeleteScriptFileStruct(NewScriptFile
);
2850 return (EFI_OUT_OF_RESOURCES
);
2854 NewScriptFile
->Argv
= NULL
;
2857 InitializeListHead(&NewScriptFile
->CommandList
);
2858 InitializeListHead(&NewScriptFile
->SubstList
);
2861 // Now build the list of all script commands.
2864 while(!ShellFileHandleEof(Handle
)) {
2865 CommandLine
= ShellFileHandleReturnLine(Handle
, &Ascii
);
2867 if (CommandLine
== NULL
|| StrLen(CommandLine
) == 0 || CommandLine
[0] == '#') {
2868 SHELL_FREE_NON_NULL(CommandLine
);
2871 NewScriptFile
->CurrentCommand
= AllocateZeroPool(sizeof(SCRIPT_COMMAND_LIST
));
2872 if (NewScriptFile
->CurrentCommand
== NULL
) {
2873 SHELL_FREE_NON_NULL(CommandLine
);
2874 DeleteScriptFileStruct(NewScriptFile
);
2875 return (EFI_OUT_OF_RESOURCES
);
2878 NewScriptFile
->CurrentCommand
->Cl
= CommandLine
;
2879 NewScriptFile
->CurrentCommand
->Data
= NULL
;
2880 NewScriptFile
->CurrentCommand
->Line
= LineCount
;
2882 InsertTailList(&NewScriptFile
->CommandList
, &NewScriptFile
->CurrentCommand
->Link
);
2886 // Add this as the topmost script file
2888 ShellCommandSetNewScript (NewScriptFile
);
2891 // Now enumerate through the commands and run each one.
2893 CommandLine
= AllocateZeroPool(PrintBuffSize
);
2894 if (CommandLine
== NULL
) {
2895 DeleteScriptFileStruct(NewScriptFile
);
2896 return (EFI_OUT_OF_RESOURCES
);
2898 CommandLine2
= AllocateZeroPool(PrintBuffSize
);
2899 if (CommandLine2
== NULL
) {
2900 FreePool(CommandLine
);
2901 DeleteScriptFileStruct(NewScriptFile
);
2902 return (EFI_OUT_OF_RESOURCES
);
2905 for ( NewScriptFile
->CurrentCommand
= (SCRIPT_COMMAND_LIST
*)GetFirstNode(&NewScriptFile
->CommandList
)
2906 ; !IsNull(&NewScriptFile
->CommandList
, &NewScriptFile
->CurrentCommand
->Link
)
2907 ; // conditional increment in the body of the loop
2909 ASSERT(CommandLine2
!= NULL
);
2910 StrnCpyS( CommandLine2
,
2911 PrintBuffSize
/sizeof(CHAR16
),
2912 NewScriptFile
->CurrentCommand
->Cl
,
2913 PrintBuffSize
/sizeof(CHAR16
) - 1
2916 SaveBufferList(&OldBufferList
);
2919 // NULL out comments
2921 for (CommandLine3
= CommandLine2
; CommandLine3
!= NULL
&& *CommandLine3
!= CHAR_NULL
; CommandLine3
++) {
2922 if (*CommandLine3
== L
'^') {
2923 if ( *(CommandLine3
+1) == L
':') {
2924 CopyMem(CommandLine3
, CommandLine3
+1, StrSize(CommandLine3
) - sizeof(CommandLine3
[0]));
2925 } else if (*(CommandLine3
+1) == L
'#') {
2928 } else if (*CommandLine3
== L
'#') {
2929 *CommandLine3
= CHAR_NULL
;
2933 if (CommandLine2
!= NULL
&& StrLen(CommandLine2
) >= 1) {
2935 // Due to variability in starting the find and replace action we need to have both buffers the same.
2937 StrnCpyS( CommandLine
,
2938 PrintBuffSize
/sizeof(CHAR16
),
2940 PrintBuffSize
/sizeof(CHAR16
) - 1
2944 // Remove the %0 to %9 from the command line (if we have some arguments)
2946 if (NewScriptFile
->Argv
!= NULL
) {
2947 switch (NewScriptFile
->Argc
) {
2949 Status
= ShellCopySearchAndReplace(CommandLine2
, CommandLine
, PrintBuffSize
, L
"%9", NewScriptFile
->Argv
[9], FALSE
, FALSE
);
2950 ASSERT_EFI_ERROR(Status
);
2952 Status
= ShellCopySearchAndReplace(CommandLine
, CommandLine2
, PrintBuffSize
, L
"%8", NewScriptFile
->Argv
[8], FALSE
, FALSE
);
2953 ASSERT_EFI_ERROR(Status
);
2955 Status
= ShellCopySearchAndReplace(CommandLine2
, CommandLine
, PrintBuffSize
, L
"%7", NewScriptFile
->Argv
[7], FALSE
, FALSE
);
2956 ASSERT_EFI_ERROR(Status
);
2958 Status
= ShellCopySearchAndReplace(CommandLine
, CommandLine2
, PrintBuffSize
, L
"%6", NewScriptFile
->Argv
[6], FALSE
, FALSE
);
2959 ASSERT_EFI_ERROR(Status
);
2961 Status
= ShellCopySearchAndReplace(CommandLine2
, CommandLine
, PrintBuffSize
, L
"%5", NewScriptFile
->Argv
[5], FALSE
, FALSE
);
2962 ASSERT_EFI_ERROR(Status
);
2964 Status
= ShellCopySearchAndReplace(CommandLine
, CommandLine2
, PrintBuffSize
, L
"%4", NewScriptFile
->Argv
[4], FALSE
, FALSE
);
2965 ASSERT_EFI_ERROR(Status
);
2967 Status
= ShellCopySearchAndReplace(CommandLine2
, CommandLine
, PrintBuffSize
, L
"%3", NewScriptFile
->Argv
[3], FALSE
, FALSE
);
2968 ASSERT_EFI_ERROR(Status
);
2970 Status
= ShellCopySearchAndReplace(CommandLine
, CommandLine2
, PrintBuffSize
, L
"%2", NewScriptFile
->Argv
[2], FALSE
, FALSE
);
2971 ASSERT_EFI_ERROR(Status
);
2973 Status
= ShellCopySearchAndReplace(CommandLine2
, CommandLine
, PrintBuffSize
, L
"%1", NewScriptFile
->Argv
[1], FALSE
, FALSE
);
2974 ASSERT_EFI_ERROR(Status
);
2976 Status
= ShellCopySearchAndReplace(CommandLine
, CommandLine2
, PrintBuffSize
, L
"%0", NewScriptFile
->Argv
[0], FALSE
, FALSE
);
2977 ASSERT_EFI_ERROR(Status
);
2983 Status
= ShellCopySearchAndReplace(CommandLine2
, CommandLine
, PrintBuffSize
, L
"%1", L
"\"\"", FALSE
, FALSE
);
2984 Status
= ShellCopySearchAndReplace(CommandLine
, CommandLine2
, PrintBuffSize
, L
"%2", L
"\"\"", FALSE
, FALSE
);
2985 Status
= ShellCopySearchAndReplace(CommandLine2
, CommandLine
, PrintBuffSize
, L
"%3", L
"\"\"", FALSE
, FALSE
);
2986 Status
= ShellCopySearchAndReplace(CommandLine
, CommandLine2
, PrintBuffSize
, L
"%4", L
"\"\"", FALSE
, FALSE
);
2987 Status
= ShellCopySearchAndReplace(CommandLine2
, CommandLine
, PrintBuffSize
, L
"%5", L
"\"\"", FALSE
, FALSE
);
2988 Status
= ShellCopySearchAndReplace(CommandLine
, CommandLine2
, PrintBuffSize
, L
"%6", L
"\"\"", FALSE
, FALSE
);
2989 Status
= ShellCopySearchAndReplace(CommandLine2
, CommandLine
, PrintBuffSize
, L
"%7", L
"\"\"", FALSE
, FALSE
);
2990 Status
= ShellCopySearchAndReplace(CommandLine
, CommandLine2
, PrintBuffSize
, L
"%8", L
"\"\"", FALSE
, FALSE
);
2991 Status
= ShellCopySearchAndReplace(CommandLine2
, CommandLine
, PrintBuffSize
, L
"%9", L
"\"\"", FALSE
, FALSE
);
2993 StrnCpyS( CommandLine2
,
2994 PrintBuffSize
/sizeof(CHAR16
),
2996 PrintBuffSize
/sizeof(CHAR16
) - 1
2999 LastCommand
= NewScriptFile
->CurrentCommand
;
3001 for (CommandLine3
= CommandLine2
; CommandLine3
[0] == L
' ' ; CommandLine3
++);
3003 if (CommandLine3
!= NULL
&& CommandLine3
[0] == L
':' ) {
3005 // This line is a goto target / label
3008 if (CommandLine3
!= NULL
&& StrLen(CommandLine3
) > 0) {
3009 if (CommandLine3
[0] == L
'@') {
3011 // We need to save the current echo state
3012 // and disable echo for just this command.
3014 PreCommandEchoState
= ShellCommandGetEchoState();
3015 ShellCommandSetEchoState(FALSE
);
3016 Status
= RunCommand(CommandLine3
+1);
3019 // If command was "@echo -off" or "@echo -on" then don't restore echo state
3021 if (StrCmp (L
"@echo -off", CommandLine3
) != 0 &&
3022 StrCmp (L
"@echo -on", CommandLine3
) != 0) {
3024 // Now restore the pre-'@' echo state.
3026 ShellCommandSetEchoState(PreCommandEchoState
);
3029 if (ShellCommandGetEchoState()) {
3030 CurDir
= ShellInfoObject
.NewEfiShellProtocol
->GetEnv(L
"cwd");
3031 if (CurDir
!= NULL
&& StrLen(CurDir
) > 1) {
3032 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_CURDIR
), ShellInfoObject
.HiiHandle
, CurDir
);
3034 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_SHELL
), ShellInfoObject
.HiiHandle
);
3036 ShellPrintEx(-1, -1, L
"%s\r\n", CommandLine2
);
3038 Status
= RunCommand(CommandLine3
);
3042 if (ShellCommandGetScriptExit()) {
3044 // ShellCommandGetExitCode() always returns a UINT64
3046 UnicodeSPrint(LeString
, sizeof(LeString
), L
"0x%Lx", ShellCommandGetExitCode());
3047 DEBUG_CODE(InternalEfiShellSetEnv(L
"debuglasterror", LeString
, TRUE
););
3048 InternalEfiShellSetEnv(L
"lasterror", LeString
, TRUE
);
3050 ShellCommandRegisterExit(FALSE
, 0);
3051 Status
= EFI_SUCCESS
;
3052 RestoreBufferList(&OldBufferList
);
3055 if (ShellGetExecutionBreakFlag()) {
3056 RestoreBufferList(&OldBufferList
);
3059 if (EFI_ERROR(Status
)) {
3060 RestoreBufferList(&OldBufferList
);
3063 if (ShellCommandGetExit()) {
3064 RestoreBufferList(&OldBufferList
);
3069 // If that commend did not update the CurrentCommand then we need to advance it...
3071 if (LastCommand
== NewScriptFile
->CurrentCommand
) {
3072 NewScriptFile
->CurrentCommand
= (SCRIPT_COMMAND_LIST
*)GetNextNode(&NewScriptFile
->CommandList
, &NewScriptFile
->CurrentCommand
->Link
);
3073 if (!IsNull(&NewScriptFile
->CommandList
, &NewScriptFile
->CurrentCommand
->Link
)) {
3074 NewScriptFile
->CurrentCommand
->Reset
= TRUE
;
3078 NewScriptFile
->CurrentCommand
= (SCRIPT_COMMAND_LIST
*)GetNextNode(&NewScriptFile
->CommandList
, &NewScriptFile
->CurrentCommand
->Link
);
3079 if (!IsNull(&NewScriptFile
->CommandList
, &NewScriptFile
->CurrentCommand
->Link
)) {
3080 NewScriptFile
->CurrentCommand
->Reset
= TRUE
;
3083 RestoreBufferList(&OldBufferList
);
3087 FreePool(CommandLine
);
3088 FreePool(CommandLine2
);
3089 ShellCommandSetNewScript (NULL
);
3092 // Only if this was the last script reset the state.
3094 if (ShellCommandGetCurrentScriptFile()==NULL
) {
3095 ShellCommandSetEchoState(PreScriptEchoState
);
3097 return (EFI_SUCCESS
);
3101 Function to process a NSH script file.
3103 @param[in] ScriptPath Pointer to the script file name (including file system path).
3104 @param[in] Handle the handle of the script file already opened.
3105 @param[in] CmdLine the command line to run.
3106 @param[in] ParamProtocol the shell parameters protocol pointer
3108 @retval EFI_SUCCESS the script completed successfully
3112 IN CONST CHAR16
*ScriptPath
,
3113 IN SHELL_FILE_HANDLE Handle OPTIONAL
,
3114 IN CONST CHAR16
*CmdLine
,
3115 IN EFI_SHELL_PARAMETERS_PROTOCOL
*ParamProtocol
3119 SHELL_FILE_HANDLE FileHandle
;
3123 if (ShellIsFile(ScriptPath
) != EFI_SUCCESS
) {
3124 return (EFI_INVALID_PARAMETER
);
3128 // get the argc and argv updated for scripts
3130 Status
= UpdateArgcArgv(ParamProtocol
, CmdLine
, Script_File_Name
, &Argv
, &Argc
);
3131 if (!EFI_ERROR(Status
)) {
3133 if (Handle
== NULL
) {
3137 Status
= ShellOpenFileByName(ScriptPath
, &FileHandle
, EFI_FILE_MODE_READ
, 0);
3138 if (!EFI_ERROR(Status
)) {
3142 Status
= RunScriptFileHandle(FileHandle
, ScriptPath
);
3145 // now close the file
3147 ShellCloseFile(&FileHandle
);
3150 Status
= RunScriptFileHandle(Handle
, ScriptPath
);
3155 // This is guaranteed to be called after UpdateArgcArgv no matter what else happened.
3156 // This is safe even if the update API failed. In this case, it may be a no-op.
3158 RestoreArgcArgv(ParamProtocol
, &Argv
, &Argc
);
3164 Return the pointer to the first occurrence of any character from a list of characters.
3166 @param[in] String the string to parse
3167 @param[in] CharacterList the list of character to look for
3168 @param[in] EscapeCharacter An escape character to skip
3170 @return the location of the first character in the string
3171 @retval CHAR_NULL no instance of any character in CharacterList was found in String
3175 IN CONST CHAR16
*String
,
3176 IN CONST CHAR16
*CharacterList
,
3177 IN CONST CHAR16 EscapeCharacter
3183 for (WalkStr
= 0; WalkStr
< StrLen(String
); WalkStr
++) {
3184 if (String
[WalkStr
] == EscapeCharacter
) {
3188 for (WalkChar
= 0; WalkChar
< StrLen(CharacterList
); WalkChar
++) {
3189 if (String
[WalkStr
] == CharacterList
[WalkChar
]) {
3190 return (&String
[WalkStr
]);
3194 return (String
+ StrLen(String
));