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 EFI_DEVICE_PATH_TO_TEXT_PROTOCOL
*gDevPathToText
= 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
);
61 if (gDevPathToText
== NULL
) {
62 Status
= gBS
->LocateProtocol(&gEfiDevicePathToTextProtocolGuid
, NULL
, (VOID
**)&gDevPathToText
);
63 if (EFI_ERROR(Status
)) {
64 return (EFI_DEVICE_ERROR
);
71 Constructor for the Shell Command library.
73 Initialize the library and determine if the underlying is a UEFI Shell 2.0 or an EFI shell.
75 @param ImageHandle the image handle of the process
76 @param SystemTable the EFI System Table pointer
78 @retval EFI_SUCCESS the initialization was complete sucessfully
82 ShellCommandLibConstructor (
83 IN EFI_HANDLE ImageHandle
,
84 IN EFI_SYSTEM_TABLE
*SystemTable
88 InitializeListHead(&gShellMapList
.Link
);
89 InitializeListHead(&mCommandList
.Link
);
90 InitializeListHead(&mAliasList
.Link
);
91 InitializeListHead(&mScriptList
.Link
);
92 InitializeListHead(&mFileHandleList
.Link
);
95 mExitRequested
= FALSE
;
100 if (gUnicodeCollation
== NULL
) {
101 Status
= gBS
->LocateProtocol(&gEfiUnicodeCollation2ProtocolGuid
, NULL
, (VOID
**)&gUnicodeCollation
);
102 if (EFI_ERROR(Status
)) {
103 return (EFI_DEVICE_ERROR
);
107 return (RETURN_SUCCESS
);
111 Destructor for the library. free any resources.
113 @param ImageHandle the image handle of the process
114 @param SystemTable the EFI System Table pointer
116 @retval RETURN_SUCCESS this function always returns success
120 ShellCommandLibDestructor (
121 IN EFI_HANDLE ImageHandle
,
122 IN EFI_SYSTEM_TABLE
*SystemTable
125 SHELL_COMMAND_INTERNAL_LIST_ENTRY
*Node
;
127 SCRIPT_FILE_LIST
*Node3
;
128 SHELL_MAP_LIST
*MapNode
;
130 // enumerate throught the list and free all the memory
132 while (!IsListEmpty (&mCommandList
.Link
)) {
133 Node
= (SHELL_COMMAND_INTERNAL_LIST_ENTRY
*)GetFirstNode(&mCommandList
.Link
);
134 RemoveEntryList(&Node
->Link
);
135 SHELL_FREE_NON_NULL(Node
->CommandString
);
137 DEBUG_CODE(Node
= NULL
;);
141 // enumerate through the alias list and free all memory
143 while (!IsListEmpty (&mAliasList
.Link
)) {
144 Node2
= (ALIAS_LIST
*)GetFirstNode(&mAliasList
.Link
);
145 RemoveEntryList(&Node2
->Link
);
146 SHELL_FREE_NON_NULL(Node2
->CommandString
);
147 SHELL_FREE_NON_NULL(Node2
->Alias
);
148 SHELL_FREE_NON_NULL(Node2
);
149 DEBUG_CODE(Node2
= NULL
;);
153 // enumerate throught the list and free all the memory
155 while (!IsListEmpty (&mScriptList
.Link
)) {
156 Node3
= (SCRIPT_FILE_LIST
*)GetFirstNode(&mScriptList
.Link
);
157 RemoveEntryList(&Node3
->Link
);
158 DeleteScriptFileStruct(Node3
->Data
);
163 // enumerate throught the mappings list and free all the memory
165 if (!IsListEmpty(&gShellMapList
.Link
)) {
166 for (MapNode
= (SHELL_MAP_LIST
*)GetFirstNode(&gShellMapList
.Link
)
167 ; !IsListEmpty (&gShellMapList
.Link
)
168 ; MapNode
= (SHELL_MAP_LIST
*)GetFirstNode(&gShellMapList
.Link
)
170 ASSERT(MapNode
!= NULL
);
171 RemoveEntryList(&MapNode
->Link
);
172 SHELL_FREE_NON_NULL(MapNode
->DevicePath
);
173 SHELL_FREE_NON_NULL(MapNode
->MapName
);
174 SHELL_FREE_NON_NULL(MapNode
->CurrentDirectoryPath
);
178 if (!IsListEmpty(&mFileHandleList
.Link
)){
179 FreeBufferList(&mFileHandleList
);
182 if (mProfileList
!= NULL
) {
183 FreePool(mProfileList
);
186 gUnicodeCollation
= NULL
;
187 gDevPathToText
= NULL
;
190 return (RETURN_SUCCESS
);
194 Checks if a command is already on the list.
196 @param[in] CommandString The command string to check for on the list.
200 ShellCommandIsCommandOnList (
201 IN CONST CHAR16
*CommandString
204 SHELL_COMMAND_INTERNAL_LIST_ENTRY
*Node
;
207 // assert for NULL parameter
209 ASSERT(CommandString
!= NULL
);
212 // check for the command
214 for ( Node
= (SHELL_COMMAND_INTERNAL_LIST_ENTRY
*)GetFirstNode(&mCommandList
.Link
)
215 ; !IsNull(&mCommandList
.Link
, &Node
->Link
)
216 ; Node
= (SHELL_COMMAND_INTERNAL_LIST_ENTRY
*)GetNextNode(&mCommandList
.Link
, &Node
->Link
)
218 ASSERT(Node
->CommandString
!= NULL
);
219 if (gUnicodeCollation
->StriColl(
221 (CHAR16
*)CommandString
,
222 Node
->CommandString
) == 0
231 Get the help text for a command.
233 @param[in] CommandString The command name.
235 @retval NULL No help text was found.
236 @return String of help text. Caller reuiqred to free.
240 ShellCommandGetCommandHelp (
241 IN CONST CHAR16
*CommandString
244 SHELL_COMMAND_INTERNAL_LIST_ENTRY
*Node
;
247 // assert for NULL parameter
249 ASSERT(CommandString
!= NULL
);
252 // check for the command
254 for ( Node
= (SHELL_COMMAND_INTERNAL_LIST_ENTRY
*)GetFirstNode(&mCommandList
.Link
)
255 ; !IsNull(&mCommandList
.Link
, &Node
->Link
)
256 ; Node
= (SHELL_COMMAND_INTERNAL_LIST_ENTRY
*)GetNextNode(&mCommandList
.Link
, &Node
->Link
)
258 ASSERT(Node
->CommandString
!= NULL
);
259 if (gUnicodeCollation
->StriColl(
261 (CHAR16
*)CommandString
,
262 Node
->CommandString
) == 0
264 return (HiiGetString(Node
->HiiHandle
, Node
->ManFormatHelp
, NULL
));
271 Registers handlers of type SHELL_RUN_COMMAND and
272 SHELL_GET_MAN_FILENAME for each shell command.
274 If the ShellSupportLevel is greater than the value of the
275 PcdShellSupportLevel then return RETURN_UNSUPPORTED.
277 Registers the handlers specified by GetHelpInfoHandler and CommandHandler
278 with the command specified by CommandString. If the command named by
279 CommandString has already been registered, then return
280 RETURN_ALREADY_STARTED.
282 If there are not enough resources available to register the handlers then
283 RETURN_OUT_OF_RESOURCES is returned.
285 If CommandString is NULL, then ASSERT().
286 If GetHelpInfoHandler is NULL, then ASSERT().
287 If CommandHandler is NULL, then ASSERT().
288 If ProfileName is NULL, then ASSERT().
290 @param[in] CommandString Pointer to the command name. This is the
291 name to look for on the command line in
293 @param[in] CommandHandler Pointer to a function that runs the
295 @param[in] GetManFileName Pointer to a function that provides man
297 @param[in] ShellMinSupportLevel minimum Shell Support Level which has this
299 @param[in] ProfileName profile name to require for support of this
301 @param[in] CanAffectLE indicates whether this command's return value
302 can change the LASTERROR environment variable.
303 @param[in] HiiHandle Handle of this command's HII entry.
304 @param[in] ManFormatHelp HII locator for the help text.
306 @retval RETURN_SUCCESS The handlers were registered.
307 @retval RETURN_OUT_OF_RESOURCES There are not enough resources available to
308 register the shell command.
309 @retval RETURN_UNSUPPORTED the ShellMinSupportLevel was higher than the
310 currently allowed support level.
311 @retval RETURN_ALREADY_STARTED The CommandString represents a command that
312 is already registered. Only 1 handler set for
313 a given command is allowed.
314 @sa SHELL_GET_MAN_FILENAME
315 @sa SHELL_RUN_COMMAND
319 ShellCommandRegisterCommandName (
320 IN CONST CHAR16
*CommandString
,
321 IN SHELL_RUN_COMMAND CommandHandler
,
322 IN SHELL_GET_MAN_FILENAME GetManFileName
,
323 IN UINT32 ShellMinSupportLevel
,
324 IN CONST CHAR16
*ProfileName
,
325 IN CONST BOOLEAN CanAffectLE
,
326 IN CONST EFI_HANDLE HiiHandle
,
327 IN CONST EFI_STRING_ID ManFormatHelp
330 SHELL_COMMAND_INTERNAL_LIST_ENTRY
*Node
;
331 SHELL_COMMAND_INTERNAL_LIST_ENTRY
*Command
;
332 SHELL_COMMAND_INTERNAL_LIST_ENTRY
*PrevCommand
;
333 INTN LexicalMatchValue
;
336 // Initialize local variables.
340 LexicalMatchValue
= 0;
343 // ASSERTs for NULL parameters
345 ASSERT(CommandString
!= NULL
);
346 ASSERT(GetManFileName
!= NULL
);
347 ASSERT(CommandHandler
!= NULL
);
348 ASSERT(ProfileName
!= NULL
);
351 // check for shell support level
353 if (PcdGet8(PcdShellSupportLevel
) < ShellMinSupportLevel
) {
354 return (RETURN_UNSUPPORTED
);
358 // check for already on the list
360 if (ShellCommandIsCommandOnList(CommandString
)) {
361 return (RETURN_ALREADY_STARTED
);
365 // allocate memory for new struct
367 Node
= AllocateZeroPool(sizeof(SHELL_COMMAND_INTERNAL_LIST_ENTRY
));
368 ASSERT(Node
!= NULL
);
369 Node
->CommandString
= AllocateZeroPool(StrSize(CommandString
));
370 ASSERT(Node
->CommandString
!= NULL
);
373 // populate the new struct
375 StrCpy(Node
->CommandString
, CommandString
);
377 Node
->GetManFileName
= GetManFileName
;
378 Node
->CommandHandler
= CommandHandler
;
379 Node
->LastError
= CanAffectLE
;
380 Node
->HiiHandle
= HiiHandle
;
381 Node
->ManFormatHelp
= ManFormatHelp
;
383 if ( StrLen(ProfileName
)>0
384 && ((mProfileList
!= NULL
385 && StrStr(mProfileList
, ProfileName
) == NULL
) || mProfileList
== NULL
)
387 ASSERT((mProfileList
== NULL
&& mProfileListSize
== 0) || (mProfileList
!= NULL
));
388 if (mProfileList
== NULL
) {
390 // If this is the first make a leading ';'
392 StrnCatGrow(&mProfileList
, &mProfileListSize
, L
";", 0);
394 StrnCatGrow(&mProfileList
, &mProfileListSize
, ProfileName
, 0);
395 StrnCatGrow(&mProfileList
, &mProfileListSize
, L
";", 0);
399 // Insert a new entry on top of the list
401 InsertHeadList (&mCommandList
.Link
, &Node
->Link
);
404 // Move a new registered command to its sorted ordered location in the list
406 for (Command
= (SHELL_COMMAND_INTERNAL_LIST_ENTRY
*)GetFirstNode (&mCommandList
.Link
),
407 PrevCommand
= (SHELL_COMMAND_INTERNAL_LIST_ENTRY
*)GetFirstNode (&mCommandList
.Link
)
408 ; !IsNull (&mCommandList
.Link
, &Command
->Link
)
409 ; Command
= (SHELL_COMMAND_INTERNAL_LIST_ENTRY
*)GetNextNode (&mCommandList
.Link
, &Command
->Link
)) {
412 // Get Lexical Comparison Value between PrevCommand and Command list entry
414 LexicalMatchValue
= gUnicodeCollation
->StriColl (
416 PrevCommand
->CommandString
,
417 Command
->CommandString
421 // Swap PrevCommand and Command list entry if PrevCommand list entry
422 // is alphabetically greater than Command list entry
424 if (LexicalMatchValue
> 0){
425 Command
= (SHELL_COMMAND_INTERNAL_LIST_ENTRY
*) SwapListEntries (&PrevCommand
->Link
, &Command
->Link
);
426 } else if (LexicalMatchValue
< 0) {
428 // PrevCommand entry is lexically lower than Command entry
434 return (RETURN_SUCCESS
);
438 Function to get the current Profile string.
440 @retval NULL There are no installed profiles.
441 @return A semi-colon delimited list of profiles.
445 ShellCommandGetProfileList (
449 return (mProfileList
);
453 Checks if a command string has been registered for CommandString and if so it runs
454 the previously registered handler for that command with the command line.
456 If CommandString is NULL, then ASSERT().
458 If Sections is specified, then each section name listed will be compared in a casesensitive
459 manner, to the section names described in Appendix B UEFI Shell 2.0 spec. If the section exists,
460 it will be appended to the returned help text. If the section does not exist, no
461 information will be returned. If Sections is NULL, then all help text information
462 available will be returned.
464 @param[in] CommandString Pointer to the command name. This is the name
465 found on the command line in the shell.
466 @param[in, out] RetVal Pointer to the return vaule from the command handler.
468 @param[in, out] CanAffectLE indicates whether this command's return value
469 needs to be placed into LASTERROR environment variable.
471 @retval RETURN_SUCCESS The handler was run.
472 @retval RETURN_NOT_FOUND The CommandString did not match a registered
474 @sa SHELL_RUN_COMMAND
478 ShellCommandRunCommandHandler (
479 IN CONST CHAR16
*CommandString
,
480 IN OUT SHELL_STATUS
*RetVal
,
481 IN OUT BOOLEAN
*CanAffectLE OPTIONAL
484 SHELL_COMMAND_INTERNAL_LIST_ENTRY
*Node
;
487 // assert for NULL parameters
489 ASSERT(CommandString
!= NULL
);
492 // check for the command
494 for ( Node
= (SHELL_COMMAND_INTERNAL_LIST_ENTRY
*)GetFirstNode(&mCommandList
.Link
)
495 ; !IsNull(&mCommandList
.Link
, &Node
->Link
)
496 ; Node
= (SHELL_COMMAND_INTERNAL_LIST_ENTRY
*)GetNextNode(&mCommandList
.Link
, &Node
->Link
)
498 ASSERT(Node
->CommandString
!= NULL
);
499 if (gUnicodeCollation
->StriColl(
501 (CHAR16
*)CommandString
,
502 Node
->CommandString
) == 0
504 if (CanAffectLE
!= NULL
) {
505 *CanAffectLE
= Node
->LastError
;
507 if (RetVal
!= NULL
) {
508 *RetVal
= Node
->CommandHandler(NULL
, gST
);
510 Node
->CommandHandler(NULL
, gST
);
512 return (RETURN_SUCCESS
);
515 return (RETURN_NOT_FOUND
);
519 Checks if a command string has been registered for CommandString and if so it
520 returns the MAN filename specified for that command.
522 If CommandString is NULL, then ASSERT().
524 @param[in] CommandString Pointer to the command name. This is the name
525 found on the command line in the shell.\
527 @retval NULL the commandString was not a registered command.
528 @return other the name of the MAN file.
529 @sa SHELL_GET_MAN_FILENAME
533 ShellCommandGetManFileNameHandler (
534 IN CONST CHAR16
*CommandString
537 SHELL_COMMAND_INTERNAL_LIST_ENTRY
*Node
;
540 // assert for NULL parameters
542 ASSERT(CommandString
!= NULL
);
545 // check for the command
547 for ( Node
= (SHELL_COMMAND_INTERNAL_LIST_ENTRY
*)GetFirstNode(&mCommandList
.Link
)
548 ; !IsNull(&mCommandList
.Link
, &Node
->Link
)
549 ; Node
= (SHELL_COMMAND_INTERNAL_LIST_ENTRY
*)GetNextNode(&mCommandList
.Link
, &Node
->Link
)
551 ASSERT(Node
->CommandString
!= NULL
);
552 if (gUnicodeCollation
->StriColl(
554 (CHAR16
*)CommandString
,
555 Node
->CommandString
) == 0
557 return (Node
->GetManFileName());
564 Get the list of all available shell internal commands. This is a linked list
565 (via LIST_ENTRY structure). enumerate through it using the BaseLib linked
566 list functions. do not modify the values.
568 @param[in] Sort TRUE to alphabetically sort the values first. FALSE otherwise.
570 @return a Linked list of all available shell commands.
574 ShellCommandGetCommandList (
575 IN CONST BOOLEAN Sort
579 // return ((COMMAND_LIST*)(&mCommandList));
581 return ((COMMAND_LIST
*)(&mCommandList
));
585 Registers aliases to be set as part of the initialization of the shell application.
587 If Command is NULL, then ASSERT().
588 If Alias is NULL, then ASSERT().
590 @param[in] Command Pointer to the Command
591 @param[in] Alias Pointer to Alias
593 @retval RETURN_SUCCESS The handlers were registered.
594 @retval RETURN_OUT_OF_RESOURCES There are not enough resources available to
595 register the shell command.
599 ShellCommandRegisterAlias (
600 IN CONST CHAR16
*Command
,
601 IN CONST CHAR16
*Alias
609 ASSERT(Command
!= NULL
);
610 ASSERT(Alias
!= NULL
);
613 // allocate memory for new struct
615 Node
= AllocateZeroPool(sizeof(ALIAS_LIST
));
616 ASSERT(Node
!= NULL
);
617 Node
->CommandString
= AllocateZeroPool(StrSize(Command
));
618 Node
->Alias
= AllocateZeroPool(StrSize(Alias
));
619 ASSERT(Node
->CommandString
!= NULL
);
620 ASSERT(Node
->Alias
!= NULL
);
623 // populate the new struct
625 StrCpy(Node
->CommandString
, Command
);
626 StrCpy(Node
->Alias
, Alias
);
629 // add the new struct to the list
631 InsertTailList (&mAliasList
.Link
, &Node
->Link
);
633 return (RETURN_SUCCESS
);
637 Get the list of all shell alias commands. This is a linked list
638 (via LIST_ENTRY structure). enumerate through it using the BaseLib linked
639 list functions. do not modify the values.
641 @return a Linked list of all requested shell alias'.
645 ShellCommandGetInitAliasList (
649 return (&mAliasList
);
653 Determine if a given alias is on the list of built in alias'.
655 @param[in] Alias The alias to test for
657 @retval TRUE The alias is a built in alias
658 @retval FALSE The alias is not a built in alias
662 ShellCommandIsOnAliasList(
663 IN CONST CHAR16
*Alias
669 // assert for NULL parameter
671 ASSERT(Alias
!= NULL
);
674 // check for the Alias
676 for ( Node
= (ALIAS_LIST
*)GetFirstNode(&mAliasList
.Link
)
677 ; !IsNull(&mAliasList
.Link
, &Node
->Link
)
678 ; Node
= (ALIAS_LIST
*)GetNextNode(&mAliasList
.Link
, &Node
->Link
)
680 ASSERT(Node
->CommandString
!= NULL
);
681 ASSERT(Node
->Alias
!= NULL
);
682 if (gUnicodeCollation
->StriColl(
685 Node
->CommandString
) == 0
689 if (gUnicodeCollation
->StriColl(
701 Function to determine current state of ECHO. Echo determins if lines from scripts
702 and ECHO commands are enabled.
704 @retval TRUE Echo is currently enabled
705 @retval FALSE Echo is currently disabled
709 ShellCommandGetEchoState(
717 Function to set current state of ECHO. Echo determins if lines from scripts
718 and ECHO commands are enabled.
720 If State is TRUE, Echo will be enabled.
721 If State is FALSE, Echo will be disabled.
723 @param[in] State How to set echo.
727 ShellCommandSetEchoState(
735 Indicate that the current shell or script should exit.
737 @param[in] ScriptOnly TRUE if exiting a script; FALSE otherwise.
738 @param[in] ErrorCode The 64 bit error code to return.
742 ShellCommandRegisterExit (
743 IN BOOLEAN ScriptOnly
,
744 IN CONST UINT64 ErrorCode
747 mExitRequested
= (BOOLEAN
)(!mExitRequested
);
748 if (mExitRequested
) {
749 mExitScript
= ScriptOnly
;
753 mExitCode
= ErrorCode
;
757 Retrieve the Exit indicator.
759 @retval TRUE Exit was indicated.
760 @retval FALSE Exis was not indicated.
764 ShellCommandGetExit (
768 return (mExitRequested
);
772 Retrieve the Exit code.
774 If ShellCommandGetExit returns FALSE than the return from this is undefined.
776 @return the value passed into RegisterExit.
780 ShellCommandGetExitCode (
787 Retrieve the Exit script indicator.
789 If ShellCommandGetExit returns FALSE than the return from this is undefined.
791 @retval TRUE ScriptOnly was indicated.
792 @retval FALSE ScriptOnly was not indicated.
796 ShellCommandGetScriptExit (
800 return (mExitScript
);
804 Function to cleanup all memory from a SCRIPT_FILE structure.
806 @param[in] Script The pointer to the structure to cleanup.
810 DeleteScriptFileStruct (
811 IN SCRIPT_FILE
*Script
816 if (Script
== NULL
) {
820 for (LoopVar
= 0 ; LoopVar
< Script
->Argc
; LoopVar
++) {
821 SHELL_FREE_NON_NULL(Script
->Argv
[LoopVar
]);
823 if (Script
->Argv
!= NULL
) {
824 SHELL_FREE_NON_NULL(Script
->Argv
);
826 Script
->CurrentCommand
= NULL
;
827 while (!IsListEmpty (&Script
->CommandList
)) {
828 Script
->CurrentCommand
= (SCRIPT_COMMAND_LIST
*)GetFirstNode(&Script
->CommandList
);
829 if (Script
->CurrentCommand
!= NULL
) {
830 RemoveEntryList(&Script
->CurrentCommand
->Link
);
831 if (Script
->CurrentCommand
->Cl
!= NULL
) {
832 SHELL_FREE_NON_NULL(Script
->CurrentCommand
->Cl
);
834 if (Script
->CurrentCommand
->Data
!= NULL
) {
835 SHELL_FREE_NON_NULL(Script
->CurrentCommand
->Data
);
837 SHELL_FREE_NON_NULL(Script
->CurrentCommand
);
840 SHELL_FREE_NON_NULL(Script
->ScriptName
);
841 SHELL_FREE_NON_NULL(Script
);
845 Function to return a pointer to the currently running script file object.
847 @retval NULL A script file is not currently running.
848 @return A pointer to the current script file object.
852 ShellCommandGetCurrentScriptFile (
856 SCRIPT_FILE_LIST
*List
;
857 if (IsListEmpty (&mScriptList
.Link
)) {
860 List
= ((SCRIPT_FILE_LIST
*)GetFirstNode(&mScriptList
.Link
));
865 Function to set a new script as the currently running one.
867 This function will correctly stack and unstack nested scripts.
869 @param[in] Script Pointer to new script information structure. if NULL
870 will remove and de-allocate the top-most Script structure.
872 @return A pointer to the current running script file after this
873 change. NULL if removing the final script.
877 ShellCommandSetNewScript (
878 IN SCRIPT_FILE
*Script OPTIONAL
881 SCRIPT_FILE_LIST
*Node
;
882 if (Script
== NULL
) {
883 if (IsListEmpty (&mScriptList
.Link
)) {
886 Node
= (SCRIPT_FILE_LIST
*)GetFirstNode(&mScriptList
.Link
);
887 RemoveEntryList(&Node
->Link
);
888 DeleteScriptFileStruct(Node
->Data
);
891 Node
= AllocateZeroPool(sizeof(SCRIPT_FILE_LIST
));
896 InsertHeadList(&mScriptList
.Link
, &Node
->Link
);
898 return (ShellCommandGetCurrentScriptFile());
902 Function to generate the next default mapping name.
904 If the return value is not NULL then it must be callee freed.
906 @param Type What kind of mapping name to make.
908 @retval NULL a memory allocation failed.
909 @return a new map name string
913 ShellCommandCreateNewMappingName(
914 IN CONST SHELL_MAPPING_TYPE Type
918 ASSERT(Type
< MappingTypeMax
);
922 String
= AllocateZeroPool(PcdGet8(PcdShellMapNameLength
) * sizeof(String
[0]));
925 PcdGet8(PcdShellMapNameLength
) * sizeof(String
[0]),
926 Type
== MappingTypeFileSystem
?L
"FS%d:":L
"BLK%d:",
927 Type
== MappingTypeFileSystem
?mFsMaxCount
++:mBlkMaxCount
++);
933 Function to add a map node to the list of map items and update the "path" environment variable (optionally).
935 If Path is TRUE (during initialization only), the path environment variable will also be updated to include
936 default paths on the new map name...
938 Path should be FALSE when this function is called from the protocol SetMap function.
940 @param[in] Name The human readable mapped name.
941 @param[in] DevicePath The Device Path for this map.
942 @param[in] Flags The Flags attribute for this map item.
943 @param[in] Path TRUE to update path, FALSE to skip this step (should only be TRUE during initialization).
945 @retval EFI_SUCCESS The addition was sucessful.
946 @retval EFI_OUT_OF_RESOURCES A memory allocation failed.
947 @retval EFI_INVALID_PARAMETER A parameter was invalid.
951 ShellCommandAddMapItemAndUpdatePath(
952 IN CONST CHAR16
*Name
,
953 IN CONST EFI_DEVICE_PATH_PROTOCOL
*DevicePath
,
954 IN CONST UINT64 Flags
,
955 IN CONST BOOLEAN Path
959 SHELL_MAP_LIST
*MapListNode
;
960 CONST CHAR16
*OriginalPath
;
967 Status
= EFI_SUCCESS
;
969 MapListNode
= AllocateZeroPool(sizeof(SHELL_MAP_LIST
));
970 if (MapListNode
== NULL
) {
971 Status
= EFI_OUT_OF_RESOURCES
;
973 MapListNode
->Flags
= Flags
;
974 MapListNode
->MapName
= AllocateZeroPool(StrSize(Name
));
975 MapListNode
->DevicePath
= DuplicateDevicePath(DevicePath
);
976 if ((MapListNode
->MapName
== NULL
) || (MapListNode
->DevicePath
== NULL
)){
977 Status
= EFI_OUT_OF_RESOURCES
;
979 StrCpy(MapListNode
->MapName
, Name
);
980 InsertTailList(&gShellMapList
.Link
, &MapListNode
->Link
);
983 if (EFI_ERROR(Status
)) {
984 if (MapListNode
!= NULL
) {
985 if (MapListNode
->DevicePath
!= NULL
) {
986 FreePool(MapListNode
->DevicePath
);
988 if (MapListNode
->MapName
!= NULL
) {
989 FreePool(MapListNode
->MapName
);
991 FreePool(MapListNode
);
995 // Since there was no error and Path was TRUE
996 // Now add the correct path for that mapping
998 OriginalPath
= gEfiShellProtocol
->GetEnv(L
"path");
999 ASSERT((NewPath
== NULL
&& NewPathSize
== 0) || (NewPath
!= NULL
));
1000 if (OriginalPath
!= NULL
) {
1001 StrnCatGrow(&NewPath
, &NewPathSize
, OriginalPath
, 0);
1003 StrnCatGrow(&NewPath
, &NewPathSize
, L
".\\", 0);
1005 StrnCatGrow(&NewPath
, &NewPathSize
, L
";", 0);
1006 StrnCatGrow(&NewPath
, &NewPathSize
, Name
, 0);
1007 StrnCatGrow(&NewPath
, &NewPathSize
, L
"\\efi\\tools\\;", 0);
1008 StrnCatGrow(&NewPath
, &NewPathSize
, Name
, 0);
1009 StrnCatGrow(&NewPath
, &NewPathSize
, L
"\\efi\\boot\\;", 0);
1010 StrnCatGrow(&NewPath
, &NewPathSize
, Name
, 0);
1011 StrnCatGrow(&NewPath
, &NewPathSize
, L
"\\", 0);
1013 Status
= gEfiShellProtocol
->SetEnv(L
"path", NewPath
, TRUE
);
1014 ASSERT_EFI_ERROR(Status
);
1021 Creates the default map names for each device path in the system with
1022 a protocol depending on the Type.
1024 Creates the consistent map names for each device path in the system with
1025 a protocol depending on the Type.
1027 Note: This will reset all mappings in the system("map -r").
1029 Also sets up the default path environment variable if Type is FileSystem.
1031 @retval EFI_SUCCESS All map names were created sucessfully.
1032 @retval EFI_NOT_FOUND No protocols were found in the system.
1033 @return Error returned from gBS->LocateHandle().
1039 ShellCommandCreateInitialMappingsAndPaths(
1044 EFI_HANDLE
*HandleList
;
1046 EFI_DEVICE_PATH_PROTOCOL
**DevicePathList
;
1047 CHAR16
*NewDefaultName
;
1048 CHAR16
*NewConsistName
;
1049 EFI_DEVICE_PATH_PROTOCOL
**ConsistMappingTable
;
1050 SHELL_MAP_LIST
*MapListNode
;
1055 // Reset the static members back to zero
1060 gEfiShellProtocol
->SetEnv(L
"path", L
"", TRUE
);
1063 // First empty out the existing list.
1065 if (!IsListEmpty(&gShellMapList
.Link
)) {
1066 for ( MapListNode
= (SHELL_MAP_LIST
*)GetFirstNode(&gShellMapList
.Link
)
1067 ; !IsListEmpty(&gShellMapList
.Link
)
1068 ; MapListNode
= (SHELL_MAP_LIST
*)GetFirstNode(&gShellMapList
.Link
)
1070 RemoveEntryList(&MapListNode
->Link
);
1071 FreePool(MapListNode
);
1076 // Find each handle with Simple File System
1078 HandleList
= GetHandleListByProtocol(&gEfiSimpleFileSystemProtocolGuid
);
1079 if (HandleList
!= NULL
) {
1081 // Do a count of the handles
1083 for (Count
= 0 ; HandleList
[Count
] != NULL
; Count
++);
1086 // Get all Device Paths
1088 DevicePathList
= AllocateZeroPool(sizeof(EFI_DEVICE_PATH_PROTOCOL
*) * Count
);
1089 ASSERT(DevicePathList
!= NULL
);
1091 for (Count
= 0 ; HandleList
[Count
] != NULL
; Count
++) {
1092 DevicePathList
[Count
] = DevicePathFromHandle(HandleList
[Count
]);
1096 // Sort all DevicePaths
1098 PerformQuickSort(DevicePathList
, Count
, sizeof(EFI_DEVICE_PATH_PROTOCOL
*), DevicePathCompare
);
1100 ShellCommandConsistMappingInitialize(&ConsistMappingTable
);
1102 // Assign new Mappings to all...
1104 for (Count
= 0 ; HandleList
[Count
] != NULL
; Count
++) {
1106 // Get default name first
1108 NewDefaultName
= ShellCommandCreateNewMappingName(MappingTypeFileSystem
);
1109 ASSERT(NewDefaultName
!= NULL
);
1110 Status
= ShellCommandAddMapItemAndUpdatePath(NewDefaultName
, DevicePathList
[Count
], 0, TRUE
);
1111 ASSERT_EFI_ERROR(Status
);
1112 FreePool(NewDefaultName
);
1115 // Now do consistent name
1117 NewConsistName
= ShellCommandConsistMappingGenMappingName(DevicePathList
[Count
], ConsistMappingTable
);
1118 if (NewConsistName
!= NULL
) {
1119 Status
= ShellCommandAddMapItemAndUpdatePath(NewConsistName
, DevicePathList
[Count
], 0, FALSE
);
1120 ASSERT_EFI_ERROR(Status
);
1121 FreePool(NewConsistName
);
1125 ShellCommandConsistMappingUnInitialize(ConsistMappingTable
);
1127 SHELL_FREE_NON_NULL(HandleList
);
1128 SHELL_FREE_NON_NULL(DevicePathList
);
1136 // Find each handle with Block Io
1138 HandleList
= GetHandleListByProtocol(&gEfiBlockIoProtocolGuid
);
1139 if (HandleList
!= NULL
) {
1140 for (Count
= 0 ; HandleList
[Count
] != NULL
; Count
++);
1143 // Get all Device Paths
1145 DevicePathList
= AllocateZeroPool(sizeof(EFI_DEVICE_PATH_PROTOCOL
*) * Count
);
1146 ASSERT(DevicePathList
!= NULL
);
1148 for (Count
= 0 ; HandleList
[Count
] != NULL
; Count
++) {
1149 DevicePathList
[Count
] = DevicePathFromHandle(HandleList
[Count
]);
1153 // Sort all DevicePaths
1155 PerformQuickSort(DevicePathList
, Count
, sizeof(EFI_DEVICE_PATH_PROTOCOL
*), DevicePathCompare
);
1158 // Assign new Mappings to all...
1160 for (Count
= 0 ; HandleList
[Count
] != NULL
; Count
++) {
1162 // Get default name first
1164 NewDefaultName
= ShellCommandCreateNewMappingName(MappingTypeBlockIo
);
1165 ASSERT(NewDefaultName
!= NULL
);
1166 Status
= ShellCommandAddMapItemAndUpdatePath(NewDefaultName
, DevicePathList
[Count
], 0, FALSE
);
1167 ASSERT_EFI_ERROR(Status
);
1168 FreePool(NewDefaultName
);
1171 SHELL_FREE_NON_NULL(HandleList
);
1172 SHELL_FREE_NON_NULL(DevicePathList
);
1173 } else if (Count
== (UINTN
)-1) {
1174 return (EFI_NOT_FOUND
);
1177 return (EFI_SUCCESS
);
1181 Converts a SHELL_FILE_HANDLE to an EFI_FILE_PROTOCOL*.
1183 @param[in] Handle The SHELL_FILE_HANDLE to convert.
1185 @return a EFI_FILE_PROTOCOL* representing the same file.
1189 ConvertShellHandleToEfiFileProtocol(
1190 IN CONST SHELL_FILE_HANDLE Handle
1193 return ((EFI_FILE_PROTOCOL
*)(Handle
));
1197 Converts a EFI_FILE_PROTOCOL* to an SHELL_FILE_HANDLE.
1199 @param[in] Handle The pointer to EFI_FILE_PROTOCOL to convert.
1200 @param[in] Path The path to the file for verification.
1202 @return A SHELL_FILE_HANDLE representing the same file.
1203 @retval NULL There was not enough memory.
1207 ConvertEfiFileProtocolToShellHandle(
1208 IN CONST EFI_FILE_PROTOCOL
*Handle
,
1209 IN CONST CHAR16
*Path
1212 SHELL_COMMAND_FILE_HANDLE
*Buffer
;
1213 BUFFER_LIST
*NewNode
;
1216 Buffer
= AllocateZeroPool(sizeof(SHELL_COMMAND_FILE_HANDLE
));
1217 if (Buffer
== NULL
) {
1220 NewNode
= AllocateZeroPool(sizeof(BUFFER_LIST
));
1221 if (NewNode
== NULL
) {
1224 Buffer
->FileHandle
= (EFI_FILE_PROTOCOL
*)Handle
;
1225 Buffer
->Path
= StrnCatGrow(&Buffer
->Path
, NULL
, Path
, 0);
1226 if (Buffer
->Path
== NULL
) {
1229 NewNode
->Buffer
= Buffer
;
1231 InsertHeadList(&mFileHandleList
.Link
, &NewNode
->Link
);
1233 return ((SHELL_FILE_HANDLE
)(Handle
));
1237 Find the path that was logged with the specified SHELL_FILE_HANDLE.
1239 @param[in] Handle The SHELL_FILE_HANDLE to query on.
1241 @return A pointer to the path for the file.
1245 ShellFileHandleGetPath(
1246 IN CONST SHELL_FILE_HANDLE Handle
1251 for (Node
= (BUFFER_LIST
*)GetFirstNode(&mFileHandleList
.Link
)
1252 ; !IsNull(&mFileHandleList
.Link
, &Node
->Link
)
1253 ; Node
= (BUFFER_LIST
*)GetNextNode(&mFileHandleList
.Link
, &Node
->Link
)
1255 if ((Node
->Buffer
) && (((SHELL_COMMAND_FILE_HANDLE
*)Node
->Buffer
)->FileHandle
== Handle
)){
1256 return (((SHELL_COMMAND_FILE_HANDLE
*)Node
->Buffer
)->Path
);
1263 Remove a SHELL_FILE_HANDLE from the list of SHELL_FILE_HANDLES.
1265 @param[in] Handle The SHELL_FILE_HANDLE to remove.
1267 @retval TRUE The item was removed.
1268 @retval FALSE The item was not found.
1272 ShellFileHandleRemove(
1273 IN CONST SHELL_FILE_HANDLE Handle
1278 for (Node
= (BUFFER_LIST
*)GetFirstNode(&mFileHandleList
.Link
)
1279 ; !IsNull(&mFileHandleList
.Link
, &Node
->Link
)
1280 ; Node
= (BUFFER_LIST
*)GetNextNode(&mFileHandleList
.Link
, &Node
->Link
)
1282 if ((Node
->Buffer
) && (((SHELL_COMMAND_FILE_HANDLE
*)Node
->Buffer
)->FileHandle
== Handle
)){
1283 RemoveEntryList(&Node
->Link
);
1284 SHELL_FREE_NON_NULL(((SHELL_COMMAND_FILE_HANDLE
*)Node
->Buffer
)->Path
);
1285 SHELL_FREE_NON_NULL(Node
->Buffer
);
1286 SHELL_FREE_NON_NULL(Node
);
1294 Function to determine if a SHELL_FILE_HANDLE is at the end of the file.
1296 This will NOT work on directories.
1298 If Handle is NULL, then ASSERT.
1300 @param[in] Handle the file handle
1302 @retval TRUE the position is at the end of the file
1303 @retval FALSE the position is not at the end of the file
1308 IN SHELL_FILE_HANDLE Handle
1311 EFI_FILE_INFO
*Info
;
1316 // ASSERT if Handle is NULL
1318 ASSERT(Handle
!= NULL
);
1320 gEfiShellProtocol
->GetFilePosition(Handle
, &Pos
);
1321 Info
= gEfiShellProtocol
->GetFileInfo (Handle
);
1322 ASSERT(Info
!= NULL
);
1323 gEfiShellProtocol
->SetFilePosition(Handle
, Pos
);
1329 if (Pos
== Info
->FileSize
) {
1341 Frees any BUFFER_LIST defined type.
1343 @param[in] List The BUFFER_LIST object to free.
1348 IN BUFFER_LIST
*List
1351 BUFFER_LIST
*BufferListEntry
;
1357 // enumerate through the buffer list and free all memory
1359 for ( BufferListEntry
= ( BUFFER_LIST
*)GetFirstNode(&List
->Link
)
1360 ; !IsListEmpty (&List
->Link
)
1361 ; BufferListEntry
= (BUFFER_LIST
*)GetFirstNode(&List
->Link
)
1363 RemoveEntryList(&BufferListEntry
->Link
);
1364 ASSERT(BufferListEntry
->Buffer
!= NULL
);
1365 if (BufferListEntry
->Buffer
!= NULL
) {
1366 FreePool(BufferListEntry
->Buffer
);
1368 FreePool(BufferListEntry
);