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;
624 // Reset the CTRL-C event (yes we ignore the return values)
626 Status
= gBS
->CheckEvent (ShellInfoObject
.NewEfiShellProtocol
->ExecutionBreak
);
631 Status
= DoShellPrompt();
632 } while (!ShellCommandGetExit());
634 if (OldConIn
!= NULL
&& ConInHandle
!= NULL
) {
635 CloseSimpleTextInOnFile (gST
->ConIn
);
636 gST
->ConIn
= OldConIn
;
637 gST
->ConsoleInHandle
= ConInHandle
;
644 // uninstall protocols / free memory / etc...
646 if (ShellInfoObject
.UserBreakTimer
!= NULL
) {
647 gBS
->CloseEvent(ShellInfoObject
.UserBreakTimer
);
648 DEBUG_CODE(ShellInfoObject
.UserBreakTimer
= NULL
;);
650 if (ShellInfoObject
.ImageDevPath
!= NULL
) {
651 FreePool(ShellInfoObject
.ImageDevPath
);
652 DEBUG_CODE(ShellInfoObject
.ImageDevPath
= NULL
;);
654 if (ShellInfoObject
.FileDevPath
!= NULL
) {
655 FreePool(ShellInfoObject
.FileDevPath
);
656 DEBUG_CODE(ShellInfoObject
.FileDevPath
= NULL
;);
658 if (ShellInfoObject
.NewShellParametersProtocol
!= NULL
) {
659 CleanUpShellParametersProtocol(ShellInfoObject
.NewShellParametersProtocol
);
660 DEBUG_CODE(ShellInfoObject
.NewShellParametersProtocol
= NULL
;);
662 if (ShellInfoObject
.NewEfiShellProtocol
!= NULL
){
663 if (ShellInfoObject
.NewEfiShellProtocol
->IsRootShell()){
664 InternalEfiShellSetEnv(L
"cwd", NULL
, TRUE
);
666 CleanUpShellEnvironment (ShellInfoObject
.NewEfiShellProtocol
);
667 DEBUG_CODE(ShellInfoObject
.NewEfiShellProtocol
= NULL
;);
670 if (!IsListEmpty(&ShellInfoObject
.BufferToFreeList
.Link
)){
671 FreeBufferList(&ShellInfoObject
.BufferToFreeList
);
674 if (!IsListEmpty(&ShellInfoObject
.SplitList
.Link
)){
675 ASSERT(FALSE
); ///@todo finish this de-allocation (free SplitStdIn/Out when needed).
677 for ( Split
= (SPLIT_LIST
*)GetFirstNode (&ShellInfoObject
.SplitList
.Link
)
678 ; !IsNull (&ShellInfoObject
.SplitList
.Link
, &Split
->Link
)
679 ; Split
= (SPLIT_LIST
*)GetNextNode (&ShellInfoObject
.SplitList
.Link
, &Split
->Link
)
681 RemoveEntryList (&Split
->Link
);
685 DEBUG_CODE (InitializeListHead (&ShellInfoObject
.SplitList
.Link
););
688 if (ShellInfoObject
.ShellInitSettings
.FileName
!= NULL
) {
689 FreePool(ShellInfoObject
.ShellInitSettings
.FileName
);
690 DEBUG_CODE(ShellInfoObject
.ShellInitSettings
.FileName
= NULL
;);
693 if (ShellInfoObject
.ShellInitSettings
.FileOptions
!= NULL
) {
694 FreePool(ShellInfoObject
.ShellInitSettings
.FileOptions
);
695 DEBUG_CODE(ShellInfoObject
.ShellInitSettings
.FileOptions
= NULL
;);
698 if (ShellInfoObject
.HiiHandle
!= NULL
) {
699 HiiRemovePackages(ShellInfoObject
.HiiHandle
);
700 DEBUG_CODE(ShellInfoObject
.HiiHandle
= NULL
;);
703 if (!IsListEmpty(&ShellInfoObject
.ViewingSettings
.CommandHistory
.Link
)){
704 FreeBufferList(&ShellInfoObject
.ViewingSettings
.CommandHistory
);
707 ASSERT(ShellInfoObject
.ConsoleInfo
!= NULL
);
708 if (ShellInfoObject
.ConsoleInfo
!= NULL
) {
709 ConsoleLoggerUninstall(ShellInfoObject
.ConsoleInfo
);
710 FreePool(ShellInfoObject
.ConsoleInfo
);
711 DEBUG_CODE(ShellInfoObject
.ConsoleInfo
= NULL
;);
714 ShellFreeEnvVarList ();
716 if (ShellCommandGetExit()) {
717 return ((EFI_STATUS
)ShellCommandGetExitCode());
723 Sets all the alias' that were registered with the ShellCommandLib library.
725 @retval EFI_SUCCESS all init commands were run successfully.
732 CONST ALIAS_LIST
*List
;
736 // Get all the commands we want to run
738 List
= ShellCommandGetInitAliasList();
741 // for each command in the List
743 for ( Node
= (ALIAS_LIST
*)GetFirstNode(&List
->Link
)
744 ; !IsNull (&List
->Link
, &Node
->Link
)
745 ; Node
= (ALIAS_LIST
*)GetNextNode(&List
->Link
, &Node
->Link
)
748 // install the alias'
750 Status
= InternalSetAlias(Node
->CommandString
, Node
->Alias
, TRUE
);
751 ASSERT_EFI_ERROR(Status
);
753 return (EFI_SUCCESS
);
757 Internal function to determine if 2 command names are really the same.
759 @param[in] Command1 The pointer to the first command name.
760 @param[in] Command2 The pointer to the second command name.
762 @retval TRUE The 2 command names are the same.
763 @retval FALSE The 2 command names are not the same.
767 IN CONST CHAR16
*Command1
,
768 IN CONST CHAR16
*Command2
771 if (StringNoCaseCompare(&Command1
, &Command2
) == 0) {
778 Internal function to determine if a command is a script only command.
780 @param[in] CommandName The pointer to the command name.
782 @retval TRUE The command is a script only command.
783 @retval FALSE The command is not a script only command.
787 IN CONST CHAR16
*CommandName
790 if (IsCommand(CommandName
, L
"for")
791 ||IsCommand(CommandName
, L
"endfor")
792 ||IsCommand(CommandName
, L
"if")
793 ||IsCommand(CommandName
, L
"else")
794 ||IsCommand(CommandName
, L
"endif")
795 ||IsCommand(CommandName
, L
"goto")) {
802 This function will populate the 2 device path protocol parameters based on the
803 global gImageHandle. The DevPath will point to the device path for the handle that has
804 loaded image protocol installed on it. The FilePath will point to the device path
805 for the file that was loaded.
807 @param[in, out] DevPath On a successful return the device path to the loaded image.
808 @param[in, out] FilePath On a successful return the device path to the file.
810 @retval EFI_SUCCESS The 2 device paths were successfully returned.
811 @retval other A error from gBS->HandleProtocol.
816 GetDevicePathsForImageAndFile (
817 IN OUT EFI_DEVICE_PATH_PROTOCOL
**DevPath
,
818 IN OUT EFI_DEVICE_PATH_PROTOCOL
**FilePath
822 EFI_LOADED_IMAGE_PROTOCOL
*LoadedImage
;
823 EFI_DEVICE_PATH_PROTOCOL
*ImageDevicePath
;
825 ASSERT(DevPath
!= NULL
);
826 ASSERT(FilePath
!= NULL
);
828 Status
= gBS
->OpenProtocol (
830 &gEfiLoadedImageProtocolGuid
,
831 (VOID
**)&LoadedImage
,
834 EFI_OPEN_PROTOCOL_GET_PROTOCOL
836 if (!EFI_ERROR (Status
)) {
837 Status
= gBS
->OpenProtocol (
838 LoadedImage
->DeviceHandle
,
839 &gEfiDevicePathProtocolGuid
,
840 (VOID
**)&ImageDevicePath
,
843 EFI_OPEN_PROTOCOL_GET_PROTOCOL
845 if (!EFI_ERROR (Status
)) {
846 *DevPath
= DuplicateDevicePath (ImageDevicePath
);
847 *FilePath
= DuplicateDevicePath (LoadedImage
->FilePath
);
849 LoadedImage
->DeviceHandle
,
850 &gEfiDevicePathProtocolGuid
,
856 &gEfiLoadedImageProtocolGuid
,
864 Process all Uefi Shell 2.0 command line options.
866 see Uefi Shell 2.0 section 3.2 for full details.
868 the command line must resemble the following:
870 shell.efi [ShellOpt-options] [options] [file-name [file-name-options]]
872 ShellOpt-options Options which control the initialization behavior of the shell.
873 These options are read from the EFI global variable "ShellOpt"
874 and are processed before options or file-name.
876 options Options which control the initialization behavior of the shell.
878 file-name The name of a UEFI shell application or script to be executed
879 after initialization is complete. By default, if file-name is
880 specified, then -nostartup is implied. Scripts are not supported
883 file-name-options The command-line options that are passed to file-name when it
886 This will initialize the ShellInfoObject.ShellInitSettings global variable.
888 @retval EFI_SUCCESS The variable is initialized.
898 CHAR16
*DelayValueStr
;
901 EFI_UNICODE_COLLATION_PROTOCOL
*UnicodeCollation
;
903 // `file-name-options` will contain arguments to `file-name` that we don't
904 // know about. This would cause ShellCommandLineParse to error, so we parse
905 // arguments manually, ignoring those after the first thing that doesn't look
906 // like a shell option (which is assumed to be `file-name`).
908 Status
= gBS
->LocateProtocol (
909 &gEfiUnicodeCollation2ProtocolGuid
,
911 (VOID
**) &UnicodeCollation
913 if (EFI_ERROR (Status
)) {
914 Status
= gBS
->LocateProtocol (
915 &gEfiUnicodeCollationProtocolGuid
,
917 (VOID
**) &UnicodeCollation
919 if (EFI_ERROR (Status
)) {
924 // Set default options
925 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.Startup
= FALSE
;
926 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoStartup
= FALSE
;
927 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoConsoleOut
= FALSE
;
928 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoConsoleIn
= FALSE
;
929 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoInterrupt
= FALSE
;
930 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoMap
= FALSE
;
931 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoVersion
= FALSE
;
932 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.Delay
= FALSE
;
933 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.Exit
= FALSE
;
934 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoNest
= FALSE
;
935 ShellInfoObject
.ShellInitSettings
.Delay
= 5;
938 // Start LoopVar at 0 to parse only optional arguments at Argv[0]
939 // and parse other parameters from Argv[1]. This is for use case that
940 // UEFI Shell boot option is created, and OptionalData is provided
941 // that starts with shell command-line options.
943 for (LoopVar
= 0 ; LoopVar
< gEfiShellParametersProtocol
->Argc
; LoopVar
++) {
944 CurrentArg
= gEfiShellParametersProtocol
->Argv
[LoopVar
];
945 if (UnicodeCollation
->StriColl (
950 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.Startup
= TRUE
;
952 else if (UnicodeCollation
->StriColl (
957 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoStartup
= TRUE
;
959 else if (UnicodeCollation
->StriColl (
964 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoConsoleOut
= TRUE
;
966 else if (UnicodeCollation
->StriColl (
971 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoConsoleIn
= TRUE
;
973 else if (UnicodeCollation
->StriColl (
978 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoInterrupt
= TRUE
;
980 else if (UnicodeCollation
->StriColl (
985 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoMap
= TRUE
;
987 else if (UnicodeCollation
->StriColl (
992 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoVersion
= TRUE
;
994 else if (UnicodeCollation
->StriColl (
999 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoNest
= TRUE
;
1001 else if (UnicodeCollation
->StriColl (
1006 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.Delay
= TRUE
;
1007 // Check for optional delay value following "-delay"
1008 DelayValueStr
= gEfiShellParametersProtocol
->Argv
[LoopVar
+ 1];
1009 if (DelayValueStr
!= NULL
){
1010 if (*DelayValueStr
== L
':') {
1013 if (!EFI_ERROR(ShellConvertStringToUint64 (
1019 ShellInfoObject
.ShellInitSettings
.Delay
= (UINTN
)DelayValue
;
1023 } else if (UnicodeCollation
->StriColl (
1028 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.Exit
= TRUE
;
1029 } else if (StrnCmp (L
"-", CurrentArg
, 1) == 0) {
1030 // Unrecognized option
1031 ShellPrintHiiEx(-1, -1, NULL
,
1032 STRING_TOKEN (STR_GEN_PROBLEM
),
1033 ShellInfoObject
.HiiHandle
,
1036 return EFI_INVALID_PARAMETER
;
1039 // First argument should be Shell.efi image name
1045 ShellInfoObject
.ShellInitSettings
.FileName
= NULL
;
1048 // If first argument contains a space, then add double quotes before the argument
1050 if (StrStr (CurrentArg
, L
" ") != NULL
) {
1051 StrnCatGrow(&ShellInfoObject
.ShellInitSettings
.FileName
, &Size
, L
"\"", 0);
1052 if (ShellInfoObject
.ShellInitSettings
.FileName
== NULL
) {
1053 return (EFI_OUT_OF_RESOURCES
);
1056 StrnCatGrow(&ShellInfoObject
.ShellInitSettings
.FileName
, &Size
, CurrentArg
, 0);
1057 if (ShellInfoObject
.ShellInitSettings
.FileName
== NULL
) {
1058 return (EFI_OUT_OF_RESOURCES
);
1061 // If first argument contains a space, then add double quotes after the argument
1063 if (StrStr (CurrentArg
, L
" ") != NULL
) {
1064 StrnCatGrow(&ShellInfoObject
.ShellInitSettings
.FileName
, &Size
, L
"\"", 0);
1065 if (ShellInfoObject
.ShellInitSettings
.FileName
== NULL
) {
1066 return (EFI_OUT_OF_RESOURCES
);
1070 // We found `file-name`.
1072 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoStartup
= 1;
1075 // Add `file-name-options`
1076 for (Size
= 0 ; LoopVar
< gEfiShellParametersProtocol
->Argc
; LoopVar
++) {
1077 ASSERT((ShellInfoObject
.ShellInitSettings
.FileOptions
== NULL
&& Size
== 0) || (ShellInfoObject
.ShellInitSettings
.FileOptions
!= NULL
));
1079 // Add a space between arguments
1081 if (ShellInfoObject
.ShellInitSettings
.FileOptions
!= NULL
) {
1082 StrnCatGrow(&ShellInfoObject
.ShellInitSettings
.FileOptions
, &Size
, L
" ", 0);
1083 if (ShellInfoObject
.ShellInitSettings
.FileOptions
== NULL
) {
1084 SHELL_FREE_NON_NULL(ShellInfoObject
.ShellInitSettings
.FileName
);
1085 return (EFI_OUT_OF_RESOURCES
);
1089 // If an argumnent contains a space, then add double quotes before the argument
1091 if (StrStr (gEfiShellParametersProtocol
->Argv
[LoopVar
], L
" ") != NULL
) {
1092 StrnCatGrow(&ShellInfoObject
.ShellInitSettings
.FileOptions
,
1096 if (ShellInfoObject
.ShellInitSettings
.FileOptions
== NULL
) {
1097 SHELL_FREE_NON_NULL(ShellInfoObject
.ShellInitSettings
.FileName
);
1098 return (EFI_OUT_OF_RESOURCES
);
1101 StrnCatGrow(&ShellInfoObject
.ShellInitSettings
.FileOptions
,
1103 gEfiShellParametersProtocol
->Argv
[LoopVar
],
1105 if (ShellInfoObject
.ShellInitSettings
.FileOptions
== NULL
) {
1106 SHELL_FREE_NON_NULL(ShellInfoObject
.ShellInitSettings
.FileName
);
1107 return (EFI_OUT_OF_RESOURCES
);
1110 // If an argumnent contains a space, then add double quotes after the argument
1112 if (StrStr (gEfiShellParametersProtocol
->Argv
[LoopVar
], L
" ") != NULL
) {
1113 StrnCatGrow(&ShellInfoObject
.ShellInitSettings
.FileOptions
,
1117 if (ShellInfoObject
.ShellInitSettings
.FileOptions
== NULL
) {
1118 SHELL_FREE_NON_NULL(ShellInfoObject
.ShellInitSettings
.FileName
);
1119 return (EFI_OUT_OF_RESOURCES
);
1126 // "-nointerrupt" overrides "-delay"
1127 if (ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoInterrupt
) {
1128 ShellInfoObject
.ShellInitSettings
.Delay
= 0;
1135 Function try to find location of the Startup.nsh file.
1137 The buffer is callee allocated and should be freed by the caller.
1139 @param ImageDevicePath The path to the image for shell. first place to look for the startup script
1140 @param FileDevicePath The path to the file for shell. second place to look for the startup script.
1142 @retval NULL No Startup.nsh file was found.
1143 @return !=NULL Pointer to NULL-terminated path.
1146 LocateStartupScript (
1147 IN EFI_DEVICE_PATH_PROTOCOL
*ImageDevicePath
,
1148 IN EFI_DEVICE_PATH_PROTOCOL
*FileDevicePath
1151 CHAR16
*StartupScriptPath
;
1153 CONST CHAR16
*MapName
;
1156 StartupScriptPath
= NULL
;
1160 // Try to find 'Startup.nsh' in the directory where the shell itself was launched.
1162 MapName
= ShellInfoObject
.NewEfiShellProtocol
->GetMapFromDevicePath (&ImageDevicePath
);
1163 if (MapName
!= NULL
) {
1164 StartupScriptPath
= StrnCatGrow (&StartupScriptPath
, &Size
, MapName
, 0);
1165 if (StartupScriptPath
== NULL
) {
1167 // Do not locate the startup script in sys path when out of resource.
1171 TempSpot
= StrStr (StartupScriptPath
, L
";");
1172 if (TempSpot
!= NULL
) {
1173 *TempSpot
= CHAR_NULL
;
1176 StartupScriptPath
= StrnCatGrow (&StartupScriptPath
, &Size
, ((FILEPATH_DEVICE_PATH
*)FileDevicePath
)->PathName
, 0);
1177 PathRemoveLastItem (StartupScriptPath
);
1178 StartupScriptPath
= StrnCatGrow (&StartupScriptPath
, &Size
, mStartupScript
, 0);
1182 // Try to find 'Startup.nsh' in the execution path defined by the envrionment variable PATH.
1184 if ((StartupScriptPath
== NULL
) || EFI_ERROR (ShellIsFile (StartupScriptPath
))) {
1185 SHELL_FREE_NON_NULL (StartupScriptPath
);
1186 StartupScriptPath
= ShellFindFilePath (mStartupScript
);
1189 return StartupScriptPath
;
1193 Handles all interaction with the default startup script.
1195 this will check that the correct command line parameters were passed, handle the delay, and then start running the script.
1197 @param ImagePath the path to the image for shell. first place to look for the startup script
1198 @param FilePath the path to the file for shell. second place to look for the startup script.
1200 @retval EFI_SUCCESS the variable is initialized.
1204 IN EFI_DEVICE_PATH_PROTOCOL
*ImagePath
,
1205 IN EFI_DEVICE_PATH_PROTOCOL
*FilePath
1209 EFI_STATUS CalleeStatus
;
1212 CHAR16
*FileStringPath
;
1215 Key
.UnicodeChar
= CHAR_NULL
;
1218 if (!ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.Startup
&& ShellInfoObject
.ShellInitSettings
.FileName
!= NULL
) {
1220 // launch something else instead
1222 NewSize
= StrSize(ShellInfoObject
.ShellInitSettings
.FileName
);
1223 if (ShellInfoObject
.ShellInitSettings
.FileOptions
!= NULL
) {
1224 NewSize
+= StrSize(ShellInfoObject
.ShellInitSettings
.FileOptions
) + sizeof(CHAR16
);
1226 FileStringPath
= AllocateZeroPool(NewSize
);
1227 if (FileStringPath
== NULL
) {
1228 return (EFI_OUT_OF_RESOURCES
);
1230 StrCpyS(FileStringPath
, NewSize
/sizeof(CHAR16
), ShellInfoObject
.ShellInitSettings
.FileName
);
1231 if (ShellInfoObject
.ShellInitSettings
.FileOptions
!= NULL
) {
1232 StrnCatS(FileStringPath
, NewSize
/sizeof(CHAR16
), L
" ", NewSize
/sizeof(CHAR16
) - StrLen(FileStringPath
) -1);
1233 StrnCatS(FileStringPath
, NewSize
/sizeof(CHAR16
), ShellInfoObject
.ShellInitSettings
.FileOptions
, NewSize
/sizeof(CHAR16
) - StrLen(FileStringPath
) -1);
1235 Status
= RunShellCommand(FileStringPath
, &CalleeStatus
);
1236 if (ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.Exit
== TRUE
) {
1237 ShellCommandRegisterExit(gEfiShellProtocol
->BatchIsActive(), (UINT64
)CalleeStatus
);
1239 FreePool(FileStringPath
);
1245 // for shell level 0 we do no scripts
1246 // Without the Startup bit overriding we allow for nostartup to prevent scripts
1248 if ( (PcdGet8(PcdShellSupportLevel
) < 1)
1249 || (ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoStartup
&& !ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.Startup
)
1251 return (EFI_SUCCESS
);
1254 gST
->ConOut
->EnableCursor(gST
->ConOut
, FALSE
);
1256 // print out our warning and see if they press a key
1258 for ( Status
= EFI_UNSUPPORTED
, Delay
= ShellInfoObject
.ShellInitSettings
.Delay
1259 ; Delay
!= 0 && EFI_ERROR(Status
)
1262 ShellPrintHiiEx(0, gST
->ConOut
->Mode
->CursorRow
, NULL
, STRING_TOKEN (STR_SHELL_STARTUP_QUESTION
), ShellInfoObject
.HiiHandle
, Delay
);
1263 gBS
->Stall (1000000);
1264 if (!ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoConsoleIn
) {
1265 Status
= gST
->ConIn
->ReadKeyStroke (gST
->ConIn
, &Key
);
1268 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_CRLF
), ShellInfoObject
.HiiHandle
);
1269 gST
->ConOut
->EnableCursor(gST
->ConOut
, TRUE
);
1274 if (Status
== EFI_SUCCESS
&& Key
.UnicodeChar
== 0 && Key
.ScanCode
== SCAN_ESC
) {
1275 return (EFI_SUCCESS
);
1278 FileStringPath
= LocateStartupScript (ImagePath
, FilePath
);
1279 if (FileStringPath
!= NULL
) {
1280 Status
= RunScriptFile (FileStringPath
, NULL
, L
"", ShellInfoObject
.NewShellParametersProtocol
);
1281 FreePool (FileStringPath
);
1284 // we return success since startup script is not mandatory.
1286 Status
= EFI_SUCCESS
;
1293 Function to perform the shell prompt looping. It will do a single prompt,
1294 dispatch the result, and then return. It is expected that the caller will
1295 call this function in a loop many times.
1298 @retval RETURN_ABORTED
1308 CONST CHAR16
*CurDir
;
1311 LIST_ENTRY OldBufferList
;
1316 // Get screen setting to decide size of the command line buffer
1318 gST
->ConOut
->QueryMode (gST
->ConOut
, gST
->ConOut
->Mode
->Mode
, &Column
, &Row
);
1319 BufferSize
= Column
* Row
* sizeof (CHAR16
);
1320 CmdLine
= AllocateZeroPool (BufferSize
);
1321 if (CmdLine
== NULL
) {
1322 return EFI_OUT_OF_RESOURCES
;
1325 SaveBufferList(&OldBufferList
);
1326 CurDir
= ShellInfoObject
.NewEfiShellProtocol
->GetEnv(L
"cwd");
1331 gST
->ConOut
->SetCursorPosition (gST
->ConOut
, 0, gST
->ConOut
->Mode
->CursorRow
);
1333 if (CurDir
!= NULL
&& StrLen(CurDir
) > 1) {
1334 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_CURDIR
), ShellInfoObject
.HiiHandle
, CurDir
);
1336 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_SHELL
), ShellInfoObject
.HiiHandle
);
1340 // Read a line from the console
1342 Status
= ShellInfoObject
.NewEfiShellProtocol
->ReadFile(ShellInfoObject
.NewShellParametersProtocol
->StdIn
, &BufferSize
, CmdLine
);
1345 // Null terminate the string and parse it
1347 if (!EFI_ERROR (Status
)) {
1348 CmdLine
[BufferSize
/ sizeof (CHAR16
)] = CHAR_NULL
;
1349 Status
= RunCommand(CmdLine
);
1353 // Done with this command
1355 RestoreBufferList(&OldBufferList
);
1361 Add a buffer to the Buffer To Free List for safely returning buffers to other
1362 places without risking letting them modify internal shell information.
1364 @param Buffer Something to pass to FreePool when the shell is exiting.
1367 AddBufferToFreeList (
1371 BUFFER_LIST
*BufferListEntry
;
1373 if (Buffer
== NULL
) {
1377 BufferListEntry
= AllocateZeroPool (sizeof (BUFFER_LIST
));
1378 if (BufferListEntry
== NULL
) {
1382 BufferListEntry
->Buffer
= Buffer
;
1383 InsertTailList (&ShellInfoObject
.BufferToFreeList
.Link
, &BufferListEntry
->Link
);
1389 Create a new buffer list and stores the old one to OldBufferList
1391 @param OldBufferList The temporary list head used to store the nodes in BufferToFreeList.
1395 OUT LIST_ENTRY
*OldBufferList
1398 CopyMem (OldBufferList
, &ShellInfoObject
.BufferToFreeList
.Link
, sizeof (LIST_ENTRY
));
1399 InitializeListHead (&ShellInfoObject
.BufferToFreeList
.Link
);
1403 Restore previous nodes into BufferToFreeList .
1405 @param OldBufferList The temporary list head used to store the nodes in BufferToFreeList.
1409 IN OUT LIST_ENTRY
*OldBufferList
1412 FreeBufferList (&ShellInfoObject
.BufferToFreeList
);
1413 CopyMem (&ShellInfoObject
.BufferToFreeList
.Link
, OldBufferList
, sizeof (LIST_ENTRY
));
1418 Add a buffer to the Line History List
1420 @param Buffer The line buffer to add.
1423 AddLineToCommandHistory(
1424 IN CONST CHAR16
*Buffer
1428 BUFFER_LIST
*Walker
;
1429 UINT16 MaxHistoryCmdCount
;
1433 MaxHistoryCmdCount
= PcdGet16(PcdShellMaxHistoryCommandCount
);
1435 if (MaxHistoryCmdCount
== 0) {
1440 Node
= AllocateZeroPool(sizeof(BUFFER_LIST
));
1445 Node
->Buffer
= AllocateCopyPool (StrSize (Buffer
), Buffer
);
1446 if (Node
->Buffer
== NULL
) {
1451 for ( Walker
= (BUFFER_LIST
*)GetFirstNode(&ShellInfoObject
.ViewingSettings
.CommandHistory
.Link
)
1452 ; !IsNull(&ShellInfoObject
.ViewingSettings
.CommandHistory
.Link
, &Walker
->Link
)
1453 ; Walker
= (BUFFER_LIST
*)GetNextNode(&ShellInfoObject
.ViewingSettings
.CommandHistory
.Link
, &Walker
->Link
)
1457 if (Count
< MaxHistoryCmdCount
){
1458 InsertTailList(&ShellInfoObject
.ViewingSettings
.CommandHistory
.Link
, &Node
->Link
);
1460 Walker
= (BUFFER_LIST
*)GetFirstNode(&ShellInfoObject
.ViewingSettings
.CommandHistory
.Link
);
1461 RemoveEntryList(&Walker
->Link
);
1462 if (Walker
->Buffer
!= NULL
) {
1463 FreePool(Walker
->Buffer
);
1466 InsertTailList(&ShellInfoObject
.ViewingSettings
.CommandHistory
.Link
, &Node
->Link
);
1471 Checks if a string is an alias for another command. If yes, then it replaces the alias name
1472 with the correct command name.
1474 @param[in, out] CommandString Upon entry the potential alias. Upon return the
1475 command name if it was an alias. If it was not
1476 an alias it will be unchanged. This function may
1477 change the buffer to fit the command name.
1479 @retval EFI_SUCCESS The name was changed.
1480 @retval EFI_SUCCESS The name was not an alias.
1481 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
1485 IN OUT CHAR16
**CommandString
1488 CONST CHAR16
*NewString
;
1490 NewString
= ShellInfoObject
.NewEfiShellProtocol
->GetAlias(*CommandString
, NULL
);
1491 if (NewString
== NULL
) {
1492 return (EFI_SUCCESS
);
1494 FreePool(*CommandString
);
1495 *CommandString
= AllocateCopyPool(StrSize(NewString
), NewString
);
1496 if (*CommandString
== NULL
) {
1497 return (EFI_OUT_OF_RESOURCES
);
1499 return (EFI_SUCCESS
);
1503 This function will eliminate unreplaced (and therefore non-found) environment variables.
1505 @param[in,out] CmdLine The command line to update.
1508 StripUnreplacedEnvironmentVariables(
1509 IN OUT CHAR16
*CmdLine
1512 CHAR16
*FirstPercent
;
1514 CHAR16
*SecondPercent
;
1515 CHAR16
*SecondQuote
;
1516 CHAR16
*CurrentLocator
;
1518 for (CurrentLocator
= CmdLine
; CurrentLocator
!= NULL
; ) {
1519 FirstQuote
= FindNextInstance(CurrentLocator
, L
"\"", TRUE
);
1520 FirstPercent
= FindNextInstance(CurrentLocator
, L
"%", TRUE
);
1521 SecondPercent
= FirstPercent
!=NULL
?FindNextInstance(FirstPercent
+1, L
"%", TRUE
):NULL
;
1522 if (FirstPercent
== NULL
|| SecondPercent
== NULL
) {
1524 // If we ever don't have 2 % we are done.
1529 if (FirstQuote
!= NULL
&& FirstQuote
< FirstPercent
) {
1530 SecondQuote
= FindNextInstance(FirstQuote
+1, L
"\"", TRUE
);
1532 // Quote is first found
1535 if (SecondQuote
< FirstPercent
) {
1537 // restart after the pair of "
1539 CurrentLocator
= SecondQuote
+ 1;
1540 } else /* FirstPercent < SecondQuote */{
1542 // Restart on the first percent
1544 CurrentLocator
= FirstPercent
;
1549 if (FirstQuote
== NULL
|| SecondPercent
< FirstQuote
) {
1550 if (IsValidEnvironmentVariableName(FirstPercent
, SecondPercent
)) {
1552 // We need to remove from FirstPercent to SecondPercent
1554 CopyMem(FirstPercent
, SecondPercent
+ 1, StrSize(SecondPercent
+ 1));
1556 // don't need to update the locator. both % characters are gone.
1559 CurrentLocator
= SecondPercent
+ 1;
1563 CurrentLocator
= FirstQuote
;
1565 return (EFI_SUCCESS
);
1569 Function allocates a new command line and replaces all instances of environment
1570 variable names that are correctly preset to their values.
1572 If the return value is not NULL the memory must be caller freed.
1574 @param[in] OriginalCommandLine The original command line
1576 @retval NULL An error occurred.
1577 @return The new command line with no environment variables present.
1580 ShellConvertVariables (
1581 IN CONST CHAR16
*OriginalCommandLine
1584 CONST CHAR16
*MasterEnvList
;
1586 CHAR16
*NewCommandLine1
;
1587 CHAR16
*NewCommandLine2
;
1591 SCRIPT_FILE
*CurrentScriptFile
;
1592 ALIAS_LIST
*AliasListNode
;
1594 ASSERT(OriginalCommandLine
!= NULL
);
1597 NewSize
= StrSize(OriginalCommandLine
);
1598 CurrentScriptFile
= ShellCommandGetCurrentScriptFile();
1601 ///@todo update this to handle the %0 - %9 for scripting only (borrow from line 1256 area) ? ? ?
1604 // calculate the size required for the post-conversion string...
1606 if (CurrentScriptFile
!= NULL
) {
1607 for (AliasListNode
= (ALIAS_LIST
*)GetFirstNode(&CurrentScriptFile
->SubstList
)
1608 ; !IsNull(&CurrentScriptFile
->SubstList
, &AliasListNode
->Link
)
1609 ; AliasListNode
= (ALIAS_LIST
*)GetNextNode(&CurrentScriptFile
->SubstList
, &AliasListNode
->Link
)
1611 for (Temp
= StrStr(OriginalCommandLine
, AliasListNode
->Alias
)
1613 ; Temp
= StrStr(Temp
+1, AliasListNode
->Alias
)
1616 // we need a preceding and if there is space no ^ preceding (if no space ignore)
1618 if ((((Temp
-OriginalCommandLine
)>2) && *(Temp
-2) != L
'^') || ((Temp
-OriginalCommandLine
)<=2)) {
1619 NewSize
+= StrSize(AliasListNode
->CommandString
);
1625 for (MasterEnvList
= EfiShellGetEnv(NULL
)
1626 ; MasterEnvList
!= NULL
&& *MasterEnvList
!= CHAR_NULL
//&& *(MasterEnvList+1) != CHAR_NULL
1627 ; MasterEnvList
+= StrLen(MasterEnvList
) + 1
1629 if (StrSize(MasterEnvList
) > ItemSize
) {
1630 ItemSize
= StrSize(MasterEnvList
);
1632 for (Temp
= StrStr(OriginalCommandLine
, MasterEnvList
)
1634 ; Temp
= StrStr(Temp
+1, MasterEnvList
)
1637 // we need a preceding and following % and if there is space no ^ preceding (if no space ignore)
1639 if (*(Temp
-1) == L
'%' && *(Temp
+StrLen(MasterEnvList
)) == L
'%' &&
1640 ((((Temp
-OriginalCommandLine
)>2) && *(Temp
-2) != L
'^') || ((Temp
-OriginalCommandLine
)<=2))) {
1641 NewSize
+=StrSize(EfiShellGetEnv(MasterEnvList
));
1647 // now do the replacements...
1649 NewCommandLine1
= AllocateZeroPool (NewSize
);
1650 NewCommandLine2
= AllocateZeroPool(NewSize
);
1651 ItemTemp
= AllocateZeroPool(ItemSize
+(2*sizeof(CHAR16
)));
1652 if (NewCommandLine1
== NULL
|| NewCommandLine2
== NULL
|| ItemTemp
== NULL
) {
1653 SHELL_FREE_NON_NULL(NewCommandLine1
);
1654 SHELL_FREE_NON_NULL(NewCommandLine2
);
1655 SHELL_FREE_NON_NULL(ItemTemp
);
1658 CopyMem (NewCommandLine1
, OriginalCommandLine
, StrSize (OriginalCommandLine
));
1660 for (MasterEnvList
= EfiShellGetEnv(NULL
)
1661 ; MasterEnvList
!= NULL
&& *MasterEnvList
!= CHAR_NULL
1662 ; MasterEnvList
+= StrLen(MasterEnvList
) + 1
1665 ((ItemSize
+(2*sizeof(CHAR16
)))/sizeof(CHAR16
)),
1669 ((ItemSize
+(2*sizeof(CHAR16
)))/sizeof(CHAR16
)),
1673 ((ItemSize
+(2*sizeof(CHAR16
)))/sizeof(CHAR16
)),
1676 ShellCopySearchAndReplace(NewCommandLine1
, NewCommandLine2
, NewSize
, ItemTemp
, EfiShellGetEnv(MasterEnvList
), TRUE
, FALSE
);
1677 StrCpyS(NewCommandLine1
, NewSize
/sizeof(CHAR16
), NewCommandLine2
);
1679 if (CurrentScriptFile
!= NULL
) {
1680 for (AliasListNode
= (ALIAS_LIST
*)GetFirstNode(&CurrentScriptFile
->SubstList
)
1681 ; !IsNull(&CurrentScriptFile
->SubstList
, &AliasListNode
->Link
)
1682 ; AliasListNode
= (ALIAS_LIST
*)GetNextNode(&CurrentScriptFile
->SubstList
, &AliasListNode
->Link
)
1684 ShellCopySearchAndReplace(NewCommandLine1
, NewCommandLine2
, NewSize
, AliasListNode
->Alias
, AliasListNode
->CommandString
, TRUE
, FALSE
);
1685 StrCpyS(NewCommandLine1
, NewSize
/sizeof(CHAR16
), NewCommandLine2
);
1690 // Remove non-existent environment variables
1692 StripUnreplacedEnvironmentVariables(NewCommandLine1
);
1695 // Now cleanup any straggler intentionally ignored "%" characters
1697 ShellCopySearchAndReplace(NewCommandLine1
, NewCommandLine2
, NewSize
, L
"^%", L
"%", TRUE
, FALSE
);
1698 StrCpyS(NewCommandLine1
, NewSize
/sizeof(CHAR16
), NewCommandLine2
);
1700 FreePool(NewCommandLine2
);
1703 return (NewCommandLine1
);
1707 Internal function to run a command line with pipe usage.
1709 @param[in] CmdLine The pointer to the command line.
1710 @param[in] StdIn The pointer to the Standard input.
1711 @param[in] StdOut The pointer to the Standard output.
1713 @retval EFI_SUCCESS The split command is executed successfully.
1714 @retval other Some error occurs when executing the split command.
1718 IN CONST CHAR16
*CmdLine
,
1719 IN SHELL_FILE_HANDLE StdIn
,
1720 IN SHELL_FILE_HANDLE StdOut
1724 CHAR16
*NextCommandLine
;
1725 CHAR16
*OurCommandLine
;
1729 SHELL_FILE_HANDLE TempFileHandle
;
1732 ASSERT(StdOut
== NULL
);
1734 ASSERT(StrStr(CmdLine
, L
"|") != NULL
);
1736 Status
= EFI_SUCCESS
;
1737 NextCommandLine
= NULL
;
1738 OurCommandLine
= NULL
;
1742 NextCommandLine
= StrnCatGrow(&NextCommandLine
, &Size1
, StrStr(CmdLine
, L
"|")+1, 0);
1743 OurCommandLine
= StrnCatGrow(&OurCommandLine
, &Size2
, CmdLine
, StrStr(CmdLine
, L
"|") - CmdLine
);
1745 if (NextCommandLine
== NULL
|| OurCommandLine
== NULL
) {
1746 SHELL_FREE_NON_NULL(OurCommandLine
);
1747 SHELL_FREE_NON_NULL(NextCommandLine
);
1748 return (EFI_OUT_OF_RESOURCES
);
1749 } else if (StrStr(OurCommandLine
, L
"|") != NULL
|| Size1
== 0 || Size2
== 0) {
1750 SHELL_FREE_NON_NULL(OurCommandLine
);
1751 SHELL_FREE_NON_NULL(NextCommandLine
);
1752 return (EFI_INVALID_PARAMETER
);
1753 } else if (NextCommandLine
[0] == L
'a' &&
1754 (NextCommandLine
[1] == L
' ' || NextCommandLine
[1] == CHAR_NULL
)
1756 CopyMem(NextCommandLine
, NextCommandLine
+1, StrSize(NextCommandLine
) - sizeof(NextCommandLine
[0]));
1757 while (NextCommandLine
[0] == L
' ') {
1758 CopyMem(NextCommandLine
, NextCommandLine
+1, StrSize(NextCommandLine
) - sizeof(NextCommandLine
[0]));
1760 if (NextCommandLine
[0] == CHAR_NULL
) {
1761 SHELL_FREE_NON_NULL(OurCommandLine
);
1762 SHELL_FREE_NON_NULL(NextCommandLine
);
1763 return (EFI_INVALID_PARAMETER
);
1772 // make a SPLIT_LIST item and add to list
1774 Split
= AllocateZeroPool(sizeof(SPLIT_LIST
));
1775 if (Split
== NULL
) {
1776 return EFI_OUT_OF_RESOURCES
;
1778 Split
->SplitStdIn
= StdIn
;
1779 Split
->SplitStdOut
= ConvertEfiFileProtocolToShellHandle(CreateFileInterfaceMem(Unicode
), NULL
);
1780 ASSERT(Split
->SplitStdOut
!= NULL
);
1781 InsertHeadList(&ShellInfoObject
.SplitList
.Link
, &Split
->Link
);
1783 Status
= RunCommand(OurCommandLine
);
1786 // move the output from the first to the in to the second.
1788 TempFileHandle
= Split
->SplitStdOut
;
1789 if (Split
->SplitStdIn
== StdIn
) {
1790 Split
->SplitStdOut
= NULL
;
1792 Split
->SplitStdOut
= Split
->SplitStdIn
;
1794 Split
->SplitStdIn
= TempFileHandle
;
1795 ShellInfoObject
.NewEfiShellProtocol
->SetFilePosition (Split
->SplitStdIn
, 0);
1797 if (!EFI_ERROR(Status
)) {
1798 Status
= RunCommand(NextCommandLine
);
1802 // remove the top level from the ScriptList
1804 ASSERT((SPLIT_LIST
*)GetFirstNode(&ShellInfoObject
.SplitList
.Link
) == Split
);
1805 RemoveEntryList(&Split
->Link
);
1808 // Note that the original StdIn is now the StdOut...
1810 if (Split
->SplitStdOut
!= NULL
) {
1811 ShellInfoObject
.NewEfiShellProtocol
->CloseFile (Split
->SplitStdOut
);
1813 if (Split
->SplitStdIn
!= NULL
) {
1814 ShellInfoObject
.NewEfiShellProtocol
->CloseFile (Split
->SplitStdIn
);
1818 FreePool(NextCommandLine
);
1819 FreePool(OurCommandLine
);
1825 Take the original command line, substitute any variables, free
1826 the original string, return the modified copy.
1828 @param[in] CmdLine pointer to the command line to update.
1830 @retval EFI_SUCCESS the function was successful.
1831 @retval EFI_OUT_OF_RESOURCES a memory allocation failed.
1834 ShellSubstituteVariables(
1839 NewCmdLine
= ShellConvertVariables(*CmdLine
);
1840 SHELL_FREE_NON_NULL(*CmdLine
);
1841 if (NewCmdLine
== NULL
) {
1842 return (EFI_OUT_OF_RESOURCES
);
1844 *CmdLine
= NewCmdLine
;
1845 return (EFI_SUCCESS
);
1849 Take the original command line, substitute any alias in the first group of space delimited characters, free
1850 the original string, return the modified copy.
1852 @param[in] CmdLine pointer to the command line to update.
1854 @retval EFI_SUCCESS the function was successful.
1855 @retval EFI_OUT_OF_RESOURCES a memory allocation failed.
1858 ShellSubstituteAliases(
1863 CHAR16
*CommandName
;
1865 UINTN PostAliasSize
;
1866 ASSERT(CmdLine
!= NULL
);
1867 ASSERT(*CmdLine
!= NULL
);
1871 if (StrStr((*CmdLine
), L
" ") == NULL
){
1872 StrnCatGrow(&CommandName
, NULL
, (*CmdLine
), 0);
1874 StrnCatGrow(&CommandName
, NULL
, (*CmdLine
), StrStr((*CmdLine
), L
" ") - (*CmdLine
));
1878 // This cannot happen 'inline' since the CmdLine can need extra space.
1881 if (!ShellCommandIsCommandOnList(CommandName
)) {
1883 // Convert via alias
1885 Status
= ShellConvertAlias(&CommandName
);
1886 if (EFI_ERROR(Status
)){
1890 NewCmdLine
= StrnCatGrow(&NewCmdLine
, &PostAliasSize
, CommandName
, 0);
1891 if (NewCmdLine
== NULL
) {
1892 SHELL_FREE_NON_NULL(CommandName
);
1893 SHELL_FREE_NON_NULL(*CmdLine
);
1894 return (EFI_OUT_OF_RESOURCES
);
1896 NewCmdLine
= StrnCatGrow(&NewCmdLine
, &PostAliasSize
, StrStr((*CmdLine
), L
" "), 0);
1897 if (NewCmdLine
== NULL
) {
1898 SHELL_FREE_NON_NULL(CommandName
);
1899 SHELL_FREE_NON_NULL(*CmdLine
);
1900 return (EFI_OUT_OF_RESOURCES
);
1903 NewCmdLine
= StrnCatGrow(&NewCmdLine
, NULL
, (*CmdLine
), 0);
1906 SHELL_FREE_NON_NULL(*CmdLine
);
1907 SHELL_FREE_NON_NULL(CommandName
);
1910 // re-assign the passed in double pointer to point to our newly allocated buffer
1912 *CmdLine
= NewCmdLine
;
1914 return (EFI_SUCCESS
);
1918 Takes the Argv[0] part of the command line and determine the meaning of it.
1920 @param[in] CmdName pointer to the command line to update.
1922 @retval Internal_Command The name is an internal command.
1923 @retval File_Sys_Change the name is a file system change.
1924 @retval Script_File_Name the name is a NSH script file.
1925 @retval Unknown_Invalid the name is unknown.
1926 @retval Efi_Application the name is an application (.EFI).
1928 SHELL_OPERATION_TYPES
1930 IN CONST CHAR16
*CmdName
1933 CHAR16
* FileWithPath
;
1934 CONST CHAR16
* TempLocation
;
1935 CONST CHAR16
* TempLocation2
;
1937 FileWithPath
= NULL
;
1939 // test for an internal command.
1941 if (ShellCommandIsCommandOnList(CmdName
)) {
1942 return (Internal_Command
);
1946 // Test for file system change request. anything ending with first : and cant have spaces.
1948 if (CmdName
[(StrLen(CmdName
)-1)] == L
':') {
1949 if ( StrStr(CmdName
, L
" ") != NULL
1950 || StrLen(StrStr(CmdName
, L
":")) > 1
1952 return (Unknown_Invalid
);
1954 return (File_Sys_Change
);
1960 if ((FileWithPath
= ShellFindFilePathEx(CmdName
, mExecutableExtensions
)) != NULL
) {
1962 // See if that file has a script file extension
1964 if (StrLen(FileWithPath
) > 4) {
1965 TempLocation
= FileWithPath
+StrLen(FileWithPath
)-4;
1966 TempLocation2
= mScriptExtension
;
1967 if (StringNoCaseCompare((VOID
*)(&TempLocation
), (VOID
*)(&TempLocation2
)) == 0) {
1968 SHELL_FREE_NON_NULL(FileWithPath
);
1969 return (Script_File_Name
);
1974 // Was a file, but not a script. we treat this as an application.
1976 SHELL_FREE_NON_NULL(FileWithPath
);
1977 return (Efi_Application
);
1980 SHELL_FREE_NON_NULL(FileWithPath
);
1982 // No clue what this is... return invalid flag...
1984 return (Unknown_Invalid
);
1988 Determine if the first item in a command line is valid.
1990 @param[in] CmdLine The command line to parse.
1992 @retval EFI_SUCCESS The item is valid.
1993 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
1994 @retval EFI_NOT_FOUND The operation type is unknown or invalid.
1998 IN CONST CHAR16
*CmdLine
2002 CHAR16
*FirstParameter
;
2008 Temp
= StrnCatGrow(&Temp
, NULL
, CmdLine
, 0);
2010 return (EFI_OUT_OF_RESOURCES
);
2013 FirstParameter
= StrStr(Temp
, L
"|");
2014 if (FirstParameter
!= NULL
) {
2015 *FirstParameter
= CHAR_NULL
;
2018 FirstParameter
= NULL
;
2021 // Process the command line
2023 Status
= ProcessCommandLineToFinal(&Temp
);
2025 if (!EFI_ERROR(Status
)) {
2026 FirstParameter
= AllocateZeroPool(StrSize(CmdLine
));
2027 if (FirstParameter
== NULL
) {
2028 SHELL_FREE_NON_NULL(Temp
);
2029 return (EFI_OUT_OF_RESOURCES
);
2031 TempWalker
= (CHAR16
*)Temp
;
2032 if (!EFI_ERROR(GetNextParameter(&TempWalker
, &FirstParameter
, StrSize(CmdLine
), TRUE
))) {
2033 if (GetOperationType(FirstParameter
) == Unknown_Invalid
) {
2034 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_NOT_FOUND
), ShellInfoObject
.HiiHandle
, FirstParameter
);
2035 SetLastError(SHELL_NOT_FOUND
);
2036 Status
= EFI_NOT_FOUND
;
2041 SHELL_FREE_NON_NULL(Temp
);
2042 SHELL_FREE_NON_NULL(FirstParameter
);
2047 Determine if a command line contains with a split contains only valid commands.
2049 @param[in] CmdLine The command line to parse.
2051 @retval EFI_SUCCESS CmdLine has only valid commands, application, or has no split.
2052 @retval EFI_ABORTED CmdLine has at least one invalid command or application.
2056 IN CONST CHAR16
*CmdLine
2059 CONST CHAR16
*TempSpot
;
2063 // If this was the only item, then get out
2065 if (!ContainsSplit(CmdLine
)) {
2066 return (EFI_SUCCESS
);
2070 // Verify up to the pipe or end character
2072 Status
= IsValidSplit(CmdLine
);
2073 if (EFI_ERROR(Status
)) {
2078 // recurse to verify the next item
2080 TempSpot
= FindFirstCharacter(CmdLine
, L
"|", L
'^') + 1;
2081 if (*TempSpot
== L
'a' &&
2082 (*(TempSpot
+ 1) == L
' ' || *(TempSpot
+ 1) == CHAR_NULL
)
2084 // If it's an ASCII pipe '|a'
2088 return (VerifySplit(TempSpot
));
2092 Process a split based operation.
2094 @param[in] CmdLine pointer to the command line to process
2096 @retval EFI_SUCCESS The operation was successful
2097 @return an error occurred.
2100 ProcessNewSplitCommandLine(
2101 IN CONST CHAR16
*CmdLine
2107 Status
= VerifySplit(CmdLine
);
2108 if (EFI_ERROR(Status
)) {
2115 // are we in an existing split???
2117 if (!IsListEmpty(&ShellInfoObject
.SplitList
.Link
)) {
2118 Split
= (SPLIT_LIST
*)GetFirstNode(&ShellInfoObject
.SplitList
.Link
);
2121 if (Split
== NULL
) {
2122 Status
= RunSplitCommand(CmdLine
, NULL
, NULL
);
2124 Status
= RunSplitCommand(CmdLine
, Split
->SplitStdIn
, Split
->SplitStdOut
);
2126 if (EFI_ERROR(Status
)) {
2127 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_INVALID_SPLIT
), ShellInfoObject
.HiiHandle
, CmdLine
);
2133 Handle a request to change the current file system.
2135 @param[in] CmdLine The passed in command line.
2137 @retval EFI_SUCCESS The operation was successful.
2141 IN CONST CHAR16
*CmdLine
2145 Status
= EFI_SUCCESS
;
2148 // make sure we are the right operation
2150 ASSERT(CmdLine
[(StrLen(CmdLine
)-1)] == L
':' && StrStr(CmdLine
, L
" ") == NULL
);
2153 // Call the protocol API to do the work
2155 Status
= ShellInfoObject
.NewEfiShellProtocol
->SetCurDir(NULL
, CmdLine
);
2158 // Report any errors
2160 if (EFI_ERROR(Status
)) {
2161 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_INVALID_MAPPING
), ShellInfoObject
.HiiHandle
, CmdLine
);
2168 Reprocess the command line to direct all -? to the help command.
2170 if found, will add "help" as argv[0], and move the rest later.
2172 @param[in,out] CmdLine pointer to the command line to update
2176 IN OUT CHAR16
**CmdLine
2179 CHAR16
*CurrentParameter
;
2181 CHAR16
*NewCommandLine
;
2183 UINTN NewCmdLineSize
;
2185 Status
= EFI_SUCCESS
;
2187 CurrentParameter
= AllocateZeroPool(StrSize(*CmdLine
));
2188 if (CurrentParameter
== NULL
) {
2189 return (EFI_OUT_OF_RESOURCES
);
2193 while(Walker
!= NULL
&& *Walker
!= CHAR_NULL
) {
2194 if (!EFI_ERROR(GetNextParameter(&Walker
, &CurrentParameter
, StrSize(*CmdLine
), TRUE
))) {
2195 if (StrStr(CurrentParameter
, L
"-?") == CurrentParameter
) {
2196 CurrentParameter
[0] = L
' ';
2197 CurrentParameter
[1] = L
' ';
2198 NewCmdLineSize
= StrSize(L
"help ") + StrSize(*CmdLine
);
2199 NewCommandLine
= AllocateZeroPool(NewCmdLineSize
);
2200 if (NewCommandLine
== NULL
) {
2201 Status
= EFI_OUT_OF_RESOURCES
;
2206 // We know the space is sufficient since we just calculated it.
2208 StrnCpyS(NewCommandLine
, NewCmdLineSize
/sizeof(CHAR16
), L
"help ", 5);
2209 StrnCatS(NewCommandLine
, NewCmdLineSize
/sizeof(CHAR16
), *CmdLine
, StrLen(*CmdLine
));
2210 SHELL_FREE_NON_NULL(*CmdLine
);
2211 *CmdLine
= NewCommandLine
;
2217 SHELL_FREE_NON_NULL(CurrentParameter
);
2223 Function to update the shell variable "lasterror".
2225 @param[in] ErrorCode the error code to put into lasterror.
2229 IN CONST SHELL_STATUS ErrorCode
2232 CHAR16 LeString
[19];
2233 if (sizeof(EFI_STATUS
) == sizeof(UINT64
)) {
2234 UnicodeSPrint(LeString
, sizeof(LeString
), L
"0x%Lx", ErrorCode
);
2236 UnicodeSPrint(LeString
, sizeof(LeString
), L
"0x%x", ErrorCode
);
2238 DEBUG_CODE(InternalEfiShellSetEnv(L
"debuglasterror", LeString
, TRUE
););
2239 InternalEfiShellSetEnv(L
"lasterror", LeString
, TRUE
);
2241 return (EFI_SUCCESS
);
2245 Converts the command line to it's post-processed form. this replaces variables and alias' per UEFI Shell spec.
2247 @param[in,out] CmdLine pointer to the command line to update
2249 @retval EFI_SUCCESS The operation was successful
2250 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
2251 @return some other error occurred
2254 ProcessCommandLineToFinal(
2255 IN OUT CHAR16
**CmdLine
2259 TrimSpaces(CmdLine
);
2261 Status
= ShellSubstituteAliases(CmdLine
);
2262 if (EFI_ERROR(Status
)) {
2266 TrimSpaces(CmdLine
);
2268 Status
= ShellSubstituteVariables(CmdLine
);
2269 if (EFI_ERROR(Status
)) {
2272 ASSERT (*CmdLine
!= NULL
);
2274 TrimSpaces(CmdLine
);
2277 // update for help parsing
2279 if (StrStr(*CmdLine
, L
"?") != NULL
) {
2281 // This may do nothing if the ? does not indicate help.
2282 // Save all the details for in the API below.
2284 Status
= DoHelpUpdate(CmdLine
);
2287 TrimSpaces(CmdLine
);
2289 return (EFI_SUCCESS
);
2293 Run an internal shell command.
2295 This API will update the shell's environment since these commands are libraries.
2297 @param[in] CmdLine the command line to run.
2298 @param[in] FirstParameter the first parameter on the command line
2299 @param[in] ParamProtocol the shell parameters protocol pointer
2300 @param[out] CommandStatus the status from the command line.
2302 @retval EFI_SUCCESS The command was completed.
2303 @retval EFI_ABORTED The command's operation was aborted.
2307 IN CONST CHAR16
*CmdLine
,
2308 IN CHAR16
*FirstParameter
,
2309 IN EFI_SHELL_PARAMETERS_PROTOCOL
*ParamProtocol
,
2310 OUT EFI_STATUS
*CommandStatus
2316 SHELL_STATUS CommandReturnedStatus
;
2321 NewCmdLine
= AllocateCopyPool (StrSize (CmdLine
), CmdLine
);
2322 if (NewCmdLine
== NULL
) {
2323 return EFI_OUT_OF_RESOURCES
;
2326 for (Walker
= NewCmdLine
; Walker
!= NULL
&& *Walker
!= CHAR_NULL
; Walker
++) {
2327 if (*Walker
== L
'^' && *(Walker
+1) == L
'#') {
2328 CopyMem(Walker
, Walker
+1, StrSize(Walker
) - sizeof(Walker
[0]));
2333 // get the argc and argv updated for internal commands
2335 Status
= UpdateArgcArgv(ParamProtocol
, NewCmdLine
, Internal_Command
, &Argv
, &Argc
);
2336 if (!EFI_ERROR(Status
)) {
2338 // Run the internal command.
2340 Status
= ShellCommandRunCommandHandler(FirstParameter
, &CommandReturnedStatus
, &LastError
);
2342 if (!EFI_ERROR(Status
)) {
2343 if (CommandStatus
!= NULL
) {
2344 if (CommandReturnedStatus
!= SHELL_SUCCESS
) {
2345 *CommandStatus
= (EFI_STATUS
)(CommandReturnedStatus
| MAX_BIT
);
2347 *CommandStatus
= EFI_SUCCESS
;
2352 // Update last error status.
2353 // some commands do not update last error.
2356 SetLastError(CommandReturnedStatus
);
2360 // Pass thru the exitcode from the app.
2362 if (ShellCommandGetExit()) {
2364 // An Exit was requested ("exit" command), pass its value up.
2366 Status
= CommandReturnedStatus
;
2367 } else if (CommandReturnedStatus
!= SHELL_SUCCESS
&& IsScriptOnlyCommand(FirstParameter
)) {
2369 // Always abort when a script only command fails for any reason
2371 Status
= EFI_ABORTED
;
2372 } else if (ShellCommandGetCurrentScriptFile() != NULL
&& CommandReturnedStatus
== SHELL_ABORTED
) {
2374 // Abort when in a script and a command aborted
2376 Status
= EFI_ABORTED
;
2382 // This is guaranteed to be called after UpdateArgcArgv no matter what else happened.
2383 // This is safe even if the update API failed. In this case, it may be a no-op.
2385 RestoreArgcArgv(ParamProtocol
, &Argv
, &Argc
);
2388 // If a script is running and the command is not a script only command, then
2389 // change return value to success so the script won't halt (unless aborted).
2391 // Script only commands have to be able halt the script since the script will
2392 // not operate if they are failing.
2394 if ( ShellCommandGetCurrentScriptFile() != NULL
2395 && !IsScriptOnlyCommand(FirstParameter
)
2396 && Status
!= EFI_ABORTED
2398 Status
= EFI_SUCCESS
;
2401 FreePool (NewCmdLine
);
2406 Function to run the command or file.
2408 @param[in] Type the type of operation being run.
2409 @param[in] CmdLine the command line to run.
2410 @param[in] FirstParameter the first parameter on the command line
2411 @param[in] ParamProtocol the shell parameters protocol pointer
2412 @param[out] CommandStatus the status from the command line.
2414 @retval EFI_SUCCESS The command was completed.
2415 @retval EFI_ABORTED The command's operation was aborted.
2419 IN SHELL_OPERATION_TYPES Type
,
2420 IN CONST CHAR16
*CmdLine
,
2421 IN CHAR16
*FirstParameter
,
2422 IN EFI_SHELL_PARAMETERS_PROTOCOL
*ParamProtocol
,
2423 OUT EFI_STATUS
*CommandStatus
2427 EFI_STATUS StartStatus
;
2428 CHAR16
*CommandWithPath
;
2429 EFI_DEVICE_PATH_PROTOCOL
*DevPath
;
2430 SHELL_STATUS CalleeExitStatus
;
2432 Status
= EFI_SUCCESS
;
2433 CommandWithPath
= NULL
;
2435 CalleeExitStatus
= SHELL_INVALID_PARAMETER
;
2438 case Internal_Command
:
2439 Status
= RunInternalCommand(CmdLine
, FirstParameter
, ParamProtocol
, CommandStatus
);
2441 case Script_File_Name
:
2442 case Efi_Application
:
2444 // Process a fully qualified path
2446 if (StrStr(FirstParameter
, L
":") != NULL
) {
2447 ASSERT (CommandWithPath
== NULL
);
2448 if (ShellIsFile(FirstParameter
) == EFI_SUCCESS
) {
2449 CommandWithPath
= StrnCatGrow(&CommandWithPath
, NULL
, FirstParameter
, 0);
2454 // Process a relative path and also check in the path environment variable
2456 if (CommandWithPath
== NULL
) {
2457 CommandWithPath
= ShellFindFilePathEx(FirstParameter
, mExecutableExtensions
);
2461 // This should be impossible now.
2463 ASSERT(CommandWithPath
!= NULL
);
2466 // Make sure that path is not just a directory (or not found)
2468 if (!EFI_ERROR(ShellIsDirectory(CommandWithPath
))) {
2469 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_NOT_FOUND
), ShellInfoObject
.HiiHandle
, FirstParameter
);
2470 SetLastError(SHELL_NOT_FOUND
);
2473 case Script_File_Name
:
2474 Status
= RunScriptFile (CommandWithPath
, NULL
, CmdLine
, ParamProtocol
);
2476 case Efi_Application
:
2478 // Get the device path of the application image
2480 DevPath
= ShellInfoObject
.NewEfiShellProtocol
->GetDevicePathFromFilePath(CommandWithPath
);
2481 if (DevPath
== NULL
){
2482 Status
= EFI_OUT_OF_RESOURCES
;
2487 // Execute the device path
2489 Status
= InternalShellExecuteDevicePath(
2497 SHELL_FREE_NON_NULL(DevPath
);
2499 if(EFI_ERROR (Status
)) {
2500 CalleeExitStatus
= (SHELL_STATUS
) (Status
& (~MAX_BIT
));
2502 CalleeExitStatus
= (SHELL_STATUS
) StartStatus
;
2505 if (CommandStatus
!= NULL
) {
2506 *CommandStatus
= CalleeExitStatus
;
2510 // Update last error status.
2512 // Status is an EFI_STATUS. Clear top bit to convert to SHELL_STATUS
2513 SetLastError(CalleeExitStatus
);
2529 SHELL_FREE_NON_NULL(CommandWithPath
);
2535 Function to setup StdIn, StdErr, StdOut, and then run the command or file.
2537 @param[in] Type the type of operation being run.
2538 @param[in] CmdLine the command line to run.
2539 @param[in] FirstParameter the first parameter on the command line.
2540 @param[in] ParamProtocol the shell parameters protocol pointer
2541 @param[out] CommandStatus the status from the command line.
2543 @retval EFI_SUCCESS The command was completed.
2544 @retval EFI_ABORTED The command's operation was aborted.
2547 SetupAndRunCommandOrFile(
2548 IN SHELL_OPERATION_TYPES Type
,
2550 IN CHAR16
*FirstParameter
,
2551 IN EFI_SHELL_PARAMETERS_PROTOCOL
*ParamProtocol
,
2552 OUT EFI_STATUS
*CommandStatus
2556 SHELL_FILE_HANDLE OriginalStdIn
;
2557 SHELL_FILE_HANDLE OriginalStdOut
;
2558 SHELL_FILE_HANDLE OriginalStdErr
;
2559 SYSTEM_TABLE_INFO OriginalSystemTableInfo
;
2560 CONST SCRIPT_FILE
*ConstScriptFile
;
2563 // Update the StdIn, StdOut, and StdErr for redirection to environment variables, files, etc... unicode and ASCII
2565 Status
= UpdateStdInStdOutStdErr(ParamProtocol
, CmdLine
, &OriginalStdIn
, &OriginalStdOut
, &OriginalStdErr
, &OriginalSystemTableInfo
);
2568 // The StdIn, StdOut, and StdErr are set up.
2569 // Now run the command, script, or application
2571 if (!EFI_ERROR(Status
)) {
2572 TrimSpaces(&CmdLine
);
2573 Status
= RunCommandOrFile(Type
, CmdLine
, FirstParameter
, ParamProtocol
, CommandStatus
);
2579 if (EFI_ERROR(Status
)) {
2580 ConstScriptFile
= ShellCommandGetCurrentScriptFile();
2581 if (ConstScriptFile
== NULL
|| ConstScriptFile
->CurrentCommand
== NULL
) {
2582 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_ERROR
), ShellInfoObject
.HiiHandle
, (VOID
*)(Status
));
2584 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_ERROR_SCRIPT
), ShellInfoObject
.HiiHandle
, (VOID
*)(Status
), ConstScriptFile
->CurrentCommand
->Line
);
2589 // put back the original StdIn, StdOut, and StdErr
2591 RestoreStdInStdOutStdErr(ParamProtocol
, &OriginalStdIn
, &OriginalStdOut
, &OriginalStdErr
, &OriginalSystemTableInfo
);
2597 Function will process and run a command line.
2599 This will determine if the command line represents an internal shell
2600 command or dispatch an external application.
2602 @param[in] CmdLine The command line to parse.
2603 @param[out] CommandStatus The status from the command line.
2605 @retval EFI_SUCCESS The command was completed.
2606 @retval EFI_ABORTED The command's operation was aborted.
2610 IN CONST CHAR16
*CmdLine
,
2611 OUT EFI_STATUS
*CommandStatus
2615 CHAR16
*CleanOriginal
;
2616 CHAR16
*FirstParameter
;
2618 SHELL_OPERATION_TYPES Type
;
2619 CONST CHAR16
*CurDir
;
2621 ASSERT(CmdLine
!= NULL
);
2622 if (StrLen(CmdLine
) == 0) {
2623 return (EFI_SUCCESS
);
2626 Status
= EFI_SUCCESS
;
2627 CleanOriginal
= NULL
;
2629 CleanOriginal
= StrnCatGrow(&CleanOriginal
, NULL
, CmdLine
, 0);
2630 if (CleanOriginal
== NULL
) {
2631 return (EFI_OUT_OF_RESOURCES
);
2634 TrimSpaces(&CleanOriginal
);
2637 // NULL out comments (leveraged from RunScriptFileHandle() ).
2638 // The # character on a line is used to denote that all characters on the same line
2639 // and to the right of the # are to be ignored by the shell.
2640 // Afterwards, again remove spaces, in case any were between the last command-parameter and '#'.
2642 for (TempWalker
= CleanOriginal
; TempWalker
!= NULL
&& *TempWalker
!= CHAR_NULL
; TempWalker
++) {
2643 if (*TempWalker
== L
'^') {
2644 if (*(TempWalker
+ 1) == L
'#') {
2647 } else if (*TempWalker
== L
'#') {
2648 *TempWalker
= CHAR_NULL
;
2652 TrimSpaces(&CleanOriginal
);
2655 // Handle case that passed in command line is just 1 or more " " characters.
2657 if (StrLen (CleanOriginal
) == 0) {
2658 SHELL_FREE_NON_NULL(CleanOriginal
);
2659 return (EFI_SUCCESS
);
2662 Status
= ProcessCommandLineToFinal(&CleanOriginal
);
2663 if (EFI_ERROR(Status
)) {
2664 SHELL_FREE_NON_NULL(CleanOriginal
);
2669 // We don't do normal processing with a split command line (output from one command input to another)
2671 if (ContainsSplit(CleanOriginal
)) {
2672 Status
= ProcessNewSplitCommandLine(CleanOriginal
);
2673 SHELL_FREE_NON_NULL(CleanOriginal
);
2678 // We need the first parameter information so we can determine the operation type
2680 FirstParameter
= AllocateZeroPool(StrSize(CleanOriginal
));
2681 if (FirstParameter
== NULL
) {
2682 SHELL_FREE_NON_NULL(CleanOriginal
);
2683 return (EFI_OUT_OF_RESOURCES
);
2685 TempWalker
= CleanOriginal
;
2686 if (!EFI_ERROR(GetNextParameter(&TempWalker
, &FirstParameter
, StrSize(CleanOriginal
), TRUE
))) {
2688 // Depending on the first parameter we change the behavior
2690 switch (Type
= GetOperationType(FirstParameter
)) {
2691 case File_Sys_Change
:
2692 Status
= ChangeMappedDrive (FirstParameter
);
2694 case Internal_Command
:
2695 case Script_File_Name
:
2696 case Efi_Application
:
2697 Status
= SetupAndRunCommandOrFile(Type
, CleanOriginal
, FirstParameter
, ShellInfoObject
.NewShellParametersProtocol
, CommandStatus
);
2701 // Whatever was typed, it was invalid.
2703 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_NOT_FOUND
), ShellInfoObject
.HiiHandle
, FirstParameter
);
2704 SetLastError(SHELL_NOT_FOUND
);
2708 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_NOT_FOUND
), ShellInfoObject
.HiiHandle
, FirstParameter
);
2709 SetLastError(SHELL_NOT_FOUND
);
2712 // Check whether the current file system still exists. If not exist, we need update "cwd" and gShellCurMapping.
2714 CurDir
= EfiShellGetCurDir (NULL
);
2715 if (CurDir
!= NULL
) {
2716 if (EFI_ERROR(ShellFileExists (CurDir
))) {
2718 // EfiShellSetCurDir() cannot set current directory to NULL.
2719 // EfiShellSetEnv() is not allowed to set the "cwd" variable.
2720 // Only InternalEfiShellSetEnv () is allowed setting the "cwd" variable.
2722 InternalEfiShellSetEnv (L
"cwd", NULL
, TRUE
);
2723 gShellCurMapping
= NULL
;
2727 SHELL_FREE_NON_NULL(CleanOriginal
);
2728 SHELL_FREE_NON_NULL(FirstParameter
);
2734 Function will process and run a command line.
2736 This will determine if the command line represents an internal shell
2737 command or dispatch an external application.
2739 @param[in] CmdLine The command line to parse.
2741 @retval EFI_SUCCESS The command was completed.
2742 @retval EFI_ABORTED The command's operation was aborted.
2746 IN CONST CHAR16
*CmdLine
2749 return (RunShellCommand(CmdLine
, NULL
));
2753 STATIC CONST UINT16 InvalidChars
[] = {L
'*', L
'?', L
'<', L
'>', L
'\\', L
'/', L
'\"', 0x0001, 0x0002};
2755 Function determines if the CommandName COULD be a valid command. It does not determine whether
2756 this is a valid command. It only checks for invalid characters.
2758 @param[in] CommandName The name to check
2760 @retval TRUE CommandName could be a command name
2761 @retval FALSE CommandName could not be a valid command name
2765 IN CONST CHAR16
*CommandName
2769 if (CommandName
== NULL
) {
2774 ; Count
< sizeof(InvalidChars
) / sizeof(InvalidChars
[0])
2777 if (ScanMem16(CommandName
, StrSize(CommandName
), InvalidChars
[Count
]) != NULL
) {
2785 Function to process a NSH script file via SHELL_FILE_HANDLE.
2787 @param[in] Handle The handle to the already opened file.
2788 @param[in] Name The name of the script file.
2790 @retval EFI_SUCCESS the script completed successfully
2793 RunScriptFileHandle (
2794 IN SHELL_FILE_HANDLE Handle
,
2795 IN CONST CHAR16
*Name
2799 SCRIPT_FILE
*NewScriptFile
;
2801 UINTN PrintBuffSize
;
2802 CHAR16
*CommandLine
;
2803 CHAR16
*CommandLine2
;
2804 CHAR16
*CommandLine3
;
2805 SCRIPT_COMMAND_LIST
*LastCommand
;
2807 BOOLEAN PreScriptEchoState
;
2808 BOOLEAN PreCommandEchoState
;
2809 CONST CHAR16
*CurDir
;
2811 CHAR16 LeString
[50];
2812 LIST_ENTRY OldBufferList
;
2814 ASSERT(!ShellCommandGetScriptExit());
2816 PreScriptEchoState
= ShellCommandGetEchoState();
2817 PrintBuffSize
= PcdGet16(PcdShellPrintBufferSize
);
2819 NewScriptFile
= (SCRIPT_FILE
*)AllocateZeroPool(sizeof(SCRIPT_FILE
));
2820 if (NewScriptFile
== NULL
) {
2821 return (EFI_OUT_OF_RESOURCES
);
2827 ASSERT(NewScriptFile
->ScriptName
== NULL
);
2828 NewScriptFile
->ScriptName
= StrnCatGrow(&NewScriptFile
->ScriptName
, NULL
, Name
, 0);
2829 if (NewScriptFile
->ScriptName
== NULL
) {
2830 DeleteScriptFileStruct(NewScriptFile
);
2831 return (EFI_OUT_OF_RESOURCES
);
2835 // Save the parameters (used to replace %0 to %9 later on)
2837 NewScriptFile
->Argc
= ShellInfoObject
.NewShellParametersProtocol
->Argc
;
2838 if (NewScriptFile
->Argc
!= 0) {
2839 NewScriptFile
->Argv
= (CHAR16
**)AllocateZeroPool(NewScriptFile
->Argc
* sizeof(CHAR16
*));
2840 if (NewScriptFile
->Argv
== NULL
) {
2841 DeleteScriptFileStruct(NewScriptFile
);
2842 return (EFI_OUT_OF_RESOURCES
);
2844 for (LoopVar
= 0 ; LoopVar
< 10 && LoopVar
< NewScriptFile
->Argc
; LoopVar
++) {
2845 ASSERT(NewScriptFile
->Argv
[LoopVar
] == NULL
);
2846 NewScriptFile
->Argv
[LoopVar
] = StrnCatGrow(&NewScriptFile
->Argv
[LoopVar
], NULL
, ShellInfoObject
.NewShellParametersProtocol
->Argv
[LoopVar
], 0);
2847 if (NewScriptFile
->Argv
[LoopVar
] == NULL
) {
2848 DeleteScriptFileStruct(NewScriptFile
);
2849 return (EFI_OUT_OF_RESOURCES
);
2853 NewScriptFile
->Argv
= NULL
;
2856 InitializeListHead(&NewScriptFile
->CommandList
);
2857 InitializeListHead(&NewScriptFile
->SubstList
);
2860 // Now build the list of all script commands.
2863 while(!ShellFileHandleEof(Handle
)) {
2864 CommandLine
= ShellFileHandleReturnLine(Handle
, &Ascii
);
2866 if (CommandLine
== NULL
|| StrLen(CommandLine
) == 0 || CommandLine
[0] == '#') {
2867 SHELL_FREE_NON_NULL(CommandLine
);
2870 NewScriptFile
->CurrentCommand
= AllocateZeroPool(sizeof(SCRIPT_COMMAND_LIST
));
2871 if (NewScriptFile
->CurrentCommand
== NULL
) {
2872 SHELL_FREE_NON_NULL(CommandLine
);
2873 DeleteScriptFileStruct(NewScriptFile
);
2874 return (EFI_OUT_OF_RESOURCES
);
2877 NewScriptFile
->CurrentCommand
->Cl
= CommandLine
;
2878 NewScriptFile
->CurrentCommand
->Data
= NULL
;
2879 NewScriptFile
->CurrentCommand
->Line
= LineCount
;
2881 InsertTailList(&NewScriptFile
->CommandList
, &NewScriptFile
->CurrentCommand
->Link
);
2885 // Add this as the topmost script file
2887 ShellCommandSetNewScript (NewScriptFile
);
2890 // Now enumerate through the commands and run each one.
2892 CommandLine
= AllocateZeroPool(PrintBuffSize
);
2893 if (CommandLine
== NULL
) {
2894 DeleteScriptFileStruct(NewScriptFile
);
2895 return (EFI_OUT_OF_RESOURCES
);
2897 CommandLine2
= AllocateZeroPool(PrintBuffSize
);
2898 if (CommandLine2
== NULL
) {
2899 FreePool(CommandLine
);
2900 DeleteScriptFileStruct(NewScriptFile
);
2901 return (EFI_OUT_OF_RESOURCES
);
2904 for ( NewScriptFile
->CurrentCommand
= (SCRIPT_COMMAND_LIST
*)GetFirstNode(&NewScriptFile
->CommandList
)
2905 ; !IsNull(&NewScriptFile
->CommandList
, &NewScriptFile
->CurrentCommand
->Link
)
2906 ; // conditional increment in the body of the loop
2908 ASSERT(CommandLine2
!= NULL
);
2909 StrnCpyS( CommandLine2
,
2910 PrintBuffSize
/sizeof(CHAR16
),
2911 NewScriptFile
->CurrentCommand
->Cl
,
2912 PrintBuffSize
/sizeof(CHAR16
) - 1
2915 SaveBufferList(&OldBufferList
);
2918 // NULL out comments
2920 for (CommandLine3
= CommandLine2
; CommandLine3
!= NULL
&& *CommandLine3
!= CHAR_NULL
; CommandLine3
++) {
2921 if (*CommandLine3
== L
'^') {
2922 if ( *(CommandLine3
+1) == L
':') {
2923 CopyMem(CommandLine3
, CommandLine3
+1, StrSize(CommandLine3
) - sizeof(CommandLine3
[0]));
2924 } else if (*(CommandLine3
+1) == L
'#') {
2927 } else if (*CommandLine3
== L
'#') {
2928 *CommandLine3
= CHAR_NULL
;
2932 if (CommandLine2
!= NULL
&& StrLen(CommandLine2
) >= 1) {
2934 // Due to variability in starting the find and replace action we need to have both buffers the same.
2936 StrnCpyS( CommandLine
,
2937 PrintBuffSize
/sizeof(CHAR16
),
2939 PrintBuffSize
/sizeof(CHAR16
) - 1
2943 // Remove the %0 to %9 from the command line (if we have some arguments)
2945 if (NewScriptFile
->Argv
!= NULL
) {
2946 switch (NewScriptFile
->Argc
) {
2948 Status
= ShellCopySearchAndReplace(CommandLine2
, CommandLine
, PrintBuffSize
, L
"%9", NewScriptFile
->Argv
[9], FALSE
, FALSE
);
2949 ASSERT_EFI_ERROR(Status
);
2951 Status
= ShellCopySearchAndReplace(CommandLine
, CommandLine2
, PrintBuffSize
, L
"%8", NewScriptFile
->Argv
[8], FALSE
, FALSE
);
2952 ASSERT_EFI_ERROR(Status
);
2954 Status
= ShellCopySearchAndReplace(CommandLine2
, CommandLine
, PrintBuffSize
, L
"%7", NewScriptFile
->Argv
[7], FALSE
, FALSE
);
2955 ASSERT_EFI_ERROR(Status
);
2957 Status
= ShellCopySearchAndReplace(CommandLine
, CommandLine2
, PrintBuffSize
, L
"%6", NewScriptFile
->Argv
[6], FALSE
, FALSE
);
2958 ASSERT_EFI_ERROR(Status
);
2960 Status
= ShellCopySearchAndReplace(CommandLine2
, CommandLine
, PrintBuffSize
, L
"%5", NewScriptFile
->Argv
[5], FALSE
, FALSE
);
2961 ASSERT_EFI_ERROR(Status
);
2963 Status
= ShellCopySearchAndReplace(CommandLine
, CommandLine2
, PrintBuffSize
, L
"%4", NewScriptFile
->Argv
[4], FALSE
, FALSE
);
2964 ASSERT_EFI_ERROR(Status
);
2966 Status
= ShellCopySearchAndReplace(CommandLine2
, CommandLine
, PrintBuffSize
, L
"%3", NewScriptFile
->Argv
[3], FALSE
, FALSE
);
2967 ASSERT_EFI_ERROR(Status
);
2969 Status
= ShellCopySearchAndReplace(CommandLine
, CommandLine2
, PrintBuffSize
, L
"%2", NewScriptFile
->Argv
[2], FALSE
, FALSE
);
2970 ASSERT_EFI_ERROR(Status
);
2972 Status
= ShellCopySearchAndReplace(CommandLine2
, CommandLine
, PrintBuffSize
, L
"%1", NewScriptFile
->Argv
[1], FALSE
, FALSE
);
2973 ASSERT_EFI_ERROR(Status
);
2975 Status
= ShellCopySearchAndReplace(CommandLine
, CommandLine2
, PrintBuffSize
, L
"%0", NewScriptFile
->Argv
[0], FALSE
, FALSE
);
2976 ASSERT_EFI_ERROR(Status
);
2982 Status
= ShellCopySearchAndReplace(CommandLine2
, CommandLine
, PrintBuffSize
, L
"%1", L
"\"\"", FALSE
, FALSE
);
2983 Status
= ShellCopySearchAndReplace(CommandLine
, CommandLine2
, PrintBuffSize
, L
"%2", L
"\"\"", FALSE
, FALSE
);
2984 Status
= ShellCopySearchAndReplace(CommandLine2
, CommandLine
, PrintBuffSize
, L
"%3", L
"\"\"", FALSE
, FALSE
);
2985 Status
= ShellCopySearchAndReplace(CommandLine
, CommandLine2
, PrintBuffSize
, L
"%4", L
"\"\"", FALSE
, FALSE
);
2986 Status
= ShellCopySearchAndReplace(CommandLine2
, CommandLine
, PrintBuffSize
, L
"%5", L
"\"\"", FALSE
, FALSE
);
2987 Status
= ShellCopySearchAndReplace(CommandLine
, CommandLine2
, PrintBuffSize
, L
"%6", L
"\"\"", FALSE
, FALSE
);
2988 Status
= ShellCopySearchAndReplace(CommandLine2
, CommandLine
, PrintBuffSize
, L
"%7", L
"\"\"", FALSE
, FALSE
);
2989 Status
= ShellCopySearchAndReplace(CommandLine
, CommandLine2
, PrintBuffSize
, L
"%8", L
"\"\"", FALSE
, FALSE
);
2990 Status
= ShellCopySearchAndReplace(CommandLine2
, CommandLine
, PrintBuffSize
, L
"%9", L
"\"\"", FALSE
, FALSE
);
2992 StrnCpyS( CommandLine2
,
2993 PrintBuffSize
/sizeof(CHAR16
),
2995 PrintBuffSize
/sizeof(CHAR16
) - 1
2998 LastCommand
= NewScriptFile
->CurrentCommand
;
3000 for (CommandLine3
= CommandLine2
; CommandLine3
[0] == L
' ' ; CommandLine3
++);
3002 if (CommandLine3
!= NULL
&& CommandLine3
[0] == L
':' ) {
3004 // This line is a goto target / label
3007 if (CommandLine3
!= NULL
&& StrLen(CommandLine3
) > 0) {
3008 if (CommandLine3
[0] == L
'@') {
3010 // We need to save the current echo state
3011 // and disable echo for just this command.
3013 PreCommandEchoState
= ShellCommandGetEchoState();
3014 ShellCommandSetEchoState(FALSE
);
3015 Status
= RunCommand(CommandLine3
+1);
3018 // If command was "@echo -off" or "@echo -on" then don't restore echo state
3020 if (StrCmp (L
"@echo -off", CommandLine3
) != 0 &&
3021 StrCmp (L
"@echo -on", CommandLine3
) != 0) {
3023 // Now restore the pre-'@' echo state.
3025 ShellCommandSetEchoState(PreCommandEchoState
);
3028 if (ShellCommandGetEchoState()) {
3029 CurDir
= ShellInfoObject
.NewEfiShellProtocol
->GetEnv(L
"cwd");
3030 if (CurDir
!= NULL
&& StrLen(CurDir
) > 1) {
3031 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_CURDIR
), ShellInfoObject
.HiiHandle
, CurDir
);
3033 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_SHELL
), ShellInfoObject
.HiiHandle
);
3035 ShellPrintEx(-1, -1, L
"%s\r\n", CommandLine2
);
3037 Status
= RunCommand(CommandLine3
);
3041 if (ShellCommandGetScriptExit()) {
3043 // ShellCommandGetExitCode() always returns a UINT64
3045 UnicodeSPrint(LeString
, sizeof(LeString
), L
"0x%Lx", ShellCommandGetExitCode());
3046 DEBUG_CODE(InternalEfiShellSetEnv(L
"debuglasterror", LeString
, TRUE
););
3047 InternalEfiShellSetEnv(L
"lasterror", LeString
, TRUE
);
3049 ShellCommandRegisterExit(FALSE
, 0);
3050 Status
= EFI_SUCCESS
;
3051 RestoreBufferList(&OldBufferList
);
3054 if (ShellGetExecutionBreakFlag()) {
3055 RestoreBufferList(&OldBufferList
);
3058 if (EFI_ERROR(Status
)) {
3059 RestoreBufferList(&OldBufferList
);
3062 if (ShellCommandGetExit()) {
3063 RestoreBufferList(&OldBufferList
);
3068 // If that commend did not update the CurrentCommand then we need to advance it...
3070 if (LastCommand
== NewScriptFile
->CurrentCommand
) {
3071 NewScriptFile
->CurrentCommand
= (SCRIPT_COMMAND_LIST
*)GetNextNode(&NewScriptFile
->CommandList
, &NewScriptFile
->CurrentCommand
->Link
);
3072 if (!IsNull(&NewScriptFile
->CommandList
, &NewScriptFile
->CurrentCommand
->Link
)) {
3073 NewScriptFile
->CurrentCommand
->Reset
= TRUE
;
3077 NewScriptFile
->CurrentCommand
= (SCRIPT_COMMAND_LIST
*)GetNextNode(&NewScriptFile
->CommandList
, &NewScriptFile
->CurrentCommand
->Link
);
3078 if (!IsNull(&NewScriptFile
->CommandList
, &NewScriptFile
->CurrentCommand
->Link
)) {
3079 NewScriptFile
->CurrentCommand
->Reset
= TRUE
;
3082 RestoreBufferList(&OldBufferList
);
3086 FreePool(CommandLine
);
3087 FreePool(CommandLine2
);
3088 ShellCommandSetNewScript (NULL
);
3091 // Only if this was the last script reset the state.
3093 if (ShellCommandGetCurrentScriptFile()==NULL
) {
3094 ShellCommandSetEchoState(PreScriptEchoState
);
3096 return (EFI_SUCCESS
);
3100 Function to process a NSH script file.
3102 @param[in] ScriptPath Pointer to the script file name (including file system path).
3103 @param[in] Handle the handle of the script file already opened.
3104 @param[in] CmdLine the command line to run.
3105 @param[in] ParamProtocol the shell parameters protocol pointer
3107 @retval EFI_SUCCESS the script completed successfully
3111 IN CONST CHAR16
*ScriptPath
,
3112 IN SHELL_FILE_HANDLE Handle OPTIONAL
,
3113 IN CONST CHAR16
*CmdLine
,
3114 IN EFI_SHELL_PARAMETERS_PROTOCOL
*ParamProtocol
3118 SHELL_FILE_HANDLE FileHandle
;
3122 if (ShellIsFile(ScriptPath
) != EFI_SUCCESS
) {
3123 return (EFI_INVALID_PARAMETER
);
3127 // get the argc and argv updated for scripts
3129 Status
= UpdateArgcArgv(ParamProtocol
, CmdLine
, Script_File_Name
, &Argv
, &Argc
);
3130 if (!EFI_ERROR(Status
)) {
3132 if (Handle
== NULL
) {
3136 Status
= ShellOpenFileByName(ScriptPath
, &FileHandle
, EFI_FILE_MODE_READ
, 0);
3137 if (!EFI_ERROR(Status
)) {
3141 Status
= RunScriptFileHandle(FileHandle
, ScriptPath
);
3144 // now close the file
3146 ShellCloseFile(&FileHandle
);
3149 Status
= RunScriptFileHandle(Handle
, ScriptPath
);
3154 // This is guaranteed to be called after UpdateArgcArgv no matter what else happened.
3155 // This is safe even if the update API failed. In this case, it may be a no-op.
3157 RestoreArgcArgv(ParamProtocol
, &Argv
, &Argc
);
3163 Return the pointer to the first occurrence of any character from a list of characters.
3165 @param[in] String the string to parse
3166 @param[in] CharacterList the list of character to look for
3167 @param[in] EscapeCharacter An escape character to skip
3169 @return the location of the first character in the string
3170 @retval CHAR_NULL no instance of any character in CharacterList was found in String
3174 IN CONST CHAR16
*String
,
3175 IN CONST CHAR16
*CharacterList
,
3176 IN CONST CHAR16 EscapeCharacter
3182 for (WalkStr
= 0; WalkStr
< StrLen(String
); WalkStr
++) {
3183 if (String
[WalkStr
] == EscapeCharacter
) {
3187 for (WalkChar
= 0; WalkChar
< StrLen(CharacterList
); WalkChar
++) {
3188 if (String
[WalkStr
] == CharacterList
[WalkChar
]) {
3189 return (&String
[WalkStr
]);
3193 return (String
+ StrLen(String
));