2 This is THE shell (application)
4 Copyright (c) 2009 - 2014, Intel Corporation. All rights reserved.<BR>
5 Copyright (c) 2013, 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 ShellInfoObject
.ConsoleInfo
->Enabled
= TRUE
;
513 ShellInfoObject
.ConsoleInfo
->RowCounter
= 0;
516 // Reset the CTRL-C event (yes we ignore the return values)
518 Status
= gBS
->CheckEvent (ShellInfoObject
.NewEfiShellProtocol
->ExecutionBreak
);
523 Status
= DoShellPrompt();
524 } while (!ShellCommandGetExit());
525 ExitStatus
= (SHELL_STATUS
) ShellCommandGetExitCode();
527 if (OldConIn
!= NULL
&& ConInHandle
!= NULL
) {
528 CloseSimpleTextInOnFile (gST
->ConIn
);
529 gST
->ConIn
= OldConIn
;
530 gST
->ConsoleInHandle
= ConInHandle
;
537 // uninstall protocols / free memory / etc...
539 if (ShellInfoObject
.UserBreakTimer
!= NULL
) {
540 gBS
->CloseEvent(ShellInfoObject
.UserBreakTimer
);
541 DEBUG_CODE(ShellInfoObject
.UserBreakTimer
= NULL
;);
543 if (ShellInfoObject
.ImageDevPath
!= NULL
) {
544 FreePool(ShellInfoObject
.ImageDevPath
);
545 DEBUG_CODE(ShellInfoObject
.ImageDevPath
= NULL
;);
547 if (ShellInfoObject
.FileDevPath
!= NULL
) {
548 FreePool(ShellInfoObject
.FileDevPath
);
549 DEBUG_CODE(ShellInfoObject
.FileDevPath
= NULL
;);
551 if (ShellInfoObject
.NewShellParametersProtocol
!= NULL
) {
552 CleanUpShellParametersProtocol(ShellInfoObject
.NewShellParametersProtocol
);
553 DEBUG_CODE(ShellInfoObject
.NewShellParametersProtocol
= NULL
;);
555 if (ShellInfoObject
.NewEfiShellProtocol
!= NULL
){
556 if (ShellInfoObject
.NewEfiShellProtocol
->IsRootShell()){
557 InternalEfiShellSetEnv(L
"cwd", NULL
, TRUE
);
559 CleanUpShellProtocol(ShellInfoObject
.NewEfiShellProtocol
);
560 DEBUG_CODE(ShellInfoObject
.NewEfiShellProtocol
= NULL
;);
563 if (!IsListEmpty(&ShellInfoObject
.BufferToFreeList
.Link
)){
564 FreeBufferList(&ShellInfoObject
.BufferToFreeList
);
567 if (!IsListEmpty(&ShellInfoObject
.SplitList
.Link
)){
568 ASSERT(FALSE
); ///@todo finish this de-allocation.
571 if (ShellInfoObject
.ShellInitSettings
.FileName
!= NULL
) {
572 FreePool(ShellInfoObject
.ShellInitSettings
.FileName
);
573 DEBUG_CODE(ShellInfoObject
.ShellInitSettings
.FileName
= NULL
;);
576 if (ShellInfoObject
.ShellInitSettings
.FileOptions
!= NULL
) {
577 FreePool(ShellInfoObject
.ShellInitSettings
.FileOptions
);
578 DEBUG_CODE(ShellInfoObject
.ShellInitSettings
.FileOptions
= NULL
;);
581 if (ShellInfoObject
.HiiHandle
!= NULL
) {
582 HiiRemovePackages(ShellInfoObject
.HiiHandle
);
583 DEBUG_CODE(ShellInfoObject
.HiiHandle
= NULL
;);
586 if (!IsListEmpty(&ShellInfoObject
.ViewingSettings
.CommandHistory
.Link
)){
587 FreeBufferList(&ShellInfoObject
.ViewingSettings
.CommandHistory
);
590 ASSERT(ShellInfoObject
.ConsoleInfo
!= NULL
);
591 if (ShellInfoObject
.ConsoleInfo
!= NULL
) {
592 ConsoleLoggerUninstall(ShellInfoObject
.ConsoleInfo
);
593 FreePool(ShellInfoObject
.ConsoleInfo
);
594 DEBUG_CODE(ShellInfoObject
.ConsoleInfo
= NULL
;);
597 if (!EFI_ERROR (Status
)) {
598 // If the command exited with an error, we pass this error out in the ExitData
599 // so that it can be retrieved by the EfiShellExecute function (which may
600 // start the shell with gBS->StartImage)
601 if (ExitStatus
!= SHELL_SUCCESS
) {
602 // Allocate a buffer for exit data to pass to gBS->Exit().
603 // This buffer will contain the empty string immediately followed by
604 // the shell's exit status. (The empty string is required by the UEFI spec)
605 ExitDataSize
= (sizeof (CHAR16
) + sizeof (SHELL_STATUS
));
606 ExitData
= AllocatePool (ExitDataSize
);
607 if (ExitData
== NULL
) {
608 return EFI_OUT_OF_RESOURCES
;
611 // Use CopyMem to avoid alignment faults
612 CopyMem ((ExitData
+ 1), &ExitStatus
, sizeof (ExitStatus
));
614 gBS
->Exit (ImageHandle
, EFI_ABORTED
, ExitDataSize
, ExitData
);
627 Sets all the alias' that were registered with the ShellCommandLib library.
629 @retval EFI_SUCCESS all init commands were run sucessfully.
637 CONST ALIAS_LIST
*List
;
641 // Get all the commands we want to run
643 List
= ShellCommandGetInitAliasList();
646 // for each command in the List
648 for ( Node
= (ALIAS_LIST
*)GetFirstNode(&List
->Link
)
649 ; !IsNull (&List
->Link
, &Node
->Link
)
650 ; Node
= (ALIAS_LIST
*)GetNextNode(&List
->Link
, &Node
->Link
)
653 // install the alias'
655 Status
= InternalSetAlias(Node
->CommandString
, Node
->Alias
, TRUE
);
656 ASSERT_EFI_ERROR(Status
);
658 return (EFI_SUCCESS
);
662 Internal function to determine if 2 command names are really the same.
664 @param[in] Command1 The pointer to the first command name.
665 @param[in] Command2 The pointer to the second command name.
667 @retval TRUE The 2 command names are the same.
668 @retval FALSE The 2 command names are not the same.
673 IN CONST CHAR16
*Command1
,
674 IN CONST CHAR16
*Command2
677 if (StringNoCaseCompare(&Command1
, &Command2
) == 0) {
684 Internal function to determine if a command is a script only command.
686 @param[in] CommandName The pointer to the command name.
688 @retval TRUE The command is a script only command.
689 @retval FALSE The command is not a script only command.
694 IN CONST CHAR16
*CommandName
697 if (IsCommand(CommandName
, L
"for")
698 ||IsCommand(CommandName
, L
"endfor")
699 ||IsCommand(CommandName
, L
"if")
700 ||IsCommand(CommandName
, L
"else")
701 ||IsCommand(CommandName
, L
"endif")
702 ||IsCommand(CommandName
, L
"goto")) {
709 This function will populate the 2 device path protocol parameters based on the
710 global gImageHandle. The DevPath will point to the device path for the handle that has
711 loaded image protocol installed on it. The FilePath will point to the device path
712 for the file that was loaded.
714 @param[in, out] DevPath On a sucessful return the device path to the loaded image.
715 @param[in, out] FilePath On a sucessful return the device path to the file.
717 @retval EFI_SUCCESS The 2 device paths were sucessfully returned.
718 @retval other A error from gBS->HandleProtocol.
724 GetDevicePathsForImageAndFile (
725 IN OUT EFI_DEVICE_PATH_PROTOCOL
**DevPath
,
726 IN OUT EFI_DEVICE_PATH_PROTOCOL
**FilePath
730 EFI_LOADED_IMAGE_PROTOCOL
*LoadedImage
;
731 EFI_DEVICE_PATH_PROTOCOL
*ImageDevicePath
;
733 ASSERT(DevPath
!= NULL
);
734 ASSERT(FilePath
!= NULL
);
736 Status
= gBS
->OpenProtocol (
738 &gEfiLoadedImageProtocolGuid
,
739 (VOID
**)&LoadedImage
,
742 EFI_OPEN_PROTOCOL_GET_PROTOCOL
744 if (!EFI_ERROR (Status
)) {
745 Status
= gBS
->OpenProtocol (
746 LoadedImage
->DeviceHandle
,
747 &gEfiDevicePathProtocolGuid
,
748 (VOID
**)&ImageDevicePath
,
751 EFI_OPEN_PROTOCOL_GET_PROTOCOL
753 if (!EFI_ERROR (Status
)) {
754 *DevPath
= DuplicateDevicePath (ImageDevicePath
);
755 *FilePath
= DuplicateDevicePath (LoadedImage
->FilePath
);
757 LoadedImage
->DeviceHandle
,
758 &gEfiDevicePathProtocolGuid
,
764 &gEfiLoadedImageProtocolGuid
,
772 Process all Uefi Shell 2.0 command line options.
774 see Uefi Shell 2.0 section 3.2 for full details.
776 the command line must resemble the following:
778 shell.efi [ShellOpt-options] [options] [file-name [file-name-options]]
780 ShellOpt-options Options which control the initialization behavior of the shell.
781 These options are read from the EFI global variable "ShellOpt"
782 and are processed before options or file-name.
784 options Options which control the initialization behavior of the shell.
786 file-name The name of a UEFI shell application or script to be executed
787 after initialization is complete. By default, if file-name is
788 specified, then -nostartup is implied. Scripts are not supported
791 file-name-options The command-line options that are passed to file-name when it
794 This will initialize the ShellInfoObject.ShellInitSettings global variable.
796 @retval EFI_SUCCESS The variable is initialized.
807 CHAR16
*DelayValueStr
;
810 EFI_UNICODE_COLLATION_PROTOCOL
*UnicodeCollation
;
812 // `file-name-options` will contain arguments to `file-name` that we don't
813 // know about. This would cause ShellCommandLineParse to error, so we parse
814 // arguments manually, ignoring those after the first thing that doesn't look
815 // like a shell option (which is assumed to be `file-name`).
817 Status
= gBS
->LocateProtocol (
818 &gEfiUnicodeCollationProtocolGuid
,
820 (VOID
**) &UnicodeCollation
822 if (EFI_ERROR (Status
)) {
826 // Set default options
827 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.Startup
= FALSE
;
828 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoStartup
= FALSE
;
829 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoConsoleOut
= FALSE
;
830 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoConsoleIn
= FALSE
;
831 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoInterrupt
= FALSE
;
832 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoMap
= FALSE
;
833 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoVersion
= FALSE
;
834 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.Delay
= FALSE
;
835 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.Exit
= FALSE
;
836 ShellInfoObject
.ShellInitSettings
.Delay
= 5;
838 // Start LoopVar at 1 to ignore Argv[0] which is the name of this binary
839 // (probably "Shell.efi")
840 for (LoopVar
= 1 ; LoopVar
< gEfiShellParametersProtocol
->Argc
; LoopVar
++) {
841 CurrentArg
= gEfiShellParametersProtocol
->Argv
[LoopVar
];
842 if (UnicodeCollation
->StriColl (
847 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.Startup
= TRUE
;
849 else if (UnicodeCollation
->StriColl (
854 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoStartup
= TRUE
;
856 else if (UnicodeCollation
->StriColl (
861 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoConsoleOut
= TRUE
;
863 else if (UnicodeCollation
->StriColl (
868 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoConsoleIn
= TRUE
;
870 else if (UnicodeCollation
->StriColl (
875 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoInterrupt
= TRUE
;
877 else if (UnicodeCollation
->StriColl (
882 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoMap
= TRUE
;
884 else if (UnicodeCollation
->StriColl (
889 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoVersion
= TRUE
;
891 else if (UnicodeCollation
->StriColl (
896 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.Delay
= TRUE
;
897 // Check for optional delay value following "-delay"
898 DelayValueStr
= gEfiShellParametersProtocol
->Argv
[LoopVar
+ 1];
899 if (DelayValueStr
!= NULL
){
900 if (*DelayValueStr
== L
':') {
903 if (!EFI_ERROR(ShellConvertStringToUint64 (
909 ShellInfoObject
.ShellInitSettings
.Delay
= (UINTN
)DelayValue
;
913 } else if (UnicodeCollation
->StriColl (
918 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.Exit
= TRUE
;
919 } else if (StrnCmp (L
"-", CurrentArg
, 1) == 0) {
920 // Unrecognised option
921 ShellPrintHiiEx(-1, -1, NULL
,
922 STRING_TOKEN (STR_GEN_PROBLEM
),
923 ShellInfoObject
.HiiHandle
,
926 return EFI_INVALID_PARAMETER
;
928 ShellInfoObject
.ShellInitSettings
.FileName
= AllocateZeroPool(StrSize(CurrentArg
));
929 if (ShellInfoObject
.ShellInitSettings
.FileName
== NULL
) {
930 return (EFI_OUT_OF_RESOURCES
);
933 // We found `file-name`.
935 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoStartup
= 1;
937 StrCpy (ShellInfoObject
.ShellInitSettings
.FileName
, CurrentArg
);
940 // Add `file-name-options`
941 for (Size
= 0 ; LoopVar
< gEfiShellParametersProtocol
->Argc
; LoopVar
++) {
942 ASSERT((ShellInfoObject
.ShellInitSettings
.FileOptions
== NULL
&& Size
== 0) || (ShellInfoObject
.ShellInitSettings
.FileOptions
!= NULL
));
943 StrnCatGrow(&ShellInfoObject
.ShellInitSettings
.FileOptions
,
947 if (ShellInfoObject
.ShellInitSettings
.FileOptions
== NULL
) {
948 SHELL_FREE_NON_NULL(ShellInfoObject
.ShellInitSettings
.FileName
);
949 return (EFI_OUT_OF_RESOURCES
);
951 StrnCatGrow(&ShellInfoObject
.ShellInitSettings
.FileOptions
,
953 gEfiShellParametersProtocol
->Argv
[LoopVar
],
955 if (ShellInfoObject
.ShellInitSettings
.FileOptions
== NULL
) {
956 SHELL_FREE_NON_NULL(ShellInfoObject
.ShellInitSettings
.FileName
);
957 return (EFI_OUT_OF_RESOURCES
);
963 // "-nointerrupt" overrides "-delay"
964 if (ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoInterrupt
) {
965 ShellInfoObject
.ShellInitSettings
.Delay
= 0;
972 Handles all interaction with the default startup script.
974 this will check that the correct command line parameters were passed, handle the delay, and then start running the script.
976 @param ImagePath the path to the image for shell. first place to look for the startup script
977 @param FilePath the path to the file for shell. second place to look for the startup script.
979 @param[out] ExitStatus The exit code of the script. Ignored if NULL.
981 @retval EFI_SUCCESS the variable is initialized.
986 IN EFI_DEVICE_PATH_PROTOCOL
*ImagePath
,
987 IN EFI_DEVICE_PATH_PROTOCOL
*FilePath
,
988 OUT SHELL_STATUS
*ExitStatus
994 SHELL_FILE_HANDLE FileHandle
;
995 EFI_DEVICE_PATH_PROTOCOL
*NewPath
;
996 EFI_DEVICE_PATH_PROTOCOL
*NamePath
;
997 CHAR16
*FileStringPath
;
1000 CONST CHAR16
*MapName
;
1002 Key
.UnicodeChar
= CHAR_NULL
;
1006 if (!ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.Startup
&& ShellInfoObject
.ShellInitSettings
.FileName
!= NULL
) {
1008 // launch something else instead
1010 NewSize
= StrSize(ShellInfoObject
.ShellInitSettings
.FileName
);
1011 if (ShellInfoObject
.ShellInitSettings
.FileOptions
!= NULL
) {
1012 NewSize
+= StrSize(ShellInfoObject
.ShellInitSettings
.FileOptions
) + sizeof(CHAR16
);
1014 FileStringPath
= AllocateZeroPool(NewSize
);
1015 if (FileStringPath
== NULL
) {
1016 return (EFI_OUT_OF_RESOURCES
);
1018 StrCpy(FileStringPath
, ShellInfoObject
.ShellInitSettings
.FileName
);
1019 if (ShellInfoObject
.ShellInitSettings
.FileOptions
!= NULL
) {
1020 StrCat(FileStringPath
, L
" ");
1021 StrCat(FileStringPath
, ShellInfoObject
.ShellInitSettings
.FileOptions
);
1023 Status
= RunCommand(FileStringPath
, ExitStatus
);
1024 FreePool(FileStringPath
);
1030 // for shell level 0 we do no scripts
1031 // Without the Startup bit overriding we allow for nostartup to prevent scripts
1033 if ( (PcdGet8(PcdShellSupportLevel
) < 1)
1034 || (ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoStartup
&& !ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.Startup
)
1036 return (EFI_SUCCESS
);
1039 gST
->ConOut
->EnableCursor(gST
->ConOut
, FALSE
);
1041 // print out our warning and see if they press a key
1043 for ( Status
= EFI_UNSUPPORTED
, Delay
= ShellInfoObject
.ShellInitSettings
.Delay
1044 ; Delay
!= 0 && EFI_ERROR(Status
)
1047 ShellPrintHiiEx(0, gST
->ConOut
->Mode
->CursorRow
, NULL
, STRING_TOKEN (STR_SHELL_STARTUP_QUESTION
), ShellInfoObject
.HiiHandle
, Delay
);
1048 gBS
->Stall (1000000);
1049 if (!ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoConsoleIn
) {
1050 Status
= gST
->ConIn
->ReadKeyStroke (gST
->ConIn
, &Key
);
1053 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_CRLF
), ShellInfoObject
.HiiHandle
);
1054 gST
->ConOut
->EnableCursor(gST
->ConOut
, TRUE
);
1059 if (Status
== EFI_SUCCESS
&& Key
.UnicodeChar
== 0 && Key
.ScanCode
== SCAN_ESC
) {
1060 return (EFI_SUCCESS
);
1064 // Try the first location (must be file system)
1066 MapName
= ShellInfoObject
.NewEfiShellProtocol
->GetMapFromDevicePath(&ImagePath
);
1067 if (MapName
!= NULL
) {
1068 FileStringPath
= NULL
;
1070 FileStringPath
= StrnCatGrow(&FileStringPath
, &NewSize
, MapName
, 0);
1071 if (FileStringPath
== NULL
) {
1072 Status
= EFI_OUT_OF_RESOURCES
;
1074 TempSpot
= StrStr(FileStringPath
, L
";");
1075 if (TempSpot
!= NULL
) {
1076 *TempSpot
= CHAR_NULL
;
1078 FileStringPath
= StrnCatGrow(&FileStringPath
, &NewSize
, ((FILEPATH_DEVICE_PATH
*)FilePath
)->PathName
, 0);
1079 PathRemoveLastItem(FileStringPath
);
1080 FileStringPath
= StrnCatGrow(&FileStringPath
, &NewSize
, mStartupScript
, 0);
1081 Status
= ShellInfoObject
.NewEfiShellProtocol
->OpenFileByName(FileStringPath
, &FileHandle
, EFI_FILE_MODE_READ
);
1082 FreePool(FileStringPath
);
1085 if (EFI_ERROR(Status
)) {
1086 NamePath
= FileDevicePath (NULL
, mStartupScript
);
1087 NewPath
= AppendDevicePathNode (ImagePath
, NamePath
);
1093 Status
= InternalOpenFileDevicePath(NewPath
, &FileHandle
, EFI_FILE_MODE_READ
, 0);
1097 // If we got a file, run it
1099 if (!EFI_ERROR(Status
) && FileHandle
!= NULL
) {
1100 Status
= RunScriptFile (
1104 ShellInfoObject
.NewShellParametersProtocol
,
1107 ShellInfoObject
.NewEfiShellProtocol
->CloseFile(FileHandle
);
1109 FileStringPath
= ShellFindFilePath(mStartupScript
);
1110 if (FileStringPath
== NULL
) {
1112 // we return success since we dont need to have a startup script
1114 Status
= EFI_SUCCESS
;
1115 ASSERT(FileHandle
== NULL
);
1117 Status
= RunScriptFile(
1121 ShellInfoObject
.NewShellParametersProtocol
,
1124 FreePool(FileStringPath
);
1133 Function to perform the shell prompt looping. It will do a single prompt,
1134 dispatch the result, and then return. It is expected that the caller will
1135 call this function in a loop many times.
1138 @retval RETURN_ABORTED
1149 CONST CHAR16
*CurDir
;
1156 // Get screen setting to decide size of the command line buffer
1158 gST
->ConOut
->QueryMode (gST
->ConOut
, gST
->ConOut
->Mode
->Mode
, &Column
, &Row
);
1159 BufferSize
= Column
* Row
* sizeof (CHAR16
);
1160 CmdLine
= AllocateZeroPool (BufferSize
);
1161 if (CmdLine
== NULL
) {
1162 return EFI_OUT_OF_RESOURCES
;
1165 CurDir
= ShellInfoObject
.NewEfiShellProtocol
->GetEnv(L
"cwd");
1170 gST
->ConOut
->SetCursorPosition (gST
->ConOut
, 0, gST
->ConOut
->Mode
->CursorRow
);
1172 if (CurDir
!= NULL
&& StrLen(CurDir
) > 1) {
1173 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_CURDIR
), ShellInfoObject
.HiiHandle
, CurDir
);
1175 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_SHELL
), ShellInfoObject
.HiiHandle
);
1179 // Read a line from the console
1181 Status
= ShellInfoObject
.NewEfiShellProtocol
->ReadFile(ShellInfoObject
.NewShellParametersProtocol
->StdIn
, &BufferSize
, CmdLine
);
1184 // Null terminate the string and parse it
1186 if (!EFI_ERROR (Status
)) {
1187 CmdLine
[BufferSize
/ sizeof (CHAR16
)] = CHAR_NULL
;
1188 Status
= RunCommand(CmdLine
, NULL
);
1192 // Done with this command
1199 Add a buffer to the Buffer To Free List for safely returning buffers to other
1200 places without risking letting them modify internal shell information.
1202 @param Buffer Something to pass to FreePool when the shell is exiting.
1206 AddBufferToFreeList(
1210 BUFFER_LIST
*BufferListEntry
;
1212 if (Buffer
== NULL
) {
1216 BufferListEntry
= AllocateZeroPool(sizeof(BUFFER_LIST
));
1217 ASSERT(BufferListEntry
!= NULL
);
1218 BufferListEntry
->Buffer
= Buffer
;
1219 InsertTailList(&ShellInfoObject
.BufferToFreeList
.Link
, &BufferListEntry
->Link
);
1224 Add a buffer to the Line History List
1226 @param Buffer The line buffer to add.
1230 AddLineToCommandHistory(
1231 IN CONST CHAR16
*Buffer
1236 Node
= AllocateZeroPool(sizeof(BUFFER_LIST
));
1237 ASSERT(Node
!= NULL
);
1238 Node
->Buffer
= AllocateZeroPool(StrSize(Buffer
));
1239 ASSERT(Node
->Buffer
!= NULL
);
1240 StrCpy(Node
->Buffer
, Buffer
);
1242 InsertTailList(&ShellInfoObject
.ViewingSettings
.CommandHistory
.Link
, &Node
->Link
);
1246 Checks if a string is an alias for another command. If yes, then it replaces the alias name
1247 with the correct command name.
1249 @param[in, out] CommandString Upon entry the potential alias. Upon return the
1250 command name if it was an alias. If it was not
1251 an alias it will be unchanged. This function may
1252 change the buffer to fit the command name.
1254 @retval EFI_SUCCESS The name was changed.
1255 @retval EFI_SUCCESS The name was not an alias.
1256 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
1261 IN OUT CHAR16
**CommandString
1264 CONST CHAR16
*NewString
;
1266 NewString
= ShellInfoObject
.NewEfiShellProtocol
->GetAlias(*CommandString
, NULL
);
1267 if (NewString
== NULL
) {
1268 return (EFI_SUCCESS
);
1270 FreePool(*CommandString
);
1271 *CommandString
= AllocateZeroPool(StrSize(NewString
));
1272 if (*CommandString
== NULL
) {
1273 return (EFI_OUT_OF_RESOURCES
);
1275 StrCpy(*CommandString
, NewString
);
1276 return (EFI_SUCCESS
);
1280 Function allocates a new command line and replaces all instances of environment
1281 variable names that are correctly preset to their values.
1283 If the return value is not NULL the memory must be caller freed.
1285 @param[in] OriginalCommandLine The original command line
1287 @retval NULL An error ocurred.
1288 @return The new command line with no environment variables present.
1292 ShellConvertVariables (
1293 IN CONST CHAR16
*OriginalCommandLine
1296 CONST CHAR16
*MasterEnvList
;
1298 CHAR16
*NewCommandLine1
;
1299 CHAR16
*NewCommandLine2
;
1304 SCRIPT_FILE
*CurrentScriptFile
;
1305 ALIAS_LIST
*AliasListNode
;
1307 ASSERT(OriginalCommandLine
!= NULL
);
1310 NewSize
= StrSize(OriginalCommandLine
);
1311 CurrentScriptFile
= ShellCommandGetCurrentScriptFile();
1314 ///@todo update this to handle the %0 - %9 for scripting only (borrow from line 1256 area) ? ? ?
1317 // calculate the size required for the post-conversion string...
1319 if (CurrentScriptFile
!= NULL
) {
1320 for (AliasListNode
= (ALIAS_LIST
*)GetFirstNode(&CurrentScriptFile
->SubstList
)
1321 ; !IsNull(&CurrentScriptFile
->SubstList
, &AliasListNode
->Link
)
1322 ; AliasListNode
= (ALIAS_LIST
*)GetNextNode(&CurrentScriptFile
->SubstList
, &AliasListNode
->Link
)
1324 for (Temp
= StrStr(OriginalCommandLine
, AliasListNode
->Alias
)
1326 ; Temp
= StrStr(Temp
+1, AliasListNode
->Alias
)
1329 // we need a preceeding and if there is space no ^ preceeding (if no space ignore)
1331 if ((((Temp
-OriginalCommandLine
)>2) && *(Temp
-2) != L
'^') || ((Temp
-OriginalCommandLine
)<=2)) {
1332 NewSize
+= StrSize(AliasListNode
->CommandString
);
1338 for (MasterEnvList
= EfiShellGetEnv(NULL
)
1339 ; MasterEnvList
!= NULL
&& *MasterEnvList
!= CHAR_NULL
//&& *(MasterEnvList+1) != CHAR_NULL
1340 ; MasterEnvList
+= StrLen(MasterEnvList
) + 1
1342 if (StrSize(MasterEnvList
) > ItemSize
) {
1343 ItemSize
= StrSize(MasterEnvList
);
1345 for (Temp
= StrStr(OriginalCommandLine
, MasterEnvList
)
1347 ; Temp
= StrStr(Temp
+1, MasterEnvList
)
1350 // we need a preceeding and following % and if there is space no ^ preceeding (if no space ignore)
1352 if (*(Temp
-1) == L
'%' && *(Temp
+StrLen(MasterEnvList
)) == L
'%' &&
1353 ((((Temp
-OriginalCommandLine
)>2) && *(Temp
-2) != L
'^') || ((Temp
-OriginalCommandLine
)<=2))) {
1354 NewSize
+=StrSize(EfiShellGetEnv(MasterEnvList
));
1360 // now do the replacements...
1362 NewCommandLine1
= AllocateZeroPool(NewSize
);
1363 NewCommandLine2
= AllocateZeroPool(NewSize
);
1364 ItemTemp
= AllocateZeroPool(ItemSize
+(2*sizeof(CHAR16
)));
1365 if (NewCommandLine1
== NULL
|| NewCommandLine2
== NULL
|| ItemTemp
== NULL
) {
1366 SHELL_FREE_NON_NULL(NewCommandLine1
);
1367 SHELL_FREE_NON_NULL(NewCommandLine2
);
1368 SHELL_FREE_NON_NULL(ItemTemp
);
1371 StrCpy(NewCommandLine1
, OriginalCommandLine
);
1372 for (MasterEnvList
= EfiShellGetEnv(NULL
)
1373 ; MasterEnvList
!= NULL
&& *MasterEnvList
!= CHAR_NULL
//&& *(MasterEnvList+1) != CHAR_NULL
1374 ; MasterEnvList
+= StrLen(MasterEnvList
) + 1
1376 StrCpy(ItemTemp
, L
"%");
1377 StrCat(ItemTemp
, MasterEnvList
);
1378 StrCat(ItemTemp
, L
"%");
1379 ShellCopySearchAndReplace(NewCommandLine1
, NewCommandLine2
, NewSize
, ItemTemp
, EfiShellGetEnv(MasterEnvList
), TRUE
, FALSE
);
1380 StrCpy(NewCommandLine1
, NewCommandLine2
);
1382 if (CurrentScriptFile
!= NULL
) {
1383 for (AliasListNode
= (ALIAS_LIST
*)GetFirstNode(&CurrentScriptFile
->SubstList
)
1384 ; !IsNull(&CurrentScriptFile
->SubstList
, &AliasListNode
->Link
)
1385 ; AliasListNode
= (ALIAS_LIST
*)GetNextNode(&CurrentScriptFile
->SubstList
, &AliasListNode
->Link
)
1387 ShellCopySearchAndReplace(NewCommandLine1
, NewCommandLine2
, NewSize
, AliasListNode
->Alias
, AliasListNode
->CommandString
, TRUE
, FALSE
);
1388 StrCpy(NewCommandLine1
, NewCommandLine2
);
1392 // Remove non-existant environment variables in scripts only
1394 for (Temp
= NewCommandLine1
; Temp
!= NULL
; ) {
1395 Temp
= StrStr(Temp
, L
"%");
1399 while (*(Temp
- 1) == L
'^') {
1400 Temp
= StrStr(Temp
+ 1, L
"%");
1409 Temp2
= StrStr(Temp
+ 1, L
"%");
1410 if (Temp2
== NULL
) {
1413 while (*(Temp2
- 1) == L
'^') {
1414 Temp2
= StrStr(Temp2
+ 1, L
"%");
1415 if (Temp2
== NULL
) {
1419 if (Temp2
== NULL
) {
1424 CopyMem(Temp
, Temp2
, StrSize(Temp2
));
1430 // Now cleanup any straggler intentionally ignored "%" characters
1432 ShellCopySearchAndReplace(NewCommandLine1
, NewCommandLine2
, NewSize
, L
"^%", L
"%", TRUE
, FALSE
);
1433 StrCpy(NewCommandLine1
, NewCommandLine2
);
1435 FreePool(NewCommandLine2
);
1438 return (NewCommandLine1
);
1442 Internal function to run a command line with pipe usage.
1444 @param[in] CmdLine The pointer to the command line.
1445 @param[in] StdIn The pointer to the Standard input.
1446 @param[in] StdOut The pointer to the Standard output.
1448 @param[out] ExitStatus The exit code of the last command in the pipeline.
1451 @retval EFI_SUCCESS The split command is executed successfully.
1452 @retval other Some error occurs when executing the split command.
1457 IN CONST CHAR16
*CmdLine
,
1458 IN SHELL_FILE_HANDLE
*StdIn
,
1459 IN SHELL_FILE_HANDLE
*StdOut
,
1460 OUT SHELL_STATUS
*ExitStatus
1464 CHAR16
*NextCommandLine
;
1465 CHAR16
*OurCommandLine
;
1469 SHELL_FILE_HANDLE
*TempFileHandle
;
1472 ASSERT(StdOut
== NULL
);
1474 ASSERT(StrStr(CmdLine
, L
"|") != NULL
);
1476 Status
= EFI_SUCCESS
;
1477 NextCommandLine
= NULL
;
1478 OurCommandLine
= NULL
;
1482 NextCommandLine
= StrnCatGrow(&NextCommandLine
, &Size1
, StrStr(CmdLine
, L
"|")+1, 0);
1483 OurCommandLine
= StrnCatGrow(&OurCommandLine
, &Size2
, CmdLine
, StrStr(CmdLine
, L
"|") - CmdLine
);
1485 if (NextCommandLine
== NULL
|| OurCommandLine
== NULL
) {
1486 SHELL_FREE_NON_NULL(OurCommandLine
);
1487 SHELL_FREE_NON_NULL(NextCommandLine
);
1488 return (EFI_OUT_OF_RESOURCES
);
1489 } else if (StrStr(OurCommandLine
, L
"|") != NULL
|| Size1
== 0 || Size2
== 0) {
1490 SHELL_FREE_NON_NULL(OurCommandLine
);
1491 SHELL_FREE_NON_NULL(NextCommandLine
);
1492 return (EFI_INVALID_PARAMETER
);
1493 } else if (NextCommandLine
[0] != CHAR_NULL
&&
1494 NextCommandLine
[0] == L
'a' &&
1495 NextCommandLine
[1] == L
' '
1497 CopyMem(NextCommandLine
, NextCommandLine
+1, StrSize(NextCommandLine
) - sizeof(NextCommandLine
[0]));
1505 // make a SPLIT_LIST item and add to list
1507 Split
= AllocateZeroPool(sizeof(SPLIT_LIST
));
1508 ASSERT(Split
!= NULL
);
1509 Split
->SplitStdIn
= StdIn
;
1510 Split
->SplitStdOut
= ConvertEfiFileProtocolToShellHandle(CreateFileInterfaceMem(Unicode
), NULL
);
1511 ASSERT(Split
->SplitStdOut
!= NULL
);
1512 InsertHeadList(&ShellInfoObject
.SplitList
.Link
, &Split
->Link
);
1514 Status
= RunCommand(OurCommandLine
, NULL
);
1517 // move the output from the first to the in to the second.
1519 TempFileHandle
= Split
->SplitStdOut
;
1520 if (Split
->SplitStdIn
== StdIn
) {
1521 Split
->SplitStdOut
= NULL
;
1523 Split
->SplitStdOut
= Split
->SplitStdIn
;
1525 Split
->SplitStdIn
= TempFileHandle
;
1526 ShellInfoObject
.NewEfiShellProtocol
->SetFilePosition(ConvertShellHandleToEfiFileProtocol(Split
->SplitStdIn
), 0);
1528 if (!EFI_ERROR(Status
)) {
1529 Status
= RunCommand(NextCommandLine
, ExitStatus
);
1533 // remove the top level from the ScriptList
1535 ASSERT((SPLIT_LIST
*)GetFirstNode(&ShellInfoObject
.SplitList
.Link
) == Split
);
1536 RemoveEntryList(&Split
->Link
);
1539 // Note that the original StdIn is now the StdOut...
1541 if (Split
->SplitStdOut
!= NULL
&& Split
->SplitStdOut
!= StdIn
) {
1542 ShellInfoObject
.NewEfiShellProtocol
->CloseFile(ConvertShellHandleToEfiFileProtocol(Split
->SplitStdOut
));
1544 if (Split
->SplitStdIn
!= NULL
) {
1545 ShellInfoObject
.NewEfiShellProtocol
->CloseFile(ConvertShellHandleToEfiFileProtocol(Split
->SplitStdIn
));
1549 FreePool(NextCommandLine
);
1550 FreePool(OurCommandLine
);
1556 Take the original command line, substitute any variables, free
1557 the original string, return the modified copy.
1559 @param[in] CmdLine pointer to the command line to update.
1561 @retval EFI_SUCCESS the function was successful.
1562 @retval EFI_OUT_OF_RESOURCES a memory allocation failed.
1566 ShellSubstituteVariables(
1571 NewCmdLine
= ShellConvertVariables(*CmdLine
);
1572 SHELL_FREE_NON_NULL(*CmdLine
);
1573 if (NewCmdLine
== NULL
) {
1574 return (EFI_OUT_OF_RESOURCES
);
1576 *CmdLine
= NewCmdLine
;
1577 return (EFI_SUCCESS
);
1581 Take the original command line, substitute any alias in the first group of space delimited characters, free
1582 the original string, return the modified copy.
1584 @param[in] CmdLine pointer to the command line to update.
1586 @retval EFI_SUCCESS the function was successful.
1587 @retval EFI_OUT_OF_RESOURCES a memory allocation failed.
1591 ShellSubstituteAliases(
1596 CHAR16
*CommandName
;
1598 UINTN PostAliasSize
;
1599 ASSERT(CmdLine
!= NULL
);
1600 ASSERT(*CmdLine
!= NULL
);
1604 if (StrStr((*CmdLine
), L
" ") == NULL
){
1605 StrnCatGrow(&CommandName
, NULL
, (*CmdLine
), 0);
1607 StrnCatGrow(&CommandName
, NULL
, (*CmdLine
), StrStr((*CmdLine
), L
" ") - (*CmdLine
));
1611 // This cannot happen 'inline' since the CmdLine can need extra space.
1614 if (!ShellCommandIsCommandOnList(CommandName
)) {
1616 // Convert via alias
1618 Status
= ShellConvertAlias(&CommandName
);
1619 if (EFI_ERROR(Status
)){
1623 NewCmdLine
= StrnCatGrow(&NewCmdLine
, &PostAliasSize
, CommandName
, 0);
1624 if (NewCmdLine
== NULL
) {
1625 SHELL_FREE_NON_NULL(CommandName
);
1626 SHELL_FREE_NON_NULL(*CmdLine
);
1627 return (EFI_OUT_OF_RESOURCES
);
1629 NewCmdLine
= StrnCatGrow(&NewCmdLine
, &PostAliasSize
, StrStr((*CmdLine
), L
" "), 0);
1630 if (NewCmdLine
== NULL
) {
1631 SHELL_FREE_NON_NULL(CommandName
);
1632 SHELL_FREE_NON_NULL(*CmdLine
);
1633 return (EFI_OUT_OF_RESOURCES
);
1636 NewCmdLine
= StrnCatGrow(&NewCmdLine
, NULL
, (*CmdLine
), 0);
1639 SHELL_FREE_NON_NULL(*CmdLine
);
1640 SHELL_FREE_NON_NULL(CommandName
);
1643 // re-assign the passed in double pointer to point to our newly allocated buffer
1645 *CmdLine
= NewCmdLine
;
1647 return (EFI_SUCCESS
);
1651 Takes the Argv[0] part of the command line and determine the meaning of it.
1653 @param[in] CmdName pointer to the command line to update.
1655 @retval Internal_Command The name is an internal command.
1656 @retval File_Sys_Change the name is a file system change.
1657 @retval Script_File_Name the name is a NSH script file.
1658 @retval Unknown_Invalid the name is unknown.
1659 @retval Efi_Application the name is an application (.EFI).
1661 SHELL_OPERATION_TYPES
1664 IN CONST CHAR16
*CmdName
1667 CHAR16
* FileWithPath
;
1668 CONST CHAR16
* TempLocation
;
1669 CONST CHAR16
* TempLocation2
;
1671 FileWithPath
= NULL
;
1673 // test for an internal command.
1675 if (ShellCommandIsCommandOnList(CmdName
)) {
1676 return (Internal_Command
);
1680 // Test for file system change request. anything ending with : and cant have spaces.
1682 if (CmdName
[(StrLen(CmdName
)-1)] == L
':') {
1683 if (StrStr(CmdName
, L
" ") != NULL
) {
1684 return (Unknown_Invalid
);
1686 return (File_Sys_Change
);
1692 if ((FileWithPath
= ShellFindFilePathEx(CmdName
, mExecutableExtensions
)) != NULL
) {
1694 // See if that file has a script file extension
1696 if (StrLen(FileWithPath
) > 4) {
1697 TempLocation
= FileWithPath
+StrLen(FileWithPath
)-4;
1698 TempLocation2
= mScriptExtension
;
1699 if (StringNoCaseCompare((VOID
*)(&TempLocation
), (VOID
*)(&TempLocation2
)) == 0) {
1700 SHELL_FREE_NON_NULL(FileWithPath
);
1701 return (Script_File_Name
);
1706 // Was a file, but not a script. we treat this as an application.
1708 SHELL_FREE_NON_NULL(FileWithPath
);
1709 return (Efi_Application
);
1712 SHELL_FREE_NON_NULL(FileWithPath
);
1714 // No clue what this is... return invalid flag...
1716 return (Unknown_Invalid
);
1720 Determine if the first item in a command line is valid.
1722 @param[in] CmdLine The command line to parse.
1724 @retval EFI_SUCCESS The item is valid.
1725 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
1726 @retval EFI_NOT_FOUND The operation type is unknown or invalid.
1731 IN CONST CHAR16
*CmdLine
1735 CHAR16
*FirstParameter
;
1741 Temp
= StrnCatGrow(&Temp
, NULL
, CmdLine
, 0);
1743 return (EFI_OUT_OF_RESOURCES
);
1746 FirstParameter
= StrStr(Temp
, L
"|");
1747 if (FirstParameter
!= NULL
) {
1748 *FirstParameter
= CHAR_NULL
;
1751 FirstParameter
= NULL
;
1754 // Process the command line
1756 Status
= ProcessCommandLineToFinal(&Temp
);
1758 if (!EFI_ERROR(Status
)) {
1759 FirstParameter
= AllocateZeroPool(StrSize(CmdLine
));
1760 if (FirstParameter
== NULL
) {
1761 SHELL_FREE_NON_NULL(Temp
);
1762 return (EFI_OUT_OF_RESOURCES
);
1764 TempWalker
= (CHAR16
*)Temp
;
1765 GetNextParameter(&TempWalker
, &FirstParameter
);
1767 if (GetOperationType(FirstParameter
) == Unknown_Invalid
) {
1768 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_NOT_FOUND
), ShellInfoObject
.HiiHandle
, FirstParameter
);
1769 SetLastError(SHELL_NOT_FOUND
);
1770 Status
= EFI_NOT_FOUND
;
1774 SHELL_FREE_NON_NULL(Temp
);
1775 SHELL_FREE_NON_NULL(FirstParameter
);
1780 Determine if a command line contains with a split contains only valid commands.
1782 @param[in] CmdLine The command line to parse.
1784 @retval EFI_SUCCESS CmdLine has only valid commands, application, or has no split.
1785 @retval EFI_ABORTED CmdLine has at least one invalid command or application.
1790 IN CONST CHAR16
*CmdLine
1793 CONST CHAR16
*TempSpot
;
1797 // Verify up to the pipe or end character
1799 Status
= IsValidSplit(CmdLine
);
1800 if (EFI_ERROR(Status
)) {
1805 // If this was the only item, then get out
1807 if (!ContainsSplit(CmdLine
)) {
1808 return (EFI_SUCCESS
);
1812 // recurse to verify the next item
1814 TempSpot
= FindSplit(CmdLine
)+1;
1815 return (VerifySplit(TempSpot
));
1819 Process a split based operation.
1821 @param[in] CmdLine Pointer to the command line to process
1822 @param[out] ExitStatus The exit status of the command. Ignored if NULL.
1823 Invalid if this function returns an error.
1825 @retval EFI_SUCCESS The operation was successful
1826 @return an error occured.
1830 ProcessNewSplitCommandLine(
1831 IN CONST CHAR16
*CmdLine
,
1832 OUT SHELL_STATUS
*ExitStatus
1838 Status
= VerifySplit(CmdLine
);
1839 if (EFI_ERROR(Status
)) {
1846 // are we in an existing split???
1848 if (!IsListEmpty(&ShellInfoObject
.SplitList
.Link
)) {
1849 Split
= (SPLIT_LIST
*)GetFirstNode(&ShellInfoObject
.SplitList
.Link
);
1852 if (Split
== NULL
) {
1853 Status
= RunSplitCommand(CmdLine
, NULL
, NULL
, ExitStatus
);
1855 Status
= RunSplitCommand(
1862 if (EFI_ERROR(Status
)) {
1863 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_INVALID_SPLIT
), ShellInfoObject
.HiiHandle
, CmdLine
);
1869 Handle a request to change the current file system.
1871 @param[in] CmdLine The passed in command line.
1873 @retval EFI_SUCCESS The operation was successful.
1878 IN CONST CHAR16
*CmdLine
1882 Status
= EFI_SUCCESS
;
1885 // make sure we are the right operation
1887 ASSERT(CmdLine
[(StrLen(CmdLine
)-1)] == L
':' && StrStr(CmdLine
, L
" ") == NULL
);
1890 // Call the protocol API to do the work
1892 Status
= ShellInfoObject
.NewEfiShellProtocol
->SetCurDir(NULL
, CmdLine
);
1895 // Report any errors
1897 if (EFI_ERROR(Status
)) {
1898 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_INVALID_MAPPING
), ShellInfoObject
.HiiHandle
, CmdLine
);
1905 Reprocess the command line to direct all -? to the help command.
1907 if found, will add "help" as argv[0], and move the rest later.
1909 @param[in,out] CmdLine pointer to the command line to update
1914 IN OUT CHAR16
**CmdLine
1917 CHAR16
*CurrentParameter
;
1920 CHAR16
*NewCommandLine
;
1923 Status
= EFI_SUCCESS
;
1925 CurrentParameter
= AllocateZeroPool(StrSize(*CmdLine
));
1926 if (CurrentParameter
== NULL
) {
1927 return (EFI_OUT_OF_RESOURCES
);
1931 while(Walker
!= NULL
&& *Walker
!= CHAR_NULL
) {
1932 LastWalker
= Walker
;
1933 GetNextParameter(&Walker
, &CurrentParameter
);
1934 if (StrStr(CurrentParameter
, L
"-?") == CurrentParameter
) {
1935 LastWalker
[0] = L
' ';
1936 LastWalker
[1] = L
' ';
1937 NewCommandLine
= AllocateZeroPool(StrSize(L
"help ") + StrSize(*CmdLine
));
1938 if (NewCommandLine
== NULL
) {
1939 Status
= EFI_OUT_OF_RESOURCES
;
1942 StrCpy(NewCommandLine
, L
"help ");
1943 StrCat(NewCommandLine
, *CmdLine
);
1944 SHELL_FREE_NON_NULL(*CmdLine
);
1945 *CmdLine
= NewCommandLine
;
1950 SHELL_FREE_NON_NULL(CurrentParameter
);
1956 Function to update the shell variable "lasterror".
1958 @param[in] ErrorCode the error code to put into lasterror.
1963 IN CONST SHELL_STATUS ErrorCode
1966 CHAR16 LeString
[19];
1967 if (sizeof(EFI_STATUS
) == sizeof(UINT64
)) {
1968 UnicodeSPrint(LeString
, sizeof(LeString
), L
"0x%Lx", ErrorCode
);
1970 UnicodeSPrint(LeString
, sizeof(LeString
), L
"0x%x", ErrorCode
);
1972 DEBUG_CODE(InternalEfiShellSetEnv(L
"debuglasterror", LeString
, TRUE
););
1973 InternalEfiShellSetEnv(L
"lasterror", LeString
, TRUE
);
1975 return (EFI_SUCCESS
);
1979 Converts the command line to it's post-processed form. this replaces variables and alias' per UEFI Shell spec.
1981 @param[in,out] CmdLine pointer to the command line to update
1983 @retval EFI_SUCCESS The operation was successful
1984 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
1985 @return some other error occured
1989 ProcessCommandLineToFinal(
1990 IN OUT CHAR16
**CmdLine
1994 TrimSpaces(CmdLine
);
1996 Status
= ShellSubstituteAliases(CmdLine
);
1997 if (EFI_ERROR(Status
)) {
2001 TrimSpaces(CmdLine
);
2003 Status
= ShellSubstituteVariables(CmdLine
);
2004 if (EFI_ERROR(Status
)) {
2008 TrimSpaces(CmdLine
);
2011 // update for help parsing
2013 if (StrStr(*CmdLine
, L
"?") != NULL
) {
2015 // This may do nothing if the ? does not indicate help.
2016 // Save all the details for in the API below.
2018 Status
= DoHelpUpdate(CmdLine
);
2021 TrimSpaces(CmdLine
);
2023 return (EFI_SUCCESS
);
2027 Run an internal shell command.
2029 This API will upadate the shell's environment since these commands are libraries.
2031 @param[in] CmdLine the command line to run.
2032 @param[in] FirstParameter the first parameter on the command line
2033 @param[in] ParamProtocol the shell parameters protocol pointer
2035 @param[out] ExitStatus The exit code of the command. Ignored if NULL.
2037 @retval EFI_SUCCESS The command was completed.
2038 @retval EFI_ABORTED The command's operation was aborted.
2043 IN CONST CHAR16
*CmdLine
,
2044 IN CHAR16
*FirstParameter
,
2045 IN EFI_SHELL_PARAMETERS_PROTOCOL
*ParamProtocol
,
2046 OUT SHELL_STATUS
*ExitStatus OPTIONAL
2052 SHELL_STATUS CommandReturnedStatus
;
2056 // get the argc and argv updated for internal commands
2058 Status
= UpdateArgcArgv(ParamProtocol
, CmdLine
, &Argv
, &Argc
);
2059 if (!EFI_ERROR(Status
)) {
2061 // Run the internal command.
2063 Status
= ShellCommandRunCommandHandler(FirstParameter
, &CommandReturnedStatus
, &LastError
);
2065 if (!EFI_ERROR(Status
)) {
2067 // Update last error status.
2068 // some commands do not update last error.
2071 SetLastError(CommandReturnedStatus
);
2073 if (ExitStatus
!= NULL
) {
2074 *ExitStatus
= CommandReturnedStatus
;
2078 // Pass thru the exitcode from the app.
2080 if (ShellCommandGetExit()) {
2082 // An Exit was requested ("exit" command), pass its value up.
2084 Status
= CommandReturnedStatus
;
2085 } else if (CommandReturnedStatus
!= SHELL_SUCCESS
&& IsScriptOnlyCommand(FirstParameter
)) {
2087 // Always abort when a script only command fails for any reason
2089 Status
= EFI_ABORTED
;
2090 } else if (ShellCommandGetCurrentScriptFile() != NULL
&& CommandReturnedStatus
== SHELL_ABORTED
) {
2092 // Abort when in a script and a command aborted
2094 Status
= EFI_ABORTED
;
2100 // This is guarenteed to be called after UpdateArgcArgv no matter what else happened.
2101 // This is safe even if the update API failed. In this case, it may be a no-op.
2103 RestoreArgcArgv(ParamProtocol
, &Argv
, &Argc
);
2106 // If a script is running and the command is not a scipt only command, then
2107 // change return value to success so the script won't halt (unless aborted).
2109 // Script only commands have to be able halt the script since the script will
2110 // not operate if they are failing.
2112 if ( ShellCommandGetCurrentScriptFile() != NULL
2113 && !IsScriptOnlyCommand(FirstParameter
)
2114 && Status
!= EFI_ABORTED
2116 Status
= EFI_SUCCESS
;
2123 Function to run the command or file.
2125 @param[in] Type the type of operation being run.
2126 @param[in] CmdLine the command line to run.
2127 @param[in] FirstParameter the first parameter on the command line
2128 @param[in] ParamProtocol the shell parameters protocol pointer
2130 @param[out] ExitStatus The exit code of the command or file.
2133 @retval EFI_SUCCESS The command was completed.
2134 @retval EFI_ABORTED The command's operation was aborted.
2139 IN SHELL_OPERATION_TYPES Type
,
2140 IN CONST CHAR16
*CmdLine
,
2141 IN CHAR16
*FirstParameter
,
2142 IN EFI_SHELL_PARAMETERS_PROTOCOL
*ParamProtocol
,
2143 OUT SHELL_STATUS
*ExitStatus
2147 CHAR16
*CommandWithPath
;
2148 EFI_DEVICE_PATH_PROTOCOL
*DevPath
;
2149 SHELL_STATUS CalleeExitStatus
;
2151 Status
= EFI_SUCCESS
;
2152 CommandWithPath
= NULL
;
2154 CalleeExitStatus
= SHELL_INVALID_PARAMETER
;
2157 case Internal_Command
:
2158 Status
= RunInternalCommand(
2165 case Script_File_Name
:
2166 case Efi_Application
:
2168 // Process a fully qualified path
2170 if (StrStr(FirstParameter
, L
":") != NULL
) {
2171 ASSERT (CommandWithPath
== NULL
);
2172 if (ShellIsFile(FirstParameter
) == EFI_SUCCESS
) {
2173 CommandWithPath
= StrnCatGrow(&CommandWithPath
, NULL
, FirstParameter
, 0);
2178 // Process a relative path and also check in the path environment variable
2180 if (CommandWithPath
== NULL
) {
2181 CommandWithPath
= ShellFindFilePathEx(FirstParameter
, mExecutableExtensions
);
2185 // This should be impossible now.
2187 ASSERT(CommandWithPath
!= NULL
);
2190 // Make sure that path is not just a directory (or not found)
2192 if (!EFI_ERROR(ShellIsDirectory(CommandWithPath
))) {
2193 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_NOT_FOUND
), ShellInfoObject
.HiiHandle
, FirstParameter
);
2194 SetLastError(SHELL_NOT_FOUND
);
2197 case Script_File_Name
:
2198 Status
= RunScriptFile (
2206 case Efi_Application
:
2208 // Get the device path of the application image
2210 DevPath
= ShellInfoObject
.NewEfiShellProtocol
->GetDevicePathFromFilePath(CommandWithPath
);
2211 if (DevPath
== NULL
){
2212 Status
= EFI_OUT_OF_RESOURCES
;
2217 // Execute the device path
2219 Status
= InternalShellExecuteDevicePath(
2228 SHELL_FREE_NON_NULL(DevPath
);
2230 if(EFI_ERROR (Status
)) {
2231 CalleeExitStatus
= (SHELL_STATUS
) (Status
& (~MAX_BIT
));
2233 CalleeExitStatus
= SHELL_SUCCESS
;
2237 // Update last error status.
2239 // Status is an EFI_STATUS. Clear top bit to convert to SHELL_STATUS
2240 SetLastError(CalleeExitStatus
);
2256 SHELL_FREE_NON_NULL(CommandWithPath
);
2258 if (ExitStatus
!= NULL
) {
2259 *ExitStatus
= CalleeExitStatus
;
2266 Function to setup StdIn, StdErr, StdOut, and then run the command or file.
2268 @param[in] Type the type of operation being run.
2269 @param[in] CmdLine the command line to run.
2270 @param[in] FirstParameter the first parameter on the command line.
2271 @param[in] ParamProtocol the shell parameters protocol pointer
2273 @param[out] ExitStatus The exit code of the command or file.
2276 @retval EFI_SUCCESS The command was completed.
2277 @retval EFI_ABORTED The command's operation was aborted.
2281 SetupAndRunCommandOrFile(
2282 IN SHELL_OPERATION_TYPES Type
,
2284 IN CHAR16
*FirstParameter
,
2285 IN EFI_SHELL_PARAMETERS_PROTOCOL
*ParamProtocol
,
2286 OUT SHELL_STATUS
*ExitStatus
2290 SHELL_FILE_HANDLE OriginalStdIn
;
2291 SHELL_FILE_HANDLE OriginalStdOut
;
2292 SHELL_FILE_HANDLE OriginalStdErr
;
2293 SYSTEM_TABLE_INFO OriginalSystemTableInfo
;
2296 // Update the StdIn, StdOut, and StdErr for redirection to environment variables, files, etc... unicode and ASCII
2298 Status
= UpdateStdInStdOutStdErr(ParamProtocol
, CmdLine
, &OriginalStdIn
, &OriginalStdOut
, &OriginalStdErr
, &OriginalSystemTableInfo
);
2301 // The StdIn, StdOut, and StdErr are set up.
2302 // Now run the command, script, or application
2304 if (!EFI_ERROR(Status
)) {
2305 Status
= RunCommandOrFile(
2317 if (EFI_ERROR(Status
)) {
2318 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_ERROR
), ShellInfoObject
.HiiHandle
, (VOID
*)(Status
));
2322 // put back the original StdIn, StdOut, and StdErr
2324 RestoreStdInStdOutStdErr(ParamProtocol
, &OriginalStdIn
, &OriginalStdOut
, &OriginalStdErr
, &OriginalSystemTableInfo
);
2330 Function will process and run a command line.
2332 This will determine if the command line represents an internal shell
2333 command or dispatch an external application.
2335 @param[in] CmdLine The command line to parse.
2336 @param[out] ExitStatus The exit code of the command. Ignored if NULL.
2338 @retval EFI_SUCCESS The command was completed.
2339 @retval EFI_ABORTED The command's operation was aborted.
2344 IN CONST CHAR16
*CmdLine
,
2345 OUT SHELL_STATUS
*ExitStatus
2349 CHAR16
*CleanOriginal
;
2350 CHAR16
*FirstParameter
;
2352 SHELL_OPERATION_TYPES Type
;
2354 ASSERT(CmdLine
!= NULL
);
2355 if (StrLen(CmdLine
) == 0) {
2356 return (EFI_SUCCESS
);
2359 Status
= EFI_SUCCESS
;
2360 CleanOriginal
= NULL
;
2362 CleanOriginal
= StrnCatGrow(&CleanOriginal
, NULL
, CmdLine
, 0);
2363 if (CleanOriginal
== NULL
) {
2364 return (EFI_OUT_OF_RESOURCES
);
2367 TrimSpaces(&CleanOriginal
);
2370 // Handle case that passed in command line is just 1 or more " " characters.
2372 if (StrLen (CleanOriginal
) == 0) {
2373 SHELL_FREE_NON_NULL(CleanOriginal
);
2374 return (EFI_SUCCESS
);
2377 Status
= ProcessCommandLineToFinal(&CleanOriginal
);
2378 if (EFI_ERROR(Status
)) {
2379 SHELL_FREE_NON_NULL(CleanOriginal
);
2384 // We dont do normal processing with a split command line (output from one command input to another)
2386 if (ContainsSplit(CleanOriginal
)) {
2387 Status
= ProcessNewSplitCommandLine(CleanOriginal
, ExitStatus
);
2388 SHELL_FREE_NON_NULL(CleanOriginal
);
2393 // We need the first parameter information so we can determine the operation type
2395 FirstParameter
= AllocateZeroPool(StrSize(CleanOriginal
));
2396 if (FirstParameter
== NULL
) {
2397 SHELL_FREE_NON_NULL(CleanOriginal
);
2398 return (EFI_OUT_OF_RESOURCES
);
2400 TempWalker
= CleanOriginal
;
2401 GetNextParameter(&TempWalker
, &FirstParameter
);
2404 // Depending on the first parameter we change the behavior
2406 switch (Type
= GetOperationType(FirstParameter
)) {
2407 case File_Sys_Change
:
2408 Status
= ChangeMappedDrive(CleanOriginal
);
2410 case Internal_Command
:
2411 case Script_File_Name
:
2412 case Efi_Application
:
2413 Status
= SetupAndRunCommandOrFile(
2417 ShellInfoObject
.NewShellParametersProtocol
,
2423 // Whatever was typed, it was invalid.
2425 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_NOT_FOUND
), ShellInfoObject
.HiiHandle
, FirstParameter
);
2426 SetLastError(SHELL_NOT_FOUND
);
2430 SHELL_FREE_NON_NULL(CleanOriginal
);
2431 SHELL_FREE_NON_NULL(FirstParameter
);
2436 STATIC CONST UINT16 InvalidChars
[] = {L
'*', L
'?', L
'<', L
'>', L
'\\', L
'/', L
'\"', 0x0001, 0x0002};
2438 Function determins if the CommandName COULD be a valid command. It does not determine whether
2439 this is a valid command. It only checks for invalid characters.
2441 @param[in] CommandName The name to check
2443 @retval TRUE CommandName could be a command name
2444 @retval FALSE CommandName could not be a valid command name
2449 IN CONST CHAR16
*CommandName
2453 if (CommandName
== NULL
) {
2458 ; Count
< sizeof(InvalidChars
) / sizeof(InvalidChars
[0])
2461 if (ScanMem16(CommandName
, StrSize(CommandName
), InvalidChars
[Count
]) != NULL
) {
2469 Function to process a NSH script file via SHELL_FILE_HANDLE.
2471 @param[in] Handle The handle to the already opened file.
2472 @param[in] Name The name of the script file.
2474 @param[out] ExitStatus The exit code of the script. Ignored if NULL.
2476 @retval EFI_SUCCESS the script completed sucessfully
2480 RunScriptFileHandle (
2481 IN SHELL_FILE_HANDLE Handle
,
2482 IN CONST CHAR16
*Name
,
2483 OUT SHELL_STATUS
*ExitStatus
2487 SCRIPT_FILE
*NewScriptFile
;
2489 CHAR16
*CommandLine
;
2490 CHAR16
*CommandLine2
;
2491 CHAR16
*CommandLine3
;
2492 SCRIPT_COMMAND_LIST
*LastCommand
;
2494 BOOLEAN PreScriptEchoState
;
2495 BOOLEAN PreCommandEchoState
;
2496 CONST CHAR16
*CurDir
;
2498 CHAR16 LeString
[50];
2499 SHELL_STATUS CalleeExitStatus
;
2501 ASSERT(!ShellCommandGetScriptExit());
2503 CalleeExitStatus
= SHELL_SUCCESS
;
2505 PreScriptEchoState
= ShellCommandGetEchoState();
2507 NewScriptFile
= (SCRIPT_FILE
*)AllocateZeroPool(sizeof(SCRIPT_FILE
));
2508 if (NewScriptFile
== NULL
) {
2509 return (EFI_OUT_OF_RESOURCES
);
2515 ASSERT(NewScriptFile
->ScriptName
== NULL
);
2516 NewScriptFile
->ScriptName
= StrnCatGrow(&NewScriptFile
->ScriptName
, NULL
, Name
, 0);
2517 if (NewScriptFile
->ScriptName
== NULL
) {
2518 DeleteScriptFileStruct(NewScriptFile
);
2519 return (EFI_OUT_OF_RESOURCES
);
2523 // Save the parameters (used to replace %0 to %9 later on)
2525 NewScriptFile
->Argc
= ShellInfoObject
.NewShellParametersProtocol
->Argc
;
2526 if (NewScriptFile
->Argc
!= 0) {
2527 NewScriptFile
->Argv
= (CHAR16
**)AllocateZeroPool(NewScriptFile
->Argc
* sizeof(CHAR16
*));
2528 if (NewScriptFile
->Argv
== NULL
) {
2529 DeleteScriptFileStruct(NewScriptFile
);
2530 return (EFI_OUT_OF_RESOURCES
);
2532 for (LoopVar
= 0 ; LoopVar
< 10 && LoopVar
< NewScriptFile
->Argc
; LoopVar
++) {
2533 ASSERT(NewScriptFile
->Argv
[LoopVar
] == NULL
);
2534 NewScriptFile
->Argv
[LoopVar
] = StrnCatGrow(&NewScriptFile
->Argv
[LoopVar
], NULL
, ShellInfoObject
.NewShellParametersProtocol
->Argv
[LoopVar
], 0);
2535 if (NewScriptFile
->Argv
[LoopVar
] == NULL
) {
2536 DeleteScriptFileStruct(NewScriptFile
);
2537 return (EFI_OUT_OF_RESOURCES
);
2541 NewScriptFile
->Argv
= NULL
;
2544 InitializeListHead(&NewScriptFile
->CommandList
);
2545 InitializeListHead(&NewScriptFile
->SubstList
);
2548 // Now build the list of all script commands.
2551 while(!ShellFileHandleEof(Handle
)) {
2552 CommandLine
= ShellFileHandleReturnLine(Handle
, &Ascii
);
2554 if (CommandLine
== NULL
|| StrLen(CommandLine
) == 0 || CommandLine
[0] == '#') {
2555 SHELL_FREE_NON_NULL(CommandLine
);
2558 NewScriptFile
->CurrentCommand
= AllocateZeroPool(sizeof(SCRIPT_COMMAND_LIST
));
2559 if (NewScriptFile
->CurrentCommand
== NULL
) {
2560 SHELL_FREE_NON_NULL(CommandLine
);
2561 DeleteScriptFileStruct(NewScriptFile
);
2562 return (EFI_OUT_OF_RESOURCES
);
2565 NewScriptFile
->CurrentCommand
->Cl
= CommandLine
;
2566 NewScriptFile
->CurrentCommand
->Data
= NULL
;
2567 NewScriptFile
->CurrentCommand
->Line
= LineCount
;
2569 InsertTailList(&NewScriptFile
->CommandList
, &NewScriptFile
->CurrentCommand
->Link
);
2573 // Add this as the topmost script file
2575 ShellCommandSetNewScript (NewScriptFile
);
2578 // Now enumerate through the commands and run each one.
2580 CommandLine
= AllocateZeroPool(PcdGet16(PcdShellPrintBufferSize
));
2581 if (CommandLine
== NULL
) {
2582 DeleteScriptFileStruct(NewScriptFile
);
2583 return (EFI_OUT_OF_RESOURCES
);
2585 CommandLine2
= AllocateZeroPool(PcdGet16(PcdShellPrintBufferSize
));
2586 if (CommandLine2
== NULL
) {
2587 FreePool(CommandLine
);
2588 DeleteScriptFileStruct(NewScriptFile
);
2589 return (EFI_OUT_OF_RESOURCES
);
2592 for ( NewScriptFile
->CurrentCommand
= (SCRIPT_COMMAND_LIST
*)GetFirstNode(&NewScriptFile
->CommandList
)
2593 ; !IsNull(&NewScriptFile
->CommandList
, &NewScriptFile
->CurrentCommand
->Link
)
2594 ; // conditional increment in the body of the loop
2596 ASSERT(CommandLine2
!= NULL
);
2597 StrCpy(CommandLine2
, NewScriptFile
->CurrentCommand
->Cl
);
2600 // NULL out comments
2602 for (CommandLine3
= CommandLine2
; CommandLine3
!= NULL
&& *CommandLine3
!= CHAR_NULL
; CommandLine3
++) {
2603 if (*CommandLine3
== L
'^') {
2604 if (*(CommandLine3
+1) == L
'#' || *(CommandLine3
+1) == L
':') {
2605 CopyMem(CommandLine3
, CommandLine3
+1, StrSize(CommandLine3
) - sizeof(CommandLine3
[0]));
2607 } else if (*CommandLine3
== L
'#') {
2608 *CommandLine3
= CHAR_NULL
;
2612 if (CommandLine2
!= NULL
&& StrLen(CommandLine2
) >= 1) {
2614 // Due to variability in starting the find and replace action we need to have both buffers the same.
2616 StrCpy(CommandLine
, CommandLine2
);
2619 // Remove the %0 to %9 from the command line (if we have some arguments)
2621 if (NewScriptFile
->Argv
!= NULL
) {
2622 switch (NewScriptFile
->Argc
) {
2624 Status
= ShellCopySearchAndReplace(CommandLine2
, CommandLine
, PcdGet16 (PcdShellPrintBufferSize
), L
"%9", NewScriptFile
->Argv
[9], FALSE
, TRUE
);
2625 ASSERT_EFI_ERROR(Status
);
2627 Status
= ShellCopySearchAndReplace(CommandLine
, CommandLine2
, PcdGet16 (PcdShellPrintBufferSize
), L
"%8", NewScriptFile
->Argv
[8], FALSE
, TRUE
);
2628 ASSERT_EFI_ERROR(Status
);
2630 Status
= ShellCopySearchAndReplace(CommandLine2
, CommandLine
, PcdGet16 (PcdShellPrintBufferSize
), L
"%7", NewScriptFile
->Argv
[7], FALSE
, TRUE
);
2631 ASSERT_EFI_ERROR(Status
);
2633 Status
= ShellCopySearchAndReplace(CommandLine
, CommandLine2
, PcdGet16 (PcdShellPrintBufferSize
), L
"%6", NewScriptFile
->Argv
[6], FALSE
, TRUE
);
2634 ASSERT_EFI_ERROR(Status
);
2636 Status
= ShellCopySearchAndReplace(CommandLine2
, CommandLine
, PcdGet16 (PcdShellPrintBufferSize
), L
"%5", NewScriptFile
->Argv
[5], FALSE
, TRUE
);
2637 ASSERT_EFI_ERROR(Status
);
2639 Status
= ShellCopySearchAndReplace(CommandLine
, CommandLine2
, PcdGet16 (PcdShellPrintBufferSize
), L
"%4", NewScriptFile
->Argv
[4], FALSE
, TRUE
);
2640 ASSERT_EFI_ERROR(Status
);
2642 Status
= ShellCopySearchAndReplace(CommandLine2
, CommandLine
, PcdGet16 (PcdShellPrintBufferSize
), L
"%3", NewScriptFile
->Argv
[3], FALSE
, TRUE
);
2643 ASSERT_EFI_ERROR(Status
);
2645 Status
= ShellCopySearchAndReplace(CommandLine
, CommandLine2
, PcdGet16 (PcdShellPrintBufferSize
), L
"%2", NewScriptFile
->Argv
[2], FALSE
, TRUE
);
2646 ASSERT_EFI_ERROR(Status
);
2648 Status
= ShellCopySearchAndReplace(CommandLine2
, CommandLine
, PcdGet16 (PcdShellPrintBufferSize
), L
"%1", NewScriptFile
->Argv
[1], FALSE
, TRUE
);
2649 ASSERT_EFI_ERROR(Status
);
2651 Status
= ShellCopySearchAndReplace(CommandLine
, CommandLine2
, PcdGet16 (PcdShellPrintBufferSize
), L
"%0", NewScriptFile
->Argv
[0], FALSE
, TRUE
);
2652 ASSERT_EFI_ERROR(Status
);
2658 Status
= ShellCopySearchAndReplace(CommandLine2
, CommandLine
, PcdGet16 (PcdShellPrintBufferSize
), L
"%1", L
"\"\"", FALSE
, FALSE
);
2659 Status
= ShellCopySearchAndReplace(CommandLine
, CommandLine2
, PcdGet16 (PcdShellPrintBufferSize
), L
"%2", L
"\"\"", FALSE
, FALSE
);
2660 Status
= ShellCopySearchAndReplace(CommandLine2
, CommandLine
, PcdGet16 (PcdShellPrintBufferSize
), L
"%3", L
"\"\"", FALSE
, FALSE
);
2661 Status
= ShellCopySearchAndReplace(CommandLine
, CommandLine2
, PcdGet16 (PcdShellPrintBufferSize
), L
"%4", L
"\"\"", FALSE
, FALSE
);
2662 Status
= ShellCopySearchAndReplace(CommandLine2
, CommandLine
, PcdGet16 (PcdShellPrintBufferSize
), L
"%5", L
"\"\"", FALSE
, FALSE
);
2663 Status
= ShellCopySearchAndReplace(CommandLine
, CommandLine2
, PcdGet16 (PcdShellPrintBufferSize
), L
"%6", L
"\"\"", FALSE
, FALSE
);
2664 Status
= ShellCopySearchAndReplace(CommandLine2
, CommandLine
, PcdGet16 (PcdShellPrintBufferSize
), L
"%7", L
"\"\"", FALSE
, FALSE
);
2665 Status
= ShellCopySearchAndReplace(CommandLine
, CommandLine2
, PcdGet16 (PcdShellPrintBufferSize
), L
"%8", L
"\"\"", FALSE
, FALSE
);
2666 Status
= ShellCopySearchAndReplace(CommandLine2
, CommandLine
, PcdGet16 (PcdShellPrintBufferSize
), L
"%9", L
"\"\"", FALSE
, FALSE
);
2668 StrCpy(CommandLine2
, CommandLine
);
2670 LastCommand
= NewScriptFile
->CurrentCommand
;
2672 for (CommandLine3
= CommandLine2
; CommandLine3
[0] == L
' ' ; CommandLine3
++);
2674 if (CommandLine3
!= NULL
&& CommandLine3
[0] == L
':' ) {
2676 // This line is a goto target / label
2679 if (CommandLine3
!= NULL
&& StrLen(CommandLine3
) > 0) {
2680 if (CommandLine3
[0] == L
'@') {
2682 // We need to save the current echo state
2683 // and disable echo for just this command.
2685 PreCommandEchoState
= ShellCommandGetEchoState();
2686 ShellCommandSetEchoState(FALSE
);
2687 Status
= RunCommand(CommandLine3
+1, NULL
);
2690 // If command was "@echo -off" or "@echo -on" then don't restore echo state
2692 if (StrCmp (L
"@echo -off", CommandLine3
) != 0 &&
2693 StrCmp (L
"@echo -on", CommandLine3
) != 0) {
2695 // Now restore the pre-'@' echo state.
2697 ShellCommandSetEchoState(PreCommandEchoState
);
2700 if (ShellCommandGetEchoState()) {
2701 CurDir
= ShellInfoObject
.NewEfiShellProtocol
->GetEnv(L
"cwd");
2702 if (CurDir
!= NULL
&& StrLen(CurDir
) > 1) {
2703 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_CURDIR
), ShellInfoObject
.HiiHandle
, CurDir
);
2705 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_SHELL
), ShellInfoObject
.HiiHandle
);
2707 ShellPrintEx(-1, -1, L
"%s\r\n", CommandLine2
);
2709 Status
= RunCommand(CommandLine3
, NULL
);
2713 if (ShellCommandGetScriptExit()) {
2715 // ShellCommandGetExitCode() always returns a UINT64
2717 CalleeExitStatus
= (SHELL_STATUS
) ShellCommandGetExitCode();
2718 UnicodeSPrint(LeString
, sizeof(LeString
), L
"0x%Lx", CalleeExitStatus
);
2719 DEBUG_CODE(InternalEfiShellSetEnv(L
"debuglasterror", LeString
, TRUE
););
2720 InternalEfiShellSetEnv(L
"lasterror", LeString
, TRUE
);
2722 ShellCommandRegisterExit(FALSE
, 0);
2723 Status
= EFI_SUCCESS
;
2726 if (ShellGetExecutionBreakFlag()) {
2729 if (EFI_ERROR(Status
)) {
2730 CalleeExitStatus
= (SHELL_STATUS
) Status
;
2733 if (ShellCommandGetExit()) {
2734 CalleeExitStatus
= (SHELL_STATUS
) ShellCommandGetExitCode();
2739 // If that commend did not update the CurrentCommand then we need to advance it...
2741 if (LastCommand
== NewScriptFile
->CurrentCommand
) {
2742 NewScriptFile
->CurrentCommand
= (SCRIPT_COMMAND_LIST
*)GetNextNode(&NewScriptFile
->CommandList
, &NewScriptFile
->CurrentCommand
->Link
);
2743 if (!IsNull(&NewScriptFile
->CommandList
, &NewScriptFile
->CurrentCommand
->Link
)) {
2744 NewScriptFile
->CurrentCommand
->Reset
= TRUE
;
2748 NewScriptFile
->CurrentCommand
= (SCRIPT_COMMAND_LIST
*)GetNextNode(&NewScriptFile
->CommandList
, &NewScriptFile
->CurrentCommand
->Link
);
2749 if (!IsNull(&NewScriptFile
->CommandList
, &NewScriptFile
->CurrentCommand
->Link
)) {
2750 NewScriptFile
->CurrentCommand
->Reset
= TRUE
;
2756 FreePool(CommandLine
);
2757 FreePool(CommandLine2
);
2758 ShellCommandSetNewScript (NULL
);
2761 // Only if this was the last script reset the state.
2763 if (ShellCommandGetCurrentScriptFile()==NULL
) {
2764 ShellCommandSetEchoState(PreScriptEchoState
);
2767 if (ExitStatus
!= NULL
) {
2768 *ExitStatus
= CalleeExitStatus
;
2771 return (EFI_SUCCESS
);
2775 Function to process a NSH script file.
2777 @param[in] ScriptPath Pointer to the script file name (including file system path).
2778 @param[in] Handle the handle of the script file already opened.
2779 @param[in] CmdLine the command line to run.
2780 @param[in] ParamProtocol the shell parameters protocol pointer
2782 @param[out] ExitStatus The exit code of the script. Ignored if NULL.
2784 @retval EFI_SUCCESS the script completed sucessfully
2789 IN CONST CHAR16
*ScriptPath
,
2790 IN SHELL_FILE_HANDLE Handle OPTIONAL
,
2791 IN CONST CHAR16
*CmdLine
,
2792 IN EFI_SHELL_PARAMETERS_PROTOCOL
*ParamProtocol
,
2793 OUT SHELL_STATUS
*ExitStatus
2797 SHELL_FILE_HANDLE FileHandle
;
2801 if (ShellIsFile(ScriptPath
) != EFI_SUCCESS
) {
2802 return (EFI_INVALID_PARAMETER
);
2806 // get the argc and argv updated for scripts
2808 Status
= UpdateArgcArgv(ParamProtocol
, CmdLine
, &Argv
, &Argc
);
2809 if (!EFI_ERROR(Status
)) {
2811 if (Handle
== NULL
) {
2815 Status
= ShellOpenFileByName(ScriptPath
, &FileHandle
, EFI_FILE_MODE_READ
, 0);
2816 if (!EFI_ERROR(Status
)) {
2820 Status
= RunScriptFileHandle(FileHandle
, ScriptPath
, ExitStatus
);
2823 // now close the file
2825 ShellCloseFile(&FileHandle
);
2828 Status
= RunScriptFileHandle(Handle
, ScriptPath
, ExitStatus
);
2833 // This is guarenteed to be called after UpdateArgcArgv no matter what else happened.
2834 // This is safe even if the update API failed. In this case, it may be a no-op.
2836 RestoreArgcArgv(ParamProtocol
, &Argv
, &Argc
);