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 Determine if a command line contains a split operation
195 @param[in] CmdLine The command line to parse.
197 @retval TRUE CmdLine has a valid split.
198 @retval FALSE CmdLine does not have a valid split.
203 IN CONST CHAR16
*CmdLine
206 CONST CHAR16
*TempSpot
;
207 CONST CHAR16
*FirstQuote
;
208 CONST CHAR16
*SecondQuote
;
210 FirstQuote
= FindNextInstance (CmdLine
, L
"\"", TRUE
);
212 TempSpot
= FindFirstCharacter(CmdLine
, L
"|", L
'^');
214 if (FirstQuote
== NULL
||
215 *FirstQuote
== CHAR_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
== CHAR_NULL
|| FirstQuote
> TempSpot
) {
227 SecondQuote
= FindNextInstance (FirstQuote
+ 1, L
"\"", TRUE
);
228 if (SecondQuote
== NULL
|| *SecondQuote
== CHAR_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 mnemory available.
253 InternalEfiShellStartCtrlSMonitor(
257 EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL
*SimpleEx
;
258 EFI_KEY_DATA KeyData
;
261 Status
= gBS
->OpenProtocol(
262 gST
->ConsoleInHandle
,
263 &gEfiSimpleTextInputExProtocolGuid
,
267 EFI_OPEN_PROTOCOL_GET_PROTOCOL
);
268 if (EFI_ERROR(Status
)) {
273 STRING_TOKEN (STR_SHELL_NO_IN_EX
),
274 ShellInfoObject
.HiiHandle
);
275 return (EFI_SUCCESS
);
278 KeyData
.KeyState
.KeyToggleState
= 0;
279 KeyData
.Key
.ScanCode
= 0;
280 KeyData
.KeyState
.KeyShiftState
= EFI_SHIFT_STATE_VALID
|EFI_LEFT_CONTROL_PRESSED
;
281 KeyData
.Key
.UnicodeChar
= L
's';
283 Status
= SimpleEx
->RegisterKeyNotify(
286 NotificationFunction
,
287 &ShellInfoObject
.CtrlSNotifyHandle1
);
289 KeyData
.KeyState
.KeyShiftState
= EFI_SHIFT_STATE_VALID
|EFI_RIGHT_CONTROL_PRESSED
;
290 if (!EFI_ERROR(Status
)) {
291 Status
= SimpleEx
->RegisterKeyNotify(
294 NotificationFunction
,
295 &ShellInfoObject
.CtrlSNotifyHandle2
);
297 KeyData
.KeyState
.KeyShiftState
= EFI_SHIFT_STATE_VALID
|EFI_LEFT_CONTROL_PRESSED
;
298 KeyData
.Key
.UnicodeChar
= 19;
300 if (!EFI_ERROR(Status
)) {
301 Status
= SimpleEx
->RegisterKeyNotify(
304 NotificationFunction
,
305 &ShellInfoObject
.CtrlSNotifyHandle3
);
307 KeyData
.KeyState
.KeyShiftState
= EFI_SHIFT_STATE_VALID
|EFI_RIGHT_CONTROL_PRESSED
;
308 if (!EFI_ERROR(Status
)) {
309 Status
= SimpleEx
->RegisterKeyNotify(
312 NotificationFunction
,
313 &ShellInfoObject
.CtrlSNotifyHandle4
);
321 The entry point for the application.
323 @param[in] ImageHandle The firmware allocated handle for the EFI image.
324 @param[in] SystemTable A pointer to the EFI System Table.
326 @retval EFI_SUCCESS The entry point is executed successfully.
327 @retval other Some error occurs when executing this entry point.
333 IN EFI_HANDLE ImageHandle
,
334 IN EFI_SYSTEM_TABLE
*SystemTable
340 EFI_HANDLE ConInHandle
;
341 EFI_SIMPLE_TEXT_INPUT_PROTOCOL
*OldConIn
;
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
);
446 // Check the command line
448 Status
= ProcessCommandLine ();
449 if (EFI_ERROR (Status
)) {
454 // If shell support level is >= 1 create the mappings and paths
456 if (PcdGet8(PcdShellSupportLevel
) >= 1) {
457 Status
= ShellCommandCreateInitialMappingsAndPaths();
461 // save the device path for the loaded image and the device path for the filepath (under loaded image)
462 // These are where to look for the startup.nsh file
464 Status
= GetDevicePathsForImageAndFile(&ShellInfoObject
.ImageDevPath
, &ShellInfoObject
.FileDevPath
);
465 ASSERT_EFI_ERROR(Status
);
468 // Display the version
470 if (!ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoVersion
) {
473 gST
->ConOut
->Mode
->CursorRow
,
475 STRING_TOKEN (STR_VER_OUTPUT_MAIN_SHELL
),
476 ShellInfoObject
.HiiHandle
,
477 SupportLevel
[PcdGet8(PcdShellSupportLevel
)],
478 gEfiShellProtocol
->MajorVersion
,
479 gEfiShellProtocol
->MinorVersion
486 STRING_TOKEN (STR_VER_OUTPUT_MAIN_SUPPLIER
),
487 ShellInfoObject
.HiiHandle
,
488 (CHAR16
*) PcdGetPtr (PcdShellSupplier
)
495 STRING_TOKEN (STR_VER_OUTPUT_MAIN_UEFI
),
496 ShellInfoObject
.HiiHandle
,
497 (gST
->Hdr
.Revision
&0xffff0000)>>16,
498 (gST
->Hdr
.Revision
&0x0000ffff),
500 gST
->FirmwareRevision
505 // Display the mapping
507 if (PcdGet8(PcdShellSupportLevel
) >= 2 && !ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoMap
) {
508 Status
= RunCommand(L
"map");
509 ASSERT_EFI_ERROR(Status
);
513 // init all the built in alias'
515 Status
= SetBuiltInAlias();
516 ASSERT_EFI_ERROR(Status
);
519 // Initialize environment variables
521 if (ShellCommandGetProfileList() != NULL
) {
522 Status
= InternalEfiShellSetEnv(L
"profiles", ShellCommandGetProfileList(), TRUE
);
523 ASSERT_EFI_ERROR(Status
);
527 TempString
= AllocateZeroPool(Size
);
529 UnicodeSPrint(TempString
, Size
, L
"%d", PcdGet8(PcdShellSupportLevel
));
530 Status
= InternalEfiShellSetEnv(L
"uefishellsupport", TempString
, TRUE
);
531 ASSERT_EFI_ERROR(Status
);
533 UnicodeSPrint(TempString
, Size
, L
"%d.%d", ShellInfoObject
.NewEfiShellProtocol
->MajorVersion
, ShellInfoObject
.NewEfiShellProtocol
->MinorVersion
);
534 Status
= InternalEfiShellSetEnv(L
"uefishellversion", TempString
, TRUE
);
535 ASSERT_EFI_ERROR(Status
);
537 UnicodeSPrint(TempString
, Size
, L
"%d.%d", (gST
->Hdr
.Revision
& 0xFFFF0000) >> 16, gST
->Hdr
.Revision
& 0x0000FFFF);
538 Status
= InternalEfiShellSetEnv(L
"uefiversion", TempString
, TRUE
);
539 ASSERT_EFI_ERROR(Status
);
541 FreePool(TempString
);
543 if (!EFI_ERROR(Status
)) {
544 if (!ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoInterrupt
) {
546 // Set up the event for CTRL-C monitoring...
548 Status
= InernalEfiShellStartMonitor();
551 if (!EFI_ERROR(Status
) && !ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoConsoleIn
) {
553 // Set up the event for CTRL-S monitoring...
555 Status
= InternalEfiShellStartCtrlSMonitor();
558 if (!EFI_ERROR(Status
) && ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoConsoleIn
) {
560 // close off the gST->ConIn
562 OldConIn
= gST
->ConIn
;
563 ConInHandle
= gST
->ConsoleInHandle
;
564 gST
->ConIn
= CreateSimpleTextInOnFile((SHELL_FILE_HANDLE
)&FileInterfaceNulFile
, &gST
->ConsoleInHandle
);
570 if (!EFI_ERROR(Status
) && PcdGet8(PcdShellSupportLevel
) >= 1) {
572 // process the startup script or launch the called app.
574 Status
= DoStartupScript(ShellInfoObject
.ImageDevPath
, ShellInfoObject
.FileDevPath
);
577 if (!ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.Exit
&& !ShellCommandGetExit() && (PcdGet8(PcdShellSupportLevel
) >= 3 || PcdGetBool(PcdShellForceConsole
)) && !EFI_ERROR(Status
) && !ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoConsoleIn
) {
579 // begin the UI waiting loop
583 // clean out all the memory allocated for CONST <something> * return values
584 // between each shell prompt presentation
586 if (!IsListEmpty(&ShellInfoObject
.BufferToFreeList
.Link
)){
587 FreeBufferList(&ShellInfoObject
.BufferToFreeList
);
591 // Reset page break back to default.
593 ShellInfoObject
.PageBreakEnabled
= PcdGetBool(PcdShellPageBreakDefault
);
594 ASSERT (ShellInfoObject
.ConsoleInfo
!= NULL
);
595 ShellInfoObject
.ConsoleInfo
->Enabled
= TRUE
;
596 ShellInfoObject
.ConsoleInfo
->RowCounter
= 0;
599 // Reset the CTRL-C event (yes we ignore the return values)
601 Status
= gBS
->CheckEvent (ShellInfoObject
.NewEfiShellProtocol
->ExecutionBreak
);
606 Status
= DoShellPrompt();
607 } while (!ShellCommandGetExit());
609 if (OldConIn
!= NULL
&& ConInHandle
!= NULL
) {
610 CloseSimpleTextInOnFile (gST
->ConIn
);
611 gST
->ConIn
= OldConIn
;
612 gST
->ConsoleInHandle
= ConInHandle
;
619 // uninstall protocols / free memory / etc...
621 if (ShellInfoObject
.UserBreakTimer
!= NULL
) {
622 gBS
->CloseEvent(ShellInfoObject
.UserBreakTimer
);
623 DEBUG_CODE(ShellInfoObject
.UserBreakTimer
= NULL
;);
625 if (ShellInfoObject
.ImageDevPath
!= NULL
) {
626 FreePool(ShellInfoObject
.ImageDevPath
);
627 DEBUG_CODE(ShellInfoObject
.ImageDevPath
= NULL
;);
629 if (ShellInfoObject
.FileDevPath
!= NULL
) {
630 FreePool(ShellInfoObject
.FileDevPath
);
631 DEBUG_CODE(ShellInfoObject
.FileDevPath
= NULL
;);
633 if (ShellInfoObject
.NewShellParametersProtocol
!= NULL
) {
634 CleanUpShellParametersProtocol(ShellInfoObject
.NewShellParametersProtocol
);
635 DEBUG_CODE(ShellInfoObject
.NewShellParametersProtocol
= NULL
;);
637 if (ShellInfoObject
.NewEfiShellProtocol
!= NULL
){
638 if (ShellInfoObject
.NewEfiShellProtocol
->IsRootShell()){
639 InternalEfiShellSetEnv(L
"cwd", NULL
, TRUE
);
641 CleanUpShellProtocol(ShellInfoObject
.NewEfiShellProtocol
);
642 DEBUG_CODE(ShellInfoObject
.NewEfiShellProtocol
= NULL
;);
645 if (!IsListEmpty(&ShellInfoObject
.BufferToFreeList
.Link
)){
646 FreeBufferList(&ShellInfoObject
.BufferToFreeList
);
649 if (!IsListEmpty(&ShellInfoObject
.SplitList
.Link
)){
650 ASSERT(FALSE
); ///@todo finish this de-allocation.
653 if (ShellInfoObject
.ShellInitSettings
.FileName
!= NULL
) {
654 FreePool(ShellInfoObject
.ShellInitSettings
.FileName
);
655 DEBUG_CODE(ShellInfoObject
.ShellInitSettings
.FileName
= NULL
;);
658 if (ShellInfoObject
.ShellInitSettings
.FileOptions
!= NULL
) {
659 FreePool(ShellInfoObject
.ShellInitSettings
.FileOptions
);
660 DEBUG_CODE(ShellInfoObject
.ShellInitSettings
.FileOptions
= NULL
;);
663 if (ShellInfoObject
.HiiHandle
!= NULL
) {
664 HiiRemovePackages(ShellInfoObject
.HiiHandle
);
665 DEBUG_CODE(ShellInfoObject
.HiiHandle
= NULL
;);
668 if (!IsListEmpty(&ShellInfoObject
.ViewingSettings
.CommandHistory
.Link
)){
669 FreeBufferList(&ShellInfoObject
.ViewingSettings
.CommandHistory
);
672 ASSERT(ShellInfoObject
.ConsoleInfo
!= NULL
);
673 if (ShellInfoObject
.ConsoleInfo
!= NULL
) {
674 ConsoleLoggerUninstall(ShellInfoObject
.ConsoleInfo
);
675 FreePool(ShellInfoObject
.ConsoleInfo
);
676 DEBUG_CODE(ShellInfoObject
.ConsoleInfo
= NULL
;);
679 if (ShellCommandGetExit()) {
680 return ((EFI_STATUS
)ShellCommandGetExitCode());
686 Sets all the alias' that were registered with the ShellCommandLib library.
688 @retval EFI_SUCCESS all init commands were run sucessfully.
696 CONST ALIAS_LIST
*List
;
700 // Get all the commands we want to run
702 List
= ShellCommandGetInitAliasList();
705 // for each command in the List
707 for ( Node
= (ALIAS_LIST
*)GetFirstNode(&List
->Link
)
708 ; !IsNull (&List
->Link
, &Node
->Link
)
709 ; Node
= (ALIAS_LIST
*)GetNextNode(&List
->Link
, &Node
->Link
)
712 // install the alias'
714 Status
= InternalSetAlias(Node
->CommandString
, Node
->Alias
, TRUE
);
715 ASSERT_EFI_ERROR(Status
);
717 return (EFI_SUCCESS
);
721 Internal function to determine if 2 command names are really the same.
723 @param[in] Command1 The pointer to the first command name.
724 @param[in] Command2 The pointer to the second command name.
726 @retval TRUE The 2 command names are the same.
727 @retval FALSE The 2 command names are not the same.
732 IN CONST CHAR16
*Command1
,
733 IN CONST CHAR16
*Command2
736 if (StringNoCaseCompare(&Command1
, &Command2
) == 0) {
743 Internal function to determine if a command is a script only command.
745 @param[in] CommandName The pointer to the command name.
747 @retval TRUE The command is a script only command.
748 @retval FALSE The command is not a script only command.
753 IN CONST CHAR16
*CommandName
756 if (IsCommand(CommandName
, L
"for")
757 ||IsCommand(CommandName
, L
"endfor")
758 ||IsCommand(CommandName
, L
"if")
759 ||IsCommand(CommandName
, L
"else")
760 ||IsCommand(CommandName
, L
"endif")
761 ||IsCommand(CommandName
, L
"goto")) {
768 This function will populate the 2 device path protocol parameters based on the
769 global gImageHandle. The DevPath will point to the device path for the handle that has
770 loaded image protocol installed on it. The FilePath will point to the device path
771 for the file that was loaded.
773 @param[in, out] DevPath On a sucessful return the device path to the loaded image.
774 @param[in, out] FilePath On a sucessful return the device path to the file.
776 @retval EFI_SUCCESS The 2 device paths were sucessfully returned.
777 @retval other A error from gBS->HandleProtocol.
783 GetDevicePathsForImageAndFile (
784 IN OUT EFI_DEVICE_PATH_PROTOCOL
**DevPath
,
785 IN OUT EFI_DEVICE_PATH_PROTOCOL
**FilePath
789 EFI_LOADED_IMAGE_PROTOCOL
*LoadedImage
;
790 EFI_DEVICE_PATH_PROTOCOL
*ImageDevicePath
;
792 ASSERT(DevPath
!= NULL
);
793 ASSERT(FilePath
!= NULL
);
795 Status
= gBS
->OpenProtocol (
797 &gEfiLoadedImageProtocolGuid
,
798 (VOID
**)&LoadedImage
,
801 EFI_OPEN_PROTOCOL_GET_PROTOCOL
803 if (!EFI_ERROR (Status
)) {
804 Status
= gBS
->OpenProtocol (
805 LoadedImage
->DeviceHandle
,
806 &gEfiDevicePathProtocolGuid
,
807 (VOID
**)&ImageDevicePath
,
810 EFI_OPEN_PROTOCOL_GET_PROTOCOL
812 if (!EFI_ERROR (Status
)) {
813 *DevPath
= DuplicateDevicePath (ImageDevicePath
);
814 *FilePath
= DuplicateDevicePath (LoadedImage
->FilePath
);
816 LoadedImage
->DeviceHandle
,
817 &gEfiDevicePathProtocolGuid
,
823 &gEfiLoadedImageProtocolGuid
,
831 Process all Uefi Shell 2.0 command line options.
833 see Uefi Shell 2.0 section 3.2 for full details.
835 the command line must resemble the following:
837 shell.efi [ShellOpt-options] [options] [file-name [file-name-options]]
839 ShellOpt-options Options which control the initialization behavior of the shell.
840 These options are read from the EFI global variable "ShellOpt"
841 and are processed before options or file-name.
843 options Options which control the initialization behavior of the shell.
845 file-name The name of a UEFI shell application or script to be executed
846 after initialization is complete. By default, if file-name is
847 specified, then -nostartup is implied. Scripts are not supported
850 file-name-options The command-line options that are passed to file-name when it
853 This will initialize the ShellInfoObject.ShellInitSettings global variable.
855 @retval EFI_SUCCESS The variable is initialized.
866 CHAR16
*DelayValueStr
;
869 EFI_UNICODE_COLLATION_PROTOCOL
*UnicodeCollation
;
871 // `file-name-options` will contain arguments to `file-name` that we don't
872 // know about. This would cause ShellCommandLineParse to error, so we parse
873 // arguments manually, ignoring those after the first thing that doesn't look
874 // like a shell option (which is assumed to be `file-name`).
876 Status
= gBS
->LocateProtocol (
877 &gEfiUnicodeCollationProtocolGuid
,
879 (VOID
**) &UnicodeCollation
881 if (EFI_ERROR (Status
)) {
885 // Set default options
886 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.Startup
= FALSE
;
887 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoStartup
= FALSE
;
888 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoConsoleOut
= FALSE
;
889 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoConsoleIn
= FALSE
;
890 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoInterrupt
= FALSE
;
891 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoMap
= FALSE
;
892 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoVersion
= FALSE
;
893 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.Delay
= FALSE
;
894 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.Exit
= FALSE
;
895 ShellInfoObject
.ShellInitSettings
.Delay
= 5;
898 // Start LoopVar at 0 to parse only optional arguments at Argv[0]
899 // and parse other parameters from Argv[1]. This is for use case that
900 // UEFI Shell boot option is created, and OptionalData is provided
901 // that starts with shell command-line options.
903 for (LoopVar
= 0 ; LoopVar
< gEfiShellParametersProtocol
->Argc
; LoopVar
++) {
904 CurrentArg
= gEfiShellParametersProtocol
->Argv
[LoopVar
];
905 if (UnicodeCollation
->StriColl (
910 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.Startup
= TRUE
;
912 else if (UnicodeCollation
->StriColl (
917 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoStartup
= TRUE
;
919 else if (UnicodeCollation
->StriColl (
924 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoConsoleOut
= TRUE
;
926 else if (UnicodeCollation
->StriColl (
931 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoConsoleIn
= TRUE
;
933 else if (UnicodeCollation
->StriColl (
938 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoInterrupt
= TRUE
;
940 else if (UnicodeCollation
->StriColl (
945 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoMap
= TRUE
;
947 else if (UnicodeCollation
->StriColl (
952 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoVersion
= TRUE
;
954 else if (UnicodeCollation
->StriColl (
959 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.Delay
= TRUE
;
960 // Check for optional delay value following "-delay"
961 DelayValueStr
= gEfiShellParametersProtocol
->Argv
[LoopVar
+ 1];
962 if (DelayValueStr
!= NULL
){
963 if (*DelayValueStr
== L
':') {
966 if (!EFI_ERROR(ShellConvertStringToUint64 (
972 ShellInfoObject
.ShellInitSettings
.Delay
= (UINTN
)DelayValue
;
976 } else if (UnicodeCollation
->StriColl (
981 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.Exit
= TRUE
;
982 } else if (StrnCmp (L
"-", CurrentArg
, 1) == 0) {
983 // Unrecognised option
984 ShellPrintHiiEx(-1, -1, NULL
,
985 STRING_TOKEN (STR_GEN_PROBLEM
),
986 ShellInfoObject
.HiiHandle
,
989 return EFI_INVALID_PARAMETER
;
992 // First argument should be Shell.efi image name
998 ShellInfoObject
.ShellInitSettings
.FileName
= AllocateCopyPool(StrSize(CurrentArg
), CurrentArg
);
999 if (ShellInfoObject
.ShellInitSettings
.FileName
== NULL
) {
1000 return (EFI_OUT_OF_RESOURCES
);
1003 // We found `file-name`.
1005 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoStartup
= 1;
1008 // Add `file-name-options`
1009 for (Size
= 0 ; LoopVar
< gEfiShellParametersProtocol
->Argc
; LoopVar
++) {
1010 ASSERT((ShellInfoObject
.ShellInitSettings
.FileOptions
== NULL
&& Size
== 0) || (ShellInfoObject
.ShellInitSettings
.FileOptions
!= NULL
));
1011 StrnCatGrow(&ShellInfoObject
.ShellInitSettings
.FileOptions
,
1015 if (ShellInfoObject
.ShellInitSettings
.FileOptions
== NULL
) {
1016 SHELL_FREE_NON_NULL(ShellInfoObject
.ShellInitSettings
.FileName
);
1017 return (EFI_OUT_OF_RESOURCES
);
1019 StrnCatGrow(&ShellInfoObject
.ShellInitSettings
.FileOptions
,
1021 gEfiShellParametersProtocol
->Argv
[LoopVar
],
1023 if (ShellInfoObject
.ShellInitSettings
.FileOptions
== NULL
) {
1024 SHELL_FREE_NON_NULL(ShellInfoObject
.ShellInitSettings
.FileName
);
1025 return (EFI_OUT_OF_RESOURCES
);
1031 // "-nointerrupt" overrides "-delay"
1032 if (ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoInterrupt
) {
1033 ShellInfoObject
.ShellInitSettings
.Delay
= 0;
1040 Handles all interaction with the default startup script.
1042 this will check that the correct command line parameters were passed, handle the delay, and then start running the script.
1044 @param ImagePath the path to the image for shell. first place to look for the startup script
1045 @param FilePath the path to the file for shell. second place to look for the startup script.
1047 @retval EFI_SUCCESS the variable is initialized.
1052 IN EFI_DEVICE_PATH_PROTOCOL
*ImagePath
,
1053 IN EFI_DEVICE_PATH_PROTOCOL
*FilePath
1059 SHELL_FILE_HANDLE FileHandle
;
1060 EFI_DEVICE_PATH_PROTOCOL
*NewPath
;
1061 EFI_DEVICE_PATH_PROTOCOL
*NamePath
;
1062 CHAR16
*FileStringPath
;
1065 CONST CHAR16
*MapName
;
1067 Key
.UnicodeChar
= CHAR_NULL
;
1071 if (!ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.Startup
&& ShellInfoObject
.ShellInitSettings
.FileName
!= NULL
) {
1073 // launch something else instead
1075 NewSize
= StrSize(ShellInfoObject
.ShellInitSettings
.FileName
);
1076 if (ShellInfoObject
.ShellInitSettings
.FileOptions
!= NULL
) {
1077 NewSize
+= StrSize(ShellInfoObject
.ShellInitSettings
.FileOptions
) + sizeof(CHAR16
);
1079 FileStringPath
= AllocateZeroPool(NewSize
);
1080 if (FileStringPath
== NULL
) {
1081 return (EFI_OUT_OF_RESOURCES
);
1083 StrnCpy(FileStringPath
, ShellInfoObject
.ShellInitSettings
.FileName
, NewSize
/sizeof(CHAR16
) -1);
1084 if (ShellInfoObject
.ShellInitSettings
.FileOptions
!= NULL
) {
1085 StrnCat(FileStringPath
, L
" ", NewSize
/sizeof(CHAR16
) - StrLen(FileStringPath
) -1);
1086 StrnCat(FileStringPath
, ShellInfoObject
.ShellInitSettings
.FileOptions
, NewSize
/sizeof(CHAR16
) - StrLen(FileStringPath
) -1);
1088 Status
= RunCommand(FileStringPath
);
1089 FreePool(FileStringPath
);
1095 // for shell level 0 we do no scripts
1096 // Without the Startup bit overriding we allow for nostartup to prevent scripts
1098 if ( (PcdGet8(PcdShellSupportLevel
) < 1)
1099 || (ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoStartup
&& !ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.Startup
)
1101 return (EFI_SUCCESS
);
1104 gST
->ConOut
->EnableCursor(gST
->ConOut
, FALSE
);
1106 // print out our warning and see if they press a key
1108 for ( Status
= EFI_UNSUPPORTED
, Delay
= ShellInfoObject
.ShellInitSettings
.Delay
1109 ; Delay
!= 0 && EFI_ERROR(Status
)
1112 ShellPrintHiiEx(0, gST
->ConOut
->Mode
->CursorRow
, NULL
, STRING_TOKEN (STR_SHELL_STARTUP_QUESTION
), ShellInfoObject
.HiiHandle
, Delay
);
1113 gBS
->Stall (1000000);
1114 if (!ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoConsoleIn
) {
1115 Status
= gST
->ConIn
->ReadKeyStroke (gST
->ConIn
, &Key
);
1118 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_CRLF
), ShellInfoObject
.HiiHandle
);
1119 gST
->ConOut
->EnableCursor(gST
->ConOut
, TRUE
);
1124 if (Status
== EFI_SUCCESS
&& Key
.UnicodeChar
== 0 && Key
.ScanCode
== SCAN_ESC
) {
1125 return (EFI_SUCCESS
);
1129 // Try the first location (must be file system)
1131 MapName
= ShellInfoObject
.NewEfiShellProtocol
->GetMapFromDevicePath(&ImagePath
);
1132 if (MapName
!= NULL
) {
1133 FileStringPath
= NULL
;
1135 FileStringPath
= StrnCatGrow(&FileStringPath
, &NewSize
, MapName
, 0);
1136 if (FileStringPath
== NULL
) {
1137 Status
= EFI_OUT_OF_RESOURCES
;
1139 TempSpot
= StrStr(FileStringPath
, L
";");
1140 if (TempSpot
!= NULL
) {
1141 *TempSpot
= CHAR_NULL
;
1143 FileStringPath
= StrnCatGrow(&FileStringPath
, &NewSize
, ((FILEPATH_DEVICE_PATH
*)FilePath
)->PathName
, 0);
1144 PathRemoveLastItem(FileStringPath
);
1145 FileStringPath
= StrnCatGrow(&FileStringPath
, &NewSize
, mStartupScript
, 0);
1146 Status
= ShellInfoObject
.NewEfiShellProtocol
->OpenFileByName(FileStringPath
, &FileHandle
, EFI_FILE_MODE_READ
);
1147 FreePool(FileStringPath
);
1150 if (EFI_ERROR(Status
)) {
1151 NamePath
= FileDevicePath (NULL
, mStartupScript
);
1152 NewPath
= AppendDevicePathNode (ImagePath
, NamePath
);
1158 Status
= InternalOpenFileDevicePath(NewPath
, &FileHandle
, EFI_FILE_MODE_READ
, 0);
1162 // If we got a file, run it
1164 if (!EFI_ERROR(Status
) && FileHandle
!= NULL
) {
1165 Status
= RunScriptFile (mStartupScript
, FileHandle
, L
"", ShellInfoObject
.NewShellParametersProtocol
);
1166 ShellInfoObject
.NewEfiShellProtocol
->CloseFile(FileHandle
);
1168 FileStringPath
= ShellFindFilePath(mStartupScript
);
1169 if (FileStringPath
== NULL
) {
1171 // we return success since we dont need to have a startup script
1173 Status
= EFI_SUCCESS
;
1174 ASSERT(FileHandle
== NULL
);
1176 Status
= RunScriptFile(FileStringPath
, NULL
, L
"", ShellInfoObject
.NewShellParametersProtocol
);
1177 FreePool(FileStringPath
);
1186 Function to perform the shell prompt looping. It will do a single prompt,
1187 dispatch the result, and then return. It is expected that the caller will
1188 call this function in a loop many times.
1191 @retval RETURN_ABORTED
1202 CONST CHAR16
*CurDir
;
1209 // Get screen setting to decide size of the command line buffer
1211 gST
->ConOut
->QueryMode (gST
->ConOut
, gST
->ConOut
->Mode
->Mode
, &Column
, &Row
);
1212 BufferSize
= Column
* Row
* sizeof (CHAR16
);
1213 CmdLine
= AllocateZeroPool (BufferSize
);
1214 if (CmdLine
== NULL
) {
1215 return EFI_OUT_OF_RESOURCES
;
1218 CurDir
= ShellInfoObject
.NewEfiShellProtocol
->GetEnv(L
"cwd");
1223 gST
->ConOut
->SetCursorPosition (gST
->ConOut
, 0, gST
->ConOut
->Mode
->CursorRow
);
1225 if (CurDir
!= NULL
&& StrLen(CurDir
) > 1) {
1226 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_CURDIR
), ShellInfoObject
.HiiHandle
, CurDir
);
1228 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_SHELL
), ShellInfoObject
.HiiHandle
);
1232 // Read a line from the console
1234 Status
= ShellInfoObject
.NewEfiShellProtocol
->ReadFile(ShellInfoObject
.NewShellParametersProtocol
->StdIn
, &BufferSize
, CmdLine
);
1237 // Null terminate the string and parse it
1239 if (!EFI_ERROR (Status
)) {
1240 CmdLine
[BufferSize
/ sizeof (CHAR16
)] = CHAR_NULL
;
1241 Status
= RunCommand(CmdLine
);
1245 // Done with this command
1252 Add a buffer to the Buffer To Free List for safely returning buffers to other
1253 places without risking letting them modify internal shell information.
1255 @param Buffer Something to pass to FreePool when the shell is exiting.
1259 AddBufferToFreeList(
1263 BUFFER_LIST
*BufferListEntry
;
1265 if (Buffer
== NULL
) {
1269 BufferListEntry
= AllocateZeroPool(sizeof(BUFFER_LIST
));
1270 ASSERT(BufferListEntry
!= NULL
);
1271 BufferListEntry
->Buffer
= Buffer
;
1272 InsertTailList(&ShellInfoObject
.BufferToFreeList
.Link
, &BufferListEntry
->Link
);
1277 Add a buffer to the Line History List
1279 @param Buffer The line buffer to add.
1283 AddLineToCommandHistory(
1284 IN CONST CHAR16
*Buffer
1289 Node
= AllocateZeroPool(sizeof(BUFFER_LIST
));
1290 ASSERT(Node
!= NULL
);
1291 Node
->Buffer
= AllocateCopyPool(StrSize(Buffer
), Buffer
);
1292 ASSERT(Node
->Buffer
!= NULL
);
1294 InsertTailList(&ShellInfoObject
.ViewingSettings
.CommandHistory
.Link
, &Node
->Link
);
1298 Checks if a string is an alias for another command. If yes, then it replaces the alias name
1299 with the correct command name.
1301 @param[in, out] CommandString Upon entry the potential alias. Upon return the
1302 command name if it was an alias. If it was not
1303 an alias it will be unchanged. This function may
1304 change the buffer to fit the command name.
1306 @retval EFI_SUCCESS The name was changed.
1307 @retval EFI_SUCCESS The name was not an alias.
1308 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
1313 IN OUT CHAR16
**CommandString
1316 CONST CHAR16
*NewString
;
1318 NewString
= ShellInfoObject
.NewEfiShellProtocol
->GetAlias(*CommandString
, NULL
);
1319 if (NewString
== NULL
) {
1320 return (EFI_SUCCESS
);
1322 FreePool(*CommandString
);
1323 *CommandString
= AllocateCopyPool(StrSize(NewString
), NewString
);
1324 if (*CommandString
== NULL
) {
1325 return (EFI_OUT_OF_RESOURCES
);
1327 return (EFI_SUCCESS
);
1331 This function will eliminate unreplaced (and therefore non-found) environment variables.
1333 @param[in,out] CmdLine The command line to update.
1337 StripUnreplacedEnvironmentVariables(
1338 IN OUT CHAR16
*CmdLine
1341 CHAR16
*FirstPercent
;
1343 CHAR16
*SecondPercent
;
1344 CHAR16
*SecondQuote
;
1345 CHAR16
*CurrentLocator
;
1347 for (CurrentLocator
= CmdLine
; CurrentLocator
!= NULL
; ) {
1348 FirstQuote
= FindNextInstance(CurrentLocator
, L
"\"", TRUE
);
1349 FirstPercent
= FindNextInstance(CurrentLocator
, L
"%", TRUE
);
1350 SecondPercent
= FirstPercent
!=NULL
?FindNextInstance(FirstPercent
+1, L
"%", TRUE
):NULL
;
1351 if (FirstPercent
== NULL
|| SecondPercent
== NULL
) {
1353 // If we ever dont have 2 % we are done.
1358 if (FirstQuote
!= NULL
&& FirstQuote
< FirstPercent
) {
1359 SecondQuote
= FindNextInstance(FirstQuote
+1, L
"\"", TRUE
);
1361 // Quote is first found
1364 if (SecondQuote
< FirstPercent
) {
1366 // restart after the pair of "
1368 CurrentLocator
= SecondQuote
+ 1;
1369 } else /* FirstPercent < SecondQuote */{
1371 // Restart on the first percent
1373 CurrentLocator
= FirstPercent
;
1378 if (FirstQuote
== NULL
|| *FirstQuote
== CHAR_NULL
|| SecondPercent
< FirstQuote
) {
1379 if (IsValidEnvironmentVariableName(FirstPercent
, SecondPercent
)) {
1381 // We need to remove from FirstPercent to SecondPercent
1383 CopyMem(FirstPercent
, SecondPercent
+ 1, StrSize(SecondPercent
+ 1));
1385 // dont need to update the locator. both % characters are gone.
1388 CurrentLocator
= SecondPercent
+ 1;
1392 CurrentLocator
= FirstQuote
;
1394 return (EFI_SUCCESS
);
1398 Function allocates a new command line and replaces all instances of environment
1399 variable names that are correctly preset to their values.
1401 If the return value is not NULL the memory must be caller freed.
1403 @param[in] OriginalCommandLine The original command line
1405 @retval NULL An error ocurred.
1406 @return The new command line with no environment variables present.
1410 ShellConvertVariables (
1411 IN CONST CHAR16
*OriginalCommandLine
1414 CONST CHAR16
*MasterEnvList
;
1416 CHAR16
*NewCommandLine1
;
1417 CHAR16
*NewCommandLine2
;
1421 SCRIPT_FILE
*CurrentScriptFile
;
1422 ALIAS_LIST
*AliasListNode
;
1424 ASSERT(OriginalCommandLine
!= NULL
);
1427 NewSize
= StrSize(OriginalCommandLine
);
1428 CurrentScriptFile
= ShellCommandGetCurrentScriptFile();
1431 ///@todo update this to handle the %0 - %9 for scripting only (borrow from line 1256 area) ? ? ?
1434 // calculate the size required for the post-conversion string...
1436 if (CurrentScriptFile
!= NULL
) {
1437 for (AliasListNode
= (ALIAS_LIST
*)GetFirstNode(&CurrentScriptFile
->SubstList
)
1438 ; !IsNull(&CurrentScriptFile
->SubstList
, &AliasListNode
->Link
)
1439 ; AliasListNode
= (ALIAS_LIST
*)GetNextNode(&CurrentScriptFile
->SubstList
, &AliasListNode
->Link
)
1441 for (Temp
= StrStr(OriginalCommandLine
, AliasListNode
->Alias
)
1443 ; Temp
= StrStr(Temp
+1, AliasListNode
->Alias
)
1446 // we need a preceeding and if there is space no ^ preceeding (if no space ignore)
1448 if ((((Temp
-OriginalCommandLine
)>2) && *(Temp
-2) != L
'^') || ((Temp
-OriginalCommandLine
)<=2)) {
1449 NewSize
+= StrSize(AliasListNode
->CommandString
);
1455 for (MasterEnvList
= EfiShellGetEnv(NULL
)
1456 ; MasterEnvList
!= NULL
&& *MasterEnvList
!= CHAR_NULL
//&& *(MasterEnvList+1) != CHAR_NULL
1457 ; MasterEnvList
+= StrLen(MasterEnvList
) + 1
1459 if (StrSize(MasterEnvList
) > ItemSize
) {
1460 ItemSize
= StrSize(MasterEnvList
);
1462 for (Temp
= StrStr(OriginalCommandLine
, MasterEnvList
)
1464 ; Temp
= StrStr(Temp
+1, MasterEnvList
)
1467 // we need a preceeding and following % and if there is space no ^ preceeding (if no space ignore)
1469 if (*(Temp
-1) == L
'%' && *(Temp
+StrLen(MasterEnvList
)) == L
'%' &&
1470 ((((Temp
-OriginalCommandLine
)>2) && *(Temp
-2) != L
'^') || ((Temp
-OriginalCommandLine
)<=2))) {
1471 NewSize
+=StrSize(EfiShellGetEnv(MasterEnvList
));
1477 // now do the replacements...
1479 NewCommandLine1
= AllocateCopyPool(NewSize
, OriginalCommandLine
);
1480 NewCommandLine2
= AllocateZeroPool(NewSize
);
1481 ItemTemp
= AllocateZeroPool(ItemSize
+(2*sizeof(CHAR16
)));
1482 if (NewCommandLine1
== NULL
|| NewCommandLine2
== NULL
|| ItemTemp
== NULL
) {
1483 SHELL_FREE_NON_NULL(NewCommandLine1
);
1484 SHELL_FREE_NON_NULL(NewCommandLine2
);
1485 SHELL_FREE_NON_NULL(ItemTemp
);
1488 for (MasterEnvList
= EfiShellGetEnv(NULL
)
1489 ; MasterEnvList
!= NULL
&& *MasterEnvList
!= CHAR_NULL
1490 ; MasterEnvList
+= StrLen(MasterEnvList
) + 1
1492 StrnCpy(ItemTemp
, L
"%", ((ItemSize
+(2*sizeof(CHAR16
)))/sizeof(CHAR16
))-1);
1493 StrnCat(ItemTemp
, MasterEnvList
, ((ItemSize
+(2*sizeof(CHAR16
)))/sizeof(CHAR16
))-1 - StrLen(ItemTemp
));
1494 StrnCat(ItemTemp
, L
"%", ((ItemSize
+(2*sizeof(CHAR16
)))/sizeof(CHAR16
))-1 - StrLen(ItemTemp
));
1495 ShellCopySearchAndReplace(NewCommandLine1
, NewCommandLine2
, NewSize
, ItemTemp
, EfiShellGetEnv(MasterEnvList
), TRUE
, FALSE
);
1496 StrnCpy(NewCommandLine1
, NewCommandLine2
, NewSize
/sizeof(CHAR16
)-1);
1498 if (CurrentScriptFile
!= NULL
) {
1499 for (AliasListNode
= (ALIAS_LIST
*)GetFirstNode(&CurrentScriptFile
->SubstList
)
1500 ; !IsNull(&CurrentScriptFile
->SubstList
, &AliasListNode
->Link
)
1501 ; AliasListNode
= (ALIAS_LIST
*)GetNextNode(&CurrentScriptFile
->SubstList
, &AliasListNode
->Link
)
1503 ShellCopySearchAndReplace(NewCommandLine1
, NewCommandLine2
, NewSize
, AliasListNode
->Alias
, AliasListNode
->CommandString
, TRUE
, FALSE
);
1504 StrnCpy(NewCommandLine1
, NewCommandLine2
, NewSize
/sizeof(CHAR16
)-1);
1509 // Remove non-existant environment variables
1511 StripUnreplacedEnvironmentVariables(NewCommandLine1
);
1514 // Now cleanup any straggler intentionally ignored "%" characters
1516 ShellCopySearchAndReplace(NewCommandLine1
, NewCommandLine2
, NewSize
, L
"^%", L
"%", TRUE
, FALSE
);
1517 StrnCpy(NewCommandLine1
, NewCommandLine2
, NewSize
/sizeof(CHAR16
)-1);
1519 FreePool(NewCommandLine2
);
1522 return (NewCommandLine1
);
1526 Internal function to run a command line with pipe usage.
1528 @param[in] CmdLine The pointer to the command line.
1529 @param[in] StdIn The pointer to the Standard input.
1530 @param[in] StdOut The pointer to the Standard output.
1532 @retval EFI_SUCCESS The split command is executed successfully.
1533 @retval other Some error occurs when executing the split command.
1538 IN CONST CHAR16
*CmdLine
,
1539 IN SHELL_FILE_HANDLE
*StdIn
,
1540 IN SHELL_FILE_HANDLE
*StdOut
1544 CHAR16
*NextCommandLine
;
1545 CHAR16
*OurCommandLine
;
1549 SHELL_FILE_HANDLE
*TempFileHandle
;
1552 ASSERT(StdOut
== NULL
);
1554 ASSERT(StrStr(CmdLine
, L
"|") != NULL
);
1556 Status
= EFI_SUCCESS
;
1557 NextCommandLine
= NULL
;
1558 OurCommandLine
= NULL
;
1562 NextCommandLine
= StrnCatGrow(&NextCommandLine
, &Size1
, StrStr(CmdLine
, L
"|")+1, 0);
1563 OurCommandLine
= StrnCatGrow(&OurCommandLine
, &Size2
, CmdLine
, StrStr(CmdLine
, L
"|") - CmdLine
);
1565 if (NextCommandLine
== NULL
|| OurCommandLine
== NULL
) {
1566 SHELL_FREE_NON_NULL(OurCommandLine
);
1567 SHELL_FREE_NON_NULL(NextCommandLine
);
1568 return (EFI_OUT_OF_RESOURCES
);
1569 } else if (StrStr(OurCommandLine
, L
"|") != NULL
|| Size1
== 0 || Size2
== 0) {
1570 SHELL_FREE_NON_NULL(OurCommandLine
);
1571 SHELL_FREE_NON_NULL(NextCommandLine
);
1572 return (EFI_INVALID_PARAMETER
);
1573 } else if (NextCommandLine
[0] != CHAR_NULL
&&
1574 NextCommandLine
[0] == L
'a' &&
1575 NextCommandLine
[1] == L
' '
1577 CopyMem(NextCommandLine
, NextCommandLine
+1, StrSize(NextCommandLine
) - sizeof(NextCommandLine
[0]));
1585 // make a SPLIT_LIST item and add to list
1587 Split
= AllocateZeroPool(sizeof(SPLIT_LIST
));
1588 ASSERT(Split
!= NULL
);
1589 Split
->SplitStdIn
= StdIn
;
1590 Split
->SplitStdOut
= ConvertEfiFileProtocolToShellHandle(CreateFileInterfaceMem(Unicode
), NULL
);
1591 ASSERT(Split
->SplitStdOut
!= NULL
);
1592 InsertHeadList(&ShellInfoObject
.SplitList
.Link
, &Split
->Link
);
1594 Status
= RunCommand(OurCommandLine
);
1597 // move the output from the first to the in to the second.
1599 TempFileHandle
= Split
->SplitStdOut
;
1600 if (Split
->SplitStdIn
== StdIn
) {
1601 Split
->SplitStdOut
= NULL
;
1603 Split
->SplitStdOut
= Split
->SplitStdIn
;
1605 Split
->SplitStdIn
= TempFileHandle
;
1606 ShellInfoObject
.NewEfiShellProtocol
->SetFilePosition(ConvertShellHandleToEfiFileProtocol(Split
->SplitStdIn
), 0);
1608 if (!EFI_ERROR(Status
)) {
1609 Status
= RunCommand(NextCommandLine
);
1613 // remove the top level from the ScriptList
1615 ASSERT((SPLIT_LIST
*)GetFirstNode(&ShellInfoObject
.SplitList
.Link
) == Split
);
1616 RemoveEntryList(&Split
->Link
);
1619 // Note that the original StdIn is now the StdOut...
1621 if (Split
->SplitStdOut
!= NULL
&& Split
->SplitStdOut
!= StdIn
) {
1622 ShellInfoObject
.NewEfiShellProtocol
->CloseFile(ConvertShellHandleToEfiFileProtocol(Split
->SplitStdOut
));
1624 if (Split
->SplitStdIn
!= NULL
) {
1625 ShellInfoObject
.NewEfiShellProtocol
->CloseFile(ConvertShellHandleToEfiFileProtocol(Split
->SplitStdIn
));
1629 FreePool(NextCommandLine
);
1630 FreePool(OurCommandLine
);
1636 Take the original command line, substitute any variables, free
1637 the original string, return the modified copy.
1639 @param[in] CmdLine pointer to the command line to update.
1641 @retval EFI_SUCCESS the function was successful.
1642 @retval EFI_OUT_OF_RESOURCES a memory allocation failed.
1646 ShellSubstituteVariables(
1651 NewCmdLine
= ShellConvertVariables(*CmdLine
);
1652 SHELL_FREE_NON_NULL(*CmdLine
);
1653 if (NewCmdLine
== NULL
) {
1654 return (EFI_OUT_OF_RESOURCES
);
1656 *CmdLine
= NewCmdLine
;
1657 return (EFI_SUCCESS
);
1661 Take the original command line, substitute any alias in the first group of space delimited characters, free
1662 the original string, return the modified copy.
1664 @param[in] CmdLine pointer to the command line to update.
1666 @retval EFI_SUCCESS the function was successful.
1667 @retval EFI_OUT_OF_RESOURCES a memory allocation failed.
1671 ShellSubstituteAliases(
1676 CHAR16
*CommandName
;
1678 UINTN PostAliasSize
;
1679 ASSERT(CmdLine
!= NULL
);
1680 ASSERT(*CmdLine
!= NULL
);
1684 if (StrStr((*CmdLine
), L
" ") == NULL
){
1685 StrnCatGrow(&CommandName
, NULL
, (*CmdLine
), 0);
1687 StrnCatGrow(&CommandName
, NULL
, (*CmdLine
), StrStr((*CmdLine
), L
" ") - (*CmdLine
));
1691 // This cannot happen 'inline' since the CmdLine can need extra space.
1694 if (!ShellCommandIsCommandOnList(CommandName
)) {
1696 // Convert via alias
1698 Status
= ShellConvertAlias(&CommandName
);
1699 if (EFI_ERROR(Status
)){
1703 NewCmdLine
= StrnCatGrow(&NewCmdLine
, &PostAliasSize
, CommandName
, 0);
1704 if (NewCmdLine
== NULL
) {
1705 SHELL_FREE_NON_NULL(CommandName
);
1706 SHELL_FREE_NON_NULL(*CmdLine
);
1707 return (EFI_OUT_OF_RESOURCES
);
1709 NewCmdLine
= StrnCatGrow(&NewCmdLine
, &PostAliasSize
, StrStr((*CmdLine
), L
" "), 0);
1710 if (NewCmdLine
== NULL
) {
1711 SHELL_FREE_NON_NULL(CommandName
);
1712 SHELL_FREE_NON_NULL(*CmdLine
);
1713 return (EFI_OUT_OF_RESOURCES
);
1716 NewCmdLine
= StrnCatGrow(&NewCmdLine
, NULL
, (*CmdLine
), 0);
1719 SHELL_FREE_NON_NULL(*CmdLine
);
1720 SHELL_FREE_NON_NULL(CommandName
);
1723 // re-assign the passed in double pointer to point to our newly allocated buffer
1725 *CmdLine
= NewCmdLine
;
1727 return (EFI_SUCCESS
);
1731 Takes the Argv[0] part of the command line and determine the meaning of it.
1733 @param[in] CmdName pointer to the command line to update.
1735 @retval Internal_Command The name is an internal command.
1736 @retval File_Sys_Change the name is a file system change.
1737 @retval Script_File_Name the name is a NSH script file.
1738 @retval Unknown_Invalid the name is unknown.
1739 @retval Efi_Application the name is an application (.EFI).
1741 SHELL_OPERATION_TYPES
1744 IN CONST CHAR16
*CmdName
1747 CHAR16
* FileWithPath
;
1748 CONST CHAR16
* TempLocation
;
1749 CONST CHAR16
* TempLocation2
;
1751 FileWithPath
= NULL
;
1753 // test for an internal command.
1755 if (ShellCommandIsCommandOnList(CmdName
)) {
1756 return (Internal_Command
);
1760 // Test for file system change request. anything ending with first : and cant have spaces.
1762 if (CmdName
[(StrLen(CmdName
)-1)] == L
':') {
1763 if ( StrStr(CmdName
, L
" ") != NULL
1764 || StrLen(StrStr(CmdName
, L
":")) > 1
1766 return (Unknown_Invalid
);
1768 return (File_Sys_Change
);
1774 if ((FileWithPath
= ShellFindFilePathEx(CmdName
, mExecutableExtensions
)) != NULL
) {
1776 // See if that file has a script file extension
1778 if (StrLen(FileWithPath
) > 4) {
1779 TempLocation
= FileWithPath
+StrLen(FileWithPath
)-4;
1780 TempLocation2
= mScriptExtension
;
1781 if (StringNoCaseCompare((VOID
*)(&TempLocation
), (VOID
*)(&TempLocation2
)) == 0) {
1782 SHELL_FREE_NON_NULL(FileWithPath
);
1783 return (Script_File_Name
);
1788 // Was a file, but not a script. we treat this as an application.
1790 SHELL_FREE_NON_NULL(FileWithPath
);
1791 return (Efi_Application
);
1794 SHELL_FREE_NON_NULL(FileWithPath
);
1796 // No clue what this is... return invalid flag...
1798 return (Unknown_Invalid
);
1802 Determine if the first item in a command line is valid.
1804 @param[in] CmdLine The command line to parse.
1806 @retval EFI_SUCCESS The item is valid.
1807 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
1808 @retval EFI_NOT_FOUND The operation type is unknown or invalid.
1813 IN CONST CHAR16
*CmdLine
1817 CHAR16
*FirstParameter
;
1823 Temp
= StrnCatGrow(&Temp
, NULL
, CmdLine
, 0);
1825 return (EFI_OUT_OF_RESOURCES
);
1828 FirstParameter
= StrStr(Temp
, L
"|");
1829 if (FirstParameter
!= NULL
) {
1830 *FirstParameter
= CHAR_NULL
;
1833 FirstParameter
= NULL
;
1836 // Process the command line
1838 Status
= ProcessCommandLineToFinal(&Temp
);
1840 if (!EFI_ERROR(Status
)) {
1841 FirstParameter
= AllocateZeroPool(StrSize(CmdLine
));
1842 if (FirstParameter
== NULL
) {
1843 SHELL_FREE_NON_NULL(Temp
);
1844 return (EFI_OUT_OF_RESOURCES
);
1846 TempWalker
= (CHAR16
*)Temp
;
1847 if (!EFI_ERROR(GetNextParameter(&TempWalker
, &FirstParameter
, StrSize(CmdLine
)))) {
1848 if (GetOperationType(FirstParameter
) == Unknown_Invalid
) {
1849 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_NOT_FOUND
), ShellInfoObject
.HiiHandle
, FirstParameter
);
1850 SetLastError(SHELL_NOT_FOUND
);
1851 Status
= EFI_NOT_FOUND
;
1856 SHELL_FREE_NON_NULL(Temp
);
1857 SHELL_FREE_NON_NULL(FirstParameter
);
1862 Determine if a command line contains with a split contains only valid commands.
1864 @param[in] CmdLine The command line to parse.
1866 @retval EFI_SUCCESS CmdLine has only valid commands, application, or has no split.
1867 @retval EFI_ABORTED CmdLine has at least one invalid command or application.
1872 IN CONST CHAR16
*CmdLine
1875 CONST CHAR16
*TempSpot
;
1879 // Verify up to the pipe or end character
1881 Status
= IsValidSplit(CmdLine
);
1882 if (EFI_ERROR(Status
)) {
1887 // If this was the only item, then get out
1889 if (!ContainsSplit(CmdLine
)) {
1890 return (EFI_SUCCESS
);
1894 // recurse to verify the next item
1896 TempSpot
= FindFirstCharacter(CmdLine
, L
"|", L
'^') + 1;
1897 return (VerifySplit(TempSpot
));
1901 Process a split based operation.
1903 @param[in] CmdLine pointer to the command line to process
1905 @retval EFI_SUCCESS The operation was successful
1906 @return an error occured.
1910 ProcessNewSplitCommandLine(
1911 IN CONST CHAR16
*CmdLine
1917 Status
= VerifySplit(CmdLine
);
1918 if (EFI_ERROR(Status
)) {
1925 // are we in an existing split???
1927 if (!IsListEmpty(&ShellInfoObject
.SplitList
.Link
)) {
1928 Split
= (SPLIT_LIST
*)GetFirstNode(&ShellInfoObject
.SplitList
.Link
);
1931 if (Split
== NULL
) {
1932 Status
= RunSplitCommand(CmdLine
, NULL
, NULL
);
1934 Status
= RunSplitCommand(CmdLine
, Split
->SplitStdIn
, Split
->SplitStdOut
);
1936 if (EFI_ERROR(Status
)) {
1937 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_INVALID_SPLIT
), ShellInfoObject
.HiiHandle
, CmdLine
);
1943 Handle a request to change the current file system.
1945 @param[in] CmdLine The passed in command line.
1947 @retval EFI_SUCCESS The operation was successful.
1952 IN CONST CHAR16
*CmdLine
1956 Status
= EFI_SUCCESS
;
1959 // make sure we are the right operation
1961 ASSERT(CmdLine
[(StrLen(CmdLine
)-1)] == L
':' && StrStr(CmdLine
, L
" ") == NULL
);
1964 // Call the protocol API to do the work
1966 Status
= ShellInfoObject
.NewEfiShellProtocol
->SetCurDir(NULL
, CmdLine
);
1969 // Report any errors
1971 if (EFI_ERROR(Status
)) {
1972 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_INVALID_MAPPING
), ShellInfoObject
.HiiHandle
, CmdLine
);
1979 Reprocess the command line to direct all -? to the help command.
1981 if found, will add "help" as argv[0], and move the rest later.
1983 @param[in,out] CmdLine pointer to the command line to update
1988 IN OUT CHAR16
**CmdLine
1991 CHAR16
*CurrentParameter
;
1994 CHAR16
*NewCommandLine
;
1997 Status
= EFI_SUCCESS
;
1999 CurrentParameter
= AllocateZeroPool(StrSize(*CmdLine
));
2000 if (CurrentParameter
== NULL
) {
2001 return (EFI_OUT_OF_RESOURCES
);
2005 while(Walker
!= NULL
&& *Walker
!= CHAR_NULL
) {
2006 LastWalker
= Walker
;
2007 if (!EFI_ERROR(GetNextParameter(&Walker
, &CurrentParameter
, StrSize(*CmdLine
)))) {
2008 if (StrStr(CurrentParameter
, L
"-?") == CurrentParameter
) {
2009 LastWalker
[0] = L
' ';
2010 LastWalker
[1] = L
' ';
2011 NewCommandLine
= AllocateZeroPool(StrSize(L
"help ") + StrSize(*CmdLine
));
2012 if (NewCommandLine
== NULL
) {
2013 Status
= EFI_OUT_OF_RESOURCES
;
2018 // We know the space is sufficient since we just calculated it.
2020 StrnCpy(NewCommandLine
, L
"help ", 5);
2021 StrnCat(NewCommandLine
, *CmdLine
, StrLen(*CmdLine
));
2022 SHELL_FREE_NON_NULL(*CmdLine
);
2023 *CmdLine
= NewCommandLine
;
2029 SHELL_FREE_NON_NULL(CurrentParameter
);
2035 Function to update the shell variable "lasterror".
2037 @param[in] ErrorCode the error code to put into lasterror.
2042 IN CONST SHELL_STATUS ErrorCode
2045 CHAR16 LeString
[19];
2046 if (sizeof(EFI_STATUS
) == sizeof(UINT64
)) {
2047 UnicodeSPrint(LeString
, sizeof(LeString
), L
"0x%Lx", ErrorCode
);
2049 UnicodeSPrint(LeString
, sizeof(LeString
), L
"0x%x", ErrorCode
);
2051 DEBUG_CODE(InternalEfiShellSetEnv(L
"debuglasterror", LeString
, TRUE
););
2052 InternalEfiShellSetEnv(L
"lasterror", LeString
, TRUE
);
2054 return (EFI_SUCCESS
);
2058 Converts the command line to it's post-processed form. this replaces variables and alias' per UEFI Shell spec.
2060 @param[in,out] CmdLine pointer to the command line to update
2062 @retval EFI_SUCCESS The operation was successful
2063 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
2064 @return some other error occured
2068 ProcessCommandLineToFinal(
2069 IN OUT CHAR16
**CmdLine
2073 TrimSpaces(CmdLine
);
2075 Status
= ShellSubstituteAliases(CmdLine
);
2076 if (EFI_ERROR(Status
)) {
2080 TrimSpaces(CmdLine
);
2082 Status
= ShellSubstituteVariables(CmdLine
);
2083 if (EFI_ERROR(Status
)) {
2086 ASSERT (*CmdLine
!= NULL
);
2088 TrimSpaces(CmdLine
);
2091 // update for help parsing
2093 if (StrStr(*CmdLine
, L
"?") != NULL
) {
2095 // This may do nothing if the ? does not indicate help.
2096 // Save all the details for in the API below.
2098 Status
= DoHelpUpdate(CmdLine
);
2101 TrimSpaces(CmdLine
);
2103 return (EFI_SUCCESS
);
2107 Run an internal shell command.
2109 This API will upadate the shell's environment since these commands are libraries.
2111 @param[in] CmdLine the command line to run.
2112 @param[in] FirstParameter the first parameter on the command line
2113 @param[in] ParamProtocol the shell parameters protocol pointer
2115 @retval EFI_SUCCESS The command was completed.
2116 @retval EFI_ABORTED The command's operation was aborted.
2121 IN CONST CHAR16
*CmdLine
,
2122 IN CHAR16
*FirstParameter
,
2123 IN EFI_SHELL_PARAMETERS_PROTOCOL
*ParamProtocol
2129 SHELL_STATUS CommandReturnedStatus
;
2134 NewCmdLine
= AllocateCopyPool (StrSize (CmdLine
), CmdLine
);
2135 if (NewCmdLine
== NULL
) {
2136 return EFI_OUT_OF_RESOURCES
;
2139 for (Walker
= NewCmdLine
; Walker
!= NULL
&& *Walker
!= CHAR_NULL
; Walker
++) {
2140 if (*Walker
== L
'^' && *(Walker
+1) == L
'#') {
2141 CopyMem(Walker
, Walker
+1, StrSize(Walker
) - sizeof(Walker
[0]));
2146 // get the argc and argv updated for internal commands
2148 Status
= UpdateArgcArgv(ParamProtocol
, NewCmdLine
, &Argv
, &Argc
);
2149 if (!EFI_ERROR(Status
)) {
2151 // Run the internal command.
2153 Status
= ShellCommandRunCommandHandler(FirstParameter
, &CommandReturnedStatus
, &LastError
);
2155 if (!EFI_ERROR(Status
)) {
2157 // Update last error status.
2158 // some commands do not update last error.
2161 SetLastError(CommandReturnedStatus
);
2165 // Pass thru the exitcode from the app.
2167 if (ShellCommandGetExit()) {
2169 // An Exit was requested ("exit" command), pass its value up.
2171 Status
= CommandReturnedStatus
;
2172 } else if (CommandReturnedStatus
!= SHELL_SUCCESS
&& IsScriptOnlyCommand(FirstParameter
)) {
2174 // Always abort when a script only command fails for any reason
2176 Status
= EFI_ABORTED
;
2177 } else if (ShellCommandGetCurrentScriptFile() != NULL
&& CommandReturnedStatus
== SHELL_ABORTED
) {
2179 // Abort when in a script and a command aborted
2181 Status
= EFI_ABORTED
;
2187 // This is guarenteed to be called after UpdateArgcArgv no matter what else happened.
2188 // This is safe even if the update API failed. In this case, it may be a no-op.
2190 RestoreArgcArgv(ParamProtocol
, &Argv
, &Argc
);
2193 // If a script is running and the command is not a scipt only command, then
2194 // change return value to success so the script won't halt (unless aborted).
2196 // Script only commands have to be able halt the script since the script will
2197 // not operate if they are failing.
2199 if ( ShellCommandGetCurrentScriptFile() != NULL
2200 && !IsScriptOnlyCommand(FirstParameter
)
2201 && Status
!= EFI_ABORTED
2203 Status
= EFI_SUCCESS
;
2206 FreePool (NewCmdLine
);
2211 Function to run the command or file.
2213 @param[in] Type the type of operation being run.
2214 @param[in] CmdLine the command line to run.
2215 @param[in] FirstParameter the first parameter on the command line
2216 @param[in] ParamProtocol the shell parameters protocol pointer
2218 @retval EFI_SUCCESS The command was completed.
2219 @retval EFI_ABORTED The command's operation was aborted.
2224 IN SHELL_OPERATION_TYPES Type
,
2225 IN CONST CHAR16
*CmdLine
,
2226 IN CHAR16
*FirstParameter
,
2227 IN EFI_SHELL_PARAMETERS_PROTOCOL
*ParamProtocol
2231 EFI_STATUS StartStatus
;
2232 CHAR16
*CommandWithPath
;
2233 EFI_DEVICE_PATH_PROTOCOL
*DevPath
;
2234 SHELL_STATUS CalleeExitStatus
;
2236 Status
= EFI_SUCCESS
;
2237 CommandWithPath
= NULL
;
2239 CalleeExitStatus
= SHELL_INVALID_PARAMETER
;
2242 case Internal_Command
:
2243 Status
= RunInternalCommand(CmdLine
, FirstParameter
, ParamProtocol
);
2245 case Script_File_Name
:
2246 case Efi_Application
:
2248 // Process a fully qualified path
2250 if (StrStr(FirstParameter
, L
":") != NULL
) {
2251 ASSERT (CommandWithPath
== NULL
);
2252 if (ShellIsFile(FirstParameter
) == EFI_SUCCESS
) {
2253 CommandWithPath
= StrnCatGrow(&CommandWithPath
, NULL
, FirstParameter
, 0);
2258 // Process a relative path and also check in the path environment variable
2260 if (CommandWithPath
== NULL
) {
2261 CommandWithPath
= ShellFindFilePathEx(FirstParameter
, mExecutableExtensions
);
2265 // This should be impossible now.
2267 ASSERT(CommandWithPath
!= NULL
);
2270 // Make sure that path is not just a directory (or not found)
2272 if (!EFI_ERROR(ShellIsDirectory(CommandWithPath
))) {
2273 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_NOT_FOUND
), ShellInfoObject
.HiiHandle
, FirstParameter
);
2274 SetLastError(SHELL_NOT_FOUND
);
2277 case Script_File_Name
:
2278 Status
= RunScriptFile (CommandWithPath
, NULL
, CmdLine
, ParamProtocol
);
2280 case Efi_Application
:
2282 // Get the device path of the application image
2284 DevPath
= ShellInfoObject
.NewEfiShellProtocol
->GetDevicePathFromFilePath(CommandWithPath
);
2285 if (DevPath
== NULL
){
2286 Status
= EFI_OUT_OF_RESOURCES
;
2291 // Execute the device path
2293 Status
= InternalShellExecuteDevicePath(
2301 SHELL_FREE_NON_NULL(DevPath
);
2303 if(EFI_ERROR (Status
)) {
2304 CalleeExitStatus
= (SHELL_STATUS
) (Status
& (~MAX_BIT
));
2306 CalleeExitStatus
= (SHELL_STATUS
) StartStatus
;
2310 // Update last error status.
2312 // Status is an EFI_STATUS. Clear top bit to convert to SHELL_STATUS
2313 SetLastError(CalleeExitStatus
);
2329 SHELL_FREE_NON_NULL(CommandWithPath
);
2335 Function to setup StdIn, StdErr, StdOut, and then run the command or file.
2337 @param[in] Type the type of operation being run.
2338 @param[in] CmdLine the command line to run.
2339 @param[in] FirstParameter the first parameter on the command line.
2340 @param[in] ParamProtocol the shell parameters protocol pointer
2342 @retval EFI_SUCCESS The command was completed.
2343 @retval EFI_ABORTED The command's operation was aborted.
2347 SetupAndRunCommandOrFile(
2348 IN SHELL_OPERATION_TYPES Type
,
2350 IN CHAR16
*FirstParameter
,
2351 IN EFI_SHELL_PARAMETERS_PROTOCOL
*ParamProtocol
2355 SHELL_FILE_HANDLE OriginalStdIn
;
2356 SHELL_FILE_HANDLE OriginalStdOut
;
2357 SHELL_FILE_HANDLE OriginalStdErr
;
2358 SYSTEM_TABLE_INFO OriginalSystemTableInfo
;
2361 // Update the StdIn, StdOut, and StdErr for redirection to environment variables, files, etc... unicode and ASCII
2363 Status
= UpdateStdInStdOutStdErr(ParamProtocol
, CmdLine
, &OriginalStdIn
, &OriginalStdOut
, &OriginalStdErr
, &OriginalSystemTableInfo
);
2366 // The StdIn, StdOut, and StdErr are set up.
2367 // Now run the command, script, or application
2369 if (!EFI_ERROR(Status
)) {
2370 TrimSpaces(&CmdLine
);
2371 Status
= RunCommandOrFile(Type
, CmdLine
, FirstParameter
, ParamProtocol
);
2377 if (EFI_ERROR(Status
)) {
2378 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_ERROR
), ShellInfoObject
.HiiHandle
, (VOID
*)(Status
));
2382 // put back the original StdIn, StdOut, and StdErr
2384 RestoreStdInStdOutStdErr(ParamProtocol
, &OriginalStdIn
, &OriginalStdOut
, &OriginalStdErr
, &OriginalSystemTableInfo
);
2390 Function will process and run a command line.
2392 This will determine if the command line represents an internal shell
2393 command or dispatch an external application.
2395 @param[in] CmdLine The command line to parse.
2397 @retval EFI_SUCCESS The command was completed.
2398 @retval EFI_ABORTED The command's operation was aborted.
2403 IN CONST CHAR16
*CmdLine
2407 CHAR16
*CleanOriginal
;
2408 CHAR16
*FirstParameter
;
2410 SHELL_OPERATION_TYPES Type
;
2412 ASSERT(CmdLine
!= NULL
);
2413 if (StrLen(CmdLine
) == 0) {
2414 return (EFI_SUCCESS
);
2417 Status
= EFI_SUCCESS
;
2418 CleanOriginal
= NULL
;
2420 CleanOriginal
= StrnCatGrow(&CleanOriginal
, NULL
, CmdLine
, 0);
2421 if (CleanOriginal
== NULL
) {
2422 return (EFI_OUT_OF_RESOURCES
);
2425 TrimSpaces(&CleanOriginal
);
2428 // NULL out comments (leveraged from RunScriptFileHandle() ).
2429 // The # character on a line is used to denote that all characters on the same line
2430 // and to the right of the # are to be ignored by the shell.
2431 // Afterward, again remove spaces, in case any were between the last command-parameter and '#'.
2433 for (TempWalker
= CleanOriginal
; TempWalker
!= NULL
&& *TempWalker
!= CHAR_NULL
; TempWalker
++) {
2434 if (*TempWalker
== L
'^') {
2435 if (*(TempWalker
+ 1) == L
'#') {
2438 } else if (*TempWalker
== L
'#') {
2439 *TempWalker
= CHAR_NULL
;
2443 TrimSpaces(&CleanOriginal
);
2446 // Handle case that passed in command line is just 1 or more " " characters.
2448 if (StrLen (CleanOriginal
) == 0) {
2449 SHELL_FREE_NON_NULL(CleanOriginal
);
2450 return (EFI_SUCCESS
);
2453 Status
= ProcessCommandLineToFinal(&CleanOriginal
);
2454 if (EFI_ERROR(Status
)) {
2455 SHELL_FREE_NON_NULL(CleanOriginal
);
2460 // We dont do normal processing with a split command line (output from one command input to another)
2462 if (ContainsSplit(CleanOriginal
)) {
2463 Status
= ProcessNewSplitCommandLine(CleanOriginal
);
2464 SHELL_FREE_NON_NULL(CleanOriginal
);
2469 // We need the first parameter information so we can determine the operation type
2471 FirstParameter
= AllocateZeroPool(StrSize(CleanOriginal
));
2472 if (FirstParameter
== NULL
) {
2473 SHELL_FREE_NON_NULL(CleanOriginal
);
2474 return (EFI_OUT_OF_RESOURCES
);
2476 TempWalker
= CleanOriginal
;
2477 if (!EFI_ERROR(GetNextParameter(&TempWalker
, &FirstParameter
, StrSize(CleanOriginal
)))) {
2479 // Depending on the first parameter we change the behavior
2481 switch (Type
= GetOperationType(FirstParameter
)) {
2482 case File_Sys_Change
:
2483 Status
= ChangeMappedDrive (FirstParameter
);
2485 case Internal_Command
:
2486 case Script_File_Name
:
2487 case Efi_Application
:
2488 Status
= SetupAndRunCommandOrFile(Type
, CleanOriginal
, FirstParameter
, ShellInfoObject
.NewShellParametersProtocol
);
2492 // Whatever was typed, it was invalid.
2494 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_NOT_FOUND
), ShellInfoObject
.HiiHandle
, FirstParameter
);
2495 SetLastError(SHELL_NOT_FOUND
);
2499 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_NOT_FOUND
), ShellInfoObject
.HiiHandle
, FirstParameter
);
2500 SetLastError(SHELL_NOT_FOUND
);
2503 SHELL_FREE_NON_NULL(CleanOriginal
);
2504 SHELL_FREE_NON_NULL(FirstParameter
);
2509 STATIC CONST UINT16 InvalidChars
[] = {L
'*', L
'?', L
'<', L
'>', L
'\\', L
'/', L
'\"', 0x0001, 0x0002};
2511 Function determins if the CommandName COULD be a valid command. It does not determine whether
2512 this is a valid command. It only checks for invalid characters.
2514 @param[in] CommandName The name to check
2516 @retval TRUE CommandName could be a command name
2517 @retval FALSE CommandName could not be a valid command name
2522 IN CONST CHAR16
*CommandName
2526 if (CommandName
== NULL
) {
2531 ; Count
< sizeof(InvalidChars
) / sizeof(InvalidChars
[0])
2534 if (ScanMem16(CommandName
, StrSize(CommandName
), InvalidChars
[Count
]) != NULL
) {
2542 Function to process a NSH script file via SHELL_FILE_HANDLE.
2544 @param[in] Handle The handle to the already opened file.
2545 @param[in] Name The name of the script file.
2547 @retval EFI_SUCCESS the script completed sucessfully
2551 RunScriptFileHandle (
2552 IN SHELL_FILE_HANDLE Handle
,
2553 IN CONST CHAR16
*Name
2557 SCRIPT_FILE
*NewScriptFile
;
2559 CHAR16
*CommandLine
;
2560 CHAR16
*CommandLine2
;
2561 CHAR16
*CommandLine3
;
2562 SCRIPT_COMMAND_LIST
*LastCommand
;
2564 BOOLEAN PreScriptEchoState
;
2565 BOOLEAN PreCommandEchoState
;
2566 CONST CHAR16
*CurDir
;
2568 CHAR16 LeString
[50];
2570 ASSERT(!ShellCommandGetScriptExit());
2572 PreScriptEchoState
= ShellCommandGetEchoState();
2574 NewScriptFile
= (SCRIPT_FILE
*)AllocateZeroPool(sizeof(SCRIPT_FILE
));
2575 if (NewScriptFile
== NULL
) {
2576 return (EFI_OUT_OF_RESOURCES
);
2582 ASSERT(NewScriptFile
->ScriptName
== NULL
);
2583 NewScriptFile
->ScriptName
= StrnCatGrow(&NewScriptFile
->ScriptName
, NULL
, Name
, 0);
2584 if (NewScriptFile
->ScriptName
== NULL
) {
2585 DeleteScriptFileStruct(NewScriptFile
);
2586 return (EFI_OUT_OF_RESOURCES
);
2590 // Save the parameters (used to replace %0 to %9 later on)
2592 NewScriptFile
->Argc
= ShellInfoObject
.NewShellParametersProtocol
->Argc
;
2593 if (NewScriptFile
->Argc
!= 0) {
2594 NewScriptFile
->Argv
= (CHAR16
**)AllocateZeroPool(NewScriptFile
->Argc
* sizeof(CHAR16
*));
2595 if (NewScriptFile
->Argv
== NULL
) {
2596 DeleteScriptFileStruct(NewScriptFile
);
2597 return (EFI_OUT_OF_RESOURCES
);
2599 for (LoopVar
= 0 ; LoopVar
< 10 && LoopVar
< NewScriptFile
->Argc
; LoopVar
++) {
2600 ASSERT(NewScriptFile
->Argv
[LoopVar
] == NULL
);
2601 NewScriptFile
->Argv
[LoopVar
] = StrnCatGrow(&NewScriptFile
->Argv
[LoopVar
], NULL
, ShellInfoObject
.NewShellParametersProtocol
->Argv
[LoopVar
], 0);
2602 if (NewScriptFile
->Argv
[LoopVar
] == NULL
) {
2603 DeleteScriptFileStruct(NewScriptFile
);
2604 return (EFI_OUT_OF_RESOURCES
);
2608 NewScriptFile
->Argv
= NULL
;
2611 InitializeListHead(&NewScriptFile
->CommandList
);
2612 InitializeListHead(&NewScriptFile
->SubstList
);
2615 // Now build the list of all script commands.
2618 while(!ShellFileHandleEof(Handle
)) {
2619 CommandLine
= ShellFileHandleReturnLine(Handle
, &Ascii
);
2621 if (CommandLine
== NULL
|| StrLen(CommandLine
) == 0 || CommandLine
[0] == '#') {
2622 SHELL_FREE_NON_NULL(CommandLine
);
2625 NewScriptFile
->CurrentCommand
= AllocateZeroPool(sizeof(SCRIPT_COMMAND_LIST
));
2626 if (NewScriptFile
->CurrentCommand
== NULL
) {
2627 SHELL_FREE_NON_NULL(CommandLine
);
2628 DeleteScriptFileStruct(NewScriptFile
);
2629 return (EFI_OUT_OF_RESOURCES
);
2632 NewScriptFile
->CurrentCommand
->Cl
= CommandLine
;
2633 NewScriptFile
->CurrentCommand
->Data
= NULL
;
2634 NewScriptFile
->CurrentCommand
->Line
= LineCount
;
2636 InsertTailList(&NewScriptFile
->CommandList
, &NewScriptFile
->CurrentCommand
->Link
);
2640 // Add this as the topmost script file
2642 ShellCommandSetNewScript (NewScriptFile
);
2645 // Now enumerate through the commands and run each one.
2647 CommandLine
= AllocateZeroPool(PcdGet16(PcdShellPrintBufferSize
));
2648 if (CommandLine
== NULL
) {
2649 DeleteScriptFileStruct(NewScriptFile
);
2650 return (EFI_OUT_OF_RESOURCES
);
2652 CommandLine2
= AllocateZeroPool(PcdGet16(PcdShellPrintBufferSize
));
2653 if (CommandLine2
== NULL
) {
2654 FreePool(CommandLine
);
2655 DeleteScriptFileStruct(NewScriptFile
);
2656 return (EFI_OUT_OF_RESOURCES
);
2659 for ( NewScriptFile
->CurrentCommand
= (SCRIPT_COMMAND_LIST
*)GetFirstNode(&NewScriptFile
->CommandList
)
2660 ; !IsNull(&NewScriptFile
->CommandList
, &NewScriptFile
->CurrentCommand
->Link
)
2661 ; // conditional increment in the body of the loop
2663 ASSERT(CommandLine2
!= NULL
);
2664 StrnCpy(CommandLine2
, NewScriptFile
->CurrentCommand
->Cl
, PcdGet16(PcdShellPrintBufferSize
)/sizeof(CHAR16
)-1);
2667 // NULL out comments
2669 for (CommandLine3
= CommandLine2
; CommandLine3
!= NULL
&& *CommandLine3
!= CHAR_NULL
; CommandLine3
++) {
2670 if (*CommandLine3
== L
'^') {
2671 if ( *(CommandLine3
+1) == L
':') {
2672 CopyMem(CommandLine3
, CommandLine3
+1, StrSize(CommandLine3
) - sizeof(CommandLine3
[0]));
2673 } else if (*(CommandLine3
+1) == L
'#') {
2676 } else if (*CommandLine3
== L
'#') {
2677 *CommandLine3
= CHAR_NULL
;
2681 if (CommandLine2
!= NULL
&& StrLen(CommandLine2
) >= 1) {
2683 // Due to variability in starting the find and replace action we need to have both buffers the same.
2685 StrnCpy(CommandLine
, CommandLine2
, PcdGet16(PcdShellPrintBufferSize
)/sizeof(CHAR16
)-1);
2688 // Remove the %0 to %9 from the command line (if we have some arguments)
2690 if (NewScriptFile
->Argv
!= NULL
) {
2691 switch (NewScriptFile
->Argc
) {
2693 Status
= ShellCopySearchAndReplace(CommandLine2
, CommandLine
, PcdGet16 (PcdShellPrintBufferSize
), L
"%9", NewScriptFile
->Argv
[9], FALSE
, TRUE
);
2694 ASSERT_EFI_ERROR(Status
);
2696 Status
= ShellCopySearchAndReplace(CommandLine
, CommandLine2
, PcdGet16 (PcdShellPrintBufferSize
), L
"%8", NewScriptFile
->Argv
[8], FALSE
, TRUE
);
2697 ASSERT_EFI_ERROR(Status
);
2699 Status
= ShellCopySearchAndReplace(CommandLine2
, CommandLine
, PcdGet16 (PcdShellPrintBufferSize
), L
"%7", NewScriptFile
->Argv
[7], FALSE
, TRUE
);
2700 ASSERT_EFI_ERROR(Status
);
2702 Status
= ShellCopySearchAndReplace(CommandLine
, CommandLine2
, PcdGet16 (PcdShellPrintBufferSize
), L
"%6", NewScriptFile
->Argv
[6], FALSE
, TRUE
);
2703 ASSERT_EFI_ERROR(Status
);
2705 Status
= ShellCopySearchAndReplace(CommandLine2
, CommandLine
, PcdGet16 (PcdShellPrintBufferSize
), L
"%5", NewScriptFile
->Argv
[5], FALSE
, TRUE
);
2706 ASSERT_EFI_ERROR(Status
);
2708 Status
= ShellCopySearchAndReplace(CommandLine
, CommandLine2
, PcdGet16 (PcdShellPrintBufferSize
), L
"%4", NewScriptFile
->Argv
[4], FALSE
, TRUE
);
2709 ASSERT_EFI_ERROR(Status
);
2711 Status
= ShellCopySearchAndReplace(CommandLine2
, CommandLine
, PcdGet16 (PcdShellPrintBufferSize
), L
"%3", NewScriptFile
->Argv
[3], FALSE
, TRUE
);
2712 ASSERT_EFI_ERROR(Status
);
2714 Status
= ShellCopySearchAndReplace(CommandLine
, CommandLine2
, PcdGet16 (PcdShellPrintBufferSize
), L
"%2", NewScriptFile
->Argv
[2], FALSE
, TRUE
);
2715 ASSERT_EFI_ERROR(Status
);
2717 Status
= ShellCopySearchAndReplace(CommandLine2
, CommandLine
, PcdGet16 (PcdShellPrintBufferSize
), L
"%1", NewScriptFile
->Argv
[1], FALSE
, TRUE
);
2718 ASSERT_EFI_ERROR(Status
);
2720 Status
= ShellCopySearchAndReplace(CommandLine
, CommandLine2
, PcdGet16 (PcdShellPrintBufferSize
), L
"%0", NewScriptFile
->Argv
[0], FALSE
, TRUE
);
2721 ASSERT_EFI_ERROR(Status
);
2727 Status
= ShellCopySearchAndReplace(CommandLine2
, CommandLine
, PcdGet16 (PcdShellPrintBufferSize
), L
"%1", L
"\"\"", FALSE
, FALSE
);
2728 Status
= ShellCopySearchAndReplace(CommandLine
, CommandLine2
, PcdGet16 (PcdShellPrintBufferSize
), L
"%2", L
"\"\"", FALSE
, FALSE
);
2729 Status
= ShellCopySearchAndReplace(CommandLine2
, CommandLine
, PcdGet16 (PcdShellPrintBufferSize
), L
"%3", L
"\"\"", FALSE
, FALSE
);
2730 Status
= ShellCopySearchAndReplace(CommandLine
, CommandLine2
, PcdGet16 (PcdShellPrintBufferSize
), L
"%4", L
"\"\"", FALSE
, FALSE
);
2731 Status
= ShellCopySearchAndReplace(CommandLine2
, CommandLine
, PcdGet16 (PcdShellPrintBufferSize
), L
"%5", L
"\"\"", FALSE
, FALSE
);
2732 Status
= ShellCopySearchAndReplace(CommandLine
, CommandLine2
, PcdGet16 (PcdShellPrintBufferSize
), L
"%6", L
"\"\"", FALSE
, FALSE
);
2733 Status
= ShellCopySearchAndReplace(CommandLine2
, CommandLine
, PcdGet16 (PcdShellPrintBufferSize
), L
"%7", L
"\"\"", FALSE
, FALSE
);
2734 Status
= ShellCopySearchAndReplace(CommandLine
, CommandLine2
, PcdGet16 (PcdShellPrintBufferSize
), L
"%8", L
"\"\"", FALSE
, FALSE
);
2735 Status
= ShellCopySearchAndReplace(CommandLine2
, CommandLine
, PcdGet16 (PcdShellPrintBufferSize
), L
"%9", L
"\"\"", FALSE
, FALSE
);
2737 StrnCpy(CommandLine2
, CommandLine
, PcdGet16(PcdShellPrintBufferSize
)/sizeof(CHAR16
)-1);
2739 LastCommand
= NewScriptFile
->CurrentCommand
;
2741 for (CommandLine3
= CommandLine2
; CommandLine3
[0] == L
' ' ; CommandLine3
++);
2743 if (CommandLine3
!= NULL
&& CommandLine3
[0] == L
':' ) {
2745 // This line is a goto target / label
2748 if (CommandLine3
!= NULL
&& StrLen(CommandLine3
) > 0) {
2749 if (CommandLine3
[0] == L
'@') {
2751 // We need to save the current echo state
2752 // and disable echo for just this command.
2754 PreCommandEchoState
= ShellCommandGetEchoState();
2755 ShellCommandSetEchoState(FALSE
);
2756 Status
= RunCommand(CommandLine3
+1);
2759 // If command was "@echo -off" or "@echo -on" then don't restore echo state
2761 if (StrCmp (L
"@echo -off", CommandLine3
) != 0 &&
2762 StrCmp (L
"@echo -on", CommandLine3
) != 0) {
2764 // Now restore the pre-'@' echo state.
2766 ShellCommandSetEchoState(PreCommandEchoState
);
2769 if (ShellCommandGetEchoState()) {
2770 CurDir
= ShellInfoObject
.NewEfiShellProtocol
->GetEnv(L
"cwd");
2771 if (CurDir
!= NULL
&& StrLen(CurDir
) > 1) {
2772 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_CURDIR
), ShellInfoObject
.HiiHandle
, CurDir
);
2774 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_SHELL
), ShellInfoObject
.HiiHandle
);
2776 ShellPrintEx(-1, -1, L
"%s\r\n", CommandLine2
);
2778 Status
= RunCommand(CommandLine3
);
2782 if (ShellCommandGetScriptExit()) {
2784 // ShellCommandGetExitCode() always returns a UINT64
2786 UnicodeSPrint(LeString
, sizeof(LeString
), L
"0x%Lx", ShellCommandGetExitCode());
2787 DEBUG_CODE(InternalEfiShellSetEnv(L
"debuglasterror", LeString
, TRUE
););
2788 InternalEfiShellSetEnv(L
"lasterror", LeString
, TRUE
);
2790 ShellCommandRegisterExit(FALSE
, 0);
2791 Status
= EFI_SUCCESS
;
2794 if (ShellGetExecutionBreakFlag()) {
2797 if (EFI_ERROR(Status
)) {
2800 if (ShellCommandGetExit()) {
2805 // If that commend did not update the CurrentCommand then we need to advance it...
2807 if (LastCommand
== NewScriptFile
->CurrentCommand
) {
2808 NewScriptFile
->CurrentCommand
= (SCRIPT_COMMAND_LIST
*)GetNextNode(&NewScriptFile
->CommandList
, &NewScriptFile
->CurrentCommand
->Link
);
2809 if (!IsNull(&NewScriptFile
->CommandList
, &NewScriptFile
->CurrentCommand
->Link
)) {
2810 NewScriptFile
->CurrentCommand
->Reset
= TRUE
;
2814 NewScriptFile
->CurrentCommand
= (SCRIPT_COMMAND_LIST
*)GetNextNode(&NewScriptFile
->CommandList
, &NewScriptFile
->CurrentCommand
->Link
);
2815 if (!IsNull(&NewScriptFile
->CommandList
, &NewScriptFile
->CurrentCommand
->Link
)) {
2816 NewScriptFile
->CurrentCommand
->Reset
= TRUE
;
2822 FreePool(CommandLine
);
2823 FreePool(CommandLine2
);
2824 ShellCommandSetNewScript (NULL
);
2827 // Only if this was the last script reset the state.
2829 if (ShellCommandGetCurrentScriptFile()==NULL
) {
2830 ShellCommandSetEchoState(PreScriptEchoState
);
2832 return (EFI_SUCCESS
);
2836 Function to process a NSH script file.
2838 @param[in] ScriptPath Pointer to the script file name (including file system path).
2839 @param[in] Handle the handle of the script file already opened.
2840 @param[in] CmdLine the command line to run.
2841 @param[in] ParamProtocol the shell parameters protocol pointer
2843 @retval EFI_SUCCESS the script completed sucessfully
2848 IN CONST CHAR16
*ScriptPath
,
2849 IN SHELL_FILE_HANDLE Handle OPTIONAL
,
2850 IN CONST CHAR16
*CmdLine
,
2851 IN EFI_SHELL_PARAMETERS_PROTOCOL
*ParamProtocol
2855 SHELL_FILE_HANDLE FileHandle
;
2859 if (ShellIsFile(ScriptPath
) != EFI_SUCCESS
) {
2860 return (EFI_INVALID_PARAMETER
);
2864 // get the argc and argv updated for scripts
2866 Status
= UpdateArgcArgv(ParamProtocol
, CmdLine
, &Argv
, &Argc
);
2867 if (!EFI_ERROR(Status
)) {
2869 if (Handle
== NULL
) {
2873 Status
= ShellOpenFileByName(ScriptPath
, &FileHandle
, EFI_FILE_MODE_READ
, 0);
2874 if (!EFI_ERROR(Status
)) {
2878 Status
= RunScriptFileHandle(FileHandle
, ScriptPath
);
2881 // now close the file
2883 ShellCloseFile(&FileHandle
);
2886 Status
= RunScriptFileHandle(Handle
, ScriptPath
);
2891 // This is guarenteed to be called after UpdateArgcArgv no matter what else happened.
2892 // This is safe even if the update API failed. In this case, it may be a no-op.
2894 RestoreArgcArgv(ParamProtocol
, &Argv
, &Argc
);
2900 Return the pointer to the first occurance of any character from a list of characters.
2902 @param[in] String the string to parse
2903 @param[in] CharacterList the list of character to look for
2904 @param[in] EscapeCharacter An escape character to skip
2906 @return the location of the first character in the string
2907 @retval CHAR_NULL no instance of any character in CharacterList was found in String
2912 IN CONST CHAR16
*String
,
2913 IN CONST CHAR16
*CharacterList
,
2914 IN CONST CHAR16 EscapeCharacter
2920 for (WalkStr
= 0; WalkStr
< StrLen(String
); WalkStr
++) {
2921 if (String
[WalkStr
] == EscapeCharacter
) {
2925 for (WalkChar
= 0; WalkChar
< StrLen(CharacterList
); WalkChar
++) {
2926 if (String
[WalkStr
] == CharacterList
[WalkChar
]) {
2927 return (&String
[WalkStr
]);
2931 return (String
+ StrLen(String
));