2 This is THE shell (application)
4 Copyright (c) 2009 - 2014, 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 Find a command line contains a split operation
148 @param[in] CmdLine The command line to parse.
150 @retval A pointer to the | character in CmdLine or NULL if not present.
155 IN CONST CHAR16
*CmdLine
158 CONST CHAR16
*TempSpot
;
160 if (StrStr(CmdLine
, L
"|") != NULL
) {
161 for (TempSpot
= CmdLine
; TempSpot
!= NULL
&& *TempSpot
!= CHAR_NULL
; TempSpot
++) {
162 if (*TempSpot
== L
'^' && *(TempSpot
+1) == L
'|') {
164 } else if (*TempSpot
== L
'|') {
173 Determine if a command line contains a split operation
175 @param[in] CmdLine The command line to parse.
177 @retval TRUE CmdLine has a valid split.
178 @retval FALSE CmdLine does not have a valid split.
183 IN CONST CHAR16
*CmdLine
186 CONST CHAR16
*TempSpot
;
187 CONST CHAR16
*FirstQuote
;
188 CONST CHAR16
*SecondQuote
;
190 FirstQuote
= FindNextInstance (CmdLine
, L
"\"", TRUE
);
192 TempSpot
= FindSplit(CmdLine
);
194 if (FirstQuote
== NULL
||
196 TempSpot
== CHAR_NULL
||
197 FirstQuote
> TempSpot
199 return (BOOLEAN
) ((TempSpot
!= NULL
) && (*TempSpot
!= CHAR_NULL
));
202 while ((TempSpot
!= NULL
) && (*TempSpot
!= CHAR_NULL
)) {
203 if (FirstQuote
== NULL
|| FirstQuote
> TempSpot
) {
206 SecondQuote
= FindNextInstance (FirstQuote
+ 1, L
"\"", TRUE
);
207 if (SecondQuote
== NULL
) {
210 if (SecondQuote
< TempSpot
) {
211 FirstQuote
= FindNextInstance (SecondQuote
+ 1, L
"\"", TRUE
);
214 FirstQuote
= FindNextInstance (SecondQuote
+ 1, L
"\"", TRUE
);
215 TempSpot
= FindSplit(TempSpot
+ 1);
220 return (BOOLEAN
) ((TempSpot
!= NULL
) && (*TempSpot
!= CHAR_NULL
));
224 Function to start monitoring for CTRL-S using SimpleTextInputEx. This
225 feature's enabled state was not known when the shell initially launched.
227 @retval EFI_SUCCESS The feature is enabled.
228 @retval EFI_OUT_OF_RESOURCES There is not enough mnemory available.
232 InternalEfiShellStartCtrlSMonitor(
236 EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL
*SimpleEx
;
237 EFI_KEY_DATA KeyData
;
240 Status
= gBS
->OpenProtocol(
241 gST
->ConsoleInHandle
,
242 &gEfiSimpleTextInputExProtocolGuid
,
246 EFI_OPEN_PROTOCOL_GET_PROTOCOL
);
247 if (EFI_ERROR(Status
)) {
252 STRING_TOKEN (STR_SHELL_NO_IN_EX
),
253 ShellInfoObject
.HiiHandle
);
254 return (EFI_SUCCESS
);
257 KeyData
.KeyState
.KeyToggleState
= 0;
258 KeyData
.Key
.ScanCode
= 0;
259 KeyData
.KeyState
.KeyShiftState
= EFI_SHIFT_STATE_VALID
|EFI_LEFT_CONTROL_PRESSED
;
260 KeyData
.Key
.UnicodeChar
= L
's';
262 Status
= SimpleEx
->RegisterKeyNotify(
265 NotificationFunction
,
266 &ShellInfoObject
.CtrlSNotifyHandle1
);
268 KeyData
.KeyState
.KeyShiftState
= EFI_SHIFT_STATE_VALID
|EFI_RIGHT_CONTROL_PRESSED
;
269 if (!EFI_ERROR(Status
)) {
270 Status
= SimpleEx
->RegisterKeyNotify(
273 NotificationFunction
,
274 &ShellInfoObject
.CtrlSNotifyHandle2
);
276 KeyData
.KeyState
.KeyShiftState
= EFI_SHIFT_STATE_VALID
|EFI_LEFT_CONTROL_PRESSED
;
277 KeyData
.Key
.UnicodeChar
= 19;
279 if (!EFI_ERROR(Status
)) {
280 Status
= SimpleEx
->RegisterKeyNotify(
283 NotificationFunction
,
284 &ShellInfoObject
.CtrlSNotifyHandle3
);
286 KeyData
.KeyState
.KeyShiftState
= EFI_SHIFT_STATE_VALID
|EFI_RIGHT_CONTROL_PRESSED
;
287 if (!EFI_ERROR(Status
)) {
288 Status
= SimpleEx
->RegisterKeyNotify(
291 NotificationFunction
,
292 &ShellInfoObject
.CtrlSNotifyHandle4
);
300 The entry point for the application.
302 @param[in] ImageHandle The firmware allocated handle for the EFI image.
303 @param[in] SystemTable A pointer to the EFI System Table.
305 @retval EFI_SUCCESS The entry point is executed successfully.
306 @retval other Some error occurs when executing this entry point.
312 IN EFI_HANDLE ImageHandle
,
313 IN EFI_SYSTEM_TABLE
*SystemTable
319 EFI_HANDLE ConInHandle
;
320 EFI_SIMPLE_TEXT_INPUT_PROTOCOL
*OldConIn
;
322 if (PcdGet8(PcdShellSupportLevel
) > 3) {
323 return (EFI_UNSUPPORTED
);
329 Status
= gST
->ConOut
->ClearScreen(gST
->ConOut
);
330 if (EFI_ERROR(Status
)) {
335 // Populate the global structure from PCDs
337 ShellInfoObject
.ImageDevPath
= NULL
;
338 ShellInfoObject
.FileDevPath
= NULL
;
339 ShellInfoObject
.PageBreakEnabled
= PcdGetBool(PcdShellPageBreakDefault
);
340 ShellInfoObject
.ViewingSettings
.InsertMode
= PcdGetBool(PcdShellInsertModeDefault
);
341 ShellInfoObject
.LogScreenCount
= PcdGet8 (PcdShellScreenLogCount
);
344 // verify we dont allow for spec violation
346 ASSERT(ShellInfoObject
.LogScreenCount
>= 3);
349 // Initialize the LIST ENTRY objects...
351 InitializeListHead(&ShellInfoObject
.BufferToFreeList
.Link
);
352 InitializeListHead(&ShellInfoObject
.ViewingSettings
.CommandHistory
.Link
);
353 InitializeListHead(&ShellInfoObject
.SplitList
.Link
);
356 // Check PCDs for optional features that are not implemented yet.
358 if ( PcdGetBool(PcdShellSupportOldProtocols
)
359 || !FeaturePcdGet(PcdShellRequireHiiPlatform
)
360 || FeaturePcdGet(PcdShellSupportFrameworkHii
)
362 return (EFI_UNSUPPORTED
);
366 // turn off the watchdog timer
368 gBS
->SetWatchdogTimer (0, 0, 0, NULL
);
371 // install our console logger. This will keep a log of the output for back-browsing
373 Status
= ConsoleLoggerInstall(ShellInfoObject
.LogScreenCount
, &ShellInfoObject
.ConsoleInfo
);
374 if (!EFI_ERROR(Status
)) {
376 // Enable the cursor to be visible
378 gST
->ConOut
->EnableCursor (gST
->ConOut
, TRUE
);
381 // If supporting EFI 1.1 we need to install HII protocol
382 // only do this if PcdShellRequireHiiPlatform == FALSE
384 // remove EFI_UNSUPPORTED check above when complete.
385 ///@todo add support for Framework HII
388 // install our (solitary) HII package
390 ShellInfoObject
.HiiHandle
= HiiAddPackages (&gEfiCallerIdGuid
, gImageHandle
, ShellStrings
, NULL
);
391 if (ShellInfoObject
.HiiHandle
== NULL
) {
392 if (PcdGetBool(PcdShellSupportFrameworkHii
)) {
393 ///@todo Add our package into Framework HII
395 if (ShellInfoObject
.HiiHandle
== NULL
) {
396 Status
= EFI_NOT_STARTED
;
402 // create and install the EfiShellParametersProtocol
404 Status
= CreatePopulateInstallShellParametersProtocol(&ShellInfoObject
.NewShellParametersProtocol
, &ShellInfoObject
.RootShellInstance
);
405 ASSERT_EFI_ERROR(Status
);
406 ASSERT(ShellInfoObject
.NewShellParametersProtocol
!= NULL
);
409 // create and install the EfiShellProtocol
411 Status
= CreatePopulateInstallShellProtocol(&ShellInfoObject
.NewEfiShellProtocol
);
412 ASSERT_EFI_ERROR(Status
);
413 ASSERT(ShellInfoObject
.NewEfiShellProtocol
!= NULL
);
416 // Now initialize the shell library (it requires Shell Parameters protocol)
418 Status
= ShellInitialize();
419 ASSERT_EFI_ERROR(Status
);
421 Status
= CommandInit();
422 ASSERT_EFI_ERROR(Status
);
425 // Check the command line
427 Status
= ProcessCommandLine ();
428 if (EFI_ERROR (Status
)) {
433 // If shell support level is >= 1 create the mappings and paths
435 if (PcdGet8(PcdShellSupportLevel
) >= 1) {
436 Status
= ShellCommandCreateInitialMappingsAndPaths();
440 // save the device path for the loaded image and the device path for the filepath (under loaded image)
441 // These are where to look for the startup.nsh file
443 Status
= GetDevicePathsForImageAndFile(&ShellInfoObject
.ImageDevPath
, &ShellInfoObject
.FileDevPath
);
444 ASSERT_EFI_ERROR(Status
);
447 // Display the version
449 if (!ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoVersion
) {
452 gST
->ConOut
->Mode
->CursorRow
,
454 STRING_TOKEN (STR_VER_OUTPUT_MAIN_SHELL
),
455 ShellInfoObject
.HiiHandle
,
456 SupportLevel
[PcdGet8(PcdShellSupportLevel
)],
457 gEfiShellProtocol
->MajorVersion
,
458 gEfiShellProtocol
->MinorVersion
465 STRING_TOKEN (STR_VER_OUTPUT_MAIN_SUPPLIER
),
466 ShellInfoObject
.HiiHandle
,
467 (CHAR16
*) PcdGetPtr (PcdShellSupplier
)
474 STRING_TOKEN (STR_VER_OUTPUT_MAIN_UEFI
),
475 ShellInfoObject
.HiiHandle
,
476 (gST
->Hdr
.Revision
&0xffff0000)>>16,
477 (gST
->Hdr
.Revision
&0x0000ffff),
479 gST
->FirmwareRevision
484 // Display the mapping
486 if (PcdGet8(PcdShellSupportLevel
) >= 2 && !ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoMap
) {
487 Status
= RunCommand(L
"map");
488 ASSERT_EFI_ERROR(Status
);
492 // init all the built in alias'
494 Status
= SetBuiltInAlias();
495 ASSERT_EFI_ERROR(Status
);
498 // Initialize environment variables
500 if (ShellCommandGetProfileList() != NULL
) {
501 Status
= InternalEfiShellSetEnv(L
"profiles", ShellCommandGetProfileList(), TRUE
);
502 ASSERT_EFI_ERROR(Status
);
506 TempString
= AllocateZeroPool(Size
);
508 UnicodeSPrint(TempString
, Size
, L
"%d", PcdGet8(PcdShellSupportLevel
));
509 Status
= InternalEfiShellSetEnv(L
"uefishellsupport", TempString
, TRUE
);
510 ASSERT_EFI_ERROR(Status
);
512 UnicodeSPrint(TempString
, Size
, L
"%d.%d", ShellInfoObject
.NewEfiShellProtocol
->MajorVersion
, ShellInfoObject
.NewEfiShellProtocol
->MinorVersion
);
513 Status
= InternalEfiShellSetEnv(L
"uefishellversion", TempString
, TRUE
);
514 ASSERT_EFI_ERROR(Status
);
516 UnicodeSPrint(TempString
, Size
, L
"%d.%d", (gST
->Hdr
.Revision
& 0xFFFF0000) >> 16, gST
->Hdr
.Revision
& 0x0000FFFF);
517 Status
= InternalEfiShellSetEnv(L
"uefiversion", TempString
, TRUE
);
518 ASSERT_EFI_ERROR(Status
);
520 FreePool(TempString
);
522 if (!EFI_ERROR(Status
)) {
523 if (!ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoInterrupt
) {
525 // Set up the event for CTRL-C monitoring...
527 Status
= InernalEfiShellStartMonitor();
530 if (!EFI_ERROR(Status
) && !ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoConsoleIn
) {
532 // Set up the event for CTRL-S monitoring...
534 Status
= InternalEfiShellStartCtrlSMonitor();
537 if (!EFI_ERROR(Status
) && ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoConsoleIn
) {
539 // close off the gST->ConIn
541 OldConIn
= gST
->ConIn
;
542 ConInHandle
= gST
->ConsoleInHandle
;
543 gST
->ConIn
= CreateSimpleTextInOnFile((SHELL_FILE_HANDLE
)&FileInterfaceNulFile
, &gST
->ConsoleInHandle
);
549 if (!EFI_ERROR(Status
) && PcdGet8(PcdShellSupportLevel
) >= 1) {
551 // process the startup script or launch the called app.
553 Status
= DoStartupScript(ShellInfoObject
.ImageDevPath
, ShellInfoObject
.FileDevPath
);
556 if (!ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.Exit
&& !ShellCommandGetExit() && (PcdGet8(PcdShellSupportLevel
) >= 3 || PcdGetBool(PcdShellForceConsole
)) && !EFI_ERROR(Status
) && !ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoConsoleIn
) {
558 // begin the UI waiting loop
562 // clean out all the memory allocated for CONST <something> * return values
563 // between each shell prompt presentation
565 if (!IsListEmpty(&ShellInfoObject
.BufferToFreeList
.Link
)){
566 FreeBufferList(&ShellInfoObject
.BufferToFreeList
);
570 // Reset page break back to default.
572 ShellInfoObject
.PageBreakEnabled
= PcdGetBool(PcdShellPageBreakDefault
);
573 ASSERT (ShellInfoObject
.ConsoleInfo
!= NULL
);
574 ShellInfoObject
.ConsoleInfo
->Enabled
= TRUE
;
575 ShellInfoObject
.ConsoleInfo
->RowCounter
= 0;
578 // Reset the CTRL-C event (yes we ignore the return values)
580 Status
= gBS
->CheckEvent (ShellInfoObject
.NewEfiShellProtocol
->ExecutionBreak
);
585 Status
= DoShellPrompt();
586 } while (!ShellCommandGetExit());
588 if (OldConIn
!= NULL
&& ConInHandle
!= NULL
) {
589 CloseSimpleTextInOnFile (gST
->ConIn
);
590 gST
->ConIn
= OldConIn
;
591 gST
->ConsoleInHandle
= ConInHandle
;
598 // uninstall protocols / free memory / etc...
600 if (ShellInfoObject
.UserBreakTimer
!= NULL
) {
601 gBS
->CloseEvent(ShellInfoObject
.UserBreakTimer
);
602 DEBUG_CODE(ShellInfoObject
.UserBreakTimer
= NULL
;);
604 if (ShellInfoObject
.ImageDevPath
!= NULL
) {
605 FreePool(ShellInfoObject
.ImageDevPath
);
606 DEBUG_CODE(ShellInfoObject
.ImageDevPath
= NULL
;);
608 if (ShellInfoObject
.FileDevPath
!= NULL
) {
609 FreePool(ShellInfoObject
.FileDevPath
);
610 DEBUG_CODE(ShellInfoObject
.FileDevPath
= NULL
;);
612 if (ShellInfoObject
.NewShellParametersProtocol
!= NULL
) {
613 CleanUpShellParametersProtocol(ShellInfoObject
.NewShellParametersProtocol
);
614 DEBUG_CODE(ShellInfoObject
.NewShellParametersProtocol
= NULL
;);
616 if (ShellInfoObject
.NewEfiShellProtocol
!= NULL
){
617 if (ShellInfoObject
.NewEfiShellProtocol
->IsRootShell()){
618 InternalEfiShellSetEnv(L
"cwd", NULL
, TRUE
);
620 CleanUpShellProtocol(ShellInfoObject
.NewEfiShellProtocol
);
621 DEBUG_CODE(ShellInfoObject
.NewEfiShellProtocol
= NULL
;);
624 if (!IsListEmpty(&ShellInfoObject
.BufferToFreeList
.Link
)){
625 FreeBufferList(&ShellInfoObject
.BufferToFreeList
);
628 if (!IsListEmpty(&ShellInfoObject
.SplitList
.Link
)){
629 ASSERT(FALSE
); ///@todo finish this de-allocation.
632 if (ShellInfoObject
.ShellInitSettings
.FileName
!= NULL
) {
633 FreePool(ShellInfoObject
.ShellInitSettings
.FileName
);
634 DEBUG_CODE(ShellInfoObject
.ShellInitSettings
.FileName
= NULL
;);
637 if (ShellInfoObject
.ShellInitSettings
.FileOptions
!= NULL
) {
638 FreePool(ShellInfoObject
.ShellInitSettings
.FileOptions
);
639 DEBUG_CODE(ShellInfoObject
.ShellInitSettings
.FileOptions
= NULL
;);
642 if (ShellInfoObject
.HiiHandle
!= NULL
) {
643 HiiRemovePackages(ShellInfoObject
.HiiHandle
);
644 DEBUG_CODE(ShellInfoObject
.HiiHandle
= NULL
;);
647 if (!IsListEmpty(&ShellInfoObject
.ViewingSettings
.CommandHistory
.Link
)){
648 FreeBufferList(&ShellInfoObject
.ViewingSettings
.CommandHistory
);
651 ASSERT(ShellInfoObject
.ConsoleInfo
!= NULL
);
652 if (ShellInfoObject
.ConsoleInfo
!= NULL
) {
653 ConsoleLoggerUninstall(ShellInfoObject
.ConsoleInfo
);
654 FreePool(ShellInfoObject
.ConsoleInfo
);
655 DEBUG_CODE(ShellInfoObject
.ConsoleInfo
= NULL
;);
658 if (ShellCommandGetExit()) {
659 return ((EFI_STATUS
)ShellCommandGetExitCode());
665 Sets all the alias' that were registered with the ShellCommandLib library.
667 @retval EFI_SUCCESS all init commands were run sucessfully.
675 CONST ALIAS_LIST
*List
;
679 // Get all the commands we want to run
681 List
= ShellCommandGetInitAliasList();
684 // for each command in the List
686 for ( Node
= (ALIAS_LIST
*)GetFirstNode(&List
->Link
)
687 ; !IsNull (&List
->Link
, &Node
->Link
)
688 ; Node
= (ALIAS_LIST
*)GetNextNode(&List
->Link
, &Node
->Link
)
691 // install the alias'
693 Status
= InternalSetAlias(Node
->CommandString
, Node
->Alias
, TRUE
);
694 ASSERT_EFI_ERROR(Status
);
696 return (EFI_SUCCESS
);
700 Internal function to determine if 2 command names are really the same.
702 @param[in] Command1 The pointer to the first command name.
703 @param[in] Command2 The pointer to the second command name.
705 @retval TRUE The 2 command names are the same.
706 @retval FALSE The 2 command names are not the same.
711 IN CONST CHAR16
*Command1
,
712 IN CONST CHAR16
*Command2
715 if (StringNoCaseCompare(&Command1
, &Command2
) == 0) {
722 Internal function to determine if a command is a script only command.
724 @param[in] CommandName The pointer to the command name.
726 @retval TRUE The command is a script only command.
727 @retval FALSE The command is not a script only command.
732 IN CONST CHAR16
*CommandName
735 if (IsCommand(CommandName
, L
"for")
736 ||IsCommand(CommandName
, L
"endfor")
737 ||IsCommand(CommandName
, L
"if")
738 ||IsCommand(CommandName
, L
"else")
739 ||IsCommand(CommandName
, L
"endif")
740 ||IsCommand(CommandName
, L
"goto")) {
747 This function will populate the 2 device path protocol parameters based on the
748 global gImageHandle. The DevPath will point to the device path for the handle that has
749 loaded image protocol installed on it. The FilePath will point to the device path
750 for the file that was loaded.
752 @param[in, out] DevPath On a sucessful return the device path to the loaded image.
753 @param[in, out] FilePath On a sucessful return the device path to the file.
755 @retval EFI_SUCCESS The 2 device paths were sucessfully returned.
756 @retval other A error from gBS->HandleProtocol.
762 GetDevicePathsForImageAndFile (
763 IN OUT EFI_DEVICE_PATH_PROTOCOL
**DevPath
,
764 IN OUT EFI_DEVICE_PATH_PROTOCOL
**FilePath
768 EFI_LOADED_IMAGE_PROTOCOL
*LoadedImage
;
769 EFI_DEVICE_PATH_PROTOCOL
*ImageDevicePath
;
771 ASSERT(DevPath
!= NULL
);
772 ASSERT(FilePath
!= NULL
);
774 Status
= gBS
->OpenProtocol (
776 &gEfiLoadedImageProtocolGuid
,
777 (VOID
**)&LoadedImage
,
780 EFI_OPEN_PROTOCOL_GET_PROTOCOL
782 if (!EFI_ERROR (Status
)) {
783 Status
= gBS
->OpenProtocol (
784 LoadedImage
->DeviceHandle
,
785 &gEfiDevicePathProtocolGuid
,
786 (VOID
**)&ImageDevicePath
,
789 EFI_OPEN_PROTOCOL_GET_PROTOCOL
791 if (!EFI_ERROR (Status
)) {
792 *DevPath
= DuplicateDevicePath (ImageDevicePath
);
793 *FilePath
= DuplicateDevicePath (LoadedImage
->FilePath
);
795 LoadedImage
->DeviceHandle
,
796 &gEfiDevicePathProtocolGuid
,
802 &gEfiLoadedImageProtocolGuid
,
810 Process all Uefi Shell 2.0 command line options.
812 see Uefi Shell 2.0 section 3.2 for full details.
814 the command line must resemble the following:
816 shell.efi [ShellOpt-options] [options] [file-name [file-name-options]]
818 ShellOpt-options Options which control the initialization behavior of the shell.
819 These options are read from the EFI global variable "ShellOpt"
820 and are processed before options or file-name.
822 options Options which control the initialization behavior of the shell.
824 file-name The name of a UEFI shell application or script to be executed
825 after initialization is complete. By default, if file-name is
826 specified, then -nostartup is implied. Scripts are not supported
829 file-name-options The command-line options that are passed to file-name when it
832 This will initialize the ShellInfoObject.ShellInitSettings global variable.
834 @retval EFI_SUCCESS The variable is initialized.
845 CHAR16
*DelayValueStr
;
848 EFI_UNICODE_COLLATION_PROTOCOL
*UnicodeCollation
;
850 // `file-name-options` will contain arguments to `file-name` that we don't
851 // know about. This would cause ShellCommandLineParse to error, so we parse
852 // arguments manually, ignoring those after the first thing that doesn't look
853 // like a shell option (which is assumed to be `file-name`).
855 Status
= gBS
->LocateProtocol (
856 &gEfiUnicodeCollationProtocolGuid
,
858 (VOID
**) &UnicodeCollation
860 if (EFI_ERROR (Status
)) {
864 // Set default options
865 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.Startup
= FALSE
;
866 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoStartup
= FALSE
;
867 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoConsoleOut
= FALSE
;
868 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoConsoleIn
= FALSE
;
869 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoInterrupt
= FALSE
;
870 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoMap
= FALSE
;
871 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoVersion
= FALSE
;
872 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.Delay
= FALSE
;
873 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.Exit
= FALSE
;
874 ShellInfoObject
.ShellInitSettings
.Delay
= 5;
877 // Start LoopVar at 0 to parse only optional arguments at Argv[0]
878 // and parse other parameters from Argv[1]. This is for use case that
879 // UEFI Shell boot option is created, and OptionalData is provided
880 // that starts with shell command-line options.
882 for (LoopVar
= 0 ; LoopVar
< gEfiShellParametersProtocol
->Argc
; LoopVar
++) {
883 CurrentArg
= gEfiShellParametersProtocol
->Argv
[LoopVar
];
884 if (UnicodeCollation
->StriColl (
889 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.Startup
= TRUE
;
891 else if (UnicodeCollation
->StriColl (
896 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoStartup
= TRUE
;
898 else if (UnicodeCollation
->StriColl (
903 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoConsoleOut
= TRUE
;
905 else if (UnicodeCollation
->StriColl (
910 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoConsoleIn
= TRUE
;
912 else if (UnicodeCollation
->StriColl (
917 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoInterrupt
= TRUE
;
919 else if (UnicodeCollation
->StriColl (
924 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoMap
= TRUE
;
926 else if (UnicodeCollation
->StriColl (
931 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoVersion
= TRUE
;
933 else if (UnicodeCollation
->StriColl (
938 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.Delay
= TRUE
;
939 // Check for optional delay value following "-delay"
940 DelayValueStr
= gEfiShellParametersProtocol
->Argv
[LoopVar
+ 1];
941 if (DelayValueStr
!= NULL
){
942 if (*DelayValueStr
== L
':') {
945 if (!EFI_ERROR(ShellConvertStringToUint64 (
951 ShellInfoObject
.ShellInitSettings
.Delay
= (UINTN
)DelayValue
;
955 } else if (UnicodeCollation
->StriColl (
960 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.Exit
= TRUE
;
961 } else if (StrnCmp (L
"-", CurrentArg
, 1) == 0) {
962 // Unrecognised option
963 ShellPrintHiiEx(-1, -1, NULL
,
964 STRING_TOKEN (STR_GEN_PROBLEM
),
965 ShellInfoObject
.HiiHandle
,
968 return EFI_INVALID_PARAMETER
;
971 // First argument should be Shell.efi image name
977 ShellInfoObject
.ShellInitSettings
.FileName
= AllocateCopyPool(StrSize(CurrentArg
), CurrentArg
);
978 if (ShellInfoObject
.ShellInitSettings
.FileName
== NULL
) {
979 return (EFI_OUT_OF_RESOURCES
);
982 // We found `file-name`.
984 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoStartup
= 1;
987 // Add `file-name-options`
988 for (Size
= 0 ; LoopVar
< gEfiShellParametersProtocol
->Argc
; LoopVar
++) {
989 ASSERT((ShellInfoObject
.ShellInitSettings
.FileOptions
== NULL
&& Size
== 0) || (ShellInfoObject
.ShellInitSettings
.FileOptions
!= NULL
));
990 StrnCatGrow(&ShellInfoObject
.ShellInitSettings
.FileOptions
,
994 if (ShellInfoObject
.ShellInitSettings
.FileOptions
== NULL
) {
995 SHELL_FREE_NON_NULL(ShellInfoObject
.ShellInitSettings
.FileName
);
996 return (EFI_OUT_OF_RESOURCES
);
998 StrnCatGrow(&ShellInfoObject
.ShellInitSettings
.FileOptions
,
1000 gEfiShellParametersProtocol
->Argv
[LoopVar
],
1002 if (ShellInfoObject
.ShellInitSettings
.FileOptions
== NULL
) {
1003 SHELL_FREE_NON_NULL(ShellInfoObject
.ShellInitSettings
.FileName
);
1004 return (EFI_OUT_OF_RESOURCES
);
1010 // "-nointerrupt" overrides "-delay"
1011 if (ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoInterrupt
) {
1012 ShellInfoObject
.ShellInitSettings
.Delay
= 0;
1019 Handles all interaction with the default startup script.
1021 this will check that the correct command line parameters were passed, handle the delay, and then start running the script.
1023 @param ImagePath the path to the image for shell. first place to look for the startup script
1024 @param FilePath the path to the file for shell. second place to look for the startup script.
1026 @retval EFI_SUCCESS the variable is initialized.
1031 IN EFI_DEVICE_PATH_PROTOCOL
*ImagePath
,
1032 IN EFI_DEVICE_PATH_PROTOCOL
*FilePath
1038 SHELL_FILE_HANDLE FileHandle
;
1039 EFI_DEVICE_PATH_PROTOCOL
*NewPath
;
1040 EFI_DEVICE_PATH_PROTOCOL
*NamePath
;
1041 CHAR16
*FileStringPath
;
1044 CONST CHAR16
*MapName
;
1046 Key
.UnicodeChar
= CHAR_NULL
;
1050 if (!ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.Startup
&& ShellInfoObject
.ShellInitSettings
.FileName
!= NULL
) {
1052 // launch something else instead
1054 NewSize
= StrSize(ShellInfoObject
.ShellInitSettings
.FileName
);
1055 if (ShellInfoObject
.ShellInitSettings
.FileOptions
!= NULL
) {
1056 NewSize
+= StrSize(ShellInfoObject
.ShellInitSettings
.FileOptions
) + sizeof(CHAR16
);
1058 FileStringPath
= AllocateZeroPool(NewSize
);
1059 if (FileStringPath
== NULL
) {
1060 return (EFI_OUT_OF_RESOURCES
);
1062 StrnCpy(FileStringPath
, ShellInfoObject
.ShellInitSettings
.FileName
, NewSize
/sizeof(CHAR16
) -1);
1063 if (ShellInfoObject
.ShellInitSettings
.FileOptions
!= NULL
) {
1064 StrnCat(FileStringPath
, L
" ", NewSize
/sizeof(CHAR16
) - StrLen(FileStringPath
) -1);
1065 StrnCat(FileStringPath
, ShellInfoObject
.ShellInitSettings
.FileOptions
, NewSize
/sizeof(CHAR16
) - StrLen(FileStringPath
) -1);
1067 Status
= RunCommand(FileStringPath
);
1068 FreePool(FileStringPath
);
1074 // for shell level 0 we do no scripts
1075 // Without the Startup bit overriding we allow for nostartup to prevent scripts
1077 if ( (PcdGet8(PcdShellSupportLevel
) < 1)
1078 || (ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoStartup
&& !ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.Startup
)
1080 return (EFI_SUCCESS
);
1083 gST
->ConOut
->EnableCursor(gST
->ConOut
, FALSE
);
1085 // print out our warning and see if they press a key
1087 for ( Status
= EFI_UNSUPPORTED
, Delay
= ShellInfoObject
.ShellInitSettings
.Delay
1088 ; Delay
!= 0 && EFI_ERROR(Status
)
1091 ShellPrintHiiEx(0, gST
->ConOut
->Mode
->CursorRow
, NULL
, STRING_TOKEN (STR_SHELL_STARTUP_QUESTION
), ShellInfoObject
.HiiHandle
, Delay
);
1092 gBS
->Stall (1000000);
1093 if (!ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoConsoleIn
) {
1094 Status
= gST
->ConIn
->ReadKeyStroke (gST
->ConIn
, &Key
);
1097 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_CRLF
), ShellInfoObject
.HiiHandle
);
1098 gST
->ConOut
->EnableCursor(gST
->ConOut
, TRUE
);
1103 if (Status
== EFI_SUCCESS
&& Key
.UnicodeChar
== 0 && Key
.ScanCode
== SCAN_ESC
) {
1104 return (EFI_SUCCESS
);
1108 // Try the first location (must be file system)
1110 MapName
= ShellInfoObject
.NewEfiShellProtocol
->GetMapFromDevicePath(&ImagePath
);
1111 if (MapName
!= NULL
) {
1112 FileStringPath
= NULL
;
1114 FileStringPath
= StrnCatGrow(&FileStringPath
, &NewSize
, MapName
, 0);
1115 if (FileStringPath
== NULL
) {
1116 Status
= EFI_OUT_OF_RESOURCES
;
1118 TempSpot
= StrStr(FileStringPath
, L
";");
1119 if (TempSpot
!= NULL
) {
1120 *TempSpot
= CHAR_NULL
;
1122 FileStringPath
= StrnCatGrow(&FileStringPath
, &NewSize
, ((FILEPATH_DEVICE_PATH
*)FilePath
)->PathName
, 0);
1123 PathRemoveLastItem(FileStringPath
);
1124 FileStringPath
= StrnCatGrow(&FileStringPath
, &NewSize
, mStartupScript
, 0);
1125 Status
= ShellInfoObject
.NewEfiShellProtocol
->OpenFileByName(FileStringPath
, &FileHandle
, EFI_FILE_MODE_READ
);
1126 FreePool(FileStringPath
);
1129 if (EFI_ERROR(Status
)) {
1130 NamePath
= FileDevicePath (NULL
, mStartupScript
);
1131 NewPath
= AppendDevicePathNode (ImagePath
, NamePath
);
1137 Status
= InternalOpenFileDevicePath(NewPath
, &FileHandle
, EFI_FILE_MODE_READ
, 0);
1141 // If we got a file, run it
1143 if (!EFI_ERROR(Status
) && FileHandle
!= NULL
) {
1144 Status
= RunScriptFile (mStartupScript
, FileHandle
, L
"", ShellInfoObject
.NewShellParametersProtocol
);
1145 ShellInfoObject
.NewEfiShellProtocol
->CloseFile(FileHandle
);
1147 FileStringPath
= ShellFindFilePath(mStartupScript
);
1148 if (FileStringPath
== NULL
) {
1150 // we return success since we dont need to have a startup script
1152 Status
= EFI_SUCCESS
;
1153 ASSERT(FileHandle
== NULL
);
1155 Status
= RunScriptFile(FileStringPath
, NULL
, L
"", ShellInfoObject
.NewShellParametersProtocol
);
1156 FreePool(FileStringPath
);
1165 Function to perform the shell prompt looping. It will do a single prompt,
1166 dispatch the result, and then return. It is expected that the caller will
1167 call this function in a loop many times.
1170 @retval RETURN_ABORTED
1181 CONST CHAR16
*CurDir
;
1188 // Get screen setting to decide size of the command line buffer
1190 gST
->ConOut
->QueryMode (gST
->ConOut
, gST
->ConOut
->Mode
->Mode
, &Column
, &Row
);
1191 BufferSize
= Column
* Row
* sizeof (CHAR16
);
1192 CmdLine
= AllocateZeroPool (BufferSize
);
1193 if (CmdLine
== NULL
) {
1194 return EFI_OUT_OF_RESOURCES
;
1197 CurDir
= ShellInfoObject
.NewEfiShellProtocol
->GetEnv(L
"cwd");
1202 gST
->ConOut
->SetCursorPosition (gST
->ConOut
, 0, gST
->ConOut
->Mode
->CursorRow
);
1204 if (CurDir
!= NULL
&& StrLen(CurDir
) > 1) {
1205 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_CURDIR
), ShellInfoObject
.HiiHandle
, CurDir
);
1207 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_SHELL
), ShellInfoObject
.HiiHandle
);
1211 // Read a line from the console
1213 Status
= ShellInfoObject
.NewEfiShellProtocol
->ReadFile(ShellInfoObject
.NewShellParametersProtocol
->StdIn
, &BufferSize
, CmdLine
);
1216 // Null terminate the string and parse it
1218 if (!EFI_ERROR (Status
)) {
1219 CmdLine
[BufferSize
/ sizeof (CHAR16
)] = CHAR_NULL
;
1220 Status
= RunCommand(CmdLine
);
1224 // Done with this command
1231 Add a buffer to the Buffer To Free List for safely returning buffers to other
1232 places without risking letting them modify internal shell information.
1234 @param Buffer Something to pass to FreePool when the shell is exiting.
1238 AddBufferToFreeList(
1242 BUFFER_LIST
*BufferListEntry
;
1244 if (Buffer
== NULL
) {
1248 BufferListEntry
= AllocateZeroPool(sizeof(BUFFER_LIST
));
1249 ASSERT(BufferListEntry
!= NULL
);
1250 BufferListEntry
->Buffer
= Buffer
;
1251 InsertTailList(&ShellInfoObject
.BufferToFreeList
.Link
, &BufferListEntry
->Link
);
1256 Add a buffer to the Line History List
1258 @param Buffer The line buffer to add.
1262 AddLineToCommandHistory(
1263 IN CONST CHAR16
*Buffer
1268 Node
= AllocateZeroPool(sizeof(BUFFER_LIST
));
1269 ASSERT(Node
!= NULL
);
1270 Node
->Buffer
= AllocateCopyPool(StrSize(Buffer
), Buffer
);
1271 ASSERT(Node
->Buffer
!= NULL
);
1273 InsertTailList(&ShellInfoObject
.ViewingSettings
.CommandHistory
.Link
, &Node
->Link
);
1277 Checks if a string is an alias for another command. If yes, then it replaces the alias name
1278 with the correct command name.
1280 @param[in, out] CommandString Upon entry the potential alias. Upon return the
1281 command name if it was an alias. If it was not
1282 an alias it will be unchanged. This function may
1283 change the buffer to fit the command name.
1285 @retval EFI_SUCCESS The name was changed.
1286 @retval EFI_SUCCESS The name was not an alias.
1287 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
1292 IN OUT CHAR16
**CommandString
1295 CONST CHAR16
*NewString
;
1297 NewString
= ShellInfoObject
.NewEfiShellProtocol
->GetAlias(*CommandString
, NULL
);
1298 if (NewString
== NULL
) {
1299 return (EFI_SUCCESS
);
1301 FreePool(*CommandString
);
1302 *CommandString
= AllocateCopyPool(StrSize(NewString
), NewString
);
1303 if (*CommandString
== NULL
) {
1304 return (EFI_OUT_OF_RESOURCES
);
1306 return (EFI_SUCCESS
);
1310 This function will eliminate unreplaced (and therefore non-found) environment variables.
1312 @param[in,out] CmdLine The command line to update.
1316 StripUnreplacedEnvironmentVariables(
1317 IN OUT CHAR16
*CmdLine
1320 CHAR16
*FirstPercent
;
1322 CHAR16
*SecondPercent
;
1323 CHAR16
*SecondQuote
;
1324 CHAR16
*CurrentLocator
;
1326 for (CurrentLocator
= CmdLine
; CurrentLocator
!= NULL
; ) {
1327 FirstQuote
= FindNextInstance(CurrentLocator
, L
"\"", TRUE
);
1328 FirstPercent
= FindNextInstance(CurrentLocator
, L
"%", TRUE
);
1329 SecondPercent
= FirstPercent
!=NULL
?FindNextInstance(FirstPercent
+1, L
"%", TRUE
):NULL
;
1330 if (FirstPercent
== NULL
|| SecondPercent
== NULL
) {
1332 // If we ever dont have 2 % we are done.
1337 if (FirstQuote
< FirstPercent
) {
1338 SecondQuote
= FirstQuote
!= NULL
?FindNextInstance(FirstQuote
+1, L
"\"", TRUE
):NULL
;
1340 // Quote is first found
1343 if (SecondQuote
< FirstPercent
) {
1345 // restart after the pair of "
1347 CurrentLocator
= SecondQuote
+ 1;
1348 } else /* FirstPercent < SecondQuote */{
1350 // Restart on the first percent
1352 CurrentLocator
= FirstPercent
;
1356 ASSERT(FirstPercent
< FirstQuote
);
1357 if (SecondPercent
< FirstQuote
) {
1358 FirstPercent
[0] = L
'\"';
1359 SecondPercent
[0] = L
'\"';
1362 // We need to remove from FirstPercent to SecondPercent
1364 CopyMem(FirstPercent
+ 1, SecondPercent
, StrSize(SecondPercent
));
1365 CurrentLocator
= FirstPercent
+ 2;
1368 ASSERT(FirstQuote
< SecondPercent
);
1369 CurrentLocator
= FirstQuote
;
1371 return (EFI_SUCCESS
);
1375 Function allocates a new command line and replaces all instances of environment
1376 variable names that are correctly preset to their values.
1378 If the return value is not NULL the memory must be caller freed.
1380 @param[in] OriginalCommandLine The original command line
1382 @retval NULL An error ocurred.
1383 @return The new command line with no environment variables present.
1387 ShellConvertVariables (
1388 IN CONST CHAR16
*OriginalCommandLine
1391 CONST CHAR16
*MasterEnvList
;
1393 CHAR16
*NewCommandLine1
;
1394 CHAR16
*NewCommandLine2
;
1398 SCRIPT_FILE
*CurrentScriptFile
;
1399 ALIAS_LIST
*AliasListNode
;
1401 ASSERT(OriginalCommandLine
!= NULL
);
1404 NewSize
= StrSize(OriginalCommandLine
);
1405 CurrentScriptFile
= ShellCommandGetCurrentScriptFile();
1408 ///@todo update this to handle the %0 - %9 for scripting only (borrow from line 1256 area) ? ? ?
1411 // calculate the size required for the post-conversion string...
1413 if (CurrentScriptFile
!= NULL
) {
1414 for (AliasListNode
= (ALIAS_LIST
*)GetFirstNode(&CurrentScriptFile
->SubstList
)
1415 ; !IsNull(&CurrentScriptFile
->SubstList
, &AliasListNode
->Link
)
1416 ; AliasListNode
= (ALIAS_LIST
*)GetNextNode(&CurrentScriptFile
->SubstList
, &AliasListNode
->Link
)
1418 for (Temp
= StrStr(OriginalCommandLine
, AliasListNode
->Alias
)
1420 ; Temp
= StrStr(Temp
+1, AliasListNode
->Alias
)
1423 // we need a preceeding and if there is space no ^ preceeding (if no space ignore)
1425 if ((((Temp
-OriginalCommandLine
)>2) && *(Temp
-2) != L
'^') || ((Temp
-OriginalCommandLine
)<=2)) {
1426 NewSize
+= StrSize(AliasListNode
->CommandString
);
1432 for (MasterEnvList
= EfiShellGetEnv(NULL
)
1433 ; MasterEnvList
!= NULL
&& *MasterEnvList
!= CHAR_NULL
//&& *(MasterEnvList+1) != CHAR_NULL
1434 ; MasterEnvList
+= StrLen(MasterEnvList
) + 1
1436 if (StrSize(MasterEnvList
) > ItemSize
) {
1437 ItemSize
= StrSize(MasterEnvList
);
1439 for (Temp
= StrStr(OriginalCommandLine
, MasterEnvList
)
1441 ; Temp
= StrStr(Temp
+1, MasterEnvList
)
1444 // we need a preceeding and following % and if there is space no ^ preceeding (if no space ignore)
1446 if (*(Temp
-1) == L
'%' && *(Temp
+StrLen(MasterEnvList
)) == L
'%' &&
1447 ((((Temp
-OriginalCommandLine
)>2) && *(Temp
-2) != L
'^') || ((Temp
-OriginalCommandLine
)<=2))) {
1448 NewSize
+=StrSize(EfiShellGetEnv(MasterEnvList
));
1454 // now do the replacements...
1456 NewCommandLine1
= AllocateCopyPool(NewSize
, OriginalCommandLine
);
1457 NewCommandLine2
= AllocateZeroPool(NewSize
);
1458 ItemTemp
= AllocateZeroPool(ItemSize
+(2*sizeof(CHAR16
)));
1459 if (NewCommandLine1
== NULL
|| NewCommandLine2
== NULL
|| ItemTemp
== NULL
) {
1460 SHELL_FREE_NON_NULL(NewCommandLine1
);
1461 SHELL_FREE_NON_NULL(NewCommandLine2
);
1462 SHELL_FREE_NON_NULL(ItemTemp
);
1465 for (MasterEnvList
= EfiShellGetEnv(NULL
)
1466 ; MasterEnvList
!= NULL
&& *MasterEnvList
!= CHAR_NULL
1467 ; MasterEnvList
+= StrLen(MasterEnvList
) + 1
1469 StrnCpy(ItemTemp
, L
"%", ((ItemSize
+(2*sizeof(CHAR16
)))/sizeof(CHAR16
))-1);
1470 StrnCat(ItemTemp
, MasterEnvList
, ((ItemSize
+(2*sizeof(CHAR16
)))/sizeof(CHAR16
))-1 - StrLen(ItemTemp
));
1471 StrnCat(ItemTemp
, L
"%", ((ItemSize
+(2*sizeof(CHAR16
)))/sizeof(CHAR16
))-1 - StrLen(ItemTemp
));
1472 ShellCopySearchAndReplace(NewCommandLine1
, NewCommandLine2
, NewSize
, ItemTemp
, EfiShellGetEnv(MasterEnvList
), TRUE
, FALSE
);
1473 StrnCpy(NewCommandLine1
, NewCommandLine2
, NewSize
/sizeof(CHAR16
)-1);
1475 if (CurrentScriptFile
!= NULL
) {
1476 for (AliasListNode
= (ALIAS_LIST
*)GetFirstNode(&CurrentScriptFile
->SubstList
)
1477 ; !IsNull(&CurrentScriptFile
->SubstList
, &AliasListNode
->Link
)
1478 ; AliasListNode
= (ALIAS_LIST
*)GetNextNode(&CurrentScriptFile
->SubstList
, &AliasListNode
->Link
)
1480 ShellCopySearchAndReplace(NewCommandLine1
, NewCommandLine2
, NewSize
, AliasListNode
->Alias
, AliasListNode
->CommandString
, TRUE
, FALSE
);
1481 StrnCpy(NewCommandLine1
, NewCommandLine2
, NewSize
/sizeof(CHAR16
)-1);
1485 // Remove non-existant environment variables in scripts only
1487 StripUnreplacedEnvironmentVariables(NewCommandLine1
);
1491 // Now cleanup any straggler intentionally ignored "%" characters
1493 ShellCopySearchAndReplace(NewCommandLine1
, NewCommandLine2
, NewSize
, L
"^%", L
"%", TRUE
, FALSE
);
1494 StrnCpy(NewCommandLine1
, NewCommandLine2
, NewSize
/sizeof(CHAR16
)-1);
1496 FreePool(NewCommandLine2
);
1499 return (NewCommandLine1
);
1503 Internal function to run a command line with pipe usage.
1505 @param[in] CmdLine The pointer to the command line.
1506 @param[in] StdIn The pointer to the Standard input.
1507 @param[in] StdOut The pointer to the Standard output.
1509 @retval EFI_SUCCESS The split command is executed successfully.
1510 @retval other Some error occurs when executing the split command.
1515 IN CONST CHAR16
*CmdLine
,
1516 IN SHELL_FILE_HANDLE
*StdIn
,
1517 IN SHELL_FILE_HANDLE
*StdOut
1521 CHAR16
*NextCommandLine
;
1522 CHAR16
*OurCommandLine
;
1526 SHELL_FILE_HANDLE
*TempFileHandle
;
1529 ASSERT(StdOut
== NULL
);
1531 ASSERT(StrStr(CmdLine
, L
"|") != NULL
);
1533 Status
= EFI_SUCCESS
;
1534 NextCommandLine
= NULL
;
1535 OurCommandLine
= NULL
;
1539 NextCommandLine
= StrnCatGrow(&NextCommandLine
, &Size1
, StrStr(CmdLine
, L
"|")+1, 0);
1540 OurCommandLine
= StrnCatGrow(&OurCommandLine
, &Size2
, CmdLine
, StrStr(CmdLine
, L
"|") - CmdLine
);
1542 if (NextCommandLine
== NULL
|| OurCommandLine
== NULL
) {
1543 SHELL_FREE_NON_NULL(OurCommandLine
);
1544 SHELL_FREE_NON_NULL(NextCommandLine
);
1545 return (EFI_OUT_OF_RESOURCES
);
1546 } else if (StrStr(OurCommandLine
, L
"|") != NULL
|| Size1
== 0 || Size2
== 0) {
1547 SHELL_FREE_NON_NULL(OurCommandLine
);
1548 SHELL_FREE_NON_NULL(NextCommandLine
);
1549 return (EFI_INVALID_PARAMETER
);
1550 } else if (NextCommandLine
[0] != CHAR_NULL
&&
1551 NextCommandLine
[0] == L
'a' &&
1552 NextCommandLine
[1] == L
' '
1554 CopyMem(NextCommandLine
, NextCommandLine
+1, StrSize(NextCommandLine
) - sizeof(NextCommandLine
[0]));
1562 // make a SPLIT_LIST item and add to list
1564 Split
= AllocateZeroPool(sizeof(SPLIT_LIST
));
1565 ASSERT(Split
!= NULL
);
1566 Split
->SplitStdIn
= StdIn
;
1567 Split
->SplitStdOut
= ConvertEfiFileProtocolToShellHandle(CreateFileInterfaceMem(Unicode
), NULL
);
1568 ASSERT(Split
->SplitStdOut
!= NULL
);
1569 InsertHeadList(&ShellInfoObject
.SplitList
.Link
, &Split
->Link
);
1571 Status
= RunCommand(OurCommandLine
);
1574 // move the output from the first to the in to the second.
1576 TempFileHandle
= Split
->SplitStdOut
;
1577 if (Split
->SplitStdIn
== StdIn
) {
1578 Split
->SplitStdOut
= NULL
;
1580 Split
->SplitStdOut
= Split
->SplitStdIn
;
1582 Split
->SplitStdIn
= TempFileHandle
;
1583 ShellInfoObject
.NewEfiShellProtocol
->SetFilePosition(ConvertShellHandleToEfiFileProtocol(Split
->SplitStdIn
), 0);
1585 if (!EFI_ERROR(Status
)) {
1586 Status
= RunCommand(NextCommandLine
);
1590 // remove the top level from the ScriptList
1592 ASSERT((SPLIT_LIST
*)GetFirstNode(&ShellInfoObject
.SplitList
.Link
) == Split
);
1593 RemoveEntryList(&Split
->Link
);
1596 // Note that the original StdIn is now the StdOut...
1598 if (Split
->SplitStdOut
!= NULL
&& Split
->SplitStdOut
!= StdIn
) {
1599 ShellInfoObject
.NewEfiShellProtocol
->CloseFile(ConvertShellHandleToEfiFileProtocol(Split
->SplitStdOut
));
1601 if (Split
->SplitStdIn
!= NULL
) {
1602 ShellInfoObject
.NewEfiShellProtocol
->CloseFile(ConvertShellHandleToEfiFileProtocol(Split
->SplitStdIn
));
1606 FreePool(NextCommandLine
);
1607 FreePool(OurCommandLine
);
1613 Take the original command line, substitute any variables, free
1614 the original string, return the modified copy.
1616 @param[in] CmdLine pointer to the command line to update.
1618 @retval EFI_SUCCESS the function was successful.
1619 @retval EFI_OUT_OF_RESOURCES a memory allocation failed.
1623 ShellSubstituteVariables(
1628 NewCmdLine
= ShellConvertVariables(*CmdLine
);
1629 SHELL_FREE_NON_NULL(*CmdLine
);
1630 if (NewCmdLine
== NULL
) {
1631 return (EFI_OUT_OF_RESOURCES
);
1633 *CmdLine
= NewCmdLine
;
1634 return (EFI_SUCCESS
);
1638 Take the original command line, substitute any alias in the first group of space delimited characters, free
1639 the original string, return the modified copy.
1641 @param[in] CmdLine pointer to the command line to update.
1643 @retval EFI_SUCCESS the function was successful.
1644 @retval EFI_OUT_OF_RESOURCES a memory allocation failed.
1648 ShellSubstituteAliases(
1653 CHAR16
*CommandName
;
1655 UINTN PostAliasSize
;
1656 ASSERT(CmdLine
!= NULL
);
1657 ASSERT(*CmdLine
!= NULL
);
1661 if (StrStr((*CmdLine
), L
" ") == NULL
){
1662 StrnCatGrow(&CommandName
, NULL
, (*CmdLine
), 0);
1664 StrnCatGrow(&CommandName
, NULL
, (*CmdLine
), StrStr((*CmdLine
), L
" ") - (*CmdLine
));
1668 // This cannot happen 'inline' since the CmdLine can need extra space.
1671 if (!ShellCommandIsCommandOnList(CommandName
)) {
1673 // Convert via alias
1675 Status
= ShellConvertAlias(&CommandName
);
1676 if (EFI_ERROR(Status
)){
1680 NewCmdLine
= StrnCatGrow(&NewCmdLine
, &PostAliasSize
, CommandName
, 0);
1681 if (NewCmdLine
== NULL
) {
1682 SHELL_FREE_NON_NULL(CommandName
);
1683 SHELL_FREE_NON_NULL(*CmdLine
);
1684 return (EFI_OUT_OF_RESOURCES
);
1686 NewCmdLine
= StrnCatGrow(&NewCmdLine
, &PostAliasSize
, StrStr((*CmdLine
), L
" "), 0);
1687 if (NewCmdLine
== NULL
) {
1688 SHELL_FREE_NON_NULL(CommandName
);
1689 SHELL_FREE_NON_NULL(*CmdLine
);
1690 return (EFI_OUT_OF_RESOURCES
);
1693 NewCmdLine
= StrnCatGrow(&NewCmdLine
, NULL
, (*CmdLine
), 0);
1696 SHELL_FREE_NON_NULL(*CmdLine
);
1697 SHELL_FREE_NON_NULL(CommandName
);
1700 // re-assign the passed in double pointer to point to our newly allocated buffer
1702 *CmdLine
= NewCmdLine
;
1704 return (EFI_SUCCESS
);
1708 Takes the Argv[0] part of the command line and determine the meaning of it.
1710 @param[in] CmdName pointer to the command line to update.
1712 @retval Internal_Command The name is an internal command.
1713 @retval File_Sys_Change the name is a file system change.
1714 @retval Script_File_Name the name is a NSH script file.
1715 @retval Unknown_Invalid the name is unknown.
1716 @retval Efi_Application the name is an application (.EFI).
1718 SHELL_OPERATION_TYPES
1721 IN CONST CHAR16
*CmdName
1724 CHAR16
* FileWithPath
;
1725 CONST CHAR16
* TempLocation
;
1726 CONST CHAR16
* TempLocation2
;
1728 FileWithPath
= NULL
;
1730 // test for an internal command.
1732 if (ShellCommandIsCommandOnList(CmdName
)) {
1733 return (Internal_Command
);
1737 // Test for file system change request. anything ending with first : and cant have spaces.
1739 if (CmdName
[(StrLen(CmdName
)-1)] == L
':') {
1740 if ( StrStr(CmdName
, L
" ") != NULL
1741 || StrLen(StrStr(CmdName
, L
":")) > 1
1743 return (Unknown_Invalid
);
1745 return (File_Sys_Change
);
1751 if ((FileWithPath
= ShellFindFilePathEx(CmdName
, mExecutableExtensions
)) != NULL
) {
1753 // See if that file has a script file extension
1755 if (StrLen(FileWithPath
) > 4) {
1756 TempLocation
= FileWithPath
+StrLen(FileWithPath
)-4;
1757 TempLocation2
= mScriptExtension
;
1758 if (StringNoCaseCompare((VOID
*)(&TempLocation
), (VOID
*)(&TempLocation2
)) == 0) {
1759 SHELL_FREE_NON_NULL(FileWithPath
);
1760 return (Script_File_Name
);
1765 // Was a file, but not a script. we treat this as an application.
1767 SHELL_FREE_NON_NULL(FileWithPath
);
1768 return (Efi_Application
);
1771 SHELL_FREE_NON_NULL(FileWithPath
);
1773 // No clue what this is... return invalid flag...
1775 return (Unknown_Invalid
);
1779 Determine if the first item in a command line is valid.
1781 @param[in] CmdLine The command line to parse.
1783 @retval EFI_SUCCESS The item is valid.
1784 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
1785 @retval EFI_NOT_FOUND The operation type is unknown or invalid.
1790 IN CONST CHAR16
*CmdLine
1794 CHAR16
*FirstParameter
;
1800 Temp
= StrnCatGrow(&Temp
, NULL
, CmdLine
, 0);
1802 return (EFI_OUT_OF_RESOURCES
);
1805 FirstParameter
= StrStr(Temp
, L
"|");
1806 if (FirstParameter
!= NULL
) {
1807 *FirstParameter
= CHAR_NULL
;
1810 FirstParameter
= NULL
;
1813 // Process the command line
1815 Status
= ProcessCommandLineToFinal(&Temp
);
1817 if (!EFI_ERROR(Status
)) {
1818 FirstParameter
= AllocateZeroPool(StrSize(CmdLine
));
1819 if (FirstParameter
== NULL
) {
1820 SHELL_FREE_NON_NULL(Temp
);
1821 return (EFI_OUT_OF_RESOURCES
);
1823 TempWalker
= (CHAR16
*)Temp
;
1824 GetNextParameter(&TempWalker
, &FirstParameter
, StrSize(CmdLine
));
1826 if (GetOperationType(FirstParameter
) == Unknown_Invalid
) {
1827 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_NOT_FOUND
), ShellInfoObject
.HiiHandle
, FirstParameter
);
1828 SetLastError(SHELL_NOT_FOUND
);
1829 Status
= EFI_NOT_FOUND
;
1833 SHELL_FREE_NON_NULL(Temp
);
1834 SHELL_FREE_NON_NULL(FirstParameter
);
1839 Determine if a command line contains with a split contains only valid commands.
1841 @param[in] CmdLine The command line to parse.
1843 @retval EFI_SUCCESS CmdLine has only valid commands, application, or has no split.
1844 @retval EFI_ABORTED CmdLine has at least one invalid command or application.
1849 IN CONST CHAR16
*CmdLine
1852 CONST CHAR16
*TempSpot
;
1856 // Verify up to the pipe or end character
1858 Status
= IsValidSplit(CmdLine
);
1859 if (EFI_ERROR(Status
)) {
1864 // If this was the only item, then get out
1866 if (!ContainsSplit(CmdLine
)) {
1867 return (EFI_SUCCESS
);
1871 // recurse to verify the next item
1873 TempSpot
= FindSplit(CmdLine
)+1;
1874 return (VerifySplit(TempSpot
));
1878 Process a split based operation.
1880 @param[in] CmdLine pointer to the command line to process
1882 @retval EFI_SUCCESS The operation was successful
1883 @return an error occured.
1887 ProcessNewSplitCommandLine(
1888 IN CONST CHAR16
*CmdLine
1894 Status
= VerifySplit(CmdLine
);
1895 if (EFI_ERROR(Status
)) {
1902 // are we in an existing split???
1904 if (!IsListEmpty(&ShellInfoObject
.SplitList
.Link
)) {
1905 Split
= (SPLIT_LIST
*)GetFirstNode(&ShellInfoObject
.SplitList
.Link
);
1908 if (Split
== NULL
) {
1909 Status
= RunSplitCommand(CmdLine
, NULL
, NULL
);
1911 Status
= RunSplitCommand(CmdLine
, Split
->SplitStdIn
, Split
->SplitStdOut
);
1913 if (EFI_ERROR(Status
)) {
1914 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_INVALID_SPLIT
), ShellInfoObject
.HiiHandle
, CmdLine
);
1920 Handle a request to change the current file system.
1922 @param[in] CmdLine The passed in command line.
1924 @retval EFI_SUCCESS The operation was successful.
1929 IN CONST CHAR16
*CmdLine
1933 Status
= EFI_SUCCESS
;
1936 // make sure we are the right operation
1938 ASSERT(CmdLine
[(StrLen(CmdLine
)-1)] == L
':' && StrStr(CmdLine
, L
" ") == NULL
);
1941 // Call the protocol API to do the work
1943 Status
= ShellInfoObject
.NewEfiShellProtocol
->SetCurDir(NULL
, CmdLine
);
1946 // Report any errors
1948 if (EFI_ERROR(Status
)) {
1949 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_INVALID_MAPPING
), ShellInfoObject
.HiiHandle
, CmdLine
);
1956 Reprocess the command line to direct all -? to the help command.
1958 if found, will add "help" as argv[0], and move the rest later.
1960 @param[in,out] CmdLine pointer to the command line to update
1965 IN OUT CHAR16
**CmdLine
1968 CHAR16
*CurrentParameter
;
1971 CHAR16
*NewCommandLine
;
1974 Status
= EFI_SUCCESS
;
1976 CurrentParameter
= AllocateZeroPool(StrSize(*CmdLine
));
1977 if (CurrentParameter
== NULL
) {
1978 return (EFI_OUT_OF_RESOURCES
);
1982 while(Walker
!= NULL
&& *Walker
!= CHAR_NULL
) {
1983 LastWalker
= Walker
;
1984 GetNextParameter(&Walker
, &CurrentParameter
, StrSize(*CmdLine
));
1985 if (StrStr(CurrentParameter
, L
"-?") == CurrentParameter
) {
1986 LastWalker
[0] = L
' ';
1987 LastWalker
[1] = L
' ';
1988 NewCommandLine
= AllocateZeroPool(StrSize(L
"help ") + StrSize(*CmdLine
));
1989 if (NewCommandLine
== NULL
) {
1990 Status
= EFI_OUT_OF_RESOURCES
;
1995 // We know the space is sufficient since we just calculated it.
1997 StrnCpy(NewCommandLine
, L
"help ", 5);
1998 StrnCat(NewCommandLine
, *CmdLine
, StrLen(*CmdLine
));
1999 SHELL_FREE_NON_NULL(*CmdLine
);
2000 *CmdLine
= NewCommandLine
;
2005 SHELL_FREE_NON_NULL(CurrentParameter
);
2011 Function to update the shell variable "lasterror".
2013 @param[in] ErrorCode the error code to put into lasterror.
2018 IN CONST SHELL_STATUS ErrorCode
2021 CHAR16 LeString
[19];
2022 if (sizeof(EFI_STATUS
) == sizeof(UINT64
)) {
2023 UnicodeSPrint(LeString
, sizeof(LeString
), L
"0x%Lx", ErrorCode
);
2025 UnicodeSPrint(LeString
, sizeof(LeString
), L
"0x%x", ErrorCode
);
2027 DEBUG_CODE(InternalEfiShellSetEnv(L
"debuglasterror", LeString
, TRUE
););
2028 InternalEfiShellSetEnv(L
"lasterror", LeString
, TRUE
);
2030 return (EFI_SUCCESS
);
2034 Converts the command line to it's post-processed form. this replaces variables and alias' per UEFI Shell spec.
2036 @param[in,out] CmdLine pointer to the command line to update
2038 @retval EFI_SUCCESS The operation was successful
2039 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
2040 @return some other error occured
2044 ProcessCommandLineToFinal(
2045 IN OUT CHAR16
**CmdLine
2049 TrimSpaces(CmdLine
);
2051 Status
= ShellSubstituteAliases(CmdLine
);
2052 if (EFI_ERROR(Status
)) {
2056 TrimSpaces(CmdLine
);
2058 Status
= ShellSubstituteVariables(CmdLine
);
2059 if (EFI_ERROR(Status
)) {
2062 ASSERT (*CmdLine
!= NULL
);
2064 TrimSpaces(CmdLine
);
2067 // update for help parsing
2069 if (StrStr(*CmdLine
, L
"?") != NULL
) {
2071 // This may do nothing if the ? does not indicate help.
2072 // Save all the details for in the API below.
2074 Status
= DoHelpUpdate(CmdLine
);
2077 TrimSpaces(CmdLine
);
2079 return (EFI_SUCCESS
);
2083 Run an internal shell command.
2085 This API will upadate the shell's environment since these commands are libraries.
2087 @param[in] CmdLine the command line to run.
2088 @param[in] FirstParameter the first parameter on the command line
2089 @param[in] ParamProtocol the shell parameters protocol pointer
2091 @retval EFI_SUCCESS The command was completed.
2092 @retval EFI_ABORTED The command's operation was aborted.
2097 IN CONST CHAR16
*CmdLine
,
2098 IN CHAR16
*FirstParameter
,
2099 IN EFI_SHELL_PARAMETERS_PROTOCOL
*ParamProtocol
2105 SHELL_STATUS CommandReturnedStatus
;
2110 NewCmdLine
= AllocateCopyPool (StrSize (CmdLine
), CmdLine
);
2111 if (NewCmdLine
== NULL
) {
2112 return EFI_OUT_OF_RESOURCES
;
2115 for (Walker
= NewCmdLine
; Walker
!= NULL
&& *Walker
!= CHAR_NULL
; Walker
++) {
2116 if (*Walker
== L
'^' && *(Walker
+1) == L
'#') {
2117 CopyMem(Walker
, Walker
+1, StrSize(Walker
) - sizeof(Walker
[0]));
2122 // get the argc and argv updated for internal commands
2124 Status
= UpdateArgcArgv(ParamProtocol
, NewCmdLine
, &Argv
, &Argc
);
2125 if (!EFI_ERROR(Status
)) {
2127 // Run the internal command.
2129 Status
= ShellCommandRunCommandHandler(FirstParameter
, &CommandReturnedStatus
, &LastError
);
2131 if (!EFI_ERROR(Status
)) {
2133 // Update last error status.
2134 // some commands do not update last error.
2137 SetLastError(CommandReturnedStatus
);
2141 // Pass thru the exitcode from the app.
2143 if (ShellCommandGetExit()) {
2145 // An Exit was requested ("exit" command), pass its value up.
2147 Status
= CommandReturnedStatus
;
2148 } else if (CommandReturnedStatus
!= SHELL_SUCCESS
&& IsScriptOnlyCommand(FirstParameter
)) {
2150 // Always abort when a script only command fails for any reason
2152 Status
= EFI_ABORTED
;
2153 } else if (ShellCommandGetCurrentScriptFile() != NULL
&& CommandReturnedStatus
== SHELL_ABORTED
) {
2155 // Abort when in a script and a command aborted
2157 Status
= EFI_ABORTED
;
2163 // This is guarenteed to be called after UpdateArgcArgv no matter what else happened.
2164 // This is safe even if the update API failed. In this case, it may be a no-op.
2166 RestoreArgcArgv(ParamProtocol
, &Argv
, &Argc
);
2169 // If a script is running and the command is not a scipt only command, then
2170 // change return value to success so the script won't halt (unless aborted).
2172 // Script only commands have to be able halt the script since the script will
2173 // not operate if they are failing.
2175 if ( ShellCommandGetCurrentScriptFile() != NULL
2176 && !IsScriptOnlyCommand(FirstParameter
)
2177 && Status
!= EFI_ABORTED
2179 Status
= EFI_SUCCESS
;
2182 FreePool (NewCmdLine
);
2187 Function to run the command or file.
2189 @param[in] Type the type of operation being run.
2190 @param[in] CmdLine the command line to run.
2191 @param[in] FirstParameter the first parameter on the command line
2192 @param[in] ParamProtocol the shell parameters protocol pointer
2194 @retval EFI_SUCCESS The command was completed.
2195 @retval EFI_ABORTED The command's operation was aborted.
2200 IN SHELL_OPERATION_TYPES Type
,
2201 IN CONST CHAR16
*CmdLine
,
2202 IN CHAR16
*FirstParameter
,
2203 IN EFI_SHELL_PARAMETERS_PROTOCOL
*ParamProtocol
2207 EFI_STATUS StartStatus
;
2208 CHAR16
*CommandWithPath
;
2209 EFI_DEVICE_PATH_PROTOCOL
*DevPath
;
2210 SHELL_STATUS CalleeExitStatus
;
2212 Status
= EFI_SUCCESS
;
2213 CommandWithPath
= NULL
;
2215 CalleeExitStatus
= SHELL_INVALID_PARAMETER
;
2218 case Internal_Command
:
2219 Status
= RunInternalCommand(CmdLine
, FirstParameter
, ParamProtocol
);
2221 case Script_File_Name
:
2222 case Efi_Application
:
2224 // Process a fully qualified path
2226 if (StrStr(FirstParameter
, L
":") != NULL
) {
2227 ASSERT (CommandWithPath
== NULL
);
2228 if (ShellIsFile(FirstParameter
) == EFI_SUCCESS
) {
2229 CommandWithPath
= StrnCatGrow(&CommandWithPath
, NULL
, FirstParameter
, 0);
2234 // Process a relative path and also check in the path environment variable
2236 if (CommandWithPath
== NULL
) {
2237 CommandWithPath
= ShellFindFilePathEx(FirstParameter
, mExecutableExtensions
);
2241 // This should be impossible now.
2243 ASSERT(CommandWithPath
!= NULL
);
2246 // Make sure that path is not just a directory (or not found)
2248 if (!EFI_ERROR(ShellIsDirectory(CommandWithPath
))) {
2249 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_NOT_FOUND
), ShellInfoObject
.HiiHandle
, FirstParameter
);
2250 SetLastError(SHELL_NOT_FOUND
);
2253 case Script_File_Name
:
2254 Status
= RunScriptFile (CommandWithPath
, NULL
, CmdLine
, ParamProtocol
);
2256 case Efi_Application
:
2258 // Get the device path of the application image
2260 DevPath
= ShellInfoObject
.NewEfiShellProtocol
->GetDevicePathFromFilePath(CommandWithPath
);
2261 if (DevPath
== NULL
){
2262 Status
= EFI_OUT_OF_RESOURCES
;
2267 // Execute the device path
2269 Status
= InternalShellExecuteDevicePath(
2277 SHELL_FREE_NON_NULL(DevPath
);
2279 if(EFI_ERROR (Status
)) {
2280 CalleeExitStatus
= (SHELL_STATUS
) (Status
& (~MAX_BIT
));
2282 CalleeExitStatus
= (SHELL_STATUS
) StartStatus
;
2286 // Update last error status.
2288 // Status is an EFI_STATUS. Clear top bit to convert to SHELL_STATUS
2289 SetLastError(CalleeExitStatus
);
2305 SHELL_FREE_NON_NULL(CommandWithPath
);
2311 Function to setup StdIn, StdErr, StdOut, and then run the command or file.
2313 @param[in] Type the type of operation being run.
2314 @param[in] CmdLine the command line to run.
2315 @param[in] FirstParameter the first parameter on the command line.
2316 @param[in] ParamProtocol the shell parameters protocol pointer
2318 @retval EFI_SUCCESS The command was completed.
2319 @retval EFI_ABORTED The command's operation was aborted.
2323 SetupAndRunCommandOrFile(
2324 IN SHELL_OPERATION_TYPES Type
,
2326 IN CHAR16
*FirstParameter
,
2327 IN EFI_SHELL_PARAMETERS_PROTOCOL
*ParamProtocol
2331 SHELL_FILE_HANDLE OriginalStdIn
;
2332 SHELL_FILE_HANDLE OriginalStdOut
;
2333 SHELL_FILE_HANDLE OriginalStdErr
;
2334 SYSTEM_TABLE_INFO OriginalSystemTableInfo
;
2337 // Update the StdIn, StdOut, and StdErr for redirection to environment variables, files, etc... unicode and ASCII
2339 Status
= UpdateStdInStdOutStdErr(ParamProtocol
, CmdLine
, &OriginalStdIn
, &OriginalStdOut
, &OriginalStdErr
, &OriginalSystemTableInfo
);
2342 // The StdIn, StdOut, and StdErr are set up.
2343 // Now run the command, script, or application
2345 if (!EFI_ERROR(Status
)) {
2346 TrimSpaces(&CmdLine
);
2347 Status
= RunCommandOrFile(Type
, CmdLine
, FirstParameter
, ParamProtocol
);
2353 if (EFI_ERROR(Status
)) {
2354 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_ERROR
), ShellInfoObject
.HiiHandle
, (VOID
*)(Status
));
2358 // put back the original StdIn, StdOut, and StdErr
2360 RestoreStdInStdOutStdErr(ParamProtocol
, &OriginalStdIn
, &OriginalStdOut
, &OriginalStdErr
, &OriginalSystemTableInfo
);
2366 Function will process and run a command line.
2368 This will determine if the command line represents an internal shell
2369 command or dispatch an external application.
2371 @param[in] CmdLine The command line to parse.
2373 @retval EFI_SUCCESS The command was completed.
2374 @retval EFI_ABORTED The command's operation was aborted.
2379 IN CONST CHAR16
*CmdLine
2383 CHAR16
*CleanOriginal
;
2384 CHAR16
*FirstParameter
;
2386 SHELL_OPERATION_TYPES Type
;
2388 ASSERT(CmdLine
!= NULL
);
2389 if (StrLen(CmdLine
) == 0) {
2390 return (EFI_SUCCESS
);
2393 Status
= EFI_SUCCESS
;
2394 CleanOriginal
= NULL
;
2396 CleanOriginal
= StrnCatGrow(&CleanOriginal
, NULL
, CmdLine
, 0);
2397 if (CleanOriginal
== NULL
) {
2398 return (EFI_OUT_OF_RESOURCES
);
2401 TrimSpaces(&CleanOriginal
);
2404 // NULL out comments (leveraged from RunScriptFileHandle() ).
2405 // The # character on a line is used to denote that all characters on the same line
2406 // and to the right of the # are to be ignored by the shell.
2407 // Afterward, again remove spaces, in case any were between the last command-parameter and '#'.
2409 for (TempWalker
= CleanOriginal
; TempWalker
!= NULL
&& *TempWalker
!= CHAR_NULL
; TempWalker
++) {
2410 if (*TempWalker
== L
'^') {
2411 if (*(TempWalker
+ 1) == L
'#') {
2414 } else if (*TempWalker
== L
'#') {
2415 *TempWalker
= CHAR_NULL
;
2419 TrimSpaces(&CleanOriginal
);
2422 // Handle case that passed in command line is just 1 or more " " characters.
2424 if (StrLen (CleanOriginal
) == 0) {
2425 SHELL_FREE_NON_NULL(CleanOriginal
);
2426 return (EFI_SUCCESS
);
2429 Status
= ProcessCommandLineToFinal(&CleanOriginal
);
2430 if (EFI_ERROR(Status
)) {
2431 SHELL_FREE_NON_NULL(CleanOriginal
);
2436 // We dont do normal processing with a split command line (output from one command input to another)
2438 if (ContainsSplit(CleanOriginal
)) {
2439 Status
= ProcessNewSplitCommandLine(CleanOriginal
);
2440 SHELL_FREE_NON_NULL(CleanOriginal
);
2445 // We need the first parameter information so we can determine the operation type
2447 FirstParameter
= AllocateZeroPool(StrSize(CleanOriginal
));
2448 if (FirstParameter
== NULL
) {
2449 SHELL_FREE_NON_NULL(CleanOriginal
);
2450 return (EFI_OUT_OF_RESOURCES
);
2452 TempWalker
= CleanOriginal
;
2453 GetNextParameter(&TempWalker
, &FirstParameter
, StrSize(CleanOriginal
));
2456 // Depending on the first parameter we change the behavior
2458 switch (Type
= GetOperationType(FirstParameter
)) {
2459 case File_Sys_Change
:
2460 Status
= ChangeMappedDrive (FirstParameter
);
2462 case Internal_Command
:
2463 case Script_File_Name
:
2464 case Efi_Application
:
2465 Status
= SetupAndRunCommandOrFile(Type
, CleanOriginal
, FirstParameter
, ShellInfoObject
.NewShellParametersProtocol
);
2469 // Whatever was typed, it was invalid.
2471 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_NOT_FOUND
), ShellInfoObject
.HiiHandle
, FirstParameter
);
2472 SetLastError(SHELL_NOT_FOUND
);
2476 SHELL_FREE_NON_NULL(CleanOriginal
);
2477 SHELL_FREE_NON_NULL(FirstParameter
);
2482 STATIC CONST UINT16 InvalidChars
[] = {L
'*', L
'?', L
'<', L
'>', L
'\\', L
'/', L
'\"', 0x0001, 0x0002};
2484 Function determins if the CommandName COULD be a valid command. It does not determine whether
2485 this is a valid command. It only checks for invalid characters.
2487 @param[in] CommandName The name to check
2489 @retval TRUE CommandName could be a command name
2490 @retval FALSE CommandName could not be a valid command name
2495 IN CONST CHAR16
*CommandName
2499 if (CommandName
== NULL
) {
2504 ; Count
< sizeof(InvalidChars
) / sizeof(InvalidChars
[0])
2507 if (ScanMem16(CommandName
, StrSize(CommandName
), InvalidChars
[Count
]) != NULL
) {
2515 Function to process a NSH script file via SHELL_FILE_HANDLE.
2517 @param[in] Handle The handle to the already opened file.
2518 @param[in] Name The name of the script file.
2520 @retval EFI_SUCCESS the script completed sucessfully
2524 RunScriptFileHandle (
2525 IN SHELL_FILE_HANDLE Handle
,
2526 IN CONST CHAR16
*Name
2530 SCRIPT_FILE
*NewScriptFile
;
2532 CHAR16
*CommandLine
;
2533 CHAR16
*CommandLine2
;
2534 CHAR16
*CommandLine3
;
2535 SCRIPT_COMMAND_LIST
*LastCommand
;
2537 BOOLEAN PreScriptEchoState
;
2538 BOOLEAN PreCommandEchoState
;
2539 CONST CHAR16
*CurDir
;
2541 CHAR16 LeString
[50];
2543 ASSERT(!ShellCommandGetScriptExit());
2545 PreScriptEchoState
= ShellCommandGetEchoState();
2547 NewScriptFile
= (SCRIPT_FILE
*)AllocateZeroPool(sizeof(SCRIPT_FILE
));
2548 if (NewScriptFile
== NULL
) {
2549 return (EFI_OUT_OF_RESOURCES
);
2555 ASSERT(NewScriptFile
->ScriptName
== NULL
);
2556 NewScriptFile
->ScriptName
= StrnCatGrow(&NewScriptFile
->ScriptName
, NULL
, Name
, 0);
2557 if (NewScriptFile
->ScriptName
== NULL
) {
2558 DeleteScriptFileStruct(NewScriptFile
);
2559 return (EFI_OUT_OF_RESOURCES
);
2563 // Save the parameters (used to replace %0 to %9 later on)
2565 NewScriptFile
->Argc
= ShellInfoObject
.NewShellParametersProtocol
->Argc
;
2566 if (NewScriptFile
->Argc
!= 0) {
2567 NewScriptFile
->Argv
= (CHAR16
**)AllocateZeroPool(NewScriptFile
->Argc
* sizeof(CHAR16
*));
2568 if (NewScriptFile
->Argv
== NULL
) {
2569 DeleteScriptFileStruct(NewScriptFile
);
2570 return (EFI_OUT_OF_RESOURCES
);
2572 for (LoopVar
= 0 ; LoopVar
< 10 && LoopVar
< NewScriptFile
->Argc
; LoopVar
++) {
2573 ASSERT(NewScriptFile
->Argv
[LoopVar
] == NULL
);
2574 NewScriptFile
->Argv
[LoopVar
] = StrnCatGrow(&NewScriptFile
->Argv
[LoopVar
], NULL
, ShellInfoObject
.NewShellParametersProtocol
->Argv
[LoopVar
], 0);
2575 if (NewScriptFile
->Argv
[LoopVar
] == NULL
) {
2576 DeleteScriptFileStruct(NewScriptFile
);
2577 return (EFI_OUT_OF_RESOURCES
);
2581 NewScriptFile
->Argv
= NULL
;
2584 InitializeListHead(&NewScriptFile
->CommandList
);
2585 InitializeListHead(&NewScriptFile
->SubstList
);
2588 // Now build the list of all script commands.
2591 while(!ShellFileHandleEof(Handle
)) {
2592 CommandLine
= ShellFileHandleReturnLine(Handle
, &Ascii
);
2594 if (CommandLine
== NULL
|| StrLen(CommandLine
) == 0 || CommandLine
[0] == '#') {
2595 SHELL_FREE_NON_NULL(CommandLine
);
2598 NewScriptFile
->CurrentCommand
= AllocateZeroPool(sizeof(SCRIPT_COMMAND_LIST
));
2599 if (NewScriptFile
->CurrentCommand
== NULL
) {
2600 SHELL_FREE_NON_NULL(CommandLine
);
2601 DeleteScriptFileStruct(NewScriptFile
);
2602 return (EFI_OUT_OF_RESOURCES
);
2605 NewScriptFile
->CurrentCommand
->Cl
= CommandLine
;
2606 NewScriptFile
->CurrentCommand
->Data
= NULL
;
2607 NewScriptFile
->CurrentCommand
->Line
= LineCount
;
2609 InsertTailList(&NewScriptFile
->CommandList
, &NewScriptFile
->CurrentCommand
->Link
);
2613 // Add this as the topmost script file
2615 ShellCommandSetNewScript (NewScriptFile
);
2618 // Now enumerate through the commands and run each one.
2620 CommandLine
= AllocateZeroPool(PcdGet16(PcdShellPrintBufferSize
));
2621 if (CommandLine
== NULL
) {
2622 DeleteScriptFileStruct(NewScriptFile
);
2623 return (EFI_OUT_OF_RESOURCES
);
2625 CommandLine2
= AllocateZeroPool(PcdGet16(PcdShellPrintBufferSize
));
2626 if (CommandLine2
== NULL
) {
2627 FreePool(CommandLine
);
2628 DeleteScriptFileStruct(NewScriptFile
);
2629 return (EFI_OUT_OF_RESOURCES
);
2632 for ( NewScriptFile
->CurrentCommand
= (SCRIPT_COMMAND_LIST
*)GetFirstNode(&NewScriptFile
->CommandList
)
2633 ; !IsNull(&NewScriptFile
->CommandList
, &NewScriptFile
->CurrentCommand
->Link
)
2634 ; // conditional increment in the body of the loop
2636 ASSERT(CommandLine2
!= NULL
);
2637 StrnCpy(CommandLine2
, NewScriptFile
->CurrentCommand
->Cl
, PcdGet16(PcdShellPrintBufferSize
)/sizeof(CHAR16
)-1);
2640 // NULL out comments
2642 for (CommandLine3
= CommandLine2
; CommandLine3
!= NULL
&& *CommandLine3
!= CHAR_NULL
; CommandLine3
++) {
2643 if (*CommandLine3
== L
'^') {
2644 if ( *(CommandLine3
+1) == L
':') {
2645 CopyMem(CommandLine3
, CommandLine3
+1, StrSize(CommandLine3
) - sizeof(CommandLine3
[0]));
2646 } else if (*(CommandLine3
+1) == L
'#') {
2649 } else if (*CommandLine3
== L
'#') {
2650 *CommandLine3
= CHAR_NULL
;
2654 if (CommandLine2
!= NULL
&& StrLen(CommandLine2
) >= 1) {
2656 // Due to variability in starting the find and replace action we need to have both buffers the same.
2658 StrnCpy(CommandLine
, CommandLine2
, PcdGet16(PcdShellPrintBufferSize
)/sizeof(CHAR16
)-1);
2661 // Remove the %0 to %9 from the command line (if we have some arguments)
2663 if (NewScriptFile
->Argv
!= NULL
) {
2664 switch (NewScriptFile
->Argc
) {
2666 Status
= ShellCopySearchAndReplace(CommandLine2
, CommandLine
, PcdGet16 (PcdShellPrintBufferSize
), L
"%9", NewScriptFile
->Argv
[9], FALSE
, FALSE
);
2667 ASSERT_EFI_ERROR(Status
);
2669 Status
= ShellCopySearchAndReplace(CommandLine
, CommandLine2
, PcdGet16 (PcdShellPrintBufferSize
), L
"%8", NewScriptFile
->Argv
[8], FALSE
, FALSE
);
2670 ASSERT_EFI_ERROR(Status
);
2672 Status
= ShellCopySearchAndReplace(CommandLine2
, CommandLine
, PcdGet16 (PcdShellPrintBufferSize
), L
"%7", NewScriptFile
->Argv
[7], FALSE
, FALSE
);
2673 ASSERT_EFI_ERROR(Status
);
2675 Status
= ShellCopySearchAndReplace(CommandLine
, CommandLine2
, PcdGet16 (PcdShellPrintBufferSize
), L
"%6", NewScriptFile
->Argv
[6], FALSE
, FALSE
);
2676 ASSERT_EFI_ERROR(Status
);
2678 Status
= ShellCopySearchAndReplace(CommandLine2
, CommandLine
, PcdGet16 (PcdShellPrintBufferSize
), L
"%5", NewScriptFile
->Argv
[5], FALSE
, FALSE
);
2679 ASSERT_EFI_ERROR(Status
);
2681 Status
= ShellCopySearchAndReplace(CommandLine
, CommandLine2
, PcdGet16 (PcdShellPrintBufferSize
), L
"%4", NewScriptFile
->Argv
[4], FALSE
, FALSE
);
2682 ASSERT_EFI_ERROR(Status
);
2684 Status
= ShellCopySearchAndReplace(CommandLine2
, CommandLine
, PcdGet16 (PcdShellPrintBufferSize
), L
"%3", NewScriptFile
->Argv
[3], FALSE
, FALSE
);
2685 ASSERT_EFI_ERROR(Status
);
2687 Status
= ShellCopySearchAndReplace(CommandLine
, CommandLine2
, PcdGet16 (PcdShellPrintBufferSize
), L
"%2", NewScriptFile
->Argv
[2], FALSE
, FALSE
);
2688 ASSERT_EFI_ERROR(Status
);
2690 Status
= ShellCopySearchAndReplace(CommandLine2
, CommandLine
, PcdGet16 (PcdShellPrintBufferSize
), L
"%1", NewScriptFile
->Argv
[1], FALSE
, FALSE
);
2691 ASSERT_EFI_ERROR(Status
);
2693 Status
= ShellCopySearchAndReplace(CommandLine
, CommandLine2
, PcdGet16 (PcdShellPrintBufferSize
), L
"%0", NewScriptFile
->Argv
[0], FALSE
, FALSE
);
2694 ASSERT_EFI_ERROR(Status
);
2700 Status
= ShellCopySearchAndReplace(CommandLine2
, CommandLine
, PcdGet16 (PcdShellPrintBufferSize
), L
"%1", L
"\"\"", FALSE
, FALSE
);
2701 Status
= ShellCopySearchAndReplace(CommandLine
, CommandLine2
, PcdGet16 (PcdShellPrintBufferSize
), L
"%2", L
"\"\"", FALSE
, FALSE
);
2702 Status
= ShellCopySearchAndReplace(CommandLine2
, CommandLine
, PcdGet16 (PcdShellPrintBufferSize
), L
"%3", L
"\"\"", FALSE
, FALSE
);
2703 Status
= ShellCopySearchAndReplace(CommandLine
, CommandLine2
, PcdGet16 (PcdShellPrintBufferSize
), L
"%4", L
"\"\"", FALSE
, FALSE
);
2704 Status
= ShellCopySearchAndReplace(CommandLine2
, CommandLine
, PcdGet16 (PcdShellPrintBufferSize
), L
"%5", L
"\"\"", FALSE
, FALSE
);
2705 Status
= ShellCopySearchAndReplace(CommandLine
, CommandLine2
, PcdGet16 (PcdShellPrintBufferSize
), L
"%6", L
"\"\"", FALSE
, FALSE
);
2706 Status
= ShellCopySearchAndReplace(CommandLine2
, CommandLine
, PcdGet16 (PcdShellPrintBufferSize
), L
"%7", L
"\"\"", FALSE
, FALSE
);
2707 Status
= ShellCopySearchAndReplace(CommandLine
, CommandLine2
, PcdGet16 (PcdShellPrintBufferSize
), L
"%8", L
"\"\"", FALSE
, FALSE
);
2708 Status
= ShellCopySearchAndReplace(CommandLine2
, CommandLine
, PcdGet16 (PcdShellPrintBufferSize
), L
"%9", L
"\"\"", FALSE
, FALSE
);
2710 StrnCpy(CommandLine2
, CommandLine
, PcdGet16(PcdShellPrintBufferSize
)/sizeof(CHAR16
)-1);
2712 LastCommand
= NewScriptFile
->CurrentCommand
;
2714 for (CommandLine3
= CommandLine2
; CommandLine3
[0] == L
' ' ; CommandLine3
++);
2716 if (CommandLine3
!= NULL
&& CommandLine3
[0] == L
':' ) {
2718 // This line is a goto target / label
2721 if (CommandLine3
!= NULL
&& StrLen(CommandLine3
) > 0) {
2722 if (CommandLine3
[0] == L
'@') {
2724 // We need to save the current echo state
2725 // and disable echo for just this command.
2727 PreCommandEchoState
= ShellCommandGetEchoState();
2728 ShellCommandSetEchoState(FALSE
);
2729 Status
= RunCommand(CommandLine3
+1);
2732 // If command was "@echo -off" or "@echo -on" then don't restore echo state
2734 if (StrCmp (L
"@echo -off", CommandLine3
) != 0 &&
2735 StrCmp (L
"@echo -on", CommandLine3
) != 0) {
2737 // Now restore the pre-'@' echo state.
2739 ShellCommandSetEchoState(PreCommandEchoState
);
2742 if (ShellCommandGetEchoState()) {
2743 CurDir
= ShellInfoObject
.NewEfiShellProtocol
->GetEnv(L
"cwd");
2744 if (CurDir
!= NULL
&& StrLen(CurDir
) > 1) {
2745 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_CURDIR
), ShellInfoObject
.HiiHandle
, CurDir
);
2747 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_SHELL
), ShellInfoObject
.HiiHandle
);
2749 ShellPrintEx(-1, -1, L
"%s\r\n", CommandLine2
);
2751 Status
= RunCommand(CommandLine3
);
2755 if (ShellCommandGetScriptExit()) {
2757 // ShellCommandGetExitCode() always returns a UINT64
2759 UnicodeSPrint(LeString
, sizeof(LeString
), L
"0x%Lx", ShellCommandGetExitCode());
2760 DEBUG_CODE(InternalEfiShellSetEnv(L
"debuglasterror", LeString
, TRUE
););
2761 InternalEfiShellSetEnv(L
"lasterror", LeString
, TRUE
);
2763 ShellCommandRegisterExit(FALSE
, 0);
2764 Status
= EFI_SUCCESS
;
2767 if (ShellGetExecutionBreakFlag()) {
2770 if (EFI_ERROR(Status
)) {
2773 if (ShellCommandGetExit()) {
2778 // If that commend did not update the CurrentCommand then we need to advance it...
2780 if (LastCommand
== NewScriptFile
->CurrentCommand
) {
2781 NewScriptFile
->CurrentCommand
= (SCRIPT_COMMAND_LIST
*)GetNextNode(&NewScriptFile
->CommandList
, &NewScriptFile
->CurrentCommand
->Link
);
2782 if (!IsNull(&NewScriptFile
->CommandList
, &NewScriptFile
->CurrentCommand
->Link
)) {
2783 NewScriptFile
->CurrentCommand
->Reset
= TRUE
;
2787 NewScriptFile
->CurrentCommand
= (SCRIPT_COMMAND_LIST
*)GetNextNode(&NewScriptFile
->CommandList
, &NewScriptFile
->CurrentCommand
->Link
);
2788 if (!IsNull(&NewScriptFile
->CommandList
, &NewScriptFile
->CurrentCommand
->Link
)) {
2789 NewScriptFile
->CurrentCommand
->Reset
= TRUE
;
2795 FreePool(CommandLine
);
2796 FreePool(CommandLine2
);
2797 ShellCommandSetNewScript (NULL
);
2800 // Only if this was the last script reset the state.
2802 if (ShellCommandGetCurrentScriptFile()==NULL
) {
2803 ShellCommandSetEchoState(PreScriptEchoState
);
2805 return (EFI_SUCCESS
);
2809 Function to process a NSH script file.
2811 @param[in] ScriptPath Pointer to the script file name (including file system path).
2812 @param[in] Handle the handle of the script file already opened.
2813 @param[in] CmdLine the command line to run.
2814 @param[in] ParamProtocol the shell parameters protocol pointer
2816 @retval EFI_SUCCESS the script completed sucessfully
2821 IN CONST CHAR16
*ScriptPath
,
2822 IN SHELL_FILE_HANDLE Handle OPTIONAL
,
2823 IN CONST CHAR16
*CmdLine
,
2824 IN EFI_SHELL_PARAMETERS_PROTOCOL
*ParamProtocol
2828 SHELL_FILE_HANDLE FileHandle
;
2832 if (ShellIsFile(ScriptPath
) != EFI_SUCCESS
) {
2833 return (EFI_INVALID_PARAMETER
);
2837 // get the argc and argv updated for scripts
2839 Status
= UpdateArgcArgv(ParamProtocol
, CmdLine
, &Argv
, &Argc
);
2840 if (!EFI_ERROR(Status
)) {
2842 if (Handle
== NULL
) {
2846 Status
= ShellOpenFileByName(ScriptPath
, &FileHandle
, EFI_FILE_MODE_READ
, 0);
2847 if (!EFI_ERROR(Status
)) {
2851 Status
= RunScriptFileHandle(FileHandle
, ScriptPath
);
2854 // now close the file
2856 ShellCloseFile(&FileHandle
);
2859 Status
= RunScriptFileHandle(Handle
, ScriptPath
);
2864 // This is guarenteed to be called after UpdateArgcArgv no matter what else happened.
2865 // This is safe even if the update API failed. In this case, it may be a no-op.
2867 RestoreArgcArgv(ParamProtocol
, &Argv
, &Argc
);