2 This is THE shell (application)
4 Copyright (c) 2009 - 2015, Intel Corporation. All rights reserved.<BR>
5 (C) Copyright 2013-2014, Hewlett-Packard Development Company, L.P.
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
= {
56 {{NULL
, NULL
}, NULL
, NULL
},
57 {{NULL
, NULL
}, NULL
, NULL
},
69 STATIC CONST CHAR16 mScriptExtension
[] = L
".NSH";
70 STATIC CONST CHAR16 mExecutableExtensions
[] = L
".NSH;.EFI";
71 STATIC CONST CHAR16 mStartupScript
[] = L
"startup.nsh";
74 Cleans off leading and trailing spaces and tabs.
76 @param[in] String pointer to the string to trim them off.
84 ASSERT(String
!= NULL
);
85 ASSERT(*String
!= NULL
);
87 // Remove any spaces and tabs at the beginning of the (*String).
89 while (((*String
)[0] == L
' ') || ((*String
)[0] == L
'\t')) {
90 CopyMem((*String
), (*String
)+1, StrSize((*String
)) - sizeof((*String
)[0]));
94 // Remove any spaces and tabs at the end of the (*String).
96 while ((StrLen (*String
) > 0) && (((*String
)[StrLen((*String
))-1] == L
' ') || ((*String
)[StrLen((*String
))-1] == L
'\t'))) {
97 (*String
)[StrLen((*String
))-1] = CHAR_NULL
;
100 return (EFI_SUCCESS
);
104 Parse for the next instance of one string within another string. Can optionally make sure that
105 the string was not escaped (^ character) per the shell specification.
107 @param[in] SourceString The string to search within
108 @param[in] FindString The string to look for
109 @param[in] CheckForEscapeCharacter TRUE to skip escaped instances of FinfString, otherwise will return even escaped instances
114 IN CONST CHAR16
*SourceString
,
115 IN CONST CHAR16
*FindString
,
116 IN CONST BOOLEAN CheckForEscapeCharacter
120 if (SourceString
== NULL
) {
123 Temp
= StrStr(SourceString
, FindString
);
126 // If nothing found, or we dont care about escape characters
128 if (Temp
== NULL
|| !CheckForEscapeCharacter
) {
133 // If we found an escaped character, try again on the remainder of the string
135 if ((Temp
> (SourceString
)) && *(Temp
-1) == L
'^') {
136 return FindNextInstance(Temp
+1, FindString
, CheckForEscapeCharacter
);
140 // we found the right character
146 Check whether the string between a pair of % is a valid envifronment variable name.
148 @param[in] BeginPercent pointer to the first percent.
149 @param[in] EndPercent pointer to the last percent.
151 @retval TRUE is a valid environment variable name.
152 @retval FALSE is NOT a valid environment variable name.
155 IsValidEnvironmentVariableName(
156 IN CONST CHAR16
*BeginPercent
,
157 IN CONST CHAR16
*EndPercent
160 CONST CHAR16
*Walker
;
164 ASSERT (BeginPercent
!= NULL
);
165 ASSERT (EndPercent
!= NULL
);
166 ASSERT (BeginPercent
< EndPercent
);
168 if ((BeginPercent
+ 1) == EndPercent
) {
172 for (Walker
= BeginPercent
+ 1; Walker
< EndPercent
; Walker
++) {
174 (*Walker
>= L
'0' && *Walker
<= L
'9') ||
175 (*Walker
>= L
'A' && *Walker
<= L
'Z') ||
176 (*Walker
>= L
'a' && *Walker
<= L
'z') ||
179 if (Walker
== BeginPercent
+ 1 && (*Walker
>= L
'0' && *Walker
<= L
'9')) {
193 Find a command line contains a split operation
195 @param[in] CmdLine The command line to parse.
197 @retval A pointer to the | character in CmdLine or NULL if not present.
202 IN CONST CHAR16
*CmdLine
205 CONST CHAR16
*TempSpot
;
207 if (StrStr(CmdLine
, L
"|") != NULL
) {
208 for (TempSpot
= CmdLine
; TempSpot
!= NULL
&& *TempSpot
!= CHAR_NULL
; TempSpot
++) {
209 if (*TempSpot
== L
'^' && *(TempSpot
+1) == L
'|') {
211 } else if (*TempSpot
== L
'|') {
220 Determine if a command line contains a split operation
222 @param[in] CmdLine The command line to parse.
224 @retval TRUE CmdLine has a valid split.
225 @retval FALSE CmdLine does not have a valid split.
230 IN CONST CHAR16
*CmdLine
233 CONST CHAR16
*TempSpot
;
234 CONST CHAR16
*FirstQuote
;
235 CONST CHAR16
*SecondQuote
;
237 FirstQuote
= FindNextInstance (CmdLine
, L
"\"", TRUE
);
239 TempSpot
= FindSplit(CmdLine
);
241 if (FirstQuote
== NULL
||
243 TempSpot
== CHAR_NULL
||
244 FirstQuote
> TempSpot
246 return (BOOLEAN
) ((TempSpot
!= NULL
) && (*TempSpot
!= CHAR_NULL
));
249 while ((TempSpot
!= NULL
) && (*TempSpot
!= CHAR_NULL
)) {
250 if (FirstQuote
== NULL
|| FirstQuote
> TempSpot
) {
253 SecondQuote
= FindNextInstance (FirstQuote
+ 1, L
"\"", TRUE
);
254 if (SecondQuote
== NULL
) {
257 if (SecondQuote
< TempSpot
) {
258 FirstQuote
= FindNextInstance (SecondQuote
+ 1, L
"\"", TRUE
);
261 FirstQuote
= FindNextInstance (SecondQuote
+ 1, L
"\"", TRUE
);
262 TempSpot
= FindSplit(TempSpot
+ 1);
267 return (BOOLEAN
) ((TempSpot
!= NULL
) && (*TempSpot
!= CHAR_NULL
));
271 Function to start monitoring for CTRL-S using SimpleTextInputEx. This
272 feature's enabled state was not known when the shell initially launched.
274 @retval EFI_SUCCESS The feature is enabled.
275 @retval EFI_OUT_OF_RESOURCES There is not enough mnemory available.
279 InternalEfiShellStartCtrlSMonitor(
283 EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL
*SimpleEx
;
284 EFI_KEY_DATA KeyData
;
287 Status
= gBS
->OpenProtocol(
288 gST
->ConsoleInHandle
,
289 &gEfiSimpleTextInputExProtocolGuid
,
293 EFI_OPEN_PROTOCOL_GET_PROTOCOL
);
294 if (EFI_ERROR(Status
)) {
299 STRING_TOKEN (STR_SHELL_NO_IN_EX
),
300 ShellInfoObject
.HiiHandle
);
301 return (EFI_SUCCESS
);
304 KeyData
.KeyState
.KeyToggleState
= 0;
305 KeyData
.Key
.ScanCode
= 0;
306 KeyData
.KeyState
.KeyShiftState
= EFI_SHIFT_STATE_VALID
|EFI_LEFT_CONTROL_PRESSED
;
307 KeyData
.Key
.UnicodeChar
= L
's';
309 Status
= SimpleEx
->RegisterKeyNotify(
312 NotificationFunction
,
313 &ShellInfoObject
.CtrlSNotifyHandle1
);
315 KeyData
.KeyState
.KeyShiftState
= EFI_SHIFT_STATE_VALID
|EFI_RIGHT_CONTROL_PRESSED
;
316 if (!EFI_ERROR(Status
)) {
317 Status
= SimpleEx
->RegisterKeyNotify(
320 NotificationFunction
,
321 &ShellInfoObject
.CtrlSNotifyHandle2
);
323 KeyData
.KeyState
.KeyShiftState
= EFI_SHIFT_STATE_VALID
|EFI_LEFT_CONTROL_PRESSED
;
324 KeyData
.Key
.UnicodeChar
= 19;
326 if (!EFI_ERROR(Status
)) {
327 Status
= SimpleEx
->RegisterKeyNotify(
330 NotificationFunction
,
331 &ShellInfoObject
.CtrlSNotifyHandle3
);
333 KeyData
.KeyState
.KeyShiftState
= EFI_SHIFT_STATE_VALID
|EFI_RIGHT_CONTROL_PRESSED
;
334 if (!EFI_ERROR(Status
)) {
335 Status
= SimpleEx
->RegisterKeyNotify(
338 NotificationFunction
,
339 &ShellInfoObject
.CtrlSNotifyHandle4
);
347 The entry point for the application.
349 @param[in] ImageHandle The firmware allocated handle for the EFI image.
350 @param[in] SystemTable A pointer to the EFI System Table.
352 @retval EFI_SUCCESS The entry point is executed successfully.
353 @retval other Some error occurs when executing this entry point.
359 IN EFI_HANDLE ImageHandle
,
360 IN EFI_SYSTEM_TABLE
*SystemTable
366 EFI_HANDLE ConInHandle
;
367 EFI_SIMPLE_TEXT_INPUT_PROTOCOL
*OldConIn
;
369 if (PcdGet8(PcdShellSupportLevel
) > 3) {
370 return (EFI_UNSUPPORTED
);
376 Status
= gST
->ConOut
->ClearScreen(gST
->ConOut
);
377 if (EFI_ERROR(Status
)) {
382 // Populate the global structure from PCDs
384 ShellInfoObject
.ImageDevPath
= NULL
;
385 ShellInfoObject
.FileDevPath
= NULL
;
386 ShellInfoObject
.PageBreakEnabled
= PcdGetBool(PcdShellPageBreakDefault
);
387 ShellInfoObject
.ViewingSettings
.InsertMode
= PcdGetBool(PcdShellInsertModeDefault
);
388 ShellInfoObject
.LogScreenCount
= PcdGet8 (PcdShellScreenLogCount
);
391 // verify we dont allow for spec violation
393 ASSERT(ShellInfoObject
.LogScreenCount
>= 3);
396 // Initialize the LIST ENTRY objects...
398 InitializeListHead(&ShellInfoObject
.BufferToFreeList
.Link
);
399 InitializeListHead(&ShellInfoObject
.ViewingSettings
.CommandHistory
.Link
);
400 InitializeListHead(&ShellInfoObject
.SplitList
.Link
);
403 // Check PCDs for optional features that are not implemented yet.
405 if ( PcdGetBool(PcdShellSupportOldProtocols
)
406 || !FeaturePcdGet(PcdShellRequireHiiPlatform
)
407 || FeaturePcdGet(PcdShellSupportFrameworkHii
)
409 return (EFI_UNSUPPORTED
);
413 // turn off the watchdog timer
415 gBS
->SetWatchdogTimer (0, 0, 0, NULL
);
418 // install our console logger. This will keep a log of the output for back-browsing
420 Status
= ConsoleLoggerInstall(ShellInfoObject
.LogScreenCount
, &ShellInfoObject
.ConsoleInfo
);
421 if (!EFI_ERROR(Status
)) {
423 // Enable the cursor to be visible
425 gST
->ConOut
->EnableCursor (gST
->ConOut
, TRUE
);
428 // If supporting EFI 1.1 we need to install HII protocol
429 // only do this if PcdShellRequireHiiPlatform == FALSE
431 // remove EFI_UNSUPPORTED check above when complete.
432 ///@todo add support for Framework HII
435 // install our (solitary) HII package
437 ShellInfoObject
.HiiHandle
= HiiAddPackages (&gEfiCallerIdGuid
, gImageHandle
, ShellStrings
, NULL
);
438 if (ShellInfoObject
.HiiHandle
== NULL
) {
439 if (PcdGetBool(PcdShellSupportFrameworkHii
)) {
440 ///@todo Add our package into Framework HII
442 if (ShellInfoObject
.HiiHandle
== NULL
) {
443 Status
= EFI_NOT_STARTED
;
449 // create and install the EfiShellParametersProtocol
451 Status
= CreatePopulateInstallShellParametersProtocol(&ShellInfoObject
.NewShellParametersProtocol
, &ShellInfoObject
.RootShellInstance
);
452 ASSERT_EFI_ERROR(Status
);
453 ASSERT(ShellInfoObject
.NewShellParametersProtocol
!= NULL
);
456 // create and install the EfiShellProtocol
458 Status
= CreatePopulateInstallShellProtocol(&ShellInfoObject
.NewEfiShellProtocol
);
459 ASSERT_EFI_ERROR(Status
);
460 ASSERT(ShellInfoObject
.NewEfiShellProtocol
!= NULL
);
463 // Now initialize the shell library (it requires Shell Parameters protocol)
465 Status
= ShellInitialize();
466 ASSERT_EFI_ERROR(Status
);
468 Status
= CommandInit();
469 ASSERT_EFI_ERROR(Status
);
472 // Check the command line
474 Status
= ProcessCommandLine ();
475 if (EFI_ERROR (Status
)) {
480 // If shell support level is >= 1 create the mappings and paths
482 if (PcdGet8(PcdShellSupportLevel
) >= 1) {
483 Status
= ShellCommandCreateInitialMappingsAndPaths();
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 sucessfully.
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 sucessful return the device path to the loaded image.
800 @param[in, out] FilePath On a sucessful return the device path to the file.
802 @retval EFI_SUCCESS The 2 device paths were sucessfully 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
.Delay
= 5;
924 // Start LoopVar at 0 to parse only optional arguments at Argv[0]
925 // and parse other parameters from Argv[1]. This is for use case that
926 // UEFI Shell boot option is created, and OptionalData is provided
927 // that starts with shell command-line options.
929 for (LoopVar
= 0 ; LoopVar
< gEfiShellParametersProtocol
->Argc
; LoopVar
++) {
930 CurrentArg
= gEfiShellParametersProtocol
->Argv
[LoopVar
];
931 if (UnicodeCollation
->StriColl (
936 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.Startup
= TRUE
;
938 else if (UnicodeCollation
->StriColl (
943 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoStartup
= TRUE
;
945 else if (UnicodeCollation
->StriColl (
950 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoConsoleOut
= TRUE
;
952 else if (UnicodeCollation
->StriColl (
957 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoConsoleIn
= TRUE
;
959 else if (UnicodeCollation
->StriColl (
964 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoInterrupt
= TRUE
;
966 else if (UnicodeCollation
->StriColl (
971 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoMap
= TRUE
;
973 else if (UnicodeCollation
->StriColl (
978 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoVersion
= TRUE
;
980 else if (UnicodeCollation
->StriColl (
985 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.Delay
= TRUE
;
986 // Check for optional delay value following "-delay"
987 DelayValueStr
= gEfiShellParametersProtocol
->Argv
[LoopVar
+ 1];
988 if (DelayValueStr
!= NULL
){
989 if (*DelayValueStr
== L
':') {
992 if (!EFI_ERROR(ShellConvertStringToUint64 (
998 ShellInfoObject
.ShellInitSettings
.Delay
= (UINTN
)DelayValue
;
1002 } else if (UnicodeCollation
->StriColl (
1007 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.Exit
= TRUE
;
1008 } else if (StrnCmp (L
"-", CurrentArg
, 1) == 0) {
1009 // Unrecognised option
1010 ShellPrintHiiEx(-1, -1, NULL
,
1011 STRING_TOKEN (STR_GEN_PROBLEM
),
1012 ShellInfoObject
.HiiHandle
,
1015 return EFI_INVALID_PARAMETER
;
1018 // First argument should be Shell.efi image name
1024 ShellInfoObject
.ShellInitSettings
.FileName
= AllocateCopyPool(StrSize(CurrentArg
), CurrentArg
);
1025 if (ShellInfoObject
.ShellInitSettings
.FileName
== NULL
) {
1026 return (EFI_OUT_OF_RESOURCES
);
1029 // We found `file-name`.
1031 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoStartup
= 1;
1034 // Add `file-name-options`
1035 for (Size
= 0 ; LoopVar
< gEfiShellParametersProtocol
->Argc
; LoopVar
++) {
1036 ASSERT((ShellInfoObject
.ShellInitSettings
.FileOptions
== NULL
&& Size
== 0) || (ShellInfoObject
.ShellInitSettings
.FileOptions
!= NULL
));
1037 StrnCatGrow(&ShellInfoObject
.ShellInitSettings
.FileOptions
,
1041 if (ShellInfoObject
.ShellInitSettings
.FileOptions
== NULL
) {
1042 SHELL_FREE_NON_NULL(ShellInfoObject
.ShellInitSettings
.FileName
);
1043 return (EFI_OUT_OF_RESOURCES
);
1045 StrnCatGrow(&ShellInfoObject
.ShellInitSettings
.FileOptions
,
1047 gEfiShellParametersProtocol
->Argv
[LoopVar
],
1049 if (ShellInfoObject
.ShellInitSettings
.FileOptions
== NULL
) {
1050 SHELL_FREE_NON_NULL(ShellInfoObject
.ShellInitSettings
.FileName
);
1051 return (EFI_OUT_OF_RESOURCES
);
1057 // "-nointerrupt" overrides "-delay"
1058 if (ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoInterrupt
) {
1059 ShellInfoObject
.ShellInitSettings
.Delay
= 0;
1066 Handles all interaction with the default startup script.
1068 this will check that the correct command line parameters were passed, handle the delay, and then start running the script.
1070 @param ImagePath the path to the image for shell. first place to look for the startup script
1071 @param FilePath the path to the file for shell. second place to look for the startup script.
1073 @retval EFI_SUCCESS the variable is initialized.
1078 IN EFI_DEVICE_PATH_PROTOCOL
*ImagePath
,
1079 IN EFI_DEVICE_PATH_PROTOCOL
*FilePath
1085 SHELL_FILE_HANDLE FileHandle
;
1086 EFI_DEVICE_PATH_PROTOCOL
*NewPath
;
1087 EFI_DEVICE_PATH_PROTOCOL
*NamePath
;
1088 CHAR16
*FileStringPath
;
1091 CONST CHAR16
*MapName
;
1093 Key
.UnicodeChar
= CHAR_NULL
;
1097 if (!ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.Startup
&& ShellInfoObject
.ShellInitSettings
.FileName
!= NULL
) {
1099 // launch something else instead
1101 NewSize
= StrSize(ShellInfoObject
.ShellInitSettings
.FileName
);
1102 if (ShellInfoObject
.ShellInitSettings
.FileOptions
!= NULL
) {
1103 NewSize
+= StrSize(ShellInfoObject
.ShellInitSettings
.FileOptions
) + sizeof(CHAR16
);
1105 FileStringPath
= AllocateZeroPool(NewSize
);
1106 if (FileStringPath
== NULL
) {
1107 return (EFI_OUT_OF_RESOURCES
);
1109 StrnCpy(FileStringPath
, ShellInfoObject
.ShellInitSettings
.FileName
, NewSize
/sizeof(CHAR16
) -1);
1110 if (ShellInfoObject
.ShellInitSettings
.FileOptions
!= NULL
) {
1111 StrnCat(FileStringPath
, L
" ", NewSize
/sizeof(CHAR16
) - StrLen(FileStringPath
) -1);
1112 StrnCat(FileStringPath
, ShellInfoObject
.ShellInitSettings
.FileOptions
, NewSize
/sizeof(CHAR16
) - StrLen(FileStringPath
) -1);
1114 Status
= RunCommand(FileStringPath
);
1115 FreePool(FileStringPath
);
1121 // for shell level 0 we do no scripts
1122 // Without the Startup bit overriding we allow for nostartup to prevent scripts
1124 if ( (PcdGet8(PcdShellSupportLevel
) < 1)
1125 || (ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoStartup
&& !ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.Startup
)
1127 return (EFI_SUCCESS
);
1130 gST
->ConOut
->EnableCursor(gST
->ConOut
, FALSE
);
1132 // print out our warning and see if they press a key
1134 for ( Status
= EFI_UNSUPPORTED
, Delay
= ShellInfoObject
.ShellInitSettings
.Delay
1135 ; Delay
!= 0 && EFI_ERROR(Status
)
1138 ShellPrintHiiEx(0, gST
->ConOut
->Mode
->CursorRow
, NULL
, STRING_TOKEN (STR_SHELL_STARTUP_QUESTION
), ShellInfoObject
.HiiHandle
, Delay
);
1139 gBS
->Stall (1000000);
1140 if (!ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoConsoleIn
) {
1141 Status
= gST
->ConIn
->ReadKeyStroke (gST
->ConIn
, &Key
);
1144 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_CRLF
), ShellInfoObject
.HiiHandle
);
1145 gST
->ConOut
->EnableCursor(gST
->ConOut
, TRUE
);
1150 if (Status
== EFI_SUCCESS
&& Key
.UnicodeChar
== 0 && Key
.ScanCode
== SCAN_ESC
) {
1151 return (EFI_SUCCESS
);
1155 // Try the first location (must be file system)
1157 MapName
= ShellInfoObject
.NewEfiShellProtocol
->GetMapFromDevicePath(&ImagePath
);
1158 if (MapName
!= NULL
) {
1159 FileStringPath
= NULL
;
1161 FileStringPath
= StrnCatGrow(&FileStringPath
, &NewSize
, MapName
, 0);
1162 if (FileStringPath
== NULL
) {
1163 Status
= EFI_OUT_OF_RESOURCES
;
1165 TempSpot
= StrStr(FileStringPath
, L
";");
1166 if (TempSpot
!= NULL
) {
1167 *TempSpot
= CHAR_NULL
;
1169 FileStringPath
= StrnCatGrow(&FileStringPath
, &NewSize
, ((FILEPATH_DEVICE_PATH
*)FilePath
)->PathName
, 0);
1170 PathRemoveLastItem(FileStringPath
);
1171 FileStringPath
= StrnCatGrow(&FileStringPath
, &NewSize
, mStartupScript
, 0);
1172 Status
= ShellInfoObject
.NewEfiShellProtocol
->OpenFileByName(FileStringPath
, &FileHandle
, EFI_FILE_MODE_READ
);
1173 FreePool(FileStringPath
);
1176 if (EFI_ERROR(Status
)) {
1177 NamePath
= FileDevicePath (NULL
, mStartupScript
);
1178 NewPath
= AppendDevicePathNode (ImagePath
, NamePath
);
1184 Status
= InternalOpenFileDevicePath(NewPath
, &FileHandle
, EFI_FILE_MODE_READ
, 0);
1188 // If we got a file, run it
1190 if (!EFI_ERROR(Status
) && FileHandle
!= NULL
) {
1191 Status
= RunScriptFile (mStartupScript
, FileHandle
, L
"", ShellInfoObject
.NewShellParametersProtocol
);
1192 ShellInfoObject
.NewEfiShellProtocol
->CloseFile(FileHandle
);
1194 FileStringPath
= ShellFindFilePath(mStartupScript
);
1195 if (FileStringPath
== NULL
) {
1197 // we return success since we dont need to have a startup script
1199 Status
= EFI_SUCCESS
;
1200 ASSERT(FileHandle
== NULL
);
1202 Status
= RunScriptFile(FileStringPath
, NULL
, L
"", ShellInfoObject
.NewShellParametersProtocol
);
1203 FreePool(FileStringPath
);
1212 Function to perform the shell prompt looping. It will do a single prompt,
1213 dispatch the result, and then return. It is expected that the caller will
1214 call this function in a loop many times.
1217 @retval RETURN_ABORTED
1228 CONST CHAR16
*CurDir
;
1235 // Get screen setting to decide size of the command line buffer
1237 gST
->ConOut
->QueryMode (gST
->ConOut
, gST
->ConOut
->Mode
->Mode
, &Column
, &Row
);
1238 BufferSize
= Column
* Row
* sizeof (CHAR16
);
1239 CmdLine
= AllocateZeroPool (BufferSize
);
1240 if (CmdLine
== NULL
) {
1241 return EFI_OUT_OF_RESOURCES
;
1244 CurDir
= ShellInfoObject
.NewEfiShellProtocol
->GetEnv(L
"cwd");
1249 gST
->ConOut
->SetCursorPosition (gST
->ConOut
, 0, gST
->ConOut
->Mode
->CursorRow
);
1251 if (CurDir
!= NULL
&& StrLen(CurDir
) > 1) {
1252 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_CURDIR
), ShellInfoObject
.HiiHandle
, CurDir
);
1254 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_SHELL
), ShellInfoObject
.HiiHandle
);
1258 // Read a line from the console
1260 Status
= ShellInfoObject
.NewEfiShellProtocol
->ReadFile(ShellInfoObject
.NewShellParametersProtocol
->StdIn
, &BufferSize
, CmdLine
);
1263 // Null terminate the string and parse it
1265 if (!EFI_ERROR (Status
)) {
1266 CmdLine
[BufferSize
/ sizeof (CHAR16
)] = CHAR_NULL
;
1267 Status
= RunCommand(CmdLine
);
1271 // Done with this command
1278 Add a buffer to the Buffer To Free List for safely returning buffers to other
1279 places without risking letting them modify internal shell information.
1281 @param Buffer Something to pass to FreePool when the shell is exiting.
1285 AddBufferToFreeList(
1289 BUFFER_LIST
*BufferListEntry
;
1291 if (Buffer
== NULL
) {
1295 BufferListEntry
= AllocateZeroPool(sizeof(BUFFER_LIST
));
1296 ASSERT(BufferListEntry
!= NULL
);
1297 BufferListEntry
->Buffer
= Buffer
;
1298 InsertTailList(&ShellInfoObject
.BufferToFreeList
.Link
, &BufferListEntry
->Link
);
1303 Add a buffer to the Line History List
1305 @param Buffer The line buffer to add.
1309 AddLineToCommandHistory(
1310 IN CONST CHAR16
*Buffer
1315 Node
= AllocateZeroPool(sizeof(BUFFER_LIST
));
1316 ASSERT(Node
!= NULL
);
1317 Node
->Buffer
= AllocateCopyPool(StrSize(Buffer
), Buffer
);
1318 ASSERT(Node
->Buffer
!= NULL
);
1320 InsertTailList(&ShellInfoObject
.ViewingSettings
.CommandHistory
.Link
, &Node
->Link
);
1324 Checks if a string is an alias for another command. If yes, then it replaces the alias name
1325 with the correct command name.
1327 @param[in, out] CommandString Upon entry the potential alias. Upon return the
1328 command name if it was an alias. If it was not
1329 an alias it will be unchanged. This function may
1330 change the buffer to fit the command name.
1332 @retval EFI_SUCCESS The name was changed.
1333 @retval EFI_SUCCESS The name was not an alias.
1334 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
1339 IN OUT CHAR16
**CommandString
1342 CONST CHAR16
*NewString
;
1344 NewString
= ShellInfoObject
.NewEfiShellProtocol
->GetAlias(*CommandString
, NULL
);
1345 if (NewString
== NULL
) {
1346 return (EFI_SUCCESS
);
1348 FreePool(*CommandString
);
1349 *CommandString
= AllocateCopyPool(StrSize(NewString
), NewString
);
1350 if (*CommandString
== NULL
) {
1351 return (EFI_OUT_OF_RESOURCES
);
1353 return (EFI_SUCCESS
);
1357 This function will eliminate unreplaced (and therefore non-found) environment variables.
1359 @param[in,out] CmdLine The command line to update.
1363 StripUnreplacedEnvironmentVariables(
1364 IN OUT CHAR16
*CmdLine
1367 CHAR16
*FirstPercent
;
1369 CHAR16
*SecondPercent
;
1370 CHAR16
*SecondQuote
;
1371 CHAR16
*CurrentLocator
;
1373 for (CurrentLocator
= CmdLine
; CurrentLocator
!= NULL
; ) {
1374 FirstQuote
= FindNextInstance(CurrentLocator
, L
"\"", TRUE
);
1375 FirstPercent
= FindNextInstance(CurrentLocator
, L
"%", TRUE
);
1376 SecondPercent
= FirstPercent
!=NULL
?FindNextInstance(FirstPercent
+1, L
"%", TRUE
):NULL
;
1377 if (FirstPercent
== NULL
|| SecondPercent
== NULL
) {
1379 // If we ever dont have 2 % we are done.
1384 if (FirstQuote
!= NULL
&& FirstQuote
< FirstPercent
) {
1385 SecondQuote
= FindNextInstance(FirstQuote
+1, L
"\"", TRUE
);
1387 // Quote is first found
1390 if (SecondQuote
< FirstPercent
) {
1392 // restart after the pair of "
1394 CurrentLocator
= SecondQuote
+ 1;
1395 } else /* FirstPercent < SecondQuote */{
1397 // Restart on the first percent
1399 CurrentLocator
= FirstPercent
;
1404 if (FirstQuote
== NULL
|| SecondPercent
< FirstQuote
) {
1405 if (IsValidEnvironmentVariableName(FirstPercent
, SecondPercent
)) {
1407 // We need to remove from FirstPercent to SecondPercent
1409 CopyMem(FirstPercent
, SecondPercent
+ 1, StrSize(SecondPercent
+ 1));
1411 // dont need to update the locator. both % characters are gone.
1414 CurrentLocator
= SecondPercent
+ 1;
1418 CurrentLocator
= FirstQuote
;
1420 return (EFI_SUCCESS
);
1424 Function allocates a new command line and replaces all instances of environment
1425 variable names that are correctly preset to their values.
1427 If the return value is not NULL the memory must be caller freed.
1429 @param[in] OriginalCommandLine The original command line
1431 @retval NULL An error ocurred.
1432 @return The new command line with no environment variables present.
1436 ShellConvertVariables (
1437 IN CONST CHAR16
*OriginalCommandLine
1440 CONST CHAR16
*MasterEnvList
;
1442 CHAR16
*NewCommandLine1
;
1443 CHAR16
*NewCommandLine2
;
1447 SCRIPT_FILE
*CurrentScriptFile
;
1448 ALIAS_LIST
*AliasListNode
;
1450 ASSERT(OriginalCommandLine
!= NULL
);
1453 NewSize
= StrSize(OriginalCommandLine
);
1454 CurrentScriptFile
= ShellCommandGetCurrentScriptFile();
1457 ///@todo update this to handle the %0 - %9 for scripting only (borrow from line 1256 area) ? ? ?
1460 // calculate the size required for the post-conversion string...
1462 if (CurrentScriptFile
!= NULL
) {
1463 for (AliasListNode
= (ALIAS_LIST
*)GetFirstNode(&CurrentScriptFile
->SubstList
)
1464 ; !IsNull(&CurrentScriptFile
->SubstList
, &AliasListNode
->Link
)
1465 ; AliasListNode
= (ALIAS_LIST
*)GetNextNode(&CurrentScriptFile
->SubstList
, &AliasListNode
->Link
)
1467 for (Temp
= StrStr(OriginalCommandLine
, AliasListNode
->Alias
)
1469 ; Temp
= StrStr(Temp
+1, AliasListNode
->Alias
)
1472 // we need a preceeding and if there is space no ^ preceeding (if no space ignore)
1474 if ((((Temp
-OriginalCommandLine
)>2) && *(Temp
-2) != L
'^') || ((Temp
-OriginalCommandLine
)<=2)) {
1475 NewSize
+= StrSize(AliasListNode
->CommandString
);
1481 for (MasterEnvList
= EfiShellGetEnv(NULL
)
1482 ; MasterEnvList
!= NULL
&& *MasterEnvList
!= CHAR_NULL
//&& *(MasterEnvList+1) != CHAR_NULL
1483 ; MasterEnvList
+= StrLen(MasterEnvList
) + 1
1485 if (StrSize(MasterEnvList
) > ItemSize
) {
1486 ItemSize
= StrSize(MasterEnvList
);
1488 for (Temp
= StrStr(OriginalCommandLine
, MasterEnvList
)
1490 ; Temp
= StrStr(Temp
+1, MasterEnvList
)
1493 // we need a preceeding and following % and if there is space no ^ preceeding (if no space ignore)
1495 if (*(Temp
-1) == L
'%' && *(Temp
+StrLen(MasterEnvList
)) == L
'%' &&
1496 ((((Temp
-OriginalCommandLine
)>2) && *(Temp
-2) != L
'^') || ((Temp
-OriginalCommandLine
)<=2))) {
1497 NewSize
+=StrSize(EfiShellGetEnv(MasterEnvList
));
1503 // now do the replacements...
1505 NewCommandLine1
= AllocateCopyPool(NewSize
, OriginalCommandLine
);
1506 NewCommandLine2
= AllocateZeroPool(NewSize
);
1507 ItemTemp
= AllocateZeroPool(ItemSize
+(2*sizeof(CHAR16
)));
1508 if (NewCommandLine1
== NULL
|| NewCommandLine2
== NULL
|| ItemTemp
== NULL
) {
1509 SHELL_FREE_NON_NULL(NewCommandLine1
);
1510 SHELL_FREE_NON_NULL(NewCommandLine2
);
1511 SHELL_FREE_NON_NULL(ItemTemp
);
1514 for (MasterEnvList
= EfiShellGetEnv(NULL
)
1515 ; MasterEnvList
!= NULL
&& *MasterEnvList
!= CHAR_NULL
1516 ; MasterEnvList
+= StrLen(MasterEnvList
) + 1
1518 StrnCpy(ItemTemp
, L
"%", ((ItemSize
+(2*sizeof(CHAR16
)))/sizeof(CHAR16
))-1);
1519 StrnCat(ItemTemp
, MasterEnvList
, ((ItemSize
+(2*sizeof(CHAR16
)))/sizeof(CHAR16
))-1 - StrLen(ItemTemp
));
1520 StrnCat(ItemTemp
, L
"%", ((ItemSize
+(2*sizeof(CHAR16
)))/sizeof(CHAR16
))-1 - StrLen(ItemTemp
));
1521 ShellCopySearchAndReplace(NewCommandLine1
, NewCommandLine2
, NewSize
, ItemTemp
, EfiShellGetEnv(MasterEnvList
), TRUE
, FALSE
);
1522 StrnCpy(NewCommandLine1
, NewCommandLine2
, NewSize
/sizeof(CHAR16
)-1);
1524 if (CurrentScriptFile
!= NULL
) {
1525 for (AliasListNode
= (ALIAS_LIST
*)GetFirstNode(&CurrentScriptFile
->SubstList
)
1526 ; !IsNull(&CurrentScriptFile
->SubstList
, &AliasListNode
->Link
)
1527 ; AliasListNode
= (ALIAS_LIST
*)GetNextNode(&CurrentScriptFile
->SubstList
, &AliasListNode
->Link
)
1529 ShellCopySearchAndReplace(NewCommandLine1
, NewCommandLine2
, NewSize
, AliasListNode
->Alias
, AliasListNode
->CommandString
, TRUE
, FALSE
);
1530 StrnCpy(NewCommandLine1
, NewCommandLine2
, NewSize
/sizeof(CHAR16
)-1);
1535 // Remove non-existant environment variables
1537 StripUnreplacedEnvironmentVariables(NewCommandLine1
);
1540 // Now cleanup any straggler intentionally ignored "%" characters
1542 ShellCopySearchAndReplace(NewCommandLine1
, NewCommandLine2
, NewSize
, L
"^%", L
"%", TRUE
, FALSE
);
1543 StrnCpy(NewCommandLine1
, NewCommandLine2
, NewSize
/sizeof(CHAR16
)-1);
1545 FreePool(NewCommandLine2
);
1548 return (NewCommandLine1
);
1552 Internal function to run a command line with pipe usage.
1554 @param[in] CmdLine The pointer to the command line.
1555 @param[in] StdIn The pointer to the Standard input.
1556 @param[in] StdOut The pointer to the Standard output.
1558 @retval EFI_SUCCESS The split command is executed successfully.
1559 @retval other Some error occurs when executing the split command.
1564 IN CONST CHAR16
*CmdLine
,
1565 IN SHELL_FILE_HANDLE
*StdIn
,
1566 IN SHELL_FILE_HANDLE
*StdOut
1570 CHAR16
*NextCommandLine
;
1571 CHAR16
*OurCommandLine
;
1575 SHELL_FILE_HANDLE
*TempFileHandle
;
1578 ASSERT(StdOut
== NULL
);
1580 ASSERT(StrStr(CmdLine
, L
"|") != NULL
);
1582 Status
= EFI_SUCCESS
;
1583 NextCommandLine
= NULL
;
1584 OurCommandLine
= NULL
;
1588 NextCommandLine
= StrnCatGrow(&NextCommandLine
, &Size1
, StrStr(CmdLine
, L
"|")+1, 0);
1589 OurCommandLine
= StrnCatGrow(&OurCommandLine
, &Size2
, CmdLine
, StrStr(CmdLine
, L
"|") - CmdLine
);
1591 if (NextCommandLine
== NULL
|| OurCommandLine
== NULL
) {
1592 SHELL_FREE_NON_NULL(OurCommandLine
);
1593 SHELL_FREE_NON_NULL(NextCommandLine
);
1594 return (EFI_OUT_OF_RESOURCES
);
1595 } else if (StrStr(OurCommandLine
, L
"|") != NULL
|| Size1
== 0 || Size2
== 0) {
1596 SHELL_FREE_NON_NULL(OurCommandLine
);
1597 SHELL_FREE_NON_NULL(NextCommandLine
);
1598 return (EFI_INVALID_PARAMETER
);
1599 } else if (NextCommandLine
[0] != CHAR_NULL
&&
1600 NextCommandLine
[0] == L
'a' &&
1601 NextCommandLine
[1] == L
' '
1603 CopyMem(NextCommandLine
, NextCommandLine
+1, StrSize(NextCommandLine
) - sizeof(NextCommandLine
[0]));
1611 // make a SPLIT_LIST item and add to list
1613 Split
= AllocateZeroPool(sizeof(SPLIT_LIST
));
1614 ASSERT(Split
!= NULL
);
1615 Split
->SplitStdIn
= StdIn
;
1616 Split
->SplitStdOut
= ConvertEfiFileProtocolToShellHandle(CreateFileInterfaceMem(Unicode
), NULL
);
1617 ASSERT(Split
->SplitStdOut
!= NULL
);
1618 InsertHeadList(&ShellInfoObject
.SplitList
.Link
, &Split
->Link
);
1620 Status
= RunCommand(OurCommandLine
);
1623 // move the output from the first to the in to the second.
1625 TempFileHandle
= Split
->SplitStdOut
;
1626 if (Split
->SplitStdIn
== StdIn
) {
1627 Split
->SplitStdOut
= NULL
;
1629 Split
->SplitStdOut
= Split
->SplitStdIn
;
1631 Split
->SplitStdIn
= TempFileHandle
;
1632 ShellInfoObject
.NewEfiShellProtocol
->SetFilePosition(ConvertShellHandleToEfiFileProtocol(Split
->SplitStdIn
), 0);
1634 if (!EFI_ERROR(Status
)) {
1635 Status
= RunCommand(NextCommandLine
);
1639 // remove the top level from the ScriptList
1641 ASSERT((SPLIT_LIST
*)GetFirstNode(&ShellInfoObject
.SplitList
.Link
) == Split
);
1642 RemoveEntryList(&Split
->Link
);
1645 // Note that the original StdIn is now the StdOut...
1647 if (Split
->SplitStdOut
!= NULL
&& Split
->SplitStdOut
!= StdIn
) {
1648 ShellInfoObject
.NewEfiShellProtocol
->CloseFile(ConvertShellHandleToEfiFileProtocol(Split
->SplitStdOut
));
1650 if (Split
->SplitStdIn
!= NULL
) {
1651 ShellInfoObject
.NewEfiShellProtocol
->CloseFile(ConvertShellHandleToEfiFileProtocol(Split
->SplitStdIn
));
1655 FreePool(NextCommandLine
);
1656 FreePool(OurCommandLine
);
1662 Take the original command line, substitute any variables, free
1663 the original string, return the modified copy.
1665 @param[in] CmdLine pointer to the command line to update.
1667 @retval EFI_SUCCESS the function was successful.
1668 @retval EFI_OUT_OF_RESOURCES a memory allocation failed.
1672 ShellSubstituteVariables(
1677 NewCmdLine
= ShellConvertVariables(*CmdLine
);
1678 SHELL_FREE_NON_NULL(*CmdLine
);
1679 if (NewCmdLine
== NULL
) {
1680 return (EFI_OUT_OF_RESOURCES
);
1682 *CmdLine
= NewCmdLine
;
1683 return (EFI_SUCCESS
);
1687 Take the original command line, substitute any alias in the first group of space delimited characters, free
1688 the original string, return the modified copy.
1690 @param[in] CmdLine pointer to the command line to update.
1692 @retval EFI_SUCCESS the function was successful.
1693 @retval EFI_OUT_OF_RESOURCES a memory allocation failed.
1697 ShellSubstituteAliases(
1702 CHAR16
*CommandName
;
1704 UINTN PostAliasSize
;
1705 ASSERT(CmdLine
!= NULL
);
1706 ASSERT(*CmdLine
!= NULL
);
1710 if (StrStr((*CmdLine
), L
" ") == NULL
){
1711 StrnCatGrow(&CommandName
, NULL
, (*CmdLine
), 0);
1713 StrnCatGrow(&CommandName
, NULL
, (*CmdLine
), StrStr((*CmdLine
), L
" ") - (*CmdLine
));
1717 // This cannot happen 'inline' since the CmdLine can need extra space.
1720 if (!ShellCommandIsCommandOnList(CommandName
)) {
1722 // Convert via alias
1724 Status
= ShellConvertAlias(&CommandName
);
1725 if (EFI_ERROR(Status
)){
1729 NewCmdLine
= StrnCatGrow(&NewCmdLine
, &PostAliasSize
, CommandName
, 0);
1730 if (NewCmdLine
== NULL
) {
1731 SHELL_FREE_NON_NULL(CommandName
);
1732 SHELL_FREE_NON_NULL(*CmdLine
);
1733 return (EFI_OUT_OF_RESOURCES
);
1735 NewCmdLine
= StrnCatGrow(&NewCmdLine
, &PostAliasSize
, StrStr((*CmdLine
), L
" "), 0);
1736 if (NewCmdLine
== NULL
) {
1737 SHELL_FREE_NON_NULL(CommandName
);
1738 SHELL_FREE_NON_NULL(*CmdLine
);
1739 return (EFI_OUT_OF_RESOURCES
);
1742 NewCmdLine
= StrnCatGrow(&NewCmdLine
, NULL
, (*CmdLine
), 0);
1745 SHELL_FREE_NON_NULL(*CmdLine
);
1746 SHELL_FREE_NON_NULL(CommandName
);
1749 // re-assign the passed in double pointer to point to our newly allocated buffer
1751 *CmdLine
= NewCmdLine
;
1753 return (EFI_SUCCESS
);
1757 Takes the Argv[0] part of the command line and determine the meaning of it.
1759 @param[in] CmdName pointer to the command line to update.
1761 @retval Internal_Command The name is an internal command.
1762 @retval File_Sys_Change the name is a file system change.
1763 @retval Script_File_Name the name is a NSH script file.
1764 @retval Unknown_Invalid the name is unknown.
1765 @retval Efi_Application the name is an application (.EFI).
1767 SHELL_OPERATION_TYPES
1770 IN CONST CHAR16
*CmdName
1773 CHAR16
* FileWithPath
;
1774 CONST CHAR16
* TempLocation
;
1775 CONST CHAR16
* TempLocation2
;
1777 FileWithPath
= NULL
;
1779 // test for an internal command.
1781 if (ShellCommandIsCommandOnList(CmdName
)) {
1782 return (Internal_Command
);
1786 // Test for file system change request. anything ending with first : and cant have spaces.
1788 if (CmdName
[(StrLen(CmdName
)-1)] == L
':') {
1789 if ( StrStr(CmdName
, L
" ") != NULL
1790 || StrLen(StrStr(CmdName
, L
":")) > 1
1792 return (Unknown_Invalid
);
1794 return (File_Sys_Change
);
1800 if ((FileWithPath
= ShellFindFilePathEx(CmdName
, mExecutableExtensions
)) != NULL
) {
1802 // See if that file has a script file extension
1804 if (StrLen(FileWithPath
) > 4) {
1805 TempLocation
= FileWithPath
+StrLen(FileWithPath
)-4;
1806 TempLocation2
= mScriptExtension
;
1807 if (StringNoCaseCompare((VOID
*)(&TempLocation
), (VOID
*)(&TempLocation2
)) == 0) {
1808 SHELL_FREE_NON_NULL(FileWithPath
);
1809 return (Script_File_Name
);
1814 // Was a file, but not a script. we treat this as an application.
1816 SHELL_FREE_NON_NULL(FileWithPath
);
1817 return (Efi_Application
);
1820 SHELL_FREE_NON_NULL(FileWithPath
);
1822 // No clue what this is... return invalid flag...
1824 return (Unknown_Invalid
);
1828 Determine if the first item in a command line is valid.
1830 @param[in] CmdLine The command line to parse.
1832 @retval EFI_SUCCESS The item is valid.
1833 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
1834 @retval EFI_NOT_FOUND The operation type is unknown or invalid.
1839 IN CONST CHAR16
*CmdLine
1843 CHAR16
*FirstParameter
;
1849 Temp
= StrnCatGrow(&Temp
, NULL
, CmdLine
, 0);
1851 return (EFI_OUT_OF_RESOURCES
);
1854 FirstParameter
= StrStr(Temp
, L
"|");
1855 if (FirstParameter
!= NULL
) {
1856 *FirstParameter
= CHAR_NULL
;
1859 FirstParameter
= NULL
;
1862 // Process the command line
1864 Status
= ProcessCommandLineToFinal(&Temp
);
1866 if (!EFI_ERROR(Status
)) {
1867 FirstParameter
= AllocateZeroPool(StrSize(CmdLine
));
1868 if (FirstParameter
== NULL
) {
1869 SHELL_FREE_NON_NULL(Temp
);
1870 return (EFI_OUT_OF_RESOURCES
);
1872 TempWalker
= (CHAR16
*)Temp
;
1873 GetNextParameter(&TempWalker
, &FirstParameter
, StrSize(CmdLine
));
1875 if (GetOperationType(FirstParameter
) == Unknown_Invalid
) {
1876 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_NOT_FOUND
), ShellInfoObject
.HiiHandle
, FirstParameter
);
1877 SetLastError(SHELL_NOT_FOUND
);
1878 Status
= EFI_NOT_FOUND
;
1882 SHELL_FREE_NON_NULL(Temp
);
1883 SHELL_FREE_NON_NULL(FirstParameter
);
1888 Determine if a command line contains with a split contains only valid commands.
1890 @param[in] CmdLine The command line to parse.
1892 @retval EFI_SUCCESS CmdLine has only valid commands, application, or has no split.
1893 @retval EFI_ABORTED CmdLine has at least one invalid command or application.
1898 IN CONST CHAR16
*CmdLine
1901 CONST CHAR16
*TempSpot
;
1905 // Verify up to the pipe or end character
1907 Status
= IsValidSplit(CmdLine
);
1908 if (EFI_ERROR(Status
)) {
1913 // If this was the only item, then get out
1915 if (!ContainsSplit(CmdLine
)) {
1916 return (EFI_SUCCESS
);
1920 // recurse to verify the next item
1922 TempSpot
= FindSplit(CmdLine
)+1;
1923 return (VerifySplit(TempSpot
));
1927 Process a split based operation.
1929 @param[in] CmdLine pointer to the command line to process
1931 @retval EFI_SUCCESS The operation was successful
1932 @return an error occured.
1936 ProcessNewSplitCommandLine(
1937 IN CONST CHAR16
*CmdLine
1943 Status
= VerifySplit(CmdLine
);
1944 if (EFI_ERROR(Status
)) {
1951 // are we in an existing split???
1953 if (!IsListEmpty(&ShellInfoObject
.SplitList
.Link
)) {
1954 Split
= (SPLIT_LIST
*)GetFirstNode(&ShellInfoObject
.SplitList
.Link
);
1957 if (Split
== NULL
) {
1958 Status
= RunSplitCommand(CmdLine
, NULL
, NULL
);
1960 Status
= RunSplitCommand(CmdLine
, Split
->SplitStdIn
, Split
->SplitStdOut
);
1962 if (EFI_ERROR(Status
)) {
1963 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_INVALID_SPLIT
), ShellInfoObject
.HiiHandle
, CmdLine
);
1969 Handle a request to change the current file system.
1971 @param[in] CmdLine The passed in command line.
1973 @retval EFI_SUCCESS The operation was successful.
1978 IN CONST CHAR16
*CmdLine
1982 Status
= EFI_SUCCESS
;
1985 // make sure we are the right operation
1987 ASSERT(CmdLine
[(StrLen(CmdLine
)-1)] == L
':' && StrStr(CmdLine
, L
" ") == NULL
);
1990 // Call the protocol API to do the work
1992 Status
= ShellInfoObject
.NewEfiShellProtocol
->SetCurDir(NULL
, CmdLine
);
1995 // Report any errors
1997 if (EFI_ERROR(Status
)) {
1998 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_INVALID_MAPPING
), ShellInfoObject
.HiiHandle
, CmdLine
);
2005 Reprocess the command line to direct all -? to the help command.
2007 if found, will add "help" as argv[0], and move the rest later.
2009 @param[in,out] CmdLine pointer to the command line to update
2014 IN OUT CHAR16
**CmdLine
2017 CHAR16
*CurrentParameter
;
2020 CHAR16
*NewCommandLine
;
2023 Status
= EFI_SUCCESS
;
2025 CurrentParameter
= AllocateZeroPool(StrSize(*CmdLine
));
2026 if (CurrentParameter
== NULL
) {
2027 return (EFI_OUT_OF_RESOURCES
);
2031 while(Walker
!= NULL
&& *Walker
!= CHAR_NULL
) {
2032 LastWalker
= Walker
;
2033 GetNextParameter(&Walker
, &CurrentParameter
, StrSize(*CmdLine
));
2034 if (StrStr(CurrentParameter
, L
"-?") == CurrentParameter
) {
2035 LastWalker
[0] = L
' ';
2036 LastWalker
[1] = L
' ';
2037 NewCommandLine
= AllocateZeroPool(StrSize(L
"help ") + StrSize(*CmdLine
));
2038 if (NewCommandLine
== NULL
) {
2039 Status
= EFI_OUT_OF_RESOURCES
;
2044 // We know the space is sufficient since we just calculated it.
2046 StrnCpy(NewCommandLine
, L
"help ", 5);
2047 StrnCat(NewCommandLine
, *CmdLine
, StrLen(*CmdLine
));
2048 SHELL_FREE_NON_NULL(*CmdLine
);
2049 *CmdLine
= NewCommandLine
;
2054 SHELL_FREE_NON_NULL(CurrentParameter
);
2060 Function to update the shell variable "lasterror".
2062 @param[in] ErrorCode the error code to put into lasterror.
2067 IN CONST SHELL_STATUS ErrorCode
2070 CHAR16 LeString
[19];
2071 if (sizeof(EFI_STATUS
) == sizeof(UINT64
)) {
2072 UnicodeSPrint(LeString
, sizeof(LeString
), L
"0x%Lx", ErrorCode
);
2074 UnicodeSPrint(LeString
, sizeof(LeString
), L
"0x%x", ErrorCode
);
2076 DEBUG_CODE(InternalEfiShellSetEnv(L
"debuglasterror", LeString
, TRUE
););
2077 InternalEfiShellSetEnv(L
"lasterror", LeString
, TRUE
);
2079 return (EFI_SUCCESS
);
2083 Converts the command line to it's post-processed form. this replaces variables and alias' per UEFI Shell spec.
2085 @param[in,out] CmdLine pointer to the command line to update
2087 @retval EFI_SUCCESS The operation was successful
2088 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
2089 @return some other error occured
2093 ProcessCommandLineToFinal(
2094 IN OUT CHAR16
**CmdLine
2098 TrimSpaces(CmdLine
);
2100 Status
= ShellSubstituteAliases(CmdLine
);
2101 if (EFI_ERROR(Status
)) {
2105 TrimSpaces(CmdLine
);
2107 Status
= ShellSubstituteVariables(CmdLine
);
2108 if (EFI_ERROR(Status
)) {
2111 ASSERT (*CmdLine
!= NULL
);
2113 TrimSpaces(CmdLine
);
2116 // update for help parsing
2118 if (StrStr(*CmdLine
, L
"?") != NULL
) {
2120 // This may do nothing if the ? does not indicate help.
2121 // Save all the details for in the API below.
2123 Status
= DoHelpUpdate(CmdLine
);
2126 TrimSpaces(CmdLine
);
2128 return (EFI_SUCCESS
);
2132 Run an internal shell command.
2134 This API will upadate the shell's environment since these commands are libraries.
2136 @param[in] CmdLine the command line to run.
2137 @param[in] FirstParameter the first parameter on the command line
2138 @param[in] ParamProtocol the shell parameters protocol pointer
2140 @retval EFI_SUCCESS The command was completed.
2141 @retval EFI_ABORTED The command's operation was aborted.
2146 IN CONST CHAR16
*CmdLine
,
2147 IN CHAR16
*FirstParameter
,
2148 IN EFI_SHELL_PARAMETERS_PROTOCOL
*ParamProtocol
2154 SHELL_STATUS CommandReturnedStatus
;
2159 NewCmdLine
= AllocateCopyPool (StrSize (CmdLine
), CmdLine
);
2160 if (NewCmdLine
== NULL
) {
2161 return EFI_OUT_OF_RESOURCES
;
2164 for (Walker
= NewCmdLine
; Walker
!= NULL
&& *Walker
!= CHAR_NULL
; Walker
++) {
2165 if (*Walker
== L
'^' && *(Walker
+1) == L
'#') {
2166 CopyMem(Walker
, Walker
+1, StrSize(Walker
) - sizeof(Walker
[0]));
2171 // get the argc and argv updated for internal commands
2173 Status
= UpdateArgcArgv(ParamProtocol
, NewCmdLine
, &Argv
, &Argc
);
2174 if (!EFI_ERROR(Status
)) {
2176 // Run the internal command.
2178 Status
= ShellCommandRunCommandHandler(FirstParameter
, &CommandReturnedStatus
, &LastError
);
2180 if (!EFI_ERROR(Status
)) {
2182 // Update last error status.
2183 // some commands do not update last error.
2186 SetLastError(CommandReturnedStatus
);
2190 // Pass thru the exitcode from the app.
2192 if (ShellCommandGetExit()) {
2194 // An Exit was requested ("exit" command), pass its value up.
2196 Status
= CommandReturnedStatus
;
2197 } else if (CommandReturnedStatus
!= SHELL_SUCCESS
&& IsScriptOnlyCommand(FirstParameter
)) {
2199 // Always abort when a script only command fails for any reason
2201 Status
= EFI_ABORTED
;
2202 } else if (ShellCommandGetCurrentScriptFile() != NULL
&& CommandReturnedStatus
== SHELL_ABORTED
) {
2204 // Abort when in a script and a command aborted
2206 Status
= EFI_ABORTED
;
2212 // This is guarenteed to be called after UpdateArgcArgv no matter what else happened.
2213 // This is safe even if the update API failed. In this case, it may be a no-op.
2215 RestoreArgcArgv(ParamProtocol
, &Argv
, &Argc
);
2218 // If a script is running and the command is not a scipt only command, then
2219 // change return value to success so the script won't halt (unless aborted).
2221 // Script only commands have to be able halt the script since the script will
2222 // not operate if they are failing.
2224 if ( ShellCommandGetCurrentScriptFile() != NULL
2225 && !IsScriptOnlyCommand(FirstParameter
)
2226 && Status
!= EFI_ABORTED
2228 Status
= EFI_SUCCESS
;
2231 FreePool (NewCmdLine
);
2236 Function to run the command or file.
2238 @param[in] Type the type of operation being run.
2239 @param[in] CmdLine the command line to run.
2240 @param[in] FirstParameter the first parameter on the command line
2241 @param[in] ParamProtocol the shell parameters protocol pointer
2243 @retval EFI_SUCCESS The command was completed.
2244 @retval EFI_ABORTED The command's operation was aborted.
2249 IN SHELL_OPERATION_TYPES Type
,
2250 IN CONST CHAR16
*CmdLine
,
2251 IN CHAR16
*FirstParameter
,
2252 IN EFI_SHELL_PARAMETERS_PROTOCOL
*ParamProtocol
2256 EFI_STATUS StartStatus
;
2257 CHAR16
*CommandWithPath
;
2258 EFI_DEVICE_PATH_PROTOCOL
*DevPath
;
2259 SHELL_STATUS CalleeExitStatus
;
2261 Status
= EFI_SUCCESS
;
2262 CommandWithPath
= NULL
;
2264 CalleeExitStatus
= SHELL_INVALID_PARAMETER
;
2267 case Internal_Command
:
2268 Status
= RunInternalCommand(CmdLine
, FirstParameter
, ParamProtocol
);
2270 case Script_File_Name
:
2271 case Efi_Application
:
2273 // Process a fully qualified path
2275 if (StrStr(FirstParameter
, L
":") != NULL
) {
2276 ASSERT (CommandWithPath
== NULL
);
2277 if (ShellIsFile(FirstParameter
) == EFI_SUCCESS
) {
2278 CommandWithPath
= StrnCatGrow(&CommandWithPath
, NULL
, FirstParameter
, 0);
2283 // Process a relative path and also check in the path environment variable
2285 if (CommandWithPath
== NULL
) {
2286 CommandWithPath
= ShellFindFilePathEx(FirstParameter
, mExecutableExtensions
);
2290 // This should be impossible now.
2292 ASSERT(CommandWithPath
!= NULL
);
2295 // Make sure that path is not just a directory (or not found)
2297 if (!EFI_ERROR(ShellIsDirectory(CommandWithPath
))) {
2298 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_NOT_FOUND
), ShellInfoObject
.HiiHandle
, FirstParameter
);
2299 SetLastError(SHELL_NOT_FOUND
);
2302 case Script_File_Name
:
2303 Status
= RunScriptFile (CommandWithPath
, NULL
, CmdLine
, ParamProtocol
);
2305 case Efi_Application
:
2307 // Get the device path of the application image
2309 DevPath
= ShellInfoObject
.NewEfiShellProtocol
->GetDevicePathFromFilePath(CommandWithPath
);
2310 if (DevPath
== NULL
){
2311 Status
= EFI_OUT_OF_RESOURCES
;
2316 // Execute the device path
2318 Status
= InternalShellExecuteDevicePath(
2326 SHELL_FREE_NON_NULL(DevPath
);
2328 if(EFI_ERROR (Status
)) {
2329 CalleeExitStatus
= (SHELL_STATUS
) (Status
& (~MAX_BIT
));
2331 CalleeExitStatus
= (SHELL_STATUS
) StartStatus
;
2335 // Update last error status.
2337 // Status is an EFI_STATUS. Clear top bit to convert to SHELL_STATUS
2338 SetLastError(CalleeExitStatus
);
2354 SHELL_FREE_NON_NULL(CommandWithPath
);
2360 Function to setup StdIn, StdErr, StdOut, and then run the command or file.
2362 @param[in] Type the type of operation being run.
2363 @param[in] CmdLine the command line to run.
2364 @param[in] FirstParameter the first parameter on the command line.
2365 @param[in] ParamProtocol the shell parameters protocol pointer
2367 @retval EFI_SUCCESS The command was completed.
2368 @retval EFI_ABORTED The command's operation was aborted.
2372 SetupAndRunCommandOrFile(
2373 IN SHELL_OPERATION_TYPES Type
,
2375 IN CHAR16
*FirstParameter
,
2376 IN EFI_SHELL_PARAMETERS_PROTOCOL
*ParamProtocol
2380 SHELL_FILE_HANDLE OriginalStdIn
;
2381 SHELL_FILE_HANDLE OriginalStdOut
;
2382 SHELL_FILE_HANDLE OriginalStdErr
;
2383 SYSTEM_TABLE_INFO OriginalSystemTableInfo
;
2386 // Update the StdIn, StdOut, and StdErr for redirection to environment variables, files, etc... unicode and ASCII
2388 Status
= UpdateStdInStdOutStdErr(ParamProtocol
, CmdLine
, &OriginalStdIn
, &OriginalStdOut
, &OriginalStdErr
, &OriginalSystemTableInfo
);
2391 // The StdIn, StdOut, and StdErr are set up.
2392 // Now run the command, script, or application
2394 if (!EFI_ERROR(Status
)) {
2395 TrimSpaces(&CmdLine
);
2396 Status
= RunCommandOrFile(Type
, CmdLine
, FirstParameter
, ParamProtocol
);
2402 if (EFI_ERROR(Status
)) {
2403 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_ERROR
), ShellInfoObject
.HiiHandle
, (VOID
*)(Status
));
2407 // put back the original StdIn, StdOut, and StdErr
2409 RestoreStdInStdOutStdErr(ParamProtocol
, &OriginalStdIn
, &OriginalStdOut
, &OriginalStdErr
, &OriginalSystemTableInfo
);
2415 Function will process and run a command line.
2417 This will determine if the command line represents an internal shell
2418 command or dispatch an external application.
2420 @param[in] CmdLine The command line to parse.
2422 @retval EFI_SUCCESS The command was completed.
2423 @retval EFI_ABORTED The command's operation was aborted.
2428 IN CONST CHAR16
*CmdLine
2432 CHAR16
*CleanOriginal
;
2433 CHAR16
*FirstParameter
;
2435 SHELL_OPERATION_TYPES Type
;
2437 ASSERT(CmdLine
!= NULL
);
2438 if (StrLen(CmdLine
) == 0) {
2439 return (EFI_SUCCESS
);
2442 Status
= EFI_SUCCESS
;
2443 CleanOriginal
= NULL
;
2445 CleanOriginal
= StrnCatGrow(&CleanOriginal
, NULL
, CmdLine
, 0);
2446 if (CleanOriginal
== NULL
) {
2447 return (EFI_OUT_OF_RESOURCES
);
2450 TrimSpaces(&CleanOriginal
);
2453 // NULL out comments (leveraged from RunScriptFileHandle() ).
2454 // The # character on a line is used to denote that all characters on the same line
2455 // and to the right of the # are to be ignored by the shell.
2456 // Afterward, again remove spaces, in case any were between the last command-parameter and '#'.
2458 for (TempWalker
= CleanOriginal
; TempWalker
!= NULL
&& *TempWalker
!= CHAR_NULL
; TempWalker
++) {
2459 if (*TempWalker
== L
'^') {
2460 if (*(TempWalker
+ 1) == L
'#') {
2463 } else if (*TempWalker
== L
'#') {
2464 *TempWalker
= CHAR_NULL
;
2468 TrimSpaces(&CleanOriginal
);
2471 // Handle case that passed in command line is just 1 or more " " characters.
2473 if (StrLen (CleanOriginal
) == 0) {
2474 SHELL_FREE_NON_NULL(CleanOriginal
);
2475 return (EFI_SUCCESS
);
2478 Status
= ProcessCommandLineToFinal(&CleanOriginal
);
2479 if (EFI_ERROR(Status
)) {
2480 SHELL_FREE_NON_NULL(CleanOriginal
);
2485 // We dont do normal processing with a split command line (output from one command input to another)
2487 if (ContainsSplit(CleanOriginal
)) {
2488 Status
= ProcessNewSplitCommandLine(CleanOriginal
);
2489 SHELL_FREE_NON_NULL(CleanOriginal
);
2494 // We need the first parameter information so we can determine the operation type
2496 FirstParameter
= AllocateZeroPool(StrSize(CleanOriginal
));
2497 if (FirstParameter
== NULL
) {
2498 SHELL_FREE_NON_NULL(CleanOriginal
);
2499 return (EFI_OUT_OF_RESOURCES
);
2501 TempWalker
= CleanOriginal
;
2502 GetNextParameter(&TempWalker
, &FirstParameter
, StrSize(CleanOriginal
));
2505 // Depending on the first parameter we change the behavior
2507 switch (Type
= GetOperationType(FirstParameter
)) {
2508 case File_Sys_Change
:
2509 Status
= ChangeMappedDrive (FirstParameter
);
2511 case Internal_Command
:
2512 case Script_File_Name
:
2513 case Efi_Application
:
2514 Status
= SetupAndRunCommandOrFile(Type
, CleanOriginal
, FirstParameter
, ShellInfoObject
.NewShellParametersProtocol
);
2518 // Whatever was typed, it was invalid.
2520 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_NOT_FOUND
), ShellInfoObject
.HiiHandle
, FirstParameter
);
2521 SetLastError(SHELL_NOT_FOUND
);
2525 SHELL_FREE_NON_NULL(CleanOriginal
);
2526 SHELL_FREE_NON_NULL(FirstParameter
);
2531 STATIC CONST UINT16 InvalidChars
[] = {L
'*', L
'?', L
'<', L
'>', L
'\\', L
'/', L
'\"', 0x0001, 0x0002};
2533 Function determins if the CommandName COULD be a valid command. It does not determine whether
2534 this is a valid command. It only checks for invalid characters.
2536 @param[in] CommandName The name to check
2538 @retval TRUE CommandName could be a command name
2539 @retval FALSE CommandName could not be a valid command name
2544 IN CONST CHAR16
*CommandName
2548 if (CommandName
== NULL
) {
2553 ; Count
< sizeof(InvalidChars
) / sizeof(InvalidChars
[0])
2556 if (ScanMem16(CommandName
, StrSize(CommandName
), InvalidChars
[Count
]) != NULL
) {
2564 Function to process a NSH script file via SHELL_FILE_HANDLE.
2566 @param[in] Handle The handle to the already opened file.
2567 @param[in] Name The name of the script file.
2569 @retval EFI_SUCCESS the script completed sucessfully
2573 RunScriptFileHandle (
2574 IN SHELL_FILE_HANDLE Handle
,
2575 IN CONST CHAR16
*Name
2579 SCRIPT_FILE
*NewScriptFile
;
2581 CHAR16
*CommandLine
;
2582 CHAR16
*CommandLine2
;
2583 CHAR16
*CommandLine3
;
2584 SCRIPT_COMMAND_LIST
*LastCommand
;
2586 BOOLEAN PreScriptEchoState
;
2587 BOOLEAN PreCommandEchoState
;
2588 CONST CHAR16
*CurDir
;
2590 CHAR16 LeString
[50];
2592 ASSERT(!ShellCommandGetScriptExit());
2594 PreScriptEchoState
= ShellCommandGetEchoState();
2596 NewScriptFile
= (SCRIPT_FILE
*)AllocateZeroPool(sizeof(SCRIPT_FILE
));
2597 if (NewScriptFile
== NULL
) {
2598 return (EFI_OUT_OF_RESOURCES
);
2604 ASSERT(NewScriptFile
->ScriptName
== NULL
);
2605 NewScriptFile
->ScriptName
= StrnCatGrow(&NewScriptFile
->ScriptName
, NULL
, Name
, 0);
2606 if (NewScriptFile
->ScriptName
== NULL
) {
2607 DeleteScriptFileStruct(NewScriptFile
);
2608 return (EFI_OUT_OF_RESOURCES
);
2612 // Save the parameters (used to replace %0 to %9 later on)
2614 NewScriptFile
->Argc
= ShellInfoObject
.NewShellParametersProtocol
->Argc
;
2615 if (NewScriptFile
->Argc
!= 0) {
2616 NewScriptFile
->Argv
= (CHAR16
**)AllocateZeroPool(NewScriptFile
->Argc
* sizeof(CHAR16
*));
2617 if (NewScriptFile
->Argv
== NULL
) {
2618 DeleteScriptFileStruct(NewScriptFile
);
2619 return (EFI_OUT_OF_RESOURCES
);
2621 for (LoopVar
= 0 ; LoopVar
< 10 && LoopVar
< NewScriptFile
->Argc
; LoopVar
++) {
2622 ASSERT(NewScriptFile
->Argv
[LoopVar
] == NULL
);
2623 NewScriptFile
->Argv
[LoopVar
] = StrnCatGrow(&NewScriptFile
->Argv
[LoopVar
], NULL
, ShellInfoObject
.NewShellParametersProtocol
->Argv
[LoopVar
], 0);
2624 if (NewScriptFile
->Argv
[LoopVar
] == NULL
) {
2625 DeleteScriptFileStruct(NewScriptFile
);
2626 return (EFI_OUT_OF_RESOURCES
);
2630 NewScriptFile
->Argv
= NULL
;
2633 InitializeListHead(&NewScriptFile
->CommandList
);
2634 InitializeListHead(&NewScriptFile
->SubstList
);
2637 // Now build the list of all script commands.
2640 while(!ShellFileHandleEof(Handle
)) {
2641 CommandLine
= ShellFileHandleReturnLine(Handle
, &Ascii
);
2643 if (CommandLine
== NULL
|| StrLen(CommandLine
) == 0 || CommandLine
[0] == '#') {
2644 SHELL_FREE_NON_NULL(CommandLine
);
2647 NewScriptFile
->CurrentCommand
= AllocateZeroPool(sizeof(SCRIPT_COMMAND_LIST
));
2648 if (NewScriptFile
->CurrentCommand
== NULL
) {
2649 SHELL_FREE_NON_NULL(CommandLine
);
2650 DeleteScriptFileStruct(NewScriptFile
);
2651 return (EFI_OUT_OF_RESOURCES
);
2654 NewScriptFile
->CurrentCommand
->Cl
= CommandLine
;
2655 NewScriptFile
->CurrentCommand
->Data
= NULL
;
2656 NewScriptFile
->CurrentCommand
->Line
= LineCount
;
2658 InsertTailList(&NewScriptFile
->CommandList
, &NewScriptFile
->CurrentCommand
->Link
);
2662 // Add this as the topmost script file
2664 ShellCommandSetNewScript (NewScriptFile
);
2667 // Now enumerate through the commands and run each one.
2669 CommandLine
= AllocateZeroPool(PcdGet16(PcdShellPrintBufferSize
));
2670 if (CommandLine
== NULL
) {
2671 DeleteScriptFileStruct(NewScriptFile
);
2672 return (EFI_OUT_OF_RESOURCES
);
2674 CommandLine2
= AllocateZeroPool(PcdGet16(PcdShellPrintBufferSize
));
2675 if (CommandLine2
== NULL
) {
2676 FreePool(CommandLine
);
2677 DeleteScriptFileStruct(NewScriptFile
);
2678 return (EFI_OUT_OF_RESOURCES
);
2681 for ( NewScriptFile
->CurrentCommand
= (SCRIPT_COMMAND_LIST
*)GetFirstNode(&NewScriptFile
->CommandList
)
2682 ; !IsNull(&NewScriptFile
->CommandList
, &NewScriptFile
->CurrentCommand
->Link
)
2683 ; // conditional increment in the body of the loop
2685 ASSERT(CommandLine2
!= NULL
);
2686 StrnCpy(CommandLine2
, NewScriptFile
->CurrentCommand
->Cl
, PcdGet16(PcdShellPrintBufferSize
)/sizeof(CHAR16
)-1);
2689 // NULL out comments
2691 for (CommandLine3
= CommandLine2
; CommandLine3
!= NULL
&& *CommandLine3
!= CHAR_NULL
; CommandLine3
++) {
2692 if (*CommandLine3
== L
'^') {
2693 if ( *(CommandLine3
+1) == L
':') {
2694 CopyMem(CommandLine3
, CommandLine3
+1, StrSize(CommandLine3
) - sizeof(CommandLine3
[0]));
2695 } else if (*(CommandLine3
+1) == L
'#') {
2698 } else if (*CommandLine3
== L
'#') {
2699 *CommandLine3
= CHAR_NULL
;
2703 if (CommandLine2
!= NULL
&& StrLen(CommandLine2
) >= 1) {
2705 // Due to variability in starting the find and replace action we need to have both buffers the same.
2707 StrnCpy(CommandLine
, CommandLine2
, PcdGet16(PcdShellPrintBufferSize
)/sizeof(CHAR16
)-1);
2710 // Remove the %0 to %9 from the command line (if we have some arguments)
2712 if (NewScriptFile
->Argv
!= NULL
) {
2713 switch (NewScriptFile
->Argc
) {
2715 Status
= ShellCopySearchAndReplace(CommandLine2
, CommandLine
, PcdGet16 (PcdShellPrintBufferSize
), L
"%9", NewScriptFile
->Argv
[9], FALSE
, FALSE
);
2716 ASSERT_EFI_ERROR(Status
);
2718 Status
= ShellCopySearchAndReplace(CommandLine
, CommandLine2
, PcdGet16 (PcdShellPrintBufferSize
), L
"%8", NewScriptFile
->Argv
[8], FALSE
, FALSE
);
2719 ASSERT_EFI_ERROR(Status
);
2721 Status
= ShellCopySearchAndReplace(CommandLine2
, CommandLine
, PcdGet16 (PcdShellPrintBufferSize
), L
"%7", NewScriptFile
->Argv
[7], FALSE
, FALSE
);
2722 ASSERT_EFI_ERROR(Status
);
2724 Status
= ShellCopySearchAndReplace(CommandLine
, CommandLine2
, PcdGet16 (PcdShellPrintBufferSize
), L
"%6", NewScriptFile
->Argv
[6], FALSE
, FALSE
);
2725 ASSERT_EFI_ERROR(Status
);
2727 Status
= ShellCopySearchAndReplace(CommandLine2
, CommandLine
, PcdGet16 (PcdShellPrintBufferSize
), L
"%5", NewScriptFile
->Argv
[5], FALSE
, FALSE
);
2728 ASSERT_EFI_ERROR(Status
);
2730 Status
= ShellCopySearchAndReplace(CommandLine
, CommandLine2
, PcdGet16 (PcdShellPrintBufferSize
), L
"%4", NewScriptFile
->Argv
[4], FALSE
, FALSE
);
2731 ASSERT_EFI_ERROR(Status
);
2733 Status
= ShellCopySearchAndReplace(CommandLine2
, CommandLine
, PcdGet16 (PcdShellPrintBufferSize
), L
"%3", NewScriptFile
->Argv
[3], FALSE
, FALSE
);
2734 ASSERT_EFI_ERROR(Status
);
2736 Status
= ShellCopySearchAndReplace(CommandLine
, CommandLine2
, PcdGet16 (PcdShellPrintBufferSize
), L
"%2", NewScriptFile
->Argv
[2], FALSE
, FALSE
);
2737 ASSERT_EFI_ERROR(Status
);
2739 Status
= ShellCopySearchAndReplace(CommandLine2
, CommandLine
, PcdGet16 (PcdShellPrintBufferSize
), L
"%1", NewScriptFile
->Argv
[1], FALSE
, FALSE
);
2740 ASSERT_EFI_ERROR(Status
);
2742 Status
= ShellCopySearchAndReplace(CommandLine
, CommandLine2
, PcdGet16 (PcdShellPrintBufferSize
), L
"%0", NewScriptFile
->Argv
[0], FALSE
, FALSE
);
2743 ASSERT_EFI_ERROR(Status
);
2749 Status
= ShellCopySearchAndReplace(CommandLine2
, CommandLine
, PcdGet16 (PcdShellPrintBufferSize
), L
"%1", L
"\"\"", FALSE
, FALSE
);
2750 Status
= ShellCopySearchAndReplace(CommandLine
, CommandLine2
, PcdGet16 (PcdShellPrintBufferSize
), L
"%2", L
"\"\"", FALSE
, FALSE
);
2751 Status
= ShellCopySearchAndReplace(CommandLine2
, CommandLine
, PcdGet16 (PcdShellPrintBufferSize
), L
"%3", L
"\"\"", FALSE
, FALSE
);
2752 Status
= ShellCopySearchAndReplace(CommandLine
, CommandLine2
, PcdGet16 (PcdShellPrintBufferSize
), L
"%4", L
"\"\"", FALSE
, FALSE
);
2753 Status
= ShellCopySearchAndReplace(CommandLine2
, CommandLine
, PcdGet16 (PcdShellPrintBufferSize
), L
"%5", L
"\"\"", FALSE
, FALSE
);
2754 Status
= ShellCopySearchAndReplace(CommandLine
, CommandLine2
, PcdGet16 (PcdShellPrintBufferSize
), L
"%6", L
"\"\"", FALSE
, FALSE
);
2755 Status
= ShellCopySearchAndReplace(CommandLine2
, CommandLine
, PcdGet16 (PcdShellPrintBufferSize
), L
"%7", L
"\"\"", FALSE
, FALSE
);
2756 Status
= ShellCopySearchAndReplace(CommandLine
, CommandLine2
, PcdGet16 (PcdShellPrintBufferSize
), L
"%8", L
"\"\"", FALSE
, FALSE
);
2757 Status
= ShellCopySearchAndReplace(CommandLine2
, CommandLine
, PcdGet16 (PcdShellPrintBufferSize
), L
"%9", L
"\"\"", FALSE
, FALSE
);
2759 StrnCpy(CommandLine2
, CommandLine
, PcdGet16(PcdShellPrintBufferSize
)/sizeof(CHAR16
)-1);
2761 LastCommand
= NewScriptFile
->CurrentCommand
;
2763 for (CommandLine3
= CommandLine2
; CommandLine3
[0] == L
' ' ; CommandLine3
++);
2765 if (CommandLine3
!= NULL
&& CommandLine3
[0] == L
':' ) {
2767 // This line is a goto target / label
2770 if (CommandLine3
!= NULL
&& StrLen(CommandLine3
) > 0) {
2771 if (CommandLine3
[0] == L
'@') {
2773 // We need to save the current echo state
2774 // and disable echo for just this command.
2776 PreCommandEchoState
= ShellCommandGetEchoState();
2777 ShellCommandSetEchoState(FALSE
);
2778 Status
= RunCommand(CommandLine3
+1);
2781 // If command was "@echo -off" or "@echo -on" then don't restore echo state
2783 if (StrCmp (L
"@echo -off", CommandLine3
) != 0 &&
2784 StrCmp (L
"@echo -on", CommandLine3
) != 0) {
2786 // Now restore the pre-'@' echo state.
2788 ShellCommandSetEchoState(PreCommandEchoState
);
2791 if (ShellCommandGetEchoState()) {
2792 CurDir
= ShellInfoObject
.NewEfiShellProtocol
->GetEnv(L
"cwd");
2793 if (CurDir
!= NULL
&& StrLen(CurDir
) > 1) {
2794 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_CURDIR
), ShellInfoObject
.HiiHandle
, CurDir
);
2796 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_SHELL
), ShellInfoObject
.HiiHandle
);
2798 ShellPrintEx(-1, -1, L
"%s\r\n", CommandLine2
);
2800 Status
= RunCommand(CommandLine3
);
2804 if (ShellCommandGetScriptExit()) {
2806 // ShellCommandGetExitCode() always returns a UINT64
2808 UnicodeSPrint(LeString
, sizeof(LeString
), L
"0x%Lx", ShellCommandGetExitCode());
2809 DEBUG_CODE(InternalEfiShellSetEnv(L
"debuglasterror", LeString
, TRUE
););
2810 InternalEfiShellSetEnv(L
"lasterror", LeString
, TRUE
);
2812 ShellCommandRegisterExit(FALSE
, 0);
2813 Status
= EFI_SUCCESS
;
2816 if (ShellGetExecutionBreakFlag()) {
2819 if (EFI_ERROR(Status
)) {
2822 if (ShellCommandGetExit()) {
2827 // If that commend did not update the CurrentCommand then we need to advance it...
2829 if (LastCommand
== NewScriptFile
->CurrentCommand
) {
2830 NewScriptFile
->CurrentCommand
= (SCRIPT_COMMAND_LIST
*)GetNextNode(&NewScriptFile
->CommandList
, &NewScriptFile
->CurrentCommand
->Link
);
2831 if (!IsNull(&NewScriptFile
->CommandList
, &NewScriptFile
->CurrentCommand
->Link
)) {
2832 NewScriptFile
->CurrentCommand
->Reset
= TRUE
;
2836 NewScriptFile
->CurrentCommand
= (SCRIPT_COMMAND_LIST
*)GetNextNode(&NewScriptFile
->CommandList
, &NewScriptFile
->CurrentCommand
->Link
);
2837 if (!IsNull(&NewScriptFile
->CommandList
, &NewScriptFile
->CurrentCommand
->Link
)) {
2838 NewScriptFile
->CurrentCommand
->Reset
= TRUE
;
2844 FreePool(CommandLine
);
2845 FreePool(CommandLine2
);
2846 ShellCommandSetNewScript (NULL
);
2849 // Only if this was the last script reset the state.
2851 if (ShellCommandGetCurrentScriptFile()==NULL
) {
2852 ShellCommandSetEchoState(PreScriptEchoState
);
2854 return (EFI_SUCCESS
);
2858 Function to process a NSH script file.
2860 @param[in] ScriptPath Pointer to the script file name (including file system path).
2861 @param[in] Handle the handle of the script file already opened.
2862 @param[in] CmdLine the command line to run.
2863 @param[in] ParamProtocol the shell parameters protocol pointer
2865 @retval EFI_SUCCESS the script completed sucessfully
2870 IN CONST CHAR16
*ScriptPath
,
2871 IN SHELL_FILE_HANDLE Handle OPTIONAL
,
2872 IN CONST CHAR16
*CmdLine
,
2873 IN EFI_SHELL_PARAMETERS_PROTOCOL
*ParamProtocol
2877 SHELL_FILE_HANDLE FileHandle
;
2881 if (ShellIsFile(ScriptPath
) != EFI_SUCCESS
) {
2882 return (EFI_INVALID_PARAMETER
);
2886 // get the argc and argv updated for scripts
2888 Status
= UpdateArgcArgv(ParamProtocol
, CmdLine
, &Argv
, &Argc
);
2889 if (!EFI_ERROR(Status
)) {
2891 if (Handle
== NULL
) {
2895 Status
= ShellOpenFileByName(ScriptPath
, &FileHandle
, EFI_FILE_MODE_READ
, 0);
2896 if (!EFI_ERROR(Status
)) {
2900 Status
= RunScriptFileHandle(FileHandle
, ScriptPath
);
2903 // now close the file
2905 ShellCloseFile(&FileHandle
);
2908 Status
= RunScriptFileHandle(Handle
, ScriptPath
);
2913 // This is guarenteed to be called after UpdateArgcArgv no matter what else happened.
2914 // This is safe even if the update API failed. In this case, it may be a no-op.
2916 RestoreArgcArgv(ParamProtocol
, &Argv
, &Argc
);