2 This is THE shell (application)
4 Copyright (c) 2009 - 2016, Intel Corporation. All rights reserved.<BR>
5 (C) Copyright 2013-2014 Hewlett-Packard Development Company, L.P.<BR>
6 This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
19 // Initialize the global structure
21 SHELL_INFO ShellInfoObject
= {
57 {{NULL
, NULL
}, NULL
, NULL
},
58 {{NULL
, NULL
}, NULL
, NULL
},
70 STATIC CONST CHAR16 mScriptExtension
[] = L
".NSH";
71 STATIC CONST CHAR16 mExecutableExtensions
[] = L
".NSH;.EFI";
72 STATIC CONST CHAR16 mStartupScript
[] = L
"startup.nsh";
73 CONST CHAR16 mNoNestingEnvVarName
[] = L
"nonesting";
74 CONST CHAR16 mNoNestingTrue
[] = L
"True";
75 CONST CHAR16 mNoNestingFalse
[] = L
"False";
78 Cleans off leading and trailing spaces and tabs.
80 @param[in] String pointer to the string to trim them off.
88 ASSERT(String
!= NULL
);
89 ASSERT(*String
!= NULL
);
91 // Remove any spaces and tabs at the beginning of the (*String).
93 while (((*String
)[0] == L
' ') || ((*String
)[0] == L
'\t')) {
94 CopyMem((*String
), (*String
)+1, StrSize((*String
)) - sizeof((*String
)[0]));
98 // Remove any spaces and tabs at the end of the (*String).
100 while ((StrLen (*String
) > 0) && (((*String
)[StrLen((*String
))-1] == L
' ') || ((*String
)[StrLen((*String
))-1] == L
'\t'))) {
101 (*String
)[StrLen((*String
))-1] = CHAR_NULL
;
104 return (EFI_SUCCESS
);
108 Parse for the next instance of one string within another string. Can optionally make sure that
109 the string was not escaped (^ character) per the shell specification.
111 @param[in] SourceString The string to search within
112 @param[in] FindString The string to look for
113 @param[in] CheckForEscapeCharacter TRUE to skip escaped instances of FinfString, otherwise will return even escaped instances
118 IN CONST CHAR16
*SourceString
,
119 IN CONST CHAR16
*FindString
,
120 IN CONST BOOLEAN CheckForEscapeCharacter
124 if (SourceString
== NULL
) {
127 Temp
= StrStr(SourceString
, FindString
);
130 // If nothing found, or we don't care about escape characters
132 if (Temp
== NULL
|| !CheckForEscapeCharacter
) {
137 // If we found an escaped character, try again on the remainder of the string
139 if ((Temp
> (SourceString
)) && *(Temp
-1) == L
'^') {
140 return FindNextInstance(Temp
+1, FindString
, CheckForEscapeCharacter
);
144 // we found the right character
150 Check whether the string between a pair of % is a valid environment variable name.
152 @param[in] BeginPercent pointer to the first percent.
153 @param[in] EndPercent pointer to the last percent.
155 @retval TRUE is a valid environment variable name.
156 @retval FALSE is NOT a valid environment variable name.
159 IsValidEnvironmentVariableName(
160 IN CONST CHAR16
*BeginPercent
,
161 IN CONST CHAR16
*EndPercent
164 CONST CHAR16
*Walker
;
168 ASSERT (BeginPercent
!= NULL
);
169 ASSERT (EndPercent
!= NULL
);
170 ASSERT (BeginPercent
< EndPercent
);
172 if ((BeginPercent
+ 1) == EndPercent
) {
176 for (Walker
= BeginPercent
+ 1; Walker
< EndPercent
; Walker
++) {
178 (*Walker
>= L
'0' && *Walker
<= L
'9') ||
179 (*Walker
>= L
'A' && *Walker
<= L
'Z') ||
180 (*Walker
>= L
'a' && *Walker
<= L
'z') ||
183 if (Walker
== BeginPercent
+ 1 && (*Walker
>= L
'0' && *Walker
<= L
'9')) {
197 Determine if a command line contains a split operation
199 @param[in] CmdLine The command line to parse.
201 @retval TRUE CmdLine has a valid split.
202 @retval FALSE CmdLine does not have a valid split.
207 IN CONST CHAR16
*CmdLine
210 CONST CHAR16
*TempSpot
;
211 CONST CHAR16
*FirstQuote
;
212 CONST CHAR16
*SecondQuote
;
214 FirstQuote
= FindNextInstance (CmdLine
, L
"\"", TRUE
);
216 TempSpot
= FindFirstCharacter(CmdLine
, L
"|", L
'^');
218 if (FirstQuote
== NULL
||
220 TempSpot
== CHAR_NULL
||
221 FirstQuote
> TempSpot
223 return (BOOLEAN
) ((TempSpot
!= NULL
) && (*TempSpot
!= CHAR_NULL
));
226 while ((TempSpot
!= NULL
) && (*TempSpot
!= CHAR_NULL
)) {
227 if (FirstQuote
== NULL
|| FirstQuote
> TempSpot
) {
230 SecondQuote
= FindNextInstance (FirstQuote
+ 1, L
"\"", TRUE
);
231 if (SecondQuote
== NULL
) {
234 if (SecondQuote
< TempSpot
) {
235 FirstQuote
= FindNextInstance (SecondQuote
+ 1, L
"\"", TRUE
);
238 FirstQuote
= FindNextInstance (SecondQuote
+ 1, L
"\"", TRUE
);
239 TempSpot
= FindFirstCharacter(TempSpot
+ 1, L
"|", L
'^');
244 return (BOOLEAN
) ((TempSpot
!= NULL
) && (*TempSpot
!= CHAR_NULL
));
248 Function to start monitoring for CTRL-S using SimpleTextInputEx. This
249 feature's enabled state was not known when the shell initially launched.
251 @retval EFI_SUCCESS The feature is enabled.
252 @retval EFI_OUT_OF_RESOURCES There is not enough memory available.
256 InternalEfiShellStartCtrlSMonitor(
260 EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL
*SimpleEx
;
261 EFI_KEY_DATA KeyData
;
264 Status
= gBS
->OpenProtocol(
265 gST
->ConsoleInHandle
,
266 &gEfiSimpleTextInputExProtocolGuid
,
270 EFI_OPEN_PROTOCOL_GET_PROTOCOL
);
271 if (EFI_ERROR(Status
)) {
276 STRING_TOKEN (STR_SHELL_NO_IN_EX
),
277 ShellInfoObject
.HiiHandle
);
278 return (EFI_SUCCESS
);
281 KeyData
.KeyState
.KeyToggleState
= 0;
282 KeyData
.Key
.ScanCode
= 0;
283 KeyData
.KeyState
.KeyShiftState
= EFI_SHIFT_STATE_VALID
|EFI_LEFT_CONTROL_PRESSED
;
284 KeyData
.Key
.UnicodeChar
= L
's';
286 Status
= SimpleEx
->RegisterKeyNotify(
289 NotificationFunction
,
290 &ShellInfoObject
.CtrlSNotifyHandle1
);
292 KeyData
.KeyState
.KeyShiftState
= EFI_SHIFT_STATE_VALID
|EFI_RIGHT_CONTROL_PRESSED
;
293 if (!EFI_ERROR(Status
)) {
294 Status
= SimpleEx
->RegisterKeyNotify(
297 NotificationFunction
,
298 &ShellInfoObject
.CtrlSNotifyHandle2
);
300 KeyData
.KeyState
.KeyShiftState
= EFI_SHIFT_STATE_VALID
|EFI_LEFT_CONTROL_PRESSED
;
301 KeyData
.Key
.UnicodeChar
= 19;
303 if (!EFI_ERROR(Status
)) {
304 Status
= SimpleEx
->RegisterKeyNotify(
307 NotificationFunction
,
308 &ShellInfoObject
.CtrlSNotifyHandle3
);
310 KeyData
.KeyState
.KeyShiftState
= EFI_SHIFT_STATE_VALID
|EFI_RIGHT_CONTROL_PRESSED
;
311 if (!EFI_ERROR(Status
)) {
312 Status
= SimpleEx
->RegisterKeyNotify(
315 NotificationFunction
,
316 &ShellInfoObject
.CtrlSNotifyHandle4
);
324 The entry point for the application.
326 @param[in] ImageHandle The firmware allocated handle for the EFI image.
327 @param[in] SystemTable A pointer to the EFI System Table.
329 @retval EFI_SUCCESS The entry point is executed successfully.
330 @retval other Some error occurs when executing this entry point.
336 IN EFI_HANDLE ImageHandle
,
337 IN EFI_SYSTEM_TABLE
*SystemTable
343 EFI_HANDLE ConInHandle
;
344 EFI_SIMPLE_TEXT_INPUT_PROTOCOL
*OldConIn
;
346 if (PcdGet8(PcdShellSupportLevel
) > 3) {
347 return (EFI_UNSUPPORTED
);
353 Status
= gST
->ConOut
->ClearScreen(gST
->ConOut
);
354 if (EFI_ERROR(Status
)) {
359 // Populate the global structure from PCDs
361 ShellInfoObject
.ImageDevPath
= NULL
;
362 ShellInfoObject
.FileDevPath
= NULL
;
363 ShellInfoObject
.PageBreakEnabled
= PcdGetBool(PcdShellPageBreakDefault
);
364 ShellInfoObject
.ViewingSettings
.InsertMode
= PcdGetBool(PcdShellInsertModeDefault
);
365 ShellInfoObject
.LogScreenCount
= PcdGet8 (PcdShellScreenLogCount
);
368 // verify we dont allow for spec violation
370 ASSERT(ShellInfoObject
.LogScreenCount
>= 3);
373 // Initialize the LIST ENTRY objects...
375 InitializeListHead(&ShellInfoObject
.BufferToFreeList
.Link
);
376 InitializeListHead(&ShellInfoObject
.ViewingSettings
.CommandHistory
.Link
);
377 InitializeListHead(&ShellInfoObject
.SplitList
.Link
);
380 // Check PCDs for optional features that are not implemented yet.
382 if ( PcdGetBool(PcdShellSupportOldProtocols
)
383 || !FeaturePcdGet(PcdShellRequireHiiPlatform
)
384 || FeaturePcdGet(PcdShellSupportFrameworkHii
)
386 return (EFI_UNSUPPORTED
);
390 // turn off the watchdog timer
392 gBS
->SetWatchdogTimer (0, 0, 0, NULL
);
395 // install our console logger. This will keep a log of the output for back-browsing
397 Status
= ConsoleLoggerInstall(ShellInfoObject
.LogScreenCount
, &ShellInfoObject
.ConsoleInfo
);
398 if (!EFI_ERROR(Status
)) {
400 // Enable the cursor to be visible
402 gST
->ConOut
->EnableCursor (gST
->ConOut
, TRUE
);
405 // If supporting EFI 1.1 we need to install HII protocol
406 // only do this if PcdShellRequireHiiPlatform == FALSE
408 // remove EFI_UNSUPPORTED check above when complete.
409 ///@todo add support for Framework HII
412 // install our (solitary) HII package
414 ShellInfoObject
.HiiHandle
= HiiAddPackages (&gEfiCallerIdGuid
, gImageHandle
, ShellStrings
, NULL
);
415 if (ShellInfoObject
.HiiHandle
== NULL
) {
416 if (PcdGetBool(PcdShellSupportFrameworkHii
)) {
417 ///@todo Add our package into Framework HII
419 if (ShellInfoObject
.HiiHandle
== NULL
) {
420 Status
= EFI_NOT_STARTED
;
426 // create and install the EfiShellParametersProtocol
428 Status
= CreatePopulateInstallShellParametersProtocol(&ShellInfoObject
.NewShellParametersProtocol
, &ShellInfoObject
.RootShellInstance
);
429 ASSERT_EFI_ERROR(Status
);
430 ASSERT(ShellInfoObject
.NewShellParametersProtocol
!= NULL
);
433 // create and install the EfiShellProtocol
435 Status
= CreatePopulateInstallShellProtocol(&ShellInfoObject
.NewEfiShellProtocol
);
436 ASSERT_EFI_ERROR(Status
);
437 ASSERT(ShellInfoObject
.NewEfiShellProtocol
!= NULL
);
440 // Now initialize the shell library (it requires Shell Parameters protocol)
442 Status
= ShellInitialize();
443 ASSERT_EFI_ERROR(Status
);
445 Status
= CommandInit();
446 ASSERT_EFI_ERROR(Status
);
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;
625 // Reset the CTRL-C event (yes we ignore the return values)
627 Status
= gBS
->CheckEvent (ShellInfoObject
.NewEfiShellProtocol
->ExecutionBreak
);
632 Status
= DoShellPrompt();
633 } while (!ShellCommandGetExit());
635 if (OldConIn
!= NULL
&& ConInHandle
!= NULL
) {
636 CloseSimpleTextInOnFile (gST
->ConIn
);
637 gST
->ConIn
= OldConIn
;
638 gST
->ConsoleInHandle
= ConInHandle
;
645 // uninstall protocols / free memory / etc...
647 if (ShellInfoObject
.UserBreakTimer
!= NULL
) {
648 gBS
->CloseEvent(ShellInfoObject
.UserBreakTimer
);
649 DEBUG_CODE(ShellInfoObject
.UserBreakTimer
= NULL
;);
651 if (ShellInfoObject
.ImageDevPath
!= NULL
) {
652 FreePool(ShellInfoObject
.ImageDevPath
);
653 DEBUG_CODE(ShellInfoObject
.ImageDevPath
= NULL
;);
655 if (ShellInfoObject
.FileDevPath
!= NULL
) {
656 FreePool(ShellInfoObject
.FileDevPath
);
657 DEBUG_CODE(ShellInfoObject
.FileDevPath
= NULL
;);
659 if (ShellInfoObject
.NewShellParametersProtocol
!= NULL
) {
660 CleanUpShellParametersProtocol(ShellInfoObject
.NewShellParametersProtocol
);
661 DEBUG_CODE(ShellInfoObject
.NewShellParametersProtocol
= NULL
;);
663 if (ShellInfoObject
.NewEfiShellProtocol
!= NULL
){
664 if (ShellInfoObject
.NewEfiShellProtocol
->IsRootShell()){
665 InternalEfiShellSetEnv(L
"cwd", NULL
, TRUE
);
667 CleanUpShellProtocol(ShellInfoObject
.NewEfiShellProtocol
);
668 DEBUG_CODE(ShellInfoObject
.NewEfiShellProtocol
= NULL
;);
671 if (!IsListEmpty(&ShellInfoObject
.BufferToFreeList
.Link
)){
672 FreeBufferList(&ShellInfoObject
.BufferToFreeList
);
675 if (!IsListEmpty(&ShellInfoObject
.SplitList
.Link
)){
676 ASSERT(FALSE
); ///@todo finish this de-allocation.
679 if (ShellInfoObject
.ShellInitSettings
.FileName
!= NULL
) {
680 FreePool(ShellInfoObject
.ShellInitSettings
.FileName
);
681 DEBUG_CODE(ShellInfoObject
.ShellInitSettings
.FileName
= NULL
;);
684 if (ShellInfoObject
.ShellInitSettings
.FileOptions
!= NULL
) {
685 FreePool(ShellInfoObject
.ShellInitSettings
.FileOptions
);
686 DEBUG_CODE(ShellInfoObject
.ShellInitSettings
.FileOptions
= NULL
;);
689 if (ShellInfoObject
.HiiHandle
!= NULL
) {
690 HiiRemovePackages(ShellInfoObject
.HiiHandle
);
691 DEBUG_CODE(ShellInfoObject
.HiiHandle
= NULL
;);
694 if (!IsListEmpty(&ShellInfoObject
.ViewingSettings
.CommandHistory
.Link
)){
695 FreeBufferList(&ShellInfoObject
.ViewingSettings
.CommandHistory
);
698 ASSERT(ShellInfoObject
.ConsoleInfo
!= NULL
);
699 if (ShellInfoObject
.ConsoleInfo
!= NULL
) {
700 ConsoleLoggerUninstall(ShellInfoObject
.ConsoleInfo
);
701 FreePool(ShellInfoObject
.ConsoleInfo
);
702 DEBUG_CODE(ShellInfoObject
.ConsoleInfo
= NULL
;);
705 if (ShellCommandGetExit()) {
706 return ((EFI_STATUS
)ShellCommandGetExitCode());
712 Sets all the alias' that were registered with the ShellCommandLib library.
714 @retval EFI_SUCCESS all init commands were run successfully.
722 CONST ALIAS_LIST
*List
;
726 // Get all the commands we want to run
728 List
= ShellCommandGetInitAliasList();
731 // for each command in the List
733 for ( Node
= (ALIAS_LIST
*)GetFirstNode(&List
->Link
)
734 ; !IsNull (&List
->Link
, &Node
->Link
)
735 ; Node
= (ALIAS_LIST
*)GetNextNode(&List
->Link
, &Node
->Link
)
738 // install the alias'
740 Status
= InternalSetAlias(Node
->CommandString
, Node
->Alias
, TRUE
);
741 ASSERT_EFI_ERROR(Status
);
743 return (EFI_SUCCESS
);
747 Internal function to determine if 2 command names are really the same.
749 @param[in] Command1 The pointer to the first command name.
750 @param[in] Command2 The pointer to the second command name.
752 @retval TRUE The 2 command names are the same.
753 @retval FALSE The 2 command names are not the same.
758 IN CONST CHAR16
*Command1
,
759 IN CONST CHAR16
*Command2
762 if (StringNoCaseCompare(&Command1
, &Command2
) == 0) {
769 Internal function to determine if a command is a script only command.
771 @param[in] CommandName The pointer to the command name.
773 @retval TRUE The command is a script only command.
774 @retval FALSE The command is not a script only command.
779 IN CONST CHAR16
*CommandName
782 if (IsCommand(CommandName
, L
"for")
783 ||IsCommand(CommandName
, L
"endfor")
784 ||IsCommand(CommandName
, L
"if")
785 ||IsCommand(CommandName
, L
"else")
786 ||IsCommand(CommandName
, L
"endif")
787 ||IsCommand(CommandName
, L
"goto")) {
794 This function will populate the 2 device path protocol parameters based on the
795 global gImageHandle. The DevPath will point to the device path for the handle that has
796 loaded image protocol installed on it. The FilePath will point to the device path
797 for the file that was loaded.
799 @param[in, out] DevPath On a successful return the device path to the loaded image.
800 @param[in, out] FilePath On a successful return the device path to the file.
802 @retval EFI_SUCCESS The 2 device paths were successfully returned.
803 @retval other A error from gBS->HandleProtocol.
809 GetDevicePathsForImageAndFile (
810 IN OUT EFI_DEVICE_PATH_PROTOCOL
**DevPath
,
811 IN OUT EFI_DEVICE_PATH_PROTOCOL
**FilePath
815 EFI_LOADED_IMAGE_PROTOCOL
*LoadedImage
;
816 EFI_DEVICE_PATH_PROTOCOL
*ImageDevicePath
;
818 ASSERT(DevPath
!= NULL
);
819 ASSERT(FilePath
!= NULL
);
821 Status
= gBS
->OpenProtocol (
823 &gEfiLoadedImageProtocolGuid
,
824 (VOID
**)&LoadedImage
,
827 EFI_OPEN_PROTOCOL_GET_PROTOCOL
829 if (!EFI_ERROR (Status
)) {
830 Status
= gBS
->OpenProtocol (
831 LoadedImage
->DeviceHandle
,
832 &gEfiDevicePathProtocolGuid
,
833 (VOID
**)&ImageDevicePath
,
836 EFI_OPEN_PROTOCOL_GET_PROTOCOL
838 if (!EFI_ERROR (Status
)) {
839 *DevPath
= DuplicateDevicePath (ImageDevicePath
);
840 *FilePath
= DuplicateDevicePath (LoadedImage
->FilePath
);
842 LoadedImage
->DeviceHandle
,
843 &gEfiDevicePathProtocolGuid
,
849 &gEfiLoadedImageProtocolGuid
,
857 Process all Uefi Shell 2.0 command line options.
859 see Uefi Shell 2.0 section 3.2 for full details.
861 the command line must resemble the following:
863 shell.efi [ShellOpt-options] [options] [file-name [file-name-options]]
865 ShellOpt-options Options which control the initialization behavior of the shell.
866 These options are read from the EFI global variable "ShellOpt"
867 and are processed before options or file-name.
869 options Options which control the initialization behavior of the shell.
871 file-name The name of a UEFI shell application or script to be executed
872 after initialization is complete. By default, if file-name is
873 specified, then -nostartup is implied. Scripts are not supported
876 file-name-options The command-line options that are passed to file-name when it
879 This will initialize the ShellInfoObject.ShellInitSettings global variable.
881 @retval EFI_SUCCESS The variable is initialized.
892 CHAR16
*DelayValueStr
;
895 EFI_UNICODE_COLLATION_PROTOCOL
*UnicodeCollation
;
897 // `file-name-options` will contain arguments to `file-name` that we don't
898 // know about. This would cause ShellCommandLineParse to error, so we parse
899 // arguments manually, ignoring those after the first thing that doesn't look
900 // like a shell option (which is assumed to be `file-name`).
902 Status
= gBS
->LocateProtocol (
903 &gEfiUnicodeCollationProtocolGuid
,
905 (VOID
**) &UnicodeCollation
907 if (EFI_ERROR (Status
)) {
911 // Set default options
912 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.Startup
= FALSE
;
913 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoStartup
= FALSE
;
914 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoConsoleOut
= FALSE
;
915 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoConsoleIn
= FALSE
;
916 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoInterrupt
= FALSE
;
917 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoMap
= FALSE
;
918 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoVersion
= FALSE
;
919 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.Delay
= FALSE
;
920 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.Exit
= FALSE
;
921 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoNest
= FALSE
;
922 ShellInfoObject
.ShellInitSettings
.Delay
= 5;
925 // Start LoopVar at 0 to parse only optional arguments at Argv[0]
926 // and parse other parameters from Argv[1]. This is for use case that
927 // UEFI Shell boot option is created, and OptionalData is provided
928 // that starts with shell command-line options.
930 for (LoopVar
= 0 ; LoopVar
< gEfiShellParametersProtocol
->Argc
; LoopVar
++) {
931 CurrentArg
= gEfiShellParametersProtocol
->Argv
[LoopVar
];
932 if (UnicodeCollation
->StriColl (
937 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.Startup
= TRUE
;
939 else if (UnicodeCollation
->StriColl (
944 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoStartup
= TRUE
;
946 else if (UnicodeCollation
->StriColl (
951 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoConsoleOut
= TRUE
;
953 else if (UnicodeCollation
->StriColl (
958 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoConsoleIn
= TRUE
;
960 else if (UnicodeCollation
->StriColl (
965 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoInterrupt
= TRUE
;
967 else if (UnicodeCollation
->StriColl (
972 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoMap
= TRUE
;
974 else if (UnicodeCollation
->StriColl (
979 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoVersion
= TRUE
;
981 else if (UnicodeCollation
->StriColl (
986 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoNest
= TRUE
;
988 else if (UnicodeCollation
->StriColl (
993 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.Delay
= TRUE
;
994 // Check for optional delay value following "-delay"
995 DelayValueStr
= gEfiShellParametersProtocol
->Argv
[LoopVar
+ 1];
996 if (DelayValueStr
!= NULL
){
997 if (*DelayValueStr
== L
':') {
1000 if (!EFI_ERROR(ShellConvertStringToUint64 (
1006 ShellInfoObject
.ShellInitSettings
.Delay
= (UINTN
)DelayValue
;
1010 } else if (UnicodeCollation
->StriColl (
1015 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.Exit
= TRUE
;
1016 } else if (StrnCmp (L
"-", CurrentArg
, 1) == 0) {
1017 // Unrecognized option
1018 ShellPrintHiiEx(-1, -1, NULL
,
1019 STRING_TOKEN (STR_GEN_PROBLEM
),
1020 ShellInfoObject
.HiiHandle
,
1023 return EFI_INVALID_PARAMETER
;
1026 // First argument should be Shell.efi image name
1032 ShellInfoObject
.ShellInitSettings
.FileName
= AllocateCopyPool(StrSize(CurrentArg
), CurrentArg
);
1033 if (ShellInfoObject
.ShellInitSettings
.FileName
== NULL
) {
1034 return (EFI_OUT_OF_RESOURCES
);
1037 // We found `file-name`.
1039 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoStartup
= 1;
1042 // Add `file-name-options`
1043 for (Size
= 0 ; LoopVar
< gEfiShellParametersProtocol
->Argc
; LoopVar
++) {
1044 ASSERT((ShellInfoObject
.ShellInitSettings
.FileOptions
== NULL
&& Size
== 0) || (ShellInfoObject
.ShellInitSettings
.FileOptions
!= NULL
));
1045 StrnCatGrow(&ShellInfoObject
.ShellInitSettings
.FileOptions
,
1049 if (ShellInfoObject
.ShellInitSettings
.FileOptions
== NULL
) {
1050 SHELL_FREE_NON_NULL(ShellInfoObject
.ShellInitSettings
.FileName
);
1051 return (EFI_OUT_OF_RESOURCES
);
1053 StrnCatGrow(&ShellInfoObject
.ShellInitSettings
.FileOptions
,
1055 gEfiShellParametersProtocol
->Argv
[LoopVar
],
1057 if (ShellInfoObject
.ShellInitSettings
.FileOptions
== NULL
) {
1058 SHELL_FREE_NON_NULL(ShellInfoObject
.ShellInitSettings
.FileName
);
1059 return (EFI_OUT_OF_RESOURCES
);
1065 // "-nointerrupt" overrides "-delay"
1066 if (ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoInterrupt
) {
1067 ShellInfoObject
.ShellInitSettings
.Delay
= 0;
1074 Handles all interaction with the default startup script.
1076 this will check that the correct command line parameters were passed, handle the delay, and then start running the script.
1078 @param ImagePath the path to the image for shell. first place to look for the startup script
1079 @param FilePath the path to the file for shell. second place to look for the startup script.
1081 @retval EFI_SUCCESS the variable is initialized.
1086 IN EFI_DEVICE_PATH_PROTOCOL
*ImagePath
,
1087 IN EFI_DEVICE_PATH_PROTOCOL
*FilePath
1091 EFI_STATUS CalleeStatus
;
1094 SHELL_FILE_HANDLE FileHandle
;
1095 EFI_DEVICE_PATH_PROTOCOL
*NewPath
;
1096 EFI_DEVICE_PATH_PROTOCOL
*NamePath
;
1097 CHAR16
*FileStringPath
;
1100 CONST CHAR16
*MapName
;
1102 Key
.UnicodeChar
= CHAR_NULL
;
1106 if (!ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.Startup
&& ShellInfoObject
.ShellInitSettings
.FileName
!= NULL
) {
1108 // launch something else instead
1110 NewSize
= StrSize(ShellInfoObject
.ShellInitSettings
.FileName
);
1111 if (ShellInfoObject
.ShellInitSettings
.FileOptions
!= NULL
) {
1112 NewSize
+= StrSize(ShellInfoObject
.ShellInitSettings
.FileOptions
) + sizeof(CHAR16
);
1114 FileStringPath
= AllocateZeroPool(NewSize
);
1115 if (FileStringPath
== NULL
) {
1116 return (EFI_OUT_OF_RESOURCES
);
1118 StrCpyS(FileStringPath
, NewSize
/sizeof(CHAR16
), ShellInfoObject
.ShellInitSettings
.FileName
);
1119 if (ShellInfoObject
.ShellInitSettings
.FileOptions
!= NULL
) {
1120 StrnCatS(FileStringPath
, NewSize
/sizeof(CHAR16
), L
" ", NewSize
/sizeof(CHAR16
) - StrLen(FileStringPath
) -1);
1121 StrnCatS(FileStringPath
, NewSize
/sizeof(CHAR16
), ShellInfoObject
.ShellInitSettings
.FileOptions
, NewSize
/sizeof(CHAR16
) - StrLen(FileStringPath
) -1);
1123 Status
= RunShellCommand(FileStringPath
, &CalleeStatus
);
1124 if (ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.Exit
== TRUE
) {
1125 ShellCommandRegisterExit(gEfiShellProtocol
->BatchIsActive(), (UINT64
)CalleeStatus
);
1127 FreePool(FileStringPath
);
1133 // for shell level 0 we do no scripts
1134 // Without the Startup bit overriding we allow for nostartup to prevent scripts
1136 if ( (PcdGet8(PcdShellSupportLevel
) < 1)
1137 || (ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoStartup
&& !ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.Startup
)
1139 return (EFI_SUCCESS
);
1142 gST
->ConOut
->EnableCursor(gST
->ConOut
, FALSE
);
1144 // print out our warning and see if they press a key
1146 for ( Status
= EFI_UNSUPPORTED
, Delay
= ShellInfoObject
.ShellInitSettings
.Delay
1147 ; Delay
!= 0 && EFI_ERROR(Status
)
1150 ShellPrintHiiEx(0, gST
->ConOut
->Mode
->CursorRow
, NULL
, STRING_TOKEN (STR_SHELL_STARTUP_QUESTION
), ShellInfoObject
.HiiHandle
, Delay
);
1151 gBS
->Stall (1000000);
1152 if (!ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoConsoleIn
) {
1153 Status
= gST
->ConIn
->ReadKeyStroke (gST
->ConIn
, &Key
);
1156 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_CRLF
), ShellInfoObject
.HiiHandle
);
1157 gST
->ConOut
->EnableCursor(gST
->ConOut
, TRUE
);
1162 if (Status
== EFI_SUCCESS
&& Key
.UnicodeChar
== 0 && Key
.ScanCode
== SCAN_ESC
) {
1163 return (EFI_SUCCESS
);
1167 // Try the first location (must be file system)
1169 MapName
= ShellInfoObject
.NewEfiShellProtocol
->GetMapFromDevicePath(&ImagePath
);
1170 if (MapName
!= NULL
) {
1171 FileStringPath
= NULL
;
1173 FileStringPath
= StrnCatGrow(&FileStringPath
, &NewSize
, MapName
, 0);
1174 if (FileStringPath
== NULL
) {
1175 Status
= EFI_OUT_OF_RESOURCES
;
1177 TempSpot
= StrStr(FileStringPath
, L
";");
1178 if (TempSpot
!= NULL
) {
1179 *TempSpot
= CHAR_NULL
;
1181 FileStringPath
= StrnCatGrow(&FileStringPath
, &NewSize
, ((FILEPATH_DEVICE_PATH
*)FilePath
)->PathName
, 0);
1182 PathRemoveLastItem(FileStringPath
);
1183 FileStringPath
= StrnCatGrow(&FileStringPath
, &NewSize
, mStartupScript
, 0);
1184 Status
= ShellInfoObject
.NewEfiShellProtocol
->OpenFileByName(FileStringPath
, &FileHandle
, EFI_FILE_MODE_READ
);
1185 FreePool(FileStringPath
);
1188 if (EFI_ERROR(Status
)) {
1189 NamePath
= FileDevicePath (NULL
, mStartupScript
);
1190 NewPath
= AppendDevicePathNode (ImagePath
, NamePath
);
1196 Status
= InternalOpenFileDevicePath(NewPath
, &FileHandle
, EFI_FILE_MODE_READ
, 0);
1200 // If we got a file, run it
1202 if (!EFI_ERROR(Status
) && FileHandle
!= NULL
) {
1203 Status
= RunScriptFile (mStartupScript
, FileHandle
, L
"", ShellInfoObject
.NewShellParametersProtocol
);
1204 ShellInfoObject
.NewEfiShellProtocol
->CloseFile(FileHandle
);
1206 FileStringPath
= ShellFindFilePath(mStartupScript
);
1207 if (FileStringPath
== NULL
) {
1209 // we return success since we don't need to have a startup script
1211 Status
= EFI_SUCCESS
;
1212 ASSERT(FileHandle
== NULL
);
1214 Status
= RunScriptFile(FileStringPath
, NULL
, L
"", ShellInfoObject
.NewShellParametersProtocol
);
1215 FreePool(FileStringPath
);
1224 Function to perform the shell prompt looping. It will do a single prompt,
1225 dispatch the result, and then return. It is expected that the caller will
1226 call this function in a loop many times.
1229 @retval RETURN_ABORTED
1240 CONST CHAR16
*CurDir
;
1243 LIST_ENTRY OldBufferList
;
1248 // Get screen setting to decide size of the command line buffer
1250 gST
->ConOut
->QueryMode (gST
->ConOut
, gST
->ConOut
->Mode
->Mode
, &Column
, &Row
);
1251 BufferSize
= Column
* Row
* sizeof (CHAR16
);
1252 CmdLine
= AllocateZeroPool (BufferSize
);
1253 if (CmdLine
== NULL
) {
1254 return EFI_OUT_OF_RESOURCES
;
1257 SaveBufferList(&OldBufferList
);
1258 CurDir
= ShellInfoObject
.NewEfiShellProtocol
->GetEnv(L
"cwd");
1263 gST
->ConOut
->SetCursorPosition (gST
->ConOut
, 0, gST
->ConOut
->Mode
->CursorRow
);
1265 if (CurDir
!= NULL
&& StrLen(CurDir
) > 1) {
1266 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_CURDIR
), ShellInfoObject
.HiiHandle
, CurDir
);
1268 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_SHELL
), ShellInfoObject
.HiiHandle
);
1272 // Read a line from the console
1274 Status
= ShellInfoObject
.NewEfiShellProtocol
->ReadFile(ShellInfoObject
.NewShellParametersProtocol
->StdIn
, &BufferSize
, CmdLine
);
1277 // Null terminate the string and parse it
1279 if (!EFI_ERROR (Status
)) {
1280 CmdLine
[BufferSize
/ sizeof (CHAR16
)] = CHAR_NULL
;
1281 Status
= RunCommand(CmdLine
);
1285 // Done with this command
1287 RestoreBufferList(&OldBufferList
);
1293 Add a buffer to the Buffer To Free List for safely returning buffers to other
1294 places without risking letting them modify internal shell information.
1296 @param Buffer Something to pass to FreePool when the shell is exiting.
1300 AddBufferToFreeList(
1304 BUFFER_LIST
*BufferListEntry
;
1306 if (Buffer
== NULL
) {
1310 BufferListEntry
= AllocateZeroPool(sizeof(BUFFER_LIST
));
1311 ASSERT(BufferListEntry
!= NULL
);
1312 BufferListEntry
->Buffer
= Buffer
;
1313 InsertTailList(&ShellInfoObject
.BufferToFreeList
.Link
, &BufferListEntry
->Link
);
1319 Create a new buffer list and stores the old one to OldBufferList
1321 @param OldBufferList The temporary list head used to store the nodes in BufferToFreeList.
1325 OUT LIST_ENTRY
*OldBufferList
1328 CopyMem (OldBufferList
, &ShellInfoObject
.BufferToFreeList
.Link
, sizeof (LIST_ENTRY
));
1329 InitializeListHead (&ShellInfoObject
.BufferToFreeList
.Link
);
1333 Restore previous nodes into BufferToFreeList .
1335 @param OldBufferList The temporary list head used to store the nodes in BufferToFreeList.
1339 IN OUT LIST_ENTRY
*OldBufferList
1342 FreeBufferList (&ShellInfoObject
.BufferToFreeList
);
1343 CopyMem (&ShellInfoObject
.BufferToFreeList
.Link
, OldBufferList
, sizeof (LIST_ENTRY
));
1348 Add a buffer to the Line History List
1350 @param Buffer The line buffer to add.
1354 AddLineToCommandHistory(
1355 IN CONST CHAR16
*Buffer
1359 BUFFER_LIST
*Walker
;
1360 UINT16 MaxHistoryCmdCount
;
1364 MaxHistoryCmdCount
= PcdGet16(PcdShellMaxHistoryCommandCount
);
1366 if (MaxHistoryCmdCount
== 0) {
1371 Node
= AllocateZeroPool(sizeof(BUFFER_LIST
));
1372 ASSERT(Node
!= NULL
);
1373 Node
->Buffer
= AllocateCopyPool(StrSize(Buffer
), Buffer
);
1374 ASSERT(Node
->Buffer
!= NULL
);
1376 for ( Walker
= (BUFFER_LIST
*)GetFirstNode(&ShellInfoObject
.ViewingSettings
.CommandHistory
.Link
)
1377 ; !IsNull(&ShellInfoObject
.ViewingSettings
.CommandHistory
.Link
, &Walker
->Link
)
1378 ; Walker
= (BUFFER_LIST
*)GetNextNode(&ShellInfoObject
.ViewingSettings
.CommandHistory
.Link
, &Walker
->Link
)
1382 if (Count
< MaxHistoryCmdCount
){
1383 InsertTailList(&ShellInfoObject
.ViewingSettings
.CommandHistory
.Link
, &Node
->Link
);
1385 Walker
= (BUFFER_LIST
*)GetFirstNode(&ShellInfoObject
.ViewingSettings
.CommandHistory
.Link
);
1386 RemoveEntryList(&Walker
->Link
);
1387 if (Walker
->Buffer
!= NULL
) {
1388 FreePool(Walker
->Buffer
);
1391 InsertTailList(&ShellInfoObject
.ViewingSettings
.CommandHistory
.Link
, &Node
->Link
);
1396 Checks if a string is an alias for another command. If yes, then it replaces the alias name
1397 with the correct command name.
1399 @param[in, out] CommandString Upon entry the potential alias. Upon return the
1400 command name if it was an alias. If it was not
1401 an alias it will be unchanged. This function may
1402 change the buffer to fit the command name.
1404 @retval EFI_SUCCESS The name was changed.
1405 @retval EFI_SUCCESS The name was not an alias.
1406 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
1411 IN OUT CHAR16
**CommandString
1414 CONST CHAR16
*NewString
;
1416 NewString
= ShellInfoObject
.NewEfiShellProtocol
->GetAlias(*CommandString
, NULL
);
1417 if (NewString
== NULL
) {
1418 return (EFI_SUCCESS
);
1420 FreePool(*CommandString
);
1421 *CommandString
= AllocateCopyPool(StrSize(NewString
), NewString
);
1422 if (*CommandString
== NULL
) {
1423 return (EFI_OUT_OF_RESOURCES
);
1425 return (EFI_SUCCESS
);
1429 This function will eliminate unreplaced (and therefore non-found) environment variables.
1431 @param[in,out] CmdLine The command line to update.
1435 StripUnreplacedEnvironmentVariables(
1436 IN OUT CHAR16
*CmdLine
1439 CHAR16
*FirstPercent
;
1441 CHAR16
*SecondPercent
;
1442 CHAR16
*SecondQuote
;
1443 CHAR16
*CurrentLocator
;
1445 for (CurrentLocator
= CmdLine
; CurrentLocator
!= NULL
; ) {
1446 FirstQuote
= FindNextInstance(CurrentLocator
, L
"\"", TRUE
);
1447 FirstPercent
= FindNextInstance(CurrentLocator
, L
"%", TRUE
);
1448 SecondPercent
= FirstPercent
!=NULL
?FindNextInstance(FirstPercent
+1, L
"%", TRUE
):NULL
;
1449 if (FirstPercent
== NULL
|| SecondPercent
== NULL
) {
1451 // If we ever don't have 2 % we are done.
1456 if (FirstQuote
!= NULL
&& FirstQuote
< FirstPercent
) {
1457 SecondQuote
= FindNextInstance(FirstQuote
+1, L
"\"", TRUE
);
1459 // Quote is first found
1462 if (SecondQuote
< FirstPercent
) {
1464 // restart after the pair of "
1466 CurrentLocator
= SecondQuote
+ 1;
1467 } else /* FirstPercent < SecondQuote */{
1469 // Restart on the first percent
1471 CurrentLocator
= FirstPercent
;
1476 if (FirstQuote
== NULL
|| SecondPercent
< FirstQuote
) {
1477 if (IsValidEnvironmentVariableName(FirstPercent
, SecondPercent
)) {
1479 // We need to remove from FirstPercent to SecondPercent
1481 CopyMem(FirstPercent
, SecondPercent
+ 1, StrSize(SecondPercent
+ 1));
1483 // don't need to update the locator. both % characters are gone.
1486 CurrentLocator
= SecondPercent
+ 1;
1490 CurrentLocator
= FirstQuote
;
1492 return (EFI_SUCCESS
);
1496 Function allocates a new command line and replaces all instances of environment
1497 variable names that are correctly preset to their values.
1499 If the return value is not NULL the memory must be caller freed.
1501 @param[in] OriginalCommandLine The original command line
1503 @retval NULL An error occurred.
1504 @return The new command line with no environment variables present.
1508 ShellConvertVariables (
1509 IN CONST CHAR16
*OriginalCommandLine
1512 CONST CHAR16
*MasterEnvList
;
1514 CHAR16
*NewCommandLine1
;
1515 CHAR16
*NewCommandLine2
;
1519 SCRIPT_FILE
*CurrentScriptFile
;
1520 ALIAS_LIST
*AliasListNode
;
1522 ASSERT(OriginalCommandLine
!= NULL
);
1525 NewSize
= StrSize(OriginalCommandLine
);
1526 CurrentScriptFile
= ShellCommandGetCurrentScriptFile();
1529 ///@todo update this to handle the %0 - %9 for scripting only (borrow from line 1256 area) ? ? ?
1532 // calculate the size required for the post-conversion string...
1534 if (CurrentScriptFile
!= NULL
) {
1535 for (AliasListNode
= (ALIAS_LIST
*)GetFirstNode(&CurrentScriptFile
->SubstList
)
1536 ; !IsNull(&CurrentScriptFile
->SubstList
, &AliasListNode
->Link
)
1537 ; AliasListNode
= (ALIAS_LIST
*)GetNextNode(&CurrentScriptFile
->SubstList
, &AliasListNode
->Link
)
1539 for (Temp
= StrStr(OriginalCommandLine
, AliasListNode
->Alias
)
1541 ; Temp
= StrStr(Temp
+1, AliasListNode
->Alias
)
1544 // we need a preceding and if there is space no ^ preceding (if no space ignore)
1546 if ((((Temp
-OriginalCommandLine
)>2) && *(Temp
-2) != L
'^') || ((Temp
-OriginalCommandLine
)<=2)) {
1547 NewSize
+= StrSize(AliasListNode
->CommandString
);
1553 for (MasterEnvList
= EfiShellGetEnv(NULL
)
1554 ; MasterEnvList
!= NULL
&& *MasterEnvList
!= CHAR_NULL
//&& *(MasterEnvList+1) != CHAR_NULL
1555 ; MasterEnvList
+= StrLen(MasterEnvList
) + 1
1557 if (StrSize(MasterEnvList
) > ItemSize
) {
1558 ItemSize
= StrSize(MasterEnvList
);
1560 for (Temp
= StrStr(OriginalCommandLine
, MasterEnvList
)
1562 ; Temp
= StrStr(Temp
+1, MasterEnvList
)
1565 // we need a preceding and following % and if there is space no ^ preceding (if no space ignore)
1567 if (*(Temp
-1) == L
'%' && *(Temp
+StrLen(MasterEnvList
)) == L
'%' &&
1568 ((((Temp
-OriginalCommandLine
)>2) && *(Temp
-2) != L
'^') || ((Temp
-OriginalCommandLine
)<=2))) {
1569 NewSize
+=StrSize(EfiShellGetEnv(MasterEnvList
));
1575 // now do the replacements...
1577 NewCommandLine1
= AllocateCopyPool(NewSize
, OriginalCommandLine
);
1578 NewCommandLine2
= AllocateZeroPool(NewSize
);
1579 ItemTemp
= AllocateZeroPool(ItemSize
+(2*sizeof(CHAR16
)));
1580 if (NewCommandLine1
== NULL
|| NewCommandLine2
== NULL
|| ItemTemp
== NULL
) {
1581 SHELL_FREE_NON_NULL(NewCommandLine1
);
1582 SHELL_FREE_NON_NULL(NewCommandLine2
);
1583 SHELL_FREE_NON_NULL(ItemTemp
);
1586 for (MasterEnvList
= EfiShellGetEnv(NULL
)
1587 ; MasterEnvList
!= NULL
&& *MasterEnvList
!= CHAR_NULL
1588 ; MasterEnvList
+= StrLen(MasterEnvList
) + 1
1591 ((ItemSize
+(2*sizeof(CHAR16
)))/sizeof(CHAR16
)),
1595 ((ItemSize
+(2*sizeof(CHAR16
)))/sizeof(CHAR16
)),
1599 ((ItemSize
+(2*sizeof(CHAR16
)))/sizeof(CHAR16
)),
1602 ShellCopySearchAndReplace(NewCommandLine1
, NewCommandLine2
, NewSize
, ItemTemp
, EfiShellGetEnv(MasterEnvList
), TRUE
, FALSE
);
1603 StrCpyS(NewCommandLine1
, NewSize
/sizeof(CHAR16
), NewCommandLine2
);
1605 if (CurrentScriptFile
!= NULL
) {
1606 for (AliasListNode
= (ALIAS_LIST
*)GetFirstNode(&CurrentScriptFile
->SubstList
)
1607 ; !IsNull(&CurrentScriptFile
->SubstList
, &AliasListNode
->Link
)
1608 ; AliasListNode
= (ALIAS_LIST
*)GetNextNode(&CurrentScriptFile
->SubstList
, &AliasListNode
->Link
)
1610 ShellCopySearchAndReplace(NewCommandLine1
, NewCommandLine2
, NewSize
, AliasListNode
->Alias
, AliasListNode
->CommandString
, TRUE
, FALSE
);
1611 StrCpyS(NewCommandLine1
, NewSize
/sizeof(CHAR16
), NewCommandLine2
);
1616 // Remove non-existent environment variables
1618 StripUnreplacedEnvironmentVariables(NewCommandLine1
);
1621 // Now cleanup any straggler intentionally ignored "%" characters
1623 ShellCopySearchAndReplace(NewCommandLine1
, NewCommandLine2
, NewSize
, L
"^%", L
"%", TRUE
, FALSE
);
1624 StrCpyS(NewCommandLine1
, NewSize
/sizeof(CHAR16
), NewCommandLine2
);
1626 FreePool(NewCommandLine2
);
1629 return (NewCommandLine1
);
1633 Internal function to run a command line with pipe usage.
1635 @param[in] CmdLine The pointer to the command line.
1636 @param[in] StdIn The pointer to the Standard input.
1637 @param[in] StdOut The pointer to the Standard output.
1639 @retval EFI_SUCCESS The split command is executed successfully.
1640 @retval other Some error occurs when executing the split command.
1645 IN CONST CHAR16
*CmdLine
,
1646 IN SHELL_FILE_HANDLE
*StdIn
,
1647 IN SHELL_FILE_HANDLE
*StdOut
1651 CHAR16
*NextCommandLine
;
1652 CHAR16
*OurCommandLine
;
1656 SHELL_FILE_HANDLE
*TempFileHandle
;
1659 ASSERT(StdOut
== NULL
);
1661 ASSERT(StrStr(CmdLine
, L
"|") != NULL
);
1663 Status
= EFI_SUCCESS
;
1664 NextCommandLine
= NULL
;
1665 OurCommandLine
= NULL
;
1669 NextCommandLine
= StrnCatGrow(&NextCommandLine
, &Size1
, StrStr(CmdLine
, L
"|")+1, 0);
1670 OurCommandLine
= StrnCatGrow(&OurCommandLine
, &Size2
, CmdLine
, StrStr(CmdLine
, L
"|") - CmdLine
);
1672 if (NextCommandLine
== NULL
|| OurCommandLine
== NULL
) {
1673 SHELL_FREE_NON_NULL(OurCommandLine
);
1674 SHELL_FREE_NON_NULL(NextCommandLine
);
1675 return (EFI_OUT_OF_RESOURCES
);
1676 } else if (StrStr(OurCommandLine
, L
"|") != NULL
|| Size1
== 0 || Size2
== 0) {
1677 SHELL_FREE_NON_NULL(OurCommandLine
);
1678 SHELL_FREE_NON_NULL(NextCommandLine
);
1679 return (EFI_INVALID_PARAMETER
);
1680 } else if (NextCommandLine
[0] == L
'a' &&
1681 (NextCommandLine
[1] == L
' ' || NextCommandLine
[1] == CHAR_NULL
)
1683 CopyMem(NextCommandLine
, NextCommandLine
+1, StrSize(NextCommandLine
) - sizeof(NextCommandLine
[0]));
1684 while (NextCommandLine
[0] == L
' ') {
1685 CopyMem(NextCommandLine
, NextCommandLine
+1, StrSize(NextCommandLine
) - sizeof(NextCommandLine
[0]));
1687 if (NextCommandLine
[0] == CHAR_NULL
) {
1688 SHELL_FREE_NON_NULL(OurCommandLine
);
1689 SHELL_FREE_NON_NULL(NextCommandLine
);
1690 return (EFI_INVALID_PARAMETER
);
1699 // make a SPLIT_LIST item and add to list
1701 Split
= AllocateZeroPool(sizeof(SPLIT_LIST
));
1702 ASSERT(Split
!= NULL
);
1703 Split
->SplitStdIn
= StdIn
;
1704 Split
->SplitStdOut
= ConvertEfiFileProtocolToShellHandle(CreateFileInterfaceMem(Unicode
), NULL
);
1705 ASSERT(Split
->SplitStdOut
!= NULL
);
1706 InsertHeadList(&ShellInfoObject
.SplitList
.Link
, &Split
->Link
);
1708 Status
= RunCommand(OurCommandLine
);
1711 // move the output from the first to the in to the second.
1713 TempFileHandle
= Split
->SplitStdOut
;
1714 if (Split
->SplitStdIn
== StdIn
) {
1715 Split
->SplitStdOut
= NULL
;
1717 Split
->SplitStdOut
= Split
->SplitStdIn
;
1719 Split
->SplitStdIn
= TempFileHandle
;
1720 ShellInfoObject
.NewEfiShellProtocol
->SetFilePosition(ConvertShellHandleToEfiFileProtocol(Split
->SplitStdIn
), 0);
1722 if (!EFI_ERROR(Status
)) {
1723 Status
= RunCommand(NextCommandLine
);
1727 // remove the top level from the ScriptList
1729 ASSERT((SPLIT_LIST
*)GetFirstNode(&ShellInfoObject
.SplitList
.Link
) == Split
);
1730 RemoveEntryList(&Split
->Link
);
1733 // Note that the original StdIn is now the StdOut...
1735 if (Split
->SplitStdOut
!= NULL
&& Split
->SplitStdOut
!= StdIn
) {
1736 ShellInfoObject
.NewEfiShellProtocol
->CloseFile(ConvertShellHandleToEfiFileProtocol(Split
->SplitStdOut
));
1738 if (Split
->SplitStdIn
!= NULL
) {
1739 ShellInfoObject
.NewEfiShellProtocol
->CloseFile(ConvertShellHandleToEfiFileProtocol(Split
->SplitStdIn
));
1743 FreePool(NextCommandLine
);
1744 FreePool(OurCommandLine
);
1750 Take the original command line, substitute any variables, free
1751 the original string, return the modified copy.
1753 @param[in] CmdLine pointer to the command line to update.
1755 @retval EFI_SUCCESS the function was successful.
1756 @retval EFI_OUT_OF_RESOURCES a memory allocation failed.
1760 ShellSubstituteVariables(
1765 NewCmdLine
= ShellConvertVariables(*CmdLine
);
1766 SHELL_FREE_NON_NULL(*CmdLine
);
1767 if (NewCmdLine
== NULL
) {
1768 return (EFI_OUT_OF_RESOURCES
);
1770 *CmdLine
= NewCmdLine
;
1771 return (EFI_SUCCESS
);
1775 Take the original command line, substitute any alias in the first group of space delimited characters, free
1776 the original string, return the modified copy.
1778 @param[in] CmdLine pointer to the command line to update.
1780 @retval EFI_SUCCESS the function was successful.
1781 @retval EFI_OUT_OF_RESOURCES a memory allocation failed.
1785 ShellSubstituteAliases(
1790 CHAR16
*CommandName
;
1792 UINTN PostAliasSize
;
1793 ASSERT(CmdLine
!= NULL
);
1794 ASSERT(*CmdLine
!= NULL
);
1798 if (StrStr((*CmdLine
), L
" ") == NULL
){
1799 StrnCatGrow(&CommandName
, NULL
, (*CmdLine
), 0);
1801 StrnCatGrow(&CommandName
, NULL
, (*CmdLine
), StrStr((*CmdLine
), L
" ") - (*CmdLine
));
1805 // This cannot happen 'inline' since the CmdLine can need extra space.
1808 if (!ShellCommandIsCommandOnList(CommandName
)) {
1810 // Convert via alias
1812 Status
= ShellConvertAlias(&CommandName
);
1813 if (EFI_ERROR(Status
)){
1817 NewCmdLine
= StrnCatGrow(&NewCmdLine
, &PostAliasSize
, CommandName
, 0);
1818 if (NewCmdLine
== NULL
) {
1819 SHELL_FREE_NON_NULL(CommandName
);
1820 SHELL_FREE_NON_NULL(*CmdLine
);
1821 return (EFI_OUT_OF_RESOURCES
);
1823 NewCmdLine
= StrnCatGrow(&NewCmdLine
, &PostAliasSize
, StrStr((*CmdLine
), L
" "), 0);
1824 if (NewCmdLine
== NULL
) {
1825 SHELL_FREE_NON_NULL(CommandName
);
1826 SHELL_FREE_NON_NULL(*CmdLine
);
1827 return (EFI_OUT_OF_RESOURCES
);
1830 NewCmdLine
= StrnCatGrow(&NewCmdLine
, NULL
, (*CmdLine
), 0);
1833 SHELL_FREE_NON_NULL(*CmdLine
);
1834 SHELL_FREE_NON_NULL(CommandName
);
1837 // re-assign the passed in double pointer to point to our newly allocated buffer
1839 *CmdLine
= NewCmdLine
;
1841 return (EFI_SUCCESS
);
1845 Takes the Argv[0] part of the command line and determine the meaning of it.
1847 @param[in] CmdName pointer to the command line to update.
1849 @retval Internal_Command The name is an internal command.
1850 @retval File_Sys_Change the name is a file system change.
1851 @retval Script_File_Name the name is a NSH script file.
1852 @retval Unknown_Invalid the name is unknown.
1853 @retval Efi_Application the name is an application (.EFI).
1855 SHELL_OPERATION_TYPES
1858 IN CONST CHAR16
*CmdName
1861 CHAR16
* FileWithPath
;
1862 CONST CHAR16
* TempLocation
;
1863 CONST CHAR16
* TempLocation2
;
1865 FileWithPath
= NULL
;
1867 // test for an internal command.
1869 if (ShellCommandIsCommandOnList(CmdName
)) {
1870 return (Internal_Command
);
1874 // Test for file system change request. anything ending with first : and cant have spaces.
1876 if (CmdName
[(StrLen(CmdName
)-1)] == L
':') {
1877 if ( StrStr(CmdName
, L
" ") != NULL
1878 || StrLen(StrStr(CmdName
, L
":")) > 1
1880 return (Unknown_Invalid
);
1882 return (File_Sys_Change
);
1888 if ((FileWithPath
= ShellFindFilePathEx(CmdName
, mExecutableExtensions
)) != NULL
) {
1890 // See if that file has a script file extension
1892 if (StrLen(FileWithPath
) > 4) {
1893 TempLocation
= FileWithPath
+StrLen(FileWithPath
)-4;
1894 TempLocation2
= mScriptExtension
;
1895 if (StringNoCaseCompare((VOID
*)(&TempLocation
), (VOID
*)(&TempLocation2
)) == 0) {
1896 SHELL_FREE_NON_NULL(FileWithPath
);
1897 return (Script_File_Name
);
1902 // Was a file, but not a script. we treat this as an application.
1904 SHELL_FREE_NON_NULL(FileWithPath
);
1905 return (Efi_Application
);
1908 SHELL_FREE_NON_NULL(FileWithPath
);
1910 // No clue what this is... return invalid flag...
1912 return (Unknown_Invalid
);
1916 Determine if the first item in a command line is valid.
1918 @param[in] CmdLine The command line to parse.
1920 @retval EFI_SUCCESS The item is valid.
1921 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
1922 @retval EFI_NOT_FOUND The operation type is unknown or invalid.
1927 IN CONST CHAR16
*CmdLine
1931 CHAR16
*FirstParameter
;
1937 Temp
= StrnCatGrow(&Temp
, NULL
, CmdLine
, 0);
1939 return (EFI_OUT_OF_RESOURCES
);
1942 FirstParameter
= StrStr(Temp
, L
"|");
1943 if (FirstParameter
!= NULL
) {
1944 *FirstParameter
= CHAR_NULL
;
1947 FirstParameter
= NULL
;
1950 // Process the command line
1952 Status
= ProcessCommandLineToFinal(&Temp
);
1954 if (!EFI_ERROR(Status
)) {
1955 FirstParameter
= AllocateZeroPool(StrSize(CmdLine
));
1956 if (FirstParameter
== NULL
) {
1957 SHELL_FREE_NON_NULL(Temp
);
1958 return (EFI_OUT_OF_RESOURCES
);
1960 TempWalker
= (CHAR16
*)Temp
;
1961 if (!EFI_ERROR(GetNextParameter(&TempWalker
, &FirstParameter
, StrSize(CmdLine
), TRUE
))) {
1962 if (GetOperationType(FirstParameter
) == Unknown_Invalid
) {
1963 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_NOT_FOUND
), ShellInfoObject
.HiiHandle
, FirstParameter
);
1964 SetLastError(SHELL_NOT_FOUND
);
1965 Status
= EFI_NOT_FOUND
;
1970 SHELL_FREE_NON_NULL(Temp
);
1971 SHELL_FREE_NON_NULL(FirstParameter
);
1976 Determine if a command line contains with a split contains only valid commands.
1978 @param[in] CmdLine The command line to parse.
1980 @retval EFI_SUCCESS CmdLine has only valid commands, application, or has no split.
1981 @retval EFI_ABORTED CmdLine has at least one invalid command or application.
1986 IN CONST CHAR16
*CmdLine
1989 CONST CHAR16
*TempSpot
;
1993 // If this was the only item, then get out
1995 if (!ContainsSplit(CmdLine
)) {
1996 return (EFI_SUCCESS
);
2000 // Verify up to the pipe or end character
2002 Status
= IsValidSplit(CmdLine
);
2003 if (EFI_ERROR(Status
)) {
2008 // recurse to verify the next item
2010 TempSpot
= FindFirstCharacter(CmdLine
, L
"|", L
'^') + 1;
2011 if (*TempSpot
== L
'a' &&
2012 (*(TempSpot
+ 1) == L
' ' || *(TempSpot
+ 1) == CHAR_NULL
)
2014 // If it's an ASCII pipe '|a'
2018 return (VerifySplit(TempSpot
));
2022 Process a split based operation.
2024 @param[in] CmdLine pointer to the command line to process
2026 @retval EFI_SUCCESS The operation was successful
2027 @return an error occurred.
2031 ProcessNewSplitCommandLine(
2032 IN CONST CHAR16
*CmdLine
2038 Status
= VerifySplit(CmdLine
);
2039 if (EFI_ERROR(Status
)) {
2046 // are we in an existing split???
2048 if (!IsListEmpty(&ShellInfoObject
.SplitList
.Link
)) {
2049 Split
= (SPLIT_LIST
*)GetFirstNode(&ShellInfoObject
.SplitList
.Link
);
2052 if (Split
== NULL
) {
2053 Status
= RunSplitCommand(CmdLine
, NULL
, NULL
);
2055 Status
= RunSplitCommand(CmdLine
, Split
->SplitStdIn
, Split
->SplitStdOut
);
2057 if (EFI_ERROR(Status
)) {
2058 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_INVALID_SPLIT
), ShellInfoObject
.HiiHandle
, CmdLine
);
2064 Handle a request to change the current file system.
2066 @param[in] CmdLine The passed in command line.
2068 @retval EFI_SUCCESS The operation was successful.
2073 IN CONST CHAR16
*CmdLine
2077 Status
= EFI_SUCCESS
;
2080 // make sure we are the right operation
2082 ASSERT(CmdLine
[(StrLen(CmdLine
)-1)] == L
':' && StrStr(CmdLine
, L
" ") == NULL
);
2085 // Call the protocol API to do the work
2087 Status
= ShellInfoObject
.NewEfiShellProtocol
->SetCurDir(NULL
, CmdLine
);
2090 // Report any errors
2092 if (EFI_ERROR(Status
)) {
2093 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_INVALID_MAPPING
), ShellInfoObject
.HiiHandle
, CmdLine
);
2100 Reprocess the command line to direct all -? to the help command.
2102 if found, will add "help" as argv[0], and move the rest later.
2104 @param[in,out] CmdLine pointer to the command line to update
2109 IN OUT CHAR16
**CmdLine
2112 CHAR16
*CurrentParameter
;
2114 CHAR16
*NewCommandLine
;
2116 UINTN NewCmdLineSize
;
2118 Status
= EFI_SUCCESS
;
2120 CurrentParameter
= AllocateZeroPool(StrSize(*CmdLine
));
2121 if (CurrentParameter
== NULL
) {
2122 return (EFI_OUT_OF_RESOURCES
);
2126 while(Walker
!= NULL
&& *Walker
!= CHAR_NULL
) {
2127 if (!EFI_ERROR(GetNextParameter(&Walker
, &CurrentParameter
, StrSize(*CmdLine
), TRUE
))) {
2128 if (StrStr(CurrentParameter
, L
"-?") == CurrentParameter
) {
2129 CurrentParameter
[0] = L
' ';
2130 CurrentParameter
[1] = L
' ';
2131 NewCmdLineSize
= StrSize(L
"help ") + StrSize(*CmdLine
);
2132 NewCommandLine
= AllocateZeroPool(NewCmdLineSize
);
2133 if (NewCommandLine
== NULL
) {
2134 Status
= EFI_OUT_OF_RESOURCES
;
2139 // We know the space is sufficient since we just calculated it.
2141 StrnCpyS(NewCommandLine
, NewCmdLineSize
/sizeof(CHAR16
), L
"help ", 5);
2142 StrnCatS(NewCommandLine
, NewCmdLineSize
/sizeof(CHAR16
), *CmdLine
, StrLen(*CmdLine
));
2143 SHELL_FREE_NON_NULL(*CmdLine
);
2144 *CmdLine
= NewCommandLine
;
2150 SHELL_FREE_NON_NULL(CurrentParameter
);
2156 Function to update the shell variable "lasterror".
2158 @param[in] ErrorCode the error code to put into lasterror.
2163 IN CONST SHELL_STATUS ErrorCode
2166 CHAR16 LeString
[19];
2167 if (sizeof(EFI_STATUS
) == sizeof(UINT64
)) {
2168 UnicodeSPrint(LeString
, sizeof(LeString
), L
"0x%Lx", ErrorCode
);
2170 UnicodeSPrint(LeString
, sizeof(LeString
), L
"0x%x", ErrorCode
);
2172 DEBUG_CODE(InternalEfiShellSetEnv(L
"debuglasterror", LeString
, TRUE
););
2173 InternalEfiShellSetEnv(L
"lasterror", LeString
, TRUE
);
2175 return (EFI_SUCCESS
);
2179 Converts the command line to it's post-processed form. this replaces variables and alias' per UEFI Shell spec.
2181 @param[in,out] CmdLine pointer to the command line to update
2183 @retval EFI_SUCCESS The operation was successful
2184 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
2185 @return some other error occurred
2189 ProcessCommandLineToFinal(
2190 IN OUT CHAR16
**CmdLine
2194 TrimSpaces(CmdLine
);
2196 Status
= ShellSubstituteAliases(CmdLine
);
2197 if (EFI_ERROR(Status
)) {
2201 TrimSpaces(CmdLine
);
2203 Status
= ShellSubstituteVariables(CmdLine
);
2204 if (EFI_ERROR(Status
)) {
2207 ASSERT (*CmdLine
!= NULL
);
2209 TrimSpaces(CmdLine
);
2212 // update for help parsing
2214 if (StrStr(*CmdLine
, L
"?") != NULL
) {
2216 // This may do nothing if the ? does not indicate help.
2217 // Save all the details for in the API below.
2219 Status
= DoHelpUpdate(CmdLine
);
2222 TrimSpaces(CmdLine
);
2224 return (EFI_SUCCESS
);
2228 Run an internal shell command.
2230 This API will update the shell's environment since these commands are libraries.
2232 @param[in] CmdLine the command line to run.
2233 @param[in] FirstParameter the first parameter on the command line
2234 @param[in] ParamProtocol the shell parameters protocol pointer
2235 @param[out] CommandStatus the status from the command line.
2237 @retval EFI_SUCCESS The command was completed.
2238 @retval EFI_ABORTED The command's operation was aborted.
2243 IN CONST CHAR16
*CmdLine
,
2244 IN CHAR16
*FirstParameter
,
2245 IN EFI_SHELL_PARAMETERS_PROTOCOL
*ParamProtocol
,
2246 OUT EFI_STATUS
*CommandStatus
2252 SHELL_STATUS CommandReturnedStatus
;
2257 NewCmdLine
= AllocateCopyPool (StrSize (CmdLine
), CmdLine
);
2258 if (NewCmdLine
== NULL
) {
2259 return EFI_OUT_OF_RESOURCES
;
2262 for (Walker
= NewCmdLine
; Walker
!= NULL
&& *Walker
!= CHAR_NULL
; Walker
++) {
2263 if (*Walker
== L
'^' && *(Walker
+1) == L
'#') {
2264 CopyMem(Walker
, Walker
+1, StrSize(Walker
) - sizeof(Walker
[0]));
2269 // get the argc and argv updated for internal commands
2271 Status
= UpdateArgcArgv(ParamProtocol
, NewCmdLine
, Internal_Command
, &Argv
, &Argc
);
2272 if (!EFI_ERROR(Status
)) {
2274 // Run the internal command.
2276 Status
= ShellCommandRunCommandHandler(FirstParameter
, &CommandReturnedStatus
, &LastError
);
2278 if (!EFI_ERROR(Status
)) {
2279 if (CommandStatus
!= NULL
) {
2280 if (CommandReturnedStatus
!= SHELL_SUCCESS
) {
2281 *CommandStatus
= (EFI_STATUS
)(CommandReturnedStatus
| MAX_BIT
);
2283 *CommandStatus
= EFI_SUCCESS
;
2288 // Update last error status.
2289 // some commands do not update last error.
2292 SetLastError(CommandReturnedStatus
);
2296 // Pass thru the exitcode from the app.
2298 if (ShellCommandGetExit()) {
2300 // An Exit was requested ("exit" command), pass its value up.
2302 Status
= CommandReturnedStatus
;
2303 } else if (CommandReturnedStatus
!= SHELL_SUCCESS
&& IsScriptOnlyCommand(FirstParameter
)) {
2305 // Always abort when a script only command fails for any reason
2307 Status
= EFI_ABORTED
;
2308 } else if (ShellCommandGetCurrentScriptFile() != NULL
&& CommandReturnedStatus
== SHELL_ABORTED
) {
2310 // Abort when in a script and a command aborted
2312 Status
= EFI_ABORTED
;
2318 // This is guaranteed to be called after UpdateArgcArgv no matter what else happened.
2319 // This is safe even if the update API failed. In this case, it may be a no-op.
2321 RestoreArgcArgv(ParamProtocol
, &Argv
, &Argc
);
2324 // If a script is running and the command is not a script only command, then
2325 // change return value to success so the script won't halt (unless aborted).
2327 // Script only commands have to be able halt the script since the script will
2328 // not operate if they are failing.
2330 if ( ShellCommandGetCurrentScriptFile() != NULL
2331 && !IsScriptOnlyCommand(FirstParameter
)
2332 && Status
!= EFI_ABORTED
2334 Status
= EFI_SUCCESS
;
2337 FreePool (NewCmdLine
);
2342 Function to run the command or file.
2344 @param[in] Type the type of operation being run.
2345 @param[in] CmdLine the command line to run.
2346 @param[in] FirstParameter the first parameter on the command line
2347 @param[in] ParamProtocol the shell parameters protocol pointer
2348 @param[out] CommandStatus the status from the command line.
2350 @retval EFI_SUCCESS The command was completed.
2351 @retval EFI_ABORTED The command's operation was aborted.
2356 IN SHELL_OPERATION_TYPES Type
,
2357 IN CONST CHAR16
*CmdLine
,
2358 IN CHAR16
*FirstParameter
,
2359 IN EFI_SHELL_PARAMETERS_PROTOCOL
*ParamProtocol
,
2360 OUT EFI_STATUS
*CommandStatus
2364 EFI_STATUS StartStatus
;
2365 CHAR16
*CommandWithPath
;
2366 EFI_DEVICE_PATH_PROTOCOL
*DevPath
;
2367 SHELL_STATUS CalleeExitStatus
;
2369 Status
= EFI_SUCCESS
;
2370 CommandWithPath
= NULL
;
2372 CalleeExitStatus
= SHELL_INVALID_PARAMETER
;
2375 case Internal_Command
:
2376 Status
= RunInternalCommand(CmdLine
, FirstParameter
, ParamProtocol
, CommandStatus
);
2378 case Script_File_Name
:
2379 case Efi_Application
:
2381 // Process a fully qualified path
2383 if (StrStr(FirstParameter
, L
":") != NULL
) {
2384 ASSERT (CommandWithPath
== NULL
);
2385 if (ShellIsFile(FirstParameter
) == EFI_SUCCESS
) {
2386 CommandWithPath
= StrnCatGrow(&CommandWithPath
, NULL
, FirstParameter
, 0);
2391 // Process a relative path and also check in the path environment variable
2393 if (CommandWithPath
== NULL
) {
2394 CommandWithPath
= ShellFindFilePathEx(FirstParameter
, mExecutableExtensions
);
2398 // This should be impossible now.
2400 ASSERT(CommandWithPath
!= NULL
);
2403 // Make sure that path is not just a directory (or not found)
2405 if (!EFI_ERROR(ShellIsDirectory(CommandWithPath
))) {
2406 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_NOT_FOUND
), ShellInfoObject
.HiiHandle
, FirstParameter
);
2407 SetLastError(SHELL_NOT_FOUND
);
2410 case Script_File_Name
:
2411 Status
= RunScriptFile (CommandWithPath
, NULL
, CmdLine
, ParamProtocol
);
2413 case Efi_Application
:
2415 // Get the device path of the application image
2417 DevPath
= ShellInfoObject
.NewEfiShellProtocol
->GetDevicePathFromFilePath(CommandWithPath
);
2418 if (DevPath
== NULL
){
2419 Status
= EFI_OUT_OF_RESOURCES
;
2424 // Execute the device path
2426 Status
= InternalShellExecuteDevicePath(
2434 SHELL_FREE_NON_NULL(DevPath
);
2436 if(EFI_ERROR (Status
)) {
2437 CalleeExitStatus
= (SHELL_STATUS
) (Status
& (~MAX_BIT
));
2439 CalleeExitStatus
= (SHELL_STATUS
) StartStatus
;
2442 if (CommandStatus
!= NULL
) {
2443 *CommandStatus
= CalleeExitStatus
;
2447 // Update last error status.
2449 // Status is an EFI_STATUS. Clear top bit to convert to SHELL_STATUS
2450 SetLastError(CalleeExitStatus
);
2466 SHELL_FREE_NON_NULL(CommandWithPath
);
2472 Function to setup StdIn, StdErr, StdOut, and then run the command or file.
2474 @param[in] Type the type of operation being run.
2475 @param[in] CmdLine the command line to run.
2476 @param[in] FirstParameter the first parameter on the command line.
2477 @param[in] ParamProtocol the shell parameters protocol pointer
2478 @param[out] CommandStatus the status from the command line.
2480 @retval EFI_SUCCESS The command was completed.
2481 @retval EFI_ABORTED The command's operation was aborted.
2485 SetupAndRunCommandOrFile(
2486 IN SHELL_OPERATION_TYPES Type
,
2488 IN CHAR16
*FirstParameter
,
2489 IN EFI_SHELL_PARAMETERS_PROTOCOL
*ParamProtocol
,
2490 OUT EFI_STATUS
*CommandStatus
2494 SHELL_FILE_HANDLE OriginalStdIn
;
2495 SHELL_FILE_HANDLE OriginalStdOut
;
2496 SHELL_FILE_HANDLE OriginalStdErr
;
2497 SYSTEM_TABLE_INFO OriginalSystemTableInfo
;
2500 // Update the StdIn, StdOut, and StdErr for redirection to environment variables, files, etc... unicode and ASCII
2502 Status
= UpdateStdInStdOutStdErr(ParamProtocol
, CmdLine
, &OriginalStdIn
, &OriginalStdOut
, &OriginalStdErr
, &OriginalSystemTableInfo
);
2505 // The StdIn, StdOut, and StdErr are set up.
2506 // Now run the command, script, or application
2508 if (!EFI_ERROR(Status
)) {
2509 TrimSpaces(&CmdLine
);
2510 Status
= RunCommandOrFile(Type
, CmdLine
, FirstParameter
, ParamProtocol
, CommandStatus
);
2516 if (EFI_ERROR(Status
)) {
2517 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_ERROR
), ShellInfoObject
.HiiHandle
, (VOID
*)(Status
));
2521 // put back the original StdIn, StdOut, and StdErr
2523 RestoreStdInStdOutStdErr(ParamProtocol
, &OriginalStdIn
, &OriginalStdOut
, &OriginalStdErr
, &OriginalSystemTableInfo
);
2529 Function will process and run a command line.
2531 This will determine if the command line represents an internal shell
2532 command or dispatch an external application.
2534 @param[in] CmdLine The command line to parse.
2535 @param[out] CommandStatus The status from the command line.
2537 @retval EFI_SUCCESS The command was completed.
2538 @retval EFI_ABORTED The command's operation was aborted.
2543 IN CONST CHAR16
*CmdLine
,
2544 OUT EFI_STATUS
*CommandStatus
2548 CHAR16
*CleanOriginal
;
2549 CHAR16
*FirstParameter
;
2551 SHELL_OPERATION_TYPES Type
;
2553 ASSERT(CmdLine
!= NULL
);
2554 if (StrLen(CmdLine
) == 0) {
2555 return (EFI_SUCCESS
);
2558 Status
= EFI_SUCCESS
;
2559 CleanOriginal
= NULL
;
2561 CleanOriginal
= StrnCatGrow(&CleanOriginal
, NULL
, CmdLine
, 0);
2562 if (CleanOriginal
== NULL
) {
2563 return (EFI_OUT_OF_RESOURCES
);
2566 TrimSpaces(&CleanOriginal
);
2569 // NULL out comments (leveraged from RunScriptFileHandle() ).
2570 // The # character on a line is used to denote that all characters on the same line
2571 // and to the right of the # are to be ignored by the shell.
2572 // Afterwards, again remove spaces, in case any were between the last command-parameter and '#'.
2574 for (TempWalker
= CleanOriginal
; TempWalker
!= NULL
&& *TempWalker
!= CHAR_NULL
; TempWalker
++) {
2575 if (*TempWalker
== L
'^') {
2576 if (*(TempWalker
+ 1) == L
'#') {
2579 } else if (*TempWalker
== L
'#') {
2580 *TempWalker
= CHAR_NULL
;
2584 TrimSpaces(&CleanOriginal
);
2587 // Handle case that passed in command line is just 1 or more " " characters.
2589 if (StrLen (CleanOriginal
) == 0) {
2590 SHELL_FREE_NON_NULL(CleanOriginal
);
2591 return (EFI_SUCCESS
);
2594 Status
= ProcessCommandLineToFinal(&CleanOriginal
);
2595 if (EFI_ERROR(Status
)) {
2596 SHELL_FREE_NON_NULL(CleanOriginal
);
2601 // We don't do normal processing with a split command line (output from one command input to another)
2603 if (ContainsSplit(CleanOriginal
)) {
2604 Status
= ProcessNewSplitCommandLine(CleanOriginal
);
2605 SHELL_FREE_NON_NULL(CleanOriginal
);
2610 // We need the first parameter information so we can determine the operation type
2612 FirstParameter
= AllocateZeroPool(StrSize(CleanOriginal
));
2613 if (FirstParameter
== NULL
) {
2614 SHELL_FREE_NON_NULL(CleanOriginal
);
2615 return (EFI_OUT_OF_RESOURCES
);
2617 TempWalker
= CleanOriginal
;
2618 if (!EFI_ERROR(GetNextParameter(&TempWalker
, &FirstParameter
, StrSize(CleanOriginal
), TRUE
))) {
2620 // Depending on the first parameter we change the behavior
2622 switch (Type
= GetOperationType(FirstParameter
)) {
2623 case File_Sys_Change
:
2624 Status
= ChangeMappedDrive (FirstParameter
);
2626 case Internal_Command
:
2627 case Script_File_Name
:
2628 case Efi_Application
:
2629 Status
= SetupAndRunCommandOrFile(Type
, CleanOriginal
, FirstParameter
, ShellInfoObject
.NewShellParametersProtocol
, CommandStatus
);
2633 // Whatever was typed, it was invalid.
2635 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_NOT_FOUND
), ShellInfoObject
.HiiHandle
, FirstParameter
);
2636 SetLastError(SHELL_NOT_FOUND
);
2640 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_NOT_FOUND
), ShellInfoObject
.HiiHandle
, FirstParameter
);
2641 SetLastError(SHELL_NOT_FOUND
);
2644 SHELL_FREE_NON_NULL(CleanOriginal
);
2645 SHELL_FREE_NON_NULL(FirstParameter
);
2651 Function will process and run a command line.
2653 This will determine if the command line represents an internal shell
2654 command or dispatch an external application.
2656 @param[in] CmdLine The command line to parse.
2658 @retval EFI_SUCCESS The command was completed.
2659 @retval EFI_ABORTED The command's operation was aborted.
2664 IN CONST CHAR16
*CmdLine
2667 return (RunShellCommand(CmdLine
, NULL
));
2671 STATIC CONST UINT16 InvalidChars
[] = {L
'*', L
'?', L
'<', L
'>', L
'\\', L
'/', L
'\"', 0x0001, 0x0002};
2673 Function determines if the CommandName COULD be a valid command. It does not determine whether
2674 this is a valid command. It only checks for invalid characters.
2676 @param[in] CommandName The name to check
2678 @retval TRUE CommandName could be a command name
2679 @retval FALSE CommandName could not be a valid command name
2684 IN CONST CHAR16
*CommandName
2688 if (CommandName
== NULL
) {
2693 ; Count
< sizeof(InvalidChars
) / sizeof(InvalidChars
[0])
2696 if (ScanMem16(CommandName
, StrSize(CommandName
), InvalidChars
[Count
]) != NULL
) {
2704 Function to process a NSH script file via SHELL_FILE_HANDLE.
2706 @param[in] Handle The handle to the already opened file.
2707 @param[in] Name The name of the script file.
2709 @retval EFI_SUCCESS the script completed successfully
2713 RunScriptFileHandle (
2714 IN SHELL_FILE_HANDLE Handle
,
2715 IN CONST CHAR16
*Name
2719 SCRIPT_FILE
*NewScriptFile
;
2721 UINTN PrintBuffSize
;
2722 CHAR16
*CommandLine
;
2723 CHAR16
*CommandLine2
;
2724 CHAR16
*CommandLine3
;
2725 SCRIPT_COMMAND_LIST
*LastCommand
;
2727 BOOLEAN PreScriptEchoState
;
2728 BOOLEAN PreCommandEchoState
;
2729 CONST CHAR16
*CurDir
;
2731 CHAR16 LeString
[50];
2732 LIST_ENTRY OldBufferList
;
2734 ASSERT(!ShellCommandGetScriptExit());
2736 PreScriptEchoState
= ShellCommandGetEchoState();
2737 PrintBuffSize
= PcdGet16(PcdShellPrintBufferSize
);
2739 NewScriptFile
= (SCRIPT_FILE
*)AllocateZeroPool(sizeof(SCRIPT_FILE
));
2740 if (NewScriptFile
== NULL
) {
2741 return (EFI_OUT_OF_RESOURCES
);
2747 ASSERT(NewScriptFile
->ScriptName
== NULL
);
2748 NewScriptFile
->ScriptName
= StrnCatGrow(&NewScriptFile
->ScriptName
, NULL
, Name
, 0);
2749 if (NewScriptFile
->ScriptName
== NULL
) {
2750 DeleteScriptFileStruct(NewScriptFile
);
2751 return (EFI_OUT_OF_RESOURCES
);
2755 // Save the parameters (used to replace %0 to %9 later on)
2757 NewScriptFile
->Argc
= ShellInfoObject
.NewShellParametersProtocol
->Argc
;
2758 if (NewScriptFile
->Argc
!= 0) {
2759 NewScriptFile
->Argv
= (CHAR16
**)AllocateZeroPool(NewScriptFile
->Argc
* sizeof(CHAR16
*));
2760 if (NewScriptFile
->Argv
== NULL
) {
2761 DeleteScriptFileStruct(NewScriptFile
);
2762 return (EFI_OUT_OF_RESOURCES
);
2764 for (LoopVar
= 0 ; LoopVar
< 10 && LoopVar
< NewScriptFile
->Argc
; LoopVar
++) {
2765 ASSERT(NewScriptFile
->Argv
[LoopVar
] == NULL
);
2766 NewScriptFile
->Argv
[LoopVar
] = StrnCatGrow(&NewScriptFile
->Argv
[LoopVar
], NULL
, ShellInfoObject
.NewShellParametersProtocol
->Argv
[LoopVar
], 0);
2767 if (NewScriptFile
->Argv
[LoopVar
] == NULL
) {
2768 DeleteScriptFileStruct(NewScriptFile
);
2769 return (EFI_OUT_OF_RESOURCES
);
2773 NewScriptFile
->Argv
= NULL
;
2776 InitializeListHead(&NewScriptFile
->CommandList
);
2777 InitializeListHead(&NewScriptFile
->SubstList
);
2780 // Now build the list of all script commands.
2783 while(!ShellFileHandleEof(Handle
)) {
2784 CommandLine
= ShellFileHandleReturnLine(Handle
, &Ascii
);
2786 if (CommandLine
== NULL
|| StrLen(CommandLine
) == 0 || CommandLine
[0] == '#') {
2787 SHELL_FREE_NON_NULL(CommandLine
);
2790 NewScriptFile
->CurrentCommand
= AllocateZeroPool(sizeof(SCRIPT_COMMAND_LIST
));
2791 if (NewScriptFile
->CurrentCommand
== NULL
) {
2792 SHELL_FREE_NON_NULL(CommandLine
);
2793 DeleteScriptFileStruct(NewScriptFile
);
2794 return (EFI_OUT_OF_RESOURCES
);
2797 NewScriptFile
->CurrentCommand
->Cl
= CommandLine
;
2798 NewScriptFile
->CurrentCommand
->Data
= NULL
;
2799 NewScriptFile
->CurrentCommand
->Line
= LineCount
;
2801 InsertTailList(&NewScriptFile
->CommandList
, &NewScriptFile
->CurrentCommand
->Link
);
2805 // Add this as the topmost script file
2807 ShellCommandSetNewScript (NewScriptFile
);
2810 // Now enumerate through the commands and run each one.
2812 CommandLine
= AllocateZeroPool(PrintBuffSize
);
2813 if (CommandLine
== NULL
) {
2814 DeleteScriptFileStruct(NewScriptFile
);
2815 return (EFI_OUT_OF_RESOURCES
);
2817 CommandLine2
= AllocateZeroPool(PrintBuffSize
);
2818 if (CommandLine2
== NULL
) {
2819 FreePool(CommandLine
);
2820 DeleteScriptFileStruct(NewScriptFile
);
2821 return (EFI_OUT_OF_RESOURCES
);
2824 for ( NewScriptFile
->CurrentCommand
= (SCRIPT_COMMAND_LIST
*)GetFirstNode(&NewScriptFile
->CommandList
)
2825 ; !IsNull(&NewScriptFile
->CommandList
, &NewScriptFile
->CurrentCommand
->Link
)
2826 ; // conditional increment in the body of the loop
2828 ASSERT(CommandLine2
!= NULL
);
2829 StrnCpyS( CommandLine2
,
2830 PrintBuffSize
/sizeof(CHAR16
),
2831 NewScriptFile
->CurrentCommand
->Cl
,
2832 PrintBuffSize
/sizeof(CHAR16
) - 1
2835 SaveBufferList(&OldBufferList
);
2838 // NULL out comments
2840 for (CommandLine3
= CommandLine2
; CommandLine3
!= NULL
&& *CommandLine3
!= CHAR_NULL
; CommandLine3
++) {
2841 if (*CommandLine3
== L
'^') {
2842 if ( *(CommandLine3
+1) == L
':') {
2843 CopyMem(CommandLine3
, CommandLine3
+1, StrSize(CommandLine3
) - sizeof(CommandLine3
[0]));
2844 } else if (*(CommandLine3
+1) == L
'#') {
2847 } else if (*CommandLine3
== L
'#') {
2848 *CommandLine3
= CHAR_NULL
;
2852 if (CommandLine2
!= NULL
&& StrLen(CommandLine2
) >= 1) {
2854 // Due to variability in starting the find and replace action we need to have both buffers the same.
2856 StrnCpyS( CommandLine
,
2857 PrintBuffSize
/sizeof(CHAR16
),
2859 PrintBuffSize
/sizeof(CHAR16
) - 1
2863 // Remove the %0 to %9 from the command line (if we have some arguments)
2865 if (NewScriptFile
->Argv
!= NULL
) {
2866 switch (NewScriptFile
->Argc
) {
2868 Status
= ShellCopySearchAndReplace(CommandLine2
, CommandLine
, PrintBuffSize
, L
"%9", NewScriptFile
->Argv
[9], FALSE
, FALSE
);
2869 ASSERT_EFI_ERROR(Status
);
2871 Status
= ShellCopySearchAndReplace(CommandLine
, CommandLine2
, PrintBuffSize
, L
"%8", NewScriptFile
->Argv
[8], FALSE
, FALSE
);
2872 ASSERT_EFI_ERROR(Status
);
2874 Status
= ShellCopySearchAndReplace(CommandLine2
, CommandLine
, PrintBuffSize
, L
"%7", NewScriptFile
->Argv
[7], FALSE
, FALSE
);
2875 ASSERT_EFI_ERROR(Status
);
2877 Status
= ShellCopySearchAndReplace(CommandLine
, CommandLine2
, PrintBuffSize
, L
"%6", NewScriptFile
->Argv
[6], FALSE
, FALSE
);
2878 ASSERT_EFI_ERROR(Status
);
2880 Status
= ShellCopySearchAndReplace(CommandLine2
, CommandLine
, PrintBuffSize
, L
"%5", NewScriptFile
->Argv
[5], FALSE
, FALSE
);
2881 ASSERT_EFI_ERROR(Status
);
2883 Status
= ShellCopySearchAndReplace(CommandLine
, CommandLine2
, PrintBuffSize
, L
"%4", NewScriptFile
->Argv
[4], FALSE
, FALSE
);
2884 ASSERT_EFI_ERROR(Status
);
2886 Status
= ShellCopySearchAndReplace(CommandLine2
, CommandLine
, PrintBuffSize
, L
"%3", NewScriptFile
->Argv
[3], FALSE
, FALSE
);
2887 ASSERT_EFI_ERROR(Status
);
2889 Status
= ShellCopySearchAndReplace(CommandLine
, CommandLine2
, PrintBuffSize
, L
"%2", NewScriptFile
->Argv
[2], FALSE
, FALSE
);
2890 ASSERT_EFI_ERROR(Status
);
2892 Status
= ShellCopySearchAndReplace(CommandLine2
, CommandLine
, PrintBuffSize
, L
"%1", NewScriptFile
->Argv
[1], FALSE
, FALSE
);
2893 ASSERT_EFI_ERROR(Status
);
2895 Status
= ShellCopySearchAndReplace(CommandLine
, CommandLine2
, PrintBuffSize
, L
"%0", NewScriptFile
->Argv
[0], FALSE
, FALSE
);
2896 ASSERT_EFI_ERROR(Status
);
2902 Status
= ShellCopySearchAndReplace(CommandLine2
, CommandLine
, PrintBuffSize
, L
"%1", L
"\"\"", FALSE
, FALSE
);
2903 Status
= ShellCopySearchAndReplace(CommandLine
, CommandLine2
, PrintBuffSize
, L
"%2", L
"\"\"", FALSE
, FALSE
);
2904 Status
= ShellCopySearchAndReplace(CommandLine2
, CommandLine
, PrintBuffSize
, L
"%3", L
"\"\"", FALSE
, FALSE
);
2905 Status
= ShellCopySearchAndReplace(CommandLine
, CommandLine2
, PrintBuffSize
, L
"%4", L
"\"\"", FALSE
, FALSE
);
2906 Status
= ShellCopySearchAndReplace(CommandLine2
, CommandLine
, PrintBuffSize
, L
"%5", L
"\"\"", FALSE
, FALSE
);
2907 Status
= ShellCopySearchAndReplace(CommandLine
, CommandLine2
, PrintBuffSize
, L
"%6", L
"\"\"", FALSE
, FALSE
);
2908 Status
= ShellCopySearchAndReplace(CommandLine2
, CommandLine
, PrintBuffSize
, L
"%7", L
"\"\"", FALSE
, FALSE
);
2909 Status
= ShellCopySearchAndReplace(CommandLine
, CommandLine2
, PrintBuffSize
, L
"%8", L
"\"\"", FALSE
, FALSE
);
2910 Status
= ShellCopySearchAndReplace(CommandLine2
, CommandLine
, PrintBuffSize
, L
"%9", L
"\"\"", FALSE
, FALSE
);
2912 StrnCpyS( CommandLine2
,
2913 PrintBuffSize
/sizeof(CHAR16
),
2915 PrintBuffSize
/sizeof(CHAR16
) - 1
2918 LastCommand
= NewScriptFile
->CurrentCommand
;
2920 for (CommandLine3
= CommandLine2
; CommandLine3
[0] == L
' ' ; CommandLine3
++);
2922 if (CommandLine3
!= NULL
&& CommandLine3
[0] == L
':' ) {
2924 // This line is a goto target / label
2927 if (CommandLine3
!= NULL
&& StrLen(CommandLine3
) > 0) {
2928 if (CommandLine3
[0] == L
'@') {
2930 // We need to save the current echo state
2931 // and disable echo for just this command.
2933 PreCommandEchoState
= ShellCommandGetEchoState();
2934 ShellCommandSetEchoState(FALSE
);
2935 Status
= RunCommand(CommandLine3
+1);
2938 // If command was "@echo -off" or "@echo -on" then don't restore echo state
2940 if (StrCmp (L
"@echo -off", CommandLine3
) != 0 &&
2941 StrCmp (L
"@echo -on", CommandLine3
) != 0) {
2943 // Now restore the pre-'@' echo state.
2945 ShellCommandSetEchoState(PreCommandEchoState
);
2948 if (ShellCommandGetEchoState()) {
2949 CurDir
= ShellInfoObject
.NewEfiShellProtocol
->GetEnv(L
"cwd");
2950 if (CurDir
!= NULL
&& StrLen(CurDir
) > 1) {
2951 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_CURDIR
), ShellInfoObject
.HiiHandle
, CurDir
);
2953 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_SHELL
), ShellInfoObject
.HiiHandle
);
2955 ShellPrintEx(-1, -1, L
"%s\r\n", CommandLine2
);
2957 Status
= RunCommand(CommandLine3
);
2961 if (ShellCommandGetScriptExit()) {
2963 // ShellCommandGetExitCode() always returns a UINT64
2965 UnicodeSPrint(LeString
, sizeof(LeString
), L
"0x%Lx", ShellCommandGetExitCode());
2966 DEBUG_CODE(InternalEfiShellSetEnv(L
"debuglasterror", LeString
, TRUE
););
2967 InternalEfiShellSetEnv(L
"lasterror", LeString
, TRUE
);
2969 ShellCommandRegisterExit(FALSE
, 0);
2970 Status
= EFI_SUCCESS
;
2971 RestoreBufferList(&OldBufferList
);
2974 if (ShellGetExecutionBreakFlag()) {
2975 RestoreBufferList(&OldBufferList
);
2978 if (EFI_ERROR(Status
)) {
2979 RestoreBufferList(&OldBufferList
);
2982 if (ShellCommandGetExit()) {
2983 RestoreBufferList(&OldBufferList
);
2988 // If that commend did not update the CurrentCommand then we need to advance it...
2990 if (LastCommand
== NewScriptFile
->CurrentCommand
) {
2991 NewScriptFile
->CurrentCommand
= (SCRIPT_COMMAND_LIST
*)GetNextNode(&NewScriptFile
->CommandList
, &NewScriptFile
->CurrentCommand
->Link
);
2992 if (!IsNull(&NewScriptFile
->CommandList
, &NewScriptFile
->CurrentCommand
->Link
)) {
2993 NewScriptFile
->CurrentCommand
->Reset
= TRUE
;
2997 NewScriptFile
->CurrentCommand
= (SCRIPT_COMMAND_LIST
*)GetNextNode(&NewScriptFile
->CommandList
, &NewScriptFile
->CurrentCommand
->Link
);
2998 if (!IsNull(&NewScriptFile
->CommandList
, &NewScriptFile
->CurrentCommand
->Link
)) {
2999 NewScriptFile
->CurrentCommand
->Reset
= TRUE
;
3002 RestoreBufferList(&OldBufferList
);
3006 FreePool(CommandLine
);
3007 FreePool(CommandLine2
);
3008 ShellCommandSetNewScript (NULL
);
3011 // Only if this was the last script reset the state.
3013 if (ShellCommandGetCurrentScriptFile()==NULL
) {
3014 ShellCommandSetEchoState(PreScriptEchoState
);
3016 return (EFI_SUCCESS
);
3020 Function to process a NSH script file.
3022 @param[in] ScriptPath Pointer to the script file name (including file system path).
3023 @param[in] Handle the handle of the script file already opened.
3024 @param[in] CmdLine the command line to run.
3025 @param[in] ParamProtocol the shell parameters protocol pointer
3027 @retval EFI_SUCCESS the script completed successfully
3032 IN CONST CHAR16
*ScriptPath
,
3033 IN SHELL_FILE_HANDLE Handle OPTIONAL
,
3034 IN CONST CHAR16
*CmdLine
,
3035 IN EFI_SHELL_PARAMETERS_PROTOCOL
*ParamProtocol
3039 SHELL_FILE_HANDLE FileHandle
;
3043 if (ShellIsFile(ScriptPath
) != EFI_SUCCESS
) {
3044 return (EFI_INVALID_PARAMETER
);
3048 // get the argc and argv updated for scripts
3050 Status
= UpdateArgcArgv(ParamProtocol
, CmdLine
, Script_File_Name
, &Argv
, &Argc
);
3051 if (!EFI_ERROR(Status
)) {
3053 if (Handle
== NULL
) {
3057 Status
= ShellOpenFileByName(ScriptPath
, &FileHandle
, EFI_FILE_MODE_READ
, 0);
3058 if (!EFI_ERROR(Status
)) {
3062 Status
= RunScriptFileHandle(FileHandle
, ScriptPath
);
3065 // now close the file
3067 ShellCloseFile(&FileHandle
);
3070 Status
= RunScriptFileHandle(Handle
, ScriptPath
);
3075 // This is guaranteed to be called after UpdateArgcArgv no matter what else happened.
3076 // This is safe even if the update API failed. In this case, it may be a no-op.
3078 RestoreArgcArgv(ParamProtocol
, &Argv
, &Argc
);
3084 Return the pointer to the first occurrence of any character from a list of characters.
3086 @param[in] String the string to parse
3087 @param[in] CharacterList the list of character to look for
3088 @param[in] EscapeCharacter An escape character to skip
3090 @return the location of the first character in the string
3091 @retval CHAR_NULL no instance of any character in CharacterList was found in String
3096 IN CONST CHAR16
*String
,
3097 IN CONST CHAR16
*CharacterList
,
3098 IN CONST CHAR16 EscapeCharacter
3104 for (WalkStr
= 0; WalkStr
< StrLen(String
); WalkStr
++) {
3105 if (String
[WalkStr
] == EscapeCharacter
) {
3109 for (WalkChar
= 0; WalkChar
< StrLen(CharacterList
); WalkChar
++) {
3110 if (String
[WalkStr
] == CharacterList
[WalkChar
]) {
3111 return (&String
[WalkStr
]);
3115 return (String
+ StrLen(String
));