2 Provides interface to shell internal functions for shell commands.
4 (C) Copyright 2013-2014, Hewlett-Packard Development Company, L.P.
5 Copyright (c) 2009 - 2014, Intel Corporation. All rights reserved.<BR>
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.
16 #include "UefiShellCommandLib.h"
18 // STATIC local variables
19 STATIC SHELL_COMMAND_INTERNAL_LIST_ENTRY mCommandList
;
20 STATIC SCRIPT_FILE_LIST mScriptList
;
21 STATIC ALIAS_LIST mAliasList
;
22 STATIC BOOLEAN mEchoState
;
23 STATIC BOOLEAN mExitRequested
;
24 STATIC UINT64 mExitCode
;
25 STATIC BOOLEAN mExitScript
;
26 STATIC CHAR16
*mProfileList
;
27 STATIC UINTN mProfileListSize
;
28 STATIC UINTN mFsMaxCount
= 0;
29 STATIC UINTN mBlkMaxCount
= 0;
30 STATIC BUFFER_LIST mFileHandleList
;
32 // global variables required by library class.
33 EFI_UNICODE_COLLATION_PROTOCOL
*gUnicodeCollation
= NULL
;
34 SHELL_MAP_LIST gShellMapList
;
35 SHELL_MAP_LIST
*gShellCurDir
= NULL
;
37 CONST CHAR16
* SupportLevel
[] = {
45 Function to make sure that the global protocol pointers are valid.
46 must be called after constructor before accessing the pointers.
55 if (gUnicodeCollation
== NULL
) {
56 Status
= gBS
->LocateProtocol(&gEfiUnicodeCollation2ProtocolGuid
, NULL
, (VOID
**)&gUnicodeCollation
);
57 if (EFI_ERROR(Status
)) {
58 return (EFI_DEVICE_ERROR
);
65 Constructor for the Shell Command library.
67 Initialize the library and determine if the underlying is a UEFI Shell 2.0 or an EFI shell.
69 @param ImageHandle the image handle of the process
70 @param SystemTable the EFI System Table pointer
72 @retval EFI_SUCCESS the initialization was complete sucessfully
76 ShellCommandLibConstructor (
77 IN EFI_HANDLE ImageHandle
,
78 IN EFI_SYSTEM_TABLE
*SystemTable
82 InitializeListHead(&gShellMapList
.Link
);
83 InitializeListHead(&mCommandList
.Link
);
84 InitializeListHead(&mAliasList
.Link
);
85 InitializeListHead(&mScriptList
.Link
);
86 InitializeListHead(&mFileHandleList
.Link
);
89 mExitRequested
= FALSE
;
94 if (gUnicodeCollation
== NULL
) {
95 Status
= gBS
->LocateProtocol(&gEfiUnicodeCollation2ProtocolGuid
, NULL
, (VOID
**)&gUnicodeCollation
);
96 if (EFI_ERROR(Status
)) {
97 return (EFI_DEVICE_ERROR
);
101 return (RETURN_SUCCESS
);
105 Frees list of file handles.
107 @param[in] List The list to free.
115 BUFFER_LIST
*BufferListEntry
;
121 // enumerate through the buffer list and free all memory
123 for ( BufferListEntry
= ( BUFFER_LIST
*)GetFirstNode(&List
->Link
)
124 ; !IsListEmpty (&List
->Link
)
125 ; BufferListEntry
= (BUFFER_LIST
*)GetFirstNode(&List
->Link
)
127 RemoveEntryList(&BufferListEntry
->Link
);
128 ASSERT(BufferListEntry
->Buffer
!= NULL
);
129 SHELL_FREE_NON_NULL(((SHELL_COMMAND_FILE_HANDLE
*)(BufferListEntry
->Buffer
))->Path
);
130 SHELL_FREE_NON_NULL(BufferListEntry
->Buffer
);
131 SHELL_FREE_NON_NULL(BufferListEntry
);
136 Destructor for the library. free any resources.
138 @param ImageHandle the image handle of the process
139 @param SystemTable the EFI System Table pointer
141 @retval RETURN_SUCCESS this function always returns success
145 ShellCommandLibDestructor (
146 IN EFI_HANDLE ImageHandle
,
147 IN EFI_SYSTEM_TABLE
*SystemTable
150 SHELL_COMMAND_INTERNAL_LIST_ENTRY
*Node
;
152 SCRIPT_FILE_LIST
*Node3
;
153 SHELL_MAP_LIST
*MapNode
;
155 // enumerate throught the list and free all the memory
157 while (!IsListEmpty (&mCommandList
.Link
)) {
158 Node
= (SHELL_COMMAND_INTERNAL_LIST_ENTRY
*)GetFirstNode(&mCommandList
.Link
);
159 RemoveEntryList(&Node
->Link
);
160 SHELL_FREE_NON_NULL(Node
->CommandString
);
162 DEBUG_CODE(Node
= NULL
;);
166 // enumerate through the alias list and free all memory
168 while (!IsListEmpty (&mAliasList
.Link
)) {
169 Node2
= (ALIAS_LIST
*)GetFirstNode(&mAliasList
.Link
);
170 RemoveEntryList(&Node2
->Link
);
171 SHELL_FREE_NON_NULL(Node2
->CommandString
);
172 SHELL_FREE_NON_NULL(Node2
->Alias
);
173 SHELL_FREE_NON_NULL(Node2
);
174 DEBUG_CODE(Node2
= NULL
;);
178 // enumerate throught the list and free all the memory
180 while (!IsListEmpty (&mScriptList
.Link
)) {
181 Node3
= (SCRIPT_FILE_LIST
*)GetFirstNode(&mScriptList
.Link
);
182 RemoveEntryList(&Node3
->Link
);
183 DeleteScriptFileStruct(Node3
->Data
);
188 // enumerate throught the mappings list and free all the memory
190 if (!IsListEmpty(&gShellMapList
.Link
)) {
191 for (MapNode
= (SHELL_MAP_LIST
*)GetFirstNode(&gShellMapList
.Link
)
192 ; !IsListEmpty (&gShellMapList
.Link
)
193 ; MapNode
= (SHELL_MAP_LIST
*)GetFirstNode(&gShellMapList
.Link
)
195 ASSERT(MapNode
!= NULL
);
196 RemoveEntryList(&MapNode
->Link
);
197 SHELL_FREE_NON_NULL(MapNode
->DevicePath
);
198 SHELL_FREE_NON_NULL(MapNode
->MapName
);
199 SHELL_FREE_NON_NULL(MapNode
->CurrentDirectoryPath
);
203 if (!IsListEmpty(&mFileHandleList
.Link
)){
204 FreeFileHandleList(&mFileHandleList
);
207 if (mProfileList
!= NULL
) {
208 FreePool(mProfileList
);
211 gUnicodeCollation
= NULL
;
214 return (RETURN_SUCCESS
);
218 Find a dynamic command protocol instance given a command name string
220 @param CommandString the command name string
222 @return instance the command protocol instance, if dynamic command instance found
223 @retval NULL no dynamic command protocol instance found for name
225 CONST EFI_SHELL_DYNAMIC_COMMAND_PROTOCOL
*
227 ShellCommandFindDynamicCommand (
228 IN CONST CHAR16
*CommandString
232 EFI_HANDLE
*CommandHandleList
;
233 EFI_HANDLE
*NextCommand
;
234 EFI_SHELL_DYNAMIC_COMMAND_PROTOCOL
*DynamicCommand
;
236 CommandHandleList
= GetHandleListByProtocol(&gEfiShellDynamicCommandProtocolGuid
);
237 if (CommandHandleList
== NULL
) {
239 // not found or out of resources
244 for (NextCommand
= CommandHandleList
; *NextCommand
!= NULL
; NextCommand
++) {
245 Status
= gBS
->HandleProtocol(
247 &gEfiShellDynamicCommandProtocolGuid
,
248 (VOID
**)&DynamicCommand
251 if (EFI_ERROR(Status
)) {
255 if (gUnicodeCollation
->StriColl(
257 (CHAR16
*)CommandString
,
258 (CHAR16
*)DynamicCommand
->CommandName
) == 0
260 FreePool(CommandHandleList
);
261 return (DynamicCommand
);
265 FreePool(CommandHandleList
);
270 Checks if a command exists as a dynamic command protocol instance
272 @param[in] CommandString The command string to check for on the list.
276 ShellCommandDynamicCommandExists (
277 IN CONST CHAR16
*CommandString
280 return (ShellCommandFindDynamicCommand(CommandString
) != NULL
);
284 Checks if a command is already on the internal command list.
286 @param[in] CommandString The command string to check for on the list.
290 ShellCommandIsCommandOnInternalList(
291 IN CONST CHAR16
*CommandString
294 SHELL_COMMAND_INTERNAL_LIST_ENTRY
*Node
;
297 // assert for NULL parameter
299 ASSERT(CommandString
!= NULL
);
302 // check for the command
304 for ( Node
= (SHELL_COMMAND_INTERNAL_LIST_ENTRY
*)GetFirstNode(&mCommandList
.Link
)
305 ; !IsNull(&mCommandList
.Link
, &Node
->Link
)
306 ; Node
= (SHELL_COMMAND_INTERNAL_LIST_ENTRY
*)GetNextNode(&mCommandList
.Link
, &Node
->Link
)
308 ASSERT(Node
->CommandString
!= NULL
);
309 if (gUnicodeCollation
->StriColl(
311 (CHAR16
*)CommandString
,
312 Node
->CommandString
) == 0
321 Checks if a command exists, either internally or through the dynamic command protocol.
323 @param[in] CommandString The command string to check for on the list.
327 ShellCommandIsCommandOnList(
328 IN CONST CHAR16
*CommandString
331 if (ShellCommandIsCommandOnInternalList(CommandString
)) {
335 return ShellCommandDynamicCommandExists(CommandString
);
339 Get the help text for a dynamic command.
341 @param[in] CommandString The command name.
343 @retval NULL No help text was found.
344 @return String of help text. Caller required to free.
348 ShellCommandGetDynamicCommandHelp(
349 IN CONST CHAR16
*CommandString
352 EFI_SHELL_DYNAMIC_COMMAND_PROTOCOL
*DynamicCommand
;
354 DynamicCommand
= (EFI_SHELL_DYNAMIC_COMMAND_PROTOCOL
*)ShellCommandFindDynamicCommand(CommandString
);
355 if (DynamicCommand
== NULL
) {
360 // TODO: how to get proper language?
362 return DynamicCommand
->GetHelp(DynamicCommand
, "en");
366 Get the help text for an internal command.
368 @param[in] CommandString The command name.
370 @retval NULL No help text was found.
371 @return String of help text. Caller reuiqred to free.
375 ShellCommandGetInternalCommandHelp(
376 IN CONST CHAR16
*CommandString
379 SHELL_COMMAND_INTERNAL_LIST_ENTRY
*Node
;
382 // assert for NULL parameter
384 ASSERT(CommandString
!= NULL
);
387 // check for the command
389 for ( Node
= (SHELL_COMMAND_INTERNAL_LIST_ENTRY
*)GetFirstNode(&mCommandList
.Link
)
390 ; !IsNull(&mCommandList
.Link
, &Node
->Link
)
391 ; Node
= (SHELL_COMMAND_INTERNAL_LIST_ENTRY
*)GetNextNode(&mCommandList
.Link
, &Node
->Link
)
393 ASSERT(Node
->CommandString
!= NULL
);
394 if (gUnicodeCollation
->StriColl(
396 (CHAR16
*)CommandString
,
397 Node
->CommandString
) == 0
399 return (HiiGetString(Node
->HiiHandle
, Node
->ManFormatHelp
, NULL
));
406 Get the help text for a command.
408 @param[in] CommandString The command name.
410 @retval NULL No help text was found.
411 @return String of help text.Caller reuiqred to free.
415 ShellCommandGetCommandHelp (
416 IN CONST CHAR16
*CommandString
420 HelpStr
= ShellCommandGetInternalCommandHelp(CommandString
);
422 if (HelpStr
== NULL
) {
423 HelpStr
= ShellCommandGetDynamicCommandHelp(CommandString
);
431 Registers handlers of type SHELL_RUN_COMMAND and
432 SHELL_GET_MAN_FILENAME for each shell command.
434 If the ShellSupportLevel is greater than the value of the
435 PcdShellSupportLevel then return RETURN_UNSUPPORTED.
437 Registers the handlers specified by GetHelpInfoHandler and CommandHandler
438 with the command specified by CommandString. If the command named by
439 CommandString has already been registered, then return
440 RETURN_ALREADY_STARTED.
442 If there are not enough resources available to register the handlers then
443 RETURN_OUT_OF_RESOURCES is returned.
445 If CommandString is NULL, then ASSERT().
446 If GetHelpInfoHandler is NULL, then ASSERT().
447 If CommandHandler is NULL, then ASSERT().
448 If ProfileName is NULL, then ASSERT().
450 @param[in] CommandString Pointer to the command name. This is the
451 name to look for on the command line in
453 @param[in] CommandHandler Pointer to a function that runs the
455 @param[in] GetManFileName Pointer to a function that provides man
457 @param[in] ShellMinSupportLevel minimum Shell Support Level which has this
459 @param[in] ProfileName profile name to require for support of this
461 @param[in] CanAffectLE indicates whether this command's return value
462 can change the LASTERROR environment variable.
463 @param[in] HiiHandle Handle of this command's HII entry.
464 @param[in] ManFormatHelp HII locator for the help text.
466 @retval RETURN_SUCCESS The handlers were registered.
467 @retval RETURN_OUT_OF_RESOURCES There are not enough resources available to
468 register the shell command.
469 @retval RETURN_UNSUPPORTED the ShellMinSupportLevel was higher than the
470 currently allowed support level.
471 @retval RETURN_ALREADY_STARTED The CommandString represents a command that
472 is already registered. Only 1 handler set for
473 a given command is allowed.
474 @sa SHELL_GET_MAN_FILENAME
475 @sa SHELL_RUN_COMMAND
479 ShellCommandRegisterCommandName (
480 IN CONST CHAR16
*CommandString
,
481 IN SHELL_RUN_COMMAND CommandHandler
,
482 IN SHELL_GET_MAN_FILENAME GetManFileName
,
483 IN UINT32 ShellMinSupportLevel
,
484 IN CONST CHAR16
*ProfileName
,
485 IN CONST BOOLEAN CanAffectLE
,
486 IN CONST EFI_HANDLE HiiHandle
,
487 IN CONST EFI_STRING_ID ManFormatHelp
490 SHELL_COMMAND_INTERNAL_LIST_ENTRY
*Node
;
491 SHELL_COMMAND_INTERNAL_LIST_ENTRY
*Command
;
492 SHELL_COMMAND_INTERNAL_LIST_ENTRY
*PrevCommand
;
493 INTN LexicalMatchValue
;
496 // Initialize local variables.
500 LexicalMatchValue
= 0;
503 // ASSERTs for NULL parameters
505 ASSERT(CommandString
!= NULL
);
506 ASSERT(GetManFileName
!= NULL
);
507 ASSERT(CommandHandler
!= NULL
);
508 ASSERT(ProfileName
!= NULL
);
511 // check for shell support level
513 if (PcdGet8(PcdShellSupportLevel
) < ShellMinSupportLevel
) {
514 return (RETURN_UNSUPPORTED
);
518 // check for already on the list
520 if (ShellCommandIsCommandOnList(CommandString
)) {
521 return (RETURN_ALREADY_STARTED
);
525 // allocate memory for new struct
527 Node
= AllocateZeroPool(sizeof(SHELL_COMMAND_INTERNAL_LIST_ENTRY
));
528 ASSERT(Node
!= NULL
);
529 Node
->CommandString
= AllocateZeroPool(StrSize(CommandString
));
530 ASSERT(Node
->CommandString
!= NULL
);
533 // populate the new struct
535 StrCpy(Node
->CommandString
, CommandString
);
537 Node
->GetManFileName
= GetManFileName
;
538 Node
->CommandHandler
= CommandHandler
;
539 Node
->LastError
= CanAffectLE
;
540 Node
->HiiHandle
= HiiHandle
;
541 Node
->ManFormatHelp
= ManFormatHelp
;
543 if ( StrLen(ProfileName
)>0
544 && ((mProfileList
!= NULL
545 && StrStr(mProfileList
, ProfileName
) == NULL
) || mProfileList
== NULL
)
547 ASSERT((mProfileList
== NULL
&& mProfileListSize
== 0) || (mProfileList
!= NULL
));
548 if (mProfileList
== NULL
) {
550 // If this is the first make a leading ';'
552 StrnCatGrow(&mProfileList
, &mProfileListSize
, L
";", 0);
554 StrnCatGrow(&mProfileList
, &mProfileListSize
, ProfileName
, 0);
555 StrnCatGrow(&mProfileList
, &mProfileListSize
, L
";", 0);
559 // Insert a new entry on top of the list
561 InsertHeadList (&mCommandList
.Link
, &Node
->Link
);
564 // Move a new registered command to its sorted ordered location in the list
566 for (Command
= (SHELL_COMMAND_INTERNAL_LIST_ENTRY
*)GetFirstNode (&mCommandList
.Link
),
567 PrevCommand
= (SHELL_COMMAND_INTERNAL_LIST_ENTRY
*)GetFirstNode (&mCommandList
.Link
)
568 ; !IsNull (&mCommandList
.Link
, &Command
->Link
)
569 ; Command
= (SHELL_COMMAND_INTERNAL_LIST_ENTRY
*)GetNextNode (&mCommandList
.Link
, &Command
->Link
)) {
572 // Get Lexical Comparison Value between PrevCommand and Command list entry
574 LexicalMatchValue
= gUnicodeCollation
->StriColl (
576 PrevCommand
->CommandString
,
577 Command
->CommandString
581 // Swap PrevCommand and Command list entry if PrevCommand list entry
582 // is alphabetically greater than Command list entry
584 if (LexicalMatchValue
> 0){
585 Command
= (SHELL_COMMAND_INTERNAL_LIST_ENTRY
*) SwapListEntries (&PrevCommand
->Link
, &Command
->Link
);
586 } else if (LexicalMatchValue
< 0) {
588 // PrevCommand entry is lexically lower than Command entry
594 return (RETURN_SUCCESS
);
598 Function to get the current Profile string.
600 @retval NULL There are no installed profiles.
601 @return A semi-colon delimited list of profiles.
605 ShellCommandGetProfileList (
609 return (mProfileList
);
613 Checks if a command string has been registered for CommandString and if so it runs
614 the previously registered handler for that command with the command line.
616 If CommandString is NULL, then ASSERT().
618 If Sections is specified, then each section name listed will be compared in a casesensitive
619 manner, to the section names described in Appendix B UEFI Shell 2.0 spec. If the section exists,
620 it will be appended to the returned help text. If the section does not exist, no
621 information will be returned. If Sections is NULL, then all help text information
622 available will be returned.
624 @param[in] CommandString Pointer to the command name. This is the name
625 found on the command line in the shell.
626 @param[in, out] RetVal Pointer to the return vaule from the command handler.
628 @param[in, out] CanAffectLE indicates whether this command's return value
629 needs to be placed into LASTERROR environment variable.
631 @retval RETURN_SUCCESS The handler was run.
632 @retval RETURN_NOT_FOUND The CommandString did not match a registered
634 @sa SHELL_RUN_COMMAND
638 ShellCommandRunCommandHandler (
639 IN CONST CHAR16
*CommandString
,
640 IN OUT SHELL_STATUS
*RetVal
,
641 IN OUT BOOLEAN
*CanAffectLE OPTIONAL
644 SHELL_COMMAND_INTERNAL_LIST_ENTRY
*Node
;
645 EFI_SHELL_DYNAMIC_COMMAND_PROTOCOL
*DynamicCommand
;
648 // assert for NULL parameters
650 ASSERT(CommandString
!= NULL
);
653 // check for the command
655 for ( Node
= (SHELL_COMMAND_INTERNAL_LIST_ENTRY
*)GetFirstNode(&mCommandList
.Link
)
656 ; !IsNull(&mCommandList
.Link
, &Node
->Link
)
657 ; Node
= (SHELL_COMMAND_INTERNAL_LIST_ENTRY
*)GetNextNode(&mCommandList
.Link
, &Node
->Link
)
659 ASSERT(Node
->CommandString
!= NULL
);
660 if (gUnicodeCollation
->StriColl(
662 (CHAR16
*)CommandString
,
663 Node
->CommandString
) == 0
665 if (CanAffectLE
!= NULL
) {
666 *CanAffectLE
= Node
->LastError
;
668 if (RetVal
!= NULL
) {
669 *RetVal
= Node
->CommandHandler(NULL
, gST
);
671 Node
->CommandHandler(NULL
, gST
);
673 return (RETURN_SUCCESS
);
678 // An internal command was not found, try to find a dynamic command
680 DynamicCommand
= (EFI_SHELL_DYNAMIC_COMMAND_PROTOCOL
*)ShellCommandFindDynamicCommand(CommandString
);
681 if (DynamicCommand
!= NULL
) {
682 if (RetVal
!= NULL
) {
683 *RetVal
= DynamicCommand
->Handler(DynamicCommand
, gST
, gEfiShellParametersProtocol
, gEfiShellProtocol
);
685 DynamicCommand
->Handler(DynamicCommand
, gST
, gEfiShellParametersProtocol
, gEfiShellProtocol
);
687 return (RETURN_SUCCESS
);
690 return (RETURN_NOT_FOUND
);
694 Checks if a command string has been registered for CommandString and if so it
695 returns the MAN filename specified for that command.
697 If CommandString is NULL, then ASSERT().
699 @param[in] CommandString Pointer to the command name. This is the name
700 found on the command line in the shell.\
702 @retval NULL the commandString was not a registered command.
703 @return other the name of the MAN file.
704 @sa SHELL_GET_MAN_FILENAME
708 ShellCommandGetManFileNameHandler (
709 IN CONST CHAR16
*CommandString
712 SHELL_COMMAND_INTERNAL_LIST_ENTRY
*Node
;
715 // assert for NULL parameters
717 ASSERT(CommandString
!= NULL
);
720 // check for the command
722 for ( Node
= (SHELL_COMMAND_INTERNAL_LIST_ENTRY
*)GetFirstNode(&mCommandList
.Link
)
723 ; !IsNull(&mCommandList
.Link
, &Node
->Link
)
724 ; Node
= (SHELL_COMMAND_INTERNAL_LIST_ENTRY
*)GetNextNode(&mCommandList
.Link
, &Node
->Link
)
726 ASSERT(Node
->CommandString
!= NULL
);
727 if (gUnicodeCollation
->StriColl(
729 (CHAR16
*)CommandString
,
730 Node
->CommandString
) == 0
732 return (Node
->GetManFileName());
739 Get the list of all available shell internal commands. This is a linked list
740 (via LIST_ENTRY structure). enumerate through it using the BaseLib linked
741 list functions. do not modify the values.
743 @param[in] Sort TRUE to alphabetically sort the values first. FALSE otherwise.
745 @return a Linked list of all available shell commands.
749 ShellCommandGetCommandList (
750 IN CONST BOOLEAN Sort
754 // return ((COMMAND_LIST*)(&mCommandList));
756 return ((COMMAND_LIST
*)(&mCommandList
));
760 Registers aliases to be set as part of the initialization of the shell application.
762 If Command is NULL, then ASSERT().
763 If Alias is NULL, then ASSERT().
765 @param[in] Command Pointer to the Command
766 @param[in] Alias Pointer to Alias
768 @retval RETURN_SUCCESS The handlers were registered.
769 @retval RETURN_OUT_OF_RESOURCES There are not enough resources available to
770 register the shell command.
774 ShellCommandRegisterAlias (
775 IN CONST CHAR16
*Command
,
776 IN CONST CHAR16
*Alias
784 ASSERT(Command
!= NULL
);
785 ASSERT(Alias
!= NULL
);
788 // allocate memory for new struct
790 Node
= AllocateZeroPool(sizeof(ALIAS_LIST
));
791 ASSERT(Node
!= NULL
);
792 Node
->CommandString
= AllocateZeroPool(StrSize(Command
));
793 Node
->Alias
= AllocateZeroPool(StrSize(Alias
));
794 ASSERT(Node
->CommandString
!= NULL
);
795 ASSERT(Node
->Alias
!= NULL
);
798 // populate the new struct
800 StrCpy(Node
->CommandString
, Command
);
801 StrCpy(Node
->Alias
, Alias
);
804 // add the new struct to the list
806 InsertTailList (&mAliasList
.Link
, &Node
->Link
);
808 return (RETURN_SUCCESS
);
812 Get the list of all shell alias commands. This is a linked list
813 (via LIST_ENTRY structure). enumerate through it using the BaseLib linked
814 list functions. do not modify the values.
816 @return a Linked list of all requested shell alias'.
820 ShellCommandGetInitAliasList (
824 return (&mAliasList
);
828 Determine if a given alias is on the list of built in alias'.
830 @param[in] Alias The alias to test for
832 @retval TRUE The alias is a built in alias
833 @retval FALSE The alias is not a built in alias
837 ShellCommandIsOnAliasList(
838 IN CONST CHAR16
*Alias
844 // assert for NULL parameter
846 ASSERT(Alias
!= NULL
);
849 // check for the Alias
851 for ( Node
= (ALIAS_LIST
*)GetFirstNode(&mAliasList
.Link
)
852 ; !IsNull(&mAliasList
.Link
, &Node
->Link
)
853 ; Node
= (ALIAS_LIST
*)GetNextNode(&mAliasList
.Link
, &Node
->Link
)
855 ASSERT(Node
->CommandString
!= NULL
);
856 ASSERT(Node
->Alias
!= NULL
);
857 if (gUnicodeCollation
->StriColl(
860 Node
->CommandString
) == 0
864 if (gUnicodeCollation
->StriColl(
876 Function to determine current state of ECHO. Echo determins if lines from scripts
877 and ECHO commands are enabled.
879 @retval TRUE Echo is currently enabled
880 @retval FALSE Echo is currently disabled
884 ShellCommandGetEchoState(
892 Function to set current state of ECHO. Echo determins if lines from scripts
893 and ECHO commands are enabled.
895 If State is TRUE, Echo will be enabled.
896 If State is FALSE, Echo will be disabled.
898 @param[in] State How to set echo.
902 ShellCommandSetEchoState(
910 Indicate that the current shell or script should exit.
912 @param[in] ScriptOnly TRUE if exiting a script; FALSE otherwise.
913 @param[in] ErrorCode The 64 bit error code to return.
917 ShellCommandRegisterExit (
918 IN BOOLEAN ScriptOnly
,
919 IN CONST UINT64 ErrorCode
922 mExitRequested
= (BOOLEAN
)(!mExitRequested
);
923 if (mExitRequested
) {
924 mExitScript
= ScriptOnly
;
928 mExitCode
= ErrorCode
;
932 Retrieve the Exit indicator.
934 @retval TRUE Exit was indicated.
935 @retval FALSE Exis was not indicated.
939 ShellCommandGetExit (
943 return (mExitRequested
);
947 Retrieve the Exit code.
949 If ShellCommandGetExit returns FALSE than the return from this is undefined.
951 @return the value passed into RegisterExit.
955 ShellCommandGetExitCode (
962 Retrieve the Exit script indicator.
964 If ShellCommandGetExit returns FALSE than the return from this is undefined.
966 @retval TRUE ScriptOnly was indicated.
967 @retval FALSE ScriptOnly was not indicated.
971 ShellCommandGetScriptExit (
975 return (mExitScript
);
979 Function to cleanup all memory from a SCRIPT_FILE structure.
981 @param[in] Script The pointer to the structure to cleanup.
985 DeleteScriptFileStruct (
986 IN SCRIPT_FILE
*Script
991 if (Script
== NULL
) {
995 for (LoopVar
= 0 ; LoopVar
< Script
->Argc
; LoopVar
++) {
996 SHELL_FREE_NON_NULL(Script
->Argv
[LoopVar
]);
998 if (Script
->Argv
!= NULL
) {
999 SHELL_FREE_NON_NULL(Script
->Argv
);
1001 Script
->CurrentCommand
= NULL
;
1002 while (!IsListEmpty (&Script
->CommandList
)) {
1003 Script
->CurrentCommand
= (SCRIPT_COMMAND_LIST
*)GetFirstNode(&Script
->CommandList
);
1004 if (Script
->CurrentCommand
!= NULL
) {
1005 RemoveEntryList(&Script
->CurrentCommand
->Link
);
1006 if (Script
->CurrentCommand
->Cl
!= NULL
) {
1007 SHELL_FREE_NON_NULL(Script
->CurrentCommand
->Cl
);
1009 if (Script
->CurrentCommand
->Data
!= NULL
) {
1010 SHELL_FREE_NON_NULL(Script
->CurrentCommand
->Data
);
1012 SHELL_FREE_NON_NULL(Script
->CurrentCommand
);
1015 SHELL_FREE_NON_NULL(Script
->ScriptName
);
1016 SHELL_FREE_NON_NULL(Script
);
1020 Function to return a pointer to the currently running script file object.
1022 @retval NULL A script file is not currently running.
1023 @return A pointer to the current script file object.
1027 ShellCommandGetCurrentScriptFile (
1031 SCRIPT_FILE_LIST
*List
;
1032 if (IsListEmpty (&mScriptList
.Link
)) {
1035 List
= ((SCRIPT_FILE_LIST
*)GetFirstNode(&mScriptList
.Link
));
1036 return (List
->Data
);
1040 Function to set a new script as the currently running one.
1042 This function will correctly stack and unstack nested scripts.
1044 @param[in] Script Pointer to new script information structure. if NULL
1045 will remove and de-allocate the top-most Script structure.
1047 @return A pointer to the current running script file after this
1048 change. NULL if removing the final script.
1052 ShellCommandSetNewScript (
1053 IN SCRIPT_FILE
*Script OPTIONAL
1056 SCRIPT_FILE_LIST
*Node
;
1057 if (Script
== NULL
) {
1058 if (IsListEmpty (&mScriptList
.Link
)) {
1061 Node
= (SCRIPT_FILE_LIST
*)GetFirstNode(&mScriptList
.Link
);
1062 RemoveEntryList(&Node
->Link
);
1063 DeleteScriptFileStruct(Node
->Data
);
1066 Node
= AllocateZeroPool(sizeof(SCRIPT_FILE_LIST
));
1070 Node
->Data
= Script
;
1071 InsertHeadList(&mScriptList
.Link
, &Node
->Link
);
1073 return (ShellCommandGetCurrentScriptFile());
1077 Function to generate the next default mapping name.
1079 If the return value is not NULL then it must be callee freed.
1081 @param Type What kind of mapping name to make.
1083 @retval NULL a memory allocation failed.
1084 @return a new map name string
1088 ShellCommandCreateNewMappingName(
1089 IN CONST SHELL_MAPPING_TYPE Type
1093 ASSERT(Type
< MappingTypeMax
);
1097 String
= AllocateZeroPool(PcdGet8(PcdShellMapNameLength
) * sizeof(String
[0]));
1100 PcdGet8(PcdShellMapNameLength
) * sizeof(String
[0]),
1101 Type
== MappingTypeFileSystem
?L
"FS%d:":L
"BLK%d:",
1102 Type
== MappingTypeFileSystem
?mFsMaxCount
++:mBlkMaxCount
++);
1108 Function to add a map node to the list of map items and update the "path" environment variable (optionally).
1110 If Path is TRUE (during initialization only), the path environment variable will also be updated to include
1111 default paths on the new map name...
1113 Path should be FALSE when this function is called from the protocol SetMap function.
1115 @param[in] Name The human readable mapped name.
1116 @param[in] DevicePath The Device Path for this map.
1117 @param[in] Flags The Flags attribute for this map item.
1118 @param[in] Path TRUE to update path, FALSE to skip this step (should only be TRUE during initialization).
1120 @retval EFI_SUCCESS The addition was sucessful.
1121 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
1122 @retval EFI_INVALID_PARAMETER A parameter was invalid.
1126 ShellCommandAddMapItemAndUpdatePath(
1127 IN CONST CHAR16
*Name
,
1128 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePath
,
1129 IN CONST UINT64 Flags
,
1130 IN CONST BOOLEAN Path
1134 SHELL_MAP_LIST
*MapListNode
;
1135 CONST CHAR16
*OriginalPath
;
1141 OriginalPath
= NULL
;
1142 Status
= EFI_SUCCESS
;
1144 MapListNode
= AllocateZeroPool(sizeof(SHELL_MAP_LIST
));
1145 if (MapListNode
== NULL
) {
1146 Status
= EFI_OUT_OF_RESOURCES
;
1148 MapListNode
->Flags
= Flags
;
1149 MapListNode
->MapName
= AllocateZeroPool(StrSize(Name
));
1150 MapListNode
->DevicePath
= DuplicateDevicePath(DevicePath
);
1151 if ((MapListNode
->MapName
== NULL
) || (MapListNode
->DevicePath
== NULL
)){
1152 Status
= EFI_OUT_OF_RESOURCES
;
1154 StrCpy(MapListNode
->MapName
, Name
);
1155 InsertTailList(&gShellMapList
.Link
, &MapListNode
->Link
);
1158 if (EFI_ERROR(Status
)) {
1159 if (MapListNode
!= NULL
) {
1160 if (MapListNode
->DevicePath
!= NULL
) {
1161 FreePool(MapListNode
->DevicePath
);
1163 if (MapListNode
->MapName
!= NULL
) {
1164 FreePool(MapListNode
->MapName
);
1166 FreePool(MapListNode
);
1170 // Since there was no error and Path was TRUE
1171 // Now add the correct path for that mapping
1173 OriginalPath
= gEfiShellProtocol
->GetEnv(L
"path");
1174 ASSERT((NewPath
== NULL
&& NewPathSize
== 0) || (NewPath
!= NULL
));
1175 if (OriginalPath
!= NULL
) {
1176 StrnCatGrow(&NewPath
, &NewPathSize
, OriginalPath
, 0);
1178 StrnCatGrow(&NewPath
, &NewPathSize
, L
".\\", 0);
1180 StrnCatGrow(&NewPath
, &NewPathSize
, L
";", 0);
1181 StrnCatGrow(&NewPath
, &NewPathSize
, Name
, 0);
1182 StrnCatGrow(&NewPath
, &NewPathSize
, L
"\\efi\\tools\\;", 0);
1183 StrnCatGrow(&NewPath
, &NewPathSize
, Name
, 0);
1184 StrnCatGrow(&NewPath
, &NewPathSize
, L
"\\efi\\boot\\;", 0);
1185 StrnCatGrow(&NewPath
, &NewPathSize
, Name
, 0);
1186 StrnCatGrow(&NewPath
, &NewPathSize
, L
"\\", 0);
1188 Status
= gEfiShellProtocol
->SetEnv(L
"path", NewPath
, TRUE
);
1189 ASSERT_EFI_ERROR(Status
);
1196 Creates the default map names for each device path in the system with
1197 a protocol depending on the Type.
1199 Creates the consistent map names for each device path in the system with
1200 a protocol depending on the Type.
1202 Note: This will reset all mappings in the system("map -r").
1204 Also sets up the default path environment variable if Type is FileSystem.
1206 @retval EFI_SUCCESS All map names were created sucessfully.
1207 @retval EFI_NOT_FOUND No protocols were found in the system.
1208 @return Error returned from gBS->LocateHandle().
1214 ShellCommandCreateInitialMappingsAndPaths(
1219 EFI_HANDLE
*HandleList
;
1221 EFI_DEVICE_PATH_PROTOCOL
**DevicePathList
;
1222 CHAR16
*NewDefaultName
;
1223 CHAR16
*NewConsistName
;
1224 EFI_DEVICE_PATH_PROTOCOL
**ConsistMappingTable
;
1225 SHELL_MAP_LIST
*MapListNode
;
1230 // Reset the static members back to zero
1235 gEfiShellProtocol
->SetEnv(L
"path", L
"", TRUE
);
1238 // First empty out the existing list.
1240 if (!IsListEmpty(&gShellMapList
.Link
)) {
1241 for ( MapListNode
= (SHELL_MAP_LIST
*)GetFirstNode(&gShellMapList
.Link
)
1242 ; !IsListEmpty(&gShellMapList
.Link
)
1243 ; MapListNode
= (SHELL_MAP_LIST
*)GetFirstNode(&gShellMapList
.Link
)
1245 RemoveEntryList(&MapListNode
->Link
);
1246 FreePool(MapListNode
);
1251 // Find each handle with Simple File System
1253 HandleList
= GetHandleListByProtocol(&gEfiSimpleFileSystemProtocolGuid
);
1254 if (HandleList
!= NULL
) {
1256 // Do a count of the handles
1258 for (Count
= 0 ; HandleList
[Count
] != NULL
; Count
++);
1261 // Get all Device Paths
1263 DevicePathList
= AllocateZeroPool(sizeof(EFI_DEVICE_PATH_PROTOCOL
*) * Count
);
1264 ASSERT(DevicePathList
!= NULL
);
1266 for (Count
= 0 ; HandleList
[Count
] != NULL
; Count
++) {
1267 DevicePathList
[Count
] = DevicePathFromHandle(HandleList
[Count
]);
1271 // Sort all DevicePaths
1273 PerformQuickSort(DevicePathList
, Count
, sizeof(EFI_DEVICE_PATH_PROTOCOL
*), DevicePathCompare
);
1275 ShellCommandConsistMappingInitialize(&ConsistMappingTable
);
1277 // Assign new Mappings to all...
1279 for (Count
= 0 ; HandleList
[Count
] != NULL
; Count
++) {
1281 // Get default name first
1283 NewDefaultName
= ShellCommandCreateNewMappingName(MappingTypeFileSystem
);
1284 ASSERT(NewDefaultName
!= NULL
);
1285 Status
= ShellCommandAddMapItemAndUpdatePath(NewDefaultName
, DevicePathList
[Count
], 0, TRUE
);
1286 ASSERT_EFI_ERROR(Status
);
1287 FreePool(NewDefaultName
);
1290 // Now do consistent name
1292 NewConsistName
= ShellCommandConsistMappingGenMappingName(DevicePathList
[Count
], ConsistMappingTable
);
1293 if (NewConsistName
!= NULL
) {
1294 Status
= ShellCommandAddMapItemAndUpdatePath(NewConsistName
, DevicePathList
[Count
], 0, FALSE
);
1295 ASSERT_EFI_ERROR(Status
);
1296 FreePool(NewConsistName
);
1300 ShellCommandConsistMappingUnInitialize(ConsistMappingTable
);
1302 SHELL_FREE_NON_NULL(HandleList
);
1303 SHELL_FREE_NON_NULL(DevicePathList
);
1311 // Find each handle with Block Io
1313 HandleList
= GetHandleListByProtocol(&gEfiBlockIoProtocolGuid
);
1314 if (HandleList
!= NULL
) {
1315 for (Count
= 0 ; HandleList
[Count
] != NULL
; Count
++);
1318 // Get all Device Paths
1320 DevicePathList
= AllocateZeroPool(sizeof(EFI_DEVICE_PATH_PROTOCOL
*) * Count
);
1321 ASSERT(DevicePathList
!= NULL
);
1323 for (Count
= 0 ; HandleList
[Count
] != NULL
; Count
++) {
1324 DevicePathList
[Count
] = DevicePathFromHandle(HandleList
[Count
]);
1328 // Sort all DevicePaths
1330 PerformQuickSort(DevicePathList
, Count
, sizeof(EFI_DEVICE_PATH_PROTOCOL
*), DevicePathCompare
);
1333 // Assign new Mappings to all...
1335 for (Count
= 0 ; HandleList
[Count
] != NULL
; Count
++) {
1337 // Get default name first
1339 NewDefaultName
= ShellCommandCreateNewMappingName(MappingTypeBlockIo
);
1340 ASSERT(NewDefaultName
!= NULL
);
1341 Status
= ShellCommandAddMapItemAndUpdatePath(NewDefaultName
, DevicePathList
[Count
], 0, FALSE
);
1342 ASSERT_EFI_ERROR(Status
);
1343 FreePool(NewDefaultName
);
1346 SHELL_FREE_NON_NULL(HandleList
);
1347 SHELL_FREE_NON_NULL(DevicePathList
);
1348 } else if (Count
== (UINTN
)-1) {
1349 return (EFI_NOT_FOUND
);
1352 return (EFI_SUCCESS
);
1356 Add mappings for any devices without one. Do not change any existing maps.
1358 @retval EFI_SUCCESS The operation was successful.
1362 ShellCommandUpdateMapping (
1367 EFI_HANDLE
*HandleList
;
1369 EFI_DEVICE_PATH_PROTOCOL
**DevicePathList
;
1370 CHAR16
*NewDefaultName
;
1371 CHAR16
*NewConsistName
;
1372 EFI_DEVICE_PATH_PROTOCOL
**ConsistMappingTable
;
1375 Status
= EFI_SUCCESS
;
1378 // remove mappings that represent removed devices.
1382 // Find each handle with Simple File System
1384 HandleList
= GetHandleListByProtocol(&gEfiSimpleFileSystemProtocolGuid
);
1385 if (HandleList
!= NULL
) {
1387 // Do a count of the handles
1389 for (Count
= 0 ; HandleList
[Count
] != NULL
; Count
++);
1392 // Get all Device Paths
1394 DevicePathList
= AllocateZeroPool(sizeof(EFI_DEVICE_PATH_PROTOCOL
*) * Count
);
1395 if (DevicePathList
== NULL
) {
1396 return (EFI_OUT_OF_RESOURCES
);
1399 for (Count
= 0 ; HandleList
[Count
] != NULL
; Count
++) {
1400 DevicePathList
[Count
] = DevicePathFromHandle(HandleList
[Count
]);
1404 // Sort all DevicePaths
1406 PerformQuickSort(DevicePathList
, Count
, sizeof(EFI_DEVICE_PATH_PROTOCOL
*), DevicePathCompare
);
1408 ShellCommandConsistMappingInitialize(&ConsistMappingTable
);
1411 // Assign new Mappings to remainders
1413 for (Count
= 0 ; !EFI_ERROR(Status
) && HandleList
[Count
] != NULL
&& !EFI_ERROR(Status
); Count
++) {
1415 // Skip ones that already have
1417 if (gEfiShellProtocol
->GetMapFromDevicePath(&DevicePathList
[Count
]) != NULL
) {
1423 NewDefaultName
= ShellCommandCreateNewMappingName(MappingTypeFileSystem
);
1424 if (NewDefaultName
== NULL
) {
1425 Status
= EFI_OUT_OF_RESOURCES
;
1430 // Call shell protocol SetMap function now...
1432 Status
= gEfiShellProtocol
->SetMap(DevicePathList
[Count
], NewDefaultName
);
1434 if (!EFI_ERROR(Status
)) {
1436 // Now do consistent name
1438 NewConsistName
= ShellCommandConsistMappingGenMappingName(DevicePathList
[Count
], ConsistMappingTable
);
1439 if (NewConsistName
!= NULL
) {
1440 Status
= gEfiShellProtocol
->SetMap(DevicePathList
[Count
], NewConsistName
);
1441 FreePool(NewConsistName
);
1445 FreePool(NewDefaultName
);
1447 ShellCommandConsistMappingUnInitialize(ConsistMappingTable
);
1448 SHELL_FREE_NON_NULL(HandleList
);
1449 SHELL_FREE_NON_NULL(DevicePathList
);
1456 // Do it all over again for gEfiBlockIoProtocolGuid
1463 Converts a SHELL_FILE_HANDLE to an EFI_FILE_PROTOCOL*.
1465 @param[in] Handle The SHELL_FILE_HANDLE to convert.
1467 @return a EFI_FILE_PROTOCOL* representing the same file.
1471 ConvertShellHandleToEfiFileProtocol(
1472 IN CONST SHELL_FILE_HANDLE Handle
1475 return ((EFI_FILE_PROTOCOL
*)(Handle
));
1479 Converts a EFI_FILE_PROTOCOL* to an SHELL_FILE_HANDLE.
1481 @param[in] Handle The pointer to EFI_FILE_PROTOCOL to convert.
1482 @param[in] Path The path to the file for verification.
1484 @return A SHELL_FILE_HANDLE representing the same file.
1485 @retval NULL There was not enough memory.
1489 ConvertEfiFileProtocolToShellHandle(
1490 IN CONST EFI_FILE_PROTOCOL
*Handle
,
1491 IN CONST CHAR16
*Path
1494 SHELL_COMMAND_FILE_HANDLE
*Buffer
;
1495 BUFFER_LIST
*NewNode
;
1498 Buffer
= AllocateZeroPool(sizeof(SHELL_COMMAND_FILE_HANDLE
));
1499 if (Buffer
== NULL
) {
1502 NewNode
= AllocateZeroPool(sizeof(BUFFER_LIST
));
1503 if (NewNode
== NULL
) {
1504 SHELL_FREE_NON_NULL(Buffer
);
1507 Buffer
->FileHandle
= (EFI_FILE_PROTOCOL
*)Handle
;
1508 Buffer
->Path
= StrnCatGrow(&Buffer
->Path
, NULL
, Path
, 0);
1509 if (Buffer
->Path
== NULL
) {
1510 SHELL_FREE_NON_NULL(NewNode
);
1511 SHELL_FREE_NON_NULL(Buffer
);
1514 NewNode
->Buffer
= Buffer
;
1516 InsertHeadList(&mFileHandleList
.Link
, &NewNode
->Link
);
1518 return ((SHELL_FILE_HANDLE
)(Handle
));
1522 Find the path that was logged with the specified SHELL_FILE_HANDLE.
1524 @param[in] Handle The SHELL_FILE_HANDLE to query on.
1526 @return A pointer to the path for the file.
1530 ShellFileHandleGetPath(
1531 IN CONST SHELL_FILE_HANDLE Handle
1536 for (Node
= (BUFFER_LIST
*)GetFirstNode(&mFileHandleList
.Link
)
1537 ; !IsNull(&mFileHandleList
.Link
, &Node
->Link
)
1538 ; Node
= (BUFFER_LIST
*)GetNextNode(&mFileHandleList
.Link
, &Node
->Link
)
1540 if ((Node
->Buffer
) && (((SHELL_COMMAND_FILE_HANDLE
*)Node
->Buffer
)->FileHandle
== Handle
)){
1541 return (((SHELL_COMMAND_FILE_HANDLE
*)Node
->Buffer
)->Path
);
1548 Remove a SHELL_FILE_HANDLE from the list of SHELL_FILE_HANDLES.
1550 @param[in] Handle The SHELL_FILE_HANDLE to remove.
1552 @retval TRUE The item was removed.
1553 @retval FALSE The item was not found.
1557 ShellFileHandleRemove(
1558 IN CONST SHELL_FILE_HANDLE Handle
1563 for (Node
= (BUFFER_LIST
*)GetFirstNode(&mFileHandleList
.Link
)
1564 ; !IsNull(&mFileHandleList
.Link
, &Node
->Link
)
1565 ; Node
= (BUFFER_LIST
*)GetNextNode(&mFileHandleList
.Link
, &Node
->Link
)
1567 if ((Node
->Buffer
) && (((SHELL_COMMAND_FILE_HANDLE
*)Node
->Buffer
)->FileHandle
== Handle
)){
1568 RemoveEntryList(&Node
->Link
);
1569 SHELL_FREE_NON_NULL(((SHELL_COMMAND_FILE_HANDLE
*)Node
->Buffer
)->Path
);
1570 SHELL_FREE_NON_NULL(Node
->Buffer
);
1571 SHELL_FREE_NON_NULL(Node
);
1579 Function to determine if a SHELL_FILE_HANDLE is at the end of the file.
1581 This will NOT work on directories.
1583 If Handle is NULL, then ASSERT.
1585 @param[in] Handle the file handle
1587 @retval TRUE the position is at the end of the file
1588 @retval FALSE the position is not at the end of the file
1593 IN SHELL_FILE_HANDLE Handle
1596 EFI_FILE_INFO
*Info
;
1601 // ASSERT if Handle is NULL
1603 ASSERT(Handle
!= NULL
);
1605 gEfiShellProtocol
->GetFilePosition(Handle
, &Pos
);
1606 Info
= gEfiShellProtocol
->GetFileInfo (Handle
);
1607 ASSERT(Info
!= NULL
);
1608 gEfiShellProtocol
->SetFilePosition(Handle
, Pos
);
1614 if (Pos
== Info
->FileSize
) {
1626 Frees any BUFFER_LIST defined type.
1628 @param[in] List The BUFFER_LIST object to free.
1633 IN BUFFER_LIST
*List
1636 BUFFER_LIST
*BufferListEntry
;
1642 // enumerate through the buffer list and free all memory
1644 for ( BufferListEntry
= ( BUFFER_LIST
*)GetFirstNode(&List
->Link
)
1645 ; !IsListEmpty (&List
->Link
)
1646 ; BufferListEntry
= (BUFFER_LIST
*)GetFirstNode(&List
->Link
)
1648 RemoveEntryList(&BufferListEntry
->Link
);
1649 if (BufferListEntry
->Buffer
!= NULL
) {
1650 FreePool(BufferListEntry
->Buffer
);
1652 FreePool(BufferListEntry
);