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
;
248 if (PcdGet8(PcdShellSupportLevel
) > 3) {
249 return (EFI_UNSUPPORTED
);
255 Status
= gST
->ConOut
->ClearScreen(gST
->ConOut
);
256 if (EFI_ERROR(Status
)) {
261 // Populate the global structure from PCDs
263 ShellInfoObject
.ImageDevPath
= NULL
;
264 ShellInfoObject
.FileDevPath
= NULL
;
265 ShellInfoObject
.PageBreakEnabled
= PcdGetBool(PcdShellPageBreakDefault
);
266 ShellInfoObject
.ViewingSettings
.InsertMode
= PcdGetBool(PcdShellInsertModeDefault
);
267 ShellInfoObject
.LogScreenCount
= PcdGet8 (PcdShellScreenLogCount
);
270 // verify we dont allow for spec violation
272 ASSERT(ShellInfoObject
.LogScreenCount
>= 3);
275 // Initialize the LIST ENTRY objects...
277 InitializeListHead(&ShellInfoObject
.BufferToFreeList
.Link
);
278 InitializeListHead(&ShellInfoObject
.ViewingSettings
.CommandHistory
.Link
);
279 InitializeListHead(&ShellInfoObject
.SplitList
.Link
);
282 // Check PCDs for optional features that are not implemented yet.
284 if ( PcdGetBool(PcdShellSupportOldProtocols
)
285 || !FeaturePcdGet(PcdShellRequireHiiPlatform
)
286 || FeaturePcdGet(PcdShellSupportFrameworkHii
)
288 return (EFI_UNSUPPORTED
);
292 // turn off the watchdog timer
294 gBS
->SetWatchdogTimer (0, 0, 0, NULL
);
297 // install our console logger. This will keep a log of the output for back-browsing
299 Status
= ConsoleLoggerInstall(ShellInfoObject
.LogScreenCount
, &ShellInfoObject
.ConsoleInfo
);
300 if (!EFI_ERROR(Status
)) {
302 // Enable the cursor to be visible
304 gST
->ConOut
->EnableCursor (gST
->ConOut
, TRUE
);
307 // If supporting EFI 1.1 we need to install HII protocol
308 // only do this if PcdShellRequireHiiPlatform == FALSE
310 // remove EFI_UNSUPPORTED check above when complete.
311 ///@todo add support for Framework HII
314 // install our (solitary) HII package
316 ShellInfoObject
.HiiHandle
= HiiAddPackages (&gEfiCallerIdGuid
, gImageHandle
, ShellStrings
, NULL
);
317 if (ShellInfoObject
.HiiHandle
== NULL
) {
318 if (PcdGetBool(PcdShellSupportFrameworkHii
)) {
319 ///@todo Add our package into Framework HII
321 if (ShellInfoObject
.HiiHandle
== NULL
) {
322 return (EFI_NOT_STARTED
);
327 // create and install the EfiShellParametersProtocol
329 Status
= CreatePopulateInstallShellParametersProtocol(&ShellInfoObject
.NewShellParametersProtocol
, &ShellInfoObject
.RootShellInstance
);
330 ASSERT_EFI_ERROR(Status
);
331 ASSERT(ShellInfoObject
.NewShellParametersProtocol
!= NULL
);
334 // create and install the EfiShellProtocol
336 Status
= CreatePopulateInstallShellProtocol(&ShellInfoObject
.NewEfiShellProtocol
);
337 ASSERT_EFI_ERROR(Status
);
338 ASSERT(ShellInfoObject
.NewEfiShellProtocol
!= NULL
);
341 // Now initialize the shell library (it requires Shell Parameters protocol)
343 Status
= ShellInitialize();
344 ASSERT_EFI_ERROR(Status
);
346 Status
= CommandInit();
347 ASSERT_EFI_ERROR(Status
);
350 // Check the command line
352 Status
= ProcessCommandLine();
355 // If shell support level is >= 1 create the mappings and paths
357 if (PcdGet8(PcdShellSupportLevel
) >= 1) {
358 Status
= ShellCommandCreateInitialMappingsAndPaths();
362 // save the device path for the loaded image and the device path for the filepath (under loaded image)
363 // These are where to look for the startup.nsh file
365 Status
= GetDevicePathsForImageAndFile(&ShellInfoObject
.ImageDevPath
, &ShellInfoObject
.FileDevPath
);
366 ASSERT_EFI_ERROR(Status
);
369 // Display the version
371 if (!ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoVersion
) {
374 gST
->ConOut
->Mode
->CursorRow
,
376 STRING_TOKEN (STR_VER_OUTPUT_MAIN_SHELL
),
377 ShellInfoObject
.HiiHandle
,
378 SupportLevel
[PcdGet8(PcdShellSupportLevel
)],
379 gEfiShellProtocol
->MajorVersion
,
380 gEfiShellProtocol
->MinorVersion
387 STRING_TOKEN (STR_VER_OUTPUT_MAIN_SUPPLIER
),
388 ShellInfoObject
.HiiHandle
,
389 (CHAR16
*) PcdGetPtr (PcdShellSupplier
)
396 STRING_TOKEN (STR_VER_OUTPUT_MAIN_UEFI
),
397 ShellInfoObject
.HiiHandle
,
398 (gST
->Hdr
.Revision
&0xffff0000)>>16,
399 (gST
->Hdr
.Revision
&0x0000ffff),
401 gST
->FirmwareRevision
406 // Display the mapping
408 if (PcdGet8(PcdShellSupportLevel
) >= 2 && !ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoMap
) {
409 Status
= RunCommand(L
"map");
410 ASSERT_EFI_ERROR(Status
);
414 // init all the built in alias'
416 Status
= SetBuiltInAlias();
417 ASSERT_EFI_ERROR(Status
);
420 // Initialize environment variables
422 if (ShellCommandGetProfileList() != NULL
) {
423 Status
= InternalEfiShellSetEnv(L
"profiles", ShellCommandGetProfileList(), TRUE
);
424 ASSERT_EFI_ERROR(Status
);
428 TempString
= AllocateZeroPool(Size
);
430 UnicodeSPrint(TempString
, Size
, L
"%d", PcdGet8(PcdShellSupportLevel
));
431 Status
= InternalEfiShellSetEnv(L
"uefishellsupport", TempString
, TRUE
);
432 ASSERT_EFI_ERROR(Status
);
434 UnicodeSPrint(TempString
, Size
, L
"%d.%d", ShellInfoObject
.NewEfiShellProtocol
->MajorVersion
, ShellInfoObject
.NewEfiShellProtocol
->MinorVersion
);
435 Status
= InternalEfiShellSetEnv(L
"uefishellversion", TempString
, TRUE
);
436 ASSERT_EFI_ERROR(Status
);
438 UnicodeSPrint(TempString
, Size
, L
"%d.%d", (gST
->Hdr
.Revision
& 0xFFFF0000) >> 16, gST
->Hdr
.Revision
& 0x0000FFFF);
439 Status
= InternalEfiShellSetEnv(L
"uefiversion", TempString
, TRUE
);
440 ASSERT_EFI_ERROR(Status
);
442 FreePool(TempString
);
444 if (!EFI_ERROR(Status
)) {
445 if (!ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoInterrupt
) {
447 // Set up the event for CTRL-C monitoring...
449 Status
= InernalEfiShellStartMonitor();
452 if (!EFI_ERROR(Status
) && !ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoConsoleIn
) {
454 // Set up the event for CTRL-S monitoring...
456 Status
= InternalEfiShellStartCtrlSMonitor();
459 if (!EFI_ERROR(Status
) && ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoConsoleIn
) {
461 // close off the gST->ConIn
463 OldConIn
= gST
->ConIn
;
464 ConInHandle
= gST
->ConsoleInHandle
;
465 gST
->ConIn
= CreateSimpleTextInOnFile((SHELL_FILE_HANDLE
)&FileInterfaceNulFile
, &gST
->ConsoleInHandle
);
471 if (!EFI_ERROR(Status
) && PcdGet8(PcdShellSupportLevel
) >= 1) {
473 // process the startup script or launch the called app.
475 Status
= DoStartupScript(ShellInfoObject
.ImageDevPath
, ShellInfoObject
.FileDevPath
);
478 if (!ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.Exit
&& !ShellCommandGetExit() && (PcdGet8(PcdShellSupportLevel
) >= 3 || PcdGetBool(PcdShellForceConsole
)) && !EFI_ERROR(Status
) && !ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoConsoleIn
) {
480 // begin the UI waiting loop
484 // clean out all the memory allocated for CONST <something> * return values
485 // between each shell prompt presentation
487 if (!IsListEmpty(&ShellInfoObject
.BufferToFreeList
.Link
)){
488 FreeBufferList(&ShellInfoObject
.BufferToFreeList
);
492 // Reset page break back to default.
494 ShellInfoObject
.PageBreakEnabled
= PcdGetBool(PcdShellPageBreakDefault
);
495 ShellInfoObject
.ConsoleInfo
->Enabled
= TRUE
;
496 ShellInfoObject
.ConsoleInfo
->RowCounter
= 0;
499 // Reset the CTRL-C event (yes we ignore the return values)
501 Status
= gBS
->CheckEvent (ShellInfoObject
.NewEfiShellProtocol
->ExecutionBreak
);
506 Status
= DoShellPrompt();
507 } while (!ShellCommandGetExit());
509 if (OldConIn
!= NULL
&& ConInHandle
!= NULL
) {
510 CloseSimpleTextInOnFile (gST
->ConIn
);
511 gST
->ConIn
= OldConIn
;
512 gST
->ConsoleInHandle
= ConInHandle
;
518 // uninstall protocols / free memory / etc...
520 if (ShellInfoObject
.UserBreakTimer
!= NULL
) {
521 gBS
->CloseEvent(ShellInfoObject
.UserBreakTimer
);
522 DEBUG_CODE(ShellInfoObject
.UserBreakTimer
= NULL
;);
524 if (ShellInfoObject
.ImageDevPath
!= NULL
) {
525 FreePool(ShellInfoObject
.ImageDevPath
);
526 DEBUG_CODE(ShellInfoObject
.ImageDevPath
= NULL
;);
528 if (ShellInfoObject
.FileDevPath
!= NULL
) {
529 FreePool(ShellInfoObject
.FileDevPath
);
530 DEBUG_CODE(ShellInfoObject
.FileDevPath
= NULL
;);
532 if (ShellInfoObject
.NewShellParametersProtocol
!= NULL
) {
533 CleanUpShellParametersProtocol(ShellInfoObject
.NewShellParametersProtocol
);
534 DEBUG_CODE(ShellInfoObject
.NewShellParametersProtocol
= NULL
;);
536 if (ShellInfoObject
.NewEfiShellProtocol
!= NULL
){
537 if (ShellInfoObject
.NewEfiShellProtocol
->IsRootShell()){
538 InternalEfiShellSetEnv(L
"cwd", NULL
, TRUE
);
540 CleanUpShellProtocol(ShellInfoObject
.NewEfiShellProtocol
);
541 DEBUG_CODE(ShellInfoObject
.NewEfiShellProtocol
= NULL
;);
544 if (!IsListEmpty(&ShellInfoObject
.BufferToFreeList
.Link
)){
545 FreeBufferList(&ShellInfoObject
.BufferToFreeList
);
548 if (!IsListEmpty(&ShellInfoObject
.SplitList
.Link
)){
549 ASSERT(FALSE
); ///@todo finish this de-allocation.
552 if (ShellInfoObject
.ShellInitSettings
.FileName
!= NULL
) {
553 FreePool(ShellInfoObject
.ShellInitSettings
.FileName
);
554 DEBUG_CODE(ShellInfoObject
.ShellInitSettings
.FileName
= NULL
;);
557 if (ShellInfoObject
.ShellInitSettings
.FileOptions
!= NULL
) {
558 FreePool(ShellInfoObject
.ShellInitSettings
.FileOptions
);
559 DEBUG_CODE(ShellInfoObject
.ShellInitSettings
.FileOptions
= NULL
;);
562 if (ShellInfoObject
.HiiHandle
!= NULL
) {
563 HiiRemovePackages(ShellInfoObject
.HiiHandle
);
564 DEBUG_CODE(ShellInfoObject
.HiiHandle
= NULL
;);
567 if (!IsListEmpty(&ShellInfoObject
.ViewingSettings
.CommandHistory
.Link
)){
568 FreeBufferList(&ShellInfoObject
.ViewingSettings
.CommandHistory
);
571 ASSERT(ShellInfoObject
.ConsoleInfo
!= NULL
);
572 if (ShellInfoObject
.ConsoleInfo
!= NULL
) {
573 ConsoleLoggerUninstall(ShellInfoObject
.ConsoleInfo
);
574 FreePool(ShellInfoObject
.ConsoleInfo
);
575 DEBUG_CODE(ShellInfoObject
.ConsoleInfo
= NULL
;);
578 if (ShellCommandGetExit()) {
579 return ((EFI_STATUS
)ShellCommandGetExitCode());
585 Sets all the alias' that were registered with the ShellCommandLib library.
587 @retval EFI_SUCCESS all init commands were run sucessfully.
595 CONST ALIAS_LIST
*List
;
599 // Get all the commands we want to run
601 List
= ShellCommandGetInitAliasList();
604 // for each command in the List
606 for ( Node
= (ALIAS_LIST
*)GetFirstNode(&List
->Link
)
607 ; !IsNull (&List
->Link
, &Node
->Link
)
608 ; Node
= (ALIAS_LIST
*)GetNextNode(&List
->Link
, &Node
->Link
)
611 // install the alias'
613 Status
= InternalSetAlias(Node
->CommandString
, Node
->Alias
, TRUE
);
614 ASSERT_EFI_ERROR(Status
);
616 return (EFI_SUCCESS
);
620 Internal function to determine if 2 command names are really the same.
622 @param[in] Command1 The pointer to the first command name.
623 @param[in] Command2 The pointer to the second command name.
625 @retval TRUE The 2 command names are the same.
626 @retval FALSE The 2 command names are not the same.
631 IN CONST CHAR16
*Command1
,
632 IN CONST CHAR16
*Command2
635 if (StringNoCaseCompare(&Command1
, &Command2
) == 0) {
642 Internal function to determine if a command is a script only command.
644 @param[in] CommandName The pointer to the command name.
646 @retval TRUE The command is a script only command.
647 @retval FALSE The command is not a script only command.
652 IN CONST CHAR16
*CommandName
655 if (IsCommand(CommandName
, L
"for")
656 ||IsCommand(CommandName
, L
"endfor")
657 ||IsCommand(CommandName
, L
"if")
658 ||IsCommand(CommandName
, L
"else")
659 ||IsCommand(CommandName
, L
"endif")
660 ||IsCommand(CommandName
, L
"goto")) {
667 This function will populate the 2 device path protocol parameters based on the
668 global gImageHandle. The DevPath will point to the device path for the handle that has
669 loaded image protocol installed on it. The FilePath will point to the device path
670 for the file that was loaded.
672 @param[in, out] DevPath On a sucessful return the device path to the loaded image.
673 @param[in, out] FilePath On a sucessful return the device path to the file.
675 @retval EFI_SUCCESS The 2 device paths were sucessfully returned.
676 @retval other A error from gBS->HandleProtocol.
682 GetDevicePathsForImageAndFile (
683 IN OUT EFI_DEVICE_PATH_PROTOCOL
**DevPath
,
684 IN OUT EFI_DEVICE_PATH_PROTOCOL
**FilePath
688 EFI_LOADED_IMAGE_PROTOCOL
*LoadedImage
;
689 EFI_DEVICE_PATH_PROTOCOL
*ImageDevicePath
;
691 ASSERT(DevPath
!= NULL
);
692 ASSERT(FilePath
!= NULL
);
694 Status
= gBS
->OpenProtocol (
696 &gEfiLoadedImageProtocolGuid
,
697 (VOID
**)&LoadedImage
,
700 EFI_OPEN_PROTOCOL_GET_PROTOCOL
702 if (!EFI_ERROR (Status
)) {
703 Status
= gBS
->OpenProtocol (
704 LoadedImage
->DeviceHandle
,
705 &gEfiDevicePathProtocolGuid
,
706 (VOID
**)&ImageDevicePath
,
709 EFI_OPEN_PROTOCOL_GET_PROTOCOL
711 if (!EFI_ERROR (Status
)) {
712 *DevPath
= DuplicateDevicePath (ImageDevicePath
);
713 *FilePath
= DuplicateDevicePath (LoadedImage
->FilePath
);
715 LoadedImage
->DeviceHandle
,
716 &gEfiDevicePathProtocolGuid
,
722 &gEfiLoadedImageProtocolGuid
,
729 STATIC CONST SHELL_PARAM_ITEM mShellParamList
[] = {
730 {L
"-nostartup", TypeFlag
},
731 {L
"-startup", TypeFlag
},
732 {L
"-noconsoleout", TypeFlag
},
733 {L
"-noconsolein", TypeFlag
},
734 {L
"-nointerrupt", TypeFlag
},
735 {L
"-nomap", TypeFlag
},
736 {L
"-noversion", TypeFlag
},
737 {L
"-startup", TypeFlag
},
738 {L
"-delay", TypeValue
},
739 {L
"-_exit", TypeFlag
},
744 Process all Uefi Shell 2.0 command line options.
746 see Uefi Shell 2.0 section 3.2 for full details.
748 the command line must resemble the following:
750 shell.efi [ShellOpt-options] [options] [file-name [file-name-options]]
752 ShellOpt-options Options which control the initialization behavior of the shell.
753 These options are read from the EFI global variable "ShellOpt"
754 and are processed before options or file-name.
756 options Options which control the initialization behavior of the shell.
758 file-name The name of a UEFI shell application or script to be executed
759 after initialization is complete. By default, if file-name is
760 specified, then -nostartup is implied. Scripts are not supported
763 file-name-options The command-line options that are passed to file-name when it
766 This will initialize the ShellInfoObject.ShellInitSettings global variable.
768 @retval EFI_SUCCESS The variable is initialized.
779 CONST CHAR16
*TempConst
;
782 CHAR16
*ProblemParam
;
788 Status
= ShellCommandLineParse (mShellParamList
, &Package
, NULL
, FALSE
);
792 TempConst
= ShellCommandLineGetRawValue(Package
, Count
++);
793 if (TempConst
!= NULL
&& StrLen(TempConst
)) {
794 ShellInfoObject
.ShellInitSettings
.FileName
= AllocateZeroPool(StrSize(TempConst
));
795 if (ShellInfoObject
.ShellInitSettings
.FileName
== NULL
) {
796 return (EFI_OUT_OF_RESOURCES
);
798 StrCpy(ShellInfoObject
.ShellInitSettings
.FileName
, TempConst
);
799 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoStartup
= 1;
800 for (LoopVar
= 0 ; LoopVar
< gEfiShellParametersProtocol
->Argc
; LoopVar
++) {
801 if (StrCmp(gEfiShellParametersProtocol
->Argv
[LoopVar
], ShellInfoObject
.ShellInitSettings
.FileName
)==0) {
804 // We found the file... add the rest of the params...
806 for ( ; LoopVar
< gEfiShellParametersProtocol
->Argc
; LoopVar
++) {
807 ASSERT((ShellInfoObject
.ShellInitSettings
.FileOptions
== NULL
&& Size
== 0) || (ShellInfoObject
.ShellInitSettings
.FileOptions
!= NULL
));
808 StrnCatGrow(&ShellInfoObject
.ShellInitSettings
.FileOptions
,
812 if (ShellInfoObject
.ShellInitSettings
.FileOptions
== NULL
) {
813 SHELL_FREE_NON_NULL(ShellInfoObject
.ShellInitSettings
.FileName
);
814 return (EFI_OUT_OF_RESOURCES
);
816 StrnCatGrow(&ShellInfoObject
.ShellInitSettings
.FileOptions
,
818 gEfiShellParametersProtocol
->Argv
[LoopVar
],
820 if (ShellInfoObject
.ShellInitSettings
.FileOptions
== NULL
) {
821 SHELL_FREE_NON_NULL(ShellInfoObject
.ShellInitSettings
.FileName
);
822 return (EFI_OUT_OF_RESOURCES
);
828 ShellCommandLineFreeVarList(Package
);
830 Status
= ShellCommandLineParse (mShellParamList
, &Package
, &ProblemParam
, FALSE
);
831 if (EFI_ERROR(Status
)) {
832 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_PROBLEM
), ShellInfoObject
.HiiHandle
, ProblemParam
);
833 FreePool(ProblemParam
);
834 ShellCommandLineFreeVarList(Package
);
835 return (EFI_INVALID_PARAMETER
);
839 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.Startup
= ShellCommandLineGetFlag(Package
, L
"-startup");
840 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoStartup
= ShellCommandLineGetFlag(Package
, L
"-nostartup");
841 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoConsoleOut
= ShellCommandLineGetFlag(Package
, L
"-noconsoleout");
842 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoConsoleIn
= ShellCommandLineGetFlag(Package
, L
"-noconsolein");
843 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoInterrupt
= ShellCommandLineGetFlag(Package
, L
"-nointerrupt");
844 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoMap
= ShellCommandLineGetFlag(Package
, L
"-nomap");
845 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoVersion
= ShellCommandLineGetFlag(Package
, L
"-noversion");
846 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.Delay
= ShellCommandLineGetFlag(Package
, L
"-delay");
847 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.Exit
= ShellCommandLineGetFlag(Package
, L
"-_exit");
849 ShellInfoObject
.ShellInitSettings
.Delay
= 5;
851 if (ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoInterrupt
) {
852 ShellInfoObject
.ShellInitSettings
.Delay
= 0;
853 } else if (ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.Delay
) {
854 TempConst
= ShellCommandLineGetValue(Package
, L
"-delay");
855 if (TempConst
!= NULL
&& *TempConst
== L
':') {
858 if (TempConst
!= NULL
&& !EFI_ERROR(ShellConvertStringToUint64(TempConst
, &Intermediate
, FALSE
, FALSE
))) {
859 ShellInfoObject
.ShellInitSettings
.Delay
= (UINTN
)Intermediate
;
862 ShellCommandLineFreeVarList(Package
);
868 Handles all interaction with the default startup script.
870 this will check that the correct command line parameters were passed, handle the delay, and then start running the script.
872 @param ImagePath the path to the image for shell. first place to look for the startup script
873 @param FilePath the path to the file for shell. second place to look for the startup script.
875 @retval EFI_SUCCESS the variable is initialized.
880 EFI_DEVICE_PATH_PROTOCOL
*ImagePath
,
881 EFI_DEVICE_PATH_PROTOCOL
*FilePath
887 SHELL_FILE_HANDLE FileHandle
;
888 EFI_DEVICE_PATH_PROTOCOL
*NewPath
;
889 EFI_DEVICE_PATH_PROTOCOL
*NamePath
;
890 CHAR16
*FileStringPath
;
893 CONST CHAR16
*MapName
;
895 Key
.UnicodeChar
= CHAR_NULL
;
899 if (!ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.Startup
&& ShellInfoObject
.ShellInitSettings
.FileName
!= NULL
) {
901 // launch something else instead
903 NewSize
= StrSize(ShellInfoObject
.ShellInitSettings
.FileName
);
904 if (ShellInfoObject
.ShellInitSettings
.FileOptions
!= NULL
) {
905 NewSize
+= StrSize(ShellInfoObject
.ShellInitSettings
.FileOptions
) + sizeof(CHAR16
);
907 FileStringPath
= AllocateZeroPool(NewSize
);
908 if (FileStringPath
== NULL
) {
909 return (EFI_OUT_OF_RESOURCES
);
911 StrCpy(FileStringPath
, ShellInfoObject
.ShellInitSettings
.FileName
);
912 if (ShellInfoObject
.ShellInitSettings
.FileOptions
!= NULL
) {
913 StrCat(FileStringPath
, L
" ");
914 StrCat(FileStringPath
, ShellInfoObject
.ShellInitSettings
.FileOptions
);
916 Status
= RunCommand(FileStringPath
);
917 FreePool(FileStringPath
);
923 // for shell level 0 we do no scripts
924 // Without the Startup bit overriding we allow for nostartup to prevent scripts
926 if ( (PcdGet8(PcdShellSupportLevel
) < 1)
927 || (ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoStartup
&& !ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.Startup
)
929 return (EFI_SUCCESS
);
932 gST
->ConOut
->EnableCursor(gST
->ConOut
, FALSE
);
934 // print out our warning and see if they press a key
936 for ( Status
= EFI_UNSUPPORTED
, Delay
= ShellInfoObject
.ShellInitSettings
.Delay
937 ; Delay
!= 0 && EFI_ERROR(Status
)
940 ShellPrintHiiEx(0, gST
->ConOut
->Mode
->CursorRow
, NULL
, STRING_TOKEN (STR_SHELL_STARTUP_QUESTION
), ShellInfoObject
.HiiHandle
, Delay
);
941 gBS
->Stall (1000000);
942 if (!ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoConsoleIn
) {
943 Status
= gST
->ConIn
->ReadKeyStroke (gST
->ConIn
, &Key
);
946 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_CRLF
), ShellInfoObject
.HiiHandle
);
947 gST
->ConOut
->EnableCursor(gST
->ConOut
, TRUE
);
952 if (Status
== EFI_SUCCESS
&& Key
.UnicodeChar
== 0 && Key
.ScanCode
== SCAN_ESC
) {
953 return (EFI_SUCCESS
);
957 // Try the first location (must be file system)
959 MapName
= ShellInfoObject
.NewEfiShellProtocol
->GetMapFromDevicePath(&ImagePath
);
960 if (MapName
!= NULL
) {
961 FileStringPath
= NULL
;
963 FileStringPath
= StrnCatGrow(&FileStringPath
, &NewSize
, MapName
, 0);
964 if (FileStringPath
== NULL
) {
965 Status
= EFI_OUT_OF_RESOURCES
;
967 TempSpot
= StrStr(FileStringPath
, L
";");
968 if (TempSpot
!= NULL
) {
969 *TempSpot
= CHAR_NULL
;
971 FileStringPath
= StrnCatGrow(&FileStringPath
, &NewSize
, ((FILEPATH_DEVICE_PATH
*)FilePath
)->PathName
, 0);
972 PathRemoveLastItem(FileStringPath
);
973 FileStringPath
= StrnCatGrow(&FileStringPath
, &NewSize
, mStartupScript
, 0);
974 Status
= ShellInfoObject
.NewEfiShellProtocol
->OpenFileByName(FileStringPath
, &FileHandle
, EFI_FILE_MODE_READ
);
975 FreePool(FileStringPath
);
978 if (EFI_ERROR(Status
)) {
979 NamePath
= FileDevicePath (NULL
, mStartupScript
);
980 NewPath
= AppendDevicePathNode (ImagePath
, NamePath
);
986 Status
= InternalOpenFileDevicePath(NewPath
, &FileHandle
, EFI_FILE_MODE_READ
, 0);
990 // If we got a file, run it
992 if (!EFI_ERROR(Status
) && FileHandle
!= NULL
) {
993 Status
= RunScriptFile (mStartupScript
, FileHandle
, L
"", ShellInfoObject
.NewShellParametersProtocol
);
994 ShellInfoObject
.NewEfiShellProtocol
->CloseFile(FileHandle
);
996 FileStringPath
= ShellFindFilePath(mStartupScript
);
997 if (FileStringPath
== NULL
) {
999 // we return success since we dont need to have a startup script
1001 Status
= EFI_SUCCESS
;
1002 ASSERT(FileHandle
== NULL
);
1004 Status
= RunScriptFile(FileStringPath
, NULL
, L
"", ShellInfoObject
.NewShellParametersProtocol
);
1005 FreePool(FileStringPath
);
1014 Function to perform the shell prompt looping. It will do a single prompt,
1015 dispatch the result, and then return. It is expected that the caller will
1016 call this function in a loop many times.
1019 @retval RETURN_ABORTED
1030 CONST CHAR16
*CurDir
;
1037 // Get screen setting to decide size of the command line buffer
1039 gST
->ConOut
->QueryMode (gST
->ConOut
, gST
->ConOut
->Mode
->Mode
, &Column
, &Row
);
1040 BufferSize
= Column
* Row
* sizeof (CHAR16
);
1041 CmdLine
= AllocateZeroPool (BufferSize
);
1042 if (CmdLine
== NULL
) {
1043 return EFI_OUT_OF_RESOURCES
;
1046 CurDir
= ShellInfoObject
.NewEfiShellProtocol
->GetEnv(L
"cwd");
1051 gST
->ConOut
->SetCursorPosition (gST
->ConOut
, 0, gST
->ConOut
->Mode
->CursorRow
);
1053 if (CurDir
!= NULL
&& StrLen(CurDir
) > 1) {
1054 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_CURDIR
), ShellInfoObject
.HiiHandle
, CurDir
);
1056 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_SHELL
), ShellInfoObject
.HiiHandle
);
1060 // Read a line from the console
1062 Status
= ShellInfoObject
.NewEfiShellProtocol
->ReadFile(ShellInfoObject
.NewShellParametersProtocol
->StdIn
, &BufferSize
, CmdLine
);
1065 // Null terminate the string and parse it
1067 if (!EFI_ERROR (Status
)) {
1068 CmdLine
[BufferSize
/ sizeof (CHAR16
)] = CHAR_NULL
;
1069 Status
= RunCommand(CmdLine
);
1073 // Done with this command
1080 Add a buffer to the Buffer To Free List for safely returning buffers to other
1081 places without risking letting them modify internal shell information.
1083 @param Buffer Something to pass to FreePool when the shell is exiting.
1087 AddBufferToFreeList(
1091 BUFFER_LIST
*BufferListEntry
;
1093 if (Buffer
== NULL
) {
1097 BufferListEntry
= AllocateZeroPool(sizeof(BUFFER_LIST
));
1098 ASSERT(BufferListEntry
!= NULL
);
1099 BufferListEntry
->Buffer
= Buffer
;
1100 InsertTailList(&ShellInfoObject
.BufferToFreeList
.Link
, &BufferListEntry
->Link
);
1105 Add a buffer to the Line History List
1107 @param Buffer The line buffer to add.
1111 AddLineToCommandHistory(
1112 IN CONST CHAR16
*Buffer
1117 Node
= AllocateZeroPool(sizeof(BUFFER_LIST
));
1118 ASSERT(Node
!= NULL
);
1119 Node
->Buffer
= AllocateZeroPool(StrSize(Buffer
));
1120 ASSERT(Node
->Buffer
!= NULL
);
1121 StrCpy(Node
->Buffer
, Buffer
);
1123 InsertTailList(&ShellInfoObject
.ViewingSettings
.CommandHistory
.Link
, &Node
->Link
);
1127 Checks if a string is an alias for another command. If yes, then it replaces the alias name
1128 with the correct command name.
1130 @param[in, out] CommandString Upon entry the potential alias. Upon return the
1131 command name if it was an alias. If it was not
1132 an alias it will be unchanged. This function may
1133 change the buffer to fit the command name.
1135 @retval EFI_SUCCESS The name was changed.
1136 @retval EFI_SUCCESS The name was not an alias.
1137 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
1142 IN OUT CHAR16
**CommandString
1145 CONST CHAR16
*NewString
;
1147 NewString
= ShellInfoObject
.NewEfiShellProtocol
->GetAlias(*CommandString
, NULL
);
1148 if (NewString
== NULL
) {
1149 return (EFI_SUCCESS
);
1151 FreePool(*CommandString
);
1152 *CommandString
= AllocateZeroPool(StrSize(NewString
));
1153 if (*CommandString
== NULL
) {
1154 return (EFI_OUT_OF_RESOURCES
);
1156 StrCpy(*CommandString
, NewString
);
1157 return (EFI_SUCCESS
);
1161 Function allocates a new command line and replaces all instances of environment
1162 variable names that are correctly preset to their values.
1164 If the return value is not NULL the memory must be caller freed.
1166 @param[in] OriginalCommandLine The original command line
1168 @retval NULL An error ocurred.
1169 @return The new command line with no environment variables present.
1173 ShellConvertVariables (
1174 IN CONST CHAR16
*OriginalCommandLine
1177 CONST CHAR16
*MasterEnvList
;
1179 CHAR16
*NewCommandLine1
;
1180 CHAR16
*NewCommandLine2
;
1185 SCRIPT_FILE
*CurrentScriptFile
;
1186 ALIAS_LIST
*AliasListNode
;
1188 ASSERT(OriginalCommandLine
!= NULL
);
1191 NewSize
= StrSize(OriginalCommandLine
);
1192 CurrentScriptFile
= ShellCommandGetCurrentScriptFile();
1195 ///@todo update this to handle the %0 - %9 for scripting only (borrow from line 1256 area) ? ? ?
1198 // calculate the size required for the post-conversion string...
1200 if (CurrentScriptFile
!= NULL
) {
1201 for (AliasListNode
= (ALIAS_LIST
*)GetFirstNode(&CurrentScriptFile
->SubstList
)
1202 ; !IsNull(&CurrentScriptFile
->SubstList
, &AliasListNode
->Link
)
1203 ; AliasListNode
= (ALIAS_LIST
*)GetNextNode(&CurrentScriptFile
->SubstList
, &AliasListNode
->Link
)
1205 for (Temp
= StrStr(OriginalCommandLine
, AliasListNode
->Alias
)
1207 ; Temp
= StrStr(Temp
+1, AliasListNode
->Alias
)
1210 // we need a preceeding and if there is space no ^ preceeding (if no space ignore)
1212 if ((((Temp
-OriginalCommandLine
)>2) && *(Temp
-2) != L
'^') || ((Temp
-OriginalCommandLine
)<=2)) {
1213 NewSize
+= StrSize(AliasListNode
->CommandString
);
1219 for (MasterEnvList
= EfiShellGetEnv(NULL
)
1220 ; MasterEnvList
!= NULL
&& *MasterEnvList
!= CHAR_NULL
//&& *(MasterEnvList+1) != CHAR_NULL
1221 ; MasterEnvList
+= StrLen(MasterEnvList
) + 1
1223 if (StrSize(MasterEnvList
) > ItemSize
) {
1224 ItemSize
= StrSize(MasterEnvList
);
1226 for (Temp
= StrStr(OriginalCommandLine
, MasterEnvList
)
1228 ; Temp
= StrStr(Temp
+1, MasterEnvList
)
1231 // we need a preceeding and following % and if there is space no ^ preceeding (if no space ignore)
1233 if (*(Temp
-1) == L
'%' && *(Temp
+StrLen(MasterEnvList
)) == L
'%' &&
1234 ((((Temp
-OriginalCommandLine
)>2) && *(Temp
-2) != L
'^') || ((Temp
-OriginalCommandLine
)<=2))) {
1235 NewSize
+=StrSize(EfiShellGetEnv(MasterEnvList
));
1241 // now do the replacements...
1243 NewCommandLine1
= AllocateZeroPool(NewSize
);
1244 NewCommandLine2
= AllocateZeroPool(NewSize
);
1245 ItemTemp
= AllocateZeroPool(ItemSize
+(2*sizeof(CHAR16
)));
1246 if (NewCommandLine1
== NULL
|| NewCommandLine2
== NULL
|| ItemTemp
== NULL
) {
1247 SHELL_FREE_NON_NULL(NewCommandLine1
);
1248 SHELL_FREE_NON_NULL(NewCommandLine2
);
1249 SHELL_FREE_NON_NULL(ItemTemp
);
1252 StrCpy(NewCommandLine1
, OriginalCommandLine
);
1253 for (MasterEnvList
= EfiShellGetEnv(NULL
)
1254 ; MasterEnvList
!= NULL
&& *MasterEnvList
!= CHAR_NULL
//&& *(MasterEnvList+1) != CHAR_NULL
1255 ; MasterEnvList
+= StrLen(MasterEnvList
) + 1
1257 StrCpy(ItemTemp
, L
"%");
1258 StrCat(ItemTemp
, MasterEnvList
);
1259 StrCat(ItemTemp
, L
"%");
1260 ShellCopySearchAndReplace(NewCommandLine1
, NewCommandLine2
, NewSize
, ItemTemp
, EfiShellGetEnv(MasterEnvList
), TRUE
, FALSE
);
1261 StrCpy(NewCommandLine1
, NewCommandLine2
);
1263 if (CurrentScriptFile
!= NULL
) {
1264 for (AliasListNode
= (ALIAS_LIST
*)GetFirstNode(&CurrentScriptFile
->SubstList
)
1265 ; !IsNull(&CurrentScriptFile
->SubstList
, &AliasListNode
->Link
)
1266 ; AliasListNode
= (ALIAS_LIST
*)GetNextNode(&CurrentScriptFile
->SubstList
, &AliasListNode
->Link
)
1268 ShellCopySearchAndReplace(NewCommandLine1
, NewCommandLine2
, NewSize
, AliasListNode
->Alias
, AliasListNode
->CommandString
, TRUE
, FALSE
);
1269 StrCpy(NewCommandLine1
, NewCommandLine2
);
1273 // Remove non-existant environment variables in scripts only
1275 for (Temp
= NewCommandLine1
; Temp
!= NULL
; ) {
1276 Temp
= StrStr(Temp
, L
"%");
1280 while (*(Temp
- 1) == L
'^') {
1281 Temp
= StrStr(Temp
+ 1, L
"%");
1290 Temp2
= StrStr(Temp
+ 1, L
"%");
1291 if (Temp2
== NULL
) {
1294 while (*(Temp2
- 1) == L
'^') {
1295 Temp2
= StrStr(Temp2
+ 1, L
"%");
1296 if (Temp2
== NULL
) {
1300 if (Temp2
== NULL
) {
1305 CopyMem(Temp
, Temp2
, StrSize(Temp2
));
1311 // Now cleanup any straggler intentionally ignored "%" characters
1313 ShellCopySearchAndReplace(NewCommandLine1
, NewCommandLine2
, NewSize
, L
"^%", L
"%", TRUE
, FALSE
);
1314 StrCpy(NewCommandLine1
, NewCommandLine2
);
1316 FreePool(NewCommandLine2
);
1319 return (NewCommandLine1
);
1323 Internal function to run a command line with pipe usage.
1325 @param[in] CmdLine The pointer to the command line.
1326 @param[in] StdIn The pointer to the Standard input.
1327 @param[in] StdOut The pointer to the Standard output.
1329 @retval EFI_SUCCESS The split command is executed successfully.
1330 @retval other Some error occurs when executing the split command.
1335 IN CONST CHAR16
*CmdLine
,
1336 IN SHELL_FILE_HANDLE
*StdIn
,
1337 IN SHELL_FILE_HANDLE
*StdOut
1341 CHAR16
*NextCommandLine
;
1342 CHAR16
*OurCommandLine
;
1346 SHELL_FILE_HANDLE
*TempFileHandle
;
1349 ASSERT(StdOut
== NULL
);
1351 ASSERT(StrStr(CmdLine
, L
"|") != NULL
);
1353 Status
= EFI_SUCCESS
;
1354 NextCommandLine
= NULL
;
1355 OurCommandLine
= NULL
;
1359 NextCommandLine
= StrnCatGrow(&NextCommandLine
, &Size1
, StrStr(CmdLine
, L
"|")+1, 0);
1360 OurCommandLine
= StrnCatGrow(&OurCommandLine
, &Size2
, CmdLine
, StrStr(CmdLine
, L
"|") - CmdLine
);
1362 if (NextCommandLine
== NULL
|| OurCommandLine
== NULL
) {
1363 SHELL_FREE_NON_NULL(OurCommandLine
);
1364 SHELL_FREE_NON_NULL(NextCommandLine
);
1365 return (EFI_OUT_OF_RESOURCES
);
1366 } else if (StrStr(OurCommandLine
, L
"|") != NULL
|| Size1
== 0 || Size2
== 0) {
1367 SHELL_FREE_NON_NULL(OurCommandLine
);
1368 SHELL_FREE_NON_NULL(NextCommandLine
);
1369 return (EFI_INVALID_PARAMETER
);
1370 } else if (NextCommandLine
[0] != CHAR_NULL
&&
1371 NextCommandLine
[0] == L
'a' &&
1372 NextCommandLine
[1] == L
' '
1374 CopyMem(NextCommandLine
, NextCommandLine
+1, StrSize(NextCommandLine
) - sizeof(NextCommandLine
[0]));
1382 // make a SPLIT_LIST item and add to list
1384 Split
= AllocateZeroPool(sizeof(SPLIT_LIST
));
1385 ASSERT(Split
!= NULL
);
1386 Split
->SplitStdIn
= StdIn
;
1387 Split
->SplitStdOut
= ConvertEfiFileProtocolToShellHandle(CreateFileInterfaceMem(Unicode
), NULL
);
1388 ASSERT(Split
->SplitStdOut
!= NULL
);
1389 InsertHeadList(&ShellInfoObject
.SplitList
.Link
, &Split
->Link
);
1391 Status
= RunCommand(OurCommandLine
);
1394 // move the output from the first to the in to the second.
1396 TempFileHandle
= Split
->SplitStdOut
;
1397 if (Split
->SplitStdIn
== StdIn
) {
1398 Split
->SplitStdOut
= NULL
;
1400 Split
->SplitStdOut
= Split
->SplitStdIn
;
1402 Split
->SplitStdIn
= TempFileHandle
;
1403 ShellInfoObject
.NewEfiShellProtocol
->SetFilePosition(ConvertShellHandleToEfiFileProtocol(Split
->SplitStdIn
), 0);
1405 if (!EFI_ERROR(Status
)) {
1406 Status
= RunCommand(NextCommandLine
);
1410 // remove the top level from the ScriptList
1412 ASSERT((SPLIT_LIST
*)GetFirstNode(&ShellInfoObject
.SplitList
.Link
) == Split
);
1413 RemoveEntryList(&Split
->Link
);
1416 // Note that the original StdIn is now the StdOut...
1418 if (Split
->SplitStdOut
!= NULL
&& Split
->SplitStdOut
!= StdIn
) {
1419 ShellInfoObject
.NewEfiShellProtocol
->CloseFile(ConvertShellHandleToEfiFileProtocol(Split
->SplitStdOut
));
1421 if (Split
->SplitStdIn
!= NULL
) {
1422 ShellInfoObject
.NewEfiShellProtocol
->CloseFile(ConvertShellHandleToEfiFileProtocol(Split
->SplitStdIn
));
1426 FreePool(NextCommandLine
);
1427 FreePool(OurCommandLine
);
1433 Take the original command line, substitute any variables, free
1434 the original string, return the modified copy.
1436 @param[in] CmdLine pointer to the command line to update.
1438 @retval EFI_SUCCESS the function was successful.
1439 @retval EFI_OUT_OF_RESOURCES a memory allocation failed.
1443 ShellSubstituteVariables(
1448 NewCmdLine
= ShellConvertVariables(*CmdLine
);
1449 SHELL_FREE_NON_NULL(*CmdLine
);
1450 if (NewCmdLine
== NULL
) {
1451 return (EFI_OUT_OF_RESOURCES
);
1453 *CmdLine
= NewCmdLine
;
1454 return (EFI_SUCCESS
);
1458 Take the original command line, substitute any alias in the first group of space delimited characters, free
1459 the original string, return the modified copy.
1461 @param[in] CmdLine pointer to the command line to update.
1463 @retval EFI_SUCCESS the function was successful.
1464 @retval EFI_OUT_OF_RESOURCES a memory allocation failed.
1468 ShellSubstituteAliases(
1473 CHAR16
*CommandName
;
1475 UINTN PostAliasSize
;
1476 ASSERT(CmdLine
!= NULL
);
1477 ASSERT(*CmdLine
!= NULL
);
1481 if (StrStr((*CmdLine
), L
" ") == NULL
){
1482 StrnCatGrow(&CommandName
, NULL
, (*CmdLine
), 0);
1484 StrnCatGrow(&CommandName
, NULL
, (*CmdLine
), StrStr((*CmdLine
), L
" ") - (*CmdLine
));
1488 // This cannot happen 'inline' since the CmdLine can need extra space.
1491 if (!ShellCommandIsCommandOnList(CommandName
)) {
1493 // Convert via alias
1495 Status
= ShellConvertAlias(&CommandName
);
1496 if (EFI_ERROR(Status
)){
1500 NewCmdLine
= StrnCatGrow(&NewCmdLine
, &PostAliasSize
, CommandName
, 0);
1501 if (NewCmdLine
== NULL
) {
1502 SHELL_FREE_NON_NULL(CommandName
);
1503 SHELL_FREE_NON_NULL(*CmdLine
);
1504 return (EFI_OUT_OF_RESOURCES
);
1506 NewCmdLine
= StrnCatGrow(&NewCmdLine
, &PostAliasSize
, StrStr((*CmdLine
), L
" "), 0);
1507 if (NewCmdLine
== NULL
) {
1508 SHELL_FREE_NON_NULL(CommandName
);
1509 SHELL_FREE_NON_NULL(*CmdLine
);
1510 return (EFI_OUT_OF_RESOURCES
);
1513 NewCmdLine
= StrnCatGrow(&NewCmdLine
, NULL
, (*CmdLine
), 0);
1516 SHELL_FREE_NON_NULL(*CmdLine
);
1517 SHELL_FREE_NON_NULL(CommandName
);
1520 // re-assign the passed in double pointer to point to our newly allocated buffer
1522 *CmdLine
= NewCmdLine
;
1524 return (EFI_SUCCESS
);
1528 Takes the Argv[0] part of the command line and determine the meaning of it.
1530 @param[in] CmdName pointer to the command line to update.
1532 @retval Internal_Command The name is an internal command.
1533 @retval File_Sys_Change the name is a file system change.
1534 @retval Script_File_Name the name is a NSH script file.
1535 @retval Unknown_Invalid the name is unknown.
1536 @retval Efi_Application the name is an application (.EFI).
1538 SHELL_OPERATION_TYPES
1541 IN CONST CHAR16
*CmdName
1544 CHAR16
* FileWithPath
;
1545 CONST CHAR16
* TempLocation
;
1546 CONST CHAR16
* TempLocation2
;
1548 FileWithPath
= NULL
;
1550 // test for an internal command.
1552 if (ShellCommandIsCommandOnList(CmdName
)) {
1553 return (Internal_Command
);
1557 // Test for file system change request. anything ending with : and cant have spaces.
1559 if (CmdName
[(StrLen(CmdName
)-1)] == L
':') {
1560 if (StrStr(CmdName
, L
" ") != NULL
) {
1561 return (Unknown_Invalid
);
1563 return (File_Sys_Change
);
1569 if ((FileWithPath
= ShellFindFilePathEx(CmdName
, mExecutableExtensions
)) != NULL
) {
1571 // See if that file has a script file extension
1573 if (StrLen(FileWithPath
) > 4) {
1574 TempLocation
= FileWithPath
+StrLen(FileWithPath
)-4;
1575 TempLocation2
= mScriptExtension
;
1576 if (StringNoCaseCompare((VOID
*)(&TempLocation
), (VOID
*)(&TempLocation2
)) == 0) {
1577 SHELL_FREE_NON_NULL(FileWithPath
);
1578 return (Script_File_Name
);
1583 // Was a file, but not a script. we treat this as an application.
1585 SHELL_FREE_NON_NULL(FileWithPath
);
1586 return (Efi_Application
);
1589 SHELL_FREE_NON_NULL(FileWithPath
);
1591 // No clue what this is... return invalid flag...
1593 return (Unknown_Invalid
);
1597 Determine if the first item in a command line is valid.
1599 @param[in] CmdLine The command line to parse.
1601 @retval EFI_SUCCESS The item is valid.
1602 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
1603 @retval EFI_NOT_FOUND The operation type is unknown or invalid.
1608 IN CONST CHAR16
*CmdLine
1612 CHAR16
*FirstParameter
;
1618 Temp
= StrnCatGrow(&Temp
, NULL
, CmdLine
, 0);
1620 return (EFI_OUT_OF_RESOURCES
);
1623 FirstParameter
= StrStr(Temp
, L
"|");
1624 if (FirstParameter
!= NULL
) {
1625 *FirstParameter
= CHAR_NULL
;
1628 FirstParameter
= NULL
;
1631 // Process the command line
1633 Status
= ProcessCommandLineToFinal(&Temp
);
1635 if (!EFI_ERROR(Status
)) {
1636 FirstParameter
= AllocateZeroPool(StrSize(CmdLine
));
1637 if (FirstParameter
== NULL
) {
1638 SHELL_FREE_NON_NULL(Temp
);
1639 return (EFI_OUT_OF_RESOURCES
);
1641 TempWalker
= (CHAR16
*)Temp
;
1642 GetNextParameter(&TempWalker
, &FirstParameter
);
1644 if (GetOperationType(FirstParameter
) == Unknown_Invalid
) {
1645 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_NOT_FOUND
), ShellInfoObject
.HiiHandle
, FirstParameter
);
1646 SetLastError(SHELL_NOT_FOUND
);
1647 Status
= EFI_NOT_FOUND
;
1651 SHELL_FREE_NON_NULL(Temp
);
1652 SHELL_FREE_NON_NULL(FirstParameter
);
1657 Determine if a command line contains with a split contains only valid commands.
1659 @param[in] CmdLine The command line to parse.
1661 @retval EFI_SUCCESS CmdLine has only valid commands, application, or has no split.
1662 @retval EFI_ABORTED CmdLine has at least one invalid command or application.
1667 IN CONST CHAR16
*CmdLine
1670 CONST CHAR16
*TempSpot
;
1674 // Verify up to the pipe or end character
1676 Status
= IsValidSplit(CmdLine
);
1677 if (EFI_ERROR(Status
)) {
1682 // If this was the only item, then get out
1684 if (!ContainsSplit(CmdLine
)) {
1685 return (EFI_SUCCESS
);
1689 // recurse to verify the next item
1691 TempSpot
= FindSplit(CmdLine
)+1;
1692 return (VerifySplit(TempSpot
));
1696 Process a split based operation.
1698 @param[in] CmdLine pointer to the command line to process
1700 @retval EFI_SUCCESS The operation was successful
1701 @return an error occured.
1705 ProcessNewSplitCommandLine(
1706 IN CONST CHAR16
*CmdLine
1712 Status
= VerifySplit(CmdLine
);
1713 if (EFI_ERROR(Status
)) {
1720 // are we in an existing split???
1722 if (!IsListEmpty(&ShellInfoObject
.SplitList
.Link
)) {
1723 Split
= (SPLIT_LIST
*)GetFirstNode(&ShellInfoObject
.SplitList
.Link
);
1726 if (Split
== NULL
) {
1727 Status
= RunSplitCommand(CmdLine
, NULL
, NULL
);
1729 Status
= RunSplitCommand(CmdLine
, Split
->SplitStdIn
, Split
->SplitStdOut
);
1731 if (EFI_ERROR(Status
)) {
1732 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_INVALID_SPLIT
), ShellInfoObject
.HiiHandle
, CmdLine
);
1738 Handle a request to change the current file system.
1740 @param[in] CmdLine The passed in command line.
1742 @retval EFI_SUCCESS The operation was successful.
1747 IN CONST CHAR16
*CmdLine
1751 Status
= EFI_SUCCESS
;
1754 // make sure we are the right operation
1756 ASSERT(CmdLine
[(StrLen(CmdLine
)-1)] == L
':' && StrStr(CmdLine
, L
" ") == NULL
);
1759 // Call the protocol API to do the work
1761 Status
= ShellInfoObject
.NewEfiShellProtocol
->SetCurDir(NULL
, CmdLine
);
1764 // Report any errors
1766 if (EFI_ERROR(Status
)) {
1767 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_INVALID_MAPPING
), ShellInfoObject
.HiiHandle
, CmdLine
);
1774 Reprocess the command line to direct all -? to the help command.
1776 if found, will add "help" as argv[0], and move the rest later.
1778 @param[in,out] CmdLine pointer to the command line to update
1783 IN OUT CHAR16
**CmdLine
1786 CHAR16
*CurrentParameter
;
1789 CHAR16
*NewCommandLine
;
1792 Status
= EFI_SUCCESS
;
1794 CurrentParameter
= AllocateZeroPool(StrSize(*CmdLine
));
1795 if (CurrentParameter
== NULL
) {
1796 return (EFI_OUT_OF_RESOURCES
);
1800 while(Walker
!= NULL
&& *Walker
!= CHAR_NULL
) {
1801 LastWalker
= Walker
;
1802 GetNextParameter(&Walker
, &CurrentParameter
);
1803 if (StrStr(CurrentParameter
, L
"-?") == CurrentParameter
) {
1804 LastWalker
[0] = L
' ';
1805 LastWalker
[1] = L
' ';
1806 NewCommandLine
= AllocateZeroPool(StrSize(L
"help ") + StrSize(*CmdLine
));
1807 if (NewCommandLine
== NULL
) {
1808 Status
= EFI_OUT_OF_RESOURCES
;
1811 StrCpy(NewCommandLine
, L
"help ");
1812 StrCat(NewCommandLine
, *CmdLine
);
1813 SHELL_FREE_NON_NULL(*CmdLine
);
1814 *CmdLine
= NewCommandLine
;
1819 SHELL_FREE_NON_NULL(CurrentParameter
);
1825 Function to update the shell variable "lasterror".
1827 @param[in] ErrorCode the error code to put into lasterror.
1832 IN CONST SHELL_STATUS ErrorCode
1835 CHAR16 LeString
[19];
1836 if (sizeof(EFI_STATUS
) == sizeof(UINT64
)) {
1837 UnicodeSPrint(LeString
, sizeof(LeString
), L
"0x%Lx", ErrorCode
);
1839 UnicodeSPrint(LeString
, sizeof(LeString
), L
"0x%x", ErrorCode
);
1841 DEBUG_CODE(InternalEfiShellSetEnv(L
"debuglasterror", LeString
, TRUE
););
1842 InternalEfiShellSetEnv(L
"lasterror", LeString
, TRUE
);
1844 return (EFI_SUCCESS
);
1848 Converts the command line to it's post-processed form. this replaces variables and alias' per UEFI Shell spec.
1850 @param[in,out] CmdLine pointer to the command line to update
1852 @retval EFI_SUCCESS The operation was successful
1853 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
1854 @return some other error occured
1858 ProcessCommandLineToFinal(
1859 IN OUT CHAR16
**CmdLine
1863 TrimSpaces(CmdLine
);
1865 Status
= ShellSubstituteAliases(CmdLine
);
1866 if (EFI_ERROR(Status
)) {
1870 TrimSpaces(CmdLine
);
1872 Status
= ShellSubstituteVariables(CmdLine
);
1873 if (EFI_ERROR(Status
)) {
1877 TrimSpaces(CmdLine
);
1880 // update for help parsing
1882 if (StrStr(*CmdLine
, L
"?") != NULL
) {
1884 // This may do nothing if the ? does not indicate help.
1885 // Save all the details for in the API below.
1887 Status
= DoHelpUpdate(CmdLine
);
1890 TrimSpaces(CmdLine
);
1892 return (EFI_SUCCESS
);
1896 Run an internal shell command.
1898 This API will upadate the shell's environment since these commands are libraries.
1900 @param[in] CmdLine the command line to run.
1901 @param[in] FirstParameter the first parameter on the command line
1902 @param[in] ParamProtocol the shell parameters protocol pointer
1904 @retval EFI_SUCCESS The command was completed.
1905 @retval EFI_ABORTED The command's operation was aborted.
1910 IN CONST CHAR16
*CmdLine
,
1911 IN CHAR16
*FirstParameter
,
1912 IN EFI_SHELL_PARAMETERS_PROTOCOL
*ParamProtocol
1918 SHELL_STATUS CommandReturnedStatus
;
1922 // get the argc and argv updated for internal commands
1924 Status
= UpdateArgcArgv(ParamProtocol
, CmdLine
, &Argv
, &Argc
);
1925 if (!EFI_ERROR(Status
)) {
1927 // Run the internal command.
1929 Status
= ShellCommandRunCommandHandler(FirstParameter
, &CommandReturnedStatus
, &LastError
);
1931 if (!EFI_ERROR(Status
)) {
1933 // Update last error status.
1934 // some commands do not update last error.
1937 SetLastError(CommandReturnedStatus
);
1941 // Pass thru the exitcode from the app.
1943 if (ShellCommandGetExit()) {
1945 // An Exit was requested ("exit" command), pass its value up.
1947 Status
= CommandReturnedStatus
;
1948 } else if (CommandReturnedStatus
!= SHELL_SUCCESS
&& IsScriptOnlyCommand(FirstParameter
)) {
1950 // Always abort when a script only command fails for any reason
1952 Status
= EFI_ABORTED
;
1953 } else if (ShellCommandGetCurrentScriptFile() != NULL
&& CommandReturnedStatus
== SHELL_ABORTED
) {
1955 // Abort when in a script and a command aborted
1957 Status
= EFI_ABORTED
;
1963 // This is guarenteed to be called after UpdateArgcArgv no matter what else happened.
1964 // This is safe even if the update API failed. In this case, it may be a no-op.
1966 RestoreArgcArgv(ParamProtocol
, &Argv
, &Argc
);
1969 // If a script is running and the command is not a scipt only command, then
1970 // change return value to success so the script won't halt (unless aborted).
1972 // Script only commands have to be able halt the script since the script will
1973 // not operate if they are failing.
1975 if ( ShellCommandGetCurrentScriptFile() != NULL
1976 && !IsScriptOnlyCommand(FirstParameter
)
1977 && Status
!= EFI_ABORTED
1979 Status
= EFI_SUCCESS
;
1986 Function to run the command or file.
1988 @param[in] Type the type of operation being run.
1989 @param[in] CmdLine the command line to run.
1990 @param[in] FirstParameter the first parameter on the command line
1991 @param[in] ParamProtocol the shell parameters protocol pointer
1993 @retval EFI_SUCCESS The command was completed.
1994 @retval EFI_ABORTED The command's operation was aborted.
1999 IN SHELL_OPERATION_TYPES Type
,
2000 IN CONST CHAR16
*CmdLine
,
2001 IN CHAR16
*FirstParameter
,
2002 IN EFI_SHELL_PARAMETERS_PROTOCOL
*ParamProtocol
2006 EFI_STATUS StatusCode
;
2007 CHAR16
*CommandWithPath
;
2008 EFI_DEVICE_PATH_PROTOCOL
*DevPath
;
2010 Status
= EFI_SUCCESS
;
2011 CommandWithPath
= NULL
;
2015 case Internal_Command
:
2016 Status
= RunInternalCommand(CmdLine
, FirstParameter
, ParamProtocol
);
2018 case Script_File_Name
:
2019 case Efi_Application
:
2021 // Process a fully qualified path
2023 if (StrStr(FirstParameter
, L
":") != NULL
) {
2024 ASSERT (CommandWithPath
== NULL
);
2025 if (ShellIsFile(FirstParameter
) == EFI_SUCCESS
) {
2026 CommandWithPath
= StrnCatGrow(&CommandWithPath
, NULL
, FirstParameter
, 0);
2031 // Process a relative path and also check in the path environment variable
2033 if (CommandWithPath
== NULL
) {
2034 CommandWithPath
= ShellFindFilePathEx(FirstParameter
, mExecutableExtensions
);
2038 // This should be impossible now.
2040 ASSERT(CommandWithPath
!= NULL
);
2043 // Make sure that path is not just a directory (or not found)
2045 if (!EFI_ERROR(ShellIsDirectory(CommandWithPath
))) {
2046 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_NOT_FOUND
), ShellInfoObject
.HiiHandle
, FirstParameter
);
2047 SetLastError(SHELL_NOT_FOUND
);
2050 case Script_File_Name
:
2051 Status
= RunScriptFile (CommandWithPath
, NULL
, CmdLine
, ParamProtocol
);
2053 case Efi_Application
:
2055 // Get the device path of the application image
2057 DevPath
= ShellInfoObject
.NewEfiShellProtocol
->GetDevicePathFromFilePath(CommandWithPath
);
2058 if (DevPath
== NULL
){
2059 Status
= EFI_OUT_OF_RESOURCES
;
2064 // Execute the device path
2066 Status
= InternalShellExecuteDevicePath(
2074 SHELL_FREE_NON_NULL(DevPath
);
2077 // Update last error status.
2079 SetLastError((SHELL_STATUS
) StatusCode
);
2095 SHELL_FREE_NON_NULL(CommandWithPath
);
2101 Function to setup StdIn, StdErr, StdOut, and then run the command or file.
2103 @param[in] Type the type of operation being run.
2104 @param[in] CmdLine the command line to run.
2105 @param[in] FirstParameter the first parameter on the command line.
2106 @param[in] ParamProtocol the shell parameters protocol pointer
2108 @retval EFI_SUCCESS The command was completed.
2109 @retval EFI_ABORTED The command's operation was aborted.
2113 SetupAndRunCommandOrFile(
2114 IN SHELL_OPERATION_TYPES Type
,
2116 IN CHAR16
*FirstParameter
,
2117 IN EFI_SHELL_PARAMETERS_PROTOCOL
*ParamProtocol
2121 SHELL_FILE_HANDLE OriginalStdIn
;
2122 SHELL_FILE_HANDLE OriginalStdOut
;
2123 SHELL_FILE_HANDLE OriginalStdErr
;
2124 SYSTEM_TABLE_INFO OriginalSystemTableInfo
;
2127 // Update the StdIn, StdOut, and StdErr for redirection to environment variables, files, etc... unicode and ASCII
2129 Status
= UpdateStdInStdOutStdErr(ParamProtocol
, CmdLine
, &OriginalStdIn
, &OriginalStdOut
, &OriginalStdErr
, &OriginalSystemTableInfo
);
2132 // The StdIn, StdOut, and StdErr are set up.
2133 // Now run the command, script, or application
2135 if (!EFI_ERROR(Status
)) {
2136 Status
= RunCommandOrFile(Type
, CmdLine
, FirstParameter
, ParamProtocol
);
2142 if (EFI_ERROR(Status
)) {
2143 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_ERROR
), ShellInfoObject
.HiiHandle
, (VOID
*)(Status
));
2147 // put back the original StdIn, StdOut, and StdErr
2149 RestoreStdInStdOutStdErr(ParamProtocol
, &OriginalStdIn
, &OriginalStdOut
, &OriginalStdErr
, &OriginalSystemTableInfo
);
2155 Function will process and run a command line.
2157 This will determine if the command line represents an internal shell
2158 command or dispatch an external application.
2160 @param[in] CmdLine The command line to parse.
2162 @retval EFI_SUCCESS The command was completed.
2163 @retval EFI_ABORTED The command's operation was aborted.
2168 IN CONST CHAR16
*CmdLine
2172 CHAR16
*CleanOriginal
;
2173 CHAR16
*FirstParameter
;
2175 SHELL_OPERATION_TYPES Type
;
2177 ASSERT(CmdLine
!= NULL
);
2178 if (StrLen(CmdLine
) == 0) {
2179 return (EFI_SUCCESS
);
2182 Status
= EFI_SUCCESS
;
2183 CleanOriginal
= NULL
;
2185 CleanOriginal
= StrnCatGrow(&CleanOriginal
, NULL
, CmdLine
, 0);
2186 if (CleanOriginal
== NULL
) {
2187 return (EFI_OUT_OF_RESOURCES
);
2190 TrimSpaces(&CleanOriginal
);
2193 // Handle case that passed in command line is just 1 or more " " characters.
2195 if (StrLen (CleanOriginal
) == 0) {
2196 SHELL_FREE_NON_NULL(CleanOriginal
);
2197 return (EFI_SUCCESS
);
2200 Status
= ProcessCommandLineToFinal(&CleanOriginal
);
2201 if (EFI_ERROR(Status
)) {
2202 SHELL_FREE_NON_NULL(CleanOriginal
);
2207 // We dont do normal processing with a split command line (output from one command input to another)
2209 if (ContainsSplit(CleanOriginal
)) {
2210 Status
= ProcessNewSplitCommandLine(CleanOriginal
);
2211 SHELL_FREE_NON_NULL(CleanOriginal
);
2216 // We need the first parameter information so we can determine the operation type
2218 FirstParameter
= AllocateZeroPool(StrSize(CleanOriginal
));
2219 if (FirstParameter
== NULL
) {
2220 SHELL_FREE_NON_NULL(CleanOriginal
);
2221 return (EFI_OUT_OF_RESOURCES
);
2223 TempWalker
= CleanOriginal
;
2224 GetNextParameter(&TempWalker
, &FirstParameter
);
2227 // Depending on the first parameter we change the behavior
2229 switch (Type
= GetOperationType(FirstParameter
)) {
2230 case File_Sys_Change
:
2231 Status
= ChangeMappedDrive(CleanOriginal
);
2233 case Internal_Command
:
2234 case Script_File_Name
:
2235 case Efi_Application
:
2236 Status
= SetupAndRunCommandOrFile(Type
, CleanOriginal
, FirstParameter
, ShellInfoObject
.NewShellParametersProtocol
);
2240 // Whatever was typed, it was invalid.
2242 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_NOT_FOUND
), ShellInfoObject
.HiiHandle
, FirstParameter
);
2243 SetLastError(SHELL_NOT_FOUND
);
2247 SHELL_FREE_NON_NULL(CleanOriginal
);
2248 SHELL_FREE_NON_NULL(FirstParameter
);
2253 STATIC CONST UINT16 InvalidChars
[] = {L
'*', L
'?', L
'<', L
'>', L
'\\', L
'/', L
'\"', 0x0001, 0x0002};
2255 Function determins if the CommandName COULD be a valid command. It does not determine whether
2256 this is a valid command. It only checks for invalid characters.
2258 @param[in] CommandName The name to check
2260 @retval TRUE CommandName could be a command name
2261 @retval FALSE CommandName could not be a valid command name
2266 IN CONST CHAR16
*CommandName
2270 if (CommandName
== NULL
) {
2275 ; Count
< sizeof(InvalidChars
) / sizeof(InvalidChars
[0])
2278 if (ScanMem16(CommandName
, StrSize(CommandName
), InvalidChars
[Count
]) != NULL
) {
2286 Function to process a NSH script file via SHELL_FILE_HANDLE.
2288 @param[in] Handle The handle to the already opened file.
2289 @param[in] Name The name of the script file.
2291 @retval EFI_SUCCESS the script completed sucessfully
2295 RunScriptFileHandle (
2296 IN SHELL_FILE_HANDLE Handle
,
2297 IN CONST CHAR16
*Name
2301 SCRIPT_FILE
*NewScriptFile
;
2303 CHAR16
*CommandLine
;
2304 CHAR16
*CommandLine2
;
2305 CHAR16
*CommandLine3
;
2306 SCRIPT_COMMAND_LIST
*LastCommand
;
2308 BOOLEAN PreScriptEchoState
;
2309 BOOLEAN PreCommandEchoState
;
2310 CONST CHAR16
*CurDir
;
2312 CHAR16 LeString
[50];
2314 ASSERT(!ShellCommandGetScriptExit());
2316 PreScriptEchoState
= ShellCommandGetEchoState();
2318 NewScriptFile
= (SCRIPT_FILE
*)AllocateZeroPool(sizeof(SCRIPT_FILE
));
2319 if (NewScriptFile
== NULL
) {
2320 return (EFI_OUT_OF_RESOURCES
);
2326 ASSERT(NewScriptFile
->ScriptName
== NULL
);
2327 NewScriptFile
->ScriptName
= StrnCatGrow(&NewScriptFile
->ScriptName
, NULL
, Name
, 0);
2328 if (NewScriptFile
->ScriptName
== NULL
) {
2329 DeleteScriptFileStruct(NewScriptFile
);
2330 return (EFI_OUT_OF_RESOURCES
);
2334 // Save the parameters (used to replace %0 to %9 later on)
2336 NewScriptFile
->Argc
= ShellInfoObject
.NewShellParametersProtocol
->Argc
;
2337 if (NewScriptFile
->Argc
!= 0) {
2338 NewScriptFile
->Argv
= (CHAR16
**)AllocateZeroPool(NewScriptFile
->Argc
* sizeof(CHAR16
*));
2339 if (NewScriptFile
->Argv
== NULL
) {
2340 DeleteScriptFileStruct(NewScriptFile
);
2341 return (EFI_OUT_OF_RESOURCES
);
2343 for (LoopVar
= 0 ; LoopVar
< 10 && LoopVar
< NewScriptFile
->Argc
; LoopVar
++) {
2344 ASSERT(NewScriptFile
->Argv
[LoopVar
] == NULL
);
2345 NewScriptFile
->Argv
[LoopVar
] = StrnCatGrow(&NewScriptFile
->Argv
[LoopVar
], NULL
, ShellInfoObject
.NewShellParametersProtocol
->Argv
[LoopVar
], 0);
2346 if (NewScriptFile
->Argv
[LoopVar
] == NULL
) {
2347 DeleteScriptFileStruct(NewScriptFile
);
2348 return (EFI_OUT_OF_RESOURCES
);
2352 NewScriptFile
->Argv
= NULL
;
2355 InitializeListHead(&NewScriptFile
->CommandList
);
2356 InitializeListHead(&NewScriptFile
->SubstList
);
2359 // Now build the list of all script commands.
2362 while(!ShellFileHandleEof(Handle
)) {
2363 CommandLine
= ShellFileHandleReturnLine(Handle
, &Ascii
);
2365 if (CommandLine
== NULL
|| StrLen(CommandLine
) == 0 || CommandLine
[0] == '#') {
2366 SHELL_FREE_NON_NULL(CommandLine
);
2369 NewScriptFile
->CurrentCommand
= AllocateZeroPool(sizeof(SCRIPT_COMMAND_LIST
));
2370 if (NewScriptFile
->CurrentCommand
== NULL
) {
2371 SHELL_FREE_NON_NULL(CommandLine
);
2372 DeleteScriptFileStruct(NewScriptFile
);
2373 return (EFI_OUT_OF_RESOURCES
);
2376 NewScriptFile
->CurrentCommand
->Cl
= CommandLine
;
2377 NewScriptFile
->CurrentCommand
->Data
= NULL
;
2378 NewScriptFile
->CurrentCommand
->Line
= LineCount
;
2380 InsertTailList(&NewScriptFile
->CommandList
, &NewScriptFile
->CurrentCommand
->Link
);
2384 // Add this as the topmost script file
2386 ShellCommandSetNewScript (NewScriptFile
);
2389 // Now enumerate through the commands and run each one.
2391 CommandLine
= AllocateZeroPool(PcdGet16(PcdShellPrintBufferSize
));
2392 if (CommandLine
== NULL
) {
2393 DeleteScriptFileStruct(NewScriptFile
);
2394 return (EFI_OUT_OF_RESOURCES
);
2396 CommandLine2
= AllocateZeroPool(PcdGet16(PcdShellPrintBufferSize
));
2397 if (CommandLine2
== NULL
) {
2398 FreePool(CommandLine
);
2399 DeleteScriptFileStruct(NewScriptFile
);
2400 return (EFI_OUT_OF_RESOURCES
);
2403 for ( NewScriptFile
->CurrentCommand
= (SCRIPT_COMMAND_LIST
*)GetFirstNode(&NewScriptFile
->CommandList
)
2404 ; !IsNull(&NewScriptFile
->CommandList
, &NewScriptFile
->CurrentCommand
->Link
)
2405 ; // conditional increment in the body of the loop
2407 ASSERT(CommandLine2
!= NULL
);
2408 StrCpy(CommandLine2
, NewScriptFile
->CurrentCommand
->Cl
);
2411 // NULL out comments
2413 for (CommandLine3
= CommandLine2
; CommandLine3
!= NULL
&& *CommandLine3
!= CHAR_NULL
; CommandLine3
++) {
2414 if (*CommandLine3
== L
'^') {
2415 if (*(CommandLine3
+1) == L
'#' || *(CommandLine3
+1) == L
':') {
2416 CopyMem(CommandLine3
, CommandLine3
+1, StrSize(CommandLine3
) - sizeof(CommandLine3
[0]));
2418 } else if (*CommandLine3
== L
'#') {
2419 *CommandLine3
= CHAR_NULL
;
2423 if (CommandLine2
!= NULL
&& StrLen(CommandLine2
) >= 1) {
2425 // Due to variability in starting the find and replace action we need to have both buffers the same.
2427 StrCpy(CommandLine
, CommandLine2
);
2430 // Remove the %0 to %9 from the command line (if we have some arguments)
2432 if (NewScriptFile
->Argv
!= NULL
) {
2433 switch (NewScriptFile
->Argc
) {
2435 Status
= ShellCopySearchAndReplace(CommandLine2
, CommandLine
, PcdGet16 (PcdShellPrintBufferSize
), L
"%9", NewScriptFile
->Argv
[9], FALSE
, TRUE
);
2436 ASSERT_EFI_ERROR(Status
);
2438 Status
= ShellCopySearchAndReplace(CommandLine
, CommandLine2
, PcdGet16 (PcdShellPrintBufferSize
), L
"%8", NewScriptFile
->Argv
[8], FALSE
, TRUE
);
2439 ASSERT_EFI_ERROR(Status
);
2441 Status
= ShellCopySearchAndReplace(CommandLine2
, CommandLine
, PcdGet16 (PcdShellPrintBufferSize
), L
"%7", NewScriptFile
->Argv
[7], FALSE
, TRUE
);
2442 ASSERT_EFI_ERROR(Status
);
2444 Status
= ShellCopySearchAndReplace(CommandLine
, CommandLine2
, PcdGet16 (PcdShellPrintBufferSize
), L
"%6", NewScriptFile
->Argv
[6], FALSE
, TRUE
);
2445 ASSERT_EFI_ERROR(Status
);
2447 Status
= ShellCopySearchAndReplace(CommandLine2
, CommandLine
, PcdGet16 (PcdShellPrintBufferSize
), L
"%5", NewScriptFile
->Argv
[5], FALSE
, TRUE
);
2448 ASSERT_EFI_ERROR(Status
);
2450 Status
= ShellCopySearchAndReplace(CommandLine
, CommandLine2
, PcdGet16 (PcdShellPrintBufferSize
), L
"%4", NewScriptFile
->Argv
[4], FALSE
, TRUE
);
2451 ASSERT_EFI_ERROR(Status
);
2453 Status
= ShellCopySearchAndReplace(CommandLine2
, CommandLine
, PcdGet16 (PcdShellPrintBufferSize
), L
"%3", NewScriptFile
->Argv
[3], FALSE
, TRUE
);
2454 ASSERT_EFI_ERROR(Status
);
2456 Status
= ShellCopySearchAndReplace(CommandLine
, CommandLine2
, PcdGet16 (PcdShellPrintBufferSize
), L
"%2", NewScriptFile
->Argv
[2], FALSE
, TRUE
);
2457 ASSERT_EFI_ERROR(Status
);
2459 Status
= ShellCopySearchAndReplace(CommandLine2
, CommandLine
, PcdGet16 (PcdShellPrintBufferSize
), L
"%1", NewScriptFile
->Argv
[1], FALSE
, TRUE
);
2460 ASSERT_EFI_ERROR(Status
);
2462 Status
= ShellCopySearchAndReplace(CommandLine
, CommandLine2
, PcdGet16 (PcdShellPrintBufferSize
), L
"%0", NewScriptFile
->Argv
[0], FALSE
, TRUE
);
2463 ASSERT_EFI_ERROR(Status
);
2469 Status
= ShellCopySearchAndReplace(CommandLine2
, CommandLine
, PcdGet16 (PcdShellPrintBufferSize
), L
"%1", L
"\"\"", FALSE
, FALSE
);
2470 Status
= ShellCopySearchAndReplace(CommandLine
, CommandLine2
, PcdGet16 (PcdShellPrintBufferSize
), L
"%2", L
"\"\"", FALSE
, FALSE
);
2471 Status
= ShellCopySearchAndReplace(CommandLine2
, CommandLine
, PcdGet16 (PcdShellPrintBufferSize
), L
"%3", L
"\"\"", FALSE
, FALSE
);
2472 Status
= ShellCopySearchAndReplace(CommandLine
, CommandLine2
, PcdGet16 (PcdShellPrintBufferSize
), L
"%4", L
"\"\"", FALSE
, FALSE
);
2473 Status
= ShellCopySearchAndReplace(CommandLine2
, CommandLine
, PcdGet16 (PcdShellPrintBufferSize
), L
"%5", L
"\"\"", FALSE
, FALSE
);
2474 Status
= ShellCopySearchAndReplace(CommandLine
, CommandLine2
, PcdGet16 (PcdShellPrintBufferSize
), L
"%6", L
"\"\"", FALSE
, FALSE
);
2475 Status
= ShellCopySearchAndReplace(CommandLine2
, CommandLine
, PcdGet16 (PcdShellPrintBufferSize
), L
"%7", L
"\"\"", FALSE
, FALSE
);
2476 Status
= ShellCopySearchAndReplace(CommandLine
, CommandLine2
, PcdGet16 (PcdShellPrintBufferSize
), L
"%8", L
"\"\"", FALSE
, FALSE
);
2477 Status
= ShellCopySearchAndReplace(CommandLine2
, CommandLine
, PcdGet16 (PcdShellPrintBufferSize
), L
"%9", L
"\"\"", FALSE
, FALSE
);
2479 StrCpy(CommandLine2
, CommandLine
);
2481 LastCommand
= NewScriptFile
->CurrentCommand
;
2483 for (CommandLine3
= CommandLine2
; CommandLine3
[0] == L
' ' ; CommandLine3
++);
2485 if (CommandLine3
!= NULL
&& CommandLine3
[0] == L
':' ) {
2487 // This line is a goto target / label
2490 if (CommandLine3
!= NULL
&& StrLen(CommandLine3
) > 0) {
2491 if (CommandLine3
[0] == L
'@') {
2493 // We need to save the current echo state
2494 // and disable echo for just this command.
2496 PreCommandEchoState
= ShellCommandGetEchoState();
2497 ShellCommandSetEchoState(FALSE
);
2498 Status
= RunCommand(CommandLine3
+1);
2501 // If command was "@echo -off" or "@echo -on" then don't restore echo state
2503 if (StrCmp (L
"@echo -off", CommandLine3
) != 0 &&
2504 StrCmp (L
"@echo -on", CommandLine3
) != 0) {
2506 // Now restore the pre-'@' echo state.
2508 ShellCommandSetEchoState(PreCommandEchoState
);
2511 if (ShellCommandGetEchoState()) {
2512 CurDir
= ShellInfoObject
.NewEfiShellProtocol
->GetEnv(L
"cwd");
2513 if (CurDir
!= NULL
&& StrLen(CurDir
) > 1) {
2514 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_CURDIR
), ShellInfoObject
.HiiHandle
, CurDir
);
2516 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_SHELL
), ShellInfoObject
.HiiHandle
);
2518 ShellPrintEx(-1, -1, L
"%s\r\n", CommandLine2
);
2520 Status
= RunCommand(CommandLine3
);
2524 if (ShellCommandGetScriptExit()) {
2526 // ShellCommandGetExitCode() always returns a UINT64
2528 UnicodeSPrint(LeString
, sizeof(LeString
), L
"0x%Lx", ShellCommandGetExitCode());
2529 DEBUG_CODE(InternalEfiShellSetEnv(L
"debuglasterror", LeString
, TRUE
););
2530 InternalEfiShellSetEnv(L
"lasterror", LeString
, TRUE
);
2532 ShellCommandRegisterExit(FALSE
, 0);
2533 Status
= EFI_SUCCESS
;
2536 if (ShellGetExecutionBreakFlag()) {
2539 if (EFI_ERROR(Status
)) {
2542 if (ShellCommandGetExit()) {
2547 // If that commend did not update the CurrentCommand then we need to advance it...
2549 if (LastCommand
== NewScriptFile
->CurrentCommand
) {
2550 NewScriptFile
->CurrentCommand
= (SCRIPT_COMMAND_LIST
*)GetNextNode(&NewScriptFile
->CommandList
, &NewScriptFile
->CurrentCommand
->Link
);
2551 if (!IsNull(&NewScriptFile
->CommandList
, &NewScriptFile
->CurrentCommand
->Link
)) {
2552 NewScriptFile
->CurrentCommand
->Reset
= TRUE
;
2556 NewScriptFile
->CurrentCommand
= (SCRIPT_COMMAND_LIST
*)GetNextNode(&NewScriptFile
->CommandList
, &NewScriptFile
->CurrentCommand
->Link
);
2557 if (!IsNull(&NewScriptFile
->CommandList
, &NewScriptFile
->CurrentCommand
->Link
)) {
2558 NewScriptFile
->CurrentCommand
->Reset
= TRUE
;
2564 FreePool(CommandLine
);
2565 FreePool(CommandLine2
);
2566 ShellCommandSetNewScript (NULL
);
2569 // Only if this was the last script reset the state.
2571 if (ShellCommandGetCurrentScriptFile()==NULL
) {
2572 ShellCommandSetEchoState(PreScriptEchoState
);
2574 return (EFI_SUCCESS
);
2578 Function to process a NSH script file.
2580 @param[in] ScriptPath Pointer to the script file name (including file system path).
2581 @param[in] Handle the handle of the script file already opened.
2582 @param[in] CmdLine the command line to run.
2583 @param[in] ParamProtocol the shell parameters protocol pointer
2585 @retval EFI_SUCCESS the script completed sucessfully
2590 IN CONST CHAR16
*ScriptPath
,
2591 IN SHELL_FILE_HANDLE Handle OPTIONAL
,
2592 IN CONST CHAR16
*CmdLine
,
2593 IN EFI_SHELL_PARAMETERS_PROTOCOL
*ParamProtocol
2597 SHELL_FILE_HANDLE FileHandle
;
2601 if (ShellIsFile(ScriptPath
) != EFI_SUCCESS
) {
2602 return (EFI_INVALID_PARAMETER
);
2606 // get the argc and argv updated for scripts
2608 Status
= UpdateArgcArgv(ParamProtocol
, CmdLine
, &Argv
, &Argc
);
2609 if (!EFI_ERROR(Status
)) {
2611 if (Handle
== NULL
) {
2615 Status
= ShellOpenFileByName(ScriptPath
, &FileHandle
, EFI_FILE_MODE_READ
, 0);
2616 if (!EFI_ERROR(Status
)) {
2620 Status
= RunScriptFileHandle(FileHandle
, ScriptPath
);
2623 // now close the file
2625 ShellCloseFile(&FileHandle
);
2628 Status
= RunScriptFileHandle(Handle
, ScriptPath
);
2633 // This is guarenteed to be called after UpdateArgcArgv no matter what else happened.
2634 // This is safe even if the update API failed. In this case, it may be a no-op.
2636 RestoreArgcArgv(ParamProtocol
, &Argv
, &Argc
);