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.
114 IN CONST CHAR16
*CmdLine
117 CONST CHAR16
*TempSpot
;
119 if (StrStr(CmdLine
, L
"|") != NULL
) {
120 for (TempSpot
= CmdLine
; TempSpot
!= NULL
&& *TempSpot
!= CHAR_NULL
; TempSpot
++) {
121 if (*TempSpot
== L
'^' && *(TempSpot
+1) == L
'|') {
123 } else if (*TempSpot
== L
'|') {
132 Determine if a command line contains a split operation
134 @param[in] CmdLine The command line to parse.
136 @retval TRUE CmdLine has a valid split.
137 @retval FALSE CmdLine does not have a valid split.
142 IN CONST CHAR16
*CmdLine
145 CONST CHAR16
*TempSpot
;
146 TempSpot
= FindSplit(CmdLine
);
147 return (BOOLEAN
) ((TempSpot
!= NULL
) && (*TempSpot
!= CHAR_NULL
));
151 Function to start monitoring for CTRL-S using SimpleTextInputEx. This
152 feature's enabled state was not known when the shell initially launched.
154 @retval EFI_SUCCESS The feature is enabled.
155 @retval EFI_OUT_OF_RESOURCES There is not enough mnemory available.
159 InternalEfiShellStartCtrlSMonitor(
163 EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL
*SimpleEx
;
164 EFI_KEY_DATA KeyData
;
167 Status
= gBS
->OpenProtocol(
168 gST
->ConsoleInHandle
,
169 &gEfiSimpleTextInputExProtocolGuid
,
173 EFI_OPEN_PROTOCOL_GET_PROTOCOL
);
174 if (EFI_ERROR(Status
)) {
179 STRING_TOKEN (STR_SHELL_NO_IN_EX
),
180 ShellInfoObject
.HiiHandle
);
181 return (EFI_SUCCESS
);
184 KeyData
.KeyState
.KeyToggleState
= 0;
185 KeyData
.Key
.ScanCode
= 0;
186 KeyData
.KeyState
.KeyShiftState
= EFI_SHIFT_STATE_VALID
|EFI_LEFT_CONTROL_PRESSED
;
187 KeyData
.Key
.UnicodeChar
= L
's';
189 Status
= SimpleEx
->RegisterKeyNotify(
192 NotificationFunction
,
193 &ShellInfoObject
.CtrlSNotifyHandle1
);
195 KeyData
.KeyState
.KeyShiftState
= EFI_SHIFT_STATE_VALID
|EFI_RIGHT_CONTROL_PRESSED
;
196 if (!EFI_ERROR(Status
)) {
197 Status
= SimpleEx
->RegisterKeyNotify(
200 NotificationFunction
,
201 &ShellInfoObject
.CtrlSNotifyHandle2
);
203 KeyData
.KeyState
.KeyShiftState
= EFI_SHIFT_STATE_VALID
|EFI_LEFT_CONTROL_PRESSED
;
204 KeyData
.Key
.UnicodeChar
= 19;
206 if (!EFI_ERROR(Status
)) {
207 Status
= SimpleEx
->RegisterKeyNotify(
210 NotificationFunction
,
211 &ShellInfoObject
.CtrlSNotifyHandle3
);
213 KeyData
.KeyState
.KeyShiftState
= EFI_SHIFT_STATE_VALID
|EFI_RIGHT_CONTROL_PRESSED
;
214 if (!EFI_ERROR(Status
)) {
215 Status
= SimpleEx
->RegisterKeyNotify(
218 NotificationFunction
,
219 &ShellInfoObject
.CtrlSNotifyHandle4
);
227 The entry point for the application.
229 @param[in] ImageHandle The firmware allocated handle for the EFI image.
230 @param[in] SystemTable A pointer to the EFI System Table.
232 @retval EFI_SUCCESS The entry point is executed successfully.
233 @retval other Some error occurs when executing this entry point.
239 IN EFI_HANDLE ImageHandle
,
240 IN EFI_SYSTEM_TABLE
*SystemTable
246 EFI_HANDLE ConInHandle
;
247 EFI_SIMPLE_TEXT_INPUT_PROTOCOL
*OldConIn
;
249 if (PcdGet8(PcdShellSupportLevel
) > 3) {
250 return (EFI_UNSUPPORTED
);
256 Status
= gST
->ConOut
->ClearScreen(gST
->ConOut
);
257 if (EFI_ERROR(Status
)) {
262 // Populate the global structure from PCDs
264 ShellInfoObject
.ImageDevPath
= NULL
;
265 ShellInfoObject
.FileDevPath
= NULL
;
266 ShellInfoObject
.PageBreakEnabled
= PcdGetBool(PcdShellPageBreakDefault
);
267 ShellInfoObject
.ViewingSettings
.InsertMode
= PcdGetBool(PcdShellInsertModeDefault
);
268 ShellInfoObject
.LogScreenCount
= PcdGet8 (PcdShellScreenLogCount
);
271 // verify we dont allow for spec violation
273 ASSERT(ShellInfoObject
.LogScreenCount
>= 3);
276 // Initialize the LIST ENTRY objects...
278 InitializeListHead(&ShellInfoObject
.BufferToFreeList
.Link
);
279 InitializeListHead(&ShellInfoObject
.ViewingSettings
.CommandHistory
.Link
);
280 InitializeListHead(&ShellInfoObject
.SplitList
.Link
);
283 // Check PCDs for optional features that are not implemented yet.
285 if ( PcdGetBool(PcdShellSupportOldProtocols
)
286 || !FeaturePcdGet(PcdShellRequireHiiPlatform
)
287 || FeaturePcdGet(PcdShellSupportFrameworkHii
)
289 return (EFI_UNSUPPORTED
);
293 // turn off the watchdog timer
295 gBS
->SetWatchdogTimer (0, 0, 0, NULL
);
298 // install our console logger. This will keep a log of the output for back-browsing
300 Status
= ConsoleLoggerInstall(ShellInfoObject
.LogScreenCount
, &ShellInfoObject
.ConsoleInfo
);
301 if (!EFI_ERROR(Status
)) {
303 // Enable the cursor to be visible
305 gST
->ConOut
->EnableCursor (gST
->ConOut
, TRUE
);
308 // If supporting EFI 1.1 we need to install HII protocol
309 // only do this if PcdShellRequireHiiPlatform == FALSE
311 // remove EFI_UNSUPPORTED check above when complete.
312 ///@todo add support for Framework HII
315 // install our (solitary) HII package
317 ShellInfoObject
.HiiHandle
= HiiAddPackages (&gEfiCallerIdGuid
, gImageHandle
, ShellStrings
, NULL
);
318 if (ShellInfoObject
.HiiHandle
== NULL
) {
319 if (PcdGetBool(PcdShellSupportFrameworkHii
)) {
320 ///@todo Add our package into Framework HII
322 if (ShellInfoObject
.HiiHandle
== NULL
) {
323 return (EFI_NOT_STARTED
);
328 // create and install the EfiShellParametersProtocol
330 Status
= CreatePopulateInstallShellParametersProtocol(&ShellInfoObject
.NewShellParametersProtocol
, &ShellInfoObject
.RootShellInstance
);
331 ASSERT_EFI_ERROR(Status
);
332 ASSERT(ShellInfoObject
.NewShellParametersProtocol
!= NULL
);
335 // create and install the EfiShellProtocol
337 Status
= CreatePopulateInstallShellProtocol(&ShellInfoObject
.NewEfiShellProtocol
);
338 ASSERT_EFI_ERROR(Status
);
339 ASSERT(ShellInfoObject
.NewEfiShellProtocol
!= NULL
);
342 // Now initialize the shell library (it requires Shell Parameters protocol)
344 Status
= ShellInitialize();
345 ASSERT_EFI_ERROR(Status
);
347 Status
= CommandInit();
348 ASSERT_EFI_ERROR(Status
);
351 // Check the command line
353 Status
= ProcessCommandLine();
356 // If shell support level is >= 1 create the mappings and paths
358 if (PcdGet8(PcdShellSupportLevel
) >= 1) {
359 Status
= ShellCommandCreateInitialMappingsAndPaths();
363 // save the device path for the loaded image and the device path for the filepath (under loaded image)
364 // These are where to look for the startup.nsh file
366 Status
= GetDevicePathsForImageAndFile(&ShellInfoObject
.ImageDevPath
, &ShellInfoObject
.FileDevPath
);
367 ASSERT_EFI_ERROR(Status
);
370 // Display the version
372 if (!ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoVersion
) {
375 gST
->ConOut
->Mode
->CursorRow
,
377 STRING_TOKEN (STR_VER_OUTPUT_MAIN_SHELL
),
378 ShellInfoObject
.HiiHandle
,
379 SupportLevel
[PcdGet8(PcdShellSupportLevel
)],
380 gEfiShellProtocol
->MajorVersion
,
381 gEfiShellProtocol
->MinorVersion
388 STRING_TOKEN (STR_VER_OUTPUT_MAIN_SUPPLIER
),
389 ShellInfoObject
.HiiHandle
,
390 (CHAR16
*) PcdGetPtr (PcdShellSupplier
)
397 STRING_TOKEN (STR_VER_OUTPUT_MAIN_UEFI
),
398 ShellInfoObject
.HiiHandle
,
399 (gST
->Hdr
.Revision
&0xffff0000)>>16,
400 (gST
->Hdr
.Revision
&0x0000ffff),
402 gST
->FirmwareRevision
407 // Display the mapping
409 if (PcdGet8(PcdShellSupportLevel
) >= 2 && !ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoMap
) {
410 Status
= RunCommand(L
"map");
411 ASSERT_EFI_ERROR(Status
);
415 // init all the built in alias'
417 Status
= SetBuiltInAlias();
418 ASSERT_EFI_ERROR(Status
);
421 // Initialize environment variables
423 if (ShellCommandGetProfileList() != NULL
) {
424 Status
= InternalEfiShellSetEnv(L
"profiles", ShellCommandGetProfileList(), TRUE
);
425 ASSERT_EFI_ERROR(Status
);
429 TempString
= AllocateZeroPool(Size
);
431 UnicodeSPrint(TempString
, Size
, L
"%d", PcdGet8(PcdShellSupportLevel
));
432 Status
= InternalEfiShellSetEnv(L
"uefishellsupport", TempString
, TRUE
);
433 ASSERT_EFI_ERROR(Status
);
435 UnicodeSPrint(TempString
, Size
, L
"%d.%d", ShellInfoObject
.NewEfiShellProtocol
->MajorVersion
, ShellInfoObject
.NewEfiShellProtocol
->MinorVersion
);
436 Status
= InternalEfiShellSetEnv(L
"uefishellversion", TempString
, TRUE
);
437 ASSERT_EFI_ERROR(Status
);
439 UnicodeSPrint(TempString
, Size
, L
"%d.%d", (gST
->Hdr
.Revision
& 0xFFFF0000) >> 16, gST
->Hdr
.Revision
& 0x0000FFFF);
440 Status
= InternalEfiShellSetEnv(L
"uefiversion", TempString
, TRUE
);
441 ASSERT_EFI_ERROR(Status
);
443 FreePool(TempString
);
445 if (!EFI_ERROR(Status
)) {
446 if (!ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoInterrupt
) {
448 // Set up the event for CTRL-C monitoring...
450 Status
= InernalEfiShellStartMonitor();
453 if (!EFI_ERROR(Status
) && !ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoConsoleIn
) {
455 // Set up the event for CTRL-S monitoring...
457 Status
= InternalEfiShellStartCtrlSMonitor();
460 if (!EFI_ERROR(Status
) && ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoConsoleIn
) {
462 // close off the gST->ConIn
464 OldConIn
= gST
->ConIn
;
465 ConInHandle
= gST
->ConsoleInHandle
;
466 gST
->ConIn
= CreateSimpleTextInOnFile((SHELL_FILE_HANDLE
)&FileInterfaceNulFile
, &gST
->ConsoleInHandle
);
472 if (!EFI_ERROR(Status
) && PcdGet8(PcdShellSupportLevel
) >= 1) {
474 // process the startup script or launch the called app.
476 Status
= DoStartupScript(ShellInfoObject
.ImageDevPath
, ShellInfoObject
.FileDevPath
);
479 if (!ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.Exit
&& !ShellCommandGetExit() && (PcdGet8(PcdShellSupportLevel
) >= 3 || PcdGetBool(PcdShellForceConsole
)) && !EFI_ERROR(Status
) && !ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoConsoleIn
) {
481 // begin the UI waiting loop
485 // clean out all the memory allocated for CONST <something> * return values
486 // between each shell prompt presentation
488 if (!IsListEmpty(&ShellInfoObject
.BufferToFreeList
.Link
)){
489 FreeBufferList(&ShellInfoObject
.BufferToFreeList
);
493 // Reset page break back to default.
495 ShellInfoObject
.PageBreakEnabled
= PcdGetBool(PcdShellPageBreakDefault
);
496 ShellInfoObject
.ConsoleInfo
->Enabled
= TRUE
;
497 ShellInfoObject
.ConsoleInfo
->RowCounter
= 0;
500 // Reset the CTRL-C event (yes we ignore the return values)
502 Status
= gBS
->CheckEvent (ShellInfoObject
.NewEfiShellProtocol
->ExecutionBreak
);
507 Status
= DoShellPrompt();
508 } while (!ShellCommandGetExit());
510 if (OldConIn
!= NULL
&& ConInHandle
!= NULL
) {
511 CloseSimpleTextInOnFile (gST
->ConIn
);
512 gST
->ConIn
= OldConIn
;
513 gST
->ConsoleInHandle
= ConInHandle
;
519 // uninstall protocols / free memory / etc...
521 if (ShellInfoObject
.UserBreakTimer
!= NULL
) {
522 gBS
->CloseEvent(ShellInfoObject
.UserBreakTimer
);
523 DEBUG_CODE(ShellInfoObject
.UserBreakTimer
= NULL
;);
525 if (ShellInfoObject
.ImageDevPath
!= NULL
) {
526 FreePool(ShellInfoObject
.ImageDevPath
);
527 DEBUG_CODE(ShellInfoObject
.ImageDevPath
= NULL
;);
529 if (ShellInfoObject
.FileDevPath
!= NULL
) {
530 FreePool(ShellInfoObject
.FileDevPath
);
531 DEBUG_CODE(ShellInfoObject
.FileDevPath
= NULL
;);
533 if (ShellInfoObject
.NewShellParametersProtocol
!= NULL
) {
534 CleanUpShellParametersProtocol(ShellInfoObject
.NewShellParametersProtocol
);
535 DEBUG_CODE(ShellInfoObject
.NewShellParametersProtocol
= NULL
;);
537 if (ShellInfoObject
.NewEfiShellProtocol
!= NULL
){
538 if (ShellInfoObject
.NewEfiShellProtocol
->IsRootShell()){
539 InternalEfiShellSetEnv(L
"cwd", NULL
, TRUE
);
541 CleanUpShellProtocol(ShellInfoObject
.NewEfiShellProtocol
);
542 DEBUG_CODE(ShellInfoObject
.NewEfiShellProtocol
= NULL
;);
545 if (!IsListEmpty(&ShellInfoObject
.BufferToFreeList
.Link
)){
546 FreeBufferList(&ShellInfoObject
.BufferToFreeList
);
549 if (!IsListEmpty(&ShellInfoObject
.SplitList
.Link
)){
550 ASSERT(FALSE
); ///@todo finish this de-allocation.
553 if (ShellInfoObject
.ShellInitSettings
.FileName
!= NULL
) {
554 FreePool(ShellInfoObject
.ShellInitSettings
.FileName
);
555 DEBUG_CODE(ShellInfoObject
.ShellInitSettings
.FileName
= NULL
;);
558 if (ShellInfoObject
.ShellInitSettings
.FileOptions
!= NULL
) {
559 FreePool(ShellInfoObject
.ShellInitSettings
.FileOptions
);
560 DEBUG_CODE(ShellInfoObject
.ShellInitSettings
.FileOptions
= NULL
;);
563 if (ShellInfoObject
.HiiHandle
!= NULL
) {
564 HiiRemovePackages(ShellInfoObject
.HiiHandle
);
565 DEBUG_CODE(ShellInfoObject
.HiiHandle
= NULL
;);
568 if (!IsListEmpty(&ShellInfoObject
.ViewingSettings
.CommandHistory
.Link
)){
569 FreeBufferList(&ShellInfoObject
.ViewingSettings
.CommandHistory
);
572 ASSERT(ShellInfoObject
.ConsoleInfo
!= NULL
);
573 if (ShellInfoObject
.ConsoleInfo
!= NULL
) {
574 ConsoleLoggerUninstall(ShellInfoObject
.ConsoleInfo
);
575 FreePool(ShellInfoObject
.ConsoleInfo
);
576 DEBUG_CODE(ShellInfoObject
.ConsoleInfo
= NULL
;);
579 if (ShellCommandGetExit()) {
580 return ((EFI_STATUS
)ShellCommandGetExitCode());
586 Sets all the alias' that were registered with the ShellCommandLib library.
588 @retval EFI_SUCCESS all init commands were run sucessfully.
596 CONST ALIAS_LIST
*List
;
600 // Get all the commands we want to run
602 List
= ShellCommandGetInitAliasList();
605 // for each command in the List
607 for ( Node
= (ALIAS_LIST
*)GetFirstNode(&List
->Link
)
608 ; !IsNull (&List
->Link
, &Node
->Link
)
609 ; Node
= (ALIAS_LIST
*)GetNextNode(&List
->Link
, &Node
->Link
)
612 // install the alias'
614 Status
= InternalSetAlias(Node
->CommandString
, Node
->Alias
, TRUE
);
615 ASSERT_EFI_ERROR(Status
);
617 return (EFI_SUCCESS
);
621 Internal function to determine if 2 command names are really the same.
623 @param[in] Command1 The pointer to the first command name.
624 @param[in] Command2 The pointer to the second command name.
626 @retval TRUE The 2 command names are the same.
627 @retval FALSE The 2 command names are not the same.
632 IN CONST CHAR16
*Command1
,
633 IN CONST CHAR16
*Command2
636 if (StringNoCaseCompare(&Command1
, &Command2
) == 0) {
643 Internal function to determine if a command is a script only command.
645 @param[in] CommandName The pointer to the command name.
647 @retval TRUE The command is a script only command.
648 @retval FALSE The command is not a script only command.
653 IN CONST CHAR16
*CommandName
656 if (IsCommand(CommandName
, L
"for")
657 ||IsCommand(CommandName
, L
"endfor")
658 ||IsCommand(CommandName
, L
"if")
659 ||IsCommand(CommandName
, L
"else")
660 ||IsCommand(CommandName
, L
"endif")
661 ||IsCommand(CommandName
, L
"goto")) {
668 This function will populate the 2 device path protocol parameters based on the
669 global gImageHandle. The DevPath will point to the device path for the handle that has
670 loaded image protocol installed on it. The FilePath will point to the device path
671 for the file that was loaded.
673 @param[in, out] DevPath On a sucessful return the device path to the loaded image.
674 @param[in, out] FilePath On a sucessful return the device path to the file.
676 @retval EFI_SUCCESS The 2 device paths were sucessfully returned.
677 @retval other A error from gBS->HandleProtocol.
683 GetDevicePathsForImageAndFile (
684 IN OUT EFI_DEVICE_PATH_PROTOCOL
**DevPath
,
685 IN OUT EFI_DEVICE_PATH_PROTOCOL
**FilePath
689 EFI_LOADED_IMAGE_PROTOCOL
*LoadedImage
;
690 EFI_DEVICE_PATH_PROTOCOL
*ImageDevicePath
;
692 ASSERT(DevPath
!= NULL
);
693 ASSERT(FilePath
!= NULL
);
695 Status
= gBS
->OpenProtocol (
697 &gEfiLoadedImageProtocolGuid
,
698 (VOID
**)&LoadedImage
,
701 EFI_OPEN_PROTOCOL_GET_PROTOCOL
703 if (!EFI_ERROR (Status
)) {
704 Status
= gBS
->OpenProtocol (
705 LoadedImage
->DeviceHandle
,
706 &gEfiDevicePathProtocolGuid
,
707 (VOID
**)&ImageDevicePath
,
710 EFI_OPEN_PROTOCOL_GET_PROTOCOL
712 if (!EFI_ERROR (Status
)) {
713 *DevPath
= DuplicateDevicePath (ImageDevicePath
);
714 *FilePath
= DuplicateDevicePath (LoadedImage
->FilePath
);
716 LoadedImage
->DeviceHandle
,
717 &gEfiDevicePathProtocolGuid
,
723 &gEfiLoadedImageProtocolGuid
,
730 STATIC CONST SHELL_PARAM_ITEM mShellParamList
[] = {
731 {L
"-nostartup", TypeFlag
},
732 {L
"-startup", TypeFlag
},
733 {L
"-noconsoleout", TypeFlag
},
734 {L
"-noconsolein", TypeFlag
},
735 {L
"-nointerrupt", TypeFlag
},
736 {L
"-nomap", TypeFlag
},
737 {L
"-noversion", TypeFlag
},
738 {L
"-startup", TypeFlag
},
739 {L
"-delay", TypeValue
},
740 {L
"-_exit", TypeFlag
},
745 Process all Uefi Shell 2.0 command line options.
747 see Uefi Shell 2.0 section 3.2 for full details.
749 the command line must resemble the following:
751 shell.efi [ShellOpt-options] [options] [file-name [file-name-options]]
753 ShellOpt-options Options which control the initialization behavior of the shell.
754 These options are read from the EFI global variable "ShellOpt"
755 and are processed before options or file-name.
757 options Options which control the initialization behavior of the shell.
759 file-name The name of a UEFI shell application or script to be executed
760 after initialization is complete. By default, if file-name is
761 specified, then -nostartup is implied. Scripts are not supported
764 file-name-options The command-line options that are passed to file-name when it
767 This will initialize the ShellInfoObject.ShellInitSettings global variable.
769 @retval EFI_SUCCESS The variable is initialized.
780 CONST CHAR16
*TempConst
;
783 CHAR16
*ProblemParam
;
789 Status
= ShellCommandLineParse (mShellParamList
, &Package
, NULL
, FALSE
);
793 TempConst
= ShellCommandLineGetRawValue(Package
, Count
++);
794 if (TempConst
!= NULL
&& StrLen(TempConst
)) {
795 ShellInfoObject
.ShellInitSettings
.FileName
= AllocateZeroPool(StrSize(TempConst
));
796 if (ShellInfoObject
.ShellInitSettings
.FileName
== NULL
) {
797 return (EFI_OUT_OF_RESOURCES
);
799 StrCpy(ShellInfoObject
.ShellInitSettings
.FileName
, TempConst
);
800 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoStartup
= 1;
801 for (LoopVar
= 0 ; LoopVar
< gEfiShellParametersProtocol
->Argc
; LoopVar
++) {
802 if (StrCmp(gEfiShellParametersProtocol
->Argv
[LoopVar
], ShellInfoObject
.ShellInitSettings
.FileName
)==0) {
805 // We found the file... add the rest of the params...
807 for ( ; LoopVar
< gEfiShellParametersProtocol
->Argc
; LoopVar
++) {
808 ASSERT((ShellInfoObject
.ShellInitSettings
.FileOptions
== NULL
&& Size
== 0) || (ShellInfoObject
.ShellInitSettings
.FileOptions
!= NULL
));
809 StrnCatGrow(&ShellInfoObject
.ShellInitSettings
.FileOptions
,
813 if (ShellInfoObject
.ShellInitSettings
.FileOptions
== NULL
) {
814 SHELL_FREE_NON_NULL(ShellInfoObject
.ShellInitSettings
.FileName
);
815 return (EFI_OUT_OF_RESOURCES
);
817 StrnCatGrow(&ShellInfoObject
.ShellInitSettings
.FileOptions
,
819 gEfiShellParametersProtocol
->Argv
[LoopVar
],
821 if (ShellInfoObject
.ShellInitSettings
.FileOptions
== NULL
) {
822 SHELL_FREE_NON_NULL(ShellInfoObject
.ShellInitSettings
.FileName
);
823 return (EFI_OUT_OF_RESOURCES
);
829 ShellCommandLineFreeVarList(Package
);
831 Status
= ShellCommandLineParse (mShellParamList
, &Package
, &ProblemParam
, FALSE
);
832 if (EFI_ERROR(Status
)) {
833 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_GEN_PROBLEM
), ShellInfoObject
.HiiHandle
, ProblemParam
);
834 FreePool(ProblemParam
);
835 ShellCommandLineFreeVarList(Package
);
836 return (EFI_INVALID_PARAMETER
);
840 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.Startup
= ShellCommandLineGetFlag(Package
, L
"-startup");
841 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoStartup
= ShellCommandLineGetFlag(Package
, L
"-nostartup");
842 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoConsoleOut
= ShellCommandLineGetFlag(Package
, L
"-noconsoleout");
843 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoConsoleIn
= ShellCommandLineGetFlag(Package
, L
"-noconsolein");
844 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoInterrupt
= ShellCommandLineGetFlag(Package
, L
"-nointerrupt");
845 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoMap
= ShellCommandLineGetFlag(Package
, L
"-nomap");
846 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoVersion
= ShellCommandLineGetFlag(Package
, L
"-noversion");
847 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.Delay
= ShellCommandLineGetFlag(Package
, L
"-delay");
848 ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.Exit
= ShellCommandLineGetFlag(Package
, L
"-_exit");
850 ShellInfoObject
.ShellInitSettings
.Delay
= 5;
852 if (ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoInterrupt
) {
853 ShellInfoObject
.ShellInitSettings
.Delay
= 0;
854 } else if (ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.Delay
) {
855 TempConst
= ShellCommandLineGetValue(Package
, L
"-delay");
856 if (TempConst
!= NULL
&& *TempConst
== L
':') {
859 if (TempConst
!= NULL
&& !EFI_ERROR(ShellConvertStringToUint64(TempConst
, &Intermediate
, FALSE
, FALSE
))) {
860 ShellInfoObject
.ShellInitSettings
.Delay
= (UINTN
)Intermediate
;
863 ShellCommandLineFreeVarList(Package
);
869 Handles all interaction with the default startup script.
871 this will check that the correct command line parameters were passed, handle the delay, and then start running the script.
873 @param ImagePath the path to the image for shell. first place to look for the startup script
874 @param FilePath the path to the file for shell. second place to look for the startup script.
876 @retval EFI_SUCCESS the variable is initialized.
881 EFI_DEVICE_PATH_PROTOCOL
*ImagePath
,
882 EFI_DEVICE_PATH_PROTOCOL
*FilePath
888 SHELL_FILE_HANDLE FileHandle
;
889 EFI_DEVICE_PATH_PROTOCOL
*NewPath
;
890 EFI_DEVICE_PATH_PROTOCOL
*NamePath
;
891 CHAR16
*FileStringPath
;
894 CONST CHAR16
*MapName
;
896 Key
.UnicodeChar
= CHAR_NULL
;
900 if (!ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.Startup
&& ShellInfoObject
.ShellInitSettings
.FileName
!= NULL
) {
902 // launch something else instead
904 NewSize
= StrSize(ShellInfoObject
.ShellInitSettings
.FileName
);
905 if (ShellInfoObject
.ShellInitSettings
.FileOptions
!= NULL
) {
906 NewSize
+= StrSize(ShellInfoObject
.ShellInitSettings
.FileOptions
) + sizeof(CHAR16
);
908 FileStringPath
= AllocateZeroPool(NewSize
);
909 if (FileStringPath
== NULL
) {
910 return (EFI_OUT_OF_RESOURCES
);
912 StrCpy(FileStringPath
, ShellInfoObject
.ShellInitSettings
.FileName
);
913 if (ShellInfoObject
.ShellInitSettings
.FileOptions
!= NULL
) {
914 StrCat(FileStringPath
, L
" ");
915 StrCat(FileStringPath
, ShellInfoObject
.ShellInitSettings
.FileOptions
);
917 Status
= RunCommand(FileStringPath
);
918 FreePool(FileStringPath
);
924 // for shell level 0 we do no scripts
925 // Without the Startup bit overriding we allow for nostartup to prevent scripts
927 if ( (PcdGet8(PcdShellSupportLevel
) < 1)
928 || (ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoStartup
&& !ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.Startup
)
930 return (EFI_SUCCESS
);
933 gST
->ConOut
->EnableCursor(gST
->ConOut
, FALSE
);
935 // print out our warning and see if they press a key
937 for ( Status
= EFI_UNSUPPORTED
, Delay
= ShellInfoObject
.ShellInitSettings
.Delay
938 ; Delay
!= 0 && EFI_ERROR(Status
)
941 ShellPrintHiiEx(0, gST
->ConOut
->Mode
->CursorRow
, NULL
, STRING_TOKEN (STR_SHELL_STARTUP_QUESTION
), ShellInfoObject
.HiiHandle
, Delay
);
942 gBS
->Stall (1000000);
943 if (!ShellInfoObject
.ShellInitSettings
.BitUnion
.Bits
.NoConsoleIn
) {
944 Status
= gST
->ConIn
->ReadKeyStroke (gST
->ConIn
, &Key
);
947 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_CRLF
), ShellInfoObject
.HiiHandle
);
948 gST
->ConOut
->EnableCursor(gST
->ConOut
, TRUE
);
953 if (Status
== EFI_SUCCESS
&& Key
.UnicodeChar
== 0 && Key
.ScanCode
== SCAN_ESC
) {
954 return (EFI_SUCCESS
);
958 // Try the first location (must be file system)
960 MapName
= ShellInfoObject
.NewEfiShellProtocol
->GetMapFromDevicePath(&ImagePath
);
961 if (MapName
!= NULL
) {
962 FileStringPath
= NULL
;
964 FileStringPath
= StrnCatGrow(&FileStringPath
, &NewSize
, MapName
, 0);
965 if (FileStringPath
== NULL
) {
966 Status
= EFI_OUT_OF_RESOURCES
;
968 TempSpot
= StrStr(FileStringPath
, L
";");
969 if (TempSpot
!= NULL
) {
970 *TempSpot
= CHAR_NULL
;
972 FileStringPath
= StrnCatGrow(&FileStringPath
, &NewSize
, ((FILEPATH_DEVICE_PATH
*)FilePath
)->PathName
, 0);
973 PathRemoveLastItem(FileStringPath
);
974 FileStringPath
= StrnCatGrow(&FileStringPath
, &NewSize
, mStartupScript
, 0);
975 Status
= ShellInfoObject
.NewEfiShellProtocol
->OpenFileByName(FileStringPath
, &FileHandle
, EFI_FILE_MODE_READ
);
976 FreePool(FileStringPath
);
979 if (EFI_ERROR(Status
)) {
980 NamePath
= FileDevicePath (NULL
, mStartupScript
);
981 NewPath
= AppendDevicePathNode (ImagePath
, NamePath
);
987 Status
= InternalOpenFileDevicePath(NewPath
, &FileHandle
, EFI_FILE_MODE_READ
, 0);
991 // If we got a file, run it
993 if (!EFI_ERROR(Status
) && FileHandle
!= NULL
) {
994 Status
= RunScriptFileHandle (FileHandle
, mStartupScript
);
995 ShellInfoObject
.NewEfiShellProtocol
->CloseFile(FileHandle
);
997 FileStringPath
= ShellFindFilePath(mStartupScript
);
998 if (FileStringPath
== NULL
) {
1000 // we return success since we dont need to have a startup script
1002 Status
= EFI_SUCCESS
;
1003 ASSERT(FileHandle
== NULL
);
1005 Status
= RunScriptFile(FileStringPath
);
1006 FreePool(FileStringPath
);
1015 Function to perform the shell prompt looping. It will do a single prompt,
1016 dispatch the result, and then return. It is expected that the caller will
1017 call this function in a loop many times.
1020 @retval RETURN_ABORTED
1031 CONST CHAR16
*CurDir
;
1038 // Get screen setting to decide size of the command line buffer
1040 gST
->ConOut
->QueryMode (gST
->ConOut
, gST
->ConOut
->Mode
->Mode
, &Column
, &Row
);
1041 BufferSize
= Column
* Row
* sizeof (CHAR16
);
1042 CmdLine
= AllocateZeroPool (BufferSize
);
1043 if (CmdLine
== NULL
) {
1044 return EFI_OUT_OF_RESOURCES
;
1047 CurDir
= ShellInfoObject
.NewEfiShellProtocol
->GetEnv(L
"cwd");
1052 gST
->ConOut
->SetCursorPosition (gST
->ConOut
, 0, gST
->ConOut
->Mode
->CursorRow
);
1054 if (CurDir
!= NULL
&& StrLen(CurDir
) > 1) {
1055 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_CURDIR
), ShellInfoObject
.HiiHandle
, CurDir
);
1057 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_SHELL
), ShellInfoObject
.HiiHandle
);
1061 // Read a line from the console
1063 Status
= ShellInfoObject
.NewEfiShellProtocol
->ReadFile(ShellInfoObject
.NewShellParametersProtocol
->StdIn
, &BufferSize
, CmdLine
);
1066 // Null terminate the string and parse it
1068 if (!EFI_ERROR (Status
)) {
1069 CmdLine
[BufferSize
/ sizeof (CHAR16
)] = CHAR_NULL
;
1070 Status
= RunCommand(CmdLine
);
1074 // Done with this command
1081 Add a buffer to the Buffer To Free List for safely returning buffers to other
1082 places without risking letting them modify internal shell information.
1084 @param Buffer Something to pass to FreePool when the shell is exiting.
1088 AddBufferToFreeList(
1092 BUFFER_LIST
*BufferListEntry
;
1094 if (Buffer
== NULL
) {
1098 BufferListEntry
= AllocateZeroPool(sizeof(BUFFER_LIST
));
1099 ASSERT(BufferListEntry
!= NULL
);
1100 BufferListEntry
->Buffer
= Buffer
;
1101 InsertTailList(&ShellInfoObject
.BufferToFreeList
.Link
, &BufferListEntry
->Link
);
1106 Add a buffer to the Line History List
1108 @param Buffer The line buffer to add.
1112 AddLineToCommandHistory(
1113 IN CONST CHAR16
*Buffer
1118 Node
= AllocateZeroPool(sizeof(BUFFER_LIST
));
1119 ASSERT(Node
!= NULL
);
1120 Node
->Buffer
= AllocateZeroPool(StrSize(Buffer
));
1121 ASSERT(Node
->Buffer
!= NULL
);
1122 StrCpy(Node
->Buffer
, Buffer
);
1124 InsertTailList(&ShellInfoObject
.ViewingSettings
.CommandHistory
.Link
, &Node
->Link
);
1128 Checks if a string is an alias for another command. If yes, then it replaces the alias name
1129 with the correct command name.
1131 @param[in, out] CommandString Upon entry the potential alias. Upon return the
1132 command name if it was an alias. If it was not
1133 an alias it will be unchanged. This function may
1134 change the buffer to fit the command name.
1136 @retval EFI_SUCCESS The name was changed.
1137 @retval EFI_SUCCESS The name was not an alias.
1138 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
1143 IN OUT CHAR16
**CommandString
1146 CONST CHAR16
*NewString
;
1148 NewString
= ShellInfoObject
.NewEfiShellProtocol
->GetAlias(*CommandString
, NULL
);
1149 if (NewString
== NULL
) {
1150 return (EFI_SUCCESS
);
1152 FreePool(*CommandString
);
1153 *CommandString
= AllocateZeroPool(StrSize(NewString
));
1154 if (*CommandString
== NULL
) {
1155 return (EFI_OUT_OF_RESOURCES
);
1157 StrCpy(*CommandString
, NewString
);
1158 return (EFI_SUCCESS
);
1162 Function allocates a new command line and replaces all instances of environment
1163 variable names that are correctly preset to their values.
1165 If the return value is not NULL the memory must be caller freed.
1167 @param[in] OriginalCommandLine The original command line
1169 @retval NULL An error ocurred.
1170 @return The new command line with no environment variables present.
1174 ShellConvertVariables (
1175 IN CONST CHAR16
*OriginalCommandLine
1178 CONST CHAR16
*MasterEnvList
;
1180 CHAR16
*NewCommandLine1
;
1181 CHAR16
*NewCommandLine2
;
1186 SCRIPT_FILE
*CurrentScriptFile
;
1187 ALIAS_LIST
*AliasListNode
;
1189 ASSERT(OriginalCommandLine
!= NULL
);
1192 NewSize
= StrSize(OriginalCommandLine
);
1193 CurrentScriptFile
= ShellCommandGetCurrentScriptFile();
1196 ///@todo update this to handle the %0 - %9 for scripting only (borrow from line 1256 area) ? ? ?
1199 // calculate the size required for the post-conversion string...
1201 if (CurrentScriptFile
!= NULL
) {
1202 for (AliasListNode
= (ALIAS_LIST
*)GetFirstNode(&CurrentScriptFile
->SubstList
)
1203 ; !IsNull(&CurrentScriptFile
->SubstList
, &AliasListNode
->Link
)
1204 ; AliasListNode
= (ALIAS_LIST
*)GetNextNode(&CurrentScriptFile
->SubstList
, &AliasListNode
->Link
)
1206 for (Temp
= StrStr(OriginalCommandLine
, AliasListNode
->Alias
)
1208 ; Temp
= StrStr(Temp
+1, AliasListNode
->Alias
)
1211 // we need a preceeding and if there is space no ^ preceeding (if no space ignore)
1213 if ((((Temp
-OriginalCommandLine
)>2) && *(Temp
-2) != L
'^') || ((Temp
-OriginalCommandLine
)<=2)) {
1214 NewSize
+= StrSize(AliasListNode
->CommandString
);
1220 for (MasterEnvList
= EfiShellGetEnv(NULL
)
1221 ; MasterEnvList
!= NULL
&& *MasterEnvList
!= CHAR_NULL
//&& *(MasterEnvList+1) != CHAR_NULL
1222 ; MasterEnvList
+= StrLen(MasterEnvList
) + 1
1224 if (StrSize(MasterEnvList
) > ItemSize
) {
1225 ItemSize
= StrSize(MasterEnvList
);
1227 for (Temp
= StrStr(OriginalCommandLine
, MasterEnvList
)
1229 ; Temp
= StrStr(Temp
+1, MasterEnvList
)
1232 // we need a preceeding and following % and if there is space no ^ preceeding (if no space ignore)
1234 if (*(Temp
-1) == L
'%' && *(Temp
+StrLen(MasterEnvList
)) == L
'%' &&
1235 ((((Temp
-OriginalCommandLine
)>2) && *(Temp
-2) != L
'^') || ((Temp
-OriginalCommandLine
)<=2))) {
1236 NewSize
+=StrSize(EfiShellGetEnv(MasterEnvList
));
1242 // now do the replacements...
1244 NewCommandLine1
= AllocateZeroPool(NewSize
);
1245 NewCommandLine2
= AllocateZeroPool(NewSize
);
1246 ItemTemp
= AllocateZeroPool(ItemSize
+(2*sizeof(CHAR16
)));
1247 if (NewCommandLine1
== NULL
|| NewCommandLine2
== NULL
|| ItemTemp
== NULL
) {
1248 SHELL_FREE_NON_NULL(NewCommandLine1
);
1249 SHELL_FREE_NON_NULL(NewCommandLine2
);
1250 SHELL_FREE_NON_NULL(ItemTemp
);
1253 StrCpy(NewCommandLine1
, OriginalCommandLine
);
1254 for (MasterEnvList
= EfiShellGetEnv(NULL
)
1255 ; MasterEnvList
!= NULL
&& *MasterEnvList
!= CHAR_NULL
//&& *(MasterEnvList+1) != CHAR_NULL
1256 ; MasterEnvList
+= StrLen(MasterEnvList
) + 1
1258 StrCpy(ItemTemp
, L
"%");
1259 StrCat(ItemTemp
, MasterEnvList
);
1260 StrCat(ItemTemp
, L
"%");
1261 ShellCopySearchAndReplace(NewCommandLine1
, NewCommandLine2
, NewSize
, ItemTemp
, EfiShellGetEnv(MasterEnvList
), TRUE
, FALSE
);
1262 StrCpy(NewCommandLine1
, NewCommandLine2
);
1264 if (CurrentScriptFile
!= NULL
) {
1265 for (AliasListNode
= (ALIAS_LIST
*)GetFirstNode(&CurrentScriptFile
->SubstList
)
1266 ; !IsNull(&CurrentScriptFile
->SubstList
, &AliasListNode
->Link
)
1267 ; AliasListNode
= (ALIAS_LIST
*)GetNextNode(&CurrentScriptFile
->SubstList
, &AliasListNode
->Link
)
1269 ShellCopySearchAndReplace(NewCommandLine1
, NewCommandLine2
, NewSize
, AliasListNode
->Alias
, AliasListNode
->CommandString
, TRUE
, FALSE
);
1270 StrCpy(NewCommandLine1
, NewCommandLine2
);
1274 // Remove non-existant environment variables in scripts only
1276 for (Temp
= NewCommandLine1
; Temp
!= NULL
; ) {
1277 Temp
= StrStr(Temp
, L
"%");
1281 while (*(Temp
- 1) == L
'^') {
1282 Temp
= StrStr(Temp
+ 1, L
"%");
1291 Temp2
= StrStr(Temp
+ 1, L
"%");
1292 if (Temp2
== NULL
) {
1295 while (*(Temp2
- 1) == L
'^') {
1296 Temp2
= StrStr(Temp2
+ 1, L
"%");
1297 if (Temp2
== NULL
) {
1301 if (Temp2
== NULL
) {
1306 CopyMem(Temp
, Temp2
, StrSize(Temp2
));
1312 // Now cleanup any straggler intentionally ignored "%" characters
1314 ShellCopySearchAndReplace(NewCommandLine1
, NewCommandLine2
, NewSize
, L
"^%", L
"%", TRUE
, FALSE
);
1315 StrCpy(NewCommandLine1
, NewCommandLine2
);
1317 FreePool(NewCommandLine2
);
1320 return (NewCommandLine1
);
1324 Internal function to run a command line with pipe usage.
1326 @param[in] CmdLine The pointer to the command line.
1327 @param[in] StdIn The pointer to the Standard input.
1328 @param[in] StdOut The pointer to the Standard output.
1330 @retval EFI_SUCCESS The split command is executed successfully.
1331 @retval other Some error occurs when executing the split command.
1336 IN CONST CHAR16
*CmdLine
,
1337 IN SHELL_FILE_HANDLE
*StdIn
,
1338 IN SHELL_FILE_HANDLE
*StdOut
1342 CHAR16
*NextCommandLine
;
1343 CHAR16
*OurCommandLine
;
1347 SHELL_FILE_HANDLE
*TempFileHandle
;
1350 ASSERT(StdOut
== NULL
);
1352 ASSERT(StrStr(CmdLine
, L
"|") != NULL
);
1354 Status
= EFI_SUCCESS
;
1355 NextCommandLine
= NULL
;
1356 OurCommandLine
= NULL
;
1360 NextCommandLine
= StrnCatGrow(&NextCommandLine
, &Size1
, StrStr(CmdLine
, L
"|")+1, 0);
1361 OurCommandLine
= StrnCatGrow(&OurCommandLine
, &Size2
, CmdLine
, StrStr(CmdLine
, L
"|") - CmdLine
);
1363 if (NextCommandLine
== NULL
|| OurCommandLine
== NULL
) {
1364 SHELL_FREE_NON_NULL(OurCommandLine
);
1365 SHELL_FREE_NON_NULL(NextCommandLine
);
1366 return (EFI_OUT_OF_RESOURCES
);
1367 } else if (StrStr(OurCommandLine
, L
"|") != NULL
|| Size1
== 0 || Size2
== 0) {
1368 SHELL_FREE_NON_NULL(OurCommandLine
);
1369 SHELL_FREE_NON_NULL(NextCommandLine
);
1370 return (EFI_INVALID_PARAMETER
);
1371 } else if (NextCommandLine
[0] != CHAR_NULL
&&
1372 NextCommandLine
[0] == L
'a' &&
1373 NextCommandLine
[1] == L
' '
1375 CopyMem(NextCommandLine
, NextCommandLine
+1, StrSize(NextCommandLine
) - sizeof(NextCommandLine
[0]));
1383 // make a SPLIT_LIST item and add to list
1385 Split
= AllocateZeroPool(sizeof(SPLIT_LIST
));
1386 ASSERT(Split
!= NULL
);
1387 Split
->SplitStdIn
= StdIn
;
1388 Split
->SplitStdOut
= ConvertEfiFileProtocolToShellHandle(CreateFileInterfaceMem(Unicode
), NULL
);
1389 ASSERT(Split
->SplitStdOut
!= NULL
);
1390 InsertHeadList(&ShellInfoObject
.SplitList
.Link
, &Split
->Link
);
1392 Status
= RunCommand(OurCommandLine
);
1395 // move the output from the first to the in to the second.
1397 TempFileHandle
= Split
->SplitStdOut
;
1398 if (Split
->SplitStdIn
== StdIn
) {
1399 Split
->SplitStdOut
= NULL
;
1401 Split
->SplitStdOut
= Split
->SplitStdIn
;
1403 Split
->SplitStdIn
= TempFileHandle
;
1404 ShellInfoObject
.NewEfiShellProtocol
->SetFilePosition(ConvertShellHandleToEfiFileProtocol(Split
->SplitStdIn
), 0);
1406 if (!EFI_ERROR(Status
)) {
1407 Status
= RunCommand(NextCommandLine
);
1411 // remove the top level from the ScriptList
1413 ASSERT((SPLIT_LIST
*)GetFirstNode(&ShellInfoObject
.SplitList
.Link
) == Split
);
1414 RemoveEntryList(&Split
->Link
);
1417 // Note that the original StdIn is now the StdOut...
1419 if (Split
->SplitStdOut
!= NULL
&& Split
->SplitStdOut
!= StdIn
) {
1420 ShellInfoObject
.NewEfiShellProtocol
->CloseFile(ConvertShellHandleToEfiFileProtocol(Split
->SplitStdOut
));
1422 if (Split
->SplitStdIn
!= NULL
) {
1423 ShellInfoObject
.NewEfiShellProtocol
->CloseFile(ConvertShellHandleToEfiFileProtocol(Split
->SplitStdIn
));
1427 FreePool(NextCommandLine
);
1428 FreePool(OurCommandLine
);
1434 Take the original command line, substitute any variables, free
1435 the original string, return the modified copy
1437 @param[in,out] CmdLine pointer to the command line to update
1438 @param[out]CmdName upon successful return the name of the command to be run
1440 @retval EFI_SUCCESS the function was successful
1441 @retval EFI_OUT_OF_RESOURCES a memory allocation failed
1445 ShellSubstituteVariables(
1450 NewCmdLine
= ShellConvertVariables(*CmdLine
);
1451 SHELL_FREE_NON_NULL(*CmdLine
);
1452 if (NewCmdLine
== NULL
) {
1453 return (EFI_OUT_OF_RESOURCES
);
1455 *CmdLine
= NewCmdLine
;
1456 return (EFI_SUCCESS
);
1460 Take the original command line, substitute any alias in the first group of space delimited characters, free
1461 the original string, return the modified copy
1463 @param[in] CmdLine pointer to the command line to update
1464 @param[out]CmdName upon successful return the name of the command to be run
1466 @retval EFI_SUCCESS the function was successful
1467 @retval EFI_OUT_OF_RESOURCES a memory allocation failed
1471 ShellSubstituteAliases(
1476 CHAR16
*CommandName
;
1478 UINTN PostAliasSize
;
1479 ASSERT(CmdLine
!= NULL
);
1480 ASSERT(*CmdLine
!= NULL
);
1484 if (StrStr((*CmdLine
), L
" ") == NULL
){
1485 StrnCatGrow(&CommandName
, NULL
, (*CmdLine
), 0);
1487 StrnCatGrow(&CommandName
, NULL
, (*CmdLine
), StrStr((*CmdLine
), L
" ") - (*CmdLine
));
1491 // This cannot happen 'inline' since the CmdLine can need extra space.
1494 if (!ShellCommandIsCommandOnList(CommandName
)) {
1496 // Convert via alias
1498 Status
= ShellConvertAlias(&CommandName
);
1499 if (EFI_ERROR(Status
)){
1503 NewCmdLine
= StrnCatGrow(&NewCmdLine
, &PostAliasSize
, CommandName
, 0);
1504 if (NewCmdLine
== NULL
) {
1505 SHELL_FREE_NON_NULL(CommandName
);
1506 SHELL_FREE_NON_NULL(*CmdLine
);
1507 return (EFI_OUT_OF_RESOURCES
);
1509 NewCmdLine
= StrnCatGrow(&NewCmdLine
, &PostAliasSize
, StrStr((*CmdLine
), L
" "), 0);
1510 if (NewCmdLine
== NULL
) {
1511 SHELL_FREE_NON_NULL(CommandName
);
1512 SHELL_FREE_NON_NULL(*CmdLine
);
1513 return (EFI_OUT_OF_RESOURCES
);
1516 NewCmdLine
= StrnCatGrow(&NewCmdLine
, NULL
, (*CmdLine
), 0);
1519 SHELL_FREE_NON_NULL(*CmdLine
);
1520 SHELL_FREE_NON_NULL(CommandName
);
1523 // re-assign the passed in double pointer to point to our newly allocated buffer
1525 *CmdLine
= NewCmdLine
;
1527 return (EFI_SUCCESS
);
1531 Takes the Argv[0] part of the command line and determine the meaning of it.
1533 @param[in] CmdLine pointer to the command line to update
1535 @retval INTERNAL_COMMAND The name is an internal command
1536 @retval FILE_SYS_CHANGE the name is a file system change
1537 @retval SCRIPT_FILE_NAME the name is a NSH script file
1538 @retval UNKNOWN_INVALID the name is unknown
1539 @retval EFI_APPLICATION the name is an application (.EFI)
1541 SHELL_OPERATION_TYPES
1544 IN CONST CHAR16
*CmdName
1547 CHAR16
* FileWithPath
;
1548 CONST CHAR16
* TempLocation
;
1549 CONST CHAR16
* TempLocation2
;
1551 FileWithPath
= NULL
;
1553 // test for an internal command.
1555 if (ShellCommandIsCommandOnList(CmdName
)) {
1556 return (INTERNAL_COMMAND
);
1560 // Test for file system change request. anything ending with : and cant have spaces.
1562 if (CmdName
[(StrLen(CmdName
)-1)] == L
':') {
1563 if (StrStr(CmdName
, L
" ") != NULL
) {
1564 return (UNKNOWN_INVALID
);
1566 return (FILE_SYS_CHANGE
);
1572 if ((FileWithPath
= ShellFindFilePathEx(CmdName
, mExecutableExtensions
)) != NULL
) {
1574 // See if that file has a script file extension
1576 if (StrLen(FileWithPath
) > 4) {
1577 TempLocation
= FileWithPath
+StrLen(FileWithPath
)-4;
1578 TempLocation2
= mScriptExtension
;
1579 if (StringNoCaseCompare((VOID
*)(&TempLocation
), (VOID
*)(&TempLocation2
)) == 0) {
1580 SHELL_FREE_NON_NULL(FileWithPath
);
1581 return (SCRIPT_FILE_NAME
);
1586 // Was a file, but not a script. we treat this as an application.
1588 SHELL_FREE_NON_NULL(FileWithPath
);
1589 return (EFI_APPLICATION
);
1592 SHELL_FREE_NON_NULL(FileWithPath
);
1594 // No clue what this is... return invalid flag...
1596 return (UNKNOWN_INVALID
);
1602 IN CONST CHAR16
*CmdLine
1606 CHAR16
*FirstParameter
;
1612 Temp
= StrnCatGrow(&Temp
, NULL
, CmdLine
, 0);
1614 return (EFI_OUT_OF_RESOURCES
);
1617 FirstParameter
= StrStr(Temp
, L
"|");
1618 if (FirstParameter
!= NULL
) {
1619 *FirstParameter
= CHAR_NULL
;
1622 FirstParameter
= NULL
;
1625 // Process the command line
1627 Status
= ProcessCommandLineToFinal(&Temp
);
1629 if (!EFI_ERROR(Status
)) {
1630 FirstParameter
= AllocateZeroPool(StrSize(CmdLine
));
1631 if (FirstParameter
== NULL
) {
1632 SHELL_FREE_NON_NULL(Temp
);
1633 return (EFI_OUT_OF_RESOURCES
);
1635 TempWalker
= (CHAR16
*)Temp
;
1636 GetNextParameter(&TempWalker
, &FirstParameter
);
1638 if (GetOperationType(FirstParameter
) == UNKNOWN_INVALID
) {
1639 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_NOT_FOUND
), ShellInfoObject
.HiiHandle
, FirstParameter
);
1640 SetLastError(SHELL_NOT_FOUND
);
1641 Status
= EFI_NOT_FOUND
;
1645 SHELL_FREE_NON_NULL(Temp
);
1646 SHELL_FREE_NON_NULL(FirstParameter
);
1651 Determine if a command line contains with a split contains only valid commands
1653 @param[in] CmdLine The command line to parse.
1655 @retval EFI_SUCCESS CmdLine has only valid commands, application, or has no split.
1656 @retval EFI_ABORTED CmdLine has at least one invalid command or application.
1661 IN CONST CHAR16
*CmdLine
1664 CONST CHAR16
*TempSpot
;
1668 // Verify up to the pipe or end character
1670 Status
= IsValidSplit(CmdLine
);
1671 if (EFI_ERROR(Status
)) {
1676 // If this was the only item, then get out
1678 if (!ContainsSplit(CmdLine
)) {
1679 return (EFI_SUCCESS
);
1683 // recurse to verify the next item
1685 TempSpot
= FindSplit(CmdLine
)+1;
1686 return (VerifySplit(TempSpot
));
1690 Process a split based operation.
1692 @param[in] CmdLine pointer to the command line to process
1694 @retval EFI_SUCCESS The operation was successful
1695 @return an error occured.
1699 ProcessNewSplitCommandLine(
1700 IN CONST CHAR16
*CmdLine
1706 Status
= VerifySplit(CmdLine
);
1707 if (EFI_ERROR(Status
)) {
1714 // are we in an existing split???
1716 if (!IsListEmpty(&ShellInfoObject
.SplitList
.Link
)) {
1717 Split
= (SPLIT_LIST
*)GetFirstNode(&ShellInfoObject
.SplitList
.Link
);
1720 if (Split
== NULL
) {
1721 Status
= RunSplitCommand(CmdLine
, NULL
, NULL
);
1723 Status
= RunSplitCommand(CmdLine
, Split
->SplitStdIn
, Split
->SplitStdOut
);
1725 if (EFI_ERROR(Status
)) {
1726 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_INVALID_SPLIT
), ShellInfoObject
.HiiHandle
, CmdLine
);
1732 Handle a request to change the current file system
1734 @param[in] CmdLine The passed in command line
1736 @retval EFI_SUCCESS The operation was successful
1741 IN CONST CHAR16
*CmdLine
1745 Status
= EFI_SUCCESS
;
1748 // make sure we are the right operation
1750 ASSERT(CmdLine
[(StrLen(CmdLine
)-1)] == L
':' && StrStr(CmdLine
, L
" ") == NULL
);
1753 // Call the protocol API to do the work
1755 Status
= ShellInfoObject
.NewEfiShellProtocol
->SetCurDir(NULL
, CmdLine
);
1758 // Report any errors
1760 if (EFI_ERROR(Status
)) {
1761 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_INVALID_MAPPING
), ShellInfoObject
.HiiHandle
, CmdLine
);
1768 Reprocess the command line to direct all -? to the help command.
1770 if found, will add "help" as argv[0], and move the rest later.
1772 @param[in,out] CmdLine pointer to the command line to update
1777 IN OUT CHAR16
**CmdLine
1780 CHAR16
*CurrentParameter
;
1783 CHAR16
*NewCommandLine
;
1786 Status
= EFI_SUCCESS
;
1788 CurrentParameter
= AllocateZeroPool(StrSize(*CmdLine
));
1789 if (CurrentParameter
== NULL
) {
1790 return (EFI_OUT_OF_RESOURCES
);
1794 while(Walker
!= NULL
&& *Walker
!= CHAR_NULL
) {
1795 LastWalker
= Walker
;
1796 GetNextParameter(&Walker
, &CurrentParameter
);
1797 if (StrStr(CurrentParameter
, L
"-?") == CurrentParameter
) {
1798 LastWalker
[0] = L
' ';
1799 LastWalker
[1] = L
' ';
1800 NewCommandLine
= AllocateZeroPool(StrSize(L
"help ") + StrSize(*CmdLine
));
1801 if (NewCommandLine
== NULL
) {
1802 Status
= EFI_OUT_OF_RESOURCES
;
1805 StrCpy(NewCommandLine
, L
"help ");
1806 StrCat(NewCommandLine
, *CmdLine
);
1807 SHELL_FREE_NON_NULL(*CmdLine
);
1808 *CmdLine
= NewCommandLine
;
1813 SHELL_FREE_NON_NULL(CurrentParameter
);
1819 Function to update the shell variable "lasterror"
1821 @param[in] ErrorCode the error code to put into lasterror
1826 IN CONST SHELL_STATUS ErrorCode
1829 CHAR16 LeString
[19];
1830 if (sizeof(EFI_STATUS
) == sizeof(UINT64
)) {
1831 UnicodeSPrint(LeString
, sizeof(LeString
), L
"0x%Lx", ErrorCode
);
1833 UnicodeSPrint(LeString
, sizeof(LeString
), L
"0x%x", ErrorCode
);
1835 DEBUG_CODE(InternalEfiShellSetEnv(L
"debuglasterror", LeString
, TRUE
););
1836 InternalEfiShellSetEnv(L
"lasterror", LeString
, TRUE
);
1838 return (EFI_SUCCESS
);
1842 Converts the command line to it's post-processed form. this replaces variables and alias' per UEFI Shell spec.
1844 @param[in,out] CmdLine pointer to the command line to update
1846 @retval EFI_SUCCESS The operation was successful
1847 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
1848 @return some other error occured
1852 ProcessCommandLineToFinal(
1853 IN OUT CHAR16
**CmdLine
1857 TrimSpaces(CmdLine
);
1859 Status
= ShellSubstituteAliases(CmdLine
);
1860 if (EFI_ERROR(Status
)) {
1864 TrimSpaces(CmdLine
);
1866 Status
= ShellSubstituteVariables(CmdLine
);
1867 if (EFI_ERROR(Status
)) {
1871 TrimSpaces(CmdLine
);
1874 // update for help parsing
1876 if (StrStr(*CmdLine
, L
"?") != NULL
) {
1878 // This may do nothing if the ? does not indicate help.
1879 // Save all the details for in the API below.
1881 Status
= DoHelpUpdate(CmdLine
);
1884 TrimSpaces(CmdLine
);
1886 return (EFI_SUCCESS
);
1890 Run an internal shell command.
1892 This API will upadate the shell's environment since these commands are libraries.
1894 @param[in] CmdLine the command line to run.
1895 @param[in] FirstParameter the first parameter on the command line
1896 @param[in] ParamProtocol the shell parameters protocol pointer
1898 @retval EFI_SUCCESS The command was completed.
1899 @retval EFI_ABORTED The command's operation was aborted.
1904 IN CONST CHAR16
*CmdLine
,
1905 IN CHAR16
*FirstParameter
,
1906 IN EFI_SHELL_PARAMETERS_PROTOCOL
*ParamProtocol
1912 SHELL_STATUS CommandReturnedStatus
;
1916 // get the argc and argv updated for internal commands
1918 Status
= UpdateArgcArgv(ParamProtocol
, CmdLine
, &Argv
, &Argc
);
1919 if (!EFI_ERROR(Status
)) {
1921 // Run the internal command.
1923 Status
= ShellCommandRunCommandHandler(FirstParameter
, &CommandReturnedStatus
, &LastError
);
1925 if (!EFI_ERROR(Status
)) {
1927 // Update last error status.
1928 // some commands do not update last error.
1931 SetLastError(CommandReturnedStatus
);
1935 // Pass thru the exitcode from the app.
1937 if (ShellCommandGetExit()) {
1939 // An Exit was requested ("exit" command), pass its value up.
1941 Status
= CommandReturnedStatus
;
1942 } else if (CommandReturnedStatus
!= SHELL_SUCCESS
&& IsScriptOnlyCommand(FirstParameter
)) {
1944 // Always abort when a script only command fails for any reason
1946 Status
= EFI_ABORTED
;
1947 } else if (ShellCommandGetCurrentScriptFile() != NULL
&& CommandReturnedStatus
== SHELL_ABORTED
) {
1949 // Abort when in a script and a command aborted
1951 Status
= EFI_ABORTED
;
1957 // This is guarenteed to be called after UpdateArgcArgv no matter what else happened.
1958 // This is safe even if the update API failed. In this case, it may be a no-op.
1960 RestoreArgcArgv(ParamProtocol
, &Argv
, &Argc
);
1963 // If a script is running and the command is not a scipt only command, then
1964 // change return value to success so the script won't halt (unless aborted).
1966 // Script only commands have to be able halt the script since the script will
1967 // not operate if they are failing.
1969 if ( ShellCommandGetCurrentScriptFile() != NULL
1970 && !IsScriptOnlyCommand(FirstParameter
)
1971 && Status
!= EFI_ABORTED
1973 Status
= EFI_SUCCESS
;
1980 Function to run the command or file.
1982 @param[in] Type the type of operation being run.
1983 @param[in] CmdLine the command line to run.
1984 @param[in] FirstParameter the first parameter on the command line
1985 @param[in] ParamProtocol the shell parameters protocol pointer
1987 @retval EFI_SUCCESS The command was completed.
1988 @retval EFI_ABORTED The command's operation was aborted.
1993 IN SHELL_OPERATION_TYPES Type
,
1994 IN CONST CHAR16
*CmdLine
,
1995 IN CHAR16
*FirstParameter
,
1996 IN EFI_SHELL_PARAMETERS_PROTOCOL
*ParamProtocol
2000 EFI_STATUS StatusCode
;
2001 CHAR16
*CommandWithPath
;
2002 EFI_DEVICE_PATH_PROTOCOL
*DevPath
;
2004 Status
= EFI_SUCCESS
;
2005 CommandWithPath
= NULL
;
2009 case INTERNAL_COMMAND
:
2010 Status
= RunInternalCommand(CmdLine
, FirstParameter
, ParamProtocol
);
2012 case SCRIPT_FILE_NAME
:
2013 case EFI_APPLICATION
:
2015 // Process a fully qualified path
2017 if (StrStr(FirstParameter
, L
":") != NULL
) {
2018 ASSERT (CommandWithPath
== NULL
);
2019 if (ShellIsFile(FirstParameter
) == EFI_SUCCESS
) {
2020 CommandWithPath
= StrnCatGrow(&CommandWithPath
, NULL
, FirstParameter
, 0);
2025 // Process a relative path and also check in the path environment variable
2027 if (CommandWithPath
== NULL
) {
2028 CommandWithPath
= ShellFindFilePathEx(FirstParameter
, mExecutableExtensions
);
2032 // This should be impossible now.
2034 ASSERT(CommandWithPath
!= NULL
);
2037 // Make sure that path is not just a directory (or not found)
2039 if (!EFI_ERROR(ShellIsDirectory(CommandWithPath
))) {
2040 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_NOT_FOUND
), ShellInfoObject
.HiiHandle
, FirstParameter
);
2041 SetLastError(SHELL_NOT_FOUND
);
2044 case SCRIPT_FILE_NAME
:
2045 Status
= RunScriptFile (CommandWithPath
);
2047 case EFI_APPLICATION
:
2049 // Get the device path of the application image
2051 DevPath
= ShellInfoObject
.NewEfiShellProtocol
->GetDevicePathFromFilePath(CommandWithPath
);
2052 if (DevPath
== NULL
){
2053 Status
= EFI_OUT_OF_RESOURCES
;
2058 // Execute the device path
2060 Status
= InternalShellExecuteDevicePath(
2068 SHELL_FREE_NON_NULL(DevPath
);
2071 // Update last error status.
2073 SetLastError(StatusCode
);
2089 SHELL_FREE_NON_NULL(CommandWithPath
);
2095 Function to setup StdIn, StdErr, StdOut, and then run the command or file.
2097 @param[in] Type the type of operation being run.
2098 @param[in] CmdLine the command line to run.
2099 @param[in] FirstParameter the first parameter on the command line.
2100 @param[in] ParamProtocol the shell parameters protocol pointer
2102 @retval EFI_SUCCESS The command was completed.
2103 @retval EFI_ABORTED The command's operation was aborted.
2107 SetupAndRunCommandOrFile(
2108 IN SHELL_OPERATION_TYPES Type
,
2110 IN CHAR16
*FirstParameter
,
2111 IN EFI_SHELL_PARAMETERS_PROTOCOL
*ParamProtocol
2115 SHELL_FILE_HANDLE OriginalStdIn
;
2116 SHELL_FILE_HANDLE OriginalStdOut
;
2117 SHELL_FILE_HANDLE OriginalStdErr
;
2118 SYSTEM_TABLE_INFO OriginalSystemTableInfo
;
2121 // Update the StdIn, StdOut, and StdErr for redirection to environment variables, files, etc... unicode and ASCII
2123 Status
= UpdateStdInStdOutStdErr(ParamProtocol
, CmdLine
, &OriginalStdIn
, &OriginalStdOut
, &OriginalStdErr
, &OriginalSystemTableInfo
);
2126 // The StdIn, StdOut, and StdErr are set up.
2127 // Now run the command, script, or application
2129 if (!EFI_ERROR(Status
)) {
2130 Status
= RunCommandOrFile(Type
, CmdLine
, FirstParameter
, ParamProtocol
);
2136 if (EFI_ERROR(Status
)) {
2137 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_ERROR
), ShellInfoObject
.HiiHandle
, (VOID
*)(Status
));
2141 // put back the original StdIn, StdOut, and StdErr
2143 RestoreStdInStdOutStdErr(ParamProtocol
, &OriginalStdIn
, &OriginalStdOut
, &OriginalStdErr
, &OriginalSystemTableInfo
);
2149 Function will process and run a command line.
2151 This will determine if the command line represents an internal shell
2152 command or dispatch an external application.
2154 @param[in] CmdLine The command line to parse.
2156 @retval EFI_SUCCESS The command was completed.
2157 @retval EFI_ABORTED The command's operation was aborted.
2162 IN CONST CHAR16
*CmdLine
2166 CHAR16
*CleanOriginal
;
2167 CHAR16
*FirstParameter
;
2169 SHELL_OPERATION_TYPES Type
;
2171 ASSERT(CmdLine
!= NULL
);
2172 if (StrLen(CmdLine
) == 0) {
2173 return (EFI_SUCCESS
);
2176 Status
= EFI_SUCCESS
;
2177 CleanOriginal
= NULL
;
2179 CleanOriginal
= StrnCatGrow(&CleanOriginal
, NULL
, CmdLine
, 0);
2180 if (CleanOriginal
== NULL
) {
2181 return (EFI_OUT_OF_RESOURCES
);
2184 TrimSpaces(&CleanOriginal
);
2187 // Handle case that passed in command line is just 1 or more " " characters.
2189 if (StrLen (CleanOriginal
) == 0) {
2190 SHELL_FREE_NON_NULL(CleanOriginal
);
2191 return (EFI_SUCCESS
);
2194 Status
= ProcessCommandLineToFinal(&CleanOriginal
);
2195 if (EFI_ERROR(Status
)) {
2196 SHELL_FREE_NON_NULL(CleanOriginal
);
2201 // We dont do normal processing with a split command line (output from one command input to another)
2203 if (ContainsSplit(CleanOriginal
)) {
2204 Status
= ProcessNewSplitCommandLine(CleanOriginal
);
2205 SHELL_FREE_NON_NULL(CleanOriginal
);
2210 // We need the first parameter information so we can determine the operation type
2212 FirstParameter
= AllocateZeroPool(StrSize(CleanOriginal
));
2213 if (FirstParameter
== NULL
) {
2214 SHELL_FREE_NON_NULL(CleanOriginal
);
2215 return (EFI_OUT_OF_RESOURCES
);
2217 TempWalker
= CleanOriginal
;
2218 GetNextParameter(&TempWalker
, &FirstParameter
);
2221 // Depending on the first parameter we change the behavior
2223 switch (Type
= GetOperationType(FirstParameter
)) {
2224 case FILE_SYS_CHANGE
:
2225 Status
= ChangeMappedDrive(CleanOriginal
);
2227 case INTERNAL_COMMAND
:
2228 case SCRIPT_FILE_NAME
:
2229 case EFI_APPLICATION
:
2230 Status
= SetupAndRunCommandOrFile(Type
, CleanOriginal
, FirstParameter
, ShellInfoObject
.NewShellParametersProtocol
);
2234 // Whatever was typed, it was invalid.
2236 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_NOT_FOUND
), ShellInfoObject
.HiiHandle
, FirstParameter
);
2237 SetLastError(SHELL_NOT_FOUND
);
2241 SHELL_FREE_NON_NULL(CleanOriginal
);
2242 SHELL_FREE_NON_NULL(FirstParameter
);
2247 STATIC CONST UINT16 InvalidChars
[] = {L
'*', L
'?', L
'<', L
'>', L
'\\', L
'/', L
'\"', 0x0001, 0x0002};
2249 Function determins if the CommandName COULD be a valid command. It does not determine whether
2250 this is a valid command. It only checks for invalid characters.
2252 @param[in] CommandName The name to check
2254 @retval TRUE CommandName could be a command name
2255 @retval FALSE CommandName could not be a valid command name
2260 IN CONST CHAR16
*CommandName
2264 if (CommandName
== NULL
) {
2269 ; Count
< sizeof(InvalidChars
) / sizeof(InvalidChars
[0])
2272 if (ScanMem16(CommandName
, StrSize(CommandName
), InvalidChars
[Count
]) != NULL
) {
2280 Function to process a NSH script file via SHELL_FILE_HANDLE.
2282 @param[in] Handle The handle to the already opened file.
2283 @param[in] Name The name of the script file.
2285 @retval EFI_SUCCESS the script completed sucessfully
2289 RunScriptFileHandle (
2290 IN SHELL_FILE_HANDLE Handle
,
2291 IN CONST CHAR16
*Name
2295 SCRIPT_FILE
*NewScriptFile
;
2297 CHAR16
*CommandLine
;
2298 CHAR16
*CommandLine2
;
2299 CHAR16
*CommandLine3
;
2300 SCRIPT_COMMAND_LIST
*LastCommand
;
2302 BOOLEAN PreScriptEchoState
;
2303 BOOLEAN PreCommandEchoState
;
2304 CONST CHAR16
*CurDir
;
2306 CHAR16 LeString
[50];
2308 ASSERT(!ShellCommandGetScriptExit());
2310 PreScriptEchoState
= ShellCommandGetEchoState();
2312 NewScriptFile
= (SCRIPT_FILE
*)AllocateZeroPool(sizeof(SCRIPT_FILE
));
2313 if (NewScriptFile
== NULL
) {
2314 return (EFI_OUT_OF_RESOURCES
);
2320 ASSERT(NewScriptFile
->ScriptName
== NULL
);
2321 NewScriptFile
->ScriptName
= StrnCatGrow(&NewScriptFile
->ScriptName
, NULL
, Name
, 0);
2322 if (NewScriptFile
->ScriptName
== NULL
) {
2323 DeleteScriptFileStruct(NewScriptFile
);
2324 return (EFI_OUT_OF_RESOURCES
);
2328 // Save the parameters (used to replace %0 to %9 later on)
2330 NewScriptFile
->Argc
= ShellInfoObject
.NewShellParametersProtocol
->Argc
;
2331 if (NewScriptFile
->Argc
!= 0) {
2332 NewScriptFile
->Argv
= (CHAR16
**)AllocateZeroPool(NewScriptFile
->Argc
* sizeof(CHAR16
*));
2333 if (NewScriptFile
->Argv
== NULL
) {
2334 DeleteScriptFileStruct(NewScriptFile
);
2335 return (EFI_OUT_OF_RESOURCES
);
2337 for (LoopVar
= 0 ; LoopVar
< 10 && LoopVar
< NewScriptFile
->Argc
; LoopVar
++) {
2338 ASSERT(NewScriptFile
->Argv
[LoopVar
] == NULL
);
2339 NewScriptFile
->Argv
[LoopVar
] = StrnCatGrow(&NewScriptFile
->Argv
[LoopVar
], NULL
, ShellInfoObject
.NewShellParametersProtocol
->Argv
[LoopVar
], 0);
2340 if (NewScriptFile
->Argv
[LoopVar
] == NULL
) {
2341 DeleteScriptFileStruct(NewScriptFile
);
2342 return (EFI_OUT_OF_RESOURCES
);
2346 NewScriptFile
->Argv
= NULL
;
2349 InitializeListHead(&NewScriptFile
->CommandList
);
2350 InitializeListHead(&NewScriptFile
->SubstList
);
2353 // Now build the list of all script commands.
2356 while(!ShellFileHandleEof(Handle
)) {
2357 CommandLine
= ShellFileHandleReturnLine(Handle
, &Ascii
);
2359 if (CommandLine
== NULL
|| StrLen(CommandLine
) == 0) {
2362 NewScriptFile
->CurrentCommand
= AllocateZeroPool(sizeof(SCRIPT_COMMAND_LIST
));
2363 if (NewScriptFile
->CurrentCommand
== NULL
) {
2364 DeleteScriptFileStruct(NewScriptFile
);
2365 return (EFI_OUT_OF_RESOURCES
);
2368 NewScriptFile
->CurrentCommand
->Cl
= CommandLine
;
2369 NewScriptFile
->CurrentCommand
->Data
= NULL
;
2370 NewScriptFile
->CurrentCommand
->Line
= LineCount
;
2372 InsertTailList(&NewScriptFile
->CommandList
, &NewScriptFile
->CurrentCommand
->Link
);
2376 // Add this as the topmost script file
2378 ShellCommandSetNewScript (NewScriptFile
);
2381 // Now enumerate through the commands and run each one.
2383 CommandLine
= AllocateZeroPool(PcdGet16(PcdShellPrintBufferSize
));
2384 if (CommandLine
== NULL
) {
2385 DeleteScriptFileStruct(NewScriptFile
);
2386 return (EFI_OUT_OF_RESOURCES
);
2388 CommandLine2
= AllocateZeroPool(PcdGet16(PcdShellPrintBufferSize
));
2389 if (CommandLine2
== NULL
) {
2390 FreePool(CommandLine
);
2391 DeleteScriptFileStruct(NewScriptFile
);
2392 return (EFI_OUT_OF_RESOURCES
);
2395 for ( NewScriptFile
->CurrentCommand
= (SCRIPT_COMMAND_LIST
*)GetFirstNode(&NewScriptFile
->CommandList
)
2396 ; !IsNull(&NewScriptFile
->CommandList
, &NewScriptFile
->CurrentCommand
->Link
)
2397 ; // conditional increment in the body of the loop
2399 ASSERT(CommandLine2
!= NULL
);
2400 StrCpy(CommandLine2
, NewScriptFile
->CurrentCommand
->Cl
);
2403 // NULL out comments
2405 for (CommandLine3
= CommandLine2
; CommandLine3
!= NULL
&& *CommandLine3
!= CHAR_NULL
; CommandLine3
++) {
2406 if (*CommandLine3
== L
'^') {
2407 if (*(CommandLine3
+1) == L
'#' || *(CommandLine3
+1) == L
':') {
2408 CopyMem(CommandLine3
, CommandLine3
+1, StrSize(CommandLine3
) - sizeof(CommandLine3
[0]));
2410 } else if (*CommandLine3
== L
'#') {
2411 *CommandLine3
= CHAR_NULL
;
2415 if (CommandLine2
!= NULL
&& StrLen(CommandLine2
) >= 1) {
2417 // Due to variability in starting the find and replace action we need to have both buffers the same.
2419 StrCpy(CommandLine
, CommandLine2
);
2422 // Remove the %0 to %9 from the command line (if we have some arguments)
2424 if (NewScriptFile
->Argv
!= NULL
) {
2425 switch (NewScriptFile
->Argc
) {
2427 Status
= ShellCopySearchAndReplace(CommandLine2
, CommandLine
, PcdGet16 (PcdShellPrintBufferSize
), L
"%9", NewScriptFile
->Argv
[9], FALSE
, TRUE
);
2428 ASSERT_EFI_ERROR(Status
);
2430 Status
= ShellCopySearchAndReplace(CommandLine
, CommandLine2
, PcdGet16 (PcdShellPrintBufferSize
), L
"%8", NewScriptFile
->Argv
[8], FALSE
, TRUE
);
2431 ASSERT_EFI_ERROR(Status
);
2433 Status
= ShellCopySearchAndReplace(CommandLine2
, CommandLine
, PcdGet16 (PcdShellPrintBufferSize
), L
"%7", NewScriptFile
->Argv
[7], FALSE
, TRUE
);
2434 ASSERT_EFI_ERROR(Status
);
2436 Status
= ShellCopySearchAndReplace(CommandLine
, CommandLine2
, PcdGet16 (PcdShellPrintBufferSize
), L
"%6", NewScriptFile
->Argv
[6], FALSE
, TRUE
);
2437 ASSERT_EFI_ERROR(Status
);
2439 Status
= ShellCopySearchAndReplace(CommandLine2
, CommandLine
, PcdGet16 (PcdShellPrintBufferSize
), L
"%5", NewScriptFile
->Argv
[5], FALSE
, TRUE
);
2440 ASSERT_EFI_ERROR(Status
);
2442 Status
= ShellCopySearchAndReplace(CommandLine
, CommandLine2
, PcdGet16 (PcdShellPrintBufferSize
), L
"%4", NewScriptFile
->Argv
[4], FALSE
, TRUE
);
2443 ASSERT_EFI_ERROR(Status
);
2445 Status
= ShellCopySearchAndReplace(CommandLine2
, CommandLine
, PcdGet16 (PcdShellPrintBufferSize
), L
"%3", NewScriptFile
->Argv
[3], FALSE
, TRUE
);
2446 ASSERT_EFI_ERROR(Status
);
2448 Status
= ShellCopySearchAndReplace(CommandLine
, CommandLine2
, PcdGet16 (PcdShellPrintBufferSize
), L
"%2", NewScriptFile
->Argv
[2], FALSE
, TRUE
);
2449 ASSERT_EFI_ERROR(Status
);
2451 Status
= ShellCopySearchAndReplace(CommandLine2
, CommandLine
, PcdGet16 (PcdShellPrintBufferSize
), L
"%1", NewScriptFile
->Argv
[1], FALSE
, TRUE
);
2452 ASSERT_EFI_ERROR(Status
);
2454 Status
= ShellCopySearchAndReplace(CommandLine
, CommandLine2
, PcdGet16 (PcdShellPrintBufferSize
), L
"%0", NewScriptFile
->Argv
[0], FALSE
, TRUE
);
2455 ASSERT_EFI_ERROR(Status
);
2461 Status
= ShellCopySearchAndReplace(CommandLine2
, CommandLine
, PcdGet16 (PcdShellPrintBufferSize
), L
"%1", L
"\"\"", FALSE
, FALSE
);
2462 Status
= ShellCopySearchAndReplace(CommandLine
, CommandLine2
, PcdGet16 (PcdShellPrintBufferSize
), L
"%2", L
"\"\"", FALSE
, FALSE
);
2463 Status
= ShellCopySearchAndReplace(CommandLine2
, CommandLine
, PcdGet16 (PcdShellPrintBufferSize
), L
"%3", L
"\"\"", FALSE
, FALSE
);
2464 Status
= ShellCopySearchAndReplace(CommandLine
, CommandLine2
, PcdGet16 (PcdShellPrintBufferSize
), L
"%4", L
"\"\"", FALSE
, FALSE
);
2465 Status
= ShellCopySearchAndReplace(CommandLine2
, CommandLine
, PcdGet16 (PcdShellPrintBufferSize
), L
"%5", L
"\"\"", FALSE
, FALSE
);
2466 Status
= ShellCopySearchAndReplace(CommandLine
, CommandLine2
, PcdGet16 (PcdShellPrintBufferSize
), L
"%6", L
"\"\"", FALSE
, FALSE
);
2467 Status
= ShellCopySearchAndReplace(CommandLine2
, CommandLine
, PcdGet16 (PcdShellPrintBufferSize
), L
"%7", L
"\"\"", FALSE
, FALSE
);
2468 Status
= ShellCopySearchAndReplace(CommandLine
, CommandLine2
, PcdGet16 (PcdShellPrintBufferSize
), L
"%8", L
"\"\"", FALSE
, FALSE
);
2469 Status
= ShellCopySearchAndReplace(CommandLine2
, CommandLine
, PcdGet16 (PcdShellPrintBufferSize
), L
"%9", L
"\"\"", FALSE
, FALSE
);
2471 StrCpy(CommandLine2
, CommandLine
);
2473 LastCommand
= NewScriptFile
->CurrentCommand
;
2475 for (CommandLine3
= CommandLine2
; CommandLine3
[0] == L
' ' ; CommandLine3
++);
2477 if (CommandLine3
!= NULL
&& CommandLine3
[0] == L
':' ) {
2479 // This line is a goto target / label
2482 if (CommandLine3
!= NULL
&& StrLen(CommandLine3
) > 0) {
2483 if (CommandLine3
[0] == L
'@') {
2485 // We need to save the current echo state
2486 // and disable echo for just this command.
2488 PreCommandEchoState
= ShellCommandGetEchoState();
2489 ShellCommandSetEchoState(FALSE
);
2490 Status
= RunCommand(CommandLine3
+1);
2493 // If command was "@echo -off" or "@echo -on" then don't restore echo state
2495 if (StrCmp (L
"@echo -off", CommandLine3
) != 0 &&
2496 StrCmp (L
"@echo -on", CommandLine3
) != 0) {
2498 // Now restore the pre-'@' echo state.
2500 ShellCommandSetEchoState(PreCommandEchoState
);
2503 if (ShellCommandGetEchoState()) {
2504 CurDir
= ShellInfoObject
.NewEfiShellProtocol
->GetEnv(L
"cwd");
2505 if (CurDir
!= NULL
&& StrLen(CurDir
) > 1) {
2506 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_CURDIR
), ShellInfoObject
.HiiHandle
, CurDir
);
2508 ShellPrintHiiEx(-1, -1, NULL
, STRING_TOKEN (STR_SHELL_SHELL
), ShellInfoObject
.HiiHandle
);
2510 ShellPrintEx(-1, -1, L
"%s\r\n", CommandLine2
);
2512 Status
= RunCommand(CommandLine3
);
2516 if (ShellCommandGetScriptExit()) {
2518 // ShellCommandGetExitCode() always returns a UINT64
2520 UnicodeSPrint(LeString
, sizeof(LeString
), L
"0x%Lx", ShellCommandGetExitCode());
2521 DEBUG_CODE(InternalEfiShellSetEnv(L
"debuglasterror", LeString
, TRUE
););
2522 InternalEfiShellSetEnv(L
"lasterror", LeString
, TRUE
);
2524 ShellCommandRegisterExit(FALSE
, 0);
2525 Status
= EFI_SUCCESS
;
2528 if (ShellGetExecutionBreakFlag()) {
2531 if (EFI_ERROR(Status
)) {
2534 if (ShellCommandGetExit()) {
2539 // If that commend did not update the CurrentCommand then we need to advance it...
2541 if (LastCommand
== NewScriptFile
->CurrentCommand
) {
2542 NewScriptFile
->CurrentCommand
= (SCRIPT_COMMAND_LIST
*)GetNextNode(&NewScriptFile
->CommandList
, &NewScriptFile
->CurrentCommand
->Link
);
2543 if (!IsNull(&NewScriptFile
->CommandList
, &NewScriptFile
->CurrentCommand
->Link
)) {
2544 NewScriptFile
->CurrentCommand
->Reset
= TRUE
;
2548 NewScriptFile
->CurrentCommand
= (SCRIPT_COMMAND_LIST
*)GetNextNode(&NewScriptFile
->CommandList
, &NewScriptFile
->CurrentCommand
->Link
);
2549 if (!IsNull(&NewScriptFile
->CommandList
, &NewScriptFile
->CurrentCommand
->Link
)) {
2550 NewScriptFile
->CurrentCommand
->Reset
= TRUE
;
2556 FreePool(CommandLine
);
2557 FreePool(CommandLine2
);
2558 ShellCommandSetNewScript (NULL
);
2561 // Only if this was the last script reset the state.
2563 if (ShellCommandGetCurrentScriptFile()==NULL
) {
2564 ShellCommandSetEchoState(PreScriptEchoState
);
2566 return (EFI_SUCCESS
);
2570 Function to process a NSH script file.
2572 @param[in] ScriptPath Pointer to the script file name (including file system path).
2574 @retval EFI_SUCCESS the script completed sucessfully
2579 IN CONST CHAR16
*ScriptPath
2583 SHELL_FILE_HANDLE FileHandle
;
2585 if (ShellIsFile(ScriptPath
) != EFI_SUCCESS
) {
2586 return (EFI_INVALID_PARAMETER
);
2589 Status
= ShellOpenFileByName(ScriptPath
, &FileHandle
, EFI_FILE_MODE_READ
, 0);
2590 if (EFI_ERROR(Status
)) {
2594 Status
= RunScriptFileHandle(FileHandle
, ScriptPath
);
2596 ShellCloseFile(&FileHandle
);