2 This is THE shell (application)
4 Copyright (c) 2009 - 2013, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
18 // Initialize the global structure
20 SHELL_INFO ShellInfoObject
= {
55 {{NULL
, NULL
}, NULL
, NULL
},
56 {{NULL
, NULL
}, NULL
, NULL
},
68 STATIC CONST CHAR16 mScriptExtension
[] = L
".NSH";
69 STATIC CONST CHAR16 mExecutableExtensions
[] = L
".NSH;.EFI";
70 STATIC CONST CHAR16 mStartupScript
[] = L
"startup.nsh";
73 Function to start monitoring for CTRL-S using SimpleTextInputEx. This
74 feature's enabled state was not known when the shell initially launched.
76 @retval EFI_SUCCESS The feature is enabled.
77 @retval EFI_OUT_OF_RESOURCES There is not enough mnemory available.
81 InternalEfiShellStartCtrlSMonitor(
85 EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL
*SimpleEx
;
89 Status
= gBS
->OpenProtocol(
91 &gEfiSimpleTextInputExProtocolGuid
,
95 EFI_OPEN_PROTOCOL_GET_PROTOCOL
);
96 if (EFI_ERROR(Status
)) {
101 STRING_TOKEN (STR_SHELL_NO_IN_EX
),
102 ShellInfoObject
.HiiHandle
);
103 return (EFI_SUCCESS
);
106 KeyData
.KeyState
.KeyToggleState
= 0;
107 KeyData
.Key
.ScanCode
= 0;
108 KeyData
.KeyState
.KeyShiftState
= EFI_SHIFT_STATE_VALID
|EFI_LEFT_CONTROL_PRESSED
;
109 KeyData
.Key
.UnicodeChar
= L
's';
111 Status
= SimpleEx
->RegisterKeyNotify(
114 NotificationFunction
,
115 &ShellInfoObject
.CtrlSNotifyHandle1
);
117 KeyData
.KeyState
.KeyShiftState
= EFI_SHIFT_STATE_VALID
|EFI_RIGHT_CONTROL_PRESSED
;
118 if (!EFI_ERROR(Status
)) {
119 Status
= SimpleEx
->RegisterKeyNotify(
122 NotificationFunction
,
123 &ShellInfoObject
.CtrlSNotifyHandle2
);
125 KeyData
.KeyState
.KeyShiftState
= EFI_SHIFT_STATE_VALID
|EFI_LEFT_CONTROL_PRESSED
;
126 KeyData
.Key
.UnicodeChar
= 19;
128 if (!EFI_ERROR(Status
)) {
129 Status
= SimpleEx
->RegisterKeyNotify(
132 NotificationFunction
,
133 &ShellInfoObject
.CtrlSNotifyHandle2
);
135 KeyData
.KeyState
.KeyShiftState
= EFI_SHIFT_STATE_VALID
|EFI_RIGHT_CONTROL_PRESSED
;
136 if (!EFI_ERROR(Status
)) {
137 Status
= SimpleEx
->RegisterKeyNotify(
140 NotificationFunction
,
141 &ShellInfoObject
.CtrlSNotifyHandle2
);
149 The entry point for the application.
151 @param[in] ImageHandle The firmware allocated handle for the EFI image.
152 @param[in] SystemTable A pointer to the EFI System Table.
154 @retval EFI_SUCCESS The entry point is executed successfully.
155 @retval other Some error occurs when executing this entry point.
161 IN EFI_HANDLE ImageHandle
,
162 IN EFI_SYSTEM_TABLE
*SystemTable
168 EFI_HANDLE ConInHandle
;
169 EFI_SIMPLE_TEXT_INPUT_PROTOCOL
*OldConIn
;
171 if (PcdGet8(PcdShellSupportLevel
) > 3) {
172 return (EFI_UNSUPPORTED
);
178 Status
= gST
->ConOut
->ClearScreen(gST
->ConOut
);
179 if (EFI_ERROR(Status
)) {
184 // Populate the global structure from PCDs
186 ShellInfoObject
.ImageDevPath
= NULL
;
187 ShellInfoObject
.FileDevPath
= NULL
;
188 ShellInfoObject
.PageBreakEnabled
= PcdGetBool(PcdShellPageBreakDefault
);
189 ShellInfoObject
.ViewingSettings
.InsertMode
= PcdGetBool(PcdShellInsertModeDefault
);
190 ShellInfoObject
.LogScreenCount
= PcdGet8 (PcdShellScreenLogCount
);
193 // verify we dont allow for spec violation
195 ASSERT(ShellInfoObject
.LogScreenCount
>= 3);
198 // Initialize the LIST ENTRY objects...
200 InitializeListHead(&ShellInfoObject
.BufferToFreeList
.Link
);
201 InitializeListHead(&ShellInfoObject
.ViewingSettings
.CommandHistory
.Link
);
202 InitializeListHead(&ShellInfoObject
.SplitList
.Link
);
205 // Check PCDs for optional features that are not implemented yet.
207 if ( PcdGetBool(PcdShellSupportOldProtocols
)
208 || !FeaturePcdGet(PcdShellRequireHiiPlatform
)
209 || FeaturePcdGet(PcdShellSupportFrameworkHii
)
211 return (EFI_UNSUPPORTED
);
215 // turn off the watchdog timer
217 gBS
->SetWatchdogTimer (0, 0, 0, NULL
);
220 // install our console logger. This will keep a log of the output for back-browsing
222 Status
= ConsoleLoggerInstall(ShellInfoObject
.LogScreenCount
, &ShellInfoObject
.ConsoleInfo
);
223 if (!EFI_ERROR(Status
)) {
225 // Enable the cursor to be visible
227 gST
->ConOut
->EnableCursor (gST
->ConOut
, TRUE
);
230 // If supporting EFI 1.1 we need to install HII protocol
231 // only do this if PcdShellRequireHiiPlatform == FALSE
233 // remove EFI_UNSUPPORTED check above when complete.
234 ///@todo add support for Framework HII
237 // install our (solitary) HII package
239 ShellInfoObject
.HiiHandle
= HiiAddPackages (&gEfiCallerIdGuid
, gImageHandle
, ShellStrings
, NULL
);
240 if (ShellInfoObject
.HiiHandle
== NULL
) {
241 if (PcdGetBool(PcdShellSupportFrameworkHii
)) {
242 ///@todo Add our package into Framework HII
244 if (ShellInfoObject
.HiiHandle
== NULL
) {
245 return (EFI_NOT_STARTED
);
250 // create and install the EfiShellParametersProtocol
252 Status
= CreatePopulateInstallShellParametersProtocol(&ShellInfoObject
.NewShellParametersProtocol
, &ShellInfoObject
.RootShellInstance
);
253 ASSERT_EFI_ERROR(Status
);
254 ASSERT(ShellInfoObject
.NewShellParametersProtocol
!= NULL
);
257 // create and install the EfiShellProtocol
259 Status
= CreatePopulateInstallShellProtocol(&ShellInfoObject
.NewEfiShellProtocol
);
260 ASSERT_EFI_ERROR(Status
);
261 ASSERT(ShellInfoObject
.NewEfiShellProtocol
!= NULL
);
264 // Now initialize the shell library (it requires Shell Parameters protocol)
266 Status
= ShellInitialize();
267 ASSERT_EFI_ERROR(Status
);
269 Status
= CommandInit();
270 ASSERT_EFI_ERROR(Status
);
273 // Check the command line
275 Status
= ProcessCommandLine();
278 // If shell support level is >= 1 create the mappings and paths
280 if (PcdGet8(PcdShellSupportLevel
) >= 1) {
281 Status
= ShellCommandCreateInitialMappingsAndPaths();
285 // save the device path for the loaded image and the device path for the filepath (under loaded image)
286 // These are where to look for the startup.nsh file
288 Status
= GetDevicePathsForImageAndFile(&ShellInfoObject
.ImageDevPath
, &ShellInfoObject
.FileDevPath
);
289 ASSERT_EFI_ERROR(Status
);
292 // Display the version
294 if (!ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoVersion
) {
297 gST
->ConOut
->Mode
->CursorRow
,
299 STRING_TOKEN (STR_VER_OUTPUT_MAIN
),
300 ShellInfoObject
.HiiHandle
,
301 SupportLevel
[PcdGet8(PcdShellSupportLevel
)],
302 gEfiShellProtocol
->MajorVersion
,
303 gEfiShellProtocol
->MinorVersion
,
304 (gST
->Hdr
.Revision
&0xffff0000)>>16,
305 (gST
->Hdr
.Revision
&0x0000ffff),
307 gST
->FirmwareRevision
312 // Display the mapping
314 if (PcdGet8(PcdShellSupportLevel
) >= 2 && !ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoMap
) {
315 Status
= RunCommand(L
"map");
316 ASSERT_EFI_ERROR(Status
);
320 // init all the built in alias'
322 Status
= SetBuiltInAlias();
323 ASSERT_EFI_ERROR(Status
);
326 // Initialize environment variables
328 if (ShellCommandGetProfileList() != NULL
) {
329 Status
= InternalEfiShellSetEnv(L
"profiles", ShellCommandGetProfileList(), TRUE
);
330 ASSERT_EFI_ERROR(Status
);
334 TempString
= AllocateZeroPool(Size
);
336 UnicodeSPrint(TempString
, Size
, L
"%d", PcdGet8(PcdShellSupportLevel
));
337 Status
= InternalEfiShellSetEnv(L
"uefishellsupport", TempString
, TRUE
);
338 ASSERT_EFI_ERROR(Status
);
340 UnicodeSPrint(TempString
, Size
, L
"%d.%d", ShellInfoObject
.NewEfiShellProtocol
->MajorVersion
, ShellInfoObject
.NewEfiShellProtocol
->MinorVersion
);
341 Status
= InternalEfiShellSetEnv(L
"uefishellversion", TempString
, TRUE
);
342 ASSERT_EFI_ERROR(Status
);
344 UnicodeSPrint(TempString
, Size
, L
"%d.%d", (gST
->Hdr
.Revision
& 0xFFFF0000) >> 16, gST
->Hdr
.Revision
& 0x0000FFFF);
345 Status
= InternalEfiShellSetEnv(L
"uefiversion", TempString
, TRUE
);
346 ASSERT_EFI_ERROR(Status
);
348 FreePool(TempString
);
350 if (!EFI_ERROR(Status
)) {
351 if (!ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoInterrupt
) {
353 // Set up the event for CTRL-C monitoring...
355 Status
= InernalEfiShellStartMonitor();
358 if (!EFI_ERROR(Status
) && !ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoConsoleIn
) {
360 // Set up the event for CTRL-S monitoring...
362 Status
= InternalEfiShellStartCtrlSMonitor();
365 if (!EFI_ERROR(Status
) && ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoConsoleIn
) {
367 // close off the gST->ConIn
369 OldConIn
= gST
->ConIn
;
370 ConInHandle
= gST
->ConsoleInHandle
;
371 gST
->ConIn
= CreateSimpleTextInOnFile((SHELL_FILE_HANDLE
)&FileInterfaceNulFile
, &gST
->ConsoleInHandle
);
377 if (!EFI_ERROR(Status
) && PcdGet8(PcdShellSupportLevel
) >= 1) {
379 // process the startup script or launch the called app.
381 Status
= DoStartupScript(ShellInfoObject
.ImageDevPath
, ShellInfoObject
.FileDevPath
);
384 if (!ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.Exit
&& !ShellCommandGetExit() && (PcdGet8(PcdShellSupportLevel
) >= 3 || PcdGetBool(PcdShellForceConsole
)) && !EFI_ERROR(Status
) && !ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoConsoleIn
) {
386 // begin the UI waiting loop
390 // clean out all the memory allocated for CONST <something> * return values
391 // between each shell prompt presentation
393 if (!IsListEmpty(&ShellInfoObject
.BufferToFreeList
.Link
)){
394 FreeBufferList(&ShellInfoObject
.BufferToFreeList
);
398 // Reset page break back to default.
400 ShellInfoObject
.PageBreakEnabled
= PcdGetBool(PcdShellPageBreakDefault
);
401 ShellInfoObject
.ConsoleInfo
->Enabled
= TRUE
;
402 ShellInfoObject
.ConsoleInfo
->RowCounter
= 0;
405 // Reset the CTRL-C event (yes we ignore the return values)
407 Status
= gBS
->CheckEvent (ShellInfoObject
.NewEfiShellProtocol
->ExecutionBreak
);
412 Status
= DoShellPrompt();
413 } while (!ShellCommandGetExit());
415 if (OldConIn
!= NULL
&& ConInHandle
!= NULL
) {
416 CloseSimpleTextInOnFile (gST
->ConIn
);
417 gST
->ConIn
= OldConIn
;
418 gST
->ConsoleInHandle
= ConInHandle
;
424 // uninstall protocols / free memory / etc...
426 if (ShellInfoObject
.UserBreakTimer
!= NULL
) {
427 gBS
->CloseEvent(ShellInfoObject
.UserBreakTimer
);
428 DEBUG_CODE(ShellInfoObject
.UserBreakTimer
= NULL
;);
430 if (ShellInfoObject
.ImageDevPath
!= NULL
) {
431 FreePool(ShellInfoObject
.ImageDevPath
);
432 DEBUG_CODE(ShellInfoObject
.ImageDevPath
= NULL
;);
434 if (ShellInfoObject
.FileDevPath
!= NULL
) {
435 FreePool(ShellInfoObject
.FileDevPath
);
436 DEBUG_CODE(ShellInfoObject
.FileDevPath
= NULL
;);
438 if (ShellInfoObject
.NewShellParametersProtocol
!= NULL
) {
439 CleanUpShellParametersProtocol(ShellInfoObject
.NewShellParametersProtocol
);
440 DEBUG_CODE(ShellInfoObject
.NewShellParametersProtocol
= NULL
;);
442 if (ShellInfoObject
.NewEfiShellProtocol
!= NULL
){
443 if (ShellInfoObject
.NewEfiShellProtocol
->IsRootShell()){
444 InternalEfiShellSetEnv(L
"cwd", NULL
, TRUE
);
446 CleanUpShellProtocol(ShellInfoObject
.NewEfiShellProtocol
);
447 DEBUG_CODE(ShellInfoObject
.NewEfiShellProtocol
= NULL
;);
450 if (!IsListEmpty(&ShellInfoObject
.BufferToFreeList
.Link
)){
451 FreeBufferList(&ShellInfoObject
.BufferToFreeList
);
454 if (!IsListEmpty(&ShellInfoObject
.SplitList
.Link
)){
455 ASSERT(FALSE
); ///@todo finish this de-allocation.
458 if (ShellInfoObject
.ShellInitSettings
.FileName
!= NULL
) {
459 FreePool(ShellInfoObject
.ShellInitSettings
.FileName
);
460 DEBUG_CODE(ShellInfoObject
.ShellInitSettings
.FileName
= NULL
;);
463 if (ShellInfoObject
.ShellInitSettings
.FileOptions
!= NULL
) {
464 FreePool(ShellInfoObject
.ShellInitSettings
.FileOptions
);
465 DEBUG_CODE(ShellInfoObject
.ShellInitSettings
.FileOptions
= NULL
;);
468 if (ShellInfoObject
.HiiHandle
!= NULL
) {
469 HiiRemovePackages(ShellInfoObject
.HiiHandle
);
470 DEBUG_CODE(ShellInfoObject
.HiiHandle
= NULL
;);
473 if (!IsListEmpty(&ShellInfoObject
.ViewingSettings
.CommandHistory
.Link
)){
474 FreeBufferList(&ShellInfoObject
.ViewingSettings
.CommandHistory
);
477 ASSERT(ShellInfoObject
.ConsoleInfo
!= NULL
);
478 if (ShellInfoObject
.ConsoleInfo
!= NULL
) {
479 ConsoleLoggerUninstall(ShellInfoObject
.ConsoleInfo
);
480 FreePool(ShellInfoObject
.ConsoleInfo
);
481 DEBUG_CODE(ShellInfoObject
.ConsoleInfo
= NULL
;);
484 if (ShellCommandGetExit()) {
485 return ((EFI_STATUS
)ShellCommandGetExitCode());
491 Sets all the alias' that were registered with the ShellCommandLib library.
493 @retval EFI_SUCCESS all init commands were run sucessfully.
501 CONST ALIAS_LIST
*List
;
505 // Get all the commands we want to run
507 List
= ShellCommandGetInitAliasList();
510 // for each command in the List
512 for ( Node
= (ALIAS_LIST
*)GetFirstNode(&List
->Link
)
513 ; !IsNull (&List
->Link
, &Node
->Link
)
514 ; Node
= (ALIAS_LIST
*)GetNextNode(&List
->Link
, &Node
->Link
)
517 // install the alias'
519 Status
= InternalSetAlias(Node
->CommandString
, Node
->Alias
, TRUE
);
520 ASSERT_EFI_ERROR(Status
);
522 return (EFI_SUCCESS
);
526 Internal function to determine if 2 command names are really the same.
528 @param[in] Command1 The pointer to the first command name.
529 @param[in] Command2 The pointer to the second command name.
531 @retval TRUE The 2 command names are the same.
532 @retval FALSE The 2 command names are not the same.
537 IN CONST CHAR16
*Command1
,
538 IN CONST CHAR16
*Command2
541 if (StringNoCaseCompare(&Command1
, &Command2
) == 0) {
548 Internal function to determine if a command is a script only command.
550 @param[in] CommandName The pointer to the command name.
552 @retval TRUE The command is a script only command.
553 @retval FALSE The command is not a script only command.
558 IN CONST CHAR16
*CommandName
561 if (IsCommand(CommandName
, L
"for")
562 ||IsCommand(CommandName
, L
"endfor")
563 ||IsCommand(CommandName
, L
"if")
564 ||IsCommand(CommandName
, L
"else")
565 ||IsCommand(CommandName
, L
"endif")
566 ||IsCommand(CommandName
, L
"goto")) {
573 This function will populate the 2 device path protocol parameters based on the
574 global gImageHandle. The DevPath will point to the device path for the handle that has
575 loaded image protocol installed on it. The FilePath will point to the device path
576 for the file that was loaded.
578 @param[in, out] DevPath On a sucessful return the device path to the loaded image.
579 @param[in, out] FilePath On a sucessful return the device path to the file.
581 @retval EFI_SUCCESS The 2 device paths were sucessfully returned.
582 @retval other A error from gBS->HandleProtocol.
588 GetDevicePathsForImageAndFile (
589 IN OUT EFI_DEVICE_PATH_PROTOCOL
**DevPath
,
590 IN OUT EFI_DEVICE_PATH_PROTOCOL
**FilePath
594 EFI_LOADED_IMAGE_PROTOCOL
*LoadedImage
;
595 EFI_DEVICE_PATH_PROTOCOL
*ImageDevicePath
;
597 ASSERT(DevPath
!= NULL
);
598 ASSERT(FilePath
!= NULL
);
600 Status
= gBS
->OpenProtocol (
602 &gEfiLoadedImageProtocolGuid
,
603 (VOID
**)&LoadedImage
,
606 EFI_OPEN_PROTOCOL_GET_PROTOCOL
608 if (!EFI_ERROR (Status
)) {
609 Status
= gBS
->OpenProtocol (
610 LoadedImage
->DeviceHandle
,
611 &gEfiDevicePathProtocolGuid
,
612 (VOID
**)&ImageDevicePath
,
615 EFI_OPEN_PROTOCOL_GET_PROTOCOL
617 if (!EFI_ERROR (Status
)) {
618 *DevPath
= DuplicateDevicePath (ImageDevicePath
);
619 *FilePath
= DuplicateDevicePath (LoadedImage
->FilePath
);
621 LoadedImage
->DeviceHandle
,
622 &gEfiDevicePathProtocolGuid
,
628 &gEfiLoadedImageProtocolGuid
,
635 STATIC CONST SHELL_PARAM_ITEM mShellParamList
[] = {
636 {L
"-nostartup", TypeFlag
},
637 {L
"-startup", TypeFlag
},
638 {L
"-noconsoleout", TypeFlag
},
639 {L
"-noconsolein", TypeFlag
},
640 {L
"-nointerrupt", TypeFlag
},
641 {L
"-nomap", TypeFlag
},
642 {L
"-noversion", TypeFlag
},
643 {L
"-startup", TypeFlag
},
644 {L
"-delay", TypeValue
},
645 {L
"-_exit", TypeFlag
},
650 Process all Uefi Shell 2.0 command line options.
652 see Uefi Shell 2.0 section 3.2 for full details.
654 the command line must resemble the following:
656 shell.efi [ShellOpt-options] [options] [file-name [file-name-options]]
658 ShellOpt-options Options which control the initialization behavior of the shell.
659 These options are read from the EFI global variable "ShellOpt"
660 and are processed before options or file-name.
662 options Options which control the initialization behavior of the shell.
664 file-name The name of a UEFI shell application or script to be executed
665 after initialization is complete. By default, if file-name is
666 specified, then -nostartup is implied. Scripts are not supported
669 file-name-options The command-line options that are passed to file-name when it
672 This will initialize the ShellInfoObject.ShellInitSettings global variable.
674 @retval EFI_SUCCESS The variable is initialized.
685 CONST CHAR16
*TempConst
;
688 CHAR16
*ProblemParam
;
694 Status
= ShellCommandLineParse (mShellParamList
, &Package
, NULL
, FALSE
);
698 TempConst
= ShellCommandLineGetRawValue(Package
, Count
++);
699 if (TempConst
!= NULL
&& StrLen(TempConst
)) {
700 ShellInfoObject
.ShellInitSettings
.FileName
= AllocateZeroPool(StrSize(TempConst
));
701 if (ShellInfoObject
.ShellInitSettings
.FileName
== NULL
) {
702 return (EFI_OUT_OF_RESOURCES
);
704 StrCpy(ShellInfoObject
.ShellInitSettings
.FileName
, TempConst
);
705 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoStartup
= 1;
706 for (LoopVar
= 0 ; LoopVar
< gEfiShellParametersProtocol
->Argc
; LoopVar
++) {
707 if (StrCmp(gEfiShellParametersProtocol
->Argv
[LoopVar
], ShellInfoObject
.ShellInitSettings
.FileName
)==0) {
710 // We found the file... add the rest of the params...
712 for ( ; LoopVar
< gEfiShellParametersProtocol
->Argc
; LoopVar
++) {
713 ASSERT((ShellInfoObject
.ShellInitSettings
.FileOptions
== NULL
&& Size
== 0) || (ShellInfoObject
.ShellInitSettings
.FileOptions
!= NULL
));
714 StrnCatGrow(&ShellInfoObject
.ShellInitSettings
.FileOptions
,
718 if (ShellInfoObject
.ShellInitSettings
.FileOptions
== NULL
) {
719 SHELL_FREE_NON_NULL(ShellInfoObject
.ShellInitSettings
.FileName
);
720 return (EFI_OUT_OF_RESOURCES
);
722 StrnCatGrow(&ShellInfoObject
.ShellInitSettings
.FileOptions
,
724 gEfiShellParametersProtocol
->Argv
[LoopVar
],
726 if (ShellInfoObject
.ShellInitSettings
.FileOptions
== NULL
) {
727 SHELL_FREE_NON_NULL(ShellInfoObject
.ShellInitSettings
.FileName
);
728 return (EFI_OUT_OF_RESOURCES
);
734 ShellCommandLineFreeVarList(Package
);
736 Status
= ShellCommandLineParse (mShellParamList
, &Package
, &ProblemParam
, FALSE
);
737 if (EFI_ERROR(Status
)) {
738 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_PROBLEM
), ShellInfoObject
.HiiHandle
, ProblemParam
);
739 FreePool(ProblemParam
);
740 ShellCommandLineFreeVarList(Package
);
741 return (EFI_INVALID_PARAMETER
);
745 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.Startup
= ShellCommandLineGetFlag(Package
, L
"-startup");
746 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoStartup
= ShellCommandLineGetFlag(Package
, L
"-nostartup");
747 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoConsoleOut
= ShellCommandLineGetFlag(Package
, L
"-noconsoleout");
748 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoConsoleIn
= ShellCommandLineGetFlag(Package
, L
"-noconsolein");
749 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoInterrupt
= ShellCommandLineGetFlag(Package
, L
"-nointerrupt");
750 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoMap
= ShellCommandLineGetFlag(Package
, L
"-nomap");
751 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoVersion
= ShellCommandLineGetFlag(Package
, L
"-noversion");
752 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.Delay
= ShellCommandLineGetFlag(Package
, L
"-delay");
753 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.Exit
= ShellCommandLineGetFlag(Package
, L
"-_exit");
755 ShellInfoObject
.ShellInitSettings
.Delay
= 5;
757 if (ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoInterrupt
) {
758 ShellInfoObject
.ShellInitSettings
.Delay
= 0;
759 } else if (ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.Delay
) {
760 TempConst
= ShellCommandLineGetValue(Package
, L
"-delay");
761 if (TempConst
!= NULL
&& *TempConst
== L
':') {
764 if (TempConst
!= NULL
&& !EFI_ERROR(ShellConvertStringToUint64(TempConst
, &Intermediate
, FALSE
, FALSE
))) {
765 ShellInfoObject
.ShellInitSettings
.Delay
= (UINTN
)Intermediate
;
768 ShellCommandLineFreeVarList(Package
);
774 Handles all interaction with the default startup script.
776 this will check that the correct command line parameters were passed, handle the delay, and then start running the script.
778 @param ImagePath the path to the image for shell. first place to look for the startup script
779 @param FilePath the path to the file for shell. second place to look for the startup script.
781 @retval EFI_SUCCESS the variable is initialized.
786 EFI_DEVICE_PATH_PROTOCOL
*ImagePath
,
787 EFI_DEVICE_PATH_PROTOCOL
*FilePath
793 SHELL_FILE_HANDLE FileHandle
;
794 EFI_DEVICE_PATH_PROTOCOL
*NewPath
;
795 EFI_DEVICE_PATH_PROTOCOL
*NamePath
;
796 CHAR16
*FileStringPath
;
799 CONST CHAR16
*MapName
;
801 Key
.UnicodeChar
= CHAR_NULL
;
805 if (!ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.Startup
&& ShellInfoObject
.ShellInitSettings
.FileName
!= NULL
) {
807 // launch something else instead
809 NewSize
= StrSize(ShellInfoObject
.ShellInitSettings
.FileName
);
810 if (ShellInfoObject
.ShellInitSettings
.FileOptions
!= NULL
) {
811 NewSize
+= StrSize(ShellInfoObject
.ShellInitSettings
.FileOptions
) + sizeof(CHAR16
);
813 FileStringPath
= AllocateZeroPool(NewSize
);
814 if (FileStringPath
== NULL
) {
815 return (EFI_OUT_OF_RESOURCES
);
817 StrCpy(FileStringPath
, ShellInfoObject
.ShellInitSettings
.FileName
);
818 if (ShellInfoObject
.ShellInitSettings
.FileOptions
!= NULL
) {
819 StrCat(FileStringPath
, L
" ");
820 StrCat(FileStringPath
, ShellInfoObject
.ShellInitSettings
.FileOptions
);
822 Status
= RunCommand(FileStringPath
);
823 FreePool(FileStringPath
);
829 // for shell level 0 we do no scripts
830 // Without the Startup bit overriding we allow for nostartup to prevent scripts
832 if ( (PcdGet8(PcdShellSupportLevel
) < 1)
833 || (ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoStartup
&& !ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.Startup
)
835 return (EFI_SUCCESS
);
838 gST
->ConOut
->EnableCursor(gST
->ConOut
, FALSE
);
840 // print out our warning and see if they press a key
842 for ( Status
= EFI_UNSUPPORTED
, Delay
= ShellInfoObject
.ShellInitSettings
.Delay
* 10
843 ; Delay
!= 0 && EFI_ERROR(Status
)
846 ShellPrintHiiEx(0, gST
->ConOut
->Mode
->CursorRow
, NULL
, STRING_TOKEN (STR_SHELL_STARTUP_QUESTION
), ShellInfoObject
.HiiHandle
, Delay
/10);
848 if (!ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoConsoleIn
) {
849 Status
= gST
->ConIn
->ReadKeyStroke (gST
->ConIn
, &Key
);
852 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_CRLF
), ShellInfoObject
.HiiHandle
);
853 gST
->ConOut
->EnableCursor(gST
->ConOut
, TRUE
);
858 if (Status
== EFI_SUCCESS
&& Key
.UnicodeChar
== 0 && Key
.ScanCode
== SCAN_ESC
) {
859 return (EFI_SUCCESS
);
863 // Try the first location (must be file system)
865 MapName
= ShellInfoObject
.NewEfiShellProtocol
->GetMapFromDevicePath(&ImagePath
);
866 if (MapName
!= NULL
) {
867 FileStringPath
= NULL
;
869 FileStringPath
= StrnCatGrow(&FileStringPath
, &NewSize
, MapName
, 0);
870 if (FileStringPath
== NULL
) {
871 Status
= EFI_OUT_OF_RESOURCES
;
873 TempSpot
= StrStr(FileStringPath
, L
";");
874 if (TempSpot
!= NULL
) {
875 *TempSpot
= CHAR_NULL
;
877 FileStringPath
= StrnCatGrow(&FileStringPath
, &NewSize
, ((FILEPATH_DEVICE_PATH
*)FilePath
)->PathName
, 0);
878 PathRemoveLastItem(FileStringPath
);
879 FileStringPath
= StrnCatGrow(&FileStringPath
, &NewSize
, mStartupScript
, 0);
880 Status
= ShellInfoObject
.NewEfiShellProtocol
->OpenFileByName(FileStringPath
, &FileHandle
, EFI_FILE_MODE_READ
);
881 FreePool(FileStringPath
);
884 if (EFI_ERROR(Status
)) {
885 NamePath
= FileDevicePath (NULL
, mStartupScript
);
886 NewPath
= AppendDevicePathNode (ImagePath
, NamePath
);
892 Status
= InternalOpenFileDevicePath(NewPath
, &FileHandle
, EFI_FILE_MODE_READ
, 0);
896 // If we got a file, run it
898 if (!EFI_ERROR(Status
) && FileHandle
!= NULL
) {
899 Status
= RunScriptFileHandle (FileHandle
, mStartupScript
);
900 ShellInfoObject
.NewEfiShellProtocol
->CloseFile(FileHandle
);
902 FileStringPath
= ShellFindFilePath(mStartupScript
);
903 if (FileStringPath
== NULL
) {
905 // we return success since we dont need to have a startup script
907 Status
= EFI_SUCCESS
;
908 ASSERT(FileHandle
== NULL
);
910 Status
= RunScriptFile(FileStringPath
);
911 FreePool(FileStringPath
);
920 Function to perform the shell prompt looping. It will do a single prompt,
921 dispatch the result, and then return. It is expected that the caller will
922 call this function in a loop many times.
925 @retval RETURN_ABORTED
936 CONST CHAR16
*CurDir
;
943 // Get screen setting to decide size of the command line buffer
945 gST
->ConOut
->QueryMode (gST
->ConOut
, gST
->ConOut
->Mode
->Mode
, &Column
, &Row
);
946 BufferSize
= Column
* Row
* sizeof (CHAR16
);
947 CmdLine
= AllocateZeroPool (BufferSize
);
948 if (CmdLine
== NULL
) {
949 return EFI_OUT_OF_RESOURCES
;
952 CurDir
= ShellInfoObject
.NewEfiShellProtocol
->GetEnv(L
"cwd");
957 gST
->ConOut
->SetCursorPosition (gST
->ConOut
, 0, gST
->ConOut
->Mode
->CursorRow
);
959 if (CurDir
!= NULL
&& StrLen(CurDir
) > 1) {
960 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_CURDIR
), ShellInfoObject
.HiiHandle
, CurDir
);
962 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_SHELL
), ShellInfoObject
.HiiHandle
);
966 // Read a line from the console
968 Status
= ShellInfoObject
.NewEfiShellProtocol
->ReadFile(ShellInfoObject
.NewShellParametersProtocol
->StdIn
, &BufferSize
, CmdLine
);
971 // Null terminate the string and parse it
973 if (!EFI_ERROR (Status
)) {
974 CmdLine
[BufferSize
/ sizeof (CHAR16
)] = CHAR_NULL
;
975 Status
= RunCommand(CmdLine
);
979 // Done with this command
986 Add a buffer to the Buffer To Free List for safely returning buffers to other
987 places without risking letting them modify internal shell information.
989 @param Buffer Something to pass to FreePool when the shell is exiting.
997 BUFFER_LIST
*BufferListEntry
;
999 if (Buffer
== NULL
) {
1003 BufferListEntry
= AllocateZeroPool(sizeof(BUFFER_LIST
));
1004 ASSERT(BufferListEntry
!= NULL
);
1005 BufferListEntry
->Buffer
= Buffer
;
1006 InsertTailList(&ShellInfoObject
.BufferToFreeList
.Link
, &BufferListEntry
->Link
);
1011 Add a buffer to the Line History List
1013 @param Buffer The line buffer to add.
1017 AddLineToCommandHistory(
1018 IN CONST CHAR16
*Buffer
1023 Node
= AllocateZeroPool(sizeof(BUFFER_LIST
));
1024 ASSERT(Node
!= NULL
);
1025 Node
->Buffer
= AllocateZeroPool(StrSize(Buffer
));
1026 ASSERT(Node
->Buffer
!= NULL
);
1027 StrCpy(Node
->Buffer
, Buffer
);
1029 InsertTailList(&ShellInfoObject
.ViewingSettings
.CommandHistory
.Link
, &Node
->Link
);
1033 Checks if a string is an alias for another command. If yes, then it replaces the alias name
1034 with the correct command name.
1036 @param[in, out] CommandString Upon entry the potential alias. Upon return the
1037 command name if it was an alias. If it was not
1038 an alias it will be unchanged. This function may
1039 change the buffer to fit the command name.
1041 @retval EFI_SUCCESS The name was changed.
1042 @retval EFI_SUCCESS The name was not an alias.
1043 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
1048 IN OUT CHAR16
**CommandString
1051 CONST CHAR16
*NewString
;
1053 NewString
= ShellInfoObject
.NewEfiShellProtocol
->GetAlias(*CommandString
, NULL
);
1054 if (NewString
== NULL
) {
1055 return (EFI_SUCCESS
);
1057 FreePool(*CommandString
);
1058 *CommandString
= AllocateZeroPool(StrSize(NewString
));
1059 if (*CommandString
== NULL
) {
1060 return (EFI_OUT_OF_RESOURCES
);
1062 StrCpy(*CommandString
, NewString
);
1063 return (EFI_SUCCESS
);
1067 Function allocates a new command line and replaces all instances of environment
1068 variable names that are correctly preset to their values.
1070 If the return value is not NULL the memory must be caller freed.
1072 @param[in] OriginalCommandLine The original command line
1074 @retval NULL An error ocurred.
1075 @return The new command line with no environment variables present.
1079 ShellConvertVariables (
1080 IN CONST CHAR16
*OriginalCommandLine
1083 CONST CHAR16
*MasterEnvList
;
1085 CHAR16
*NewCommandLine1
;
1086 CHAR16
*NewCommandLine2
;
1091 SCRIPT_FILE
*CurrentScriptFile
;
1092 ALIAS_LIST
*AliasListNode
;
1094 ASSERT(OriginalCommandLine
!= NULL
);
1097 NewSize
= StrSize(OriginalCommandLine
);
1098 CurrentScriptFile
= ShellCommandGetCurrentScriptFile();
1101 ///@todo update this to handle the %0 - %9 for scripting only (borrow from line 1256 area) ? ? ?
1104 // calculate the size required for the post-conversion string...
1106 if (CurrentScriptFile
!= NULL
) {
1107 for (AliasListNode
= (ALIAS_LIST
*)GetFirstNode(&CurrentScriptFile
->SubstList
)
1108 ; !IsNull(&CurrentScriptFile
->SubstList
, &AliasListNode
->Link
)
1109 ; AliasListNode
= (ALIAS_LIST
*)GetNextNode(&CurrentScriptFile
->SubstList
, &AliasListNode
->Link
)
1111 for (Temp
= StrStr(OriginalCommandLine
, AliasListNode
->Alias
)
1113 ; Temp
= StrStr(Temp
+1, AliasListNode
->Alias
)
1116 // we need a preceeding and if there is space no ^ preceeding (if no space ignore)
1118 if ((((Temp
-OriginalCommandLine
)>2) && *(Temp
-2) != L
'^') || ((Temp
-OriginalCommandLine
)<=2)) {
1119 NewSize
+= StrSize(AliasListNode
->CommandString
);
1125 for (MasterEnvList
= EfiShellGetEnv(NULL
)
1126 ; MasterEnvList
!= NULL
&& *MasterEnvList
!= CHAR_NULL
//&& *(MasterEnvList+1) != CHAR_NULL
1127 ; MasterEnvList
+= StrLen(MasterEnvList
) + 1
1129 if (StrSize(MasterEnvList
) > ItemSize
) {
1130 ItemSize
= StrSize(MasterEnvList
);
1132 for (Temp
= StrStr(OriginalCommandLine
, MasterEnvList
)
1134 ; Temp
= StrStr(Temp
+1, MasterEnvList
)
1137 // we need a preceeding and following % and if there is space no ^ preceeding (if no space ignore)
1139 if (*(Temp
-1) == L
'%' && *(Temp
+StrLen(MasterEnvList
)) == L
'%' &&
1140 ((((Temp
-OriginalCommandLine
)>2) && *(Temp
-2) != L
'^') || ((Temp
-OriginalCommandLine
)<=2))) {
1141 NewSize
+=StrSize(EfiShellGetEnv(MasterEnvList
));
1147 // now do the replacements...
1149 NewCommandLine1
= AllocateZeroPool(NewSize
);
1150 NewCommandLine2
= AllocateZeroPool(NewSize
);
1151 ItemTemp
= AllocateZeroPool(ItemSize
+(2*sizeof(CHAR16
)));
1152 if (NewCommandLine1
== NULL
|| NewCommandLine2
== NULL
|| ItemTemp
== NULL
) {
1153 SHELL_FREE_NON_NULL(NewCommandLine1
);
1154 SHELL_FREE_NON_NULL(NewCommandLine2
);
1155 SHELL_FREE_NON_NULL(ItemTemp
);
1158 StrCpy(NewCommandLine1
, OriginalCommandLine
);
1159 for (MasterEnvList
= EfiShellGetEnv(NULL
)
1160 ; MasterEnvList
!= NULL
&& *MasterEnvList
!= CHAR_NULL
//&& *(MasterEnvList+1) != CHAR_NULL
1161 ; MasterEnvList
+= StrLen(MasterEnvList
) + 1
1163 StrCpy(ItemTemp
, L
"%");
1164 StrCat(ItemTemp
, MasterEnvList
);
1165 StrCat(ItemTemp
, L
"%");
1166 ShellCopySearchAndReplace(NewCommandLine1
, NewCommandLine2
, NewSize
, ItemTemp
, EfiShellGetEnv(MasterEnvList
), TRUE
, FALSE
);
1167 StrCpy(NewCommandLine1
, NewCommandLine2
);
1169 if (CurrentScriptFile
!= NULL
) {
1170 for (AliasListNode
= (ALIAS_LIST
*)GetFirstNode(&CurrentScriptFile
->SubstList
)
1171 ; !IsNull(&CurrentScriptFile
->SubstList
, &AliasListNode
->Link
)
1172 ; AliasListNode
= (ALIAS_LIST
*)GetNextNode(&CurrentScriptFile
->SubstList
, &AliasListNode
->Link
)
1174 ShellCopySearchAndReplace(NewCommandLine1
, NewCommandLine2
, NewSize
, AliasListNode
->Alias
, AliasListNode
->CommandString
, TRUE
, FALSE
);
1175 StrCpy(NewCommandLine1
, NewCommandLine2
);
1179 // Remove non-existant environment variables in scripts only
1181 for (Temp
= NewCommandLine1
; Temp
!= NULL
; ) {
1182 Temp
= StrStr(Temp
, L
"%");
1186 while (*(Temp
- 1) == L
'^') {
1187 Temp
= StrStr(Temp
+ 1, L
"%");
1196 Temp2
= StrStr(Temp
+ 1, L
"%");
1197 if (Temp2
== NULL
) {
1200 while (*(Temp2
- 1) == L
'^') {
1201 Temp2
= StrStr(Temp2
+ 1, L
"%");
1202 if (Temp2
== NULL
) {
1206 if (Temp2
== NULL
) {
1211 CopyMem(Temp
, Temp2
, StrSize(Temp2
));
1217 // Now cleanup any straggler intentionally ignored "%" characters
1219 ShellCopySearchAndReplace(NewCommandLine1
, NewCommandLine2
, NewSize
, L
"^%", L
"%", TRUE
, FALSE
);
1220 StrCpy(NewCommandLine1
, NewCommandLine2
);
1222 FreePool(NewCommandLine2
);
1225 return (NewCommandLine1
);
1229 Internal function to run a command line with pipe usage.
1231 @param[in] CmdLine The pointer to the command line.
1232 @param[in] StdIn The pointer to the Standard input.
1233 @param[in] StdOut The pointer to the Standard output.
1235 @retval EFI_SUCCESS The split command is executed successfully.
1236 @retval other Some error occurs when executing the split command.
1241 IN CONST CHAR16
*CmdLine
,
1242 IN SHELL_FILE_HANDLE
*StdIn
,
1243 IN SHELL_FILE_HANDLE
*StdOut
1247 CHAR16
*NextCommandLine
;
1248 CHAR16
*OurCommandLine
;
1252 SHELL_FILE_HANDLE
*TempFileHandle
;
1255 ASSERT(StdOut
== NULL
);
1257 ASSERT(StrStr(CmdLine
, L
"|") != NULL
);
1259 Status
= EFI_SUCCESS
;
1260 NextCommandLine
= NULL
;
1261 OurCommandLine
= NULL
;
1265 NextCommandLine
= StrnCatGrow(&NextCommandLine
, &Size1
, StrStr(CmdLine
, L
"|")+1, 0);
1266 OurCommandLine
= StrnCatGrow(&OurCommandLine
, &Size2
, CmdLine
, StrStr(CmdLine
, L
"|") - CmdLine
);
1268 if (NextCommandLine
== NULL
|| OurCommandLine
== NULL
) {
1269 SHELL_FREE_NON_NULL(OurCommandLine
);
1270 SHELL_FREE_NON_NULL(NextCommandLine
);
1271 return (EFI_OUT_OF_RESOURCES
);
1272 } else if (StrStr(OurCommandLine
, L
"|") != NULL
|| Size1
== 0 || Size2
== 0) {
1273 SHELL_FREE_NON_NULL(OurCommandLine
);
1274 SHELL_FREE_NON_NULL(NextCommandLine
);
1275 return (EFI_INVALID_PARAMETER
);
1276 } else if (NextCommandLine
[0] != CHAR_NULL
&&
1277 NextCommandLine
[0] == L
'a' &&
1278 NextCommandLine
[1] == L
' '
1280 CopyMem(NextCommandLine
, NextCommandLine
+1, StrSize(NextCommandLine
) - sizeof(NextCommandLine
[0]));
1288 // make a SPLIT_LIST item and add to list
1290 Split
= AllocateZeroPool(sizeof(SPLIT_LIST
));
1291 ASSERT(Split
!= NULL
);
1292 Split
->SplitStdIn
= StdIn
;
1293 Split
->SplitStdOut
= ConvertEfiFileProtocolToShellHandle(CreateFileInterfaceMem(Unicode
), NULL
);
1294 ASSERT(Split
->SplitStdOut
!= NULL
);
1295 InsertHeadList(&ShellInfoObject
.SplitList
.Link
, &Split
->Link
);
1297 Status
= RunCommand(OurCommandLine
);
1300 // move the output from the first to the in to the second.
1302 TempFileHandle
= Split
->SplitStdOut
;
1303 if (Split
->SplitStdIn
== StdIn
) {
1304 Split
->SplitStdOut
= NULL
;
1306 Split
->SplitStdOut
= Split
->SplitStdIn
;
1308 Split
->SplitStdIn
= TempFileHandle
;
1309 ShellInfoObject
.NewEfiShellProtocol
->SetFilePosition(ConvertShellHandleToEfiFileProtocol(Split
->SplitStdIn
), 0);
1311 if (!EFI_ERROR(Status
)) {
1312 Status
= RunCommand(NextCommandLine
);
1316 // remove the top level from the ScriptList
1318 ASSERT((SPLIT_LIST
*)GetFirstNode(&ShellInfoObject
.SplitList
.Link
) == Split
);
1319 RemoveEntryList(&Split
->Link
);
1322 // Note that the original StdIn is now the StdOut...
1324 if (Split
->SplitStdOut
!= NULL
&& Split
->SplitStdOut
!= StdIn
) {
1325 ShellInfoObject
.NewEfiShellProtocol
->CloseFile(ConvertShellHandleToEfiFileProtocol(Split
->SplitStdOut
));
1327 if (Split
->SplitStdIn
!= NULL
) {
1328 ShellInfoObject
.NewEfiShellProtocol
->CloseFile(ConvertShellHandleToEfiFileProtocol(Split
->SplitStdIn
));
1332 FreePool(NextCommandLine
);
1333 FreePool(OurCommandLine
);
1339 Function will process and run a command line.
1341 This will determine if the command line represents an internal shell
1342 command or dispatch an external application.
1344 @param[in] CmdLine The command line to parse.
1346 @retval EFI_SUCCESS The command was completed.
1347 @retval EFI_ABORTED The command's operation was aborted.
1352 IN CONST CHAR16
*CmdLine
1356 EFI_STATUS StatusCode
;
1357 CHAR16
*CommandName
;
1358 SHELL_STATUS ShellStatus
;
1362 CHAR16 LeString
[11];
1363 CHAR16
*PostAliasCmdLine
;
1364 UINTN PostAliasSize
;
1365 CHAR16
*PostVariableCmdLine
;
1366 CHAR16
*CommandWithPath
;
1367 CONST EFI_DEVICE_PATH_PROTOCOL
*DevPath
;
1368 CONST CHAR16
*TempLocation
;
1369 CONST CHAR16
*TempLocation2
;
1370 SHELL_FILE_HANDLE OriginalStdIn
;
1371 SHELL_FILE_HANDLE OriginalStdOut
;
1372 SHELL_FILE_HANDLE OriginalStdErr
;
1373 SYSTEM_TABLE_INFO OriginalSystemTableInfo
;
1374 CHAR16
*TempLocation3
;
1377 CHAR16
*CleanOriginal
;
1380 ASSERT(CmdLine
!= NULL
);
1381 if (StrLen(CmdLine
) == 0) {
1382 return (EFI_SUCCESS
);
1386 PostVariableCmdLine
= NULL
;
1387 PostAliasCmdLine
= NULL
;
1388 CommandWithPath
= NULL
;
1390 Status
= EFI_SUCCESS
;
1391 CleanOriginal
= NULL
;
1394 CleanOriginal
= StrnCatGrow(&CleanOriginal
, NULL
, CmdLine
, 0);
1395 if (CleanOriginal
== NULL
) {
1396 return (EFI_OUT_OF_RESOURCES
);
1398 while (CleanOriginal
[StrLen(CleanOriginal
)-1] == L
' ') {
1399 CleanOriginal
[StrLen(CleanOriginal
)-1] = CHAR_NULL
;
1401 while (CleanOriginal
[0] == L
' ') {
1402 CopyMem(CleanOriginal
, CleanOriginal
+1, StrSize(CleanOriginal
) - sizeof(CleanOriginal
[0]));
1406 if (StrStr(CleanOriginal
, L
" ") == NULL
){
1407 StrnCatGrow(&CommandName
, NULL
, CleanOriginal
, 0);
1409 StrnCatGrow(&CommandName
, NULL
, CleanOriginal
, StrStr(CleanOriginal
, L
" ") - CleanOriginal
);
1412 ASSERT(PostAliasCmdLine
== NULL
);
1413 if (!ShellCommandIsCommandOnList(CommandName
)) {
1415 // Convert via alias
1417 Status
= ShellConvertAlias(&CommandName
);
1419 PostAliasCmdLine
= StrnCatGrow(&PostAliasCmdLine
, &PostAliasSize
, CommandName
, 0);
1420 PostAliasCmdLine
= StrnCatGrow(&PostAliasCmdLine
, &PostAliasSize
, StrStr(CleanOriginal
, L
" "), 0);
1421 ASSERT_EFI_ERROR(Status
);
1423 PostAliasCmdLine
= StrnCatGrow(&PostAliasCmdLine
, NULL
, CleanOriginal
, 0);
1426 if (CleanOriginal
!= NULL
) {
1427 FreePool(CleanOriginal
);
1428 CleanOriginal
= NULL
;
1431 if (CommandName
!= NULL
) {
1432 FreePool(CommandName
);
1436 PostVariableCmdLine
= ShellConvertVariables(PostAliasCmdLine
);
1439 // we can now free the modified by alias command line
1441 if (PostAliasCmdLine
!= NULL
) {
1442 FreePool(PostAliasCmdLine
);
1443 PostAliasCmdLine
= NULL
;
1446 if (PostVariableCmdLine
== NULL
) {
1447 return (EFI_OUT_OF_RESOURCES
);
1450 while (PostVariableCmdLine
[StrLen(PostVariableCmdLine
)-1] == L
' ') {
1451 PostVariableCmdLine
[StrLen(PostVariableCmdLine
)-1] = CHAR_NULL
;
1453 while (PostVariableCmdLine
[0] == L
' ') {
1454 CopyMem(PostVariableCmdLine
, PostVariableCmdLine
+1, StrSize(PostVariableCmdLine
) - sizeof(PostVariableCmdLine
[0]));
1458 // We dont do normal processing with a split command line (output from one command input to another)
1460 TempLocation3
= NULL
;
1461 if (StrStr(PostVariableCmdLine
, L
"|") != NULL
) {
1462 for (TempLocation3
= PostVariableCmdLine
; TempLocation3
!= NULL
&& *TempLocation3
!= CHAR_NULL
; TempLocation3
++) {
1463 if (*TempLocation3
== L
'^' && *(TempLocation3
+1) == L
'|') {
1465 } else if (*TempLocation3
== L
'|') {
1470 if (TempLocation3
!= NULL
&& *TempLocation3
!= CHAR_NULL
) {
1472 // are we in an existing split???
1474 if (!IsListEmpty(&ShellInfoObject
.SplitList
.Link
)) {
1475 Split
= (SPLIT_LIST
*)GetFirstNode(&ShellInfoObject
.SplitList
.Link
);
1478 if (Split
== NULL
) {
1479 Status
= RunSplitCommand(PostVariableCmdLine
, NULL
, NULL
);
1481 Status
= RunSplitCommand(PostVariableCmdLine
, Split
->SplitStdIn
, Split
->SplitStdOut
);
1483 if (EFI_ERROR(Status
)) {
1484 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_INVALID_SPLIT
), ShellInfoObject
.HiiHandle
, PostVariableCmdLine
);
1489 // If this is a mapped drive change handle that...
1491 if (PostVariableCmdLine
[(StrLen(PostVariableCmdLine
)-1)] == L
':' && StrStr(PostVariableCmdLine
, L
" ") == NULL
) {
1492 Status
= ShellInfoObject
.NewEfiShellProtocol
->SetCurDir(NULL
, PostVariableCmdLine
);
1493 if (EFI_ERROR(Status
)) {
1494 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_INVALID_MAPPING
), ShellInfoObject
.HiiHandle
, PostVariableCmdLine
);
1496 FreePool(PostVariableCmdLine
);
1500 ///@todo update this section to divide into 3 ways - run internal command, run split (above), and run an external file...
1501 /// We waste a lot of time doing processing like StdIn,StdOut,Argv,Argc for things that are external files...
1505 Status
= UpdateStdInStdOutStdErr(ShellInfoObject
.NewShellParametersProtocol
, PostVariableCmdLine
, &OriginalStdIn
, &OriginalStdOut
, &OriginalStdErr
, &OriginalSystemTableInfo
);
1506 if (EFI_ERROR(Status
)) {
1507 if (Status
== EFI_NOT_FOUND
) {
1508 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_REDUNDA_REDIR
), ShellInfoObject
.HiiHandle
);
1510 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_INVALID_REDIR
), ShellInfoObject
.HiiHandle
);
1513 while (PostVariableCmdLine
[StrLen(PostVariableCmdLine
)-1] == L
' ') {
1514 PostVariableCmdLine
[StrLen(PostVariableCmdLine
)-1] = CHAR_NULL
;
1516 while (PostVariableCmdLine
[0] == L
' ') {
1517 CopyMem(PostVariableCmdLine
, PostVariableCmdLine
+1, StrSize(PostVariableCmdLine
) - sizeof(PostVariableCmdLine
[0]));
1521 // get the argc and argv updated for internal commands
1523 Status
= UpdateArgcArgv(ShellInfoObject
.NewShellParametersProtocol
, PostVariableCmdLine
, &Argv
, &Argc
);
1524 ASSERT_EFI_ERROR(Status
);
1526 for (Count
= 0 ; Count
< ShellInfoObject
.NewShellParametersProtocol
->Argc
; Count
++) {
1527 if (StrStr(ShellInfoObject
.NewShellParametersProtocol
->Argv
[Count
], L
"-?") == ShellInfoObject
.NewShellParametersProtocol
->Argv
[Count
]
1528 || (ShellInfoObject
.NewShellParametersProtocol
->Argv
[0][0] == L
'?' && ShellInfoObject
.NewShellParametersProtocol
->Argv
[0][1] == CHAR_NULL
)
1531 // We need to redo the arguments since a parameter was -?
1532 // move them all down 1 to the end, then up one then replace the first with help
1534 FreePool(ShellInfoObject
.NewShellParametersProtocol
->Argv
[Count
]);
1535 ShellInfoObject
.NewShellParametersProtocol
->Argv
[Count
] = NULL
;
1536 for (Count2
= Count
; (Count2
+ 1) < ShellInfoObject
.NewShellParametersProtocol
->Argc
; Count2
++) {
1537 ShellInfoObject
.NewShellParametersProtocol
->Argv
[Count2
] = ShellInfoObject
.NewShellParametersProtocol
->Argv
[Count2
+1];
1539 ShellInfoObject
.NewShellParametersProtocol
->Argv
[Count2
] = NULL
;
1540 for (Count2
= ShellInfoObject
.NewShellParametersProtocol
->Argc
-1 ; Count2
> 0 ; Count2
--) {
1541 ShellInfoObject
.NewShellParametersProtocol
->Argv
[Count2
] = ShellInfoObject
.NewShellParametersProtocol
->Argv
[Count2
-1];
1543 ShellInfoObject
.NewShellParametersProtocol
->Argv
[0] = NULL
;
1544 ShellInfoObject
.NewShellParametersProtocol
->Argv
[0] = StrnCatGrow(&ShellInfoObject
.NewShellParametersProtocol
->Argv
[0], NULL
, L
"help", 0);
1552 if (ShellCommandIsCommandOnList(ShellInfoObject
.NewShellParametersProtocol
->Argv
[0])) {
1554 // Run the command (which was converted if it was an alias)
1556 if (!EFI_ERROR(Status
)) {
1557 Status
= ShellCommandRunCommandHandler(ShellInfoObject
.NewShellParametersProtocol
->Argv
[0], &ShellStatus
, &LastError
);
1558 ASSERT_EFI_ERROR(Status
);
1559 UnicodeSPrint(LeString
, sizeof(LeString
)*sizeof(LeString
[0]), L
"0x%08x", ShellStatus
);
1560 DEBUG_CODE(InternalEfiShellSetEnv(L
"DebugLasterror", LeString
, TRUE
););
1562 InternalEfiShellSetEnv(L
"Lasterror", LeString
, TRUE
);
1565 // Pass thru the exitcode from the app.
1567 if (ShellCommandGetExit()) {
1568 Status
= ShellStatus
;
1569 } else if (ShellStatus
!= 0 && IsScriptOnlyCommand(ShellInfoObject
.NewShellParametersProtocol
->Argv
[0])) {
1570 Status
= EFI_ABORTED
;
1575 // run an external file (or script)
1577 if (StrStr(ShellInfoObject
.NewShellParametersProtocol
->Argv
[0], L
":") != NULL
) {
1578 ASSERT (CommandWithPath
== NULL
);
1579 if (ShellIsFile(ShellInfoObject
.NewShellParametersProtocol
->Argv
[0]) == EFI_SUCCESS
) {
1580 CommandWithPath
= StrnCatGrow(&CommandWithPath
, NULL
, ShellInfoObject
.NewShellParametersProtocol
->Argv
[0], 0);
1583 if (CommandWithPath
== NULL
) {
1584 CommandWithPath
= ShellFindFilePathEx(ShellInfoObject
.NewShellParametersProtocol
->Argv
[0], mExecutableExtensions
);
1586 if (CommandWithPath
== NULL
|| ShellIsDirectory(CommandWithPath
) == EFI_SUCCESS
) {
1587 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_NOT_FOUND
), ShellInfoObject
.HiiHandle
, ShellInfoObject
.NewShellParametersProtocol
->Argv
[0]);
1590 // Check if it's a NSH (script) file.
1592 TempLocation
= CommandWithPath
+StrLen(CommandWithPath
)-4;
1593 TempLocation2
= mScriptExtension
;
1594 if ((StrLen(CommandWithPath
) > 4) && (StringNoCaseCompare((VOID
*)(&TempLocation
), (VOID
*)(&TempLocation2
)) == 0)) {
1595 Status
= RunScriptFile (CommandWithPath
);
1597 DevPath
= ShellInfoObject
.NewEfiShellProtocol
->GetDevicePathFromFilePath(CommandWithPath
);
1598 ASSERT(DevPath
!= NULL
);
1599 Status
= InternalShellExecuteDevicePath(
1602 PostVariableCmdLine
,
1608 // Updatet last error status.
1610 UnicodeSPrint(LeString
, sizeof(LeString
)*sizeof(LeString
[0]), L
"0x%08x", StatusCode
);
1611 DEBUG_CODE(InternalEfiShellSetEnv(L
"DebugLasterror", LeString
, TRUE
););
1612 InternalEfiShellSetEnv(L
"Lasterror", LeString
, TRUE
);
1618 // Print some error info.
1620 if (EFI_ERROR(Status
)) {
1621 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_ERROR
), ShellInfoObject
.HiiHandle
, (VOID
*)(Status
));
1624 CommandName
= StrnCatGrow(&CommandName
, NULL
, ShellInfoObject
.NewShellParametersProtocol
->Argv
[0], 0);
1626 RestoreArgcArgv(ShellInfoObject
.NewShellParametersProtocol
, &Argv
, &Argc
);
1628 RestoreStdInStdOutStdErr(ShellInfoObject
.NewShellParametersProtocol
, &OriginalStdIn
, &OriginalStdOut
, &OriginalStdErr
, &OriginalSystemTableInfo
);
1630 if (CommandName
!= NULL
) {
1631 if (ShellCommandGetCurrentScriptFile() != NULL
&& !IsScriptOnlyCommand(CommandName
)) {
1633 // if this is NOT a scipt only command return success so the script won't quit.
1634 // prevent killing the script - this is the only place where we know the actual command name (after alias and variable replacement...)
1636 Status
= EFI_SUCCESS
;
1641 SHELL_FREE_NON_NULL(CommandName
);
1642 SHELL_FREE_NON_NULL(CommandWithPath
);
1643 SHELL_FREE_NON_NULL(PostVariableCmdLine
);
1648 STATIC CONST UINT16 InvalidChars
[] = {L
'*', L
'?', L
'<', L
'>', L
'\\', L
'/', L
'\"', 0x0001, 0x0002};
1650 Function determins if the CommandName COULD be a valid command. It does not determine whether
1651 this is a valid command. It only checks for invalid characters.
1653 @param[in] CommandName The name to check
1655 @retval TRUE CommandName could be a command name
1656 @retval FALSE CommandName could not be a valid command name
1661 IN CONST CHAR16
*CommandName
1665 if (CommandName
== NULL
) {
1670 ; Count
< sizeof(InvalidChars
) / sizeof(InvalidChars
[0])
1673 if (ScanMem16(CommandName
, StrSize(CommandName
), InvalidChars
[Count
]) != NULL
) {
1681 Function to process a NSH script file via SHELL_FILE_HANDLE.
1683 @param[in] Handle The handle to the already opened file.
1684 @param[in] Name The name of the script file.
1686 @retval EFI_SUCCESS the script completed sucessfully
1690 RunScriptFileHandle (
1691 IN SHELL_FILE_HANDLE Handle
,
1692 IN CONST CHAR16
*Name
1696 SCRIPT_FILE
*NewScriptFile
;
1698 CHAR16
*CommandLine
;
1699 CHAR16
*CommandLine2
;
1700 CHAR16
*CommandLine3
;
1701 SCRIPT_COMMAND_LIST
*LastCommand
;
1703 BOOLEAN PreScriptEchoState
;
1704 BOOLEAN PreCommandEchoState
;
1705 CONST CHAR16
*CurDir
;
1707 CHAR16 LeString
[50];
1709 ASSERT(!ShellCommandGetScriptExit());
1711 PreScriptEchoState
= ShellCommandGetEchoState();
1713 NewScriptFile
= (SCRIPT_FILE
*)AllocateZeroPool(sizeof(SCRIPT_FILE
));
1714 if (NewScriptFile
== NULL
) {
1715 return (EFI_OUT_OF_RESOURCES
);
1721 ASSERT(NewScriptFile
->ScriptName
== NULL
);
1722 NewScriptFile
->ScriptName
= StrnCatGrow(&NewScriptFile
->ScriptName
, NULL
, Name
, 0);
1723 if (NewScriptFile
->ScriptName
== NULL
) {
1724 DeleteScriptFileStruct(NewScriptFile
);
1725 return (EFI_OUT_OF_RESOURCES
);
1729 // Save the parameters (used to replace %0 to %9 later on)
1731 NewScriptFile
->Argc
= ShellInfoObject
.NewShellParametersProtocol
->Argc
;
1732 if (NewScriptFile
->Argc
!= 0) {
1733 NewScriptFile
->Argv
= (CHAR16
**)AllocateZeroPool(NewScriptFile
->Argc
* sizeof(CHAR16
*));
1734 if (NewScriptFile
->Argv
== NULL
) {
1735 DeleteScriptFileStruct(NewScriptFile
);
1736 return (EFI_OUT_OF_RESOURCES
);
1738 for (LoopVar
= 0 ; LoopVar
< 10 && LoopVar
< NewScriptFile
->Argc
; LoopVar
++) {
1739 ASSERT(NewScriptFile
->Argv
[LoopVar
] == NULL
);
1740 NewScriptFile
->Argv
[LoopVar
] = StrnCatGrow(&NewScriptFile
->Argv
[LoopVar
], NULL
, ShellInfoObject
.NewShellParametersProtocol
->Argv
[LoopVar
], 0);
1741 if (NewScriptFile
->Argv
[LoopVar
] == NULL
) {
1742 DeleteScriptFileStruct(NewScriptFile
);
1743 return (EFI_OUT_OF_RESOURCES
);
1747 NewScriptFile
->Argv
= NULL
;
1750 InitializeListHead(&NewScriptFile
->CommandList
);
1751 InitializeListHead(&NewScriptFile
->SubstList
);
1754 // Now build the list of all script commands.
1757 while(!ShellFileHandleEof(Handle
)) {
1758 CommandLine
= ShellFileHandleReturnLine(Handle
, &Ascii
);
1760 if (CommandLine
== NULL
|| StrLen(CommandLine
) == 0) {
1763 NewScriptFile
->CurrentCommand
= AllocateZeroPool(sizeof(SCRIPT_COMMAND_LIST
));
1764 if (NewScriptFile
->CurrentCommand
== NULL
) {
1765 DeleteScriptFileStruct(NewScriptFile
);
1766 return (EFI_OUT_OF_RESOURCES
);
1769 NewScriptFile
->CurrentCommand
->Cl
= CommandLine
;
1770 NewScriptFile
->CurrentCommand
->Data
= NULL
;
1771 NewScriptFile
->CurrentCommand
->Line
= LineCount
;
1773 InsertTailList(&NewScriptFile
->CommandList
, &NewScriptFile
->CurrentCommand
->Link
);
1777 // Add this as the topmost script file
1779 ShellCommandSetNewScript (NewScriptFile
);
1782 // Now enumerate through the commands and run each one.
1784 CommandLine
= AllocateZeroPool(PcdGet16(PcdShellPrintBufferSize
));
1785 if (CommandLine
== NULL
) {
1786 DeleteScriptFileStruct(NewScriptFile
);
1787 return (EFI_OUT_OF_RESOURCES
);
1789 CommandLine2
= AllocateZeroPool(PcdGet16(PcdShellPrintBufferSize
));
1790 if (CommandLine2
== NULL
) {
1791 FreePool(CommandLine
);
1792 DeleteScriptFileStruct(NewScriptFile
);
1793 return (EFI_OUT_OF_RESOURCES
);
1796 for ( NewScriptFile
->CurrentCommand
= (SCRIPT_COMMAND_LIST
*)GetFirstNode(&NewScriptFile
->CommandList
)
1797 ; !IsNull(&NewScriptFile
->CommandList
, &NewScriptFile
->CurrentCommand
->Link
)
1798 ; // conditional increment in the body of the loop
1800 ASSERT(CommandLine2
!= NULL
);
1801 StrCpy(CommandLine2
, NewScriptFile
->CurrentCommand
->Cl
);
1804 // NULL out comments
1806 for (CommandLine3
= CommandLine2
; CommandLine3
!= NULL
&& *CommandLine3
!= CHAR_NULL
; CommandLine3
++) {
1807 if (*CommandLine3
== L
'^') {
1808 if (*(CommandLine3
+1) == L
'#' || *(CommandLine3
+1) == L
':') {
1809 CopyMem(CommandLine3
, CommandLine3
+1, StrSize(CommandLine3
) - sizeof(CommandLine3
[0]));
1811 } else if (*CommandLine3
== L
'#') {
1812 *CommandLine3
= CHAR_NULL
;
1816 if (CommandLine2
!= NULL
&& StrLen(CommandLine2
) >= 1) {
1818 // Due to variability in starting the find and replace action we need to have both buffers the same.
1820 StrCpy(CommandLine
, CommandLine2
);
1823 // Remove the %0 to %9 from the command line (if we have some arguments)
1825 if (NewScriptFile
->Argv
!= NULL
) {
1826 switch (NewScriptFile
->Argc
) {
1828 Status
= ShellCopySearchAndReplace(CommandLine2
, CommandLine
, PcdGet16 (PcdShellPrintBufferSize
), L
"%9", NewScriptFile
->Argv
[9], FALSE
, TRUE
);
1829 ASSERT_EFI_ERROR(Status
);
1831 Status
= ShellCopySearchAndReplace(CommandLine
, CommandLine2
, PcdGet16 (PcdShellPrintBufferSize
), L
"%8", NewScriptFile
->Argv
[8], FALSE
, TRUE
);
1832 ASSERT_EFI_ERROR(Status
);
1834 Status
= ShellCopySearchAndReplace(CommandLine2
, CommandLine
, PcdGet16 (PcdShellPrintBufferSize
), L
"%7", NewScriptFile
->Argv
[7], FALSE
, TRUE
);
1835 ASSERT_EFI_ERROR(Status
);
1837 Status
= ShellCopySearchAndReplace(CommandLine
, CommandLine2
, PcdGet16 (PcdShellPrintBufferSize
), L
"%6", NewScriptFile
->Argv
[6], FALSE
, TRUE
);
1838 ASSERT_EFI_ERROR(Status
);
1840 Status
= ShellCopySearchAndReplace(CommandLine2
, CommandLine
, PcdGet16 (PcdShellPrintBufferSize
), L
"%5", NewScriptFile
->Argv
[5], FALSE
, TRUE
);
1841 ASSERT_EFI_ERROR(Status
);
1843 Status
= ShellCopySearchAndReplace(CommandLine
, CommandLine2
, PcdGet16 (PcdShellPrintBufferSize
), L
"%4", NewScriptFile
->Argv
[4], FALSE
, TRUE
);
1844 ASSERT_EFI_ERROR(Status
);
1846 Status
= ShellCopySearchAndReplace(CommandLine2
, CommandLine
, PcdGet16 (PcdShellPrintBufferSize
), L
"%3", NewScriptFile
->Argv
[3], FALSE
, TRUE
);
1847 ASSERT_EFI_ERROR(Status
);
1849 Status
= ShellCopySearchAndReplace(CommandLine
, CommandLine2
, PcdGet16 (PcdShellPrintBufferSize
), L
"%2", NewScriptFile
->Argv
[2], FALSE
, TRUE
);
1850 ASSERT_EFI_ERROR(Status
);
1852 Status
= ShellCopySearchAndReplace(CommandLine2
, CommandLine
, PcdGet16 (PcdShellPrintBufferSize
), L
"%1", NewScriptFile
->Argv
[1], FALSE
, TRUE
);
1853 ASSERT_EFI_ERROR(Status
);
1855 Status
= ShellCopySearchAndReplace(CommandLine
, CommandLine2
, PcdGet16 (PcdShellPrintBufferSize
), L
"%0", NewScriptFile
->Argv
[0], FALSE
, TRUE
);
1856 ASSERT_EFI_ERROR(Status
);
1862 Status
= ShellCopySearchAndReplace(CommandLine2
, CommandLine
, PcdGet16 (PcdShellPrintBufferSize
), L
"%1", L
"\"\"", FALSE
, FALSE
);
1863 Status
= ShellCopySearchAndReplace(CommandLine
, CommandLine2
, PcdGet16 (PcdShellPrintBufferSize
), L
"%2", L
"\"\"", FALSE
, FALSE
);
1864 Status
= ShellCopySearchAndReplace(CommandLine2
, CommandLine
, PcdGet16 (PcdShellPrintBufferSize
), L
"%3", L
"\"\"", FALSE
, FALSE
);
1865 Status
= ShellCopySearchAndReplace(CommandLine
, CommandLine2
, PcdGet16 (PcdShellPrintBufferSize
), L
"%4", L
"\"\"", FALSE
, FALSE
);
1866 Status
= ShellCopySearchAndReplace(CommandLine2
, CommandLine
, PcdGet16 (PcdShellPrintBufferSize
), L
"%5", L
"\"\"", FALSE
, FALSE
);
1867 Status
= ShellCopySearchAndReplace(CommandLine
, CommandLine2
, PcdGet16 (PcdShellPrintBufferSize
), L
"%6", L
"\"\"", FALSE
, FALSE
);
1868 Status
= ShellCopySearchAndReplace(CommandLine2
, CommandLine
, PcdGet16 (PcdShellPrintBufferSize
), L
"%7", L
"\"\"", FALSE
, FALSE
);
1869 Status
= ShellCopySearchAndReplace(CommandLine
, CommandLine2
, PcdGet16 (PcdShellPrintBufferSize
), L
"%8", L
"\"\"", FALSE
, FALSE
);
1870 Status
= ShellCopySearchAndReplace(CommandLine2
, CommandLine
, PcdGet16 (PcdShellPrintBufferSize
), L
"%9", L
"\"\"", FALSE
, FALSE
);
1872 StrCpy(CommandLine2
, CommandLine
);
1874 LastCommand
= NewScriptFile
->CurrentCommand
;
1876 for (CommandLine3
= CommandLine2
; CommandLine3
[0] == L
' ' ; CommandLine3
++);
1878 if (CommandLine3
!= NULL
&& CommandLine3
[0] == L
':' ) {
1880 // This line is a goto target / label
1883 if (CommandLine3
!= NULL
&& StrLen(CommandLine3
) > 0) {
1884 if (CommandLine3
[0] == L
'@') {
1886 // We need to save the current echo state
1887 // and disable echo for just this command.
1889 PreCommandEchoState
= ShellCommandGetEchoState();
1890 ShellCommandSetEchoState(FALSE
);
1891 Status
= RunCommand(CommandLine3
+1);
1894 // Now restore the pre-'@' echo state.
1896 ShellCommandSetEchoState(PreCommandEchoState
);
1898 if (ShellCommandGetEchoState()) {
1899 CurDir
= ShellInfoObject
.NewEfiShellProtocol
->GetEnv(L
"cwd");
1900 if (CurDir
!= NULL
&& StrLen(CurDir
) > 1) {
1901 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_CURDIR
), ShellInfoObject
.HiiHandle
, CurDir
);
1903 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_SHELL
), ShellInfoObject
.HiiHandle
);
1905 ShellPrintEx(-1, -1, L
"%s\r\n", CommandLine2
);
1907 Status
= RunCommand(CommandLine3
);
1911 if (ShellCommandGetScriptExit()) {
1912 UnicodeSPrint(LeString
, sizeof(LeString
)*sizeof(LeString
[0]), L
"0x%Lx", ShellCommandGetExitCode());
1913 DEBUG_CODE(InternalEfiShellSetEnv(L
"DebugLasterror", LeString
, TRUE
););
1914 InternalEfiShellSetEnv(L
"Lasterror", LeString
, TRUE
);
1916 ShellCommandRegisterExit(FALSE
, 0);
1917 Status
= EFI_SUCCESS
;
1920 if (ShellGetExecutionBreakFlag()) {
1923 if (EFI_ERROR(Status
)) {
1926 if (ShellCommandGetExit()) {
1931 // If that commend did not update the CurrentCommand then we need to advance it...
1933 if (LastCommand
== NewScriptFile
->CurrentCommand
) {
1934 NewScriptFile
->CurrentCommand
= (SCRIPT_COMMAND_LIST
*)GetNextNode(&NewScriptFile
->CommandList
, &NewScriptFile
->CurrentCommand
->Link
);
1935 if (!IsNull(&NewScriptFile
->CommandList
, &NewScriptFile
->CurrentCommand
->Link
)) {
1936 NewScriptFile
->CurrentCommand
->Reset
= TRUE
;
1940 NewScriptFile
->CurrentCommand
= (SCRIPT_COMMAND_LIST
*)GetNextNode(&NewScriptFile
->CommandList
, &NewScriptFile
->CurrentCommand
->Link
);
1941 if (!IsNull(&NewScriptFile
->CommandList
, &NewScriptFile
->CurrentCommand
->Link
)) {
1942 NewScriptFile
->CurrentCommand
->Reset
= TRUE
;
1948 FreePool(CommandLine
);
1949 FreePool(CommandLine2
);
1950 ShellCommandSetNewScript (NULL
);
1953 // Only if this was the last script reset the state.
1955 if (ShellCommandGetCurrentScriptFile()==NULL
) {
1956 ShellCommandSetEchoState(PreScriptEchoState
);
1958 return (EFI_SUCCESS
);
1962 Function to process a NSH script file.
1964 @param[in] ScriptPath Pointer to the script file name (including file system path).
1966 @retval EFI_SUCCESS the script completed sucessfully
1971 IN CONST CHAR16
*ScriptPath
1975 SHELL_FILE_HANDLE FileHandle
;
1977 if (ShellIsFile(ScriptPath
) != EFI_SUCCESS
) {
1978 return (EFI_INVALID_PARAMETER
);
1981 Status
= ShellOpenFileByName(ScriptPath
, &FileHandle
, EFI_FILE_MODE_READ
, 0);
1982 if (EFI_ERROR(Status
)) {
1986 Status
= RunScriptFileHandle(FileHandle
, ScriptPath
);
1988 ShellCloseFile(&FileHandle
);