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 Find a command line contains a split operation
106 @param[in] CmdLine The command line to parse.
108 @retval A pointer to the | character in CmdLine or NULL if not present.
113 IN CONST CHAR16
*CmdLine
116 CONST CHAR16
*TempSpot
;
118 if (StrStr(CmdLine
, L
"|") != NULL
) {
119 for (TempSpot
= CmdLine
; TempSpot
!= NULL
&& *TempSpot
!= CHAR_NULL
; TempSpot
++) {
120 if (*TempSpot
== L
'^' && *(TempSpot
+1) == L
'|') {
122 } else if (*TempSpot
== L
'|') {
131 Determine if a command line contains a split operation
133 @param[in] CmdLine The command line to parse.
135 @retval TRUE CmdLine has a valid split.
136 @retval FALSE CmdLine does not have a valid split.
141 IN CONST CHAR16
*CmdLine
144 CONST CHAR16
*TempSpot
;
145 TempSpot
= FindSplit(CmdLine
);
146 return (BOOLEAN
) ((TempSpot
!= NULL
) && (*TempSpot
!= CHAR_NULL
));
150 Function to start monitoring for CTRL-S using SimpleTextInputEx. This
151 feature's enabled state was not known when the shell initially launched.
153 @retval EFI_SUCCESS The feature is enabled.
154 @retval EFI_OUT_OF_RESOURCES There is not enough mnemory available.
158 InternalEfiShellStartCtrlSMonitor(
162 EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL
*SimpleEx
;
163 EFI_KEY_DATA KeyData
;
166 Status
= gBS
->OpenProtocol(
167 gST
->ConsoleInHandle
,
168 &gEfiSimpleTextInputExProtocolGuid
,
172 EFI_OPEN_PROTOCOL_GET_PROTOCOL
);
173 if (EFI_ERROR(Status
)) {
178 STRING_TOKEN (STR_SHELL_NO_IN_EX
),
179 ShellInfoObject
.HiiHandle
);
180 return (EFI_SUCCESS
);
183 KeyData
.KeyState
.KeyToggleState
= 0;
184 KeyData
.Key
.ScanCode
= 0;
185 KeyData
.KeyState
.KeyShiftState
= EFI_SHIFT_STATE_VALID
|EFI_LEFT_CONTROL_PRESSED
;
186 KeyData
.Key
.UnicodeChar
= L
's';
188 Status
= SimpleEx
->RegisterKeyNotify(
191 NotificationFunction
,
192 &ShellInfoObject
.CtrlSNotifyHandle1
);
194 KeyData
.KeyState
.KeyShiftState
= EFI_SHIFT_STATE_VALID
|EFI_RIGHT_CONTROL_PRESSED
;
195 if (!EFI_ERROR(Status
)) {
196 Status
= SimpleEx
->RegisterKeyNotify(
199 NotificationFunction
,
200 &ShellInfoObject
.CtrlSNotifyHandle2
);
202 KeyData
.KeyState
.KeyShiftState
= EFI_SHIFT_STATE_VALID
|EFI_LEFT_CONTROL_PRESSED
;
203 KeyData
.Key
.UnicodeChar
= 19;
205 if (!EFI_ERROR(Status
)) {
206 Status
= SimpleEx
->RegisterKeyNotify(
209 NotificationFunction
,
210 &ShellInfoObject
.CtrlSNotifyHandle3
);
212 KeyData
.KeyState
.KeyShiftState
= EFI_SHIFT_STATE_VALID
|EFI_RIGHT_CONTROL_PRESSED
;
213 if (!EFI_ERROR(Status
)) {
214 Status
= SimpleEx
->RegisterKeyNotify(
217 NotificationFunction
,
218 &ShellInfoObject
.CtrlSNotifyHandle4
);
226 The entry point for the application.
228 @param[in] ImageHandle The firmware allocated handle for the EFI image.
229 @param[in] SystemTable A pointer to the EFI System Table.
231 @retval EFI_SUCCESS The entry point is executed successfully.
232 @retval other Some error occurs when executing this entry point.
238 IN EFI_HANDLE ImageHandle
,
239 IN EFI_SYSTEM_TABLE
*SystemTable
245 EFI_HANDLE ConInHandle
;
246 EFI_SIMPLE_TEXT_INPUT_PROTOCOL
*OldConIn
;
249 SHELL_STATUS ExitStatus
;
251 if (PcdGet8(PcdShellSupportLevel
) > 3) {
252 return (EFI_UNSUPPORTED
);
258 Status
= gST
->ConOut
->ClearScreen(gST
->ConOut
);
259 if (EFI_ERROR(Status
)) {
264 // Populate the global structure from PCDs
266 ShellInfoObject
.ImageDevPath
= NULL
;
267 ShellInfoObject
.FileDevPath
= NULL
;
268 ShellInfoObject
.PageBreakEnabled
= PcdGetBool(PcdShellPageBreakDefault
);
269 ShellInfoObject
.ViewingSettings
.InsertMode
= PcdGetBool(PcdShellInsertModeDefault
);
270 ShellInfoObject
.LogScreenCount
= PcdGet8 (PcdShellScreenLogCount
);
273 // verify we dont allow for spec violation
275 ASSERT(ShellInfoObject
.LogScreenCount
>= 3);
278 // Initialize the LIST ENTRY objects...
280 InitializeListHead(&ShellInfoObject
.BufferToFreeList
.Link
);
281 InitializeListHead(&ShellInfoObject
.ViewingSettings
.CommandHistory
.Link
);
282 InitializeListHead(&ShellInfoObject
.SplitList
.Link
);
285 // Check PCDs for optional features that are not implemented yet.
287 if ( PcdGetBool(PcdShellSupportOldProtocols
)
288 || !FeaturePcdGet(PcdShellRequireHiiPlatform
)
289 || FeaturePcdGet(PcdShellSupportFrameworkHii
)
291 return (EFI_UNSUPPORTED
);
295 // turn off the watchdog timer
297 gBS
->SetWatchdogTimer (0, 0, 0, NULL
);
300 // install our console logger. This will keep a log of the output for back-browsing
302 Status
= ConsoleLoggerInstall(ShellInfoObject
.LogScreenCount
, &ShellInfoObject
.ConsoleInfo
);
303 if(EFI_ERROR (Status
)) {
304 ExitStatus
= (SHELL_STATUS
) (Status
& (~MAX_BIT
));
306 ExitStatus
= SHELL_SUCCESS
;
309 if (!EFI_ERROR(Status
)) {
311 // Enable the cursor to be visible
313 gST
->ConOut
->EnableCursor (gST
->ConOut
, TRUE
);
316 // If supporting EFI 1.1 we need to install HII protocol
317 // only do this if PcdShellRequireHiiPlatform == FALSE
319 // remove EFI_UNSUPPORTED check above when complete.
320 ///@todo add support for Framework HII
323 // install our (solitary) HII package
325 ShellInfoObject
.HiiHandle
= HiiAddPackages (&gEfiCallerIdGuid
, gImageHandle
, ShellStrings
, NULL
);
326 if (ShellInfoObject
.HiiHandle
== NULL
) {
327 if (PcdGetBool(PcdShellSupportFrameworkHii
)) {
328 ///@todo Add our package into Framework HII
330 if (ShellInfoObject
.HiiHandle
== NULL
) {
331 Status
= EFI_NOT_STARTED
;
337 // create and install the EfiShellParametersProtocol
339 Status
= CreatePopulateInstallShellParametersProtocol(&ShellInfoObject
.NewShellParametersProtocol
, &ShellInfoObject
.RootShellInstance
);
340 ASSERT_EFI_ERROR(Status
);
341 ASSERT(ShellInfoObject
.NewShellParametersProtocol
!= NULL
);
344 // create and install the EfiShellProtocol
346 Status
= CreatePopulateInstallShellProtocol(&ShellInfoObject
.NewEfiShellProtocol
);
347 ASSERT_EFI_ERROR(Status
);
348 ASSERT(ShellInfoObject
.NewEfiShellProtocol
!= NULL
);
351 // Now initialize the shell library (it requires Shell Parameters protocol)
353 Status
= ShellInitialize();
354 ASSERT_EFI_ERROR(Status
);
356 Status
= CommandInit();
357 ASSERT_EFI_ERROR(Status
);
360 // Check the command line
362 Status
= ProcessCommandLine ();
363 if (EFI_ERROR (Status
)) {
368 // If shell support level is >= 1 create the mappings and paths
370 if (PcdGet8(PcdShellSupportLevel
) >= 1) {
371 Status
= ShellCommandCreateInitialMappingsAndPaths();
375 // save the device path for the loaded image and the device path for the filepath (under loaded image)
376 // These are where to look for the startup.nsh file
378 Status
= GetDevicePathsForImageAndFile(&ShellInfoObject
.ImageDevPath
, &ShellInfoObject
.FileDevPath
);
379 ASSERT_EFI_ERROR(Status
);
382 // Display the version
384 if (!ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoVersion
) {
387 gST
->ConOut
->Mode
->CursorRow
,
389 STRING_TOKEN (STR_VER_OUTPUT_MAIN_SHELL
),
390 ShellInfoObject
.HiiHandle
,
391 SupportLevel
[PcdGet8(PcdShellSupportLevel
)],
392 gEfiShellProtocol
->MajorVersion
,
393 gEfiShellProtocol
->MinorVersion
400 STRING_TOKEN (STR_VER_OUTPUT_MAIN_SUPPLIER
),
401 ShellInfoObject
.HiiHandle
,
402 (CHAR16
*) PcdGetPtr (PcdShellSupplier
)
409 STRING_TOKEN (STR_VER_OUTPUT_MAIN_UEFI
),
410 ShellInfoObject
.HiiHandle
,
411 (gST
->Hdr
.Revision
&0xffff0000)>>16,
412 (gST
->Hdr
.Revision
&0x0000ffff),
414 gST
->FirmwareRevision
419 // Display the mapping
421 if (PcdGet8(PcdShellSupportLevel
) >= 2 && !ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoMap
) {
422 Status
= RunCommand(L
"map", NULL
);
423 ASSERT_EFI_ERROR(Status
);
427 // init all the built in alias'
429 Status
= SetBuiltInAlias();
430 ASSERT_EFI_ERROR(Status
);
433 // Initialize environment variables
435 if (ShellCommandGetProfileList() != NULL
) {
436 Status
= InternalEfiShellSetEnv(L
"profiles", ShellCommandGetProfileList(), TRUE
);
437 ASSERT_EFI_ERROR(Status
);
441 TempString
= AllocateZeroPool(Size
);
443 UnicodeSPrint(TempString
, Size
, L
"%d", PcdGet8(PcdShellSupportLevel
));
444 Status
= InternalEfiShellSetEnv(L
"uefishellsupport", TempString
, TRUE
);
445 ASSERT_EFI_ERROR(Status
);
447 UnicodeSPrint(TempString
, Size
, L
"%d.%d", ShellInfoObject
.NewEfiShellProtocol
->MajorVersion
, ShellInfoObject
.NewEfiShellProtocol
->MinorVersion
);
448 Status
= InternalEfiShellSetEnv(L
"uefishellversion", TempString
, TRUE
);
449 ASSERT_EFI_ERROR(Status
);
451 UnicodeSPrint(TempString
, Size
, L
"%d.%d", (gST
->Hdr
.Revision
& 0xFFFF0000) >> 16, gST
->Hdr
.Revision
& 0x0000FFFF);
452 Status
= InternalEfiShellSetEnv(L
"uefiversion", TempString
, TRUE
);
453 ASSERT_EFI_ERROR(Status
);
455 FreePool(TempString
);
457 if (!EFI_ERROR(Status
)) {
458 if (!ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoInterrupt
) {
460 // Set up the event for CTRL-C monitoring...
462 Status
= InernalEfiShellStartMonitor();
465 if (!EFI_ERROR(Status
) && !ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoConsoleIn
) {
467 // Set up the event for CTRL-S monitoring...
469 Status
= InternalEfiShellStartCtrlSMonitor();
472 if (!EFI_ERROR(Status
) && ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoConsoleIn
) {
474 // close off the gST->ConIn
476 OldConIn
= gST
->ConIn
;
477 ConInHandle
= gST
->ConsoleInHandle
;
478 gST
->ConIn
= CreateSimpleTextInOnFile((SHELL_FILE_HANDLE
)&FileInterfaceNulFile
, &gST
->ConsoleInHandle
);
484 if (!EFI_ERROR(Status
) && PcdGet8(PcdShellSupportLevel
) >= 1) {
486 // process the startup script or launch the called app.
488 Status
= DoStartupScript(
489 ShellInfoObject
.ImageDevPath
,
490 ShellInfoObject
.FileDevPath
,
495 if (!ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.Exit
&& !ShellCommandGetExit() && (PcdGet8(PcdShellSupportLevel
) >= 3 || PcdGetBool(PcdShellForceConsole
)) && !EFI_ERROR(Status
) && !ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoConsoleIn
) {
497 // begin the UI waiting loop
501 // clean out all the memory allocated for CONST <something> * return values
502 // between each shell prompt presentation
504 if (!IsListEmpty(&ShellInfoObject
.BufferToFreeList
.Link
)){
505 FreeBufferList(&ShellInfoObject
.BufferToFreeList
);
509 // Reset page break back to default.
511 ShellInfoObject
.PageBreakEnabled
= PcdGetBool(PcdShellPageBreakDefault
);
512 ASSERT (ShellInfoObject
.ConsoleInfo
!= NULL
);
513 ShellInfoObject
.ConsoleInfo
->Enabled
= TRUE
;
514 ShellInfoObject
.ConsoleInfo
->RowCounter
= 0;
517 // Reset the CTRL-C event (yes we ignore the return values)
519 Status
= gBS
->CheckEvent (ShellInfoObject
.NewEfiShellProtocol
->ExecutionBreak
);
524 Status
= DoShellPrompt();
525 } while (!ShellCommandGetExit());
526 ExitStatus
= (SHELL_STATUS
) ShellCommandGetExitCode();
528 if (OldConIn
!= NULL
&& ConInHandle
!= NULL
) {
529 CloseSimpleTextInOnFile (gST
->ConIn
);
530 gST
->ConIn
= OldConIn
;
531 gST
->ConsoleInHandle
= ConInHandle
;
538 // uninstall protocols / free memory / etc...
540 if (ShellInfoObject
.UserBreakTimer
!= NULL
) {
541 gBS
->CloseEvent(ShellInfoObject
.UserBreakTimer
);
542 DEBUG_CODE(ShellInfoObject
.UserBreakTimer
= NULL
;);
544 if (ShellInfoObject
.ImageDevPath
!= NULL
) {
545 FreePool(ShellInfoObject
.ImageDevPath
);
546 DEBUG_CODE(ShellInfoObject
.ImageDevPath
= NULL
;);
548 if (ShellInfoObject
.FileDevPath
!= NULL
) {
549 FreePool(ShellInfoObject
.FileDevPath
);
550 DEBUG_CODE(ShellInfoObject
.FileDevPath
= NULL
;);
552 if (ShellInfoObject
.NewShellParametersProtocol
!= NULL
) {
553 CleanUpShellParametersProtocol(ShellInfoObject
.NewShellParametersProtocol
);
554 DEBUG_CODE(ShellInfoObject
.NewShellParametersProtocol
= NULL
;);
556 if (ShellInfoObject
.NewEfiShellProtocol
!= NULL
){
557 if (ShellInfoObject
.NewEfiShellProtocol
->IsRootShell()){
558 InternalEfiShellSetEnv(L
"cwd", NULL
, TRUE
);
560 CleanUpShellProtocol(ShellInfoObject
.NewEfiShellProtocol
);
561 DEBUG_CODE(ShellInfoObject
.NewEfiShellProtocol
= NULL
;);
564 if (!IsListEmpty(&ShellInfoObject
.BufferToFreeList
.Link
)){
565 FreeBufferList(&ShellInfoObject
.BufferToFreeList
);
568 if (!IsListEmpty(&ShellInfoObject
.SplitList
.Link
)){
569 ASSERT(FALSE
); ///@todo finish this de-allocation.
572 if (ShellInfoObject
.ShellInitSettings
.FileName
!= NULL
) {
573 FreePool(ShellInfoObject
.ShellInitSettings
.FileName
);
574 DEBUG_CODE(ShellInfoObject
.ShellInitSettings
.FileName
= NULL
;);
577 if (ShellInfoObject
.ShellInitSettings
.FileOptions
!= NULL
) {
578 FreePool(ShellInfoObject
.ShellInitSettings
.FileOptions
);
579 DEBUG_CODE(ShellInfoObject
.ShellInitSettings
.FileOptions
= NULL
;);
582 if (ShellInfoObject
.HiiHandle
!= NULL
) {
583 HiiRemovePackages(ShellInfoObject
.HiiHandle
);
584 DEBUG_CODE(ShellInfoObject
.HiiHandle
= NULL
;);
587 if (!IsListEmpty(&ShellInfoObject
.ViewingSettings
.CommandHistory
.Link
)){
588 FreeBufferList(&ShellInfoObject
.ViewingSettings
.CommandHistory
);
591 ASSERT(ShellInfoObject
.ConsoleInfo
!= NULL
);
592 if (ShellInfoObject
.ConsoleInfo
!= NULL
) {
593 ConsoleLoggerUninstall(ShellInfoObject
.ConsoleInfo
);
594 FreePool(ShellInfoObject
.ConsoleInfo
);
595 DEBUG_CODE(ShellInfoObject
.ConsoleInfo
= NULL
;);
598 if (!EFI_ERROR (Status
)) {
599 // If the command exited with an error, we pass this error out in the ExitData
600 // so that it can be retrieved by the EfiShellExecute function (which may
601 // start the shell with gBS->StartImage)
602 if (ExitStatus
!= SHELL_SUCCESS
) {
603 // Allocate a buffer for exit data to pass to gBS->Exit().
604 // This buffer will contain the empty string immediately followed by
605 // the shell's exit status. (The empty string is required by the UEFI spec)
606 ExitDataSize
= (sizeof (CHAR16
) + sizeof (SHELL_STATUS
));
607 ExitData
= AllocatePool (ExitDataSize
);
608 if (ExitData
== NULL
) {
609 return EFI_OUT_OF_RESOURCES
;
612 // Use CopyMem to avoid alignment faults
613 CopyMem ((ExitData
+ 1), &ExitStatus
, sizeof (ExitStatus
));
615 gBS
->Exit (ImageHandle
, EFI_ABORTED
, ExitDataSize
, ExitData
);
628 Sets all the alias' that were registered with the ShellCommandLib library.
630 @retval EFI_SUCCESS all init commands were run sucessfully.
638 CONST ALIAS_LIST
*List
;
642 // Get all the commands we want to run
644 List
= ShellCommandGetInitAliasList();
647 // for each command in the List
649 for ( Node
= (ALIAS_LIST
*)GetFirstNode(&List
->Link
)
650 ; !IsNull (&List
->Link
, &Node
->Link
)
651 ; Node
= (ALIAS_LIST
*)GetNextNode(&List
->Link
, &Node
->Link
)
654 // install the alias'
656 Status
= InternalSetAlias(Node
->CommandString
, Node
->Alias
, TRUE
);
657 ASSERT_EFI_ERROR(Status
);
659 return (EFI_SUCCESS
);
663 Internal function to determine if 2 command names are really the same.
665 @param[in] Command1 The pointer to the first command name.
666 @param[in] Command2 The pointer to the second command name.
668 @retval TRUE The 2 command names are the same.
669 @retval FALSE The 2 command names are not the same.
674 IN CONST CHAR16
*Command1
,
675 IN CONST CHAR16
*Command2
678 if (StringNoCaseCompare(&Command1
, &Command2
) == 0) {
685 Internal function to determine if a command is a script only command.
687 @param[in] CommandName The pointer to the command name.
689 @retval TRUE The command is a script only command.
690 @retval FALSE The command is not a script only command.
695 IN CONST CHAR16
*CommandName
698 if (IsCommand(CommandName
, L
"for")
699 ||IsCommand(CommandName
, L
"endfor")
700 ||IsCommand(CommandName
, L
"if")
701 ||IsCommand(CommandName
, L
"else")
702 ||IsCommand(CommandName
, L
"endif")
703 ||IsCommand(CommandName
, L
"goto")) {
710 This function will populate the 2 device path protocol parameters based on the
711 global gImageHandle. The DevPath will point to the device path for the handle that has
712 loaded image protocol installed on it. The FilePath will point to the device path
713 for the file that was loaded.
715 @param[in, out] DevPath On a sucessful return the device path to the loaded image.
716 @param[in, out] FilePath On a sucessful return the device path to the file.
718 @retval EFI_SUCCESS The 2 device paths were sucessfully returned.
719 @retval other A error from gBS->HandleProtocol.
725 GetDevicePathsForImageAndFile (
726 IN OUT EFI_DEVICE_PATH_PROTOCOL
**DevPath
,
727 IN OUT EFI_DEVICE_PATH_PROTOCOL
**FilePath
731 EFI_LOADED_IMAGE_PROTOCOL
*LoadedImage
;
732 EFI_DEVICE_PATH_PROTOCOL
*ImageDevicePath
;
734 ASSERT(DevPath
!= NULL
);
735 ASSERT(FilePath
!= NULL
);
737 Status
= gBS
->OpenProtocol (
739 &gEfiLoadedImageProtocolGuid
,
740 (VOID
**)&LoadedImage
,
743 EFI_OPEN_PROTOCOL_GET_PROTOCOL
745 if (!EFI_ERROR (Status
)) {
746 Status
= gBS
->OpenProtocol (
747 LoadedImage
->DeviceHandle
,
748 &gEfiDevicePathProtocolGuid
,
749 (VOID
**)&ImageDevicePath
,
752 EFI_OPEN_PROTOCOL_GET_PROTOCOL
754 if (!EFI_ERROR (Status
)) {
755 *DevPath
= DuplicateDevicePath (ImageDevicePath
);
756 *FilePath
= DuplicateDevicePath (LoadedImage
->FilePath
);
758 LoadedImage
->DeviceHandle
,
759 &gEfiDevicePathProtocolGuid
,
765 &gEfiLoadedImageProtocolGuid
,
773 Process all Uefi Shell 2.0 command line options.
775 see Uefi Shell 2.0 section 3.2 for full details.
777 the command line must resemble the following:
779 shell.efi [ShellOpt-options] [options] [file-name [file-name-options]]
781 ShellOpt-options Options which control the initialization behavior of the shell.
782 These options are read from the EFI global variable "ShellOpt"
783 and are processed before options or file-name.
785 options Options which control the initialization behavior of the shell.
787 file-name The name of a UEFI shell application or script to be executed
788 after initialization is complete. By default, if file-name is
789 specified, then -nostartup is implied. Scripts are not supported
792 file-name-options The command-line options that are passed to file-name when it
795 This will initialize the ShellInfoObject.ShellInitSettings global variable.
797 @retval EFI_SUCCESS The variable is initialized.
808 CHAR16
*DelayValueStr
;
811 EFI_UNICODE_COLLATION_PROTOCOL
*UnicodeCollation
;
813 // `file-name-options` will contain arguments to `file-name` that we don't
814 // know about. This would cause ShellCommandLineParse to error, so we parse
815 // arguments manually, ignoring those after the first thing that doesn't look
816 // like a shell option (which is assumed to be `file-name`).
818 Status
= gBS
->LocateProtocol (
819 &gEfiUnicodeCollationProtocolGuid
,
821 (VOID
**) &UnicodeCollation
823 if (EFI_ERROR (Status
)) {
827 // Set default options
828 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.Startup
= FALSE
;
829 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoStartup
= FALSE
;
830 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoConsoleOut
= FALSE
;
831 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoConsoleIn
= FALSE
;
832 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoInterrupt
= FALSE
;
833 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoMap
= FALSE
;
834 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoVersion
= FALSE
;
835 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.Delay
= FALSE
;
836 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.Exit
= FALSE
;
837 ShellInfoObject
.ShellInitSettings
.Delay
= 5;
840 // Start LoopVar at 0 to parse only optional arguments at Argv[0]
841 // and parse other parameters from Argv[1]. This is for use case that
842 // UEFI Shell boot option is created, and OptionalData is provided
843 // that starts with shell command-line options.
845 for (LoopVar
= 0 ; LoopVar
< gEfiShellParametersProtocol
->Argc
; LoopVar
++) {
846 CurrentArg
= gEfiShellParametersProtocol
->Argv
[LoopVar
];
847 if (UnicodeCollation
->StriColl (
852 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.Startup
= TRUE
;
854 else if (UnicodeCollation
->StriColl (
859 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoStartup
= TRUE
;
861 else if (UnicodeCollation
->StriColl (
866 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoConsoleOut
= TRUE
;
868 else if (UnicodeCollation
->StriColl (
873 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoConsoleIn
= TRUE
;
875 else if (UnicodeCollation
->StriColl (
880 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoInterrupt
= TRUE
;
882 else if (UnicodeCollation
->StriColl (
887 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoMap
= TRUE
;
889 else if (UnicodeCollation
->StriColl (
894 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoVersion
= TRUE
;
896 else if (UnicodeCollation
->StriColl (
901 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.Delay
= TRUE
;
902 // Check for optional delay value following "-delay"
903 DelayValueStr
= gEfiShellParametersProtocol
->Argv
[LoopVar
+ 1];
904 if (DelayValueStr
!= NULL
){
905 if (*DelayValueStr
== L
':') {
908 if (!EFI_ERROR(ShellConvertStringToUint64 (
914 ShellInfoObject
.ShellInitSettings
.Delay
= (UINTN
)DelayValue
;
918 } else if (UnicodeCollation
->StriColl (
923 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.Exit
= TRUE
;
924 } else if (StrnCmp (L
"-", CurrentArg
, 1) == 0) {
925 // Unrecognised option
926 ShellPrintHiiEx(-1, -1, NULL
,
927 STRING_TOKEN (STR_GEN_PROBLEM
),
928 ShellInfoObject
.HiiHandle
,
931 return EFI_INVALID_PARAMETER
;
934 // First argument should be Shell.efi image name
940 ShellInfoObject
.ShellInitSettings
.FileName
= AllocateZeroPool(StrSize(CurrentArg
));
941 if (ShellInfoObject
.ShellInitSettings
.FileName
== NULL
) {
942 return (EFI_OUT_OF_RESOURCES
);
945 // We found `file-name`.
947 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoStartup
= 1;
949 StrCpy (ShellInfoObject
.ShellInitSettings
.FileName
, CurrentArg
);
952 // Add `file-name-options`
953 for (Size
= 0 ; LoopVar
< gEfiShellParametersProtocol
->Argc
; LoopVar
++) {
954 ASSERT((ShellInfoObject
.ShellInitSettings
.FileOptions
== NULL
&& Size
== 0) || (ShellInfoObject
.ShellInitSettings
.FileOptions
!= NULL
));
955 StrnCatGrow(&ShellInfoObject
.ShellInitSettings
.FileOptions
,
959 if (ShellInfoObject
.ShellInitSettings
.FileOptions
== NULL
) {
960 SHELL_FREE_NON_NULL(ShellInfoObject
.ShellInitSettings
.FileName
);
961 return (EFI_OUT_OF_RESOURCES
);
963 StrnCatGrow(&ShellInfoObject
.ShellInitSettings
.FileOptions
,
965 gEfiShellParametersProtocol
->Argv
[LoopVar
],
967 if (ShellInfoObject
.ShellInitSettings
.FileOptions
== NULL
) {
968 SHELL_FREE_NON_NULL(ShellInfoObject
.ShellInitSettings
.FileName
);
969 return (EFI_OUT_OF_RESOURCES
);
975 // "-nointerrupt" overrides "-delay"
976 if (ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoInterrupt
) {
977 ShellInfoObject
.ShellInitSettings
.Delay
= 0;
984 Handles all interaction with the default startup script.
986 this will check that the correct command line parameters were passed, handle the delay, and then start running the script.
988 @param ImagePath the path to the image for shell. first place to look for the startup script
989 @param FilePath the path to the file for shell. second place to look for the startup script.
991 @param[out] ExitStatus The exit code of the script. Ignored if NULL.
993 @retval EFI_SUCCESS the variable is initialized.
998 IN EFI_DEVICE_PATH_PROTOCOL
*ImagePath
,
999 IN EFI_DEVICE_PATH_PROTOCOL
*FilePath
,
1000 OUT SHELL_STATUS
*ExitStatus
1006 SHELL_FILE_HANDLE FileHandle
;
1007 EFI_DEVICE_PATH_PROTOCOL
*NewPath
;
1008 EFI_DEVICE_PATH_PROTOCOL
*NamePath
;
1009 CHAR16
*FileStringPath
;
1012 CONST CHAR16
*MapName
;
1014 Key
.UnicodeChar
= CHAR_NULL
;
1018 if (!ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.Startup
&& ShellInfoObject
.ShellInitSettings
.FileName
!= NULL
) {
1020 // launch something else instead
1022 NewSize
= StrSize(ShellInfoObject
.ShellInitSettings
.FileName
);
1023 if (ShellInfoObject
.ShellInitSettings
.FileOptions
!= NULL
) {
1024 NewSize
+= StrSize(ShellInfoObject
.ShellInitSettings
.FileOptions
) + sizeof(CHAR16
);
1026 FileStringPath
= AllocateZeroPool(NewSize
);
1027 if (FileStringPath
== NULL
) {
1028 return (EFI_OUT_OF_RESOURCES
);
1030 StrCpy(FileStringPath
, ShellInfoObject
.ShellInitSettings
.FileName
);
1031 if (ShellInfoObject
.ShellInitSettings
.FileOptions
!= NULL
) {
1032 StrCat(FileStringPath
, L
" ");
1033 StrCat(FileStringPath
, ShellInfoObject
.ShellInitSettings
.FileOptions
);
1035 Status
= RunCommand(FileStringPath
, ExitStatus
);
1036 FreePool(FileStringPath
);
1042 // for shell level 0 we do no scripts
1043 // Without the Startup bit overriding we allow for nostartup to prevent scripts
1045 if ( (PcdGet8(PcdShellSupportLevel
) < 1)
1046 || (ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoStartup
&& !ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.Startup
)
1048 return (EFI_SUCCESS
);
1051 gST
->ConOut
->EnableCursor(gST
->ConOut
, FALSE
);
1053 // print out our warning and see if they press a key
1055 for ( Status
= EFI_UNSUPPORTED
, Delay
= ShellInfoObject
.ShellInitSettings
.Delay
1056 ; Delay
!= 0 && EFI_ERROR(Status
)
1059 ShellPrintHiiEx(0, gST
->ConOut
->Mode
->CursorRow
, NULL
, STRING_TOKEN (STR_SHELL_STARTUP_QUESTION
), ShellInfoObject
.HiiHandle
, Delay
);
1060 gBS
->Stall (1000000);
1061 if (!ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoConsoleIn
) {
1062 Status
= gST
->ConIn
->ReadKeyStroke (gST
->ConIn
, &Key
);
1065 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_CRLF
), ShellInfoObject
.HiiHandle
);
1066 gST
->ConOut
->EnableCursor(gST
->ConOut
, TRUE
);
1071 if (Status
== EFI_SUCCESS
&& Key
.UnicodeChar
== 0 && Key
.ScanCode
== SCAN_ESC
) {
1072 return (EFI_SUCCESS
);
1076 // Try the first location (must be file system)
1078 MapName
= ShellInfoObject
.NewEfiShellProtocol
->GetMapFromDevicePath(&ImagePath
);
1079 if (MapName
!= NULL
) {
1080 FileStringPath
= NULL
;
1082 FileStringPath
= StrnCatGrow(&FileStringPath
, &NewSize
, MapName
, 0);
1083 if (FileStringPath
== NULL
) {
1084 Status
= EFI_OUT_OF_RESOURCES
;
1086 TempSpot
= StrStr(FileStringPath
, L
";");
1087 if (TempSpot
!= NULL
) {
1088 *TempSpot
= CHAR_NULL
;
1090 FileStringPath
= StrnCatGrow(&FileStringPath
, &NewSize
, ((FILEPATH_DEVICE_PATH
*)FilePath
)->PathName
, 0);
1091 PathRemoveLastItem(FileStringPath
);
1092 FileStringPath
= StrnCatGrow(&FileStringPath
, &NewSize
, mStartupScript
, 0);
1093 Status
= ShellInfoObject
.NewEfiShellProtocol
->OpenFileByName(FileStringPath
, &FileHandle
, EFI_FILE_MODE_READ
);
1094 FreePool(FileStringPath
);
1097 if (EFI_ERROR(Status
)) {
1098 NamePath
= FileDevicePath (NULL
, mStartupScript
);
1099 NewPath
= AppendDevicePathNode (ImagePath
, NamePath
);
1105 Status
= InternalOpenFileDevicePath(NewPath
, &FileHandle
, EFI_FILE_MODE_READ
, 0);
1109 // If we got a file, run it
1111 if (!EFI_ERROR(Status
) && FileHandle
!= NULL
) {
1112 Status
= RunScriptFile (
1116 ShellInfoObject
.NewShellParametersProtocol
,
1119 ShellInfoObject
.NewEfiShellProtocol
->CloseFile(FileHandle
);
1121 FileStringPath
= ShellFindFilePath(mStartupScript
);
1122 if (FileStringPath
== NULL
) {
1124 // we return success since we dont need to have a startup script
1126 Status
= EFI_SUCCESS
;
1127 ASSERT(FileHandle
== NULL
);
1129 Status
= RunScriptFile(
1133 ShellInfoObject
.NewShellParametersProtocol
,
1136 FreePool(FileStringPath
);
1145 Function to perform the shell prompt looping. It will do a single prompt,
1146 dispatch the result, and then return. It is expected that the caller will
1147 call this function in a loop many times.
1150 @retval RETURN_ABORTED
1161 CONST CHAR16
*CurDir
;
1168 // Get screen setting to decide size of the command line buffer
1170 gST
->ConOut
->QueryMode (gST
->ConOut
, gST
->ConOut
->Mode
->Mode
, &Column
, &Row
);
1171 BufferSize
= Column
* Row
* sizeof (CHAR16
);
1172 CmdLine
= AllocateZeroPool (BufferSize
);
1173 if (CmdLine
== NULL
) {
1174 return EFI_OUT_OF_RESOURCES
;
1177 CurDir
= ShellInfoObject
.NewEfiShellProtocol
->GetEnv(L
"cwd");
1182 gST
->ConOut
->SetCursorPosition (gST
->ConOut
, 0, gST
->ConOut
->Mode
->CursorRow
);
1184 if (CurDir
!= NULL
&& StrLen(CurDir
) > 1) {
1185 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_CURDIR
), ShellInfoObject
.HiiHandle
, CurDir
);
1187 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_SHELL
), ShellInfoObject
.HiiHandle
);
1191 // Read a line from the console
1193 Status
= ShellInfoObject
.NewEfiShellProtocol
->ReadFile(ShellInfoObject
.NewShellParametersProtocol
->StdIn
, &BufferSize
, CmdLine
);
1196 // Null terminate the string and parse it
1198 if (!EFI_ERROR (Status
)) {
1199 CmdLine
[BufferSize
/ sizeof (CHAR16
)] = CHAR_NULL
;
1200 Status
= RunCommand(CmdLine
, NULL
);
1204 // Done with this command
1211 Add a buffer to the Buffer To Free List for safely returning buffers to other
1212 places without risking letting them modify internal shell information.
1214 @param Buffer Something to pass to FreePool when the shell is exiting.
1218 AddBufferToFreeList(
1222 BUFFER_LIST
*BufferListEntry
;
1224 if (Buffer
== NULL
) {
1228 BufferListEntry
= AllocateZeroPool(sizeof(BUFFER_LIST
));
1229 ASSERT(BufferListEntry
!= NULL
);
1230 BufferListEntry
->Buffer
= Buffer
;
1231 InsertTailList(&ShellInfoObject
.BufferToFreeList
.Link
, &BufferListEntry
->Link
);
1236 Add a buffer to the Line History List
1238 @param Buffer The line buffer to add.
1242 AddLineToCommandHistory(
1243 IN CONST CHAR16
*Buffer
1248 Node
= AllocateZeroPool(sizeof(BUFFER_LIST
));
1249 ASSERT(Node
!= NULL
);
1250 Node
->Buffer
= AllocateZeroPool(StrSize(Buffer
));
1251 ASSERT(Node
->Buffer
!= NULL
);
1252 StrCpy(Node
->Buffer
, Buffer
);
1254 InsertTailList(&ShellInfoObject
.ViewingSettings
.CommandHistory
.Link
, &Node
->Link
);
1258 Checks if a string is an alias for another command. If yes, then it replaces the alias name
1259 with the correct command name.
1261 @param[in, out] CommandString Upon entry the potential alias. Upon return the
1262 command name if it was an alias. If it was not
1263 an alias it will be unchanged. This function may
1264 change the buffer to fit the command name.
1266 @retval EFI_SUCCESS The name was changed.
1267 @retval EFI_SUCCESS The name was not an alias.
1268 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
1273 IN OUT CHAR16
**CommandString
1276 CONST CHAR16
*NewString
;
1278 NewString
= ShellInfoObject
.NewEfiShellProtocol
->GetAlias(*CommandString
, NULL
);
1279 if (NewString
== NULL
) {
1280 return (EFI_SUCCESS
);
1282 FreePool(*CommandString
);
1283 *CommandString
= AllocateZeroPool(StrSize(NewString
));
1284 if (*CommandString
== NULL
) {
1285 return (EFI_OUT_OF_RESOURCES
);
1287 StrCpy(*CommandString
, NewString
);
1288 return (EFI_SUCCESS
);
1292 Parse for the next instance of one string within another string. Can optionally make sure that
1293 the string was not escaped (^ character) per the shell specification.
1295 @param[in] SourceString The string to search within
1296 @param[in] FindString The string to look for
1297 @param[in] CheckForEscapeCharacter TRUE to skip escaped instances of FinfString, otherwise will return even escaped instances
1302 IN CONST CHAR16
*SourceString
,
1303 IN CONST CHAR16
*FindString
,
1304 IN CONST BOOLEAN CheckForEscapeCharacter
1308 if (SourceString
== NULL
) {
1311 Temp
= StrStr(SourceString
, FindString
);
1314 // If nothing found, or we dont care about escape characters
1316 if (Temp
== NULL
|| !CheckForEscapeCharacter
) {
1321 // If we found an escaped character, try again on the remainder of the string
1323 if ((Temp
> (SourceString
)) && *(Temp
-1) == L
'^') {
1324 return FindNextInstance(Temp
+1, FindString
, CheckForEscapeCharacter
);
1328 // we found the right character
1334 This function will eliminate unreplaced (and therefore non-found) environment variables.
1336 @param[in,out] CmdLine The command line to update.
1340 StripUnreplacedEnvironmentVariables(
1341 IN OUT CHAR16
*CmdLine
1344 CHAR16
*FirstPercent
;
1346 CHAR16
*SecondPercent
;
1347 CHAR16
*SecondQuote
;
1348 CHAR16
*CurrentLocator
;
1350 for (CurrentLocator
= CmdLine
; CurrentLocator
!= NULL
; ) {
1351 FirstQuote
= FindNextInstance(CurrentLocator
, L
"\"", TRUE
);
1352 FirstPercent
= FindNextInstance(CurrentLocator
, L
"%", TRUE
);
1353 SecondPercent
= FirstPercent
!=NULL
?FindNextInstance(FirstPercent
+1, L
"%", TRUE
):NULL
;
1354 if (FirstPercent
== NULL
|| SecondPercent
== NULL
) {
1356 // If we ever dont have 2 % we are done.
1361 if (FirstQuote
< FirstPercent
) {
1362 SecondQuote
= FirstQuote
!= NULL
?FindNextInstance(FirstQuote
+1, L
"\"", TRUE
):NULL
;
1364 // Quote is first found
1367 if (SecondQuote
< FirstPercent
) {
1369 // restart after the pair of "
1371 CurrentLocator
= SecondQuote
+ 1;
1372 } else /* FirstPercent < SecondQuote */{
1374 // Restart on the first percent
1376 CurrentLocator
= FirstPercent
;
1380 ASSERT(FirstPercent
< FirstQuote
);
1381 if (SecondPercent
< FirstQuote
) {
1382 FirstPercent
[0] = L
'\"';
1383 SecondPercent
[0] = L
'\"';
1386 // We need to remove from FirstPercent to SecondPercent
1388 CopyMem(FirstPercent
+ 1, SecondPercent
, StrSize(SecondPercent
));
1389 CurrentLocator
= FirstPercent
+ 2;
1392 ASSERT(FirstQuote
< SecondPercent
);
1393 CurrentLocator
= FirstQuote
;
1395 return (EFI_SUCCESS
);
1399 Function allocates a new command line and replaces all instances of environment
1400 variable names that are correctly preset to their values.
1402 If the return value is not NULL the memory must be caller freed.
1404 @param[in] OriginalCommandLine The original command line
1406 @retval NULL An error ocurred.
1407 @return The new command line with no environment variables present.
1411 ShellConvertVariables (
1412 IN CONST CHAR16
*OriginalCommandLine
1415 CONST CHAR16
*MasterEnvList
;
1417 CHAR16
*NewCommandLine1
;
1418 CHAR16
*NewCommandLine2
;
1422 SCRIPT_FILE
*CurrentScriptFile
;
1423 ALIAS_LIST
*AliasListNode
;
1425 ASSERT(OriginalCommandLine
!= NULL
);
1428 NewSize
= StrSize(OriginalCommandLine
);
1429 CurrentScriptFile
= ShellCommandGetCurrentScriptFile();
1432 ///@todo update this to handle the %0 - %9 for scripting only (borrow from line 1256 area) ? ? ?
1435 // calculate the size required for the post-conversion string...
1437 if (CurrentScriptFile
!= NULL
) {
1438 for (AliasListNode
= (ALIAS_LIST
*)GetFirstNode(&CurrentScriptFile
->SubstList
)
1439 ; !IsNull(&CurrentScriptFile
->SubstList
, &AliasListNode
->Link
)
1440 ; AliasListNode
= (ALIAS_LIST
*)GetNextNode(&CurrentScriptFile
->SubstList
, &AliasListNode
->Link
)
1442 for (Temp
= StrStr(OriginalCommandLine
, AliasListNode
->Alias
)
1444 ; Temp
= StrStr(Temp
+1, AliasListNode
->Alias
)
1447 // we need a preceeding and if there is space no ^ preceeding (if no space ignore)
1449 if ((((Temp
-OriginalCommandLine
)>2) && *(Temp
-2) != L
'^') || ((Temp
-OriginalCommandLine
)<=2)) {
1450 NewSize
+= StrSize(AliasListNode
->CommandString
);
1456 for (MasterEnvList
= EfiShellGetEnv(NULL
)
1457 ; MasterEnvList
!= NULL
&& *MasterEnvList
!= CHAR_NULL
//&& *(MasterEnvList+1) != CHAR_NULL
1458 ; MasterEnvList
+= StrLen(MasterEnvList
) + 1
1460 if (StrSize(MasterEnvList
) > ItemSize
) {
1461 ItemSize
= StrSize(MasterEnvList
);
1463 for (Temp
= StrStr(OriginalCommandLine
, MasterEnvList
)
1465 ; Temp
= StrStr(Temp
+1, MasterEnvList
)
1468 // we need a preceeding and following % and if there is space no ^ preceeding (if no space ignore)
1470 if (*(Temp
-1) == L
'%' && *(Temp
+StrLen(MasterEnvList
)) == L
'%' &&
1471 ((((Temp
-OriginalCommandLine
)>2) && *(Temp
-2) != L
'^') || ((Temp
-OriginalCommandLine
)<=2))) {
1472 NewSize
+=StrSize(EfiShellGetEnv(MasterEnvList
));
1478 // now do the replacements...
1480 NewCommandLine1
= AllocateZeroPool(NewSize
);
1481 NewCommandLine2
= AllocateZeroPool(NewSize
);
1482 ItemTemp
= AllocateZeroPool(ItemSize
+(2*sizeof(CHAR16
)));
1483 if (NewCommandLine1
== NULL
|| NewCommandLine2
== NULL
|| ItemTemp
== NULL
) {
1484 SHELL_FREE_NON_NULL(NewCommandLine1
);
1485 SHELL_FREE_NON_NULL(NewCommandLine2
);
1486 SHELL_FREE_NON_NULL(ItemTemp
);
1489 StrCpy(NewCommandLine1
, OriginalCommandLine
);
1490 for (MasterEnvList
= EfiShellGetEnv(NULL
)
1491 ; MasterEnvList
!= NULL
&& *MasterEnvList
!= CHAR_NULL
//&& *(MasterEnvList+1) != CHAR_NULL
1492 ; MasterEnvList
+= StrLen(MasterEnvList
) + 1
1494 StrCpy(ItemTemp
, L
"%");
1495 StrCat(ItemTemp
, MasterEnvList
);
1496 StrCat(ItemTemp
, L
"%");
1497 ShellCopySearchAndReplace(NewCommandLine1
, NewCommandLine2
, NewSize
, ItemTemp
, EfiShellGetEnv(MasterEnvList
), TRUE
, FALSE
);
1498 StrCpy(NewCommandLine1
, NewCommandLine2
);
1500 if (CurrentScriptFile
!= NULL
) {
1501 for (AliasListNode
= (ALIAS_LIST
*)GetFirstNode(&CurrentScriptFile
->SubstList
)
1502 ; !IsNull(&CurrentScriptFile
->SubstList
, &AliasListNode
->Link
)
1503 ; AliasListNode
= (ALIAS_LIST
*)GetNextNode(&CurrentScriptFile
->SubstList
, &AliasListNode
->Link
)
1505 ShellCopySearchAndReplace(NewCommandLine1
, NewCommandLine2
, NewSize
, AliasListNode
->Alias
, AliasListNode
->CommandString
, TRUE
, FALSE
);
1506 StrCpy(NewCommandLine1
, NewCommandLine2
);
1510 // Remove non-existant environment variables in scripts only
1512 StripUnreplacedEnvironmentVariables(NewCommandLine1
);
1516 // Now cleanup any straggler intentionally ignored "%" characters
1518 ShellCopySearchAndReplace(NewCommandLine1
, NewCommandLine2
, NewSize
, L
"^%", L
"%", TRUE
, FALSE
);
1519 StrCpy(NewCommandLine1
, NewCommandLine2
);
1521 FreePool(NewCommandLine2
);
1524 return (NewCommandLine1
);
1528 Internal function to run a command line with pipe usage.
1530 @param[in] CmdLine The pointer to the command line.
1531 @param[in] StdIn The pointer to the Standard input.
1532 @param[in] StdOut The pointer to the Standard output.
1534 @param[out] ExitStatus The exit code of the last command in the pipeline.
1537 @retval EFI_SUCCESS The split command is executed successfully.
1538 @retval other Some error occurs when executing the split command.
1543 IN CONST CHAR16
*CmdLine
,
1544 IN SHELL_FILE_HANDLE
*StdIn
,
1545 IN SHELL_FILE_HANDLE
*StdOut
,
1546 OUT SHELL_STATUS
*ExitStatus
1550 CHAR16
*NextCommandLine
;
1551 CHAR16
*OurCommandLine
;
1555 SHELL_FILE_HANDLE
*TempFileHandle
;
1558 ASSERT(StdOut
== NULL
);
1560 ASSERT(StrStr(CmdLine
, L
"|") != NULL
);
1562 Status
= EFI_SUCCESS
;
1563 NextCommandLine
= NULL
;
1564 OurCommandLine
= NULL
;
1568 NextCommandLine
= StrnCatGrow(&NextCommandLine
, &Size1
, StrStr(CmdLine
, L
"|")+1, 0);
1569 OurCommandLine
= StrnCatGrow(&OurCommandLine
, &Size2
, CmdLine
, StrStr(CmdLine
, L
"|") - CmdLine
);
1571 if (NextCommandLine
== NULL
|| OurCommandLine
== NULL
) {
1572 SHELL_FREE_NON_NULL(OurCommandLine
);
1573 SHELL_FREE_NON_NULL(NextCommandLine
);
1574 return (EFI_OUT_OF_RESOURCES
);
1575 } else if (StrStr(OurCommandLine
, L
"|") != NULL
|| Size1
== 0 || Size2
== 0) {
1576 SHELL_FREE_NON_NULL(OurCommandLine
);
1577 SHELL_FREE_NON_NULL(NextCommandLine
);
1578 return (EFI_INVALID_PARAMETER
);
1579 } else if (NextCommandLine
[0] != CHAR_NULL
&&
1580 NextCommandLine
[0] == L
'a' &&
1581 NextCommandLine
[1] == L
' '
1583 CopyMem(NextCommandLine
, NextCommandLine
+1, StrSize(NextCommandLine
) - sizeof(NextCommandLine
[0]));
1591 // make a SPLIT_LIST item and add to list
1593 Split
= AllocateZeroPool(sizeof(SPLIT_LIST
));
1594 ASSERT(Split
!= NULL
);
1595 Split
->SplitStdIn
= StdIn
;
1596 Split
->SplitStdOut
= ConvertEfiFileProtocolToShellHandle(CreateFileInterfaceMem(Unicode
), NULL
);
1597 ASSERT(Split
->SplitStdOut
!= NULL
);
1598 InsertHeadList(&ShellInfoObject
.SplitList
.Link
, &Split
->Link
);
1600 Status
= RunCommand(OurCommandLine
, NULL
);
1603 // move the output from the first to the in to the second.
1605 TempFileHandle
= Split
->SplitStdOut
;
1606 if (Split
->SplitStdIn
== StdIn
) {
1607 Split
->SplitStdOut
= NULL
;
1609 Split
->SplitStdOut
= Split
->SplitStdIn
;
1611 Split
->SplitStdIn
= TempFileHandle
;
1612 ShellInfoObject
.NewEfiShellProtocol
->SetFilePosition(ConvertShellHandleToEfiFileProtocol(Split
->SplitStdIn
), 0);
1614 if (!EFI_ERROR(Status
)) {
1615 Status
= RunCommand(NextCommandLine
, ExitStatus
);
1619 // remove the top level from the ScriptList
1621 ASSERT((SPLIT_LIST
*)GetFirstNode(&ShellInfoObject
.SplitList
.Link
) == Split
);
1622 RemoveEntryList(&Split
->Link
);
1625 // Note that the original StdIn is now the StdOut...
1627 if (Split
->SplitStdOut
!= NULL
&& Split
->SplitStdOut
!= StdIn
) {
1628 ShellInfoObject
.NewEfiShellProtocol
->CloseFile(ConvertShellHandleToEfiFileProtocol(Split
->SplitStdOut
));
1630 if (Split
->SplitStdIn
!= NULL
) {
1631 ShellInfoObject
.NewEfiShellProtocol
->CloseFile(ConvertShellHandleToEfiFileProtocol(Split
->SplitStdIn
));
1635 FreePool(NextCommandLine
);
1636 FreePool(OurCommandLine
);
1642 Take the original command line, substitute any variables, free
1643 the original string, return the modified copy.
1645 @param[in] CmdLine pointer to the command line to update.
1647 @retval EFI_SUCCESS the function was successful.
1648 @retval EFI_OUT_OF_RESOURCES a memory allocation failed.
1652 ShellSubstituteVariables(
1657 NewCmdLine
= ShellConvertVariables(*CmdLine
);
1658 SHELL_FREE_NON_NULL(*CmdLine
);
1659 if (NewCmdLine
== NULL
) {
1660 return (EFI_OUT_OF_RESOURCES
);
1662 *CmdLine
= NewCmdLine
;
1663 return (EFI_SUCCESS
);
1667 Take the original command line, substitute any alias in the first group of space delimited characters, free
1668 the original string, return the modified copy.
1670 @param[in] CmdLine pointer to the command line to update.
1672 @retval EFI_SUCCESS the function was successful.
1673 @retval EFI_OUT_OF_RESOURCES a memory allocation failed.
1677 ShellSubstituteAliases(
1682 CHAR16
*CommandName
;
1684 UINTN PostAliasSize
;
1685 ASSERT(CmdLine
!= NULL
);
1686 ASSERT(*CmdLine
!= NULL
);
1690 if (StrStr((*CmdLine
), L
" ") == NULL
){
1691 StrnCatGrow(&CommandName
, NULL
, (*CmdLine
), 0);
1693 StrnCatGrow(&CommandName
, NULL
, (*CmdLine
), StrStr((*CmdLine
), L
" ") - (*CmdLine
));
1697 // This cannot happen 'inline' since the CmdLine can need extra space.
1700 if (!ShellCommandIsCommandOnList(CommandName
)) {
1702 // Convert via alias
1704 Status
= ShellConvertAlias(&CommandName
);
1705 if (EFI_ERROR(Status
)){
1709 NewCmdLine
= StrnCatGrow(&NewCmdLine
, &PostAliasSize
, CommandName
, 0);
1710 if (NewCmdLine
== NULL
) {
1711 SHELL_FREE_NON_NULL(CommandName
);
1712 SHELL_FREE_NON_NULL(*CmdLine
);
1713 return (EFI_OUT_OF_RESOURCES
);
1715 NewCmdLine
= StrnCatGrow(&NewCmdLine
, &PostAliasSize
, StrStr((*CmdLine
), L
" "), 0);
1716 if (NewCmdLine
== NULL
) {
1717 SHELL_FREE_NON_NULL(CommandName
);
1718 SHELL_FREE_NON_NULL(*CmdLine
);
1719 return (EFI_OUT_OF_RESOURCES
);
1722 NewCmdLine
= StrnCatGrow(&NewCmdLine
, NULL
, (*CmdLine
), 0);
1725 SHELL_FREE_NON_NULL(*CmdLine
);
1726 SHELL_FREE_NON_NULL(CommandName
);
1729 // re-assign the passed in double pointer to point to our newly allocated buffer
1731 *CmdLine
= NewCmdLine
;
1733 return (EFI_SUCCESS
);
1737 Takes the Argv[0] part of the command line and determine the meaning of it.
1739 @param[in] CmdName pointer to the command line to update.
1741 @retval Internal_Command The name is an internal command.
1742 @retval File_Sys_Change the name is a file system change.
1743 @retval Script_File_Name the name is a NSH script file.
1744 @retval Unknown_Invalid the name is unknown.
1745 @retval Efi_Application the name is an application (.EFI).
1747 SHELL_OPERATION_TYPES
1750 IN CONST CHAR16
*CmdName
1753 CHAR16
* FileWithPath
;
1754 CONST CHAR16
* TempLocation
;
1755 CONST CHAR16
* TempLocation2
;
1757 FileWithPath
= NULL
;
1759 // test for an internal command.
1761 if (ShellCommandIsCommandOnList(CmdName
)) {
1762 return (Internal_Command
);
1766 // Test for file system change request. anything ending with first : and cant have spaces.
1768 if (CmdName
[(StrLen(CmdName
)-1)] == L
':') {
1769 if ( StrStr(CmdName
, L
" ") != NULL
1770 || StrLen(StrStr(CmdName
, L
":")) > 1
1772 return (Unknown_Invalid
);
1774 return (File_Sys_Change
);
1780 if ((FileWithPath
= ShellFindFilePathEx(CmdName
, mExecutableExtensions
)) != NULL
) {
1782 // See if that file has a script file extension
1784 if (StrLen(FileWithPath
) > 4) {
1785 TempLocation
= FileWithPath
+StrLen(FileWithPath
)-4;
1786 TempLocation2
= mScriptExtension
;
1787 if (StringNoCaseCompare((VOID
*)(&TempLocation
), (VOID
*)(&TempLocation2
)) == 0) {
1788 SHELL_FREE_NON_NULL(FileWithPath
);
1789 return (Script_File_Name
);
1794 // Was a file, but not a script. we treat this as an application.
1796 SHELL_FREE_NON_NULL(FileWithPath
);
1797 return (Efi_Application
);
1800 SHELL_FREE_NON_NULL(FileWithPath
);
1802 // No clue what this is... return invalid flag...
1804 return (Unknown_Invalid
);
1808 Determine if the first item in a command line is valid.
1810 @param[in] CmdLine The command line to parse.
1812 @retval EFI_SUCCESS The item is valid.
1813 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
1814 @retval EFI_NOT_FOUND The operation type is unknown or invalid.
1819 IN CONST CHAR16
*CmdLine
1823 CHAR16
*FirstParameter
;
1829 Temp
= StrnCatGrow(&Temp
, NULL
, CmdLine
, 0);
1831 return (EFI_OUT_OF_RESOURCES
);
1834 FirstParameter
= StrStr(Temp
, L
"|");
1835 if (FirstParameter
!= NULL
) {
1836 *FirstParameter
= CHAR_NULL
;
1839 FirstParameter
= NULL
;
1842 // Process the command line
1844 Status
= ProcessCommandLineToFinal(&Temp
);
1846 if (!EFI_ERROR(Status
)) {
1847 FirstParameter
= AllocateZeroPool(StrSize(CmdLine
));
1848 if (FirstParameter
== NULL
) {
1849 SHELL_FREE_NON_NULL(Temp
);
1850 return (EFI_OUT_OF_RESOURCES
);
1852 TempWalker
= (CHAR16
*)Temp
;
1853 GetNextParameter(&TempWalker
, &FirstParameter
);
1855 if (GetOperationType(FirstParameter
) == Unknown_Invalid
) {
1856 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_NOT_FOUND
), ShellInfoObject
.HiiHandle
, FirstParameter
);
1857 SetLastError(SHELL_NOT_FOUND
);
1858 Status
= EFI_NOT_FOUND
;
1862 SHELL_FREE_NON_NULL(Temp
);
1863 SHELL_FREE_NON_NULL(FirstParameter
);
1868 Determine if a command line contains with a split contains only valid commands.
1870 @param[in] CmdLine The command line to parse.
1872 @retval EFI_SUCCESS CmdLine has only valid commands, application, or has no split.
1873 @retval EFI_ABORTED CmdLine has at least one invalid command or application.
1878 IN CONST CHAR16
*CmdLine
1881 CONST CHAR16
*TempSpot
;
1885 // Verify up to the pipe or end character
1887 Status
= IsValidSplit(CmdLine
);
1888 if (EFI_ERROR(Status
)) {
1893 // If this was the only item, then get out
1895 if (!ContainsSplit(CmdLine
)) {
1896 return (EFI_SUCCESS
);
1900 // recurse to verify the next item
1902 TempSpot
= FindSplit(CmdLine
)+1;
1903 return (VerifySplit(TempSpot
));
1907 Process a split based operation.
1909 @param[in] CmdLine Pointer to the command line to process
1910 @param[out] ExitStatus The exit status of the command. Ignored if NULL.
1911 Invalid if this function returns an error.
1913 @retval EFI_SUCCESS The operation was successful
1914 @return an error occured.
1918 ProcessNewSplitCommandLine(
1919 IN CONST CHAR16
*CmdLine
,
1920 OUT SHELL_STATUS
*ExitStatus
1926 Status
= VerifySplit(CmdLine
);
1927 if (EFI_ERROR(Status
)) {
1934 // are we in an existing split???
1936 if (!IsListEmpty(&ShellInfoObject
.SplitList
.Link
)) {
1937 Split
= (SPLIT_LIST
*)GetFirstNode(&ShellInfoObject
.SplitList
.Link
);
1940 if (Split
== NULL
) {
1941 Status
= RunSplitCommand(CmdLine
, NULL
, NULL
, ExitStatus
);
1943 Status
= RunSplitCommand(
1950 if (EFI_ERROR(Status
)) {
1951 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_INVALID_SPLIT
), ShellInfoObject
.HiiHandle
, CmdLine
);
1957 Handle a request to change the current file system.
1959 @param[in] CmdLine The passed in command line.
1961 @retval EFI_SUCCESS The operation was successful.
1966 IN CONST CHAR16
*CmdLine
1970 Status
= EFI_SUCCESS
;
1973 // make sure we are the right operation
1975 ASSERT(CmdLine
[(StrLen(CmdLine
)-1)] == L
':' && StrStr(CmdLine
, L
" ") == NULL
);
1978 // Call the protocol API to do the work
1980 Status
= ShellInfoObject
.NewEfiShellProtocol
->SetCurDir(NULL
, CmdLine
);
1983 // Report any errors
1985 if (EFI_ERROR(Status
)) {
1986 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_INVALID_MAPPING
), ShellInfoObject
.HiiHandle
, CmdLine
);
1993 Reprocess the command line to direct all -? to the help command.
1995 if found, will add "help" as argv[0], and move the rest later.
1997 @param[in,out] CmdLine pointer to the command line to update
2002 IN OUT CHAR16
**CmdLine
2005 CHAR16
*CurrentParameter
;
2008 CHAR16
*NewCommandLine
;
2011 Status
= EFI_SUCCESS
;
2013 CurrentParameter
= AllocateZeroPool(StrSize(*CmdLine
));
2014 if (CurrentParameter
== NULL
) {
2015 return (EFI_OUT_OF_RESOURCES
);
2019 while(Walker
!= NULL
&& *Walker
!= CHAR_NULL
) {
2020 LastWalker
= Walker
;
2021 GetNextParameter(&Walker
, &CurrentParameter
);
2022 if (StrStr(CurrentParameter
, L
"-?") == CurrentParameter
) {
2023 LastWalker
[0] = L
' ';
2024 LastWalker
[1] = L
' ';
2025 NewCommandLine
= AllocateZeroPool(StrSize(L
"help ") + StrSize(*CmdLine
));
2026 if (NewCommandLine
== NULL
) {
2027 Status
= EFI_OUT_OF_RESOURCES
;
2030 StrCpy(NewCommandLine
, L
"help ");
2031 StrCat(NewCommandLine
, *CmdLine
);
2032 SHELL_FREE_NON_NULL(*CmdLine
);
2033 *CmdLine
= NewCommandLine
;
2038 SHELL_FREE_NON_NULL(CurrentParameter
);
2044 Function to update the shell variable "lasterror".
2046 @param[in] ErrorCode the error code to put into lasterror.
2051 IN CONST SHELL_STATUS ErrorCode
2054 CHAR16 LeString
[19];
2055 if (sizeof(EFI_STATUS
) == sizeof(UINT64
)) {
2056 UnicodeSPrint(LeString
, sizeof(LeString
), L
"0x%Lx", ErrorCode
);
2058 UnicodeSPrint(LeString
, sizeof(LeString
), L
"0x%x", ErrorCode
);
2060 DEBUG_CODE(InternalEfiShellSetEnv(L
"debuglasterror", LeString
, TRUE
););
2061 InternalEfiShellSetEnv(L
"lasterror", LeString
, TRUE
);
2063 return (EFI_SUCCESS
);
2067 Converts the command line to it's post-processed form. this replaces variables and alias' per UEFI Shell spec.
2069 @param[in,out] CmdLine pointer to the command line to update
2071 @retval EFI_SUCCESS The operation was successful
2072 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
2073 @return some other error occured
2077 ProcessCommandLineToFinal(
2078 IN OUT CHAR16
**CmdLine
2082 TrimSpaces(CmdLine
);
2084 Status
= ShellSubstituteAliases(CmdLine
);
2085 if (EFI_ERROR(Status
)) {
2089 TrimSpaces(CmdLine
);
2091 Status
= ShellSubstituteVariables(CmdLine
);
2092 if (EFI_ERROR(Status
)) {
2095 ASSERT (*CmdLine
!= NULL
);
2097 TrimSpaces(CmdLine
);
2100 // update for help parsing
2102 if (StrStr(*CmdLine
, L
"?") != NULL
) {
2104 // This may do nothing if the ? does not indicate help.
2105 // Save all the details for in the API below.
2107 Status
= DoHelpUpdate(CmdLine
);
2110 TrimSpaces(CmdLine
);
2112 return (EFI_SUCCESS
);
2116 Run an internal shell command.
2118 This API will upadate the shell's environment since these commands are libraries.
2120 @param[in] CmdLine the command line to run.
2121 @param[in] FirstParameter the first parameter on the command line
2122 @param[in] ParamProtocol the shell parameters protocol pointer
2124 @param[out] ExitStatus The exit code of the command. Ignored if NULL.
2126 @retval EFI_SUCCESS The command was completed.
2127 @retval EFI_ABORTED The command's operation was aborted.
2132 IN CONST CHAR16
*CmdLine
,
2133 IN CHAR16
*FirstParameter
,
2134 IN EFI_SHELL_PARAMETERS_PROTOCOL
*ParamProtocol
,
2135 OUT SHELL_STATUS
*ExitStatus OPTIONAL
2141 SHELL_STATUS CommandReturnedStatus
;
2145 // get the argc and argv updated for internal commands
2147 Status
= UpdateArgcArgv(ParamProtocol
, CmdLine
, &Argv
, &Argc
);
2148 if (!EFI_ERROR(Status
)) {
2150 // Run the internal command.
2152 Status
= ShellCommandRunCommandHandler(FirstParameter
, &CommandReturnedStatus
, &LastError
);
2154 if (!EFI_ERROR(Status
)) {
2156 // Update last error status.
2157 // some commands do not update last error.
2160 SetLastError(CommandReturnedStatus
);
2162 if (ExitStatus
!= NULL
) {
2163 *ExitStatus
= CommandReturnedStatus
;
2167 // Pass thru the exitcode from the app.
2169 if (ShellCommandGetExit()) {
2171 // An Exit was requested ("exit" command), pass its value up.
2173 Status
= CommandReturnedStatus
;
2174 } else if (CommandReturnedStatus
!= SHELL_SUCCESS
&& IsScriptOnlyCommand(FirstParameter
)) {
2176 // Always abort when a script only command fails for any reason
2178 Status
= EFI_ABORTED
;
2179 } else if (ShellCommandGetCurrentScriptFile() != NULL
&& CommandReturnedStatus
== SHELL_ABORTED
) {
2181 // Abort when in a script and a command aborted
2183 Status
= EFI_ABORTED
;
2189 // This is guarenteed to be called after UpdateArgcArgv no matter what else happened.
2190 // This is safe even if the update API failed. In this case, it may be a no-op.
2192 RestoreArgcArgv(ParamProtocol
, &Argv
, &Argc
);
2195 // If a script is running and the command is not a scipt only command, then
2196 // change return value to success so the script won't halt (unless aborted).
2198 // Script only commands have to be able halt the script since the script will
2199 // not operate if they are failing.
2201 if ( ShellCommandGetCurrentScriptFile() != NULL
2202 && !IsScriptOnlyCommand(FirstParameter
)
2203 && Status
!= EFI_ABORTED
2205 Status
= EFI_SUCCESS
;
2212 Function to run the command or file.
2214 @param[in] Type the type of operation being run.
2215 @param[in] CmdLine the command line to run.
2216 @param[in] FirstParameter the first parameter on the command line
2217 @param[in] ParamProtocol the shell parameters protocol pointer
2219 @param[out] ExitStatus The exit code of the command or file.
2222 @retval EFI_SUCCESS The command was completed.
2223 @retval EFI_ABORTED The command's operation was aborted.
2228 IN SHELL_OPERATION_TYPES Type
,
2229 IN CONST CHAR16
*CmdLine
,
2230 IN CHAR16
*FirstParameter
,
2231 IN EFI_SHELL_PARAMETERS_PROTOCOL
*ParamProtocol
,
2232 OUT SHELL_STATUS
*ExitStatus
2236 EFI_STATUS StartStatus
;
2237 CHAR16
*CommandWithPath
;
2238 EFI_DEVICE_PATH_PROTOCOL
*DevPath
;
2239 SHELL_STATUS CalleeExitStatus
;
2241 Status
= EFI_SUCCESS
;
2242 CommandWithPath
= NULL
;
2244 CalleeExitStatus
= SHELL_INVALID_PARAMETER
;
2247 case Internal_Command
:
2248 Status
= RunInternalCommand(
2255 case Script_File_Name
:
2256 case Efi_Application
:
2258 // Process a fully qualified path
2260 if (StrStr(FirstParameter
, L
":") != NULL
) {
2261 ASSERT (CommandWithPath
== NULL
);
2262 if (ShellIsFile(FirstParameter
) == EFI_SUCCESS
) {
2263 CommandWithPath
= StrnCatGrow(&CommandWithPath
, NULL
, FirstParameter
, 0);
2268 // Process a relative path and also check in the path environment variable
2270 if (CommandWithPath
== NULL
) {
2271 CommandWithPath
= ShellFindFilePathEx(FirstParameter
, mExecutableExtensions
);
2275 // This should be impossible now.
2277 ASSERT(CommandWithPath
!= NULL
);
2280 // Make sure that path is not just a directory (or not found)
2282 if (!EFI_ERROR(ShellIsDirectory(CommandWithPath
))) {
2283 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_NOT_FOUND
), ShellInfoObject
.HiiHandle
, FirstParameter
);
2284 SetLastError(SHELL_NOT_FOUND
);
2287 case Script_File_Name
:
2288 Status
= RunScriptFile (
2296 case Efi_Application
:
2298 // Get the device path of the application image
2300 DevPath
= ShellInfoObject
.NewEfiShellProtocol
->GetDevicePathFromFilePath(CommandWithPath
);
2301 if (DevPath
== NULL
){
2302 Status
= EFI_OUT_OF_RESOURCES
;
2307 // Execute the device path
2309 Status
= InternalShellExecuteDevicePath(
2319 SHELL_FREE_NON_NULL(DevPath
);
2321 if(EFI_ERROR (Status
)) {
2322 CalleeExitStatus
= (SHELL_STATUS
) (Status
& (~MAX_BIT
));
2324 CalleeExitStatus
= (SHELL_STATUS
) StartStatus
;
2328 // Update last error status.
2330 // Status is an EFI_STATUS. Clear top bit to convert to SHELL_STATUS
2331 SetLastError(CalleeExitStatus
);
2347 SHELL_FREE_NON_NULL(CommandWithPath
);
2349 if (ExitStatus
!= NULL
) {
2350 *ExitStatus
= CalleeExitStatus
;
2357 Function to setup StdIn, StdErr, StdOut, and then run the command or file.
2359 @param[in] Type the type of operation being run.
2360 @param[in] CmdLine the command line to run.
2361 @param[in] FirstParameter the first parameter on the command line.
2362 @param[in] ParamProtocol the shell parameters protocol pointer
2364 @param[out] ExitStatus The exit code of the command or file.
2367 @retval EFI_SUCCESS The command was completed.
2368 @retval EFI_ABORTED The command's operation was aborted.
2372 SetupAndRunCommandOrFile(
2373 IN SHELL_OPERATION_TYPES Type
,
2375 IN CHAR16
*FirstParameter
,
2376 IN EFI_SHELL_PARAMETERS_PROTOCOL
*ParamProtocol
,
2377 OUT SHELL_STATUS
*ExitStatus
2381 SHELL_FILE_HANDLE OriginalStdIn
;
2382 SHELL_FILE_HANDLE OriginalStdOut
;
2383 SHELL_FILE_HANDLE OriginalStdErr
;
2384 SYSTEM_TABLE_INFO OriginalSystemTableInfo
;
2387 // Update the StdIn, StdOut, and StdErr for redirection to environment variables, files, etc... unicode and ASCII
2389 Status
= UpdateStdInStdOutStdErr(ParamProtocol
, CmdLine
, &OriginalStdIn
, &OriginalStdOut
, &OriginalStdErr
, &OriginalSystemTableInfo
);
2392 // The StdIn, StdOut, and StdErr are set up.
2393 // Now run the command, script, or application
2395 if (!EFI_ERROR(Status
)) {
2396 Status
= RunCommandOrFile(
2408 if (EFI_ERROR(Status
)) {
2409 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_ERROR
), ShellInfoObject
.HiiHandle
, (VOID
*)(Status
));
2413 // put back the original StdIn, StdOut, and StdErr
2415 RestoreStdInStdOutStdErr(ParamProtocol
, &OriginalStdIn
, &OriginalStdOut
, &OriginalStdErr
, &OriginalSystemTableInfo
);
2421 Function will process and run a command line.
2423 This will determine if the command line represents an internal shell
2424 command or dispatch an external application.
2426 @param[in] CmdLine The command line to parse.
2427 @param[out] ExitStatus The exit code of the command. Ignored if NULL.
2429 @retval EFI_SUCCESS The command was completed.
2430 @retval EFI_ABORTED The command's operation was aborted.
2435 IN CONST CHAR16
*CmdLine
,
2436 OUT SHELL_STATUS
*ExitStatus
2440 CHAR16
*CleanOriginal
;
2441 CHAR16
*FirstParameter
;
2443 SHELL_OPERATION_TYPES Type
;
2445 ASSERT(CmdLine
!= NULL
);
2446 if (StrLen(CmdLine
) == 0) {
2447 return (EFI_SUCCESS
);
2450 Status
= EFI_SUCCESS
;
2451 CleanOriginal
= NULL
;
2453 CleanOriginal
= StrnCatGrow(&CleanOriginal
, NULL
, CmdLine
, 0);
2454 if (CleanOriginal
== NULL
) {
2455 return (EFI_OUT_OF_RESOURCES
);
2458 TrimSpaces(&CleanOriginal
);
2461 // Handle case that passed in command line is just 1 or more " " characters.
2463 if (StrLen (CleanOriginal
) == 0) {
2464 SHELL_FREE_NON_NULL(CleanOriginal
);
2465 return (EFI_SUCCESS
);
2468 Status
= ProcessCommandLineToFinal(&CleanOriginal
);
2469 if (EFI_ERROR(Status
)) {
2470 SHELL_FREE_NON_NULL(CleanOriginal
);
2475 // We dont do normal processing with a split command line (output from one command input to another)
2477 if (ContainsSplit(CleanOriginal
)) {
2478 Status
= ProcessNewSplitCommandLine(CleanOriginal
, ExitStatus
);
2479 SHELL_FREE_NON_NULL(CleanOriginal
);
2484 // We need the first parameter information so we can determine the operation type
2486 FirstParameter
= AllocateZeroPool(StrSize(CleanOriginal
));
2487 if (FirstParameter
== NULL
) {
2488 SHELL_FREE_NON_NULL(CleanOriginal
);
2489 return (EFI_OUT_OF_RESOURCES
);
2491 TempWalker
= CleanOriginal
;
2492 GetNextParameter(&TempWalker
, &FirstParameter
);
2495 // Depending on the first parameter we change the behavior
2497 switch (Type
= GetOperationType(FirstParameter
)) {
2498 case File_Sys_Change
:
2499 Status
= ChangeMappedDrive (FirstParameter
);
2501 case Internal_Command
:
2502 case Script_File_Name
:
2503 case Efi_Application
:
2504 Status
= SetupAndRunCommandOrFile(
2508 ShellInfoObject
.NewShellParametersProtocol
,
2514 // Whatever was typed, it was invalid.
2516 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_NOT_FOUND
), ShellInfoObject
.HiiHandle
, FirstParameter
);
2517 SetLastError(SHELL_NOT_FOUND
);
2521 SHELL_FREE_NON_NULL(CleanOriginal
);
2522 SHELL_FREE_NON_NULL(FirstParameter
);
2527 STATIC CONST UINT16 InvalidChars
[] = {L
'*', L
'?', L
'<', L
'>', L
'\\', L
'/', L
'\"', 0x0001, 0x0002};
2529 Function determins if the CommandName COULD be a valid command. It does not determine whether
2530 this is a valid command. It only checks for invalid characters.
2532 @param[in] CommandName The name to check
2534 @retval TRUE CommandName could be a command name
2535 @retval FALSE CommandName could not be a valid command name
2540 IN CONST CHAR16
*CommandName
2544 if (CommandName
== NULL
) {
2549 ; Count
< sizeof(InvalidChars
) / sizeof(InvalidChars
[0])
2552 if (ScanMem16(CommandName
, StrSize(CommandName
), InvalidChars
[Count
]) != NULL
) {
2560 Function to process a NSH script file via SHELL_FILE_HANDLE.
2562 @param[in] Handle The handle to the already opened file.
2563 @param[in] Name The name of the script file.
2565 @param[out] ExitStatus The exit code of the script. Ignored if NULL.
2567 @retval EFI_SUCCESS the script completed sucessfully
2571 RunScriptFileHandle (
2572 IN SHELL_FILE_HANDLE Handle
,
2573 IN CONST CHAR16
*Name
,
2574 OUT SHELL_STATUS
*ExitStatus
2578 SCRIPT_FILE
*NewScriptFile
;
2580 CHAR16
*CommandLine
;
2581 CHAR16
*CommandLine2
;
2582 CHAR16
*CommandLine3
;
2583 SCRIPT_COMMAND_LIST
*LastCommand
;
2585 BOOLEAN PreScriptEchoState
;
2586 BOOLEAN PreCommandEchoState
;
2587 CONST CHAR16
*CurDir
;
2589 CHAR16 LeString
[50];
2590 SHELL_STATUS CalleeExitStatus
;
2592 ASSERT(!ShellCommandGetScriptExit());
2594 CalleeExitStatus
= SHELL_SUCCESS
;
2596 PreScriptEchoState
= ShellCommandGetEchoState();
2598 NewScriptFile
= (SCRIPT_FILE
*)AllocateZeroPool(sizeof(SCRIPT_FILE
));
2599 if (NewScriptFile
== NULL
) {
2600 return (EFI_OUT_OF_RESOURCES
);
2606 ASSERT(NewScriptFile
->ScriptName
== NULL
);
2607 NewScriptFile
->ScriptName
= StrnCatGrow(&NewScriptFile
->ScriptName
, NULL
, Name
, 0);
2608 if (NewScriptFile
->ScriptName
== NULL
) {
2609 DeleteScriptFileStruct(NewScriptFile
);
2610 return (EFI_OUT_OF_RESOURCES
);
2614 // Save the parameters (used to replace %0 to %9 later on)
2616 NewScriptFile
->Argc
= ShellInfoObject
.NewShellParametersProtocol
->Argc
;
2617 if (NewScriptFile
->Argc
!= 0) {
2618 NewScriptFile
->Argv
= (CHAR16
**)AllocateZeroPool(NewScriptFile
->Argc
* sizeof(CHAR16
*));
2619 if (NewScriptFile
->Argv
== NULL
) {
2620 DeleteScriptFileStruct(NewScriptFile
);
2621 return (EFI_OUT_OF_RESOURCES
);
2623 for (LoopVar
= 0 ; LoopVar
< 10 && LoopVar
< NewScriptFile
->Argc
; LoopVar
++) {
2624 ASSERT(NewScriptFile
->Argv
[LoopVar
] == NULL
);
2625 NewScriptFile
->Argv
[LoopVar
] = StrnCatGrow(&NewScriptFile
->Argv
[LoopVar
], NULL
, ShellInfoObject
.NewShellParametersProtocol
->Argv
[LoopVar
], 0);
2626 if (NewScriptFile
->Argv
[LoopVar
] == NULL
) {
2627 DeleteScriptFileStruct(NewScriptFile
);
2628 return (EFI_OUT_OF_RESOURCES
);
2632 NewScriptFile
->Argv
= NULL
;
2635 InitializeListHead(&NewScriptFile
->CommandList
);
2636 InitializeListHead(&NewScriptFile
->SubstList
);
2639 // Now build the list of all script commands.
2642 while(!ShellFileHandleEof(Handle
)) {
2643 CommandLine
= ShellFileHandleReturnLine(Handle
, &Ascii
);
2645 if (CommandLine
== NULL
|| StrLen(CommandLine
) == 0 || CommandLine
[0] == '#') {
2646 SHELL_FREE_NON_NULL(CommandLine
);
2649 NewScriptFile
->CurrentCommand
= AllocateZeroPool(sizeof(SCRIPT_COMMAND_LIST
));
2650 if (NewScriptFile
->CurrentCommand
== NULL
) {
2651 SHELL_FREE_NON_NULL(CommandLine
);
2652 DeleteScriptFileStruct(NewScriptFile
);
2653 return (EFI_OUT_OF_RESOURCES
);
2656 NewScriptFile
->CurrentCommand
->Cl
= CommandLine
;
2657 NewScriptFile
->CurrentCommand
->Data
= NULL
;
2658 NewScriptFile
->CurrentCommand
->Line
= LineCount
;
2660 InsertTailList(&NewScriptFile
->CommandList
, &NewScriptFile
->CurrentCommand
->Link
);
2664 // Add this as the topmost script file
2666 ShellCommandSetNewScript (NewScriptFile
);
2669 // Now enumerate through the commands and run each one.
2671 CommandLine
= AllocateZeroPool(PcdGet16(PcdShellPrintBufferSize
));
2672 if (CommandLine
== NULL
) {
2673 DeleteScriptFileStruct(NewScriptFile
);
2674 return (EFI_OUT_OF_RESOURCES
);
2676 CommandLine2
= AllocateZeroPool(PcdGet16(PcdShellPrintBufferSize
));
2677 if (CommandLine2
== NULL
) {
2678 FreePool(CommandLine
);
2679 DeleteScriptFileStruct(NewScriptFile
);
2680 return (EFI_OUT_OF_RESOURCES
);
2683 for ( NewScriptFile
->CurrentCommand
= (SCRIPT_COMMAND_LIST
*)GetFirstNode(&NewScriptFile
->CommandList
)
2684 ; !IsNull(&NewScriptFile
->CommandList
, &NewScriptFile
->CurrentCommand
->Link
)
2685 ; // conditional increment in the body of the loop
2687 ASSERT(CommandLine2
!= NULL
);
2688 StrCpy(CommandLine2
, NewScriptFile
->CurrentCommand
->Cl
);
2691 // NULL out comments
2693 for (CommandLine3
= CommandLine2
; CommandLine3
!= NULL
&& *CommandLine3
!= CHAR_NULL
; CommandLine3
++) {
2694 if (*CommandLine3
== L
'^') {
2695 if (*(CommandLine3
+1) == L
'#' || *(CommandLine3
+1) == L
':') {
2696 CopyMem(CommandLine3
, CommandLine3
+1, StrSize(CommandLine3
) - sizeof(CommandLine3
[0]));
2698 } else if (*CommandLine3
== L
'#') {
2699 *CommandLine3
= CHAR_NULL
;
2703 if (CommandLine2
!= NULL
&& StrLen(CommandLine2
) >= 1) {
2705 // Due to variability in starting the find and replace action we need to have both buffers the same.
2707 StrCpy(CommandLine
, CommandLine2
);
2710 // Remove the %0 to %9 from the command line (if we have some arguments)
2712 if (NewScriptFile
->Argv
!= NULL
) {
2713 switch (NewScriptFile
->Argc
) {
2715 Status
= ShellCopySearchAndReplace(CommandLine2
, CommandLine
, PcdGet16 (PcdShellPrintBufferSize
), L
"%9", NewScriptFile
->Argv
[9], FALSE
, TRUE
);
2716 ASSERT_EFI_ERROR(Status
);
2718 Status
= ShellCopySearchAndReplace(CommandLine
, CommandLine2
, PcdGet16 (PcdShellPrintBufferSize
), L
"%8", NewScriptFile
->Argv
[8], FALSE
, TRUE
);
2719 ASSERT_EFI_ERROR(Status
);
2721 Status
= ShellCopySearchAndReplace(CommandLine2
, CommandLine
, PcdGet16 (PcdShellPrintBufferSize
), L
"%7", NewScriptFile
->Argv
[7], FALSE
, TRUE
);
2722 ASSERT_EFI_ERROR(Status
);
2724 Status
= ShellCopySearchAndReplace(CommandLine
, CommandLine2
, PcdGet16 (PcdShellPrintBufferSize
), L
"%6", NewScriptFile
->Argv
[6], FALSE
, TRUE
);
2725 ASSERT_EFI_ERROR(Status
);
2727 Status
= ShellCopySearchAndReplace(CommandLine2
, CommandLine
, PcdGet16 (PcdShellPrintBufferSize
), L
"%5", NewScriptFile
->Argv
[5], FALSE
, TRUE
);
2728 ASSERT_EFI_ERROR(Status
);
2730 Status
= ShellCopySearchAndReplace(CommandLine
, CommandLine2
, PcdGet16 (PcdShellPrintBufferSize
), L
"%4", NewScriptFile
->Argv
[4], FALSE
, TRUE
);
2731 ASSERT_EFI_ERROR(Status
);
2733 Status
= ShellCopySearchAndReplace(CommandLine2
, CommandLine
, PcdGet16 (PcdShellPrintBufferSize
), L
"%3", NewScriptFile
->Argv
[3], FALSE
, TRUE
);
2734 ASSERT_EFI_ERROR(Status
);
2736 Status
= ShellCopySearchAndReplace(CommandLine
, CommandLine2
, PcdGet16 (PcdShellPrintBufferSize
), L
"%2", NewScriptFile
->Argv
[2], FALSE
, TRUE
);
2737 ASSERT_EFI_ERROR(Status
);
2739 Status
= ShellCopySearchAndReplace(CommandLine2
, CommandLine
, PcdGet16 (PcdShellPrintBufferSize
), L
"%1", NewScriptFile
->Argv
[1], FALSE
, TRUE
);
2740 ASSERT_EFI_ERROR(Status
);
2742 Status
= ShellCopySearchAndReplace(CommandLine
, CommandLine2
, PcdGet16 (PcdShellPrintBufferSize
), L
"%0", NewScriptFile
->Argv
[0], FALSE
, TRUE
);
2743 ASSERT_EFI_ERROR(Status
);
2749 Status
= ShellCopySearchAndReplace(CommandLine2
, CommandLine
, PcdGet16 (PcdShellPrintBufferSize
), L
"%1", L
"\"\"", FALSE
, FALSE
);
2750 Status
= ShellCopySearchAndReplace(CommandLine
, CommandLine2
, PcdGet16 (PcdShellPrintBufferSize
), L
"%2", L
"\"\"", FALSE
, FALSE
);
2751 Status
= ShellCopySearchAndReplace(CommandLine2
, CommandLine
, PcdGet16 (PcdShellPrintBufferSize
), L
"%3", L
"\"\"", FALSE
, FALSE
);
2752 Status
= ShellCopySearchAndReplace(CommandLine
, CommandLine2
, PcdGet16 (PcdShellPrintBufferSize
), L
"%4", L
"\"\"", FALSE
, FALSE
);
2753 Status
= ShellCopySearchAndReplace(CommandLine2
, CommandLine
, PcdGet16 (PcdShellPrintBufferSize
), L
"%5", L
"\"\"", FALSE
, FALSE
);
2754 Status
= ShellCopySearchAndReplace(CommandLine
, CommandLine2
, PcdGet16 (PcdShellPrintBufferSize
), L
"%6", L
"\"\"", FALSE
, FALSE
);
2755 Status
= ShellCopySearchAndReplace(CommandLine2
, CommandLine
, PcdGet16 (PcdShellPrintBufferSize
), L
"%7", L
"\"\"", FALSE
, FALSE
);
2756 Status
= ShellCopySearchAndReplace(CommandLine
, CommandLine2
, PcdGet16 (PcdShellPrintBufferSize
), L
"%8", L
"\"\"", FALSE
, FALSE
);
2757 Status
= ShellCopySearchAndReplace(CommandLine2
, CommandLine
, PcdGet16 (PcdShellPrintBufferSize
), L
"%9", L
"\"\"", FALSE
, FALSE
);
2759 StrCpy(CommandLine2
, CommandLine
);
2761 LastCommand
= NewScriptFile
->CurrentCommand
;
2763 for (CommandLine3
= CommandLine2
; CommandLine3
[0] == L
' ' ; CommandLine3
++);
2765 if (CommandLine3
!= NULL
&& CommandLine3
[0] == L
':' ) {
2767 // This line is a goto target / label
2770 if (CommandLine3
!= NULL
&& StrLen(CommandLine3
) > 0) {
2771 if (CommandLine3
[0] == L
'@') {
2773 // We need to save the current echo state
2774 // and disable echo for just this command.
2776 PreCommandEchoState
= ShellCommandGetEchoState();
2777 ShellCommandSetEchoState(FALSE
);
2778 Status
= RunCommand(CommandLine3
+1, NULL
);
2781 // If command was "@echo -off" or "@echo -on" then don't restore echo state
2783 if (StrCmp (L
"@echo -off", CommandLine3
) != 0 &&
2784 StrCmp (L
"@echo -on", CommandLine3
) != 0) {
2786 // Now restore the pre-'@' echo state.
2788 ShellCommandSetEchoState(PreCommandEchoState
);
2791 if (ShellCommandGetEchoState()) {
2792 CurDir
= ShellInfoObject
.NewEfiShellProtocol
->GetEnv(L
"cwd");
2793 if (CurDir
!= NULL
&& StrLen(CurDir
) > 1) {
2794 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_CURDIR
), ShellInfoObject
.HiiHandle
, CurDir
);
2796 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_SHELL
), ShellInfoObject
.HiiHandle
);
2798 ShellPrintEx(-1, -1, L
"%s\r\n", CommandLine2
);
2800 Status
= RunCommand(CommandLine3
, NULL
);
2804 if (ShellCommandGetScriptExit()) {
2806 // ShellCommandGetExitCode() always returns a UINT64
2808 CalleeExitStatus
= (SHELL_STATUS
) ShellCommandGetExitCode();
2809 UnicodeSPrint(LeString
, sizeof(LeString
), L
"0x%Lx", CalleeExitStatus
);
2810 DEBUG_CODE(InternalEfiShellSetEnv(L
"debuglasterror", LeString
, TRUE
););
2811 InternalEfiShellSetEnv(L
"lasterror", LeString
, TRUE
);
2813 ShellCommandRegisterExit(FALSE
, 0);
2814 Status
= EFI_SUCCESS
;
2817 if (ShellGetExecutionBreakFlag()) {
2820 if (EFI_ERROR(Status
)) {
2821 CalleeExitStatus
= (SHELL_STATUS
) Status
;
2824 if (ShellCommandGetExit()) {
2825 CalleeExitStatus
= (SHELL_STATUS
) ShellCommandGetExitCode();
2830 // If that commend did not update the CurrentCommand then we need to advance it...
2832 if (LastCommand
== NewScriptFile
->CurrentCommand
) {
2833 NewScriptFile
->CurrentCommand
= (SCRIPT_COMMAND_LIST
*)GetNextNode(&NewScriptFile
->CommandList
, &NewScriptFile
->CurrentCommand
->Link
);
2834 if (!IsNull(&NewScriptFile
->CommandList
, &NewScriptFile
->CurrentCommand
->Link
)) {
2835 NewScriptFile
->CurrentCommand
->Reset
= TRUE
;
2839 NewScriptFile
->CurrentCommand
= (SCRIPT_COMMAND_LIST
*)GetNextNode(&NewScriptFile
->CommandList
, &NewScriptFile
->CurrentCommand
->Link
);
2840 if (!IsNull(&NewScriptFile
->CommandList
, &NewScriptFile
->CurrentCommand
->Link
)) {
2841 NewScriptFile
->CurrentCommand
->Reset
= TRUE
;
2847 FreePool(CommandLine
);
2848 FreePool(CommandLine2
);
2849 ShellCommandSetNewScript (NULL
);
2852 // Only if this was the last script reset the state.
2854 if (ShellCommandGetCurrentScriptFile()==NULL
) {
2855 ShellCommandSetEchoState(PreScriptEchoState
);
2858 if (ExitStatus
!= NULL
) {
2859 *ExitStatus
= CalleeExitStatus
;
2862 return (EFI_SUCCESS
);
2866 Function to process a NSH script file.
2868 @param[in] ScriptPath Pointer to the script file name (including file system path).
2869 @param[in] Handle the handle of the script file already opened.
2870 @param[in] CmdLine the command line to run.
2871 @param[in] ParamProtocol the shell parameters protocol pointer
2873 @param[out] ExitStatus The exit code of the script. Ignored if NULL.
2875 @retval EFI_SUCCESS the script completed sucessfully
2880 IN CONST CHAR16
*ScriptPath
,
2881 IN SHELL_FILE_HANDLE Handle OPTIONAL
,
2882 IN CONST CHAR16
*CmdLine
,
2883 IN EFI_SHELL_PARAMETERS_PROTOCOL
*ParamProtocol
,
2884 OUT SHELL_STATUS
*ExitStatus
2888 SHELL_FILE_HANDLE FileHandle
;
2892 if (ShellIsFile(ScriptPath
) != EFI_SUCCESS
) {
2893 return (EFI_INVALID_PARAMETER
);
2897 // get the argc and argv updated for scripts
2899 Status
= UpdateArgcArgv(ParamProtocol
, CmdLine
, &Argv
, &Argc
);
2900 if (!EFI_ERROR(Status
)) {
2902 if (Handle
== NULL
) {
2906 Status
= ShellOpenFileByName(ScriptPath
, &FileHandle
, EFI_FILE_MODE_READ
, 0);
2907 if (!EFI_ERROR(Status
)) {
2911 Status
= RunScriptFileHandle(FileHandle
, ScriptPath
, ExitStatus
);
2914 // now close the file
2916 ShellCloseFile(&FileHandle
);
2919 Status
= RunScriptFileHandle(Handle
, ScriptPath
, ExitStatus
);
2924 // This is guarenteed to be called after UpdateArgcArgv no matter what else happened.
2925 // This is safe even if the update API failed. In this case, it may be a no-op.
2927 RestoreArgcArgv(ParamProtocol
, &Argv
, &Argc
);