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 return (EFI_NOT_STARTED
);
336 // create and install the EfiShellParametersProtocol
338 Status
= CreatePopulateInstallShellParametersProtocol(&ShellInfoObject
.NewShellParametersProtocol
, &ShellInfoObject
.RootShellInstance
);
339 ASSERT_EFI_ERROR(Status
);
340 ASSERT(ShellInfoObject
.NewShellParametersProtocol
!= NULL
);
343 // create and install the EfiShellProtocol
345 Status
= CreatePopulateInstallShellProtocol(&ShellInfoObject
.NewEfiShellProtocol
);
346 ASSERT_EFI_ERROR(Status
);
347 ASSERT(ShellInfoObject
.NewEfiShellProtocol
!= NULL
);
350 // Now initialize the shell library (it requires Shell Parameters protocol)
352 Status
= ShellInitialize();
353 ASSERT_EFI_ERROR(Status
);
355 Status
= CommandInit();
356 ASSERT_EFI_ERROR(Status
);
359 // Check the command line
361 Status
= ProcessCommandLine();
364 // If shell support level is >= 1 create the mappings and paths
366 if (PcdGet8(PcdShellSupportLevel
) >= 1) {
367 Status
= ShellCommandCreateInitialMappingsAndPaths();
371 // save the device path for the loaded image and the device path for the filepath (under loaded image)
372 // These are where to look for the startup.nsh file
374 Status
= GetDevicePathsForImageAndFile(&ShellInfoObject
.ImageDevPath
, &ShellInfoObject
.FileDevPath
);
375 ASSERT_EFI_ERROR(Status
);
378 // Display the version
380 if (!ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoVersion
) {
383 gST
->ConOut
->Mode
->CursorRow
,
385 STRING_TOKEN (STR_VER_OUTPUT_MAIN_SHELL
),
386 ShellInfoObject
.HiiHandle
,
387 SupportLevel
[PcdGet8(PcdShellSupportLevel
)],
388 gEfiShellProtocol
->MajorVersion
,
389 gEfiShellProtocol
->MinorVersion
396 STRING_TOKEN (STR_VER_OUTPUT_MAIN_SUPPLIER
),
397 ShellInfoObject
.HiiHandle
,
398 (CHAR16
*) PcdGetPtr (PcdShellSupplier
)
405 STRING_TOKEN (STR_VER_OUTPUT_MAIN_UEFI
),
406 ShellInfoObject
.HiiHandle
,
407 (gST
->Hdr
.Revision
&0xffff0000)>>16,
408 (gST
->Hdr
.Revision
&0x0000ffff),
410 gST
->FirmwareRevision
415 // Display the mapping
417 if (PcdGet8(PcdShellSupportLevel
) >= 2 && !ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoMap
) {
418 Status
= RunCommand(L
"map", NULL
);
419 ASSERT_EFI_ERROR(Status
);
423 // init all the built in alias'
425 Status
= SetBuiltInAlias();
426 ASSERT_EFI_ERROR(Status
);
429 // Initialize environment variables
431 if (ShellCommandGetProfileList() != NULL
) {
432 Status
= InternalEfiShellSetEnv(L
"profiles", ShellCommandGetProfileList(), TRUE
);
433 ASSERT_EFI_ERROR(Status
);
437 TempString
= AllocateZeroPool(Size
);
439 UnicodeSPrint(TempString
, Size
, L
"%d", PcdGet8(PcdShellSupportLevel
));
440 Status
= InternalEfiShellSetEnv(L
"uefishellsupport", TempString
, TRUE
);
441 ASSERT_EFI_ERROR(Status
);
443 UnicodeSPrint(TempString
, Size
, L
"%d.%d", ShellInfoObject
.NewEfiShellProtocol
->MajorVersion
, ShellInfoObject
.NewEfiShellProtocol
->MinorVersion
);
444 Status
= InternalEfiShellSetEnv(L
"uefishellversion", TempString
, TRUE
);
445 ASSERT_EFI_ERROR(Status
);
447 UnicodeSPrint(TempString
, Size
, L
"%d.%d", (gST
->Hdr
.Revision
& 0xFFFF0000) >> 16, gST
->Hdr
.Revision
& 0x0000FFFF);
448 Status
= InternalEfiShellSetEnv(L
"uefiversion", TempString
, TRUE
);
449 ASSERT_EFI_ERROR(Status
);
451 FreePool(TempString
);
453 if (!EFI_ERROR(Status
)) {
454 if (!ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoInterrupt
) {
456 // Set up the event for CTRL-C monitoring...
458 Status
= InernalEfiShellStartMonitor();
461 if (!EFI_ERROR(Status
) && !ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoConsoleIn
) {
463 // Set up the event for CTRL-S monitoring...
465 Status
= InternalEfiShellStartCtrlSMonitor();
468 if (!EFI_ERROR(Status
) && ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoConsoleIn
) {
470 // close off the gST->ConIn
472 OldConIn
= gST
->ConIn
;
473 ConInHandle
= gST
->ConsoleInHandle
;
474 gST
->ConIn
= CreateSimpleTextInOnFile((SHELL_FILE_HANDLE
)&FileInterfaceNulFile
, &gST
->ConsoleInHandle
);
480 if (!EFI_ERROR(Status
) && PcdGet8(PcdShellSupportLevel
) >= 1) {
482 // process the startup script or launch the called app.
484 Status
= DoStartupScript(
485 ShellInfoObject
.ImageDevPath
,
486 ShellInfoObject
.FileDevPath
,
491 if (!ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.Exit
&& !ShellCommandGetExit() && (PcdGet8(PcdShellSupportLevel
) >= 3 || PcdGetBool(PcdShellForceConsole
)) && !EFI_ERROR(Status
) && !ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoConsoleIn
) {
493 // begin the UI waiting loop
497 // clean out all the memory allocated for CONST <something> * return values
498 // between each shell prompt presentation
500 if (!IsListEmpty(&ShellInfoObject
.BufferToFreeList
.Link
)){
501 FreeBufferList(&ShellInfoObject
.BufferToFreeList
);
505 // Reset page break back to default.
507 ShellInfoObject
.PageBreakEnabled
= PcdGetBool(PcdShellPageBreakDefault
);
508 ShellInfoObject
.ConsoleInfo
->Enabled
= TRUE
;
509 ShellInfoObject
.ConsoleInfo
->RowCounter
= 0;
512 // Reset the CTRL-C event (yes we ignore the return values)
514 Status
= gBS
->CheckEvent (ShellInfoObject
.NewEfiShellProtocol
->ExecutionBreak
);
519 Status
= DoShellPrompt();
520 } while (!ShellCommandGetExit());
521 ExitStatus
= (SHELL_STATUS
) ShellCommandGetExitCode();
523 if (OldConIn
!= NULL
&& ConInHandle
!= NULL
) {
524 CloseSimpleTextInOnFile (gST
->ConIn
);
525 gST
->ConIn
= OldConIn
;
526 gST
->ConsoleInHandle
= ConInHandle
;
532 // uninstall protocols / free memory / etc...
534 if (ShellInfoObject
.UserBreakTimer
!= NULL
) {
535 gBS
->CloseEvent(ShellInfoObject
.UserBreakTimer
);
536 DEBUG_CODE(ShellInfoObject
.UserBreakTimer
= NULL
;);
538 if (ShellInfoObject
.ImageDevPath
!= NULL
) {
539 FreePool(ShellInfoObject
.ImageDevPath
);
540 DEBUG_CODE(ShellInfoObject
.ImageDevPath
= NULL
;);
542 if (ShellInfoObject
.FileDevPath
!= NULL
) {
543 FreePool(ShellInfoObject
.FileDevPath
);
544 DEBUG_CODE(ShellInfoObject
.FileDevPath
= NULL
;);
546 if (ShellInfoObject
.NewShellParametersProtocol
!= NULL
) {
547 CleanUpShellParametersProtocol(ShellInfoObject
.NewShellParametersProtocol
);
548 DEBUG_CODE(ShellInfoObject
.NewShellParametersProtocol
= NULL
;);
550 if (ShellInfoObject
.NewEfiShellProtocol
!= NULL
){
551 if (ShellInfoObject
.NewEfiShellProtocol
->IsRootShell()){
552 InternalEfiShellSetEnv(L
"cwd", NULL
, TRUE
);
554 CleanUpShellProtocol(ShellInfoObject
.NewEfiShellProtocol
);
555 DEBUG_CODE(ShellInfoObject
.NewEfiShellProtocol
= NULL
;);
558 if (!IsListEmpty(&ShellInfoObject
.BufferToFreeList
.Link
)){
559 FreeBufferList(&ShellInfoObject
.BufferToFreeList
);
562 if (!IsListEmpty(&ShellInfoObject
.SplitList
.Link
)){
563 ASSERT(FALSE
); ///@todo finish this de-allocation.
566 if (ShellInfoObject
.ShellInitSettings
.FileName
!= NULL
) {
567 FreePool(ShellInfoObject
.ShellInitSettings
.FileName
);
568 DEBUG_CODE(ShellInfoObject
.ShellInitSettings
.FileName
= NULL
;);
571 if (ShellInfoObject
.ShellInitSettings
.FileOptions
!= NULL
) {
572 FreePool(ShellInfoObject
.ShellInitSettings
.FileOptions
);
573 DEBUG_CODE(ShellInfoObject
.ShellInitSettings
.FileOptions
= NULL
;);
576 if (ShellInfoObject
.HiiHandle
!= NULL
) {
577 HiiRemovePackages(ShellInfoObject
.HiiHandle
);
578 DEBUG_CODE(ShellInfoObject
.HiiHandle
= NULL
;);
581 if (!IsListEmpty(&ShellInfoObject
.ViewingSettings
.CommandHistory
.Link
)){
582 FreeBufferList(&ShellInfoObject
.ViewingSettings
.CommandHistory
);
585 ASSERT(ShellInfoObject
.ConsoleInfo
!= NULL
);
586 if (ShellInfoObject
.ConsoleInfo
!= NULL
) {
587 ConsoleLoggerUninstall(ShellInfoObject
.ConsoleInfo
);
588 FreePool(ShellInfoObject
.ConsoleInfo
);
589 DEBUG_CODE(ShellInfoObject
.ConsoleInfo
= NULL
;);
592 // If the command exited with an error, we pass this error out in the ExitData
593 // so that it can be retrieved by the EfiShellExecute function (which may
594 // start the shell with gBS->StartImage)
595 if (ExitStatus
!= SHELL_SUCCESS
) {
596 // Allocate a buffer for exit data to pass to gBS->Exit().
597 // This buffer will contain the empty string immediately followed by
598 // the shell's exit status. (The empty string is required by the UEFI spec)
599 ExitDataSize
= (sizeof (CHAR16
) + sizeof (SHELL_STATUS
));
600 ExitData
= AllocatePool (ExitDataSize
);
601 if (ExitData
== NULL
) {
602 return EFI_OUT_OF_RESOURCES
;
605 // Use CopyMem to avoid alignment faults
606 CopyMem ((ExitData
+ 1), &ExitStatus
, sizeof (ExitStatus
));
608 gBS
->Exit (ImageHandle
, EFI_ABORTED
, ExitDataSize
, ExitData
);
618 Sets all the alias' that were registered with the ShellCommandLib library.
620 @retval EFI_SUCCESS all init commands were run sucessfully.
628 CONST ALIAS_LIST
*List
;
632 // Get all the commands we want to run
634 List
= ShellCommandGetInitAliasList();
637 // for each command in the List
639 for ( Node
= (ALIAS_LIST
*)GetFirstNode(&List
->Link
)
640 ; !IsNull (&List
->Link
, &Node
->Link
)
641 ; Node
= (ALIAS_LIST
*)GetNextNode(&List
->Link
, &Node
->Link
)
644 // install the alias'
646 Status
= InternalSetAlias(Node
->CommandString
, Node
->Alias
, TRUE
);
647 ASSERT_EFI_ERROR(Status
);
649 return (EFI_SUCCESS
);
653 Internal function to determine if 2 command names are really the same.
655 @param[in] Command1 The pointer to the first command name.
656 @param[in] Command2 The pointer to the second command name.
658 @retval TRUE The 2 command names are the same.
659 @retval FALSE The 2 command names are not the same.
664 IN CONST CHAR16
*Command1
,
665 IN CONST CHAR16
*Command2
668 if (StringNoCaseCompare(&Command1
, &Command2
) == 0) {
675 Internal function to determine if a command is a script only command.
677 @param[in] CommandName The pointer to the command name.
679 @retval TRUE The command is a script only command.
680 @retval FALSE The command is not a script only command.
685 IN CONST CHAR16
*CommandName
688 if (IsCommand(CommandName
, L
"for")
689 ||IsCommand(CommandName
, L
"endfor")
690 ||IsCommand(CommandName
, L
"if")
691 ||IsCommand(CommandName
, L
"else")
692 ||IsCommand(CommandName
, L
"endif")
693 ||IsCommand(CommandName
, L
"goto")) {
700 This function will populate the 2 device path protocol parameters based on the
701 global gImageHandle. The DevPath will point to the device path for the handle that has
702 loaded image protocol installed on it. The FilePath will point to the device path
703 for the file that was loaded.
705 @param[in, out] DevPath On a sucessful return the device path to the loaded image.
706 @param[in, out] FilePath On a sucessful return the device path to the file.
708 @retval EFI_SUCCESS The 2 device paths were sucessfully returned.
709 @retval other A error from gBS->HandleProtocol.
715 GetDevicePathsForImageAndFile (
716 IN OUT EFI_DEVICE_PATH_PROTOCOL
**DevPath
,
717 IN OUT EFI_DEVICE_PATH_PROTOCOL
**FilePath
721 EFI_LOADED_IMAGE_PROTOCOL
*LoadedImage
;
722 EFI_DEVICE_PATH_PROTOCOL
*ImageDevicePath
;
724 ASSERT(DevPath
!= NULL
);
725 ASSERT(FilePath
!= NULL
);
727 Status
= gBS
->OpenProtocol (
729 &gEfiLoadedImageProtocolGuid
,
730 (VOID
**)&LoadedImage
,
733 EFI_OPEN_PROTOCOL_GET_PROTOCOL
735 if (!EFI_ERROR (Status
)) {
736 Status
= gBS
->OpenProtocol (
737 LoadedImage
->DeviceHandle
,
738 &gEfiDevicePathProtocolGuid
,
739 (VOID
**)&ImageDevicePath
,
742 EFI_OPEN_PROTOCOL_GET_PROTOCOL
744 if (!EFI_ERROR (Status
)) {
745 *DevPath
= DuplicateDevicePath (ImageDevicePath
);
746 *FilePath
= DuplicateDevicePath (LoadedImage
->FilePath
);
748 LoadedImage
->DeviceHandle
,
749 &gEfiDevicePathProtocolGuid
,
755 &gEfiLoadedImageProtocolGuid
,
762 STATIC CONST SHELL_PARAM_ITEM mShellParamList
[] = {
763 {L
"-nostartup", TypeFlag
},
764 {L
"-startup", TypeFlag
},
765 {L
"-noconsoleout", TypeFlag
},
766 {L
"-noconsolein", TypeFlag
},
767 {L
"-nointerrupt", TypeFlag
},
768 {L
"-nomap", TypeFlag
},
769 {L
"-noversion", TypeFlag
},
770 {L
"-startup", TypeFlag
},
771 {L
"-delay", TypeValue
},
772 {L
"-_exit", TypeFlag
},
777 Process all Uefi Shell 2.0 command line options.
779 see Uefi Shell 2.0 section 3.2 for full details.
781 the command line must resemble the following:
783 shell.efi [ShellOpt-options] [options] [file-name [file-name-options]]
785 ShellOpt-options Options which control the initialization behavior of the shell.
786 These options are read from the EFI global variable "ShellOpt"
787 and are processed before options or file-name.
789 options Options which control the initialization behavior of the shell.
791 file-name The name of a UEFI shell application or script to be executed
792 after initialization is complete. By default, if file-name is
793 specified, then -nostartup is implied. Scripts are not supported
796 file-name-options The command-line options that are passed to file-name when it
799 This will initialize the ShellInfoObject.ShellInitSettings global variable.
801 @retval EFI_SUCCESS The variable is initialized.
812 CONST CHAR16
*TempConst
;
815 CHAR16
*ProblemParam
;
821 Status
= ShellCommandLineParse (mShellParamList
, &Package
, NULL
, FALSE
);
825 TempConst
= ShellCommandLineGetRawValue(Package
, Count
++);
826 if (TempConst
!= NULL
&& StrLen(TempConst
)) {
827 ShellInfoObject
.ShellInitSettings
.FileName
= AllocateZeroPool(StrSize(TempConst
));
828 if (ShellInfoObject
.ShellInitSettings
.FileName
== NULL
) {
829 return (EFI_OUT_OF_RESOURCES
);
831 StrCpy(ShellInfoObject
.ShellInitSettings
.FileName
, TempConst
);
832 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoStartup
= 1;
833 for (LoopVar
= 0 ; LoopVar
< gEfiShellParametersProtocol
->Argc
; LoopVar
++) {
834 if (StrCmp(gEfiShellParametersProtocol
->Argv
[LoopVar
], ShellInfoObject
.ShellInitSettings
.FileName
)==0) {
837 // We found the file... add the rest of the params...
839 for ( ; LoopVar
< gEfiShellParametersProtocol
->Argc
; LoopVar
++) {
840 ASSERT((ShellInfoObject
.ShellInitSettings
.FileOptions
== NULL
&& Size
== 0) || (ShellInfoObject
.ShellInitSettings
.FileOptions
!= NULL
));
841 StrnCatGrow(&ShellInfoObject
.ShellInitSettings
.FileOptions
,
845 if (ShellInfoObject
.ShellInitSettings
.FileOptions
== NULL
) {
846 SHELL_FREE_NON_NULL(ShellInfoObject
.ShellInitSettings
.FileName
);
847 return (EFI_OUT_OF_RESOURCES
);
849 StrnCatGrow(&ShellInfoObject
.ShellInitSettings
.FileOptions
,
851 gEfiShellParametersProtocol
->Argv
[LoopVar
],
853 if (ShellInfoObject
.ShellInitSettings
.FileOptions
== NULL
) {
854 SHELL_FREE_NON_NULL(ShellInfoObject
.ShellInitSettings
.FileName
);
855 return (EFI_OUT_OF_RESOURCES
);
861 ShellCommandLineFreeVarList(Package
);
863 Status
= ShellCommandLineParse (mShellParamList
, &Package
, &ProblemParam
, FALSE
);
864 if (EFI_ERROR(Status
)) {
865 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_PROBLEM
), ShellInfoObject
.HiiHandle
, ProblemParam
);
866 FreePool(ProblemParam
);
867 ShellCommandLineFreeVarList(Package
);
868 return (EFI_INVALID_PARAMETER
);
872 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.Startup
= ShellCommandLineGetFlag(Package
, L
"-startup");
873 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoStartup
= ShellCommandLineGetFlag(Package
, L
"-nostartup");
874 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoConsoleOut
= ShellCommandLineGetFlag(Package
, L
"-noconsoleout");
875 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoConsoleIn
= ShellCommandLineGetFlag(Package
, L
"-noconsolein");
876 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoInterrupt
= ShellCommandLineGetFlag(Package
, L
"-nointerrupt");
877 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoMap
= ShellCommandLineGetFlag(Package
, L
"-nomap");
878 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoVersion
= ShellCommandLineGetFlag(Package
, L
"-noversion");
879 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.Delay
= ShellCommandLineGetFlag(Package
, L
"-delay");
880 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.Exit
= ShellCommandLineGetFlag(Package
, L
"-_exit");
882 ShellInfoObject
.ShellInitSettings
.Delay
= 5;
884 if (ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoInterrupt
) {
885 ShellInfoObject
.ShellInitSettings
.Delay
= 0;
886 } else if (ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.Delay
) {
887 TempConst
= ShellCommandLineGetValue(Package
, L
"-delay");
888 if (TempConst
!= NULL
&& *TempConst
== L
':') {
891 if (TempConst
!= NULL
&& !EFI_ERROR(ShellConvertStringToUint64(TempConst
, &Intermediate
, FALSE
, FALSE
))) {
892 ShellInfoObject
.ShellInitSettings
.Delay
= (UINTN
)Intermediate
;
895 ShellCommandLineFreeVarList(Package
);
901 Handles all interaction with the default startup script.
903 this will check that the correct command line parameters were passed, handle the delay, and then start running the script.
905 @param ImagePath the path to the image for shell. first place to look for the startup script
906 @param FilePath the path to the file for shell. second place to look for the startup script.
908 @param[out] ExitStatus The exit code of the script. Ignored if NULL.
910 @retval EFI_SUCCESS the variable is initialized.
915 IN EFI_DEVICE_PATH_PROTOCOL
*ImagePath
,
916 IN EFI_DEVICE_PATH_PROTOCOL
*FilePath
,
917 OUT SHELL_STATUS
*ExitStatus
923 SHELL_FILE_HANDLE FileHandle
;
924 EFI_DEVICE_PATH_PROTOCOL
*NewPath
;
925 EFI_DEVICE_PATH_PROTOCOL
*NamePath
;
926 CHAR16
*FileStringPath
;
929 CONST CHAR16
*MapName
;
931 Key
.UnicodeChar
= CHAR_NULL
;
935 if (!ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.Startup
&& ShellInfoObject
.ShellInitSettings
.FileName
!= NULL
) {
937 // launch something else instead
939 NewSize
= StrSize(ShellInfoObject
.ShellInitSettings
.FileName
);
940 if (ShellInfoObject
.ShellInitSettings
.FileOptions
!= NULL
) {
941 NewSize
+= StrSize(ShellInfoObject
.ShellInitSettings
.FileOptions
) + sizeof(CHAR16
);
943 FileStringPath
= AllocateZeroPool(NewSize
);
944 if (FileStringPath
== NULL
) {
945 return (EFI_OUT_OF_RESOURCES
);
947 StrCpy(FileStringPath
, ShellInfoObject
.ShellInitSettings
.FileName
);
948 if (ShellInfoObject
.ShellInitSettings
.FileOptions
!= NULL
) {
949 StrCat(FileStringPath
, L
" ");
950 StrCat(FileStringPath
, ShellInfoObject
.ShellInitSettings
.FileOptions
);
952 Status
= RunCommand(FileStringPath
, ExitStatus
);
953 FreePool(FileStringPath
);
959 // for shell level 0 we do no scripts
960 // Without the Startup bit overriding we allow for nostartup to prevent scripts
962 if ( (PcdGet8(PcdShellSupportLevel
) < 1)
963 || (ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoStartup
&& !ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.Startup
)
965 return (EFI_SUCCESS
);
968 gST
->ConOut
->EnableCursor(gST
->ConOut
, FALSE
);
970 // print out our warning and see if they press a key
972 for ( Status
= EFI_UNSUPPORTED
, Delay
= ShellInfoObject
.ShellInitSettings
.Delay
973 ; Delay
!= 0 && EFI_ERROR(Status
)
976 ShellPrintHiiEx(0, gST
->ConOut
->Mode
->CursorRow
, NULL
, STRING_TOKEN (STR_SHELL_STARTUP_QUESTION
), ShellInfoObject
.HiiHandle
, Delay
);
977 gBS
->Stall (1000000);
978 if (!ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoConsoleIn
) {
979 Status
= gST
->ConIn
->ReadKeyStroke (gST
->ConIn
, &Key
);
982 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_CRLF
), ShellInfoObject
.HiiHandle
);
983 gST
->ConOut
->EnableCursor(gST
->ConOut
, TRUE
);
988 if (Status
== EFI_SUCCESS
&& Key
.UnicodeChar
== 0 && Key
.ScanCode
== SCAN_ESC
) {
989 return (EFI_SUCCESS
);
993 // Try the first location (must be file system)
995 MapName
= ShellInfoObject
.NewEfiShellProtocol
->GetMapFromDevicePath(&ImagePath
);
996 if (MapName
!= NULL
) {
997 FileStringPath
= NULL
;
999 FileStringPath
= StrnCatGrow(&FileStringPath
, &NewSize
, MapName
, 0);
1000 if (FileStringPath
== NULL
) {
1001 Status
= EFI_OUT_OF_RESOURCES
;
1003 TempSpot
= StrStr(FileStringPath
, L
";");
1004 if (TempSpot
!= NULL
) {
1005 *TempSpot
= CHAR_NULL
;
1007 FileStringPath
= StrnCatGrow(&FileStringPath
, &NewSize
, ((FILEPATH_DEVICE_PATH
*)FilePath
)->PathName
, 0);
1008 PathRemoveLastItem(FileStringPath
);
1009 FileStringPath
= StrnCatGrow(&FileStringPath
, &NewSize
, mStartupScript
, 0);
1010 Status
= ShellInfoObject
.NewEfiShellProtocol
->OpenFileByName(FileStringPath
, &FileHandle
, EFI_FILE_MODE_READ
);
1011 FreePool(FileStringPath
);
1014 if (EFI_ERROR(Status
)) {
1015 NamePath
= FileDevicePath (NULL
, mStartupScript
);
1016 NewPath
= AppendDevicePathNode (ImagePath
, NamePath
);
1022 Status
= InternalOpenFileDevicePath(NewPath
, &FileHandle
, EFI_FILE_MODE_READ
, 0);
1026 // If we got a file, run it
1028 if (!EFI_ERROR(Status
) && FileHandle
!= NULL
) {
1029 Status
= RunScriptFile (
1033 ShellInfoObject
.NewShellParametersProtocol
,
1036 ShellInfoObject
.NewEfiShellProtocol
->CloseFile(FileHandle
);
1038 FileStringPath
= ShellFindFilePath(mStartupScript
);
1039 if (FileStringPath
== NULL
) {
1041 // we return success since we dont need to have a startup script
1043 Status
= EFI_SUCCESS
;
1044 ASSERT(FileHandle
== NULL
);
1046 Status
= RunScriptFile(
1050 ShellInfoObject
.NewShellParametersProtocol
,
1053 FreePool(FileStringPath
);
1062 Function to perform the shell prompt looping. It will do a single prompt,
1063 dispatch the result, and then return. It is expected that the caller will
1064 call this function in a loop many times.
1067 @retval RETURN_ABORTED
1078 CONST CHAR16
*CurDir
;
1085 // Get screen setting to decide size of the command line buffer
1087 gST
->ConOut
->QueryMode (gST
->ConOut
, gST
->ConOut
->Mode
->Mode
, &Column
, &Row
);
1088 BufferSize
= Column
* Row
* sizeof (CHAR16
);
1089 CmdLine
= AllocateZeroPool (BufferSize
);
1090 if (CmdLine
== NULL
) {
1091 return EFI_OUT_OF_RESOURCES
;
1094 CurDir
= ShellInfoObject
.NewEfiShellProtocol
->GetEnv(L
"cwd");
1099 gST
->ConOut
->SetCursorPosition (gST
->ConOut
, 0, gST
->ConOut
->Mode
->CursorRow
);
1101 if (CurDir
!= NULL
&& StrLen(CurDir
) > 1) {
1102 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_CURDIR
), ShellInfoObject
.HiiHandle
, CurDir
);
1104 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_SHELL
), ShellInfoObject
.HiiHandle
);
1108 // Read a line from the console
1110 Status
= ShellInfoObject
.NewEfiShellProtocol
->ReadFile(ShellInfoObject
.NewShellParametersProtocol
->StdIn
, &BufferSize
, CmdLine
);
1113 // Null terminate the string and parse it
1115 if (!EFI_ERROR (Status
)) {
1116 CmdLine
[BufferSize
/ sizeof (CHAR16
)] = CHAR_NULL
;
1117 Status
= RunCommand(CmdLine
, NULL
);
1121 // Done with this command
1128 Add a buffer to the Buffer To Free List for safely returning buffers to other
1129 places without risking letting them modify internal shell information.
1131 @param Buffer Something to pass to FreePool when the shell is exiting.
1135 AddBufferToFreeList(
1139 BUFFER_LIST
*BufferListEntry
;
1141 if (Buffer
== NULL
) {
1145 BufferListEntry
= AllocateZeroPool(sizeof(BUFFER_LIST
));
1146 ASSERT(BufferListEntry
!= NULL
);
1147 BufferListEntry
->Buffer
= Buffer
;
1148 InsertTailList(&ShellInfoObject
.BufferToFreeList
.Link
, &BufferListEntry
->Link
);
1153 Add a buffer to the Line History List
1155 @param Buffer The line buffer to add.
1159 AddLineToCommandHistory(
1160 IN CONST CHAR16
*Buffer
1165 Node
= AllocateZeroPool(sizeof(BUFFER_LIST
));
1166 ASSERT(Node
!= NULL
);
1167 Node
->Buffer
= AllocateZeroPool(StrSize(Buffer
));
1168 ASSERT(Node
->Buffer
!= NULL
);
1169 StrCpy(Node
->Buffer
, Buffer
);
1171 InsertTailList(&ShellInfoObject
.ViewingSettings
.CommandHistory
.Link
, &Node
->Link
);
1175 Checks if a string is an alias for another command. If yes, then it replaces the alias name
1176 with the correct command name.
1178 @param[in, out] CommandString Upon entry the potential alias. Upon return the
1179 command name if it was an alias. If it was not
1180 an alias it will be unchanged. This function may
1181 change the buffer to fit the command name.
1183 @retval EFI_SUCCESS The name was changed.
1184 @retval EFI_SUCCESS The name was not an alias.
1185 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
1190 IN OUT CHAR16
**CommandString
1193 CONST CHAR16
*NewString
;
1195 NewString
= ShellInfoObject
.NewEfiShellProtocol
->GetAlias(*CommandString
, NULL
);
1196 if (NewString
== NULL
) {
1197 return (EFI_SUCCESS
);
1199 FreePool(*CommandString
);
1200 *CommandString
= AllocateZeroPool(StrSize(NewString
));
1201 if (*CommandString
== NULL
) {
1202 return (EFI_OUT_OF_RESOURCES
);
1204 StrCpy(*CommandString
, NewString
);
1205 return (EFI_SUCCESS
);
1209 Function allocates a new command line and replaces all instances of environment
1210 variable names that are correctly preset to their values.
1212 If the return value is not NULL the memory must be caller freed.
1214 @param[in] OriginalCommandLine The original command line
1216 @retval NULL An error ocurred.
1217 @return The new command line with no environment variables present.
1221 ShellConvertVariables (
1222 IN CONST CHAR16
*OriginalCommandLine
1225 CONST CHAR16
*MasterEnvList
;
1227 CHAR16
*NewCommandLine1
;
1228 CHAR16
*NewCommandLine2
;
1233 SCRIPT_FILE
*CurrentScriptFile
;
1234 ALIAS_LIST
*AliasListNode
;
1236 ASSERT(OriginalCommandLine
!= NULL
);
1239 NewSize
= StrSize(OriginalCommandLine
);
1240 CurrentScriptFile
= ShellCommandGetCurrentScriptFile();
1243 ///@todo update this to handle the %0 - %9 for scripting only (borrow from line 1256 area) ? ? ?
1246 // calculate the size required for the post-conversion string...
1248 if (CurrentScriptFile
!= NULL
) {
1249 for (AliasListNode
= (ALIAS_LIST
*)GetFirstNode(&CurrentScriptFile
->SubstList
)
1250 ; !IsNull(&CurrentScriptFile
->SubstList
, &AliasListNode
->Link
)
1251 ; AliasListNode
= (ALIAS_LIST
*)GetNextNode(&CurrentScriptFile
->SubstList
, &AliasListNode
->Link
)
1253 for (Temp
= StrStr(OriginalCommandLine
, AliasListNode
->Alias
)
1255 ; Temp
= StrStr(Temp
+1, AliasListNode
->Alias
)
1258 // we need a preceeding and if there is space no ^ preceeding (if no space ignore)
1260 if ((((Temp
-OriginalCommandLine
)>2) && *(Temp
-2) != L
'^') || ((Temp
-OriginalCommandLine
)<=2)) {
1261 NewSize
+= StrSize(AliasListNode
->CommandString
);
1267 for (MasterEnvList
= EfiShellGetEnv(NULL
)
1268 ; MasterEnvList
!= NULL
&& *MasterEnvList
!= CHAR_NULL
//&& *(MasterEnvList+1) != CHAR_NULL
1269 ; MasterEnvList
+= StrLen(MasterEnvList
) + 1
1271 if (StrSize(MasterEnvList
) > ItemSize
) {
1272 ItemSize
= StrSize(MasterEnvList
);
1274 for (Temp
= StrStr(OriginalCommandLine
, MasterEnvList
)
1276 ; Temp
= StrStr(Temp
+1, MasterEnvList
)
1279 // we need a preceeding and following % and if there is space no ^ preceeding (if no space ignore)
1281 if (*(Temp
-1) == L
'%' && *(Temp
+StrLen(MasterEnvList
)) == L
'%' &&
1282 ((((Temp
-OriginalCommandLine
)>2) && *(Temp
-2) != L
'^') || ((Temp
-OriginalCommandLine
)<=2))) {
1283 NewSize
+=StrSize(EfiShellGetEnv(MasterEnvList
));
1289 // now do the replacements...
1291 NewCommandLine1
= AllocateZeroPool(NewSize
);
1292 NewCommandLine2
= AllocateZeroPool(NewSize
);
1293 ItemTemp
= AllocateZeroPool(ItemSize
+(2*sizeof(CHAR16
)));
1294 if (NewCommandLine1
== NULL
|| NewCommandLine2
== NULL
|| ItemTemp
== NULL
) {
1295 SHELL_FREE_NON_NULL(NewCommandLine1
);
1296 SHELL_FREE_NON_NULL(NewCommandLine2
);
1297 SHELL_FREE_NON_NULL(ItemTemp
);
1300 StrCpy(NewCommandLine1
, OriginalCommandLine
);
1301 for (MasterEnvList
= EfiShellGetEnv(NULL
)
1302 ; MasterEnvList
!= NULL
&& *MasterEnvList
!= CHAR_NULL
//&& *(MasterEnvList+1) != CHAR_NULL
1303 ; MasterEnvList
+= StrLen(MasterEnvList
) + 1
1305 StrCpy(ItemTemp
, L
"%");
1306 StrCat(ItemTemp
, MasterEnvList
);
1307 StrCat(ItemTemp
, L
"%");
1308 ShellCopySearchAndReplace(NewCommandLine1
, NewCommandLine2
, NewSize
, ItemTemp
, EfiShellGetEnv(MasterEnvList
), TRUE
, FALSE
);
1309 StrCpy(NewCommandLine1
, NewCommandLine2
);
1311 if (CurrentScriptFile
!= NULL
) {
1312 for (AliasListNode
= (ALIAS_LIST
*)GetFirstNode(&CurrentScriptFile
->SubstList
)
1313 ; !IsNull(&CurrentScriptFile
->SubstList
, &AliasListNode
->Link
)
1314 ; AliasListNode
= (ALIAS_LIST
*)GetNextNode(&CurrentScriptFile
->SubstList
, &AliasListNode
->Link
)
1316 ShellCopySearchAndReplace(NewCommandLine1
, NewCommandLine2
, NewSize
, AliasListNode
->Alias
, AliasListNode
->CommandString
, TRUE
, FALSE
);
1317 StrCpy(NewCommandLine1
, NewCommandLine2
);
1321 // Remove non-existant environment variables in scripts only
1323 for (Temp
= NewCommandLine1
; Temp
!= NULL
; ) {
1324 Temp
= StrStr(Temp
, L
"%");
1328 while (*(Temp
- 1) == L
'^') {
1329 Temp
= StrStr(Temp
+ 1, L
"%");
1338 Temp2
= StrStr(Temp
+ 1, L
"%");
1339 if (Temp2
== NULL
) {
1342 while (*(Temp2
- 1) == L
'^') {
1343 Temp2
= StrStr(Temp2
+ 1, L
"%");
1344 if (Temp2
== NULL
) {
1348 if (Temp2
== NULL
) {
1353 CopyMem(Temp
, Temp2
, StrSize(Temp2
));
1359 // Now cleanup any straggler intentionally ignored "%" characters
1361 ShellCopySearchAndReplace(NewCommandLine1
, NewCommandLine2
, NewSize
, L
"^%", L
"%", TRUE
, FALSE
);
1362 StrCpy(NewCommandLine1
, NewCommandLine2
);
1364 FreePool(NewCommandLine2
);
1367 return (NewCommandLine1
);
1371 Internal function to run a command line with pipe usage.
1373 @param[in] CmdLine The pointer to the command line.
1374 @param[in] StdIn The pointer to the Standard input.
1375 @param[in] StdOut The pointer to the Standard output.
1377 @param[out] ExitStatus The exit code of the last command in the pipeline.
1380 @retval EFI_SUCCESS The split command is executed successfully.
1381 @retval other Some error occurs when executing the split command.
1386 IN CONST CHAR16
*CmdLine
,
1387 IN SHELL_FILE_HANDLE
*StdIn
,
1388 IN SHELL_FILE_HANDLE
*StdOut
,
1389 OUT SHELL_STATUS
*ExitStatus
1393 CHAR16
*NextCommandLine
;
1394 CHAR16
*OurCommandLine
;
1398 SHELL_FILE_HANDLE
*TempFileHandle
;
1401 ASSERT(StdOut
== NULL
);
1403 ASSERT(StrStr(CmdLine
, L
"|") != NULL
);
1405 Status
= EFI_SUCCESS
;
1406 NextCommandLine
= NULL
;
1407 OurCommandLine
= NULL
;
1411 NextCommandLine
= StrnCatGrow(&NextCommandLine
, &Size1
, StrStr(CmdLine
, L
"|")+1, 0);
1412 OurCommandLine
= StrnCatGrow(&OurCommandLine
, &Size2
, CmdLine
, StrStr(CmdLine
, L
"|") - CmdLine
);
1414 if (NextCommandLine
== NULL
|| OurCommandLine
== NULL
) {
1415 SHELL_FREE_NON_NULL(OurCommandLine
);
1416 SHELL_FREE_NON_NULL(NextCommandLine
);
1417 return (EFI_OUT_OF_RESOURCES
);
1418 } else if (StrStr(OurCommandLine
, L
"|") != NULL
|| Size1
== 0 || Size2
== 0) {
1419 SHELL_FREE_NON_NULL(OurCommandLine
);
1420 SHELL_FREE_NON_NULL(NextCommandLine
);
1421 return (EFI_INVALID_PARAMETER
);
1422 } else if (NextCommandLine
[0] != CHAR_NULL
&&
1423 NextCommandLine
[0] == L
'a' &&
1424 NextCommandLine
[1] == L
' '
1426 CopyMem(NextCommandLine
, NextCommandLine
+1, StrSize(NextCommandLine
) - sizeof(NextCommandLine
[0]));
1434 // make a SPLIT_LIST item and add to list
1436 Split
= AllocateZeroPool(sizeof(SPLIT_LIST
));
1437 ASSERT(Split
!= NULL
);
1438 Split
->SplitStdIn
= StdIn
;
1439 Split
->SplitStdOut
= ConvertEfiFileProtocolToShellHandle(CreateFileInterfaceMem(Unicode
), NULL
);
1440 ASSERT(Split
->SplitStdOut
!= NULL
);
1441 InsertHeadList(&ShellInfoObject
.SplitList
.Link
, &Split
->Link
);
1443 Status
= RunCommand(OurCommandLine
, NULL
);
1446 // move the output from the first to the in to the second.
1448 TempFileHandle
= Split
->SplitStdOut
;
1449 if (Split
->SplitStdIn
== StdIn
) {
1450 Split
->SplitStdOut
= NULL
;
1452 Split
->SplitStdOut
= Split
->SplitStdIn
;
1454 Split
->SplitStdIn
= TempFileHandle
;
1455 ShellInfoObject
.NewEfiShellProtocol
->SetFilePosition(ConvertShellHandleToEfiFileProtocol(Split
->SplitStdIn
), 0);
1457 if (!EFI_ERROR(Status
)) {
1458 Status
= RunCommand(NextCommandLine
, ExitStatus
);
1462 // remove the top level from the ScriptList
1464 ASSERT((SPLIT_LIST
*)GetFirstNode(&ShellInfoObject
.SplitList
.Link
) == Split
);
1465 RemoveEntryList(&Split
->Link
);
1468 // Note that the original StdIn is now the StdOut...
1470 if (Split
->SplitStdOut
!= NULL
&& Split
->SplitStdOut
!= StdIn
) {
1471 ShellInfoObject
.NewEfiShellProtocol
->CloseFile(ConvertShellHandleToEfiFileProtocol(Split
->SplitStdOut
));
1473 if (Split
->SplitStdIn
!= NULL
) {
1474 ShellInfoObject
.NewEfiShellProtocol
->CloseFile(ConvertShellHandleToEfiFileProtocol(Split
->SplitStdIn
));
1478 FreePool(NextCommandLine
);
1479 FreePool(OurCommandLine
);
1485 Take the original command line, substitute any variables, free
1486 the original string, return the modified copy.
1488 @param[in] CmdLine pointer to the command line to update.
1490 @retval EFI_SUCCESS the function was successful.
1491 @retval EFI_OUT_OF_RESOURCES a memory allocation failed.
1495 ShellSubstituteVariables(
1500 NewCmdLine
= ShellConvertVariables(*CmdLine
);
1501 SHELL_FREE_NON_NULL(*CmdLine
);
1502 if (NewCmdLine
== NULL
) {
1503 return (EFI_OUT_OF_RESOURCES
);
1505 *CmdLine
= NewCmdLine
;
1506 return (EFI_SUCCESS
);
1510 Take the original command line, substitute any alias in the first group of space delimited characters, free
1511 the original string, return the modified copy.
1513 @param[in] CmdLine pointer to the command line to update.
1515 @retval EFI_SUCCESS the function was successful.
1516 @retval EFI_OUT_OF_RESOURCES a memory allocation failed.
1520 ShellSubstituteAliases(
1525 CHAR16
*CommandName
;
1527 UINTN PostAliasSize
;
1528 ASSERT(CmdLine
!= NULL
);
1529 ASSERT(*CmdLine
!= NULL
);
1533 if (StrStr((*CmdLine
), L
" ") == NULL
){
1534 StrnCatGrow(&CommandName
, NULL
, (*CmdLine
), 0);
1536 StrnCatGrow(&CommandName
, NULL
, (*CmdLine
), StrStr((*CmdLine
), L
" ") - (*CmdLine
));
1540 // This cannot happen 'inline' since the CmdLine can need extra space.
1543 if (!ShellCommandIsCommandOnList(CommandName
)) {
1545 // Convert via alias
1547 Status
= ShellConvertAlias(&CommandName
);
1548 if (EFI_ERROR(Status
)){
1552 NewCmdLine
= StrnCatGrow(&NewCmdLine
, &PostAliasSize
, CommandName
, 0);
1553 if (NewCmdLine
== NULL
) {
1554 SHELL_FREE_NON_NULL(CommandName
);
1555 SHELL_FREE_NON_NULL(*CmdLine
);
1556 return (EFI_OUT_OF_RESOURCES
);
1558 NewCmdLine
= StrnCatGrow(&NewCmdLine
, &PostAliasSize
, StrStr((*CmdLine
), L
" "), 0);
1559 if (NewCmdLine
== NULL
) {
1560 SHELL_FREE_NON_NULL(CommandName
);
1561 SHELL_FREE_NON_NULL(*CmdLine
);
1562 return (EFI_OUT_OF_RESOURCES
);
1565 NewCmdLine
= StrnCatGrow(&NewCmdLine
, NULL
, (*CmdLine
), 0);
1568 SHELL_FREE_NON_NULL(*CmdLine
);
1569 SHELL_FREE_NON_NULL(CommandName
);
1572 // re-assign the passed in double pointer to point to our newly allocated buffer
1574 *CmdLine
= NewCmdLine
;
1576 return (EFI_SUCCESS
);
1580 Takes the Argv[0] part of the command line and determine the meaning of it.
1582 @param[in] CmdName pointer to the command line to update.
1584 @retval Internal_Command The name is an internal command.
1585 @retval File_Sys_Change the name is a file system change.
1586 @retval Script_File_Name the name is a NSH script file.
1587 @retval Unknown_Invalid the name is unknown.
1588 @retval Efi_Application the name is an application (.EFI).
1590 SHELL_OPERATION_TYPES
1593 IN CONST CHAR16
*CmdName
1596 CHAR16
* FileWithPath
;
1597 CONST CHAR16
* TempLocation
;
1598 CONST CHAR16
* TempLocation2
;
1600 FileWithPath
= NULL
;
1602 // test for an internal command.
1604 if (ShellCommandIsCommandOnList(CmdName
)) {
1605 return (Internal_Command
);
1609 // Test for file system change request. anything ending with : and cant have spaces.
1611 if (CmdName
[(StrLen(CmdName
)-1)] == L
':') {
1612 if (StrStr(CmdName
, L
" ") != NULL
) {
1613 return (Unknown_Invalid
);
1615 return (File_Sys_Change
);
1621 if ((FileWithPath
= ShellFindFilePathEx(CmdName
, mExecutableExtensions
)) != NULL
) {
1623 // See if that file has a script file extension
1625 if (StrLen(FileWithPath
) > 4) {
1626 TempLocation
= FileWithPath
+StrLen(FileWithPath
)-4;
1627 TempLocation2
= mScriptExtension
;
1628 if (StringNoCaseCompare((VOID
*)(&TempLocation
), (VOID
*)(&TempLocation2
)) == 0) {
1629 SHELL_FREE_NON_NULL(FileWithPath
);
1630 return (Script_File_Name
);
1635 // Was a file, but not a script. we treat this as an application.
1637 SHELL_FREE_NON_NULL(FileWithPath
);
1638 return (Efi_Application
);
1641 SHELL_FREE_NON_NULL(FileWithPath
);
1643 // No clue what this is... return invalid flag...
1645 return (Unknown_Invalid
);
1649 Determine if the first item in a command line is valid.
1651 @param[in] CmdLine The command line to parse.
1653 @retval EFI_SUCCESS The item is valid.
1654 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
1655 @retval EFI_NOT_FOUND The operation type is unknown or invalid.
1660 IN CONST CHAR16
*CmdLine
1664 CHAR16
*FirstParameter
;
1670 Temp
= StrnCatGrow(&Temp
, NULL
, CmdLine
, 0);
1672 return (EFI_OUT_OF_RESOURCES
);
1675 FirstParameter
= StrStr(Temp
, L
"|");
1676 if (FirstParameter
!= NULL
) {
1677 *FirstParameter
= CHAR_NULL
;
1680 FirstParameter
= NULL
;
1683 // Process the command line
1685 Status
= ProcessCommandLineToFinal(&Temp
);
1687 if (!EFI_ERROR(Status
)) {
1688 FirstParameter
= AllocateZeroPool(StrSize(CmdLine
));
1689 if (FirstParameter
== NULL
) {
1690 SHELL_FREE_NON_NULL(Temp
);
1691 return (EFI_OUT_OF_RESOURCES
);
1693 TempWalker
= (CHAR16
*)Temp
;
1694 GetNextParameter(&TempWalker
, &FirstParameter
);
1696 if (GetOperationType(FirstParameter
) == Unknown_Invalid
) {
1697 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_NOT_FOUND
), ShellInfoObject
.HiiHandle
, FirstParameter
);
1698 SetLastError(SHELL_NOT_FOUND
);
1699 Status
= EFI_NOT_FOUND
;
1703 SHELL_FREE_NON_NULL(Temp
);
1704 SHELL_FREE_NON_NULL(FirstParameter
);
1709 Determine if a command line contains with a split contains only valid commands.
1711 @param[in] CmdLine The command line to parse.
1713 @retval EFI_SUCCESS CmdLine has only valid commands, application, or has no split.
1714 @retval EFI_ABORTED CmdLine has at least one invalid command or application.
1719 IN CONST CHAR16
*CmdLine
1722 CONST CHAR16
*TempSpot
;
1726 // Verify up to the pipe or end character
1728 Status
= IsValidSplit(CmdLine
);
1729 if (EFI_ERROR(Status
)) {
1734 // If this was the only item, then get out
1736 if (!ContainsSplit(CmdLine
)) {
1737 return (EFI_SUCCESS
);
1741 // recurse to verify the next item
1743 TempSpot
= FindSplit(CmdLine
)+1;
1744 return (VerifySplit(TempSpot
));
1748 Process a split based operation.
1750 @param[in] CmdLine Pointer to the command line to process
1751 @param[out] ExitStatus The exit status of the command. Ignored if NULL.
1752 Invalid if this function returns an error.
1754 @retval EFI_SUCCESS The operation was successful
1755 @return an error occured.
1759 ProcessNewSplitCommandLine(
1760 IN CONST CHAR16
*CmdLine
,
1761 OUT SHELL_STATUS
*ExitStatus
1767 Status
= VerifySplit(CmdLine
);
1768 if (EFI_ERROR(Status
)) {
1775 // are we in an existing split???
1777 if (!IsListEmpty(&ShellInfoObject
.SplitList
.Link
)) {
1778 Split
= (SPLIT_LIST
*)GetFirstNode(&ShellInfoObject
.SplitList
.Link
);
1781 if (Split
== NULL
) {
1782 Status
= RunSplitCommand(CmdLine
, NULL
, NULL
, ExitStatus
);
1784 Status
= RunSplitCommand(
1791 if (EFI_ERROR(Status
)) {
1792 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_INVALID_SPLIT
), ShellInfoObject
.HiiHandle
, CmdLine
);
1798 Handle a request to change the current file system.
1800 @param[in] CmdLine The passed in command line.
1802 @retval EFI_SUCCESS The operation was successful.
1807 IN CONST CHAR16
*CmdLine
1811 Status
= EFI_SUCCESS
;
1814 // make sure we are the right operation
1816 ASSERT(CmdLine
[(StrLen(CmdLine
)-1)] == L
':' && StrStr(CmdLine
, L
" ") == NULL
);
1819 // Call the protocol API to do the work
1821 Status
= ShellInfoObject
.NewEfiShellProtocol
->SetCurDir(NULL
, CmdLine
);
1824 // Report any errors
1826 if (EFI_ERROR(Status
)) {
1827 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_INVALID_MAPPING
), ShellInfoObject
.HiiHandle
, CmdLine
);
1834 Reprocess the command line to direct all -? to the help command.
1836 if found, will add "help" as argv[0], and move the rest later.
1838 @param[in,out] CmdLine pointer to the command line to update
1843 IN OUT CHAR16
**CmdLine
1846 CHAR16
*CurrentParameter
;
1849 CHAR16
*NewCommandLine
;
1852 Status
= EFI_SUCCESS
;
1854 CurrentParameter
= AllocateZeroPool(StrSize(*CmdLine
));
1855 if (CurrentParameter
== NULL
) {
1856 return (EFI_OUT_OF_RESOURCES
);
1860 while(Walker
!= NULL
&& *Walker
!= CHAR_NULL
) {
1861 LastWalker
= Walker
;
1862 GetNextParameter(&Walker
, &CurrentParameter
);
1863 if (StrStr(CurrentParameter
, L
"-?") == CurrentParameter
) {
1864 LastWalker
[0] = L
' ';
1865 LastWalker
[1] = L
' ';
1866 NewCommandLine
= AllocateZeroPool(StrSize(L
"help ") + StrSize(*CmdLine
));
1867 if (NewCommandLine
== NULL
) {
1868 Status
= EFI_OUT_OF_RESOURCES
;
1871 StrCpy(NewCommandLine
, L
"help ");
1872 StrCat(NewCommandLine
, *CmdLine
);
1873 SHELL_FREE_NON_NULL(*CmdLine
);
1874 *CmdLine
= NewCommandLine
;
1879 SHELL_FREE_NON_NULL(CurrentParameter
);
1885 Function to update the shell variable "lasterror".
1887 @param[in] ErrorCode the error code to put into lasterror.
1892 IN CONST SHELL_STATUS ErrorCode
1895 CHAR16 LeString
[19];
1896 if (sizeof(EFI_STATUS
) == sizeof(UINT64
)) {
1897 UnicodeSPrint(LeString
, sizeof(LeString
), L
"0x%Lx", ErrorCode
);
1899 UnicodeSPrint(LeString
, sizeof(LeString
), L
"0x%x", ErrorCode
);
1901 DEBUG_CODE(InternalEfiShellSetEnv(L
"debuglasterror", LeString
, TRUE
););
1902 InternalEfiShellSetEnv(L
"lasterror", LeString
, TRUE
);
1904 return (EFI_SUCCESS
);
1908 Converts the command line to it's post-processed form. this replaces variables and alias' per UEFI Shell spec.
1910 @param[in,out] CmdLine pointer to the command line to update
1912 @retval EFI_SUCCESS The operation was successful
1913 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
1914 @return some other error occured
1918 ProcessCommandLineToFinal(
1919 IN OUT CHAR16
**CmdLine
1923 TrimSpaces(CmdLine
);
1925 Status
= ShellSubstituteAliases(CmdLine
);
1926 if (EFI_ERROR(Status
)) {
1930 TrimSpaces(CmdLine
);
1932 Status
= ShellSubstituteVariables(CmdLine
);
1933 if (EFI_ERROR(Status
)) {
1937 TrimSpaces(CmdLine
);
1940 // update for help parsing
1942 if (StrStr(*CmdLine
, L
"?") != NULL
) {
1944 // This may do nothing if the ? does not indicate help.
1945 // Save all the details for in the API below.
1947 Status
= DoHelpUpdate(CmdLine
);
1950 TrimSpaces(CmdLine
);
1952 return (EFI_SUCCESS
);
1956 Run an internal shell command.
1958 This API will upadate the shell's environment since these commands are libraries.
1960 @param[in] CmdLine the command line to run.
1961 @param[in] FirstParameter the first parameter on the command line
1962 @param[in] ParamProtocol the shell parameters protocol pointer
1964 @param[out] ExitStatus The exit code of the command. Ignored if NULL.
1966 @retval EFI_SUCCESS The command was completed.
1967 @retval EFI_ABORTED The command's operation was aborted.
1972 IN CONST CHAR16
*CmdLine
,
1973 IN CHAR16
*FirstParameter
,
1974 IN EFI_SHELL_PARAMETERS_PROTOCOL
*ParamProtocol
,
1975 OUT SHELL_STATUS
*ExitStatus OPTIONAL
1981 SHELL_STATUS CommandReturnedStatus
;
1985 // get the argc and argv updated for internal commands
1987 Status
= UpdateArgcArgv(ParamProtocol
, CmdLine
, &Argv
, &Argc
);
1988 if (!EFI_ERROR(Status
)) {
1990 // Run the internal command.
1992 Status
= ShellCommandRunCommandHandler(FirstParameter
, &CommandReturnedStatus
, &LastError
);
1994 if (!EFI_ERROR(Status
)) {
1996 // Update last error status.
1997 // some commands do not update last error.
2000 SetLastError(CommandReturnedStatus
);
2002 if (ExitStatus
!= NULL
) {
2003 *ExitStatus
= CommandReturnedStatus
;
2007 // Pass thru the exitcode from the app.
2009 if (ShellCommandGetExit()) {
2011 // An Exit was requested ("exit" command), pass its value up.
2013 Status
= CommandReturnedStatus
;
2014 } else if (CommandReturnedStatus
!= SHELL_SUCCESS
&& IsScriptOnlyCommand(FirstParameter
)) {
2016 // Always abort when a script only command fails for any reason
2018 Status
= EFI_ABORTED
;
2019 } else if (ShellCommandGetCurrentScriptFile() != NULL
&& CommandReturnedStatus
== SHELL_ABORTED
) {
2021 // Abort when in a script and a command aborted
2023 Status
= EFI_ABORTED
;
2029 // This is guarenteed to be called after UpdateArgcArgv no matter what else happened.
2030 // This is safe even if the update API failed. In this case, it may be a no-op.
2032 RestoreArgcArgv(ParamProtocol
, &Argv
, &Argc
);
2035 // If a script is running and the command is not a scipt only command, then
2036 // change return value to success so the script won't halt (unless aborted).
2038 // Script only commands have to be able halt the script since the script will
2039 // not operate if they are failing.
2041 if ( ShellCommandGetCurrentScriptFile() != NULL
2042 && !IsScriptOnlyCommand(FirstParameter
)
2043 && Status
!= EFI_ABORTED
2045 Status
= EFI_SUCCESS
;
2052 Function to run the command or file.
2054 @param[in] Type the type of operation being run.
2055 @param[in] CmdLine the command line to run.
2056 @param[in] FirstParameter the first parameter on the command line
2057 @param[in] ParamProtocol the shell parameters protocol pointer
2059 @param[out] ExitStatus The exit code of the command or file.
2062 @retval EFI_SUCCESS The command was completed.
2063 @retval EFI_ABORTED The command's operation was aborted.
2068 IN SHELL_OPERATION_TYPES Type
,
2069 IN CONST CHAR16
*CmdLine
,
2070 IN CHAR16
*FirstParameter
,
2071 IN EFI_SHELL_PARAMETERS_PROTOCOL
*ParamProtocol
,
2072 OUT SHELL_STATUS
*ExitStatus
2076 CHAR16
*CommandWithPath
;
2077 EFI_DEVICE_PATH_PROTOCOL
*DevPath
;
2078 SHELL_STATUS CalleeExitStatus
;
2080 Status
= EFI_SUCCESS
;
2081 CommandWithPath
= NULL
;
2083 CalleeExitStatus
= SHELL_INVALID_PARAMETER
;
2086 case Internal_Command
:
2087 Status
= RunInternalCommand(
2094 case Script_File_Name
:
2095 case Efi_Application
:
2097 // Process a fully qualified path
2099 if (StrStr(FirstParameter
, L
":") != NULL
) {
2100 ASSERT (CommandWithPath
== NULL
);
2101 if (ShellIsFile(FirstParameter
) == EFI_SUCCESS
) {
2102 CommandWithPath
= StrnCatGrow(&CommandWithPath
, NULL
, FirstParameter
, 0);
2107 // Process a relative path and also check in the path environment variable
2109 if (CommandWithPath
== NULL
) {
2110 CommandWithPath
= ShellFindFilePathEx(FirstParameter
, mExecutableExtensions
);
2114 // This should be impossible now.
2116 ASSERT(CommandWithPath
!= NULL
);
2119 // Make sure that path is not just a directory (or not found)
2121 if (!EFI_ERROR(ShellIsDirectory(CommandWithPath
))) {
2122 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_NOT_FOUND
), ShellInfoObject
.HiiHandle
, FirstParameter
);
2123 SetLastError(SHELL_NOT_FOUND
);
2126 case Script_File_Name
:
2127 Status
= RunScriptFile (
2135 case Efi_Application
:
2137 // Get the device path of the application image
2139 DevPath
= ShellInfoObject
.NewEfiShellProtocol
->GetDevicePathFromFilePath(CommandWithPath
);
2140 if (DevPath
== NULL
){
2141 Status
= EFI_OUT_OF_RESOURCES
;
2146 // Execute the device path
2148 Status
= InternalShellExecuteDevicePath(
2157 SHELL_FREE_NON_NULL(DevPath
);
2159 if(EFI_ERROR (Status
)) {
2160 CalleeExitStatus
= (SHELL_STATUS
) (Status
& (~MAX_BIT
));
2162 CalleeExitStatus
= SHELL_SUCCESS
;
2166 // Update last error status.
2168 // Status is an EFI_STATUS. Clear top bit to convert to SHELL_STATUS
2169 SetLastError(CalleeExitStatus
);
2185 SHELL_FREE_NON_NULL(CommandWithPath
);
2187 if (ExitStatus
!= NULL
) {
2188 *ExitStatus
= CalleeExitStatus
;
2195 Function to setup StdIn, StdErr, StdOut, and then run the command or file.
2197 @param[in] Type the type of operation being run.
2198 @param[in] CmdLine the command line to run.
2199 @param[in] FirstParameter the first parameter on the command line.
2200 @param[in] ParamProtocol the shell parameters protocol pointer
2202 @param[out] ExitStatus The exit code of the command or file.
2205 @retval EFI_SUCCESS The command was completed.
2206 @retval EFI_ABORTED The command's operation was aborted.
2210 SetupAndRunCommandOrFile(
2211 IN SHELL_OPERATION_TYPES Type
,
2213 IN CHAR16
*FirstParameter
,
2214 IN EFI_SHELL_PARAMETERS_PROTOCOL
*ParamProtocol
,
2215 OUT SHELL_STATUS
*ExitStatus
2219 SHELL_FILE_HANDLE OriginalStdIn
;
2220 SHELL_FILE_HANDLE OriginalStdOut
;
2221 SHELL_FILE_HANDLE OriginalStdErr
;
2222 SYSTEM_TABLE_INFO OriginalSystemTableInfo
;
2225 // Update the StdIn, StdOut, and StdErr for redirection to environment variables, files, etc... unicode and ASCII
2227 Status
= UpdateStdInStdOutStdErr(ParamProtocol
, CmdLine
, &OriginalStdIn
, &OriginalStdOut
, &OriginalStdErr
, &OriginalSystemTableInfo
);
2230 // The StdIn, StdOut, and StdErr are set up.
2231 // Now run the command, script, or application
2233 if (!EFI_ERROR(Status
)) {
2234 Status
= RunCommandOrFile(
2246 if (EFI_ERROR(Status
)) {
2247 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_ERROR
), ShellInfoObject
.HiiHandle
, (VOID
*)(Status
));
2251 // put back the original StdIn, StdOut, and StdErr
2253 RestoreStdInStdOutStdErr(ParamProtocol
, &OriginalStdIn
, &OriginalStdOut
, &OriginalStdErr
, &OriginalSystemTableInfo
);
2259 Function will process and run a command line.
2261 This will determine if the command line represents an internal shell
2262 command or dispatch an external application.
2264 @param[in] CmdLine The command line to parse.
2265 @param[out] ExitStatus The exit code of the command. Ignored if NULL.
2267 @retval EFI_SUCCESS The command was completed.
2268 @retval EFI_ABORTED The command's operation was aborted.
2273 IN CONST CHAR16
*CmdLine
,
2274 OUT SHELL_STATUS
*ExitStatus
2278 CHAR16
*CleanOriginal
;
2279 CHAR16
*FirstParameter
;
2281 SHELL_OPERATION_TYPES Type
;
2283 ASSERT(CmdLine
!= NULL
);
2284 if (StrLen(CmdLine
) == 0) {
2285 return (EFI_SUCCESS
);
2288 Status
= EFI_SUCCESS
;
2289 CleanOriginal
= NULL
;
2291 CleanOriginal
= StrnCatGrow(&CleanOriginal
, NULL
, CmdLine
, 0);
2292 if (CleanOriginal
== NULL
) {
2293 return (EFI_OUT_OF_RESOURCES
);
2296 TrimSpaces(&CleanOriginal
);
2299 // Handle case that passed in command line is just 1 or more " " characters.
2301 if (StrLen (CleanOriginal
) == 0) {
2302 SHELL_FREE_NON_NULL(CleanOriginal
);
2303 return (EFI_SUCCESS
);
2306 Status
= ProcessCommandLineToFinal(&CleanOriginal
);
2307 if (EFI_ERROR(Status
)) {
2308 SHELL_FREE_NON_NULL(CleanOriginal
);
2313 // We dont do normal processing with a split command line (output from one command input to another)
2315 if (ContainsSplit(CleanOriginal
)) {
2316 Status
= ProcessNewSplitCommandLine(CleanOriginal
, ExitStatus
);
2317 SHELL_FREE_NON_NULL(CleanOriginal
);
2322 // We need the first parameter information so we can determine the operation type
2324 FirstParameter
= AllocateZeroPool(StrSize(CleanOriginal
));
2325 if (FirstParameter
== NULL
) {
2326 SHELL_FREE_NON_NULL(CleanOriginal
);
2327 return (EFI_OUT_OF_RESOURCES
);
2329 TempWalker
= CleanOriginal
;
2330 GetNextParameter(&TempWalker
, &FirstParameter
);
2333 // Depending on the first parameter we change the behavior
2335 switch (Type
= GetOperationType(FirstParameter
)) {
2336 case File_Sys_Change
:
2337 Status
= ChangeMappedDrive(CleanOriginal
);
2339 case Internal_Command
:
2340 case Script_File_Name
:
2341 case Efi_Application
:
2342 Status
= SetupAndRunCommandOrFile(
2346 ShellInfoObject
.NewShellParametersProtocol
,
2352 // Whatever was typed, it was invalid.
2354 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_NOT_FOUND
), ShellInfoObject
.HiiHandle
, FirstParameter
);
2355 SetLastError(SHELL_NOT_FOUND
);
2359 SHELL_FREE_NON_NULL(CleanOriginal
);
2360 SHELL_FREE_NON_NULL(FirstParameter
);
2365 STATIC CONST UINT16 InvalidChars
[] = {L
'*', L
'?', L
'<', L
'>', L
'\\', L
'/', L
'\"', 0x0001, 0x0002};
2367 Function determins if the CommandName COULD be a valid command. It does not determine whether
2368 this is a valid command. It only checks for invalid characters.
2370 @param[in] CommandName The name to check
2372 @retval TRUE CommandName could be a command name
2373 @retval FALSE CommandName could not be a valid command name
2378 IN CONST CHAR16
*CommandName
2382 if (CommandName
== NULL
) {
2387 ; Count
< sizeof(InvalidChars
) / sizeof(InvalidChars
[0])
2390 if (ScanMem16(CommandName
, StrSize(CommandName
), InvalidChars
[Count
]) != NULL
) {
2398 Function to process a NSH script file via SHELL_FILE_HANDLE.
2400 @param[in] Handle The handle to the already opened file.
2401 @param[in] Name The name of the script file.
2403 @param[out] ExitStatus The exit code of the script. Ignored if NULL.
2405 @retval EFI_SUCCESS the script completed sucessfully
2409 RunScriptFileHandle (
2410 IN SHELL_FILE_HANDLE Handle
,
2411 IN CONST CHAR16
*Name
,
2412 OUT SHELL_STATUS
*ExitStatus
2416 SCRIPT_FILE
*NewScriptFile
;
2418 CHAR16
*CommandLine
;
2419 CHAR16
*CommandLine2
;
2420 CHAR16
*CommandLine3
;
2421 SCRIPT_COMMAND_LIST
*LastCommand
;
2423 BOOLEAN PreScriptEchoState
;
2424 BOOLEAN PreCommandEchoState
;
2425 CONST CHAR16
*CurDir
;
2427 CHAR16 LeString
[50];
2428 SHELL_STATUS CalleeExitStatus
;
2430 ASSERT(!ShellCommandGetScriptExit());
2432 CalleeExitStatus
= SHELL_SUCCESS
;
2434 PreScriptEchoState
= ShellCommandGetEchoState();
2436 NewScriptFile
= (SCRIPT_FILE
*)AllocateZeroPool(sizeof(SCRIPT_FILE
));
2437 if (NewScriptFile
== NULL
) {
2438 return (EFI_OUT_OF_RESOURCES
);
2444 ASSERT(NewScriptFile
->ScriptName
== NULL
);
2445 NewScriptFile
->ScriptName
= StrnCatGrow(&NewScriptFile
->ScriptName
, NULL
, Name
, 0);
2446 if (NewScriptFile
->ScriptName
== NULL
) {
2447 DeleteScriptFileStruct(NewScriptFile
);
2448 return (EFI_OUT_OF_RESOURCES
);
2452 // Save the parameters (used to replace %0 to %9 later on)
2454 NewScriptFile
->Argc
= ShellInfoObject
.NewShellParametersProtocol
->Argc
;
2455 if (NewScriptFile
->Argc
!= 0) {
2456 NewScriptFile
->Argv
= (CHAR16
**)AllocateZeroPool(NewScriptFile
->Argc
* sizeof(CHAR16
*));
2457 if (NewScriptFile
->Argv
== NULL
) {
2458 DeleteScriptFileStruct(NewScriptFile
);
2459 return (EFI_OUT_OF_RESOURCES
);
2461 for (LoopVar
= 0 ; LoopVar
< 10 && LoopVar
< NewScriptFile
->Argc
; LoopVar
++) {
2462 ASSERT(NewScriptFile
->Argv
[LoopVar
] == NULL
);
2463 NewScriptFile
->Argv
[LoopVar
] = StrnCatGrow(&NewScriptFile
->Argv
[LoopVar
], NULL
, ShellInfoObject
.NewShellParametersProtocol
->Argv
[LoopVar
], 0);
2464 if (NewScriptFile
->Argv
[LoopVar
] == NULL
) {
2465 DeleteScriptFileStruct(NewScriptFile
);
2466 return (EFI_OUT_OF_RESOURCES
);
2470 NewScriptFile
->Argv
= NULL
;
2473 InitializeListHead(&NewScriptFile
->CommandList
);
2474 InitializeListHead(&NewScriptFile
->SubstList
);
2477 // Now build the list of all script commands.
2480 while(!ShellFileHandleEof(Handle
)) {
2481 CommandLine
= ShellFileHandleReturnLine(Handle
, &Ascii
);
2483 if (CommandLine
== NULL
|| StrLen(CommandLine
) == 0 || CommandLine
[0] == '#') {
2484 SHELL_FREE_NON_NULL(CommandLine
);
2487 NewScriptFile
->CurrentCommand
= AllocateZeroPool(sizeof(SCRIPT_COMMAND_LIST
));
2488 if (NewScriptFile
->CurrentCommand
== NULL
) {
2489 SHELL_FREE_NON_NULL(CommandLine
);
2490 DeleteScriptFileStruct(NewScriptFile
);
2491 return (EFI_OUT_OF_RESOURCES
);
2494 NewScriptFile
->CurrentCommand
->Cl
= CommandLine
;
2495 NewScriptFile
->CurrentCommand
->Data
= NULL
;
2496 NewScriptFile
->CurrentCommand
->Line
= LineCount
;
2498 InsertTailList(&NewScriptFile
->CommandList
, &NewScriptFile
->CurrentCommand
->Link
);
2502 // Add this as the topmost script file
2504 ShellCommandSetNewScript (NewScriptFile
);
2507 // Now enumerate through the commands and run each one.
2509 CommandLine
= AllocateZeroPool(PcdGet16(PcdShellPrintBufferSize
));
2510 if (CommandLine
== NULL
) {
2511 DeleteScriptFileStruct(NewScriptFile
);
2512 return (EFI_OUT_OF_RESOURCES
);
2514 CommandLine2
= AllocateZeroPool(PcdGet16(PcdShellPrintBufferSize
));
2515 if (CommandLine2
== NULL
) {
2516 FreePool(CommandLine
);
2517 DeleteScriptFileStruct(NewScriptFile
);
2518 return (EFI_OUT_OF_RESOURCES
);
2521 for ( NewScriptFile
->CurrentCommand
= (SCRIPT_COMMAND_LIST
*)GetFirstNode(&NewScriptFile
->CommandList
)
2522 ; !IsNull(&NewScriptFile
->CommandList
, &NewScriptFile
->CurrentCommand
->Link
)
2523 ; // conditional increment in the body of the loop
2525 ASSERT(CommandLine2
!= NULL
);
2526 StrCpy(CommandLine2
, NewScriptFile
->CurrentCommand
->Cl
);
2529 // NULL out comments
2531 for (CommandLine3
= CommandLine2
; CommandLine3
!= NULL
&& *CommandLine3
!= CHAR_NULL
; CommandLine3
++) {
2532 if (*CommandLine3
== L
'^') {
2533 if (*(CommandLine3
+1) == L
'#' || *(CommandLine3
+1) == L
':') {
2534 CopyMem(CommandLine3
, CommandLine3
+1, StrSize(CommandLine3
) - sizeof(CommandLine3
[0]));
2536 } else if (*CommandLine3
== L
'#') {
2537 *CommandLine3
= CHAR_NULL
;
2541 if (CommandLine2
!= NULL
&& StrLen(CommandLine2
) >= 1) {
2543 // Due to variability in starting the find and replace action we need to have both buffers the same.
2545 StrCpy(CommandLine
, CommandLine2
);
2548 // Remove the %0 to %9 from the command line (if we have some arguments)
2550 if (NewScriptFile
->Argv
!= NULL
) {
2551 switch (NewScriptFile
->Argc
) {
2553 Status
= ShellCopySearchAndReplace(CommandLine2
, CommandLine
, PcdGet16 (PcdShellPrintBufferSize
), L
"%9", NewScriptFile
->Argv
[9], FALSE
, TRUE
);
2554 ASSERT_EFI_ERROR(Status
);
2556 Status
= ShellCopySearchAndReplace(CommandLine
, CommandLine2
, PcdGet16 (PcdShellPrintBufferSize
), L
"%8", NewScriptFile
->Argv
[8], FALSE
, TRUE
);
2557 ASSERT_EFI_ERROR(Status
);
2559 Status
= ShellCopySearchAndReplace(CommandLine2
, CommandLine
, PcdGet16 (PcdShellPrintBufferSize
), L
"%7", NewScriptFile
->Argv
[7], FALSE
, TRUE
);
2560 ASSERT_EFI_ERROR(Status
);
2562 Status
= ShellCopySearchAndReplace(CommandLine
, CommandLine2
, PcdGet16 (PcdShellPrintBufferSize
), L
"%6", NewScriptFile
->Argv
[6], FALSE
, TRUE
);
2563 ASSERT_EFI_ERROR(Status
);
2565 Status
= ShellCopySearchAndReplace(CommandLine2
, CommandLine
, PcdGet16 (PcdShellPrintBufferSize
), L
"%5", NewScriptFile
->Argv
[5], FALSE
, TRUE
);
2566 ASSERT_EFI_ERROR(Status
);
2568 Status
= ShellCopySearchAndReplace(CommandLine
, CommandLine2
, PcdGet16 (PcdShellPrintBufferSize
), L
"%4", NewScriptFile
->Argv
[4], FALSE
, TRUE
);
2569 ASSERT_EFI_ERROR(Status
);
2571 Status
= ShellCopySearchAndReplace(CommandLine2
, CommandLine
, PcdGet16 (PcdShellPrintBufferSize
), L
"%3", NewScriptFile
->Argv
[3], FALSE
, TRUE
);
2572 ASSERT_EFI_ERROR(Status
);
2574 Status
= ShellCopySearchAndReplace(CommandLine
, CommandLine2
, PcdGet16 (PcdShellPrintBufferSize
), L
"%2", NewScriptFile
->Argv
[2], FALSE
, TRUE
);
2575 ASSERT_EFI_ERROR(Status
);
2577 Status
= ShellCopySearchAndReplace(CommandLine2
, CommandLine
, PcdGet16 (PcdShellPrintBufferSize
), L
"%1", NewScriptFile
->Argv
[1], FALSE
, TRUE
);
2578 ASSERT_EFI_ERROR(Status
);
2580 Status
= ShellCopySearchAndReplace(CommandLine
, CommandLine2
, PcdGet16 (PcdShellPrintBufferSize
), L
"%0", NewScriptFile
->Argv
[0], FALSE
, TRUE
);
2581 ASSERT_EFI_ERROR(Status
);
2587 Status
= ShellCopySearchAndReplace(CommandLine2
, CommandLine
, PcdGet16 (PcdShellPrintBufferSize
), L
"%1", L
"\"\"", FALSE
, FALSE
);
2588 Status
= ShellCopySearchAndReplace(CommandLine
, CommandLine2
, PcdGet16 (PcdShellPrintBufferSize
), L
"%2", L
"\"\"", FALSE
, FALSE
);
2589 Status
= ShellCopySearchAndReplace(CommandLine2
, CommandLine
, PcdGet16 (PcdShellPrintBufferSize
), L
"%3", L
"\"\"", FALSE
, FALSE
);
2590 Status
= ShellCopySearchAndReplace(CommandLine
, CommandLine2
, PcdGet16 (PcdShellPrintBufferSize
), L
"%4", L
"\"\"", FALSE
, FALSE
);
2591 Status
= ShellCopySearchAndReplace(CommandLine2
, CommandLine
, PcdGet16 (PcdShellPrintBufferSize
), L
"%5", L
"\"\"", FALSE
, FALSE
);
2592 Status
= ShellCopySearchAndReplace(CommandLine
, CommandLine2
, PcdGet16 (PcdShellPrintBufferSize
), L
"%6", L
"\"\"", FALSE
, FALSE
);
2593 Status
= ShellCopySearchAndReplace(CommandLine2
, CommandLine
, PcdGet16 (PcdShellPrintBufferSize
), L
"%7", L
"\"\"", FALSE
, FALSE
);
2594 Status
= ShellCopySearchAndReplace(CommandLine
, CommandLine2
, PcdGet16 (PcdShellPrintBufferSize
), L
"%8", L
"\"\"", FALSE
, FALSE
);
2595 Status
= ShellCopySearchAndReplace(CommandLine2
, CommandLine
, PcdGet16 (PcdShellPrintBufferSize
), L
"%9", L
"\"\"", FALSE
, FALSE
);
2597 StrCpy(CommandLine2
, CommandLine
);
2599 LastCommand
= NewScriptFile
->CurrentCommand
;
2601 for (CommandLine3
= CommandLine2
; CommandLine3
[0] == L
' ' ; CommandLine3
++);
2603 if (CommandLine3
!= NULL
&& CommandLine3
[0] == L
':' ) {
2605 // This line is a goto target / label
2608 if (CommandLine3
!= NULL
&& StrLen(CommandLine3
) > 0) {
2609 if (CommandLine3
[0] == L
'@') {
2611 // We need to save the current echo state
2612 // and disable echo for just this command.
2614 PreCommandEchoState
= ShellCommandGetEchoState();
2615 ShellCommandSetEchoState(FALSE
);
2616 Status
= RunCommand(CommandLine3
+1, NULL
);
2619 // If command was "@echo -off" or "@echo -on" then don't restore echo state
2621 if (StrCmp (L
"@echo -off", CommandLine3
) != 0 &&
2622 StrCmp (L
"@echo -on", CommandLine3
) != 0) {
2624 // Now restore the pre-'@' echo state.
2626 ShellCommandSetEchoState(PreCommandEchoState
);
2629 if (ShellCommandGetEchoState()) {
2630 CurDir
= ShellInfoObject
.NewEfiShellProtocol
->GetEnv(L
"cwd");
2631 if (CurDir
!= NULL
&& StrLen(CurDir
) > 1) {
2632 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_CURDIR
), ShellInfoObject
.HiiHandle
, CurDir
);
2634 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_SHELL
), ShellInfoObject
.HiiHandle
);
2636 ShellPrintEx(-1, -1, L
"%s\r\n", CommandLine2
);
2638 Status
= RunCommand(CommandLine3
, NULL
);
2642 if (ShellCommandGetScriptExit()) {
2644 // ShellCommandGetExitCode() always returns a UINT64
2646 CalleeExitStatus
= (SHELL_STATUS
) ShellCommandGetExitCode();
2647 UnicodeSPrint(LeString
, sizeof(LeString
), L
"0x%Lx", CalleeExitStatus
);
2648 DEBUG_CODE(InternalEfiShellSetEnv(L
"debuglasterror", LeString
, TRUE
););
2649 InternalEfiShellSetEnv(L
"lasterror", LeString
, TRUE
);
2651 ShellCommandRegisterExit(FALSE
, 0);
2652 Status
= EFI_SUCCESS
;
2655 if (ShellGetExecutionBreakFlag()) {
2658 if (EFI_ERROR(Status
)) {
2659 CalleeExitStatus
= (SHELL_STATUS
) Status
;
2662 if (ShellCommandGetExit()) {
2663 CalleeExitStatus
= (SHELL_STATUS
) ShellCommandGetExitCode();
2668 // If that commend did not update the CurrentCommand then we need to advance it...
2670 if (LastCommand
== NewScriptFile
->CurrentCommand
) {
2671 NewScriptFile
->CurrentCommand
= (SCRIPT_COMMAND_LIST
*)GetNextNode(&NewScriptFile
->CommandList
, &NewScriptFile
->CurrentCommand
->Link
);
2672 if (!IsNull(&NewScriptFile
->CommandList
, &NewScriptFile
->CurrentCommand
->Link
)) {
2673 NewScriptFile
->CurrentCommand
->Reset
= TRUE
;
2677 NewScriptFile
->CurrentCommand
= (SCRIPT_COMMAND_LIST
*)GetNextNode(&NewScriptFile
->CommandList
, &NewScriptFile
->CurrentCommand
->Link
);
2678 if (!IsNull(&NewScriptFile
->CommandList
, &NewScriptFile
->CurrentCommand
->Link
)) {
2679 NewScriptFile
->CurrentCommand
->Reset
= TRUE
;
2685 FreePool(CommandLine
);
2686 FreePool(CommandLine2
);
2687 ShellCommandSetNewScript (NULL
);
2690 // Only if this was the last script reset the state.
2692 if (ShellCommandGetCurrentScriptFile()==NULL
) {
2693 ShellCommandSetEchoState(PreScriptEchoState
);
2696 if (ExitStatus
!= NULL
) {
2697 *ExitStatus
= CalleeExitStatus
;
2700 return (EFI_SUCCESS
);
2704 Function to process a NSH script file.
2706 @param[in] ScriptPath Pointer to the script file name (including file system path).
2707 @param[in] Handle the handle of the script file already opened.
2708 @param[in] CmdLine the command line to run.
2709 @param[in] ParamProtocol the shell parameters protocol pointer
2711 @param[out] ExitStatus The exit code of the script. Ignored if NULL.
2713 @retval EFI_SUCCESS the script completed sucessfully
2718 IN CONST CHAR16
*ScriptPath
,
2719 IN SHELL_FILE_HANDLE Handle OPTIONAL
,
2720 IN CONST CHAR16
*CmdLine
,
2721 IN EFI_SHELL_PARAMETERS_PROTOCOL
*ParamProtocol
,
2722 OUT SHELL_STATUS
*ExitStatus
2726 SHELL_FILE_HANDLE FileHandle
;
2730 if (ShellIsFile(ScriptPath
) != EFI_SUCCESS
) {
2731 return (EFI_INVALID_PARAMETER
);
2735 // get the argc and argv updated for scripts
2737 Status
= UpdateArgcArgv(ParamProtocol
, CmdLine
, &Argv
, &Argc
);
2738 if (!EFI_ERROR(Status
)) {
2740 if (Handle
== NULL
) {
2744 Status
= ShellOpenFileByName(ScriptPath
, &FileHandle
, EFI_FILE_MODE_READ
, 0);
2745 if (!EFI_ERROR(Status
)) {
2749 Status
= RunScriptFileHandle(FileHandle
, ScriptPath
, ExitStatus
);
2752 // now close the file
2754 ShellCloseFile(&FileHandle
);
2757 Status
= RunScriptFileHandle(Handle
, ScriptPath
, ExitStatus
);
2762 // This is guarenteed to be called after UpdateArgcArgv no matter what else happened.
2763 // This is safe even if the update API failed. In this case, it may be a no-op.
2765 RestoreArgcArgv(ParamProtocol
, &Argv
, &Argc
);