2 This is THE shell (application)
4 Copyright (c) 2009 - 2013, 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
= RunScriptFileHandle (FileHandle
, mStartupScript
);
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
);
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
);
1599 IN CONST CHAR16
*CmdLine
1603 CHAR16
*FirstParameter
;
1609 Temp
= StrnCatGrow(&Temp
, NULL
, CmdLine
, 0);
1611 return (EFI_OUT_OF_RESOURCES
);
1614 FirstParameter
= StrStr(Temp
, L
"|");
1615 if (FirstParameter
!= NULL
) {
1616 *FirstParameter
= CHAR_NULL
;
1619 FirstParameter
= NULL
;
1622 // Process the command line
1624 Status
= ProcessCommandLineToFinal(&Temp
);
1626 if (!EFI_ERROR(Status
)) {
1627 FirstParameter
= AllocateZeroPool(StrSize(CmdLine
));
1628 if (FirstParameter
== NULL
) {
1629 SHELL_FREE_NON_NULL(Temp
);
1630 return (EFI_OUT_OF_RESOURCES
);
1632 TempWalker
= (CHAR16
*)Temp
;
1633 GetNextParameter(&TempWalker
, &FirstParameter
);
1635 if (GetOperationType(FirstParameter
) == Unknown_Invalid
) {
1636 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_NOT_FOUND
), ShellInfoObject
.HiiHandle
, FirstParameter
);
1637 SetLastError(SHELL_NOT_FOUND
);
1638 Status
= EFI_NOT_FOUND
;
1642 SHELL_FREE_NON_NULL(Temp
);
1643 SHELL_FREE_NON_NULL(FirstParameter
);
1648 Determine if a command line contains with a split contains only valid commands.
1650 @param[in] CmdLine The command line to parse.
1652 @retval EFI_SUCCESS CmdLine has only valid commands, application, or has no split.
1653 @retval EFI_ABORTED CmdLine has at least one invalid command or application.
1658 IN CONST CHAR16
*CmdLine
1661 CONST CHAR16
*TempSpot
;
1665 // Verify up to the pipe or end character
1667 Status
= IsValidSplit(CmdLine
);
1668 if (EFI_ERROR(Status
)) {
1673 // If this was the only item, then get out
1675 if (!ContainsSplit(CmdLine
)) {
1676 return (EFI_SUCCESS
);
1680 // recurse to verify the next item
1682 TempSpot
= FindSplit(CmdLine
)+1;
1683 return (VerifySplit(TempSpot
));
1687 Process a split based operation.
1689 @param[in] CmdLine pointer to the command line to process
1691 @retval EFI_SUCCESS The operation was successful
1692 @return an error occured.
1696 ProcessNewSplitCommandLine(
1697 IN CONST CHAR16
*CmdLine
1703 Status
= VerifySplit(CmdLine
);
1704 if (EFI_ERROR(Status
)) {
1711 // are we in an existing split???
1713 if (!IsListEmpty(&ShellInfoObject
.SplitList
.Link
)) {
1714 Split
= (SPLIT_LIST
*)GetFirstNode(&ShellInfoObject
.SplitList
.Link
);
1717 if (Split
== NULL
) {
1718 Status
= RunSplitCommand(CmdLine
, NULL
, NULL
);
1720 Status
= RunSplitCommand(CmdLine
, Split
->SplitStdIn
, Split
->SplitStdOut
);
1722 if (EFI_ERROR(Status
)) {
1723 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_INVALID_SPLIT
), ShellInfoObject
.HiiHandle
, CmdLine
);
1729 Handle a request to change the current file system.
1731 @param[in] CmdLine The passed in command line.
1733 @retval EFI_SUCCESS The operation was successful.
1738 IN CONST CHAR16
*CmdLine
1742 Status
= EFI_SUCCESS
;
1745 // make sure we are the right operation
1747 ASSERT(CmdLine
[(StrLen(CmdLine
)-1)] == L
':' && StrStr(CmdLine
, L
" ") == NULL
);
1750 // Call the protocol API to do the work
1752 Status
= ShellInfoObject
.NewEfiShellProtocol
->SetCurDir(NULL
, CmdLine
);
1755 // Report any errors
1757 if (EFI_ERROR(Status
)) {
1758 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_INVALID_MAPPING
), ShellInfoObject
.HiiHandle
, CmdLine
);
1765 Reprocess the command line to direct all -? to the help command.
1767 if found, will add "help" as argv[0], and move the rest later.
1769 @param[in,out] CmdLine pointer to the command line to update
1774 IN OUT CHAR16
**CmdLine
1777 CHAR16
*CurrentParameter
;
1780 CHAR16
*NewCommandLine
;
1783 Status
= EFI_SUCCESS
;
1785 CurrentParameter
= AllocateZeroPool(StrSize(*CmdLine
));
1786 if (CurrentParameter
== NULL
) {
1787 return (EFI_OUT_OF_RESOURCES
);
1791 while(Walker
!= NULL
&& *Walker
!= CHAR_NULL
) {
1792 LastWalker
= Walker
;
1793 GetNextParameter(&Walker
, &CurrentParameter
);
1794 if (StrStr(CurrentParameter
, L
"-?") == CurrentParameter
) {
1795 LastWalker
[0] = L
' ';
1796 LastWalker
[1] = L
' ';
1797 NewCommandLine
= AllocateZeroPool(StrSize(L
"help ") + StrSize(*CmdLine
));
1798 if (NewCommandLine
== NULL
) {
1799 Status
= EFI_OUT_OF_RESOURCES
;
1802 StrCpy(NewCommandLine
, L
"help ");
1803 StrCat(NewCommandLine
, *CmdLine
);
1804 SHELL_FREE_NON_NULL(*CmdLine
);
1805 *CmdLine
= NewCommandLine
;
1810 SHELL_FREE_NON_NULL(CurrentParameter
);
1816 Function to update the shell variable "lasterror".
1818 @param[in] ErrorCode the error code to put into lasterror.
1823 IN CONST SHELL_STATUS ErrorCode
1826 CHAR16 LeString
[19];
1827 if (sizeof(EFI_STATUS
) == sizeof(UINT64
)) {
1828 UnicodeSPrint(LeString
, sizeof(LeString
), L
"0x%Lx", ErrorCode
);
1830 UnicodeSPrint(LeString
, sizeof(LeString
), L
"0x%x", ErrorCode
);
1832 DEBUG_CODE(InternalEfiShellSetEnv(L
"debuglasterror", LeString
, TRUE
););
1833 InternalEfiShellSetEnv(L
"lasterror", LeString
, TRUE
);
1835 return (EFI_SUCCESS
);
1839 Converts the command line to it's post-processed form. this replaces variables and alias' per UEFI Shell spec.
1841 @param[in,out] CmdLine pointer to the command line to update
1843 @retval EFI_SUCCESS The operation was successful
1844 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
1845 @return some other error occured
1849 ProcessCommandLineToFinal(
1850 IN OUT CHAR16
**CmdLine
1854 TrimSpaces(CmdLine
);
1856 Status
= ShellSubstituteAliases(CmdLine
);
1857 if (EFI_ERROR(Status
)) {
1861 TrimSpaces(CmdLine
);
1863 Status
= ShellSubstituteVariables(CmdLine
);
1864 if (EFI_ERROR(Status
)) {
1868 TrimSpaces(CmdLine
);
1871 // update for help parsing
1873 if (StrStr(*CmdLine
, L
"?") != NULL
) {
1875 // This may do nothing if the ? does not indicate help.
1876 // Save all the details for in the API below.
1878 Status
= DoHelpUpdate(CmdLine
);
1881 TrimSpaces(CmdLine
);
1883 return (EFI_SUCCESS
);
1887 Run an internal shell command.
1889 This API will upadate the shell's environment since these commands are libraries.
1891 @param[in] CmdLine the command line to run.
1892 @param[in] FirstParameter the first parameter on the command line
1893 @param[in] ParamProtocol the shell parameters protocol pointer
1895 @retval EFI_SUCCESS The command was completed.
1896 @retval EFI_ABORTED The command's operation was aborted.
1901 IN CONST CHAR16
*CmdLine
,
1902 IN CHAR16
*FirstParameter
,
1903 IN EFI_SHELL_PARAMETERS_PROTOCOL
*ParamProtocol
1909 SHELL_STATUS CommandReturnedStatus
;
1913 // get the argc and argv updated for internal commands
1915 Status
= UpdateArgcArgv(ParamProtocol
, CmdLine
, &Argv
, &Argc
);
1916 if (!EFI_ERROR(Status
)) {
1918 // Run the internal command.
1920 Status
= ShellCommandRunCommandHandler(FirstParameter
, &CommandReturnedStatus
, &LastError
);
1922 if (!EFI_ERROR(Status
)) {
1924 // Update last error status.
1925 // some commands do not update last error.
1928 SetLastError(CommandReturnedStatus
);
1932 // Pass thru the exitcode from the app.
1934 if (ShellCommandGetExit()) {
1936 // An Exit was requested ("exit" command), pass its value up.
1938 Status
= CommandReturnedStatus
;
1939 } else if (CommandReturnedStatus
!= SHELL_SUCCESS
&& IsScriptOnlyCommand(FirstParameter
)) {
1941 // Always abort when a script only command fails for any reason
1943 Status
= EFI_ABORTED
;
1944 } else if (ShellCommandGetCurrentScriptFile() != NULL
&& CommandReturnedStatus
== SHELL_ABORTED
) {
1946 // Abort when in a script and a command aborted
1948 Status
= EFI_ABORTED
;
1954 // This is guarenteed to be called after UpdateArgcArgv no matter what else happened.
1955 // This is safe even if the update API failed. In this case, it may be a no-op.
1957 RestoreArgcArgv(ParamProtocol
, &Argv
, &Argc
);
1960 // If a script is running and the command is not a scipt only command, then
1961 // change return value to success so the script won't halt (unless aborted).
1963 // Script only commands have to be able halt the script since the script will
1964 // not operate if they are failing.
1966 if ( ShellCommandGetCurrentScriptFile() != NULL
1967 && !IsScriptOnlyCommand(FirstParameter
)
1968 && Status
!= EFI_ABORTED
1970 Status
= EFI_SUCCESS
;
1977 Function to run the command or file.
1979 @param[in] Type the type of operation being run.
1980 @param[in] CmdLine the command line to run.
1981 @param[in] FirstParameter the first parameter on the command line
1982 @param[in] ParamProtocol the shell parameters protocol pointer
1984 @retval EFI_SUCCESS The command was completed.
1985 @retval EFI_ABORTED The command's operation was aborted.
1990 IN SHELL_OPERATION_TYPES Type
,
1991 IN CONST CHAR16
*CmdLine
,
1992 IN CHAR16
*FirstParameter
,
1993 IN EFI_SHELL_PARAMETERS_PROTOCOL
*ParamProtocol
1997 EFI_STATUS StatusCode
;
1998 CHAR16
*CommandWithPath
;
1999 EFI_DEVICE_PATH_PROTOCOL
*DevPath
;
2001 Status
= EFI_SUCCESS
;
2002 CommandWithPath
= NULL
;
2006 case Internal_Command
:
2007 Status
= RunInternalCommand(CmdLine
, FirstParameter
, ParamProtocol
);
2009 case Script_File_Name
:
2010 case Efi_Application
:
2012 // Process a fully qualified path
2014 if (StrStr(FirstParameter
, L
":") != NULL
) {
2015 ASSERT (CommandWithPath
== NULL
);
2016 if (ShellIsFile(FirstParameter
) == EFI_SUCCESS
) {
2017 CommandWithPath
= StrnCatGrow(&CommandWithPath
, NULL
, FirstParameter
, 0);
2022 // Process a relative path and also check in the path environment variable
2024 if (CommandWithPath
== NULL
) {
2025 CommandWithPath
= ShellFindFilePathEx(FirstParameter
, mExecutableExtensions
);
2029 // This should be impossible now.
2031 ASSERT(CommandWithPath
!= NULL
);
2034 // Make sure that path is not just a directory (or not found)
2036 if (!EFI_ERROR(ShellIsDirectory(CommandWithPath
))) {
2037 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_NOT_FOUND
), ShellInfoObject
.HiiHandle
, FirstParameter
);
2038 SetLastError(SHELL_NOT_FOUND
);
2041 case Script_File_Name
:
2042 Status
= RunScriptFile (CommandWithPath
);
2044 case Efi_Application
:
2046 // Get the device path of the application image
2048 DevPath
= ShellInfoObject
.NewEfiShellProtocol
->GetDevicePathFromFilePath(CommandWithPath
);
2049 if (DevPath
== NULL
){
2050 Status
= EFI_OUT_OF_RESOURCES
;
2055 // Execute the device path
2057 Status
= InternalShellExecuteDevicePath(
2065 SHELL_FREE_NON_NULL(DevPath
);
2068 // Update last error status.
2070 SetLastError((SHELL_STATUS
) StatusCode
);
2086 SHELL_FREE_NON_NULL(CommandWithPath
);
2092 Function to setup StdIn, StdErr, StdOut, and then run the command or file.
2094 @param[in] Type the type of operation being run.
2095 @param[in] CmdLine the command line to run.
2096 @param[in] FirstParameter the first parameter on the command line.
2097 @param[in] ParamProtocol the shell parameters protocol pointer
2099 @retval EFI_SUCCESS The command was completed.
2100 @retval EFI_ABORTED The command's operation was aborted.
2104 SetupAndRunCommandOrFile(
2105 IN SHELL_OPERATION_TYPES Type
,
2107 IN CHAR16
*FirstParameter
,
2108 IN EFI_SHELL_PARAMETERS_PROTOCOL
*ParamProtocol
2112 SHELL_FILE_HANDLE OriginalStdIn
;
2113 SHELL_FILE_HANDLE OriginalStdOut
;
2114 SHELL_FILE_HANDLE OriginalStdErr
;
2115 SYSTEM_TABLE_INFO OriginalSystemTableInfo
;
2118 // Update the StdIn, StdOut, and StdErr for redirection to environment variables, files, etc... unicode and ASCII
2120 Status
= UpdateStdInStdOutStdErr(ParamProtocol
, CmdLine
, &OriginalStdIn
, &OriginalStdOut
, &OriginalStdErr
, &OriginalSystemTableInfo
);
2123 // The StdIn, StdOut, and StdErr are set up.
2124 // Now run the command, script, or application
2126 if (!EFI_ERROR(Status
)) {
2127 Status
= RunCommandOrFile(Type
, CmdLine
, FirstParameter
, ParamProtocol
);
2133 if (EFI_ERROR(Status
)) {
2134 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_ERROR
), ShellInfoObject
.HiiHandle
, (VOID
*)(Status
));
2138 // put back the original StdIn, StdOut, and StdErr
2140 RestoreStdInStdOutStdErr(ParamProtocol
, &OriginalStdIn
, &OriginalStdOut
, &OriginalStdErr
, &OriginalSystemTableInfo
);
2146 Function will process and run a command line.
2148 This will determine if the command line represents an internal shell
2149 command or dispatch an external application.
2151 @param[in] CmdLine The command line to parse.
2153 @retval EFI_SUCCESS The command was completed.
2154 @retval EFI_ABORTED The command's operation was aborted.
2159 IN CONST CHAR16
*CmdLine
2163 CHAR16
*CleanOriginal
;
2164 CHAR16
*FirstParameter
;
2166 SHELL_OPERATION_TYPES Type
;
2168 ASSERT(CmdLine
!= NULL
);
2169 if (StrLen(CmdLine
) == 0) {
2170 return (EFI_SUCCESS
);
2173 Status
= EFI_SUCCESS
;
2174 CleanOriginal
= NULL
;
2176 CleanOriginal
= StrnCatGrow(&CleanOriginal
, NULL
, CmdLine
, 0);
2177 if (CleanOriginal
== NULL
) {
2178 return (EFI_OUT_OF_RESOURCES
);
2181 TrimSpaces(&CleanOriginal
);
2184 // Handle case that passed in command line is just 1 or more " " characters.
2186 if (StrLen (CleanOriginal
) == 0) {
2187 SHELL_FREE_NON_NULL(CleanOriginal
);
2188 return (EFI_SUCCESS
);
2191 Status
= ProcessCommandLineToFinal(&CleanOriginal
);
2192 if (EFI_ERROR(Status
)) {
2193 SHELL_FREE_NON_NULL(CleanOriginal
);
2198 // We dont do normal processing with a split command line (output from one command input to another)
2200 if (ContainsSplit(CleanOriginal
)) {
2201 Status
= ProcessNewSplitCommandLine(CleanOriginal
);
2202 SHELL_FREE_NON_NULL(CleanOriginal
);
2207 // We need the first parameter information so we can determine the operation type
2209 FirstParameter
= AllocateZeroPool(StrSize(CleanOriginal
));
2210 if (FirstParameter
== NULL
) {
2211 SHELL_FREE_NON_NULL(CleanOriginal
);
2212 return (EFI_OUT_OF_RESOURCES
);
2214 TempWalker
= CleanOriginal
;
2215 GetNextParameter(&TempWalker
, &FirstParameter
);
2218 // Depending on the first parameter we change the behavior
2220 switch (Type
= GetOperationType(FirstParameter
)) {
2221 case File_Sys_Change
:
2222 Status
= ChangeMappedDrive(CleanOriginal
);
2224 case Internal_Command
:
2225 case Script_File_Name
:
2226 case Efi_Application
:
2227 Status
= SetupAndRunCommandOrFile(Type
, CleanOriginal
, FirstParameter
, ShellInfoObject
.NewShellParametersProtocol
);
2231 // Whatever was typed, it was invalid.
2233 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_NOT_FOUND
), ShellInfoObject
.HiiHandle
, FirstParameter
);
2234 SetLastError(SHELL_NOT_FOUND
);
2238 SHELL_FREE_NON_NULL(CleanOriginal
);
2239 SHELL_FREE_NON_NULL(FirstParameter
);
2244 STATIC CONST UINT16 InvalidChars
[] = {L
'*', L
'?', L
'<', L
'>', L
'\\', L
'/', L
'\"', 0x0001, 0x0002};
2246 Function determins if the CommandName COULD be a valid command. It does not determine whether
2247 this is a valid command. It only checks for invalid characters.
2249 @param[in] CommandName The name to check
2251 @retval TRUE CommandName could be a command name
2252 @retval FALSE CommandName could not be a valid command name
2257 IN CONST CHAR16
*CommandName
2261 if (CommandName
== NULL
) {
2266 ; Count
< sizeof(InvalidChars
) / sizeof(InvalidChars
[0])
2269 if (ScanMem16(CommandName
, StrSize(CommandName
), InvalidChars
[Count
]) != NULL
) {
2277 Function to process a NSH script file via SHELL_FILE_HANDLE.
2279 @param[in] Handle The handle to the already opened file.
2280 @param[in] Name The name of the script file.
2282 @retval EFI_SUCCESS the script completed sucessfully
2286 RunScriptFileHandle (
2287 IN SHELL_FILE_HANDLE Handle
,
2288 IN CONST CHAR16
*Name
2292 SCRIPT_FILE
*NewScriptFile
;
2294 CHAR16
*CommandLine
;
2295 CHAR16
*CommandLine2
;
2296 CHAR16
*CommandLine3
;
2297 SCRIPT_COMMAND_LIST
*LastCommand
;
2299 BOOLEAN PreScriptEchoState
;
2300 BOOLEAN PreCommandEchoState
;
2301 CONST CHAR16
*CurDir
;
2303 CHAR16 LeString
[50];
2305 ASSERT(!ShellCommandGetScriptExit());
2307 PreScriptEchoState
= ShellCommandGetEchoState();
2309 NewScriptFile
= (SCRIPT_FILE
*)AllocateZeroPool(sizeof(SCRIPT_FILE
));
2310 if (NewScriptFile
== NULL
) {
2311 return (EFI_OUT_OF_RESOURCES
);
2317 ASSERT(NewScriptFile
->ScriptName
== NULL
);
2318 NewScriptFile
->ScriptName
= StrnCatGrow(&NewScriptFile
->ScriptName
, NULL
, Name
, 0);
2319 if (NewScriptFile
->ScriptName
== NULL
) {
2320 DeleteScriptFileStruct(NewScriptFile
);
2321 return (EFI_OUT_OF_RESOURCES
);
2325 // Save the parameters (used to replace %0 to %9 later on)
2327 NewScriptFile
->Argc
= ShellInfoObject
.NewShellParametersProtocol
->Argc
;
2328 if (NewScriptFile
->Argc
!= 0) {
2329 NewScriptFile
->Argv
= (CHAR16
**)AllocateZeroPool(NewScriptFile
->Argc
* sizeof(CHAR16
*));
2330 if (NewScriptFile
->Argv
== NULL
) {
2331 DeleteScriptFileStruct(NewScriptFile
);
2332 return (EFI_OUT_OF_RESOURCES
);
2334 for (LoopVar
= 0 ; LoopVar
< 10 && LoopVar
< NewScriptFile
->Argc
; LoopVar
++) {
2335 ASSERT(NewScriptFile
->Argv
[LoopVar
] == NULL
);
2336 NewScriptFile
->Argv
[LoopVar
] = StrnCatGrow(&NewScriptFile
->Argv
[LoopVar
], NULL
, ShellInfoObject
.NewShellParametersProtocol
->Argv
[LoopVar
], 0);
2337 if (NewScriptFile
->Argv
[LoopVar
] == NULL
) {
2338 DeleteScriptFileStruct(NewScriptFile
);
2339 return (EFI_OUT_OF_RESOURCES
);
2343 NewScriptFile
->Argv
= NULL
;
2346 InitializeListHead(&NewScriptFile
->CommandList
);
2347 InitializeListHead(&NewScriptFile
->SubstList
);
2350 // Now build the list of all script commands.
2353 while(!ShellFileHandleEof(Handle
)) {
2354 CommandLine
= ShellFileHandleReturnLine(Handle
, &Ascii
);
2356 if (CommandLine
== NULL
|| StrLen(CommandLine
) == 0) {
2359 NewScriptFile
->CurrentCommand
= AllocateZeroPool(sizeof(SCRIPT_COMMAND_LIST
));
2360 if (NewScriptFile
->CurrentCommand
== NULL
) {
2361 DeleteScriptFileStruct(NewScriptFile
);
2362 return (EFI_OUT_OF_RESOURCES
);
2365 NewScriptFile
->CurrentCommand
->Cl
= CommandLine
;
2366 NewScriptFile
->CurrentCommand
->Data
= NULL
;
2367 NewScriptFile
->CurrentCommand
->Line
= LineCount
;
2369 InsertTailList(&NewScriptFile
->CommandList
, &NewScriptFile
->CurrentCommand
->Link
);
2373 // Add this as the topmost script file
2375 ShellCommandSetNewScript (NewScriptFile
);
2378 // Now enumerate through the commands and run each one.
2380 CommandLine
= AllocateZeroPool(PcdGet16(PcdShellPrintBufferSize
));
2381 if (CommandLine
== NULL
) {
2382 DeleteScriptFileStruct(NewScriptFile
);
2383 return (EFI_OUT_OF_RESOURCES
);
2385 CommandLine2
= AllocateZeroPool(PcdGet16(PcdShellPrintBufferSize
));
2386 if (CommandLine2
== NULL
) {
2387 FreePool(CommandLine
);
2388 DeleteScriptFileStruct(NewScriptFile
);
2389 return (EFI_OUT_OF_RESOURCES
);
2392 for ( NewScriptFile
->CurrentCommand
= (SCRIPT_COMMAND_LIST
*)GetFirstNode(&NewScriptFile
->CommandList
)
2393 ; !IsNull(&NewScriptFile
->CommandList
, &NewScriptFile
->CurrentCommand
->Link
)
2394 ; // conditional increment in the body of the loop
2396 ASSERT(CommandLine2
!= NULL
);
2397 StrCpy(CommandLine2
, NewScriptFile
->CurrentCommand
->Cl
);
2400 // NULL out comments
2402 for (CommandLine3
= CommandLine2
; CommandLine3
!= NULL
&& *CommandLine3
!= CHAR_NULL
; CommandLine3
++) {
2403 if (*CommandLine3
== L
'^') {
2404 if (*(CommandLine3
+1) == L
'#' || *(CommandLine3
+1) == L
':') {
2405 CopyMem(CommandLine3
, CommandLine3
+1, StrSize(CommandLine3
) - sizeof(CommandLine3
[0]));
2407 } else if (*CommandLine3
== L
'#') {
2408 *CommandLine3
= CHAR_NULL
;
2412 if (CommandLine2
!= NULL
&& StrLen(CommandLine2
) >= 1) {
2414 // Due to variability in starting the find and replace action we need to have both buffers the same.
2416 StrCpy(CommandLine
, CommandLine2
);
2419 // Remove the %0 to %9 from the command line (if we have some arguments)
2421 if (NewScriptFile
->Argv
!= NULL
) {
2422 switch (NewScriptFile
->Argc
) {
2424 Status
= ShellCopySearchAndReplace(CommandLine2
, CommandLine
, PcdGet16 (PcdShellPrintBufferSize
), L
"%9", NewScriptFile
->Argv
[9], FALSE
, TRUE
);
2425 ASSERT_EFI_ERROR(Status
);
2427 Status
= ShellCopySearchAndReplace(CommandLine
, CommandLine2
, PcdGet16 (PcdShellPrintBufferSize
), L
"%8", NewScriptFile
->Argv
[8], FALSE
, TRUE
);
2428 ASSERT_EFI_ERROR(Status
);
2430 Status
= ShellCopySearchAndReplace(CommandLine2
, CommandLine
, PcdGet16 (PcdShellPrintBufferSize
), L
"%7", NewScriptFile
->Argv
[7], FALSE
, TRUE
);
2431 ASSERT_EFI_ERROR(Status
);
2433 Status
= ShellCopySearchAndReplace(CommandLine
, CommandLine2
, PcdGet16 (PcdShellPrintBufferSize
), L
"%6", NewScriptFile
->Argv
[6], FALSE
, TRUE
);
2434 ASSERT_EFI_ERROR(Status
);
2436 Status
= ShellCopySearchAndReplace(CommandLine2
, CommandLine
, PcdGet16 (PcdShellPrintBufferSize
), L
"%5", NewScriptFile
->Argv
[5], FALSE
, TRUE
);
2437 ASSERT_EFI_ERROR(Status
);
2439 Status
= ShellCopySearchAndReplace(CommandLine
, CommandLine2
, PcdGet16 (PcdShellPrintBufferSize
), L
"%4", NewScriptFile
->Argv
[4], FALSE
, TRUE
);
2440 ASSERT_EFI_ERROR(Status
);
2442 Status
= ShellCopySearchAndReplace(CommandLine2
, CommandLine
, PcdGet16 (PcdShellPrintBufferSize
), L
"%3", NewScriptFile
->Argv
[3], FALSE
, TRUE
);
2443 ASSERT_EFI_ERROR(Status
);
2445 Status
= ShellCopySearchAndReplace(CommandLine
, CommandLine2
, PcdGet16 (PcdShellPrintBufferSize
), L
"%2", NewScriptFile
->Argv
[2], FALSE
, TRUE
);
2446 ASSERT_EFI_ERROR(Status
);
2448 Status
= ShellCopySearchAndReplace(CommandLine2
, CommandLine
, PcdGet16 (PcdShellPrintBufferSize
), L
"%1", NewScriptFile
->Argv
[1], FALSE
, TRUE
);
2449 ASSERT_EFI_ERROR(Status
);
2451 Status
= ShellCopySearchAndReplace(CommandLine
, CommandLine2
, PcdGet16 (PcdShellPrintBufferSize
), L
"%0", NewScriptFile
->Argv
[0], FALSE
, TRUE
);
2452 ASSERT_EFI_ERROR(Status
);
2458 Status
= ShellCopySearchAndReplace(CommandLine2
, CommandLine
, PcdGet16 (PcdShellPrintBufferSize
), L
"%1", L
"\"\"", FALSE
, FALSE
);
2459 Status
= ShellCopySearchAndReplace(CommandLine
, CommandLine2
, PcdGet16 (PcdShellPrintBufferSize
), L
"%2", L
"\"\"", FALSE
, FALSE
);
2460 Status
= ShellCopySearchAndReplace(CommandLine2
, CommandLine
, PcdGet16 (PcdShellPrintBufferSize
), L
"%3", L
"\"\"", FALSE
, FALSE
);
2461 Status
= ShellCopySearchAndReplace(CommandLine
, CommandLine2
, PcdGet16 (PcdShellPrintBufferSize
), L
"%4", L
"\"\"", FALSE
, FALSE
);
2462 Status
= ShellCopySearchAndReplace(CommandLine2
, CommandLine
, PcdGet16 (PcdShellPrintBufferSize
), L
"%5", L
"\"\"", FALSE
, FALSE
);
2463 Status
= ShellCopySearchAndReplace(CommandLine
, CommandLine2
, PcdGet16 (PcdShellPrintBufferSize
), L
"%6", L
"\"\"", FALSE
, FALSE
);
2464 Status
= ShellCopySearchAndReplace(CommandLine2
, CommandLine
, PcdGet16 (PcdShellPrintBufferSize
), L
"%7", L
"\"\"", FALSE
, FALSE
);
2465 Status
= ShellCopySearchAndReplace(CommandLine
, CommandLine2
, PcdGet16 (PcdShellPrintBufferSize
), L
"%8", L
"\"\"", FALSE
, FALSE
);
2466 Status
= ShellCopySearchAndReplace(CommandLine2
, CommandLine
, PcdGet16 (PcdShellPrintBufferSize
), L
"%9", L
"\"\"", FALSE
, FALSE
);
2468 StrCpy(CommandLine2
, CommandLine
);
2470 LastCommand
= NewScriptFile
->CurrentCommand
;
2472 for (CommandLine3
= CommandLine2
; CommandLine3
[0] == L
' ' ; CommandLine3
++);
2474 if (CommandLine3
!= NULL
&& CommandLine3
[0] == L
':' ) {
2476 // This line is a goto target / label
2479 if (CommandLine3
!= NULL
&& StrLen(CommandLine3
) > 0) {
2480 if (CommandLine3
[0] == L
'@') {
2482 // We need to save the current echo state
2483 // and disable echo for just this command.
2485 PreCommandEchoState
= ShellCommandGetEchoState();
2486 ShellCommandSetEchoState(FALSE
);
2487 Status
= RunCommand(CommandLine3
+1);
2490 // If command was "@echo -off" or "@echo -on" then don't restore echo state
2492 if (StrCmp (L
"@echo -off", CommandLine3
) != 0 &&
2493 StrCmp (L
"@echo -on", CommandLine3
) != 0) {
2495 // Now restore the pre-'@' echo state.
2497 ShellCommandSetEchoState(PreCommandEchoState
);
2500 if (ShellCommandGetEchoState()) {
2501 CurDir
= ShellInfoObject
.NewEfiShellProtocol
->GetEnv(L
"cwd");
2502 if (CurDir
!= NULL
&& StrLen(CurDir
) > 1) {
2503 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_CURDIR
), ShellInfoObject
.HiiHandle
, CurDir
);
2505 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_SHELL
), ShellInfoObject
.HiiHandle
);
2507 ShellPrintEx(-1, -1, L
"%s\r\n", CommandLine2
);
2509 Status
= RunCommand(CommandLine3
);
2513 if (ShellCommandGetScriptExit()) {
2515 // ShellCommandGetExitCode() always returns a UINT64
2517 UnicodeSPrint(LeString
, sizeof(LeString
), L
"0x%Lx", ShellCommandGetExitCode());
2518 DEBUG_CODE(InternalEfiShellSetEnv(L
"debuglasterror", LeString
, TRUE
););
2519 InternalEfiShellSetEnv(L
"lasterror", LeString
, TRUE
);
2521 ShellCommandRegisterExit(FALSE
, 0);
2522 Status
= EFI_SUCCESS
;
2525 if (ShellGetExecutionBreakFlag()) {
2528 if (EFI_ERROR(Status
)) {
2531 if (ShellCommandGetExit()) {
2536 // If that commend did not update the CurrentCommand then we need to advance it...
2538 if (LastCommand
== NewScriptFile
->CurrentCommand
) {
2539 NewScriptFile
->CurrentCommand
= (SCRIPT_COMMAND_LIST
*)GetNextNode(&NewScriptFile
->CommandList
, &NewScriptFile
->CurrentCommand
->Link
);
2540 if (!IsNull(&NewScriptFile
->CommandList
, &NewScriptFile
->CurrentCommand
->Link
)) {
2541 NewScriptFile
->CurrentCommand
->Reset
= TRUE
;
2545 NewScriptFile
->CurrentCommand
= (SCRIPT_COMMAND_LIST
*)GetNextNode(&NewScriptFile
->CommandList
, &NewScriptFile
->CurrentCommand
->Link
);
2546 if (!IsNull(&NewScriptFile
->CommandList
, &NewScriptFile
->CurrentCommand
->Link
)) {
2547 NewScriptFile
->CurrentCommand
->Reset
= TRUE
;
2553 FreePool(CommandLine
);
2554 FreePool(CommandLine2
);
2555 ShellCommandSetNewScript (NULL
);
2558 // Only if this was the last script reset the state.
2560 if (ShellCommandGetCurrentScriptFile()==NULL
) {
2561 ShellCommandSetEchoState(PreScriptEchoState
);
2563 return (EFI_SUCCESS
);
2567 Function to process a NSH script file.
2569 @param[in] ScriptPath Pointer to the script file name (including file system path).
2571 @retval EFI_SUCCESS the script completed sucessfully
2576 IN CONST CHAR16
*ScriptPath
2580 SHELL_FILE_HANDLE FileHandle
;
2582 if (ShellIsFile(ScriptPath
) != EFI_SUCCESS
) {
2583 return (EFI_INVALID_PARAMETER
);
2586 Status
= ShellOpenFileByName(ScriptPath
, &FileHandle
, EFI_FILE_MODE_READ
, 0);
2587 if (EFI_ERROR(Status
)) {
2591 Status
= RunScriptFileHandle(FileHandle
, ScriptPath
);
2593 ShellCloseFile(&FileHandle
);