2 Provides interface to shell internal functions for shell commands.
4 Copyright (c) 2009 - 2013, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
15 #include "UefiShellCommandLib.h"
17 // STATIC local variables
18 STATIC SHELL_COMMAND_INTERNAL_LIST_ENTRY mCommandList
;
19 STATIC SCRIPT_FILE_LIST mScriptList
;
20 STATIC ALIAS_LIST mAliasList
;
21 STATIC BOOLEAN mEchoState
;
22 STATIC BOOLEAN mExitRequested
;
23 STATIC UINT64 mExitCode
;
24 STATIC BOOLEAN mExitScript
;
25 STATIC CHAR16
*mProfileList
;
26 STATIC UINTN mProfileListSize
;
27 STATIC UINTN mFsMaxCount
= 0;
28 STATIC UINTN mBlkMaxCount
= 0;
29 STATIC BUFFER_LIST mFileHandleList
;
31 // global variables required by library class.
32 EFI_UNICODE_COLLATION_PROTOCOL
*gUnicodeCollation
= NULL
;
33 SHELL_MAP_LIST gShellMapList
;
34 SHELL_MAP_LIST
*gShellCurDir
= NULL
;
36 CONST CHAR16
* SupportLevel
[] = {
44 Function to make sure that the global protocol pointers are valid.
45 must be called after constructor before accessing the pointers.
54 if (gUnicodeCollation
== NULL
) {
55 Status
= gBS
->LocateProtocol(&gEfiUnicodeCollation2ProtocolGuid
, NULL
, (VOID
**)&gUnicodeCollation
);
56 if (EFI_ERROR(Status
)) {
57 return (EFI_DEVICE_ERROR
);
64 Constructor for the Shell Command library.
66 Initialize the library and determine if the underlying is a UEFI Shell 2.0 or an EFI shell.
68 @param ImageHandle the image handle of the process
69 @param SystemTable the EFI System Table pointer
71 @retval EFI_SUCCESS the initialization was complete sucessfully
75 ShellCommandLibConstructor (
76 IN EFI_HANDLE ImageHandle
,
77 IN EFI_SYSTEM_TABLE
*SystemTable
81 InitializeListHead(&gShellMapList
.Link
);
82 InitializeListHead(&mCommandList
.Link
);
83 InitializeListHead(&mAliasList
.Link
);
84 InitializeListHead(&mScriptList
.Link
);
85 InitializeListHead(&mFileHandleList
.Link
);
88 mExitRequested
= FALSE
;
93 if (gUnicodeCollation
== NULL
) {
94 Status
= gBS
->LocateProtocol(&gEfiUnicodeCollation2ProtocolGuid
, NULL
, (VOID
**)&gUnicodeCollation
);
95 if (EFI_ERROR(Status
)) {
96 return (EFI_DEVICE_ERROR
);
100 return (RETURN_SUCCESS
);
104 Destructor for the library. free any resources.
106 @param ImageHandle the image handle of the process
107 @param SystemTable the EFI System Table pointer
109 @retval RETURN_SUCCESS this function always returns success
113 ShellCommandLibDestructor (
114 IN EFI_HANDLE ImageHandle
,
115 IN EFI_SYSTEM_TABLE
*SystemTable
118 SHELL_COMMAND_INTERNAL_LIST_ENTRY
*Node
;
120 SCRIPT_FILE_LIST
*Node3
;
121 SHELL_MAP_LIST
*MapNode
;
123 // enumerate throught the list and free all the memory
125 while (!IsListEmpty (&mCommandList
.Link
)) {
126 Node
= (SHELL_COMMAND_INTERNAL_LIST_ENTRY
*)GetFirstNode(&mCommandList
.Link
);
127 RemoveEntryList(&Node
->Link
);
128 SHELL_FREE_NON_NULL(Node
->CommandString
);
130 DEBUG_CODE(Node
= NULL
;);
134 // enumerate through the alias list and free all memory
136 while (!IsListEmpty (&mAliasList
.Link
)) {
137 Node2
= (ALIAS_LIST
*)GetFirstNode(&mAliasList
.Link
);
138 RemoveEntryList(&Node2
->Link
);
139 SHELL_FREE_NON_NULL(Node2
->CommandString
);
140 SHELL_FREE_NON_NULL(Node2
->Alias
);
141 SHELL_FREE_NON_NULL(Node2
);
142 DEBUG_CODE(Node2
= NULL
;);
146 // enumerate throught the list and free all the memory
148 while (!IsListEmpty (&mScriptList
.Link
)) {
149 Node3
= (SCRIPT_FILE_LIST
*)GetFirstNode(&mScriptList
.Link
);
150 RemoveEntryList(&Node3
->Link
);
151 DeleteScriptFileStruct(Node3
->Data
);
156 // enumerate throught the mappings list and free all the memory
158 if (!IsListEmpty(&gShellMapList
.Link
)) {
159 for (MapNode
= (SHELL_MAP_LIST
*)GetFirstNode(&gShellMapList
.Link
)
160 ; !IsListEmpty (&gShellMapList
.Link
)
161 ; MapNode
= (SHELL_MAP_LIST
*)GetFirstNode(&gShellMapList
.Link
)
163 ASSERT(MapNode
!= NULL
);
164 RemoveEntryList(&MapNode
->Link
);
165 SHELL_FREE_NON_NULL(MapNode
->DevicePath
);
166 SHELL_FREE_NON_NULL(MapNode
->MapName
);
167 SHELL_FREE_NON_NULL(MapNode
->CurrentDirectoryPath
);
171 if (!IsListEmpty(&mFileHandleList
.Link
)){
172 FreeBufferList(&mFileHandleList
);
175 if (mProfileList
!= NULL
) {
176 FreePool(mProfileList
);
179 gUnicodeCollation
= NULL
;
182 return (RETURN_SUCCESS
);
186 Checks if a command is already on the list.
188 @param[in] CommandString The command string to check for on the list.
192 ShellCommandIsCommandOnList (
193 IN CONST CHAR16
*CommandString
196 SHELL_COMMAND_INTERNAL_LIST_ENTRY
*Node
;
199 // assert for NULL parameter
201 ASSERT(CommandString
!= NULL
);
204 // check for the command
206 for ( Node
= (SHELL_COMMAND_INTERNAL_LIST_ENTRY
*)GetFirstNode(&mCommandList
.Link
)
207 ; !IsNull(&mCommandList
.Link
, &Node
->Link
)
208 ; Node
= (SHELL_COMMAND_INTERNAL_LIST_ENTRY
*)GetNextNode(&mCommandList
.Link
, &Node
->Link
)
210 ASSERT(Node
->CommandString
!= NULL
);
211 if (gUnicodeCollation
->StriColl(
213 (CHAR16
*)CommandString
,
214 Node
->CommandString
) == 0
223 Get the help text for a command.
225 @param[in] CommandString The command name.
227 @retval NULL No help text was found.
228 @return String of help text. Caller reuiqred to free.
232 ShellCommandGetCommandHelp (
233 IN CONST CHAR16
*CommandString
236 SHELL_COMMAND_INTERNAL_LIST_ENTRY
*Node
;
239 // assert for NULL parameter
241 ASSERT(CommandString
!= NULL
);
244 // check for the command
246 for ( Node
= (SHELL_COMMAND_INTERNAL_LIST_ENTRY
*)GetFirstNode(&mCommandList
.Link
)
247 ; !IsNull(&mCommandList
.Link
, &Node
->Link
)
248 ; Node
= (SHELL_COMMAND_INTERNAL_LIST_ENTRY
*)GetNextNode(&mCommandList
.Link
, &Node
->Link
)
250 ASSERT(Node
->CommandString
!= NULL
);
251 if (gUnicodeCollation
->StriColl(
253 (CHAR16
*)CommandString
,
254 Node
->CommandString
) == 0
256 return (HiiGetString(Node
->HiiHandle
, Node
->ManFormatHelp
, NULL
));
263 Registers handlers of type SHELL_RUN_COMMAND and
264 SHELL_GET_MAN_FILENAME for each shell command.
266 If the ShellSupportLevel is greater than the value of the
267 PcdShellSupportLevel then return RETURN_UNSUPPORTED.
269 Registers the handlers specified by GetHelpInfoHandler and CommandHandler
270 with the command specified by CommandString. If the command named by
271 CommandString has already been registered, then return
272 RETURN_ALREADY_STARTED.
274 If there are not enough resources available to register the handlers then
275 RETURN_OUT_OF_RESOURCES is returned.
277 If CommandString is NULL, then ASSERT().
278 If GetHelpInfoHandler is NULL, then ASSERT().
279 If CommandHandler is NULL, then ASSERT().
280 If ProfileName is NULL, then ASSERT().
282 @param[in] CommandString Pointer to the command name. This is the
283 name to look for on the command line in
285 @param[in] CommandHandler Pointer to a function that runs the
287 @param[in] GetManFileName Pointer to a function that provides man
289 @param[in] ShellMinSupportLevel minimum Shell Support Level which has this
291 @param[in] ProfileName profile name to require for support of this
293 @param[in] CanAffectLE indicates whether this command's return value
294 can change the LASTERROR environment variable.
295 @param[in] HiiHandle Handle of this command's HII entry.
296 @param[in] ManFormatHelp HII locator for the help text.
298 @retval RETURN_SUCCESS The handlers were registered.
299 @retval RETURN_OUT_OF_RESOURCES There are not enough resources available to
300 register the shell command.
301 @retval RETURN_UNSUPPORTED the ShellMinSupportLevel was higher than the
302 currently allowed support level.
303 @retval RETURN_ALREADY_STARTED The CommandString represents a command that
304 is already registered. Only 1 handler set for
305 a given command is allowed.
306 @sa SHELL_GET_MAN_FILENAME
307 @sa SHELL_RUN_COMMAND
311 ShellCommandRegisterCommandName (
312 IN CONST CHAR16
*CommandString
,
313 IN SHELL_RUN_COMMAND CommandHandler
,
314 IN SHELL_GET_MAN_FILENAME GetManFileName
,
315 IN UINT32 ShellMinSupportLevel
,
316 IN CONST CHAR16
*ProfileName
,
317 IN CONST BOOLEAN CanAffectLE
,
318 IN CONST EFI_HANDLE HiiHandle
,
319 IN CONST EFI_STRING_ID ManFormatHelp
322 SHELL_COMMAND_INTERNAL_LIST_ENTRY
*Node
;
323 SHELL_COMMAND_INTERNAL_LIST_ENTRY
*Command
;
324 SHELL_COMMAND_INTERNAL_LIST_ENTRY
*PrevCommand
;
325 INTN LexicalMatchValue
;
328 // Initialize local variables.
332 LexicalMatchValue
= 0;
335 // ASSERTs for NULL parameters
337 ASSERT(CommandString
!= NULL
);
338 ASSERT(GetManFileName
!= NULL
);
339 ASSERT(CommandHandler
!= NULL
);
340 ASSERT(ProfileName
!= NULL
);
343 // check for shell support level
345 if (PcdGet8(PcdShellSupportLevel
) < ShellMinSupportLevel
) {
346 return (RETURN_UNSUPPORTED
);
350 // check for already on the list
352 if (ShellCommandIsCommandOnList(CommandString
)) {
353 return (RETURN_ALREADY_STARTED
);
357 // allocate memory for new struct
359 Node
= AllocateZeroPool(sizeof(SHELL_COMMAND_INTERNAL_LIST_ENTRY
));
360 ASSERT(Node
!= NULL
);
361 Node
->CommandString
= AllocateZeroPool(StrSize(CommandString
));
362 ASSERT(Node
->CommandString
!= NULL
);
365 // populate the new struct
367 StrCpy(Node
->CommandString
, CommandString
);
369 Node
->GetManFileName
= GetManFileName
;
370 Node
->CommandHandler
= CommandHandler
;
371 Node
->LastError
= CanAffectLE
;
372 Node
->HiiHandle
= HiiHandle
;
373 Node
->ManFormatHelp
= ManFormatHelp
;
375 if ( StrLen(ProfileName
)>0
376 && ((mProfileList
!= NULL
377 && StrStr(mProfileList
, ProfileName
) == NULL
) || mProfileList
== NULL
)
379 ASSERT((mProfileList
== NULL
&& mProfileListSize
== 0) || (mProfileList
!= NULL
));
380 if (mProfileList
== NULL
) {
382 // If this is the first make a leading ';'
384 StrnCatGrow(&mProfileList
, &mProfileListSize
, L
";", 0);
386 StrnCatGrow(&mProfileList
, &mProfileListSize
, ProfileName
, 0);
387 StrnCatGrow(&mProfileList
, &mProfileListSize
, L
";", 0);
391 // Insert a new entry on top of the list
393 InsertHeadList (&mCommandList
.Link
, &Node
->Link
);
396 // Move a new registered command to its sorted ordered location in the list
398 for (Command
= (SHELL_COMMAND_INTERNAL_LIST_ENTRY
*)GetFirstNode (&mCommandList
.Link
),
399 PrevCommand
= (SHELL_COMMAND_INTERNAL_LIST_ENTRY
*)GetFirstNode (&mCommandList
.Link
)
400 ; !IsNull (&mCommandList
.Link
, &Command
->Link
)
401 ; Command
= (SHELL_COMMAND_INTERNAL_LIST_ENTRY
*)GetNextNode (&mCommandList
.Link
, &Command
->Link
)) {
404 // Get Lexical Comparison Value between PrevCommand and Command list entry
406 LexicalMatchValue
= gUnicodeCollation
->StriColl (
408 PrevCommand
->CommandString
,
409 Command
->CommandString
413 // Swap PrevCommand and Command list entry if PrevCommand list entry
414 // is alphabetically greater than Command list entry
416 if (LexicalMatchValue
> 0){
417 Command
= (SHELL_COMMAND_INTERNAL_LIST_ENTRY
*) SwapListEntries (&PrevCommand
->Link
, &Command
->Link
);
418 } else if (LexicalMatchValue
< 0) {
420 // PrevCommand entry is lexically lower than Command entry
426 return (RETURN_SUCCESS
);
430 Function to get the current Profile string.
432 @retval NULL There are no installed profiles.
433 @return A semi-colon delimited list of profiles.
437 ShellCommandGetProfileList (
441 return (mProfileList
);
445 Checks if a command string has been registered for CommandString and if so it runs
446 the previously registered handler for that command with the command line.
448 If CommandString is NULL, then ASSERT().
450 If Sections is specified, then each section name listed will be compared in a casesensitive
451 manner, to the section names described in Appendix B UEFI Shell 2.0 spec. If the section exists,
452 it will be appended to the returned help text. If the section does not exist, no
453 information will be returned. If Sections is NULL, then all help text information
454 available will be returned.
456 @param[in] CommandString Pointer to the command name. This is the name
457 found on the command line in the shell.
458 @param[in, out] RetVal Pointer to the return vaule from the command handler.
460 @param[in, out] CanAffectLE indicates whether this command's return value
461 needs to be placed into LASTERROR environment variable.
463 @retval RETURN_SUCCESS The handler was run.
464 @retval RETURN_NOT_FOUND The CommandString did not match a registered
466 @sa SHELL_RUN_COMMAND
470 ShellCommandRunCommandHandler (
471 IN CONST CHAR16
*CommandString
,
472 IN OUT SHELL_STATUS
*RetVal
,
473 IN OUT BOOLEAN
*CanAffectLE OPTIONAL
476 SHELL_COMMAND_INTERNAL_LIST_ENTRY
*Node
;
479 // assert for NULL parameters
481 ASSERT(CommandString
!= NULL
);
484 // check for the command
486 for ( Node
= (SHELL_COMMAND_INTERNAL_LIST_ENTRY
*)GetFirstNode(&mCommandList
.Link
)
487 ; !IsNull(&mCommandList
.Link
, &Node
->Link
)
488 ; Node
= (SHELL_COMMAND_INTERNAL_LIST_ENTRY
*)GetNextNode(&mCommandList
.Link
, &Node
->Link
)
490 ASSERT(Node
->CommandString
!= NULL
);
491 if (gUnicodeCollation
->StriColl(
493 (CHAR16
*)CommandString
,
494 Node
->CommandString
) == 0
496 if (CanAffectLE
!= NULL
) {
497 *CanAffectLE
= Node
->LastError
;
499 if (RetVal
!= NULL
) {
500 *RetVal
= Node
->CommandHandler(NULL
, gST
);
502 Node
->CommandHandler(NULL
, gST
);
504 return (RETURN_SUCCESS
);
507 return (RETURN_NOT_FOUND
);
511 Checks if a command string has been registered for CommandString and if so it
512 returns the MAN filename specified for that command.
514 If CommandString is NULL, then ASSERT().
516 @param[in] CommandString Pointer to the command name. This is the name
517 found on the command line in the shell.\
519 @retval NULL the commandString was not a registered command.
520 @return other the name of the MAN file.
521 @sa SHELL_GET_MAN_FILENAME
525 ShellCommandGetManFileNameHandler (
526 IN CONST CHAR16
*CommandString
529 SHELL_COMMAND_INTERNAL_LIST_ENTRY
*Node
;
532 // assert for NULL parameters
534 ASSERT(CommandString
!= NULL
);
537 // check for the command
539 for ( Node
= (SHELL_COMMAND_INTERNAL_LIST_ENTRY
*)GetFirstNode(&mCommandList
.Link
)
540 ; !IsNull(&mCommandList
.Link
, &Node
->Link
)
541 ; Node
= (SHELL_COMMAND_INTERNAL_LIST_ENTRY
*)GetNextNode(&mCommandList
.Link
, &Node
->Link
)
543 ASSERT(Node
->CommandString
!= NULL
);
544 if (gUnicodeCollation
->StriColl(
546 (CHAR16
*)CommandString
,
547 Node
->CommandString
) == 0
549 return (Node
->GetManFileName());
556 Get the list of all available shell internal commands. This is a linked list
557 (via LIST_ENTRY structure). enumerate through it using the BaseLib linked
558 list functions. do not modify the values.
560 @param[in] Sort TRUE to alphabetically sort the values first. FALSE otherwise.
562 @return a Linked list of all available shell commands.
566 ShellCommandGetCommandList (
567 IN CONST BOOLEAN Sort
571 // return ((COMMAND_LIST*)(&mCommandList));
573 return ((COMMAND_LIST
*)(&mCommandList
));
577 Registers aliases to be set as part of the initialization of the shell application.
579 If Command is NULL, then ASSERT().
580 If Alias is NULL, then ASSERT().
582 @param[in] Command Pointer to the Command
583 @param[in] Alias Pointer to Alias
585 @retval RETURN_SUCCESS The handlers were registered.
586 @retval RETURN_OUT_OF_RESOURCES There are not enough resources available to
587 register the shell command.
591 ShellCommandRegisterAlias (
592 IN CONST CHAR16
*Command
,
593 IN CONST CHAR16
*Alias
601 ASSERT(Command
!= NULL
);
602 ASSERT(Alias
!= NULL
);
605 // allocate memory for new struct
607 Node
= AllocateZeroPool(sizeof(ALIAS_LIST
));
608 ASSERT(Node
!= NULL
);
609 Node
->CommandString
= AllocateZeroPool(StrSize(Command
));
610 Node
->Alias
= AllocateZeroPool(StrSize(Alias
));
611 ASSERT(Node
->CommandString
!= NULL
);
612 ASSERT(Node
->Alias
!= NULL
);
615 // populate the new struct
617 StrCpy(Node
->CommandString
, Command
);
618 StrCpy(Node
->Alias
, Alias
);
621 // add the new struct to the list
623 InsertTailList (&mAliasList
.Link
, &Node
->Link
);
625 return (RETURN_SUCCESS
);
629 Get the list of all shell alias commands. This is a linked list
630 (via LIST_ENTRY structure). enumerate through it using the BaseLib linked
631 list functions. do not modify the values.
633 @return a Linked list of all requested shell alias'.
637 ShellCommandGetInitAliasList (
641 return (&mAliasList
);
645 Determine if a given alias is on the list of built in alias'.
647 @param[in] Alias The alias to test for
649 @retval TRUE The alias is a built in alias
650 @retval FALSE The alias is not a built in alias
654 ShellCommandIsOnAliasList(
655 IN CONST CHAR16
*Alias
661 // assert for NULL parameter
663 ASSERT(Alias
!= NULL
);
666 // check for the Alias
668 for ( Node
= (ALIAS_LIST
*)GetFirstNode(&mAliasList
.Link
)
669 ; !IsNull(&mAliasList
.Link
, &Node
->Link
)
670 ; Node
= (ALIAS_LIST
*)GetNextNode(&mAliasList
.Link
, &Node
->Link
)
672 ASSERT(Node
->CommandString
!= NULL
);
673 ASSERT(Node
->Alias
!= NULL
);
674 if (gUnicodeCollation
->StriColl(
677 Node
->CommandString
) == 0
681 if (gUnicodeCollation
->StriColl(
693 Function to determine current state of ECHO. Echo determins if lines from scripts
694 and ECHO commands are enabled.
696 @retval TRUE Echo is currently enabled
697 @retval FALSE Echo is currently disabled
701 ShellCommandGetEchoState(
709 Function to set current state of ECHO. Echo determins if lines from scripts
710 and ECHO commands are enabled.
712 If State is TRUE, Echo will be enabled.
713 If State is FALSE, Echo will be disabled.
715 @param[in] State How to set echo.
719 ShellCommandSetEchoState(
727 Indicate that the current shell or script should exit.
729 @param[in] ScriptOnly TRUE if exiting a script; FALSE otherwise.
730 @param[in] ErrorCode The 64 bit error code to return.
734 ShellCommandRegisterExit (
735 IN BOOLEAN ScriptOnly
,
736 IN CONST UINT64 ErrorCode
739 mExitRequested
= (BOOLEAN
)(!mExitRequested
);
740 if (mExitRequested
) {
741 mExitScript
= ScriptOnly
;
745 mExitCode
= ErrorCode
;
749 Retrieve the Exit indicator.
751 @retval TRUE Exit was indicated.
752 @retval FALSE Exis was not indicated.
756 ShellCommandGetExit (
760 return (mExitRequested
);
764 Retrieve the Exit code.
766 If ShellCommandGetExit returns FALSE than the return from this is undefined.
768 @return the value passed into RegisterExit.
772 ShellCommandGetExitCode (
779 Retrieve the Exit script indicator.
781 If ShellCommandGetExit returns FALSE than the return from this is undefined.
783 @retval TRUE ScriptOnly was indicated.
784 @retval FALSE ScriptOnly was not indicated.
788 ShellCommandGetScriptExit (
792 return (mExitScript
);
796 Function to cleanup all memory from a SCRIPT_FILE structure.
798 @param[in] Script The pointer to the structure to cleanup.
802 DeleteScriptFileStruct (
803 IN SCRIPT_FILE
*Script
808 if (Script
== NULL
) {
812 for (LoopVar
= 0 ; LoopVar
< Script
->Argc
; LoopVar
++) {
813 SHELL_FREE_NON_NULL(Script
->Argv
[LoopVar
]);
815 if (Script
->Argv
!= NULL
) {
816 SHELL_FREE_NON_NULL(Script
->Argv
);
818 Script
->CurrentCommand
= NULL
;
819 while (!IsListEmpty (&Script
->CommandList
)) {
820 Script
->CurrentCommand
= (SCRIPT_COMMAND_LIST
*)GetFirstNode(&Script
->CommandList
);
821 if (Script
->CurrentCommand
!= NULL
) {
822 RemoveEntryList(&Script
->CurrentCommand
->Link
);
823 if (Script
->CurrentCommand
->Cl
!= NULL
) {
824 SHELL_FREE_NON_NULL(Script
->CurrentCommand
->Cl
);
826 if (Script
->CurrentCommand
->Data
!= NULL
) {
827 SHELL_FREE_NON_NULL(Script
->CurrentCommand
->Data
);
829 SHELL_FREE_NON_NULL(Script
->CurrentCommand
);
832 SHELL_FREE_NON_NULL(Script
->ScriptName
);
833 SHELL_FREE_NON_NULL(Script
);
837 Function to return a pointer to the currently running script file object.
839 @retval NULL A script file is not currently running.
840 @return A pointer to the current script file object.
844 ShellCommandGetCurrentScriptFile (
848 SCRIPT_FILE_LIST
*List
;
849 if (IsListEmpty (&mScriptList
.Link
)) {
852 List
= ((SCRIPT_FILE_LIST
*)GetFirstNode(&mScriptList
.Link
));
857 Function to set a new script as the currently running one.
859 This function will correctly stack and unstack nested scripts.
861 @param[in] Script Pointer to new script information structure. if NULL
862 will remove and de-allocate the top-most Script structure.
864 @return A pointer to the current running script file after this
865 change. NULL if removing the final script.
869 ShellCommandSetNewScript (
870 IN SCRIPT_FILE
*Script OPTIONAL
873 SCRIPT_FILE_LIST
*Node
;
874 if (Script
== NULL
) {
875 if (IsListEmpty (&mScriptList
.Link
)) {
878 Node
= (SCRIPT_FILE_LIST
*)GetFirstNode(&mScriptList
.Link
);
879 RemoveEntryList(&Node
->Link
);
880 DeleteScriptFileStruct(Node
->Data
);
883 Node
= AllocateZeroPool(sizeof(SCRIPT_FILE_LIST
));
888 InsertHeadList(&mScriptList
.Link
, &Node
->Link
);
890 return (ShellCommandGetCurrentScriptFile());
894 Function to generate the next default mapping name.
896 If the return value is not NULL then it must be callee freed.
898 @param Type What kind of mapping name to make.
900 @retval NULL a memory allocation failed.
901 @return a new map name string
905 ShellCommandCreateNewMappingName(
906 IN CONST SHELL_MAPPING_TYPE Type
910 ASSERT(Type
< MappingTypeMax
);
914 String
= AllocateZeroPool(PcdGet8(PcdShellMapNameLength
) * sizeof(String
[0]));
917 PcdGet8(PcdShellMapNameLength
) * sizeof(String
[0]),
918 Type
== MappingTypeFileSystem
?L
"FS%d:":L
"BLK%d:",
919 Type
== MappingTypeFileSystem
?mFsMaxCount
++:mBlkMaxCount
++);
925 Function to add a map node to the list of map items and update the "path" environment variable (optionally).
927 If Path is TRUE (during initialization only), the path environment variable will also be updated to include
928 default paths on the new map name...
930 Path should be FALSE when this function is called from the protocol SetMap function.
932 @param[in] Name The human readable mapped name.
933 @param[in] DevicePath The Device Path for this map.
934 @param[in] Flags The Flags attribute for this map item.
935 @param[in] Path TRUE to update path, FALSE to skip this step (should only be TRUE during initialization).
937 @retval EFI_SUCCESS The addition was sucessful.
938 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
939 @retval EFI_INVALID_PARAMETER A parameter was invalid.
943 ShellCommandAddMapItemAndUpdatePath(
944 IN CONST CHAR16
*Name
,
945 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePath
,
946 IN CONST UINT64 Flags
,
947 IN CONST BOOLEAN Path
951 SHELL_MAP_LIST
*MapListNode
;
952 CONST CHAR16
*OriginalPath
;
959 Status
= EFI_SUCCESS
;
961 MapListNode
= AllocateZeroPool(sizeof(SHELL_MAP_LIST
));
962 if (MapListNode
== NULL
) {
963 Status
= EFI_OUT_OF_RESOURCES
;
965 MapListNode
->Flags
= Flags
;
966 MapListNode
->MapName
= AllocateZeroPool(StrSize(Name
));
967 MapListNode
->DevicePath
= DuplicateDevicePath(DevicePath
);
968 if ((MapListNode
->MapName
== NULL
) || (MapListNode
->DevicePath
== NULL
)){
969 Status
= EFI_OUT_OF_RESOURCES
;
971 StrCpy(MapListNode
->MapName
, Name
);
972 InsertTailList(&gShellMapList
.Link
, &MapListNode
->Link
);
975 if (EFI_ERROR(Status
)) {
976 if (MapListNode
!= NULL
) {
977 if (MapListNode
->DevicePath
!= NULL
) {
978 FreePool(MapListNode
->DevicePath
);
980 if (MapListNode
->MapName
!= NULL
) {
981 FreePool(MapListNode
->MapName
);
983 FreePool(MapListNode
);
987 // Since there was no error and Path was TRUE
988 // Now add the correct path for that mapping
990 OriginalPath
= gEfiShellProtocol
->GetEnv(L
"path");
991 ASSERT((NewPath
== NULL
&& NewPathSize
== 0) || (NewPath
!= NULL
));
992 if (OriginalPath
!= NULL
) {
993 StrnCatGrow(&NewPath
, &NewPathSize
, OriginalPath
, 0);
995 StrnCatGrow(&NewPath
, &NewPathSize
, L
".\\", 0);
997 StrnCatGrow(&NewPath
, &NewPathSize
, L
";", 0);
998 StrnCatGrow(&NewPath
, &NewPathSize
, Name
, 0);
999 StrnCatGrow(&NewPath
, &NewPathSize
, L
"\\efi\\tools\\;", 0);
1000 StrnCatGrow(&NewPath
, &NewPathSize
, Name
, 0);
1001 StrnCatGrow(&NewPath
, &NewPathSize
, L
"\\efi\\boot\\;", 0);
1002 StrnCatGrow(&NewPath
, &NewPathSize
, Name
, 0);
1003 StrnCatGrow(&NewPath
, &NewPathSize
, L
"\\", 0);
1005 Status
= gEfiShellProtocol
->SetEnv(L
"path", NewPath
, TRUE
);
1006 ASSERT_EFI_ERROR(Status
);
1013 Creates the default map names for each device path in the system with
1014 a protocol depending on the Type.
1016 Creates the consistent map names for each device path in the system with
1017 a protocol depending on the Type.
1019 Note: This will reset all mappings in the system("map -r").
1021 Also sets up the default path environment variable if Type is FileSystem.
1023 @retval EFI_SUCCESS All map names were created sucessfully.
1024 @retval EFI_NOT_FOUND No protocols were found in the system.
1025 @return Error returned from gBS->LocateHandle().
1031 ShellCommandCreateInitialMappingsAndPaths(
1036 EFI_HANDLE
*HandleList
;
1038 EFI_DEVICE_PATH_PROTOCOL
**DevicePathList
;
1039 CHAR16
*NewDefaultName
;
1040 CHAR16
*NewConsistName
;
1041 EFI_DEVICE_PATH_PROTOCOL
**ConsistMappingTable
;
1042 SHELL_MAP_LIST
*MapListNode
;
1047 // Reset the static members back to zero
1052 gEfiShellProtocol
->SetEnv(L
"path", L
"", TRUE
);
1055 // First empty out the existing list.
1057 if (!IsListEmpty(&gShellMapList
.Link
)) {
1058 for ( MapListNode
= (SHELL_MAP_LIST
*)GetFirstNode(&gShellMapList
.Link
)
1059 ; !IsListEmpty(&gShellMapList
.Link
)
1060 ; MapListNode
= (SHELL_MAP_LIST
*)GetFirstNode(&gShellMapList
.Link
)
1062 RemoveEntryList(&MapListNode
->Link
);
1063 FreePool(MapListNode
);
1068 // Find each handle with Simple File System
1070 HandleList
= GetHandleListByProtocol(&gEfiSimpleFileSystemProtocolGuid
);
1071 if (HandleList
!= NULL
) {
1073 // Do a count of the handles
1075 for (Count
= 0 ; HandleList
[Count
] != NULL
; Count
++);
1078 // Get all Device Paths
1080 DevicePathList
= AllocateZeroPool(sizeof(EFI_DEVICE_PATH_PROTOCOL
*) * Count
);
1081 ASSERT(DevicePathList
!= NULL
);
1083 for (Count
= 0 ; HandleList
[Count
] != NULL
; Count
++) {
1084 DevicePathList
[Count
] = DevicePathFromHandle(HandleList
[Count
]);
1088 // Sort all DevicePaths
1090 PerformQuickSort(DevicePathList
, Count
, sizeof(EFI_DEVICE_PATH_PROTOCOL
*), DevicePathCompare
);
1092 ShellCommandConsistMappingInitialize(&ConsistMappingTable
);
1094 // Assign new Mappings to all...
1096 for (Count
= 0 ; HandleList
[Count
] != NULL
; Count
++) {
1098 // Get default name first
1100 NewDefaultName
= ShellCommandCreateNewMappingName(MappingTypeFileSystem
);
1101 ASSERT(NewDefaultName
!= NULL
);
1102 Status
= ShellCommandAddMapItemAndUpdatePath(NewDefaultName
, DevicePathList
[Count
], 0, TRUE
);
1103 ASSERT_EFI_ERROR(Status
);
1104 FreePool(NewDefaultName
);
1107 // Now do consistent name
1109 NewConsistName
= ShellCommandConsistMappingGenMappingName(DevicePathList
[Count
], ConsistMappingTable
);
1110 if (NewConsistName
!= NULL
) {
1111 Status
= ShellCommandAddMapItemAndUpdatePath(NewConsistName
, DevicePathList
[Count
], 0, FALSE
);
1112 ASSERT_EFI_ERROR(Status
);
1113 FreePool(NewConsistName
);
1117 ShellCommandConsistMappingUnInitialize(ConsistMappingTable
);
1119 SHELL_FREE_NON_NULL(HandleList
);
1120 SHELL_FREE_NON_NULL(DevicePathList
);
1128 // Find each handle with Block Io
1130 HandleList
= GetHandleListByProtocol(&gEfiBlockIoProtocolGuid
);
1131 if (HandleList
!= NULL
) {
1132 for (Count
= 0 ; HandleList
[Count
] != NULL
; Count
++);
1135 // Get all Device Paths
1137 DevicePathList
= AllocateZeroPool(sizeof(EFI_DEVICE_PATH_PROTOCOL
*) * Count
);
1138 ASSERT(DevicePathList
!= NULL
);
1140 for (Count
= 0 ; HandleList
[Count
] != NULL
; Count
++) {
1141 DevicePathList
[Count
] = DevicePathFromHandle(HandleList
[Count
]);
1145 // Sort all DevicePaths
1147 PerformQuickSort(DevicePathList
, Count
, sizeof(EFI_DEVICE_PATH_PROTOCOL
*), DevicePathCompare
);
1150 // Assign new Mappings to all...
1152 for (Count
= 0 ; HandleList
[Count
] != NULL
; Count
++) {
1154 // Get default name first
1156 NewDefaultName
= ShellCommandCreateNewMappingName(MappingTypeBlockIo
);
1157 ASSERT(NewDefaultName
!= NULL
);
1158 Status
= ShellCommandAddMapItemAndUpdatePath(NewDefaultName
, DevicePathList
[Count
], 0, FALSE
);
1159 ASSERT_EFI_ERROR(Status
);
1160 FreePool(NewDefaultName
);
1163 SHELL_FREE_NON_NULL(HandleList
);
1164 SHELL_FREE_NON_NULL(DevicePathList
);
1165 } else if (Count
== (UINTN
)-1) {
1166 return (EFI_NOT_FOUND
);
1169 return (EFI_SUCCESS
);
1173 Converts a SHELL_FILE_HANDLE to an EFI_FILE_PROTOCOL*.
1175 @param[in] Handle The SHELL_FILE_HANDLE to convert.
1177 @return a EFI_FILE_PROTOCOL* representing the same file.
1181 ConvertShellHandleToEfiFileProtocol(
1182 IN CONST SHELL_FILE_HANDLE Handle
1185 return ((EFI_FILE_PROTOCOL
*)(Handle
));
1189 Converts a EFI_FILE_PROTOCOL* to an SHELL_FILE_HANDLE.
1191 @param[in] Handle The pointer to EFI_FILE_PROTOCOL to convert.
1192 @param[in] Path The path to the file for verification.
1194 @return A SHELL_FILE_HANDLE representing the same file.
1195 @retval NULL There was not enough memory.
1199 ConvertEfiFileProtocolToShellHandle(
1200 IN CONST EFI_FILE_PROTOCOL
*Handle
,
1201 IN CONST CHAR16
*Path
1204 SHELL_COMMAND_FILE_HANDLE
*Buffer
;
1205 BUFFER_LIST
*NewNode
;
1208 Buffer
= AllocateZeroPool(sizeof(SHELL_COMMAND_FILE_HANDLE
));
1209 if (Buffer
== NULL
) {
1212 NewNode
= AllocateZeroPool(sizeof(BUFFER_LIST
));
1213 if (NewNode
== NULL
) {
1216 Buffer
->FileHandle
= (EFI_FILE_PROTOCOL
*)Handle
;
1217 Buffer
->Path
= StrnCatGrow(&Buffer
->Path
, NULL
, Path
, 0);
1218 if (Buffer
->Path
== NULL
) {
1221 NewNode
->Buffer
= Buffer
;
1223 InsertHeadList(&mFileHandleList
.Link
, &NewNode
->Link
);
1225 return ((SHELL_FILE_HANDLE
)(Handle
));
1229 Find the path that was logged with the specified SHELL_FILE_HANDLE.
1231 @param[in] Handle The SHELL_FILE_HANDLE to query on.
1233 @return A pointer to the path for the file.
1237 ShellFileHandleGetPath(
1238 IN CONST SHELL_FILE_HANDLE Handle
1243 for (Node
= (BUFFER_LIST
*)GetFirstNode(&mFileHandleList
.Link
)
1244 ; !IsNull(&mFileHandleList
.Link
, &Node
->Link
)
1245 ; Node
= (BUFFER_LIST
*)GetNextNode(&mFileHandleList
.Link
, &Node
->Link
)
1247 if ((Node
->Buffer
) && (((SHELL_COMMAND_FILE_HANDLE
*)Node
->Buffer
)->FileHandle
== Handle
)){
1248 return (((SHELL_COMMAND_FILE_HANDLE
*)Node
->Buffer
)->Path
);
1255 Remove a SHELL_FILE_HANDLE from the list of SHELL_FILE_HANDLES.
1257 @param[in] Handle The SHELL_FILE_HANDLE to remove.
1259 @retval TRUE The item was removed.
1260 @retval FALSE The item was not found.
1264 ShellFileHandleRemove(
1265 IN CONST SHELL_FILE_HANDLE Handle
1270 for (Node
= (BUFFER_LIST
*)GetFirstNode(&mFileHandleList
.Link
)
1271 ; !IsNull(&mFileHandleList
.Link
, &Node
->Link
)
1272 ; Node
= (BUFFER_LIST
*)GetNextNode(&mFileHandleList
.Link
, &Node
->Link
)
1274 if ((Node
->Buffer
) && (((SHELL_COMMAND_FILE_HANDLE
*)Node
->Buffer
)->FileHandle
== Handle
)){
1275 RemoveEntryList(&Node
->Link
);
1276 SHELL_FREE_NON_NULL(((SHELL_COMMAND_FILE_HANDLE
*)Node
->Buffer
)->Path
);
1277 SHELL_FREE_NON_NULL(Node
->Buffer
);
1278 SHELL_FREE_NON_NULL(Node
);
1286 Function to determine if a SHELL_FILE_HANDLE is at the end of the file.
1288 This will NOT work on directories.
1290 If Handle is NULL, then ASSERT.
1292 @param[in] Handle the file handle
1294 @retval TRUE the position is at the end of the file
1295 @retval FALSE the position is not at the end of the file
1300 IN SHELL_FILE_HANDLE Handle
1303 EFI_FILE_INFO
*Info
;
1308 // ASSERT if Handle is NULL
1310 ASSERT(Handle
!= NULL
);
1312 gEfiShellProtocol
->GetFilePosition(Handle
, &Pos
);
1313 Info
= gEfiShellProtocol
->GetFileInfo (Handle
);
1314 ASSERT(Info
!= NULL
);
1315 gEfiShellProtocol
->SetFilePosition(Handle
, Pos
);
1321 if (Pos
== Info
->FileSize
) {
1333 Frees any BUFFER_LIST defined type.
1335 @param[in] List The BUFFER_LIST object to free.
1340 IN BUFFER_LIST
*List
1343 BUFFER_LIST
*BufferListEntry
;
1349 // enumerate through the buffer list and free all memory
1351 for ( BufferListEntry
= ( BUFFER_LIST
*)GetFirstNode(&List
->Link
)
1352 ; !IsListEmpty (&List
->Link
)
1353 ; BufferListEntry
= (BUFFER_LIST
*)GetFirstNode(&List
->Link
)
1355 RemoveEntryList(&BufferListEntry
->Link
);
1356 ASSERT(BufferListEntry
->Buffer
!= NULL
);
1357 if (BufferListEntry
->Buffer
!= NULL
) {
1358 FreePool(BufferListEntry
->Buffer
);
1360 FreePool(BufferListEntry
);